| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#ifndef _XPG4_2 |
| 32 |
# define _XPG4_2 |
| 33 |
#endif |
| 34 |
|
| 35 |
#include <stdlib.h> |
| 36 |
#include <sys/types.h> |
| 37 |
#include <sys/socket.h> |
| 38 |
#include <sys/uio.h> |
| 39 |
#include <assert.h> |
| 40 |
#if defined(__FreeBSD__) |
| 41 |
# include <sys/param.h> |
| 42 |
#endif |
| 43 |
|
| 44 |
#include "ancillary.h" |
| 45 |
|
| 46 |
int |
| 47 |
ancil_recv_fds_with_buffer(int sock, int *fds, unsigned n_fds, void *buffer) |
| 48 |
{ |
| 49 |
struct msghdr msghdr; |
| 50 |
char nothing; |
| 51 |
struct iovec nothing_ptr; |
| 52 |
struct cmsghdr *cmsg; |
| 53 |
int i; |
| 54 |
|
| 55 |
nothing_ptr.iov_base = ¬hing; |
| 56 |
nothing_ptr.iov_len = 1; |
| 57 |
msghdr.msg_name = NULL; |
| 58 |
msghdr.msg_namelen = 0; |
| 59 |
msghdr.msg_iov = ¬hing_ptr; |
| 60 |
msghdr.msg_iovlen = 1; |
| 61 |
msghdr.msg_flags = 0; |
| 62 |
msghdr.msg_control = buffer; |
| 63 |
msghdr.msg_controllen = sizeof(struct cmsghdr) + sizeof(int) * n_fds; |
| 64 |
cmsg = CMSG_FIRSTHDR(&msghdr); |
| 65 |
cmsg->cmsg_len = msghdr.msg_controllen; |
| 66 |
cmsg->cmsg_level = SOL_SOCKET; |
| 67 |
cmsg->cmsg_type = SCM_RIGHTS; |
| 68 |
for(i = 0; i < n_fds; i++) |
| 69 |
((int *)CMSG_DATA(cmsg))[i] = -1; |
| 70 |
|
| 71 |
if(recvmsg(sock, &msghdr, 0) < 0) |
| 72 |
return(-1); |
| 73 |
for(i = 0; i < n_fds; i++) |
| 74 |
fds[i] = ((int *)CMSG_DATA(cmsg))[i]; |
| 75 |
n_fds = (cmsg->cmsg_len - sizeof(struct cmsghdr)) / sizeof(int); |
| 76 |
return(n_fds); |
| 77 |
} |
| 78 |
|
| 79 |
#ifndef SPARE_RECV_FDS |
| 80 |
int |
| 81 |
ancil_recv_fds(int sock, int *fd, unsigned n_fds) |
| 82 |
{ |
| 83 |
ANCIL_FD_BUFFER(ANCIL_MAX_N_FDS) buffer; |
| 84 |
|
| 85 |
assert(n_fds <= ANCIL_MAX_N_FDS); |
| 86 |
return(ancil_recv_fds_with_buffer(sock, fd, n_fds, &buffer)); |
| 87 |
} |
| 88 |
#endif |
| 89 |
|
| 90 |
#ifndef SPARE_RECV_FD |
| 91 |
int |
| 92 |
ancil_recv_fd(int sock, int *fd) |
| 93 |
{ |
| 94 |
ANCIL_FD_BUFFER(1) buffer; |
| 95 |
|
| 96 |
return(ancil_recv_fds_with_buffer(sock, fd, 1, &buffer) == 1 ? 0 : -1); |
| 97 |
} |
| 98 |
#endif |