---
name: "sd_event_add_child"
section: "3"
platform: "linux"
package: "systemd-libs"
version: "262.1-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/sd_event_add_child"
---

# sd\_event\_add\_child(3)

Add a child process state change event source to an event loop

## NAME

sd\_event\_add\_child, sd\_event\_add\_child\_pidfd, sd\_event\_source\_get\_child\_pid, sd\_event\_source\_get\_child\_pidfd, sd\_event\_source\_get\_child\_pidfd\_own, sd\_event\_source\_set\_child\_pidfd\_own, sd\_event\_source\_get\_child\_process\_own, sd\_event\_source\_set\_child\_process\_own, sd\_event\_source\_send\_child\_signal, sd\_event\_child\_handler\_t - Add a child process state change event source to an event loop

## SYNOPSIS

```
#include <systemd/sd-event.h>
```

```
typedef struct sd_event_source sd_event_source;
```

**typedef int (\*sd\_event\_child\_handler\_t)(sd\_event\_source \****s***, const siginfo\_t \****si***, void \****userdata***);**

**int sd\_event\_add\_child(sd\_event \****event***, sd\_event\_source \*\****source***, pid\_t** *pid***, int** *options***, sd\_event\_child\_handler\_t** *handler***, void \****userdata***);**

**int sd\_event\_add\_child\_pidfd(sd\_event \****event***, sd\_event\_source \*\****source***, int** *pidfd***, int** *options***, sd\_event\_child\_handler\_t** *handler***, void \****userdata***);**

**int sd\_event\_source\_get\_child\_pid(sd\_event\_source \****source***, pid\_t \****ret***);**

**int sd\_event\_source\_get\_child\_pidfd(sd\_event\_source \****source***);**

**int sd\_event\_source\_get\_child\_pidfd\_own(sd\_event\_source \****source***);**

**int sd\_event\_source\_set\_child\_pidfd\_own(sd\_event\_source \****source***, int** *own***);**

**int sd\_event\_source\_get\_child\_process\_own(sd\_event\_source \****source***);**

**int sd\_event\_source\_set\_child\_process\_own(sd\_event\_source \****source***, int** *own***);**

**int sd\_event\_source\_send\_child\_signal(sd\_event\_source \****source***, int** *sig***, const siginfo\_t \****info***, unsigned** *flags***);**

## DESCRIPTION

**sd\_event\_add\_child()** adds a new child process state change event source to an event loop. The event loop object is specified in the *event* parameter, the event source object is returned in the *source* parameter. The *pid* parameter specifies the PID of the process to watch, which must be a direct child process of the invoking process. The *options* parameter determines which state changes will be watched for. It must contain an OR-ed mask of **WEXITED** (watch for the child process terminating), **WSTOPPED** (watch for the child process being stopped by a signal), **WCONTINUED** (watch for the child process being resumed by a signal) and **WNOWAIT** (Do not reap the child process after it exits). See [**waitid**(2)](/waitid/2.md) for further information.

The *handler* must be a function to call when the process changes state or **NULL**. The handler function will be passed the *userdata* pointer, which may be chosen freely by the caller. The handler also receives a pointer to a siginfo\_t structure containing information about the child process event. The handler may return negative to signal an error (see below), other return values are ignored. If *handler* is **NULL**, a default handler that calls [**sd\_event\_exit**(3)](/sd_event_exit/3.md) will be used.

Only a single handler may be installed for a specific child process. The handler is enabled for a single event (**SD\_EVENT\_ONESHOT**), but this may be changed with [**sd\_event\_source\_set\_enabled**(3)](/sd_event_source_set_enabled/3.md). If the handler function returns a negative error code, it will either be disabled after the invocation, even if the **SD\_EVENT\_ON** mode was requested before, or it will cause the loop to terminate, see [**sd\_event\_source\_set\_exit\_on\_failure**(3)](/sd_event_source_set_exit_on_failure/3.md).

