]> git.saurik.com Git - apple/xnu.git/blob - tools/tests/unit_tests/pipe_test_10807398_src/parent.c
xnu-2422.1.72.tar.gz
[apple/xnu.git] / tools / tests / unit_tests / pipe_test_10807398_src / parent.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <unistd.h>
4 #include <poll.h>
5 #include <sys/errno.h>
6
7 int main(int argc, char **argv)
8 {
9 int fd[2], pid, r;
10 char *args[3], buf[32];
11 struct pollfd pfd;
12 extern char **environ;
13
14 if (pipe(fd) < 0) {
15 perror("pipe");
16 return 1;
17 }
18
19 snprintf(buf, sizeof buf, "%d", fd[0]);
20
21 args[0] = "./child";
22 args[1] = buf;
23 args[2] = 0;
24
25 switch (pid = fork()) {
26 case -1:
27 perror("fork");
28 return 1;
29 case 0: /* child */
30 close(fd[1]);
31 execve(args[0], args, environ);
32 perror(args[0]);
33 _exit(1);
34 default: /* parent */
35 close(fd[0]);
36 pfd.fd = fd[1];
37 pfd.events = POLLOUT;
38 pfd.revents = 0;
39 printf("parent poll(%d)...\n", pfd.fd);
40 errno = 0;
41 r = poll(&pfd, 1, -1);
42 printf("parent poll(%d) returned %d errno %d[%s]\n",
43 pfd.fd, r, errno, strerror(errno));
44 write(fd[1], "howdy", 5);
45 close(fd[1]);
46 printf("parent done\n");
47 }
48
49 return 0;
50 }