1 mike 1.1.2.2 /*
|
2 mike 1.1.2.1 //%2006////////////////////////////////////////////////////////////////////////
3 //
4 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
5 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
6 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
7 // IBM Corp.; EMC Corporation, The Open Group.
8 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
9 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
10 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
11 // EMC Corporation; VERITAS Software Corporation; The Open Group.
12 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
13 // EMC Corporation; Symantec Corporation; The Open Group.
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining a copy
16 // of this software and associated documentation files (the "Software"), to
17 // deal in the Software without restriction, including without limitation the
18 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
19 // sell copies of the Software, and to permit persons to whom the Software is
20 // furnished to do so, subject to the following conditions:
21 //
22 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
23 mike 1.1.2.1 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
24 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
25 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
26 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
27 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31 //%/////////////////////////////////////////////////////////////////////////////
|
32 mike 1.1.2.2 */
|
33 mike 1.1.2.1
34 #ifndef _Executor_Socket_h
35 #define _Executor_Socket_h
36
|
37 mike 1.1.2.5 #if !defined(_XOPEN_SOURCE_EXTENDED)
38 # define _XOPEN_SOURCE_EXTENDED
39 #endif
40
|
41 mike 1.1.2.1 #include <stdlib.h>
|
42 mike 1.1.2.3 #include "Defines.h"
43 #include <sys/socket.h>
44 #include <string.h>
|
45 mike 1.1.2.7 #include "Defines.h"
|
46 mike 1.1.2.1
|
47 mike 1.1.2.3 EXECUTOR_LINKAGE
|
48 mike 1.1.2.1 int SetNonBlocking(int sock);
49
|
50 mike 1.1.2.3 EXECUTOR_LINKAGE
|
51 mike 1.1.2.6 int SetBlocking(int sock);
52
53 EXECUTOR_LINKAGE
|
54 mike 1.1.2.1 ssize_t RecvNonBlock(int sock, void* buffer, size_t size);
55
|
56 mike 1.1.2.3 EXECUTOR_LINKAGE
|
57 mike 1.1.2.1 ssize_t SendNonBlock(int sock, const void* buffer, size_t size);
58
|
59 mike 1.1.2.3 EXECUTOR_LINKAGE
|
60 mike 1.1.2.1 int CloseOnExec(int fd);
61
|
62 mike 1.1.2.3 EXECUTOR_LINKAGE
|
63 mike 1.1.2.1 ssize_t SendDescriptorArray(int sock, int descriptors[], size_t count);
64
|
65 mike 1.1.2.3 EXECUTOR_LINKAGE
|
66 mike 1.1.2.1 int CreateSocketPair(int pair[2]);
67
|
68 mike 1.1.2.3 static int RecvDescriptorArray(int sock, int descriptors[], size_t count)
69 {
70 struct iovec iov[1];
71 char dummy;
|
72 mike 1.1.2.7 struct msghdr mh;
|
73 mike 1.1.2.3 ssize_t n;
|
74 mike 1.1.2.7 #if defined(HAVE_MSG_CONTROL)
75 size_t size;
76 char* data;
|
77 mike 1.1.2.3 struct cmsghdr* cmh = CMSG_FIRSTHDR(&mh);
78
79 /*
80 * This control data begins with a cmsghdr struct followed by the data
81 * (a descriptor in this case). The union ensures that the data is
82 * aligned suitably for the leading cmsghdr struct. The descriptor
83 * itself is properly aligned since the cmsghdr ends on a boundary
84 * that is suitably aligned for any type (including int).
85 *
86 * ControlData = [ cmsghdr | int ]
87 */
88
89 size = CMSG_SPACE(sizeof(int) * count);
90 data = (char*)malloc(size);
91
92 /* Define a msghdr that refers to the control data, which is filled in
93 * by calling recvmsg() below.
94 */
95
96 memset(&mh, 0, sizeof(mh));
97 mh.msg_control = data;
98 mike 1.1.2.3 mh.msg_controllen = size;
99
|
100 mike 1.1.2.7 #else /* !defined(HAVE_MSG_CONTROL) */
101
102 mh.msg_accrights = (caddr_t)descriptors;
103 mh.msg_accrightslength = sizeof(int) * count;
104
105 #endif /* defined(HAVE_MSG_CONTROL) */
106
|
107 mike 1.1.2.3 /*
108 * The other process sends a single-byte message. This byte is not
109 * used since we only need the control data (the descriptor) but we
110 * must request at least one byte from recvmsg().
111 */
112
113 memset(iov, 0, sizeof(iov));
114
115 iov[0].iov_base = &dummy;
116 iov[0].iov_len = 1;
117 mh.msg_iov = iov;
118 mh.msg_iovlen = 1;
119
120 /* Receive the message from the other process. */
121
122 n = recvmsg(sock, &mh, 0);
123
124 if (n <= 0)
125 return -1;
126
127 /* Get a pointer to control message. Return if the header is null or
128 mike 1.1.2.3 * does not contain what we expect.
129 */
130
|
131 mike 1.1.2.7 #if defined(HAVE_MSG_CONTROL)
132
|
133 mike 1.1.2.3 cmh = CMSG_FIRSTHDR(&mh);
134
135 if (!cmh ||
136 cmh->cmsg_len != CMSG_LEN(sizeof(int) * count) ||
137 cmh->cmsg_level != SOL_SOCKET ||
138 cmh->cmsg_type != SCM_RIGHTS)
139 {
140 return -1;
141 }
142
143 /* Copy the data: */
144
145 memcpy(descriptors, CMSG_DATA(cmh), sizeof(int) * count);
146
|
147 mike 1.1.2.7 #else /* !defined(HAVE_MSG_CONTROL) */
148
149 if (mh.msg_accrightslength != sizeof(int) * count)
150 return -1;
151
152 memcpy(descriptors, mh.msg_accrights, sizeof(int) * count);
153
154 #endif /* defined(HAVE_MSG_CONTROL) */
155
|
156 mike 1.1.2.3 return 0;
157 }
158
|
159 mike 1.1.2.1 #endif /* _Executor_Socket_h */
|