socketpair2.c
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#define _POSIX_C_SOURCE 199309L
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/epoll.h>
void
errorExit(const char * const message)
{
if (errno != 0) {
fprintf(stderr, "%s: %s\n", strerror(errno), message);
} else {
fprintf(stderr, "%s\n", message);
}
exit(EXIT_FAILURE);
}
void
childCode(int socket)
{
struct msghdr msg;
char cmsgbuf[CMSG_SPACE(sizeof(int))];
struct cmsghdr * cmsg;
int fd = 0;
FILE * handle;
char buffer[1024];
int num = 0;
#define MAX_EVENTS 1
struct epoll_event ev, events[MAX_EVENTS];
int nfds, epollfd;
int flags = fcntl(socket, F_GETFL, 0);
fcntl(socket, F_SETFL, flags | O_NONBLOCK);
epollfd = epoll_create1(0);
if (epollfd == -1) {
errorExit("failed to create epoll fd");
}
ev.events = EPOLLIN;
ev.data.fd = socket;
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, socket, &ev) == -1) {
errorExit("failed to add socket to epoll poll");
}
while (num++ < 2) {
nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1);
if (nfds == -1) {
errorExit("failed in epoll_wait");
}
memset(&msg, 0, sizeof(msg));
msg.msg_control = cmsgbuf;
msg.msg_controllen = sizeof(cmsgbuf);
if (0 > recvmsg(events[0].data.fd, &msg, 0)) {
errorExit("error receiving fd");
}
cmsg = CMSG_FIRSTHDR(&msg);
if (cmsg == NULL || cmsg->cmsg_type != SCM_RIGHTS) {
errorExit(
"The first control structure contains "
"no file descriptor.");
}
memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
handle = fdopen(fd, "r");
if(NULL == fgets(buffer, 1024, handle)) {
errorExit("child fails to read from retrieved handle");
}
close(fd);
printf("%s", buffer);
}
}
void
parentCode(int socket)
{
struct msghdr msg;
struct cmsghdr * cmsg;
char cmsgbuf[CMSG_SPACE(sizeof(int))];
int fd = open("dummy", O_RDONLY);
if (0 > fd) {
errorExit("error opening testfile: dummy\n");
}
memset(&msg, 0, sizeof(msg));
msg.msg_control = cmsgbuf;
msg.msg_controllen = sizeof(cmsgbuf); // necessary for CMSG_FIRSTHDR to
// return the correct value
cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = CMSG_LEN(sizeof(int));
memcpy(CMSG_DATA(cmsg), &fd, sizeof(int));
msg.msg_controllen = cmsg->cmsg_len; // total size of all control blocks
if(0 > sendmsg(socket, &msg, 0)) {
errorExit("error sending descriptor");
}
// send it twice to get the child to exit
close(fd);
fd = open("dummy", O_RDONLY);
memcpy(CMSG_DATA(cmsg), &fd, sizeof(int));
if(0 > sendmsg(socket, &msg, 0)) {
errorExit("error sending descriptor");
}
close(fd);
}
int
main(int argc, char * argv[])
{
int socket[2];
pid_t pid;
if (0 > socketpair(AF_UNIX, SOCK_DGRAM, 0, socket)) {
errorExit("error creating socketpair");
}
switch (pid = fork()) {
case -1:
perror("error in fork");
exit(EXIT_FAILURE);
case 0:
close(socket[0]);
childCode(socket[1]);
close(socket[1]);
break;
default:
close(socket[1]);
parentCode(socket[0]);
close(socket[0]);
wait(NULL);
}
return 0;
}
// vim: set ts=4 sw=4: