fdpass.c revision 5bfa6f45b077220fe4d5fd74aad6fb2820aebf79
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/*
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen fdpass.c - FD passing
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen Copyright (c) 2002 Timo Sirainen
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen Permission is hereby granted, free of charge, to any person obtaining
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen a copy of this software and associated documentation files (the
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "Software"), to deal in the Software without restriction, including
5fb3bff645380804c9db2510940c41db6b8fdb01Timo Sirainen without limitation the rights to use, copy, modify, merge, publish,
5fb3bff645380804c9db2510940c41db6b8fdb01Timo Sirainen distribute, sublicense, and/or sell copies of the Software, and to
15b9759df8e4f6fb00c115353827a2aebbebfebcTimo Sirainen permit persons to whom the Software is furnished to do so, subject to
15b9759df8e4f6fb00c115353827a2aebbebfebcTimo Sirainen the following conditions:
15b9759df8e4f6fb00c115353827a2aebbebfebcTimo Sirainen
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen The above copyright notice and this permission notice shall be
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen included in all copies or substantial portions of the Software.
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
d6badc27cd6e8d3398877b6766cb0aaeef3a7800Timo Sirainen SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
d6badc27cd6e8d3398877b6766cb0aaeef3a7800Timo Sirainen*/
d6badc27cd6e8d3398877b6766cb0aaeef3a7800Timo Sirainen
f7539a17ea306191b53b8f5e752e228937df9ec3Timo Sirainen#ifdef __sun__
2dd39e478269d6fb0bb26d12b394aa30ee965e38Timo Sirainen# define _XPG4_2
2dd39e478269d6fb0bb26d12b394aa30ee965e38Timo Sirainen#endif
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "lib.h"
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen#include "network.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "fdpass.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include <sys/un.h>
04ab375449dd97eed50ada88dd0df2abab01cfeeTimo Sirainen#include <sys/uio.h>
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#if defined (__sun__) && !defined(CMSG_SPACE)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen# define CMSG_ALIGN(len) \
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen (((len) + sizeof(size_t) - 1) & ~(sizeof(size_t) - 1))
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen# define CMSG_SPACE(len) \
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen (CMSG_ALIGN(len) + CMSG_ALIGN(sizeof(struct cmsghdr)))
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen# define CMSG_LEN(len) \
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen (CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen#endif
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainenint fd_send(int handle, int send_fd, const void *data, int size)
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen{
57a8c6a95e4bce3eeaba36985adb81c07dd683ffTimo Sirainen struct msghdr msg;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen struct iovec iov;
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen struct cmsghdr *cmsg;
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen int *fdptr;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen char buf[CMSG_SPACE(sizeof(int))];
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen memset(&msg, 0, sizeof (struct msghdr));
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen iov.iov_base = (void *) data;
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen iov.iov_len = size;
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen msg.msg_control = buf;
e8490a52a1bc71bc53034e68f464435684ad810fTimo Sirainen msg.msg_controllen = sizeof(buf);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen msg.msg_iov = &iov;
ccc895c0358108d2304239063e940b7d75f364abTimo Sirainen msg.msg_iovlen = 1;
ccc895c0358108d2304239063e940b7d75f364abTimo Sirainen
8d630c15a8ed6f85553467c3a231a273defca5f6Timo Sirainen cmsg = CMSG_FIRSTHDR(&msg);
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen cmsg->cmsg_level = SOL_SOCKET;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen cmsg->cmsg_type = SCM_RIGHTS;
c5ab90cfad9cc3e33bcb1baeb30ffc82a7b7053aTimo Sirainen cmsg->cmsg_len = CMSG_LEN(sizeof(int));
c5ab90cfad9cc3e33bcb1baeb30ffc82a7b7053aTimo Sirainen fdptr = (int *) CMSG_DATA(cmsg);
c5ab90cfad9cc3e33bcb1baeb30ffc82a7b7053aTimo Sirainen *fdptr = send_fd;
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen return sendmsg(handle, &msg, 0);
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenint fd_read(int handle, void *data, int size, int *fd)
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen{
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen struct msghdr msg;
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen struct iovec iov;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct cmsghdr *cmsg;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen int ret;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen char buf[CMSG_SPACE(sizeof(int))];
88187ee880b4829443e0d55ea7d145d9d5880217Timo Sirainen
88187ee880b4829443e0d55ea7d145d9d5880217Timo Sirainen memset(&msg, 0, sizeof (struct msghdr));
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
d3442384ca53d4b18a493db7dd0b000f470419cfTimo Sirainen msg.msg_control = buf;
d3442384ca53d4b18a493db7dd0b000f470419cfTimo Sirainen msg.msg_controllen = sizeof(buf);
d3442384ca53d4b18a493db7dd0b000f470419cfTimo Sirainen msg.msg_iov = &iov;
d3442384ca53d4b18a493db7dd0b000f470419cfTimo Sirainen msg.msg_iovlen = 1;
d3442384ca53d4b18a493db7dd0b000f470419cfTimo Sirainen cmsg = CMSG_FIRSTHDR(&msg);
d3442384ca53d4b18a493db7dd0b000f470419cfTimo Sirainen cmsg->cmsg_level = SOL_SOCKET;
d3442384ca53d4b18a493db7dd0b000f470419cfTimo Sirainen cmsg->cmsg_type = SCM_RIGHTS;
d3442384ca53d4b18a493db7dd0b000f470419cfTimo Sirainen iov.iov_base = data;
d3442384ca53d4b18a493db7dd0b000f470419cfTimo Sirainen iov.iov_len = size;
d3442384ca53d4b18a493db7dd0b000f470419cfTimo Sirainen
d756ebcfa96bd7cff02097c8f26df9df368b81b1Timo Sirainen ret = recvmsg(handle, &msg, 0);
d756ebcfa96bd7cff02097c8f26df9df368b81b1Timo Sirainen *fd = *(int *) CMSG_DATA(cmsg);
d756ebcfa96bd7cff02097c8f26df9df368b81b1Timo Sirainen return ret;
d756ebcfa96bd7cff02097c8f26df9df368b81b1Timo Sirainen}
d756ebcfa96bd7cff02097c8f26df9df368b81b1Timo Sirainen