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