1SD_EVENT_GET_FD(3)              sd_event_get_fd             SD_EVENT_GET_FD(3)
2
3
4

NAME

6       sd_event_get_fd - Obtain a file descriptor to poll for event loop
7       events
8

SYNOPSIS

10       #include <systemd/sd-event.h>
11
12       int sd_event_get_fd(sd_event *event);
13

DESCRIPTION

15       sd_event_get_fd() returns the file descriptor that an event loop object
16       returned by the sd_event_new(3) function uses to wait for events. This
17       file descriptor may itself be polled for POLLIN/EPOLLIN events. This
18       makes it possible to embed an sd-event(3) event loop into another,
19       possibly foreign, event loop.
20
21       The returned file descriptor refers to an epoll(7) object. It is
22       recommended not to alter it by invoking epoll_ctl(2) on it, in order to
23       avoid interference with the event loop's inner logic and assumptions.
24

RETURN VALUE

26       On success, sd_event_get_fd() returns a non-negative file descriptor.
27       On failure, it returns a negative errno-style error code.
28

ERRORS

30       Returned errors may indicate the following problems:
31
32       -EINVAL
33           event is not a valid pointer to an sd_event structure.
34
35       -ECHILD
36           The event loop has been created in a different process.
37

EXAMPLES

39       Example 1. Integration in the GLib event loop
40
41           /* SPDX-License-Identifier: MIT */
42
43           #include <stdlib.h>
44           #include <glib.h>
45           #include <systemd/sd-event.h>
46
47           typedef struct SDEventSource {
48             GSource source;
49             GPollFD pollfd;
50             sd_event *event;
51           } SDEventSource;
52
53           static gboolean event_prepare(GSource *source, gint *timeout_) {
54             return sd_event_prepare(((SDEventSource *)source)->event) > 0;
55           }
56
57           static gboolean event_check(GSource *source) {
58             return sd_event_wait(((SDEventSource *)source)->event, 0) > 0;
59           }
60
61           static gboolean event_dispatch(GSource *source, GSourceFunc callback, gpointer user_data) {
62             return sd_event_dispatch(((SDEventSource *)source)->event) > 0;
63           }
64
65           static void event_finalize(GSource *source) {
66             sd_event_unref(((SDEventSource *)source)->event);
67           }
68
69           static GSourceFuncs event_funcs = {
70             .prepare = event_prepare,
71             .check = event_check,
72             .dispatch = event_dispatch,
73             .finalize = event_finalize,
74           };
75
76           GSource *g_sd_event_create_source(sd_event *event) {
77             SDEventSource *source;
78
79             source = (SDEventSource *)g_source_new(&event_funcs, sizeof(SDEventSource));
80
81             source->event = sd_event_ref(event);
82             source->pollfd.fd = sd_event_get_fd(event);
83             source->pollfd.events = G_IO_IN | G_IO_HUP | G_IO_ERR;
84
85             g_source_add_poll((GSource *)source, &source->pollfd);
86
87             return (GSource *)source;
88           }
89

NOTES

91       These APIs are implemented as a shared library, which can be compiled
92       and linked to with the libsystemd pkg-config(1) file.
93

SEE ALSO

95       sd-event(3), sd_event_new(3), sd_event_wait(3), epoll_ctl(3), epoll(7)
96
97
98
99systemd 239                                                 SD_EVENT_GET_FD(3)
Impressum