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