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 //%/////////////////////////////////////////////////////////////////////////////
|
61 mike 1.1.2.3 static int RecvDescriptorArray(int sock, int descriptors[], size_t count)
62 {
63 struct msghdr mh;
64 size_t size;
65 char* data;
66 struct iovec iov[1];
67 char dummy;
68 ssize_t n;
69 struct cmsghdr* cmh = CMSG_FIRSTHDR(&mh);
70
71 /*
72 * This control data begins with a cmsghdr struct followed by the data
73 * (a descriptor in this case). The union ensures that the data is
74 * aligned suitably for the leading cmsghdr struct. The descriptor
75 * itself is properly aligned since the cmsghdr ends on a boundary
76 * that is suitably aligned for any type (including int).
77 *
78 * ControlData = [ cmsghdr | int ]
79 */
80
81 size = CMSG_SPACE(sizeof(int) * count);
82 mike 1.1.2.3 data = (char*)malloc(size);
83
84 /* Define a msghdr that refers to the control data, which is filled in
85 * by calling recvmsg() below.
86 */
87
88 memset(&mh, 0, sizeof(mh));
89 mh.msg_control = data;
90 mh.msg_controllen = size;
91
92 /*
93 * The other process sends a single-byte message. This byte is not
94 * used since we only need the control data (the descriptor) but we
95 * must request at least one byte from recvmsg().
96 */
97
98 memset(iov, 0, sizeof(iov));
99
100 iov[0].iov_base = &dummy;
101 iov[0].iov_len = 1;
102 mh.msg_iov = iov;
103 mike 1.1.2.3 mh.msg_iovlen = 1;
104
105 /* Receive the message from the other process. */
106
107 n = recvmsg(sock, &mh, 0);
108
109 if (n <= 0)
110 return -1;
111
112 /* Get a pointer to control message. Return if the header is null or
113 * does not contain what we expect.
114 */
115
116 cmh = CMSG_FIRSTHDR(&mh);
117
118 if (!cmh ||
119 cmh->cmsg_len != CMSG_LEN(sizeof(int) * count) ||
120 cmh->cmsg_level != SOL_SOCKET ||
121 cmh->cmsg_type != SCM_RIGHTS)
122 {
123 return -1;
124 mike 1.1.2.3 }
125
126 /* Copy the data: */
127
128 memcpy(descriptors, CMSG_DATA(cmh), sizeof(int) * count);
129
130 return 0;
131 }
132
|