Coverage Report

Created: 2019-06-19 13:33

/src/systemd/src/libsystemd/sd-event/sd-event.c
Line
Count
Source (jump to first uncovered line)
1
/* SPDX-License-Identifier: LGPL-2.1+ */
2
3
#include <sys/epoll.h>
4
#include <sys/timerfd.h>
5
#include <sys/wait.h>
6
7
#include "sd-daemon.h"
8
#include "sd-event.h"
9
#include "sd-id128.h"
10
11
#include "alloc-util.h"
12
#include "event-source.h"
13
#include "fd-util.h"
14
#include "fs-util.h"
15
#include "hashmap.h"
16
#include "list.h"
17
#include "macro.h"
18
#include "memory-util.h"
19
#include "missing.h"
20
#include "prioq.h"
21
#include "process-util.h"
22
#include "set.h"
23
#include "signal-util.h"
24
#include "string-table.h"
25
#include "string-util.h"
26
#include "time-util.h"
27
28
52.0k
#define DEFAULT_ACCURACY_USEC (250 * USEC_PER_MSEC)
29
30
static const char* const event_source_type_table[_SOURCE_EVENT_SOURCE_TYPE_MAX] = {
31
        [SOURCE_IO] = "io",
32
        [SOURCE_TIME_REALTIME] = "realtime",
33
        [SOURCE_TIME_BOOTTIME] = "bootime",
34
        [SOURCE_TIME_MONOTONIC] = "monotonic",
35
        [SOURCE_TIME_REALTIME_ALARM] = "realtime-alarm",
36
        [SOURCE_TIME_BOOTTIME_ALARM] = "boottime-alarm",
37
        [SOURCE_SIGNAL] = "signal",
38
        [SOURCE_CHILD] = "child",
39
        [SOURCE_DEFER] = "defer",
40
        [SOURCE_POST] = "post",
41
        [SOURCE_EXIT] = "exit",
42
        [SOURCE_WATCHDOG] = "watchdog",
43
        [SOURCE_INOTIFY] = "inotify",
44
};
45
46
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(event_source_type, int);
47
48
2.54M
#define EVENT_SOURCE_IS_TIME(t) IN_SET((t), SOURCE_TIME_REALTIME, SOURCE_TIME_BOOTTIME, SOURCE_TIME_MONOTONIC, SOURCE_TIME_REALTIME_ALARM, SOURCE_TIME_BOOTTIME_ALARM)
49
50
struct sd_event {
51
        unsigned n_ref;
52
53
        int epoll_fd;
54
        int watchdog_fd;
55
56
        Prioq *pending;
57
        Prioq *prepare;
58
59
        /* timerfd_create() only supports these five clocks so far. We
60
         * can add support for more clocks when the kernel learns to
61
         * deal with them, too. */
62
        struct clock_data realtime;
63
        struct clock_data boottime;
64
        struct clock_data monotonic;
65
        struct clock_data realtime_alarm;
66
        struct clock_data boottime_alarm;
67
68
        usec_t perturb;
69
70
        sd_event_source **signal_sources; /* indexed by signal number */
71
        Hashmap *signal_data; /* indexed by priority */
72
73
        Hashmap *child_sources;
74
        unsigned n_enabled_child_sources;
75
76
        Set *post_sources;
77
78
        Prioq *exit;
79
80
        Hashmap *inotify_data; /* indexed by priority */
81
82
        /* A list of inode structures that still have an fd open, that we need to close before the next loop iteration */
83
        LIST_HEAD(struct inode_data, inode_data_to_close);
84
85
        /* A list of inotify objects that already have events buffered which aren't processed yet */
86
        LIST_HEAD(struct inotify_data, inotify_data_buffered);
87
88
        pid_t original_pid;
89
90
        uint64_t iteration;
91
        triple_timestamp timestamp;
92
        int state;
93
94
        bool exit_requested:1;
95
        bool need_process_child:1;
96
        bool watchdog:1;
97
        bool profile_delays:1;
98
99
        int exit_code;
100
101
        pid_t tid;
102
        sd_event **default_event_ptr;
103
104
        usec_t watchdog_last, watchdog_period;
105
106
        unsigned n_sources;
107
108
        LIST_HEAD(sd_event_source, sources);
109
110
        usec_t last_run, last_log;
111
        unsigned delays[sizeof(usec_t) * 8];
112
};
113
114
static thread_local sd_event *default_event = NULL;
115
116
static void source_disconnect(sd_event_source *s);
117
static void event_gc_inode_data(sd_event *e, struct inode_data *d);
118
119
11.5M
static sd_event *event_resolve(sd_event *e) {
120
11.5M
        return e == SD_EVENT_DEFAULT ? default_event : e;
121
11.5M
}
122
123
6.88M
static int pending_prioq_compare(const void *a, const void *b) {
124
6.88M
        const sd_event_source *x = a, *y = b;
125
6.88M
        int r;
126
6.88M
127
6.88M
        assert(x->pending);
128
6.88M
        assert(y->pending);
129
6.88M
130
6.88M
        /* Enabled ones first */
131
6.88M
        if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
132
1.92M
                return -1;
133
4.95M
        if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
134
709k
                return 1;
135
4.24M
136
4.24M
        /* Lower priority values first */
137
4.24M
        r = CMP(x->priority, y->priority);
138
4.24M
        if (r != 0)
139
1.50M
                return r;
140
2.73M
141
2.73M
        /* Older entries first */
142
2.73M
        return CMP(x->pending_iteration, y->pending_iteration);
143
2.73M
}
144
145
2.63M
static int prepare_prioq_compare(const void *a, const void *b) {
146
2.63M
        const sd_event_source *x = a, *y = b;
147
2.63M
        int r;
148
2.63M
149
2.63M
        assert(x->prepare);
150
2.63M
        assert(y->prepare);
151
2.63M
152
2.63M
        /* Enabled ones first */
153
2.63M
        if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
154
6.56k
                return -1;
155
2.63M
        if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
156
0
                return 1;
157
2.63M
158
2.63M
        /* Move most recently prepared ones last, so that we can stop
159
2.63M
         * preparing as soon as we hit one that has already been
160
2.63M
         * prepared in the current iteration */
161
2.63M
        r = CMP(x->prepare_iteration, y->prepare_iteration);
162
2.63M
        if (r != 0)
163
1.73M
                return r;
164
892k
165
892k
        /* Lower priority values first */
166
892k
        return CMP(x->priority, y->priority);
167
892k
}
168
169
911k
static int earliest_time_prioq_compare(const void *a, const void *b) {
170
911k
        const sd_event_source *x = a, *y = b;
171
911k
172
911k
        assert(EVENT_SOURCE_IS_TIME(x->type));
173
911k
        assert(x->type == y->type);
174
911k
175
911k
        /* Enabled ones first */
176
911k
        if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
177
8.32k
                return -1;
178
903k
        if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
179
3.83k
                return 1;
180
899k
181
899k
        /* Move the pending ones to the end */
182
899k
        if (!x->pending && y->pending)
183
1.02k
                return -1;
184
898k
        if (x->pending && !y->pending)
185
0
                return 1;
186
898k
187
898k
        /* Order by time */
188
898k
        return CMP(x->time.next, y->time.next);
189
898k
}
190
191
1.80M
static usec_t time_event_source_latest(const sd_event_source *s) {
192
1.80M
        return usec_add(s->time.next, s->time.accuracy);
193
1.80M
}
194
195
911k
static int latest_time_prioq_compare(const void *a, const void *b) {
196
911k
        const sd_event_source *x = a, *y = b;
197
911k
198
911k
        assert(EVENT_SOURCE_IS_TIME(x->type));
199
911k
        assert(x->type == y->type);
200
911k
201
911k
        /* Enabled ones first */
202
911k
        if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
203
8.32k
                return -1;
204
903k
        if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
205
3.83k
                return 1;
206
899k
207
899k
        /* Move the pending ones to the end */
208
899k
        if (!x->pending && y->pending)
209
1.02k
                return -1;
210
898k
        if (x->pending && !y->pending)
211
0
                return 1;
212
898k
213
898k
        /* Order by time */
214
898k
        return CMP(time_event_source_latest(x), time_event_source_latest(y));
215
898k
}
216
217
9.95k
static int exit_prioq_compare(const void *a, const void *b) {
218
9.95k
        const sd_event_source *x = a, *y = b;
219
9.95k
220
9.95k
        assert(x->type == SOURCE_EXIT);
221
9.95k
        assert(y->type == SOURCE_EXIT);
222
9.95k
223
9.95k
        /* Enabled ones first */
224
9.95k
        if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
225
6.26k
                return -1;
226
3.69k
        if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
227
0
                return 1;
228
3.69k
229
3.69k
        /* Lower priority values first */
230
3.69k
        return CMP(x->priority, y->priority);
231
3.69k
}
232
233
288k
static void free_clock_data(struct clock_data *d) {
234
288k
        assert(d);
235
288k
        assert(d->wakeup == WAKEUP_CLOCK_DATA);
236
288k
237
288k
        safe_close(d->fd);
238
288k
        prioq_free(d->earliest);
239
288k
        prioq_free(d->latest);
240
288k
}
241
242
57.7k
static sd_event *event_free(sd_event *e) {
243
57.7k
        sd_event_source *s;
244
57.7k
245
57.7k
        assert(e);
246
57.7k
247
77.7k
        while ((s = e->sources)) {
248
20.0k
                assert(s->floating);
249
20.0k
                source_disconnect(s);
250
20.0k
                sd_event_source_unref(s);
251
20.0k
        }
252
57.7k
253
57.7k
        assert(e->n_sources == 0);
254
57.7k
255
57.7k
        if (e->default_event_ptr)
256
52.6k
                *(e->default_event_ptr) = NULL;
257
57.7k
258
57.7k
        safe_close(e->epoll_fd);
259
57.7k
        safe_close(e->watchdog_fd);
260
57.7k
261
57.7k
        free_clock_data(&e->realtime);
262
57.7k
        free_clock_data(&e->boottime);
263
57.7k
        free_clock_data(&e->monotonic);
264
57.7k
        free_clock_data(&e->realtime_alarm);
265
57.7k
        free_clock_data(&e->boottime_alarm);
266
57.7k
267
57.7k
        prioq_free(e->pending);
268
57.7k
        prioq_free(e->prepare);
269
57.7k
        prioq_free(e->exit);
270
57.7k
271
57.7k
        free(e->signal_sources);
272
57.7k
        hashmap_free(e->signal_data);
273
57.7k
274
57.7k
        hashmap_free(e->inotify_data);
275
57.7k
276
57.7k
        hashmap_free(e->child_sources);
277
57.7k
        set_free(e->post_sources);
278
57.7k
279
57.7k
        return mfree(e);
280
57.7k
}
281
282
57.7k
_public_ int sd_event_new(sd_event** ret) {
283
57.7k
        sd_event *e;
284
57.7k
        int r;
285
57.7k
286
57.7k
        assert_return(ret, -EINVAL);
287
57.7k
288
57.7k
        e = new(sd_event, 1);
289
57.7k
        if (!e)
290
0
                return -ENOMEM;
291
57.7k
292
57.7k
        *e = (sd_event) {
293
57.7k
                .n_ref = 1,
294
57.7k
                .epoll_fd = -1,
295
57.7k
                .watchdog_fd = -1,
296
57.7k
                .realtime.wakeup = WAKEUP_CLOCK_DATA,
297
57.7k
                .realtime.fd = -1,
298
57.7k
                .realtime.next = USEC_INFINITY,
299
57.7k
                .boottime.wakeup = WAKEUP_CLOCK_DATA,
300
57.7k
                .boottime.fd = -1,
301
57.7k
                .boottime.next = USEC_INFINITY,
302
57.7k
                .monotonic.wakeup = WAKEUP_CLOCK_DATA,
303
57.7k
                .monotonic.fd = -1,
304
57.7k
                .monotonic.next = USEC_INFINITY,
305
57.7k
                .realtime_alarm.wakeup = WAKEUP_CLOCK_DATA,
306
57.7k
                .realtime_alarm.fd = -1,
307
57.7k
                .realtime_alarm.next = USEC_INFINITY,
308
57.7k
                .boottime_alarm.wakeup = WAKEUP_CLOCK_DATA,
309
57.7k
                .boottime_alarm.fd = -1,
310
57.7k
                .boottime_alarm.next = USEC_INFINITY,
311
57.7k
                .perturb = USEC_INFINITY,
312
57.7k
                .original_pid = getpid_cached(),
313
57.7k
        };
314
57.7k
315
57.7k
        r = prioq_ensure_allocated(&e->pending, pending_prioq_compare);
316
57.7k
        if (r < 0)
317
0
                goto fail;
318
57.7k
319
57.7k
        e->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
320
57.7k
        if (e->epoll_fd < 0) {
321
0
                r = -errno;
322
0
                goto fail;
323
0
        }
324
57.7k
325
57.7k
        e->epoll_fd = fd_move_above_stdio(e->epoll_fd);
326
57.7k
327
57.7k
        if (secure_getenv("SD_EVENT_PROFILE_DELAYS")) {
328
0
                log_debug("Event loop profiling enabled. Logarithmic histogram of event loop iterations in the range 2^0 ... 2^63 us will be logged every 5s.");
329
0
                e->profile_delays = true;
330
0
        }
331
57.7k
332
57.7k
        *ret = e;
333
57.7k
        return 0;
334
0
335
0
fail:
336
0
        event_free(e);
337
0
        return r;
338
57.7k
}
339
340
DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_event, sd_event, event_free);
341
342
59.0k
_public_ sd_event_source* sd_event_source_disable_unref(sd_event_source *s) {
343
59.0k
        if (s)
344
29.5k
                (void) sd_event_source_set_enabled(s, SD_EVENT_OFF);
345
59.0k
        return sd_event_source_unref(s);
346
59.0k
}
347
348
22.2M
static bool event_pid_changed(sd_event *e) {
349
22.2M
        assert(e);
350
22.2M
351
22.2M
        /* We don't support people creating an event loop and keeping
352
22.2M
         * it around over a fork(). Let's complain. */
353
22.2M
354
22.2M
        return e->original_pid != getpid_cached();
355
22.2M
}
356
357
120k
static void source_io_unregister(sd_event_source *s) {
358
120k
        int r;
359
120k
360
120k
        assert(s);
361
120k
        assert(s->type == SOURCE_IO);
362
120k
363
120k
        if (event_pid_changed(s->event))
364
0
                return;
365
120k
366
120k
        if (!s->io.registered)
367
39.5k
                return;
368
81.0k
369
81.0k
        r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, s->io.fd, NULL);
370
81.0k
        if (r < 0)
371
81.0k
                log_debug_errno(errno, "Failed to remove source %s (type %s) from epoll: %m",
372
81.0k
                                strna(s->description), event_source_type_to_string(s->type));
373
81.0k
374
81.0k
        s->io.registered = false;
375
81.0k
}
376
377
static int source_io_register(
378
                sd_event_source *s,
379
                int enabled,
380
943k
                uint32_t events) {
381
943k
382
943k
        struct epoll_event ev;
383
943k
        int r;
384
943k
385
943k
        assert(s);
386
943k
        assert(s->type == SOURCE_IO);
387
943k
        assert(enabled != SD_EVENT_OFF);
388
943k
389
943k
        ev = (struct epoll_event) {
390
943k
                .events = events | (enabled == SD_EVENT_ONESHOT ? EPOLLONESHOT : 0),
391
943k
                .data.ptr = s,
392
943k
        };
393
943k
394
943k
        if (s->io.registered)
395
858k
                r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_MOD, s->io.fd, &ev);
396
85.3k
        else
397
85.3k
                r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_ADD, s->io.fd, &ev);
398
943k
        if (r < 0)
399
4.30k
                return -errno;
400
939k
401
939k
        s->io.registered = true;
402
939k
403
939k
        return 0;
