chiark / gitweb /
873634fe4519cf3149201b19727aee22e1b880a1
[elogind.git] / udev / udevd.c
1 /*
2  * Copyright (C) 2004-2009 Kay Sievers <kay.sievers@vrfy.org>
3  * Copyright (C) 2004 Chris Friesen <chris_friesen@sympatico.ca>
4  * Copyright (C) 2009 Canonical Ltd.
5  * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <stddef.h>
22 #include <signal.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdbool.h>
28 #include <string.h>
29 #include <ctype.h>
30 #include <fcntl.h>
31 #include <time.h>
32 #include <getopt.h>
33 #include <dirent.h>
34 #include <sys/time.h>
35 #include <sys/prctl.h>
36 #include <sys/socket.h>
37 #include <sys/un.h>
38 #include <sys/signalfd.h>
39 #include <sys/select.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
49 #define UDEVD_PRIORITY                  -4
50 #define UDEV_PRIORITY                   -2
51
52 static bool debug;
53
54 static void log_fn(struct udev *udev, int priority,
55                    const char *file, int line, const char *fn,
56                    const char *format, va_list args)
57 {
58         if (debug) {
59                 char buf[1024];
60                 struct timeval tv;
61                 struct timezone tz;
62
63                 vsnprintf(buf, sizeof(buf), format, args);
64                 gettimeofday(&tv, &tz);
65                 fprintf(stderr, "%llu.%06u [%u] %s: %s",
66                         (unsigned long long) tv.tv_sec, (unsigned int) tv.tv_usec,
67                         (int) getpid(), fn, buf);
68         } else {
69                 vsyslog(priority, format, args);
70         }
71 }
72
73 static struct udev_rules *rules;
74 static struct udev_queue_export *udev_queue_export;
75 static struct udev_ctrl *udev_ctrl;
76 static struct udev_monitor *monitor;
77 static int worker_watch[2];
78 static pid_t settle_pid;
79 static bool stop_exec_queue;
80 static bool reload_config;
81 static int children;
82 static int children_max;
83 static int exec_delay;
84 static sigset_t orig_sigmask;
85 static struct udev_list_node event_list;
86 static struct udev_list_node worker_list;
87 static bool udev_exit;
88 static volatile sig_atomic_t worker_exit;
89
90 enum poll_fd {
91         FD_CONTROL,
92         FD_NETLINK,
93         FD_INOTIFY,
94         FD_SIGNAL,
95         FD_WORKER,
96 };
97
98 static struct pollfd pfd[] = {
99         [FD_NETLINK] = { .events = POLLIN },
100         [FD_WORKER] =  { .events = POLLIN },
101         [FD_SIGNAL] =  { .events = POLLIN },
102         [FD_INOTIFY] = { .events = POLLIN },
103         [FD_CONTROL] = { .events = POLLIN },
104 };
105
106 enum event_state {
107         EVENT_UNDEF,
108         EVENT_QUEUED,
109         EVENT_RUNNING,
110 };
111
112 struct event {
113         struct udev_list_node node;
114         struct udev *udev;
115         struct udev_device *dev;
116         enum event_state state;
117         int exitcode;
118         unsigned long long int delaying_seqnum;
119         unsigned long long int seqnum;
120         const char *devpath;
121         size_t devpath_len;
122         const char *devpath_old;
123         dev_t devnum;
124         bool is_block;
125 };
126
127 static struct event *node_to_event(struct udev_list_node *node)
128 {
129         char *event;
130
131         event = (char *)node;
132         event -= offsetof(struct event, node);
133         return (struct event *)event;
134 }
135
136 enum worker_state {
137         WORKER_UNDEF,
138         WORKER_RUNNING,
139         WORKER_IDLE,
140         WORKER_KILLED,
141 };
142
143 struct worker {
144         struct udev_list_node node;
145         struct udev *udev;
146         int refcount;
147         pid_t pid;
148         struct udev_monitor *monitor;
149         enum worker_state state;
150         struct event *event;
151 };
152
153 /* passed from worker to main process */
154 struct worker_message {
155         pid_t pid;
156         int exitcode;
157 };
158
159 static struct worker *node_to_worker(struct udev_list_node *node)
160 {
161         char *worker;
162
163         worker = (char *)node;
164         worker -= offsetof(struct worker, node);
165         return (struct worker *)worker;
166 }
167
168 static void event_queue_delete(struct event *event)
169 {
170         udev_list_node_remove(&event->node);
171
172         /* mark as failed, if "add" event returns non-zero */
173         if (event->exitcode != 0 && strcmp(udev_device_get_action(event->dev), "remove") != 0)
174                 udev_queue_export_device_failed(udev_queue_export, event->dev);
175         else
176                 udev_queue_export_device_finished(udev_queue_export, event->dev);
177
178         info(event->udev, "seq %llu done with %i\n", udev_device_get_seqnum(event->dev), event->exitcode);
179         udev_device_unref(event->dev);
180         free(event);
181 }
182
183 static void event_sig_handler(int signum)
184 {
185         switch (signum) {
186         case SIGALRM:
187                 _exit(1);
188                 break;
189         case SIGTERM:
190                 worker_exit = true;
191                 break;
192         }
193 }
194
195 static struct worker *worker_ref(struct worker *worker)
196 {
197         worker->refcount++;
198         return worker;
199 }
200
201 static void worker_unref(struct worker *worker)
202 {
203         worker->refcount--;
204         if (worker->refcount > 0)
205                 return;
206
207         udev_list_node_remove(&worker->node);
208         udev_monitor_unref(worker->monitor);
209         children--;
210         info(worker->udev, "worker [%u] cleaned up\n", worker->pid);
211         free(worker);
212 }
213
214 static void worker_new(struct event *event)
215 {
216         struct worker *worker;
217         struct udev_monitor *worker_monitor;
218         pid_t pid;
219         struct sigaction act;
220
221         /* listen for new events */
222         worker_monitor = udev_monitor_new_from_netlink(event->udev, NULL);
223         if (worker_monitor == NULL)
224                 return;
225         /* allow the main daemon netlink address to send devices to the worker */
226         udev_monitor_allow_unicast_sender(worker_monitor, monitor);
227         udev_monitor_enable_receiving(worker_monitor);
228
229         worker = calloc(1, sizeof(struct worker));
230         if (worker == NULL)
231                 return;
232         /* worker + event reference */
233         worker->refcount = 2;
234         worker->udev = event->udev;
235
236         pid = fork();
237         switch (pid) {
238         case 0: {
239                 sigset_t sigmask;
240                 struct udev_device *dev;
241                 struct pollfd pmon = {
242                         .fd = udev_monitor_get_fd(worker_monitor),
243                         .events = POLLIN,
244                 };
245
246                 udev_queue_export_unref(udev_queue_export);
247                 udev_monitor_unref(monitor);
248                 udev_ctrl_unref(udev_ctrl);
249                 close(pfd[FD_SIGNAL].fd);
250                 close(worker_watch[READ_END]);
251                 udev_log_close();
252                 udev_log_init("udevd-work");
253                 setpriority(PRIO_PROCESS, 0, UDEV_PRIORITY);
254
255                 /* set signal handlers */
256                 memset(&act, 0x00, sizeof(act));
257                 act.sa_handler = event_sig_handler;
258                 sigemptyset (&act.sa_mask);
259                 act.sa_flags = 0;
260                 sigaction(SIGTERM, &act, NULL);
261                 sigaction(SIGALRM, &act, NULL);
262
263                 /* unblock SIGALRM */
264                 sigfillset(&sigmask);
265                 sigdelset(&sigmask, SIGALRM);
266                 sigprocmask(SIG_SETMASK, &sigmask, NULL);
267                 /* SIGTERM is unblocked in ppoll() */
268                 sigdelset(&sigmask, SIGTERM);
269
270                 /* request TERM signal if parent exits */
271                 prctl(PR_SET_PDEATHSIG, SIGTERM);
272
273                 /* initial device */
274                 dev = event->dev;
275
276                 do {
277                         struct udev_event *udev_event;
278                         struct worker_message msg = {};
279                         int err;
280                         int failed = 0;
281
282                         info(event->udev, "seq %llu running\n", udev_device_get_seqnum(dev));
283                         udev_event = udev_event_new(dev);
284                         if (udev_event == NULL)
285                                 _exit(3);
286
287                         /* set timeout to prevent hanging processes */
288                         alarm(UDEV_EVENT_TIMEOUT);
289
290                         if (exec_delay > 0)
291                                 udev_event->exec_delay = exec_delay;
292
293                         /* apply rules, create node, symlinks */
294                         err = udev_event_execute_rules(udev_event, rules);
295
296                         /* rules may change/disable the timeout */
297                         if (udev_device_get_event_timeout(dev) >= 0)
298                                 alarm(udev_device_get_event_timeout(dev));
299
300                         if (err == 0)
301                                 failed = udev_event_execute_run(udev_event, &orig_sigmask);
302
303                         alarm(0);
304
305                         /* apply/restore inotify watch */
306                         if (err == 0 && udev_event->inotify_watch) {
307                                 udev_watch_begin(udev_event->udev, dev);
308                                 udev_device_update_db(dev);
309                         }
310
311                         /* send processed event back to libudev listeners */
312                         udev_monitor_send_device(worker_monitor, NULL, dev);
313
314                         /* send udevd the result of the event execution */
315                         if (err != 0)
316                                 msg.exitcode = err;
317                         else if (failed != 0)
318                                 msg.exitcode = failed;
319                         msg.pid = getpid();
320                         send(worker_watch[WRITE_END], &msg, sizeof(struct worker_message), 0);
321
322                         info(event->udev, "seq %llu processed with %i\n", udev_device_get_seqnum(dev), err);
323                         udev_event_unref(udev_event);
324                         udev_device_unref(dev);
325                         dev = NULL;
326
327                         /* wait for more device messages or signal from udevd */
328                         while (!worker_exit) {
329                                 int fdcount;
330
331                                 fdcount = ppoll(&pmon, 1, NULL, &sigmask);
332                                 if (fdcount < 0)
333                                         continue;
334
335                                 if (pmon.revents & POLLIN) {
336                                         dev = udev_monitor_receive_device(worker_monitor);
337                                         if (dev != NULL)
338                                                 break;
339                                 }
340                         }
341                 } while (dev != NULL);
342
343                 udev_monitor_unref(worker_monitor);
344                 udev_log_close();
345                 exit(0);
346         }
347         case -1:
348                 udev_monitor_unref(worker_monitor);
349                 event->state = EVENT_QUEUED;
350                 free(worker);
351                 err(event->udev, "fork of child failed: %m\n");
352                 break;
353         default:
354                 /* close monitor, but keep address around */
355                 udev_monitor_disconnect(worker_monitor);
356                 worker->monitor = worker_monitor;
357                 worker->pid = pid;
358                 worker->state = WORKER_RUNNING;
359                 worker->event = event;
360                 event->state = EVENT_RUNNING;
361                 udev_list_node_append(&worker->node, &worker_list);
362                 children++;
363                 info(event->udev, "seq %llu forked new worker [%u]\n", udev_device_get_seqnum(event->dev), pid);
364                 break;
365         }
366 }
367
368 static void event_run(struct event *event, bool force)
369 {
370         struct udev_list_node *loop;
371
372         udev_list_node_foreach(loop, &worker_list) {
373                 struct worker *worker = node_to_worker(loop);
374                 ssize_t count;
375
376                 if (worker->state != WORKER_IDLE)
377                         continue;
378
379                 count = udev_monitor_send_device(monitor, worker->monitor, event->dev);
380                 if (count < 0) {
381                         err(event->udev, "worker [%u] did not accept message %zi (%m), kill it\n", worker->pid, count);
382                         kill(worker->pid, SIGKILL);
383                         worker->state = WORKER_KILLED;
384                         continue;
385                 }
386                 worker_ref(worker);
387                 worker->event = event;
388                 worker->state = WORKER_RUNNING;
389                 event->state = EVENT_RUNNING;
390                 return;
391         }
392
393         if (!force && children >= children_max) {
394                 if (children_max > 1)
395                         info(event->udev, "maximum number (%i) of children reached\n", children);
396                 return;
397         }
398
399         /* start new worker and pass initial device */
400         worker_new(event);
401 }
402
403 static void event_queue_insert(struct udev_device *dev)
404 {
405         struct event *event;
406
407         event = calloc(1, sizeof(struct event));
408         if (event == NULL)
409                 return;
410
411         event->udev = udev_device_get_udev(dev);
412         event->dev = dev;
413         event->seqnum = udev_device_get_seqnum(dev);
414         event->devpath = udev_device_get_devpath(dev);
415         event->devpath_len = strlen(event->devpath);
416         event->devpath_old = udev_device_get_devpath_old(dev);
417         event->devnum = udev_device_get_devnum(dev);
418         event->is_block = (strcmp("block", udev_device_get_subsystem(dev)) == 0);
419
420         udev_queue_export_device_queued(udev_queue_export, dev);
421         info(event->udev, "seq %llu queued, '%s' '%s'\n", udev_device_get_seqnum(dev),
422              udev_device_get_action(dev), udev_device_get_subsystem(dev));
423
424         event->state = EVENT_QUEUED;
425         udev_list_node_append(&event->node, &event_list);
426
427         /* run all events with a timeout set immediately */
428         if (udev_device_get_timeout(dev) > 0) {
429                 event_run(event, true);
430                 return;
431         }
432 }
433
434 static void worker_kill(struct udev *udev, int retain)
435 {
436         struct udev_list_node *loop;
437         int max;
438
439         if (children <= retain)
440                 return;
441
442         max = children - retain;
443
444         udev_list_node_foreach(loop, &worker_list) {
445                 struct worker *worker = node_to_worker(loop);
446
447                 if (max-- <= 0)
448                         break;
449
450                 if (worker->state == WORKER_KILLED)
451                         continue;
452
453                 worker->state = WORKER_KILLED;
454                 kill(worker->pid, SIGTERM);
455         }
456 }
457
458 /* lookup event for identical, parent, child device */
459 static bool is_devpath_busy(struct event *event)
460 {
461         struct udev_list_node *loop;
462         size_t common;
463
464         /* check if queue contains events we depend on */
465         udev_list_node_foreach(loop, &event_list) {
466                 struct event *loop_event = node_to_event(loop);
467
468                 /* we already found a later event, earlier can not block us, no need to check again */
469                 if (loop_event->seqnum < event->delaying_seqnum)
470                         continue;
471
472                 /* event we checked earlier still exists, no need to check again */
473                 if (loop_event->seqnum == event->delaying_seqnum)
474                         return true;
475
476                 /* found ourself, no later event can block us */
477                 if (loop_event->seqnum >= event->seqnum)
478                         break;
479
480                 /* check major/minor */
481                 if (major(event->devnum) != 0 && event->devnum == loop_event->devnum && event->is_block == loop_event->is_block)
482                         return true;
483
484                 /* check our old name */
485                 if (event->devpath_old != NULL && strcmp(loop_event->devpath, event->devpath_old) == 0) {
486                         event->delaying_seqnum = loop_event->seqnum;
487                         return true;
488                 }
489
490                 /* compare devpath */
491                 common = MIN(loop_event->devpath_len, event->devpath_len);
492
493                 /* one devpath is contained in the other? */
494                 if (memcmp(loop_event->devpath, event->devpath, common) != 0)
495                         continue;
496
497                 /* identical device event found */
498                 if (loop_event->devpath_len == event->devpath_len) {
499                         event->delaying_seqnum = loop_event->seqnum;
500                         return true;
501                 }
502
503                 /* parent device event found */
504                 if (event->devpath[common] == '/') {
505                         event->delaying_seqnum = loop_event->seqnum;
506                         return true;
507                 }
508
509                 /* child device event found */
510                 if (loop_event->devpath[common] == '/') {
511                         event->delaying_seqnum = loop_event->seqnum;
512                         return true;
513                 }
514
515                 /* no matching device */
516                 continue;
517         }
518
519         return false;
520 }
521
522 static void events_start(struct udev *udev)
523 {
524         struct udev_list_node *loop;
525
526         udev_list_node_foreach(loop, &event_list) {
527                 struct event *event = node_to_event(loop);
528
529                 if (event->state != EVENT_QUEUED)
530                         continue;
531
532                 /* do not start event if parent or child event is still running */
533                 if (is_devpath_busy(event)) {
534                         dbg(udev, "delay seq %llu (%s)\n", event->seqnum, event->devpath);
535                         continue;
536                 }
537
538                 event_run(event, false);
539         }
540 }
541
542 static void worker_returned(void)
543 {
544         for (;;) {
545                 struct worker_message msg;
546                 ssize_t size;
547                 struct udev_list_node *loop;
548
549                 size = recv(pfd[FD_WORKER].fd, &msg, sizeof(struct worker_message), MSG_DONTWAIT);
550                 if (size != sizeof(struct worker_message))
551                         break;
552
553                 /* lookup worker who sent the signal */
554                 udev_list_node_foreach(loop, &worker_list) {
555                         struct worker *worker = node_to_worker(loop);
556
557                         if (worker->pid != msg.pid)
558                                 continue;
559
560                         /* worker returned */
561                         worker->event->exitcode = msg.exitcode;
562                         event_queue_delete(worker->event);
563                         worker->event = NULL;
564                         if (worker->state != WORKER_KILLED)
565                                 worker->state = WORKER_IDLE;
566                         worker_unref(worker);
567                         break;
568                 }
569         }
570 }
571
572 /* receive the udevd message from userspace */
573 static void handle_ctrl_msg(struct udev_ctrl *uctrl)
574 {
575         struct udev *udev = udev_ctrl_get_udev(uctrl);
576         struct udev_ctrl_msg *ctrl_msg;
577         const char *str;
578         int i;
579
580         ctrl_msg = udev_ctrl_receive_msg(uctrl);
581         if (ctrl_msg == NULL)
582                 return;
583
584         i = udev_ctrl_get_set_log_level(ctrl_msg);
585         if (i >= 0) {
586                 info(udev, "udevd message (SET_LOG_PRIORITY) received, log_priority=%i\n", i);
587                 udev_set_log_priority(udev, i);
588                 worker_kill(udev, 0);
589         }
590
591         if (udev_ctrl_get_stop_exec_queue(ctrl_msg) > 0) {
592                 info(udev, "udevd message (STOP_EXEC_QUEUE) received\n");
593                 stop_exec_queue = true;
594         }
595
596         if (udev_ctrl_get_start_exec_queue(ctrl_msg) > 0) {
597                 info(udev, "udevd message (START_EXEC_QUEUE) received\n");
598                 stop_exec_queue = false;
599         }
600
601         if (udev_ctrl_get_reload_rules(ctrl_msg) > 0) {
602                 info(udev, "udevd message (RELOAD_RULES) received\n");
603                 reload_config = true;
604         }
605
606         str = udev_ctrl_get_set_env(ctrl_msg);
607         if (str != NULL) {
608                 char *key;
609
610                 key = strdup(str);
611                 if (key != NULL) {
612                         char *val;
613
614                         val = strchr(key, '=');
615                         if (val != NULL) {
616                                 val[0] = '\0';
617                                 val = &val[1];
618                                 if (val[0] == '\0') {
619                                         info(udev, "udevd message (ENV) received, unset '%s'\n", key);
620                                         udev_add_property(udev, key, NULL);
621                                 } else {
622                                         info(udev, "udevd message (ENV) received, set '%s=%s'\n", key, val);
623                                         udev_add_property(udev, key, val);
624                                 }
625                         } else {
626                                 err(udev, "wrong key format '%s'\n", key);
627                         }
628                         free(key);
629                 }
630                 worker_kill(udev, 0);
631         }
632
633         i = udev_ctrl_get_set_children_max(ctrl_msg);
634         if (i >= 0) {
635                 info(udev, "udevd message (SET_MAX_CHILDREN) received, children_max=%i\n", i);
636                 children_max = i;
637         }
638
639         settle_pid = udev_ctrl_get_settle(ctrl_msg);
640         if (settle_pid > 0) {
641                 info(udev, "udevd message (SETTLE) received\n");
642                 kill(settle_pid, SIGUSR1);
643                 settle_pid = 0;
644         }
645         udev_ctrl_msg_unref(ctrl_msg);
646 }
647
648 /* read inotify messages */
649 static int handle_inotify(struct udev *udev)
650 {
651         int nbytes, pos;
652         char *buf;
653         struct inotify_event *ev;
654
655         if ((ioctl(pfd[FD_INOTIFY].fd, FIONREAD, &nbytes) < 0) || (nbytes <= 0))
656                 return 0;
657
658         buf = malloc(nbytes);
659         if (buf == NULL) {
660                 err(udev, "error getting buffer for inotify\n");
661                 return -1;
662         }
663
664         nbytes = read(pfd[FD_INOTIFY].fd, buf, nbytes);
665
666         for (pos = 0; pos < nbytes; pos += sizeof(struct inotify_event) + ev->len) {
667                 struct udev_device *dev;
668
669                 ev = (struct inotify_event *)(buf + pos);
670                 if (ev->len) {
671                         const char *s;
672
673                         info(udev, "inotify event: %x for %s\n", ev->mask, ev->name);
674                         s = strstr(ev->name, ".rules");
675                         if (s == NULL)
676                                 continue;
677                         if (strlen(s) != strlen(".rules"))
678                                 continue;
679                         reload_config = true;
680                         continue;
681                 }
682
683                 dev = udev_watch_lookup(udev, ev->wd);
684                 if (dev != NULL) {
685                         info(udev, "inotify event: %x for %s\n", ev->mask, udev_device_get_devnode(dev));
686                         if (ev->mask & IN_CLOSE_WRITE) {
687                                 char filename[UTIL_PATH_SIZE];
688                                 int fd;
689
690                                 info(udev, "device %s closed, synthesising 'change'\n", udev_device_get_devnode(dev));
691                                 util_strscpyl(filename, sizeof(filename), udev_device_get_syspath(dev), "/uevent", NULL);
692                                 fd = open(filename, O_WRONLY);
693                                 if (fd < 0 || write(fd, "change", 6) < 0)
694                                         info(udev, "error writing uevent: %m\n");
695                                 close(fd);
696                         }
697                         if (ev->mask & IN_IGNORED)
698                                 udev_watch_end(udev, dev);
699
700                         udev_device_unref(dev);
701                 }
702
703         }
704
705         free(buf);
706         return 0;
707 }
708
709 static void handle_signal(struct udev *udev, int signo)
710 {
711         switch (signo) {
712         case SIGINT:
713         case SIGTERM:
714                 udev_exit = true;
715                 break;
716         case SIGCHLD:
717                 for (;;) {
718                         pid_t pid;
719                         int status;
720                         struct udev_list_node *loop, *tmp;
721
722                         pid = waitpid(-1, &status, WNOHANG);
723                         if (pid <= 0)
724                                 break;
725
726                         udev_list_node_foreach_safe(loop, tmp, &worker_list) {
727                                 struct worker *worker = node_to_worker(loop);
728
729                                 if (worker->pid != pid)
730                                         continue;
731
732                                 info(udev, "worker [%u] exit\n", pid);
733                                 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
734                                         err(udev, "worker [%u] unexpectedly returned with status 0x%04x\n", pid, status);
735                                         if (worker->event != NULL) {
736                                                 err(udev, "worker [%u] failed while handling '%s'\n", pid, worker->event->devpath);
737                                                 worker->event->exitcode = -32;
738                                                 event_queue_delete(worker->event);
739                                                 /* drop reference from running event */
740                                                 worker_unref(worker);
741                                         }
742                                 }
743                                 worker_unref(worker);
744                                 break;
745                         }
746                 }
747                 break;
748         case SIGHUP:
749                 reload_config = true;
750                 break;
751         }
752 }
753
754 static void static_dev_create_from_modules(struct udev *udev)
755 {
756         struct utsname kernel;
757         char modules[UTIL_PATH_SIZE];
758         char buf[4096];
759         FILE *f;
760
761         uname(&kernel);
762         util_strscpyl(modules, sizeof(modules), "/lib/modules/", kernel.release, "/modules.devname", NULL);
763         f = fopen(modules, "r");
764         if (f == NULL)
765                 return;
766
767         while (fgets(buf, sizeof(buf), f) != NULL) {
768                 char *s;
769                 const char *modname;
770                 const char *devname;
771                 const char *devno;
772                 int maj, min;
773                 char type;
774                 mode_t mode;
775                 char filename[UTIL_PATH_SIZE];
776
777                 if (buf[0] == '#')
778                         continue;
779
780                 modname = buf;
781                 s = strchr(modname, ' ');
782                 if (s == NULL)
783                         continue;
784                 s[0] = '\0';
785
786                 devname = &s[1];
787                 s = strchr(devname, ' ');
788                 if (s == NULL)
789                         continue;
790                 s[0] = '\0';
791
792                 devno = &s[1];
793                 s = strchr(devno, ' ');
794                 if (s == NULL)
795                         s = strchr(devno, '\n');
796                 if (s != NULL)
797                         s[0] = '\0';
798                 if (sscanf(devno, "%c%u:%u", &type, &maj, &min) != 3)
799                         continue;
800
801                 if (type == 'c')
802                         mode = 0600 | S_IFCHR;
803                 else if (type == 'b')
804                         mode = 0600 | S_IFBLK;
805                 else
806                         continue;
807
808                 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/", devname, NULL);
809                 util_create_path(udev, filename);
810                 udev_selinux_setfscreatecon(udev, filename, mode);
811                 info(udev, "mknod '%s' %c%u:%u\n", filename, type, maj, min);
812                 if (mknod(filename, mode, makedev(maj, min)) < 0 && errno == EEXIST)
813                         utimensat(AT_FDCWD, filename, NULL, 0);
814                 udev_selinux_resetfscreatecon(udev);
815         }
816
817         fclose(f);
818 }
819
820 static int copy_dir(struct udev *udev, DIR *dir_from, DIR *dir_to, int maxdepth)
821 {
822         struct dirent *dent;
823
824         for (dent = readdir(dir_from); dent != NULL; dent = readdir(dir_from)) {
825                 struct stat stats;
826
827                 if (dent->d_name[0] == '.')
828                         continue;
829                 if (fstatat(dirfd(dir_from), dent->d_name, &stats, AT_SYMLINK_NOFOLLOW) != 0)
830                         continue;
831
832                 if (S_ISBLK(stats.st_mode) || S_ISCHR(stats.st_mode)) {
833                         udev_selinux_setfscreateconat(udev, dirfd(dir_to), dent->d_name, stats.st_mode & 0777);
834                         if (mknodat(dirfd(dir_to), dent->d_name, stats.st_mode, stats.st_rdev) == 0) {
835                                 fchmodat(dirfd(dir_to), dent->d_name, stats.st_mode & 0777, 0);
836                                 fchownat(dirfd(dir_to), dent->d_name, stats.st_uid, stats.st_gid, 0);
837                         } else {
838                                 utimensat(dirfd(dir_to), dent->d_name, NULL, 0);
839                         }
840                         udev_selinux_resetfscreatecon(udev);
841                 } else if (S_ISLNK(stats.st_mode)) {
842                         char target[UTIL_PATH_SIZE];
843                         ssize_t len;
844
845                         len = readlinkat(dirfd(dir_from), dent->d_name, target, sizeof(target));
846                         if (len <= 0 || len == (ssize_t)sizeof(target))
847                                 continue;
848                         target[len] = '\0';
849                         udev_selinux_setfscreateconat(udev, dirfd(dir_to), dent->d_name, S_IFLNK);
850                         if (symlinkat(target, dirfd(dir_to), dent->d_name) < 0 && errno == EEXIST)
851                                 utimensat(dirfd(dir_to), dent->d_name, NULL, AT_SYMLINK_NOFOLLOW);
852                         udev_selinux_resetfscreatecon(udev);
853                 } else if (S_ISDIR(stats.st_mode)) {
854                         DIR *dir2_from, *dir2_to;
855
856                         if (maxdepth == 0)
857                                 continue;
858
859                         udev_selinux_setfscreateconat(udev, dirfd(dir_to), dent->d_name, S_IFDIR|0755);
860                         mkdirat(dirfd(dir_to), dent->d_name, 0755);
861                         udev_selinux_resetfscreatecon(udev);
862
863                         dir2_to = fdopendir(openat(dirfd(dir_to), dent->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC));
864                         if (dir2_to == NULL)
865                                 continue;
866
867                         dir2_from = fdopendir(openat(dirfd(dir_from), dent->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC));
868                         if (dir2_from == NULL) {
869                                 closedir(dir2_to);
870                                 continue;
871                         }
872
873                         copy_dir(udev, dir2_from, dir2_to, maxdepth-1);
874
875                         closedir(dir2_to);
876                         closedir(dir2_from);
877                 }
878         }
879
880         return 0;
881 }
882
883 static void static_dev_create_links(struct udev *udev, DIR *dir)
884 {
885         struct stdlinks {
886                 const char *link;
887                 const char *target;
888         };
889         static const struct stdlinks stdlinks[] = {
890                 { "core", "/proc/kcore" },
891                 { "fd", "/proc/self/fd" },
892                 { "stdin", "/proc/self/fd/0" },
893                 { "stdout", "/proc/self/fd/1" },
894                 { "stderr", "/proc/self/fd/2" },
895         };
896         unsigned int i;
897
898         for (i = 0; i < ARRAY_SIZE(stdlinks); i++) {
899                 udev_selinux_setfscreateconat(udev, dirfd(dir), stdlinks[i].link, S_IFLNK);
900                 if (symlinkat(stdlinks[i].target, dirfd(dir), stdlinks[i].link) < 0 && errno == EEXIST)
901                         utimensat(dirfd(dir), stdlinks[i].link, NULL, AT_SYMLINK_NOFOLLOW);
902                 udev_selinux_resetfscreatecon(udev);
903         }
904 }
905
906 static void static_dev_create_from_devices(struct udev *udev, DIR *dir)
907 {
908         DIR *dir_from;
909
910         dir_from = opendir(LIBEXECDIR "/devices");
911         if (dir_from == NULL)
912                 return;
913         copy_dir(udev, dir_from, dir, 8);
914         closedir(dir_from);
915 }
916
917 static void static_dev_create(struct udev *udev)
918 {
919         DIR *dir;
920
921         dir = opendir(udev_get_dev_path(udev));
922         if (dir == NULL)
923                 return;
924
925         static_dev_create_links(udev, dir);
926         static_dev_create_from_devices(udev, dir);
927
928         closedir(dir);
929 }
930
931 static int mem_size_mb(void)
932 {
933         FILE *f;
934         char buf[4096];
935         long int memsize = -1;
936
937         f = fopen("/proc/meminfo", "r");
938         if (f == NULL)
939                 return -1;
940
941         while (fgets(buf, sizeof(buf), f) != NULL) {
942                 long int value;
943
944                 if (sscanf(buf, "MemTotal: %ld kB", &value) == 1) {
945                         memsize = value / 1024;
946                         break;
947                 }
948         }
949
950         fclose(f);
951         return memsize;
952 }
953
954 static int init_notify(const char *state)
955 {
956         int fd = -1, r;
957         struct msghdr msghdr;
958         struct iovec iovec;
959         struct ucred *ucred;
960         union {
961                 struct sockaddr sa;
962                 struct sockaddr_un un;
963         } sockaddr;
964         union {
965                 struct cmsghdr cmsghdr;
966                 uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
967         } control;
968         const char *e;
969
970         if (!(e = getenv("NOTIFY_SOCKET"))) {
971                 r = 0;
972                 goto finish;
973         }
974
975         /* Must be an abstract socket, or an absolute path */
976         if ((e[0] != '@' && e[0] != '/') || e[1] == 0) {
977                 r = -EINVAL;
978                 goto finish;
979         }
980
981         if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) {
982                 r = -errno;
983                 goto finish;
984         }
985
986         memset(&sockaddr, 0, sizeof(sockaddr));
987         sockaddr.sa.sa_family = AF_UNIX;
988         strncpy(sockaddr.un.sun_path, e, sizeof(sockaddr.un.sun_path));
989
990         if (sockaddr.un.sun_path[0] == '@')
991                 sockaddr.un.sun_path[0] = 0;
992
993         memset(&iovec, 0, sizeof(iovec));
994         iovec.iov_base = (char*) state;
995         iovec.iov_len = strlen(state);
996
997         memset(&control, 0, sizeof(control));
998         control.cmsghdr.cmsg_level = SOL_SOCKET;
999         control.cmsghdr.cmsg_type = SCM_CREDENTIALS;
1000         control.cmsghdr.cmsg_len = CMSG_LEN(sizeof(struct ucred));
1001
1002         ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
1003         ucred->pid = getpid();
1004         ucred->uid = getuid();
1005         ucred->gid = getgid();
1006
1007         memset(&msghdr, 0, sizeof(msghdr));
1008         msghdr.msg_name = &sockaddr;
1009         msghdr.msg_namelen = sizeof(sa_family_t) + strlen(e);
1010         if (msghdr.msg_namelen > sizeof(struct sockaddr_un))
1011                 msghdr.msg_namelen = sizeof(struct sockaddr_un);
1012         msghdr.msg_iov = &iovec;
1013         msghdr.msg_iovlen = 1;
1014         msghdr.msg_control = &control;
1015         msghdr.msg_controllen = control.cmsghdr.cmsg_len;
1016
1017         if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) {
1018                 r = -errno;
1019                 goto finish;
1020         }
1021
1022         r = 0;
1023
1024 finish:
1025         if (fd >= 0)
1026                 close(fd);
1027
1028         return r;
1029 }
1030
1031 int main(int argc, char *argv[])
1032 {
1033         struct udev *udev;
1034         int fd;
1035         FILE *f;
1036         sigset_t mask;
1037         int daemonize = false;
1038         int resolve_names = 1;
1039         static const struct option options[] = {
1040                 { "daemon", no_argument, NULL, 'd' },
1041                 { "debug", no_argument, NULL, 'D' },
1042                 { "children-max", required_argument, NULL, 'c' },
1043                 { "exec-delay", required_argument, NULL, 'e' },
1044                 { "resolve-names", required_argument, NULL, 'N' },
1045                 { "help", no_argument, NULL, 'h' },
1046                 { "version", no_argument, NULL, 'V' },
1047                 {}
1048         };
1049         int rc = 1;
1050
1051         udev = udev_new();
1052         if (udev == NULL)
1053                 goto exit;
1054
1055         udev_log_init("udevd");
1056         udev_set_log_fn(udev, log_fn);
1057         info(udev, "version %s\n", VERSION);
1058         udev_selinux_init(udev);
1059
1060         for (;;) {
1061                 int option;
1062
1063                 option = getopt_long(argc, argv, "cdeDthV", options, NULL);
1064                 if (option == -1)
1065                         break;
1066
1067                 switch (option) {
1068                 case 'd':
1069                         daemonize = true;
1070                         break;
1071                 case 'c':
1072                         children_max = strtoul(optarg, NULL, 0);
1073                         break;
1074                 case 'e':
1075                         exec_delay = strtoul(optarg, NULL, 0);
1076                         break;
1077                 case 'D':
1078                         debug = true;
1079                         if (udev_get_log_priority(udev) < LOG_INFO)
1080                                 udev_set_log_priority(udev, LOG_INFO);
1081                         break;
1082                 case 'N':
1083                         if (strcmp (optarg, "early") == 0) {
1084                                 resolve_names = 1;
1085                         } else if (strcmp (optarg, "late") == 0) {
1086                                 resolve_names = 0;
1087                         } else if (strcmp (optarg, "never") == 0) {
1088                                 resolve_names = -1;
1089                         } else {
1090                                 fprintf(stderr, "resolve-names must be early, late or never\n");
1091                                 err(udev, "resolve-names must be early, late or never\n");
1092                                 goto exit;
1093                         }
1094                         break;
1095                 case 'h':
1096                         printf("Usage: udevd OPTIONS\n"
1097                                "  --daemon\n"
1098                                "  --debug\n"
1099                                "  --children-max=<maximum number of workers>\n"
1100                                "  --exec-delay=<seconds to wait before executing RUN=>\n"
1101                                "  --resolve-names=early|late|never\n" 
1102                                "  --version\n"
1103                                "  --help\n"
1104                                "\n");
1105                         goto exit;
1106                 case 'V':
1107                         printf("%s\n", VERSION);
1108                         goto exit;
1109                 default:
1110                         goto exit;
1111                 }
1112         }
1113
1114         /*
1115          * read the kernel commandline, in case we need to get into debug mode
1116          *   udev.log-priority=<level>              syslog priority
1117          *   udev.children-max=<number of workers>  events are fully serialized if set to 1
1118          *
1119          */
1120         f = fopen("/proc/cmdline", "r");
1121         if (f != NULL) {
1122                 char cmdline[4096];
1123
1124                 if (fgets(cmdline, sizeof(cmdline), f) != NULL) {
1125                         char *pos;
1126
1127                         pos = strstr(cmdline, "udev.log-priority=");
1128                         if (pos != NULL) {
1129                                 pos += strlen("udev.log-priority=");
1130                                 udev_set_log_priority(udev, util_log_priority(pos));
1131                         }
1132
1133                         pos = strstr(cmdline, "udev.children-max=");
1134                         if (pos != NULL) {
1135                                 pos += strlen("udev.children-max=");
1136                                 children_max = strtoul(pos, NULL, 0);
1137                         }
1138
1139                         pos = strstr(cmdline, "udev.exec-delay=");
1140                         if (pos != NULL) {
1141                                 pos += strlen("udev.exec-delay=");
1142                                 exec_delay = strtoul(pos, NULL, 0);
1143                         }
1144                 }
1145                 fclose(f);
1146         }
1147
1148         if (getuid() != 0) {
1149                 fprintf(stderr, "root privileges required\n");
1150                 err(udev, "root privileges required\n");
1151                 goto exit;
1152         }
1153
1154         /* set umask before creating any file/directory */
1155         chdir("/");
1156         umask(022);
1157
1158         /* before opening new files, make sure std{in,out,err} fds are in a sane state */
1159         fd = open("/dev/null", O_RDWR);
1160         if (fd < 0) {
1161                 fprintf(stderr, "cannot open /dev/null\n");
1162                 err(udev, "cannot open /dev/null\n");
1163         }
1164         if (write(STDOUT_FILENO, 0, 0) < 0)
1165                 dup2(fd, STDOUT_FILENO);
1166         if (write(STDERR_FILENO, 0, 0) < 0)
1167                 dup2(fd, STDERR_FILENO);
1168
1169         udev_ctrl = udev_ctrl_new_from_socket(udev, UDEV_CTRL_SOCK_PATH);
1170         if (udev_ctrl == NULL) {
1171                 fprintf(stderr, "error initializing control socket");
1172                 err(udev, "error initializing udevd socket");
1173                 rc = 1;
1174                 goto exit;
1175         }
1176         if (udev_ctrl_enable_receiving(udev_ctrl) < 0) {
1177                 fprintf(stderr, "error binding control socket, seems udevd is already running\n");
1178                 err(udev, "error binding control socket, seems udevd is already running\n");
1179                 rc = 1;
1180                 goto exit;
1181         }
1182         pfd[FD_CONTROL].fd = udev_ctrl_get_fd(udev_ctrl);
1183
1184         monitor = udev_monitor_new_from_netlink(udev, "kernel");
1185         if (monitor == NULL || udev_monitor_enable_receiving(monitor) < 0) {
1186                 fprintf(stderr, "error initializing netlink socket\n");
1187                 err(udev, "error initializing netlink socket\n");
1188                 rc = 3;
1189                 goto exit;
1190         }
1191         udev_monitor_set_receive_buffer_size(monitor, 128*1024*1024);
1192         pfd[FD_NETLINK].fd = udev_monitor_get_fd(monitor);
1193
1194         pfd[FD_INOTIFY].fd = udev_watch_init(udev);
1195         if (pfd[FD_INOTIFY].fd < 0) {
1196                 fprintf(stderr, "error initializing inotify\n");
1197                 err(udev, "error initializing inotify\n");
1198                 rc = 4;
1199                 goto exit;
1200         }
1201
1202         if (udev_get_rules_path(udev) != NULL) {
1203                 inotify_add_watch(pfd[FD_INOTIFY].fd, udev_get_rules_path(udev),
1204                                   IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
1205         } else {
1206                 char filename[UTIL_PATH_SIZE];
1207                 struct stat statbuf;
1208
1209                 inotify_add_watch(pfd[FD_INOTIFY].fd, LIBEXECDIR "/rules.d",
1210                                   IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
1211                 inotify_add_watch(pfd[FD_INOTIFY].fd, SYSCONFDIR "/udev/rules.d",
1212                                   IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
1213
1214                 /* watch dynamic rules directory */
1215                 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/rules.d", NULL);
1216                 if (stat(filename, &statbuf) != 0) {
1217                         util_create_path(udev, filename);
1218                         udev_selinux_setfscreatecon(udev, filename, S_IFDIR|0755);
1219                         mkdir(filename, 0755);
1220                         udev_selinux_resetfscreatecon(udev);
1221                 }
1222                 inotify_add_watch(pfd[FD_INOTIFY].fd, filename,
1223                                   IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
1224         }
1225         udev_watch_restore(udev);
1226
1227         /* block and listen to all signals on signalfd */
1228         sigfillset(&mask);
1229         sigprocmask(SIG_SETMASK, &mask, &orig_sigmask);
1230         pfd[FD_SIGNAL].fd = signalfd(-1, &mask, 0);
1231         if (pfd[FD_SIGNAL].fd < 0) {
1232                 fprintf(stderr, "error getting signalfd\n");
1233                 err(udev, "error getting signalfd\n");
1234                 rc = 5;
1235                 goto exit;
1236         }
1237
1238         /* unnamed socket from workers to the main daemon */
1239         if (socketpair(AF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0, worker_watch) < 0) {
1240                 fprintf(stderr, "error getting socketpair\n");
1241                 err(udev, "error getting socketpair\n");
1242                 rc = 6;
1243                 goto exit;
1244         }
1245         pfd[FD_WORKER].fd = worker_watch[READ_END];
1246
1247         rules = udev_rules_new(udev, resolve_names);
1248         if (rules == NULL) {
1249                 err(udev, "error reading rules\n");
1250                 goto exit;
1251         }
1252
1253         udev_queue_export = udev_queue_export_new(udev);
1254         if (udev_queue_export == NULL) {
1255                 err(udev, "error creating queue file\n");
1256                 goto exit;
1257         }
1258
1259         if (!debug) {
1260                 dup2(fd, STDIN_FILENO);
1261                 dup2(fd, STDOUT_FILENO);
1262                 dup2(fd, STDERR_FILENO);
1263         }
1264         if (fd > STDERR_FILENO)
1265                 close(fd);
1266
1267         if (daemonize) {
1268                 pid_t pid;
1269
1270                 pid = fork();
1271                 switch (pid) {
1272                 case 0:
1273                         break;
1274                 case -1:
1275                         err(udev, "fork of daemon failed: %m\n");
1276                         rc = 4;
1277                         goto exit;
1278                 default:
1279                         rc = 0;
1280                         goto exit;
1281                 }
1282         } else {
1283                 init_notify("READY=1");
1284         }
1285
1286         /* set scheduling priority for the main daemon process */
1287         setpriority(PRIO_PROCESS, 0, UDEVD_PRIORITY);
1288
1289         setsid();
1290
1291         f = fopen("/dev/kmsg", "w");
1292         if (f != NULL) {
1293                 fprintf(f, "<6>udev: starting version " VERSION "\n");
1294                 fclose(f);
1295         }
1296
1297         /* OOM_DISABLE == -17 */
1298         fd = open("/proc/self/oom_adj", O_RDWR);
1299         if (fd < 0) {
1300                 err(udev, "error disabling OOM: %m\n");
1301         } else {
1302                 write(fd, "-17", 3);
1303                 close(fd);
1304         }
1305
1306         if (children_max <= 0) {
1307                 int memsize = mem_size_mb();
1308
1309                 /* set value depending on the amount of RAM */
1310                 if (memsize > 0)
1311                         children_max = 128 + (memsize / 8);
1312                 else
1313                         children_max = 128;
1314         }
1315         info(udev, "set children_max to %u\n", children_max);
1316
1317         static_dev_create(udev);
1318         static_dev_create_from_modules(udev);
1319         udev_rules_apply_static_dev_perms(rules);
1320
1321         udev_list_init(&event_list);
1322         udev_list_init(&worker_list);
1323
1324         while (!udev_exit) {
1325                 int fdcount;
1326                 int timeout;
1327
1328                 /* set timeout to kill idle workers */
1329                 if (udev_list_is_empty(&event_list) && children > 2)
1330                         timeout = 3 * 1000;
1331                 else
1332                         timeout = -1;
1333                 /* wait for events */
1334                 fdcount = poll(pfd, ARRAY_SIZE(pfd), timeout);
1335                 if (fdcount < 0)
1336                         continue;
1337
1338                 /* timeout - kill idle workers */
1339                 if (fdcount == 0)
1340                         worker_kill(udev, 2);
1341
1342                 /* event has finished */
1343                 if (pfd[FD_WORKER].revents & POLLIN)
1344                         worker_returned();
1345
1346                 /* get kernel uevent */
1347                 if (pfd[FD_NETLINK].revents & POLLIN) {
1348                         struct udev_device *dev;
1349
1350                         dev = udev_monitor_receive_device(monitor);
1351                         if (dev != NULL)
1352                                 event_queue_insert(dev);
1353                         else
1354                                 udev_device_unref(dev);
1355                 }
1356
1357                 /* start new events */
1358                 if (!udev_list_is_empty(&event_list) && !stop_exec_queue)
1359                         events_start(udev);
1360
1361                 /* get signal */
1362                 if (pfd[FD_SIGNAL].revents & POLLIN) {
1363                         struct signalfd_siginfo fdsi;
1364                         ssize_t size;
1365
1366                         size = read(pfd[FD_SIGNAL].fd, &fdsi, sizeof(struct signalfd_siginfo));
1367                         if (size == sizeof(struct signalfd_siginfo))
1368                                 handle_signal(udev, fdsi.ssi_signo);
1369                 }
1370
1371                 /* device node and rules directory inotify watch */
1372                 if (pfd[FD_INOTIFY].revents & POLLIN)
1373                         handle_inotify(udev);
1374
1375                 /*
1376                  * get control message
1377                  *
1378                  * This needs to be after the inotify handling, to make sure,
1379                  * that the settle signal is send back after the possibly generated
1380                  * "change" events by the inotify device node watch.
1381                  */
1382                 if (pfd[FD_CONTROL].revents & POLLIN)
1383                         handle_ctrl_msg(udev_ctrl);
1384
1385                 /* rules changed, set by inotify or a HUP signal */
1386                 if (reload_config) {
1387                         struct udev_rules *rules_new;
1388
1389                         worker_kill(udev, 0);
1390                         rules_new = udev_rules_new(udev, resolve_names);
1391                         if (rules_new != NULL) {
1392                                 udev_rules_unref(rules);
1393                                 rules = rules_new;
1394                         }
1395                         reload_config = 0;
1396                 }
1397         }
1398
1399         udev_queue_export_cleanup(udev_queue_export);
1400         rc = 0;
1401 exit:
1402         udev_queue_export_unref(udev_queue_export);
1403         udev_rules_unref(rules);
1404         udev_ctrl_unref(udev_ctrl);
1405         if (pfd[FD_SIGNAL].fd >= 0)
1406                 close(pfd[FD_SIGNAL].fd);
1407         if (worker_watch[READ_END] >= 0)
1408                 close(worker_watch[READ_END]);
1409         if (worker_watch[WRITE_END] >= 0)
1410                 close(worker_watch[WRITE_END]);
1411         udev_monitor_unref(monitor);
1412         udev_selinux_exit(udev);
1413         udev_unref(udev);
1414         udev_log_close();
1415         return rc;
1416 }