chiark / gitweb /
event: add new kind of event source called "post"
[elogind.git] / src / libsystemd / sd-event / sd-event.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <sys/epoll.h>
23 #include <sys/timerfd.h>
24 #include <sys/wait.h>
25 #include <pthread.h>
26
27 #include "sd-id128.h"
28 #include "sd-daemon.h"
29 #include "macro.h"
30 #include "prioq.h"
31 #include "hashmap.h"
32 #include "util.h"
33 #include "time-util.h"
34 #include "missing.h"
35 #include "set.h"
36
37 #include "sd-event.h"
38
39 #define EPOLL_QUEUE_MAX 512U
40 #define DEFAULT_ACCURACY_USEC (250 * USEC_PER_MSEC)
41
42 typedef enum EventSourceType {
43         SOURCE_IO,
44         SOURCE_MONOTONIC,
45         SOURCE_REALTIME,
46         SOURCE_SIGNAL,
47         SOURCE_CHILD,
48         SOURCE_DEFER,
49         SOURCE_POST,
50         SOURCE_EXIT,
51         SOURCE_WATCHDOG
52 } EventSourceType;
53
54 struct sd_event_source {
55         unsigned n_ref;
56
57         sd_event *event;
58         void *userdata;
59         sd_event_handler_t prepare;
60
61         EventSourceType type:4;
62         int enabled:3;
63         bool pending:1;
64         bool dispatching:1;
65
66         int64_t priority;
67         unsigned pending_index;
68         unsigned prepare_index;
69         unsigned pending_iteration;
70         unsigned prepare_iteration;
71
72         union {
73                 struct {
74                         sd_event_io_handler_t callback;
75                         int fd;
76                         uint32_t events;
77                         uint32_t revents;
78                         bool registered:1;
79                 } io;
80                 struct {
81                         sd_event_time_handler_t callback;
82                         usec_t next, accuracy;
83                         unsigned earliest_index;
84                         unsigned latest_index;
85                 } time;
86                 struct {
87                         sd_event_signal_handler_t callback;
88                         struct signalfd_siginfo siginfo;
89                         int sig;
90                 } signal;
91                 struct {
92                         sd_event_child_handler_t callback;
93                         siginfo_t siginfo;
94                         pid_t pid;
95                         int options;
96                 } child;
97                 struct {
98                         sd_event_handler_t callback;
99                 } defer;
100                 struct {
101                         sd_event_handler_t callback;
102                 } post;
103                 struct {
104                         sd_event_handler_t callback;
105                         unsigned prioq_index;
106                 } exit;
107         };
108 };
109
110 struct sd_event {
111         unsigned n_ref;
112
113         int epoll_fd;
114         int signal_fd;
115         int realtime_fd;
116         int monotonic_fd;
117         int watchdog_fd;
118
119         Prioq *pending;
120         Prioq *prepare;
121
122         /* For both clocks we maintain two priority queues each, one
123          * ordered for the earliest times the events may be
124          * dispatched, and one ordered by the latest times they must
125          * have been dispatched. The range between the top entries in
126          * the two prioqs is the time window we can freely schedule
127          * wakeups in */
128         Prioq *monotonic_earliest;
129         Prioq *monotonic_latest;
130         Prioq *realtime_earliest;
131         Prioq *realtime_latest;
132
133         usec_t realtime_next, monotonic_next;
134         usec_t perturb;
135
136         sigset_t sigset;
137         sd_event_source **signal_sources;
138
139         Hashmap *child_sources;
140         unsigned n_enabled_child_sources;
141
142         Set *post_sources;
143
144         Prioq *exit;
145
146         pid_t original_pid;
147
148         unsigned iteration;
149         dual_timestamp timestamp;
150         int state;
151
152         bool exit_requested:1;
153         bool need_process_child:1;
154         bool watchdog:1;
155
156         int exit_code;
157
158         pid_t tid;
159         sd_event **default_event_ptr;
160
161         usec_t watchdog_last, watchdog_period;
162
163         unsigned n_sources;
164 };
165
166 static int pending_prioq_compare(const void *a, const void *b) {
167         const sd_event_source *x = a, *y = b;
168
169         assert(x->pending);
170         assert(y->pending);
171
172         /* Enabled ones first */
173         if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
174                 return -1;
175         if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
176                 return 1;
177
178         /* Lower priority values first */
179         if (x->priority < y->priority)
180                 return -1;
181         if (x->priority > y->priority)
182                 return 1;
183
184         /* Older entries first */
185         if (x->pending_iteration < y->pending_iteration)
186                 return -1;
187         if (x->pending_iteration > y->pending_iteration)
188                 return 1;
189
190         /* Stability for the rest */
191         if (x < y)
192                 return -1;
193         if (x > y)
194                 return 1;
195
196         return 0;
197 }
198
199 static int prepare_prioq_compare(const void *a, const void *b) {
200         const sd_event_source *x = a, *y = b;
201
202         assert(x->prepare);
203         assert(y->prepare);
204
205         /* Move most recently prepared ones last, so that we can stop
206          * preparing as soon as we hit one that has already been
207          * prepared in the current iteration */
208         if (x->prepare_iteration < y->prepare_iteration)
209                 return -1;
210         if (x->prepare_iteration > y->prepare_iteration)
211                 return 1;
212
213         /* Enabled ones first */
214         if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
215                 return -1;
216         if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
217                 return 1;
218
219         /* Lower priority values first */
220         if (x->priority < y->priority)
221                 return -1;
222         if (x->priority > y->priority)
223                 return 1;
224
225         /* Stability for the rest */
226         if (x < y)
227                 return -1;
228         if (x > y)
229                 return 1;
230
231         return 0;
232 }
233
234 static int earliest_time_prioq_compare(const void *a, const void *b) {
235         const sd_event_source *x = a, *y = b;
236
237         assert(x->type == SOURCE_MONOTONIC || x->type == SOURCE_REALTIME);
238         assert(y->type == SOURCE_MONOTONIC || y->type == SOURCE_REALTIME);
239
240         /* Enabled ones first */
241         if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
242                 return -1;
243         if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
244                 return 1;
245
246         /* Move the pending ones to the end */
247         if (!x->pending && y->pending)
248                 return -1;
249         if (x->pending && !y->pending)
250                 return 1;
251
252         /* Order by time */
253         if (x->time.next < y->time.next)
254                 return -1;
255         if (x->time.next > y->time.next)
256                 return 1;
257
258         /* Stability for the rest */
259         if (x < y)
260                 return -1;
261         if (x > y)
262                 return 1;
263
264         return 0;
265 }
266
267 static int latest_time_prioq_compare(const void *a, const void *b) {
268         const sd_event_source *x = a, *y = b;
269
270         assert((x->type == SOURCE_MONOTONIC && y->type == SOURCE_MONOTONIC) ||
271                (x->type == SOURCE_REALTIME && y->type == SOURCE_REALTIME));
272
273         /* Enabled ones first */
274         if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
275                 return -1;
276         if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
277                 return 1;
278
279         /* Move the pending ones to the end */
280         if (!x->pending && y->pending)
281                 return -1;
282         if (x->pending && !y->pending)
283                 return 1;
284
285         /* Order by time */
286         if (x->time.next + x->time.accuracy < y->time.next + y->time.accuracy)
287                 return -1;
288         if (x->time.next + x->time.accuracy > y->time.next + y->time.accuracy)
289                 return 1;
290
291         /* Stability for the rest */
292         if (x < y)
293                 return -1;
294         if (x > y)
295                 return 1;
296
297         return 0;
298 }
299
300 static int exit_prioq_compare(const void *a, const void *b) {
301         const sd_event_source *x = a, *y = b;
302
303         assert(x->type == SOURCE_EXIT);
304         assert(y->type == SOURCE_EXIT);
305
306         /* Enabled ones first */
307         if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
308                 return -1;
309         if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
310                 return 1;
311
312         /* Lower priority values first */
313         if (x->priority < y->priority)
314                 return -1;
315         if (x->priority > y->priority)
316                 return 1;
317
318         /* Stability for the rest */
319         if (x < y)
320                 return -1;
321         if (x > y)
322                 return 1;
323
324         return 0;
325 }
326
327 static void event_free(sd_event *e) {
328         assert(e);
329         assert(e->n_sources == 0);
330
331         if (e->default_event_ptr)
332                 *(e->default_event_ptr) = NULL;
333
334         if (e->epoll_fd >= 0)
335                 close_nointr_nofail(e->epoll_fd);
336
337         if (e->signal_fd >= 0)
338                 close_nointr_nofail(e->signal_fd);
339
340         if (e->realtime_fd >= 0)
341                 close_nointr_nofail(e->realtime_fd);
342
343         if (e->monotonic_fd >= 0)
344                 close_nointr_nofail(e->monotonic_fd);
345
346         if (e->watchdog_fd >= 0)
347                 close_nointr_nofail(e->watchdog_fd);
348
349         prioq_free(e->pending);
350         prioq_free(e->prepare);
351         prioq_free(e->monotonic_earliest);
352         prioq_free(e->monotonic_latest);
353         prioq_free(e->realtime_earliest);
354         prioq_free(e->realtime_latest);
355         prioq_free(e->exit);
356
357         free(e->signal_sources);
358
359         hashmap_free(e->child_sources);
360         set_free(e->post_sources);
361         free(e);
362 }
363
364 _public_ int sd_event_new(sd_event** ret) {
365         sd_event *e;
366         int r;
367
368         assert_return(ret, -EINVAL);
369
370         e = new0(sd_event, 1);
371         if (!e)
372                 return -ENOMEM;
373
374         e->n_ref = 1;
375         e->signal_fd = e->realtime_fd = e->monotonic_fd = e->watchdog_fd = e->epoll_fd = -1;
376         e->realtime_next = e->monotonic_next = (usec_t) -1;
377         e->original_pid = getpid();
378
379         assert_se(sigemptyset(&e->sigset) == 0);
380
381         e->pending = prioq_new(pending_prioq_compare);
382         if (!e->pending) {
383                 r = -ENOMEM;
384                 goto fail;
385         }
386
387         e->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
388         if (e->epoll_fd < 0) {
389                 r = -errno;
390                 goto fail;
391         }
392
393         *ret = e;
394         return 0;
395
396 fail:
397         event_free(e);
398         return r;
399 }
400
401 _public_ sd_event* sd_event_ref(sd_event *e) {
402         assert_return(e, NULL);
403
404         assert(e->n_ref >= 1);
405         e->n_ref++;
406
407         return e;
408 }
409
410 _public_ sd_event* sd_event_unref(sd_event *e) {
411
412         if (!e)
413                 return NULL;
414
415         assert(e->n_ref >= 1);
416         e->n_ref--;
417
418         if (e->n_ref <= 0)
419                 event_free(e);
420
421         return NULL;
422 }
423
424 static bool event_pid_changed(sd_event *e) {
425         assert(e);
426
427         /* We don't support people creating am event loop and keeping
428          * it around over a fork(). Let's complain. */
429
430         return e->original_pid != getpid();
431 }
432
433 static int source_io_unregister(sd_event_source *s) {
434         int r;
435
436         assert(s);
437         assert(s->type == SOURCE_IO);
438
439         if (!s->io.registered)
440                 return 0;
441
442         r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, s->io.fd, NULL);
443         if (r < 0)
444                 return -errno;
445
446         s->io.registered = false;
447         return 0;
448 }
449
450 static int source_io_register(
451                 sd_event_source *s,
452                 int enabled,
453                 uint32_t events) {
454
455         struct epoll_event ev = {};
456         int r;
457
458         assert(s);
459         assert(s->type == SOURCE_IO);
460         assert(enabled != SD_EVENT_OFF);
461
462         ev.events = events;
463         ev.data.ptr = s;
464
465         if (enabled == SD_EVENT_ONESHOT)
466                 ev.events |= EPOLLONESHOT;
467
468         if (s->io.registered)
469                 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_MOD, s->io.fd, &ev);
470         else
471                 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_ADD, s->io.fd, &ev);
472
473         if (r < 0)
474                 return -errno;
475
476         s->io.registered = true;
477
478         return 0;
479 }
480
481 static void source_free(sd_event_source *s) {
482         assert(s);
483
484         if (s->event) {
485                 assert(s->event->n_sources > 0);
486
487                 switch (s->type) {
488
489                 case SOURCE_IO:
490                         if (s->io.fd >= 0)
491                                 source_io_unregister(s);
492
493                         break;
494
495                 case SOURCE_MONOTONIC:
496                         prioq_remove(s->event->monotonic_earliest, s, &s->time.earliest_index);
497                         prioq_remove(s->event->monotonic_latest, s, &s->time.latest_index);
498                         break;
499
500                 case SOURCE_REALTIME:
501                         prioq_remove(s->event->realtime_earliest, s, &s->time.earliest_index);
502                         prioq_remove(s->event->realtime_latest, s, &s->time.latest_index);
503                         break;
504
505                 case SOURCE_SIGNAL:
506                         if (s->signal.sig > 0) {
507                                 if (s->signal.sig != SIGCHLD || s->event->n_enabled_child_sources == 0)
508                                         assert_se(sigdelset(&s->event->sigset, s->signal.sig) == 0);
509
510                                 if (s->event->signal_sources)
511                                         s->event->signal_sources[s->signal.sig] = NULL;
512                         }
513
514                         break;
515
516                 case SOURCE_CHILD:
517                         if (s->child.pid > 0) {
518                                 if (s->enabled != SD_EVENT_OFF) {
519                                         assert(s->event->n_enabled_child_sources > 0);
520                                         s->event->n_enabled_child_sources--;
521                                 }
522
523                                 if (!s->event->signal_sources || !s->event->signal_sources[SIGCHLD])
524                                         assert_se(sigdelset(&s->event->sigset, SIGCHLD) == 0);
525
526                                 hashmap_remove(s->event->child_sources, INT_TO_PTR(s->child.pid));
527                         }
528
529                         break;
530
531                 case SOURCE_DEFER:
532                         /* nothing */
533                         break;
534
535                 case SOURCE_POST:
536                         set_remove(s->event->post_sources, s);
537                         break;
538
539                 case SOURCE_EXIT:
540                         prioq_remove(s->event->exit, s, &s->exit.prioq_index);
541                         break;
542
543                 case SOURCE_WATCHDOG:
544                         assert_not_reached("Wut? I shouldn't exist.");
545                 }
546
547                 if (s->pending)
548                         prioq_remove(s->event->pending, s, &s->pending_index);
549
550                 if (s->prepare)
551                         prioq_remove(s->event->prepare, s, &s->prepare_index);
552
553                 s->event->n_sources--;
554                 sd_event_unref(s->event);
555         }
556
557         free(s);
558 }
559
560 static int source_set_pending(sd_event_source *s, bool b) {
561         int r;
562
563         assert(s);
564         assert(s->type != SOURCE_EXIT);
565
566         if (s->pending == b)
567                 return 0;
568
569         s->pending = b;
570
571         if (b) {
572                 s->pending_iteration = s->event->iteration;
573
574                 r = prioq_put(s->event->pending, s, &s->pending_index);
575                 if (r < 0) {
576                         s->pending = false;
577                         return r;
578                 }
579         } else
580                 assert_se(prioq_remove(s->event->pending, s, &s->pending_index));
581
582         if (s->type == SOURCE_REALTIME) {
583                 prioq_reshuffle(s->event->realtime_earliest, s, &s->time.earliest_index);
584                 prioq_reshuffle(s->event->realtime_latest, s, &s->time.latest_index);
585         } else if (s->type == SOURCE_MONOTONIC) {
586                 prioq_reshuffle(s->event->monotonic_earliest, s, &s->time.earliest_index);
587                 prioq_reshuffle(s->event->monotonic_latest, s, &s->time.latest_index);
588         }
589
590         return 0;
591 }
592
593 static sd_event_source *source_new(sd_event *e, EventSourceType type) {
594         sd_event_source *s;
595
596         assert(e);
597
598         s = new0(sd_event_source, 1);
599         if (!s)
600                 return NULL;
601
602         s->n_ref = 1;
603         s->event = sd_event_ref(e);
604         s->type = type;
605         s->pending_index = s->prepare_index = PRIOQ_IDX_NULL;
606
607         e->n_sources ++;
608
609         return s;
610 }
611
612 _public_ int sd_event_add_io(
613                 sd_event *e,
614                 sd_event_source **ret,
615                 int fd,
616                 uint32_t events,
617                 sd_event_io_handler_t callback,
618                 void *userdata) {
619
620         sd_event_source *s;
621         int r;
622
623         assert_return(e, -EINVAL);
624         assert_return(fd >= 0, -EINVAL);
625         assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
626         assert_return(callback, -EINVAL);
627         assert_return(ret, -EINVAL);
628         assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
629         assert_return(!event_pid_changed(e), -ECHILD);
630
631         s = source_new(e, SOURCE_IO);
632         if (!s)
633                 return -ENOMEM;
634
635         s->io.fd = fd;
636         s->io.events = events;
637         s->io.callback = callback;
638         s->userdata = userdata;
639         s->enabled = SD_EVENT_ON;
640
641         r = source_io_register(s, s->enabled, events);
642         if (r < 0) {
643                 source_free(s);
644                 return -errno;
645         }
646
647         *ret = s;
648         return 0;
649 }
650
651 static int event_setup_timer_fd(
652                 sd_event *e,
653                 EventSourceType type,
654                 int *timer_fd,
655                 clockid_t id) {
656
657         sd_id128_t bootid = {};
658         struct epoll_event ev = {};
659         int r, fd;
660
661         assert(e);
662         assert(timer_fd);
663
664         if (_likely_(*timer_fd >= 0))
665                 return 0;
666
667         fd = timerfd_create(id, TFD_NONBLOCK|TFD_CLOEXEC);
668         if (fd < 0)
669                 return -errno;
670
671         ev.events = EPOLLIN;
672         ev.data.ptr = INT_TO_PTR(type);
673
674         r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, fd, &ev);
675         if (r < 0) {
676                 close_nointr_nofail(fd);
677                 return -errno;
678         }
679
680         /* When we sleep for longer, we try to realign the wakeup to
681            the same time wihtin each minute/second/250ms, so that
682            events all across the system can be coalesced into a single
683            CPU wakeup. However, let's take some system-specific
684            randomness for this value, so that in a network of systems
685            with synced clocks timer events are distributed a
686            bit. Here, we calculate a perturbation usec offset from the
687            boot ID. */
688
689         if (sd_id128_get_boot(&bootid) >= 0)
690                 e->perturb = (bootid.qwords[0] ^ bootid.qwords[1]) % USEC_PER_MINUTE;
691
692         *timer_fd = fd;
693         return 0;
694 }
695
696 static int event_add_time_internal(
697                 sd_event *e,
698                 sd_event_source **ret,
699                 EventSourceType type,
700                 int *timer_fd,
701                 clockid_t id,
702                 Prioq **earliest,
703                 Prioq **latest,
704                 uint64_t usec,
705                 uint64_t accuracy,
706                 sd_event_time_handler_t callback,
707                 void *userdata) {
708
709         sd_event_source *s;
710         int r;
711
712         assert_return(e, -EINVAL);
713         assert_return(callback, -EINVAL);
714         assert_return(ret, -EINVAL);
715         assert_return(usec != (uint64_t) -1, -EINVAL);
716         assert_return(accuracy != (uint64_t) -1, -EINVAL);
717         assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
718         assert_return(!event_pid_changed(e), -ECHILD);
719
720         assert(timer_fd);
721         assert(earliest);
722         assert(latest);
723
724         if (!*earliest) {
725                 *earliest = prioq_new(earliest_time_prioq_compare);
726                 if (!*earliest)
727                         return -ENOMEM;
728         }
729
730         if (!*latest) {
731                 *latest = prioq_new(latest_time_prioq_compare);
732                 if (!*latest)
733                         return -ENOMEM;
734         }
735
736         if (*timer_fd < 0) {
737                 r = event_setup_timer_fd(e, type, timer_fd, id);
738                 if (r < 0)
739                         return r;
740         }
741
742         s = source_new(e, type);
743         if (!s)
744                 return -ENOMEM;
745
746         s->time.next = usec;
747         s->time.accuracy = accuracy == 0 ? DEFAULT_ACCURACY_USEC : accuracy;
748         s->time.callback = callback;
749         s->time.earliest_index = s->time.latest_index = PRIOQ_IDX_NULL;
750         s->userdata = userdata;
751         s->enabled = SD_EVENT_ONESHOT;
752
753         r = prioq_put(*earliest, s, &s->time.earliest_index);
754         if (r < 0)
755                 goto fail;
756
757         r = prioq_put(*latest, s, &s->time.latest_index);
758         if (r < 0)
759                 goto fail;
760
761         *ret = s;
762         return 0;
763
764 fail:
765         source_free(s);
766         return r;
767 }
768
769 _public_ int sd_event_add_monotonic(sd_event *e,
770                                     sd_event_source **ret,
771                                     uint64_t usec,
772                                     uint64_t accuracy,
773                                     sd_event_time_handler_t callback,
774                                     void *userdata) {
775
776         return event_add_time_internal(e, ret, SOURCE_MONOTONIC, &e->monotonic_fd, CLOCK_MONOTONIC, &e->monotonic_earliest, &e->monotonic_latest, usec, accuracy, callback, userdata);
777 }
778
779 _public_ int sd_event_add_realtime(sd_event *e,
780                                    sd_event_source **ret,
781                                    uint64_t usec,
782                                    uint64_t accuracy,
783                                    sd_event_time_handler_t callback,
784                                    void *userdata) {
785
786         return event_add_time_internal(e, ret, SOURCE_REALTIME, &e->realtime_fd, CLOCK_REALTIME, &e->realtime_earliest, &e->realtime_latest, usec, accuracy, callback, userdata);
787 }
788
789 static int event_update_signal_fd(sd_event *e) {
790         struct epoll_event ev = {};
791         bool add_to_epoll;
792         int r;
793
794         assert(e);
795
796         add_to_epoll = e->signal_fd < 0;
797
798         r = signalfd(e->signal_fd, &e->sigset, SFD_NONBLOCK|SFD_CLOEXEC);
799         if (r < 0)
800                 return -errno;
801
802         e->signal_fd = r;
803
804         if (!add_to_epoll)
805                 return 0;
806
807         ev.events = EPOLLIN;
808         ev.data.ptr = INT_TO_PTR(SOURCE_SIGNAL);
809
810         r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->signal_fd, &ev);
811         if (r < 0) {
812                 close_nointr_nofail(e->signal_fd);
813                 e->signal_fd = -1;
814
815                 return -errno;
816         }
817
818         return 0;
819 }
820
821 _public_ int sd_event_add_signal(
822                 sd_event *e,
823                 sd_event_source **ret,
824                 int sig,
825                 sd_event_signal_handler_t callback,
826                 void *userdata) {
827
828         sd_event_source *s;
829         sigset_t ss;
830         int r;
831
832         assert_return(e, -EINVAL);
833         assert_return(sig > 0, -EINVAL);
834         assert_return(sig < _NSIG, -EINVAL);
835         assert_return(callback, -EINVAL);
836         assert_return(ret, -EINVAL);
837         assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
838         assert_return(!event_pid_changed(e), -ECHILD);
839
840         r = pthread_sigmask(SIG_SETMASK, NULL, &ss);
841         if (r < 0)
842                 return -errno;
843
844         if (!sigismember(&ss, sig))
845                 return -EBUSY;
846
847         if (!e->signal_sources) {
848                 e->signal_sources = new0(sd_event_source*, _NSIG);
849                 if (!e->signal_sources)
850                         return -ENOMEM;
851         } else if (e->signal_sources[sig])
852                 return -EBUSY;
853
854         s = source_new(e, SOURCE_SIGNAL);
855         if (!s)
856                 return -ENOMEM;
857
858         s->signal.sig = sig;
859         s->signal.callback = callback;
860         s->userdata = userdata;
861         s->enabled = SD_EVENT_ON;
862
863         e->signal_sources[sig] = s;
864         assert_se(sigaddset(&e->sigset, sig) == 0);
865
866         if (sig != SIGCHLD || e->n_enabled_child_sources == 0) {
867                 r = event_update_signal_fd(e);
868                 if (r < 0) {
869                         source_free(s);
870                         return r;
871                 }
872         }
873
874         *ret = s;
875         return 0;
876 }
877
878 _public_ int sd_event_add_child(
879                 sd_event *e,
880                 sd_event_source **ret,
881                 pid_t pid,
882                 int options,
883                 sd_event_child_handler_t callback,
884                 void *userdata) {
885
886         sd_event_source *s;
887         int r;
888
889         assert_return(e, -EINVAL);
890         assert_return(pid > 1, -EINVAL);
891         assert_return(!(options & ~(WEXITED|WSTOPPED|WCONTINUED)), -EINVAL);
892         assert_return(options != 0, -EINVAL);
893         assert_return(callback, -EINVAL);
894         assert_return(ret, -EINVAL);
895         assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
896         assert_return(!event_pid_changed(e), -ECHILD);
897
898         r = hashmap_ensure_allocated(&e->child_sources, trivial_hash_func, trivial_compare_func);
899         if (r < 0)
900                 return r;
901
902         if (hashmap_contains(e->child_sources, INT_TO_PTR(pid)))
903                 return -EBUSY;
904
905         s = source_new(e, SOURCE_CHILD);
906         if (!s)
907                 return -ENOMEM;
908
909         s->child.pid = pid;
910         s->child.options = options;
911         s->child.callback = callback;
912         s->userdata = userdata;
913         s->enabled = SD_EVENT_ONESHOT;
914
915         r = hashmap_put(e->child_sources, INT_TO_PTR(pid), s);
916         if (r < 0) {
917                 source_free(s);
918                 return r;
919         }
920
921         e->n_enabled_child_sources ++;
922
923         assert_se(sigaddset(&e->sigset, SIGCHLD) == 0);
924
925         if (!e->signal_sources || !e->signal_sources[SIGCHLD]) {
926                 r = event_update_signal_fd(e);
927                 if (r < 0) {
928                         source_free(s);
929                         return -errno;
930                 }
931         }
932
933         e->need_process_child = true;
934
935         *ret = s;
936         return 0;
937 }
938
939 _public_ int sd_event_add_defer(
940                 sd_event *e,
941                 sd_event_source **ret,
942                 sd_event_handler_t callback,
943                 void *userdata) {
944
945         sd_event_source *s;
946         int r;
947
948         assert_return(e, -EINVAL);
949         assert_return(callback, -EINVAL);
950         assert_return(ret, -EINVAL);
951         assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
952         assert_return(!event_pid_changed(e), -ECHILD);
953
954         s = source_new(e, SOURCE_DEFER);
955         if (!s)
956                 return -ENOMEM;
957
958         s->defer.callback = callback;
959         s->userdata = userdata;
960         s->enabled = SD_EVENT_ONESHOT;
961
962         r = source_set_pending(s, true);
963         if (r < 0) {
964                 source_free(s);
965                 return r;
966         }
967
968         *ret = s;
969         return 0;
970 }
971
972 _public_ int sd_event_add_post(
973                 sd_event *e,
974                 sd_event_source **ret,
975                 sd_event_handler_t callback,
976                 void *userdata) {
977
978         sd_event_source *s;
979         int r;
980
981         assert_return(e, -EINVAL);
982         assert_return(callback, -EINVAL);
983         assert_return(ret, -EINVAL);
984         assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
985         assert_return(!event_pid_changed(e), -ECHILD);
986
987         r = set_ensure_allocated(&e->post_sources, trivial_hash_func, trivial_compare_func);
988         if (r < 0)
989                 return r;
990
991         s = source_new(e, SOURCE_POST);
992         if (!s)
993                 return -ENOMEM;
994
995         s->post.callback = callback;
996         s->userdata = userdata;
997         s->enabled = SD_EVENT_ON;
998
999         r = set_put(e->post_sources, s);
1000         if (r < 0) {
1001                 source_free(s);
1002                 return r;
1003         }
1004
1005         *ret = s;
1006         return 0;
1007 }
1008
1009 _public_ int sd_event_add_exit(
1010                 sd_event *e,
1011                 sd_event_source **ret,
1012                 sd_event_handler_t callback,
1013                 void *userdata) {
1014
1015         sd_event_source *s;
1016         int r;
1017
1018         assert_return(e, -EINVAL);
1019         assert_return(callback, -EINVAL);
1020         assert_return(ret, -EINVAL);
1021         assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1022         assert_return(!event_pid_changed(e), -ECHILD);
1023
1024         if (!e->exit) {
1025                 e->exit = prioq_new(exit_prioq_compare);
1026                 if (!e->exit)
1027                         return -ENOMEM;
1028         }
1029
1030         s = source_new(e, SOURCE_EXIT);
1031         if (!s)
1032                 return -ENOMEM;
1033
1034         s->exit.callback = callback;
1035         s->userdata = userdata;
1036         s->exit.prioq_index = PRIOQ_IDX_NULL;
1037         s->enabled = SD_EVENT_ONESHOT;
1038
1039         r = prioq_put(s->event->exit, s, &s->exit.prioq_index);
1040         if (r < 0) {
1041                 source_free(s);
1042                 return r;
1043         }
1044
1045         *ret = s;
1046         return 0;
1047 }
1048
1049 _public_ sd_event_source* sd_event_source_ref(sd_event_source *s) {
1050         assert_return(s, NULL);
1051
1052         assert(s->n_ref >= 1);
1053         s->n_ref++;
1054
1055         return s;
1056 }
1057
1058 _public_ sd_event_source* sd_event_source_unref(sd_event_source *s) {
1059
1060         if (!s)
1061                 return NULL;
1062
1063         assert(s->n_ref >= 1);
1064         s->n_ref--;
1065
1066         if (s->n_ref <= 0) {
1067                 /* Here's a special hack: when we are called from a
1068                  * dispatch handler we won't free the event source
1069                  * immediately, but we will detach the fd from the
1070                  * epoll. This way it is safe for the caller to unref
1071                  * the event source and immediately close the fd, but
1072                  * we still retain a valid event source object after
1073                  * the callback. */
1074
1075                 if (s->dispatching) {
1076                         if (s->type == SOURCE_IO)
1077                                 source_io_unregister(s);
1078                 } else
1079                         source_free(s);
1080         }
1081
1082         return NULL;
1083 }
1084
1085 _public_ sd_event *sd_event_source_get_event(sd_event_source *s) {
1086         assert_return(s, NULL);
1087
1088         return s->event;
1089 }
1090
1091 _public_ int sd_event_source_get_pending(sd_event_source *s) {
1092         assert_return(s, -EINVAL);
1093         assert_return(s->type != SOURCE_EXIT, -EDOM);
1094         assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1095         assert_return(!event_pid_changed(s->event), -ECHILD);
1096
1097         return s->pending;
1098 }
1099
1100 _public_ int sd_event_source_get_io_fd(sd_event_source *s) {
1101         assert_return(s, -EINVAL);
1102         assert_return(s->type == SOURCE_IO, -EDOM);
1103         assert_return(!event_pid_changed(s->event), -ECHILD);
1104
1105         return s->io.fd;
1106 }
1107
1108 _public_ int sd_event_source_set_io_fd(sd_event_source *s, int fd) {
1109         int r;
1110
1111         assert_return(s, -EINVAL);
1112         assert_return(fd >= 0, -EINVAL);
1113         assert_return(s->type == SOURCE_IO, -EDOM);
1114         assert_return(!event_pid_changed(s->event), -ECHILD);
1115
1116         if (s->io.fd == fd)
1117                 return 0;
1118
1119         if (s->enabled == SD_EVENT_OFF) {
1120                 s->io.fd = fd;
1121                 s->io.registered = false;
1122         } else {
1123                 int saved_fd;
1124
1125                 saved_fd = s->io.fd;
1126                 assert(s->io.registered);
1127
1128                 s->io.fd = fd;
1129                 s->io.registered = false;
1130
1131                 r = source_io_register(s, s->enabled, s->io.events);
1132                 if (r < 0) {
1133                         s->io.fd = saved_fd;
1134                         s->io.registered = true;
1135                         return r;
1136                 }
1137
1138                 epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, saved_fd, NULL);
1139         }
1140
1141         return 0;
1142 }
1143
1144 _public_ int sd_event_source_get_io_events(sd_event_source *s, uint32_t* events) {
1145         assert_return(s, -EINVAL);
1146         assert_return(events, -EINVAL);
1147         assert_return(s->type == SOURCE_IO, -EDOM);
1148         assert_return(!event_pid_changed(s->event), -ECHILD);
1149
1150         *events = s->io.events;
1151         return 0;
1152 }
1153
1154 _public_ int sd_event_source_set_io_events(sd_event_source *s, uint32_t events) {
1155         int r;
1156
1157         assert_return(s, -EINVAL);
1158         assert_return(s->type == SOURCE_IO, -EDOM);
1159         assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
1160         assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1161         assert_return(!event_pid_changed(s->event), -ECHILD);
1162
1163         if (s->io.events == events)
1164                 return 0;
1165
1166         if (s->enabled != SD_EVENT_OFF) {
1167                 r = source_io_register(s, s->enabled, events);
1168                 if (r < 0)
1169                         return r;
1170         }
1171
1172         s->io.events = events;
1173         source_set_pending(s, false);
1174
1175         return 0;
1176 }
1177
1178 _public_ int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents) {
1179         assert_return(s, -EINVAL);
1180         assert_return(revents, -EINVAL);
1181         assert_return(s->type == SOURCE_IO, -EDOM);
1182         assert_return(s->pending, -ENODATA);
1183         assert_return(!event_pid_changed(s->event), -ECHILD);
1184
1185         *revents = s->io.revents;
1186         return 0;
1187 }
1188
1189 _public_ int sd_event_source_get_signal(sd_event_source *s) {
1190         assert_return(s, -EINVAL);
1191         assert_return(s->type == SOURCE_SIGNAL, -EDOM);
1192         assert_return(!event_pid_changed(s->event), -ECHILD);
1193
1194         return s->signal.sig;
1195 }
1196
1197 _public_ int sd_event_source_get_priority(sd_event_source *s, int64_t *priority) {
1198         assert_return(s, -EINVAL);
1199         assert_return(!event_pid_changed(s->event), -ECHILD);
1200
1201         return s->priority;
1202 }
1203
1204 _public_ int sd_event_source_set_priority(sd_event_source *s, int64_t priority) {
1205         assert_return(s, -EINVAL);
1206         assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1207         assert_return(!event_pid_changed(s->event), -ECHILD);
1208
1209         if (s->priority == priority)
1210                 return 0;
1211
1212         s->priority = priority;
1213
1214         if (s->pending)
1215                 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1216
1217         if (s->prepare)
1218                 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1219
1220         if (s->type == SOURCE_EXIT)
1221                 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1222
1223         return 0;
1224 }
1225
1226 _public_ int sd_event_source_get_enabled(sd_event_source *s, int *m) {
1227         assert_return(s, -EINVAL);
1228         assert_return(m, -EINVAL);
1229         assert_return(!event_pid_changed(s->event), -ECHILD);
1230
1231         *m = s->enabled;
1232         return 0;
1233 }
1234
1235 _public_ int sd_event_source_set_enabled(sd_event_source *s, int m) {
1236         int r;
1237
1238         assert_return(s, -EINVAL);
1239         assert_return(m == SD_EVENT_OFF || m == SD_EVENT_ON || m == SD_EVENT_ONESHOT, -EINVAL);
1240         assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1241         assert_return(!event_pid_changed(s->event), -ECHILD);
1242
1243         if (s->enabled == m)
1244                 return 0;
1245
1246         if (m == SD_EVENT_OFF) {
1247
1248                 switch (s->type) {
1249
1250                 case SOURCE_IO:
1251                         r = source_io_unregister(s);
1252                         if (r < 0)
1253                                 return r;
1254
1255                         s->enabled = m;
1256                         break;
1257
1258                 case SOURCE_MONOTONIC:
1259                         s->enabled = m;
1260                         prioq_reshuffle(s->event->monotonic_earliest, s, &s->time.earliest_index);
1261                         prioq_reshuffle(s->event->monotonic_latest, s, &s->time.latest_index);
1262                         break;
1263
1264                 case SOURCE_REALTIME:
1265                         s->enabled = m;
1266                         prioq_reshuffle(s->event->realtime_earliest, s, &s->time.earliest_index);
1267                         prioq_reshuffle(s->event->realtime_latest, s, &s->time.latest_index);
1268                         break;
1269
1270                 case SOURCE_SIGNAL:
1271                         s->enabled = m;
1272                         if (s->signal.sig != SIGCHLD || s->event->n_enabled_child_sources == 0) {
1273                                 assert_se(sigdelset(&s->event->sigset, s->signal.sig) == 0);
1274                                 event_update_signal_fd(s->event);
1275                         }
1276
1277                         break;
1278
1279                 case SOURCE_CHILD:
1280                         s->enabled = m;
1281
1282                         assert(s->event->n_enabled_child_sources > 0);
1283                         s->event->n_enabled_child_sources--;
1284
1285                         if (!s->event->signal_sources || !s->event->signal_sources[SIGCHLD]) {
1286                                 assert_se(sigdelset(&s->event->sigset, SIGCHLD) == 0);
1287                                 event_update_signal_fd(s->event);
1288                         }
1289
1290                         break;
1291
1292                 case SOURCE_EXIT:
1293                         s->enabled = m;
1294                         prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1295                         break;
1296
1297                 case SOURCE_DEFER:
1298                 case SOURCE_POST:
1299                         s->enabled = m;
1300                         break;
1301
1302                 case SOURCE_WATCHDOG:
1303                         assert_not_reached("Wut? I shouldn't exist.");
1304                 }
1305
1306         } else {
1307                 switch (s->type) {
1308
1309                 case SOURCE_IO:
1310                         r = source_io_register(s, m, s->io.events);
1311                         if (r < 0)
1312                                 return r;
1313
1314                         s->enabled = m;
1315                         break;
1316
1317                 case SOURCE_MONOTONIC:
1318                         s->enabled = m;
1319                         prioq_reshuffle(s->event->monotonic_earliest, s, &s->time.earliest_index);
1320                         prioq_reshuffle(s->event->monotonic_latest, s, &s->time.latest_index);
1321                         break;
1322
1323                 case SOURCE_REALTIME:
1324                         s->enabled = m;
1325                         prioq_reshuffle(s->event->realtime_earliest, s, &s->time.earliest_index);
1326                         prioq_reshuffle(s->event->realtime_latest, s, &s->time.latest_index);
1327                         break;
1328
1329                 case SOURCE_SIGNAL:
1330                         s->enabled = m;
1331
1332                         if (s->signal.sig != SIGCHLD || s->event->n_enabled_child_sources == 0)  {
1333                                 assert_se(sigaddset(&s->event->sigset, s->signal.sig) == 0);
1334                                 event_update_signal_fd(s->event);
1335                         }
1336                         break;
1337
1338                 case SOURCE_CHILD:
1339                         if (s->enabled == SD_EVENT_OFF) {
1340                                 s->event->n_enabled_child_sources++;
1341
1342                                 if (!s->event->signal_sources || !s->event->signal_sources[SIGCHLD]) {
1343                                         assert_se(sigaddset(&s->event->sigset, SIGCHLD) == 0);
1344                                         event_update_signal_fd(s->event);
1345                                 }
1346                         }
1347
1348                         s->enabled = m;
1349                         break;
1350
1351                 case SOURCE_EXIT:
1352                         s->enabled = m;
1353                         prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1354                         break;
1355
1356                 case SOURCE_DEFER:
1357                 case SOURCE_POST:
1358                         s->enabled = m;
1359                         break;
1360
1361                 case SOURCE_WATCHDOG:
1362                         assert_not_reached("Wut? I shouldn't exist.");
1363                 }
1364         }
1365
1366         if (s->pending)
1367                 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1368
1369         if (s->prepare)
1370                 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1371
1372         return 0;
1373 }
1374
1375 _public_ int sd_event_source_get_time(sd_event_source *s, uint64_t *usec) {
1376         assert_return(s, -EINVAL);
1377         assert_return(usec, -EINVAL);
1378         assert_return(s->type == SOURCE_REALTIME || s->type == SOURCE_MONOTONIC, -EDOM);
1379         assert_return(!event_pid_changed(s->event), -ECHILD);
1380
1381         *usec = s->time.next;
1382         return 0;
1383 }
1384
1385 _public_ int sd_event_source_set_time(sd_event_source *s, uint64_t usec) {
1386         assert_return(s, -EINVAL);
1387         assert_return(usec != (uint64_t) -1, -EINVAL);
1388         assert_return(s->type == SOURCE_REALTIME || s->type == SOURCE_MONOTONIC, -EDOM);
1389         assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1390         assert_return(!event_pid_changed(s->event), -ECHILD);
1391
1392         s->time.next = usec;
1393
1394         source_set_pending(s, false);
1395
1396         if (s->type == SOURCE_REALTIME) {
1397                 prioq_reshuffle(s->event->realtime_earliest, s, &s->time.earliest_index);
1398                 prioq_reshuffle(s->event->realtime_latest, s, &s->time.latest_index);
1399         } else {
1400                 prioq_reshuffle(s->event->monotonic_earliest, s, &s->time.earliest_index);
1401                 prioq_reshuffle(s->event->monotonic_latest, s, &s->time.latest_index);
1402         }
1403
1404         return 0;
1405 }
1406
1407 _public_ int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec) {
1408         assert_return(s, -EINVAL);
1409         assert_return(usec, -EINVAL);
1410         assert_return(s->type == SOURCE_REALTIME || s->type == SOURCE_MONOTONIC, -EDOM);
1411         assert_return(!event_pid_changed(s->event), -ECHILD);
1412
1413         *usec = s->time.accuracy;
1414         return 0;
1415 }
1416
1417 _public_ int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec) {
1418         assert_return(s, -EINVAL);
1419         assert_return(usec != (uint64_t) -1, -EINVAL);
1420         assert_return(s->type == SOURCE_REALTIME || s->type == SOURCE_MONOTONIC, -EDOM);
1421         assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1422         assert_return(!event_pid_changed(s->event), -ECHILD);
1423
1424         if (usec == 0)
1425                 usec = DEFAULT_ACCURACY_USEC;
1426
1427         s->time.accuracy = usec;
1428
1429         source_set_pending(s, false);
1430
1431         if (s->type == SOURCE_REALTIME)
1432                 prioq_reshuffle(s->event->realtime_latest, s, &s->time.latest_index);
1433         else
1434                 prioq_reshuffle(s->event->monotonic_latest, s, &s->time.latest_index);
1435
1436         return 0;
1437 }
1438
1439 _public_ int sd_event_source_get_child_pid(sd_event_source *s, pid_t *pid) {
1440         assert_return(s, -EINVAL);
1441         assert_return(pid, -EINVAL);
1442         assert_return(s->type == SOURCE_CHILD, -EDOM);
1443         assert_return(!event_pid_changed(s->event), -ECHILD);
1444
1445         *pid = s->child.pid;
1446         return 0;
1447 }
1448
1449 _public_ int sd_event_source_set_prepare(sd_event_source *s, sd_event_handler_t callback) {
1450         int r;
1451
1452         assert_return(s, -EINVAL);
1453         assert_return(s->type != SOURCE_EXIT, -EDOM);
1454         assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1455         assert_return(!event_pid_changed(s->event), -ECHILD);
1456
1457         if (s->prepare == callback)
1458                 return 0;
1459
1460         if (callback && s->prepare) {
1461                 s->prepare = callback;
1462                 return 0;
1463         }
1464
1465         r = prioq_ensure_allocated(&s->event->prepare, prepare_prioq_compare);
1466         if (r < 0)
1467                 return r;
1468
1469         s->prepare = callback;
1470
1471         if (callback) {
1472                 r = prioq_put(s->event->prepare, s, &s->prepare_index);
1473                 if (r < 0)
1474                         return r;
1475         } else
1476                 prioq_remove(s->event->prepare, s, &s->prepare_index);
1477
1478         return 0;
1479 }
1480
1481 _public_ void* sd_event_source_get_userdata(sd_event_source *s) {
1482         assert_return(s, NULL);
1483
1484         return s->userdata;
1485 }
1486
1487 _public_ void *sd_event_source_set_userdata(sd_event_source *s, void *userdata) {
1488         void *ret;
1489
1490         assert_return(s, NULL);
1491
1492         ret = s->userdata;
1493         s->userdata = userdata;
1494
1495         return ret;
1496 }
1497
1498 static usec_t sleep_between(sd_event *e, usec_t a, usec_t b) {
1499         usec_t c;
1500         assert(e);
1501         assert(a <= b);
1502
1503         if (a <= 0)
1504                 return 0;
1505
1506         if (b <= a + 1)
1507                 return a;
1508
1509         /*
1510           Find a good time to wake up again between times a and b. We
1511           have two goals here:
1512
1513           a) We want to wake up as seldom as possible, hence prefer
1514              later times over earlier times.
1515
1516           b) But if we have to wake up, then let's make sure to
1517              dispatch as much as possible on the entire system.
1518
1519           We implement this by waking up everywhere at the same time
1520           within any given minute if we can, synchronised via the
1521           perturbation value determined from the boot ID. If we can't,
1522           then we try to find the same spot in every 10s, then 1s and
1523           then 250ms step. Otherwise, we pick the last possible time
1524           to wake up.
1525         */
1526
1527         c = (b / USEC_PER_MINUTE) * USEC_PER_MINUTE + e->perturb;
1528         if (c >= b) {
1529                 if (_unlikely_(c < USEC_PER_MINUTE))
1530                         return b;
1531
1532                 c -= USEC_PER_MINUTE;
1533         }
1534
1535         if (c >= a)
1536                 return c;
1537
1538         c = (b / (USEC_PER_SEC*10)) * (USEC_PER_SEC*10) + (e->perturb % (USEC_PER_SEC*10));
1539         if (c >= b) {
1540                 if (_unlikely_(c < USEC_PER_SEC*10))
1541                         return b;
1542
1543                 c -= USEC_PER_SEC*10;
1544         }
1545
1546         if (c >= a)
1547                 return c;
1548
1549         c = (b / USEC_PER_SEC) * USEC_PER_SEC + (e->perturb % USEC_PER_SEC);
1550         if (c >= b) {
1551                 if (_unlikely_(c < USEC_PER_SEC))
1552                         return b;
1553
1554                 c -= USEC_PER_SEC;
1555         }
1556
1557         if (c >= a)
1558                 return c;
1559
1560         c = (b / (USEC_PER_MSEC*250)) * (USEC_PER_MSEC*250) + (e->perturb % (USEC_PER_MSEC*250));
1561         if (c >= b) {
1562                 if (_unlikely_(c < USEC_PER_MSEC*250))
1563                         return b;
1564
1565                 c -= USEC_PER_MSEC*250;
1566         }
1567
1568         if (c >= a)
1569                 return c;
1570
1571         return b;
1572 }
1573
1574 static int event_arm_timer(
1575                 sd_event *e,
1576                 int timer_fd,
1577                 Prioq *earliest,
1578                 Prioq *latest,
1579                 usec_t *next) {
1580
1581         struct itimerspec its = {};
1582         sd_event_source *a, *b;
1583         usec_t t;
1584         int r;
1585
1586         assert(e);
1587         assert(next);
1588
1589         a = prioq_peek(earliest);
1590         if (!a || a->enabled == SD_EVENT_OFF) {
1591
1592                 if (timer_fd < 0)
1593                         return 0;
1594
1595                 if (*next == (usec_t) -1)
1596                         return 0;
1597
1598                 /* disarm */
1599                 r = timerfd_settime(timer_fd, TFD_TIMER_ABSTIME, &its, NULL);
1600                 if (r < 0)
1601                         return r;
1602
1603                 *next = (usec_t) -1;
1604
1605                 return 0;
1606         }
1607
1608         b = prioq_peek(latest);
1609         assert_se(b && b->enabled != SD_EVENT_OFF);
1610
1611         t = sleep_between(e, a->time.next, b->time.next + b->time.accuracy);
1612         if (*next == t)
1613                 return 0;
1614
1615         assert_se(timer_fd >= 0);
1616
1617         if (t == 0) {
1618                 /* We don' want to disarm here, just mean some time looooong ago. */
1619                 its.it_value.tv_sec = 0;
1620                 its.it_value.tv_nsec = 1;
1621         } else
1622                 timespec_store(&its.it_value, t);
1623
1624         r = timerfd_settime(timer_fd, TFD_TIMER_ABSTIME, &its, NULL);
1625         if (r < 0)
1626                 return -errno;
1627
1628         *next = t;
1629         return 0;
1630 }
1631
1632 static int process_io(sd_event *e, sd_event_source *s, uint32_t revents) {
1633         assert(e);
1634         assert(s);
1635         assert(s->type == SOURCE_IO);
1636
1637         /* If the event source was already pending, we just OR in the
1638          * new revents, otherwise we reset the value. The ORing is
1639          * necessary to handle EPOLLONESHOT events properly where
1640          * readability might happen independently of writability, and
1641          * we need to keep track of both */
1642
1643         if (s->pending)
1644                 s->io.revents |= revents;
1645         else
1646                 s->io.revents = revents;
1647
1648         return source_set_pending(s, true);
1649 }
1650
1651 static int flush_timer(sd_event *e, int fd, uint32_t events, usec_t *next) {
1652         uint64_t x;
1653         ssize_t ss;
1654
1655         assert(e);
1656         assert(fd >= 0);
1657
1658         assert_return(events == EPOLLIN, -EIO);
1659
1660         ss = read(fd, &x, sizeof(x));
1661         if (ss < 0) {
1662                 if (errno == EAGAIN || errno == EINTR)
1663                         return 0;
1664
1665                 return -errno;
1666         }
1667
1668         if (_unlikely_(ss != sizeof(x)))
1669                 return -EIO;
1670
1671         if (next)
1672                 *next = (usec_t) -1;
1673
1674         return 0;
1675 }
1676
1677 static int process_timer(
1678                 sd_event *e,
1679                 usec_t n,
1680                 Prioq *earliest,
1681                 Prioq *latest) {
1682
1683         sd_event_source *s;
1684         int r;
1685
1686         assert(e);
1687
1688         for (;;) {
1689                 s = prioq_peek(earliest);
1690                 if (!s ||
1691                     s->time.next > n ||
1692                     s->enabled == SD_EVENT_OFF ||
1693                     s->pending)
1694                         break;
1695
1696                 r = source_set_pending(s, true);
1697                 if (r < 0)
1698                         return r;
1699
1700                 prioq_reshuffle(earliest, s, &s->time.earliest_index);
1701                 prioq_reshuffle(latest, s, &s->time.latest_index);
1702         }
1703
1704         return 0;
1705 }
1706
1707 static int process_child(sd_event *e) {
1708         sd_event_source *s;
1709         Iterator i;
1710         int r;
1711
1712         assert(e);
1713
1714         e->need_process_child = false;
1715
1716         /*
1717            So, this is ugly. We iteratively invoke waitid() with P_PID
1718            + WNOHANG for each PID we wait for, instead of using
1719            P_ALL. This is because we only want to get child
1720            information of very specific child processes, and not all
1721            of them. We might not have processed the SIGCHLD even of a
1722            previous invocation and we don't want to maintain a
1723            unbounded *per-child* event queue, hence we really don't
1724            want anything flushed out of the kernel's queue that we
1725            don't care about. Since this is O(n) this means that if you
1726            have a lot of processes you probably want to handle SIGCHLD
1727            yourself.
1728
1729            We do not reap the children here (by using WNOWAIT), this
1730            is only done after the event source is dispatched so that
1731            the callback still sees the process as a zombie.
1732         */
1733
1734         HASHMAP_FOREACH(s, e->child_sources, i) {
1735                 assert(s->type == SOURCE_CHILD);
1736
1737                 if (s->pending)
1738                         continue;
1739
1740                 if (s->enabled == SD_EVENT_OFF)
1741                         continue;
1742
1743                 zero(s->child.siginfo);
1744                 r = waitid(P_PID, s->child.pid, &s->child.siginfo,
1745                            WNOHANG | (s->child.options & WEXITED ? WNOWAIT : 0) | s->child.options);
1746                 if (r < 0)
1747                         return -errno;
1748
1749                 if (s->child.siginfo.si_pid != 0) {
1750                         bool zombie =
1751                                 s->child.siginfo.si_code == CLD_EXITED ||
1752                                 s->child.siginfo.si_code == CLD_KILLED ||
1753                                 s->child.siginfo.si_code == CLD_DUMPED;
1754
1755                         if (!zombie && (s->child.options & WEXITED)) {
1756                                 /* If the child isn't dead then let's
1757                                  * immediately remove the state change
1758                                  * from the queue, since there's no
1759                                  * benefit in leaving it queued */
1760
1761                                 assert(s->child.options & (WSTOPPED|WCONTINUED));
1762                                 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|(s->child.options & (WSTOPPED|WCONTINUED)));
1763                         }
1764
1765                         r = source_set_pending(s, true);
1766                         if (r < 0)
1767                                 return r;
1768                 }
1769         }
1770
1771         return 0;
1772 }
1773
1774 static int process_signal(sd_event *e, uint32_t events) {
1775         bool read_one = false;
1776         int r;
1777
1778         assert(e);
1779         assert(e->signal_sources);
1780
1781         assert_return(events == EPOLLIN, -EIO);
1782
1783         for (;;) {
1784                 struct signalfd_siginfo si;
1785                 ssize_t ss;
1786                 sd_event_source *s;
1787
1788                 ss = read(e->signal_fd, &si, sizeof(si));
1789                 if (ss < 0) {
1790                         if (errno == EAGAIN || errno == EINTR)
1791                                 return read_one;
1792
1793                         return -errno;
1794                 }
1795
1796                 if (_unlikely_(ss != sizeof(si)))
1797                         return -EIO;
1798
1799                 read_one = true;
1800
1801                 s = e->signal_sources[si.ssi_signo];
1802                 if (si.ssi_signo == SIGCHLD) {
1803                         r = process_child(e);
1804                         if (r < 0)
1805                                 return r;
1806                         if (r > 0 || !s)
1807                                 continue;
1808                 } else
1809                         if (!s)
1810                                 return -EIO;
1811
1812                 s->signal.siginfo = si;
1813                 r = source_set_pending(s, true);
1814                 if (r < 0)
1815                         return r;
1816         }
1817
1818         return 0;
1819 }
1820
1821 static int source_dispatch(sd_event_source *s) {
1822         int r = 0;
1823
1824         assert(s);
1825         assert(s->pending || s->type == SOURCE_EXIT);
1826
1827         if (s->type != SOURCE_DEFER && s->type != SOURCE_EXIT) {
1828                 r = source_set_pending(s, false);
1829                 if (r < 0)
1830                         return r;
1831         }
1832
1833         if (s->type != SOURCE_POST) {
1834                 sd_event_source *z;
1835                 Iterator i;
1836
1837                 /* If we execute a non-post source, let's mark all
1838                  * post sources as pending */
1839
1840                 SET_FOREACH(z, s->event->post_sources, i) {
1841                         if (z->enabled == SD_EVENT_OFF)
1842                                 continue;
1843
1844                         r = source_set_pending(z, true);
1845                         if (r < 0)
1846                                 return r;
1847                 }
1848         }
1849
1850         if (s->enabled == SD_EVENT_ONESHOT) {
1851                 r = sd_event_source_set_enabled(s, SD_EVENT_OFF);
1852                 if (r < 0)
1853                         return r;
1854         }
1855
1856         s->dispatching = true;
1857
1858         switch (s->type) {
1859
1860         case SOURCE_IO:
1861                 r = s->io.callback(s, s->io.fd, s->io.revents, s->userdata);
1862                 break;
1863
1864         case SOURCE_MONOTONIC:
1865                 r = s->time.callback(s, s->time.next, s->userdata);
1866                 break;
1867
1868         case SOURCE_REALTIME:
1869                 r = s->time.callback(s, s->time.next, s->userdata);
1870                 break;
1871
1872         case SOURCE_SIGNAL:
1873                 r = s->signal.callback(s, &s->signal.siginfo, s->userdata);
1874                 break;
1875
1876         case SOURCE_CHILD: {
1877                 bool zombie;
1878
1879                 zombie = s->child.siginfo.si_code == CLD_EXITED ||
1880                          s->child.siginfo.si_code == CLD_KILLED ||
1881                          s->child.siginfo.si_code == CLD_DUMPED;
1882
1883                 r = s->child.callback(s, &s->child.siginfo, s->userdata);
1884
1885                 /* Now, reap the PID for good. */
1886                 if (zombie)
1887                         waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|WEXITED);
1888
1889                 break;
1890         }
1891
1892         case SOURCE_DEFER:
1893                 r = s->defer.callback(s, s->userdata);
1894                 break;
1895
1896         case SOURCE_POST:
1897                 r = s->post.callback(s, s->userdata);
1898                 break;
1899
1900         case SOURCE_EXIT:
1901                 r = s->exit.callback(s, s->userdata);
1902                 break;
1903
1904         case SOURCE_WATCHDOG:
1905                 assert_not_reached("Wut? I shouldn't exist.");
1906         }
1907
1908         s->dispatching = false;
1909
1910         if (r < 0)
1911                 log_debug("Event source %p returned error, disabling: %s", s, strerror(-r));
1912
1913         if (s->n_ref == 0)
1914                 source_free(s);
1915         else if (r < 0)
1916                 sd_event_source_set_enabled(s, SD_EVENT_OFF);
1917
1918         return 1;
1919 }
1920
1921 static int event_prepare(sd_event *e) {
1922         int r;
1923
1924         assert(e);
1925
1926         for (;;) {
1927                 sd_event_source *s;
1928
1929                 s = prioq_peek(e->prepare);
1930                 if (!s || s->prepare_iteration == e->iteration || s->enabled == SD_EVENT_OFF)
1931                         break;
1932
1933                 s->prepare_iteration = e->iteration;
1934                 r = prioq_reshuffle(e->prepare, s, &s->prepare_index);
1935                 if (r < 0)
1936                         return r;
1937
1938                 assert(s->prepare);
1939
1940                 s->dispatching = true;
1941                 r = s->prepare(s, s->userdata);
1942                 s->dispatching = false;
1943
1944                 if (r < 0)
1945                         log_debug("Prepare callback of event source %p returned error, disabling: %s", s, strerror(-r));
1946
1947                 if (s->n_ref == 0)
1948                         source_free(s);
1949                 else if (r < 0)
1950                         sd_event_source_set_enabled(s, SD_EVENT_OFF);
1951         }
1952
1953         return 0;
1954 }
1955
1956 static int dispatch_exit(sd_event *e) {
1957         sd_event_source *p;
1958         int r;
1959
1960         assert(e);
1961
1962         p = prioq_peek(e->exit);
1963         if (!p || p->enabled == SD_EVENT_OFF) {
1964                 e->state = SD_EVENT_FINISHED;
1965                 return 0;
1966         }
1967
1968         sd_event_ref(e);
1969         e->iteration++;
1970         e->state = SD_EVENT_EXITING;
1971
1972         r = source_dispatch(p);
1973
1974         e->state = SD_EVENT_PASSIVE;
1975         sd_event_unref(e);
1976
1977         return r;
1978 }
1979
1980 static sd_event_source* event_next_pending(sd_event *e) {
1981         sd_event_source *p;
1982
1983         assert(e);
1984
1985         p = prioq_peek(e->pending);
1986         if (!p)
1987                 return NULL;
1988
1989         if (p->enabled == SD_EVENT_OFF)
1990                 return NULL;
1991
1992         return p;
1993 }
1994
1995 static int arm_watchdog(sd_event *e) {
1996         struct itimerspec its = {};
1997         usec_t t;
1998         int r;
1999
2000         assert(e);
2001         assert(e->watchdog_fd >= 0);
2002
2003         t = sleep_between(e,
2004                           e->watchdog_last + (e->watchdog_period / 2),
2005                           e->watchdog_last + (e->watchdog_period * 3 / 4));
2006
2007         timespec_store(&its.it_value, t);
2008
2009         r = timerfd_settime(e->watchdog_fd, TFD_TIMER_ABSTIME, &its, NULL);
2010         if (r < 0)
2011                 return -errno;
2012
2013         return 0;
2014 }
2015
2016 static int process_watchdog(sd_event *e) {
2017         assert(e);
2018
2019         if (!e->watchdog)
2020                 return 0;
2021
2022         /* Don't notify watchdog too often */
2023         if (e->watchdog_last + e->watchdog_period / 4 > e->timestamp.monotonic)
2024                 return 0;
2025
2026         sd_notify(false, "WATCHDOG=1");
2027         e->watchdog_last = e->timestamp.monotonic;
2028
2029         return arm_watchdog(e);
2030 }
2031
2032 _public_ int sd_event_run(sd_event *e, uint64_t timeout) {
2033         struct epoll_event *ev_queue;
2034         unsigned ev_queue_max;
2035         sd_event_source *p;
2036         int r, i, m;
2037
2038         assert_return(e, -EINVAL);
2039         assert_return(!event_pid_changed(e), -ECHILD);
2040         assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2041         assert_return(e->state == SD_EVENT_PASSIVE, -EBUSY);
2042
2043         if (e->exit_requested)
2044                 return dispatch_exit(e);
2045
2046         sd_event_ref(e);
2047         e->iteration++;
2048         e->state = SD_EVENT_RUNNING;
2049
2050         r = event_prepare(e);
2051         if (r < 0)
2052                 goto finish;
2053
2054         r = event_arm_timer(e, e->monotonic_fd, e->monotonic_earliest, e->monotonic_latest, &e->monotonic_next);
2055         if (r < 0)
2056                 goto finish;
2057
2058         r = event_arm_timer(e, e->realtime_fd, e->realtime_earliest, e->realtime_latest, &e->realtime_next);
2059         if (r < 0)
2060                 goto finish;
2061
2062         if (event_next_pending(e) || e->need_process_child)
2063                 timeout = 0;
2064         ev_queue_max = CLAMP(e->n_sources, 1U, EPOLL_QUEUE_MAX);
2065         ev_queue = newa(struct epoll_event, ev_queue_max);
2066
2067         m = epoll_wait(e->epoll_fd, ev_queue, ev_queue_max,
2068                        timeout == (uint64_t) -1 ? -1 : (int) ((timeout + USEC_PER_MSEC - 1) / USEC_PER_MSEC));
2069         if (m < 0) {
2070                 r = errno == EAGAIN || errno == EINTR ? 1 : -errno;
2071                 goto finish;
2072         }
2073
2074         dual_timestamp_get(&e->timestamp);
2075
2076         for (i = 0; i < m; i++) {
2077
2078                 if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_MONOTONIC))
2079                         r = flush_timer(e, e->monotonic_fd, ev_queue[i].events, &e->monotonic_next);
2080                 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_REALTIME))
2081                         r = flush_timer(e, e->realtime_fd, ev_queue[i].events, &e->realtime_next);
2082                 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_SIGNAL))
2083                         r = process_signal(e, ev_queue[i].events);
2084                 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_WATCHDOG))
2085                         r = flush_timer(e, e->watchdog_fd, ev_queue[i].events, NULL);
2086                 else
2087                         r = process_io(e, ev_queue[i].data.ptr, ev_queue[i].events);
2088
2089                 if (r < 0)
2090                         goto finish;
2091         }
2092
2093         r = process_watchdog(e);
2094         if (r < 0)
2095                 goto finish;
2096
2097         r = process_timer(e, e->timestamp.monotonic, e->monotonic_earliest, e->monotonic_latest);
2098         if (r < 0)
2099                 goto finish;
2100
2101         r = process_timer(e, e->timestamp.realtime, e->realtime_earliest, e->realtime_latest);
2102         if (r < 0)
2103                 goto finish;
2104
2105         if (e->need_process_child) {
2106                 r = process_child(e);
2107                 if (r < 0)
2108                         goto finish;
2109         }
2110
2111         p = event_next_pending(e);
2112         if (!p) {
2113                 r = 1;
2114                 goto finish;
2115         }
2116
2117         r = source_dispatch(p);
2118
2119 finish:
2120         e->state = SD_EVENT_PASSIVE;
2121         sd_event_unref(e);
2122
2123         return r;
2124 }
2125
2126 _public_ int sd_event_loop(sd_event *e) {
2127         int r;
2128
2129         assert_return(e, -EINVAL);
2130         assert_return(!event_pid_changed(e), -ECHILD);
2131         assert_return(e->state == SD_EVENT_PASSIVE, -EBUSY);
2132
2133         sd_event_ref(e);
2134
2135         while (e->state != SD_EVENT_FINISHED) {
2136                 r = sd_event_run(e, (uint64_t) -1);
2137                 if (r < 0)
2138                         goto finish;
2139         }
2140
2141         r = e->exit_code;
2142
2143 finish:
2144         sd_event_unref(e);
2145         return r;
2146 }
2147
2148 _public_ int sd_event_get_state(sd_event *e) {
2149         assert_return(e, -EINVAL);
2150         assert_return(!event_pid_changed(e), -ECHILD);
2151
2152         return e->state;
2153 }
2154
2155 _public_ int sd_event_get_exit_code(sd_event *e, int *code) {
2156         assert_return(e, -EINVAL);
2157         assert_return(code, -EINVAL);
2158         assert_return(!event_pid_changed(e), -ECHILD);
2159
2160         if (!e->exit_requested)
2161                 return -ENODATA;
2162
2163         *code = e->exit_code;
2164         return 0;
2165 }
2166
2167 _public_ int sd_event_exit(sd_event *e, int code) {
2168         assert_return(e, -EINVAL);
2169         assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2170         assert_return(!event_pid_changed(e), -ECHILD);
2171
2172         e->exit_requested = true;
2173         e->exit_code = code;
2174
2175         return 0;
2176 }
2177
2178 _public_ int sd_event_get_now_realtime(sd_event *e, uint64_t *usec) {
2179         assert_return(e, -EINVAL);
2180         assert_return(usec, -EINVAL);
2181         assert_return(dual_timestamp_is_set(&e->timestamp), -ENODATA);
2182         assert_return(!event_pid_changed(e), -ECHILD);
2183
2184         *usec = e->timestamp.realtime;
2185         return 0;
2186 }
2187
2188 _public_ int sd_event_get_now_monotonic(sd_event *e, uint64_t *usec) {
2189         assert_return(e, -EINVAL);
2190         assert_return(usec, -EINVAL);
2191         assert_return(dual_timestamp_is_set(&e->timestamp), -ENODATA);
2192         assert_return(!event_pid_changed(e), -ECHILD);
2193
2194         *usec = e->timestamp.monotonic;
2195         return 0;
2196 }
2197
2198 _public_ int sd_event_default(sd_event **ret) {
2199
2200         static thread_local sd_event *default_event = NULL;
2201         sd_event *e = NULL;
2202         int r;
2203
2204         if (!ret)
2205                 return !!default_event;
2206
2207         if (default_event) {
2208                 *ret = sd_event_ref(default_event);
2209                 return 0;
2210         }
2211
2212         r = sd_event_new(&e);
2213         if (r < 0)
2214                 return r;
2215
2216         e->default_event_ptr = &default_event;
2217         e->tid = gettid();
2218         default_event = e;
2219
2220         *ret = e;
2221         return 1;
2222 }
2223
2224 _public_ int sd_event_get_tid(sd_event *e, pid_t *tid) {
2225         assert_return(e, -EINVAL);
2226         assert_return(tid, -EINVAL);
2227         assert_return(!event_pid_changed(e), -ECHILD);
2228
2229         if (e->tid != 0) {
2230                 *tid = e->tid;
2231                 return 0;
2232         }
2233
2234         return -ENXIO;
2235 }
2236
2237 _public_ int sd_event_set_watchdog(sd_event *e, int b) {
2238         int r;
2239
2240         assert_return(e, -EINVAL);
2241         assert_return(!event_pid_changed(e), -ECHILD);
2242
2243         if (e->watchdog == !!b)
2244                 return e->watchdog;
2245
2246         if (b) {
2247                 struct epoll_event ev = {};
2248
2249                 r = sd_watchdog_enabled(false, &e->watchdog_period);
2250                 if (r <= 0)
2251                         return r;
2252
2253                 /* Issue first ping immediately */
2254                 sd_notify(false, "WATCHDOG=1");
2255                 e->watchdog_last = now(CLOCK_MONOTONIC);
2256
2257                 e->watchdog_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC);
2258                 if (e->watchdog_fd < 0)
2259                         return -errno;
2260
2261                 r = arm_watchdog(e);
2262                 if (r < 0)
2263                         goto fail;
2264
2265                 ev.events = EPOLLIN;
2266                 ev.data.ptr = INT_TO_PTR(SOURCE_WATCHDOG);
2267
2268                 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->watchdog_fd, &ev);
2269                 if (r < 0) {
2270                         r = -errno;
2271                         goto fail;
2272                 }
2273
2274         } else {
2275                 if (e->watchdog_fd >= 0) {
2276                         epoll_ctl(e->epoll_fd, EPOLL_CTL_DEL, e->watchdog_fd, NULL);
2277                         close_nointr_nofail(e->watchdog_fd);
2278                         e->watchdog_fd = -1;
2279                 }
2280         }
2281
2282         e->watchdog = !!b;
2283         return e->watchdog;
2284
2285 fail:
2286         close_nointr_nofail(e->watchdog_fd);
2287         e->watchdog_fd = -1;
2288         return r;
2289 }
2290
2291 _public_ int sd_event_get_watchdog(sd_event *e) {
2292         assert_return(e, -EINVAL);
2293         assert_return(!event_pid_changed(e), -ECHILD);
2294
2295         return e->watchdog;
2296 }