1SD_EVENT_ADD_IO(3) sd_event_add_io SD_EVENT_ADD_IO(3)
2
3
4
6 sd_event_add_io, sd_event_source_get_io_events,
7 sd_event_source_set_io_events, sd_event_source_get_io_revents,
8 sd_event_source_get_io_fd, sd_event_source_set_io_fd,
9 sd_event_source_get_io_fd_own, sd_event_source_set_io_fd_own,
10 sd_event_source, sd_event_io_handler_t - Add an I/O event source to an
11 event loop
12
14 #include <systemd/sd-event.h>
15
16 typedef struct sd_event_source sd_event_source;
17
18 typedef int (*sd_event_io_handler_t)(sd_event_source *s, int fd,
19 uint32_t revents, void *userdata);
20
21 int sd_event_add_io(sd_event *event, sd_event_source **source, int fd,
22 uint32_t events, sd_event_io_handler_t handler,
23 void *userdata);
24
25 int sd_event_source_get_io_events(sd_event_source *source,
26 uint32_t *events);
27
28 int sd_event_source_set_io_events(sd_event_source *source,
29 uint32_t events);
30
31 int sd_event_source_get_io_revents(sd_event_source *source,
32 uint32_t *revents);
33
34 int sd_event_source_get_io_fd(sd_event_source *source);
35
36 int sd_event_source_set_io_fd(sd_event_source *source, int fd);
37
38 int sd_event_source_get_io_fd_own(sd_event_source *source);
39
40 int sd_event_source_set_io_fd_own(sd_event_source *source, int b);
41
43 sd_event_add_io() adds a new I/O event source to an event loop. The
44 event loop object is specified in the event parameter, the event source
45 object is returned in the source parameter. The fd parameter takes the
46 UNIX file descriptor to watch, which may refer to a socket, a FIFO, a
47 message queue, a serial connection, a character device, or any other
48 file descriptor compatible with Linux epoll(7). The events parameter
49 takes a bit mask of events to watch for, a combination of the following
50 event flags: EPOLLIN, EPOLLOUT, EPOLLRDHUP, EPOLLPRI, and EPOLLET, see
51 epoll_ctl(2) for details. The handler shall reference a function to
52 call when the event source is triggered. The userdata pointer will be
53 passed to the handler function, and may be chosen freely by the caller.
54 The handler will also be passed the file descriptor the event was seen
55 on, as well as the actual event flags. It's generally a subset of the
56 events watched, however may additionally include EPOLLERR and EPOLLHUP.
57
58 By default, an event source will stay enabled continuously
59 (SD_EVENT_ON), but this may be changed with
60 sd_event_source_set_enabled(3). If the handler function returns a
61 negative error code, it will be disabled after the invocation, even if
62 the SD_EVENT_ON mode was requested before. Note that an event source
63 set to SD_EVENT_ON will fire continuously unless data is read from or
64 written to the file descriptor to reset the mask of events seen.
65
66 Setting the I/O event mask to watch for to 0 does not mean that the
67 event source won't be triggered anymore, as EPOLLHUP and EPOLLERR may
68 be triggered even with a zero event mask. To temporarily disable an I/O
69 event source use sd_event_source_set_enabled(3) with SD_EVENT_OFF
70 instead.
71
72 To destroy an event source object use sd_event_source_unref(3), but
73 note that the event source is only removed from the event loop when all
74 references to the event source are dropped. To make sure an event
75 source does not fire anymore, even if it is still referenced, disable
76 the event source using sd_event_source_set_enabled(3) with
77 SD_EVENT_OFF.
78
79 If the second parameter of sd_event_add_io() is NULL no reference to
80 the event source object is returned. In this case the event source is
81 considered "floating", and will be destroyed implicitly when the event
82 loop itself is destroyed.
83
84 If the handler to sd_event_add_io() is NULL, and the event source
85 fires, this will be considered a request to exit the event loop. In
86 this case, the userdata parameter, cast to an integer, is passed as the
87 exit code parameter to sd_event_exit(3).
88
89 Note that this call does not take possession of the file descriptor
90 passed in, ownership (and thus the duty to close it when it is no
91 longer needed) remains with the caller. However, with the
92 sd_event_source_set_io_fd_own() call (see below) the event source may
93 optionally take ownership of the file descriptor after the event source
94 has been created. In that case the file descriptor is closed
95 automatically as soon as the event source is released.
96
97 It is recommended to use sd_event_add_io() only in conjunction with
98 file descriptors that have O_NONBLOCK set, to ensure that all I/O
99 operations from invoked handlers are properly asynchronous and
100 non-blocking. Using file descriptors without O_NONBLOCK might result in
101 unexpected starvation of other event sources. See fcntl(2) for details
102 on enabling O_NONBLOCK mode.
103
104 sd_event_source_get_io_events() retrieves the configured mask of
105 watched I/O events of an event source created previously with
106 sd_event_add_io(). It takes the event source object and a pointer to a
107 variable to store the mask in.
108
109 sd_event_source_set_io_events() configures the mask of watched I/O
110 events of an event source created previously with sd_event_add_io(). It
111 takes the event source object and the new event mask.
112
113 sd_event_source_get_io_revents() retrieves the I/O event mask of
114 currently seen but undispatched events from an event source created
115 previously with sd_event_add_io(). It takes the event source object and
116 a pointer to a variable to store the event mask in. When called from a
117 handler function on the handler's event source object this will return
118 the same mask as passed to the handler's revents parameter. This call
119 is primarily useful to check for undispatched events of an event source
120 from the handler of an unrelated (possibly higher priority) event
121 source. Note the relation between sd_event_source_get_pending() and
122 sd_event_source_get_io_revents(): both functions will report non-zero
123 results when there's an event pending for the event source, but the
124 former applies to all event source types, the latter only to I/O event
125 sources.
126
127 sd_event_source_get_io_fd() retrieves the UNIX file descriptor of an
128 event source created previously with sd_event_add_io(). It takes the
129 event source object and returns the non-negative file descriptor or a
130 negative error number on error (see below).
131
132 sd_event_source_set_io_fd() changes the UNIX file descriptor of an I/O
133 event source created previously with sd_event_add_io(). It takes the
134 event source object and the new file descriptor.
135
136 sd_event_source_set_io_fd_own() controls whether the file descriptor of
137 the event source shall be closed automatically when the event source is
138 freed, i.e. whether it shall be considered 'owned' by the event source
139 object. By default it is not closed automatically, and the application
140 has to do this on its own. The b parameter is a boolean parameter: if
141 zero, the file descriptor is not closed automatically when the event
142 source is freed, otherwise it is closed.
143
144 sd_event_source_get_io_fd_own() may be used to query the current
145 setting of the file descriptor ownership boolean flag as set with
146 sd_event_source_set_io_fd_own(). It returns positive if the file
147 descriptor is closed automatically when the event source is destroyed,
148 zero if not, and negative on error.
149
151 On success, these functions return 0 or a positive integer. On failure,
152 they return a negative errno-style error code.
153
154 Errors
155 Returned values may indicate the following problems:
156
157 -ENOMEM
158 Not enough memory to allocate an object.
159
160 -EINVAL
161 An invalid argument has been passed.
162
163 -ESTALE
164 The event loop is already terminated.
165
166 -ECHILD
167 The event loop has been created in a different process.
168
169 -EDOM
170 The passed event source is not an I/O event source.
171
173 These APIs are implemented as a shared library, which can be compiled
174 and linked to with the libsystemd pkg-config(1) file.
175
177 systemd(1), sd-event(3), sd_event_new(3), sd_event_now(3),
178 sd_event_add_time(3), sd_event_add_signal(3), sd_event_add_child(3),
179 sd_event_add_inotify(3), sd_event_add_defer(3),
180 sd_event_source_set_enabled(3), sd_event_source_set_priority(3),
181 sd_event_source_set_userdata(3), sd_event_source_set_description(3),
182 sd_event_source_get_pending(3), sd_event_source_set_floating(3),
183 epoll_ctl(2), epoll(7)
184
185
186
187systemd 249 SD_EVENT_ADD_IO(3)