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 Note that this call does not take possession of the file descriptor
85 passed in, ownership (and thus the duty to close it when it is no
86 longer needed) remains with the caller. However, with the
87 sd_event_source_set_io_fd_own() call (see below) the event source may
88 optionally take ownership of the file descriptor after the event source
89 has been created. In that case the file descriptor is closed
90 automatically as soon as the event source is released.
91
92 It is recommended to use sd_event_add_io() only in conjunction with
93 file descriptors that have O_NONBLOCK set, to ensure that all I/O
94 operations from invoked handlers are properly asynchronous and
95 non-blocking. Using file descriptors without O_NONBLOCK might result in
96 unexpected starvation of other event sources. See fcntl(2) for details
97 on enabling O_NONBLOCK mode.
98
99 sd_event_source_get_io_events() retrieves the configured mask of
100 watched I/O events of an event source created previously with
101 sd_event_add_io(). It takes the event source object and a pointer to a
102 variable to store the mask in.
103
104 sd_event_source_set_io_events() configures the mask of watched I/O
105 events of an event source created previously with sd_event_add_io(). It
106 takes the event source object and the new event mask.
107
108 sd_event_source_get_io_revents() retrieves the I/O event mask of
109 currently seen but undispatched events from an event source created
110 previously with sd_event_add_io(). It takes the event source object and
111 a pointer to a variable to store the event mask in. When called from a
112 handler function on the handler's event source object this will return
113 the same mask as passed to the handler's revents parameter. This call
114 is primarily useful to check for undispatched events of an event source
115 from the handler of an unrelated (possibly higher priority) event
116 source. Note the relation between sd_event_source_get_pending() and
117 sd_event_source_get_io_revents(): both functions will report non-zero
118 results when there's an event pending for the event source, but the
119 former applies to all event source types, the latter only to I/O event
120 sources.
121
122 sd_event_source_get_io_fd() retrieves the UNIX file descriptor of an
123 event source created previously with sd_event_add_io(). It takes the
124 event source object and returns the non-negative file descriptor or a
125 negative error number on error (see below).
126
127 sd_event_source_set_io_fd() changes the UNIX file descriptor of an I/O
128 event source created previously with sd_event_add_io(). It takes the
129 event source object and the new file descriptor.
130
131 sd_event_source_set_io_fd_own() controls whether the file descriptor of
132 the event source shall be closed automatically when the event source is
133 freed, i.e. whether it shall be considered 'owned' by the event source
134 object. By default it is not closed automatically, and the application
135 has to do this on its own. The b parameter is a boolean parameter: if
136 zero, the file descriptor is not closed automatically when the event
137 source is freed, otherwise it is closed.
138
139 sd_event_source_get_io_fd_own() may be used to query the current
140 setting of the file descriptor ownership boolean flag as set with
141 sd_event_source_set_io_fd_own(). It returns positive if the file
142 descriptor is closed automatically when the event source is destroyed,
143 zero if not, and negative on error.
144
146 On success, these functions return 0 or a positive integer. On failure,
147 they return a negative errno-style error code.
148
149 Errors
150 Returned values may indicate the following problems:
151
152 -ENOMEM
153 Not enough memory to allocate an object.
154
155 -EINVAL
156 An invalid argument has been passed.
157
158 -ESTALE
159 The event loop is already terminated.
160
161 -ECHILD
162 The event loop has been created in a different process.
163
164 -EDOM
165 The passed event source is not an I/O event source.
166
168 These APIs are implemented as a shared library, which can be compiled
169 and linked to with the libsystemd pkg-config(1) file.
170
172 systemd(1), sd-event(3), sd_event_new(3), sd_event_now(3),
173 sd_event_add_time(3), sd_event_add_signal(3), sd_event_add_child(3),
174 sd_event_add_inotify(3), sd_event_add_defer(3),
175 sd_event_source_set_enabled(3), sd_event_source_set_priority(3),
176 sd_event_source_set_userdata(3), sd_event_source_set_description(3),
177 sd_event_source_get_pending(3), sd_event_source_set_floating(3),
178 epoll_ctl(2), epoll(7)
179
180
181
182systemd 245 SD_EVENT_ADD_IO(3)