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