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