Lines Matching refs:e

162 static void event_gc_inode_data(sd_event *e, struct inode_data *d);
164 static sd_event *event_resolve(sd_event *e) { in event_resolve() argument
165 return e == SD_EVENT_DEFAULT ? default_event : e; in event_resolve()
320 static sd_event *event_free(sd_event *e) { in event_free() argument
323 assert(e); in event_free()
325 while ((s = e->sources)) { in event_free()
331 assert(e->n_sources == 0); in event_free()
333 if (e->default_event_ptr) in event_free()
334 *(e->default_event_ptr) = NULL; in event_free()
336 safe_close(e->epoll_fd); in event_free()
337 safe_close(e->watchdog_fd); in event_free()
339 free_clock_data(&e->realtime); in event_free()
340 free_clock_data(&e->boottime); in event_free()
341 free_clock_data(&e->monotonic); in event_free()
342 free_clock_data(&e->realtime_alarm); in event_free()
343 free_clock_data(&e->boottime_alarm); in event_free()
345 prioq_free(e->pending); in event_free()
346 prioq_free(e->prepare); in event_free()
347 prioq_free(e->exit); in event_free()
349 free(e->signal_sources); in event_free()
350 hashmap_free(e->signal_data); in event_free()
352 hashmap_free(e->inotify_data); in event_free()
354 hashmap_free(e->child_sources); in event_free()
355 set_free(e->post_sources); in event_free()
357 free(e->event_queue); in event_free()
359 return mfree(e); in event_free()
363 sd_event *e; in sd_event_new() local
368 e = new(sd_event, 1); in sd_event_new()
369 if (!e) in sd_event_new()
372 *e = (sd_event) { in sd_event_new()
395 r = prioq_ensure_allocated(&e->pending, pending_prioq_compare); in sd_event_new()
399 e->epoll_fd = epoll_create1(EPOLL_CLOEXEC); in sd_event_new()
400 if (e->epoll_fd < 0) { in sd_event_new()
405 e->epoll_fd = fd_move_above_stdio(e->epoll_fd); in sd_event_new()
409 e->profile_delays = true; in sd_event_new()
412 *ret = e; in sd_event_new()
416 event_free(e); in sd_event_new()
428 static bool event_pid_changed(sd_event *e) { in event_pid_changed() argument
429 assert(e); in event_pid_changed()
434 return e->original_pid != getpid_cached(); in event_pid_changed()
565 static struct clock_data* event_get_clock_data(sd_event *e, EventSourceType t) { in event_get_clock_data() argument
566 assert(e); in event_get_clock_data()
571 return &e->realtime; in event_get_clock_data()
574 return &e->boottime; in event_get_clock_data()
577 return &e->monotonic; in event_get_clock_data()
580 return &e->realtime_alarm; in event_get_clock_data()
583 return &e->boottime_alarm; in event_get_clock_data()
590 static void event_free_signal_data(sd_event *e, struct signal_data *d) { in event_free_signal_data() argument
591 assert(e); in event_free_signal_data()
596 hashmap_remove(e->signal_data, &d->priority); in event_free_signal_data()
602 sd_event *e, in event_make_signal_data() argument
612 assert(e); in event_make_signal_data()
614 if (event_pid_changed(e)) in event_make_signal_data()
617 if (e->signal_sources && e->signal_sources[sig]) in event_make_signal_data()
618 priority = e->signal_sources[sig]->priority; in event_make_signal_data()
622 d = hashmap_get(e->signal_data, &priority); in event_make_signal_data()
640 r = hashmap_ensure_put(&e->signal_data, &uint64_hash_ops, &d->priority, d); in event_make_signal_data()
673 if (epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, d->fd, &ev) < 0) { in event_make_signal_data()
685 event_free_signal_data(e, d); in event_make_signal_data()
690 static void event_unmask_signal_data(sd_event *e, struct signal_data *d, int sig) { in event_unmask_signal_data() argument
691 assert(e); in event_unmask_signal_data()
705 event_free_signal_data(e, d); in event_unmask_signal_data()
709 if (event_pid_changed(e)) in event_unmask_signal_data()
718 static void event_gc_signal_data(sd_event *e, const int64_t *priority, int sig) { in event_gc_signal_data() argument
722 assert(e); in event_gc_signal_data()
728 e->n_online_child_sources > 0) in event_gc_signal_data()
731 if (e->signal_sources && in event_gc_signal_data()
732 e->signal_sources[sig] && in event_gc_signal_data()
733 event_source_is_online(e->signal_sources[sig])) in event_gc_signal_data()
747 d = hashmap_get(e->signal_data, priority); in event_gc_signal_data()
749 event_unmask_signal_data(e, d, sig); in event_gc_signal_data()
752 if (e->signal_sources && e->signal_sources[sig]) { in event_gc_signal_data()
753 d = hashmap_get(e->signal_data, &e->signal_sources[sig]->priority); in event_gc_signal_data()
755 event_unmask_signal_data(e, d, sig); in event_gc_signal_data()
758 d = hashmap_get(e->signal_data, &zero_priority); in event_gc_signal_data()
760 event_unmask_signal_data(e, d, sig); in event_gc_signal_data()
1050 static sd_event_source *source_new(sd_event *e, bool floating, EventSourceType type) { in source_new() argument
1053 assert(e); in source_new()
1061 .event = e, in source_new()
1069 sd_event_ref(e); in source_new()
1071 LIST_PREPEND(sources, e->sources, s); in source_new()
1072 e->n_sources++; in source_new()
1084 sd_event *e, in sd_event_add_io() argument
1094 assert_return(e, -EINVAL); in sd_event_add_io()
1095 assert_return(e = event_resolve(e), -ENOPKG); in sd_event_add_io()
1098 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); in sd_event_add_io()
1099 assert_return(!event_pid_changed(e), -ECHILD); in sd_event_add_io()
1104 s = source_new(e, !ret, SOURCE_IO); in sd_event_add_io()
1126 static void initialize_perturb(sd_event *e) { in initialize_perturb() argument
1138 if (_likely_(e->perturb != USEC_INFINITY)) in initialize_perturb()
1142 e->perturb = (bootid.qwords[0] ^ bootid.qwords[1]) % USEC_PER_MINUTE; in initialize_perturb()
1146 sd_event *e, in event_setup_timer_fd() argument
1150 assert(e); in event_setup_timer_fd()
1169 if (epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) in event_setup_timer_fd()
1182 static int setup_clock_data(sd_event *e, struct clock_data *d, clockid_t clock) { in setup_clock_data() argument
1188 r = event_setup_timer_fd(e, d, clock); in setup_clock_data()
1230 sd_event *e, in sd_event_add_time() argument
1243 assert_return(e, -EINVAL); in sd_event_add_time()
1244 assert_return(e = event_resolve(e), -ENOPKG); in sd_event_add_time()
1246 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); in sd_event_add_time()
1247 assert_return(!event_pid_changed(e), -ECHILD); in sd_event_add_time()
1259 assert_se(d = event_get_clock_data(e, type)); in sd_event_add_time()
1261 r = setup_clock_data(e, d, clock); in sd_event_add_time()
1265 s = source_new(e, !ret, type); in sd_event_add_time()
1288 sd_event *e, in sd_event_add_time_relative() argument
1302 r = sd_event_now(e, clock, &t); in sd_event_add_time_relative()
1309 return sd_event_add_time(e, ret, clock, t + usec, accuracy, callback, userdata); in sd_event_add_time_relative()
1319 sd_event *e, in sd_event_add_signal() argument
1329 assert_return(e, -EINVAL); in sd_event_add_signal()
1330 assert_return(e = event_resolve(e), -ENOPKG); in sd_event_add_signal()
1332 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); in sd_event_add_signal()
1333 assert_return(!event_pid_changed(e), -ECHILD); in sd_event_add_signal()
1344 if (!e->signal_sources) { in sd_event_add_signal()
1345 e->signal_sources = new0(sd_event_source*, _NSIG); in sd_event_add_signal()
1346 if (!e->signal_sources) in sd_event_add_signal()
1348 } else if (e->signal_sources[sig]) in sd_event_add_signal()
1351 s = source_new(e, !ret, SOURCE_SIGNAL); in sd_event_add_signal()
1360 e->signal_sources[sig] = s; in sd_event_add_signal()
1362 r = event_make_signal_data(e, sig, &d); in sd_event_add_signal()
1388 sd_event *e, in sd_event_add_child() argument
1398 assert_return(e, -EINVAL); in sd_event_add_child()
1399 assert_return(e = event_resolve(e), -ENOPKG); in sd_event_add_child()
1403 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); in sd_event_add_child()
1404 assert_return(!event_pid_changed(e), -ECHILD); in sd_event_add_child()
1409 if (e->n_online_child_sources == 0) { in sd_event_add_child()
1423 r = hashmap_ensure_allocated(&e->child_sources, NULL); in sd_event_add_child()
1427 if (hashmap_contains(e->child_sources, PID_TO_PTR(pid))) in sd_event_add_child()
1430 s = source_new(e, !ret, SOURCE_CHILD); in sd_event_add_child()
1462 r = event_make_signal_data(e, SIGCHLD, NULL); in sd_event_add_child()
1466 e->need_process_child = true; in sd_event_add_child()
1469 r = hashmap_put(e->child_sources, PID_TO_PTR(pid), s); in sd_event_add_child()
1475 e->n_online_child_sources++; in sd_event_add_child()
1484 sd_event *e, in sd_event_add_child_pidfd() argument
1496 assert_return(e, -EINVAL); in sd_event_add_child_pidfd()
1497 assert_return(e = event_resolve(e), -ENOPKG); in sd_event_add_child_pidfd()
1501 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); in sd_event_add_child_pidfd()
1502 assert_return(!event_pid_changed(e), -ECHILD); in sd_event_add_child_pidfd()
1507 if (e->n_online_child_sources == 0) { in sd_event_add_child_pidfd()
1515 r = hashmap_ensure_allocated(&e->child_sources, NULL); in sd_event_add_child_pidfd()
1523 if (hashmap_contains(e->child_sources, PID_TO_PTR(pid))) in sd_event_add_child_pidfd()
1526 s = source_new(e, !ret, SOURCE_CHILD); in sd_event_add_child_pidfd()
1539 r = hashmap_put(e->child_sources, PID_TO_PTR(pid), s); in sd_event_add_child_pidfd()
1550 r = event_make_signal_data(e, SIGCHLD, NULL); in sd_event_add_child_pidfd()
1554 e->need_process_child = true; in sd_event_add_child_pidfd()
1557 e->n_online_child_sources++; in sd_event_add_child_pidfd()
1572 sd_event *e, in sd_event_add_defer() argument
1580 assert_return(e, -EINVAL); in sd_event_add_defer()
1581 assert_return(e = event_resolve(e), -ENOPKG); in sd_event_add_defer()
1582 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); in sd_event_add_defer()
1583 assert_return(!event_pid_changed(e), -ECHILD); in sd_event_add_defer()
1588 s = source_new(e, !ret, SOURCE_DEFER); in sd_event_add_defer()
1608 sd_event *e, in sd_event_add_post() argument
1616 assert_return(e, -EINVAL); in sd_event_add_post()
1617 assert_return(e = event_resolve(e), -ENOPKG); in sd_event_add_post()
1618 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); in sd_event_add_post()
1619 assert_return(!event_pid_changed(e), -ECHILD); in sd_event_add_post()
1624 s = source_new(e, !ret, SOURCE_POST); in sd_event_add_post()
1632 r = set_ensure_put(&e->post_sources, NULL, s); in sd_event_add_post()
1645 sd_event *e, in sd_event_add_exit() argument
1653 assert_return(e, -EINVAL); in sd_event_add_exit()
1654 assert_return(e = event_resolve(e), -ENOPKG); in sd_event_add_exit()
1656 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); in sd_event_add_exit()
1657 assert_return(!event_pid_changed(e), -ECHILD); in sd_event_add_exit()
1659 r = prioq_ensure_allocated(&e->exit, exit_prioq_compare); in sd_event_add_exit()
1663 s = source_new(e, !ret, SOURCE_EXIT); in sd_event_add_exit()
1683 static void event_free_inotify_data(sd_event *e, struct inotify_data *d) { in event_free_inotify_data() argument
1684 assert(e); in event_free_inotify_data()
1693 LIST_REMOVE(buffered, e->inotify_data_buffered, d); in event_free_inotify_data()
1698 assert_se(hashmap_remove(e->inotify_data, &d->priority) == d); in event_free_inotify_data()
1701 if (!event_pid_changed(e) && in event_free_inotify_data()
1702 epoll_ctl(e->epoll_fd, EPOLL_CTL_DEL, d->fd, NULL) < 0) in event_free_inotify_data()
1711 sd_event *e, in event_make_inotify_data() argument
1719 assert(e); in event_make_inotify_data()
1721 d = hashmap_get(e->inotify_data, &priority); in event_make_inotify_data()
1744 r = hashmap_ensure_put(&e->inotify_data, &uint64_hash_ops, &d->priority, d); in event_make_inotify_data()
1756 if (epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, d->fd, &ev) < 0) { in event_make_inotify_data()
1761 event_free_inotify_data(e, d); in event_make_inotify_data()
1794 sd_event *e, in event_free_inode_data() argument
1797 assert(e); in event_free_inode_data()
1805 LIST_REMOVE(to_close, e->inode_data_to_close, d); in event_free_inode_data()
1812 if (d->inotify_data->fd >= 0 && !event_pid_changed(e)) { in event_free_inode_data()
1832 sd_event *e, in event_gc_inotify_data() argument
1835 assert(e); in event_gc_inotify_data()
1851 event_free_inotify_data(e, d); in event_gc_inotify_data()
1855 sd_event *e, in event_gc_inode_data() argument
1860 assert(e); in event_gc_inode_data()
1869 event_free_inode_data(e, d); in event_gc_inode_data()
1871 event_gc_inotify_data(e, inotify_data); in event_gc_inode_data()
1875 sd_event *e, in event_make_inode_data() argument
1884 assert(e); in event_make_inode_data()
1953 static int inode_data_realize_watch(sd_event *e, struct inode_data *d) { in inode_data_realize_watch() argument
2000 sd_event *e, in event_add_inotify_fd_internal() argument
2015 assert_return(e, -EINVAL); in event_add_inotify_fd_internal()
2016 assert_return(e = event_resolve(e), -ENOPKG); in event_add_inotify_fd_internal()
2018 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); in event_add_inotify_fd_internal()
2019 assert_return(!event_pid_changed(e), -ECHILD); in event_add_inotify_fd_internal()
2033 s = source_new(e, !ret, SOURCE_INOTIFY); in event_add_inotify_fd_internal()
2043 r = event_make_inotify_data(e, SD_EVENT_PRIORITY_NORMAL, &inotify_data); in event_add_inotify_fd_internal()
2047 r = event_make_inode_data(e, inotify_data, st.st_dev, st.st_ino, &inode_data); in event_add_inotify_fd_internal()
2049 event_gc_inotify_data(e, inotify_data); in event_add_inotify_fd_internal()
2062 event_gc_inode_data(e, inode_data); in event_add_inotify_fd_internal()
2067 LIST_PREPEND(to_close, e->inode_data_to_close, inode_data); in event_add_inotify_fd_internal()
2075 r = inode_data_realize_watch(e, inode_data); in event_add_inotify_fd_internal()
2087 sd_event *e, in sd_event_add_inotify_fd() argument
2094 … return event_add_inotify_fd_internal(e, ret, fd, /* donate= */ false, mask, callback, userdata); in sd_event_add_inotify_fd()
2098 sd_event *e, in sd_event_add_inotify() argument
2116 r = event_add_inotify_fd_internal(e, &s, fd, /* donate= */ true, mask, callback, userdata); in sd_event_add_inotify()
2984 static usec_t sleep_between(sd_event *e, usec_t a, usec_t b) { in sleep_between() argument
2986 assert(e); in sleep_between()
2997 initialize_perturb(e); in sleep_between()
3017 c = (b / USEC_PER_MINUTE) * USEC_PER_MINUTE + e->perturb; in sleep_between()
3028 c = (b / (USEC_PER_SEC*10)) * (USEC_PER_SEC*10) + (e->perturb % (USEC_PER_SEC*10)); in sleep_between()
3039 c = (b / USEC_PER_SEC) * USEC_PER_SEC + (e->perturb % USEC_PER_SEC); in sleep_between()
3050 c = (b / (USEC_PER_MSEC*250)) * (USEC_PER_MSEC*250) + (e->perturb % (USEC_PER_MSEC*250)); in sleep_between()
3065 sd_event *e, in event_arm_timer() argument
3072 assert(e); in event_arm_timer()
3102 t = sleep_between(e, time_event_source_next(a), time_event_source_latest(b)); in event_arm_timer()
3122 static int process_io(sd_event *e, sd_event_source *s, uint32_t revents) { in process_io() argument
3123 assert(e); in process_io()
3141 static int flush_timer(sd_event *e, int fd, uint32_t events, usec_t *next) { in flush_timer() argument
3145 assert(e); in flush_timer()
3168 sd_event *e, in process_timer() argument
3176 assert(e); in process_timer()
3213 static int process_child(sd_event *e, int64_t threshold, int64_t *ret_min_priority) { in process_child() argument
3219 assert(e); in process_child()
3222 if (!e->need_process_child) { in process_child()
3227 e->need_process_child = false; in process_child()
3240 HASHMAP_FOREACH(s, e->child_sources) { in process_child()
3294 static int process_pidfd(sd_event *e, sd_event_source *s, uint32_t revents) { in process_pidfd() argument
3295 assert(e); in process_pidfd()
3321 static int process_signal(sd_event *e, struct signal_data *d, uint32_t events, int64_t *min_priorit… in process_signal() argument
3324 assert(e); in process_signal()
3336 e->need_process_child = true; in process_signal()
3360 if (e->signal_sources) in process_signal()
3361 s = e->signal_sources[si.ssi_signo]; in process_signal()
3382 static int event_inotify_data_read(sd_event *e, struct inotify_data *d, uint32_t revents, int64_t t… in event_inotify_data_read() argument
3385 assert(e); in event_inotify_data_read()
3411 LIST_PREPEND(buffered, e->inotify_data_buffered, d); in event_inotify_data_read()
3416 static void event_inotify_data_drop(sd_event *e, struct inotify_data *d, size_t sz) { in event_inotify_data_drop() argument
3417 assert(e); in event_inotify_data_drop()
3429 LIST_REMOVE(buffered, e->inotify_data_buffered, d); in event_inotify_data_drop()
3432 static int event_inotify_data_process(sd_event *e, struct inotify_data *d) { in event_inotify_data_process() argument
3435 assert(e); in event_inotify_data_process()
3478 event_inotify_data_drop(e, d, sz); in event_inotify_data_process()
3487 event_inotify_data_drop(e, d, sz); in event_inotify_data_process()
3517 static int process_inotify(sd_event *e) { in process_inotify() argument
3520 assert(e); in process_inotify()
3522 LIST_FOREACH(buffered, d, e->inotify_data_buffered) { in process_inotify()
3523 r = event_inotify_data_process(e, d); in process_inotify()
3635 struct sd_event *e = s->event; in source_dispatch() local
3659 event_inotify_data_drop(e, d, sz); in source_dispatch()
3662 event_gc_inotify_data(e, d); in source_dispatch()
3692 static int event_prepare(sd_event *e) { in event_prepare() argument
3695 assert(e); in event_prepare()
3700 s = prioq_peek(e->prepare); in event_prepare()
3701 if (!s || s->prepare_iteration == e->iteration || event_source_is_offline(s)) in event_prepare()
3704 s->prepare_iteration = e->iteration; in event_prepare()
3705 r = prioq_reshuffle(e->prepare, s, &s->prepare_index); in event_prepare()
3722 (void) sd_event_exit(e, r); in event_prepare()
3734 static int dispatch_exit(sd_event *e) { in dispatch_exit() argument
3738 assert(e); in dispatch_exit()
3740 p = prioq_peek(e->exit); in dispatch_exit()
3744 e->state = SD_EVENT_FINISHED; in dispatch_exit()
3748 _unused_ _cleanup_(sd_event_unrefp) sd_event *ref = sd_event_ref(e); in dispatch_exit()
3749 e->iteration++; in dispatch_exit()
3750 e->state = SD_EVENT_EXITING; in dispatch_exit()
3752 e->state = SD_EVENT_INITIAL; in dispatch_exit()
3756 static sd_event_source* event_next_pending(sd_event *e) { in event_next_pending() argument
3759 assert(e); in event_next_pending()
3761 p = prioq_peek(e->pending); in event_next_pending()
3771 static int arm_watchdog(sd_event *e) { in arm_watchdog() argument
3775 assert(e); in arm_watchdog()
3776 assert(e->watchdog_fd >= 0); in arm_watchdog()
3778 t = sleep_between(e, in arm_watchdog()
3779 usec_add(e->watchdog_last, (e->watchdog_period / 2)), in arm_watchdog()
3780 usec_add(e->watchdog_last, (e->watchdog_period * 3 / 4))); in arm_watchdog()
3789 return RET_NERRNO(timerfd_settime(e->watchdog_fd, TFD_TIMER_ABSTIME, &its, NULL)); in arm_watchdog()
3792 static int process_watchdog(sd_event *e) { in process_watchdog() argument
3793 assert(e); in process_watchdog()
3795 if (!e->watchdog) in process_watchdog()
3799 if (e->watchdog_last + e->watchdog_period / 4 > e->timestamp.monotonic) in process_watchdog()
3803 e->watchdog_last = e->timestamp.monotonic; in process_watchdog()
3805 return arm_watchdog(e); in process_watchdog()
3808 static void event_close_inode_data_fds(sd_event *e) { in event_close_inode_data_fds() argument
3811 assert(e); in event_close_inode_data_fds()
3819 while ((d = e->inode_data_to_close)) { in event_close_inode_data_fds()
3823 LIST_REMOVE(to_close, e->inode_data_to_close, d); in event_close_inode_data_fds()
3827 _public_ int sd_event_prepare(sd_event *e) { in sd_event_prepare() argument
3830 assert_return(e, -EINVAL); in sd_event_prepare()
3831 assert_return(e = event_resolve(e), -ENOPKG); in sd_event_prepare()
3832 assert_return(!event_pid_changed(e), -ECHILD); in sd_event_prepare()
3833 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); in sd_event_prepare()
3834 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY); in sd_event_prepare()
3839 assert_return(!e->default_event_ptr || e->tid == gettid(), -EREMOTEIO); in sd_event_prepare()
3842 _unused_ _cleanup_(sd_event_unrefp) sd_event *ref = sd_event_ref(e); in sd_event_prepare()
3844 if (e->exit_requested) in sd_event_prepare()
3847 e->iteration++; in sd_event_prepare()
3849 e->state = SD_EVENT_PREPARING; in sd_event_prepare()
3850 r = event_prepare(e); in sd_event_prepare()
3851 e->state = SD_EVENT_INITIAL; in sd_event_prepare()
3855 r = event_arm_timer(e, &e->realtime); in sd_event_prepare()
3859 r = event_arm_timer(e, &e->boottime); in sd_event_prepare()
3863 r = event_arm_timer(e, &e->monotonic); in sd_event_prepare()
3867 r = event_arm_timer(e, &e->realtime_alarm); in sd_event_prepare()
3871 r = event_arm_timer(e, &e->boottime_alarm); in sd_event_prepare()
3875 event_close_inode_data_fds(e); in sd_event_prepare()
3877 if (event_next_pending(e) || e->need_process_child) in sd_event_prepare()
3880 e->state = SD_EVENT_ARMED; in sd_event_prepare()
3885 e->state = SD_EVENT_ARMED; in sd_event_prepare()
3886 r = sd_event_wait(e, 0); in sd_event_prepare()
3888 e->state = SD_EVENT_ARMED; in sd_event_prepare()
3941 static int process_epoll(sd_event *e, usec_t timeout, int64_t threshold, int64_t *ret_min_priority)… in process_epoll() argument
3947 assert(e); in process_epoll()
3950 n_event_queue = MAX(e->n_sources, 1u); in process_epoll()
3951 if (!GREEDY_REALLOC(e->event_queue, n_event_queue)) in process_epoll()
3954 n_event_max = MALLOC_ELEMENTSOF(e->event_queue); in process_epoll()
3957 if (e->inotify_data_buffered) in process_epoll()
3962 e->epoll_fd, in process_epoll()
3963 e->event_queue, in process_epoll()
3977 if (!GREEDY_REALLOC(e->event_queue, n_event_max + n_event_queue)) in process_epoll()
3980 n_event_max = MALLOC_ELEMENTSOF(e->event_queue); in process_epoll()
3986 triple_timestamp_get(&e->timestamp); in process_epoll()
3990 if (e->event_queue[i].data.ptr == INT_TO_PTR(SOURCE_WATCHDOG)) in process_epoll()
3991 r = flush_timer(e, e->watchdog_fd, e->event_queue[i].events, NULL); in process_epoll()
3993 WakeupType *t = e->event_queue[i].data.ptr; in process_epoll()
3998 sd_event_source *s = e->event_queue[i].data.ptr; in process_epoll()
4010 r = process_io(e, s, e->event_queue[i].events); in process_epoll()
4014 r = process_pidfd(e, s, e->event_queue[i].events); in process_epoll()
4025 struct clock_data *d = e->event_queue[i].data.ptr; in process_epoll()
4029 r = flush_timer(e, d->fd, e->event_queue[i].events, &d->next); in process_epoll()
4034 … r = process_signal(e, e->event_queue[i].data.ptr, e->event_queue[i].events, &min_priority); in process_epoll()
4038 … r = event_inotify_data_read(e, e->event_queue[i].data.ptr, e->event_queue[i].events, threshold); in process_epoll()
4055 _public_ int sd_event_wait(sd_event *e, uint64_t timeout) { in sd_event_wait() argument
4058 assert_return(e, -EINVAL); in sd_event_wait()
4059 assert_return(e = event_resolve(e), -ENOPKG); in sd_event_wait()
4060 assert_return(!event_pid_changed(e), -ECHILD); in sd_event_wait()
4061 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); in sd_event_wait()
4062 assert_return(e->state == SD_EVENT_ARMED, -EBUSY); in sd_event_wait()
4064 if (e->exit_requested) { in sd_event_wait()
4065 e->state = SD_EVENT_PENDING; in sd_event_wait()
4080 r = process_epoll(e, timeout, threshold, &epoll_min_priority); in sd_event_wait()
4082 e->state = SD_EVENT_PENDING; in sd_event_wait()
4091 r = process_child(e, threshold, &child_min_priority); in sd_event_wait()
4105 r = process_watchdog(e); in sd_event_wait()
4109 r = process_inotify(e); in sd_event_wait()
4113 r = process_timer(e, e->timestamp.realtime, &e->realtime); in sd_event_wait()
4117 r = process_timer(e, e->timestamp.boottime, &e->boottime); in sd_event_wait()
4121 r = process_timer(e, e->timestamp.realtime, &e->realtime_alarm); in sd_event_wait()
4125 r = process_timer(e, e->timestamp.boottime, &e->boottime_alarm); in sd_event_wait()
4129 r = process_timer(e, e->timestamp.monotonic, &e->monotonic); in sd_event_wait()
4144 if (event_next_pending(e)) { in sd_event_wait()
4145 e->state = SD_EVENT_PENDING; in sd_event_wait()
4152 e->state = SD_EVENT_INITIAL; in sd_event_wait()
4157 _public_ int sd_event_dispatch(sd_event *e) { in sd_event_dispatch() argument
4161 assert_return(e, -EINVAL); in sd_event_dispatch()
4162 assert_return(e = event_resolve(e), -ENOPKG); in sd_event_dispatch()
4163 assert_return(!event_pid_changed(e), -ECHILD); in sd_event_dispatch()
4164 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); in sd_event_dispatch()
4165 assert_return(e->state == SD_EVENT_PENDING, -EBUSY); in sd_event_dispatch()
4167 if (e->exit_requested) in sd_event_dispatch()
4168 return dispatch_exit(e); in sd_event_dispatch()
4170 p = event_next_pending(e); in sd_event_dispatch()
4172 _unused_ _cleanup_(sd_event_unrefp) sd_event *ref = sd_event_ref(e); in sd_event_dispatch()
4174 e->state = SD_EVENT_RUNNING; in sd_event_dispatch()
4176 e->state = SD_EVENT_INITIAL; in sd_event_dispatch()
4180 e->state = SD_EVENT_INITIAL; in sd_event_dispatch()
4185 static void event_log_delays(sd_event *e) { in event_log_delays() argument
4186 char b[ELEMENTSOF(e->delays) * DECIMAL_STR_MAX(unsigned) + 1], *p; in event_log_delays()
4191 for (i = 0; i < ELEMENTSOF(e->delays); i++) { in event_log_delays()
4192 l = strpcpyf(&p, l, "%u ", e->delays[i]); in event_log_delays()
4193 e->delays[i] = 0; in event_log_delays()
4198 _public_ int sd_event_run(sd_event *e, uint64_t timeout) { in sd_event_run() argument
4201 assert_return(e, -EINVAL); in sd_event_run()
4202 assert_return(e = event_resolve(e), -ENOPKG); in sd_event_run()
4203 assert_return(!event_pid_changed(e), -ECHILD); in sd_event_run()
4204 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); in sd_event_run()
4205 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY); in sd_event_run()
4207 if (e->profile_delays && e->last_run_usec != 0) { in sd_event_run()
4213 l = log2u64(this_run - e->last_run_usec); in sd_event_run()
4214 assert(l < ELEMENTSOF(e->delays)); in sd_event_run()
4215 e->delays[l]++; in sd_event_run()
4217 if (this_run - e->last_log_usec >= 5*USEC_PER_SEC) { in sd_event_run()
4218 event_log_delays(e); in sd_event_run()
4219 e->last_log_usec = this_run; in sd_event_run()
4224 _unused_ _cleanup_(sd_event_unrefp) sd_event *ref = sd_event_ref(e); in sd_event_run()
4226 r = sd_event_prepare(e); in sd_event_run()
4229 r = sd_event_wait(e, timeout); in sd_event_run()
4231 if (e->profile_delays) in sd_event_run()
4232 e->last_run_usec = now(CLOCK_MONOTONIC); in sd_event_run()
4236 r = sd_event_dispatch(e); in sd_event_run()
4246 _public_ int sd_event_loop(sd_event *e) { in sd_event_loop() argument
4249 assert_return(e, -EINVAL); in sd_event_loop()
4250 assert_return(e = event_resolve(e), -ENOPKG); in sd_event_loop()
4251 assert_return(!event_pid_changed(e), -ECHILD); in sd_event_loop()
4252 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY); in sd_event_loop()
4254 _unused_ _cleanup_(sd_event_unrefp) sd_event *ref = sd_event_ref(e); in sd_event_loop()
4256 while (e->state != SD_EVENT_FINISHED) { in sd_event_loop()
4257 r = sd_event_run(e, UINT64_MAX); in sd_event_loop()
4262 return e->exit_code; in sd_event_loop()
4265 _public_ int sd_event_get_fd(sd_event *e) { in sd_event_get_fd() argument
4266 assert_return(e, -EINVAL); in sd_event_get_fd()
4267 assert_return(e = event_resolve(e), -ENOPKG); in sd_event_get_fd()
4268 assert_return(!event_pid_changed(e), -ECHILD); in sd_event_get_fd()
4270 return e->epoll_fd; in sd_event_get_fd()
4273 _public_ int sd_event_get_state(sd_event *e) { in sd_event_get_state() argument
4274 assert_return(e, -EINVAL); in sd_event_get_state()
4275 assert_return(e = event_resolve(e), -ENOPKG); in sd_event_get_state()
4276 assert_return(!event_pid_changed(e), -ECHILD); in sd_event_get_state()
4278 return e->state; in sd_event_get_state()
4281 _public_ int sd_event_get_exit_code(sd_event *e, int *code) { in sd_event_get_exit_code() argument
4282 assert_return(e, -EINVAL); in sd_event_get_exit_code()
4283 assert_return(e = event_resolve(e), -ENOPKG); in sd_event_get_exit_code()
4285 assert_return(!event_pid_changed(e), -ECHILD); in sd_event_get_exit_code()
4287 if (!e->exit_requested) in sd_event_get_exit_code()
4290 *code = e->exit_code; in sd_event_get_exit_code()
4294 _public_ int sd_event_exit(sd_event *e, int code) { in sd_event_exit() argument
4295 assert_return(e, -EINVAL); in sd_event_exit()
4296 assert_return(e = event_resolve(e), -ENOPKG); in sd_event_exit()
4297 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); in sd_event_exit()
4298 assert_return(!event_pid_changed(e), -ECHILD); in sd_event_exit()
4300 e->exit_requested = true; in sd_event_exit()
4301 e->exit_code = code; in sd_event_exit()
4306 _public_ int sd_event_now(sd_event *e, clockid_t clock, uint64_t *usec) { in sd_event_now() argument
4307 assert_return(e, -EINVAL); in sd_event_now()
4308 assert_return(e = event_resolve(e), -ENOPKG); in sd_event_now()
4310 assert_return(!event_pid_changed(e), -ECHILD); in sd_event_now()
4315 if (!triple_timestamp_is_set(&e->timestamp)) { in sd_event_now()
4321 *usec = triple_timestamp_by_clock(&e->timestamp, clock); in sd_event_now()
4326 sd_event *e = NULL; in sd_event_default() local
4337 r = sd_event_new(&e); in sd_event_default()
4341 e->default_event_ptr = &default_event; in sd_event_default()
4342 e->tid = gettid(); in sd_event_default()
4343 default_event = e; in sd_event_default()
4345 *ret = e; in sd_event_default()
4349 _public_ int sd_event_get_tid(sd_event *e, pid_t *tid) { in sd_event_get_tid() argument
4350 assert_return(e, -EINVAL); in sd_event_get_tid()
4351 assert_return(e = event_resolve(e), -ENOPKG); in sd_event_get_tid()
4353 assert_return(!event_pid_changed(e), -ECHILD); in sd_event_get_tid()
4355 if (e->tid != 0) { in sd_event_get_tid()
4356 *tid = e->tid; in sd_event_get_tid()
4363 _public_ int sd_event_set_watchdog(sd_event *e, int b) { in sd_event_set_watchdog() argument
4366 assert_return(e, -EINVAL); in sd_event_set_watchdog()
4367 assert_return(e = event_resolve(e), -ENOPKG); in sd_event_set_watchdog()
4368 assert_return(!event_pid_changed(e), -ECHILD); in sd_event_set_watchdog()
4370 if (e->watchdog == !!b) in sd_event_set_watchdog()
4371 return e->watchdog; in sd_event_set_watchdog()
4374 r = sd_watchdog_enabled(false, &e->watchdog_period); in sd_event_set_watchdog()
4380 e->watchdog_last = now(CLOCK_MONOTONIC); in sd_event_set_watchdog()
4382 e->watchdog_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC); in sd_event_set_watchdog()
4383 if (e->watchdog_fd < 0) in sd_event_set_watchdog()
4386 r = arm_watchdog(e); in sd_event_set_watchdog()
4395 if (epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->watchdog_fd, &ev) < 0) { in sd_event_set_watchdog()
4401 if (e->watchdog_fd >= 0) { in sd_event_set_watchdog()
4402 (void) epoll_ctl(e->epoll_fd, EPOLL_CTL_DEL, e->watchdog_fd, NULL); in sd_event_set_watchdog()
4403 e->watchdog_fd = safe_close(e->watchdog_fd); in sd_event_set_watchdog()
4407 e->watchdog = !!b; in sd_event_set_watchdog()
4408 return e->watchdog; in sd_event_set_watchdog()
4411 e->watchdog_fd = safe_close(e->watchdog_fd); in sd_event_set_watchdog()
4415 _public_ int sd_event_get_watchdog(sd_event *e) { in sd_event_get_watchdog() argument
4416 assert_return(e, -EINVAL); in sd_event_get_watchdog()
4417 assert_return(e = event_resolve(e), -ENOPKG); in sd_event_get_watchdog()
4418 assert_return(!event_pid_changed(e), -ECHILD); in sd_event_get_watchdog()
4420 return e->watchdog; in sd_event_get_watchdog()
4423 _public_ int sd_event_get_iteration(sd_event *e, uint64_t *ret) { in sd_event_get_iteration() argument
4424 assert_return(e, -EINVAL); in sd_event_get_iteration()
4425 assert_return(e = event_resolve(e), -ENOPKG); in sd_event_get_iteration()
4426 assert_return(!event_pid_changed(e), -ECHILD); in sd_event_get_iteration()
4428 *ret = e->iteration; in sd_event_get_iteration()