-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_waitfd.c
More file actions
52 lines (46 loc) · 858 Bytes
/
test_waitfd.c
File metadata and controls
52 lines (46 loc) · 858 Bytes
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
#define _GNU_SOURCE
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <poll.h>
#include <fcntl.h>
#include <assert.h>
#include <waitfd.h>
int
main(int argc, char **argv)
{
pid_t pid;
pid = fork();
switch (pid) {
case -1:
perror("fork");
exit(EXIT_FAILURE);
case 0:
sleep(1);
exit(42);
default:
{
struct pollfd pollfd = { .fd = -1, .events = POLLIN };
int status;
siginfo_t siginfo;
pollfd.fd = waitfd(pid, O_NONBLOCK);
status = poll(&pollfd, 1, -1);
switch (status) {
case -1:
perror("poll");
exit(EXIT_FAILURE);
case 0:
abort();
default:
{
int size;
size = read(pollfd.fd, &siginfo, sizeof siginfo);
assert(size == sizeof siginfo);
assert(siginfo.si_code == CLD_EXITED);
assert(siginfo.si_status == 42);
exit(EXIT_SUCCESS);
}
}
}
}
}