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