404
939k
}
405
406
1
static clockid_t event_source_type_to_clock(EventSourceType t) {
407
1
408
1
        switch (t) {
409
1
410
1
        case SOURCE_TIME_REALTIME:
411
0
                return CLOCK_REALTIME;
412
1
413
1
        case SOURCE_TIME_BOOTTIME:
414
1
                return CLOCK_BOOTTIME;
415
1
416
1
        case SOURCE_TIME_MONOTONIC:
417
0
                return CLOCK_MONOTONIC;
418
1
419
1
        case SOURCE_TIME_REALTIME_ALARM:
420
0
                return CLOCK_REALTIME_ALARM;
421
1
422
1
        case SOURCE_TIME_BOOTTIME_ALARM:
423
0
                return CLOCK_BOOTTIME_ALARM;
424
1
425
1
        default:
426
0
                return (clockid_t) -1;
427
1
        }
428
1
}
429
430
52.6k
static EventSourceType clock_to_event_source_type(clockid_t clock) {
431
52.6k
432
52.6k
        switch (clock) {
433
52.6k
434
52.6k
        case CLOCK_REALTIME:
435
0
                return SOURCE_TIME_REALTIME;
436
52.6k
437
52.6k
        case CLOCK_BOOTTIME:
438
5.24k
                return SOURCE_TIME_BOOTTIME;
439
52.6k
440
52.6k
        case CLOCK_MONOTONIC:
441
47.4k
                return SOURCE_TIME_MONOTONIC;
442
52.6k
443
52.6k
        case CLOCK_REALTIME_ALARM:
444
0
                return SOURCE_TIME_REALTIME_ALARM;
445
52.6k
446
52.6k
        case CLOCK_BOOTTIME_ALARM:
447
0
                return SOURCE_TIME_BOOTTIME_ALARM;
448
52.6k
449
52.6k
        default:
450
0
                return _SOURCE_EVENT_SOURCE_TYPE_INVALID;
451
52.6k
        }
452
52.6k
}
453
454
1.00M
static struct clock_data* event_get_clock_data(sd_event *e, EventSourceType t) {
455
1.00M
        assert(e);
456
1.00M
457
1.00M
        switch (t) {
458
1.00M
459
1.00M
        case SOURCE_TIME_REALTIME:
460
0
                return &e->realtime;
461
1.00M
462
1.00M
        case SOURCE_TIME_BOOTTIME:
463
24.7k
                return &e->boottime;
464
1.00M
465
1.00M
        case SOURCE_TIME_MONOTONIC:
466
977k
                return &e->monotonic;
467
1.00M
468
1.00M
        case SOURCE_TIME_REALTIME_ALARM:
469
0
                return &e->realtime_alarm;
470
1.00M
471
1.00M
        case SOURCE_TIME_BOOTTIME_ALARM:
472
0
                return &e->boottime_alarm;
473
1.00M
474
1.00M
        default:
475
0
                return NULL;
476
1.00M
        }
477
1.00M
}
478
479
0
static void event_free_signal_data(sd_event *e, struct signal_data *d) {
480
0
        assert(e);
481
0
482
0
        if (!d)
483
0
                return;
484
0
485
0
        hashmap_remove(e->signal_data, &d->priority);
486
0
        safe_close(d->fd);
487
0
        free(d);
488
0
}
489
490
static int event_make_signal_data(
491
                sd_event *e,
492
                int sig,
493
0
                struct signal_data **ret) {
494
0
495
0
        struct epoll_event ev;
496
0
        struct signal_data *d;
497
0
        bool added = false;
498
0
        sigset_t ss_copy;
499
0
        int64_t priority;
500
0
        int r;
501
0
502
0
        assert(e);
503
0
504
0
        if (event_pid_changed(e))
505
0
                return -ECHILD;
506
0
507
0
        if (e->signal_sources && e->signal_sources[sig])
508
0
                priority = e->signal_sources[sig]->priority;
509
0
        else
510
0
                priority = SD_EVENT_PRIORITY_NORMAL;
511
0
512
0
        d = hashmap_get(e->signal_data, &priority);
513
0
        if (d) {
514
0
                if (sigismember(&d->sigset, sig) > 0) {
515
0
                        if (ret)
516
0
                                *ret = d;
517
0
                        return 0;
518
0
                }
519
0
        } else {
520
0
                r = hashmap_ensure_allocated(&e->signal_data, &uint64_hash_ops);
521
0
                if (r < 0)
522
0
                        return r;
523
0
524
0
                d = new(struct signal_data, 1);
525
0
                if (!d)
526
0
                        return -ENOMEM;
527
0
528
0
                *d = (struct signal_data) {
529
0
                        .wakeup = WAKEUP_SIGNAL_DATA,
530
0
                        .fd = -1,
531
0
                        .priority = priority,
532
0
                };
533
0
534
0
                r = hashmap_put(e->signal_data, &d->priority, d);
535
0
                if (r < 0) {
536
0
                        free(d);
537
0
                        return r;
538
0
                }
539
0
540
0
                added = true;
541
0
        }
542
0
543
0
        ss_copy = d->sigset;
544
0
        assert_se(sigaddset(&ss_copy, sig) >= 0);
545
0
546
0
        r = signalfd(d->fd, &ss_copy, SFD_NONBLOCK|SFD_CLOEXEC);
547
0
        if (r < 0) {
548
0
                r = -errno;
549
0
                goto fail;
550
0
        }
551
0
552
0
        d->sigset = ss_copy;
553
0
554
0
        if (d->fd >= 0) {
555
0
                if (ret)
556
0
                        *ret = d;
557
0
                return 0;
558
0
        }
559
0
560
0
        d->fd = fd_move_above_stdio(r);
561
0
562
0
        ev = (struct epoll_event) {
563
0
                .events = EPOLLIN,
564
0
                .data.ptr = d,
565
0
        };
566
0
567
0
        r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, d->fd, &ev);
568
0
        if (r < 0)  {
569
0
                r = -errno;
570
0
                goto fail;
571
0
        }
572
0
573
0
        if (ret)
574
0
                *ret = d;
575
0
576
0
        return 0;
577
0
578
0
fail:
579
0
        if (added)
580
0
                event_free_signal_data(e, d);
581
0
582
0
        return r;
583
0
}
584
585
0
static void event_unmask_signal_data(sd_event *e, struct signal_data *d, int sig) {
586
0
        assert(e);
587
0
        assert(d);
588
0
589
0
        /* Turns off the specified signal in the signal data
590
0
         * object. If the signal mask of the object becomes empty that
591
0
         * way removes it. */
592
0
593
0
        if (sigismember(&d->sigset, sig) == 0)
594
0
                return;
595
0
596
0
        assert_se(sigdelset(&d->sigset, sig) >= 0);
597
0
598
0
        if (sigisemptyset(&d->sigset)) {
599
0
                /* If all the mask is all-zero we can get rid of the structure */
600
0
                event_free_signal_data(e, d);
601
0
                return;
602
0
        }
603
0
604
0
        assert(d->fd >= 0);
605
0
606
0
        if (signalfd(d->fd, &d->sigset, SFD_NONBLOCK|SFD_CLOEXEC) < 0)
607
0
                log_debug_errno(errno, "Failed to unset signal bit, ignoring: %m");
608
0
}
609
610
0
static void event_gc_signal_data(sd_event *e, const int64_t *priority, int sig) {
611
0
        struct signal_data *d;
612
0
        static const int64_t zero_priority = 0;
613
0
614
0
        assert(e);
615
0
616
0
        /* Rechecks if the specified signal is still something we are
617
0
         * interested in. If not, we'll unmask it, and possibly drop
618
0
         * the signalfd for it. */
619
0
620
0
        if (sig == SIGCHLD &&
621
0
            e->n_enabled_child_sources > 0)
622
0
                return;
623
0
624
0
        if (e->signal_sources &&
625
0
            e->signal_sources[sig] &&
626
0
            e->signal_sources[sig]->enabled != SD_EVENT_OFF)
627
0
                return;
628
0
629
0
        /*
630
0
         * The specified signal might be enabled in three different queues:
631
0
         *
632
0
         * 1) the one that belongs to the priority passed (if it is non-NULL)
633
0
         * 2) the one that belongs to the priority of the event source of the signal (if there is one)
634
0
         * 3) the 0 priority (to cover the SIGCHLD case)
635
0
         *
636
0
         * Hence, let's remove it from all three here.
637
0
         */
638
0
639
0
        if (priority) {
640
0
                d = hashmap_get(e->signal_data, priority);
641
0
                if (d)
642
0
                        event_unmask_signal_data(e, d, sig);
643
0
        }
644
0
645
0
        if (e->signal_sources && e->signal_sources[sig]) {
646
0
                d = hashmap_get(e->signal_data, &e->signal_sources[sig]->priority);
647
0
                if (d)
648
0
                        event_unmask_signal_data(e, d, sig);
649
0
        }
650
0
651
0
        d = hashmap_get(e->signal_data, &zero_priority);
652
0
        if (d)
653
0
                event_unmask_signal_data(e, d, sig);
654
0
}
655
656
226k
static void source_disconnect(sd_event_source *s) {
657
226k
        sd_event *event;
658
226k
659
226k
        assert(s);
660
226k
661
226k
        if (!s->event)
662
27.7k
                return;
663
198k
664
198k
        assert(s->event->n_sources > 0);
665
198k
666
198k
        switch (s->type) {
667
198k
668
198k
        case SOURCE_IO:
669
85.3k
                if (s->io.fd >= 0)
670
85.3k
                        source_io_unregister(s);
671
85.3k
672
85.3k
                break;
673
198k
674
198k
        case SOURCE_TIME_REALTIME:
675
52.6k
        case SOURCE_TIME_BOOTTIME:
676
52.6k
        case SOURCE_TIME_MONOTONIC:
677
52.6k
        case SOURCE_TIME_REALTIME_ALARM:
678
52.6k
        case SOURCE_TIME_BOOTTIME_ALARM: {
679
52.6k
                struct clock_data *d;
680
52.6k
681
52.6k
                d = event_get_clock_data(s->event, s->type);
682
52.6k
                assert(d);
683
52.6k
684
52.6k
                prioq_remove(d->earliest, s, &s->time.earliest_index);
685
52.6k
                prioq_remove(d->latest, s, &s->time.latest_index);
686
52.6k
                d->needs_rearm = true;
687
52.6k
                break;
688
52.6k
        }
689
52.6k
690
52.6k
        case SOURCE_SIGNAL:
691
0
                if (s->signal.sig > 0) {
692
0
693
0
                        if (s->event->signal_sources)
694
0
                                s->event->signal_sources[s->signal.sig] = NULL;
695
0
696
0
                        event_gc_signal_data(s->event, &s->priority, s->signal.sig);
697
0
                }
698
0
699
0
                break;
700
52.6k
701
52.6k
        case SOURCE_CHILD:
702
0
                if (s->child.pid > 0) {
703
0
                        if (s->enabled != SD_EVENT_OFF) {
704
0
                                assert(s->event->n_enabled_child_sources > 0);
705
0
                                s->event->n_enabled_child_sources--;
706
0
                        }
707
0
708
0
                        (void) hashmap_remove(s->event->child_sources, PID_TO_PTR(s->child.pid));
709
0
                        event_gc_signal_data(s->event, &s->priority, SIGCHLD);
710
0
                }
711
0
712
0
                break;
713
0
714
33.4k
        case SOURCE_DEFER:
715
33.4k
                /* nothing */
716
33.4k
                break;
717
0
718
20.0k
        case SOURCE_POST:
719
20.0k
                set_remove(s->event->post_sources, s);
720
20.0k
                break;
721
0
722
7.38k
        case SOURCE_EXIT:
723
7.38k
                prioq_remove(s->event->exit, s, &s->exit.prioq_index);
724
7.38k
                break;
725
0
726
0
        case SOURCE_INOTIFY: {
727
0
                struct inode_data *inode_data;
728
0
729
0
                inode_data = s->inotify.inode_data;
730
0
                if (inode_data) {
731
0
                        struct inotify_data *inotify_data;
732
0
                        assert_se(inotify_data = inode_data->inotify_data);
733
0
734
0
                        /* Detach this event source from the inode object */
735
0
                        LIST_REMOVE(inotify.by_inode_data, inode_data->event_sources, s);
736
0
                        s->inotify.inode_data = NULL;
737
0
738
0
                        if (s->pending) {
739
0
                                assert(inotify_data->n_pending > 0);
740
0
                                inotify_data->n_pending--;
741
0
                        }
742
0
743
0
                        /* Note that we don't reduce the inotify mask for the watch descriptor here if the inode is
744
0
                         * continued to being watched. That's because inotify doesn't really have an API for that: we
745
0
                         * can only change watch masks with access to the original inode either by fd or by path. But
746
0
                         * paths aren't stable, and keeping an O_PATH fd open all the time would mean wasting an fd
747
0
                         * continuously and keeping the mount busy which we can't really do. We could reconstruct the
748
0
                         * original inode from /proc/self/fdinfo/$INOTIFY_FD (as all watch descriptors are listed
749
0
                         * there), but given the need for open_by_handle_at() which is privileged and not universally
750
0
                         * available this would be quite an incomplete solution. Hence we go the other way, leave the
751
0
                         * mask set, even if it is not minimized now, and ignore all events we aren't interested in
752
0
                         * anymore after reception. Yes, this sucks, but … Linux … */
753
0
754
0
                        /* Maybe release the inode data (and its inotify) */
755
0
                        event_gc_inode_data(s->event, inode_data);
756
0
                }
757
0
758
0
                break;
759
0
        }
760
0
761
0
        default:
762
0
                assert_not_reached("Wut? I shouldn't exist.");
763
198k
        }
764
198k
765
198k
        if (s->pending)
766
33.4k
                prioq_remove(s->event->pending, s, &s->pending_index);
767
198k
768
198k
        if (s->prepare)
769
47.4k
                prioq_remove(s->event->prepare, s, &s->prepare_index);
770
198k
771
198k
        event = s->event;
772
198k
773
198k
        s->type = _SOURCE_EVENT_SOURCE_TYPE_INVALID;
774
198k
        s->event = NULL;
775
198k
        LIST_REMOVE(sources, event->sources, s);
776
198k
        event->n_sources--;
777
198k
778
198k
        if (!s->floating)
779
178k
                sd_event_unref(event);
780
198k
}
781
782
198k
static void source_free(sd_event_source *s) {
783
198k
        assert(s);
784
198k
785
198k
        source_disconnect(s);
786
198k
787
198k
        if (s->type == SOURCE_IO && s->io.owned)
788
0
                s->io.fd = safe_close(s->io.fd);
789
198k
790
198k
        if (s->destroy_callback)
791
0
                s->destroy_callback(s->userdata);
792
198k
793
198k
        free(s->description);
794
198k
        free(s);
795
198k
}
796
DEFINE_TRIVIAL_CLEANUP_FUNC(sd_event_source*, source_free);
797
798
7.78M
static int source_set_pending(sd_event_source *s, bool b) {
799
7.78M
        int r;
800
7.78M
801
7.78M
        assert(s);
802
7.78M
        assert(s->type != SOURCE_EXIT);
803
7.78M
804
7.78M
        if (s->pending == b)
805
5.24M
                return 0;
806
2.54M
807
2.54M
        s->pending = b;
808
2.54M
809
2.54M
        if (b) {
810
1.28M
                s->pending_iteration = s->event->iteration;
811
1.28M
812
1.28M
                r = prioq_put(s->event->pending, s, &s->pending_index);
813
1.28M
                if (r < 0) {
814
0
                        s->pending = false;
815
0
                        return r;
816
0
                }
817
2.54M
        } else
818
2.54M
                assert_se(prioq_remove(s->event->pending, s, &s->pending_index));
819
2.54M
820
2.54M
        if (EVENT_SOURCE_IS_TIME(s->type)) {
821
9.01k
                struct clock_data *d;
822
9.01k
823
9.01k
                d = event_get_clock_data(s->event, s->type);
824
9.01k
                assert(d);
825
9.01k
826
9.01k
                prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
827
9.01k
                prioq_reshuffle(d->latest, s, &s->time.latest_index);
828
9.01k
                d->needs_rearm = true;
829
9.01k
        }
830
2.54M
831
2.54M
        if (s->type == SOURCE_SIGNAL && !b) {
832
0
                struct signal_data *d;
833
0
834
0
                d = hashmap_get(s->event->signal_data, &s->priority);
835
0
                if (d && d->current == s)
836
0
                        d->current = NULL;
837
0
        }
838
2.54M
839
2.54M
        if (s->type == SOURCE_INOTIFY) {
840
0
841
0
                assert(s->inotify.inode_data);
842
0
                assert(s->inotify.inode_data->inotify_data);
843
0
844
0
                if (b)
845
0
                        s->inotify.inode_data->inotify_data->n_pending ++;
846
0
                else {
847
0
                        assert(s->inotify.inode_data->inotify_data->n_pending > 0);
848
0
                        s->inotify.inode_data->inotify_data->n_pending --;
849
0
                }
850
0
        }
851
2.54M
852
2.54M
        return 0;
853
2.54M
}
854
855
198k
static sd_event_source *source_new(sd_event *e, bool floating, EventSourceType type) {
856
198k
        sd_event_source *s;
857
198k
858
198k
        assert(e);
859
198k
860
198k
        s = new(sd_event_source, 1);
861
198k
        if (!s)
862
0
                return NULL;
863
198k
864
198k
        *s = (struct sd_event_source) {
865
198k
                .n_ref = 1,
866
198k
                .event = e,
867
198k
                .floating = floating,
868
198k
                .type = type,
869
198k
                .pending_index = PRIOQ_IDX_NULL,
870
198k
                .prepare_index = PRIOQ_IDX_NULL,
871
198k
        };
872
198k
873
198k
        if (!floating)
874
178k
                sd_event_ref(e);
875
198k
876
198k
        LIST_PREPEND(sources, e->sources, s);
877
198k
        e->n_sources++;
878
198k
879
198k
        return s;
880
198k
}
881
882
_public_ int sd_event_add_io(
883
                sd_event *e,
884
                sd_event_source **ret,
885
                int fd,
886
                uint32_t events,
887
                sd_event_io_handler_t callback,
888
85.3k
                void *userdata) {
889
85.3k
890
85.3k
        _cleanup_(source_freep) sd_event_source *s = NULL;
891
85.3k
        int r;
892
85.3k
893
85.3k
        assert_return(e, -EINVAL);
894
85.3k
        assert_return(e = event_resolve(e), -ENOPKG);
895
85.3k
        assert_return(fd >= 0, -EBADF);
896
85.3k
        assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
897
85.3k
        assert_return(callback, -EINVAL);
898
85.3k
        assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
899
85.3k
        assert_return(!event_pid_changed(e), -ECHILD);
900
85.3k
901
85.3k
        s = source_new(e, !ret, SOURCE_IO);
902
85.3k
        if (!s)
903
0
                return -ENOMEM;
904
85.3k
905
85.3k
        s->wakeup = WAKEUP_EVENT_SOURCE;
906
85.3k
        s->io.fd = fd;
907
85.3k
        s->io.events = events;
908
85.3k
        s->io.callback = callback;
909
85.3k
        s->userdata = userdata;
910
85.3k
        s->enabled = SD_EVENT_ON;
911
85.3k
912
85.3k
        r = source_io_register(s, s->enabled, events);
913
85.3k
        if (r < 0)
914
4.30k
                return r;
915
81.0k
916
81.0k
        if (ret)
917
81.0k
                *ret = s;
918
81.0k
        TAKE_PTR(s);
919
81.0k
920
81.0k
        return 0;
921
81.0k
}
922
923
0
static void initialize_perturb(sd_event *e) {
924
0
        sd_id128_t bootid = {};
925
0
926
0
        /* When we sleep for longer, we try to realign the wakeup to
927
0
           the same time within each minute/second/250ms, so that
928
0
           events all across the system can be coalesced into a single
929
0
           CPU wakeup. However, let's take some system-specific
930
0
           randomness for this value, so that in a network of systems
931
0
           with synced clocks timer events are distributed a
932
0
           bit. Here, we calculate a perturbation usec offset from the
933
0
           boot ID. */
934
0
935
0
        if (_likely_(e->perturb != USEC_INFINITY))
936
0
                return;
937
0
938
0
        if (sd_id128_get_boot(&bootid) >= 0)
939
0
                e->perturb = (bootid.qwords[0] ^ bootid.qwords[1]) % USEC_PER_MINUTE;
940
0
}
941
942
static int event_setup_timer_fd(
943
                sd_event *e,
944
                struct clock_data *d,
945
28.3k
                clockid_t clock) {
946
28.3k
947
28.3k
        struct epoll_event ev;
948
28.3k
        int r, fd;
949
28.3k
950
28.3k
        assert(e);
951
28.3k
        assert(d);
952
28.3k
953
28.3k
        if (_likely_(d->fd >= 0))
954
28.3k
                return 0;
955
28.3k
956
28.3k
        fd = timerfd_create(clock, TFD_NONBLOCK|TFD_CLOEXEC);
957
28.3k
        if (fd < 0)
958
0
                return -errno;
959
28.3k
960
28.3k
        fd = fd_move_above_stdio(fd);
961
28.3k
962
28.3k
        ev = (struct epoll_event) {
963
28.3k
                .events = EPOLLIN,
964
28.3k
                .data.ptr = d,
965
28.3k
        };
966
28.3k
967
28.3k
        r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, fd, &ev);
