-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipe_sockpair.c
More file actions
59 lines (53 loc) · 1.05 KB
/
pipe_sockpair.c
File metadata and controls
59 lines (53 loc) · 1.05 KB
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
/**
* 'pipe' implemented using 'socketpair'
*/
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#define BUFSZ 50
int pipe_sockpair(int fds[2]) {
int socks[2];
if (socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1)
return -1;
if (shutdown(socks[0], SHUT_RD) == -1)
return -1;
if (shutdown(socks[1], SHUT_WR) == -1)
return -1;
fds[0] = socks[0];
fds[1] = socks[1];
return 0;
}
int main(int argc, char **argv) {
int fds[2];
char buf[BUFSZ];
ssize_t nread;
if (pipe_sockpair(fds) == -1) {
perror("pipe_sockpair");
exit(EXIT_FAILURE);
}
switch (fork()) {
case -1:
perror("fork");
exit(EXIT_FAILURE);
case 0:
close(fds[0]);
while ((nread = read(fds[1], buf, BUFSZ)) > 0)
write(STDOUT_FILENO, buf, nread);
if (nread == -1) {
perror("read child");
exit(EXIT_FAILURE);
}
break;
default:
close(fds[1]);
while ((nread = read(STDIN_FILENO, buf, BUFSZ)) > 0)
write(fds[0], buf, nread);
if (nread == -1) {
perror("read parent");
exit(EXIT_FAILURE);
}
break;
}
exit(EXIT_SUCCESS);
}