CUBRID Engine  latest
broker_recv_fd.c
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 
20 /*
21  * broker_recv_fd.c -
22  */
23 
24 #ident "$Id$"
25 
26 #include <stdio.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/uio.h>
32 #include <stddef.h>
33 #include <errno.h>
34 #include <stdlib.h>
35 
36 #include "porting.h"
37 #include "cas_protocol.h"
38 #include "broker_recv_fd.h"
39 #include "broker_send_recv_msg.h"
40 
41 #if defined(SOLARIS) || defined(UNIXWARE7)
42 #include <sys/sockio.h>
43 #elif defined(AIX) || defined(OSF1) || defined(LINUX) || defined(ALPHA_LINUX)
44 #include <sys/ioctl.h>
45 #endif
46 
47 #if defined(LINUX) || defined(ALPHA_LINUX) || defined(UNIXWARE7) || defined(AIX)
48 #define CONTROLLEN (sizeof(struct cmsghdr) + sizeof(int))
49 #endif
50 
51 #define SYSV
52 
53 /*
54  client_version is only used in PROXY(SHARD).
55  In CAS, client_version is set in shared memory.
56  */
57 int
58 recv_fd (int fd, int *rid, char *driver_info)
59 {
60  int new_fd = 0, rc;
61  struct iovec iov[1];
62  struct msghdr msg;
63  int pid;
64 #if defined(LINUX) || defined(ALPHA_LINUX) || defined(UNIXWARE7) || defined(AIX)
65  static struct cmsghdr *cmptr = NULL;
66 #endif
67  struct sendmsg_s send_msg;
68 
69  iov[0].iov_base = (char *) &send_msg;
70  iov[0].iov_len = sizeof (struct sendmsg_s);
71  msg.msg_iov = iov;
72  msg.msg_iovlen = 1;
73  msg.msg_name = (caddr_t) NULL;
74  msg.msg_namelen = 0;
75 #if !defined(LINUX) && !defined(ALPHA_LINUX) && !defined(UNIXWARE7) && !defined(AIX)
76  msg.msg_accrights = (caddr_t) & new_fd; /* address of descriptor */
77  msg.msg_accrightslen = sizeof (new_fd); /* receive 1 descriptor */
78 #else
79  if (cmptr == NULL && (cmptr = (cmsghdr *) malloc (CONTROLLEN)) == NULL)
80  exit (99);
81  msg.msg_control = (void *) cmptr;
82  msg.msg_controllen = CONTROLLEN;
83 #endif
84  rc = recvmsg (fd, &msg, 0);
85 
86  if (rc < (signed int) (sizeof (int)))
87  {
88 #ifdef _DEBUG
89  printf ("recvmsg failed. errno = %d. str=%s\n", errno, strerror (errno));
90 #endif
91  return (-1);
92  }
93 
94  *rid = send_msg.rid;
95  if (driver_info)
96  {
97  memcpy (driver_info, send_msg.driver_info, SRV_CON_CLIENT_INFO_SIZE);
98  }
99 
100  pid = getpid ();
101 #if defined(LINUX) || defined(ALPHA_LINUX) || defined(UNIXWARE7) || defined(AIX)
102  new_fd = *(int *) CMSG_DATA (cmptr);
103 #endif
104 
105 #ifdef SYSV
106  ioctl (new_fd, SIOCSPGRP, (caddr_t) & pid);
107 #elif !defined(VMS)
108  fcntl (new_fd, F_SETOWN, pid);
109 #endif
110 
111  return (new_fd);
112 }
#define SRV_CON_CLIENT_INFO_SIZE
Definition: cas_protocol.h:34
#define NULL
Definition: freelistheap.h:34
pid_t pid
Definition: dynamic_load.c:955
#define CONTROLLEN
Definition: tcp.c:87
static int rc
Definition: serial.c:50
char driver_info[SRV_CON_CLIENT_INFO_SIZE]
int recv_fd(int fd, int *rid, char *driver_info)