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