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 e67d738a87.

(cherry picked from commit 0582f7cba907eba6d55b686028d145061c38d0b7)
This commit is contained in:
Yu Watanabe
2026-10-16 17:24:50 +01:00
committed by Luca Boccassi
parent 275bae99e3
commit a32b34813e
+12 -20
View File
@@ -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) {