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.
This commit is contained in:
Yu Watanabe
2025-04-05 17:33:14 +09:00
parent 5edb07be62
commit beaf7e04eb
4 changed files with 45 additions and 7 deletions
+2
View 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]);
+40 -7
View 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;
}
+1
View 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);
+2
View 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