968
28.3k
        if (r < 0) {
969
0
                safe_close(fd);
970
0
                return -errno;
971
0
        }
972
28.3k
973
28.3k
        d->fd = fd;
974
28.3k
        return 0;
975
28.3k
}
976
977
0
static int time_exit_callback(sd_event_source *s, uint64_t usec, void *userdata) {
978
0
        assert(s);
979
0
980
0
        return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
981
0
}
982
983
_public_ int sd_event_add_time(
984
                sd_event *e,
985
                sd_event_source **ret,
986
                clockid_t clock,
987
                uint64_t usec,
988
                uint64_t accuracy,
989
                sd_event_time_handler_t callback,
990
52.6k
                void *userdata) {
991
52.6k
992
52.6k
        EventSourceType type;
993
52.6k
        _cleanup_(source_freep) sd_event_source *s = NULL;
994
52.6k
        struct clock_data *d;
995
52.6k
        int r;
996
52.6k
997
52.6k
        assert_return(e, -EINVAL);
998
52.6k
        assert_return(e = event_resolve(e), -ENOPKG);
999
52.6k
        assert_return(accuracy != (uint64_t) -1, -EINVAL);
1000
52.6k
        assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1001
52.6k
        assert_return(!event_pid_changed(e), -ECHILD);
1002
52.6k
1003
52.6k
        if (!clock_supported(clock)) /* Checks whether the kernel supports the clock */
1004
0
                return -EOPNOTSUPP;
1005
52.6k
1006
52.6k
        type = clock_to_event_source_type(clock); /* checks whether sd-event supports this clock */
1007
52.6k
        if (type < 0)
1008
0
                return -EOPNOTSUPP;
1009
52.6k
1010
52.6k
        if (!callback)
1011
0
                callback = time_exit_callback;
1012
52.6k
1013
52.6k
        d = event_get_clock_data(e, type);
1014
52.6k
        assert(d);
1015
52.6k
1016
52.6k
        r = prioq_ensure_allocated(&d->earliest, earliest_time_prioq_compare);
1017
52.6k
        if (r < 0)
1018
0
                return r;
1019
52.6k
1020
52.6k
        r = prioq_ensure_allocated(&d->latest, latest_time_prioq_compare);
1021
52.6k
        if (r < 0)
1022
0
                return r;
1023
52.6k
1024
52.6k
        if (d->fd < 0) {
1025
28.3k
                r = event_setup_timer_fd(e, d, clock);
1026
28.3k
                if (r < 0)
1027
0
                        return r;
1028
52.6k
        }
1029
52.6k
1030
52.6k
        s = source_new(e, !ret, type);
1031
52.6k
        if (!s)
1032
0
                return -ENOMEM;
1033
52.6k
1034
52.6k
        s->time.next = usec;
1035
52.6k
        s->time.accuracy = accuracy == 0 ? DEFAULT_ACCURACY_USEC : accuracy;
1036
52.6k
        s->time.callback = callback;
1037
52.6k
        s->time.earliest_index = s->time.latest_index = PRIOQ_IDX_NULL;
1038
52.6k
        s->userdata = userdata;
1039
52.6k
        s->enabled = SD_EVENT_ONESHOT;
1040
52.6k
1041
52.6k
        d->needs_rearm = true;
1042
52.6k
1043
52.6k
        r = prioq_put(d->earliest, s, &s->time.earliest_index);
1044
52.6k
        if (r < 0)
1045
0
                return r;
1046
52.6k
1047
52.6k
        r = prioq_put(d->latest, s, &s->time.latest_index);
1048
52.6k
        if (r < 0)
1049
0
                return r;
1050
52.6k
1051
52.6k
        if (ret)
1052
52.6k
                *ret = s;
1053
52.6k
        TAKE_PTR(s);
1054
52.6k
1055
52.6k
        return 0;
1056
52.6k
}
1057
1058
0
static int signal_exit_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
1059
0
        assert(s);
1060
0
1061
0
        return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
1062
0
}
1063
1064
_public_ int sd_event_add_signal(
1065
                sd_event *e,
1066
                sd_event_source **ret,
1067
                int sig,
1068
                sd_event_signal_handler_t callback,
1069
40.0k
                void *userdata) {
1070
40.0k
1071
40.0k
        _cleanup_(source_freep) sd_event_source *s = NULL;
1072
40.0k
        struct signal_data *d;
1073
40.0k
        sigset_t ss;
1074
40.0k
        int r;
1075
40.0k
1076
40.0k
        assert_return(e, -EINVAL);
1077
40.0k
        assert_return(e = event_resolve(e), -ENOPKG);
1078
40.0k
        assert_return(SIGNAL_VALID(sig), -EINVAL);
1079
40.0k
        assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1080
40.0k
        assert_return(!event_pid_changed(e), -ECHILD);
1081
40.0k
1082
40.0k
        if (!callback)
1083
40.0k
                callback = signal_exit_callback;
1084
40.0k
1085
40.0k
        r = pthread_sigmask(SIG_SETMASK, NULL, &ss);
1086
40.0k
        if (r != 0)
1087
0
                return -r;
1088
40.0k
1089
40.0k
        if (!sigismember(&ss, sig))
1090
40.0k
                return -EBUSY;
1091
0
1092
0
        if (!e->signal_sources) {
1093
0
                e->signal_sources = new0(sd_event_source*, _NSIG);
1094
0
                if (!e->signal_sources)
1095
0
                        return -ENOMEM;
1096
0
        } else if (e->signal_sources[sig])
1097
0
                return -EBUSY;
1098
0
1099
0
        s = source_new(e, !ret, SOURCE_SIGNAL);
1100
0
        if (!s)
1101
0
                return -ENOMEM;
1102
0
1103
0
        s->signal.sig = sig;
1104
0
        s->signal.callback = callback;
1105
0
        s->userdata = userdata;
1106
0
        s->enabled = SD_EVENT_ON;
1107
0
1108
0
        e->signal_sources[sig] = s;
1109
0
1110
0
        r = event_make_signal_data(e, sig, &d);
1111
0
        if (r < 0)
1112
0
                return r;
1113
0
1114
0
        /* Use the signal name as description for the event source by default */
1115
0
        (void) sd_event_source_set_description(s, signal_to_string(sig));
1116
0
1117
0
        if (ret)
1118
0
                *ret = s;
1119
0
        TAKE_PTR(s);
1120
0
1121
0
        return 0;
1122
0
}
1123
1124
_public_ int sd_event_add_child(
1125
                sd_event *e,
1126
                sd_event_source **ret,
1127
                pid_t pid,
1128
                int options,
1129
                sd_event_child_handler_t callback,
1130
0
                void *userdata) {
1131
0
1132
0
        _cleanup_(source_freep) sd_event_source *s = NULL;
1133
0
        int r;
1134
0
1135
0
        assert_return(e, -EINVAL);
1136
0
        assert_return(e = event_resolve(e), -ENOPKG);
1137
0
        assert_return(pid > 1, -EINVAL);
1138
0
        assert_return(!(options & ~(WEXITED|WSTOPPED|WCONTINUED)), -EINVAL);
1139
0
        assert_return(options != 0, -EINVAL);
1140
0
        assert_return(callback, -EINVAL);
1141
0
        assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1142
0
        assert_return(!event_pid_changed(e), -ECHILD);
1143
0
1144
0
        r = hashmap_ensure_allocated(&e->child_sources, NULL);
1145
0
        if (r < 0)
1146
0
                return r;
1147
0
1148
0
        if (hashmap_contains(e->child_sources, PID_TO_PTR(pid)))
1149
0
                return -EBUSY;
1150
0
1151
0
        s = source_new(e, !ret, SOURCE_CHILD);
1152
0
        if (!s)
1153
0
                return -ENOMEM;
1154
0
1155
0
        s->child.pid = pid;
1156
0
        s->child.options = options;
1157
0
        s->child.callback = callback;
1158
0
        s->userdata = userdata;
1159
0
        s->enabled = SD_EVENT_ONESHOT;
1160
0
1161
0
        r = hashmap_put(e->child_sources, PID_TO_PTR(pid), s);
1162
0
        if (r < 0)
1163
0
                return r;
1164
0
1165
0
        e->n_enabled_child_sources++;
1166
0
1167
0
        r = event_make_signal_data(e, SIGCHLD, NULL);
1168
0
        if (r < 0) {
1169
0
                e->n_enabled_child_sources--;
1170
0
                return r;
1171
0
        }
1172
0
1173
0
        e->need_process_child = true;
1174
0
1175
0
        if (ret)
1176
0
                *ret = s;
1177
0
        TAKE_PTR(s);
1178
0
1179
0
        return 0;
1180
0
}
1181
1182
_public_ int sd_event_add_defer(
1183
                sd_event *e,
1184
                sd_event_source **ret,
1185
                sd_event_handler_t callback,
1186
33.4k
                void *userdata) {
1187
33.4k
1188
33.4k
        _cleanup_(source_freep) sd_event_source *s = NULL;
1189
33.4k
        int r;
1190
33.4k
1191
33.4k
        assert_return(e, -EINVAL);
1192
33.4k
        assert_return(e = event_resolve(e), -ENOPKG);
1193
33.4k
        assert_return(callback, -EINVAL);
1194
33.4k
        assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1195
33.4k
        assert_return(!event_pid_changed(e), -ECHILD);
1196
33.4k
1197
33.4k
        s = source_new(e, !ret, SOURCE_DEFER);
1198
33.4k
        if (!s)
1199
0
                return -ENOMEM;
1200
33.4k
1201
33.4k
        s->defer.callback = callback;
1202
33.4k
        s->userdata = userdata;
1203
33.4k
        s->enabled = SD_EVENT_ONESHOT;
1204
33.4k
1205
33.4k
        r = source_set_pending(s, true);
1206
33.4k
        if (r < 0)
1207
0
                return r;
1208
33.4k
1209
33.4k
        if (ret)
1210
33.4k
                *ret = s;
1211
33.4k
        TAKE_PTR(s);
1212
33.4k
1213
33.4k
        return 0;
1214
33.4k
}
1215
1216
_public_ int sd_event_add_post(
1217
                sd_event *e,
1218
                sd_event_source **ret,
1219
                sd_event_handler_t callback,
1220
20.0k
                void *userdata) {
1221
20.0k
1222
20.0k
        _cleanup_(source_freep) sd_event_source *s = NULL;
1223
20.0k
        int r;
1224
20.0k
1225
20.0k
        assert_return(e, -EINVAL);
1226
20.0k
        assert_return(e = event_resolve(e), -ENOPKG);
1227
20.0k
        assert_return(callback, -EINVAL);
1228
20.0k
        assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1229
20.0k
        assert_return(!event_pid_changed(e), -ECHILD);
1230
20.0k
1231
20.0k
        r = set_ensure_allocated(&e->post_sources, NULL);
1232
20.0k
        if (r < 0)
1233
0
                return r;
1234
20.0k
1235
20.0k
        s = source_new(e, !ret, SOURCE_POST);
1236
20.0k
        if (!s)
1237
0
                return -ENOMEM;
1238
20.0k
1239
20.0k
        s->post.callback = callback;
1240
20.0k
        s->userdata = userdata;
1241
20.0k
        s->enabled = SD_EVENT_ON;
1242
20.0k
1243
20.0k
        r = set_put(e->post_sources, s);
1244
20.0k
        if (r < 0)
1245
0
                return r;
1246
20.0k
1247
20.0k
        if (ret)
1248
0
                *ret = s;
1249
20.0k
        TAKE_PTR(s);
1250
20.0k
1251
20.0k
        return 0;
1252
20.0k
}
1253
1254
_public_ int sd_event_add_exit(
1255
                sd_event *e,
1256
                sd_event_source **ret,
1257
                sd_event_handler_t callback,
1258
7.38k
                void *userdata) {
1259
7.38k
1260
7.38k
        _cleanup_(source_freep) sd_event_source *s = NULL;
1261
7.38k
        int r;
1262
7.38k
1263
7.38k
        assert_return(e, -EINVAL);
1264
7.38k
        assert_return(e = event_resolve(e), -ENOPKG);
1265
7.38k
        assert_return(callback, -EINVAL);
1266
7.38k
        assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1267
7.38k
        assert_return(!event_pid_changed(e), -ECHILD);
1268
7.38k
1269
7.38k
        r = prioq_ensure_allocated(&e->exit, exit_prioq_compare);
1270
7.38k
        if (r < 0)
1271
0
                return r;
1272
7.38k
1273
7.38k
        s = source_new(e, !ret, SOURCE_EXIT);
1274
7.38k
        if (!s)
1275
0
                return -ENOMEM;
1276
7.38k
1277
7.38k
        s->exit.callback = callback;
1278
7.38k
        s->userdata = userdata;
1279
7.38k
        s->exit.prioq_index = PRIOQ_IDX_NULL;
1280
7.38k
        s->enabled = SD_EVENT_ONESHOT;
1281
7.38k
1282
7.38k
        r = prioq_put(s->event->exit, s, &s->exit.prioq_index);
1283
7.38k
        if (r < 0)
1284
0
                return r;
1285
7.38k
1286
7.38k
        if (ret)
1287
7.38k
                *ret = s;
1288
7.38k
        TAKE_PTR(s);
1289
7.38k
1290
7.38k
        return 0;
1291
7.38k
}
1292
1293
0
static void event_free_inotify_data(sd_event *e, struct inotify_data *d) {
1294
0
        assert(e);
1295
0
1296
0
        if (!d)
1297
0
                return;
1298
0
1299
0
        assert(hashmap_isempty(d->inodes));
1300
0
        assert(hashmap_isempty(d->wd));
1301
0
1302
0
        if (d->buffer_filled > 0)
1303
0
                LIST_REMOVE(buffered, e->inotify_data_buffered, d);
1304
0
1305
0
        hashmap_free(d->inodes);
1306
0
        hashmap_free(d->wd);
1307
0
1308
0
        assert_se(hashmap_remove(e->inotify_data, &d->priority) == d);
1309
0
1310
0
        if (d->fd >= 0) {
1311
0
                if (epoll_ctl(e->epoll_fd, EPOLL_CTL_DEL, d->fd, NULL) < 0)
1312
0
                        log_debug_errno(errno, "Failed to remove inotify fd from epoll, ignoring: %m");
1313
0
1314
0
                safe_close(d->fd);
1315
0
        }
1316
0
        free(d);
1317
0
}
1318
1319
static int event_make_inotify_data(
1320
                sd_event *e,
1321
                int64_t priority,
1322
0
                struct inotify_data **ret) {
1323
0
1324
0
        _cleanup_close_ int fd = -1;
1325
0
        struct inotify_data *d;
1326
0
        struct epoll_event ev;
1327
0
        int r;
1328
0
1329
0
        assert(e);
1330
0
1331
0
        d = hashmap_get(e->inotify_data, &priority);
1332
0
        if (d) {
1333
0
                if (ret)
1334
0
                        *ret = d;
1335
0
                return 0;
1336
0
        }
1337
0
1338
0
        fd = inotify_init1(IN_NONBLOCK|O_CLOEXEC);
1339
0
        if (fd < 0)
1340
0
                return -errno;
1341
0
1342
0
        fd = fd_move_above_stdio(fd);
1343
0
1344
0
        r = hashmap_ensure_allocated(&e->inotify_data, &uint64_hash_ops);
1345
0
        if (r < 0)
1346
0
                return r;
1347
0
1348
0
        d = new(struct inotify_data, 1);
1349
0
        if (!d)
1350
0
                return -ENOMEM;
1351
0
1352
0
        *d = (struct inotify_data) {
1353
0
                .wakeup = WAKEUP_INOTIFY_DATA,
1354
0
                .fd = TAKE_FD(fd),
1355
0
                .priority = priority,
1356
0
        };
1357
0
1358
0
        r = hashmap_put(e->inotify_data, &d->priority, d);
1359
0
        if (r < 0) {
1360
0
                d->fd = safe_close(d->fd);
1361
0
                free(d);
1362
0
                return r;
1363
0
        }
1364
0
1365
0
        ev = (struct epoll_event) {
1366
0
                .events = EPOLLIN,
1367
0
                .data.ptr = d,
1368
0
        };
1369
0
1370
0
        if (epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, d->fd, &ev) < 0) {
1371
0
                r = -errno;
1372
0
                d->fd = safe_close(d->fd); /* let's close this ourselves, as event_free_inotify_data() would otherwise
1373
0
                                            * remove the fd from the epoll first, which we don't want as we couldn't
1374
0
                                            * add it in the first place. */
1375
0
                event_free_inotify_data(e, d);
1376
0
                return r;
1377
0
        }
1378
0
1379
0
        if (ret)
1380
0
                *ret = d;
