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