CUBRID Engine  latest
broker_send_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_send_fd.c -
22  */
23 
24 #ident "$Id$"
25 
26 #include <sys/types.h>
27 #include <netinet/in.h>
28 #include <sys/socket.h>
29 #include <sys/uio.h>
30 #include <stddef.h>
31 #include <errno.h>
32 #include <stdlib.h>
33 #include <memory.h>
34 #include <assert.h>
35 
36 #ifdef SOLARIS
37 #include <sys/sockio.h>
38 #endif
39 
40 #include "porting.h"
41 #include "cas_protocol.h"
42 #include "broker_send_fd.h"
43 #include "broker_send_recv_msg.h"
44 
45 #if defined(LINUX) || defined(ALPHA_LINUX) || defined(UNIXWARE7) || defined(AIX)
46 #define CONTROLLEN (sizeof(struct cmsghdr) + sizeof(int))
47 #endif
48 
49 #define SYSV
50 
51 int
52 send_fd (int server_fd, int client_fd, int rid, char *driver_info)
53 {
54  struct iovec iov[1];
55  struct msghdr msg;
56  int num_bytes;
57 #if defined(LINUX) || defined(ALPHA_LINUX) || defined(UNIXWARE7) || defined(AIX)
58  static struct cmsghdr *cmptr = NULL;
59 #endif
60  struct sendmsg_s send_msg;
61 
62  assert (driver_info != NULL);
63 
64  /* set send message */
65  send_msg.rid = rid;
66  memcpy (send_msg.driver_info, driver_info, SRV_CON_CLIENT_INFO_SIZE);
67 
68  /* Pass the fd to the server */
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_namelen = 0;
74  msg.msg_name = (caddr_t) 0;
75 #if !defined(LINUX) && !defined(ALPHA_LINUX) && !defined(UNIXWARE7) && !defined(AIX)
76  msg.msg_accrights = (caddr_t) & client_fd;
77  msg.msg_accrightslen = sizeof (client_fd);
78 #else
79  if (cmptr == NULL && (cmptr = (cmsghdr *) malloc (CONTROLLEN)) == NULL)
80  exit (99);
81  cmptr->cmsg_level = SOL_SOCKET;
82  cmptr->cmsg_type = SCM_RIGHTS;
83  cmptr->cmsg_len = CONTROLLEN;
84  msg.msg_control = (void *) cmptr;
85  msg.msg_controllen = CONTROLLEN;
86  *(int *) CMSG_DATA (cmptr) = client_fd;
87 #endif
88 
89  num_bytes = sendmsg (server_fd, &msg, 0);
90 
91  if (num_bytes < (signed int) sizeof (int))
92  {
93  return (-1);
94  }
95  return (num_bytes);
96 }
#define SRV_CON_CLIENT_INFO_SIZE
Definition: cas_protocol.h:34
#define assert(x)
int send_fd(int server_fd, int client_fd, int rid, char *driver_info)
#define NULL
Definition: freelistheap.h:34
#define CONTROLLEN
Definition: tcp.c:87
char driver_info[SRV_CON_CLIENT_INFO_SIZE]