]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
udev: push inotify fd to file descriptor store
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 2 Apr 2025 18:09:31 +0000 (03:09 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 5 Apr 2025 08:33:14 +0000 (17:33 +0900)
Then, if we get inotify fd on start, it is not necessary to re-enable
inotify watch.

src/udev/udev-manager.c
src/udev/udev-watch.c
src/udev/udev-watch.h
units/systemd-udevd.service.in

index 5303c4d2536519f884555b5f88f77d939036197e..c05e475dd622f2eb3fcf0ede98fb2fcc862d56ed 100644 (file)
@@ -1041,6 +1041,8 @@ static int manager_listen_fds(Manager *manager) {
                         r = manager_init_ctrl(manager, fd);
                 else if (streq(names[i], "systemd-udevd-kernel.socket"))
                         r = manager_init_device_monitor(manager, fd);
+                else if (streq(names[i], "inotify"))
+                        r = manager_init_inotify(manager, fd);
                 else
                         r = log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
                                             "Received unexpected fd (%s), ignoring.", names[i]);
index 4b76fd151f949a66d530b59dbf932df1bf98fa05..a9b06f5eb8b7880896be863f2acfbab19bf4abdc 100644 (file)
@@ -6,6 +6,7 @@
 
 #include "alloc-util.h"
 #include "blockdev-util.h"
+#include "daemon-util.h"
 #include "device-util.h"
 #include "dirent-util.h"
 #include "event-util.h"
@@ -243,27 +244,59 @@ static int udev_watch_restore(int inotify_fd) {
         return 0;
 }
 
-int manager_start_inotify(Manager *manager) {
-        _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL;
-        _cleanup_close_ int fd = -EBADF;
+int manager_init_inotify(Manager *manager, int fd) {
         int r;
 
         assert(manager);
-        assert(manager->event);
+
+        /* This takes passed file descriptor on success. */
+
+        if (fd >= 0) {
+                if (manager->inotify_fd >= 0)
+                        return log_warning_errno(SYNTHETIC_ERRNO(EALREADY), "Received multiple inotify fd (%i), ignoring.", fd);
+
+                log_debug("Received inotify fd (%i) from service manager.", fd);
+                manager->inotify_fd = fd;
+                return 0;
+        }
+
+        if (manager->inotify_fd >= 0)
+                return 0;
 
         fd = inotify_init1(IN_CLOEXEC);
         if (fd < 0)
                 return log_error_errno(errno, "Failed to create inotify descriptor: %m");
 
-        udev_watch_restore(fd);
+        log_debug("Initialized new inotify instance, restoring inotify watches of previous invocation.");
+        manager->inotify_fd = fd;
+        (void) udev_watch_restore(fd);
 
-        r = sd_event_add_io(manager->event, &s, fd, EPOLLIN, on_inotify, manager);
+        r = notify_push_fd(manager->inotify_fd, "inotify");
+        if (r < 0)
+                log_warning_errno(r, "Failed to push inotify fd to service manager, ignoring: %m");
+        else
+                log_debug("Pushed inotify fd to service manager.");
+
+        return 0;
+}
+
+int manager_start_inotify(Manager *manager) {
+        int r;
+
+        assert(manager);
+        assert(manager->event);
+
+        r = manager_init_inotify(manager, -EBADF);
+        if (r < 0)
+                return r;
+
+        _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL;
+        r = sd_event_add_io(manager->event, &s, manager->inotify_fd, EPOLLIN, on_inotify, manager);
         if (r < 0)
                 return log_error_errno(r, "Failed to create inotify event source: %m");
 
         (void) sd_event_source_set_description(s, "manager-inotify");
 
-        manager->inotify_fd = TAKE_FD(fd);
         manager->inotify_event = TAKE_PTR(s);
         return 0;
 }
index 3976a835a1de8777cb1c26a88068573f7103fa3d..8365334e71265be728cd98a15567bb510876a4fa 100644 (file)
@@ -5,6 +5,7 @@
 
 typedef struct Manager Manager;
 
+int manager_init_inotify(Manager *manager, int fd);
 int manager_start_inotify(Manager *manager);
 
 int udev_watch_begin(int inotify_fd, sd_device *dev);
index 13ec7088daaccb6cb84540e4614d71ef12193541..8e343ef836a2dc2e2bb0fbef3c4f2b8c25330540 100644 (file)
@@ -27,6 +27,8 @@ Sockets=systemd-udevd-control.socket systemd-udevd-kernel.socket systemd-udevd-v
 Restart=always
 RestartSec=0
 ExecStart={{LIBEXECDIR}}/systemd-udevd
+FileDescriptorStoreMax=512
+FileDescriptorStorePreserve=yes
 KillMode=mixed
 TasksMax=infinity
 PrivateMounts=yes