wlt: fix shl_hook API changes
[platform/upstream/kmscon.git] / src / eloop.c
1 /*
2  * Event Loop
3  *
4  * Copyright (c) 2011-2012 David Herrmann <dh.herrmann@googlemail.com>
5  * Copyright (c) 2011 University of Tuebingen
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files
9  * (the "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included
16  * in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  */
26
27 /**
28  * SECTION:eloop
29  * @short_description: Event loop
30  * @include: eloop.h
31  *
32  * The event loop allows to register event sources and poll them for events.
33  * When an event occurs, the user-supplied callback is called.
34  *
35  * The event-loop allows the callbacks to modify _any_ data they want. They can
36  * remove themself or other sources from the event loop even in a callback.
37  * This, however, means that recursive dispatch calls are not supported to
38  * increase performance and avoid internal dispatch-stacks.
39  *
40  * Sources can be one of:
41  *  - File descriptors: An fd that is watched for readable/writeable events
42  *  - Timers: An event that occurs after a relative timeout
43  *  - Counters: An event that occurs when the counter is non-zero
44  *  - Signals: An event that occurs when a signal is caught
45  *  - Idle: An event that occurs when nothing else is done
46  *  - Eloop: An event loop itself can be a source of another event loop
47  *
48  * A source can be registered for a single event-loop only! You cannot add it
49  * to multiple event loops simultaneously. Also all provided sources are based
50  * on the file-descriptor source so it is guaranteed that you can get a
51  * file-desciptor for every source-type. This is not exported via the public
52  * API, but you can get the epoll-fd which is basically a selectable FD summary
53  * of all event sources.
54  *
55  * For instance, if you're developing a library, you can use the eloop library
56  * internally and you will have a full event-loop implementation inside of a
57  * library without any side-effects. You simply export the epoll-fd of the
58  * eloop-object via your public API and the outside users think you only use a
59  * single file-descriptor. They include this FD in their own application event
60  * loop which will then dispatch the messages to your library. Internally, you
61  * simply forward this dispatching to ev_eloop_dispatch() which then calls all
62  * your internal callbacks.
63  * That is, you have an event loop inside your library without requiring the
64  * outside-user to use the same event loop. You also have no global state or
65  * thread-bound event-loops like the Qt/Gtk event loops. So you have full
66  * access to the whole event loop without any side-effects.
67  *
68  *
69  * The whole eloop library does not use any global data. Therefore, it is fully
70  * re-entrant and no synchronization needed. However, a single object is not
71  * thread-safe. This means, if you access a single eloop object or registered
72  * sources on this eloop object in two different threads, you need to
73  * synchronize them. Furthermore, all callbacks are called from the thread that
74  * calls ev_eloop_dispatch() or ev_eloop_run().
75  * This guarantees that you have full control over the eloop but that you also
76  * have to implement additional functionality like thread-affinity yourself
77  * (obviously, only if you need it).
78  *
79  * The philosophy behind this library is that a proper application needs only a
80  * single thread that uses an event loop. Multiple threads should be used to do
81  * calculations, but not to avoid learning how to do non-blocking I/O!
82  * Therefore, only the application threads needs an event-loop, all other
83  * threads only perform calculation and return the data to the main thread.
84  * However, the library does not enforce this design-choice. On the contrary,
85  * it supports all other types of application-designs, too. But as it is
86  * optimized for performance, other application-designs may need to add further
87  * functionality (like thread-affinity) by themself as it would slow down the
88  * event loop if it was natively implemented.
89  *
90  *
91  * To get started simply create an eloop object with ev_eloop_new(). All
92  * functions return 0 on success and a negative error code like -EFAULT on
93  * failure. -EINVAL is returned if invalid parameters were passed.
94  * Every object can be ref-counted. *_ref() increases the reference-count and
95  * *_unref() decreases it. *_unref() also destroys the object if the ref-count
96  * drops to 0.
97  * To create new objects you call *_new(). It stores a pointer to the new
98  * object in the location you passed as parameter. Nearly all structues are
99  * opaque, that is, you cannot access member fields directly. This guarantees
100  * ABI stability.
101  *
102  * You can create sources with ev_fd_new(), ev_timer_new(), ... and you can add
103  * them to you eloop with ev_eloop_add_fd() or ev_eloop_add_timer(), ...
104  * After they are added you can call ev_eloop_run() to run this eloop for the
105  * given time. If you pass -1 as timeout, it runs until some callback calls
106  * ev_eloop_exit() on this eloop.
107  * You can perform _any_ operations on an eloop object inside of callbacks. You
108  * can add new sources, remove sources, destroy sources, modify sources. You
109  * also do all this on the currently active source.
110  *
111  * All objects are enabled by default. You can disable them with *_disable()
112  * and re-enable them with *_enable(). Only when enabled, they are added to the
113  * dispatcher and callbacks are called.
114  *
115  * Two sources are different for performance reasons:
116  *   Idle sources: Idle sources can be registered with
117  *   ev_eloop_register_idle_cb() and unregistered with
118  *   ev_eloop_unregister_idle_cb(). They internally share a single
119  *   file-descriptor to make them faster so you cannot get the same access as
120  *   to other event sources (you cannot enable/disable them or similar).
121  *   Idle sources are called every-time ev_eloop_dispatch() is called. That is,
122  *   as long as an idle-source is registered, the event-loop will not go to
123  *   sleep!
124  *
125  *   Signal sources: Talking about the API they are very similar to
126  *   idle-sources. They same restrictions apply, however, their type is very
127  *   different. A signal-callback is called when the specified signal is
128  *   received. They are not called in signal-context! But rather called in the
129  *   same context as every other source. They are implemented with
130  *   linux-signalfd.
131  *   You can register multiple callbacks for the same signal and all callbacks
132  *   will be called (compared to plain signalfd where only one fd gets the
133  *   signal). This is done internally by sharing the signalfd.
134  *   However, there is one restriction: You cannot share a signalfd between
135  *   multiple eloop-instances. That is, if you register a callback for the same
136  *   signal on two different eloop-instances (which are connected themself),
137  *   then only one eloop-instance will fire the signal source. This is a
138  *   restriction of signalfd that cannot be overcome. However, it is very
139  *   uncommon to register multiple callbacks for a signal so this shouldn't
140  *   affect common application use-cases.
141  *   Also note that if you register a callback for SIGCHLD then the eloop-
142  *   object will automatically reap all pending zombies _after_ your callback
143  *   has been called. So if you need to check for them, then check for all of
144  *   them in the callback. After you return, they will be gone.
145  *   When adding a signal handler the signal is automatically added to the
146  *   currently blocked signals. It is not removed when dropping the
147  *   signal-source, though.
148  *
149  * Eloop uses several system calls which may fail. All errors (including memory
150  * allocation errors via -ENOMEM) are forwarded to the caller, however, it is
151  * often preferable to have a more detailed logging message. Therefore, eloop
152  * takes a loggin-function as argument for each object. Pass NULL if you are
153  * not interested in logging. This will disable logging entirely.
154  * Otherwise, pass in a callback from your application. This callback will be
155  * called when a message is to be logged. The function may be called under any
156  * circumstances (out-of-memory, etc...) and should always behave well.
157  * Nothing is ever logged except through this callback.
158  */
159
160 #include <errno.h>
161 #include <inttypes.h>
162 #include <pthread.h>
163 #include <signal.h>
164 #include <stdbool.h>
165 #include <stdlib.h>
166 #include <string.h>
167 #include <sys/epoll.h>
168 #include <sys/eventfd.h>
169 #include <sys/signalfd.h>
170 #include <sys/time.h>
171 #include <sys/timerfd.h>
172 #include <sys/wait.h>
173 #include <time.h>
174 #include <unistd.h>
175 #include "eloop.h"
176 #include "shl_dlist.h"
177 #include "shl_hook.h"
178 #include "shl_llog.h"
179
180 #define LLOG_SUBSYSTEM "eloop"
181
182 /**
183  * ev_eloop:
184  * @ref: refcnt of this object
185  * @llog: llog log function
186  * @efd: The epoll file descriptor.
187  * @fd: Event source around \efd so you can nest event loops
188  * @cnt: Counter source used for idle events
189  * @sig_list: Shared signal sources
190  * @idlers: List of idle sources
191  * @cur_fds: Current dispatch array of fds
192  * @cur_fds_cnt: current length of \cur_fds
193  * @cur_fds_size: absolute size of \cur_fds
194  * @exit: true if we should exit the main loop
195  *
196  * An event loop is an object where you can register event sources. If you then
197  * sleep on the event loop, you will be woken up if a single event source is
198  * firing up. An event loop itself is an event source so you can nest them.
199  */
200 struct ev_eloop {
201         unsigned long ref;
202         llog_submit_t llog;
203         int efd;
204         struct ev_fd *fd;
205         int idle_fd;
206
207         struct shl_dlist sig_list;
208         struct shl_hook *chlds;
209         struct shl_hook *idlers;
210         struct shl_hook *pres;
211         struct shl_hook *posts;
212
213         bool dispatching;
214         struct epoll_event *cur_fds;
215         size_t cur_fds_cnt;
216         size_t cur_fds_size;
217         bool exit;
218 };
219
220 /**
221  * ev_fd:
222  * @ref: refcnt for object
223  * @llog: llog log function
224  * @fd: the actual file desciptor
225  * @mask: the event mask for this fd (EV_READABLE, EV_WRITABLE, ...)
226  * @cb: the user callback
227  * @data: the user data
228  * @enabled: true if the object is currently enabled
229  * @loop: NULL or pointer to eloop if bound
230  *
231  * File descriptors are the most basic event source. Internally, they are used
232  * to implement all other kinds of event sources.
233  */
234 struct ev_fd {
235         unsigned long ref;
236         llog_submit_t llog;
237         int fd;
238         int mask;
239         ev_fd_cb cb;
240         void *data;
241
242         bool enabled;
243         struct ev_eloop *loop;
244 };
245
246 /**
247  * ev_timer:
248  * @ref: refcnt of this object
249  * @llog: llog log function
250  * @cb: user callback
251  * @data: user data
252  * @fd: the timerfd file desciptor
253  * @efd: fd-source for @fd
254  *
255  * Based on timerfd this allows firing events based on relative timeouts.
256  */
257 struct ev_timer {
258         unsigned long ref;
259         llog_submit_t llog;
260         ev_timer_cb cb;
261         void *data;
262
263         int fd;
264         struct ev_fd *efd;
265 };
266
267 /**
268  * ev_counter:
269  * @ref: refcnt of counter object
270  * @llog: llog log function
271  * @cb: user callback
272  * @data: user data
273  * @fd: eventfd file desciptor
274  * @efd: fd-source for @fd
275  *
276  * Counter sources fire if they are non-zero. They are based on the eventfd
277  * syscall in linux.
278  */
279 struct ev_counter {
280         unsigned long ref;
281         llog_submit_t llog;
282         ev_counter_cb cb;
283         void *data;
284
285         int fd;
286         struct ev_fd *efd;
287 };
288
289 /**
290  * ev_signal_shared:
291  * @list: list integration into ev_eloop object
292  * @fd: the signalfd file desciptor for this signal
293  * @signum: the actual signal number
294  * @hook: list of registered user callbacks for this signal
295  *
296  * A shared signal allows multiple listeners for the same signal. All listeners
297  * are called if the signal is catched.
298  */
299 struct ev_signal_shared {
300         struct shl_dlist list;
301
302         struct ev_fd *fd;
303         int signum;
304         struct shl_hook *hook;
305 };
306
307 /*
308  * Shared signals
309  * signalfd allows us to conveniently listen for incoming signals. However, if
310  * multiple signalfds are registered for the same signal, then only one of them
311  * will get signaled. To avoid this restriction, we provide shared signals.
312  * That means, the user can register for a signal and if no other user is
313  * registered for this signal, yet, we create a new shared signal. Otherwise,
314  * we add the user to the existing shared signals.
315  * If the signal is catched, we simply call all users that are registered for
316  * this signal.
317  * To avoid side-effects, we automatically block all signals for the current
318  * thread when a signalfd is created. We never unblock the signal. However,
319  * most modern linux user-space programs avoid signal handlers, anyway, so you
320  * can use signalfd only.
321  */
322
323 static void sig_child(struct ev_eloop *loop, struct signalfd_siginfo *info,
324                       void *data)
325 {
326         pid_t pid;
327         int status;
328         struct ev_child_data d;
329
330         while (1) {
331                 pid = waitpid(-1, &status, WNOHANG);
332                 if (pid == -1) {
333                         if (errno != ECHILD)
334                                 llog_warn(loop, "cannot wait on child: %m");
335                         break;
336                 } else if (pid == 0) {
337                         break;
338                 } else if (WIFEXITED(status)) {
339                         if (WEXITSTATUS(status) != 0)
340                                 llog_debug(loop, "child %d exited with status %d",
341                                            pid, WEXITSTATUS(status));
342                         else
343                                 llog_debug(loop, "child %d exited successfully",
344                                            pid);
345                 } else if (WIFSIGNALED(status)) {
346                         llog_debug(loop, "child %d exited by signal %d", pid,
347                                    WTERMSIG(status));
348                 }
349
350                 d.pid = pid;
351                 d.status = status;
352                 shl_hook_call(loop->chlds, loop, &d);
353         }
354 }
355
356 static void shared_signal_cb(struct ev_fd *fd, int mask, void *data)
357 {
358         struct ev_signal_shared *sig = data;
359         struct signalfd_siginfo info;
360         int len;
361
362         if (mask & EV_READABLE) {
363                 len = read(fd->fd, &info, sizeof(info));
364                 if (len != sizeof(info))
365                         llog_warn(fd, "cannot read signalfd (%d): %m", errno);
366                 else
367                         shl_hook_call(sig->hook, sig->fd->loop, &info);
368         } else if (mask & (EV_HUP | EV_ERR)) {
369                 llog_warn(fd, "HUP/ERR on signal source");
370         }
371 }
372
373 /**
374  * signal_new:
375  * @out: Shared signal storage where the new object is stored
376  * @loop: The event loop where this shared signal is registered
377  * @signum: Signal number that this shared signal is for
378  *
379  * This creates a new shared signal and links it into the list of shared
380  * signals in @loop. It automatically adds @signum to the signal mask of the
381  * current thread so the signal is blocked.
382  *
383  * Returns: 0 on success, otherwise negative error code
384  */
385 static int signal_new(struct ev_signal_shared **out, struct ev_eloop *loop,
386                         int signum)
387 {
388         sigset_t mask;
389         int ret, fd;
390         struct ev_signal_shared *sig;
391
392         if (signum < 0)
393                 return llog_EINVAL(loop);
394
395         sig = malloc(sizeof(*sig));
396         if (!sig)
397                 return llog_ENOMEM(loop);
398         memset(sig, 0, sizeof(*sig));
399         sig->signum = signum;
400
401         ret = shl_hook_new(&sig->hook);
402         if (ret)
403                 goto err_free;
404
405         sigemptyset(&mask);
406         sigaddset(&mask, signum);
407
408         fd = signalfd(-1, &mask, SFD_CLOEXEC | SFD_NONBLOCK);
409         if (fd < 0) {
410                 ret = -errno;
411                 llog_error(loop, "cannot created signalfd");
412                 goto err_hook;
413         }
414
415         ret = ev_eloop_new_fd(loop, &sig->fd, fd, EV_READABLE,
416                                 shared_signal_cb, sig);
417         if (ret)
418                 goto err_sig;
419
420         pthread_sigmask(SIG_BLOCK, &mask, NULL);
421         shl_dlist_link(&loop->sig_list, &sig->list);
422
423         *out = sig;
424         return 0;
425
426 err_sig:
427         close(fd);
428 err_hook:
429         shl_hook_free(sig->hook);
430 err_free:
431         free(sig);
432         return ret;
433 }
434
435 /**
436  * signal_free:
437  * @sig: The shared signal to be freed
438  *
439  * This unlinks the given shared signal from the event-loop where it was
440  * registered and destroys it. This does _not_ unblock the signal number that it
441  * was associated to. If you want this, you need to do this manually with
442  * pthread_sigmask().
443  */
444 static void signal_free(struct ev_signal_shared *sig)
445 {
446         int fd;
447
448         if (!sig)
449                 return;
450
451         shl_dlist_unlink(&sig->list);
452         fd = sig->fd->fd;
453         ev_eloop_rm_fd(sig->fd);
454         close(fd);
455         shl_hook_free(sig->hook);
456         free(sig);
457         /*
458          * We do not unblock the signal here as there may be other subsystems
459          * which blocked this signal so we do not want to interfere. If you
460          * need a clean sigmask then do it yourself.
461          */
462 }
463
464 /*
465  * Eloop mainloop
466  * The main eloop object is responsible for correctly dispatching all events.
467  * You can register fd, idle or signal sources with it. All other kinds of
468  * sources are based on these. In fact, event idle and signal sources are based
469  * on fd sources.
470  * As special feature, you can retrieve an fd of an eloop object, too, and pass
471  * it to your own event loop. If this fd is readable, then call
472  * ev_eloop_dispatch() to make this loop dispatch all pending events.
473  *
474  * There is one restriction when nesting eloops, though. You cannot share
475  * signals across eloop boundaries. That is, if you have registered for shared
476  * signals in two eloops for the _same_ signal, then only one eloop will
477  * receive the signal (and this is pretty random).
478  * However, such a setup is most often broken in design and hence should never
479  * occur. Even shared signals are quite rare.
480  * Anyway, you must take this into account when nesting eloops.
481  *
482  * For the curious reader: We implement idle sources with counter sources. That
483  * is, whenever there is an idle source we increase the counter source. Hence,
484  * the next dispatch call will call the counter source and this will call all
485  * registered idle source. If the idle sources do not unregister them, then we
486  * directly increase the counter again and the next dispatch round will call
487  * all idle sources again. This, however, has the side-effect that idle sources
488  * are _not_ called before other fd events but are rather mixed in between.
489  */
490
491 static void eloop_event(struct ev_fd *fd, int mask, void *data)
492 {
493         struct ev_eloop *eloop = data;
494
495         if (mask & EV_READABLE)
496                 ev_eloop_dispatch(eloop, 0);
497         if (mask & (EV_HUP | EV_ERR))
498                 llog_warn(eloop, "HUP/ERR on eloop source");
499 }
500
501 static int write_eventfd(llog_submit_t llog, int fd, uint64_t val)
502 {
503         int ret;
504
505         if (!val)
506                 return llog_dEINVAL(llog);
507
508         if (val == 0xffffffffffffffffULL) {
509                 llog_dwarning(llog, "increasing counter with invalid value %" PRIu64, val);
510                 return -EINVAL;;
511         }
512
513         ret = write(fd, &val, sizeof(val));
514         if (ret < 0) {
515                 if (errno == EAGAIN)
516                         llog_dwarning(llog, "eventfd overflow while writing %" PRIu64, val);
517                 else
518                         llog_dwarning(llog, "eventfd write error (%d): %m", errno);
519                 return -EFAULT;
520         } else if (ret != sizeof(val)) {
521                 llog_dwarning(llog, "wrote %d bytes instead of 8 to eventdfd", ret);
522                 return -EFAULT;
523         }
524
525         return 0;
526 }
527
528 static void eloop_idle_event(struct ev_eloop *loop, unsigned int mask)
529 {
530         int ret;
531         uint64_t val;
532
533         if (mask & (EV_HUP | EV_ERR)) {
534                 llog_warning(loop, "HUP/ERR on eventfd");
535                 goto err_out;
536         }
537
538         if (!(mask & EV_READABLE))
539                 return;
540
541         ret = read(loop->idle_fd, &val, sizeof(val));
542         if (ret < 0) {
543                 if (errno != EAGAIN) {
544                         llog_warning(loop, "reading eventfd failed (%d): %m",
545                                      errno);
546                         goto err_out;
547                 }
548         } else if (ret == 0) {
549                 llog_warning(loop, "EOF on eventfd");
550                 goto err_out;
551         } else if (ret != sizeof(val)) {
552                 llog_warning(loop, "read %d bytes instead of 8 on eventfd",
553                              ret);
554                 goto err_out;
555         } else if (val > 0) {
556                 shl_hook_call(loop->idlers, loop, NULL);
557                 if (shl_hook_num(loop->idlers) > 0)
558                         write_eventfd(loop->llog, loop->idle_fd, 1);
559         }
560
561         return;
562
563 err_out:
564         ret = epoll_ctl(loop->efd, EPOLL_CTL_DEL, loop->idle_fd, NULL);
565         if (ret)
566                 llog_warning(loop, "cannot remove fd %d from epollset (%d): %m",
567                              loop->idle_fd, errno);
568 }
569
570 /**
571  * ev_eloop_new:
572  * @out: Storage for the result
573  * @log: logging function or NULL
574  *
575  * This creates a new event-loop with ref-count 1. The new event loop is stored
576  * in @out and has no registered events.
577  *
578  * Returns: 0 on success, otherwise negative error code
579  */
580 int ev_eloop_new(struct ev_eloop **out, ev_log_t log)
581 {
582         struct ev_eloop *loop;
583         int ret;
584         struct epoll_event ep;
585
586         if (!out)
587                 return llog_dEINVAL(log);
588
589         loop = malloc(sizeof(*loop));
590         if (!loop)
591                 return llog_dENOMEM(log);
592
593         memset(loop, 0, sizeof(*loop));
594         loop->ref = 1;
595         loop->llog = log;
596         shl_dlist_init(&loop->sig_list);
597
598         loop->cur_fds_size = 32;
599         loop->cur_fds = malloc(sizeof(struct epoll_event) *
600                                loop->cur_fds_size);
601         if (!loop->cur_fds) {
602                 ret = llog_ENOMEM(loop);
603                 goto err_free;
604         }
605
606         ret = shl_hook_new(&loop->chlds);
607         if (ret)
608                 goto err_fds;
609
610         ret = shl_hook_new(&loop->idlers);
611         if (ret)
612                 goto err_childs;
613
614         ret = shl_hook_new(&loop->pres);
615         if (ret)
616                 goto err_idlers;
617
618         ret = shl_hook_new(&loop->posts);
619         if (ret)
620                 goto err_pres;
621
622         loop->efd = epoll_create1(EPOLL_CLOEXEC);
623         if (loop->efd < 0) {
624                 ret = -errno;
625                 llog_error(loop, "cannot create epoll-fd");
626                 goto err_posts;
627         }
628
629         ret = ev_fd_new(&loop->fd, loop->efd, EV_READABLE, eloop_event, loop,
630                         loop->llog);
631         if (ret)
632                 goto err_close;
633
634         loop->idle_fd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
635         if (loop->idle_fd < 0) {
636                 llog_error(loop, "cannot create eventfd (%d): %m", errno);
637                 ret = -EFAULT;
638                 goto err_fd;
639         }
640
641         memset(&ep, 0, sizeof(ep));
642         ep.events |= EPOLLIN;
643         ep.data.ptr = loop;
644
645         ret = epoll_ctl(loop->efd, EPOLL_CTL_ADD, loop->idle_fd, &ep);
646         if (ret) {
647                 llog_warning(loop, "cannot add fd %d to epoll set (%d): %m",
648                              loop->idle_fd, errno);
649                 ret = -EFAULT;
650                 goto err_idle_fd;
651         }
652
653         llog_debug(loop, "new eloop object %p", loop);
654         *out = loop;
655         return 0;
656
657 err_idle_fd:
658         close(loop->idle_fd);
659 err_fd:
660         ev_fd_unref(loop->fd);
661 err_close:
662         close(loop->efd);
663 err_posts:
664         shl_hook_free(loop->posts);
665 err_pres:
666         shl_hook_free(loop->pres);
667 err_idlers:
668         shl_hook_free(loop->idlers);
669 err_childs:
670         shl_hook_free(loop->chlds);
671 err_fds:
672         free(loop->cur_fds);
673 err_free:
674         free(loop);
675         return ret;
676 }
677
678 /**
679  * ev_eloop_ref:
680  * @loop: Event loop to be modified or NULL
681  *
682  * This increases the ref-count of @loop by 1.
683  */
684 void ev_eloop_ref(struct ev_eloop *loop)
685 {
686         if (!loop)
687                 return;
688
689         ++loop->ref;
690 }
691
692 /**
693  * ev_eloop_unref:
694  * @loop: Event loop to be modified or NULL
695  *
696  * This decreases the ref-count of @loop by 1. If it drops to zero, the event
697  * loop is destroyed. Note that every registered event source takes a ref-count
698  * of the event loop so this ref-count will never drop to zero while there is an
699  * registered event source.
700  */
701 void ev_eloop_unref(struct ev_eloop *loop)
702 {
703         struct ev_signal_shared *sig;
704         int ret;
705
706         if (!loop)
707                 return;
708         if (!loop->ref)
709                 return llog_vEINVAL(loop);
710         if (--loop->ref)
711                 return;
712
713         llog_debug(loop, "free eloop object %p", loop);
714
715         if (shl_hook_num(loop->chlds))
716                 ev_eloop_unregister_signal_cb(loop, SIGCHLD, sig_child, loop);
717
718         while (loop->sig_list.next != &loop->sig_list) {
719                 sig = shl_dlist_entry(loop->sig_list.next,
720                                         struct ev_signal_shared,
721                                         list);
722                 signal_free(sig);
723         }
724
725         ret = epoll_ctl(loop->efd, EPOLL_CTL_DEL, loop->idle_fd, NULL);
726         if (ret)
727                 llog_warning(loop, "cannot remove fd %d from epollset (%d): %m",
728                              loop->idle_fd, errno);
729         close(loop->idle_fd);
730
731         ev_fd_unref(loop->fd);
732         close(loop->efd);
733         shl_hook_free(loop->posts);
734         shl_hook_free(loop->pres);
735         shl_hook_free(loop->idlers);
736         shl_hook_free(loop->chlds);
737         free(loop->cur_fds);
738         free(loop);
739 }
740
741 /**
742  * ev_eloop_flush_fd:
743  * @loop: The event loop where @fd is registered
744  * @fd: The fd to be flushed
745  *
746  * If @loop is currently dispatching events, this will remove all pending events
747  * of @fd from the current event-list.
748  */
749 void ev_eloop_flush_fd(struct ev_eloop *loop, struct ev_fd *fd)
750 {
751         int i;
752
753         if (!loop)
754                 return;
755         if (!fd)
756                 return llog_vEINVAL(loop);
757
758         if (loop->dispatching) {
759                 for (i = 0; i < loop->cur_fds_cnt; ++i) {
760                         if (loop->cur_fds[i].data.ptr == fd)
761                                 loop->cur_fds[i].data.ptr = NULL;
762                 }
763         }
764 }
765
766 static unsigned int convert_mask(uint32_t mask)
767 {
768         unsigned int res = 0;
769
770         if (mask & EPOLLIN)
771                 res |= EV_READABLE;
772         if (mask & EPOLLOUT)
773                 res |= EV_WRITEABLE;
774         if (mask & EPOLLERR)
775                 res |= EV_ERR;
776         if (mask & EPOLLHUP)
777                 res |= EV_HUP;
778
779         return res;
780 }
781
782 /**
783  * ev_eloop_dispatch:
784  * @loop: Event loop to be dispatched
785  * @timeout: Timeout in milliseconds
786  *
787  * This listens on @loop for incoming events and handles all events that
788  * occured. This waits at most @timeout milliseconds until returning. If
789  * @timeout is -1, this waits until the first event arrives. If @timeout is 0,
790  * then this returns directly if no event is currently pending.
791  *
792  * This performs only a single dispatch round. That is, if all sources where
793  * checked for events and there are no more pending events, this will return. If
794  * it handled events and the timeout has not elapsed, this will still return.
795  *
796  * If ev_eloop_exit() was called on @loop, then this will return immediately.
797  *
798  * Returns: 0 on success, otherwise negative error code
799  */
800 int ev_eloop_dispatch(struct ev_eloop *loop, int timeout)
801 {
802         struct epoll_event *ep;
803         struct ev_fd *fd;
804         int i, count, mask, ret;
805
806         if (!loop)
807                 return -EINVAL;
808         if (loop->exit)
809                 return llog_EINVAL(loop);
810         if (loop->dispatching) {
811                 llog_warn(loop, "recursive dispatching not allowed");
812                 return -EOPNOTSUPP;
813         }
814
815         loop->dispatching = true;
816
817         shl_hook_call(loop->pres, loop, NULL);
818
819         count = epoll_wait(loop->efd,
820                            loop->cur_fds,
821                            loop->cur_fds_size,
822                            timeout);
823         if (count < 0) {
824                 if (errno == EINTR) {
825                         ret = 0;
826                         goto out_dispatch;
827                 } else {
828                         llog_warn(loop, "epoll_wait dispatching failed: %m");
829                         ret = -errno;
830                         goto out_dispatch;
831                 }
832         } else if (count > loop->cur_fds_size) {
833                 count = loop->cur_fds_size;
834         }
835
836         ep = loop->cur_fds;
837         loop->cur_fds_cnt = count;
838
839         for (i = 0; i < count; ++i) {
840                 if (ep[i].data.ptr == loop) {
841                         mask = convert_mask(ep[i].events);
842                         eloop_idle_event(loop, mask);
843                 } else {
844                         fd = ep[i].data.ptr;
845                         if (!fd || !fd->cb || !fd->enabled)
846                                 continue;
847
848                         mask = convert_mask(ep[i].events);
849                         fd->cb(fd, mask, fd->data);
850                 }
851         }
852
853         if (count == loop->cur_fds_size) {
854                 ep = realloc(loop->cur_fds, sizeof(struct epoll_event) *
855                              loop->cur_fds_size * 2);
856                 if (!ep) {
857                         llog_warning(loop, "cannot reallocate dispatch cache to size %zu",
858                                     loop->cur_fds_size * 2);
859                 } else {
860                         loop->cur_fds = ep;
861                         loop->cur_fds_size *= 2;
862                 }
863         }
864
865         ret = 0;
866
867 out_dispatch:
868         shl_hook_call(loop->posts, loop, NULL);
869         loop->dispatching = false;
870         return ret;
871 }
872
873 /**
874  * ev_eloop_run:
875  * @loop: The event loop to be run
876  * @timeout: Timeout for this operation
877  *
878  * This is similar to ev_eloop_dispatch() but runs _exactly_ for @timeout
879  * milliseconds. It calls ev_eloop_dispatch() as often as it can until the
880  * timeout has elapsed. If @timeout is -1 this will run until you call
881  * ev_eloop_exit(). If @timeout is 0 this is equal to calling
882  * ev_eloop_dispatch() with a timeout of 0.
883  *
884  * Calling ev_eloop_exit() will always interrupt this function and make it
885  * return.
886  *
887  * Returns: 0 on success, otherwise a negative error code
888  */
889 int ev_eloop_run(struct ev_eloop *loop, int timeout)
890 {
891         int ret;
892         struct timeval tv, start;
893         int64_t off, msec;
894
895         if (!loop)
896                 return -EINVAL;
897         loop->exit = false;
898
899         llog_debug(loop, "run for %d msecs", timeout);
900         gettimeofday(&start, NULL);
901
902         while (!loop->exit) {
903                 ret = ev_eloop_dispatch(loop, timeout);
904                 if (ret)
905                         return ret;
906
907                 if (!timeout) {
908                         break;
909                 } else if (timeout > 0) {
910                         gettimeofday(&tv, NULL);
911                         off = tv.tv_sec - start.tv_sec;
912                         msec = (int64_t)tv.tv_usec - (int64_t)start.tv_usec;
913                         if (msec < 0) {
914                                 off -= 1;
915                                 msec = 1000000 + msec;
916                         }
917                         off *= 1000;
918                         off += msec / 1000;
919                         if (off >= timeout)
920                                 break;
921                 }
922         }
923
924         return 0;
925 }
926
927 /**
928  * ev_eloop_exit:
929  * @loop: Event loop that should exit
930  *
931  * This makes a call to ev_eloop_run() stop.
932  */
933 void ev_eloop_exit(struct ev_eloop *loop)
934 {
935         if (!loop)
936                 return;
937
938         llog_debug(loop, "exiting %p", loop);
939
940         loop->exit = true;
941         if (loop->fd->loop)
942                 ev_eloop_exit(loop->fd->loop);
943 }
944
945 /**
946  * ev_eloop_get_fd:
947  * @loop: Event loop
948  *
949  * Returns a single file descriptor for the whole event-loop. If that FD is
950  * readable, then one of the event-sources is active and you should call
951  * ev_eloop_dispatch(loop, 0); to dispatch these events.
952  * If the fd is not readable, then ev_eloop_dispatch() would sleep as there are
953  * no active events.
954  *
955  * Returns: A file descriptor for the event loop or negative error code
956  */
957 int ev_eloop_get_fd(struct ev_eloop *loop)
958 {
959         if (!loop)
960                 return -EINVAL;
961
962         return loop->efd;
963 }
964
965 /**
966  * ev_eloop_new_eloop:
967  * @loop: The parent event-loop where the new event loop is registered
968  * @out: Storage for new event loop
969  *
970  * This creates a new event loop and directly registeres it as event source on
971  * the parent event loop \loop.
972  *
973  * Returns: 0 on success, otherwise negative error code
974  */
975 int ev_eloop_new_eloop(struct ev_eloop *loop, struct ev_eloop **out)
976 {
977         struct ev_eloop *el;
978         int ret;
979
980         if (!loop)
981                 return -EINVAL;
982         if (!out)
983                 return llog_EINVAL(loop);
984
985         ret = ev_eloop_new(&el, loop->llog);
986         if (ret)
987                 return ret;
988
989         ret = ev_eloop_add_eloop(loop, el);
990         if (ret) {
991                 ev_eloop_unref(el);
992                 return ret;
993         }
994
995         ev_eloop_unref(el);
996         *out = el;
997         return 0;
998 }
999
1000 /**
1001  * ev_eloop_add_eloop:
1002  * @loop: Parent event loop
1003  * @add: The event loop that is registered as event source on @loop
1004  *
1005  * This registers the existing event loop @add as event source on the parent
1006  * event loop @loop.
1007  *
1008  * Returns: 0 on success, otherwise negative error code
1009  */
1010 int ev_eloop_add_eloop(struct ev_eloop *loop, struct ev_eloop *add)
1011 {
1012         int ret;
1013
1014         if (!loop)
1015                 return -EINVAL;
1016         if (!add)
1017                 return llog_EINVAL(loop);
1018
1019         if (add->fd->loop)
1020                 return -EALREADY;
1021
1022         /* This adds the epoll-fd into the parent epoll-set. This works
1023          * perfectly well with registered FDs, timers, etc. However, we use
1024          * shared signals in this event-loop so if the parent and child have
1025          * overlapping shared-signals, then the signal will be randomly
1026          * delivered to either the parent-hook or child-hook but never both.
1027          * TODO:
1028          * We may fix this by linking the childs-sig_list into the parent's
1029          * siglist but we didn't need this, yet, so ignore it here.
1030          */
1031
1032         ret = ev_eloop_add_fd(loop, add->fd);
1033         if (ret)
1034                 return ret;
1035
1036         ev_eloop_ref(add);
1037         return 0;
1038 }
1039
1040 /**
1041  * ev_eloop_rm_eloop:
1042  * @rm: Event loop to be unregistered from its parent
1043  *
1044  * This unregisters the event loop @rm as event source from its parent. If this
1045  * event loop was not registered on any other event loop, then this call does
1046  * nothing.
1047  */
1048 void ev_eloop_rm_eloop(struct ev_eloop *rm)
1049 {
1050         if (!rm || !rm->fd->loop)
1051                 return;
1052
1053         ev_eloop_rm_fd(rm->fd);
1054         ev_eloop_unref(rm);
1055 }
1056
1057 /*
1058  * FD sources
1059  * This allows adding file descriptors to an eloop. A file descriptor is the
1060  * most basic kind of source and used for all other source types.
1061  * By default a source is always enabled but you can easily disable the source
1062  * by calling ev_fd_disable(). This will have the effect, that the source is
1063  * still registered with the eloop but will not wake up the thread or get
1064  * called until you enable it again.
1065  */
1066
1067 /**
1068  * ev_fd_new:
1069  * @out: Storage for result
1070  * @rfd: The actual file desciptor
1071  * @mask: Bitmask of %EV_READABLE and %EV_WRITeABLE flags
1072  * @cb: User callback
1073  * @data: User data
1074  * @log: llog function or NULL
1075  *
1076  * This creates a new file desciptor source that is watched for the events set
1077  * in @mask. @rfd is the system filedescriptor. The resulting object is stored
1078  * in @out. @cb and @data are the user callback and the user-supplied data that
1079  * is passed to the callback on events.
1080  * The FD is automatically watched for EV_HUP and EV_ERR events, too.
1081  *
1082  * Returns: 0 on success, otherwise negative error code
1083  */
1084 int ev_fd_new(struct ev_fd **out, int rfd, int mask, ev_fd_cb cb, void *data,
1085               ev_log_t log)
1086 {
1087         struct ev_fd *fd;
1088
1089         if (!out || rfd < 0)
1090                 return llog_dEINVAL(log);
1091
1092         fd = malloc(sizeof(*fd));
1093         if (!fd)
1094                 return llog_dEINVAL(log);
1095
1096         memset(fd, 0, sizeof(*fd));
1097         fd->ref = 1;
1098         fd->llog = log;
1099         fd->fd = rfd;
1100         fd->mask = mask;
1101         fd->cb = cb;
1102         fd->data = data;
1103         fd->enabled = true;
1104
1105         *out = fd;
1106         return 0;
1107 }
1108
1109 /**
1110  * ev_fd_ref:
1111  * @fd: FD object
1112  *
1113  * Increases the ref-count of @fd by 1.
1114  */
1115 void ev_fd_ref(struct ev_fd *fd)
1116 {
1117         if (!fd)
1118                 return;
1119         if (!fd->ref)
1120                 return llog_vEINVAL(fd);
1121
1122         ++fd->ref;
1123 }
1124
1125 /**
1126  * ev_fd_unref:
1127  * @fd: FD object
1128  *
1129  * Decreases the ref-count of @fd by 1. Destroys the object if the ref-count
1130  * drops to zero.
1131  */
1132 void ev_fd_unref(struct ev_fd *fd)
1133 {
1134         if (!fd)
1135                 return;
1136         if (!fd->ref)
1137                 return llog_vEINVAL(fd);
1138         if (--fd->ref)
1139                 return;
1140
1141         free(fd);
1142 }
1143
1144 static int fd_epoll_add(struct ev_fd *fd)
1145 {
1146         struct epoll_event ep;
1147         int ret;
1148
1149         if (!fd->loop)
1150                 return 0;
1151
1152         memset(&ep, 0, sizeof(ep));
1153         if (fd->mask & EV_READABLE)
1154                 ep.events |= EPOLLIN;
1155         if (fd->mask & EV_WRITEABLE)
1156                 ep.events |= EPOLLOUT;
1157         if (fd->mask & EV_ET)
1158                 ep.events |= EPOLLET;
1159         ep.data.ptr = fd;
1160
1161         ret = epoll_ctl(fd->loop->efd, EPOLL_CTL_ADD, fd->fd, &ep);
1162         if (ret) {
1163                 llog_warning(fd, "cannot add fd %d to epoll set (%d): %m",
1164                              fd->fd, errno);
1165                 return -EFAULT;
1166         }
1167
1168         return 0;
1169 }
1170
1171 static void fd_epoll_remove(struct ev_fd *fd)
1172 {
1173         int ret;
1174
1175         if (!fd->loop)
1176                 return;
1177
1178         ret = epoll_ctl(fd->loop->efd, EPOLL_CTL_DEL, fd->fd, NULL);
1179         if (ret && errno != EBADF)
1180                 llog_warning(fd, "cannot remove fd %d from epoll set (%d): %m",
1181                              fd->fd, errno);
1182 }
1183
1184 static int fd_epoll_update(struct ev_fd *fd)
1185 {
1186         struct epoll_event ep;
1187         int ret;
1188
1189         if (!fd->loop)
1190                 return 0;
1191
1192         memset(&ep, 0, sizeof(ep));
1193         if (fd->mask & EV_READABLE)
1194                 ep.events |= EPOLLIN;
1195         if (fd->mask & EV_WRITEABLE)
1196                 ep.events |= EPOLLOUT;
1197         if (fd->mask & EV_ET)
1198                 ep.events |= EPOLLET;
1199         ep.data.ptr = fd;
1200
1201         ret = epoll_ctl(fd->loop->efd,  EPOLL_CTL_MOD, fd->fd, &ep);
1202         if (ret) {
1203                 llog_warning(fd, "cannot update epoll fd %d (%d): %m",
1204                              fd->fd, errno);
1205                 return -EFAULT;
1206         }
1207
1208         return 0;
1209 }
1210
1211 /**
1212  * ev_fd_enable:
1213  * @fd: FD object
1214  *
1215  * This enables @fd. By default every fd object is enabled. If you disabled it
1216  * you can re-enable it with this call.
1217  *
1218  * Returns: 0 on success, otherwise negative error code
1219  */
1220 int ev_fd_enable(struct ev_fd *fd)
1221 {
1222         int ret;
1223
1224         if (!fd)
1225                 return -EINVAL;
1226         if (fd->enabled)
1227                 return 0;
1228
1229         ret = fd_epoll_add(fd);
1230         if (ret)
1231                 return ret;
1232
1233         fd->enabled = true;
1234         return 0;
1235 }
1236
1237 /**
1238  * ev_fd_disable:
1239  * @fd: FD object
1240  *
1241  * Disables @fd. That means, no more events are handled for @fd until you
1242  * re-enable it with ev_fd_enable().
1243  */
1244 void ev_fd_disable(struct ev_fd *fd)
1245 {
1246         if (!fd || !fd->enabled)
1247                 return;
1248
1249         fd->enabled = false;
1250         fd_epoll_remove(fd);
1251 }
1252
1253 /**
1254  * ev_fd_is_enabled:
1255  * @fd: FD object
1256  *
1257  * Returns whether the fd object is enabled or disabled.
1258  *
1259  * Returns: true if @fd is enabled, otherwise false.
1260  */
1261 bool ev_fd_is_enabled(struct ev_fd *fd)
1262 {
1263         return fd && fd->enabled;
1264 }
1265
1266 /**
1267  * ev_fd_is_bound:
1268  * @fd: FD object
1269  *
1270  * Returns true if the fd object is bound to an event loop.
1271  *
1272  * Returns: true if @fd is bound, otherwise false
1273  */
1274 bool ev_fd_is_bound(struct ev_fd *fd)
1275 {
1276         return fd && fd->loop;
1277 }
1278
1279 /**
1280  * ev_fd_set_cb_data:
1281  * @fd: FD object
1282  * @cb: New user callback
1283  * @data: New user data
1284  *
1285  * This changes the user callback and user data that were set in ev_fd_new().
1286  * Both can be set to NULL. If @cb is NULL, then the callback will not be called
1287  * anymore.
1288  */
1289 void ev_fd_set_cb_data(struct ev_fd *fd, ev_fd_cb cb, void *data)
1290 {
1291         if (!fd)
1292                 return;
1293
1294         fd->cb = cb;
1295         fd->data = data;
1296 }
1297
1298 /**
1299  * ev_fd_update:
1300  * @fd: FD object
1301  * @mask: Bitmask of %EV_READABLE and %EV_WRITEABLE
1302  *
1303  * This resets the event mask of @fd to @mask.
1304  *
1305  * Returns: 0 on success, otherwise negative error code
1306  */
1307 int ev_fd_update(struct ev_fd *fd, int mask)
1308 {
1309         int ret;
1310         int omask;
1311
1312         if (!fd)
1313                 return -EINVAL;
1314         if (fd->mask == mask && !(mask & EV_ET))
1315                 return 0;
1316
1317         omask = fd->mask;
1318         fd->mask = mask;
1319
1320         if (!fd->enabled)
1321                 return 0;
1322
1323         ret = fd_epoll_update(fd);
1324         if (ret) {
1325                 fd->mask = omask;
1326                 return ret;
1327         }
1328
1329         return 0;
1330 }
1331
1332 /**
1333  * ev_eloop_new_fd:
1334  * @loop: Event loop
1335  * @out: Storage for result
1336  * @rfd: File descriptor
1337  * @mask: Bitmask of %EV_READABLE and %EV_WRITEABLE
1338  * @cb: User callback
1339  * @data: User data
1340  *
1341  * This creates a new fd object like ev_fd_new() and directly registers it in
1342  * the event loop @loop. See ev_fd_new() and ev_eloop_add_fd() for more
1343  * information.
1344  * The ref-count of @out is 1 so you must call ev_eloop_rm_fd() to destroy the
1345  * fd. You must not call ev_fd_unref() unless you called ev_fd_ref() before.
1346  *
1347  * Returns: 0 on success, otherwise negative error code
1348  */
1349 int ev_eloop_new_fd(struct ev_eloop *loop, struct ev_fd **out, int rfd,
1350                         int mask, ev_fd_cb cb, void *data)
1351 {
1352         struct ev_fd *fd;
1353         int ret;
1354
1355         if (!loop)
1356                 return -EINVAL;
1357         if (!out || rfd < 0)
1358                 return llog_EINVAL(loop);
1359
1360         ret = ev_fd_new(&fd, rfd, mask, cb, data, loop->llog);
1361         if (ret)
1362                 return ret;
1363
1364         ret = ev_eloop_add_fd(loop, fd);
1365         if (ret) {
1366                 ev_fd_unref(fd);
1367                 return ret;
1368         }
1369
1370         ev_fd_unref(fd);
1371         *out = fd;
1372         return 0;
1373 }
1374
1375 /**
1376  * ev_eloop_add_fd:
1377  * @loop: Event loop
1378  * @fd: FD Object
1379  *
1380  * Registers @fd in the event loop @loop. This increases the ref-count of both
1381  * @loop and @fd. From now on the user callback of @fd may get called during
1382  * dispatching.
1383  *
1384  * Returns: 0 on success, otherwise negative error code
1385  */
1386 int ev_eloop_add_fd(struct ev_eloop *loop, struct ev_fd *fd)
1387 {
1388         int ret;
1389
1390         if (!loop)
1391                 return -EINVAL;
1392         if (!fd || fd->loop)
1393                 return llog_EINVAL(loop);
1394
1395         fd->loop = loop;
1396
1397         if (fd->enabled) {
1398                 ret = fd_epoll_add(fd);
1399                 if (ret) {
1400                         fd->loop = NULL;
1401                         return ret;
1402                 }
1403         }
1404
1405         ev_fd_ref(fd);
1406         ev_eloop_ref(loop);
1407         return 0;
1408 }
1409
1410 /**
1411  * ev_eloop_rm_fd:
1412  * @fd: FD object
1413  *
1414  * Removes the fd object @fd from its event loop. If you did not call
1415  * ev_eloop_add_fd() before, this will do nothing.
1416  * This decreases the refcount of @fd and the event loop by 1.
1417  * It is safe to call this in any callback. This makes sure that the current
1418  * dispatcher will not get confused or read invalid memory.
1419  */
1420 void ev_eloop_rm_fd(struct ev_fd *fd)
1421 {
1422         struct ev_eloop *loop;
1423         size_t i;
1424
1425         if (!fd || !fd->loop)
1426                 return;
1427
1428         loop = fd->loop;
1429         if (fd->enabled)
1430                 fd_epoll_remove(fd);
1431
1432         /*
1433          * If we are currently dispatching events, we need to remove ourself
1434          * from the temporary event list.
1435          */
1436         if (loop->dispatching) {
1437                 for (i = 0; i < loop->cur_fds_cnt; ++i) {
1438                         if (fd == loop->cur_fds[i].data.ptr)
1439                                 loop->cur_fds[i].data.ptr = NULL;
1440                 }
1441         }
1442
1443         fd->loop = NULL;
1444         ev_fd_unref(fd);
1445         ev_eloop_unref(loop);
1446 }
1447
1448 /*
1449  * Timer sources
1450  * Timer sources allow delaying a specific event by an relative timeout. The
1451  * timeout can be set to trigger after a specific time. Optionally, you can
1452  * also make the timeout trigger every next time the timeout elapses so you
1453  * basically get a pulse that reliably calls the callback.
1454  * The callback gets as parameter the number of timeouts that elapsed since it
1455  * was last called (in case the application couldn't call the callback fast
1456  * enough). The timeout can be specified with nano-seconds precision. However,
1457  * real precision depends on the operating-system and hardware.
1458  */
1459
1460 static int timer_drain(struct ev_timer *timer, uint64_t *out)
1461 {
1462         int len;
1463         uint64_t expirations;
1464
1465         if (out)
1466                 *out = 0;
1467
1468         len = read(timer->fd, &expirations, sizeof(expirations));
1469         if (len < 0) {
1470                 if (errno == EAGAIN) {
1471                         return 0;
1472                 } else {
1473                         llog_warning(timer, "cannot read timerfd (%d): %m",
1474                                      errno);
1475                         return errno;
1476                 }
1477         } else if (len == 0) {
1478                 llog_warning(timer, "EOF on timer source");
1479                 return -EFAULT;
1480         } else if (len != sizeof(expirations)) {
1481                 llog_warn(timer, "invalid size %d read on timerfd", len);
1482                 return -EFAULT;
1483         } else {
1484                 if (out)
1485                         *out = expirations;
1486                 return 0;
1487         }
1488 }
1489
1490 static void timer_cb(struct ev_fd *fd, int mask, void *data)
1491 {
1492         struct ev_timer *timer = data;
1493         uint64_t expirations;
1494         int ret;
1495
1496         if (mask & (EV_HUP | EV_ERR)) {
1497                 llog_warn(fd, "HUP/ERR on timer source");
1498                 goto err_cb;
1499         }
1500
1501         if (mask & EV_READABLE) {
1502                 ret = timer_drain(timer, &expirations);
1503                 if (ret)
1504                         goto err_cb;
1505                 if (expirations > 0) {
1506                         if (timer->cb)
1507                                 timer->cb(timer, expirations, timer->data);
1508                 }
1509         }
1510
1511         return;
1512
1513 err_cb:
1514         ev_timer_disable(timer);
1515         if (timer->cb)
1516                 timer->cb(timer, 0, timer->data);
1517 }
1518
1519 static const struct itimerspec ev_timer_zero;
1520
1521 /**
1522  * ev_timer_new:
1523  * @out: Timer pointer where to store the new timer
1524  * @spec: Timespan
1525  * @cb: callback to use for this event-source
1526  * @data: user-specified data
1527  * @log: logging function or NULL
1528  *
1529  * This creates a new timer-source. See "man timerfd_create" for information on
1530  * the @spec argument. The timer is always relative and uses the
1531  * monotonic-kernel clock.
1532  *
1533  * Returns: 0 on success, negative error on failure
1534  */
1535 int ev_timer_new(struct ev_timer **out, const struct itimerspec *spec,
1536                  ev_timer_cb cb, void *data, ev_log_t log)
1537 {
1538         struct ev_timer *timer;
1539         int ret;
1540
1541         if (!out)
1542                 return llog_dEINVAL(log);
1543
1544         if (!spec)
1545                 spec = &ev_timer_zero;
1546
1547         timer = malloc(sizeof(*timer));
1548         if (!timer)
1549                 return llog_dENOMEM(log);
1550
1551         memset(timer, 0, sizeof(*timer));
1552         timer->ref = 1;
1553         timer->llog = log;
1554         timer->cb = cb;
1555         timer->data = data;
1556
1557         timer->fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK);
1558         if (timer->fd < 0) {
1559                 llog_error(timer, "cannot create timerfd (%d): %m", errno);
1560                 ret = -EFAULT;
1561                 goto err_free;
1562         }
1563
1564         ret = timerfd_settime(timer->fd, 0, spec, NULL);
1565         if (ret) {
1566                 llog_warn(timer, "cannot set timerfd (%d): %m", errno);
1567                 ret = -EFAULT;
1568                 goto err_close;
1569         }
1570
1571         ret = ev_fd_new(&timer->efd, timer->fd, EV_READABLE, timer_cb, timer,
1572                         timer->llog);
1573         if (ret)
1574                 goto err_close;
1575
1576         *out = timer;
1577         return 0;
1578
1579 err_close:
1580         close(timer->fd);
1581 err_free:
1582         free(timer);
1583         return ret;
1584 }
1585
1586 /**
1587  * ev_timer_ref:
1588  * @timer: Timer object
1589  *
1590  * Increase reference count by 1.
1591  */
1592 void ev_timer_ref(struct ev_timer *timer)
1593 {
1594         if (!timer)
1595                 return;
1596         if (!timer->ref)
1597                 return llog_vEINVAL(timer);
1598
1599         ++timer->ref;
1600 }
1601
1602 /**
1603  * ev_timer_unref:
1604  * @timer: Timer object
1605  *
1606  * Decrease reference-count by 1 and destroy timer if it drops to 0.
1607  */
1608 void ev_timer_unref(struct ev_timer *timer)
1609 {
1610         if (!timer)
1611                 return;
1612         if (!timer->ref)
1613                 return llog_vEINVAL(timer);
1614         if (--timer->ref)
1615                 return;
1616
1617         ev_fd_unref(timer->efd);
1618         close(timer->fd);
1619         free(timer);
1620 }
1621
1622 /**
1623  * ev_timer_enable:
1624  * @timer: Timer object
1625  *
1626  * Enable the timer. This calls ev_fd_enable() on the fd that implements this
1627  * timer.
1628  *
1629  * Returns: 0 on success negative error code on failure
1630  */
1631 int ev_timer_enable(struct ev_timer *timer)
1632 {
1633         if (!timer)
1634                 return -EINVAL;
1635
1636         return ev_fd_enable(timer->efd);
1637 }
1638
1639 /**
1640  * ev_timer_disable:
1641  * @timer: Timer object
1642  *
1643  * Disable the timer. This calls ev_fd_disable() on the fd that implements this
1644  * timer.
1645  *
1646  * Returns: 0 on success and negative error code on failure
1647  */
1648 void ev_timer_disable(struct ev_timer *timer)
1649 {
1650         if (!timer)
1651                 return;
1652
1653         ev_fd_disable(timer->efd);
1654 }
1655
1656 /**
1657  * ev_timer_is_enabled:
1658  * @timer: Timer object
1659  *
1660  * Checks whether the timer is enabled.
1661  *
1662  * Returns: true if timer is enabled, false otherwise
1663  */
1664 bool ev_timer_is_enabled(struct ev_timer *timer)
1665 {
1666         return timer && ev_fd_is_enabled(timer->efd);
1667 }
1668
1669 /**
1670  * ev_timer_is_bound:
1671  * @timer: Timer object
1672  *
1673  * Checks whether the timer is bound to an event loop.
1674  *
1675  * Returns: true if the timer is bound, false otherwise.
1676  */
1677 bool ev_timer_is_bound(struct ev_timer *timer)
1678 {
1679         return timer && ev_fd_is_bound(timer->efd);
1680 }
1681
1682 /**
1683  * ev_timer_set_cb_data:
1684  * @timer: Timer object
1685  * @cb: User callback or NULL
1686  * @data: User data or NULL
1687  *
1688  * This changes the user-supplied callback and data that is used for this timer
1689  * object.
1690  */
1691 void ev_timer_set_cb_data(struct ev_timer *timer, ev_timer_cb cb, void *data)
1692 {
1693         if (!timer)
1694                 return;
1695
1696         timer->cb = cb;
1697         timer->data = data;
1698 }
1699
1700 /**
1701  * ev_timer_update:
1702  * @timer: Timer object
1703  * @spec: timespan
1704  *
1705  * This changes the timer timespan. See "man timerfd_settime" for information
1706  * on the @spec parameter.
1707  *
1708  * Returns: 0 on success, negative error code on failure.
1709  */
1710 int ev_timer_update(struct ev_timer *timer, const struct itimerspec *spec)
1711 {
1712         int ret;
1713
1714         if (!timer)
1715                 return -EINVAL;
1716
1717         if (!spec)
1718                 spec = &ev_timer_zero;
1719
1720         ret = timerfd_settime(timer->fd, 0, spec, NULL);
1721         if (ret) {
1722                 llog_warn(timer, "cannot set timerfd (%d): %m", errno);
1723                 return -EFAULT;
1724         }
1725
1726         return 0;
1727 }
1728
1729 /**
1730  * ev_timer_drain:
1731  * @timer: valid timer object
1732  * @expirations: destination to save result or NULL
1733  *
1734  * This reads the current expiration-count from the timer object @timer and
1735  * saves it in @expirations (if it is non-NULL). This can be used to clear the
1736  * timer after an idle-period or similar.
1737  * Note that the timer_cb() callback function automatically calls this before
1738  * calling the user-supplied callback.
1739  *
1740  * Returns: 0 on success, negative error code on failure.
1741  */
1742 int ev_timer_drain(struct ev_timer *timer, uint64_t *expirations)
1743 {
1744         if (!timer)
1745                 return -EINVAL;
1746
1747         return timer_drain(timer, expirations);
1748 }
1749
1750 /**
1751  * ev_eloop_new_timer:
1752  * @loop: event loop
1753  * @out: output where to store the new timer
1754  * @spec: timespan
1755  * @cb: user callback
1756  * @data: user-supplied data
1757  *
1758  * This is a combination of ev_timer_new() and ev_eloop_add_timer(). See both
1759  * for more information.
1760  *
1761  * Returns: 0 on success, negative error code on failure.
1762  */
1763 int ev_eloop_new_timer(struct ev_eloop *loop, struct ev_timer **out,
1764                         const struct itimerspec *spec, ev_timer_cb cb,
1765                         void *data)
1766 {
1767         struct ev_timer *timer;
1768         int ret;
1769
1770         if (!loop)
1771                 return -EINVAL;
1772         if (!out)
1773                 return llog_EINVAL(loop);
1774
1775         ret = ev_timer_new(&timer, spec, cb, data, loop->llog);
1776         if (ret)
1777                 return ret;
1778
1779         ret = ev_eloop_add_timer(loop, timer);
1780         if (ret) {
1781                 ev_timer_unref(timer);
1782                 return ret;
1783         }
1784
1785         ev_timer_unref(timer);
1786         *out = timer;
1787         return 0;
1788 }
1789
1790 /**
1791  * ev_eloop_add_timer:
1792  * @loop: event loop
1793  * @timer: Timer source
1794  *
1795  * This adds @timer as source to @loop. @timer must be currently unbound,
1796  * otherwise, this will fail with -EALREADY.
1797  *
1798  * Returns: 0 on success, negative error code on failure
1799  */
1800 int ev_eloop_add_timer(struct ev_eloop *loop, struct ev_timer *timer)
1801 {
1802         int ret;
1803
1804         if (!loop)
1805                 return -EINVAL;
1806         if (!timer)
1807                 return llog_EINVAL(loop);
1808
1809         if (ev_fd_is_bound(timer->efd))
1810                 return -EALREADY;
1811
1812         ret = ev_eloop_add_fd(loop, timer->efd);
1813         if (ret)
1814                 return ret;
1815
1816         ev_timer_ref(timer);
1817         return 0;
1818 }
1819
1820 /**
1821  * ev_eloop_rm_timer:
1822  * @timer: Timer object
1823  *
1824  * If @timer is currently bound to an event loop, this will remove this bondage
1825  * again.
1826  */
1827 void ev_eloop_rm_timer(struct ev_timer *timer)
1828 {
1829         if (!timer || !ev_fd_is_bound(timer->efd))
1830                 return;
1831
1832         ev_eloop_rm_fd(timer->efd);
1833         ev_timer_unref(timer);
1834 }
1835
1836 /*
1837  * Counter Sources
1838  * Counter sources are a very basic event notification mechanism. It is based
1839  * around the eventfd() system call on linux machines. Internally, there is a
1840  * 64bit unsigned integer that can be increased by the caller. By default it is
1841  * set to 0. If it is non-zero, the event-fd will be notified and the
1842  * user-defined callback is called. The callback gets as argument the current
1843  * state of the counter and the counter is reset to 0.
1844  *
1845  * If the internal counter would overflow, an increase() fails silently so an
1846  * overflow will never occur, however, you may loose events this way. This can
1847  * be ignored when increasing with small values, only.
1848  */
1849
1850 static void counter_event(struct ev_fd *fd, int mask, void *data)
1851 {
1852         struct ev_counter *cnt = data;
1853         int ret;
1854         uint64_t val;
1855
1856         if (mask & (EV_HUP | EV_ERR)) {
1857                 llog_warning(fd, "HUP/ERR on eventfd");
1858                 if (cnt->cb)
1859                         cnt->cb(cnt, 0, cnt->data);
1860                 return;
1861         }
1862
1863         if (!(mask & EV_READABLE))
1864                 return;
1865
1866         ret = read(cnt->fd, &val, sizeof(val));
1867         if (ret < 0) {
1868                 if (errno != EAGAIN) {
1869                         llog_warning(fd, "reading eventfd failed (%d): %m", errno);
1870                         ev_counter_disable(cnt);
1871                         if (cnt->cb)
1872                                 cnt->cb(cnt, 0, cnt->data);
1873                 }
1874         } else if (ret == 0) {
1875                 llog_warning(fd, "EOF on eventfd");
1876                 ev_counter_disable(cnt);
1877                 if (cnt->cb)
1878                         cnt->cb(cnt, 0, cnt->data);
1879         } else if (ret != sizeof(val)) {
1880                 llog_warning(fd, "read %d bytes instead of 8 on eventfd", ret);
1881                 ev_counter_disable(cnt);
1882                 if (cnt->cb)
1883                         cnt->cb(cnt, 0, cnt->data);
1884         } else if (val > 0) {
1885                 if (cnt->cb)
1886                         cnt->cb(cnt, val, cnt->data);
1887         }
1888 }
1889
1890 /**
1891  * ev_counter_new:
1892  * @out: Where to store the new counter
1893  * @cb: user-supplied callback
1894  * @data: user-supplied data
1895  * @log: logging function or NULL
1896  *
1897  * This creates a new counter object and stores it in @out.
1898  *
1899  * Returns: 0 on success, negative error code on failure.
1900  */
1901 int ev_counter_new(struct ev_counter **out, ev_counter_cb cb, void *data,
1902                    ev_log_t log)
1903 {
1904         struct ev_counter *cnt;
1905         int ret;
1906
1907         if (!out)
1908                 return llog_dEINVAL(log);
1909
1910         cnt = malloc(sizeof(*cnt));
1911         if (!cnt)
1912                 return llog_dENOMEM(log);
1913         memset(cnt, 0, sizeof(*cnt));
1914         cnt->ref = 1;
1915         cnt->llog = log;
1916         cnt->cb = cb;
1917         cnt->data = data;
1918
1919         cnt->fd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
1920         if (cnt->fd < 0) {
1921                 llog_error(cnt, "cannot create eventfd (%d): %m", errno);
1922                 ret = -EFAULT;
1923                 goto err_free;
1924         }
1925
1926         ret = ev_fd_new(&cnt->efd, cnt->fd, EV_READABLE, counter_event, cnt,
1927                         cnt->llog);
1928         if (ret)
1929                 goto err_close;
1930
1931         *out = cnt;
1932         return 0;
1933
1934 err_close:
1935         close(cnt->fd);
1936 err_free:
1937         free(cnt);
1938         return ret;
1939 }
1940
1941 /**
1942  * ev_counter_ref:
1943  * @cnt: Counter object
1944  *
1945  * This increases the reference-count of @cnt by 1.
1946  */
1947 void ev_counter_ref(struct ev_counter *cnt)
1948 {
1949         if (!cnt)
1950                 return;
1951         if (!cnt->ref)
1952                 return llog_vEINVAL(cnt);
1953
1954         ++cnt->ref;
1955 }
1956
1957 /**
1958  * ev_counter_unref:
1959  * @cnt: Counter object
1960  *
1961  * This decreases the reference-count of @cnt by 1 and destroys the object if
1962  * it drops to 0.
1963  */
1964 void ev_counter_unref(struct ev_counter *cnt)
1965 {
1966         if (!cnt)
1967                 return;
1968         if (!cnt->ref)
1969                 return llog_vEINVAL(cnt);
1970         if (--cnt->ref)
1971                 return;
1972
1973         ev_fd_unref(cnt->efd);
1974         close(cnt->fd);
1975         free(cnt);
1976 }
1977
1978 /**
1979  * ev_counter_enable:
1980  * @cnt: Counter object
1981  *
1982  * This enables the counter object. It calls ev_fd_enable() on the underlying
1983  * file-descriptor.
1984  *
1985  * Returns: 0 on success, negative error code on failure
1986  */
1987 int ev_counter_enable(struct ev_counter *cnt)
1988 {
1989         if (!cnt)
1990                 return -EINVAL;
1991
1992         return ev_fd_enable(cnt->efd);
1993 }
1994
1995 /**
1996  * ev_counter_disable:
1997  * @cnt: Counter object
1998  *
1999  * This disables the counter. It calls ev_fd_disable() on the underlying
2000  * file-descriptor.
2001  */
2002 void ev_counter_disable(struct ev_counter *cnt)
2003 {
2004         if (!cnt)
2005                 return;
2006
2007         ev_fd_disable(cnt->efd);
2008 }
2009
2010 /**
2011  * ev_counter_is_enabled:
2012  * @cnt: counter object
2013  *
2014  * Checks whether the counter is enabled.
2015  *
2016  * Returns: true if the counter is enabled, otherwise returns false.
2017  */
2018 bool ev_counter_is_enabled(struct ev_counter *cnt)
2019 {
2020         return cnt && ev_fd_is_enabled(cnt->efd);
2021 }
2022
2023 /**
2024  * ev_counter_is_bound:
2025  * @cnt: Counter object
2026  *
2027  * Checks whether the counter is bound to an event loop.
2028  *
2029  * Returns: true if the counter is bound, otherwise false is returned.
2030  */
2031 bool ev_counter_is_bound(struct ev_counter *cnt)
2032 {
2033         return cnt && ev_fd_is_bound(cnt->efd);
2034 }
2035
2036 /**
2037  * ev_counter_set_cb_data:
2038  * @cnt: Counter object
2039  * @cb: user-supplied callback
2040  * @data: user-supplied data
2041  *
2042  * This changes the user-supplied callback and data for the given counter
2043  * object.
2044  */
2045 void ev_counter_set_cb_data(struct ev_counter *cnt, ev_counter_cb cb,
2046                             void *data)
2047 {
2048         if (!cnt)
2049                 return;
2050
2051         cnt->cb = cb;
2052         cnt->data = data;
2053 }
2054
2055 /**
2056  * ev_counter_inc:
2057  * @cnt: Counter object
2058  * @val: Counter increase amount
2059  *
2060  * This increases the counter @cnt by @val.
2061  *
2062  * Returns: 0 on success, negative error code on failure.
2063  */
2064 int ev_counter_inc(struct ev_counter *cnt, uint64_t val)
2065 {
2066         if (!cnt)
2067                 return -EINVAL;
2068
2069         return write_eventfd(cnt->llog, cnt->fd, val);
2070 }
2071
2072 /**
2073  * ev_eloop_new_counter:
2074  * @eloop: event loop
2075  * @out: output storage for new counter
2076  * @cb: user-supplied callback
2077  * @data: user-supplied data
2078  *
2079  * This combines ev_counter_new() and ev_eloop_add_counter() in one call.
2080  *
2081  * Returns: 0 on success, negative error code on failure.
2082  */
2083 int ev_eloop_new_counter(struct ev_eloop *eloop, struct ev_counter **out,
2084                          ev_counter_cb cb, void *data)
2085 {
2086         int ret;
2087         struct ev_counter *cnt;
2088
2089         if (!eloop)
2090                 return -EINVAL;
2091         if (!out)
2092                 return llog_EINVAL(eloop);
2093
2094         ret = ev_counter_new(&cnt, cb, data, eloop->llog);
2095         if (ret)
2096                 return ret;
2097
2098         ret = ev_eloop_add_counter(eloop, cnt);
2099         if (ret) {
2100                 ev_counter_unref(cnt);
2101                 return ret;
2102         }
2103
2104         ev_counter_unref(cnt);
2105         *out = cnt;
2106         return 0;
2107 }
2108
2109 /**
2110  * ev_eloop_add_counter:
2111  * @eloop: Event loop
2112  * @cnt: Counter object
2113  *
2114  * This adds @cnt to the given event loop @eloop. If @cnt is already bound,
2115  * this will fail with -EALREADY.
2116  *
2117  * Returns: 0 on success, negative error code on failure.
2118  */
2119 int ev_eloop_add_counter(struct ev_eloop *eloop, struct ev_counter *cnt)
2120 {
2121         int ret;
2122
2123         if (!eloop)
2124                 return -EINVAL;
2125         if (!cnt)
2126                 return llog_EINVAL(eloop);
2127
2128         if (ev_fd_is_bound(cnt->efd))
2129                 return -EALREADY;
2130
2131         ret = ev_eloop_add_fd(eloop, cnt->efd);
2132         if (ret)
2133                 return ret;
2134
2135         ev_counter_ref(cnt);
2136         return 0;
2137 }
2138
2139 /**
2140  * ev_eloop_rm_counter:
2141  * @cnt: Counter object
2142  *
2143  * If @cnt is bound to an event-loop, then this will remove this bondage again.
2144  */
2145 void ev_eloop_rm_counter(struct ev_counter *cnt)
2146 {
2147         if (!cnt || !ev_fd_is_bound(cnt->efd))
2148                 return;
2149
2150         ev_eloop_rm_fd(cnt->efd);
2151         ev_counter_unref(cnt);
2152 }
2153
2154 /*
2155  * Shared signals
2156  * This allows registering for shared signal events. See description of the
2157  * shared signal object above for more information how this works. Also see the
2158  * eloop description to see some drawbacks when nesting eloop objects with the
2159  * same shared signal sources.
2160  */
2161
2162 /**
2163  * ev_eloop_register_signal_cb:
2164  * @loop: event loop
2165  * @signum: Signal number
2166  * @cb: user-supplied callback
2167  * @data: user-supplied data
2168  *
2169  * This register a new callback for the given signal @signum. @cb must not be
2170  * NULL!
2171  *
2172  * Returns: 0 on success, negative error code on failure.
2173  */
2174 int ev_eloop_register_signal_cb(struct ev_eloop *loop, int signum,
2175                                 ev_signal_shared_cb cb, void *data)
2176 {
2177         struct ev_signal_shared *sig = NULL;
2178         int ret;
2179         struct shl_dlist *iter;
2180
2181         if (!loop)
2182                 return -EINVAL;
2183         if (signum < 0 || !cb)
2184                 return llog_EINVAL(loop);
2185
2186         shl_dlist_for_each(iter, &loop->sig_list) {
2187                 sig = shl_dlist_entry(iter, struct ev_signal_shared, list);
2188                 if (sig->signum == signum)
2189                         break;
2190                 sig = NULL;
2191         }
2192
2193         if (!sig) {
2194                 ret = signal_new(&sig, loop, signum);
2195                 if (ret)
2196                         return ret;
2197         }
2198
2199         ret = shl_hook_add_cast(sig->hook, cb, data, false);
2200         if (ret) {
2201                 signal_free(sig);
2202                 return ret;
2203         }
2204
2205         return 0;
2206 }
2207
2208 /**
2209  * ev_eloop_unregister_signal_cb:
2210  * @loop: event loop
2211  * @signum: signal number
2212  * @cb: user-supplied callback
2213  * @data: user-supplied data
2214  *
2215  * This removes a previously registered signal-callback again. The arguments
2216  * must be the same as for the ev_eloop_register_signal_cb() call. If multiple
2217  * callbacks with the same arguments are registered, then only one callback is
2218  * removed. It doesn't matter which callback is removed as both are identical.
2219  */
2220 void ev_eloop_unregister_signal_cb(struct ev_eloop *loop, int signum,
2221                                         ev_signal_shared_cb cb, void *data)
2222 {
2223         struct ev_signal_shared *sig;
2224         struct shl_dlist *iter;
2225
2226         if (!loop)
2227                 return;
2228
2229         shl_dlist_for_each(iter, &loop->sig_list) {
2230                 sig = shl_dlist_entry(iter, struct ev_signal_shared, list);
2231                 if (sig->signum == signum) {
2232                         shl_hook_rm_cast(sig->hook, cb, data);
2233                         if (!shl_hook_num(sig->hook))
2234                                 signal_free(sig);
2235                         return;
2236                 }
2237         }
2238 }
2239
2240 /*
2241  * Child reaper sources
2242  * If at least one child-reaper callback is registered, then the eloop object
2243  * listens for SIGCHLD and waits for all exiting children. The callbacks are
2244  * then notified for each PID that signaled an event.
2245  * Note that this cannot be done via the shared-signal sources as the waitpid()
2246  * call must not be done in callbacks. Otherwise, only one callback would see
2247  * the events while others will call waitpid() and get EAGAIN.
2248  */
2249
2250 int ev_eloop_register_child_cb(struct ev_eloop *loop, ev_child_cb cb,
2251                                void *data)
2252 {
2253         int ret;
2254         bool empty;
2255
2256         if (!loop)
2257                 return -EINVAL;
2258
2259         empty = !shl_hook_num(loop->chlds);
2260         ret = shl_hook_add_cast(loop->chlds, cb, data, false);
2261         if (ret)
2262                 return ret;
2263
2264         if (empty) {
2265                 ret = ev_eloop_register_signal_cb(loop, SIGCHLD, sig_child,
2266                                                   loop);
2267                 if (ret) {
2268                         shl_hook_rm_cast(loop->chlds, cb, data);
2269                         return ret;
2270                 }
2271         }
2272
2273         return 0;
2274 }
2275
2276 void ev_eloop_unregister_child_cb(struct ev_eloop *loop, ev_child_cb cb,
2277                                   void *data)
2278 {
2279         if (!loop || !shl_hook_num(loop->chlds))
2280                 return;
2281
2282         shl_hook_rm_cast(loop->chlds, cb, data);
2283         if (!shl_hook_num(loop->chlds))
2284                 ev_eloop_unregister_signal_cb(loop, SIGCHLD, sig_child, loop);
2285 }
2286
2287 /*
2288  * Idle sources
2289  * Idle sources are called everytime when a next dispatch round is started.
2290  * That means, unless there is no idle source registered, the thread will
2291  * _never_ go to sleep. So please unregister your idle source if no longer
2292  * needed.
2293  */
2294
2295 /**
2296  * ev_eloop_register_idle_cb:
2297  * @eloop: event loop
2298  * @cb: user-supplied callback
2299  * @data: user-supplied data
2300  * @flags: flags
2301  *
2302  * This register a new idle-source with the given callback and data. @cb must
2303  * not be NULL!.
2304  *
2305  * Returns: 0 on success, negative error code on failure.
2306  */
2307 int ev_eloop_register_idle_cb(struct ev_eloop *eloop, ev_idle_cb cb,
2308                               void *data, unsigned int flags)
2309 {
2310         int ret;
2311         bool os = flags & EV_ONESHOT;
2312
2313         if (!eloop || (flags & ~EV_IDLE_ALL))
2314                 return -EINVAL;
2315
2316         if ((flags & EV_SINGLE))
2317                 ret = shl_hook_add_single_cast(eloop->idlers, cb, data, os);
2318         else
2319                 ret = shl_hook_add_cast(eloop->idlers, cb, data, os);
2320
2321         if (ret)
2322                 return ret;
2323
2324         ret = write_eventfd(eloop->llog, eloop->idle_fd, 1);
2325         if (ret) {
2326                 llog_warning(eloop, "cannot increase eloop idle-counter");
2327                 shl_hook_rm_cast(eloop->idlers, cb, data);
2328                 return ret;
2329         }
2330
2331         return 0;
2332 }
2333
2334 /**
2335  * ev_eloop_unregister_idle_cb:
2336  * @eloop: event loop
2337  * @cb: user-supplied callback
2338  * @data: user-supplied data
2339  * @flags: flags
2340  *
2341  * This removes an idle-source. The arguments must be the same as for the
2342  * ev_eloop_register_idle_cb() call. If two identical callbacks are registered,
2343  * then only one is removed. It doesn't matter which one is removed, because
2344  * they are identical.
2345  */
2346 void ev_eloop_unregister_idle_cb(struct ev_eloop *eloop, ev_idle_cb cb,
2347                                  void *data, unsigned int flags)
2348 {
2349         if (!eloop || (flags & ~EV_IDLE_ALL))
2350                 return;
2351
2352         if (flags & EV_SINGLE)
2353                 shl_hook_rm_all_cast(eloop->idlers, cb, data);
2354         else
2355                 shl_hook_rm_cast(eloop->idlers, cb, data);
2356 }
2357
2358 /*
2359  * Pre-Dispatch Callbacks
2360  * A pre-dispatch cb is called before a single dispatch round is started.
2361  * You should avoid using them and instead not rely on any specific
2362  * dispatch-behavior but expect every event to be recieved asynchronously.
2363  * However, this hook is useful to integrate other limited APIs into this event
2364  * loop if they do not provide proper FD-abstractions.
2365  */
2366
2367 /**
2368  * ev_eloop_register_pre_cb:
2369  * @eloop: event loop
2370  * @cb: user-supplied callback
2371  * @data: user-supplied data
2372  *
2373  * This register a new pre-cb with the given callback and data. @cb must
2374  * not be NULL!.
2375  *
2376  * Returns: 0 on success, negative error code on failure.
2377  */
2378 int ev_eloop_register_pre_cb(struct ev_eloop *eloop, ev_idle_cb cb,
2379                              void *data)
2380 {
2381         if (!eloop)
2382                 return -EINVAL;
2383
2384         return shl_hook_add_cast(eloop->pres, cb, data, false);
2385 }
2386
2387 /**
2388  * ev_eloop_unregister_pre_cb:
2389  * @eloop: event loop
2390  * @cb: user-supplied callback
2391  * @data: user-supplied data
2392  *
2393  * This removes a pre-cb. The arguments must be the same as for the
2394  * ev_eloop_register_pre_cb() call. If two identical callbacks are registered,
2395  * then only one is removed. It doesn't matter which one is removed, because
2396  * they are identical.
2397  */
2398 void ev_eloop_unregister_pre_cb(struct ev_eloop *eloop, ev_idle_cb cb,
2399                                 void *data)
2400 {
2401         if (!eloop)
2402                 return;
2403
2404         shl_hook_rm_cast(eloop->pres, cb, data);
2405 }
2406
2407 /*
2408  * Post-Dispatch Callbacks
2409  * A post-dispatch cb is called whenever a single dispatch round is complete.
2410  * You should avoid using them and instead not rely on any specific
2411  * dispatch-behavior but expect every event to be recieved asynchronously.
2412  * However, this hook is useful to integrate other limited APIs into this event
2413  * loop if they do not provide proper FD-abstractions.
2414  */
2415
2416 /**
2417  * ev_eloop_register_post_cb:
2418  * @eloop: event loop
2419  * @cb: user-supplied callback
2420  * @data: user-supplied data
2421  *
2422  * This register a new post-cb with the given callback and data. @cb must
2423  * not be NULL!.
2424  *
2425  * Returns: 0 on success, negative error code on failure.
2426  */
2427 int ev_eloop_register_post_cb(struct ev_eloop *eloop, ev_idle_cb cb,
2428                               void *data)
2429 {
2430         if (!eloop)
2431                 return -EINVAL;
2432
2433         return shl_hook_add_cast(eloop->posts, cb, data, false);
2434 }
2435
2436 /**
2437  * ev_eloop_unregister_post_cb:
2438  * @eloop: event loop
2439  * @cb: user-supplied callback
2440  * @data: user-supplied data
2441  *
2442  * This removes a post-cb. The arguments must be the same as for the
2443  * ev_eloop_register_post_cb() call. If two identical callbacks are registered,
2444  * then only one is removed. It doesn't matter which one is removed, because
2445  * they are identical.
2446  */
2447 void ev_eloop_unregister_post_cb(struct ev_eloop *eloop, ev_idle_cb cb,
2448                                  void *data)
2449 {
2450         if (!eloop)
2451                 return;
2452
2453         shl_hook_rm_cast(eloop->posts, cb, data);
2454 }