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