]>
Commit | Line | Data |
---|---|---|
1fe6d37e | 1 | /* SPDX-License-Identifier: MIT-0 */ |
5ca99dfa | 2 | |
040cb664 | 3 | #define _GNU_SOURCE 1 |
5ca99dfa ZJS |
4 | #include <assert.h> |
5 | #include <stdio.h> | |
6 | #include <unistd.h> | |
2548ce6a | 7 | #include <systemd/sd-event.h> |
5ca99dfa ZJS |
8 | |
9 | int main(int argc, char **argv) { | |
10 | pid_t pid = fork(); | |
11 | assert(pid >= 0); | |
12 | ||
13 | /* SIGCHLD signal must be blocked for sd_event_add_child to work */ | |
14 | sigset_t ss; | |
15 | sigemptyset(&ss); | |
16 | sigaddset(&ss, SIGCHLD); | |
17 | sigprocmask(SIG_BLOCK, &ss, NULL); | |
18 | ||
19 | if (pid == 0) /* child */ | |
20 | sleep(1); | |
21 | ||
22 | else { /* parent */ | |
23 | sd_event *e = NULL; | |
24 | int r; | |
25 | ||
26 | /* Create the default event loop */ | |
27 | sd_event_default(&e); | |
28 | assert(e); | |
29 | ||
30 | /* We create a floating child event source (attached to 'e'). | |
31 | * The default handler will be called with 666 as userdata, which | |
32 | * will become the exit value of the loop. */ | |
33 | r = sd_event_add_child(e, NULL, pid, WEXITED, NULL, (void*) 666); | |
34 | assert(r >= 0); | |
35 | ||
36 | r = sd_event_loop(e); | |
37 | assert(r == 666); | |
38 | ||
39 | sd_event_unref(e); | |
40 | } | |
41 | ||
42 | return 0; | |
43 | } |