From a32b34813e8e726e2f1e514ba4f40daa5dd0f165 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 6 Oct 2026 12:23:55 +0900 Subject: [PATCH] sd-event: make inotify event source floating if ret is NULL Otherwise, if sd_event_add_inotify() is called with NULL for ret, the event source is disabled and never triggered. Fixes a bug introduced by e67d738a8771c220a2e1ee81d5499a90589dd15d. (cherry picked from commit 0582f7cba907eba6d55b686028d145061c38d0b7) --- src/libsystemd/sd-event/sd-event.c | 32 +++++++++++------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/src/libsystemd/sd-event/sd-event.c b/src/libsystemd/sd-event/sd-event.c index dd293a89fd..4fa6fd1ba7 100644 --- a/src/libsystemd/sd-event/sd-event.c +++ b/src/libsystemd/sd-event/sd-event.c @@ -2495,6 +2495,7 @@ static int event_add_inotify_fd_internal( sd_event_source **ret, int fd, bool donate, + const char *path, uint32_t mask, sd_event_inotify_handler_t callback, void *userdata) { @@ -2560,14 +2561,14 @@ static int event_add_inotify_fd_internal( LIST_PREPEND(to_close, e->inode_data_to_close_list, inode_data); - _cleanup_free_ char *path = NULL; - r = fd_get_path(inode_data->fd, &path); + _cleanup_free_ char *fd_path = NULL; + r = fd_get_path(inode_data->fd, &fd_path); if (r < 0 && r != -ENOSYS) { /* The path is optional, hence ignore -ENOSYS. */ event_gc_inode_data(e, inode_data); return r; } - free_and_replace(inode_data->path, path); + free_and_replace(inode_data->path, fd_path); } /* Link our event source to the inode data object */ @@ -2579,6 +2580,9 @@ static int event_add_inotify_fd_internal( if (r < 0) return r; + if (path) + (void) sd_event_source_set_description(s, path); + if (ret) *ret = s; TAKE_PTR(s); @@ -2594,7 +2598,7 @@ _public_ int sd_event_add_inotify_fd( sd_event_inotify_handler_t callback, void *userdata) { - return event_add_inotify_fd_internal(e, ret, fd, /* donate= */ false, mask, callback, userdata); + return event_add_inotify_fd_internal(e, ret, fd, /* donate= */ false, /* path= */ NULL, mask, callback, userdata); } _public_ int sd_event_add_inotify( @@ -2605,27 +2609,15 @@ _public_ int sd_event_add_inotify( sd_event_inotify_handler_t callback, void *userdata) { - sd_event_source *s = NULL; /* avoid false maybe-uninitialized warning */ - int fd, r; - assert_return(path, -EINVAL); - fd = open(path, O_PATH | O_CLOEXEC | - (mask & IN_ONLYDIR ? O_DIRECTORY : 0) | - (mask & IN_DONT_FOLLOW ? O_NOFOLLOW : 0)); + int fd = open(path, O_PATH | O_CLOEXEC | + (mask & IN_ONLYDIR ? O_DIRECTORY : 0) | + (mask & IN_DONT_FOLLOW ? O_NOFOLLOW : 0)); if (fd < 0) return -errno; - r = event_add_inotify_fd_internal(e, &s, fd, /* donate= */ true, mask, callback, userdata); - if (r < 0) - return r; - - (void) sd_event_source_set_description(s, path); - - if (ret) - *ret = s; - - return r; + return event_add_inotify_fd_internal(e, ret, fd, /* donate= */ true, path, mask, callback, userdata); } static sd_event_source* event_source_free(sd_event_source *s) {