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