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