1381
0
1382
0
        return 1;
1383
0
}
1384
1385
0
static int inode_data_compare(const struct inode_data *x, const struct inode_data *y) {
1386
0
        int r;
1387
0
1388
0
        assert(x);
1389
0
        assert(y);
1390
0
1391
0
        r = CMP(x->dev, y->dev);
1392
0
        if (r != 0)
1393
0
                return r;
1394
0
1395
0
        return CMP(x->ino, y->ino);
1396
0
}
1397
1398
0
static void inode_data_hash_func(const struct inode_data *d, struct siphash *state) {
1399
0
        assert(d);
1400
0
1401
0
        siphash24_compress(&d->dev, sizeof(d->dev), state);
1402
0
        siphash24_compress(&d->ino, sizeof(d->ino), state);
1403
0
}
1404
1405
DEFINE_PRIVATE_HASH_OPS(inode_data_hash_ops, struct inode_data, inode_data_hash_func, inode_data_compare);
1406
1407
static void event_free_inode_data(
1408
                sd_event *e,
1409
0
                struct inode_data *d) {
1410
0
1411
0
        assert(e);
1412
0
1413
0
        if (!d)
1414
0
                return;
1415
0
1416
0
        assert(!d->event_sources);
1417
0
1418
0
        if (d->fd >= 0) {
1419
0
                LIST_REMOVE(to_close, e->inode_data_to_close, d);
1420
0
                safe_close(d->fd);
1421
0
        }
1422
0
1423
0
        if (d->inotify_data) {
1424
0
1425
0
                if (d->wd >= 0) {
1426
0
                        if (d->inotify_data->fd >= 0) {
1427
0
                                /* So here's a problem. At the time this runs the watch descriptor might already be
1428
0
                                 * invalidated, because an IN_IGNORED event might be queued right the moment we enter
1429
0
                                 * the syscall. Hence, whenever we get EINVAL, ignore it entirely, since it's a very
1430
0
                                 * likely case to happen. */
1431
0
1432
0
                                if (inotify_rm_watch(d->inotify_data->fd, d->wd) < 0 && errno != EINVAL)
1433
0
                                        log_debug_errno(errno, "Failed to remove watch descriptor %i from inotify, ignoring: %m", d->wd);
1434
0
                        }
1435
0
1436
0
                        assert_se(hashmap_remove(d->inotify_data->wd, INT_TO_PTR(d->wd)) == d);
1437
0
                }
1438
0
1439
0
                assert_se(hashmap_remove(d->inotify_data->inodes, d) == d);
1440
0
        }
1441
0
1442
0
        free(d);
1443
0
}
1444
1445
static void event_gc_inode_data(
1446
                sd_event *e,
1447
0
                struct inode_data *d) {
1448
0
1449
0
        struct inotify_data *inotify_data;
1450
0
1451
0
        assert(e);
1452
0
1453
0
        if (!d)
1454
0
                return;
1455
0
1456
0
        if (d->event_sources)
1457
0
                return;
1458
0
1459
0
        inotify_data = d->inotify_data;
1460
0
        event_free_inode_data(e, d);
1461
0
1462
0
        if (inotify_data && hashmap_isempty(inotify_data->inodes))
1463
0
                event_free_inotify_data(e, inotify_data);
1464
0
}
1465
1466
static int event_make_inode_data(
1467
                sd_event *e,
1468
                struct inotify_data *inotify_data,
1469
                dev_t dev,
1470
                ino_t ino,
1471
0
                struct inode_data **ret) {
1472
0
1473
0
        struct inode_data *d, key;
1474
0
        int r;
1475
0
1476
0
        assert(e);
1477
0
        assert(inotify_data);
1478
0
1479
0
        key = (struct inode_data) {
1480
0
                .ino = ino,
1481
0
                .dev = dev,
1482
0
        };
1483
0
1484
0
        d = hashmap_get(inotify_data->inodes, &key);
1485
0
        if (d) {
1486
0
                if (ret)
1487
0
                        *ret = d;
1488
0
1489
0
                return 0;
1490
0
        }
1491
0
1492
0
        r = hashmap_ensure_allocated(&inotify_data->inodes, &inode_data_hash_ops);
1493
0
        if (r < 0)
1494
0
                return r;
1495
0
1496
0
        d = new(struct inode_data, 1);
1497
0
        if (!d)
1498
0
                return -ENOMEM;
1499
0
1500
0
        *d = (struct inode_data) {
1501
0
                .dev = dev,
1502
0
                .ino = ino,
1503
0
                .wd = -1,
1504
0
                .fd = -1,
1505
0
                .inotify_data = inotify_data,
1506
0
        };
1507
0
1508
0
        r = hashmap_put(inotify_data->inodes, d, d);
1509
0
        if (r < 0) {
1510
0
                free(d);
1511
0
                return r;
1512
0
        }
1513
0
1514
0
        if (ret)
1515
0
                *ret = d;
1516
0
1517
0
        return 1;
1518
0
}
1519
1520
0
static uint32_t inode_data_determine_mask(struct inode_data *d) {
1521
0
        bool excl_unlink = true;
1522
0
        uint32_t combined = 0;
1523
0
        sd_event_source *s;
1524
0
1525
0
        assert(d);
1526
0
1527
0
        /* Combines the watch masks of all event sources watching this inode. We generally just OR them together, but
1528
0
         * the IN_EXCL_UNLINK flag is ANDed instead.
1529
0
         *
1530
0
         * Note that we add all sources to the mask here, regardless whether enabled, disabled or oneshot. That's
1531
0
         * because we cannot change the mask anymore after the event source was created once, since the kernel has no
1532
0
         * API for that. Hence we need to subscribe to the maximum mask we ever might be interested in, and suppress
1533
0
         * events we don't care for client-side. */
1534
0
1535
0
        LIST_FOREACH(inotify.by_inode_data, s, d->event_sources) {
1536
0
1537
0
                if ((s->inotify.mask & IN_EXCL_UNLINK) == 0)
1538
0
                        excl_unlink = false;
1539
0
1540
0
                combined |= s->inotify.mask;
1541
0
        }
1542
0
1543
0
        return (combined & ~(IN_ONESHOT|IN_DONT_FOLLOW|IN_ONLYDIR|IN_EXCL_UNLINK)) | (excl_unlink ? IN_EXCL_UNLINK : 0);
1544
0
}
1545
1546
0
static int inode_data_realize_watch(sd_event *e, struct inode_data *d) {
1547
0
        uint32_t combined_mask;
1548
0
        int wd, r;
1549
0
1550
0
        assert(d);
1551
0
        assert(d->fd >= 0);
1552
0
1553
0
        combined_mask = inode_data_determine_mask(d);
1554
0
1555
0
        if (d->wd >= 0 && combined_mask == d->combined_mask)
1556
0
                return 0;
1557
0
1558
0
        r = hashmap_ensure_allocated(&d->inotify_data->wd, NULL);
1559
0
        if (r < 0)
1560
0
                return r;
1561
0
1562
0
        wd = inotify_add_watch_fd(d->inotify_data->fd, d->fd, combined_mask);
1563
0
        if (wd < 0)
1564
0
                return -errno;
1565
0
1566
0
        if (d->wd < 0) {
1567
0
                r = hashmap_put(d->inotify_data->wd, INT_TO_PTR(wd), d);
1568
0
                if (r < 0) {
1569
0
                        (void) inotify_rm_watch(d->inotify_data->fd, wd);
1570
0
                        return r;
1571
0
                }
1572
0
1573
0
                d->wd = wd;
1574
0
1575
0
        } else if (d->wd != wd) {
1576
0
1577
0
                log_debug("Weird, the watch descriptor we already knew for this inode changed?");
1578
0
                (void) inotify_rm_watch(d->fd, wd);
1579
0
                return -EINVAL;
1580
0
        }
1581
0
1582
0
        d->combined_mask = combined_mask;
1583
0
        return 1;
1584
0
}
1585
1586
_public_ int sd_event_add_inotify(
1587
                sd_event *e,
1588
                sd_event_source **ret,
1589
                const char *path,
1590
                uint32_t mask,
1591
                sd_event_inotify_handler_t callback,
1592
0
                void *userdata) {
1593
0
1594
0
        struct inotify_data *inotify_data = NULL;
1595
0
        struct inode_data *inode_data = NULL;
1596
0
        _cleanup_close_ int fd = -1;
1597
0
        _cleanup_(source_freep) sd_event_source *s = NULL;
1598
0
        struct stat st;
1599
0
        int r;
1600
0
1601
0
        assert_return(e, -EINVAL);
1602
0
        assert_return(e = event_resolve(e), -ENOPKG);
1603
0
        assert_return(path, -EINVAL);
1604
0
        assert_return(callback, -EINVAL);
1605
0
        assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1606
0
        assert_return(!event_pid_changed(e), -ECHILD);
1607
0
1608
0
        /* Refuse IN_MASK_ADD since we coalesce watches on the same inode, and hence really don't want to merge
1609
0
         * masks. Or in other words, this whole code exists only to manage IN_MASK_ADD type operations for you, hence
1610
0
         * the user can't use them for us. */
1611
0
        if (mask & IN_MASK_ADD)
1612
0
                return -EINVAL;
1613
0
1614
0
        fd = open(path, O_PATH|O_CLOEXEC|
1615
0
                  (mask & IN_ONLYDIR ? O_DIRECTORY : 0)|
1616
0
                  (mask & IN_DONT_FOLLOW ? O_NOFOLLOW : 0));
1617
0
        if (fd < 0)
1618
0
                return -errno;
1619
0
1620
0
        if (fstat(fd, &st) < 0)
1621
0
                return -errno;
1622
0
1623
0
        s = source_new(e, !ret, SOURCE_INOTIFY);
1624
0
        if (!s)
1625
0
                return -ENOMEM;
1626
0
1627
0
        s->enabled = mask & IN_ONESHOT ? SD_EVENT_ONESHOT : SD_EVENT_ON;
1628
0
        s->inotify.mask = mask;
1629
0
        s->inotify.callback = callback;
1630
0
        s->userdata = userdata;
1631
0
1632
0
        /* Allocate an inotify object for this priority, and an inode object within it */
1633
0
        r = event_make_inotify_data(e, SD_EVENT_PRIORITY_NORMAL, &inotify_data);
1634
0
        if (r < 0)
1635
0
                return r;
1636
0
1637
0
        r = event_make_inode_data(e, inotify_data, st.st_dev, st.st_ino, &inode_data);
1638
0
        if (r < 0) {
1639
0
                event_free_inotify_data(e, inotify_data);
1640
0
                return r;
1641
0
        }
1642
0
1643
0
        /* Keep the O_PATH fd around until the first iteration of the loop, so that we can still change the priority of
1644
0
         * the event source, until then, for which we need the original inode. */
1645
0
        if (inode_data->fd < 0) {
1646
0
                inode_data->fd = TAKE_FD(fd);
1647
0
                LIST_PREPEND(to_close, e->inode_data_to_close, inode_data);
1648
0
        }
1649
0
1650
0
        /* Link our event source to the inode data object */
1651
0
        LIST_PREPEND(inotify.by_inode_data, inode_data->event_sources, s);
1652
0
        s->inotify.inode_data = inode_data;
1653
0
1654
0
        /* Actually realize the watch now */
1655
0
        r = inode_data_realize_watch(e, inode_data);
1656
0
        if (r < 0)
1657
0
                return r;
1658
0
1659
0
        (void) sd_event_source_set_description(s, path);
1660
0
1661
0
        if (ret)
1662
0
                *ret = s;
1663
0
        TAKE_PTR(s);
1664
0
1665
0
        return 0;
1666
0
}
1667
1668
194k
static sd_event_source* event_source_free(sd_event_source *s) {
1669
194k
        if (!s)
1670
0
                return NULL;
1671
194k
1672
194k
        /* Here's a special hack: when we are called from a
1673
194k
         * dispatch handler we won't free the event source
1674
194k
         * immediately, but we will detach the fd from the
1675
194k
         * epoll. This way it is safe for the caller to unref
1676
194k
         * the event source and immediately close the fd, but
1677
194k
         * we still retain a valid event source object after
1678
194k
         * the callback. */
1679
194k
1680
194k
        if (s->dispatching) {
1681
7.72k
                if (s->type == SOURCE_IO)
1682
342
                        source_io_unregister(s);
1683
7.72k
1684
7.72k
                source_disconnect(s);
1685
7.72k
        } else
1686
186k
                source_free(s);
1687
194k
1688
194k
        return NULL;
1689
194k
}
1690
1691
DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_event_source, sd_event_source, event_source_free);
1692
1693
142k
_public_ int sd_event_source_set_description(sd_event_source *s, const char *description) {
1694
142k
        assert_return(s, -EINVAL);
1695
142k
        assert_return(!event_pid_changed(s->event), -ECHILD);
1696
142k
1697
142k
        return free_and_strdup(&s->description, description);
1698
142k
}
1699
1700
0
_public_ int sd_event_source_get_description(sd_event_source *s, const char **description) {
1701
0
        assert_return(s, -EINVAL);
1702
0
        assert_return(description, -EINVAL);
1703
0
        assert_return(!event_pid_changed(s->event), -ECHILD);
1704
0
1705
0
        if (!s->description)
1706
0
                return -ENXIO;
1707
0
1708
0
        *description = s->description;
1709
0
        return 0;
1710
0
}
1711
1712
3.69k
_public_ sd_event *sd_event_source_get_event(sd_event_source *s) {
1713
3.69k
        assert_return(s, NULL);
1714
3.69k
1715
3.69k
        return s->event;
1716
3.69k
}
1717
1718
0
_public_ int sd_event_source_get_pending(sd_event_source *s) {
1719
0
        assert_return(s, -EINVAL);
1720
0
        assert_return(s->type != SOURCE_EXIT, -EDOM);
1721
0
        assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1722
0
        assert_return(!event_pid_changed(s->event), -ECHILD);
1723
0
1724
0
        return s->pending;
1725
0
}
1726
1727
0
_public_ int sd_event_source_get_io_fd(sd_event_source *s) {
1728
0
        assert_return(s, -EINVAL);
1729
0
        assert_return(s->type == SOURCE_IO, -EDOM);
1730
0
        assert_return(!event_pid_changed(s->event), -ECHILD);
1731
0
1732
0
        return s->io.fd;
1733
0
}
1734
1735
0
_public_ int sd_event_source_set_io_fd(sd_event_source *s, int fd) {
1736
0
        int r;
1737
0
1738
0
        assert_return(s, -EINVAL);
1739
0
        assert_return(fd >= 0, -EBADF);
1740
0
        assert_return(s->type == SOURCE_IO, -EDOM);
1741
0
        assert_return(!event_pid_changed(s->event), -ECHILD);
1742
0
1743
0
        if (s->io.fd == fd)
1744
0
                return 0;
1745
0
1746
0
        if (s->enabled == SD_EVENT_OFF) {
1747
0
                s->io.fd = fd;
1748
0
                s->io.registered = false;
1749
0
        } else {
1750
0
                int saved_fd;
1751
0
1752
0
                saved_fd = s->io.fd;
1753
0
                assert(s->io.registered);
1754
0
1755
0
                s->io.fd = fd;
1756
0
                s->io.registered = false;
1757
0
1758
0
                r = source_io_register(s, s->enabled, s->io.events);
1759
0
                if (r < 0) {
1760
0
                        s->io.fd = saved_fd;
1761
0
                        s->io.registered = true;
1762
0
                        return r;
1763
0
                }
1764
0
1765
0
                epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, saved_fd, NULL);
1766
0
        }
1767
0
1768
0
        return 0;
