]>
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" |
392ef7a2 | 51 | #include "sd-daemon.h" |
194bbe33 | 52 | #include "cgroup-util.h" |
5ba2dc25 | 53 | #include "dev-setup.h" |
a5c32cff | 54 | #include "fileio.h" |
7fafc032 | 55 | |
c3804728 | 56 | static bool debug; |
9e8fe79b | 57 | |
80df994c | 58 | void udev_main_log(struct udev *udev, int priority, |
912541b0 | 59 | const char *file, int line, const char *fn, |
9ec6e95b | 60 | const char *format, va_list args) { |
baa30fbc | 61 | log_metav(priority, file, line, fn, format, args); |
7d563a17 KS |
62 | } |
63 | ||
d7ddce18 | 64 | static struct udev_rules *rules; |
d59f11e1 | 65 | static struct udev_ctrl *udev_ctrl; |
1e03b754 | 66 | static struct udev_monitor *monitor; |
3c6ee190 | 67 | static int worker_watch[2] = { -1, -1 }; |
ff2c503d KS |
68 | static int fd_signal = -1; |
69 | static int fd_ep = -1; | |
70 | static int fd_inotify = -1; | |
c3804728 | 71 | static bool stop_exec_queue; |
7c85d636 | 72 | static bool reload; |
87d55ff6 KS |
73 | static int children; |
74 | static int children_max; | |
c830e98d | 75 | static int exec_delay; |
dd5eddd2 | 76 | static usec_t event_timeout_usec = 30 * USEC_PER_SEC; |
2181d30a | 77 | static sigset_t sigmask_orig; |
ff2c503d KS |
78 | static UDEV_LIST(event_list); |
79 | static UDEV_LIST(worker_list); | |
e3dc56a2 | 80 | static char *udev_cgroup; |
c3804728 | 81 | static bool udev_exit; |
1e03b754 | 82 | |
1e03b754 | 83 | enum event_state { |
912541b0 KS |
84 | EVENT_UNDEF, |
85 | EVENT_QUEUED, | |
86 | EVENT_RUNNING, | |
1e03b754 KS |
87 | }; |
88 | ||
89 | struct event { | |
912541b0 KS |
90 | struct udev_list_node node; |
91 | struct udev *udev; | |
92 | struct udev_device *dev; | |
93 | enum event_state state; | |
94 | int exitcode; | |
95 | unsigned long long int delaying_seqnum; | |
96 | unsigned long long int seqnum; | |
97 | const char *devpath; | |
98 | size_t devpath_len; | |
99 | const char *devpath_old; | |
100 | dev_t devnum; | |
912541b0 | 101 | int ifindex; |
ea6039a3 | 102 | bool is_block; |
d8d4bee7 | 103 | #ifdef HAVE_FIRMWARE |
ea6039a3 | 104 | bool nodelay; |
61e536e1 | 105 | #endif |
1e03b754 KS |
106 | }; |
107 | ||
9ec6e95b | 108 | static inline struct event *node_to_event(struct udev_list_node *node) { |
b27ee00b | 109 | return container_of(node, struct event, node); |
1e03b754 KS |
110 | } |
111 | ||
ff2c503d KS |
112 | static void event_queue_cleanup(struct udev *udev, enum event_state type); |
113 | ||
1e03b754 | 114 | enum worker_state { |
912541b0 KS |
115 | WORKER_UNDEF, |
116 | WORKER_RUNNING, | |
117 | WORKER_IDLE, | |
118 | WORKER_KILLED, | |
1e03b754 KS |
119 | }; |
120 | ||
121 | struct worker { | |
912541b0 KS |
122 | struct udev_list_node node; |
123 | struct udev *udev; | |
124 | int refcount; | |
125 | pid_t pid; | |
126 | struct udev_monitor *monitor; | |
127 | enum worker_state state; | |
128 | struct event *event; | |
40fe8b11 | 129 | usec_t event_start_usec; |
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; | |
206 | struct epoll_event ep_signal, ep_monitor; | |
207 | sigset_t mask; | |
208 | int rc = EXIT_SUCCESS; | |
209 | ||
43095991 | 210 | /* take initial device from queue */ |
912541b0 KS |
211 | dev = event->dev; |
212 | event->dev = NULL; | |
213 | ||
214 | free(worker); | |
215 | worker_list_cleanup(udev); | |
216 | event_queue_cleanup(udev, EVENT_UNDEF); | |
912541b0 KS |
217 | udev_monitor_unref(monitor); |
218 | udev_ctrl_unref(udev_ctrl); | |
219 | close(fd_signal); | |
220 | close(fd_ep); | |
221 | close(worker_watch[READ_END]); | |
222 | ||
223 | sigfillset(&mask); | |
224 | fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC); | |
225 | if (fd_signal < 0) { | |
9f6445e3 | 226 | log_error("error creating signalfd %m"); |
912541b0 KS |
227 | rc = 2; |
228 | goto out; | |
229 | } | |
230 | ||
231 | fd_ep = epoll_create1(EPOLL_CLOEXEC); | |
232 | if (fd_ep < 0) { | |
9f6445e3 | 233 | log_error("error creating epoll fd: %m"); |
912541b0 KS |
234 | rc = 3; |
235 | goto out; | |
236 | } | |
237 | ||
29804cc1 | 238 | memzero(&ep_signal, sizeof(struct epoll_event)); |
912541b0 KS |
239 | ep_signal.events = EPOLLIN; |
240 | ep_signal.data.fd = fd_signal; | |
241 | ||
242 | fd_monitor = udev_monitor_get_fd(worker_monitor); | |
29804cc1 | 243 | memzero(&ep_monitor, sizeof(struct epoll_event)); |
912541b0 KS |
244 | ep_monitor.events = EPOLLIN; |
245 | ep_monitor.data.fd = fd_monitor; | |
246 | ||
247 | if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_signal, &ep_signal) < 0 || | |
248 | epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_monitor, &ep_monitor) < 0) { | |
9f6445e3 | 249 | log_error("fail to add fds to epoll: %m"); |
912541b0 KS |
250 | rc = 4; |
251 | goto out; | |
252 | } | |
253 | ||
254 | /* request TERM signal if parent exits */ | |
255 | prctl(PR_SET_PDEATHSIG, SIGTERM); | |
256 | ||
145dae7e | 257 | /* reset OOM score, we only protect the main daemon */ |
574d5f2d | 258 | write_string_file("/proc/self/oom_score_adj", "0"); |
145dae7e | 259 | |
912541b0 KS |
260 | for (;;) { |
261 | struct udev_event *udev_event; | |
262 | struct worker_message msg; | |
3ebdb81e | 263 | int fd_lock = -1; |
bf9bead1 | 264 | int err = 0; |
912541b0 | 265 | |
9f6445e3 | 266 | log_debug("seq %llu running", udev_device_get_seqnum(dev)); |
912541b0 KS |
267 | udev_event = udev_event_new(dev); |
268 | if (udev_event == NULL) { | |
269 | rc = 5; | |
270 | goto out; | |
271 | } | |
272 | ||
273 | /* needed for SIGCHLD/SIGTERM in spawn() */ | |
274 | udev_event->fd_signal = fd_signal; | |
275 | ||
276 | if (exec_delay > 0) | |
277 | udev_event->exec_delay = exec_delay; | |
278 | ||
3ebdb81e | 279 | /* |
2e5b17d0 | 280 | * Take a shared lock on the device node; this establishes |
3ebdb81e | 281 | * a concept of device "ownership" to serialize device |
2e5b17d0 | 282 | * access. External processes holding an exclusive lock will |
3ebdb81e | 283 | * cause udev to skip the event handling; in the case udev |
2e5b17d0 | 284 | * acquired the lock, the external process can block until |
3ebdb81e KS |
285 | * udev has finished its event handling. |
286 | */ | |
2e5b17d0 KS |
287 | if (!streq_ptr(udev_device_get_action(dev), "remove") && |
288 | streq_ptr("block", udev_device_get_subsystem(dev)) && | |
289 | !startswith(udev_device_get_sysname(dev), "dm-") && | |
290 | !startswith(udev_device_get_sysname(dev), "md")) { | |
3ebdb81e KS |
291 | struct udev_device *d = dev; |
292 | ||
293 | if (streq_ptr("partition", udev_device_get_devtype(d))) | |
294 | d = udev_device_get_parent(d); | |
295 | ||
296 | if (d) { | |
297 | fd_lock = open(udev_device_get_devnode(d), O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK); | |
298 | if (fd_lock >= 0 && flock(fd_lock, LOCK_SH|LOCK_NB) < 0) { | |
299 | log_debug("Unable to flock(%s), skipping event handling: %m", udev_device_get_devnode(d)); | |
300 | err = -EWOULDBLOCK; | |
3d06f418 | 301 | fd_lock = safe_close(fd_lock); |
3ebdb81e KS |
302 | goto skip; |
303 | } | |
304 | } | |
305 | } | |
306 | ||
912541b0 | 307 | /* apply rules, create node, symlinks */ |
dd5eddd2 | 308 | udev_event_execute_rules(udev_event, event_timeout_usec, rules, &sigmask_orig); |
912541b0 | 309 | |
dd5eddd2 | 310 | udev_event_execute_run(udev_event, event_timeout_usec, &sigmask_orig); |
912541b0 KS |
311 | |
312 | /* apply/restore inotify watch */ | |
bf9bead1 | 313 | if (udev_event->inotify_watch) { |
912541b0 KS |
314 | udev_watch_begin(udev, dev); |
315 | udev_device_update_db(dev); | |
316 | } | |
317 | ||
3d06f418 | 318 | safe_close(fd_lock); |
3ebdb81e | 319 | |
912541b0 KS |
320 | /* send processed event back to libudev listeners */ |
321 | udev_monitor_send_device(worker_monitor, NULL, dev); | |
322 | ||
3ebdb81e | 323 | skip: |
912541b0 | 324 | /* send udevd the result of the event execution */ |
29804cc1 | 325 | memzero(&msg, sizeof(struct worker_message)); |
3ebdb81e | 326 | msg.exitcode = err; |
912541b0 KS |
327 | msg.pid = getpid(); |
328 | send(worker_watch[WRITE_END], &msg, sizeof(struct worker_message), 0); | |
329 | ||
9f6445e3 | 330 | log_debug("seq %llu processed with %i", udev_device_get_seqnum(dev), err); |
912541b0 KS |
331 | |
332 | udev_device_unref(dev); | |
333 | dev = NULL; | |
334 | ||
73814ca2 CW |
335 | if (udev_event->sigterm) { |
336 | udev_event_unref(udev_event); | |
47e737dc | 337 | goto out; |
73814ca2 CW |
338 | } |
339 | ||
340 | udev_event_unref(udev_event); | |
47e737dc | 341 | |
912541b0 KS |
342 | /* wait for more device messages from main udevd, or term signal */ |
343 | while (dev == NULL) { | |
344 | struct epoll_event ev[4]; | |
345 | int fdcount; | |
346 | int i; | |
347 | ||
8fef0ff2 | 348 | fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), -1); |
912541b0 KS |
349 | if (fdcount < 0) { |
350 | if (errno == EINTR) | |
351 | continue; | |
9f6445e3 | 352 | log_error("failed to poll: %m"); |
912541b0 KS |
353 | goto out; |
354 | } | |
355 | ||
356 | for (i = 0; i < fdcount; i++) { | |
357 | if (ev[i].data.fd == fd_monitor && ev[i].events & EPOLLIN) { | |
358 | dev = udev_monitor_receive_device(worker_monitor); | |
359 | break; | |
360 | } else if (ev[i].data.fd == fd_signal && ev[i].events & EPOLLIN) { | |
361 | struct signalfd_siginfo fdsi; | |
362 | ssize_t size; | |
363 | ||
364 | size = read(fd_signal, &fdsi, sizeof(struct signalfd_siginfo)); | |
365 | if (size != sizeof(struct signalfd_siginfo)) | |
366 | continue; | |
367 | switch (fdsi.ssi_signo) { | |
368 | case SIGTERM: | |
369 | goto out; | |
370 | } | |
371 | } | |
372 | } | |
373 | } | |
374 | } | |
82063a88 | 375 | out: |
912541b0 | 376 | udev_device_unref(dev); |
3d06f418 KS |
377 | safe_close(fd_signal); |
378 | safe_close(fd_ep); | |
912541b0 KS |
379 | close(fd_inotify); |
380 | close(worker_watch[WRITE_END]); | |
381 | udev_rules_unref(rules); | |
e64fae55 | 382 | udev_builtin_exit(udev); |
912541b0 KS |
383 | udev_monitor_unref(worker_monitor); |
384 | udev_unref(udev); | |
baa30fbc | 385 | log_close(); |
912541b0 KS |
386 | exit(rc); |
387 | } | |
388 | case -1: | |
389 | udev_monitor_unref(worker_monitor); | |
390 | event->state = EVENT_QUEUED; | |
391 | free(worker); | |
9f6445e3 | 392 | log_error("fork of child failed: %m"); |
912541b0 KS |
393 | break; |
394 | default: | |
395 | /* close monitor, but keep address around */ | |
396 | udev_monitor_disconnect(worker_monitor); | |
397 | worker->monitor = worker_monitor; | |
398 | worker->pid = pid; | |
399 | worker->state = WORKER_RUNNING; | |
40fe8b11 | 400 | worker->event_start_usec = now(CLOCK_MONOTONIC); |
912541b0 KS |
401 | worker->event = event; |
402 | event->state = EVENT_RUNNING; | |
403 | udev_list_node_append(&worker->node, &worker_list); | |
404 | children++; | |
9f6445e3 | 405 | log_debug("seq %llu forked new worker [%u]", udev_device_get_seqnum(event->dev), pid); |
912541b0 KS |
406 | break; |
407 | } | |
7fafc032 KS |
408 | } |
409 | ||
9ec6e95b | 410 | static void event_run(struct event *event) { |
912541b0 KS |
411 | struct udev_list_node *loop; |
412 | ||
413 | udev_list_node_foreach(loop, &worker_list) { | |
414 | struct worker *worker = node_to_worker(loop); | |
415 | ssize_t count; | |
416 | ||
417 | if (worker->state != WORKER_IDLE) | |
418 | continue; | |
419 | ||
420 | count = udev_monitor_send_device(monitor, worker->monitor, event->dev); | |
421 | if (count < 0) { | |
9f6445e3 | 422 | log_error("worker [%u] did not accept message %zi (%m), kill it", worker->pid, count); |
912541b0 KS |
423 | kill(worker->pid, SIGKILL); |
424 | worker->state = WORKER_KILLED; | |
425 | continue; | |
426 | } | |
427 | worker_ref(worker); | |
428 | worker->event = event; | |
429 | worker->state = WORKER_RUNNING; | |
40fe8b11 | 430 | worker->event_start_usec = now(CLOCK_MONOTONIC); |
912541b0 KS |
431 | event->state = EVENT_RUNNING; |
432 | return; | |
433 | } | |
434 | ||
435 | if (children >= children_max) { | |
436 | if (children_max > 1) | |
9f6445e3 | 437 | log_debug("maximum number (%i) of children reached", children); |
912541b0 KS |
438 | return; |
439 | } | |
440 | ||
441 | /* start new worker and pass initial device */ | |
442 | worker_new(event); | |
1e03b754 KS |
443 | } |
444 | ||
9ec6e95b | 445 | static int event_queue_insert(struct udev_device *dev) { |
912541b0 KS |
446 | struct event *event; |
447 | ||
955d98c9 | 448 | event = new0(struct event, 1); |
912541b0 KS |
449 | if (event == NULL) |
450 | return -1; | |
451 | ||
452 | event->udev = udev_device_get_udev(dev); | |
453 | event->dev = dev; | |
454 | event->seqnum = udev_device_get_seqnum(dev); | |
455 | event->devpath = udev_device_get_devpath(dev); | |
456 | event->devpath_len = strlen(event->devpath); | |
457 | event->devpath_old = udev_device_get_devpath_old(dev); | |
458 | event->devnum = udev_device_get_devnum(dev); | |
ea6039a3 | 459 | event->is_block = streq("block", udev_device_get_subsystem(dev)); |
912541b0 | 460 | event->ifindex = udev_device_get_ifindex(dev); |
d8d4bee7 | 461 | #ifdef HAVE_FIRMWARE |
ea6039a3 KS |
462 | if (streq(udev_device_get_subsystem(dev), "firmware")) |
463 | event->nodelay = true; | |
61e536e1 | 464 | #endif |
912541b0 | 465 | |
9f6445e3 | 466 | log_debug("seq %llu queued, '%s' '%s'", udev_device_get_seqnum(dev), |
912541b0 KS |
467 | udev_device_get_action(dev), udev_device_get_subsystem(dev)); |
468 | ||
469 | event->state = EVENT_QUEUED; | |
470 | udev_list_node_append(&event->node, &event_list); | |
471 | return 0; | |
fc465079 KS |
472 | } |
473 | ||
9ec6e95b | 474 | static void worker_kill(struct udev *udev) { |
912541b0 | 475 | struct udev_list_node *loop; |
1e03b754 | 476 | |
912541b0 KS |
477 | udev_list_node_foreach(loop, &worker_list) { |
478 | struct worker *worker = node_to_worker(loop); | |
1e03b754 | 479 | |
912541b0 KS |
480 | if (worker->state == WORKER_KILLED) |
481 | continue; | |
1e03b754 | 482 | |
912541b0 KS |
483 | worker->state = WORKER_KILLED; |
484 | kill(worker->pid, SIGTERM); | |
485 | } | |
1e03b754 KS |
486 | } |
487 | ||
e3196993 | 488 | /* lookup event for identical, parent, child device */ |
9ec6e95b | 489 | static bool is_devpath_busy(struct event *event) { |
912541b0 KS |
490 | struct udev_list_node *loop; |
491 | size_t common; | |
492 | ||
493 | /* check if queue contains events we depend on */ | |
494 | udev_list_node_foreach(loop, &event_list) { | |
495 | struct event *loop_event = node_to_event(loop); | |
496 | ||
497 | /* we already found a later event, earlier can not block us, no need to check again */ | |
498 | if (loop_event->seqnum < event->delaying_seqnum) | |
499 | continue; | |
500 | ||
501 | /* event we checked earlier still exists, no need to check again */ | |
502 | if (loop_event->seqnum == event->delaying_seqnum) | |
503 | return true; | |
504 | ||
505 | /* found ourself, no later event can block us */ | |
506 | if (loop_event->seqnum >= event->seqnum) | |
507 | break; | |
508 | ||
509 | /* check major/minor */ | |
510 | if (major(event->devnum) != 0 && event->devnum == loop_event->devnum && event->is_block == loop_event->is_block) | |
511 | return true; | |
512 | ||
513 | /* check network device ifindex */ | |
514 | if (event->ifindex != 0 && event->ifindex == loop_event->ifindex) | |
515 | return true; | |
516 | ||
517 | /* check our old name */ | |
090be865 | 518 | if (event->devpath_old != NULL && streq(loop_event->devpath, event->devpath_old)) { |
912541b0 KS |
519 | event->delaying_seqnum = loop_event->seqnum; |
520 | return true; | |
521 | } | |
522 | ||
523 | /* compare devpath */ | |
524 | common = MIN(loop_event->devpath_len, event->devpath_len); | |
525 | ||
526 | /* one devpath is contained in the other? */ | |
527 | if (memcmp(loop_event->devpath, event->devpath, common) != 0) | |
528 | continue; | |
529 | ||
530 | /* identical device event found */ | |
531 | if (loop_event->devpath_len == event->devpath_len) { | |
532 | /* devices names might have changed/swapped in the meantime */ | |
533 | if (major(event->devnum) != 0 && (event->devnum != loop_event->devnum || event->is_block != loop_event->is_block)) | |
534 | continue; | |
535 | if (event->ifindex != 0 && event->ifindex != loop_event->ifindex) | |
536 | continue; | |
537 | event->delaying_seqnum = loop_event->seqnum; | |
538 | return true; | |
539 | } | |
540 | ||
d8d4bee7 | 541 | #ifdef HAVE_FIRMWARE |
ea6039a3 KS |
542 | /* allow to bypass the dependency tracking */ |
543 | if (event->nodelay) | |
544 | continue; | |
61e536e1 | 545 | #endif |
ea6039a3 | 546 | |
912541b0 KS |
547 | /* parent device event found */ |
548 | if (event->devpath[common] == '/') { | |
549 | event->delaying_seqnum = loop_event->seqnum; | |
550 | return true; | |
551 | } | |
552 | ||
553 | /* child device event found */ | |
554 | if (loop_event->devpath[common] == '/') { | |
555 | event->delaying_seqnum = loop_event->seqnum; | |
556 | return true; | |
557 | } | |
558 | ||
559 | /* no matching device */ | |
560 | continue; | |
561 | } | |
562 | ||
563 | return false; | |
7fafc032 KS |
564 | } |
565 | ||
9ec6e95b | 566 | static void event_queue_start(struct udev *udev) { |
912541b0 | 567 | struct udev_list_node *loop; |
8ab44e3f | 568 | |
912541b0 KS |
569 | udev_list_node_foreach(loop, &event_list) { |
570 | struct event *event = node_to_event(loop); | |
0bc74ea7 | 571 | |
912541b0 KS |
572 | if (event->state != EVENT_QUEUED) |
573 | continue; | |
0bc74ea7 | 574 | |
912541b0 | 575 | /* do not start event if parent or child event is still running */ |
baa30fbc | 576 | if (is_devpath_busy(event)) |
912541b0 | 577 | continue; |
fc465079 | 578 | |
912541b0 KS |
579 | event_run(event); |
580 | } | |
1e03b754 KS |
581 | } |
582 | ||
9ec6e95b | 583 | static void event_queue_cleanup(struct udev *udev, enum event_state match_type) { |
912541b0 | 584 | struct udev_list_node *loop, *tmp; |
ff2c503d | 585 | |
912541b0 KS |
586 | udev_list_node_foreach_safe(loop, tmp, &event_list) { |
587 | struct event *event = node_to_event(loop); | |
ff2c503d | 588 | |
912541b0 KS |
589 | if (match_type != EVENT_UNDEF && match_type != event->state) |
590 | continue; | |
ff2c503d | 591 | |
9ea28c55 | 592 | event_queue_delete(event); |
912541b0 | 593 | } |
ff2c503d KS |
594 | } |
595 | ||
9ec6e95b | 596 | static void worker_returned(int fd_worker) { |
912541b0 KS |
597 | for (;;) { |
598 | struct worker_message msg; | |
599 | ssize_t size; | |
600 | struct udev_list_node *loop; | |
601 | ||
602 | size = recv(fd_worker, &msg, sizeof(struct worker_message), MSG_DONTWAIT); | |
603 | if (size != sizeof(struct worker_message)) | |
604 | break; | |
605 | ||
606 | /* lookup worker who sent the signal */ | |
607 | udev_list_node_foreach(loop, &worker_list) { | |
608 | struct worker *worker = node_to_worker(loop); | |
609 | ||
610 | if (worker->pid != msg.pid) | |
611 | continue; | |
612 | ||
613 | /* worker returned */ | |
e64fae55 KS |
614 | if (worker->event) { |
615 | worker->event->exitcode = msg.exitcode; | |
9ea28c55 | 616 | event_queue_delete(worker->event); |
e64fae55 KS |
617 | worker->event = NULL; |
618 | } | |
912541b0 KS |
619 | if (worker->state != WORKER_KILLED) |
620 | worker->state = WORKER_IDLE; | |
621 | worker_unref(worker); | |
622 | break; | |
623 | } | |
624 | } | |
88f4b648 KS |
625 | } |
626 | ||
3b47c739 | 627 | /* receive the udevd message from userspace */ |
9ec6e95b | 628 | static struct udev_ctrl_connection *handle_ctrl_msg(struct udev_ctrl *uctrl) { |
912541b0 KS |
629 | struct udev *udev = udev_ctrl_get_udev(uctrl); |
630 | struct udev_ctrl_connection *ctrl_conn; | |
631 | struct udev_ctrl_msg *ctrl_msg = NULL; | |
632 | const char *str; | |
633 | int i; | |
634 | ||
635 | ctrl_conn = udev_ctrl_get_connection(uctrl); | |
636 | if (ctrl_conn == NULL) | |
637 | goto out; | |
638 | ||
639 | ctrl_msg = udev_ctrl_receive_msg(ctrl_conn); | |
640 | if (ctrl_msg == NULL) | |
641 | goto out; | |
642 | ||
643 | i = udev_ctrl_get_set_log_level(ctrl_msg); | |
644 | if (i >= 0) { | |
ed14edc0 | 645 | log_debug("udevd message (SET_LOG_LEVEL) received, log_priority=%i", i); |
baa30fbc | 646 | log_set_max_level(i); |
912541b0 | 647 | udev_set_log_priority(udev, 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); |
912541b0 KS |
680 | udev_add_property(udev, key, NULL); |
681 | } else { | |
9f6445e3 | 682 | log_debug("udevd message (ENV) received, set '%s=%s'", key, val); |
912541b0 KS |
683 | udev_add_property(udev, key, val); |
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); |
912541b0 KS |
696 | children_max = i; |
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 KS |
951 | /* |
952 | * read the kernel commandline, in case we need to get into debug mode | |
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 LP |
958 | _cleanup_free_ char *line = NULL; |
959 | char *w, *state; | |
e6f86cac | 960 | size_t l; |
74df0fca | 961 | int r; |
e6f86cac | 962 | |
74df0fca LP |
963 | r = proc_cmdline(&line); |
964 | if (r < 0) | |
965 | log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r)); | |
966 | if (r <= 0) | |
e6f86cac KS |
967 | return; |
968 | ||
969 | FOREACH_WORD_QUOTED(w, l, line, state) { | |
970 | char *s, *opt; | |
971 | ||
972 | s = strndup(w, l); | |
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 | ||
023b9d33 KS |
982 | if (startswith(opt, "udev.log-priority=")) { |
983 | int prio; | |
e6f86cac | 984 | |
023b9d33 KS |
985 | prio = util_log_priority(opt + 18); |
986 | log_set_max_level(prio); | |
987 | udev_set_log_priority(udev, prio); | |
988 | } else if (startswith(opt, "udev.children-max=")) { | |
e6f86cac | 989 | children_max = strtoul(opt + 18, NULL, 0); |
023b9d33 | 990 | } else if (startswith(opt, "udev.exec-delay=")) { |
e6f86cac | 991 | exec_delay = strtoul(opt + 16, NULL, 0); |
9719859c | 992 | } else if (startswith(opt, "udev.event-timeout=")) { |
dd5eddd2 | 993 | event_timeout_usec = strtoul(opt + 16, NULL, 0) * USEC_PER_SEC; |
023b9d33 | 994 | } |
e6f86cac KS |
995 | |
996 | free(s); | |
997 | } | |
e6f86cac KS |
998 | } |
999 | ||
dd5eddd2 | 1000 | int main(int argc, char *argv[]) { |
912541b0 | 1001 | struct udev *udev; |
912541b0 KS |
1002 | sigset_t mask; |
1003 | int daemonize = false; | |
1004 | int resolve_names = 1; | |
1005 | static const struct option options[] = { | |
1006 | { "daemon", no_argument, NULL, 'd' }, | |
1007 | { "debug", no_argument, NULL, 'D' }, | |
1008 | { "children-max", required_argument, NULL, 'c' }, | |
1009 | { "exec-delay", required_argument, NULL, 'e' }, | |
9719859c | 1010 | { "event-timeout", required_argument, NULL, 't' }, |
912541b0 KS |
1011 | { "resolve-names", required_argument, NULL, 'N' }, |
1012 | { "help", no_argument, NULL, 'h' }, | |
1013 | { "version", no_argument, NULL, 'V' }, | |
1014 | {} | |
1015 | }; | |
1016 | int fd_ctrl = -1; | |
1017 | int fd_netlink = -1; | |
1018 | int fd_worker = -1; | |
1019 | struct epoll_event ep_ctrl, ep_inotify, ep_signal, ep_netlink, ep_worker; | |
1020 | struct udev_ctrl_connection *ctrl_conn = NULL; | |
912541b0 KS |
1021 | int rc = 1; |
1022 | ||
1023 | udev = udev_new(); | |
1024 | if (udev == NULL) | |
1025 | goto exit; | |
1026 | ||
4b261568 | 1027 | log_set_target(LOG_TARGET_AUTO); |
baa30fbc | 1028 | log_parse_environment(); |
4b261568 | 1029 | log_open(); |
689a97f5 | 1030 | |
912541b0 | 1031 | udev_set_log_fn(udev, udev_main_log); |
689a97f5 TG |
1032 | log_set_max_level(udev_get_log_priority(udev)); |
1033 | ||
9f6445e3 | 1034 | log_debug("version %s", VERSION); |
0f9963a8 | 1035 | label_init("/dev"); |
912541b0 KS |
1036 | |
1037 | for (;;) { | |
1038 | int option; | |
1039 | ||
5bbbe461 | 1040 | option = getopt_long(argc, argv, "c:de:DtN:hV", options, NULL); |
912541b0 KS |
1041 | if (option == -1) |
1042 | break; | |
1043 | ||
1044 | switch (option) { | |
1045 | case 'd': | |
1046 | daemonize = true; | |
1047 | break; | |
1048 | case 'c': | |
1049 | children_max = strtoul(optarg, NULL, 0); | |
1050 | break; | |
1051 | case 'e': | |
1052 | exec_delay = strtoul(optarg, NULL, 0); | |
1053 | break; | |
9719859c | 1054 | case 't': |
dd5eddd2 | 1055 | event_timeout_usec = strtoul(optarg, NULL, 0) * USEC_PER_SEC; |
9719859c | 1056 | break; |
912541b0 KS |
1057 | case 'D': |
1058 | debug = true; | |
194bbe33 | 1059 | log_set_max_level(LOG_DEBUG); |
7781e063 | 1060 | udev_set_log_priority(udev, LOG_DEBUG); |
912541b0 KS |
1061 | break; |
1062 | case 'N': | |
090be865 | 1063 | if (streq(optarg, "early")) { |
912541b0 | 1064 | resolve_names = 1; |
090be865 | 1065 | } else if (streq(optarg, "late")) { |
912541b0 | 1066 | resolve_names = 0; |
090be865 | 1067 | } else if (streq(optarg, "never")) { |
912541b0 KS |
1068 | resolve_names = -1; |
1069 | } else { | |
1070 | fprintf(stderr, "resolve-names must be early, late or never\n"); | |
9f6445e3 | 1071 | log_error("resolve-names must be early, late or never"); |
912541b0 KS |
1072 | goto exit; |
1073 | } | |
1074 | break; | |
1075 | case 'h': | |
1076 | printf("Usage: udevd OPTIONS\n" | |
1077 | " --daemon\n" | |
1078 | " --debug\n" | |
1079 | " --children-max=<maximum number of workers>\n" | |
1080 | " --exec-delay=<seconds to wait before executing RUN=>\n" | |
dd5eddd2 | 1081 | " --event-timeout=<seconds to wait before terminating an event>\n" |
912541b0 KS |
1082 | " --resolve-names=early|late|never\n" |
1083 | " --version\n" | |
1084 | " --help\n" | |
1085 | "\n"); | |
1086 | goto exit; | |
1087 | case 'V': | |
1088 | printf("%s\n", VERSION); | |
1089 | goto exit; | |
1090 | default: | |
1091 | goto exit; | |
1092 | } | |
1093 | } | |
1094 | ||
e6f86cac | 1095 | kernel_cmdline_options(udev); |
912541b0 KS |
1096 | |
1097 | if (getuid() != 0) { | |
1098 | fprintf(stderr, "root privileges required\n"); | |
9f6445e3 | 1099 | log_error("root privileges required"); |
912541b0 KS |
1100 | goto exit; |
1101 | } | |
1102 | ||
1103 | /* set umask before creating any file/directory */ | |
1104 | chdir("/"); | |
1105 | umask(022); | |
1106 | ||
6ada823a | 1107 | mkdir("/run/udev", 0755); |
912541b0 | 1108 | |
01ed0e23 | 1109 | dev_setup(NULL); |
912541b0 KS |
1110 | |
1111 | /* before opening new files, make sure std{in,out,err} fds are in a sane state */ | |
1112 | if (daemonize) { | |
1113 | int fd; | |
1114 | ||
1115 | fd = open("/dev/null", O_RDWR); | |
1116 | if (fd >= 0) { | |
1117 | if (write(STDOUT_FILENO, 0, 0) < 0) | |
1118 | dup2(fd, STDOUT_FILENO); | |
1119 | if (write(STDERR_FILENO, 0, 0) < 0) | |
1120 | dup2(fd, STDERR_FILENO); | |
1121 | if (fd > STDERR_FILENO) | |
1122 | close(fd); | |
1123 | } else { | |
1124 | fprintf(stderr, "cannot open /dev/null\n"); | |
9f6445e3 | 1125 | log_error("cannot open /dev/null"); |
912541b0 KS |
1126 | } |
1127 | } | |
1128 | ||
1129 | if (systemd_fds(udev, &fd_ctrl, &fd_netlink) >= 0) { | |
c5315881 | 1130 | /* get control and netlink socket from systemd */ |
912541b0 KS |
1131 | udev_ctrl = udev_ctrl_new_from_fd(udev, fd_ctrl); |
1132 | if (udev_ctrl == NULL) { | |
baa30fbc | 1133 | log_error("error taking over udev control socket"); |
912541b0 KS |
1134 | rc = 1; |
1135 | goto exit; | |
1136 | } | |
1137 | ||
1138 | monitor = udev_monitor_new_from_netlink_fd(udev, "kernel", fd_netlink); | |
1139 | if (monitor == NULL) { | |
9f6445e3 | 1140 | log_error("error taking over netlink socket"); |
912541b0 KS |
1141 | rc = 3; |
1142 | goto exit; | |
1143 | } | |
194bbe33 KS |
1144 | |
1145 | /* get our own cgroup, we regularly kill everything udev has left behind */ | |
7027ff61 | 1146 | if (cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 0, &udev_cgroup) < 0) |
194bbe33 | 1147 | udev_cgroup = NULL; |
912541b0 KS |
1148 | } else { |
1149 | /* open control and netlink socket */ | |
1150 | udev_ctrl = udev_ctrl_new(udev); | |
1151 | if (udev_ctrl == NULL) { | |
1152 | fprintf(stderr, "error initializing udev control socket"); | |
baa30fbc | 1153 | log_error("error initializing udev control socket"); |
912541b0 KS |
1154 | rc = 1; |
1155 | goto exit; | |
1156 | } | |
1157 | fd_ctrl = udev_ctrl_get_fd(udev_ctrl); | |
1158 | ||
1159 | monitor = udev_monitor_new_from_netlink(udev, "kernel"); | |
1160 | if (monitor == NULL) { | |
1161 | fprintf(stderr, "error initializing netlink socket\n"); | |
9f6445e3 | 1162 | log_error("error initializing netlink socket"); |
912541b0 KS |
1163 | rc = 3; |
1164 | goto exit; | |
1165 | } | |
1166 | fd_netlink = udev_monitor_get_fd(monitor); | |
1167 | } | |
1168 | ||
1169 | if (udev_monitor_enable_receiving(monitor) < 0) { | |
1170 | fprintf(stderr, "error binding netlink socket\n"); | |
9f6445e3 | 1171 | log_error("error binding netlink socket"); |
912541b0 KS |
1172 | rc = 3; |
1173 | goto exit; | |
1174 | } | |
1175 | ||
1176 | if (udev_ctrl_enable_receiving(udev_ctrl) < 0) { | |
1177 | fprintf(stderr, "error binding udev control socket\n"); | |
9f6445e3 | 1178 | log_error("error binding udev control socket"); |
912541b0 KS |
1179 | rc = 1; |
1180 | goto exit; | |
1181 | } | |
1182 | ||
9ea28c55 | 1183 | udev_monitor_set_receive_buffer_size(monitor, 128 * 1024 * 1024); |
912541b0 KS |
1184 | |
1185 | if (daemonize) { | |
1186 | pid_t pid; | |
912541b0 KS |
1187 | |
1188 | pid = fork(); | |
1189 | switch (pid) { | |
1190 | case 0: | |
1191 | break; | |
1192 | case -1: | |
9f6445e3 | 1193 | log_error("fork of daemon failed: %m"); |
912541b0 KS |
1194 | rc = 4; |
1195 | goto exit; | |
1196 | default: | |
1197 | rc = EXIT_SUCCESS; | |
1198 | goto exit_daemonize; | |
1199 | } | |
1200 | ||
1201 | setsid(); | |
1202 | ||
574d5f2d | 1203 | write_string_file("/proc/self/oom_score_adj", "-1000"); |
912541b0 KS |
1204 | } else { |
1205 | sd_notify(1, "READY=1"); | |
1206 | } | |
1207 | ||
c6eefe36 | 1208 | print_kmsg("starting version " VERSION "\n"); |
912541b0 KS |
1209 | |
1210 | if (!debug) { | |
1211 | int fd; | |
1212 | ||
1213 | fd = open("/dev/null", O_RDWR); | |
1214 | if (fd >= 0) { | |
1215 | dup2(fd, STDIN_FILENO); | |
1216 | dup2(fd, STDOUT_FILENO); | |
1217 | dup2(fd, STDERR_FILENO); | |
1218 | close(fd); | |
1219 | } | |
1220 | } | |
1221 | ||
1222 | fd_inotify = udev_watch_init(udev); | |
1223 | if (fd_inotify < 0) { | |
1224 | fprintf(stderr, "error initializing inotify\n"); | |
9f6445e3 | 1225 | log_error("error initializing inotify"); |
912541b0 KS |
1226 | rc = 4; |
1227 | goto exit; | |
1228 | } | |
1229 | udev_watch_restore(udev); | |
1230 | ||
1231 | /* block and listen to all signals on signalfd */ | |
1232 | sigfillset(&mask); | |
1233 | sigprocmask(SIG_SETMASK, &mask, &sigmask_orig); | |
1234 | fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC); | |
1235 | if (fd_signal < 0) { | |
1236 | fprintf(stderr, "error creating signalfd\n"); | |
9f6445e3 | 1237 | log_error("error creating signalfd"); |
912541b0 KS |
1238 | rc = 5; |
1239 | goto exit; | |
1240 | } | |
1241 | ||
1242 | /* unnamed socket from workers to the main daemon */ | |
1243 | if (socketpair(AF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0, worker_watch) < 0) { | |
1244 | fprintf(stderr, "error creating socketpair\n"); | |
9f6445e3 | 1245 | log_error("error creating socketpair"); |
912541b0 KS |
1246 | rc = 6; |
1247 | goto exit; | |
1248 | } | |
1249 | fd_worker = worker_watch[READ_END]; | |
1250 | ||
1251 | udev_builtin_init(udev); | |
1252 | ||
1253 | rules = udev_rules_new(udev, resolve_names); | |
1254 | if (rules == NULL) { | |
9f6445e3 | 1255 | log_error("error reading rules"); |
912541b0 KS |
1256 | goto exit; |
1257 | } | |
1258 | ||
29804cc1 | 1259 | memzero(&ep_ctrl, sizeof(struct epoll_event)); |
912541b0 KS |
1260 | ep_ctrl.events = EPOLLIN; |
1261 | ep_ctrl.data.fd = fd_ctrl; | |
1262 | ||
29804cc1 | 1263 | memzero(&ep_inotify, sizeof(struct epoll_event)); |
912541b0 KS |
1264 | ep_inotify.events = EPOLLIN; |
1265 | ep_inotify.data.fd = fd_inotify; | |
1266 | ||
29804cc1 | 1267 | memzero(&ep_signal, sizeof(struct epoll_event)); |
912541b0 KS |
1268 | ep_signal.events = EPOLLIN; |
1269 | ep_signal.data.fd = fd_signal; | |
1270 | ||
29804cc1 | 1271 | memzero(&ep_netlink, sizeof(struct epoll_event)); |
912541b0 KS |
1272 | ep_netlink.events = EPOLLIN; |
1273 | ep_netlink.data.fd = fd_netlink; | |
1274 | ||
29804cc1 | 1275 | memzero(&ep_worker, sizeof(struct epoll_event)); |
912541b0 KS |
1276 | ep_worker.events = EPOLLIN; |
1277 | ep_worker.data.fd = fd_worker; | |
1278 | ||
1279 | fd_ep = epoll_create1(EPOLL_CLOEXEC); | |
1280 | if (fd_ep < 0) { | |
9f6445e3 | 1281 | log_error("error creating epoll fd: %m"); |
912541b0 KS |
1282 | goto exit; |
1283 | } | |
1284 | if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_ctrl, &ep_ctrl) < 0 || | |
1285 | epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_inotify, &ep_inotify) < 0 || | |
1286 | epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_signal, &ep_signal) < 0 || | |
1287 | epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_netlink, &ep_netlink) < 0 || | |
1288 | epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_worker, &ep_worker) < 0) { | |
9f6445e3 | 1289 | log_error("fail to add fds to epoll: %m"); |
912541b0 KS |
1290 | goto exit; |
1291 | } | |
1292 | ||
912541b0 | 1293 | if (children_max <= 0) { |
8cc3f8c0 | 1294 | cpu_set_t cpu_set; |
912541b0 | 1295 | |
8cc3f8c0 HH |
1296 | children_max = 8; |
1297 | ||
1298 | if (sched_getaffinity(0, sizeof (cpu_set), &cpu_set) == 0) { | |
1299 | children_max += CPU_COUNT(&cpu_set) * 2; | |
1300 | } | |
912541b0 | 1301 | } |
9f6445e3 | 1302 | log_debug("set children_max to %u", children_max); |
912541b0 | 1303 | |
84b6ad70 TG |
1304 | rc = udev_rules_apply_static_dev_perms(rules); |
1305 | if (rc < 0) | |
9f6445e3 | 1306 | log_error("failed to apply permissions on static device nodes - %s", strerror(-rc)); |
912541b0 KS |
1307 | |
1308 | udev_list_node_init(&event_list); | |
1309 | udev_list_node_init(&worker_list); | |
1310 | ||
1311 | for (;;) { | |
40fe8b11 | 1312 | static usec_t last_usec; |
912541b0 KS |
1313 | struct epoll_event ev[8]; |
1314 | int fdcount; | |
1315 | int timeout; | |
1316 | bool is_worker, is_signal, is_inotify, is_netlink, is_ctrl; | |
1317 | int i; | |
1318 | ||
1319 | if (udev_exit) { | |
1320 | /* close sources of new events and discard buffered events */ | |
1321 | if (fd_ctrl >= 0) { | |
1322 | epoll_ctl(fd_ep, EPOLL_CTL_DEL, fd_ctrl, NULL); | |
1323 | fd_ctrl = -1; | |
1324 | } | |
1325 | if (monitor != NULL) { | |
1326 | epoll_ctl(fd_ep, EPOLL_CTL_DEL, fd_netlink, NULL); | |
1327 | udev_monitor_unref(monitor); | |
1328 | monitor = NULL; | |
1329 | } | |
1330 | if (fd_inotify >= 0) { | |
1331 | epoll_ctl(fd_ep, EPOLL_CTL_DEL, fd_inotify, NULL); | |
1332 | close(fd_inotify); | |
1333 | fd_inotify = -1; | |
1334 | } | |
1335 | ||
1336 | /* discard queued events and kill workers */ | |
1337 | event_queue_cleanup(udev, EVENT_QUEUED); | |
194bbe33 | 1338 | worker_kill(udev); |
912541b0 KS |
1339 | |
1340 | /* exit after all has cleaned up */ | |
9ea28c55 | 1341 | if (udev_list_node_is_empty(&event_list) && children == 0) |
912541b0 KS |
1342 | break; |
1343 | ||
1344 | /* timeout at exit for workers to finish */ | |
9ea28c55 KS |
1345 | timeout = 30 * MSEC_PER_SEC; |
1346 | } else if (udev_list_node_is_empty(&event_list) && children == 0) { | |
e64fae55 | 1347 | /* we are idle */ |
912541b0 | 1348 | timeout = -1; |
194bbe33 KS |
1349 | |
1350 | /* cleanup possible left-over processes in our cgroup */ | |
1351 | if (udev_cgroup) | |
1352 | cg_kill(SYSTEMD_CGROUP_CONTROLLER, udev_cgroup, SIGKILL, false, true, NULL); | |
e64fae55 KS |
1353 | } else { |
1354 | /* kill idle or hanging workers */ | |
9ea28c55 | 1355 | timeout = 3 * MSEC_PER_SEC; |
912541b0 | 1356 | } |
9ea28c55 KS |
1357 | |
1358 | /* tell settle that we are busy or idle */ | |
1359 | if (!udev_list_node_is_empty(&event_list)) { | |
1360 | int fd; | |
1361 | ||
1362 | fd = open("/run/udev/queue", O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444); | |
1363 | if (fd >= 0) | |
1364 | close(fd); | |
1365 | } else { | |
1366 | unlink("/run/udev/queue"); | |
1367 | } | |
1368 | ||
8fef0ff2 | 1369 | fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), timeout); |
912541b0 KS |
1370 | if (fdcount < 0) |
1371 | continue; | |
1372 | ||
1373 | if (fdcount == 0) { | |
e64fae55 KS |
1374 | struct udev_list_node *loop; |
1375 | ||
1376 | /* timeout */ | |
912541b0 | 1377 | if (udev_exit) { |
9f6445e3 | 1378 | log_error("timeout, giving up waiting for workers to finish"); |
912541b0 KS |
1379 | break; |
1380 | } | |
1381 | ||
e64fae55 KS |
1382 | /* kill idle workers */ |
1383 | if (udev_list_node_is_empty(&event_list)) { | |
9f6445e3 | 1384 | log_debug("cleanup idle workers"); |
194bbe33 | 1385 | worker_kill(udev); |
e64fae55 KS |
1386 | } |
1387 | ||
1388 | /* check for hanging events */ | |
1389 | udev_list_node_foreach(loop, &worker_list) { | |
1390 | struct worker *worker = node_to_worker(loop); | |
1391 | ||
1392 | if (worker->state != WORKER_RUNNING) | |
1393 | continue; | |
1394 | ||
dd5eddd2 KS |
1395 | if ((now(CLOCK_MONOTONIC) - worker->event_start_usec) > event_timeout_usec) { |
1396 | log_error("worker [%u] %s timeout; kill it", worker->pid, worker->event->devpath); | |
e64fae55 KS |
1397 | kill(worker->pid, SIGKILL); |
1398 | worker->state = WORKER_KILLED; | |
9ea28c55 | 1399 | |
e64fae55 KS |
1400 | /* drop reference taken for state 'running' */ |
1401 | worker_unref(worker); | |
dd5eddd2 KS |
1402 | log_error("seq %llu '%s' killed", udev_device_get_seqnum(worker->event->dev), worker->event->devpath); |
1403 | worker->event->exitcode = -64; | |
1404 | event_queue_delete(worker->event); | |
1405 | worker->event = NULL; | |
e64fae55 KS |
1406 | } |
1407 | } | |
1408 | ||
912541b0 KS |
1409 | } |
1410 | ||
1411 | is_worker = is_signal = is_inotify = is_netlink = is_ctrl = false; | |
1412 | for (i = 0; i < fdcount; i++) { | |
1413 | if (ev[i].data.fd == fd_worker && ev[i].events & EPOLLIN) | |
1414 | is_worker = true; | |
1415 | else if (ev[i].data.fd == fd_netlink && ev[i].events & EPOLLIN) | |
1416 | is_netlink = true; | |
1417 | else if (ev[i].data.fd == fd_signal && ev[i].events & EPOLLIN) | |
1418 | is_signal = true; | |
1419 | else if (ev[i].data.fd == fd_inotify && ev[i].events & EPOLLIN) | |
1420 | is_inotify = true; | |
1421 | else if (ev[i].data.fd == fd_ctrl && ev[i].events & EPOLLIN) | |
1422 | is_ctrl = true; | |
1423 | } | |
1424 | ||
1425 | /* check for changed config, every 3 seconds at most */ | |
9ea28c55 | 1426 | if ((now(CLOCK_MONOTONIC) - last_usec) > 3 * USEC_PER_SEC) { |
6ada823a | 1427 | if (udev_rules_check_timestamp(rules)) |
912541b0 KS |
1428 | reload = true; |
1429 | if (udev_builtin_validate(udev)) | |
1430 | reload = true; | |
1431 | ||
40fe8b11 | 1432 | last_usec = now(CLOCK_MONOTONIC); |
912541b0 KS |
1433 | } |
1434 | ||
1435 | /* reload requested, HUP signal received, rules changed, builtin changed */ | |
1436 | if (reload) { | |
194bbe33 | 1437 | worker_kill(udev); |
912541b0 KS |
1438 | rules = udev_rules_unref(rules); |
1439 | udev_builtin_exit(udev); | |
7781e063 | 1440 | reload = false; |
912541b0 KS |
1441 | } |
1442 | ||
1443 | /* event has finished */ | |
1444 | if (is_worker) | |
1445 | worker_returned(fd_worker); | |
1446 | ||
1447 | if (is_netlink) { | |
1448 | struct udev_device *dev; | |
1449 | ||
1450 | dev = udev_monitor_receive_device(monitor); | |
1451 | if (dev != NULL) { | |
40fe8b11 | 1452 | udev_device_set_usec_initialized(dev, now(CLOCK_MONOTONIC)); |
912541b0 KS |
1453 | if (event_queue_insert(dev) < 0) |
1454 | udev_device_unref(dev); | |
1455 | } | |
1456 | } | |
1457 | ||
1458 | /* start new events */ | |
1459 | if (!udev_list_node_is_empty(&event_list) && !udev_exit && !stop_exec_queue) { | |
7781e063 | 1460 | udev_builtin_init(udev); |
912541b0 KS |
1461 | if (rules == NULL) |
1462 | rules = udev_rules_new(udev, resolve_names); | |
1463 | if (rules != NULL) | |
1464 | event_queue_start(udev); | |
1465 | } | |
1466 | ||
1467 | if (is_signal) { | |
1468 | struct signalfd_siginfo fdsi; | |
1469 | ssize_t size; | |
1470 | ||
1471 | size = read(fd_signal, &fdsi, sizeof(struct signalfd_siginfo)); | |
1472 | if (size == sizeof(struct signalfd_siginfo)) | |
1473 | handle_signal(udev, fdsi.ssi_signo); | |
1474 | } | |
1475 | ||
1476 | /* we are shutting down, the events below are not handled anymore */ | |
1477 | if (udev_exit) | |
1478 | continue; | |
1479 | ||
1480 | /* device node watch */ | |
1481 | if (is_inotify) | |
1482 | handle_inotify(udev); | |
1483 | ||
1484 | /* | |
1485 | * This needs to be after the inotify handling, to make sure, | |
1486 | * that the ping is send back after the possibly generated | |
1487 | * "change" events by the inotify device node watch. | |
1488 | * | |
1489 | * A single time we may receive a client connection which we need to | |
1490 | * keep open to block the client. It will be closed right before we | |
1491 | * exit. | |
1492 | */ | |
1493 | if (is_ctrl) | |
1494 | ctrl_conn = handle_ctrl_msg(udev_ctrl); | |
1495 | } | |
1496 | ||
1497 | rc = EXIT_SUCCESS; | |
53921bfa | 1498 | exit: |
912541b0 | 1499 | udev_ctrl_cleanup(udev_ctrl); |
9ea28c55 | 1500 | unlink("/run/udev/queue"); |
1f5a5100 | 1501 | exit_daemonize: |
912541b0 KS |
1502 | if (fd_ep >= 0) |
1503 | close(fd_ep); | |
1504 | worker_list_cleanup(udev); | |
1505 | event_queue_cleanup(udev, EVENT_UNDEF); | |
1506 | udev_rules_unref(rules); | |
1507 | udev_builtin_exit(udev); | |
1508 | if (fd_signal >= 0) | |
1509 | close(fd_signal); | |
1510 | if (worker_watch[READ_END] >= 0) | |
1511 | close(worker_watch[READ_END]); | |
1512 | if (worker_watch[WRITE_END] >= 0) | |
1513 | close(worker_watch[WRITE_END]); | |
1514 | udev_monitor_unref(monitor); | |
912541b0 KS |
1515 | udev_ctrl_connection_unref(ctrl_conn); |
1516 | udev_ctrl_unref(udev_ctrl); | |
e9a5ef7c | 1517 | label_finish(); |
912541b0 | 1518 | udev_unref(udev); |
baa30fbc | 1519 | log_close(); |
912541b0 | 1520 | return rc; |
7fafc032 | 1521 | } |