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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
/* ISC license. */
#undef _POSIX_C_SOURCE
#undef _XOPEN_SOURCE
#ifndef _XPG4_2
# define _XPG4_2
#endif
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/uio.h>
#if defined(__FreeBSD__)
# include <sys/param.h>
#endif
typedef struct ancilbuf_s ancilbuf_t, *ancilbuf_t_ref ;
struct ancilbuf_s
{
struct cmsghdr h ;
int fd ;
} ;
static int ancil_send_fd (int sock, int fd)
{
struct msghdr msghdr ;
struct iovec nothing_ptr ;
ancilbuf_t buf ;
struct cmsghdr *cmsg ;
char nothing = '!' ;
nothing_ptr.iov_base = ¬hing ;
nothing_ptr.iov_len = 1 ;
msghdr.msg_name = 0 ;
msghdr.msg_namelen = 0 ;
msghdr.msg_iov = ¬hing_ptr ;
msghdr.msg_iovlen = 1 ;
msghdr.msg_flags = 0 ;
msghdr.msg_control = &buf ;
msghdr.msg_controllen = sizeof(ancilbuf_t) ;
cmsg = CMSG_FIRSTHDR(&msghdr) ;
cmsg->cmsg_len = msghdr.msg_controllen ;
cmsg->cmsg_level = SOL_SOCKET ;
cmsg->cmsg_type = SCM_RIGHTS ;
*((int *)CMSG_DATA(cmsg)) = fd ;
return (sendmsg(sock, &msghdr, 0) >= 0) ;
}
static int ancil_recv_fd (int sock)
{
struct msghdr msghdr ;
struct iovec nothing_ptr ;
ancilbuf_t buf ;
struct cmsghdr *cmsg ;
char nothing ;
nothing_ptr.iov_base = ¬hing ;
nothing_ptr.iov_len = 1 ;
msghdr.msg_name = 0 ;
msghdr.msg_namelen = 0 ;
msghdr.msg_iov = ¬hing_ptr ;
msghdr.msg_iovlen = 1 ;
msghdr.msg_flags = 0 ;
msghdr.msg_control = &buf ;
msghdr.msg_controllen = sizeof(ancilbuf_t) ;
cmsg = CMSG_FIRSTHDR(&msghdr) ;
cmsg->cmsg_len = msghdr.msg_controllen ;
cmsg->cmsg_level = SOL_SOCKET ;
cmsg->cmsg_type = SCM_RIGHTS ;
*((int *)CMSG_DATA(cmsg)) = -1 ;
if (recvmsg(sock, &msghdr, 0) < 0) return -1 ;
return *((int *)CMSG_DATA(cmsg)) ;
}
static int client (int sock)
{
int p ;
char c ;
if (read(sock, &c, 1) < 1) return 111 ;
if (c != 'b') return 111 ;
p = ancil_recv_fd(sock) ;
if (p < 0) return 111 ;
if (read(sock, &c, 1) < 1) return 111 ;
if (c != 'a') return 111 ;
if (read(p, &c, 1) < 1) return 111 ;
if (c != 'K') return 111 ;
if (read(p, &c, 1) < 1) return 111 ;
return c ;
}
static void server (int sock)
{
int p[2] ;
char c = 1 ;
if (pipe(p) < 0) return ;
if (write(sock, "b", 1) < 1) return ;
if (!ancil_send_fd(sock, p[0])) return ;
if (write(sock, "a", 1) < 1) return ;
if (write(p[1], "K", 1) < 1) return ;
if ((close(p[0]) < 0) && (errno == EBADF)) c = 0 ;
write(p[1], &c, 1) ;
}
int main (void)
{
int fd[2] ;
if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0) return 111 ;
switch (fork())
{
case -1 : return 111 ;
case 0 : close(fd[0]) ; server(fd[1]) ; return 0 ;
default : close(fd[1]) ; return client(fd[0]) ;
}
}
|