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.
35 #include <sys/select.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
41 #include <sys/ioctl.h>
42 #include <linux/types.h>
43 #include <linux/netlink.h>
45 #include <sys/inotify.h>
49 #include "udev_rules.h"
50 #include "udev_selinux.h"
52 #define UDEVD_PRIORITY -4
53 #define UDEV_PRIORITY -2
55 /* maximum limit of forked childs */
56 #define UDEVD_MAX_CHILDS 256
57 /* start to throttle forking if maximum number of running childs in our session is reached */
58 #define UDEVD_MAX_CHILDS_RUNNING 16
62 static void log_fn(struct udev *udev, int priority,
63 const char *file, int line, const char *fn,
64 const char *format, va_list args)
67 fprintf(stderr, "[%d] %s: ", (int) getpid(), fn);
68 vfprintf(stderr, format, args);
70 vsyslog(priority, format, args);
74 struct udevd_uevent_msg {
76 struct list_head node;
85 unsigned long long seqnum;
89 char *envp[UEVENT_NUM_ENVP+1];
93 static int debug_trace;
94 static struct udev_rules rules;
95 static struct udev_ctrl *udev_ctrl;
96 static int uevent_netlink_sock = -1;
97 static int inotify_fd = -1;
100 static int signal_pipe[2] = {-1, -1};
101 static volatile int sigchilds_waiting;
102 static volatile int udev_exit;
103 static volatile int reload_config;
104 static int run_exec_q;
105 static int stop_exec_q;
106 static int max_childs;
107 static int max_childs_running;
108 static char udev_log_env[32];
110 static LIST_HEAD(exec_list);
111 static LIST_HEAD(running_list);
113 static void asmlinkage udev_event_sig_handler(int signum)
115 if (signum == SIGALRM)
119 static int udev_event_process(struct udevd_uevent_msg *msg)
121 struct sigaction act;
122 struct udevice *udevice;
126 /* set signal handlers */
127 memset(&act, 0x00, sizeof(act));
128 act.sa_handler = (void (*)(int)) udev_event_sig_handler;
129 sigemptyset (&act.sa_mask);
131 sigaction(SIGALRM, &act, NULL);
133 /* reset to default */
134 act.sa_handler = SIG_DFL;
135 sigaction(SIGINT, &act, NULL);
136 sigaction(SIGTERM, &act, NULL);
137 sigaction(SIGCHLD, &act, NULL);
138 sigaction(SIGHUP, &act, NULL);
140 /* trigger timeout to prevent hanging processes */
141 alarm(UDEV_EVENT_TIMEOUT);
143 /* reconstruct event environment from message */
144 for (i = 0; msg->envp[i]; i++)
145 putenv(msg->envp[i]);
147 udevice = udev_device_init(msg->udev);
150 strlcpy(udevice->action, msg->action, sizeof(udevice->action));
151 sysfs_device_set_values(udevice->udev, udevice->dev, msg->devpath, msg->subsystem, msg->driver);
152 udevice->devpath_old = msg->devpath_old;
153 udevice->devt = msg->devt;
155 retval = udev_device_event(&rules, udevice);
157 /* rules may change/disable the timeout */
158 if (udevice->event_timeout >= 0)
159 alarm(udevice->event_timeout);
161 /* run programs collected by RUN-key*/
162 if (retval == 0 && !udevice->ignore_device && udev_get_run(msg->udev))
163 retval = udev_rules_run(udevice);
165 udev_device_cleanup(udevice);
175 static void export_event_state(struct udevd_uevent_msg *msg, enum event_state state)
177 char filename[PATH_SIZE];
178 char filename_failed[PATH_SIZE];
181 /* location of queue file */
182 snprintf(filename, sizeof(filename), "%s/.udev/queue/%llu", udev_get_dev_path(msg->udev), msg->seqnum);
184 /* location of failed file */
185 strlcpy(filename_failed, udev_get_dev_path(msg->udev), sizeof(filename_failed));
186 strlcat(filename_failed, "/", sizeof(filename_failed));
187 start = strlcat(filename_failed, ".udev/failed/", sizeof(filename_failed));
188 strlcat(filename_failed, msg->devpath, sizeof(filename_failed));
189 path_encode(&filename_failed[start], sizeof(filename_failed) - start);
193 unlink(filename_failed);
194 delete_path(msg->udev, filename_failed);
195 create_path(msg->udev, filename);
196 selinux_setfscreatecon(msg->udev, filename, NULL, S_IFLNK);
197 symlink(msg->devpath, filename);
198 selinux_resetfscreatecon(msg->udev);
201 if (msg->devpath_old != NULL) {
202 /* "move" event - rename failed file to current name, do not delete failed */
203 char filename_failed_old[PATH_SIZE];
205 strlcpy(filename_failed_old, udev_get_dev_path(msg->udev), sizeof(filename_failed_old));
206 strlcat(filename_failed_old, "/", sizeof(filename_failed_old));
207 start = strlcat(filename_failed_old, ".udev/failed/", sizeof(filename_failed_old));
208 strlcat(filename_failed_old, msg->devpath_old, sizeof(filename_failed_old));
209 path_encode(&filename_failed_old[start], sizeof(filename) - start);
211 if (rename(filename_failed_old, filename_failed) == 0)
212 info(msg->udev, "renamed devpath, moved failed state of '%s' to %s'\n",
213 msg->devpath_old, msg->devpath);
215 unlink(filename_failed);
216 delete_path(msg->udev, filename_failed);
220 delete_path(msg->udev, filename);
223 /* move failed event to the failed directory */
224 create_path(msg->udev, filename_failed);
225 rename(filename, filename_failed);
227 /* clean up possibly empty queue directory */
228 delete_path(msg->udev, filename);
235 static void msg_queue_delete(struct udevd_uevent_msg *msg)
237 list_del(&msg->node);
239 /* mark as failed, if "add" event returns non-zero */
240 if (msg->exitstatus && strcmp(msg->action, "add") == 0)
241 export_event_state(msg, EVENT_FAILED);
243 export_event_state(msg, EVENT_FINISHED);
248 static void udev_event_run(struct udevd_uevent_msg *msg)
257 close(uevent_netlink_sock);
258 udev_ctrl_unref(udev_ctrl);
261 close(signal_pipe[READ_END]);
262 close(signal_pipe[WRITE_END]);
264 logging_init("udevd-event");
265 setpriority(PRIO_PROCESS, 0, UDEV_PRIORITY);
267 retval = udev_event_process(msg);
268 info(msg->udev, "seq %llu finished with %i\n", msg->seqnum, retval);
275 err(msg->udev, "fork of child failed: %s\n", strerror(errno));
276 msg_queue_delete(msg);
279 /* get SIGCHLD in main loop */
280 info(msg->udev, "seq %llu forked, pid [%d], '%s' '%s', %ld seconds old\n",
281 msg->seqnum, pid, msg->action, msg->subsystem, time(NULL) - msg->queue_time);
286 static void msg_queue_insert(struct udevd_uevent_msg *msg)
288 char filename[PATH_SIZE];
291 msg->queue_time = time(NULL);
293 export_event_state(msg, EVENT_QUEUED);
294 info(msg->udev, "seq %llu queued, '%s' '%s'\n", msg->seqnum, msg->action, msg->subsystem);
296 strlcpy(filename, udev_get_dev_path(msg->udev), sizeof(filename));
297 strlcat(filename, "/.udev/uevent_seqnum", sizeof(filename));
298 fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644);
303 len = sprintf(str, "%llu\n", msg->seqnum);
308 /* run one event after the other in debug mode */
310 list_add_tail(&msg->node, &running_list);
312 waitpid(msg->pid, NULL, 0);
313 msg_queue_delete(msg);
317 /* run all events with a timeout set immediately */
318 if (msg->timeout != 0) {
319 list_add_tail(&msg->node, &running_list);
324 list_add_tail(&msg->node, &exec_list);
328 static int mem_size_mb(void)
332 long int memsize = -1;
334 f = fopen("/proc/meminfo", "r");
338 while (fgets(buf, sizeof(buf), f) != NULL) {
341 if (sscanf(buf, "MemTotal: %ld kB", &value) == 1) {
342 memsize = value / 1024;
351 static int cpu_count(void)
357 f = fopen("/proc/stat", "r");
361 while (fgets(buf, sizeof(buf), f) != NULL) {
362 if (strncmp(buf, "cpu", 3) == 0 && isdigit(buf[3]))
372 static int running_processes(void)
378 f = fopen("/proc/stat", "r");
382 while (fgets(buf, sizeof(buf), f) != NULL) {
385 if (sscanf(buf, "procs_running %u", &value) == 1) {
395 /* return the number of process es in our session, count only until limit */
396 static int running_processes_in_session(pid_t session, int limit)
402 dir = opendir("/proc");
406 /* read process info from /proc */
407 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
413 pid_t ppid, pgrp, sess;
416 if (!isdigit(dent->d_name[0]))
419 snprintf(procdir, sizeof(procdir), "/proc/%s/stat", dent->d_name);
420 procdir[sizeof(procdir)-1] = '\0';
422 f = open(procdir, O_RDONLY);
426 len = read(f, line, sizeof(line)-1);
434 /* skip ugly program name */
435 pos = strrchr(line, ')') + 2;
439 if (sscanf(pos, "%c %d %d %d ", &state, &ppid, &pgrp, &sess) != 4)
442 /* count only processes in our session */
446 /* count only running, no sleeping processes */
451 if (limit > 0 && running >= limit)
459 static int compare_devpath(const char *running, const char *waiting)
463 for (i = 0; i < PATH_SIZE; i++) {
464 /* identical device event found */
465 if (running[i] == '\0' && waiting[i] == '\0')
468 /* parent device event found */
469 if (running[i] == '\0' && waiting[i] == '/')
472 /* child device event found */
473 if (running[i] == '/' && waiting[i] == '\0')
476 /* no matching event */
477 if (running[i] != waiting[i])
484 /* lookup event for identical, parent, child, or physical device */
485 static int devpath_busy(struct udevd_uevent_msg *msg, int limit)
487 struct udevd_uevent_msg *loop_msg;
488 int childs_count = 0;
490 /* check exec-queue which may still contain delayed events we depend on */
491 list_for_each_entry(loop_msg, &exec_list, node) {
492 /* skip ourself and all later events */
493 if (loop_msg->seqnum >= msg->seqnum)
496 /* check our old name */
497 if (msg->devpath_old != NULL)
498 if (strcmp(loop_msg->devpath , msg->devpath_old) == 0)
501 /* check identical, parent, or child device event */
502 if (compare_devpath(loop_msg->devpath, msg->devpath) != 0) {
503 dbg(msg->udev, "%llu, device event still pending %llu (%s)\n",
504 msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
508 /* check for our major:minor number */
509 if (msg->devt && loop_msg->devt == msg->devt &&
510 strcmp(msg->subsystem, loop_msg->subsystem) == 0) {
511 dbg(msg->udev, "%llu, device event still pending %llu (%d:%d)\n", msg->seqnum,
512 loop_msg->seqnum, major(loop_msg->devt), minor(loop_msg->devt));
516 /* check physical device event (special case of parent) */
517 if (msg->physdevpath && msg->action && strcmp(msg->action, "add") == 0)
518 if (compare_devpath(loop_msg->devpath, msg->physdevpath) != 0) {
519 dbg(msg->udev, "%llu, physical device event still pending %llu (%s)\n",
520 msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
525 /* check run queue for still running events */
526 list_for_each_entry(loop_msg, &running_list, node) {
527 if (limit && childs_count++ > limit) {
528 dbg(msg->udev, "%llu, maximum number (%i) of childs reached\n", msg->seqnum, childs_count);
532 /* check our old name */
533 if (msg->devpath_old != NULL)
534 if (strcmp(loop_msg->devpath , msg->devpath_old) == 0)
537 /* check identical, parent, or child device event */
538 if (compare_devpath(loop_msg->devpath, msg->devpath) != 0) {
539 dbg(msg->udev, "%llu, device event still running %llu (%s)\n",
540 msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
544 /* check for our major:minor number */
545 if (msg->devt && loop_msg->devt == msg->devt &&
546 strcmp(msg->subsystem, loop_msg->subsystem) == 0) {
547 dbg(msg->udev, "%llu, device event still running %llu (%d:%d)\n", msg->seqnum,
548 loop_msg->seqnum, major(loop_msg->devt), minor(loop_msg->devt));
552 /* check physical device event (special case of parent) */
553 if (msg->physdevpath && msg->action && strcmp(msg->action, "add") == 0)
554 if (compare_devpath(loop_msg->devpath, msg->physdevpath) != 0) {
555 dbg(msg->udev, "%llu, physical device event still running %llu (%s)\n",
556 msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
563 /* serializes events for the identical and parent and child devices */
564 static void msg_queue_manager(struct udev *udev)
566 struct udevd_uevent_msg *loop_msg;
567 struct udevd_uevent_msg *tmp_msg;
570 if (list_empty(&exec_list))
573 running = running_processes();
574 dbg(udev, "%d processes runnning on system\n", running);
576 running = max_childs_running;
578 list_for_each_entry_safe(loop_msg, tmp_msg, &exec_list, node) {
579 /* check running processes in our session and possibly throttle */
580 if (running >= max_childs_running) {
581 running = running_processes_in_session(sid, max_childs_running+10);
582 dbg(udev, "at least %d processes running in session\n", running);
583 if (running >= max_childs_running) {
584 dbg(udev, "delay seq %llu, too many processes already running\n", loop_msg->seqnum);
589 /* serialize and wait for parent or child events */
590 if (devpath_busy(loop_msg, max_childs) != 0) {
591 dbg(udev, "delay seq %llu (%s)\n", loop_msg->seqnum, loop_msg->devpath);
595 /* move event to run list */
596 list_move_tail(&loop_msg->node, &running_list);
597 udev_event_run(loop_msg);
599 dbg(udev, "moved seq %llu to running list\n", loop_msg->seqnum);
603 static struct udevd_uevent_msg *get_msg_from_envbuf(struct udev *udev, const char *buf, int buf_size)
607 struct udevd_uevent_msg *msg;
608 char *physdevdriver_key = NULL;
612 msg = malloc(sizeof(struct udevd_uevent_msg) + buf_size);
615 memset(msg, 0x00, sizeof(struct udevd_uevent_msg) + buf_size);
618 /* copy environment buffer and reconstruct envp */
619 memcpy(msg->envbuf, buf, buf_size);
621 for (i = 0; (bufpos < buf_size) && (i < UEVENT_NUM_ENVP-2); i++) {
625 key = &msg->envbuf[bufpos];
626 keylen = strlen(key);
628 bufpos += keylen + 1;
629 dbg(udev, "add '%s' to msg.envp[%i]\n", msg->envp[i], i);
631 /* remember some keys for further processing */
632 if (strncmp(key, "ACTION=", 7) == 0)
633 msg->action = &key[7];
634 else if (strncmp(key, "DEVPATH=", 8) == 0)
635 msg->devpath = &key[8];
636 else if (strncmp(key, "SUBSYSTEM=", 10) == 0)
637 msg->subsystem = &key[10];
638 else if (strncmp(key, "DRIVER=", 7) == 0)
639 msg->driver = &key[7];
640 else if (strncmp(key, "SEQNUM=", 7) == 0)
641 msg->seqnum = strtoull(&key[7], NULL, 10);
642 else if (strncmp(key, "DEVPATH_OLD=", 12) == 0)
643 msg->devpath_old = &key[12];
644 else if (strncmp(key, "PHYSDEVPATH=", 12) == 0)
645 msg->physdevpath = &key[12];
646 else if (strncmp(key, "PHYSDEVDRIVER=", 14) == 0)
647 physdevdriver_key = key;
648 else if (strncmp(key, "MAJOR=", 6) == 0)
649 maj = strtoull(&key[6], NULL, 10);
650 else if (strncmp(key, "MINOR=", 6) == 0)
651 min = strtoull(&key[6], NULL, 10);
652 else if (strncmp(key, "TIMEOUT=", 8) == 0)
653 msg->timeout = strtoull(&key[8], NULL, 10);
655 msg->devt = makedev(maj, min);
656 msg->envp[i++] = "UDEVD_EVENT=1";
658 if (msg->driver == NULL && msg->physdevpath == NULL && physdevdriver_key != NULL) {
659 /* for older kernels DRIVER is empty for a bus device, export PHYSDEVDRIVER as DRIVER */
660 msg->envp[i++] = &physdevdriver_key[7];
661 msg->driver = &physdevdriver_key[14];
666 if (msg->devpath == NULL || msg->action == NULL) {
667 info(udev, "DEVPATH or ACTION missing, ignore message\n");
674 /* receive the udevd message from userspace */
675 static void handle_ctrl_msg(struct udev_ctrl *uctrl)
677 struct udev *udev = udev_ctrl_get_udev(uctrl);
678 struct udev_ctrl_msg *ctrl_msg;
682 ctrl_msg = udev_ctrl_receive_msg(uctrl);
683 if (ctrl_msg == NULL)
686 i = udev_ctrl_get_set_log_level(ctrl_msg);
688 info(udev, "udevd message (SET_LOG_PRIORITY) received, log_priority=%i\n", i);
689 udev_set_log_priority(udev, i);
690 sprintf(udev_log_env, "UDEV_LOG=%i", i);
691 putenv(udev_log_env);
694 if (udev_ctrl_get_stop_exec_queue(ctrl_msg) > 0) {
695 info(udev, "udevd message (STOP_EXEC_QUEUE) received\n");
699 if (udev_ctrl_get_start_exec_queue(ctrl_msg) > 0) {
700 info(udev, "udevd message (START_EXEC_QUEUE) received\n");
702 msg_queue_manager(udev);
705 if (udev_ctrl_get_reload_rules(ctrl_msg) > 0) {
706 info(udev, "udevd message (RELOAD_RULES) received\n");
710 str = udev_ctrl_get_set_env(ctrl_msg);
712 char *key = strdup(str);
715 val = strchr(str, '=');
719 if (val[0] == '\0') {
720 info(udev, "udevd message (ENV) received, unset '%s'\n", key);
723 info(udev, "udevd message (ENV) received, set '%s=%s'\n", key, val);
727 err(udev, "wrong key format '%s'\n", key);
732 i = udev_ctrl_get_set_max_childs(ctrl_msg);
734 info(udev, "udevd message (SET_MAX_CHILDS) received, max_childs=%i\n", i);
738 i = udev_ctrl_get_set_max_childs_running(ctrl_msg);
740 info(udev, "udevd message (SET_MAX_CHILDS_RUNNING) received, max_childs_running=%i\n", i);
741 max_childs_running = i;
744 udev_ctrl_msg_unref(ctrl_msg);
747 /* receive the kernel user event message and do some sanity checks */
748 static struct udevd_uevent_msg *get_netlink_msg(struct udev *udev)
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(udev, "unable to receive kernel netlink message: %s\n", strerror(errno));
763 if ((size_t)size > sizeof(buffer)-1)
764 size = sizeof(buffer)-1;
766 dbg(udev, "uevent_size=%zi\n", size);
768 /* start of event payload */
769 bufpos = strlen(buffer)+1;
770 msg = get_msg_from_envbuf(udev, &buffer[bufpos], size-bufpos);
774 /* validate message */
775 pos = strchr(buffer, '@');
777 err(udev, "invalid uevent '%s'\n", buffer);
783 if (msg->action == NULL) {
784 info(udev, "no ACTION in payload found, skip event '%s'\n", buffer);
789 if (strcmp(msg->action, buffer) != 0) {
790 err(udev, "ACTION in payload does not match uevent, skip event '%s'\n", 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(msg->udev, "seq %llu, pid [%d] exit with %i, %ld seconds old\n", 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_uevent_netlink_sock(struct udev *udev)
858 struct sockaddr_nl snl;
859 const int buffersize = 16 * 1024 * 1024;
862 memset(&snl, 0x00, sizeof(struct sockaddr_nl));
863 snl.nl_family = AF_NETLINK;
864 snl.nl_pid = getpid();
867 uevent_netlink_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
868 if (uevent_netlink_sock == -1) {
869 err(udev, "error getting socket: %s\n", strerror(errno));
873 /* set receive buffersize */
874 setsockopt(uevent_netlink_sock, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize));
876 retval = bind(uevent_netlink_sock, (struct sockaddr *) &snl, sizeof(struct sockaddr_nl));
878 err(udev, "bind failed: %s\n", strerror(errno));
879 close(uevent_netlink_sock);
880 uevent_netlink_sock = -1;
886 static void export_initial_seqnum(struct udev *udev)
888 char filename[PATH_SIZE];
893 strlcpy(filename, udev_get_sys_path(udev), sizeof(filename));
894 strlcat(filename, "/kernel/uevent_seqnum", sizeof(filename));
895 fd = open(filename, O_RDONLY);
897 len = read(fd, seqnum, sizeof(seqnum)-1);
901 strcpy(seqnum, "0\n");
904 strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
905 strlcat(filename, "/.udev/uevent_seqnum", sizeof(filename));
906 create_path(udev, filename);
907 fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644);
909 write(fd, seqnum, len);
914 int main(int argc, char *argv[])
919 struct sigaction act;
923 static const struct option options[] = {
924 { "daemon", 0, NULL, 'd' },
925 { "debug-trace", 0, NULL, 't' },
926 { "debug", 0, NULL, 'D' },
927 { "help", 0, NULL, 'h' },
928 { "version", 0, NULL, 'V' },
938 logging_init("udevd");
939 udev_set_log_fn(udev, log_fn);
942 dbg(udev, "version %s\n", VERSION);
947 option = getopt_long(argc, argv, "dDthV", options, NULL);
960 if (udev_get_log_priority(udev) < LOG_INFO)
961 udev_set_log_priority(udev, LOG_INFO);
964 printf("Usage: udevd [--help] [--daemon] [--debug-trace] [--debug] [--version]\n");
967 printf("%s\n", VERSION);
975 fprintf(stderr, "root privileges required\n");
976 err(udev, "root privileges required\n");
980 /* make sure std{in,out,err} fd's are in a sane state */
981 fd = open("/dev/null", O_RDWR);
983 fprintf(stderr, "cannot open /dev/null\n");
984 err(udev, "cannot open /dev/null\n");
986 if (fd > STDIN_FILENO)
987 dup2(fd, STDIN_FILENO);
988 if (write(STDOUT_FILENO, 0, 0) < 0)
989 dup2(fd, STDOUT_FILENO);
990 if (write(STDERR_FILENO, 0, 0) < 0)
991 dup2(fd, STDERR_FILENO);
993 /* init control socket, bind() ensures, that only one udevd instance is running */
994 udev_ctrl = udev_ctrl_new_from_socket(udev, UDEV_CTRL_SOCK_PATH);
995 if (udev_ctrl == NULL) {
996 fprintf(stderr, "error initializing control socket");
997 err(udev, "error initializing udevd socket");
1002 if (udev_ctrl_enable_receiving(udev_ctrl) < 0) {
1003 fprintf(stderr, "error binding control socket, seems udevd is already running\n");
1004 err(udev, "error binding control socket, seems udevd is already running\n");
1009 if (init_uevent_netlink_sock(udev) < 0) {
1010 fprintf(stderr, "error initializing netlink socket\n");
1011 err(udev, "error initializing netlink socket\n");
1016 retval = pipe(signal_pipe);
1018 err(udev, "error getting pipes: %s\n", strerror(errno));
1022 retval = fcntl(signal_pipe[READ_END], F_GETFL, 0);
1024 err(udev, "error fcntl on read pipe: %s\n", strerror(errno));
1027 retval = fcntl(signal_pipe[READ_END], F_SETFL, retval | O_NONBLOCK);
1029 err(udev, "error fcntl on read pipe: %s\n", strerror(errno));
1033 retval = fcntl(signal_pipe[WRITE_END], F_GETFL, 0);
1035 err(udev, "error fcntl on write pipe: %s\n", strerror(errno));
1038 retval = fcntl(signal_pipe[WRITE_END], F_SETFL, retval | O_NONBLOCK);
1040 err(udev, "error fcntl on write pipe: %s\n", strerror(errno));
1044 /* parse the rules and keep them in memory */
1046 udev_rules_init(udev, &rules, 1);
1048 export_initial_seqnum(udev);
1056 dbg(udev, "daemonized fork running\n");
1059 err(udev, "fork of daemon failed: %s\n", strerror(errno));
1063 dbg(udev, "child [%u] running, parent exits\n", pid);
1069 /* redirect std{out,err} */
1071 dup2(fd, STDOUT_FILENO);
1072 dup2(fd, STDERR_FILENO);
1074 if (fd > STDERR_FILENO)
1077 /* set scheduling priority for the daemon */
1078 setpriority(PRIO_PROCESS, 0, UDEVD_PRIORITY);
1083 /* become session leader */
1085 dbg(udev, "our session is %d\n", sid);
1087 /* OOM_DISABLE == -17 */
1088 fd = open("/proc/self/oom_adj", O_RDWR);
1090 err(udev, "error disabling OOM: %s\n", strerror(errno));
1092 write(fd, "-17", 3);
1096 fd = open("/dev/kmsg", O_WRONLY);
1098 const char *str = "<6>udevd version " VERSION " started\n";
1100 write(fd, str, strlen(str));
1104 /* set signal handlers */
1105 memset(&act, 0x00, sizeof(struct sigaction));
1106 act.sa_handler = (void (*)(int)) sig_handler;
1107 sigemptyset(&act.sa_mask);
1108 act.sa_flags = SA_RESTART;
1109 sigaction(SIGINT, &act, NULL);
1110 sigaction(SIGTERM, &act, NULL);
1111 sigaction(SIGCHLD, &act, NULL);
1112 sigaction(SIGHUP, &act, NULL);
1114 /* watch rules directory */
1115 inotify_fd = inotify_init();
1116 if (inotify_fd >= 0) {
1117 if (udev_get_rules_path(udev) != NULL) {
1118 inotify_add_watch(inotify_fd, udev_get_rules_path(udev),
1119 IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
1121 char filename[PATH_MAX];
1123 inotify_add_watch(inotify_fd, UDEV_PREFIX "/lib/udev/rules.d",
1124 IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
1125 inotify_add_watch(inotify_fd, SYSCONFDIR "/udev/rules.d",
1126 IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
1128 /* watch dynamic rules directory */
1129 strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
1130 strlcat(filename, "/.udev/rules.d", sizeof(filename));
1131 inotify_add_watch(inotify_fd, filename,
1132 IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
1134 } else if (errno == ENOSYS)
1135 err(udev, "the kernel does not support inotify, udevd can't monitor rules file changes\n");
1137 err(udev, "inotify_init failed: %s\n", strerror(errno));
1139 /* maximum limit of forked childs */
1140 value = getenv("UDEVD_MAX_CHILDS");
1142 max_childs = strtoul(value, NULL, 10);
1144 int memsize = mem_size_mb();
1146 max_childs = 128 + (memsize / 4);
1148 max_childs = UDEVD_MAX_CHILDS;
1150 info(udev, "initialize max_childs to %u\n", max_childs);
1152 /* start to throttle forking if maximum number of _running_ childs is reached */
1153 value = getenv("UDEVD_MAX_CHILDS_RUNNING");
1155 max_childs_running = strtoull(value, NULL, 10);
1157 int cpus = cpu_count();
1159 max_childs_running = 8 + (8 * cpus);
1161 max_childs_running = UDEVD_MAX_CHILDS_RUNNING;
1163 info(udev, "initialize max_childs_running to %u\n", max_childs_running);
1165 /* clear environment for forked event processes */
1168 /* export log_priority , as called programs may want to follow that setting */
1169 sprintf(udev_log_env, "UDEV_LOG=%i", udev_get_log_priority(udev));
1170 putenv(udev_log_env);
1174 maxfd = udev_ctrl_get_fd(udev_ctrl);
1175 maxfd = UDEV_MAX(maxfd, uevent_netlink_sock);
1176 maxfd = UDEV_MAX(maxfd, signal_pipe[READ_END]);
1177 maxfd = UDEV_MAX(maxfd, inotify_fd);
1179 while (!udev_exit) {
1180 struct udevd_uevent_msg *msg;
1184 FD_SET(signal_pipe[READ_END], &readfds);
1185 FD_SET(udev_ctrl_get_fd(udev_ctrl), &readfds);
1186 FD_SET(uevent_netlink_sock, &readfds);
1187 if (inotify_fd >= 0)
1188 FD_SET(inotify_fd, &readfds);
1190 fdcount = select(maxfd+1, &readfds, NULL, NULL, NULL);
1193 err(udev, "error in select: %s\n", strerror(errno));
1197 /* get control message */
1198 if (FD_ISSET(udev_ctrl_get_fd(udev_ctrl), &readfds))
1199 handle_ctrl_msg(udev_ctrl);
1201 /* get netlink message */
1202 if (FD_ISSET(uevent_netlink_sock, &readfds)) {
1203 msg = get_netlink_msg(udev);
1205 msg_queue_insert(msg);
1208 /* received a signal, clear our notification pipe */
1209 if (FD_ISSET(signal_pipe[READ_END], &readfds)) {
1212 read(signal_pipe[READ_END], &buf, sizeof(buf));
1215 /* rules directory inotify watch */
1216 if ((inotify_fd >= 0) && FD_ISSET(inotify_fd, &readfds)) {
1219 /* discard all possible events, we can just reload the config */
1220 if ((ioctl(inotify_fd, FIONREAD, &nbytes) == 0) && nbytes > 0) {
1224 buf = malloc(nbytes);
1226 err(udev, "error getting buffer for inotify, disable watching\n");
1230 read(inotify_fd, buf, nbytes);
1235 /* rules changed, set by inotify or a HUP signal */
1236 if (reload_config) {
1238 udev_rules_cleanup(&rules);
1239 udev_rules_init(udev, &rules, 1);
1242 /* forked child has returned */
1243 if (sigchilds_waiting) {
1244 sigchilds_waiting = 0;
1251 msg_queue_manager(udev);
1257 udev_rules_cleanup(&rules);
1261 if (signal_pipe[READ_END] >= 0)
1262 close(signal_pipe[READ_END]);
1263 if (signal_pipe[WRITE_END] >= 0)
1264 close(signal_pipe[WRITE_END]);
1266 udev_ctrl_unref(udev_ctrl);
1267 if (inotify_fd >= 0)
1269 if (uevent_netlink_sock >= 0)
1270 close(uevent_netlink_sock);