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
29   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, library or
37           module instance.
38

EXAMPLES

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

NOTES

92       Functions described here are available as a shared library, which can
93       be compiled against and linked to with the libsystemd pkg-config(1)
94       file.
95
96       The code described here uses getenv(3), which is declared to be not
97       multi-thread-safe. This means that the code calling the functions
98       described here must not call setenv(3) from a parallel thread. It is
99       recommended to only do calls to setenv() from an early phase of the
100       program when no other threads have been started.
101

SEE ALSO

103       sd-event(3), sd_event_new(3), sd_event_wait(3), epoll_ctl(2), epoll(7)
104
105
106
107systemd 254                                                 SD_EVENT_GET_FD(3)
Impressum