(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           #include "Defines.h"
 41           
 42           EXECUTOR_LINKAGE
 43 kumpf 1.2 int SetNonBlocking(int sock);
 44           
 45           EXECUTOR_LINKAGE
 46           int SetBlocking(int sock);
 47           
 48           EXECUTOR_LINKAGE
 49           ssize_t RecvNonBlock(int sock, void* buffer, size_t size);
 50           
 51           EXECUTOR_LINKAGE
 52           ssize_t SendNonBlock(int sock, const void* buffer, size_t size);
 53           
 54           EXECUTOR_LINKAGE
 55           int CloseOnExec(int fd);
 56           
 57           EXECUTOR_LINKAGE
 58           ssize_t SendDescriptorArray(int sock, int descriptors[], size_t count);
 59           
 60           EXECUTOR_LINKAGE
 61           int CreateSocketPair(int pair[2]);
 62           
 63           static int RecvDescriptorArray(int sock, int descriptors[], size_t count)
 64 kumpf 1.2 {
 65               struct iovec iov[1];
 66               char dummy;
 67               struct msghdr mh;
 68               ssize_t n;
 69           #if defined(HAVE_MSG_CONTROL)
 70               size_t size;
 71               char* data;
 72               struct cmsghdr* cmh = CMSG_FIRSTHDR(&mh);
 73           
 74               /*
 75                * This control data begins with a cmsghdr struct followed by the data
 76                * (a descriptor in this case). The union ensures that the data is
 77                * aligned suitably for the leading cmsghdr struct. The descriptor
 78                * itself is properly aligned since the cmsghdr ends on a boundary
 79                * that is suitably aligned for any type (including int).
 80                *
 81                *     ControlData = [ cmsghdr | int ]
 82                */
 83           
 84               size = CMSG_SPACE(sizeof(int) * count);
 85 kumpf 1.2     data = (char*)malloc(size);
 86           
 87               /* Define a msghdr that refers to the control data, which is filled in
 88                * by calling recvmsg() below.
 89                */
 90           
 91               memset(&mh, 0, sizeof(mh));
 92               mh.msg_control = data;
 93               mh.msg_controllen = size;
 94           
 95           #else /* !defined(HAVE_MSG_CONTROL) */
 96           
 97               memset(&mh, 0, sizeof(mh));
 98               mh.msg_accrights = (caddr_t)descriptors;
 99               mh.msg_accrightslen = sizeof(int) * count;
100           
101           #endif /* defined(HAVE_MSG_CONTROL) */
102           
103               /*
104                * The other process sends a single-byte message. This byte is not
105                * used since we only need the control data (the descriptor) but we
106 kumpf 1.2      * must request at least one byte from recvmsg().
107                */
108           
109               memset(iov, 0, sizeof(iov));
110               iov[0].iov_base = &dummy;
111               iov[0].iov_len = 1;
112               mh.msg_iov = iov;
113               mh.msg_iovlen = 1;
114           
115               /* Receive the message from the other process. */
116           
117               n = recvmsg(sock, &mh, 0);
118           
119               if (n <= 0)
120                   return -1;
121           
122               /* Get a pointer to control message. Return if the header is null or
123                * does not contain what we expect.
124                */
125           
126           #if defined(HAVE_MSG_CONTROL)
127 kumpf 1.2 
128               cmh = CMSG_FIRSTHDR(&mh);
129           
130               if (!cmh ||
131                   cmh->cmsg_len != CMSG_LEN(sizeof(int) * count) ||
132                   cmh->cmsg_level != SOL_SOCKET ||
133                   cmh->cmsg_type != SCM_RIGHTS)
134               {
135                   return -1;
136               }
137           
138               /* Copy the data: */
139           
140               memcpy(descriptors, CMSG_DATA(cmh), sizeof(int) * count);
141           
142           #else /* !defined(HAVE_MSG_CONTROL) */
143           
144               if (mh.msg_accrightslen != sizeof(int) * count)
145                   return -1;
146           
147               memcpy(descriptors, mh.msg_accrights, sizeof(int) * count);
148 kumpf 1.2 
149           #endif /* defined(HAVE_MSG_CONTROL) */
150           
151               return 0;
152           }
153           
154           #endif /* _Executor_Socket_h */

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2