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