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