]>
Commit | Line | Data |
---|---|---|
e7145211 | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
7fafc032 | 2 | /* |
810adae9 LP |
3 | * Copyright © 2004 Chris Friesen <chris_friesen@sympatico.ca> |
4 | * Copyright © 2009 Canonical Ltd. | |
5 | * Copyright © 2009 Scott James Remnant <scott@netsplit.com> | |
7fafc032 KS |
6 | */ |
7 | ||
7fafc032 | 8 | #include <errno.h> |
618234a5 LP |
9 | #include <fcntl.h> |
10 | #include <getopt.h> | |
11 | #include <signal.h> | |
12 | #include <stdbool.h> | |
13 | #include <stddef.h> | |
7fafc032 KS |
14 | #include <stdio.h> |
15 | #include <stdlib.h> | |
16 | #include <string.h> | |
618234a5 | 17 | #include <sys/epoll.h> |
3ebdb81e | 18 | #include <sys/file.h> |
618234a5 LP |
19 | #include <sys/inotify.h> |
20 | #include <sys/ioctl.h> | |
21 | #include <sys/mount.h> | |
1e03b754 | 22 | #include <sys/prctl.h> |
1e03b754 | 23 | #include <sys/signalfd.h> |
618234a5 | 24 | #include <sys/socket.h> |
dc117daa | 25 | #include <sys/stat.h> |
618234a5 LP |
26 | #include <sys/time.h> |
27 | #include <sys/wait.h> | |
28 | #include <unistd.h> | |
7fafc032 | 29 | |
392ef7a2 | 30 | #include "sd-daemon.h" |
693d371d | 31 | #include "sd-event.h" |
8314de1d | 32 | |
b5efdb8a | 33 | #include "alloc-util.h" |
681bd2c5 | 34 | #include "build.h" |
194bbe33 | 35 | #include "cgroup-util.h" |
618234a5 | 36 | #include "cpu-set-util.h" |
5ba2dc25 | 37 | #include "dev-setup.h" |
7f2e3a14 | 38 | #include "device-monitor-private.h" |
abde5ea8 | 39 | #include "device-private.h" |
70068602 | 40 | #include "device-util.h" |
6d63048a | 41 | #include "event-util.h" |
3ffd4af2 | 42 | #include "fd-util.h" |
a5c32cff | 43 | #include "fileio.h" |
f97b34a6 | 44 | #include "format-util.h" |
f4f15635 | 45 | #include "fs-util.h" |
a505965d | 46 | #include "hashmap.h" |
c004493c | 47 | #include "io-util.h" |
70068602 | 48 | #include "libudev-device-internal.h" |
40a57716 | 49 | #include "list.h" |
0c5a109a | 50 | #include "main-func.h" |
5ea78a39 | 51 | #include "mkdir.h" |
618234a5 | 52 | #include "netlink-util.h" |
6bedfcbb | 53 | #include "parse-util.h" |
294bf0c3 | 54 | #include "pretty-print.h" |
4e731273 | 55 | #include "proc-cmdline.h" |
618234a5 LP |
56 | #include "process-util.h" |
57 | #include "selinux-util.h" | |
58 | #include "signal-util.h" | |
8f328d36 | 59 | #include "socket-util.h" |
07630cea | 60 | #include "string-util.h" |
49fe5c09 | 61 | #include "strv.h" |
5ea78a39 | 62 | #include "strxcpyx.h" |
46f0fbd8 | 63 | #include "syslog-util.h" |
07a26e42 | 64 | #include "udev-builtin.h" |
7d68eb1b | 65 | #include "udev-ctrl.h" |
618234a5 | 66 | #include "udev-util.h" |
70068602 | 67 | #include "udev-watch.h" |
618234a5 | 68 | #include "udev.h" |
ee104e11 | 69 | #include "user-util.h" |
7fafc032 | 70 | |
bba7a484 TG |
71 | static bool arg_debug = false; |
72 | static int arg_daemonize = false; | |
c4d44cba | 73 | static ResolveNameTiming arg_resolve_name_timing = RESOLVE_NAME_EARLY; |
216e8bbe | 74 | static unsigned arg_children_max = 0; |
6b92f429 | 75 | static usec_t arg_exec_delay_usec = 0; |
bba7a484 | 76 | static usec_t arg_event_timeout_usec = 180 * USEC_PER_SEC; |
c0c6806b TG |
77 | |
78 | typedef struct Manager { | |
693d371d | 79 | sd_event *event; |
c0c6806b | 80 | Hashmap *workers; |
40a57716 | 81 | LIST_HEAD(struct event, events); |
c26d1879 | 82 | const char *cgroup; |
cb49a4f2 | 83 | pid_t pid; /* the process that originally allocated the manager object */ |
c0c6806b | 84 | |
9a07157d | 85 | UdevRules *rules; |
9b5150b6 | 86 | Hashmap *properties; |
c0c6806b | 87 | |
0bed242c YW |
88 | sd_netlink *rtnl; |
89 | ||
7f2e3a14 | 90 | sd_device_monitor *monitor; |
c0c6806b TG |
91 | struct udev_ctrl *ctrl; |
92 | struct udev_ctrl_connection *ctrl_conn_blocking; | |
e237d8cb | 93 | int fd_inotify; |
e237d8cb TG |
94 | int worker_watch[2]; |
95 | ||
693d371d TG |
96 | sd_event_source *ctrl_event; |
97 | sd_event_source *uevent_event; | |
98 | sd_event_source *inotify_event; | |
eca195ec | 99 | sd_event_source *kill_workers_event; |
693d371d | 100 | |
7c4c7e89 TG |
101 | usec_t last_usec; |
102 | ||
c0c6806b | 103 | bool stop_exec_queue:1; |
c0c6806b TG |
104 | bool exit:1; |
105 | } Manager; | |
1e03b754 | 106 | |
1e03b754 | 107 | enum event_state { |
912541b0 KS |
108 | EVENT_UNDEF, |
109 | EVENT_QUEUED, | |
110 | EVENT_RUNNING, | |
1e03b754 KS |
111 | }; |
112 | ||
113 | struct event { | |
cb49a4f2 | 114 | Manager *manager; |
c6aa11f2 | 115 | struct worker *worker; |
912541b0 | 116 | enum event_state state; |
eb546b35 YW |
117 | |
118 | sd_device *dev; | |
119 | sd_device *dev_kernel; /* clone of originally received device */ | |
120 | ||
121 | uint64_t seqnum; | |
122 | uint64_t delaying_seqnum; | |
d8f462b4 YW |
123 | |
124 | sd_event_source *timeout_warning_event; | |
125 | sd_event_source *timeout_event; | |
eb546b35 YW |
126 | |
127 | LIST_FIELDS(struct event, event); | |
1e03b754 KS |
128 | }; |
129 | ||
ecb17862 | 130 | static void event_queue_cleanup(Manager *manager, enum event_state type); |
ff2c503d | 131 | |
1e03b754 | 132 | enum worker_state { |
912541b0 KS |
133 | WORKER_UNDEF, |
134 | WORKER_RUNNING, | |
135 | WORKER_IDLE, | |
136 | WORKER_KILLED, | |
1e03b754 KS |
137 | }; |
138 | ||
139 | struct worker { | |
c0c6806b | 140 | Manager *manager; |
912541b0 | 141 | pid_t pid; |
7f2e3a14 | 142 | sd_device_monitor *monitor; |
912541b0 KS |
143 | enum worker_state state; |
144 | struct event *event; | |
1e03b754 KS |
145 | }; |
146 | ||
147 | /* passed from worker to main process */ | |
148 | struct worker_message { | |
1e03b754 KS |
149 | }; |
150 | ||
c6aa11f2 TG |
151 | static void event_free(struct event *event) { |
152 | if (!event) | |
153 | return; | |
ba47b71c | 154 | |
40a57716 | 155 | assert(event->manager); |
c6aa11f2 | 156 | |
40a57716 | 157 | LIST_REMOVE(event, event->manager->events, event); |
eb546b35 YW |
158 | sd_device_unref(event->dev); |
159 | sd_device_unref(event->dev_kernel); | |
c6aa11f2 | 160 | |
d8f462b4 YW |
161 | sd_event_source_unref(event->timeout_warning_event); |
162 | sd_event_source_unref(event->timeout_event); | |
693d371d | 163 | |
c6aa11f2 TG |
164 | if (event->worker) |
165 | event->worker->event = NULL; | |
166 | ||
ba47b71c YW |
167 | /* only clean up the queue from the process that created it */ |
168 | if (LIST_IS_EMPTY(event->manager->events) && | |
169 | event->manager->pid == getpid_cached()) | |
170 | if (unlink("/run/udev/queue") < 0) | |
171 | log_warning_errno(errno, "Failed to unlink /run/udev/queue: %m"); | |
cb49a4f2 | 172 | |
912541b0 | 173 | free(event); |
aa8734ff | 174 | } |
7a770250 | 175 | |
c6aa11f2 TG |
176 | static void worker_free(struct worker *worker) { |
177 | if (!worker) | |
178 | return; | |
bc113de9 | 179 | |
c0c6806b TG |
180 | assert(worker->manager); |
181 | ||
4a0b58c4 | 182 | hashmap_remove(worker->manager->workers, PID_TO_PTR(worker->pid)); |
7f2e3a14 | 183 | sd_device_monitor_unref(worker->monitor); |
c6aa11f2 TG |
184 | event_free(worker->event); |
185 | ||
c6aa11f2 | 186 | free(worker); |
ff2c503d KS |
187 | } |
188 | ||
c0c6806b | 189 | static void manager_workers_free(Manager *manager) { |
a505965d TG |
190 | struct worker *worker; |
191 | Iterator i; | |
ff2c503d | 192 | |
c0c6806b TG |
193 | assert(manager); |
194 | ||
195 | HASHMAP_FOREACH(worker, manager->workers, i) | |
c6aa11f2 | 196 | worker_free(worker); |
a505965d | 197 | |
c0c6806b | 198 | manager->workers = hashmap_free(manager->workers); |
fc465079 KS |
199 | } |
200 | ||
7f2e3a14 | 201 | static int worker_new(struct worker **ret, Manager *manager, sd_device_monitor *worker_monitor, pid_t pid) { |
a505965d TG |
202 | _cleanup_free_ struct worker *worker = NULL; |
203 | int r; | |
3a19b32a TG |
204 | |
205 | assert(ret); | |
c0c6806b | 206 | assert(manager); |
3a19b32a TG |
207 | assert(worker_monitor); |
208 | assert(pid > 1); | |
209 | ||
210 | worker = new0(struct worker, 1); | |
211 | if (!worker) | |
212 | return -ENOMEM; | |
213 | ||
c0c6806b | 214 | worker->manager = manager; |
3a19b32a | 215 | /* close monitor, but keep address around */ |
7f2e3a14 YW |
216 | device_monitor_disconnect(worker_monitor); |
217 | worker->monitor = sd_device_monitor_ref(worker_monitor); | |
3a19b32a | 218 | worker->pid = pid; |
a505965d | 219 | |
c0c6806b | 220 | r = hashmap_ensure_allocated(&manager->workers, NULL); |
a505965d TG |
221 | if (r < 0) |
222 | return r; | |
223 | ||
4a0b58c4 | 224 | r = hashmap_put(manager->workers, PID_TO_PTR(pid), worker); |
a505965d TG |
225 | if (r < 0) |
226 | return r; | |
227 | ||
ae2a15bc | 228 | *ret = TAKE_PTR(worker); |
3a19b32a TG |
229 | |
230 | return 0; | |
231 | } | |
232 | ||
4fa4d885 TG |
233 | static int on_event_timeout(sd_event_source *s, uint64_t usec, void *userdata) { |
234 | struct event *event = userdata; | |
235 | ||
236 | assert(event); | |
237 | assert(event->worker); | |
238 | ||
239 | kill_and_sigcont(event->worker->pid, SIGKILL); | |
240 | event->worker->state = WORKER_KILLED; | |
241 | ||
eb546b35 | 242 | log_device_error(event->dev, "Worker ["PID_FMT"] processing SEQNUM=%"PRIu64" killed", event->worker->pid, event->seqnum); |
4fa4d885 TG |
243 | |
244 | return 1; | |
245 | } | |
246 | ||
247 | static int on_event_timeout_warning(sd_event_source *s, uint64_t usec, void *userdata) { | |
248 | struct event *event = userdata; | |
249 | ||
250 | assert(event); | |
044497e2 | 251 | assert(event->worker); |
4fa4d885 | 252 | |
eb546b35 | 253 | log_device_warning(event->dev, "Worker ["PID_FMT"] processing SEQNUM=%"PRIu64" is taking a long time", event->worker->pid, event->seqnum); |
4fa4d885 TG |
254 | |
255 | return 1; | |
256 | } | |
257 | ||
39c19cf1 | 258 | static void worker_attach_event(struct worker *worker, struct event *event) { |
693d371d TG |
259 | sd_event *e; |
260 | uint64_t usec; | |
693d371d | 261 | |
c6aa11f2 | 262 | assert(worker); |
693d371d | 263 | assert(worker->manager); |
c6aa11f2 TG |
264 | assert(event); |
265 | assert(!event->worker); | |
266 | assert(!worker->event); | |
267 | ||
39c19cf1 | 268 | worker->state = WORKER_RUNNING; |
39c19cf1 TG |
269 | worker->event = event; |
270 | event->state = EVENT_RUNNING; | |
c6aa11f2 | 271 | event->worker = worker; |
693d371d TG |
272 | |
273 | e = worker->manager->event; | |
274 | ||
3285baa8 | 275 | assert_se(sd_event_now(e, CLOCK_MONOTONIC, &usec) >= 0); |
693d371d | 276 | |
d8f462b4 | 277 | (void) sd_event_add_time(e, &event->timeout_warning_event, CLOCK_MONOTONIC, |
66f737b4 | 278 | usec + udev_warn_timeout(arg_event_timeout_usec), USEC_PER_SEC, on_event_timeout_warning, event); |
693d371d | 279 | |
d8f462b4 | 280 | (void) sd_event_add_time(e, &event->timeout_event, CLOCK_MONOTONIC, |
693d371d | 281 | usec + arg_event_timeout_usec, USEC_PER_SEC, on_event_timeout, event); |
39c19cf1 TG |
282 | } |
283 | ||
0bed242c YW |
284 | static void manager_clear_for_worker(Manager *manager) { |
285 | assert(manager); | |
286 | ||
287 | manager->ctrl_event = sd_event_source_unref(manager->ctrl_event); | |
288 | manager->uevent_event = sd_event_source_unref(manager->uevent_event); | |
289 | manager->inotify_event = sd_event_source_unref(manager->inotify_event); | |
290 | manager->kill_workers_event = sd_event_source_unref(manager->kill_workers_event); | |
291 | ||
292 | manager->event = sd_event_unref(manager->event); | |
293 | ||
294 | manager_workers_free(manager); | |
295 | event_queue_cleanup(manager, EVENT_UNDEF); | |
296 | ||
7f2e3a14 | 297 | manager->monitor = sd_device_monitor_unref(manager->monitor); |
0bed242c YW |
298 | manager->ctrl_conn_blocking = udev_ctrl_connection_unref(manager->ctrl_conn_blocking); |
299 | manager->ctrl = udev_ctrl_unref(manager->ctrl); | |
300 | ||
301 | manager->worker_watch[READ_END] = safe_close(manager->worker_watch[READ_END]); | |
302 | } | |
303 | ||
e237d8cb TG |
304 | static void manager_free(Manager *manager) { |
305 | if (!manager) | |
306 | return; | |
307 | ||
2024ed61 | 308 | udev_builtin_exit(); |
b2d21d93 | 309 | |
0bed242c | 310 | manager_clear_for_worker(manager); |
693d371d | 311 | |
0bed242c | 312 | sd_netlink_unref(manager->rtnl); |
e237d8cb | 313 | |
9b5150b6 | 314 | hashmap_free_free_free(manager->properties); |
981fae90 | 315 | udev_rules_free(manager->rules); |
e237d8cb | 316 | |
e237d8cb TG |
317 | safe_close(manager->fd_inotify); |
318 | safe_close_pair(manager->worker_watch); | |
319 | ||
320 | free(manager); | |
321 | } | |
322 | ||
323 | DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free); | |
324 | ||
9a73bd7c TG |
325 | static int worker_send_message(int fd) { |
326 | struct worker_message message = {}; | |
327 | ||
328 | return loop_write(fd, &message, sizeof(message), false); | |
329 | } | |
330 | ||
b97897e3 | 331 | static int worker_lock_block_device(sd_device *dev, int *ret_fd) { |
0bed242c | 332 | _cleanup_close_ int fd = -1; |
b97897e3 YW |
333 | const char *val; |
334 | int r; | |
fee854ee | 335 | |
0bed242c YW |
336 | assert(dev); |
337 | assert(ret_fd); | |
338 | ||
339 | /* | |
340 | * Take a shared lock on the device node; this establishes | |
341 | * a concept of device "ownership" to serialize device | |
342 | * access. External processes holding an exclusive lock will | |
343 | * cause udev to skip the event handling; in the case udev | |
344 | * acquired the lock, the external process can block until | |
345 | * udev has finished its event handling. | |
346 | */ | |
347 | ||
b97897e3 YW |
348 | r = sd_device_get_property_value(dev, "ACTION", &val); |
349 | if (r < 0) | |
350 | return log_device_debug_errno(dev, r, "Failed to get the value of property 'ACTION': %m"); | |
351 | ||
352 | if (streq(val, "remove")) | |
0bed242c YW |
353 | return 0; |
354 | ||
b97897e3 YW |
355 | r = sd_device_get_subsystem(dev, &val); |
356 | if (r < 0) | |
357 | return log_device_debug_errno(dev, r, "Failed to get subsystem: %m"); | |
358 | ||
359 | if (!streq(val, "block")) | |
0bed242c | 360 | return 0; |
fee854ee | 361 | |
b97897e3 YW |
362 | r = sd_device_get_sysname(dev, &val); |
363 | if (r < 0) | |
364 | return log_device_debug_errno(dev, r, "Failed to get sysname: %m"); | |
365 | ||
49fe5c09 | 366 | if (STARTSWITH_SET(val, "dm-", "md", "drbd")) |
0bed242c YW |
367 | return 0; |
368 | ||
b97897e3 YW |
369 | r = sd_device_get_devtype(dev, &val); |
370 | if (r < 0 && r != -ENOENT) | |
371 | return log_device_debug_errno(dev, r, "Failed to get devtype: %m"); | |
372 | if (r >= 0 && streq(val, "partition")) { | |
373 | r = sd_device_get_parent(dev, &dev); | |
374 | if (r < 0) | |
375 | return log_device_debug_errno(dev, r, "Failed to get parent device: %m"); | |
376 | } | |
0bed242c | 377 | |
b97897e3 YW |
378 | r = sd_device_get_devname(dev, &val); |
379 | if (r == -ENOENT) | |
0bed242c | 380 | return 0; |
b97897e3 YW |
381 | if (r < 0) |
382 | return log_device_debug_errno(dev, r, "Failed to get devname: %m"); | |
0bed242c | 383 | |
b97897e3 YW |
384 | fd = open(val, O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK); |
385 | if (fd < 0) { | |
386 | log_device_debug_errno(dev, errno, "Failed to open '%s', ignoring: %m", val); | |
0bed242c | 387 | return 0; |
b97897e3 | 388 | } |
0bed242c YW |
389 | |
390 | if (flock(fd, LOCK_SH|LOCK_NB) < 0) | |
b97897e3 | 391 | return log_device_debug_errno(dev, errno, "Failed to flock(%s): %m", val); |
0bed242c YW |
392 | |
393 | *ret_fd = TAKE_FD(fd); | |
394 | return 1; | |
fee854ee RK |
395 | } |
396 | ||
abde5ea8 | 397 | static int worker_process_device(Manager *manager, sd_device *dev) { |
2e088715 | 398 | _cleanup_(udev_event_freep) UdevEvent *udev_event = NULL; |
0bed242c | 399 | _cleanup_close_ int fd_lock = -1; |
abde5ea8 | 400 | const char *seqnum; |
0bed242c | 401 | int r; |
912541b0 | 402 | |
0bed242c YW |
403 | assert(manager); |
404 | assert(dev); | |
405 | ||
abde5ea8 YW |
406 | r = sd_device_get_property_value(dev, "SEQNUM", &seqnum); |
407 | if (r < 0) | |
408 | log_device_debug_errno(dev, r, "Failed to get SEQNUM: %m"); | |
409 | ||
410 | log_device_debug(dev, "Processing device (SEQNUM=%s)", seqnum); | |
411 | ||
412 | udev_event = udev_event_new(dev, arg_exec_delay_usec, manager->rtnl); | |
0bed242c YW |
413 | if (!udev_event) |
414 | return -ENOMEM; | |
415 | ||
abde5ea8 | 416 | r = worker_lock_block_device(dev, &fd_lock); |
b6aab8ef | 417 | if (r < 0) |
0bed242c | 418 | return r; |
912541b0 | 419 | |
0bed242c YW |
420 | /* apply rules, create node, symlinks */ |
421 | udev_event_execute_rules(udev_event, arg_event_timeout_usec, manager->properties, manager->rules); | |
422 | udev_event_execute_run(udev_event, arg_event_timeout_usec); | |
2dd9f98d | 423 | |
0bed242c YW |
424 | if (!manager->rtnl) |
425 | /* in case rtnl was initialized */ | |
426 | manager->rtnl = sd_netlink_ref(udev_event->rtnl); | |
912541b0 | 427 | |
0bed242c YW |
428 | /* apply/restore inotify watch */ |
429 | if (udev_event->inotify_watch) { | |
abde5ea8 YW |
430 | (void) udev_watch_begin(dev); |
431 | r = device_update_db(dev); | |
432 | if (r < 0) | |
433 | return log_device_debug_errno(dev, r, "Failed to update database under /run/udev/data/: %m"); | |
0bed242c | 434 | } |
912541b0 | 435 | |
abde5ea8 | 436 | log_device_debug(dev, "Device (SEQNUM=%s) processed", seqnum); |
912541b0 | 437 | |
0bed242c YW |
438 | return 0; |
439 | } | |
912541b0 | 440 | |
e2130348 YW |
441 | static int worker_device_monitor_handler(sd_device_monitor *monitor, sd_device *dev, void *userdata) { |
442 | Manager *manager = userdata; | |
443 | int r; | |
444 | ||
445 | assert(dev); | |
446 | assert(manager); | |
447 | ||
448 | r = worker_process_device(manager, dev); | |
449 | if (r < 0) | |
450 | log_device_warning_errno(dev, r, "Failed to process device, ignoring: %m"); | |
451 | ||
452 | /* send processed event back to libudev listeners */ | |
453 | r = device_monitor_send_device(monitor, NULL, dev); | |
454 | if (r < 0) | |
455 | log_device_warning_errno(dev, r, "Failed to send device, ignoring: %m"); | |
456 | ||
457 | /* send udevd the result of the event execution */ | |
458 | r = worker_send_message(manager->worker_watch[WRITE_END]); | |
459 | if (r < 0) | |
460 | log_device_warning_errno(dev, r, "Failed to send signal to main daemon, ignoring: %m"); | |
461 | ||
462 | return 1; | |
463 | } | |
464 | ||
7f2e3a14 YW |
465 | static int worker_main(Manager *_manager, sd_device_monitor *monitor, sd_device *first_device) { |
466 | _cleanup_(sd_device_unrefp) sd_device *dev = first_device; | |
0bed242c | 467 | _cleanup_(manager_freep) Manager *manager = _manager; |
44dcf454 | 468 | int r; |
145dae7e | 469 | |
0bed242c YW |
470 | assert(manager); |
471 | assert(monitor); | |
472 | assert(dev); | |
912541b0 | 473 | |
0bed242c | 474 | unsetenv("NOTIFY_SOCKET"); |
3b64e4d4 | 475 | |
e2130348 YW |
476 | assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, -1) >= 0); |
477 | ||
478 | /* Reset OOM score, we only protect the main daemon. */ | |
479 | r = set_oom_score_adjust(0); | |
480 | if (r < 0) | |
481 | log_debug_errno(r, "Failed to reset OOM score, ignoring: %m"); | |
912541b0 | 482 | |
e2130348 YW |
483 | /* Clear unnecessary data in Manager object.*/ |
484 | manager_clear_for_worker(manager); | |
3ebdb81e | 485 | |
e2130348 YW |
486 | r = sd_event_new(&manager->event); |
487 | if (r < 0) | |
488 | return log_error_errno(r, "Failed to allocate event loop: %m"); | |
912541b0 | 489 | |
e2130348 YW |
490 | r = sd_event_add_signal(manager->event, NULL, SIGTERM, NULL, NULL); |
491 | if (r < 0) | |
492 | return log_error_errno(r, "Failed to set SIGTERM event: %m"); | |
4c83d994 | 493 | |
e2130348 YW |
494 | r = sd_device_monitor_attach_event(monitor, manager->event); |
495 | if (r < 0) | |
496 | return log_error_errno(r, "Failed to attach event loop to device monitor: %m"); | |
912541b0 | 497 | |
e2130348 | 498 | r = sd_device_monitor_start(monitor, worker_device_monitor_handler, manager); |
0bed242c | 499 | if (r < 0) |
e2130348 | 500 | return log_error_errno(r, "Failed to start device monitor: %m"); |
912541b0 | 501 | |
e2130348 | 502 | (void) sd_event_source_set_description(sd_device_monitor_get_event_source(monitor), "worker-device-monitor"); |
b66f29a1 | 503 | |
e2130348 YW |
504 | /* Process first device */ |
505 | (void) worker_device_monitor_handler(monitor, dev, manager); | |
912541b0 | 506 | |
e2130348 YW |
507 | r = sd_event_loop(manager->event); |
508 | if (r < 0) | |
509 | return log_error_errno(r, "Event loop failed: %m"); | |
510 | ||
44dcf454 | 511 | return 0; |
0bed242c YW |
512 | } |
513 | ||
7443654e | 514 | static int worker_spawn(Manager *manager, struct event *event) { |
7f2e3a14 | 515 | _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *worker_monitor = NULL; |
7443654e | 516 | struct worker *worker; |
0bed242c | 517 | pid_t pid; |
7443654e | 518 | int r; |
0bed242c YW |
519 | |
520 | /* listen for new events */ | |
7f2e3a14 YW |
521 | r = device_monitor_new_full(&worker_monitor, MONITOR_GROUP_NONE, -1); |
522 | if (r < 0) | |
523 | return r; | |
7443654e | 524 | |
0bed242c | 525 | /* allow the main daemon netlink address to send devices to the worker */ |
7f2e3a14 YW |
526 | r = device_monitor_allow_unicast_sender(worker_monitor, manager->monitor); |
527 | if (r < 0) | |
528 | return log_error_errno(r, "Worker: Failed to set unicast sender: %m"); | |
529 | ||
530 | r = device_monitor_enable_receiving(worker_monitor); | |
0bed242c | 531 | if (r < 0) |
7f2e3a14 | 532 | return log_error_errno(r, "Worker: Failed to enable receiving of device: %m"); |
0bed242c | 533 | |
ff86c92e | 534 | r = safe_fork(NULL, FORK_DEATHSIG, &pid); |
7443654e YW |
535 | if (r < 0) { |
536 | event->state = EVENT_QUEUED; | |
537 | return log_error_errno(r, "Failed to fork() worker: %m"); | |
538 | } | |
539 | if (r == 0) { | |
540 | /* Worker process */ | |
eb546b35 | 541 | r = worker_main(manager, worker_monitor, sd_device_ref(event->dev)); |
baa30fbc | 542 | log_close(); |
8b46c3fc | 543 | _exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS); |
912541b0 | 544 | } |
e03c7cc2 | 545 | |
7443654e YW |
546 | r = worker_new(&worker, manager, worker_monitor, pid); |
547 | if (r < 0) | |
548 | return log_error_errno(r, "Failed to create worker object: %m"); | |
e03c7cc2 | 549 | |
7443654e | 550 | worker_attach_event(worker, event); |
39c19cf1 | 551 | |
eb546b35 | 552 | log_device_debug(event->dev, "Worker ["PID_FMT"] is forked for processing SEQNUM=%"PRIu64".", pid, event->seqnum); |
7443654e | 553 | return 0; |
7fafc032 KS |
554 | } |
555 | ||
c0c6806b | 556 | static void event_run(Manager *manager, struct event *event) { |
a505965d TG |
557 | struct worker *worker; |
558 | Iterator i; | |
7f2e3a14 | 559 | int r; |
912541b0 | 560 | |
c0c6806b TG |
561 | assert(manager); |
562 | assert(event); | |
563 | ||
564 | HASHMAP_FOREACH(worker, manager->workers, i) { | |
912541b0 KS |
565 | if (worker->state != WORKER_IDLE) |
566 | continue; | |
567 | ||
eb546b35 | 568 | r = device_monitor_send_device(manager->monitor, worker->monitor, event->dev); |
7f2e3a14 | 569 | if (r < 0) { |
eb546b35 | 570 | log_device_error_errno(event->dev, r, "Worker ["PID_FMT"] did not accept message, killing the worker: %m", |
7f2e3a14 | 571 | worker->pid); |
cb542e84 | 572 | (void) kill(worker->pid, SIGKILL); |
912541b0 KS |
573 | worker->state = WORKER_KILLED; |
574 | continue; | |
575 | } | |
39c19cf1 | 576 | worker_attach_event(worker, event); |
912541b0 KS |
577 | return; |
578 | } | |
579 | ||
c0c6806b | 580 | if (hashmap_size(manager->workers) >= arg_children_max) { |
bba7a484 | 581 | if (arg_children_max > 1) |
044497e2 | 582 | log_debug("Maximum number (%u) of children reached.", hashmap_size(manager->workers)); |
912541b0 KS |
583 | return; |
584 | } | |
585 | ||
586 | /* start new worker and pass initial device */ | |
c0c6806b | 587 | worker_spawn(manager, event); |
1e03b754 KS |
588 | } |
589 | ||
eb546b35 YW |
590 | static int event_queue_insert(Manager *manager, sd_device *dev) { |
591 | _cleanup_(sd_device_unrefp) sd_device *clone = NULL; | |
912541b0 | 592 | struct event *event; |
eb546b35 YW |
593 | const char *val; |
594 | uint64_t seqnum; | |
cb49a4f2 | 595 | int r; |
912541b0 | 596 | |
ecb17862 TG |
597 | assert(manager); |
598 | assert(dev); | |
599 | ||
040e6896 TG |
600 | /* only one process can add events to the queue */ |
601 | if (manager->pid == 0) | |
df0ff127 | 602 | manager->pid = getpid_cached(); |
040e6896 | 603 | |
df0ff127 | 604 | assert(manager->pid == getpid_cached()); |
cb49a4f2 | 605 | |
eb546b35 YW |
606 | /* We only accepts devices received by device monitor. */ |
607 | r = sd_device_get_property_value(dev, "SEQNUM", &val); | |
608 | if (r < 0) | |
609 | return r; | |
610 | ||
611 | r = safe_atou64(val, &seqnum); | |
612 | if (r < 0) | |
613 | return r; | |
614 | ||
615 | if (seqnum == 0) | |
616 | return -EINVAL; | |
912541b0 | 617 | |
eb546b35 YW |
618 | /* Save original device to restore the state on failures. */ |
619 | r = device_shallow_clone(dev, &clone); | |
620 | if (r < 0) | |
621 | return r; | |
622 | ||
623 | r = device_copy_properties(clone, dev); | |
624 | if (r < 0) | |
625 | return r; | |
912541b0 | 626 | |
eb546b35 YW |
627 | event = new(struct event, 1); |
628 | if (!event) | |
629 | return -ENOMEM; | |
912541b0 | 630 | |
eb546b35 YW |
631 | *event = (struct event) { |
632 | .manager = manager, | |
633 | .dev = sd_device_ref(dev), | |
634 | .dev_kernel = TAKE_PTR(clone), | |
635 | .seqnum = seqnum, | |
636 | .state = EVENT_QUEUED, | |
637 | }; | |
cb49a4f2 | 638 | |
40a57716 | 639 | if (LIST_IS_EMPTY(manager->events)) { |
cb49a4f2 TG |
640 | r = touch("/run/udev/queue"); |
641 | if (r < 0) | |
044497e2 | 642 | log_warning_errno(r, "Failed to touch /run/udev/queue: %m"); |
cb49a4f2 TG |
643 | } |
644 | ||
40a57716 | 645 | LIST_APPEND(event, manager->events, event); |
cb49a4f2 | 646 | |
eb546b35 YW |
647 | if (DEBUG_LOGGING) { |
648 | if (sd_device_get_property_value(dev, "ACTION", &val) < 0) | |
649 | val = NULL; | |
650 | ||
651 | log_device_debug(dev, "Device (SEQNUM=%"PRIu64", ACTION=%s) is queued", seqnum, strnull(val)); | |
652 | } | |
653 | ||
912541b0 | 654 | return 0; |
fc465079 KS |
655 | } |
656 | ||
c0c6806b | 657 | static void manager_kill_workers(Manager *manager) { |
a505965d TG |
658 | struct worker *worker; |
659 | Iterator i; | |
1e03b754 | 660 | |
c0c6806b TG |
661 | assert(manager); |
662 | ||
663 | HASHMAP_FOREACH(worker, manager->workers, i) { | |
912541b0 KS |
664 | if (worker->state == WORKER_KILLED) |
665 | continue; | |
1e03b754 | 666 | |
912541b0 | 667 | worker->state = WORKER_KILLED; |
cb542e84 | 668 | (void) kill(worker->pid, SIGTERM); |
912541b0 | 669 | } |
1e03b754 KS |
670 | } |
671 | ||
e3196993 | 672 | /* lookup event for identical, parent, child device */ |
eb546b35 YW |
673 | static int is_device_busy(Manager *manager, struct event *event) { |
674 | const char *subsystem, *devpath, *devpath_old = NULL; | |
675 | dev_t devnum = makedev(0, 0); | |
40a57716 | 676 | struct event *loop_event; |
eb546b35 YW |
677 | size_t devpath_len; |
678 | int r, ifindex = 0; | |
679 | bool is_block; | |
680 | ||
681 | r = sd_device_get_subsystem(event->dev, &subsystem); | |
682 | if (r < 0) | |
683 | return r; | |
684 | ||
685 | is_block = streq(subsystem, "block"); | |
686 | ||
687 | r = sd_device_get_devpath(event->dev, &devpath); | |
688 | if (r < 0) | |
689 | return r; | |
690 | ||
691 | devpath_len = strlen(devpath); | |
692 | ||
693 | r = sd_device_get_property_value(event->dev, "DEVPATH_OLD", &devpath_old); | |
694 | if (r < 0 && r != -ENOENT) | |
695 | return r; | |
696 | ||
697 | r = sd_device_get_devnum(event->dev, &devnum); | |
698 | if (r < 0 && r != -ENOENT) | |
699 | return r; | |
700 | ||
701 | r = sd_device_get_ifindex(event->dev, &ifindex); | |
702 | if (r < 0 && r != -ENOENT) | |
703 | return r; | |
912541b0 KS |
704 | |
705 | /* check if queue contains events we depend on */ | |
40a57716 | 706 | LIST_FOREACH(event, loop_event, manager->events) { |
eb546b35 YW |
707 | size_t loop_devpath_len, common; |
708 | const char *loop_devpath; | |
709 | ||
87ac8d99 | 710 | /* we already found a later event, earlier cannot block us, no need to check again */ |
912541b0 KS |
711 | if (loop_event->seqnum < event->delaying_seqnum) |
712 | continue; | |
713 | ||
714 | /* event we checked earlier still exists, no need to check again */ | |
715 | if (loop_event->seqnum == event->delaying_seqnum) | |
716 | return true; | |
717 | ||
718 | /* found ourself, no later event can block us */ | |
719 | if (loop_event->seqnum >= event->seqnum) | |
720 | break; | |
721 | ||
722 | /* check major/minor */ | |
eb546b35 YW |
723 | if (major(devnum) != 0) { |
724 | const char *s; | |
725 | dev_t d; | |
726 | ||
727 | if (sd_device_get_subsystem(loop_event->dev, &s) < 0) | |
728 | continue; | |
729 | ||
730 | if (sd_device_get_devnum(loop_event->dev, &d) >= 0 && | |
731 | devnum == d && is_block == streq(s, "block")) | |
732 | return true; | |
733 | } | |
912541b0 KS |
734 | |
735 | /* check network device ifindex */ | |
eb546b35 YW |
736 | if (ifindex > 0) { |
737 | int i; | |
738 | ||
739 | if (sd_device_get_ifindex(loop_event->dev, &i) >= 0 && | |
740 | ifindex == i) | |
741 | return true; | |
742 | } | |
743 | ||
744 | if (sd_device_get_devpath(loop_event->dev, &loop_devpath) < 0) | |
745 | continue; | |
912541b0 KS |
746 | |
747 | /* check our old name */ | |
eb546b35 | 748 | if (devpath_old && streq(devpath_old, loop_devpath)) { |
912541b0 KS |
749 | event->delaying_seqnum = loop_event->seqnum; |
750 | return true; | |
751 | } | |
752 | ||
eb546b35 YW |
753 | loop_devpath_len = strlen(loop_devpath); |
754 | ||
912541b0 | 755 | /* compare devpath */ |
eb546b35 | 756 | common = MIN(devpath_len, loop_devpath_len); |
912541b0 KS |
757 | |
758 | /* one devpath is contained in the other? */ | |
eb546b35 | 759 | if (!strneq(devpath, loop_devpath, common)) |
912541b0 KS |
760 | continue; |
761 | ||
762 | /* identical device event found */ | |
eb546b35 | 763 | if (devpath_len == loop_devpath_len) { |
912541b0 | 764 | /* devices names might have changed/swapped in the meantime */ |
eb546b35 | 765 | if (major(devnum) != 0 || ifindex > 0) |
912541b0 KS |
766 | continue; |
767 | event->delaying_seqnum = loop_event->seqnum; | |
768 | return true; | |
769 | } | |
770 | ||
771 | /* parent device event found */ | |
eb546b35 | 772 | if (devpath[common] == '/') { |
912541b0 KS |
773 | event->delaying_seqnum = loop_event->seqnum; |
774 | return true; | |
775 | } | |
776 | ||
777 | /* child device event found */ | |
eb546b35 | 778 | if (loop_devpath[common] == '/') { |
912541b0 KS |
779 | event->delaying_seqnum = loop_event->seqnum; |
780 | return true; | |
781 | } | |
912541b0 KS |
782 | } |
783 | ||
784 | return false; | |
7fafc032 KS |
785 | } |
786 | ||
693d371d TG |
787 | static int on_exit_timeout(sd_event_source *s, uint64_t usec, void *userdata) { |
788 | Manager *manager = userdata; | |
789 | ||
790 | assert(manager); | |
791 | ||
d4e98880 | 792 | log_error("Giving up waiting for workers to finish."); |
693d371d TG |
793 | sd_event_exit(manager->event, -ETIMEDOUT); |
794 | ||
795 | return 1; | |
796 | } | |
797 | ||
62d43dac | 798 | static void manager_exit(Manager *manager) { |
693d371d TG |
799 | uint64_t usec; |
800 | int r; | |
62d43dac TG |
801 | |
802 | assert(manager); | |
803 | ||
804 | manager->exit = true; | |
805 | ||
b79aacbf TG |
806 | sd_notify(false, |
807 | "STOPPING=1\n" | |
808 | "STATUS=Starting shutdown..."); | |
809 | ||
62d43dac | 810 | /* close sources of new events and discard buffered events */ |
693d371d | 811 | manager->ctrl_event = sd_event_source_unref(manager->ctrl_event); |
ab7854df | 812 | manager->ctrl = udev_ctrl_unref(manager->ctrl); |
62d43dac | 813 | |
693d371d | 814 | manager->inotify_event = sd_event_source_unref(manager->inotify_event); |
ab7854df | 815 | manager->fd_inotify = safe_close(manager->fd_inotify); |
62d43dac | 816 | |
693d371d | 817 | manager->uevent_event = sd_event_source_unref(manager->uevent_event); |
7f2e3a14 | 818 | manager->monitor = sd_device_monitor_unref(manager->monitor); |
62d43dac TG |
819 | |
820 | /* discard queued events and kill workers */ | |
821 | event_queue_cleanup(manager, EVENT_QUEUED); | |
822 | manager_kill_workers(manager); | |
693d371d | 823 | |
3285baa8 | 824 | assert_se(sd_event_now(manager->event, CLOCK_MONOTONIC, &usec) >= 0); |
693d371d | 825 | |
3285baa8 | 826 | r = sd_event_add_time(manager->event, NULL, CLOCK_MONOTONIC, |
693d371d TG |
827 | usec + 30 * USEC_PER_SEC, USEC_PER_SEC, on_exit_timeout, manager); |
828 | if (r < 0) | |
829 | return; | |
62d43dac TG |
830 | } |
831 | ||
832 | /* reload requested, HUP signal received, rules changed, builtin changed */ | |
833 | static void manager_reload(Manager *manager) { | |
834 | ||
835 | assert(manager); | |
836 | ||
b79aacbf TG |
837 | sd_notify(false, |
838 | "RELOADING=1\n" | |
839 | "STATUS=Flushing configuration..."); | |
840 | ||
62d43dac | 841 | manager_kill_workers(manager); |
981fae90 | 842 | manager->rules = udev_rules_free(manager->rules); |
2024ed61 | 843 | udev_builtin_exit(); |
b79aacbf | 844 | |
1ef72b55 MS |
845 | sd_notifyf(false, |
846 | "READY=1\n" | |
847 | "STATUS=Processing with %u children at max", arg_children_max); | |
62d43dac TG |
848 | } |
849 | ||
eca195ec YW |
850 | static int on_kill_workers_event(sd_event_source *s, uint64_t usec, void *userdata) { |
851 | Manager *manager = userdata; | |
852 | ||
853 | assert(manager); | |
854 | ||
855 | log_debug("Cleanup idle workers"); | |
856 | manager_kill_workers(manager); | |
857 | ||
858 | return 1; | |
859 | } | |
860 | ||
c0c6806b | 861 | static void event_queue_start(Manager *manager) { |
40a57716 | 862 | struct event *event; |
693d371d | 863 | usec_t usec; |
0725c4b9 | 864 | int r; |
8ab44e3f | 865 | |
c0c6806b TG |
866 | assert(manager); |
867 | ||
40a57716 | 868 | if (LIST_IS_EMPTY(manager->events) || |
7c4c7e89 TG |
869 | manager->exit || manager->stop_exec_queue) |
870 | return; | |
871 | ||
3285baa8 | 872 | assert_se(sd_event_now(manager->event, CLOCK_MONOTONIC, &usec) >= 0); |
38a03f06 LP |
873 | /* check for changed config, every 3 seconds at most */ |
874 | if (manager->last_usec == 0 || | |
875 | (usec - manager->last_usec) > 3 * USEC_PER_SEC) { | |
876 | if (udev_rules_check_timestamp(manager->rules) || | |
2024ed61 | 877 | udev_builtin_validate()) |
38a03f06 | 878 | manager_reload(manager); |
693d371d | 879 | |
38a03f06 | 880 | manager->last_usec = usec; |
7c4c7e89 TG |
881 | } |
882 | ||
0725c4b9 YW |
883 | r = event_source_disable(manager->kill_workers_event); |
884 | if (r < 0) | |
885 | log_warning_errno(r, "Failed to disable event source for cleaning up idle workers, ignoring: %m"); | |
eca195ec | 886 | |
2024ed61 | 887 | udev_builtin_init(); |
7c4c7e89 TG |
888 | |
889 | if (!manager->rules) { | |
1d791281 ZJS |
890 | r = udev_rules_new(&manager->rules, arg_resolve_name_timing); |
891 | if (r < 0) { | |
892 | log_warning_errno(r, "Failed to read udev rules: %m"); | |
7c4c7e89 | 893 | return; |
1d791281 | 894 | } |
7c4c7e89 TG |
895 | } |
896 | ||
eb546b35 | 897 | LIST_FOREACH(event, event, manager->events) { |
912541b0 KS |
898 | if (event->state != EVENT_QUEUED) |
899 | continue; | |
0bc74ea7 | 900 | |
912541b0 | 901 | /* do not start event if parent or child event is still running */ |
eb546b35 | 902 | if (is_device_busy(manager, event) != 0) |
912541b0 | 903 | continue; |
fc465079 | 904 | |
c0c6806b | 905 | event_run(manager, event); |
912541b0 | 906 | } |
1e03b754 KS |
907 | } |
908 | ||
ecb17862 | 909 | static void event_queue_cleanup(Manager *manager, enum event_state match_type) { |
40a57716 | 910 | struct event *event, *tmp; |
ff2c503d | 911 | |
40a57716 | 912 | LIST_FOREACH_SAFE(event, event, tmp, manager->events) { |
912541b0 KS |
913 | if (match_type != EVENT_UNDEF && match_type != event->state) |
914 | continue; | |
ff2c503d | 915 | |
c6aa11f2 | 916 | event_free(event); |
912541b0 | 917 | } |
ff2c503d KS |
918 | } |
919 | ||
e82e8fa5 | 920 | static int on_worker(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
c0c6806b TG |
921 | Manager *manager = userdata; |
922 | ||
923 | assert(manager); | |
924 | ||
912541b0 KS |
925 | for (;;) { |
926 | struct worker_message msg; | |
979558f3 TG |
927 | struct iovec iovec = { |
928 | .iov_base = &msg, | |
929 | .iov_len = sizeof(msg), | |
930 | }; | |
931 | union { | |
932 | struct cmsghdr cmsghdr; | |
933 | uint8_t buf[CMSG_SPACE(sizeof(struct ucred))]; | |
934 | } control = {}; | |
935 | struct msghdr msghdr = { | |
936 | .msg_iov = &iovec, | |
937 | .msg_iovlen = 1, | |
938 | .msg_control = &control, | |
939 | .msg_controllen = sizeof(control), | |
940 | }; | |
941 | struct cmsghdr *cmsg; | |
912541b0 | 942 | ssize_t size; |
979558f3 | 943 | struct ucred *ucred = NULL; |
a505965d | 944 | struct worker *worker; |
912541b0 | 945 | |
e82e8fa5 | 946 | size = recvmsg(fd, &msghdr, MSG_DONTWAIT); |
979558f3 | 947 | if (size < 0) { |
738a7907 TG |
948 | if (errno == EINTR) |
949 | continue; | |
950 | else if (errno == EAGAIN) | |
951 | /* nothing more to read */ | |
952 | break; | |
979558f3 | 953 | |
044497e2 | 954 | return log_error_errno(errno, "Failed to receive message: %m"); |
979558f3 | 955 | } else if (size != sizeof(struct worker_message)) { |
d4e98880 | 956 | log_warning("Ignoring worker message with invalid size %zi bytes", size); |
e82e8fa5 | 957 | continue; |
979558f3 TG |
958 | } |
959 | ||
2a1288ff | 960 | CMSG_FOREACH(cmsg, &msghdr) { |
979558f3 TG |
961 | if (cmsg->cmsg_level == SOL_SOCKET && |
962 | cmsg->cmsg_type == SCM_CREDENTIALS && | |
963 | cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred))) | |
964 | ucred = (struct ucred*) CMSG_DATA(cmsg); | |
965 | } | |
966 | ||
967 | if (!ucred || ucred->pid <= 0) { | |
d4e98880 | 968 | log_warning("Ignoring worker message without valid PID"); |
979558f3 TG |
969 | continue; |
970 | } | |
912541b0 KS |
971 | |
972 | /* lookup worker who sent the signal */ | |
4a0b58c4 | 973 | worker = hashmap_get(manager->workers, PID_TO_PTR(ucred->pid)); |
a505965d | 974 | if (!worker) { |
044497e2 | 975 | log_debug("Worker ["PID_FMT"] returned, but is no longer tracked", ucred->pid); |
a505965d | 976 | continue; |
912541b0 | 977 | } |
c0bbfd72 | 978 | |
a505965d TG |
979 | if (worker->state != WORKER_KILLED) |
980 | worker->state = WORKER_IDLE; | |
981 | ||
982 | /* worker returned */ | |
983 | event_free(worker->event); | |
912541b0 | 984 | } |
e82e8fa5 | 985 | |
8302fe5a TG |
986 | /* we have free workers, try to schedule events */ |
987 | event_queue_start(manager); | |
988 | ||
e82e8fa5 TG |
989 | return 1; |
990 | } | |
991 | ||
f00d2b6d | 992 | static int on_uevent(sd_device_monitor *monitor, sd_device *dev, void *userdata) { |
c0c6806b | 993 | Manager *manager = userdata; |
e82e8fa5 TG |
994 | int r; |
995 | ||
c0c6806b | 996 | assert(manager); |
e82e8fa5 | 997 | |
7f2e3a14 YW |
998 | device_ensure_usec_initialized(dev, NULL); |
999 | ||
eb546b35 YW |
1000 | r = event_queue_insert(manager, dev); |
1001 | if (r < 0) { | |
1002 | log_device_error_errno(dev, r, "Failed to insert device into event queue: %m"); | |
7f2e3a14 | 1003 | return 1; |
eb546b35 | 1004 | } |
7f2e3a14 YW |
1005 | |
1006 | /* we have fresh events, try to schedule them */ | |
1007 | event_queue_start(manager); | |
e82e8fa5 TG |
1008 | |
1009 | return 1; | |
88f4b648 KS |
1010 | } |
1011 | ||
3b47c739 | 1012 | /* receive the udevd message from userspace */ |
e82e8fa5 | 1013 | static int on_ctrl_msg(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
c0c6806b | 1014 | Manager *manager = userdata; |
8e766630 LP |
1015 | _cleanup_(udev_ctrl_connection_unrefp) struct udev_ctrl_connection *ctrl_conn = NULL; |
1016 | _cleanup_(udev_ctrl_msg_unrefp) struct udev_ctrl_msg *ctrl_msg = NULL; | |
912541b0 | 1017 | const char *str; |
9b5150b6 | 1018 | int i, r; |
912541b0 | 1019 | |
c0c6806b | 1020 | assert(manager); |
e4f66b77 | 1021 | |
c0c6806b | 1022 | ctrl_conn = udev_ctrl_get_connection(manager->ctrl); |
e4f66b77 | 1023 | if (!ctrl_conn) |
e82e8fa5 | 1024 | return 1; |
912541b0 KS |
1025 | |
1026 | ctrl_msg = udev_ctrl_receive_msg(ctrl_conn); | |
e4f66b77 | 1027 | if (!ctrl_msg) |
e82e8fa5 | 1028 | return 1; |
912541b0 KS |
1029 | |
1030 | i = udev_ctrl_get_set_log_level(ctrl_msg); | |
1031 | if (i >= 0) { | |
044497e2 | 1032 | log_debug("Received udev control message (SET_LOG_LEVEL), setting log_priority=%i", i); |
baa30fbc | 1033 | log_set_max_level(i); |
c0c6806b | 1034 | manager_kill_workers(manager); |
912541b0 KS |
1035 | } |
1036 | ||
1037 | if (udev_ctrl_get_stop_exec_queue(ctrl_msg) > 0) { | |
044497e2 | 1038 | log_debug("Received udev control message (STOP_EXEC_QUEUE)"); |
c0c6806b | 1039 | manager->stop_exec_queue = true; |
912541b0 KS |
1040 | } |
1041 | ||
1042 | if (udev_ctrl_get_start_exec_queue(ctrl_msg) > 0) { | |
044497e2 | 1043 | log_debug("Received udev control message (START_EXEC_QUEUE)"); |
c0c6806b | 1044 | manager->stop_exec_queue = false; |
8302fe5a | 1045 | event_queue_start(manager); |
912541b0 KS |
1046 | } |
1047 | ||
1048 | if (udev_ctrl_get_reload(ctrl_msg) > 0) { | |
044497e2 | 1049 | log_debug("Received udev control message (RELOAD)"); |
62d43dac | 1050 | manager_reload(manager); |
912541b0 KS |
1051 | } |
1052 | ||
1053 | str = udev_ctrl_get_set_env(ctrl_msg); | |
9b5150b6 YW |
1054 | if (str) { |
1055 | _cleanup_free_ char *key = NULL, *val = NULL, *old_key = NULL, *old_val = NULL; | |
1056 | char *eq; | |
1057 | ||
1058 | eq = strchr(str, '='); | |
1059 | if (!eq) { | |
1060 | log_error("Invalid key format '%s'", str); | |
1061 | return 1; | |
1062 | } | |
1063 | ||
1064 | key = strndup(str, eq - str); | |
1065 | if (!key) { | |
1066 | log_oom(); | |
1067 | return 1; | |
1068 | } | |
1069 | ||
1070 | old_val = hashmap_remove2(manager->properties, key, (void **) &old_key); | |
1071 | ||
1072 | r = hashmap_ensure_allocated(&manager->properties, &string_hash_ops); | |
1073 | if (r < 0) { | |
1074 | log_oom(); | |
1075 | return 1; | |
912541b0 | 1076 | } |
9b5150b6 YW |
1077 | |
1078 | eq++; | |
1079 | if (!isempty(eq)) { | |
044497e2 | 1080 | log_debug("Received udev control message (ENV), unsetting '%s'", key); |
9b5150b6 YW |
1081 | |
1082 | r = hashmap_put(manager->properties, key, NULL); | |
1083 | if (r < 0) { | |
1084 | log_oom(); | |
1085 | return 1; | |
1086 | } | |
1087 | } else { | |
1088 | val = strdup(eq); | |
1089 | if (!val) { | |
1090 | log_oom(); | |
1091 | return 1; | |
1092 | } | |
1093 | ||
044497e2 | 1094 | log_debug("Received udev control message (ENV), setting '%s=%s'", key, val); |
9b5150b6 YW |
1095 | |
1096 | r = hashmap_put(manager->properties, key, val); | |
1097 | if (r < 0) { | |
1098 | log_oom(); | |
1099 | return 1; | |
1100 | } | |
1101 | } | |
1102 | ||
1103 | key = val = NULL; | |
c0c6806b | 1104 | manager_kill_workers(manager); |
912541b0 KS |
1105 | } |
1106 | ||
1107 | i = udev_ctrl_get_set_children_max(ctrl_msg); | |
1108 | if (i >= 0) { | |
044497e2 | 1109 | log_debug("Receivd udev control message (SET_MAX_CHILDREN), setting children_max=%i", i); |
bba7a484 | 1110 | arg_children_max = i; |
1ef72b55 MS |
1111 | |
1112 | (void) sd_notifyf(false, | |
1113 | "READY=1\n" | |
1114 | "STATUS=Processing with %u children at max", arg_children_max); | |
912541b0 KS |
1115 | } |
1116 | ||
cb49a4f2 | 1117 | if (udev_ctrl_get_ping(ctrl_msg) > 0) |
044497e2 | 1118 | log_debug("Received udev control message (SYNC)"); |
912541b0 KS |
1119 | |
1120 | if (udev_ctrl_get_exit(ctrl_msg) > 0) { | |
044497e2 | 1121 | log_debug("Received udev control message (EXIT)"); |
62d43dac | 1122 | manager_exit(manager); |
c0c6806b TG |
1123 | /* keep reference to block the client until we exit |
1124 | TODO: deal with several blocking exit requests */ | |
1125 | manager->ctrl_conn_blocking = udev_ctrl_connection_ref(ctrl_conn); | |
912541b0 | 1126 | } |
e4f66b77 | 1127 | |
e82e8fa5 | 1128 | return 1; |
88f4b648 | 1129 | } |
4a231017 | 1130 | |
70068602 YW |
1131 | static int synthesize_change(sd_device *dev) { |
1132 | const char *subsystem, *sysname, *devname, *syspath, *devtype; | |
1133 | char filename[PATH_MAX]; | |
f3a740a5 | 1134 | int r; |
edd32000 | 1135 | |
70068602 YW |
1136 | r = sd_device_get_subsystem(dev, &subsystem); |
1137 | if (r < 0) | |
1138 | return r; | |
1139 | ||
1140 | r = sd_device_get_sysname(dev, &sysname); | |
1141 | if (r < 0) | |
1142 | return r; | |
1143 | ||
1144 | r = sd_device_get_devname(dev, &devname); | |
1145 | if (r < 0) | |
1146 | return r; | |
1147 | ||
1148 | r = sd_device_get_syspath(dev, &syspath); | |
1149 | if (r < 0) | |
1150 | return r; | |
1151 | ||
1152 | r = sd_device_get_devtype(dev, &devtype); | |
1153 | if (r < 0) | |
1154 | return r; | |
1155 | ||
1156 | if (streq_ptr("block", subsystem) && | |
1157 | streq_ptr("disk", devtype) && | |
1158 | !startswith(sysname, "dm-")) { | |
1159 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; | |
1160 | bool part_table_read = false, has_partitions = false; | |
1161 | sd_device *d; | |
ede34445 | 1162 | int fd; |
f3a740a5 | 1163 | |
ede34445 | 1164 | /* |
e9fc29f4 KS |
1165 | * Try to re-read the partition table. This only succeeds if |
1166 | * none of the devices is busy. The kernel returns 0 if no | |
1167 | * partition table is found, and we will not get an event for | |
1168 | * the disk. | |
ede34445 | 1169 | */ |
70068602 | 1170 | fd = open(devname, O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK); |
ede34445 | 1171 | if (fd >= 0) { |
02ba8fb3 KS |
1172 | r = flock(fd, LOCK_EX|LOCK_NB); |
1173 | if (r >= 0) | |
1174 | r = ioctl(fd, BLKRRPART, 0); | |
1175 | ||
ede34445 KS |
1176 | close(fd); |
1177 | if (r >= 0) | |
e9fc29f4 | 1178 | part_table_read = true; |
ede34445 KS |
1179 | } |
1180 | ||
e9fc29f4 | 1181 | /* search for partitions */ |
70068602 | 1182 | r = sd_device_enumerator_new(&e); |
f3a740a5 KS |
1183 | if (r < 0) |
1184 | return r; | |
1185 | ||
70068602 | 1186 | r = sd_device_enumerator_allow_uninitialized(e); |
f3a740a5 KS |
1187 | if (r < 0) |
1188 | return r; | |
1189 | ||
70068602 | 1190 | r = sd_device_enumerator_add_match_parent(e, dev); |
47a3fa0f TA |
1191 | if (r < 0) |
1192 | return r; | |
e9fc29f4 | 1193 | |
70068602 YW |
1194 | r = sd_device_enumerator_add_match_subsystem(e, "block", true); |
1195 | if (r < 0) | |
1196 | return r; | |
e9fc29f4 | 1197 | |
70068602 YW |
1198 | FOREACH_DEVICE(e, d) { |
1199 | const char *t; | |
e9fc29f4 | 1200 | |
70068602 YW |
1201 | if (sd_device_get_devtype(d, &t) < 0 || |
1202 | !streq("partition", t)) | |
e9fc29f4 KS |
1203 | continue; |
1204 | ||
1205 | has_partitions = true; | |
1206 | break; | |
1207 | } | |
1208 | ||
1209 | /* | |
1210 | * We have partitions and re-read the table, the kernel already sent | |
1211 | * out a "change" event for the disk, and "remove/add" for all | |
1212 | * partitions. | |
1213 | */ | |
1214 | if (part_table_read && has_partitions) | |
1215 | return 0; | |
1216 | ||
1217 | /* | |
1218 | * We have partitions but re-reading the partition table did not | |
1219 | * work, synthesize "change" for the disk and all partitions. | |
1220 | */ | |
70068602 YW |
1221 | log_debug("Device '%s' is closed, synthesising 'change'", devname); |
1222 | strscpyl(filename, sizeof(filename), syspath, "/uevent", NULL); | |
57512c89 | 1223 | write_string_file(filename, "change", WRITE_STRING_FILE_DISABLE_BUFFER); |
e9fc29f4 | 1224 | |
70068602 YW |
1225 | FOREACH_DEVICE(e, d) { |
1226 | const char *t, *n, *s; | |
f3a740a5 | 1227 | |
70068602 YW |
1228 | if (sd_device_get_devtype(d, &t) < 0 || |
1229 | !streq("partition", t)) | |
f3a740a5 KS |
1230 | continue; |
1231 | ||
70068602 YW |
1232 | if (sd_device_get_devname(d, &n) < 0 || |
1233 | sd_device_get_syspath(d, &s) < 0) | |
f3a740a5 KS |
1234 | continue; |
1235 | ||
70068602 YW |
1236 | log_debug("Device '%s' is closed, synthesising partition '%s' 'change'", devname, n); |
1237 | strscpyl(filename, sizeof(filename), s, "/uevent", NULL); | |
57512c89 | 1238 | write_string_file(filename, "change", WRITE_STRING_FILE_DISABLE_BUFFER); |
f3a740a5 | 1239 | } |
ede34445 KS |
1240 | |
1241 | return 0; | |
f3a740a5 KS |
1242 | } |
1243 | ||
70068602 YW |
1244 | log_debug("Device %s is closed, synthesising 'change'", devname); |
1245 | strscpyl(filename, sizeof(filename), syspath, "/uevent", NULL); | |
57512c89 | 1246 | write_string_file(filename, "change", WRITE_STRING_FILE_DISABLE_BUFFER); |
ede34445 | 1247 | |
f3a740a5 | 1248 | return 0; |
edd32000 KS |
1249 | } |
1250 | ||
e82e8fa5 | 1251 | static int on_inotify(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
c0c6806b | 1252 | Manager *manager = userdata; |
0254e944 | 1253 | union inotify_event_buffer buffer; |
f7c1ad4f LP |
1254 | struct inotify_event *e; |
1255 | ssize_t l; | |
0725c4b9 | 1256 | int r; |
912541b0 | 1257 | |
c0c6806b | 1258 | assert(manager); |
e82e8fa5 | 1259 | |
0725c4b9 YW |
1260 | r = event_source_disable(manager->kill_workers_event); |
1261 | if (r < 0) | |
1262 | log_warning_errno(r, "Failed to disable event source for cleaning up idle workers, ignoring: %m"); | |
eca195ec | 1263 | |
e82e8fa5 | 1264 | l = read(fd, &buffer, sizeof(buffer)); |
f7c1ad4f | 1265 | if (l < 0) { |
3742095b | 1266 | if (IN_SET(errno, EAGAIN, EINTR)) |
e82e8fa5 | 1267 | return 1; |
912541b0 | 1268 | |
f7c1ad4f | 1269 | return log_error_errno(errno, "Failed to read inotify fd: %m"); |
912541b0 KS |
1270 | } |
1271 | ||
f7c1ad4f | 1272 | FOREACH_INOTIFY_EVENT(e, buffer, l) { |
70068602 YW |
1273 | _cleanup_(sd_device_unrefp) sd_device *dev = NULL; |
1274 | const char *devnode; | |
1275 | ||
7fe3324c | 1276 | if (udev_watch_lookup(e->wd, &dev) <= 0) |
70068602 | 1277 | continue; |
912541b0 | 1278 | |
70068602 | 1279 | if (sd_device_get_devname(dev, &devnode) < 0) |
edd32000 | 1280 | continue; |
912541b0 | 1281 | |
7fe3324c | 1282 | log_device_debug(dev, "Inotify event: %x for %s", e->mask, devnode); |
da143134 | 1283 | if (e->mask & IN_CLOSE_WRITE) |
edd32000 | 1284 | synthesize_change(dev); |
da143134 | 1285 | else if (e->mask & IN_IGNORED) |
2024ed61 | 1286 | udev_watch_end(dev); |
912541b0 KS |
1287 | } |
1288 | ||
e82e8fa5 | 1289 | return 1; |
bd284db1 SJR |
1290 | } |
1291 | ||
0561329d | 1292 | static int on_sigterm(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { |
c0c6806b TG |
1293 | Manager *manager = userdata; |
1294 | ||
1295 | assert(manager); | |
1296 | ||
62d43dac | 1297 | manager_exit(manager); |
912541b0 | 1298 | |
e82e8fa5 TG |
1299 | return 1; |
1300 | } | |
912541b0 | 1301 | |
0561329d | 1302 | static int on_sighup(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { |
c0c6806b TG |
1303 | Manager *manager = userdata; |
1304 | ||
1305 | assert(manager); | |
1306 | ||
62d43dac | 1307 | manager_reload(manager); |
912541b0 | 1308 | |
e82e8fa5 TG |
1309 | return 1; |
1310 | } | |
912541b0 | 1311 | |
e82e8fa5 | 1312 | static int on_sigchld(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { |
c0c6806b | 1313 | Manager *manager = userdata; |
0725c4b9 | 1314 | int r; |
c0c6806b TG |
1315 | |
1316 | assert(manager); | |
1317 | ||
e82e8fa5 TG |
1318 | for (;;) { |
1319 | pid_t pid; | |
1320 | int status; | |
1321 | struct worker *worker; | |
d1317d02 | 1322 | |
e82e8fa5 TG |
1323 | pid = waitpid(-1, &status, WNOHANG); |
1324 | if (pid <= 0) | |
f29328d6 | 1325 | break; |
e82e8fa5 | 1326 | |
4a0b58c4 | 1327 | worker = hashmap_get(manager->workers, PID_TO_PTR(pid)); |
e82e8fa5 | 1328 | if (!worker) { |
dc7faf2a | 1329 | log_warning("Worker ["PID_FMT"] is unknown, ignoring", pid); |
f29328d6 | 1330 | continue; |
912541b0 | 1331 | } |
e82e8fa5 TG |
1332 | |
1333 | if (WIFEXITED(status)) { | |
1334 | if (WEXITSTATUS(status) == 0) | |
dc7faf2a | 1335 | log_debug("Worker ["PID_FMT"] exited", pid); |
e82e8fa5 | 1336 | else |
dc7faf2a | 1337 | log_warning("Worker ["PID_FMT"] exited with return code %i", pid, WEXITSTATUS(status)); |
e82e8fa5 | 1338 | } else if (WIFSIGNALED(status)) { |
dc7faf2a | 1339 | log_warning("Worker ["PID_FMT"] terminated by signal %i (%s)", pid, WTERMSIG(status), signal_to_string(WTERMSIG(status))); |
e82e8fa5 | 1340 | } else if (WIFSTOPPED(status)) { |
dc7faf2a | 1341 | log_info("Worker ["PID_FMT"] stopped", pid); |
f29328d6 | 1342 | continue; |
e82e8fa5 | 1343 | } else if (WIFCONTINUED(status)) { |
dc7faf2a | 1344 | log_info("Worker ["PID_FMT"] continued", pid); |
f29328d6 | 1345 | continue; |
e82e8fa5 | 1346 | } else |
dc7faf2a | 1347 | log_warning("Worker ["PID_FMT"] exit with status 0x%04x", pid, status); |
e82e8fa5 | 1348 | |
05e6d9c6 | 1349 | if ((!WIFEXITED(status) || WEXITSTATUS(status) != 0) && worker->event) { |
eb546b35 | 1350 | log_device_error(worker->event->dev, "Worker ["PID_FMT"] failed", pid); |
dc7faf2a | 1351 | |
05e6d9c6 | 1352 | /* delete state from disk */ |
eb546b35 YW |
1353 | device_delete_db(worker->event->dev); |
1354 | device_tag_index(worker->event->dev, NULL, false); | |
dc7faf2a | 1355 | |
05e6d9c6 | 1356 | /* forward kernel event without amending it */ |
eb546b35 | 1357 | r = device_monitor_send_device(manager->monitor, NULL, worker->event->dev_kernel); |
dc7faf2a | 1358 | if (r < 0) |
eb546b35 | 1359 | log_device_error_errno(worker->event->dev_kernel, r, "Failed to send back device to kernel: %m"); |
e82e8fa5 TG |
1360 | } |
1361 | ||
1362 | worker_free(worker); | |
912541b0 | 1363 | } |
e82e8fa5 | 1364 | |
8302fe5a TG |
1365 | /* we can start new workers, try to schedule events */ |
1366 | event_queue_start(manager); | |
1367 | ||
eca195ec | 1368 | /* Disable unnecessary cleanup event */ |
0725c4b9 YW |
1369 | if (hashmap_isempty(manager->workers)) { |
1370 | r = event_source_disable(manager->kill_workers_event); | |
1371 | if (r < 0) | |
1372 | log_warning_errno(r, "Failed to disable event source for cleaning up idle workers, ignoring: %m"); | |
1373 | } | |
eca195ec | 1374 | |
e82e8fa5 | 1375 | return 1; |
f27125f9 | 1376 | } |
1377 | ||
693d371d TG |
1378 | static int on_post(sd_event_source *s, void *userdata) { |
1379 | Manager *manager = userdata; | |
693d371d TG |
1380 | |
1381 | assert(manager); | |
1382 | ||
b6107f01 YW |
1383 | if (!LIST_IS_EMPTY(manager->events)) |
1384 | return 1; | |
1385 | ||
1386 | /* There are no pending events. Let's cleanup idle process. */ | |
1387 | ||
1388 | if (!hashmap_isempty(manager->workers)) { | |
1389 | /* There are idle workers */ | |
6d63048a YW |
1390 | (void) event_reset_time(manager->event, &manager->kill_workers_event, CLOCK_MONOTONIC, |
1391 | now(CLOCK_MONOTONIC) + 3 * USEC_PER_SEC, USEC_PER_SEC, | |
1392 | on_kill_workers_event, manager, 0, "kill-workers-event", false); | |
b6107f01 | 1393 | return 1; |
693d371d TG |
1394 | } |
1395 | ||
b6107f01 YW |
1396 | /* There are no idle workers. */ |
1397 | ||
1398 | if (manager->exit) | |
1399 | return sd_event_exit(manager->event, 0); | |
1400 | ||
1401 | if (manager->cgroup) | |
1402 | /* cleanup possible left-over processes in our cgroup */ | |
1403 | (void) cg_kill(SYSTEMD_CGROUP_CONTROLLER, manager->cgroup, SIGKILL, CGROUP_IGNORE_SELF, NULL, NULL, NULL); | |
1404 | ||
693d371d TG |
1405 | return 1; |
1406 | } | |
1407 | ||
c4b69e99 | 1408 | static int listen_fds(int *ret_ctrl, int *ret_netlink) { |
fcff1e72 | 1409 | int ctrl_fd = -1, netlink_fd = -1; |
c4b69e99 | 1410 | int fd, n; |
912541b0 | 1411 | |
c4b69e99 YW |
1412 | assert(ret_ctrl); |
1413 | assert(ret_netlink); | |
fcff1e72 | 1414 | |
912541b0 | 1415 | n = sd_listen_fds(true); |
fcff1e72 TG |
1416 | if (n < 0) |
1417 | return n; | |
912541b0 KS |
1418 | |
1419 | for (fd = SD_LISTEN_FDS_START; fd < n + SD_LISTEN_FDS_START; fd++) { | |
c52cff07 | 1420 | if (sd_is_socket(fd, AF_LOCAL, SOCK_SEQPACKET, -1) > 0) { |
fcff1e72 TG |
1421 | if (ctrl_fd >= 0) |
1422 | return -EINVAL; | |
1423 | ctrl_fd = fd; | |
912541b0 KS |
1424 | continue; |
1425 | } | |
1426 | ||
c52cff07 | 1427 | if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1) > 0) { |
fcff1e72 TG |
1428 | if (netlink_fd >= 0) |
1429 | return -EINVAL; | |
1430 | netlink_fd = fd; | |
912541b0 KS |
1431 | continue; |
1432 | } | |
1433 | ||
fcff1e72 | 1434 | return -EINVAL; |
912541b0 KS |
1435 | } |
1436 | ||
c4b69e99 YW |
1437 | *ret_ctrl = ctrl_fd; |
1438 | *ret_netlink = netlink_fd; | |
912541b0 | 1439 | |
912541b0 | 1440 | return 0; |
7459bcdc KS |
1441 | } |
1442 | ||
e6f86cac | 1443 | /* |
3f85ef0f | 1444 | * read the kernel command line, in case we need to get into debug mode |
1d84ad94 LP |
1445 | * udev.log_priority=<level> syslog priority |
1446 | * udev.children_max=<number of workers> events are fully serialized if set to 1 | |
1447 | * udev.exec_delay=<number of seconds> delay execution of every executed program | |
1448 | * udev.event_timeout=<number of seconds> seconds to wait before terminating an event | |
e6f86cac | 1449 | */ |
96287a49 | 1450 | static int parse_proc_cmdline_item(const char *key, const char *value, void *data) { |
92e72467 | 1451 | int r = 0; |
e6f86cac | 1452 | |
614a823c | 1453 | assert(key); |
e6f86cac | 1454 | |
614a823c TG |
1455 | if (!value) |
1456 | return 0; | |
e6f86cac | 1457 | |
1d84ad94 LP |
1458 | if (proc_cmdline_key_streq(key, "udev.log_priority")) { |
1459 | ||
1460 | if (proc_cmdline_value_missing(key, value)) | |
1461 | return 0; | |
1462 | ||
46f0fbd8 | 1463 | r = log_level_from_string(value); |
92e72467 ZJS |
1464 | if (r >= 0) |
1465 | log_set_max_level(r); | |
1d84ad94 LP |
1466 | |
1467 | } else if (proc_cmdline_key_streq(key, "udev.event_timeout")) { | |
1468 | ||
1469 | if (proc_cmdline_value_missing(key, value)) | |
1470 | return 0; | |
1471 | ||
9d9264ba | 1472 | r = parse_sec(value, &arg_event_timeout_usec); |
1d84ad94 LP |
1473 | |
1474 | } else if (proc_cmdline_key_streq(key, "udev.children_max")) { | |
1475 | ||
1476 | if (proc_cmdline_value_missing(key, value)) | |
1477 | return 0; | |
1478 | ||
020328e1 | 1479 | r = safe_atou(value, &arg_children_max); |
1d84ad94 LP |
1480 | |
1481 | } else if (proc_cmdline_key_streq(key, "udev.exec_delay")) { | |
1482 | ||
1483 | if (proc_cmdline_value_missing(key, value)) | |
1484 | return 0; | |
1485 | ||
6b92f429 | 1486 | r = parse_sec(value, &arg_exec_delay_usec); |
1d84ad94 LP |
1487 | |
1488 | } else if (startswith(key, "udev.")) | |
044497e2 | 1489 | log_warning("Unknown udev kernel command line option \"%s\", ignoring", key); |
614a823c | 1490 | |
92e72467 ZJS |
1491 | if (r < 0) |
1492 | log_warning_errno(r, "Failed to parse \"%s=%s\", ignoring: %m", key, value); | |
1d84ad94 | 1493 | |
614a823c | 1494 | return 0; |
e6f86cac KS |
1495 | } |
1496 | ||
37ec0fdd LP |
1497 | static int help(void) { |
1498 | _cleanup_free_ char *link = NULL; | |
1499 | int r; | |
1500 | ||
1501 | r = terminal_urlify_man("systemd-udevd.service", "8", &link); | |
1502 | if (r < 0) | |
1503 | return log_oom(); | |
1504 | ||
ed216e1f TG |
1505 | printf("%s [OPTIONS...]\n\n" |
1506 | "Manages devices.\n\n" | |
5ac0162c | 1507 | " -h --help Print this message\n" |
2d19c17e MF |
1508 | " -V --version Print version of the program\n" |
1509 | " -d --daemon Detach and run in the background\n" | |
1510 | " -D --debug Enable debug output\n" | |
1511 | " -c --children-max=INT Set maximum number of workers\n" | |
1512 | " -e --exec-delay=SECONDS Seconds to wait before executing RUN=\n" | |
1513 | " -t --event-timeout=SECONDS Seconds to wait before terminating an event\n" | |
1514 | " -N --resolve-names=early|late|never\n" | |
5ac0162c | 1515 | " When to resolve users and groups\n" |
37ec0fdd LP |
1516 | "\nSee the %s for details.\n" |
1517 | , program_invocation_short_name | |
1518 | , link | |
1519 | ); | |
1520 | ||
1521 | return 0; | |
ed216e1f TG |
1522 | } |
1523 | ||
bba7a484 | 1524 | static int parse_argv(int argc, char *argv[]) { |
912541b0 | 1525 | static const struct option options[] = { |
bba7a484 TG |
1526 | { "daemon", no_argument, NULL, 'd' }, |
1527 | { "debug", no_argument, NULL, 'D' }, | |
1528 | { "children-max", required_argument, NULL, 'c' }, | |
1529 | { "exec-delay", required_argument, NULL, 'e' }, | |
1530 | { "event-timeout", required_argument, NULL, 't' }, | |
1531 | { "resolve-names", required_argument, NULL, 'N' }, | |
1532 | { "help", no_argument, NULL, 'h' }, | |
1533 | { "version", no_argument, NULL, 'V' }, | |
912541b0 KS |
1534 | {} |
1535 | }; | |
689a97f5 | 1536 | |
044497e2 | 1537 | int c, r; |
689a97f5 | 1538 | |
bba7a484 TG |
1539 | assert(argc >= 0); |
1540 | assert(argv); | |
912541b0 | 1541 | |
e14b6f21 | 1542 | while ((c = getopt_long(argc, argv, "c:de:Dt:N:hV", options, NULL)) >= 0) { |
bba7a484 | 1543 | switch (c) { |
912541b0 | 1544 | |
912541b0 | 1545 | case 'd': |
bba7a484 | 1546 | arg_daemonize = true; |
912541b0 KS |
1547 | break; |
1548 | case 'c': | |
020328e1 | 1549 | r = safe_atou(optarg, &arg_children_max); |
6f5cf8a8 | 1550 | if (r < 0) |
389f9bf2 | 1551 | log_warning_errno(r, "Failed to parse --children-max= value '%s', ignoring: %m", optarg); |
912541b0 KS |
1552 | break; |
1553 | case 'e': | |
6b92f429 | 1554 | r = parse_sec(optarg, &arg_exec_delay_usec); |
6f5cf8a8 | 1555 | if (r < 0) |
6b92f429 | 1556 | log_warning_errno(r, "Failed to parse --exec-delay= value '%s', ignoring: %m", optarg); |
912541b0 | 1557 | break; |
9719859c | 1558 | case 't': |
9d9264ba | 1559 | r = parse_sec(optarg, &arg_event_timeout_usec); |
f1e8664e | 1560 | if (r < 0) |
9d9264ba | 1561 | log_warning_errno(r, "Failed to parse --event-timeout= value '%s', ignoring: %m", optarg); |
9719859c | 1562 | break; |
912541b0 | 1563 | case 'D': |
bba7a484 | 1564 | arg_debug = true; |
912541b0 | 1565 | break; |
c4d44cba YW |
1566 | case 'N': { |
1567 | ResolveNameTiming t; | |
1568 | ||
1569 | t = resolve_name_timing_from_string(optarg); | |
1570 | if (t < 0) | |
1571 | log_warning("Invalid --resolve-names= value '%s', ignoring.", optarg); | |
1572 | else | |
1573 | arg_resolve_name_timing = t; | |
912541b0 | 1574 | break; |
c4d44cba | 1575 | } |
912541b0 | 1576 | case 'h': |
37ec0fdd | 1577 | return help(); |
912541b0 | 1578 | case 'V': |
681bd2c5 | 1579 | printf("%s\n", GIT_VERSION); |
bba7a484 TG |
1580 | return 0; |
1581 | case '?': | |
1582 | return -EINVAL; | |
912541b0 | 1583 | default: |
bba7a484 TG |
1584 | assert_not_reached("Unhandled option"); |
1585 | ||
912541b0 KS |
1586 | } |
1587 | } | |
1588 | ||
bba7a484 TG |
1589 | return 1; |
1590 | } | |
1591 | ||
b7f74dd4 | 1592 | static int manager_new(Manager **ret, int fd_ctrl, int fd_uevent, const char *cgroup) { |
c0c6806b | 1593 | _cleanup_(manager_freep) Manager *manager = NULL; |
b5af8c8c | 1594 | int r; |
c0c6806b TG |
1595 | |
1596 | assert(ret); | |
1597 | ||
6f19b42f | 1598 | manager = new(Manager, 1); |
c0c6806b TG |
1599 | if (!manager) |
1600 | return log_oom(); | |
1601 | ||
6f19b42f YW |
1602 | *manager = (Manager) { |
1603 | .fd_inotify = -1, | |
1604 | .worker_watch = { -1, -1 }, | |
1605 | .cgroup = cgroup, | |
1606 | }; | |
e237d8cb | 1607 | |
2024ed61 | 1608 | manager->ctrl = udev_ctrl_new_from_fd(fd_ctrl); |
f59118ec | 1609 | if (!manager->ctrl) |
886cf317 | 1610 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to initialize udev control socket"); |
e237d8cb | 1611 | |
b5af8c8c ZJS |
1612 | r = udev_ctrl_enable_receiving(manager->ctrl); |
1613 | if (r < 0) | |
1614 | return log_error_errno(r, "Failed to bind udev control socket: %m"); | |
c4b69e99 | 1615 | |
7f2e3a14 YW |
1616 | r = device_monitor_new_full(&manager->monitor, MONITOR_GROUP_KERNEL, fd_uevent); |
1617 | if (r < 0) | |
1618 | return log_error_errno(r, "Failed to initialize device monitor: %m"); | |
e237d8cb | 1619 | |
90389323 | 1620 | (void) sd_device_monitor_set_receive_buffer_size(manager->monitor, 128 * 1024 * 1024); |
c4b69e99 | 1621 | |
b5af8c8c ZJS |
1622 | r = device_monitor_enable_receiving(manager->monitor); |
1623 | if (r < 0) | |
1624 | return log_error_errno(r, "Failed to bind netlink socket: %m"); | |
1625 | ||
1626 | *ret = TAKE_PTR(manager); | |
1627 | ||
1628 | return 0; | |
1629 | } | |
1630 | ||
1631 | static int main_loop(Manager *manager) { | |
1632 | int fd_worker, fd_ctrl, r; | |
1633 | ||
e237d8cb TG |
1634 | /* unnamed socket from workers to the main daemon */ |
1635 | r = socketpair(AF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0, manager->worker_watch); | |
1636 | if (r < 0) | |
044497e2 | 1637 | return log_error_errno(errno, "Failed to create socketpair for communicating with workers: %m"); |
e237d8cb | 1638 | |
693d371d | 1639 | fd_worker = manager->worker_watch[READ_END]; |
e237d8cb | 1640 | |
2ff48e98 | 1641 | r = setsockopt_int(fd_worker, SOL_SOCKET, SO_PASSCRED, true); |
e237d8cb | 1642 | if (r < 0) |
044497e2 | 1643 | return log_error_errno(r, "Failed to enable SO_PASSCRED: %m"); |
e237d8cb | 1644 | |
b7759e04 YW |
1645 | r = udev_watch_init(); |
1646 | if (r < 0) | |
1647 | return log_error_errno(r, "Failed to create inotify descriptor: %m"); | |
1648 | manager->fd_inotify = r; | |
e237d8cb | 1649 | |
2024ed61 | 1650 | udev_watch_restore(); |
e237d8cb TG |
1651 | |
1652 | /* block and listen to all signals on signalfd */ | |
72c0a2c2 | 1653 | assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, SIGHUP, SIGCHLD, -1) >= 0); |
693d371d | 1654 | |
49f997f3 TG |
1655 | r = sd_event_default(&manager->event); |
1656 | if (r < 0) | |
044497e2 | 1657 | return log_error_errno(r, "Failed to allocate event loop: %m"); |
49f997f3 | 1658 | |
693d371d TG |
1659 | r = sd_event_add_signal(manager->event, NULL, SIGINT, on_sigterm, manager); |
1660 | if (r < 0) | |
044497e2 | 1661 | return log_error_errno(r, "Failed to create SIGINT event source: %m"); |
693d371d TG |
1662 | |
1663 | r = sd_event_add_signal(manager->event, NULL, SIGTERM, on_sigterm, manager); | |
1664 | if (r < 0) | |
044497e2 | 1665 | return log_error_errno(r, "Failed to create SIGTERM event source: %m"); |
693d371d TG |
1666 | |
1667 | r = sd_event_add_signal(manager->event, NULL, SIGHUP, on_sighup, manager); | |
1668 | if (r < 0) | |
044497e2 | 1669 | return log_error_errno(r, "Failed to create SIGHUP event source: %m"); |
693d371d TG |
1670 | |
1671 | r = sd_event_add_signal(manager->event, NULL, SIGCHLD, on_sigchld, manager); | |
1672 | if (r < 0) | |
044497e2 | 1673 | return log_error_errno(r, "Failed to create SIGCHLD event source: %m"); |
693d371d TG |
1674 | |
1675 | r = sd_event_set_watchdog(manager->event, true); | |
1676 | if (r < 0) | |
044497e2 | 1677 | return log_error_errno(r, "Failed to create watchdog event source: %m"); |
693d371d | 1678 | |
b5af8c8c ZJS |
1679 | fd_ctrl = udev_ctrl_get_fd(manager->ctrl); |
1680 | if (fd_ctrl < 0) | |
1681 | return log_error_errno(fd_ctrl, "Failed to get udev control socket fd: %m"); | |
1682 | ||
11b1dd8c | 1683 | r = sd_event_add_io(manager->event, &manager->ctrl_event, fd_ctrl, EPOLLIN, on_ctrl_msg, manager); |
693d371d | 1684 | if (r < 0) |
044497e2 | 1685 | return log_error_errno(r, "Failed to create udev control event source: %m"); |
693d371d TG |
1686 | |
1687 | /* This needs to be after the inotify and uevent handling, to make sure | |
1688 | * that the ping is send back after fully processing the pending uevents | |
1689 | * (including the synthetic ones we may create due to inotify events). | |
1690 | */ | |
1691 | r = sd_event_source_set_priority(manager->ctrl_event, SD_EVENT_PRIORITY_IDLE); | |
1692 | if (r < 0) | |
044497e2 | 1693 | return log_error_errno(r, "Failed to set IDLE event priority for udev control event source: %m"); |
693d371d TG |
1694 | |
1695 | r = sd_event_add_io(manager->event, &manager->inotify_event, manager->fd_inotify, EPOLLIN, on_inotify, manager); | |
1696 | if (r < 0) | |
044497e2 | 1697 | return log_error_errno(r, "Failed to create inotify event source: %m"); |
693d371d | 1698 | |
f00d2b6d YW |
1699 | r = sd_device_monitor_attach_event(manager->monitor, manager->event); |
1700 | if (r < 0) | |
1701 | return log_error_errno(r, "Failed to attach event to device monitor: %m"); | |
1702 | ||
1703 | r = sd_device_monitor_start(manager->monitor, on_uevent, manager); | |
693d371d | 1704 | if (r < 0) |
f00d2b6d YW |
1705 | return log_error_errno(r, "Failed to start device monitor: %m"); |
1706 | ||
1707 | (void) sd_event_source_set_description(sd_device_monitor_get_event_source(manager->monitor), "device-monitor"); | |
693d371d TG |
1708 | |
1709 | r = sd_event_add_io(manager->event, NULL, fd_worker, EPOLLIN, on_worker, manager); | |
1710 | if (r < 0) | |
044497e2 | 1711 | return log_error_errno(r, "Failed to create worker event source: %m"); |
693d371d TG |
1712 | |
1713 | r = sd_event_add_post(manager->event, NULL, on_post, manager); | |
1714 | if (r < 0) | |
044497e2 | 1715 | return log_error_errno(r, "Failed to create post event source: %m"); |
e237d8cb | 1716 | |
b5af8c8c | 1717 | udev_builtin_init(); |
077fc5e2 | 1718 | |
b5af8c8c ZJS |
1719 | r = udev_rules_new(&manager->rules, arg_resolve_name_timing); |
1720 | if (!manager->rules) | |
1721 | return log_error_errno(r, "Failed to read udev rules: %m"); | |
077fc5e2 DH |
1722 | |
1723 | r = udev_rules_apply_static_dev_perms(manager->rules); | |
1724 | if (r < 0) | |
044497e2 | 1725 | log_error_errno(r, "Failed to apply permissions on static device nodes: %m"); |
077fc5e2 | 1726 | |
1ef72b55 MS |
1727 | (void) sd_notifyf(false, |
1728 | "READY=1\n" | |
1729 | "STATUS=Processing with %u children at max", arg_children_max); | |
077fc5e2 DH |
1730 | |
1731 | r = sd_event_loop(manager->event); | |
44dcf454 | 1732 | if (r < 0) |
044497e2 | 1733 | log_error_errno(r, "Event loop failed: %m"); |
077fc5e2 | 1734 | |
077fc5e2 DH |
1735 | sd_notify(false, |
1736 | "STOPPING=1\n" | |
1737 | "STATUS=Shutting down..."); | |
077fc5e2 DH |
1738 | return r; |
1739 | } | |
1740 | ||
0c5a109a | 1741 | static int run(int argc, char *argv[]) { |
c26d1879 | 1742 | _cleanup_free_ char *cgroup = NULL; |
b5af8c8c | 1743 | _cleanup_(manager_freep) Manager *manager = NULL; |
efa1606e | 1744 | int fd_ctrl = -1, fd_uevent = -1; |
e5d7bce1 | 1745 | int r; |
bba7a484 | 1746 | |
bba7a484 | 1747 | log_set_target(LOG_TARGET_AUTO); |
a14e7af1 | 1748 | udev_parse_config_full(&arg_children_max, &arg_exec_delay_usec, &arg_event_timeout_usec, &arg_resolve_name_timing); |
bba7a484 TG |
1749 | log_parse_environment(); |
1750 | log_open(); | |
1751 | ||
bba7a484 TG |
1752 | r = parse_argv(argc, argv); |
1753 | if (r <= 0) | |
0c5a109a | 1754 | return r; |
bba7a484 | 1755 | |
1d84ad94 | 1756 | r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX); |
614a823c | 1757 | if (r < 0) |
044497e2 | 1758 | log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m"); |
912541b0 | 1759 | |
78d3e041 KS |
1760 | if (arg_debug) { |
1761 | log_set_target(LOG_TARGET_CONSOLE); | |
bba7a484 | 1762 | log_set_max_level(LOG_DEBUG); |
78d3e041 | 1763 | } |
bba7a484 | 1764 | |
6174a243 YW |
1765 | log_set_max_level_realm(LOG_REALM_SYSTEMD, log_get_max_level()); |
1766 | ||
fba868fa LP |
1767 | r = must_be_root(); |
1768 | if (r < 0) | |
0c5a109a | 1769 | return r; |
912541b0 | 1770 | |
712cebf1 TG |
1771 | if (arg_children_max == 0) { |
1772 | cpu_set_t cpu_set; | |
e438c57a | 1773 | unsigned long mem_limit; |
ebc164ef | 1774 | |
712cebf1 | 1775 | arg_children_max = 8; |
d457ff83 | 1776 | |
ece174c5 | 1777 | if (sched_getaffinity(0, sizeof(cpu_set), &cpu_set) == 0) |
faae64fa | 1778 | arg_children_max += CPU_COUNT(&cpu_set) * 8; |
912541b0 | 1779 | |
e438c57a MW |
1780 | mem_limit = physical_memory() / (128LU*1024*1024); |
1781 | arg_children_max = MAX(10U, MIN(arg_children_max, mem_limit)); | |
1782 | ||
044497e2 | 1783 | log_debug("Set children_max to %u", arg_children_max); |
d457ff83 | 1784 | } |
912541b0 | 1785 | |
712cebf1 TG |
1786 | /* set umask before creating any file/directory */ |
1787 | r = chdir("/"); | |
0c5a109a ZJS |
1788 | if (r < 0) |
1789 | return log_error_errno(errno, "Failed to change dir to '/': %m"); | |
194bbe33 | 1790 | |
712cebf1 | 1791 | umask(022); |
912541b0 | 1792 | |
c3dacc8b | 1793 | r = mac_selinux_init(); |
0c5a109a ZJS |
1794 | if (r < 0) |
1795 | return log_error_errno(r, "Could not initialize labelling: %m"); | |
912541b0 | 1796 | |
dae8b82e | 1797 | r = mkdir_errno_wrapper("/run/udev", 0755); |
0c5a109a ZJS |
1798 | if (r < 0 && r != -EEXIST) |
1799 | return log_error_errno(r, "Failed to create /run/udev: %m"); | |
712cebf1 | 1800 | |
03cfe0d5 | 1801 | dev_setup(NULL, UID_INVALID, GID_INVALID); |
912541b0 | 1802 | |
c26d1879 TG |
1803 | if (getppid() == 1) { |
1804 | /* get our own cgroup, we regularly kill everything udev has left behind | |
1805 | we only do this on systemd systems, and only if we are directly spawned | |
1806 | by PID1. otherwise we are not guaranteed to have a dedicated cgroup */ | |
1807 | r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 0, &cgroup); | |
11b9fb15 | 1808 | if (r < 0) { |
a2d61f07 | 1809 | if (IN_SET(r, -ENOENT, -ENOMEDIUM)) |
044497e2 | 1810 | log_debug_errno(r, "Dedicated cgroup not found: %m"); |
11b9fb15 | 1811 | else |
044497e2 | 1812 | log_warning_errno(r, "Failed to get cgroup: %m"); |
11b9fb15 | 1813 | } |
c26d1879 TG |
1814 | } |
1815 | ||
b7f74dd4 | 1816 | r = listen_fds(&fd_ctrl, &fd_uevent); |
0c5a109a ZJS |
1817 | if (r < 0) |
1818 | return log_error_errno(r, "Failed to listen on fds: %m"); | |
b7f74dd4 | 1819 | |
b5af8c8c ZJS |
1820 | r = manager_new(&manager, fd_ctrl, fd_uevent, cgroup); |
1821 | if (r < 0) | |
1822 | return log_error_errno(r, "Failed to create manager: %m"); | |
1823 | ||
bba7a484 | 1824 | if (arg_daemonize) { |
912541b0 | 1825 | pid_t pid; |
912541b0 | 1826 | |
b5af8c8c | 1827 | log_info("Starting version " GIT_VERSION); |
3cbb2057 | 1828 | |
40e749b5 | 1829 | /* connect /dev/null to stdin, stdout, stderr */ |
c76cf844 AK |
1830 | if (log_get_max_level() < LOG_DEBUG) { |
1831 | r = make_null_stdio(); | |
1832 | if (r < 0) | |
1833 | log_warning_errno(r, "Failed to redirect standard streams to /dev/null: %m"); | |
1834 | } | |
1835 | ||
912541b0 | 1836 | pid = fork(); |
0c5a109a ZJS |
1837 | if (pid < 0) |
1838 | return log_error_errno(errno, "Failed to fork daemon: %m"); | |
1839 | if (pid > 0) | |
1840 | /* parent */ | |
1841 | return 0; | |
912541b0 | 1842 | |
0c5a109a | 1843 | /* child */ |
ece0fe12 | 1844 | (void) setsid(); |
912541b0 | 1845 | |
76cdddfb YW |
1846 | r = set_oom_score_adjust(-1000); |
1847 | if (r < 0) | |
1848 | log_debug_errno(r, "Failed to adjust OOM score, ignoring: %m"); | |
7500cd5e | 1849 | } |
912541b0 | 1850 | |
b5af8c8c ZJS |
1851 | r = main_loop(manager); |
1852 | /* FIXME: move this into manager_free() */ | |
1853 | udev_ctrl_cleanup(manager->ctrl); | |
1854 | return r; | |
7fafc032 | 1855 | } |
0c5a109a ZJS |
1856 | |
1857 | DEFINE_MAIN_FUNCTION(run); |