From: Yu Watanabe Date: Wed, 2 Apr 2025 18:09:31 +0000 (+0900) Subject: udev: push inotify fd to file descriptor store X-Git-Tag: v258-rc1~904^2~3 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=beaf7e04ebdaf2d82e6b13eb24590c9e4560e0e0;p=thirdparty%2Fsystemd.git udev: push inotify fd to file descriptor store Then, if we get inotify fd on start, it is not necessary to re-enable inotify watch. --- diff --git a/src/udev/udev-manager.c b/src/udev/udev-manager.c index 5303c4d2536..c05e475dd62 100644 --- a/src/udev/udev-manager.c +++ b/src/udev/udev-manager.c @@ -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]); diff --git a/src/udev/udev-watch.c b/src/udev/udev-watch.c index 4b76fd151f9..a9b06f5eb8b 100644 --- a/src/udev/udev-watch.c +++ b/src/udev/udev-watch.c @@ -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; } diff --git a/src/udev/udev-watch.h b/src/udev/udev-watch.h index 3976a835a1d..8365334e712 100644 --- a/src/udev/udev-watch.h +++ b/src/udev/udev-watch.h @@ -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); diff --git a/units/systemd-udevd.service.in b/units/systemd-udevd.service.in index 13ec7088daa..8e343ef836a 100644 --- a/units/systemd-udevd.service.in +++ b/units/systemd-udevd.service.in @@ -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