HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

man/event-quick-child.c

787 bytesGit mode 100644Download original fileMIT-0

Git blob: a1d476243efe0fbabe853a9e64443407492676d3

Lines 1–38 of 38

/* SPDX-License-Identifier: MIT-0 */
 
#define _GNU_SOURCE 1
#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);
 
  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;
}