1769
0
}
1770
1771
0
_public_ int sd_event_source_get_io_fd_own(sd_event_source *s) {
1772
0
        assert_return(s, -EINVAL);
1773
0
        assert_return(s->type == SOURCE_IO, -EDOM);
1774
0
1775
0
        return s->io.owned;
1776
0
}
1777
1778
0
_public_ int sd_event_source_set_io_fd_own(sd_event_source *s, int own) {
1779
0
        assert_return(s, -EINVAL);
1780
0
        assert_return(s->type == SOURCE_IO, -EDOM);
1781
0
1782
0
        s->io.owned = own;
1783
0
        return 0;
1784
0
}
1785
1786
0
_public_ int sd_event_source_get_io_events(sd_event_source *s, uint32_t* events) {
1787
0
        assert_return(s, -EINVAL);
1788
0
        assert_return(events, -EINVAL);
1789
0
        assert_return(s->type == SOURCE_IO, -EDOM);
1790
0
        assert_return(!event_pid_changed(s->event), -ECHILD);
1791
0
1792
0
        *events = s->io.events;
1793
0
        return 0;
1794
0
}
1795
1796
3.80M
_public_ int sd_event_source_set_io_events(sd_event_source *s, uint32_t events) {
1797
3.80M
        int r;
1798
3.80M
1799
3.80M
        assert_return(s, -EINVAL);
1800
3.80M
        assert_return(s->type == SOURCE_IO, -EDOM);
1801
3.80M
        assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
1802
3.80M
        assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1803
3.80M
        assert_return(!event_pid_changed(s->event), -ECHILD);
1804
3.80M
1805
3.80M
        /* edge-triggered updates are never skipped, so we can reset edges */
1806
3.80M
        if (s->io.events == events && !(events & EPOLLET))
1807
2.94M
                return 0;
1808
858k
1809
858k
        r = source_set_pending(s, false);
1810
858k
        if (r < 0)
1811
0
                return r;
1812
858k
1813
858k
        if (s->enabled != SD_EVENT_OFF) {
1814
858k
                r = source_io_register(s, s->enabled, events);
1815
858k
                if (r < 0)
1816
0
                        return r;
1817
858k
        }
1818
858k
1819
858k
        s->io.events = events;
1820
858k
1821
858k
        return 0;
1822
858k
}
1823
1824
0
_public_ int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents) {
1825
0
        assert_return(s, -EINVAL);
1826
0
        assert_return(revents, -EINVAL);
1827
0
        assert_return(s->type == SOURCE_IO, -EDOM);
1828
0
        assert_return(s->pending, -ENODATA);
1829
0
        assert_return(!event_pid_changed(s->event), -ECHILD);
1830
0
1831
0
        *revents = s->io.revents;
1832
0
        return 0;
1833
0
}
1834
1835
0
_public_ int sd_event_source_get_signal(sd_event_source *s) {
1836
0
        assert_return(s, -EINVAL);
1837
0
        assert_return(s->type == SOURCE_SIGNAL, -EDOM);
1838
0
        assert_return(!event_pid_changed(s->event), -ECHILD);
1839
0
1840
0
        return s->signal.sig;
1841
0
}
1842
1843
0
_public_ int sd_event_source_get_priority(sd_event_source *s, int64_t *priority) {
1844
0
        assert_return(s, -EINVAL);
1845
0
        assert_return(!event_pid_changed(s->event), -ECHILD);
1846
0
1847
0
        *priority = s->priority;
1848
0
        return 0;
1849
0
}
1850
1851
162k
_public_ int sd_event_source_set_priority(sd_event_source *s, int64_t priority) {
1852
162k
        bool rm_inotify = false, rm_inode = false;
1853
162k
        struct inotify_data *new_inotify_data = NULL;
1854
162k
        struct inode_data *new_inode_data = NULL;
1855
162k
        int r;
1856
162k
1857
162k
        assert_return(s, -EINVAL);
1858
162k
        assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1859
162k
        assert_return(!event_pid_changed(s->event), -ECHILD);
1860
162k
1861
162k
        if (s->priority == priority)
1862
139k
                return 0;
1863
22.8k
1864
22.8k
        if (s->type == SOURCE_INOTIFY) {
1865
0
                struct inode_data *old_inode_data;
1866
0
1867
0
                assert(s->inotify.inode_data);
1868
0
                old_inode_data = s->inotify.inode_data;
1869
0
1870
0
                /* We need the original fd to change the priority. If we don't have it we can't change the priority,
1871
0
                 * anymore. Note that we close any fds when entering the next event loop iteration, i.e. for inotify
1872
0
                 * events we allow priority changes only until the first following iteration. */
1873
0
                if (old_inode_data->fd < 0)
1874
0
                        return -EOPNOTSUPP;
1875
0
1876
0
                r = event_make_inotify_data(s->event, priority, &new_inotify_data);
1877
0
                if (r < 0)
1878
0
                        return r;
1879
0
                rm_inotify = r > 0;
1880
0
1881
0
                r = event_make_inode_data(s->event, new_inotify_data, old_inode_data->dev, old_inode_data->ino, &new_inode_data);
1882
0
                if (r < 0)
1883
0
                        goto fail;
1884
0
                rm_inode = r > 0;
1885
0
1886
0
                if (new_inode_data->fd < 0) {
1887
0
                        /* Duplicate the fd for the new inode object if we don't have any yet */
1888
0
                        new_inode_data->fd = fcntl(old_inode_data->fd, F_DUPFD_CLOEXEC, 3);
1889
0
                        if (new_inode_data->fd < 0) {
1890
0
                                r = -errno;
1891
0
                                goto fail;
1892
0
                        }
1893
0
1894
0
                        LIST_PREPEND(to_close, s->event->inode_data_to_close, new_inode_data);
1895
0
                }
1896
0
1897
0
                /* Move the event source to the new inode data structure */
1898
0
                LIST_REMOVE(inotify.by_inode_data, old_inode_data->event_sources, s);
1899
0
                LIST_PREPEND(inotify.by_inode_data, new_inode_data->event_sources, s);
1900
0
                s->inotify.inode_data = new_inode_data;
1901
0
1902
0
                /* Now create the new watch */
1903
0
                r = inode_data_realize_watch(s->event, new_inode_data);
1904
0
                if (r < 0) {
1905
0
                        /* Move it back */
1906
0
                        LIST_REMOVE(inotify.by_inode_data, new_inode_data->event_sources, s);
1907
0
                        LIST_PREPEND(inotify.by_inode_data, old_inode_data->event_sources, s);
1908
0
                        s->inotify.inode_data = old_inode_data;
1909
0
                        goto fail;
1910
0
                }
1911
0
1912
0
                s->priority = priority;
1913
0
1914
0
                event_gc_inode_data(s->event, old_inode_data);
1915
0
1916
22.8k
        } else if (s->type == SOURCE_SIGNAL && s->enabled != SD_EVENT_OFF) {
1917
0
                struct signal_data *old, *d;
1918
0
1919
0
                /* Move us from the signalfd belonging to the old
1920
0
                 * priority to the signalfd of the new priority */
1921
0
1922
0
                assert_se(old = hashmap_get(s->event->signal_data, &s->priority));
1923
0
1924
0
                s->priority = priority;
1925
0
1926
0
                r = event_make_signal_data(s->event, s->signal.sig, &d);
1927
0
                if (r < 0) {
1928
0
                        s->priority = old->priority;
1929
0
                        return r;
1930
0
                }
1931
0
1932
0
                event_unmask_signal_data(s->event, old, s->signal.sig);
1933
0
        } else
1934
22.8k
                s->priority = priority;
1935
22.8k
1936
22.8k
        if (s->pending)
1937
21.7k
                prioq_reshuffle(s->event->pending, s, &s->pending_index);
1938
22.8k
1939
22.8k
        if (s->prepare)
1940
0
                prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1941
22.8k
1942
22.8k
        if (s->type == SOURCE_EXIT)
1943
0
                prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1944
22.8k
1945
22.8k
        return 0;
1946
0
1947
0
fail:
1948
0
        if (rm_inode)
1949
0
                event_free_inode_data(s->event, new_inode_data);
1950
0
1951
0
        if (rm_inotify)
1952
0
                event_free_inotify_data(s->event, new_inotify_data);
1953
0
1954
0
        return r;
1955
22.8k
}
1956
1957
0
_public_ int sd_event_source_get_enabled(sd_event_source *s, int *m) {
1958
0
        assert_return(s, -EINVAL);
1959
0
        assert_return(!event_pid_changed(s->event), -ECHILD);
1960
0
1961
0
        if (m)
1962
0
                *m = s->enabled;
1963
0
        return s->enabled != SD_EVENT_OFF;
1964
0
}
1965
1966
5.55M
_public_ int sd_event_source_set_enabled(sd_event_source *s, int m) {
1967
5.55M
        int r;
1968
5.55M
1969
5.55M
        assert_return(s, -EINVAL);
1970
5.55M
        assert_return(IN_SET(m, SD_EVENT_OFF, SD_EVENT_ON, SD_EVENT_ONESHOT), -EINVAL);
1971
5.55M
        assert_return(!event_pid_changed(s->event), -ECHILD);
1972
5.55M
1973
5.55M
        /* If we are dead anyway, we are fine with turning off
1974
5.55M
         * sources, but everything else needs to fail. */
1975
5.55M
        if (s->event->state == SD_EVENT_FINISHED)
1976
0
                return m == SD_EVENT_OFF ? 0 : -ESTALE;
1977
5.55M
1978
5.55M
        if (s->enabled == m)
1979
4.79M
                return 0;
1980
765k
1981
765k
        if (m == SD_EVENT_OFF) {
1982
422k
1983
422k
                /* Unset the pending flag when this event source is disabled */
1984
422k
                if (!IN_SET(s->type, SOURCE_DEFER, SOURCE_EXIT)) {
1985
47.5k
                        r = source_set_pending(s, false);
1986
47.5k
                        if (r < 0)
1987
0
                                return r;
1988
422k
                }
1989
422k
1990
422k
                switch (s->type) {
1991
422k
1992
422k
                case SOURCE_IO:
1993
34.9k
                        source_io_unregister(s);
1994
34.9k
                        s->enabled = m;
1995
34.9k
                        break;
1996
422k
1997
422k
                case SOURCE_TIME_REALTIME:
1998
12.6k
                case SOURCE_TIME_BOOTTIME:
1999
12.6k
                case SOURCE_TIME_MONOTONIC:
2000
12.6k
                case SOURCE_TIME_REALTIME_ALARM:
2001
12.6k
                case SOURCE_TIME_BOOTTIME_ALARM: {
2002
12.6k
                        struct clock_data *d;
2003
12.6k
2004
12.6k
                        s->enabled = m;
2005
12.6k
                        d = event_get_clock_data(s->event, s->type);
2006
12.6k
                        assert(d);
2007
12.6k
2008
12.6k
                        prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
2009
12.6k
                        prioq_reshuffle(d->latest, s, &s->time.latest_index);
2010
12.6k
                        d->needs_rearm = true;
2011
12.6k
                        break;
2012
12.6k
                }
2013
12.6k
2014
12.6k
                case SOURCE_SIGNAL:
2015
0
                        s->enabled = m;
2016
0
2017
0
                        event_gc_signal_data(s->event, &s->priority, s->signal.sig);
2018
0
                        break;
2019
12.6k
2020
12.6k
                case SOURCE_CHILD:
2021
0
                        s->enabled = m;
2022
0
2023
0
                        assert(s->event->n_enabled_child_sources > 0);
2024
0
                        s->event->n_enabled_child_sources--;
2025
0
2026
0
                        event_gc_signal_data(s->event, &s->priority, SIGCHLD);
2027
0
                        break;
2028
0
2029
7.38k
                case SOURCE_EXIT:
2030
7.38k
                        s->enabled = m;
2031
7.38k
                        prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
2032
7.38k
                        break;
2033
0
2034
367k
                case SOURCE_DEFER:
2035
367k
                case SOURCE_POST:
2036
367k
                case SOURCE_INOTIFY:
2037
367k
                        s->enabled = m;
2038
367k
                        break;
2039
367k
2040
367k
                default:
2041
0
                        assert_not_reached("Wut? I shouldn't exist.");
2042
422k
                }
2043
422k
2044
422k
        } else {
2045
342k
2046
342k
                /* Unset the pending flag when this event source is enabled */
2047
342k
                if (s->enabled == SD_EVENT_OFF && !IN_SET(s->type, SOURCE_DEFER, SOURCE_EXIT)) {
2048
1
                        r = source_set_pending(s, false);
2049
1
                        if (r < 0)
2050
0
                                return r;
2051
342k
                }
2052
342k
2053
342k
                switch (s->type) {
2054
342k
2055
342k
                case SOURCE_IO:
2056
0
                        r = source_io_register(s, m, s->io.events);
2057
0
                        if (r < 0)
2058
0
                                return r;
2059
0
2060
0
                        s->enabled = m;
2061
0
                        break;
2062
0
2063
1
                case SOURCE_TIME_REALTIME:
2064
1
                case SOURCE_TIME_BOOTTIME:
2065
1
                case SOURCE_TIME_MONOTONIC:
2066
1
                case SOURCE_TIME_REALTIME_ALARM:
2067
1
                case SOURCE_TIME_BOOTTIME_ALARM: {
2068
1
                        struct clock_data *d;
2069
1
2070
1
                        s->enabled = m;
2071
1
                        d = event_get_clock_data(s->event, s->type);
2072
1
                        assert(d);
2073
1
2074
1
                        prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
2075
1
                        prioq_reshuffle(d->latest, s, &s->time.latest_index);
2076
1
                        d->needs_rearm = true;
2077
1
                        break;
2078
1
                }
2079
1
2080
1
                case SOURCE_SIGNAL:
2081
0
2082
0
                        s->enabled = m;
2083
0
2084
0
                        r = event_make_signal_data(s->event, s->signal.sig, NULL);
2085
0
                        if (r < 0) {
2086
0
                                s->enabled = SD_EVENT_OFF;
2087
0
                                event_gc_signal_data(s->event, &s->priority, s->signal.sig);
2088
0
                                return r;
2089
0
                        }
2090
0
2091
0
                        break;
2092
0
2093
0
                case SOURCE_CHILD:
2094
0
2095
0
                        if (s->enabled == SD_EVENT_OFF)
2096
0
                                s->event->n_enabled_child_sources++;
2097
0
2098
0
                        s->enabled = m;
2099
0
2100
0
                        r = event_make_signal_data(s->event, SIGCHLD, NULL);
2101
0
                        if (r < 0) {
2102
0
                                s->enabled = SD_EVENT_OFF;
2103
0
                                s->event->n_enabled_child_sources--;
2104
0
                                event_gc_signal_data(s->event, &s->priority, SIGCHLD);
2105
0
                                return r;
2106
0
                        }
2107
0
2108
0
                        break;
2109
0
2110
0
                case SOURCE_EXIT:
2111
0
                        s->enabled = m;
2112
0
                        prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
2113
0
                        break;
2114
0
2115
342k
                case SOURCE_DEFER:
2116
342k
                case SOURCE_POST:
2117
342k
                case SOURCE_INOTIFY:
2118
342k
                        s->enabled = m;
2119
342k
                        break;
2120
342k
2121
342k
                default:
2122
0
                        assert_not_reached("Wut? I shouldn't exist.");
2123
342k
                }
2124
342k
        }
2125
765k
2126
765k
        if (s->pending)
2127
710k
                prioq_reshuffle(s->event->pending, s, &s->pending_index);
2128
765k
2129
765k
        if (s->prepare)
2130
7.38k
                prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
2131
765k
2132
765k
        return 0;
2133
765k
}
2134
2135
0
_public_ int sd_event_source_get_time(sd_event_source *s, uint64_t *usec) {
2136
0
        assert_return(s, -EINVAL);
2137
0
        assert_return(usec, -EINVAL);
2138
0
        assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
2139
0
        assert_return(!event_pid_changed(s->event), -ECHILD);
2140
0
2141
0
        *usec = s->time.next;
2142
0
        return 0;
2143
0
}
2144
2145
875k
_public_ int sd_event_source_set_time(sd_event_source *s, uint64_t usec) {
2146
875k
        struct clock_data *d;
2147
875k
        int r;
2148
875k
2149
875k
        assert_return(s, -EINVAL);
2150
875k
        assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
2151
875k
        assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
2152
875k
        assert_return(!event_pid_changed(s->event), -ECHILD);
2153
875k
2154
875k
        r = source_set_pending(s, false);
2155
875k
        if (r < 0)
2156
0
                return r;
2157
875k
2158
875k
        s->time.next = usec;
2159
875k
2160
875k
        d = event_get_clock_data(s->event, s->type);
2161
875k
        assert(d);
2162
875k
2163
875k
        prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
2164
875k
        prioq_reshuffle(d->latest, s, &s->time.latest_index);
2165
875k
        d->needs_rearm = true;
2166
875k
2167
875k
        return 0;
2168
875k
}
2169
2170
0
_public_ int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec) {
2171
0
        assert_return(s, -EINVAL);
2172
0
        assert_return(usec, -EINVAL);
2173
0
        assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
2174
0
        assert_return(!event_pid_changed(s->event), -ECHILD);
2175
0
2176
0
        *usec = s->time.accuracy;
2177
0
        return 0;
2178
0
}
2179
2180
1
_public_ int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec) {
2181
1
        struct clock_data *d;
2182
1
        int r;
2183
1
2184
1
        assert_return(s, -EINVAL);
2185
1
        assert_return(usec != (uint64_t) -1, -EINVAL);
2186
1
        assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
2187
1
        assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
2188
1
        assert_return(!event_pid_changed(s->event), -ECHILD);
2189
1
2190
1
        r = source_set_pending(s, false);
2191
1
        if (r < 0)
2192
0
                return r;
2193
1
2194
1
        if (usec == 0)
2195
1
                usec = DEFAULT_ACCURACY_USEC;
2196
1
2197
1
        s->time.accuracy = usec;
2198
1
2199
1
        d = event_get_clock_data(s->event, s->type);
2200
1
        assert(d);
2201
1
2202
1
        prioq_reshuffle(d->latest, s, &s->time.latest_index);
2203
1
        d->needs_rearm = true;
2204
1
2205
1
        return 0;
2206
1
}
2207
2208
1
_public_ int sd_event_source_get_time_clock(sd_event_source *s, clockid_t *clock) {
2209
1
        assert_return(s, -EINVAL);
2210
1
        assert_return(clock, -EINVAL);
2211
1
        assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
2212
1
        assert_return(!event_pid_changed(s->event), -ECHILD);
2213
1
2214
1
        *clock = event_source_type_to_clock(s->type);
2215
1
        return 0;
2216
1
}
2217
2218
0
_public_ int sd_event_source_get_child_pid(sd_event_source *s, pid_t *pid) {
2219
0
        assert_return(s, -EINVAL);
2220
0
        assert_return(pid, -EINVAL);
2221
0
        assert_return(s->type == SOURCE_CHILD, -EDOM);
2222
0
        assert_return(!event_pid_changed(s->event), -ECHILD);
2223
0
2224
0
        *pid = s->child.pid;
2225
0
        return 0;
2226
0
}
2227
2228
0
_public_ int sd_event_source_get_inotify_mask(sd_event_source *s, uint32_t *mask) {
2229
0
        assert_return(s, -EINVAL);
2230
0
        assert_return(mask, -EINVAL);
2231
0
        assert_return(s->type == SOURCE_INOTIFY, -EDOM);
2232
0
        assert_return(!event_pid_changed(s->event), -ECHILD);
2233
0
2234
0
        *mask = s->inotify.mask;
2235
0
        return 0;
2236
0
}
2237
2238
47.4k
_public_ int sd_event_source_set_prepare(sd_event_source *s, sd_event_handler_t callback) {
2239
47.4k
        int r;
2240
47.4k
2241
47.4k
        assert_return(s, -EINVAL);
2242
47.4k
        assert_return(s->type != SOURCE_EXIT, -EDOM);
2243
47.4k
        assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
2244
47.4k
        assert_return(!event_pid_changed(s->event), -ECHILD);
2245
47.4k
2246
47.4k
        if (s->prepare == callback)
2247
0
                return 0;
2248
47.4k
2249
47.4k
        if (callback && s->prepare) {
2250
0
                s->prepare = callback;
2251
0
                return 0;
2252
0
        }
2253
47.4k
2254
47.4k
        r = prioq_ensure_allocated(&s->event->prepare, prepare_prioq_compare);
2255
47.4k
        if (r < 0)
2256
0
                return r;
2257
47.4k
2258
47.4k
        s->prepare = callback;
2259
47.4k
2260
47.4k
        if (callback) {
2261
47.4k
                r = prioq_put(s->event->prepare, s, &s->prepare_index);
2262
47.4k
                if (r < 0)
2263
0
                        return r;
2264
0
        } else
2265
0
                prioq_remove(s->event->prepare, s, &s->prepare_index);
2266
47.4k
2267
47.4k
        return 0;
2268
47.4k
}
2269
2270
0
_public_ void* sd_event_source_get_userdata(sd_event_source *s) {
2271
0
        assert_return(s, NULL);
2272
0
2273
0
        return s->userdata;
2274
0
}
2275
2276
1
_public_ void *sd_event_source_set_userdata(sd_event_source *s, void *userdata) {
2277
1
        void *ret;
2278
1
2279
1
        assert_return(s, NULL);
2280
1
2281
1
        ret = s->userdata;
2282
1
        s->userdata = userdata;
2283
1
2284
1
        return ret;
2285
1
}
2286
2287
4.50k
static usec_t sleep_between(sd_event *e, usec_t a, usec_t b) {
2288
4.50k
        usec_t c;
2289
4.50k
        assert(e);
2290
4.50k
        assert(a <= b);
2291
4.50k
2292
4.50k
        if (a <= 0)
2293
4.50k
                return 0;
2294
0
        if (a >= USEC_INFINITY)
2295
0
                return USEC_INFINITY;
2296
0
2297
0
        if (b <= a + 1)
2298
0
                return a;
2299
0
2300
0
        initialize_perturb(e);
2301
0
2302
0
        /*
2303
0
          Find a good time to wake up again between times a and b. We
2304
0
          have two goals here:
2305
0
2306
0
          a) We want to wake up as seldom as possible, hence prefer
2307
0
             later times over earlier times.
2308
0
2309
0
          b) But if we have to wake up, then let's make sure to
2310
0
             dispatch as much as possible on the entire system.
2311
0
2312
0
          We implement this by waking up everywhere at the same time
2313
0
          within any given minute if we can, synchronised via the
2314
0
          perturbation value determined from the boot ID. If we can't,
2315
0
          then we try to find the same spot in every 10s, then 1s and
2316
0
          then 250ms step. Otherwise, we pick the last possible time
2317
0
          to wake up.
2318
0
        */
2319
0
2320
0
        c = (b / USEC_PER_MINUTE) * USEC_PER_MINUTE + e->perturb;
2321
0
        if (c >= b) {
2322
0
                if (_unlikely_(c < USEC_PER_MINUTE))
2323
0
                        return b;
2324
0
2325
0
                c -= USEC_PER_MINUTE;
2326
0
        }
2327
0
2328
0
        if (c >= a)
2329
0
                return c;
2330
0
2331
0
        c = (b / (USEC_PER_SEC*10)) * (USEC_PER_SEC*10) + (e->perturb % (USEC_PER_SEC*10));
2332
0
        if (c >= b) {
2333
0
                if (_unlikely_(c < USEC_PER_SEC*10))
2334
0
                        return b;
2335
0
2336
0
                c -= USEC_PER_SEC*10;
2337
0
        }
2338
0
2339
0
        if (c >= a)
2340
0
                return c;
2341
0
2342
0
        c = (b / USEC_PER_SEC) * USEC_PER_SEC + (e->perturb % USEC_PER_SEC);
2343
0
        if (c >= b) {
2344
0
                if (_unlikely_(c < USEC_PER_SEC))
2345
0
                        return b;
2346
0
2347
0
                c -= USEC_PER_SEC;
2348
0
        }
2349
0
2350
0
        if (c >= a)
2351
0
                return c;
2352
0
2353
0
        c = (b / (USEC_PER_MSEC*250)) * (USEC_PER_MSEC*250) + (e->perturb % (USEC_PER_MSEC*250));
2354
0
        if (c >= b) {
2355
0
                if (_unlikely_(c < USEC_PER_MSEC*250))
2356
0
                        return b;
2357
0
2358
0
                c -= USEC_PER_MSEC*250;
2359
0
        }
2360
0
2361
0
        if (c >= a)
2362
0
                return c;
2363
0
2364
0
        return b;
2365
0
}
2366
2367
static int event_arm_timer(
2368
                sd_event *e,
2369
14.0M
                struct clock_data *d) {
2370
14.0M
2371
14.0M
        struct itimerspec its = {};
2372
14.0M
        sd_event_source *a, *b;
2373
14.0M
        usec_t t;
2374
14.0M
        int r;
2375
14.0M
2376
14.0M
        assert(e);
2377
14.0M
        assert(d);
2378
14.0M
2379
14.0M
        if (!d->needs_rearm)
2380
13.1M
                return 0;
2381
883k
        else
2382
883k
                d->needs_rearm = false;
2383
14.0M
2384
14.0M
        a = prioq_peek(d->earliest);
2385
883k
        if (!a || a->enabled == SD_EVENT_OFF || a->time.next == USEC_INFINITY) {
2386
879k
2387
879k
                if (d->fd < 0)
2388
0
                        return 0;
2389
879k
2390
879k
                if (d->next == USEC_INFINITY)
2391
879k
                        return 0;
2392
0
2393
0
                /* disarm */
2394
0
                r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
2395
0
                if (r < 0)
2396
0
                        return r;
2397
0
2398
0
                d->next = USEC_INFINITY;
2399
0
                return 0;
2400
0
        }
2401
4.50k
2402
4.50k
        b = prioq_peek(d->latest);
2403
4.50k
        assert_se(b && b->enabled != SD_EVENT_OFF);
2404
4.50k
2405
4.50k
        t = sleep_between(e, a->time.next, time_event_source_latest(b));
2406
4.50k
        if (d->next == t)
2407
0
                return 0;
2408
4.50k
2409
4.50k
        assert_se(d->fd >= 0);
2410
4.50k
2411
4.50k
        if (t == 0) {
2412
4.50k
                /* We don' want to disarm here, just mean some time looooong ago. */
2413
4.50k
                its.it_value.tv_sec = 0;
2414
4.50k
                its.it_value.tv_nsec = 1;
2415
4.50k
        } else
2416
0
                timespec_store(&its.it_value, t);
2417
4.50k
2418
4.50k
        r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
2419
4.50k
        if (r < 0)
2420
0
                return -errno;
2421
4.50k
2422
4.50k
        d->next = t;
2423
4.50k
        return 0;
2424
4.50k
}
2425
2426
4.76M
static int process_io(sd_event *e, sd_event_source *s, uint32_t revents) {
2427
4.76M
        assert(e);
2428
4.76M
        assert(s);
2429
4.76M
        assert(s->type == SOURCE_IO);
2430
4.76M
2431
4.76M
        /* If the event source was already pending, we just OR in the
2432
4.76M
         * new revents, otherwise we reset the value. The ORing is
2433
4.76M
         * necessary to handle EPOLLONESHOT events properly where
2434
4.76M
         * readability might happen independently of writability, and
2435
4.76M
         * we need to keep track of both */
2436
4.76M
2437
4.76M
        if (s->pending)
2438
3.51M
                s->io.revents |= revents;
2439
1.24M
        else
2440
1.24M
                s->io.revents = revents;
2441
4.76M
2442
4.76M
        return source_set_pending(s, true);
2443
4.76M
}
2444
2445
4.50k
static int flush_timer(sd_event *e, int fd, uint32_t events, usec_t *next) {
2446
4.50k
        uint64_t x;
2447
4.50k
        ssize_t ss;
2448
4.50k
2449
4.50k
        assert(e);
2450
4.50k
        assert(fd >= 0);
2451
4.50k
2452
4.50k
        assert_return(events == EPOLLIN, -EIO);
2453
4.50k
2454
4.50k
        ss = read(fd, &x, sizeof(x));
2455
4.50k
        if (ss < 0) {
2456
0
                if (IN_SET(errno, EAGAIN, EINTR))
2457
0
                        return 0;
2458
0
2459
0
                return -errno;
2460
0
        }
2461
4.50k
2462
4.50k
        if (_unlikely_(ss != sizeof(x)))
2463
4.50k
                return -EIO;
2464
4.50k
2465
4.50k
        if (next)
2466
4.50k
                *next = USEC_INFINITY;
2467
4.50k
2468
4.50k
        return 0;
2469
4.50k
}
2470
2471
static int process_timer(
2472
                sd_event *e,
2473
                usec_t n,
2474
14.0M
                struct clock_data *d) {
2475
14.0M
2476
14.0M
        sd_event_source *s;
2477
14.0M
        int r;
2478
14.0M
2479
14.0M
        assert(e);
2480
14.0M
        assert(d);
2481
14.0M
2482
14.0M
        for (;;) {
2483
14.0M
                s = prioq_peek(d->earliest);
2484
14.0M
                if (!s ||
2485
14.0M
                    s->time.next > n ||
2486
14.0M
                    s->enabled == SD_EVENT_OFF ||
2487
14.0M
                    s->pending)
2488
14.0M
                        break;
2489
4.50k
2490
4.50k
                r = source_set_pending(s, true);
2491
4.50k
                if (r < 0)
2492
0
                        return r;
2493
4.50k
2494
4.50k
                prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
2495
4.50k
                prioq_reshuffle(d->latest, s, &s->time.latest_index);
2496
4.50k
                d->needs_rearm = true;
2497
4.50k
        }
2498
14.0M
2499
14.0M
        return 0;
2500
14.0M
}
2501
2502
0
static int process_child(sd_event *e) {
2503
0
        sd_event_source *s;
2504
0
        Iterator i;
2505
0
        int r;
2506
0
2507
0
        assert(e);
2508
0
2509
0
        e->need_process_child = false;
2510
0
2511
0
        /*
2512
0
           So, this is ugly. We iteratively invoke waitid() with P_PID
2513
0
           + WNOHANG for each PID we wait for, instead of using
2514
0
           P_ALL. This is because we only want to get child
2515
0
           information of very specific child processes, and not all
2516
0
           of them. We might not have processed the SIGCHLD even of a
2517
0
           previous invocation and we don't want to maintain a
2518
0
           unbounded *per-child* event queue, hence we really don't
2519
0
           want anything flushed out of the kernel's queue that we
2520
0
           don't care about. Since this is O(n) this means that if you
2521
0
           have a lot of processes you probably want to handle SIGCHLD
2522
0
           yourself.
2523
0
2524
0
           We do not reap the children here (by using WNOWAIT), this
2525
0
           is only done after the event source is dispatched so that
2526
0
           the callback still sees the process as a zombie.
2527
0
        */
2528
0
2529
0
        HASHMAP_FOREACH(s, e->child_sources, i) {
2530
0
                assert(s->type == SOURCE_CHILD);
2531
0
2532
0
                if (s->pending)
2533
0
                        continue;
2534
0
2535
0
                if (s->enabled == SD_EVENT_OFF)
2536
0
                        continue;
2537
0
2538
0
                zero(s->child.siginfo);
2539
0
                r = waitid(P_PID, s->child.pid, &s->child.siginfo,
2540
0
                           WNOHANG | (s->child.options & WEXITED ? WNOWAIT : 0) | s->child.options);
2541
0
                if (r < 0)
2542
0
                        return -errno;
2543
0
2544
0
                if (s->child.siginfo.si_pid != 0) {
2545
0
                        bool zombie = IN_SET(s->child.siginfo.si_code, CLD_EXITED, CLD_KILLED, CLD_DUMPED);
2546
0
2547
0
                        if (!zombie && (s->child.options & WEXITED)) {
2548
0
                                /* If the child isn't dead then let's
2549
0
                                 * immediately remove the state change
2550
0
                                 * from the queue, since there's no
2551
0
                                 * benefit in leaving it queued */
2552
0
2553
0
                                assert(s->child.options & (WSTOPPED|WCONTINUED));
2554
0
                                waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|(s->child.options & (WSTOPPED|WCONTINUED)));
2555
0
                        }
2556
0
2557
0
                        r = source_set_pending(s, true);
2558
0
                        if (r < 0)
2559
0
                                return r;
2560
0
                }
2561
0
        }
