D-Bus 1.14.10

dbus-pollable-set-epoll.c
1/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2/* dbus-pollable-set-epoll.c - a pollable set implemented via Linux epoll(4)
3 *
4 * Copyright © 2011 Nokia Corporation
5 *
6 * Licensed under the Academic Free License version 2.1
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21 * MA 02110-1301 USA
22 *
23 */
24
25#include <config.h>
26#include "dbus-pollable-set.h"
27
28#include <dbus/dbus-internals.h>
29#include <dbus/dbus-sysdeps.h>
30
31#ifndef __linux__
32# error This file is for Linux epoll(4)
33#endif
34
35#include <errno.h>
36#include <fcntl.h>
37#include <sys/epoll.h>
38#include <unistd.h>
39
40#ifndef DOXYGEN_SHOULD_SKIP_THIS
41
42 typedef struct {
43 DBusPollableSet parent;
44 int epfd;
45} DBusPollableSetEpoll;
46
47 static inline DBusPollableSetEpoll *
48socket_set_epoll_cast (DBusPollableSet *set)
49{
50 _dbus_assert (set->cls == &_dbus_pollable_set_epoll_class);
51 return (DBusPollableSetEpoll *) set;
52}
53
54/* this is safe to call on a partially-allocated socket set */
55 static void
56socket_set_epoll_free (DBusPollableSet *set)
57{
58 DBusPollableSetEpoll *self = socket_set_epoll_cast (set);
59
60 if (self == NULL)
61 return;
62
63 if (self->epfd != -1)
64 close (self->epfd);
65
66 dbus_free (self);
67}
68
69DBusPollableSet *
70_dbus_pollable_set_epoll_new (void)
71{
72 DBusPollableSetEpoll *self;
73
74 self = dbus_new0 (DBusPollableSetEpoll, 1);
75
76 if (self == NULL)
77 return NULL;
78
79 self->parent.cls = &_dbus_pollable_set_epoll_class;
80
81 self->epfd = epoll_create1 (EPOLL_CLOEXEC);
82
83 if (self->epfd == -1)
84 {
85 int flags;
86
87 /* the size hint is ignored unless you have a rather old kernel,
88 * but must be positive on some versions, so just pick something
89 * arbitrary; it's a hint, not a limit */
90 self->epfd = epoll_create (42);
91
92 flags = fcntl (self->epfd, F_GETFD, 0);
93
94 if (flags != -1)
95 fcntl (self->epfd, F_SETFD, flags | FD_CLOEXEC);
96 }
97
98 if (self->epfd == -1)
99 {
100 socket_set_epoll_free ((DBusPollableSet *) self);
101 return NULL;
102 }
103
104 return (DBusPollableSet *) self;
105}
106
107 static uint32_t
108watch_flags_to_epoll_events (unsigned int flags)
109{
110 uint32_t events = 0;
111
112 if (flags & DBUS_WATCH_READABLE)
113 events |= EPOLLIN;
114 if (flags & DBUS_WATCH_WRITABLE)
115 events |= EPOLLOUT;
116
117 return events;
118}
119
120 static unsigned int
121epoll_events_to_watch_flags (uint32_t events)
122{
123 short flags = 0;
124
125 if (events & EPOLLIN)
126 flags |= DBUS_WATCH_READABLE;
127 if (events & EPOLLOUT)
128 flags |= DBUS_WATCH_WRITABLE;
129 if (events & EPOLLHUP)
130 flags |= DBUS_WATCH_HANGUP;
131 if (events & EPOLLERR)
132 flags |= DBUS_WATCH_ERROR;
133
134 return flags;
135}
136
137 static dbus_bool_t
138socket_set_epoll_add (DBusPollableSet *set,
139 DBusPollable fd,
140 unsigned int flags,
141 dbus_bool_t enabled)
142{
143 DBusPollableSetEpoll *self = socket_set_epoll_cast (set);
144 struct epoll_event event;
145 int err;
146
147 _DBUS_ZERO (event);
148 event.data.fd = fd;
149
150 if (enabled)
151 {
152 event.events = watch_flags_to_epoll_events (flags);
153 }
154 else
155 {
156 /* We need to add *something* to reserve space in the kernel's data
157 * structures: see socket_set_epoll_disable for more details */
158 event.events = EPOLLET;
159 }
160
161 if (epoll_ctl (self->epfd, EPOLL_CTL_ADD, fd, &event) == 0)
162 return TRUE;
163
164 /* Anything except ENOMEM, ENOSPC means we have an internal error. */
165 err = errno;
166 switch (err)
167 {
168 case ENOMEM:
169 case ENOSPC:
170 /* be silent: this is basically OOM, which our callers are expected
171 * to cope with */
172 break;
173
174 case EBADF:
175 _dbus_warn ("Bad fd %d", fd);
176 break;
177
178 case EEXIST:
179 _dbus_warn ("fd %d added and then added again", fd);
180 break;
181
182 default:
183 _dbus_warn ("Misc error when trying to watch fd %d: %s", fd,
184 strerror (err));
185 break;
186 }
187
188 return FALSE;
189}
190
191 static void
192socket_set_epoll_enable (DBusPollableSet *set,
193 DBusPollable fd,
194 unsigned int flags)
195{
196 DBusPollableSetEpoll *self = socket_set_epoll_cast (set);
197 struct epoll_event event;
198 int err;
199
200 _DBUS_ZERO (event);
201 event.data.fd = fd;
202 event.events = watch_flags_to_epoll_events (flags);
203
204 if (epoll_ctl (self->epfd, EPOLL_CTL_MOD, fd, &event) == 0)
205 return;
206
207 err = errno;
208
209 /* Enabling a file descriptor isn't allowed to fail, even for OOM, so we
210 * do our best to avoid all of these. */
211 switch (err)
212 {
213 case EBADF:
214 _dbus_warn ("Bad fd %d", fd);
215 break;
216
217 case ENOENT:
218 _dbus_warn ("fd %d enabled before it was added", fd);
219 break;
220
221 case ENOMEM:
222 _dbus_warn ("Insufficient memory to change watch for fd %d", fd);
223 break;
224
225 default:
226 _dbus_warn ("Misc error when trying to watch fd %d: %s", fd,
227 strerror (err));
228 break;
229 }
230}
231
232 static void
233socket_set_epoll_disable (DBusPollableSet *set,
234 DBusPollable fd)
235{
236 DBusPollableSetEpoll *self = socket_set_epoll_cast (set);
237 struct epoll_event event;
238 int err;
239
240 /* The naive thing to do would be EPOLL_CTL_DEL, but that'll probably
241 * free resources in the kernel. When we come to do socket_set_epoll_enable,
242 * there might not be enough resources to bring it back!
243 *
244 * The next idea you might have is to set the flags to 0. However, events
245 * always trigger on EPOLLERR and EPOLLHUP, even if libdbus isn't actually
246 * delivering them to a DBusWatch. Because epoll is level-triggered by
247 * default, we'll busy-loop on an unhandled error or hangup; not good.
248 *
249 * So, let's set it to be edge-triggered: then the worst case is that
250 * we return from poll immediately on one iteration, ignore it because no
251 * watch is enabled, then go back to normal. When we re-enable a watch
252 * we'll switch back to level-triggered and be notified again (verified to
253 * work on 2.6.32). Compile this file with -DTEST_BEHAVIOUR_OF_EPOLLET for
254 * test code.
255 */
256 _DBUS_ZERO (event);
257 event.data.fd = fd;
258 event.events = EPOLLET;
259
260 if (epoll_ctl (self->epfd, EPOLL_CTL_MOD, fd, &event) == 0)
261 return;
262
263 err = errno;
264 _dbus_warn ("Error when trying to watch fd %d: %s", fd,
265 strerror (err));
266}
267
268 static void
269socket_set_epoll_remove (DBusPollableSet *set,
270 DBusPollable fd)
271{
272 DBusPollableSetEpoll *self = socket_set_epoll_cast (set);
273 int err;
274 /* Kernels < 2.6.9 require a non-NULL struct pointer, even though its
275 * contents are ignored */
276 struct epoll_event dummy;
277 _DBUS_ZERO (dummy);
278
279 if (epoll_ctl (self->epfd, EPOLL_CTL_DEL, fd, &dummy) == 0)
280 return;
281
282 err = errno;
283 _dbus_warn ("Error when trying to remove fd %d: %s", fd, strerror (err));
284}
285
286/* Optimally, this should be the same as in DBusLoop: we use it to translate
287 * between struct epoll_event and DBusSocketEvent without allocating heap
288 * memory. */
289#define N_STACK_DESCRIPTORS 64
290
291 static int
292socket_set_epoll_poll (DBusPollableSet *set,
293 DBusPollableEvent *revents,
294 int max_events,
295 int timeout_ms)
296{
297 DBusPollableSetEpoll *self = socket_set_epoll_cast (set);
298 struct epoll_event events[N_STACK_DESCRIPTORS];
299 int n_ready;
300 int i;
301
302 _dbus_assert (max_events > 0);
303
304 n_ready = epoll_wait (self->epfd, events,
305 MIN (_DBUS_N_ELEMENTS (events), max_events),
306 timeout_ms);
307
308 if (n_ready <= 0)
309 return n_ready;
310
311 for (i = 0; i < n_ready; i++)
312 {
313 revents[i].fd = events[i].data.fd;
314 revents[i].flags = epoll_events_to_watch_flags (events[i].events);
315 }
316
317 return n_ready;
318}
319
320DBusPollableSetClass _dbus_pollable_set_epoll_class = {
321 socket_set_epoll_free,
322 socket_set_epoll_add,
323 socket_set_epoll_remove,
324 socket_set_epoll_enable,
325 socket_set_epoll_disable,
326 socket_set_epoll_poll
327};
328
329#ifdef TEST_BEHAVIOUR_OF_EPOLLET
330/* usage: cat /dev/null | ./epoll
331 *
332 * desired output:
333 * ctl ADD: 0
334 * wait for HUP, edge-triggered: 1
335 * wait for HUP again: 0
336 * ctl MOD: 0
337 * wait for HUP: 1
338 */
339
340#include <sys/epoll.h>
341
342#include <stdio.h>
343
344 int
345main (void)
346{
347 struct epoll_event input;
348 struct epoll_event output;
349 int epfd = epoll_create1 (EPOLL_CLOEXEC);
350 int fd = 0; /* stdin */
351 int ret;
352
353 _DBUS_ZERO (input);
354
355 input.events = EPOLLHUP | EPOLLET;
356 ret = epoll_ctl (epfd, EPOLL_CTL_ADD, fd, &input);
357 printf ("ctl ADD: %d\n", ret);
358
359 ret = epoll_wait (epfd, &output, 1, -1);
360 printf ("wait for HUP, edge-triggered: %d\n", ret);
361
362 ret = epoll_wait (epfd, &output, 1, 1);
363 printf ("wait for HUP again: %d\n", ret);
364
365 input.events = EPOLLHUP;
366 ret = epoll_ctl (epfd, EPOLL_CTL_MOD, fd, &input);
367 printf ("ctl MOD: %d\n", ret);
368
369 ret = epoll_wait (epfd, &output, 1, -1);
370 printf ("wait for HUP: %d\n", ret);
371
372 return 0;
373}
374
375#endif /* TEST_BEHAVIOUR_OF_EPOLLET */
376
377#endif /* !DOXYGEN_SHOULD_SKIP_THIS */
DBUS_WATCH_READABLE
@ DBUS_WATCH_READABLE
As in POLLIN.
Definition dbus-connection.h:61
DBUS_WATCH_WRITABLE
@ DBUS_WATCH_WRITABLE
As in POLLOUT.
Definition dbus-connection.h:62
DBUS_WATCH_HANGUP
@ DBUS_WATCH_HANGUP
As in POLLHUP (can't watch for it, but can be present in current state passed to dbus_watch_handle())...
Definition dbus-connection.h:68
DBUS_WATCH_ERROR
@ DBUS_WATCH_ERROR
As in POLLERR (can't watch for this, but can be present in current state passed to dbus_watch_handle(...
Definition dbus-connection.h:63
_dbus_assert
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
Definition dbus-internals.h:153
_dbus_warn
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
Definition dbus-internals.c:238
_DBUS_N_ELEMENTS
#define _DBUS_N_ELEMENTS(array)
Computes the number of elements in a fixed-size array using sizeof().
Definition dbus-internals.h:189
_DBUS_ZERO
#define _DBUS_ZERO(object)
Sets all bits in an object to zero.
Definition dbus-internals.h:194
NULL
#define NULL
A null pointer, defined appropriately for C or C++.
Definition dbus-macros.h:49
TRUE
#define TRUE
Expands to "1".
Definition dbus-macros.h:39
FALSE
#define FALSE
Expands to "0".
Definition dbus-macros.h:42
dbus_free
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition dbus-memory.c:692
dbus_new0
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition dbus-memory.h:58