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