2562
0
2563
0
        return 0;
2564
0
}
2565
2566
0
static int process_signal(sd_event *e, struct signal_data *d, uint32_t events) {
2567
0
        bool read_one = false;
2568
0
        int r;
2569
0
2570
0
        assert(e);
2571
0
        assert(d);
2572
0
        assert_return(events == EPOLLIN, -EIO);
2573
0
2574
0
        /* If there's a signal queued on this priority and SIGCHLD is
2575
0
           on this priority too, then make sure to recheck the
2576
0
           children we watch. This is because we only ever dequeue
2577
0
           the first signal per priority, and if we dequeue one, and
2578
0
           SIGCHLD might be enqueued later we wouldn't know, but we
2579
0
           might have higher priority children we care about hence we
2580
0
           need to check that explicitly. */
2581
0
2582
0
        if (sigismember(&d->sigset, SIGCHLD))
2583
0
                e->need_process_child = true;
2584
0
2585
0
        /* If there's already an event source pending for this
2586
0
         * priority we don't read another */
2587
0
        if (d->current)
2588
0
                return 0;
2589
0
2590
0
        for (;;) {
2591
0
                struct signalfd_siginfo si;
2592
0
                ssize_t n;
2593
0
                sd_event_source *s = NULL;
2594
0
2595
0
                n = read(d->fd, &si, sizeof(si));
2596
0
                if (n < 0) {
2597
0
                        if (IN_SET(errno, EAGAIN, EINTR))
2598
0
                                return read_one;
2599
0
2600
0
                        return -errno;
2601
0
                }
2602
0
2603
0
                if (_unlikely_(n != sizeof(si)))
2604
0
                        return -EIO;
2605
0
2606
0
                assert(SIGNAL_VALID(si.ssi_signo));
2607
0
2608
0
                read_one = true;
2609
0
2610
0
                if (e->signal_sources)
2611
0
                        s = e->signal_sources[si.ssi_signo];
2612
0
                if (!s)
2613
0
                        continue;
2614
0
                if (s->pending)
2615
0
                        continue;
2616
0
2617
0
                s->signal.siginfo = si;
2618
0
                d->current = s;
2619
0
2620
0
                r = source_set_pending(s, true);
2621
0
                if (r < 0)
2622
0
                        return r;
2623
0
2624
0
                return 1;
2625
0
        }
2626
0
}
2627
2628
0
static int event_inotify_data_read(sd_event *e, struct inotify_data *d, uint32_t revents) {
2629
0
        ssize_t n;
2630
0
2631
0
        assert(e);
2632
0
        assert(d);
2633
0
2634
0
        assert_return(revents == EPOLLIN, -EIO);
2635
0
2636
0
        /* If there's already an event source pending for this priority, don't read another */
2637
0
        if (d->n_pending > 0)
2638
0
                return 0;
2639
0
2640
0
        /* Is the read buffer non-empty? If so, let's not read more */
2641
0
        if (d->buffer_filled > 0)
2642
0
                return 0;
2643
0
2644
0
        n = read(d->fd, &d->buffer, sizeof(d->buffer));
2645
0
        if (n < 0) {
2646
0
                if (IN_SET(errno, EAGAIN, EINTR))
2647
0
                        return 0;
2648
0
2649
0
                return -errno;
2650
0
        }
2651
0
2652
0
        assert(n > 0);
2653
0
        d->buffer_filled = (size_t) n;
2654
0
        LIST_PREPEND(buffered, e->inotify_data_buffered, d);
2655
0
2656
0
        return 1;
2657
0
}
2658
2659
0
static void event_inotify_data_drop(sd_event *e, struct inotify_data *d, size_t sz) {
2660
0
        assert(e);
2661
0
        assert(d);
2662
0
        assert(sz <= d->buffer_filled);
2663
0
2664
0
        if (sz == 0)
2665
0
                return;
2666
0
2667
0
        /* Move the rest to the buffer to the front, in order to get things properly aligned again */
2668
0
        memmove(d->buffer.raw, d->buffer.raw + sz, d->buffer_filled - sz);
2669
0
        d->buffer_filled -= sz;
2670
0
2671
0
        if (d->buffer_filled == 0)
2672
0
                LIST_REMOVE(buffered, e->inotify_data_buffered, d);
2673
0
}
2674
2675
0
static int event_inotify_data_process(sd_event *e, struct inotify_data *d) {
2676
0
        int r;
2677
0
2678
0
        assert(e);
2679
0
        assert(d);
2680
0
2681
0
        /* If there's already an event source pending for this priority, don't read another */
2682
0
        if (d->n_pending > 0)
2683
0
                return 0;
2684
0
2685
0
        while (d->buffer_filled > 0) {
2686
0
                size_t sz;
2687
0
2688
0
                /* Let's validate that the event structures are complete */
2689
0
                if (d->buffer_filled < offsetof(struct inotify_event, name))
2690
0
                        return -EIO;
2691
0
2692
0
                sz = offsetof(struct inotify_event, name) + d->buffer.ev.len;
2693
0
                if (d->buffer_filled < sz)
2694
0
                        return -EIO;
2695
0
2696
0
                if (d->buffer.ev.mask & IN_Q_OVERFLOW) {
2697
0
                        struct inode_data *inode_data;
2698
0
                        Iterator i;
2699
0
2700
0
                        /* The queue overran, let's pass this event to all event sources connected to this inotify
2701
0
                         * object */
2702
0
2703
0
                        HASHMAP_FOREACH(inode_data, d->inodes, i) {
2704
0
                                sd_event_source *s;
2705
0
2706
0
                                LIST_FOREACH(inotify.by_inode_data, s, inode_data->event_sources) {
2707
0
2708
0
                                        if (s->enabled == SD_EVENT_OFF)
2709
0
                                                continue;
2710
0
2711
0
                                        r = source_set_pending(s, true);
2712
0
                                        if (r < 0)
2713
0
                                                return r;
2714
0
                                }
2715
0
                        }
2716
0
                } else {
2717
0
                        struct inode_data *inode_data;
2718
0
                        sd_event_source *s;
2719
0
2720
0
                        /* Find the inode object for this watch descriptor. If IN_IGNORED is set we also remove it from
2721
0
                         * our watch descriptor table. */
2722
0
                        if (d->buffer.ev.mask & IN_IGNORED) {
2723
0
2724
0
                                inode_data = hashmap_remove(d->wd, INT_TO_PTR(d->buffer.ev.wd));
2725
0
                                if (!inode_data) {
2726
0
                                        event_inotify_data_drop(e, d, sz);
2727
0
                                        continue;
2728
0
                                }
2729
0
2730
0
                                /* The watch descriptor was removed by the kernel, let's drop it here too */
2731
0
                                inode_data->wd = -1;
2732
0
                        } else {
2733
0
                                inode_data = hashmap_get(d->wd, INT_TO_PTR(d->buffer.ev.wd));
2734
0
                                if (!inode_data) {
2735
0
                                        event_inotify_data_drop(e, d, sz);
2736
0
                                        continue;
2737
0
                                }
2738
0
                        }
2739
0
2740
0
                        /* Trigger all event sources that are interested in these events. Also trigger all event
2741
0
                         * sources if IN_IGNORED or IN_UNMOUNT is set. */
2742
0
                        LIST_FOREACH(inotify.by_inode_data, s, inode_data->event_sources) {
2743
0
2744
0
                                if (s->enabled == SD_EVENT_OFF)
2745
0
                                        continue;
2746
0
2747
0
                                if ((d->buffer.ev.mask & (IN_IGNORED|IN_UNMOUNT)) == 0 &&
2748
0
                                    (s->inotify.mask & d->buffer.ev.mask & IN_ALL_EVENTS) == 0)
2749
0
                                        continue;
2750
0
2751
0
                                r = source_set_pending(s, true);
2752
0
                                if (r < 0)
2753
0
                                        return r;
2754
0
                        }
2755
0
                }
2756
0
2757
0
                /* Something pending now? If so, let's finish, otherwise let's read more. */
2758
0
                if (d->n_pending > 0)
2759
0
                        return 1;
2760
0
        }
2761
0
2762
0
        return 0;
2763
0
}
2764
2765
2.81M
static int process_inotify(sd_event *e) {
2766
2.81M
        struct inotify_data *d;
2767
2.81M
        int r, done = 0;
2768
2.81M
2769
2.81M
        assert(e);
2770
2.81M
2771
2.81M
        LIST_FOREACH(buffered, d, e->inotify_data_buffered) {
2772
0
                r = event_inotify_data_process(e, d);
2773
0
                if (r < 0)
2774
0
                        return r;
2775
0
                if (r > 0)
2776
0
                        done ++;
2777
0
        }
2778
2.81M
2779
2.81M
        return done;
2780
2.81M
}
2781
2782
2.81M
static int source_dispatch(sd_event_source *s) {
2783
2.81M
        EventSourceType saved_type;
2784
2.81M
        int r = 0;
2785
2.81M
2786
2.81M
        assert(s);
2787
2.81M
        assert(s->pending || s->type == SOURCE_EXIT);
2788
2.81M
2789
2.81M
        /* Save the event source type, here, so that we still know it after the event callback which might invalidate
2790
2.81M
         * the event. */
2791
2.81M
        saved_type = s->type;
2792
2.81M
2793
2.81M
        if (!IN_SET(s->type, SOURCE_DEFER, SOURCE_EXIT)) {
2794
1.20M
                r = source_set_pending(s, false);
2795
1.20M
                if (r < 0)
2796
0
                        return r;
2797
2.81M
        }
2798
2.81M
2799
2.81M
        if (s->type != SOURCE_POST) {
2800
2.81M
                sd_event_source *z;
2801
2.81M
                Iterator i;
2802
2.81M
2803
2.81M
                /* If we execute a non-post source, let's mark all
2804
2.81M
                 * post sources as pending */
2805
2.81M
2806
2.81M
                SET_FOREACH(z, s->event->post_sources, i) {
2807
0
                        if (z->enabled == SD_EVENT_OFF)
2808
0
                                continue;
2809
0
2810
0
                        r = source_set_pending(z, true);
2811
0
                        if (r < 0)
2812
0
                                return r;
2813
0
                }
2814
2.81M
        }
2815
2.81M
2816
2.81M
        if (s->enabled == SD_EVENT_ONESHOT) {
2817
12.0k
                r = sd_event_source_set_enabled(s, SD_EVENT_OFF);
2818
12.0k
                if (r < 0)
2819
0
                        return r;
2820
2.81M
        }
2821
2.81M
2822
2.81M
        s->dispatching = true;
2823
2.81M
2824
2.81M
        switch (s->type) {
2825
2.81M
2826
2.81M
        case SOURCE_IO:
2827
1.20M
                r = s->io.callback(s, s->io.fd, s->io.revents, s->userdata);
2828
1.20M
                break;
2829
2.81M
2830
2.81M
        case SOURCE_TIME_REALTIME:
2831
0
        case SOURCE_TIME_BOOTTIME:
2832
0
        case SOURCE_TIME_MONOTONIC:
2833
0
        case SOURCE_TIME_REALTIME_ALARM:
2834
0
        case SOURCE_TIME_BOOTTIME_ALARM:
2835
0
                r = s->time.callback(s, s->time.next, s->userdata);
2836
0
                break;
2837
0
2838
0
        case SOURCE_SIGNAL:
2839
0
                r = s->signal.callback(s, &s->signal.siginfo, s->userdata);
2840
0
                break;
2841
0
2842
0
        case SOURCE_CHILD: {
2843
0
                bool zombie;
2844
0
2845
0
                zombie = IN_SET(s->child.siginfo.si_code, CLD_EXITED, CLD_KILLED, CLD_DUMPED);
2846
0
2847
0
                r = s->child.callback(s, &s->child.siginfo, s->userdata);
2848
0
2849
0
                /* Now, reap the PID for good. */
2850
0
                if (zombie)
2851
0
                        (void) waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|WEXITED);
2852
0
2853
0
                break;
2854
0
        }
2855
0
2856
1.60M
        case SOURCE_DEFER:
2857
1.60M
                r = s->defer.callback(s, s->userdata);
2858
1.60M
                break;
2859
0
2860
0
        case SOURCE_POST:
2861
0
                r = s->post.callback(s, s->userdata);
2862
0
                break;
2863
0
2864
963
        case SOURCE_EXIT:
2865
963
                r = s->exit.callback(s, s->userdata);
2866
963
                break;
2867
0
2868
0
        case SOURCE_INOTIFY: {
2869
0
                struct sd_event *e = s->event;
2870
0
                struct inotify_data *d;
2871
0
                size_t sz;
2872
0
2873
0
                assert(s->inotify.inode_data);
2874
0
                assert_se(d = s->inotify.inode_data->inotify_data);
2875
0
2876
0
                assert(d->buffer_filled >= offsetof(struct inotify_event, name));
2877
0
                sz = offsetof(struct inotify_event, name) + d->buffer.ev.len;
2878
0
                assert(d->buffer_filled >= sz);
2879
0
2880
0
                r = s->inotify.callback(s, &d->buffer.ev, s->userdata);
2881
0
2882
0
                /* When no event is pending anymore on this inotify object, then let's drop the event from the
2883
0
                 * buffer. */
2884
0
                if (d->n_pending == 0)
2885
0
                        event_inotify_data_drop(e, d, sz);
2886
0
2887
0
                break;
2888
0
        }
2889
0
2890
0
        case SOURCE_WATCHDOG:
2891
0
        case _SOURCE_EVENT_SOURCE_TYPE_MAX:
2892
0
        case _SOURCE_EVENT_SOURCE_TYPE_INVALID:
2893
0
                assert_not_reached("Wut? I shouldn't exist.");
2894
2.81M
        }
