+ /* set signal handlers */
+ memset(&act, 0x00, sizeof(struct sigaction));
+ act.sa_handler = (void (*)(int)) sig_handler;
+ sigemptyset(&act.sa_mask);
+ act.sa_flags = SA_RESTART;
+ sigaction(SIGINT, &act, NULL);
+ sigaction(SIGTERM, &act, NULL);
+ sigaction(SIGCHLD, &act, NULL);
+ sigaction(SIGHUP, &act, NULL);
+
+ /* watch rules directory */
+ inotify_fd = inotify_init();
+ if (inotify_fd >= 0)
+ inotify_add_watch(inotify_fd, udev_rules_dir, IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
+ else if (errno == ENOSYS)
+ err("the kernel does not support inotify, udevd can't monitor configuration file changes");
+ else
+ err("inotify_init failed: %s", strerror(errno));
+
+ /* maximum limit of forked childs */
+ value = getenv("UDEVD_MAX_CHILDS");
+ if (value)
+ max_childs = strtoul(value, NULL, 10);
+ else {
+ int memsize = mem_size_mb();
+ if (memsize > 0)
+ max_childs = 128 + (memsize / 4);
+ else
+ max_childs = UDEVD_MAX_CHILDS;
+ }
+ info("initialize max_childs to %u", max_childs);
+
+ /* start to throttle forking if maximum number of _running_ childs is reached */
+ value = getenv("UDEVD_MAX_CHILDS_RUNNING");
+ if (value)
+ max_childs_running = strtoull(value, NULL, 10);
+ else {
+ int cpus = cpu_count();
+ if (cpus > 0)
+ max_childs_running = 8 + (8 * cpus);
+ else
+ max_childs_running = UDEVD_MAX_CHILDS_RUNNING;
+ }
+ info("initialize max_childs_running to %u", max_childs_running);
+
+ /* clear environment for forked event processes */
+ clearenv();
+
+ /* export log_priority , as called programs may want to follow that setting */
+ sprintf(udev_log, "UDEV_LOG=%i", udev_log_priority);
+ putenv(udev_log);
+ if (debug_trace)
+ putenv("DEBUG=1");
+
+ maxfd = udevd_sock;
+ maxfd = UDEV_MAX(maxfd, uevent_netlink_sock);
+ maxfd = UDEV_MAX(maxfd, signal_pipe[READ_END]);
+ maxfd = UDEV_MAX(maxfd, inotify_fd);
+
+ while (!udev_exit) {
+ struct udevd_uevent_msg *msg;
+ int fdcount;
+
+ FD_ZERO(&readfds);
+ FD_SET(signal_pipe[READ_END], &readfds);
+ FD_SET(udevd_sock, &readfds);
+ FD_SET(uevent_netlink_sock, &readfds);
+ if (inotify_fd >= 0)
+ FD_SET(inotify_fd, &readfds);
+
+ fdcount = select(maxfd+1, &readfds, NULL, NULL, NULL);
+ if (fdcount < 0) {