- if (inotify_fd >= 0)
- close(inotify_fd);
- close(signal_pipe[READ_END]);
- close(signal_pipe[WRITE_END]);
- logging_close();
- logging_init("udevd-event");
- setpriority(PRIO_PROCESS, 0, UDEV_PRIORITY);
-
- /* set signal handlers */
- memset(&act, 0x00, sizeof(act));
- act.sa_handler = (void (*)(int)) event_sig_handler;
- sigemptyset (&act.sa_mask);
- act.sa_flags = 0;
- sigaction(SIGALRM, &act, NULL);
-
- /* reset to default */
- act.sa_handler = SIG_DFL;
- sigaction(SIGINT, &act, NULL);
- sigaction(SIGTERM, &act, NULL);
- sigaction(SIGCHLD, &act, NULL);
- sigaction(SIGHUP, &act, NULL);
-
- /* set timeout to prevent hanging processes */
- alarm(UDEV_EVENT_TIMEOUT);
-
- /* apply rules, create node, symlinks */
- err = udev_event_execute_rules(event, rules);
-
- /* rules may change/disable the timeout */
- if (udev_device_get_event_timeout(event->dev) >= 0)
- alarm(udev_device_get_event_timeout(event->dev));
-
- /* execute RUN= */
- if (err == 0 && !event->ignore_device && udev_get_run(event->udev))
- udev_event_execute_run(event);
-
- info(event->udev, "seq %llu exit with %i\n", udev_device_get_seqnum(event->dev), err);
- logging_close();
- if (err != 0)
- exit(1);
- exit(0);
+ close(fd_signal);
+ close(fd_ep);
+ close(worker_watch[READ_END]);
+
+ sigfillset(&mask);
+ fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
+ if (fd_signal < 0) {
+ err(udev, "error creating signalfd %m\n");
+ rc = 2;
+ goto out;
+ }
+
+ fd_ep = epoll_create1(EPOLL_CLOEXEC);
+ if (fd_ep < 0) {
+ err(udev, "error creating epoll fd: %m\n");
+ rc = 3;
+ goto out;
+ }
+
+ memset(&ep_signal, 0, sizeof(struct epoll_event));
+ ep_signal.events = EPOLLIN;
+ ep_signal.data.fd = fd_signal;
+
+ fd_monitor = udev_monitor_get_fd(worker_monitor);
+ memset(&ep_monitor, 0, sizeof(struct epoll_event));
+ ep_monitor.events = EPOLLIN;
+ ep_monitor.data.fd = fd_monitor;
+
+ if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_signal, &ep_signal) < 0 ||
+ epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_monitor, &ep_monitor) < 0) {
+ err(udev, "fail to add fds to epoll: %m\n");
+ rc = 4;
+ goto out;
+ }
+
+ /* request TERM signal if parent exits */
+ prctl(PR_SET_PDEATHSIG, SIGTERM);
+
+ for (;;) {
+ struct udev_event *udev_event;
+ struct worker_message msg;
+ int failed = 0;
+ int err;
+
+ info(udev, "seq %llu running\n", udev_device_get_seqnum(dev));
+ udev_event = udev_event_new(dev);
+ if (udev_event == NULL) {
+ rc = 5;
+ goto out;
+ }
+
+ /* needed for SIGCHLD/SIGTERM in spawn() */
+ udev_event->fd_signal = fd_signal;
+
+ if (exec_delay > 0)
+ udev_event->exec_delay = exec_delay;
+
+ /* apply rules, create node, symlinks */
+ err = udev_event_execute_rules(udev_event, rules, &sigmask_orig);
+
+ if (err == 0)
+ failed = udev_event_execute_run(udev_event, &sigmask_orig);
+
+ /* apply/restore inotify watch */
+ if (err == 0 && udev_event->inotify_watch) {
+ udev_watch_begin(udev, dev);
+ udev_device_update_db(dev);
+ }
+
+ /* send processed event back to libudev listeners */
+ udev_monitor_send_device(worker_monitor, NULL, dev);
+
+ /* send udevd the result of the event execution */
+ memset(&msg, 0, sizeof(struct worker_message));
+ if (err != 0)
+ msg.exitcode = err;
+ else if (failed != 0)
+ msg.exitcode = failed;
+ msg.pid = getpid();
+ send(worker_watch[WRITE_END], &msg, sizeof(struct worker_message), 0);
+
+ info(udev, "seq %llu processed with %i\n", udev_device_get_seqnum(dev), err);
+
+ udev_device_unref(dev);
+ dev = NULL;
+
+ if (udev_event->sigterm) {
+ udev_event_unref(udev_event);
+ goto out;
+ }
+
+ udev_event_unref(udev_event);
+
+ /* wait for more device messages from main udevd, or term signal */
+ while (dev == NULL) {
+ struct epoll_event ev[4];
+ int fdcount;
+ int i;
+
+ fdcount = epoll_wait(fd_ep, ev, ARRAY_SIZE(ev), -1);
+ if (fdcount < 0) {
+ if (errno == EINTR)
+ continue;
+ err = -errno;
+ err(udev, "failed to poll: %m\n");
+ goto out;
+ }
+
+ for (i = 0; i < fdcount; i++) {
+ if (ev[i].data.fd == fd_monitor && ev[i].events & EPOLLIN) {
+ dev = udev_monitor_receive_device(worker_monitor);
+ } else if (ev[i].data.fd == fd_signal && ev[i].events & EPOLLIN) {
+ struct signalfd_siginfo fdsi;
+ ssize_t size;
+
+ size = read(fd_signal, &fdsi, sizeof(struct signalfd_siginfo));
+ if (size != sizeof(struct signalfd_siginfo))
+ continue;
+ switch (fdsi.ssi_signo) {
+ case SIGTERM:
+ goto out;
+ }
+ }
+ }
+ }
+ }
+out:
+ udev_device_unref(dev);
+ if (fd_signal >= 0)
+ close(fd_signal);
+ if (fd_ep >= 0)
+ close(fd_ep);
+ close(fd_inotify);
+ close(worker_watch[WRITE_END]);
+ udev_rules_unref(rules);
+ udev_monitor_unref(worker_monitor);
+ udev_unref(udev);
+ udev_log_close();
+ exit(rc);
+ }