---
name: "sd_event_run"
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_run"
---

# sd\_event\_run(3)

Run an event loop

## NAME

sd\_event\_run, sd\_event\_loop - Run an event loop

## SYNOPSIS

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

**int sd\_event\_run(sd\_event \****event***, uint64\_t** *usec***);**

**int sd\_event\_loop(sd\_event \****event***);**

## DESCRIPTION

**sd\_event\_run()** may be used to run a single iteration of the event loop specified in the *event* parameter. The function waits until an event to process is available, and dispatches the registered handler for it. The *usec* parameter specifies the maximum time (in microseconds) to wait for an event. Use **(uint64\_t) -1** to specify an infinite timeout.

**sd\_event\_loop()** invokes **sd\_event\_run()** in a loop, thus implementing the actual event loop. The call returns as soon as exiting was requested using [**sd\_event\_exit**(3)](/sd_event_exit/3.md).

The event loop object *event* is created with [**sd\_event\_new**(3)](/sd_event_new/3.md). Events sources to wait for and their handlers may be registered with [**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\_child**(3)](/sd_event_add_child/3.md), [**sd\_event\_add\_defer**(3)](/sd_event_add_defer/3.md), [**sd\_event\_add\_post**(3)](/sd_event_add_post/3.md) and [**sd\_event\_add\_exit**(3)](/sd_event_add_exit/3.md).

For low-level control of event loop execution, use [**sd\_event\_prepare**(3)](/sd_event_prepare/3.md), [**sd\_event\_wait**(3)](/sd_event_wait/3.md) and [**sd\_event\_dispatch**(3)](/sd_event_dispatch/3.md) which are wrapped by **sd\_event\_run()**. Along with [**sd\_event\_get\_fd**(3)](/sd_event_get_fd/3.md), these functions allow integration of an [**sd-event**(3)](/sd-event/3.md) event loop into foreign event loop implementations.

## RETURN VALUE

On failure, these functions return a negative errno-style error code. **sd\_event\_run()** returns a positive, non-zero integer if an event source was dispatched, and zero when the specified timeout hit before an event source has seen any event, and hence no event source was dispatched. **sd\_event\_loop()** returns the exit code specified when invoking **sd\_event\_exit()**.

### Errors

Returned errors may indicate the following problems:

**-EINVAL**

The *event* parameter is invalid or **NULL**.

**-EBUSY**

The event loop object is not in the right state (see [**sd\_event\_prepare**(3)](/sd_event_prepare/3.md) for an explanation of possible states).

**-ESTALE**

The event loop is already terminated.

**-ECHILD**

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

Other errors are possible, too.

## 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.

## HISTORY

**sd\_event\_run()** and **sd\_event\_loop()** were added in version 220.

## 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\_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\_child**(3)](/sd_event_add_child/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\_exit**(3)](/sd_event_exit/3.md), [**sd\_event\_get\_fd**(3)](/sd_event_get_fd/3.md), [**sd\_event\_wait**(3)](/sd_event_wait/3.md), **GLib Main Event Loop**\[1\]

## NOTES

- 1\.

  GLib Main Event Loop

  https://developer.gnome.org/glib/unstable/glib-The-Main-Event-Loop.html
