+ goto exit;
+ }
+
+ if (init_uevent_netlink_sock() < 0)
+ err("uevent socket not available");
+
+ /* watch rules directory */
+ inotify_fd = inotify_init();
+ if (inotify_fd > 0)
+ inotify_add_watch(inotify_fd, udev_rules_filename, IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
+
+ /* 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);
+
+ /* 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);
+
+ 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);
+ if (inotify_fd > 0)
+ FD_SET(inotify_fd, &readfds);
+
+ fdcount = select(UDEV_MAX(uevent_netlink_sock, inotify_fd)+1, &readfds, NULL, NULL, NULL);
+ if (fdcount < 0) {
+ if (errno != EINTR)
+ dbg("error in select: %s", strerror(errno));
+ continue;
+ }
+
+ /* get user socket message */
+ if (FD_ISSET(udevd_sock, &readfds)) {
+ msg = get_udevd_msg();
+ if (msg) {
+ /* discard kernel messages if netlink is active */
+ if (uevent_netlink_active && msg->type == UDEVD_UEVENT_UDEVSEND && msg->seqnum != 0) {
+ info("skip uevent_helper message with SEQNUM, netlink is active");
+ free(msg);
+ } else
+ msg_queue_insert(msg);
+ }
+ }
+
+ /* get kernel netlink message */
+ if ((uevent_netlink_sock > 0) && FD_ISSET(uevent_netlink_sock, &readfds)) {
+ msg = get_netlink_msg();
+ if (msg) {
+ msg_queue_insert(msg);
+ /* disable udevsend with first netlink message */
+ if (!uevent_netlink_active) {
+ info("uevent_nl message received, disable udevsend messages");
+ uevent_netlink_active = 1;
+ }
+ }
+ }
+
+ /* received a signal, clear our notification pipe */
+ if (FD_ISSET(signal_pipe[READ_END], &readfds)) {
+ char buf[256];
+
+ read(signal_pipe[READ_END], &buf, sizeof(buf));
+ }
+
+ /* rules directory inotify watch */
+ if ((inotify_fd > 0) && FD_ISSET(inotify_fd, &readfds)) {
+ int nbytes;
+
+ /* discard all possible events, we can just reload the config */
+ if ((ioctl(inotify_fd, FIONREAD, &nbytes) == 0) && nbytes) {
+ char *buf;
+
+ reload_config = 1;
+ buf = malloc(nbytes);
+ if (!buf) {
+ err("error getting buffer for inotify, disable watching");
+ close(inotify_fd);
+ inotify_fd = -1;
+ }
+ read(inotify_fd, buf, nbytes);
+ free(buf);
+ }
+ }
+
+ /* rules changed, set by inotify or a signal*/
+ if (reload_config) {
+ reload_config = 0;
+ udev_rules_close(&rules);
+ udev_rules_init(&rules, 0, 1);
+ }
+
+ /* forked child have returned */
+ if (sigchilds_waiting) {
+ sigchilds_waiting = 0;
+ reap_sigchilds();
+ }
+
+ if (run_msg_q) {
+ run_msg_q = 0;
+ msg_queue_manager();
+ }
+
+ if (run_exec_q) {
+ /* clean up running_list before calling exec_queue_manager() */
+ if (sigchilds_waiting) {
+ sigchilds_waiting = 0;
+ reap_sigchilds();
+ }
+
+ run_exec_q = 0;
+ if (!stop_exec_q)
+ exec_queue_manager();