2895
2.81M
2896
2.81M
        s->dispatching = false;
2897
2.81M
2898
2.81M
        if (r < 0)
2899
2.81M
                log_debug_errno(r, "Event source %s (type %s) returned error, disabling: %m",
2900
2.81M
                                strna(s->description), event_source_type_to_string(saved_type));
2901
2.81M
2902
2.81M
        if (s->n_ref == 0)
2903
7.72k
                source_free(s);
2904
2.80M
        else if (r < 0)
2905
0
                sd_event_source_set_enabled(s, SD_EVENT_OFF);
2906
2.81M
2907
2.81M
        return 1;
2908
2.81M
}
2909
2910
2.81M
static int event_prepare(sd_event *e) {
2911
2.81M
        int r;
2912
2.81M
2913
2.81M
        assert(e);
2914
2.81M
2915
6.34M
        for (;;) {
2916
6.34M
                sd_event_source *s;
2917
6.34M
2918
6.34M
                s = prioq_peek(e->prepare);
2919
6.34M
                if (!s || s->prepare_iteration == e->iteration || s->enabled == SD_EVENT_OFF)
2920
2.81M
                        break;
2921
3.53M
2922
3.53M
                s->prepare_iteration = e->iteration;
2923
3.53M
                r = prioq_reshuffle(e->prepare, s, &s->prepare_index);
2924
3.53M
                if (r < 0)
2925
0
                        return r;
2926
3.53M
2927
3.53M
                assert(s->prepare);
2928
3.53M
2929
3.53M
                s->dispatching = true;
2930
3.53M
                r = s->prepare(s, s->userdata);
2931
3.53M
                s->dispatching = false;
2932
3.53M
2933
3.53M
                if (r < 0)
2934
3.53M
                        log_debug_errno(r, "Prepare callback of event source %s (type %s) returned error, disabling: %m",
2935
3.53M
                                        strna(s->description), event_source_type_to_string(s->type));
2936
3.53M
2937
3.53M
                if (s->n_ref == 0)
2938
0
                        source_free(s);
2939
3.53M
                else if (r < 0)
2940
0
                        sd_event_source_set_enabled(s, SD_EVENT_OFF);
2941
3.53M
        }
2942
2.81M
2943
2.81M
        return 0;
2944
2.81M
}
2945
2946
4.65k
static int dispatch_exit(sd_event *e) {
2947
4.65k
        sd_event_source *p;
2948
4.65k
        _cleanup_(sd_event_unrefp) sd_event *ref = NULL;
2949
4.65k
        int r;
2950
4.65k
2951
4.65k
        assert(e);
2952
4.65k
2953
4.65k
        p = prioq_peek(e->exit);
2954
4.65k
        if (!p || p->enabled == SD_EVENT_OFF) {
2955
3.69k
                e->state = SD_EVENT_FINISHED;
2956
3.69k
                return 0;
2957
3.69k
        }
2958
963
2959
963
        ref = sd_event_ref(e);
2960
963
        e->iteration++;
2961
963
        e->state = SD_EVENT_EXITING;
2962
963
        r = source_dispatch(p);
2963
963
        e->state = SD_EVENT_INITIAL;
2964
963
        return r;
2965
963
}
2966
2967
8.43M
static sd_event_source* event_next_pending(sd_event *e) {
2968
8.43M
        sd_event_source *p;
2969
8.43M
2970
8.43M
        assert(e);
2971
8.43M
2972
8.43M
        p = prioq_peek(e->pending);
2973
8.43M
        if (!p)
2974
51.2k
                return NULL;
2975
8.38M
2976
8.38M
        if (p->enabled == SD_EVENT_OFF)
2977
0
                return NULL;
2978
8.38M
2979
8.38M
        return p;
2980
8.38M
}
2981
2982
0
static int arm_watchdog(sd_event *e) {
2983
0
        struct itimerspec its = {};
2984
0
        usec_t t;
2985
0
        int r;
2986
0
2987
0
        assert(e);
2988
0
        assert(e->watchdog_fd >= 0);
2989
0
2990
0
        t = sleep_between(e,
2991
0
                          e->watchdog_last + (e->watchdog_period / 2),
2992
0
                          e->watchdog_last + (e->watchdog_period * 3 / 4));
2993
0
2994
0
        timespec_store(&its.it_value, t);
2995
0
2996
0
        /* Make sure we never set the watchdog to 0, which tells the
2997
0
         * kernel to disable it. */
2998
0
        if (its.it_value.tv_sec == 0 && its.it_value.tv_nsec == 0)
2999
0
                its.it_value.tv_nsec = 1;
3000
0
3001
0
        r = timerfd_settime(e->watchdog_fd, TFD_TIMER_ABSTIME, &its, NULL);
3002
0
        if (r < 0)
3003
0
                return -errno;
3004
0
3005
0
        return 0;
3006
0
}
3007
3008
2.81M
static int process_watchdog(sd_event *e) {
3009
2.81M
        assert(e);
3010
2.81M
3011
2.81M
        if (!e->watchdog)
3012
2.81M
                return 0;
3013
0
3014
0
        /* Don't notify watchdog too often */
3015
0
        if (e->watchdog_last + e->watchdog_period / 4 > e->timestamp.monotonic)
3016
0
                return 0;
3017
0
3018
0
        sd_notify(false, "WATCHDOG=1");
3019
0
        e->watchdog_last = e->timestamp.monotonic;
3020
0
3021
0
        return arm_watchdog(e);
3022
0
}
3023
3024
2.81M
static void event_close_inode_data_fds(sd_event *e) {
3025
2.81M
        struct inode_data *d;
3026
2.81M
3027
2.81M
        assert(e);
3028
2.81M
3029
2.81M
        /* Close the fds pointing to the inodes to watch now. We need to close them as they might otherwise pin
3030
2.81M
         * filesystems. But we can't close them right-away as we need them as long as the user still wants to make
3031
2.81M
         * adjustments to the even source, such as changing the priority (which requires us to remove and re-add a watch
3032
2.81M
         * for the inode). Hence, let's close them when entering the first iteration after they were added, as a
3033
2.81M
         * compromise. */
3034
2.81M
3035
2.81M
        while ((d = e->inode_data_to_close)) {
3036
0
                assert(d->fd >= 0);
3037
0
                d->fd = safe_close(d->fd);
3038
0
3039
0
                LIST_REMOVE(to_close, e->inode_data_to_close, d);
3040
0
        }
3041
2.81M
}
3042
3043
2.81M
_public_ int sd_event_prepare(sd_event *e) {
3044
2.81M
        int r;
3045
2.81M
3046
2.81M
        assert_return(e, -EINVAL);
3047
2.81M
        assert_return(e = event_resolve(e), -ENOPKG);
3048
2.81M
        assert_return(!event_pid_changed(e), -ECHILD);
3049
2.81M
        assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
3050
2.81M
        assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
3051
2.81M
3052
2.81M
        if (e->exit_requested)
3053
4.65k
                goto pending;
3054
2.81M
3055
2.81M
        e->iteration++;
3056
2.81M
3057
2.81M
        e->state = SD_EVENT_PREPARING;
3058
2.81M
        r = event_prepare(e);
3059
2.81M
        e->state = SD_EVENT_INITIAL;
3060
2.81M
        if (r < 0)
3061
0
                return r;
3062
2.81M
3063
2.81M
        r = event_arm_timer(e, &e->realtime);
3064
2.81M
        if (r < 0)
3065
0
                return r;
3066
2.81M
3067
2.81M
        r = event_arm_timer(e, &e->boottime);
3068
2.81M
        if (r < 0)
3069
0
                return r;
3070
2.81M
3071
2.81M
        r = event_arm_timer(e, &e->monotonic);
3072
2.81M
        if (r < 0)
3073
0
                return r;
3074
2.81M
3075
2.81M
        r = event_arm_timer(e, &e->realtime_alarm);
3076
2.81M
        if (r < 0)
3077
0
                return r;
3078
2.81M
3079
2.81M
        r = event_arm_timer(e, &e->boottime_alarm);
3080
2.81M
        if (r < 0)
3081
0
                return r;
3082
2.81M
3083
2.81M
        event_close_inode_data_fds(e);
3084
2.81M
3085
2.81M
        if (event_next_pending(e) || e->need_process_child)
3086
2.75M
                goto pending;
3087
51.2k
3088
51.2k
        e->state = SD_EVENT_ARMED;
3089
51.2k
3090
51.2k
        return 0;
3091
2.76M
3092
2.76M
pending:
3093
2.76M
        e->state = SD_EVENT_ARMED;
3094
2.76M
        r = sd_event_wait(e, 0);
3095
2.76M
        if (r == 0)
3096
0
                e->state = SD_EVENT_ARMED;
3097
2.76M
3098
2.76M
        return r;
3099
51.2k
}
3100
3101
2.81M
_public_ int sd_event_wait(sd_event *e, uint64_t timeout) {
3102
2.81M
        struct epoll_event *ev_queue;
3103
2.81M
        unsigned ev_queue_max;
3104
2.81M
        int r, m, i;
3105
2.81M
3106
2.81M
        assert_return(e, -EINVAL);
3107
2.81M
        assert_return(e = event_resolve(e), -ENOPKG);
3108
2.81M
        assert_return(!event_pid_changed(e), -ECHILD);
3109
2.81M
        assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
3110
2.81M
        assert_return(e->state == SD_EVENT_ARMED, -EBUSY);
3111
2.81M
3112
2.81M
        if (e->exit_requested) {
3113
4.65k
                e->state = SD_EVENT_PENDING;
3114
4.65k
                return 1;
3115
4.65k
        }
3116
2.81M
3117
2.81M
        ev_queue_max = MAX(e->n_sources, 1u);
3118
5.62M
        ev_queue = newa(struct epoll_event, ev_queue_max);
3119
5.62M
3120
5.62M
        /* If we still have inotify data buffered, then query the other fds, but don't wait on it */
3121
5.62M
        if (e->inotify_data_buffered)
3122
0
                timeout = 0;
3123
5.62M
3124
5.62M
        m = epoll_wait(e->epoll_fd, ev_queue, ev_queue_max,
3125
5.62M
                       timeout == (uint64_t) -1 ? -1 : (int) DIV_ROUND_UP(timeout, USEC_PER_MSEC));
3126
5.62M
        if (m < 0) {
3127
0
                if (errno == EINTR) {
3128
0
                        e->state = SD_EVENT_PENDING;
3129
0
                        return 1;
3130
0
                }
3131
0
3132
0
                r = -errno;
3133
0
                goto finish;
3134
0
        }
3135
2.81M
3136
2.81M
        triple_timestamp_get(&e->timestamp);
3137
2.81M
3138
7.57M
        for (i = 0; i < m; i++) {
3139
4.76M
3140
4.76M
                if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_WATCHDOG))
