1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-02-02 09:47:03 +03:00
systemd-stable/man/event-quick-child.c
Yu Watanabe 9d2165ea42 man/examples: fix sd- header path
(cherry picked from commit 2548ce6a30a1183a7bbbd699aafdf81249a5fe4e)
(cherry picked from commit 52f60014056dcf3e5efd5edf0c67b721a18e723c)
(cherry picked from commit ea8070be46548f23e863504737a01d39fab2050b)
2024-04-26 01:20:05 +02:00

43 lines
935 B
C

/* SPDX-License-Identifier: MIT-0 */
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <systemd/sd-event.h>
int main(int argc, char **argv) {
pid_t pid = fork();
assert(pid >= 0);
/* SIGCHLD signal must be blocked for sd_event_add_child to work */
sigset_t ss;
sigemptyset(&ss);
sigaddset(&ss, SIGCHLD);
sigprocmask(SIG_BLOCK, &ss, NULL);
if (pid == 0) /* child */
sleep(1);
else { /* parent */
sd_event *e = NULL;
int r;
/* Create the default event loop */
sd_event_default(&e);
assert(e);
/* We create a floating child event source (attached to 'e').
* The default handler will be called with 666 as userdata, which
* will become the exit value of the loop. */
r = sd_event_add_child(e, NULL, pid, WEXITED, NULL, (void*) 666);
assert(r >= 0);
r = sd_event_loop(e);
assert(r == 666);
sd_event_unref(e);
}
return 0;
}