fdpass.c revision 1d3196cbaa339a7b996cf5aebcf72ccffc76acc0
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen fdpass.c - File descriptor passing between processes via UNIX sockets
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen This isn't fully portable, but pretty much all UNIXes nowadays should
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen support this. If you're having runtime problems, check the end of fd_read()
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen and play with the if condition.
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen If this file doesn't compile at all, you should check if this is supported
105addcb709523868418cc3e3baad7ad3453a91eTimo Sirainen in your system at all. It may require some extra #define to enable it.
105addcb709523868418cc3e3baad7ad3453a91eTimo Sirainen If not, you're pretty much out of luck. Cygwin didn't last I checked.
105addcb709523868418cc3e3baad7ad3453a91eTimo Sirainen Copyright (c) 2002-2003 Timo Sirainen
105addcb709523868418cc3e3baad7ad3453a91eTimo Sirainen Permission is hereby granted, free of charge, to any person obtaining
105addcb709523868418cc3e3baad7ad3453a91eTimo Sirainen a copy of this software and associated documentation files (the
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "Software"), to deal in the Software without restriction, including
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen without limitation the rights to use, copy, modify, merge, publish,
0add8c99ca65e56dbf613595fc37c41aafff3f7fTimo Sirainen distribute, sublicense, and/or sell copies of the Software, and to
40ef82c46f6652412b068ebcdac7c3e74840a284Timo Sirainen permit persons to whom the Software is furnished to do so, subject to
105addcb709523868418cc3e3baad7ad3453a91eTimo Sirainen the following conditions:
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen The above copyright notice and this permission notice shall be
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen included in all copies or substantial portions of the Software.
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
56f45b3f3ae20e5c933701f4657dda5ef1916855Timo Sirainen IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen#if defined(irix) || defined (__irix__) || defined(sgi) || defined (__sgi__)
51795bfe9d05d92fe942cb451aec2b9d16d32a11Timo Sirainen# define _XOPEN_SOURCE_EXTENDED /* for Tru64, breaks AIX */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen (((len) + sizeof(size_t) - 1) & ~(sizeof(size_t) - 1))
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen (CMSG_ALIGN(len) + CMSG_ALIGN(sizeof(struct cmsghdr)))
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenssize_t fd_send(int handle, int send_fd, const void *data, size_t size)
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen /* at least one byte is required to be sent with fd passing */
faed8babca9914257f34fb2e603d74016d563b2dTimo Sirainen /* set the control and controllen before CMSG_FIRSTHDR() */
d143077bd518de129b8d446fb58e003903e50867Timo Sirainen /* set the real length we want to use. it's different than
d143077bd518de129b8d446fb58e003903e50867Timo Sirainen sizeof(buf) in 64bit systems. */
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainenssize_t fd_read(int handle, void *data, size_t size, int *fd)
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen /* at least one byte transferred - we should have the fd now.
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen do extra checks to make sure it really is an fd that is being
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen transferred to avoid potential DoS conditions. some systems don't
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen set all these values correctly however:
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen Linux 2.0.x - cmsg_len, cmsg_level, cmsg_type are not set
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen Tru64 - msg_controllen isn't set */
2d49f150b4bce6f2f59a84e268e4777901c3e42cTimo Sirainen msg.msg_controllen < CMSG_SPACE(sizeof(int)) ||
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen cmsg == NULL || cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||