| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include <signal.h> |
| 5 |
#include <sys/signalfd.h> |
| 6 |
|
| 7 |
#include "sd-event.h" |
| 8 |
|
| 9 |
#include "sd-forward.h" |
| 10 |
#include "inotify-util.h" |
| 11 |
#include "list.h" |
| 12 |
#include "ratelimit.h" |
| 13 |
|
| 14 |
typedef enum EventSourceType { |
| 15 |
SOURCE_IO, |
| 16 |
SOURCE_TIME_REALTIME, |
| 17 |
SOURCE_TIME_BOOTTIME, |
| 18 |
SOURCE_TIME_MONOTONIC, |
| 19 |
SOURCE_TIME_REALTIME_ALARM, |
| 20 |
SOURCE_TIME_BOOTTIME_ALARM, |
| 21 |
SOURCE_SIGNAL, |
| 22 |
SOURCE_CHILD, |
| 23 |
SOURCE_DEFER, |
| 24 |
SOURCE_POST, |
| 25 |
SOURCE_EXIT, |
| 26 |
SOURCE_WATCHDOG, |
| 27 |
SOURCE_INOTIFY, |
| 28 |
SOURCE_MEMORY_PRESSURE, |
| 29 |
SOURCE_CPU_PRESSURE, |
| 30 |
SOURCE_IO_PRESSURE, |
| 31 |
_SOURCE_EVENT_SOURCE_TYPE_MAX, |
| 32 |
_SOURCE_EVENT_SOURCE_TYPE_INVALID = -EINVAL, |
| 33 |
} EventSourceType; |
| 34 |
|
| 35 |
/* All objects we use in epoll events start with this value, so that |
| 36 |
* we know how to dispatch it */ |
| 37 |
typedef enum WakeupType { |
| 38 |
WAKEUP_NONE, |
| 39 |
WAKEUP_EVENT_SOURCE, /* either I/O or pidfd wakeup */ |
| 40 |
WAKEUP_CLOCK_DATA, |
| 41 |
WAKEUP_SIGNAL_DATA, |
| 42 |
WAKEUP_INOTIFY_DATA, |
| 43 |
_WAKEUP_TYPE_MAX, |
| 44 |
_WAKEUP_TYPE_INVALID = -EINVAL, |
| 45 |
} WakeupType; |
| 46 |
|
| 47 |
typedef struct inode_data InodeData; |
| 48 |
typedef struct inotify_data InotifyData; |
| 49 |
|
| 50 |
struct sd_event_source { |
| 51 |
WakeupType wakeup; |
| 52 |
|
| 53 |
unsigned n_ref; |
| 54 |
|
| 55 |
sd_event *event; |
| 56 |
void *userdata; |
| 57 |
sd_event_handler_t prepare; |
| 58 |
|
| 59 |
char *description; |
| 60 |
|
| 61 |
EventSourceType type; |
| 62 |
signed int enabled:3; |
| 63 |
bool pending:1; |
| 64 |
bool dispatching:1; |
| 65 |
bool floating:1; |
| 66 |
bool exit_on_failure:1; |
| 67 |
bool ratelimited:1; |
| 68 |
|
| 69 |
int64_t priority; |
| 70 |
unsigned pending_index; |
| 71 |
unsigned prepare_index; |
| 72 |
uint64_t pending_iteration; |
| 73 |
uint64_t prepare_iteration; |
| 74 |
|
| 75 |
sd_event_destroy_t destroy_callback; |
| 76 |
sd_event_handler_t ratelimit_expire_callback; |
| 77 |
|
| 78 |
LIST_FIELDS(sd_event_source, sources); |
| 79 |
|
| 80 |
RateLimit rate_limit; |
| 81 |
|
| 82 |
/* These are primarily fields relevant for time event sources, but since any event source can |
| 83 |
* effectively become one when rate-limited, this is part of the common fields. */ |
| 84 |
unsigned earliest_index; |
| 85 |
unsigned latest_index; |
| 86 |
|
| 87 |
union { |
| 88 |
struct { |
| 89 |
sd_event_io_handler_t callback; |
| 90 |
int fd; |
| 91 |
uint32_t events; |
| 92 |
uint32_t revents; |
| 93 |
bool registered; |
| 94 |
bool owned; |
| 95 |
} io; |
| 96 |
struct { |
| 97 |
sd_event_time_handler_t callback; |
| 98 |
usec_t next, accuracy; |
| 99 |
} time; |
| 100 |
struct { |
| 101 |
sd_event_signal_handler_t callback; |
| 102 |
struct signalfd_siginfo siginfo; |
| 103 |
int sig; |
| 104 |
bool unblock; |
| 105 |
} signal; |
| 106 |
struct { |
| 107 |
sd_event_child_handler_t callback; |
| 108 |
siginfo_t siginfo; |
| 109 |
pid_t pid; |
| 110 |
int options; |
| 111 |
int pidfd; |
| 112 |
/* We have five bools, and we want to fit them into 4 bytes so the whole struct |
| 113 |
* remains 32 bytes. Thus, use bitfields for two of them and single bytes for the |
| 114 |
* other three. */ |
| 115 |
bool registered; /* whether the pidfd is registered in the epoll */ |
| 116 |
bool pidfd_owned:1; /* close pidfd when event source is freed */ |
| 117 |
bool process_owned:1; /* kill+reap process when event source is freed */ |
| 118 |
bool exited; /* true if process exited (i.e. if there's value in SIGKILLing it if |
| 119 |
* we want to get rid of it) */ |
| 120 |
bool waited; /* true if process was waited for (i.e. if there's value in |
| 121 |
* waitid(P_PID)'ing it if we want to get rid of it) */ |
| 122 |
} child; |
| 123 |
struct { |
| 124 |
sd_event_handler_t callback; |
| 125 |
} defer; |
| 126 |
struct { |
| 127 |
sd_event_handler_t callback; |
| 128 |
} post; |
| 129 |
struct { |
| 130 |
sd_event_handler_t callback; |
| 131 |
unsigned prioq_index; |
| 132 |
} exit; |
| 133 |
struct { |
| 134 |
sd_event_inotify_handler_t callback; |
| 135 |
uint32_t mask; |
| 136 |
InodeData *inode_data; |
| 137 |
LIST_FIELDS(sd_event_source, by_inode_data); |
| 138 |
} inotify; |
| 139 |
struct { |
| 140 |
int fd; |
| 141 |
bool registered; |
| 142 |
bool locked; |
| 143 |
bool in_write_list; |
| 144 |
sd_event_handler_t callback; |
| 145 |
void *write_buffer; |
| 146 |
size_t write_buffer_size; |
| 147 |
uint32_t events, revents; |
| 148 |
LIST_FIELDS(sd_event_source, write_list); |
| 149 |
} pressure; |
| 150 |
}; |
| 151 |
}; |
| 152 |
|
| 153 |
struct clock_data { |
| 154 |
WakeupType wakeup; |
| 155 |
int fd; |
| 156 |
|
| 157 |
/* For all clocks we maintain two priority queues each, one |
| 158 |
* ordered for the earliest times the events may be |
| 159 |
* dispatched, and one ordered by the latest times they must |
| 160 |
* have been dispatched. The range between the top entries in |
| 161 |
* the two prioqs is the time window we can freely schedule |
| 162 |
* wakeups in */ |
| 163 |
|
| 164 |
Prioq *earliest; |
| 165 |
Prioq *latest; |
| 166 |
usec_t next; |
| 167 |
|
| 168 |
bool needs_rearm; |
| 169 |
}; |
| 170 |
|
| 171 |
struct signal_data { |
| 172 |
WakeupType wakeup; |
| 173 |
|
| 174 |
/* For each priority we maintain one signal fd, so that we |
| 175 |
* only have to dequeue a single event per priority at a |
| 176 |
* time. */ |
| 177 |
|
| 178 |
int fd; |
| 179 |
int64_t priority; |
| 180 |
sigset_t sigset; |
| 181 |
sd_event_source *current; |
| 182 |
}; |
| 183 |
|
| 184 |
/* A structure listing all event sources currently watching a specific inode */ |
| 185 |
struct inode_data { |
| 186 |
/* The identifier for the inode, the combination of the .st_dev + .st_ino fields of the file */ |
| 187 |
ino_t ino; |
| 188 |
dev_t dev; |
| 189 |
|
| 190 |
/* An fd of the inode to watch. The fd is kept open until the next iteration of the loop, so that we can |
| 191 |
* rearrange the priority still until then, as we need the original inode to change the priority as we need to |
| 192 |
* add a watch descriptor to the right inotify for the priority which we can only do if we have a handle to the |
| 193 |
* original inode. We keep a list of all InodeData objects with an open fd in the to_close list (see below) of |
| 194 |
* the sd-event object, so that it is efficient to close everything, before entering the next event loop |
| 195 |
* iteration. */ |
| 196 |
int fd; |
| 197 |
|
| 198 |
/* The path that the fd points to. The field is optional. */ |
| 199 |
char *path; |
| 200 |
|
| 201 |
/* The inotify "watch descriptor" */ |
| 202 |
int wd; |
| 203 |
|
| 204 |
/* The combination of the mask of all inotify watches on this inode we manage. This is also the mask that has |
| 205 |
* most recently been set on the watch descriptor. */ |
| 206 |
uint32_t combined_mask; |
| 207 |
|
| 208 |
/* All event sources subscribed to this inode */ |
| 209 |
LIST_HEAD(sd_event_source, event_sources); |
| 210 |
|
| 211 |
/* The inotify object we watch this inode with */ |
| 212 |
InotifyData *inotify_data; |
| 213 |
|
| 214 |
/* A linked list of all inode data objects with fds to close (see above) */ |
| 215 |
LIST_FIELDS(InodeData, to_close); |
| 216 |
}; |
| 217 |
|
| 218 |
/* A structure encapsulating an inotify fd */ |
| 219 |
struct inotify_data { |
| 220 |
WakeupType wakeup; |
| 221 |
|
| 222 |
/* For each priority we maintain one inotify fd, so that we only have to dequeue a single event per priority at |
| 223 |
* a time */ |
| 224 |
|
| 225 |
int fd; |
| 226 |
int64_t priority; |
| 227 |
|
| 228 |
Hashmap *inodes; /* The InodeData structures keyed by dev+ino */ |
| 229 |
Hashmap *wd; /* The InodeData structures keyed by the watch descriptor for each */ |
| 230 |
|
| 231 |
/* How many event sources are currently marked pending for this inotify. We won't read new events off the |
| 232 |
* inotify fd as long as there are still pending events on the inotify (because we have no strategy of queuing |
| 233 |
* the events locally if they can't be coalesced). */ |
| 234 |
unsigned n_pending; |
| 235 |
|
| 236 |
/* If this counter is non-zero, don't GC the inotify data object even if not used to watch any inode |
| 237 |
* anymore. This is useful to pin the object for a bit longer, after the last event source needing it |
| 238 |
* is gone. */ |
| 239 |
unsigned n_busy; |
| 240 |
|
| 241 |
/* A linked list of all InotifyData objects with data already read, that still need processing. We |
| 242 |
* keep this list to make it efficient to figure out what inotify objects to process data on next. */ |
| 243 |
LIST_FIELDS(InotifyData, buffered); |
| 244 |
|
| 245 |
/* The buffer we read inotify events into */ |
| 246 |
size_t buffer_filled; /* fill level of the buffer */ |
| 247 |
union inotify_event_buffer buffer; /* We use a union to allow type-punning. One of the variants in |
| 248 |
* the union — struct inotify_event — has a flex array, so C99 |
| 249 |
* only allows this union to be used at the end of the structure. |
| 250 |
* The other variant in the union defines a fixed-size buffer that |
| 251 |
* covers the maximum size that can be used for the flex variant, |
| 252 |
* so in fact this field has a fixed size and could be safely |
| 253 |
* placed in the middle of the struct. Unfortunately, there is no |
| 254 |
* mechanism to let the compiler know that. */ |
| 255 |
}; |