To destroy an event source object use [**sd\_event\_source\_unref**(3)](/sd_event_source_unref/3.md), but note that the event source is only removed from the event loop when all references to the event source are dropped. To make sure an event source does not fire anymore, even when there's still a reference to it kept, consider setting the event source to **SD\_EVENT\_OFF** with [**sd\_event\_source\_set\_enabled**(3)](/sd_event_source_set_enabled/3.md).

The kernel autoreaping logic must be disabled for this function to work as expected (see [**signal**(7)](/signal/7.md)).

When watching for **WSTOPPED** or **WCONTINUED**, the **SIGCHLD** signal must be blocked in all threads before this function is called (using [**sigprocmask**(2)](/sigprocmask/2.md) or [**pthread\_sigmask**(3)](/pthread_sigmask/3.md)).

If the second parameter of **sd\_event\_add\_child()** is passed as **NULL** no reference to the event source object is returned. In this case, the event source is considered "floating", and will be destroyed implicitly when the event loop itself is destroyed.

Note that the *handler* function is invoked at a time where the child process is not reaped yet (and thus still is exposed as a zombie process by the kernel). However, the child will be reaped automatically after the function returns. Child processes for which no child process state change event sources are installed will not be reaped by the event loop implementation.

If the *handler* parameter to **sd\_event\_add\_child()** is **NULL**, and the event source fires, this will be considered a request to exit the event loop. In this case, the *userdata* parameter, cast to an integer, is passed as the exit code parameter to [**sd\_event\_exit**(3)](/sd_event_exit/3.md).

If both a child process state change event source and a **SIGCHLD** signal event source is installed in the same event loop, the configured event source priorities decide which event source is dispatched first. If the signal handler is processed first, it should leave the child processes for which child process state change event sources are installed unreaped.

**sd\_event\_add\_child\_pidfd()** is similar to **sd\_event\_add\_child()** but takes a file descriptor referencing the process ("pidfd") instead of the numeric PID. A suitable file descriptor may be acquired via [**pidfd\_open**(2)](/pidfd_open/2.md) and related calls. The passed file descriptor is not closed when the event source is freed again, unless **sd\_event\_source\_set\_child\_pidfd\_own()** is used to turn this behaviour on. Note that regardless which of **sd\_event\_add\_child()** and **sd\_event\_add\_child\_pidfd()** is used for allocating an event source, the watched process has to be a direct child process of the invoking process. Also in both cases **SIGCHLD** has to be blocked in the invoking process when watching for **WSTOPPED** or **WCONTINUED** and the kernel autoreaping logic has to be disabled.

**sd\_event\_source\_get\_child\_pid()** retrieves the configured PID of a child process state change event source created previously with **sd\_event\_add\_child()**. It takes the event source object as the *source* parameter and a pointer to a **pid\_t** variable to return the process ID in.

**sd\_event\_source\_get\_child\_pidfd()** retrieves the file descriptor referencing the watched process ("pidfd"). The event loop internally makes use of pidfds to watch child processes, regardless of whether the individual event sources are allocated via **sd\_event\_add\_child()** or **sd\_event\_add\_child\_pidfd()**. If the latter call was used to allocate the event source, this function returns the original file descriptor used for allocation. This call takes the event source object as the *source* parameter and returns the numeric file descriptor.

**sd\_event\_source\_get\_child\_pidfd\_own()** may be used to query whether the pidfd the event source encapsulates shall be closed when the event source is freed. This function returns zero if the pidfd shall be left open, and positive if it shall be closed automatically. By default, this setting defaults to on if the event source was allocated via **sd\_event\_add\_child()** and off if it was allocated via **sd\_event\_add\_child\_pidfd()**. The **sd\_event\_source\_set\_child\_pidfd\_own()** function may be used to change the setting and takes a boolean parameter with the new setting.

**sd\_event\_source\_get\_child\_process\_own()** may be used to query whether the process the event source watches shall be killed (with **SIGKILL**) and reaped when the event source is freed. This function returns zero if the process shell be left running, and positive if it shall be killed and reaped automatically. By default, this setting defaults to off. The **sd\_event\_source\_set\_child\_process\_own()** function may be used to change the setting and takes a boolean parameter with the new setting. Note that currently if the calling process is terminated abnormally the watched process might survive even thought the event source ceases to exist. This behaviour might change eventually.

