sd-event: fix floating inotify event (#44117)

This commit is contained in:
Lennart Poettering authored and GitHub committed 2026-10-10 11:54:39 +02:00
commit c673d9985d
2 files changed
+15 -24

No files matched your search

+13 -21
View File
@@ -2547,6 +2547,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) {
@@ -2612,14 +2613,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 */
@@ -2631,6 +2632,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);
@@ -2646,7 +2650,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(
@@ -2657,27 +2661,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) {
@@ -3434,7 +3426,7 @@ _public_ void* sd_event_source_get_userdata(sd_event_source *s) {
return s->userdata;
}
_public_ void *sd_event_source_set_userdata(sd_event_source *s, void *userdata) {
_public_ void* sd_event_source_set_userdata(sd_event_source *s, void *userdata) {
void *ret;
assert_return(s, NULL);
+2 -3
View File
@@ -479,7 +479,7 @@ static int delete_self_handler(sd_event_source *s, const struct inotify_event *e
static void test_inotify_one(unsigned n_create_events) {
_cleanup_(rm_rf_physical_and_freep) char *p = NULL;
sd_event_source *a = NULL, *b = NULL, *c = NULL, *d = NULL;
sd_event_source *a = NULL, *b = NULL, *c = NULL;
struct inotify_context context = {
.n_create_events = n_create_events,
};
@@ -514,7 +514,7 @@ static void test_inotify_one(unsigned n_create_events) {
q = strjoina(p, "/sub");
ASSERT_OK(touch(q));
ASSERT_OK(sd_event_add_inotify(e, &d, q, IN_DELETE_SELF, delete_self_handler, &context));
ASSERT_OK(sd_event_add_inotify(e, /* ret= */ NULL, q, IN_DELETE_SELF, delete_self_handler, &context));
for (i = 0; i < n_create_events; i++) {
char buf[DECIMAL_STR_MAX(unsigned)+1];
@@ -533,7 +533,6 @@ static void test_inotify_one(unsigned n_create_events) {
sd_event_source_unref(a);
sd_event_source_unref(b);
sd_event_source_unref(c);
sd_event_source_unref(d);
sd_event_unref(e);
}