9 #include <sys/socket.h>
22 #include <sys/types.h>
39 struct sockaddr_un addr
;
41 fd1
= DO( open(filea
, O_RDWR
| O_CREAT
| O_TRUNC
, 0750) );
42 fd2
= DO( open(fileb
, O_RDWR
| O_CREAT
| O_TRUNC
, 0750) );
43 s
= DO( socket(PF_UNIX
, SOCK_STREAM
, 0) );
45 memset(&addr
, 0, sizeof(addr
));
46 addr
.sun_family
= AF_UNIX
;
47 sprintf(addr
.sun_path
, "%s", sock
);
50 (void) DO( bind(s
, (struct sockaddr
*)&addr
, sizeof(addr
)) );
51 (void) DO( listen(s
, 5) );
55 unsigned baddrsize
= 0;
56 struct sockaddr_un baddr
;
57 struct msghdr msg
= {NULL
, 0, NULL
, 0, 0, 0, 0};
59 char buf
[CMSG_SPACE(sizeof(int) * 2)];
62 memset(&baddr
, 0, sizeof(baddr
));
63 x
= DO( accept(s
, (struct sockaddr
*)&baddr
, &baddrsize
) );
65 msg
.msg_control
= buf
;
66 msg
.msg_controllen
= sizeof(buf
);
67 cmsg
= CMSG_FIRSTHDR(&msg
);
68 cmsg
->cmsg_level
= SOL_SOCKET
;
69 cmsg
->cmsg_type
= SCM_RIGHTS
;
70 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int) * 2);
71 ((int *)CMSG_DATA(cmsg
))[0] = fd1
;
72 ((int *)CMSG_DATA(cmsg
))[1] = fd2
;
74 iov
[0].iov_base
= "hello";
80 (void) DO( sendmsg(x
, &msg
, 0) );
86 int s
, fd1
= -1, fd2
= -1, size
, count
= 0, ret
;
87 struct sockaddr_un addr
;
91 char control
[CMSG_SPACE(sizeof(int) * 2)];
93 struct msghdr msg
= { NULL
, 0, iov
, 1, control_un
.control
,
94 sizeof(control_un
), 0 };
95 struct cmsghdr
*cmsg
= &control_un
.cm
;
98 iov
[0].iov_base
= buf
;
99 iov
[0].iov_len
= sizeof(buf
);
101 s
= socket(PF_UNIX
, SOCK_STREAM
, 0);
107 addr
.sun_family
= AF_UNIX
;
108 sprintf(addr
.sun_path
, "%s", sock
);
112 ret
= connect(s
, (struct sockaddr
*)&addr
, sizeof(addr
));
113 if (ret
== -1) sleep(1);
114 } while (count
< 10 && ret
== -1);
122 if ((size
= recvmsg(s
, &msg
, 0)) == -1) {
124 goto again
; /* SIGCHLD from server exiting could interrupt */
130 cmsg
= CMSG_FIRSTHDR(&msg
);
132 if (cmsg
->cmsg_level
== SOL_SOCKET
&&
133 cmsg
->cmsg_type
== SCM_RIGHTS
&&
134 cmsg
->cmsg_len
== CMSG_LEN(sizeof(int) * 2)) {
135 fd1
= ((int *)CMSG_DATA(cmsg
))[0];
136 fd2
= ((int *)CMSG_DATA(cmsg
))[1];
139 cmsg
= CMSG_NXTHDR(&msg
, cmsg
);
142 if (fd1
!= -1) write(fd1
, "Yeah 1\n", 8);
143 if (fd2
!= -1) write(fd2
, "Yeah 2\n", 8);
147 int main (int argc
, char **argv
)
154 sprintf(filea
, "/tmp/data1.%d", pid
);
155 sprintf(fileb
, "/tmp/data2.%d", pid
);
156 sprintf(sock
, "/tmp/sock.%d", pid
);
158 if ((pid
= fork()) == 0) {
167 (void) DO( unlink(filea
) );
168 (void) DO( unlink(fileb
) );
169 (void) DO( unlink(sock
) );