-
Notifications
You must be signed in to change notification settings - Fork 1
/
test-passfds.c
216 lines (183 loc) · 4.21 KB
/
test-passfds.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <sys/socket.h>
#include <sys/types.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <unistd.h>
#include <passfds-internal.h>
#define BUF_SIZE 80
/*
* Start with functions that invoke a aystem call, printing an error message
* and exiting if it failed. This is probably not the way errors would be
* handled in a real program, but it makes understanding the test code
* a lot simpler.
*/
static void do_close(int fd)
{
int rc;
rc = close(fd);
if (rc == -1) {
perror("close");
exit(EXIT_FAILURE);
}
}
static void do_read(int fd, const char *who)
{
char buf[BUF_SIZE];
ssize_t rc;
rc = read(fd, buf, sizeof(buf));
if (rc == -1) {
perror("read");
exit(EXIT_FAILURE);
}
printf("%s read: %s\n", who, buf);
}
static void do_write(int fd, const char *who)
{
char buf[BUF_SIZE];
ssize_t rc;
snprintf(buf, sizeof(buf), "message sent by %s", who);
rc = write(fd, buf, sizeof(buf));
if (rc == -1) {
perror("write");
exit(EXIT_FAILURE);
}
}
static void do_pipe(int pipefd[2])
{
int rc;
rc = pipe(pipefd);
if (rc == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
}
/*
* child() - child process from a fork()
* sockfd: File descriptor for a socket with family AF_LOCAL.
*
* This function creates two pipes (one used for sending data from the child
* to the parent and one for dending data in the reverse directionz). The file
* descriptors for parent to use, the read side of one pipe and the write side
* of the other) are then send to the parent. The child then writes a message
* to the parent and reads a message from the parent.
*/
static int child(int sockfd)
{
int to_parent[2], from_parent[2];
int parent_fds[2];
ssize_t rc;
do_pipe(to_parent);
do_pipe(from_parent);
parent_fds[0] = to_parent[0];
parent_fds[1] = from_parent[1];
rc = sendfds(sockfd, parent_fds, ARRAY_SIZE(parent_fds));
if (rc == -1) {
perror("sendfds");
exit(EXIT_FAILURE);
}
if (rc != ARRAY_SIZE(parent_fds)) {
fprintf(stderr, "Expected %zu file descriptors but have %zu\n",
ARRAY_SIZE(parent_fds), rc);
exit(EXIT_FAILURE);
}
do_close(to_parent[0]);
do_close(from_parent[1]);
do_write(to_parent[1], "child");
do_read(from_parent[0], "child");
return EXIT_SUCCESS;
}
/*
* parent() - parent process from a fork()
* sockfd: File descriptor for a socket with family AF_LOCAL.
*
* This is the parent form the fork. It reads two file descriptors from the
* socket into two a two-element int array. The first element will be the
* file descriptor from which a message will be read, The second is the
* file descriptor to which a message will be written. Ther is nothing special
* about the order, it just has to match the order intended by the sender.
*/
static int parent(int sockfd)
{
int parent_fds[2];
ssize_t rc;
rc = recvfds(sockfd, parent_fds, ARRAY_SIZE(parent_fds));
if (rc == -1) {
perror("sendfds");
exit(EXIT_FAILURE);
}
if (rc != ARRAY_SIZE(parent_fds)) {
fprintf(stderr, "Expected %zu file descriptors but have %zu\n",
ARRAY_SIZE(parent_fds), rc);
exit(EXIT_FAILURE);
}
do_read(parent_fds[0], "parent");
do_write(parent_fds[1], "parent");
return EXIT_SUCCESS;
}
static int run()
{
int sv[2];
pid_t pid;
int rc;
/*
* Crete the socket with the local address family. We use type
* SOCK_STREAM, but could use a different socket type.
*/
rc = socketpair(AF_LOCAL, SOCK_STREAM, 0, sv);
if (rc == -1) {
perror("socketpair");
exit(EXIT_FAILURE);
}
/* Create a child process with which to communicate. */
pid = fork();
switch (pid) {
case -1:
perror("fork");
exit(EXIT_FAILURE);
break;
case 0:
do_close(sv[0]);
rc = child(sv[1]);
exit(rc);
break;
default:
do_close(sv[1]);
rc = parent(sv[0]);
break;
}
return rc;
}
static void usage(const char *name)
{
fprintf(stderr, "usage: %s\n", name);
exit(EXIT_FAILURE);
}
int main(int argc, char *argv[])
{
int opt;
int n;
int rc;
/*
* No arguments or options supported, but it's good to verify none
* were accidentally supplied
*/
while ((opt = getopt(argc, argv, "")) != -1) {
switch (opt) {
default:
usage(argv[0]);
break;
}
}
n = argc - optind;
switch (n) {
case 0:
break;
default:
usage(argv[0]);
break;
}
rc = run();
return rc;
}