3141
4.76M
                        r = flush_timer(e, e->watchdog_fd, ev_queue[i].events, NULL);
3142
4.76M
                else {
3143
4.76M
                        WakeupType *t = ev_queue[i].data.ptr;
3144
4.76M
3145
4.76M
                        switch (*t) {
3146
4.76M
3147
4.76M
                        case WAKEUP_EVENT_SOURCE:
3148
4.76M
                                r = process_io(e, ev_queue[i].data.ptr, ev_queue[i].events);
3149
4.76M
                                break;
3150
4.76M
3151
4.76M
                        case WAKEUP_CLOCK_DATA: {
3152
4.50k
                                struct clock_data *d = ev_queue[i].data.ptr;
3153
4.50k
                                r = flush_timer(e, d->fd, ev_queue[i].events, &d->next);
3154
4.50k
                                break;
3155
4.76M
                        }
3156
4.76M
3157
4.76M
                        case WAKEUP_SIGNAL_DATA:
3158
0
                                r = process_signal(e, ev_queue[i].data.ptr, ev_queue[i].events);
3159
0
                                break;
3160
4.76M
3161
4.76M
                        case WAKEUP_INOTIFY_DATA:
3162
0
                                r = event_inotify_data_read(e, ev_queue[i].data.ptr, ev_queue[i].events);
3163
0
                                break;
3164
4.76M
3165
4.76M
                        default:
3166
0
                                assert_not_reached("Invalid wake-up pointer");
3167
4.76M
                        }
3168
4.76M
                }
3169
4.76M
                if (r < 0)
3170
0
                        goto finish;
3171
4.76M
        }
3172
2.81M
3173
2.81M
        r = process_watchdog(e);
3174
2.81M
        if (r < 0)
3175
0
                goto finish;
3176
2.81M
3177
2.81M
        r = process_timer(e, e->timestamp.realtime, &e->realtime);
3178
2.81M
        if (r < 0)
3179
0
                goto finish;
3180
2.81M
3181
2.81M
        r = process_timer(e, e->timestamp.boottime, &e->boottime);
3182
2.81M
        if (r < 0)
3183
0
                goto finish;
3184
2.81M
3185
2.81M
        r = process_timer(e, e->timestamp.monotonic, &e->monotonic);
3186
2.81M
        if (r < 0)
3187
0
                goto finish;
3188
2.81M
3189
2.81M
        r = process_timer(e, e->timestamp.realtime, &e->realtime_alarm);
3190
2.81M
        if (r < 0)
3191
0
                goto finish;
3192
2.81M
3193
2.81M
        r = process_timer(e, e->timestamp.boottime, &e->boottime_alarm);
3194
2.81M
        if (r < 0)
3195
0
                goto finish;
3196
2.81M
3197
2.81M
        if (e->need_process_child) {
3198
0
                r = process_child(e);
3199
0
                if (r < 0)
3200
0
                        goto finish;
3201
2.81M
        }
3202
2.81M
3203
2.81M
        r = process_inotify(e);
3204
2.81M
        if (r < 0)
3205
0
                goto finish;
3206
2.81M
3207
2.81M
        if (event_next_pending(e)) {
3208
2.81M
                e->state = SD_EVENT_PENDING;
3209
2.81M
3210
2.81M
                return 1;
3211
2.81M
        }
3212
0
3213
0
        r = 0;
3214
0
3215
0
finish:
3216
0
        e->state = SD_EVENT_INITIAL;
3217
0
3218
0
        return r;
3219
0
}
3220
3221
2.81M
_public_ int sd_event_dispatch(sd_event *e) {
3222
2.81M
        sd_event_source *p;
3223
2.81M
        int r;
3224
2.81M
3225
2.81M
        assert_return(e, -EINVAL);
3226
2.81M
        assert_return(e = event_resolve(e), -ENOPKG);
3227
2.81M
        assert_return(!event_pid_changed(e), -ECHILD);
3228
2.81M
        assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
3229
2.81M
        assert_return(e->state == SD_EVENT_PENDING, -EBUSY);
3230
2.81M
3231
2.81M
        if (e->exit_requested)
3232
4.65k
                return dispatch_exit(e);
3233
2.81M
3234
2.81M
        p = event_next_pending(e);
3235
2.81M
        if (p) {
3236
2.81M
                _cleanup_(sd_event_unrefp) sd_event *ref = NULL;
3237
2.81M
3238
2.81M
                ref = sd_event_ref(e);
3239
2.81M
                e->state = SD_EVENT_RUNNING;
3240
2.81M
                r = source_dispatch(p);
3241
2.81M
                e->state = SD_EVENT_INITIAL;
3242
2.81M
                return r;
3243
2.81M
        }
3244
0
3245
0
        e->state = SD_EVENT_INITIAL;
3246
0
3247
0
        return 1;
3248
0
}
3249
3250
0
static void event_log_delays(sd_event *e) {
3251
0
        char b[ELEMENTSOF(e->delays) * DECIMAL_STR_MAX(unsigned) + 1];
3252
0
        unsigned i;
3253
0
        int o;
3254
0
3255
0
        for (i = o = 0; i < ELEMENTSOF(e->delays); i++) {
3256
0
                o += snprintf(&b[o], sizeof(b) - o, "%u ", e->delays[i]);
3257
0
                e->delays[i] = 0;
3258
0
        }
3259
0
        log_debug("Event loop iterations: %.*s", o, b);
3260
0
}
3261
3262
2.81M
_public_ int sd_event_run(sd_event *e, uint64_t timeout) {
3263
2.81M
        int r;
3264
2.81M
3265
2.81M
        assert_return(e, -EINVAL);
3266
2.81M
        assert_return(e = event_resolve(e), -ENOPKG);
3267
2.81M
        assert_return(!event_pid_changed(e), -ECHILD);
3268
2.81M
        assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
3269
2.81M
        assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
3270
2.81M
3271
2.81M
        if (e->profile_delays && e->last_run) {
3272
0
                usec_t this_run;
3273
0
                unsigned l;
3274
0
3275
0
                this_run = now(CLOCK_MONOTONIC);
3276
0
3277
0
                l = u64log2(this_run - e->last_run);
3278
0
                assert(l < sizeof(e->delays));
3279
0
                e->delays[l]++;
3280
0
3281
0
                if (this_run - e->last_log >= 5*USEC_PER_SEC) {
3282
0
                        event_log_delays(e);
3283
0
                        e->last_log = this_run;
3284
0
                }
3285
0
        }
3286
2.81M
3287
2.81M
        r = sd_event_prepare(e);
3288
2.81M
        if (r == 0)
3289
51.2k
                /* There was nothing? Then wait... */
3290
51.2k
                r = sd_event_wait(e, timeout);
3291
2.81M
3292
2.81M
        if (e->profile_delays)
3293
0
                e->last_run = now(CLOCK_MONOTONIC);
3294
2.81M
3295
2.81M
        if (r > 0) {
3296
2.81M
                /* There's something now, then let's dispatch it */
3297
2.81M
                r = sd_event_dispatch(e);
3298
2.81M
                if (r < 0)
3299
0
                        return r;
3300
2.81M
3301
2.81M
                return 1;
3302
2.81M
        }
3303
0
3304
0
        return r;
3305
0
}
3306
3307
3.69k
_public_ int sd_event_loop(sd_event *e) {
3308
3.69k
        _cleanup_(sd_event_unrefp) sd_event *ref = NULL;
3309
3.69k
        int r;
3310
3.69k
3311
3.69k
        assert_return(e, -EINVAL);
3312
3.69k
        assert_return(e = event_resolve(e), -ENOPKG);
3313
3.69k
        assert_return(!event_pid_changed(e), -ECHILD);
3314
3.69k
        assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
3315
3.69k
3316
3.69k
        ref = sd_event_ref(e);
3317
3.69k
3318
2.76M
        while (e->state != SD_EVENT_FINISHED) {
3319
2.76M
                r = sd_event_run(e, (uint64_t) -1);
3320
2.76M
                if (r < 0)
3321
0
                        return r;
3322
2.76M
        }
3323
3.69k
3324
3.69k
        return e->exit_code;
3325
3.69k
}
3326
3327
0
_public_ int sd_event_get_fd(sd_event *e) {
3328
0
3329
0
        assert_return(e, -EINVAL);
3330
0
        assert_return(e = event_resolve(e), -ENOPKG);
3331
0
        assert_return(!event_pid_changed(e), -ECHILD);
3332
0
3333
0
        return e->epoll_fd;
3334
0
}
3335
3336
0
_public_ int sd_event_get_state(sd_event *e) {
3337
0
        assert_return(e, -EINVAL);
3338
0
        assert_return(e = event_resolve(e), -ENOPKG);
3339
0
        assert_return(!event_pid_changed(e), -ECHILD);
3340
0
3341
0
        return e->state;
3342
0
}
3343
3344
0
_public_ int sd_event_get_exit_code(sd_event *e, int *code) {
3345
0
        assert_return(e, -EINVAL);
3346
0
        assert_return(e = event_resolve(e), -ENOPKG);
3347
0
        assert_return(code, -EINVAL);
3348
0
        assert_return(!event_pid_changed(e), -ECHILD);
3349
0
3350
0
        if (!e->exit_requested)
3351
0
                return -ENODATA;
3352
0
3353
0
        *code = e->exit_code;
3354
0
        return 0;
3355
0
}
3356
3357
3.69k
_public_ int sd_event_exit(sd_event *e, int code) {
3358
3.69k
        assert_return(e, -EINVAL);
3359
3.69k
        assert_return(e = event_resolve(e), -ENOPKG);
3360
3.69k
        assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
3361
3.69k
        assert_return(!event_pid_changed(e), -ECHILD);
3362
3.69k
3363
3.69k
        e->exit_requested = true;
3364
3.69k
        e->exit_code = code;
3365
3.69k
3366
3.69k
        return 0;
3367
3.69k
}
3368
3369
5.15k
_public_ int sd_event_now(sd_event *e, clockid_t clock, uint64_t *usec) {
3370
5.15k
        assert_return(e, -EINVAL);
3371
5.15k
        assert_return(e = event_resolve(e), -ENOPKG);
3372
5.14k
        assert_return(usec, -EINVAL);
3373
5.14k
        assert_return(!event_pid_changed(e), -ECHILD);
3374
5.14k
3375
5.14k
        if (!TRIPLE_TIMESTAMP_HAS_CLOCK(clock))
3376
5.14k
                return -EOPNOTSUPP;
3377
5.14k
3378
5.14k
        /* Generate a clean error in case CLOCK_BOOTTIME is not available. Note that don't use clock_supported() here,
3379
5.14k
         * for a reason: there are systems where CLOCK_BOOTTIME is supported, but CLOCK_BOOTTIME_ALARM is not, but for
3380
5.14k
         * the purpose of getting the time this doesn't matter. */
3381
5.14k
        if (IN_SET(clock, CLOCK_BOOTTIME, CLOCK_BOOTTIME_ALARM) && !clock_boottime_supported())
3382
0
                return -EOPNOTSUPP;
3383
5.14k
3384
5.14k
        if (!triple_timestamp_is_set(&e->timestamp)) {
3385
4.50k
                /* Implicitly fall back to now() if we never ran
3386
4.50k
                 * before and thus have no cached time. */
3387
4.50k
                *usec = now(clock);
3388
4.50k
                return 1;
3389
4.50k
        }
3390
641
3391
641
        *usec = triple_timestamp_by_clock(&e->timestamp, clock);
3392
641
        return 0;
3393
641
}
3394
3395
52.6k
_public_ int sd_event_default(sd_event **ret) {
3396
52.6k
        sd_event *e = NULL;
3397
52.6k
        int r;
3398
52.6k
3399
52.6k
        if (!ret)
3400
0
                return !!default_event;
3401
52.6k
3402
52.6k
        if (default_event) {
3403
0
                *ret = sd_event_ref(default_event);
3404
0
                return 0;
3405
0
        }
3406
52.6k
3407
52.6k
        r = sd_event_new(&e);
3408
52.6k
        if (r < 0)
3409
0
                return r;
3410
52.6k
3411
52.6k
        e->default_event_ptr = &default_event;
3412
52.6k
        e->tid = gettid();
3413
52.6k
        default_event = e;
3414
52.6k
3415
52.6k
        *ret = e;
3416
52.6k
        return 1;
3417
52.6k
}
3418
3419
0
_public_ int sd_event_get_tid(sd_event *e, pid_t *tid) {
3420
0
        assert_return(e, -EINVAL);
3421
0
        assert_return(e = event_resolve(e), -ENOPKG);
3422
0
        assert_return(tid, -EINVAL);
3423
0
        assert_return(!event_pid_changed(e), -ECHILD);
3424
0
3425
0
        if (e->tid != 0) {
3426
0
                *tid = e->tid;
3427
0
                return 0;
3428
0
        }
3429
0
3430
0
        return -ENXIO;
3431
0
}
3432
3433
20.0k
_public_ int sd_event_set_watchdog(sd_event *e, int b) {
3434
20.0k
        int r;
3435
20.0k
3436
20.0k
        assert_return(e, -EINVAL);
3437
20.0k
        assert_return(e = event_resolve(e), -ENOPKG);
3438
20.0k
        assert_return(!event_pid_changed(e), -ECHILD);
3439
20.0k
3440
20.0k
        if (e->watchdog == !!b)
3441
0
                return e->watchdog;
3442
20.0k
3443
20.0k
        if (b) {
3444
20.0k
                struct epoll_event ev;
3445
20.0k
3446
20.0k
                r = sd_watchdog_enabled(false, &e->watchdog_period);
3447
20.0k
                if (r <= 0)
3448
20.0k
                        return r;
3449
0
3450
0
                /* Issue first ping immediately */
3451
0
                sd_notify(false, "WATCHDOG=1");
3452
0
                e->watchdog_last = now(CLOCK_MONOTONIC);
3453
0
3454
0
                e->watchdog_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC);
3455
0
                if (e->watchdog_fd < 0)
3456
0
                        return -errno;
3457
0
3458
0
                r = arm_watchdog(e);
3459
0
                if (r < 0)
3460
0
                        goto fail;
3461
0
3462
0
                ev = (struct epoll_event) {
3463
0
                        .events = EPOLLIN,
3464
0
                        .data.ptr = INT_TO_PTR(SOURCE_WATCHDOG),
3465
0
                };
3466
0
3467
0
                r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->watchdog_fd, &ev);
3468
0
                if (r < 0) {
3469
0
                        r = -errno;
3470
0
                        goto fail;
3471
0
                }
3472
0
3473
0
        } else {
3474
0
                if (e->watchdog_fd >= 0) {
3475
0
                        epoll_ctl(e->epoll_fd, EPOLL_CTL_DEL, e->watchdog_fd, NULL);
3476
0
                        e->watchdog_fd = safe_close(e->watchdog_fd);
3477
0
                }
3478
0
        }
3479
20.0k
3480
20.0k
        e->watchdog = !!b;
3481
0
        return e->watchdog;
3482
0
3483
0
fail:
3484
0
        e->watchdog_fd = safe_close(e->watchdog_fd);
3485
0
        return r;
3486
20.0k
}
3487
3488
0
_public_ int sd_event_get_watchdog(sd_event *e) {
3489
0
        assert_return(e, -EINVAL);
3490
0
        assert_return(e = event_resolve(e), -ENOPKG);
3491
0
        assert_return(!event_pid_changed(e), -ECHILD);
3492
0
3493
0
        return e->watchdog;
3494
0
}
3495
3496
0
_public_ int sd_event_get_iteration(sd_event *e, uint64_t *ret) {
3497
0
        assert_return(e, -EINVAL);
3498
0
        assert_return(e = event_resolve(e), -ENOPKG);
3499
0
        assert_return(!event_pid_changed(e), -ECHILD);
3500
0
3501
0
        *ret = e->iteration;
3502
0
        return 0;
3503
0
}
3504
3505
0
_public_ int sd_event_source_set_destroy_callback(sd_event_source *s, sd_event_destroy_t callback) {
3506
0
        assert_return(s, -EINVAL);
3507
0
3508
0
        s->destroy_callback = callback;
3509
0
        return 0;
3510
0
}
3511
3512
0
_public_ int sd_event_source_get_destroy_callback(sd_event_source *s, sd_event_destroy_t *ret) {
3513
0
        assert_return(s, -EINVAL);
3514
0
3515
0
        if (ret)
3516
0
                *ret = s->destroy_callback;
3517
0
3518
0
        return !!s->destroy_callback;
3519
0
}
3520
3521
0
_public_ int sd_event_source_get_floating(sd_event_source *s) {
3522
0
        assert_return(s, -EINVAL);
3523
0
3524
0
        return s->floating;
3525
0
}
3526
3527
0
_public_ int sd_event_source_set_floating(sd_event_source *s, int b) {
3528
0
        assert_return(s, -EINVAL);
3529
0
3530
0
        if (s->floating == !!b)
3531
0
                return 0;
3532
0
3533
0
        if (!s->event) /* Already disconnected */
3534
0
                return -ESTALE;
3535
0
3536
0
        s->floating = b;
3537
0
3538
0
        if (b) {
3539
0
                sd_event_source_ref(s);
3540
0
                sd_event_unref(s->event);
3541
0
        } else {
3542
0
                sd_event_ref(s->event);
3543
0
                sd_event_source_unref(s);
3544
0
        }
3545
0
3546
0
        return 1;
3547
0
}