(file) Return to Socket.h CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Executor

  1 kumpf 1.2 /*
  2           //%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 kumpf 1.2 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 23           // 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           */
 33           
 34           #ifndef _Executor_Socket_h
 35           #define _Executor_Socket_h
 36           
 37           #include <stdlib.h>
 38           #include <sys/socket.h>
 39           #include <string.h>
 40 kumpf 1.4 #include <errno.h>
 41           #include <unistd.h>
 42           #include <Executor/Defines.h>
 43           
 44           #ifdef PEGASUS_ENABLE_PRIVILEGE_SEPARATION
 45 kumpf 1.2 
 46           EXECUTOR_LINKAGE
 47           int SetNonBlocking(int sock);
 48           
 49           EXECUTOR_LINKAGE
 50           int SetBlocking(int sock);
 51           
 52           EXECUTOR_LINKAGE
 53 kumpf 1.4.4.1 int WaitForReadEnable(int sock, long timeoutMsec);
 54               
 55               EXECUTOR_LINKAGE
 56 kumpf 1.2     ssize_t RecvNonBlock(int sock, void* buffer, size_t size);
 57               
 58               EXECUTOR_LINKAGE
 59               ssize_t SendNonBlock(int sock, const void* buffer, size_t size);
 60               
 61               EXECUTOR_LINKAGE
 62               int CloseOnExec(int fd);
 63               
 64               EXECUTOR_LINKAGE
 65               ssize_t SendDescriptorArray(int sock, int descriptors[], size_t count);
 66               
 67               EXECUTOR_LINKAGE
 68               int CreateSocketPair(int pair[2]);
 69               
 70               static int RecvDescriptorArray(int sock, int descriptors[], size_t count)
 71               {
 72                   struct iovec iov[1];
 73                   char dummy;
 74                   struct msghdr mh;
 75                   ssize_t n;
 76               #if defined(HAVE_MSG_CONTROL)
 77 kumpf 1.2         size_t size;
 78                   char* data;
 79 kumpf 1.3         struct cmsghdr* cmh;
 80 kumpf 1.2     
 81                   /*
 82                    * This control data begins with a cmsghdr struct followed by the data
 83                    * (a descriptor in this case). The union ensures that the data is
 84                    * aligned suitably for the leading cmsghdr struct. The descriptor
 85                    * itself is properly aligned since the cmsghdr ends on a boundary
 86                    * that is suitably aligned for any type (including int).
 87                    *
 88                    *     ControlData = [ cmsghdr | int ]
 89                    */
 90               
 91                   size = CMSG_SPACE(sizeof(int) * count);
 92                   data = (char*)malloc(size);
 93               
 94                   /* Define a msghdr that refers to the control data, which is filled in
 95                    * by calling recvmsg() below.
 96                    */
 97               
 98                   memset(&mh, 0, sizeof(mh));
 99                   mh.msg_control = data;
100                   mh.msg_controllen = size;
101 kumpf 1.2     
102               #else /* !defined(HAVE_MSG_CONTROL) */
103               
104                   memset(&mh, 0, sizeof(mh));
105                   mh.msg_accrights = (caddr_t)descriptors;
106                   mh.msg_accrightslen = sizeof(int) * count;
107               
108               #endif /* defined(HAVE_MSG_CONTROL) */
109               
110                   /*
111                    * The other process sends a single-byte message. This byte is not
112                    * used since we only need the control data (the descriptor) but we
113                    * must request at least one byte from recvmsg().
114                    */
115               
116                   memset(iov, 0, sizeof(iov));
117                   iov[0].iov_base = &dummy;
118                   iov[0].iov_len = 1;
119                   mh.msg_iov = iov;
120                   mh.msg_iovlen = 1;
121               
122 kumpf 1.2         /* Receive the message from the other process. */
123               
124                   n = recvmsg(sock, &mh, 0);
125               
126                   if (n <= 0)
127 kumpf 1.3         {
128               #if defined(HAVE_MSG_CONTROL)
129                       free(data);
130               #endif
131 kumpf 1.2             return -1;
132 kumpf 1.3         }
133 kumpf 1.2     
134                   /* Get a pointer to control message. Return if the header is null or
135                    * does not contain what we expect.
136                    */
137               
138               #if defined(HAVE_MSG_CONTROL)
139               
140                   cmh = CMSG_FIRSTHDR(&mh);
141               
142                   if (!cmh ||
143                       cmh->cmsg_len != CMSG_LEN(sizeof(int) * count) ||
144                       cmh->cmsg_level != SOL_SOCKET ||
145                       cmh->cmsg_type != SCM_RIGHTS)
146                   {
147 kumpf 1.3             free(data);
148 kumpf 1.2             return -1;
149                   }
150               
151                   /* Copy the data: */
152               
153                   memcpy(descriptors, CMSG_DATA(cmh), sizeof(int) * count);
154               
155 kumpf 1.3         free(data);
156               
157 kumpf 1.2     #else /* !defined(HAVE_MSG_CONTROL) */
158               
159                   if (mh.msg_accrightslen != sizeof(int) * count)
160                       return -1;
161               
162                   memcpy(descriptors, mh.msg_accrights, sizeof(int) * count);
163               
164               #endif /* defined(HAVE_MSG_CONTROL) */
165               
166                   return 0;
167               }
168               
169 kumpf 1.4     #endif
170               
171               /*
172                   These functions are used by the PAM cimservera implementation regardless
173                   of whether privilege separation is enabled.
174               */
175               
176               static ssize_t RecvBlock(int sock, void* buffer, size_t size)
177               {
178                   size_t r = size;
179                   char* p = (char*)buffer;
180               
181                   if (size == 0)
182                       return -1;
183               
184                   while (r)
185                   {
186                       ssize_t n;
187               
188                       EXECUTOR_RESTART(read(sock, p, r), n);
189               
190 kumpf 1.4             if (n == -1)
191                           return -1;
192                       else if (n == 0)
193                           return size - r;
194               
195                       r -= n;
196                       p += n;
197                   }
198               
199                   return size - r;
200               }
201               
202               static ssize_t SendBlock(int sock, void* buffer, size_t size)
203               {
204                   size_t r = size;
205                   char* p = (char*)buffer;
206               
207                   while (r)
208                   {
209                       ssize_t n;
210                       EXECUTOR_RESTART(write(sock, p, r), n);
211 kumpf 1.4     
212                       if (n == -1)
213                           return -1;
214                       else if (n == 0)
215                           return size - r;
216               
217                       r -= n;
218                       p += n;
219                   }
220               
221                   return size - r;
222               }
223               
224 kumpf 1.2     #endif /* _Executor_Socket_h */

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2