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