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