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