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