1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-socket-set-epoll.c - a socket set implemented via Linux epoll(4)
4 * Copyright © 2011 Nokia Corporation
6 * Licensed under the Academic Free License version 2.1
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.
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.
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,
26 #include "dbus-socket-set.h"
28 #include <dbus/dbus-internals.h>
29 #include <dbus/dbus-sysdeps.h>
32 # error This file is for Linux epoll(4)
37 #include <sys/epoll.h>
40 #ifndef DOXYGEN_SHOULD_SKIP_THIS
47 static inline DBusSocketSetEpoll *
48 socket_set_epoll_cast (DBusSocketSet *set)
50 _dbus_assert (set->cls == &_dbus_socket_set_epoll_class);
51 return (DBusSocketSetEpoll *) set;
54 /* this is safe to call on a partially-allocated socket set */
56 socket_set_epoll_free (DBusSocketSet *set)
58 DBusSocketSetEpoll *self = socket_set_epoll_cast (set);
70 _dbus_socket_set_epoll_new (void)
72 DBusSocketSetEpoll *self;
74 self = dbus_new0 (DBusSocketSetEpoll, 1);
79 self->parent.cls = &_dbus_socket_set_epoll_class;
81 self->epfd = epoll_create1 (EPOLL_CLOEXEC);
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);
92 flags = fcntl (self->epfd, F_GETFD, 0);
95 fcntl (self->epfd, F_SETFD, flags | FD_CLOEXEC);
100 socket_set_epoll_free ((DBusSocketSet *) self);
104 return (DBusSocketSet *) self;
108 watch_flags_to_epoll_events (unsigned int flags)
112 if (flags & DBUS_WATCH_READABLE)
114 if (flags & DBUS_WATCH_WRITABLE)
121 epoll_events_to_watch_flags (uint32_t events)
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;
138 socket_set_epoll_add (DBusSocketSet *set,
143 DBusSocketSetEpoll *self = socket_set_epoll_cast (set);
144 struct epoll_event event;
152 event.events = watch_flags_to_epoll_events (flags);
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;
161 if (epoll_ctl (self->epfd, EPOLL_CTL_ADD, fd, &event) == 0)
164 /* Anything except ENOMEM, ENOSPC means we have an internal error. */
170 /* be silent: this is basically OOM, which our callers are expected
175 _dbus_warn ("Bad fd %d", fd);
179 _dbus_warn ("fd %d added and then added again", fd);
183 _dbus_warn ("Misc error when trying to watch fd %d: %s", fd,
192 socket_set_epoll_enable (DBusSocketSet *set,
196 DBusSocketSetEpoll *self = socket_set_epoll_cast (set);
197 struct epoll_event event;
202 event.events = watch_flags_to_epoll_events (flags);
204 if (epoll_ctl (self->epfd, EPOLL_CTL_MOD, fd, &event) == 0)
209 /* Enabling a file descriptor isn't allowed to fail, even for OOM, so we
210 * do our best to avoid all of these. */
214 _dbus_warn ("Bad fd %d", fd);
218 _dbus_warn ("fd %d enabled before it was added", fd);
222 _dbus_warn ("Insufficient memory to change watch for fd %d", fd);
226 _dbus_warn ("Misc error when trying to watch fd %d: %s", fd,
233 socket_set_epoll_disable (DBusSocketSet *set,
236 DBusSocketSetEpoll *self = socket_set_epoll_cast (set);
237 struct epoll_event event;
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!
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.
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
258 event.events = EPOLLET;
260 if (epoll_ctl (self->epfd, EPOLL_CTL_MOD, fd, &event) == 0)
264 _dbus_warn ("Error when trying to watch fd %d: %s", fd,
269 socket_set_epoll_remove (DBusSocketSet *set,
272 DBusSocketSetEpoll *self = socket_set_epoll_cast (set);
274 /* Kernels < 2.6.9 require a non-NULL struct pointer, even though its
275 * contents are ignored */
276 struct epoll_event dummy;
279 if (epoll_ctl (self->epfd, EPOLL_CTL_DEL, fd, &dummy) == 0)
283 _dbus_warn ("Error when trying to remove fd %d: %s", fd, strerror (err));
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
289 #define N_STACK_DESCRIPTORS 64
292 socket_set_epoll_poll (DBusSocketSet *set,
293 DBusSocketEvent *revents,
297 DBusSocketSetEpoll *self = socket_set_epoll_cast (set);
298 struct epoll_event events[N_STACK_DESCRIPTORS];
302 _dbus_assert (max_events > 0);
304 n_ready = epoll_wait (self->epfd, events,
305 MIN (_DBUS_N_ELEMENTS (events), max_events),
311 for (i = 0; i < n_ready; i++)
313 revents[i].fd = events[i].data.fd;
314 revents[i].flags = epoll_events_to_watch_flags (events[i].events);
320 DBusSocketSetClass _dbus_socket_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
329 #ifdef TEST_BEHAVIOUR_OF_EPOLLET
330 /* usage: cat /dev/null | ./epoll
334 * wait for HUP, edge-triggered: 1
335 * wait for HUP again: 0
340 #include <sys/epoll.h>
347 struct epoll_event input;
348 struct epoll_event output;
349 int epfd = epoll_create1 (EPOLL_CLOEXEC);
350 int fd = 0; /* stdin */
355 input.events = EPOLLHUP | EPOLLET;
356 ret = epoll_ctl (epfd, EPOLL_CTL_ADD, fd, &input);
357 printf ("ctl ADD: %d\n", ret);
359 ret = epoll_wait (epfd, &output, 1, -1);
360 printf ("wait for HUP, edge-triggered: %d\n", ret);
362 ret = epoll_wait (epfd, &output, 1, 1);
363 printf ("wait for HUP again: %d\n", ret);
365 input.events = EPOLLHUP;
366 ret = epoll_ctl (epfd, EPOLL_CTL_MOD, fd, &input);
367 printf ("ctl MOD: %d\n", ret);
369 ret = epoll_wait (epfd, &output, 1, -1);
370 printf ("wait for HUP: %d\n", ret);
375 #endif /* TEST_BEHAVIOUR_OF_EPOLLET */
377 #endif /* !DOXYGEN_SHOULD_SKIP_THIS */