**sd\_event\_source\_send\_child\_signal()** may be used to send a UNIX signal to the watched process via [**pidfd\_send\_signal**(2)](/pidfd_send_signal/2.md). The specified parameters match those of the underlying system call, except that the *info* is never modified (and is thus declared constant). Like for the underlying system call, the *flags* parameter currently must be zero.

## RETURN VALUE

On success, these functions return 0 or a positive integer. On failure, they return a negative errno-style error code.

### Errors

Returned errors may indicate the following problems:

**-ENOMEM**

Not enough memory to allocate an object.

**-EINVAL**

An invalid argument has been passed. This includes specifying an empty mask in *options* or a mask which contains values different than a combination of **WEXITED**, **WSTOPPED**, and **WCONTINUED**.

**-EBUSY**

A handler is already installed for this child process, or **SIGCHLD** is not blocked.

**-ESTALE**

The event loop is already terminated.

**-ECHILD**

The event loop has been created in a different process, library or module instance.

**-EDOM**

The passed event source is not a child process event source.

## NOTES

Functions described here are available as a shared library, which can be compiled against and linked to with the **libsystemd** [**pkg-config**(1)](/pkg-config/1.md) file.

All functions listed here are thread-agnostic and only a single thread may operate on a given object at any given time. Different threads may access the same object at different times. Multiple independent objects may be used from different threads in parallel.

The code described here uses [**getenv**(3)](/getenv/3.md), which is declared to be not multi-thread-safe. This means that the code calling the functions described here must not call [**setenv**(3)](/setenv/3.md) from a parallel thread. It is recommended to only do calls to **setenv()** from an early phase of the program when no other threads have been started.

## EXAMPLE

**Example 1. Exit loop when the child terminates**

```
/* 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;
}
```

## HISTORY

**sd\_event\_add\_child()**, **sd\_event\_child\_handler\_t()**, and **sd\_event\_source\_get\_child\_pid()** were added in version 217.

**sd\_event\_add\_child\_pidfd()**, **sd\_event\_source\_get\_child\_pidfd()**, **sd\_event\_source\_get\_child\_pidfd\_own()**, **sd\_event\_source\_set\_child\_pidfd\_own()**, **sd\_event\_source\_get\_child\_process\_own()**, **sd\_event\_source\_set\_child\_process\_own()**, and **sd\_event\_source\_send\_child\_signal()** were added in version 245.

## SEE ALSO

[**systemd**(1)](/systemd/1.md), [**sd-event**(3)](/sd-event/3.md), [**sd\_event\_new**(3)](/sd_event_new/3.md), [**sd\_event\_now**(3)](/sd_event_now/3.md), [**sd\_event\_add\_io**(3)](/sd_event_add_io/3.md), [**sd\_event\_add\_time**(3)](/sd_event_add_time/3.md), [**sd\_event\_add\_signal**(3)](/sd_event_add_signal/3.md), [**sd\_event\_add\_inotify**(3)](/sd_event_add_inotify/3.md), [**sd\_event\_add\_defer**(3)](/sd_event_add_defer/3.md), [**sd\_event\_source\_set\_enabled**(3)](/sd_event_source_set_enabled/3.md), [**sd\_event\_source\_set\_priority**(3)](/sd_event_source_set_priority/3.md), [**sd\_event\_source\_set\_userdata**(3)](/sd_event_source_set_userdata/3.md), [**sd\_event\_source\_set\_description**(3)](/sd_event_source_set_description/3.md), [**sd\_event\_source\_set\_floating**(3)](/sd_event_source_set_floating/3.md), [**waitid**(2)](/waitid/2.md), [**sigprocmask**(2)](/sigprocmask/2.md), [**pthread\_sigmask**(3)](/pthread_sigmask/3.md), [**pidfd\_open**(2)](/pidfd_open/2.md), [**pidfd\_send\_signal**(2)](/pidfd_send_signal/2.md), [**rt\_sigqueueinfo**(2)](/rt_sigqueueinfo/2.md), [**kill**(2)](/kill/2.md)
