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