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