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