blob: 939d06c8f2a0228ec20562202cb9d37d6ac336c6 (
plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
|
/* ISC license. */
#include <skalibs/nonposix.h>
#include <errno.h>
#include <string.h>
#include <sys/uio.h>
#include <sys/socket.h>
#include <skalibs/ancil.h>
#include <skalibs/posixishard.h>
union aligner_u
{
struct cmsghdr cmsghdr ;
int i ;
} ;
int ancil_send_fd (int sock, int fd, char ch)
{
ssize_t r ;
struct iovec v = { .iov_base = &ch, .iov_len = 1 } ;
union aligner_u ancilbuf[1 + (CMSG_SPACE(sizeof(int)) - 1) / sizeof(union aligner_u)] ;
struct msghdr hdr =
{
.msg_name = 0,
.msg_namelen = 0,
.msg_iov = &v,
.msg_iovlen = 1,
.msg_control = ancilbuf,
.msg_controllen = CMSG_SPACE(sizeof(int))
} ;
struct cmsghdr *c = CMSG_FIRSTHDR(&hdr) ;
memset(hdr.msg_control, 0, hdr.msg_controllen) ;
c->cmsg_level = SOL_SOCKET ;
c->cmsg_type = SCM_RIGHTS ;
c->cmsg_len = CMSG_LEN(sizeof(int)) ;
*(int *)CMSG_DATA(c) = fd ;
do r = sendmsg(sock, &hdr, MSG_NOSIGNAL) ;
while (r < 0 && errno == EINTR) ;
if (r <= 0) return 0 ;
return 1 ;
}
|