1 /* SPDX-License-Identifier: LGPL-2.1-or-later */ 2 #ifndef foosdeventhfoo 3 #define foosdeventhfoo 4 5 /*** 6 systemd is free software; you can redistribute it and/or modify it 7 under the terms of the GNU Lesser General Public License as published by 8 the Free Software Foundation; either version 2.1 of the License, or 9 (at your option) any later version. 10 11 systemd is distributed in the hope that it will be useful, but 12 WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 Lesser General Public License for more details. 15 16 You should have received a copy of the GNU Lesser General Public License 17 along with systemd; If not, see <http://www.gnu.org/licenses/>. 18 ***/ 19 20 #include <inttypes.h> 21 #include <signal.h> 22 #include <sys/epoll.h> 23 #include <sys/inotify.h> 24 #include <sys/signalfd.h> 25 #include <sys/types.h> 26 #include <sys/wait.h> 27 #include <time.h> 28 29 #include "_sd-common.h" 30 31 /* 32 Why is this better than pure epoll? 33 34 - Supports event source prioritization 35 - Scales better with a large number of time events because it does not require one timerfd each 36 - Automatically tries to coalesce timer events system-wide 37 - Handles signals, child PIDs, inotify events 38 - Supports systemd-style automatic watchdog event generation 39 */ 40 41 _SD_BEGIN_DECLARATIONS; 42 43 #define SD_EVENT_DEFAULT ((sd_event *) 1) 44 45 typedef struct sd_event sd_event; 46 typedef struct sd_event_source sd_event_source; 47 48 enum { 49 SD_EVENT_OFF = 0, 50 SD_EVENT_ON = 1, 51 SD_EVENT_ONESHOT = -1 52 }; 53 54 enum { 55 SD_EVENT_INITIAL, 56 SD_EVENT_ARMED, 57 SD_EVENT_PENDING, 58 SD_EVENT_RUNNING, 59 SD_EVENT_EXITING, 60 SD_EVENT_FINISHED, 61 SD_EVENT_PREPARING 62 }; 63 64 enum { 65 /* And everything in-between and outside is good too */ 66 SD_EVENT_PRIORITY_IMPORTANT = -100, 67 SD_EVENT_PRIORITY_NORMAL = 0, 68 SD_EVENT_PRIORITY_IDLE = 100 69 }; 70 71 typedef int (*sd_event_handler_t)(sd_event_source *s, void *userdata); 72 typedef int (*sd_event_io_handler_t)(sd_event_source *s, int fd, uint32_t revents, void *userdata); 73 typedef int (*sd_event_time_handler_t)(sd_event_source *s, uint64_t usec, void *userdata); 74 typedef int (*sd_event_signal_handler_t)(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata); 75 #if defined _GNU_SOURCE || (defined _POSIX_C_SOURCE && _POSIX_C_SOURCE >= 199309L) 76 typedef int (*sd_event_child_handler_t)(sd_event_source *s, const siginfo_t *si, void *userdata); 77 #else 78 typedef void* sd_event_child_handler_t; 79 #endif 80 typedef int (*sd_event_inotify_handler_t)(sd_event_source *s, const struct inotify_event *event, void *userdata); 81 typedef _sd_destroy_t sd_event_destroy_t; 82 83 int sd_event_default(sd_event **e); 84 85 int sd_event_new(sd_event **e); 86 sd_event* sd_event_ref(sd_event *e); 87 sd_event* sd_event_unref(sd_event *e); 88 89 int sd_event_add_io(sd_event *e, sd_event_source **s, int fd, uint32_t events, sd_event_io_handler_t callback, void *userdata); 90 int sd_event_add_time(sd_event *e, sd_event_source **s, clockid_t clock, uint64_t usec, uint64_t accuracy, sd_event_time_handler_t callback, void *userdata); 91 int sd_event_add_time_relative(sd_event *e, sd_event_source **s, clockid_t clock, uint64_t usec, uint64_t accuracy, sd_event_time_handler_t callback, void *userdata); 92 int sd_event_add_signal(sd_event *e, sd_event_source **s, int sig, sd_event_signal_handler_t callback, void *userdata); 93 int sd_event_add_child(sd_event *e, sd_event_source **s, pid_t pid, int options, sd_event_child_handler_t callback, void *userdata); 94 int sd_event_add_child_pidfd(sd_event *e, sd_event_source **s, int pidfd, int options, sd_event_child_handler_t callback, void *userdata); 95 int sd_event_add_inotify(sd_event *e, sd_event_source **s, const char *path, uint32_t mask, sd_event_inotify_handler_t callback, void *userdata); 96 int sd_event_add_inotify_fd(sd_event *e, sd_event_source **s, int fd, uint32_t mask, sd_event_inotify_handler_t callback, void *userdata); 97 int sd_event_add_defer(sd_event *e, sd_event_source **s, sd_event_handler_t callback, void *userdata); 98 int sd_event_add_post(sd_event *e, sd_event_source **s, sd_event_handler_t callback, void *userdata); 99 int sd_event_add_exit(sd_event *e, sd_event_source **s, sd_event_handler_t callback, void *userdata); 100 101 int sd_event_prepare(sd_event *e); 102 int sd_event_wait(sd_event *e, uint64_t usec); 103 int sd_event_dispatch(sd_event *e); 104 int sd_event_run(sd_event *e, uint64_t usec); 105 int sd_event_loop(sd_event *e); 106 int sd_event_exit(sd_event *e, int code); 107 108 int sd_event_now(sd_event *e, clockid_t clock, uint64_t *usec); 109 110 int sd_event_get_fd(sd_event *e); 111 int sd_event_get_state(sd_event *e); 112 int sd_event_get_tid(sd_event *e, pid_t *tid); 113 int sd_event_get_exit_code(sd_event *e, int *code); 114 int sd_event_set_watchdog(sd_event *e, int b); 115 int sd_event_get_watchdog(sd_event *e); 116 int sd_event_get_iteration(sd_event *e, uint64_t *ret); 117 118 sd_event_source* sd_event_source_ref(sd_event_source *s); 119 sd_event_source* sd_event_source_unref(sd_event_source *s); 120 sd_event_source* sd_event_source_disable_unref(sd_event_source *s); 121 122 sd_event *sd_event_source_get_event(sd_event_source *s); 123 void* sd_event_source_get_userdata(sd_event_source *s); 124 void* sd_event_source_set_userdata(sd_event_source *s, void *userdata); 125 126 int sd_event_source_set_description(sd_event_source *s, const char *description); 127 int sd_event_source_get_description(sd_event_source *s, const char **description); 128 int sd_event_source_set_prepare(sd_event_source *s, sd_event_handler_t callback); 129 int sd_event_source_get_pending(sd_event_source *s); 130 int sd_event_source_get_priority(sd_event_source *s, int64_t *priority); 131 int sd_event_source_set_priority(sd_event_source *s, int64_t priority); 132 int sd_event_source_get_enabled(sd_event_source *s, int *enabled); 133 int sd_event_source_set_enabled(sd_event_source *s, int enabled); 134 int sd_event_source_get_io_fd(sd_event_source *s); 135 int sd_event_source_set_io_fd(sd_event_source *s, int fd); 136 int sd_event_source_get_io_fd_own(sd_event_source *s); 137 int sd_event_source_set_io_fd_own(sd_event_source *s, int own); 138 int sd_event_source_get_io_events(sd_event_source *s, uint32_t* events); 139 int sd_event_source_set_io_events(sd_event_source *s, uint32_t events); 140 int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents); 141 int sd_event_source_get_time(sd_event_source *s, uint64_t *usec); 142 int sd_event_source_set_time(sd_event_source *s, uint64_t usec); 143 int sd_event_source_set_time_relative(sd_event_source *s, uint64_t usec); 144 int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec); 145 int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec); 146 int sd_event_source_get_time_clock(sd_event_source *s, clockid_t *clock); 147 int sd_event_source_get_signal(sd_event_source *s); 148 int sd_event_source_get_child_pid(sd_event_source *s, pid_t *pid); 149 int sd_event_source_get_child_pidfd(sd_event_source *s); 150 int sd_event_source_get_child_pidfd_own(sd_event_source *s); 151 int sd_event_source_set_child_pidfd_own(sd_event_source *s, int own); 152 int sd_event_source_get_child_process_own(sd_event_source *s); 153 int sd_event_source_set_child_process_own(sd_event_source *s, int own); 154 #if defined _GNU_SOURCE || (defined _POSIX_C_SOURCE && _POSIX_C_SOURCE >= 199309L) 155 int sd_event_source_send_child_signal(sd_event_source *s, int sig, const siginfo_t *si, unsigned flags); 156 #else 157 int sd_event_source_send_child_signal(sd_event_source *s, int sig, const void *si, unsigned flags); 158 #endif 159 int sd_event_source_get_inotify_mask(sd_event_source *s, uint32_t *ret); 160 int sd_event_source_set_destroy_callback(sd_event_source *s, sd_event_destroy_t callback); 161 int sd_event_source_get_destroy_callback(sd_event_source *s, sd_event_destroy_t *ret); 162 int sd_event_source_get_floating(sd_event_source *s); 163 int sd_event_source_set_floating(sd_event_source *s, int b); 164 int sd_event_source_get_exit_on_failure(sd_event_source *s); 165 int sd_event_source_set_exit_on_failure(sd_event_source *s, int b); 166 int sd_event_source_set_ratelimit(sd_event_source *s, uint64_t interval_usec, unsigned burst); 167 int sd_event_source_get_ratelimit(sd_event_source *s, uint64_t *ret_interval_usec, unsigned *ret_burst); 168 int sd_event_source_is_ratelimited(sd_event_source *s); 169 int sd_event_source_set_ratelimit_expire_callback(sd_event_source *s, sd_event_handler_t callback); 170 171 /* Define helpers so that __attribute__((cleanup(sd_event_unrefp))) and similar may be used. */ 172 _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_event, sd_event_unref); 173 _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_event_source, sd_event_source_unref); 174 _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_event_source, sd_event_source_disable_unref); 175 176 _SD_END_DECLARATIONS; 177 178 #endif 179