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>
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.
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.
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/>.
35 #include <sys/prctl.h>
36 #include <sys/socket.h>
38 #include <sys/signalfd.h>
39 #include <sys/select.h>
43 #include <sys/ioctl.h>
44 #include <sys/inotify.h>
45 #include <sys/utsname.h>
48 #include "sd-daemon.h"
50 #define UDEVD_PRIORITY -4
51 #define UDEV_PRIORITY -2
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)
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);
70 vsyslog(priority, format, args);
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;
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;
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 },
114 struct udev_list_node node;
116 struct udev_device *dev;
117 enum event_state state;
119 unsigned long long int delaying_seqnum;
120 unsigned long long int seqnum;
123 const char *devpath_old;
128 static struct event *node_to_event(struct udev_list_node *node)
132 event = (char *)node;
133 event -= offsetof(struct event, node);
134 return (struct event *)event;
145 struct udev_list_node node;
149 struct udev_monitor *monitor;
150 enum worker_state state;
154 /* passed from worker to main process */
155 struct worker_message {
160 static struct worker *node_to_worker(struct udev_list_node *node)
164 worker = (char *)node;
165 worker -= offsetof(struct worker, node);
166 return (struct worker *)worker;
169 static void event_queue_delete(struct event *event)
171 udev_list_node_remove(&event->node);
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);
177 udev_queue_export_device_finished(udev_queue_export, event->dev);
179 info(event->udev, "seq %llu done with %i\n", udev_device_get_seqnum(event->dev), event->exitcode);
180 udev_device_unref(event->dev);
184 static void event_sig_handler(int signum)
196 static struct worker *worker_ref(struct worker *worker)
202 static void worker_unref(struct worker *worker)
205 if (worker->refcount > 0)
208 udev_list_node_remove(&worker->node);
209 udev_monitor_unref(worker->monitor);
211 info(worker->udev, "worker [%u] cleaned up\n", worker->pid);
215 static void worker_new(struct event *event)
217 struct worker *worker;
218 struct udev_monitor *worker_monitor;
220 struct sigaction act;
222 /* listen for new events */
223 worker_monitor = udev_monitor_new_from_netlink(event->udev, NULL);
224 if (worker_monitor == NULL)
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);
230 worker = calloc(1, sizeof(struct worker));
231 if (worker == NULL) {
232 udev_monitor_unref(worker_monitor);
235 /* worker + event reference */
236 worker->refcount = 2;
237 worker->udev = event->udev;
243 struct udev_device *dev;
244 struct pollfd pmon = {
245 .fd = udev_monitor_get_fd(worker_monitor),
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]);
255 udev_log_init("udevd-work");
256 setpriority(PRIO_PROCESS, 0, UDEV_PRIORITY);
258 /* set signal handlers */
259 memset(&act, 0x00, sizeof(act));
260 act.sa_handler = event_sig_handler;
261 sigemptyset (&act.sa_mask);
263 sigaction(SIGTERM, &act, NULL);
264 sigaction(SIGALRM, &act, NULL);
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);
273 /* request TERM signal if parent exits */
274 prctl(PR_SET_PDEATHSIG, SIGTERM);
280 struct udev_event *udev_event;
281 struct worker_message msg = {};
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)
290 /* set timeout to prevent hanging processes */
291 alarm(UDEV_EVENT_TIMEOUT);
294 udev_event->exec_delay = exec_delay;
296 /* apply rules, create node, symlinks */
297 err = udev_event_execute_rules(udev_event, rules);
299 /* rules may change/disable the timeout */
300 if (udev_device_get_event_timeout(dev) >= 0)
301 alarm(udev_device_get_event_timeout(dev));
304 failed = udev_event_execute_run(udev_event, &orig_sigmask);
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);
314 /* send processed event back to libudev listeners */
315 udev_monitor_send_device(worker_monitor, NULL, dev);
317 /* send udevd the result of the event execution */
320 else if (failed != 0)
321 msg.exitcode = failed;
323 send(worker_watch[WRITE_END], &msg, sizeof(struct worker_message), 0);
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);
330 /* wait for more device messages or signal from udevd */
331 while (!worker_exit) {
334 fdcount = ppoll(&pmon, 1, NULL, &sigmask);
338 if (pmon.revents & POLLIN) {
339 dev = udev_monitor_receive_device(worker_monitor);
344 } while (dev != NULL);
346 udev_monitor_unref(worker_monitor);
351 udev_monitor_unref(worker_monitor);
352 event->state = EVENT_QUEUED;
354 err(event->udev, "fork of child failed: %m\n");
357 /* close monitor, but keep address around */
358 udev_monitor_disconnect(worker_monitor);
359 worker->monitor = worker_monitor;
361 worker->state = WORKER_RUNNING;
362 worker->event = event;
363 event->state = EVENT_RUNNING;
364 udev_list_node_append(&worker->node, &worker_list);
366 info(event->udev, "seq %llu forked new worker [%u]\n", udev_device_get_seqnum(event->dev), pid);
371 static void event_run(struct event *event, bool force)
373 struct udev_list_node *loop;
375 udev_list_node_foreach(loop, &worker_list) {
376 struct worker *worker = node_to_worker(loop);
379 if (worker->state != WORKER_IDLE)
382 count = udev_monitor_send_device(monitor, worker->monitor, event->dev);
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;
390 worker->event = event;
391 worker->state = WORKER_RUNNING;
392 event->state = EVENT_RUNNING;
396 if (!force && children >= children_max) {
397 if (children_max > 1)
398 info(event->udev, "maximum number (%i) of children reached\n", children);
402 /* start new worker and pass initial device */
406 static int event_queue_insert(struct udev_device *dev)
410 event = calloc(1, sizeof(struct event));
414 event->udev = udev_device_get_udev(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);
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));
427 event->state = EVENT_QUEUED;
428 udev_list_node_append(&event->node, &event_list);
430 /* run all events with a timeout set immediately */
431 if (udev_device_get_timeout(dev) > 0) {
432 event_run(event, true);
439 static void worker_kill(struct udev *udev, int retain)
441 struct udev_list_node *loop;
444 if (children <= retain)
447 max = children - retain;
449 udev_list_node_foreach(loop, &worker_list) {
450 struct worker *worker = node_to_worker(loop);
455 if (worker->state == WORKER_KILLED)
458 worker->state = WORKER_KILLED;
459 kill(worker->pid, SIGTERM);
463 /* lookup event for identical, parent, child device */
464 static bool is_devpath_busy(struct event *event)
466 struct udev_list_node *loop;
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);
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)
477 /* event we checked earlier still exists, no need to check again */
478 if (loop_event->seqnum == event->delaying_seqnum)
481 /* found ourself, no later event can block us */
482 if (loop_event->seqnum >= event->seqnum)
485 /* check major/minor */
486 if (major(event->devnum) != 0 && event->devnum == loop_event->devnum && event->is_block == loop_event->is_block)
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;
495 /* compare devpath */
496 common = MIN(loop_event->devpath_len, event->devpath_len);
498 /* one devpath is contained in the other? */
499 if (memcmp(loop_event->devpath, event->devpath, common) != 0)
502 /* identical device event found */
503 if (loop_event->devpath_len == event->devpath_len) {
504 event->delaying_seqnum = loop_event->seqnum;
508 /* parent device event found */
509 if (event->devpath[common] == '/') {
510 event->delaying_seqnum = loop_event->seqnum;
514 /* child device event found */
515 if (loop_event->devpath[common] == '/') {
516 event->delaying_seqnum = loop_event->seqnum;
520 /* no matching device */
527 static void events_start(struct udev *udev)
529 struct udev_list_node *loop;
531 udev_list_node_foreach(loop, &event_list) {
532 struct event *event = node_to_event(loop);
534 if (event->state != EVENT_QUEUED)
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);
543 event_run(event, false);
547 static void worker_returned(void)
550 struct worker_message msg;
552 struct udev_list_node *loop;
554 size = recv(pfd[FD_WORKER].fd, &msg, sizeof(struct worker_message), MSG_DONTWAIT);
555 if (size != sizeof(struct worker_message))
558 /* lookup worker who sent the signal */
559 udev_list_node_foreach(loop, &worker_list) {
560 struct worker *worker = node_to_worker(loop);
562 if (worker->pid != msg.pid)
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);
577 /* receive the udevd message from userspace */
578 static void handle_ctrl_msg(struct udev_ctrl *uctrl)
580 struct udev *udev = udev_ctrl_get_udev(uctrl);
581 struct udev_ctrl_msg *ctrl_msg;
585 ctrl_msg = udev_ctrl_receive_msg(uctrl);
586 if (ctrl_msg == NULL)
589 i = udev_ctrl_get_set_log_level(ctrl_msg);
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);
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;
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;
606 if (udev_ctrl_get_reload_rules(ctrl_msg) > 0) {
607 info(udev, "udevd message (RELOAD_RULES) received\n");
608 reload_config = true;
611 str = udev_ctrl_get_set_env(ctrl_msg);
619 val = strchr(key, '=');
623 if (val[0] == '\0') {
624 info(udev, "udevd message (ENV) received, unset '%s'\n", key);
625 udev_add_property(udev, key, NULL);
627 info(udev, "udevd message (ENV) received, set '%s=%s'\n", key, val);
628 udev_add_property(udev, key, val);
631 err(udev, "wrong key format '%s'\n", key);
635 worker_kill(udev, 0);
638 i = udev_ctrl_get_set_children_max(ctrl_msg);
640 info(udev, "udevd message (SET_MAX_CHILDREN) received, children_max=%i\n", i);
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);
650 udev_ctrl_msg_unref(ctrl_msg);
653 /* read inotify messages */
654 static int handle_inotify(struct udev *udev)
658 struct inotify_event *ev;
660 if ((ioctl(pfd[FD_INOTIFY].fd, FIONREAD, &nbytes) < 0) || (nbytes <= 0))
663 buf = malloc(nbytes);
665 err(udev, "error getting buffer for inotify\n");
669 nbytes = read(pfd[FD_INOTIFY].fd, buf, nbytes);
671 for (pos = 0; pos < nbytes; pos += sizeof(struct inotify_event) + ev->len) {
672 struct udev_device *dev;
674 ev = (struct inotify_event *)(buf + pos);
678 info(udev, "inotify event: %x for %s\n", ev->mask, ev->name);
679 s = strstr(ev->name, ".rules");
682 if (strlen(s) != strlen(".rules"))
684 reload_config = true;
688 dev = udev_watch_lookup(udev, ev->wd);
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];
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");
702 if (ev->mask & IN_IGNORED)
703 udev_watch_end(udev, dev);
705 udev_device_unref(dev);
714 static void handle_signal(struct udev *udev, int signo)
725 struct udev_list_node *loop, *tmp;
727 pid = waitpid(-1, &status, WNOHANG);
731 udev_list_node_foreach_safe(loop, tmp, &worker_list) {
732 struct worker *worker = node_to_worker(loop);
734 if (worker->pid != pid)
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);
748 worker_unref(worker);
754 reload_config = true;
759 static void static_dev_create_from_modules(struct udev *udev)
761 struct utsname kernel;
762 char modules[UTIL_PATH_SIZE];
767 util_strscpyl(modules, sizeof(modules), "/lib/modules/", kernel.release, "/modules.devname", NULL);
768 f = fopen(modules, "r");
772 while (fgets(buf, sizeof(buf), f) != NULL) {
780 char filename[UTIL_PATH_SIZE];
786 s = strchr(modname, ' ');
792 s = strchr(devname, ' ');
798 s = strchr(devno, ' ');
800 s = strchr(devno, '\n');
803 if (sscanf(devno, "%c%u:%u", &type, &maj, &min) != 3)
807 mode = 0600 | S_IFCHR;
808 else if (type == 'b')
809 mode = 0600 | S_IFBLK;
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);
825 static int copy_dir(struct udev *udev, DIR *dir_from, DIR *dir_to, int maxdepth)
829 for (dent = readdir(dir_from); dent != NULL; dent = readdir(dir_from)) {
832 if (dent->d_name[0] == '.')
834 if (fstatat(dirfd(dir_from), dent->d_name, &stats, AT_SYMLINK_NOFOLLOW) != 0)
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);
843 utimensat(dirfd(dir_to), dent->d_name, NULL, 0);
845 udev_selinux_resetfscreatecon(udev);
846 } else if (S_ISLNK(stats.st_mode)) {
847 char target[UTIL_PATH_SIZE];
850 len = readlinkat(dirfd(dir_from), dent->d_name, target, sizeof(target));
851 if (len <= 0 || len == (ssize_t)sizeof(target))
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;
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);
868 dir2_to = fdopendir(openat(dirfd(dir_to), dent->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC));
872 dir2_from = fdopendir(openat(dirfd(dir_from), dent->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC));
873 if (dir2_from == NULL) {
878 copy_dir(udev, dir2_from, dir2_to, maxdepth-1);
888 static void static_dev_create_links(struct udev *udev, DIR *dir)
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" },
903 for (i = 0; i < ARRAY_SIZE(stdlinks); i++) {
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);
915 static void static_dev_create_from_devices(struct udev *udev, DIR *dir)
919 dir_from = opendir(LIBEXECDIR "/devices");
920 if (dir_from == NULL)
922 copy_dir(udev, dir_from, dir, 8);
926 static void static_dev_create(struct udev *udev)
930 dir = opendir(udev_get_dev_path(udev));
934 static_dev_create_links(udev, dir);
935 static_dev_create_from_devices(udev, dir);
940 static int mem_size_mb(void)
944 long int memsize = -1;
946 f = fopen("/proc/meminfo", "r");
950 while (fgets(buf, sizeof(buf), f) != NULL) {
953 if (sscanf(buf, "MemTotal: %ld kB", &value) == 1) {
954 memsize = value / 1024;
963 int main(int argc, char *argv[])
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' },
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);
995 option = getopt_long(argc, argv, "c:deDthV", options, NULL);
1004 children_max = strtoul(optarg, NULL, 0);
1007 exec_delay = strtoul(optarg, NULL, 0);
1011 if (udev_get_log_priority(udev) < LOG_INFO)
1012 udev_set_log_priority(udev, LOG_INFO);
1015 if (strcmp (optarg, "early") == 0) {
1017 } else if (strcmp (optarg, "late") == 0) {
1019 } else if (strcmp (optarg, "never") == 0) {
1022 fprintf(stderr, "resolve-names must be early, late or never\n");
1023 err(udev, "resolve-names must be early, late or never\n");
1028 printf("Usage: udevd OPTIONS\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"
1039 printf("%s\n", VERSION);
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
1052 f = fopen("/proc/cmdline", "r");
1056 if (fgets(cmdline, sizeof(cmdline), f) != NULL) {
1059 pos = strstr(cmdline, "udev.log-priority=");
1061 pos += strlen("udev.log-priority=");
1062 udev_set_log_priority(udev, util_log_priority(pos));
1065 pos = strstr(cmdline, "udev.children-max=");
1067 pos += strlen("udev.children-max=");
1068 children_max = strtoul(pos, NULL, 0);
1071 pos = strstr(cmdline, "udev.exec-delay=");
1073 pos += strlen("udev.exec-delay=");
1074 exec_delay = strtoul(pos, NULL, 0);
1080 if (getuid() != 0) {
1081 fprintf(stderr, "root privileges required\n");
1082 err(udev, "root privileges required\n");
1086 /* set umask before creating any file/directory */
1090 /* before opening new files, make sure std{in,out,err} fds are in a sane state */
1091 fd = open("/dev/null", O_RDWR);
1093 fprintf(stderr, "cannot open /dev/null\n");
1094 err(udev, "cannot open /dev/null\n");
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);
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");
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");
1114 pfd[FD_CONTROL].fd = udev_ctrl_get_fd(udev_ctrl);
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");
1123 udev_monitor_set_receive_buffer_size(monitor, 128*1024*1024);
1124 pfd[FD_NETLINK].fd = udev_monitor_get_fd(monitor);
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");
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);
1138 char filename[UTIL_PATH_SIZE];
1139 struct stat statbuf;
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);
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);
1154 inotify_add_watch(pfd[FD_INOTIFY].fd, filename,
1155 IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
1157 udev_watch_restore(udev);
1159 /* block and listen to all signals on signalfd */
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");
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");
1177 pfd[FD_WORKER].fd = worker_watch[READ_END];
1179 rules = udev_rules_new(udev, resolve_names);
1180 if (rules == NULL) {
1181 err(udev, "error reading rules\n");
1185 udev_queue_export = udev_queue_export_new(udev);
1186 if (udev_queue_export == NULL) {
1187 err(udev, "error creating queue file\n");
1192 dup2(fd, STDIN_FILENO);
1193 dup2(fd, STDOUT_FILENO);
1194 dup2(fd, STDERR_FILENO);
1196 if (fd > STDERR_FILENO)
1207 err(udev, "fork of daemon failed: %m\n");
1215 sd_notify(1, "READY=1");
1218 /* set scheduling priority for the main daemon process */
1219 setpriority(PRIO_PROCESS, 0, UDEVD_PRIORITY);
1223 f = fopen("/dev/kmsg", "w");
1225 fprintf(f, "<6>udev[%u]: starting version " VERSION "\n", getpid());
1229 fd = open("/proc/self/oom_score_adj", O_RDWR);
1231 /* Fallback to old interface */
1232 fd = open("/proc/self/oom_adj", O_RDWR);
1234 err(udev, "error disabling OOM: %m\n");
1236 /* OOM_DISABLE == -17 */
1237 write(fd, "-17", 3);
1241 write(fd, "-1000", 5);
1245 if (children_max <= 0) {
1246 int memsize = mem_size_mb();
1248 /* set value depending on the amount of RAM */
1250 children_max = 128 + (memsize / 8);
1254 info(udev, "set children_max to %u\n", children_max);
1256 static_dev_create(udev);
1257 static_dev_create_from_modules(udev);
1258 udev_rules_apply_static_dev_perms(rules);
1260 udev_list_init(&event_list);
1261 udev_list_init(&worker_list);
1263 while (!udev_exit) {
1267 /* set timeout to kill idle workers */
1268 if (udev_list_is_empty(&event_list) && children > 2)
1272 /* wait for events */
1273 fdcount = poll(pfd, ARRAY_SIZE(pfd), timeout);
1277 /* timeout - kill idle workers */
1279 worker_kill(udev, 2);
1281 /* event has finished */
1282 if (pfd[FD_WORKER].revents & POLLIN)
1285 /* get kernel uevent */
1286 if (pfd[FD_NETLINK].revents & POLLIN) {
1287 struct udev_device *dev;
1289 dev = udev_monitor_receive_device(monitor);
1291 if (event_queue_insert(dev) < 0)
1292 udev_device_unref(dev);
1295 /* start new events */
1296 if (!udev_list_is_empty(&event_list) && !stop_exec_queue)
1300 if (pfd[FD_SIGNAL].revents & POLLIN) {
1301 struct signalfd_siginfo fdsi;
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);
1309 /* device node and rules directory inotify watch */
1310 if (pfd[FD_INOTIFY].revents & POLLIN)
1311 handle_inotify(udev);
1314 * get control message
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.
1320 if (pfd[FD_CONTROL].revents & POLLIN)
1321 handle_ctrl_msg(udev_ctrl);
1323 /* rules changed, set by inotify or a HUP signal */
1324 if (reload_config) {
1325 struct udev_rules *rules_new;
1327 worker_kill(udev, 0);
1328 rules_new = udev_rules_new(udev, resolve_names);
1329 if (rules_new != NULL) {
1330 udev_rules_unref(rules);
1337 udev_queue_export_cleanup(udev_queue_export);
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);