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