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