+ /* become session leader */
+ sid = setsid();
+ dbg("our session is %d", sid);
+
+ chdir("/");
+ umask(umask(077) | 022);
+
+ /* set a reasonable scheduling priority for the daemon */
+ setpriority(PRIO_PROCESS, 0, UDEVD_PRIORITY);
+
+ /* setup signal handler pipe */
+ retval = pipe(signal_pipe);
+ if (retval < 0) {
+ err("error getting pipes: %s", strerror(errno));
+ goto exit;
+ }
+ retval = fcntl(signal_pipe[READ_END], F_SETFL, O_NONBLOCK);
+ if (retval < 0) {
+ err("error fcntl on read pipe: %s", strerror(errno));
+ goto exit;
+ }
+ retval = fcntl(signal_pipe[READ_END], F_SETFD, FD_CLOEXEC);
+ if (retval < 0)
+ err("error fcntl on read pipe: %s", strerror(errno));
+ retval = fcntl(signal_pipe[WRITE_END], F_SETFL, O_NONBLOCK);
+ if (retval < 0) {
+ err("error fcntl on write pipe: %s", strerror(errno));
+ goto exit;
+ }
+ retval = fcntl(signal_pipe[WRITE_END], F_SETFD, FD_CLOEXEC);
+ if (retval < 0)
+ err("error fcntl on write pipe: %s", strerror(errno));
+
+ /* 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(SIGALRM, &act, NULL);
+ sigaction(SIGCHLD, &act, NULL);
+ sigaction(SIGHUP, &act, NULL);
+
+ if (init_udevd_socket() < 0) {
+ if (errno == EADDRINUSE)
+ dbg("another udevd running, exit");
+ else
+ dbg("error initialising udevd socket: %s", strerror(errno));
+
+ goto exit;
+ }
+
+ if (init_uevent_netlink_sock() < 0)
+ info("uevent socket not available");
+
+ /* override of forked udev binary, used for testing */
+ udev_bin = getenv("UDEV_BIN");
+ if (udev_bin != NULL)
+ info("udev binary is set to '%s'", udev_bin);
+ else
+ udev_bin = UDEV_BIN;
+
+ /* init of expected_seqnum value */
+ value = getenv("UDEVD_EXPECTED_SEQNUM");
+ if (value) {
+ expected_seqnum = strtoull(value, NULL, 10);
+ info("initialize expected_seqnum to %llu", expected_seqnum);
+ }
+
+ /* timeout to wait for missing events */
+ value = getenv("UDEVD_EVENT_TIMEOUT");
+ if (value)
+ event_timeout = strtoul(value, NULL, 10);
+ else
+ event_timeout = UDEVD_EVENT_TIMEOUT;
+ info("initialize event_timeout to %u", event_timeout);
+
+ /* maximum limit of forked childs */
+ value = getenv("UDEVD_MAX_CHILDS");
+ if (value)
+ max_childs = strtoul(value, NULL, 10);
+ 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
+ max_childs_running = UDEVD_MAX_CHILDS_RUNNING;
+ info("initialize max_childs_running to %u", max_childs_running);
+
+ /* export log_priority , as called programs may want to follow that setting */
+ sprintf(log, "UDEV_LOG=%i", udev_log_priority);
+ putenv(log);
+
+ while (!udev_exit) {
+ struct uevent_msg *msg;
+ int fdcount;
+
+ FD_ZERO(&readfds);
+ FD_SET(signal_pipe[READ_END], &readfds);
+ FD_SET(udevd_sock, &readfds);
+ if (uevent_netlink_sock > 0)
+ FD_SET(uevent_netlink_sock, &readfds);
+
+ fdcount = select(UDEV_MAX(udevd_sock, uevent_netlink_sock)+1, &readfds, NULL, NULL, NULL);
+ if (fdcount < 0) {
+ if (errno != EINTR)
+ dbg("error in select: %s", strerror(errno));