2 * Copyright (C) 2004-2006 Kay Sievers <kay.sievers@vrfy.org>
3 * Copyright (C) 2004 Chris Friesen <chris_friesen@sympatico.ca>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation version 2 of the License.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
33 #include <sys/select.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
39 #include <sys/ioctl.h>
40 #include <linux/types.h>
41 #include <linux/netlink.h>
44 #include "udev_rules.h"
46 #include "udev_selinux.h"
48 static int debug_trace;
51 static struct udev_rules rules;
52 static int udevd_sock = -1;
53 static int uevent_netlink_sock = -1;
54 static int inotify_fd = -1;
57 static int signal_pipe[2] = {-1, -1};
58 static volatile int sigchilds_waiting;
59 static volatile int udev_exit;
60 static volatile int reload_config;
61 static int run_exec_q;
62 static int stop_exec_q;
63 static int max_childs;
64 static int max_childs_running;
65 static char udev_log[32];
67 static LIST_HEAD(exec_list);
68 static LIST_HEAD(running_list);
72 void log_message(int priority, const char *format, ...)
76 if (priority > udev_log_priority)
79 va_start(args, format);
81 printf("[%d] ", (int) getpid());
82 vprintf(format, args);
85 vsyslog(priority, format, args);
91 static void asmlinkage udev_event_sig_handler(int signum)
93 if (signum == SIGALRM)
97 static int udev_event_process(struct udevd_uevent_msg *msg)
100 struct udevice *udev;
104 /* set signal handlers */
105 memset(&act, 0x00, sizeof(act));
106 act.sa_handler = (void (*)(int)) udev_event_sig_handler;
107 sigemptyset (&act.sa_mask);
109 sigaction(SIGALRM, &act, NULL);
111 /* reset to default */
112 act.sa_handler = SIG_DFL;
113 sigaction(SIGINT, &act, NULL);
114 sigaction(SIGTERM, &act, NULL);
115 sigaction(SIGCHLD, &act, NULL);
116 sigaction(SIGHUP, &act, NULL);
118 /* trigger timeout to prevent hanging processes */
119 alarm(UDEV_ALARM_TIMEOUT);
121 /* reconstruct event environment from message */
122 for (i = 0; msg->envp[i]; i++)
123 putenv(msg->envp[i]);
125 udev = udev_device_init(NULL);
128 strlcpy(udev->action, msg->action, sizeof(udev->action));
129 sysfs_device_set_values(udev->dev, msg->devpath, msg->subsystem, msg->driver);
130 udev->devpath_old = msg->devpath_old;
131 udev->devt = msg->devt;
133 retval = udev_device_event(&rules, udev);
135 /* run programs collected by RUN-key*/
136 if (retval == 0 && !udev->ignore_device && udev_run)
137 retval = udev_rules_run(udev);
139 udev_device_cleanup(udev);
149 static void export_event_state(struct udevd_uevent_msg *msg, enum event_state state)
151 char filename[PATH_SIZE];
152 char filename_failed[PATH_SIZE];
155 /* location of queue file */
156 snprintf(filename, sizeof(filename), "%s/"EVENT_QUEUE_DIR"/%llu", udev_root, msg->seqnum);
158 /* location of failed file */
159 strlcpy(filename_failed, udev_root, sizeof(filename_failed));
160 strlcat(filename_failed, "/", sizeof(filename_failed));
161 start = strlcat(filename_failed, EVENT_FAILED_DIR"/", sizeof(filename_failed));
162 strlcat(filename_failed, msg->devpath, sizeof(filename_failed));
163 path_encode(&filename_failed[start], sizeof(filename_failed) - start);
167 unlink(filename_failed);
168 delete_path(filename_failed);
170 create_path(filename);
171 selinux_setfscreatecon(filename, NULL, S_IFLNK);
172 symlink(msg->devpath, filename);
173 selinux_resetfscreatecon();
176 if (msg->devpath_old != NULL) {
177 /* "move" event - rename failed file to current name, do not delete failed */
178 char filename_failed_old[PATH_SIZE];
180 strlcpy(filename_failed_old, udev_root, sizeof(filename_failed_old));
181 strlcat(filename_failed_old, "/", sizeof(filename_failed_old));
182 start = strlcat(filename_failed_old, EVENT_FAILED_DIR"/", sizeof(filename_failed_old));
183 strlcat(filename_failed_old, msg->devpath_old, sizeof(filename_failed_old));
184 path_encode(&filename_failed_old[start], sizeof(filename) - start);
186 if (rename(filename_failed_old, filename_failed) == 0)
187 info("renamed devpath, moved failed state of '%s' to %s'",
188 msg->devpath_old, msg->devpath);
190 unlink(filename_failed);
191 delete_path(filename_failed);
195 delete_path(filename);
198 /* move failed event to the failed directory */
199 create_path(filename_failed);
200 rename(filename, filename_failed);
202 /* clean up possibly empty queue directory */
203 delete_path(filename);
210 static void msg_queue_delete(struct udevd_uevent_msg *msg)
212 list_del(&msg->node);
214 /* mark as failed, if "add" event returns non-zero */
215 if (msg->exitstatus && strcmp(msg->action, "add") == 0)
216 export_event_state(msg, EVENT_FAILED);
218 export_event_state(msg, EVENT_FINISHED);
223 static void udev_event_run(struct udevd_uevent_msg *msg)
232 close(uevent_netlink_sock);
236 close(signal_pipe[READ_END]);
237 close(signal_pipe[WRITE_END]);
240 logging_init("udevd-event");
241 setpriority(PRIO_PROCESS, 0, UDEV_PRIORITY);
243 retval = udev_event_process(msg);
244 info("seq %llu finished with %i", msg->seqnum, retval);
251 err("fork of child failed: %s", strerror(errno));
252 msg_queue_delete(msg);
255 /* get SIGCHLD in main loop */
256 info("seq %llu forked, pid [%d], '%s' '%s', %ld seconds old",
257 msg->seqnum, pid, msg->action, msg->subsystem, time(NULL) - msg->queue_time);
262 static void msg_queue_insert(struct udevd_uevent_msg *msg)
264 char filename[PATH_SIZE];
267 msg->queue_time = time(NULL);
269 strlcpy(filename, udev_root, sizeof(filename));
270 strlcat(filename, "/" EVENT_SEQNUM, sizeof(filename));
271 fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644);
276 len = sprintf(str, "%llu\n", msg->seqnum);
281 export_event_state(msg, EVENT_QUEUED);
282 info("seq %llu queued, '%s' '%s'", msg->seqnum, msg->action, msg->subsystem);
284 /* run one event after the other in debug mode */
286 list_add_tail(&msg->node, &running_list);
288 waitpid(msg->pid, NULL, 0);
289 msg_queue_delete(msg);
293 /* run all events with a timeout set immediately */
294 if (msg->timeout != 0) {
295 list_add_tail(&msg->node, &running_list);
300 list_add_tail(&msg->node, &exec_list);
304 static int mem_size_mb(void)
308 long int memsize = -1;
310 f = fopen("/proc/meminfo", "r");
314 while (fgets(buf, sizeof(buf), f) != NULL) {
317 if (sscanf(buf, "MemTotal: %ld kB", &value) == 1) {
318 memsize = value / 1024;
327 static int cpu_count(void)
333 f = fopen("/proc/stat", "r");
337 while (fgets(buf, sizeof(buf), f) != NULL) {
338 if (strncmp(buf, "cpu", 3) == 0 && isdigit(buf[3]))
348 static int running_processes(void)
354 f = fopen("/proc/stat", "r");
358 while (fgets(buf, sizeof(buf), f) != NULL) {
361 if (sscanf(buf, "procs_running %u", &value) == 1) {
371 /* return the number of process es in our session, count only until limit */
372 static int running_processes_in_session(pid_t session, int limit)
378 dir = opendir("/proc");
382 /* read process info from /proc */
383 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
389 pid_t ppid, pgrp, sess;
392 if (!isdigit(dent->d_name[0]))
395 snprintf(procdir, sizeof(procdir), "/proc/%s/stat", dent->d_name);
396 procdir[sizeof(procdir)-1] = '\0';
398 f = open(procdir, O_RDONLY);
402 len = read(f, line, sizeof(line)-1);
410 /* skip ugly program name */
411 pos = strrchr(line, ')') + 2;
415 if (sscanf(pos, "%c %d %d %d ", &state, &ppid, &pgrp, &sess) != 4)
418 /* count only processes in our session */
422 /* count only running, no sleeping processes */
427 if (limit > 0 && running >= limit)
435 static int compare_devpath(const char *running, const char *waiting)
439 for (i = 0; i < PATH_SIZE; i++) {
440 /* identical device event found */
441 if (running[i] == '\0' && waiting[i] == '\0')
444 /* parent device event found */
445 if (running[i] == '\0' && waiting[i] == '/')
448 /* child device event found */
449 if (running[i] == '/' && waiting[i] == '\0')
452 /* no matching event */
453 if (running[i] != waiting[i])
460 /* lookup event for identical, parent, child, or physical device */
461 static int devpath_busy(struct udevd_uevent_msg *msg, int limit)
463 struct udevd_uevent_msg *loop_msg;
464 int childs_count = 0;
466 /* check exec-queue which may still contain delayed events we depend on */
467 list_for_each_entry(loop_msg, &exec_list, node) {
468 /* skip ourself and all later events */
469 if (loop_msg->seqnum >= msg->seqnum)
472 /* check our old name */
473 if (msg->devpath_old != NULL)
474 if (strcmp(loop_msg->devpath , msg->devpath_old) == 0)
477 /* check identical, parent, or child device event */
478 if (compare_devpath(loop_msg->devpath, msg->devpath) != 0) {
479 dbg("%llu, device event still pending %llu (%s)",
480 msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
484 /* check for our major:minor number */
485 if (msg->devt && loop_msg->devt == msg->devt &&
486 strcmp(msg->subsystem, loop_msg->subsystem) == 0) {
487 dbg("%llu, device event still pending %llu (%d:%d)", msg->seqnum,
488 loop_msg->seqnum, major(loop_msg->devt), minor(loop_msg->devt));
492 /* check physical device event (special case of parent) */
493 if (msg->physdevpath && msg->action && strcmp(msg->action, "add") == 0)
494 if (compare_devpath(loop_msg->devpath, msg->physdevpath) != 0) {
495 dbg("%llu, physical device event still pending %llu (%s)",
496 msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
501 /* check run queue for still running events */
502 list_for_each_entry(loop_msg, &running_list, node) {
503 if (limit && childs_count++ > limit) {
504 dbg("%llu, maximum number (%i) of childs reached", msg->seqnum, childs_count);
508 /* check our old name */
509 if (msg->devpath_old != NULL)
510 if (strcmp(loop_msg->devpath , msg->devpath_old) == 0)
513 /* check identical, parent, or child device event */
514 if (compare_devpath(loop_msg->devpath, msg->devpath) != 0) {
515 dbg("%llu, device event still running %llu (%s)",
516 msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
520 /* check for our major:minor number */
521 if (msg->devt && loop_msg->devt == msg->devt &&
522 strcmp(msg->subsystem, loop_msg->subsystem) == 0) {
523 dbg("%llu, device event still running %llu (%d:%d)", msg->seqnum,
524 loop_msg->seqnum, major(loop_msg->devt), minor(loop_msg->devt));
528 /* check physical device event (special case of parent) */
529 if (msg->physdevpath && msg->action && strcmp(msg->action, "add") == 0)
530 if (compare_devpath(loop_msg->devpath, msg->physdevpath) != 0) {
531 dbg("%llu, physical device event still running %llu (%s)",
532 msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
539 /* serializes events for the identical and parent and child devices */
540 static void msg_queue_manager(void)
542 struct udevd_uevent_msg *loop_msg;
543 struct udevd_uevent_msg *tmp_msg;
546 if (list_empty(&exec_list))
549 running = running_processes();
550 dbg("%d processes runnning on system", running);
552 running = max_childs_running;
554 list_for_each_entry_safe(loop_msg, tmp_msg, &exec_list, node) {
555 /* check running processes in our session and possibly throttle */
556 if (running >= max_childs_running) {
557 running = running_processes_in_session(sid, max_childs_running+10);
558 dbg("at least %d processes running in session", running);
559 if (running >= max_childs_running) {
560 dbg("delay seq %llu, too many processes already running", loop_msg->seqnum);
565 /* serialize and wait for parent or child events */
566 if (devpath_busy(loop_msg, max_childs) != 0) {
567 dbg("delay seq %llu (%s)", loop_msg->seqnum, loop_msg->devpath);
571 /* move event to run list */
572 list_move_tail(&loop_msg->node, &running_list);
573 udev_event_run(loop_msg);
575 dbg("moved seq %llu to running list", loop_msg->seqnum);
579 static struct udevd_uevent_msg *get_msg_from_envbuf(const char *buf, int buf_size)
583 struct udevd_uevent_msg *msg;
584 char *physdevdriver_key = NULL;
588 msg = malloc(sizeof(struct udevd_uevent_msg) + buf_size);
591 memset(msg, 0x00, sizeof(struct udevd_uevent_msg) + buf_size);
593 /* copy environment buffer and reconstruct envp */
594 memcpy(msg->envbuf, buf, buf_size);
596 for (i = 0; (bufpos < buf_size) && (i < UEVENT_NUM_ENVP-2); i++) {
600 key = &msg->envbuf[bufpos];
601 keylen = strlen(key);
603 bufpos += keylen + 1;
604 dbg("add '%s' to msg.envp[%i]", msg->envp[i], i);
606 /* remember some keys for further processing */
607 if (strncmp(key, "ACTION=", 7) == 0)
608 msg->action = &key[7];
609 else if (strncmp(key, "DEVPATH=", 8) == 0)
610 msg->devpath = &key[8];
611 else if (strncmp(key, "SUBSYSTEM=", 10) == 0)
612 msg->subsystem = &key[10];
613 else if (strncmp(key, "DRIVER=", 7) == 0)
614 msg->driver = &key[7];
615 else if (strncmp(key, "SEQNUM=", 7) == 0)
616 msg->seqnum = strtoull(&key[7], NULL, 10);
617 else if (strncmp(key, "DEVPATH_OLD=", 12) == 0)
618 msg->devpath_old = &key[12];
619 else if (strncmp(key, "PHYSDEVPATH=", 12) == 0)
620 msg->physdevpath = &key[12];
621 else if (strncmp(key, "PHYSDEVDRIVER=", 14) == 0)
622 physdevdriver_key = key;
623 else if (strncmp(key, "MAJOR=", 6) == 0)
624 maj = strtoull(&key[6], NULL, 10);
625 else if (strncmp(key, "MINOR=", 6) == 0)
626 min = strtoull(&key[6], NULL, 10);
627 else if (strncmp(key, "TIMEOUT=", 8) == 0)
628 msg->timeout = strtoull(&key[8], NULL, 10);
630 msg->devt = makedev(maj, min);
631 msg->envp[i++] = "UDEVD_EVENT=1";
633 if (msg->driver == NULL && msg->physdevpath == NULL && physdevdriver_key != NULL) {
634 /* for older kernels DRIVER is empty for a bus device, export PHYSDEVDRIVER as DRIVER */
635 msg->envp[i++] = &physdevdriver_key[7];
636 msg->driver = &physdevdriver_key[14];
641 if (msg->devpath == NULL || msg->action == NULL) {
642 info("DEVPATH or ACTION missing, ignore message");
649 /* receive the udevd message from userspace */
650 static void get_ctrl_msg(void)
652 struct udevd_ctrl_msg ctrl_msg;
655 struct cmsghdr *cmsg;
658 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
662 memset(&ctrl_msg, 0x00, sizeof(struct udevd_ctrl_msg));
663 iov.iov_base = &ctrl_msg;
664 iov.iov_len = sizeof(struct udevd_ctrl_msg);
666 memset(&smsg, 0x00, sizeof(struct msghdr));
669 smsg.msg_control = cred_msg;
670 smsg.msg_controllen = sizeof(cred_msg);
672 size = recvmsg(udevd_sock, &smsg, 0);
675 err("unable to receive user udevd message: %s", strerror(errno));
678 cmsg = CMSG_FIRSTHDR(&smsg);
679 cred = (struct ucred *) CMSG_DATA(cmsg);
681 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
682 err("no sender credentials received, message ignored");
686 if (cred->uid != 0) {
687 err("sender uid=%i, message ignored", cred->uid);
691 if (strncmp(ctrl_msg.magic, UDEVD_CTRL_MAGIC, sizeof(UDEVD_CTRL_MAGIC)) != 0 ) {
692 err("message magic '%s' doesn't match, ignore it", ctrl_msg.magic);
696 switch (ctrl_msg.type) {
698 pos = strchr(ctrl_msg.buf, '=');
700 err("wrong key format '%s'", ctrl_msg.buf);
704 if (pos[1] == '\0') {
705 info("udevd message (ENV) received, unset '%s'", ctrl_msg.buf);
706 unsetenv(ctrl_msg.buf);
708 info("udevd message (ENV) received, set '%s=%s'", ctrl_msg.buf, &pos[1]);
709 setenv(ctrl_msg.buf, &pos[1], 1);
712 case UDEVD_CTRL_STOP_EXEC_QUEUE:
713 info("udevd message (STOP_EXEC_QUEUE) received");
716 case UDEVD_CTRL_START_EXEC_QUEUE:
717 info("udevd message (START_EXEC_QUEUE) received");
721 case UDEVD_CTRL_SET_LOG_LEVEL:
722 intval = (int *) ctrl_msg.buf;
723 info("udevd message (SET_LOG_PRIORITY) received, udev_log_priority=%i", *intval);
724 udev_log_priority = *intval;
725 sprintf(udev_log, "UDEV_LOG=%i", udev_log_priority);
728 case UDEVD_CTRL_SET_MAX_CHILDS:
729 intval = (int *) ctrl_msg.buf;
730 info("udevd message (UDEVD_SET_MAX_CHILDS) received, max_childs=%i", *intval);
731 max_childs = *intval;
733 case UDEVD_CTRL_SET_MAX_CHILDS_RUNNING:
734 intval = (int *) ctrl_msg.buf;
735 info("udevd message (UDEVD_SET_MAX_CHILDS_RUNNING) received, max_childs=%i", *intval);
736 max_childs_running = *intval;
738 case UDEVD_CTRL_RELOAD_RULES:
739 info("udevd message (RELOAD_RULES) received");
743 err("unknown control message type");
747 /* receive the kernel user event message and do some sanity checks */
748 static struct udevd_uevent_msg *get_netlink_msg(void)
750 struct udevd_uevent_msg *msg;
753 static char buffer[UEVENT_BUFFER_SIZE+512];
756 size = recv(uevent_netlink_sock, &buffer, sizeof(buffer), 0);
759 err("unable to receive kernel netlink message: %s", strerror(errno));
763 if ((size_t)size > sizeof(buffer)-1)
764 size = sizeof(buffer)-1;
766 dbg("uevent_size=%zi", size);
768 /* start of event payload */
769 bufpos = strlen(buffer)+1;
770 msg = get_msg_from_envbuf(&buffer[bufpos], size-bufpos);
774 /* validate message */
775 pos = strchr(buffer, '@');
777 err("invalid uevent '%s'", buffer);
783 if (msg->action == NULL) {
784 info("no ACTION in payload found, skip event '%s'", buffer);
789 if (strcmp(msg->action, buffer) != 0) {
790 err("ACTION in payload does not match uevent, skip event '%s'", buffer);
798 static void asmlinkage sig_handler(int signum)
806 /* set flag, then write to pipe if needed */
807 sigchilds_waiting = 1;
814 /* write to pipe, which will wakeup select() in our mainloop */
815 write(signal_pipe[WRITE_END], "", 1);
818 static void udev_done(int pid, int exitstatus)
820 /* find msg associated with pid and delete it */
821 struct udevd_uevent_msg *msg;
823 list_for_each_entry(msg, &running_list, node) {
824 if (msg->pid == pid) {
825 info("seq %llu, pid [%d] exit with %i, %ld seconds old", msg->seqnum, msg->pid,
826 exitstatus, time(NULL) - msg->queue_time);
827 msg->exitstatus = exitstatus;
828 msg_queue_delete(msg);
830 /* there may be events waiting with the same devpath */
837 static void reap_sigchilds(void)
843 pid = waitpid(-1, &status, WNOHANG);
846 if (WIFEXITED(status))
847 status = WEXITSTATUS(status);
848 else if (WIFSIGNALED(status))
849 status = WTERMSIG(status) + 128;
852 udev_done(pid, status);
856 static int init_udevd_socket(void)
858 struct sockaddr_un saddr;
860 const int feature_on = 1;
863 memset(&saddr, 0x00, sizeof(saddr));
864 saddr.sun_family = AF_LOCAL;
865 /* use abstract namespace for socket path */
866 strcpy(&saddr.sun_path[1], UDEVD_CTRL_SOCK_PATH);
867 addrlen = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(&saddr.sun_path[1]);
869 udevd_sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
870 if (udevd_sock == -1) {
871 err("error getting socket: %s", strerror(errno));
875 /* the bind takes care of ensuring only one copy running */
876 retval = bind(udevd_sock, (struct sockaddr *) &saddr, addrlen);
878 err("bind failed: %s", strerror(errno));
884 /* enable receiving of the sender credentials */
885 setsockopt(udevd_sock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on));
890 static int init_uevent_netlink_sock(void)
892 struct sockaddr_nl snl;
893 const int buffersize = 16 * 1024 * 1024;
896 memset(&snl, 0x00, sizeof(struct sockaddr_nl));
897 snl.nl_family = AF_NETLINK;
898 snl.nl_pid = getpid();
901 uevent_netlink_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
902 if (uevent_netlink_sock == -1) {
903 err("error getting socket: %s", strerror(errno));
907 /* set receive buffersize */
908 setsockopt(uevent_netlink_sock, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize));
910 retval = bind(uevent_netlink_sock, (struct sockaddr *) &snl, sizeof(struct sockaddr_nl));
912 err("bind failed: %s", strerror(errno));
913 close(uevent_netlink_sock);
914 uevent_netlink_sock = -1;
920 static void export_initial_seqnum(void)
922 char filename[PATH_SIZE];
927 strlcpy(filename, sysfs_path, sizeof(filename));
928 strlcat(filename, "/kernel/uevent_seqnum", sizeof(filename));
929 fd = open(filename, O_RDONLY);
931 len = read(fd, seqnum, sizeof(seqnum)-1);
935 strcpy(seqnum, "0\n");
938 strlcpy(filename, udev_root, sizeof(filename));
939 strlcat(filename, "/" EVENT_SEQNUM, sizeof(filename));
940 create_path(filename);
941 fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644);
943 write(fd, seqnum, len);
948 int main(int argc, char *argv[], char *envp[])
952 struct sigaction act;
957 static const struct option options[] = {
958 { "daemon", 0, NULL, 'd' },
959 { "debug-trace", 0, NULL, 't' },
960 { "verbose", 0, NULL, 'v' },
961 { "help", 0, NULL, 'h' },
962 { "version", 0, NULL, 'V' },
968 logging_init("udevd");
971 dbg("version %s", UDEV_VERSION);
974 option = getopt_long(argc, argv, "dtvhV", options, NULL);
987 if (udev_log_priority < LOG_INFO)
988 udev_log_priority = LOG_INFO;
991 printf("Usage: udevd [--help] [--daemon] [--debug-trace] [--verbose] [--version]\n");
994 printf("%s\n", UDEV_VERSION);
1001 if (getuid() != 0) {
1002 fprintf(stderr, "root privileges required\n");
1003 err("root privileges required");
1007 /* make sure std{in,out,err} fd's are in a sane state */
1008 fd = open("/dev/null", O_RDWR);
1010 fprintf(stderr, "cannot open /dev/null\n");
1011 err("cannot open /dev/null");
1013 if (fd > STDIN_FILENO)
1014 dup2(fd, STDIN_FILENO);
1015 if (write(STDOUT_FILENO, 0, 0) < 0)
1016 dup2(fd, STDOUT_FILENO);
1017 if (write(STDERR_FILENO, 0, 0) < 0)
1018 dup2(fd, STDERR_FILENO);
1020 /* init sockets to receive events */
1021 if (init_udevd_socket() < 0) {
1022 if (errno == EADDRINUSE) {
1023 fprintf(stderr, "another udev daemon already running\n");
1024 err("another udev daemon already running");
1027 fprintf(stderr, "error initializing udevd socket\n");
1028 err("error initializing udevd socket");
1034 if (init_uevent_netlink_sock() < 0) {
1035 fprintf(stderr, "error initializing netlink socket\n");
1036 err("error initializing netlink socket");
1041 /* setup signal handler pipe */
1042 retval = pipe(signal_pipe);
1044 err("error getting pipes: %s", strerror(errno));
1048 retval = fcntl(signal_pipe[READ_END], F_GETFL, 0);
1050 err("error fcntl on read pipe: %s", strerror(errno));
1053 retval = fcntl(signal_pipe[READ_END], F_SETFL, retval | O_NONBLOCK);
1055 err("error fcntl on read pipe: %s", strerror(errno));
1059 retval = fcntl(signal_pipe[WRITE_END], F_GETFL, 0);
1061 err("error fcntl on write pipe: %s", strerror(errno));
1064 retval = fcntl(signal_pipe[WRITE_END], F_SETFL, retval | O_NONBLOCK);
1066 err("error fcntl on write pipe: %s", strerror(errno));
1070 /* parse the rules and keep them in memory */
1072 udev_rules_init(&rules, 1);
1074 export_initial_seqnum();
1082 dbg("daemonized fork running");
1085 err("fork of daemon failed: %s", strerror(errno));
1089 dbg("child [%u] running, parent exits", pid);
1095 /* redirect std{out,err} fd's */
1097 dup2(fd, STDOUT_FILENO);
1098 dup2(fd, STDERR_FILENO);
1099 if (fd > STDERR_FILENO)
1102 /* set scheduling priority for the daemon */
1103 setpriority(PRIO_PROCESS, 0, UDEVD_PRIORITY);
1108 /* become session leader */
1110 dbg("our session is %d", sid);
1112 /* OOM_DISABLE == -17 */
1113 fd = open("/proc/self/oom_adj", O_RDWR);
1115 err("error disabling OOM: %s", strerror(errno));
1117 write(fd, "-17", 3);
1121 fd = open("/dev/kmsg", O_WRONLY);
1123 const char *str = "<6>udevd version " UDEV_VERSION " started\n";
1125 write(fd, str, strlen(str));
1129 /* set signal handlers */
1130 memset(&act, 0x00, sizeof(struct sigaction));
1131 act.sa_handler = (void (*)(int)) sig_handler;
1132 sigemptyset(&act.sa_mask);
1133 act.sa_flags = SA_RESTART;
1134 sigaction(SIGINT, &act, NULL);
1135 sigaction(SIGTERM, &act, NULL);
1136 sigaction(SIGCHLD, &act, NULL);
1137 sigaction(SIGHUP, &act, NULL);
1139 /* watch rules directory */
1140 inotify_fd = inotify_init();
1141 if (inotify_fd >= 0) {
1142 char filename[PATH_MAX];
1144 inotify_add_watch(inotify_fd, udev_rules_dir, IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
1146 /* watch dynamic rules directory */
1147 strlcpy(filename, udev_root, sizeof(filename));
1148 strlcat(filename, "/"RULES_DYN_DIR, sizeof(filename));
1149 inotify_add_watch(inotify_fd, filename, IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
1150 } else if (errno == ENOSYS)
1151 err("the kernel does not support inotify, udevd can't monitor rules file changes");
1153 err("inotify_init failed: %s", strerror(errno));
1155 /* maximum limit of forked childs */
1156 value = getenv("UDEVD_MAX_CHILDS");
1158 max_childs = strtoul(value, NULL, 10);
1160 int memsize = mem_size_mb();
1162 max_childs = 128 + (memsize / 4);
1164 max_childs = UDEVD_MAX_CHILDS;
1166 info("initialize max_childs to %u", max_childs);
1168 /* start to throttle forking if maximum number of _running_ childs is reached */
1169 value = getenv("UDEVD_MAX_CHILDS_RUNNING");
1171 max_childs_running = strtoull(value, NULL, 10);
1173 int cpus = cpu_count();
1175 max_childs_running = 8 + (8 * cpus);
1177 max_childs_running = UDEVD_MAX_CHILDS_RUNNING;
1179 info("initialize max_childs_running to %u", max_childs_running);
1181 /* clear environment for forked event processes */
1184 /* export log_priority , as called programs may want to follow that setting */
1185 sprintf(udev_log, "UDEV_LOG=%i", udev_log_priority);
1191 maxfd = UDEV_MAX(maxfd, uevent_netlink_sock);
1192 maxfd = UDEV_MAX(maxfd, signal_pipe[READ_END]);
1193 maxfd = UDEV_MAX(maxfd, inotify_fd);
1195 while (!udev_exit) {
1196 struct udevd_uevent_msg *msg;
1200 FD_SET(signal_pipe[READ_END], &readfds);
1201 FD_SET(udevd_sock, &readfds);
1202 FD_SET(uevent_netlink_sock, &readfds);
1203 if (inotify_fd >= 0)
1204 FD_SET(inotify_fd, &readfds);
1206 fdcount = select(maxfd+1, &readfds, NULL, NULL, NULL);
1209 err("error in select: %s", strerror(errno));
1213 /* get control message */
1214 if (FD_ISSET(udevd_sock, &readfds))
1217 /* get netlink message */
1218 if (FD_ISSET(uevent_netlink_sock, &readfds)) {
1219 msg = get_netlink_msg();
1221 msg_queue_insert(msg);
1224 /* received a signal, clear our notification pipe */
1225 if (FD_ISSET(signal_pipe[READ_END], &readfds)) {
1228 read(signal_pipe[READ_END], &buf, sizeof(buf));
1231 /* rules directory inotify watch */
1232 if ((inotify_fd >= 0) && FD_ISSET(inotify_fd, &readfds)) {
1235 /* discard all possible events, we can just reload the config */
1236 if ((ioctl(inotify_fd, FIONREAD, &nbytes) == 0) && nbytes > 0) {
1240 buf = malloc(nbytes);
1242 err("error getting buffer for inotify, disable watching");
1246 read(inotify_fd, buf, nbytes);
1251 /* rules changed, set by inotify or a HUP signal */
1252 if (reload_config) {
1254 udev_rules_cleanup(&rules);
1255 udev_rules_init(&rules, 1);
1258 /* forked child has returned */
1259 if (sigchilds_waiting) {
1260 sigchilds_waiting = 0;
1267 msg_queue_manager();
1273 udev_rules_cleanup(&rules);
1277 if (signal_pipe[READ_END] >= 0)
1278 close(signal_pipe[READ_END]);
1279 if (signal_pipe[WRITE_END] >= 0)
1280 close(signal_pipe[WRITE_END]);
1282 if (udevd_sock >= 0)
1284 if (inotify_fd >= 0)
1286 if (uevent_netlink_sock >= 0)
1287 close(uevent_netlink_sock);