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