From e67d738a8771c220a2e1ee81d5499a90589dd15d Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 9 Nov 2021 00:10:58 +0100 Subject: [PATCH] sd-event: add sd_event_add_inotify_fd() call sd_event_add_inotify_fd() is like sd_event_add_inotify(), but takes an fd to an inode instead of a path, and is hence a ton nicer. --- man/rules/meson.build | 4 +- man/sd_event_add_inotify.xml | 29 ++++++++++++ src/libsystemd/libsystemd.sym | 1 + src/libsystemd/sd-event/sd-event.c | 75 ++++++++++++++++++++++++------ src/systemd/sd-event.h | 1 + 5 files changed, 94 insertions(+), 16 deletions(-) diff --git a/man/rules/meson.build b/man/rules/meson.build index ad7cc41c983..7bf93fc16b6 100644 --- a/man/rules/meson.build +++ b/man/rules/meson.build @@ -518,7 +518,9 @@ manpages = [ ''], ['sd_event_add_inotify', '3', - ['sd_event_inotify_handler_t', 'sd_event_source_get_inotify_mask'], + ['sd_event_add_inotify_fd', + 'sd_event_inotify_handler_t', + 'sd_event_source_get_inotify_mask'], ''], ['sd_event_add_io', '3', diff --git a/man/sd_event_add_inotify.xml b/man/sd_event_add_inotify.xml index 1681143eb15..d632bf7282e 100644 --- a/man/sd_event_add_inotify.xml +++ b/man/sd_event_add_inotify.xml @@ -17,6 +17,7 @@ sd_event_add_inotify + sd_event_add_inotify_fd sd_event_source_get_inotify_mask sd_event_inotify_handler_t @@ -46,6 +47,16 @@ void *userdata + + int sd_event_add_inotify_fd + sd_event *event + sd_event_source **source + int fd + uint32_t mask + sd_event_inotify_handler_t handler + void *userdata + + int sd_event_source_get_inotify_mask sd_event_source *source @@ -71,6 +82,11 @@ inotify7 for further information. + sd_event_add_inotify_fd() is identical to + sd_event_add_inotify(), except that it takes a file descriptor to an inode (possibly + an O_PATH one, but any other will do too) instead of a path in the file + system. + If multiple event sources are installed for the same inode the backing inotify watch descriptor is automatically shared. The mask parameter may contain any flag defined by the inotify API, with the exception of IN_MASK_ADD. @@ -157,6 +173,19 @@ The passed event source is not an inotify process event source. + + -EBADF + + The passed file descriptor is not valid. + + + + -ENOSYS + + sd_event_add_inotify_fd() was called without + /proc/ mounted. + + diff --git a/src/libsystemd/libsystemd.sym b/src/libsystemd/libsystemd.sym index 04db791f644..5e2fc9e2319 100644 --- a/src/libsystemd/libsystemd.sym +++ b/src/libsystemd/libsystemd.sym @@ -766,4 +766,5 @@ global: LIBSYSTEMD_250 { global: sd_device_get_diskseq; + sd_event_add_inotify_fd; } LIBSYSTEMD_249; diff --git a/src/libsystemd/sd-event/sd-event.c b/src/libsystemd/sd-event/sd-event.c index 2a15e686d45..0ca02485108 100644 --- a/src/libsystemd/sd-event/sd-event.c +++ b/src/libsystemd/sd-event/sd-event.c @@ -1989,24 +1989,25 @@ static int inotify_exit_callback(sd_event_source *s, const struct inotify_event return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata)); } -_public_ int sd_event_add_inotify( +static int event_add_inotify_fd_internal( sd_event *e, sd_event_source **ret, - const char *path, + int fd, + bool donate, uint32_t mask, sd_event_inotify_handler_t callback, void *userdata) { + _cleanup_close_ int donated_fd = donate ? fd : -1; + _cleanup_(source_freep) sd_event_source *s = NULL; struct inotify_data *inotify_data = NULL; struct inode_data *inode_data = NULL; - _cleanup_close_ int fd = -1; - _cleanup_(source_freep) sd_event_source *s = NULL; struct stat st; int r; assert_return(e, -EINVAL); assert_return(e = event_resolve(e), -ENOPKG); - assert_return(path, -EINVAL); + assert_return(fd >= 0, -EBADF); assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); assert_return(!event_pid_changed(e), -ECHILD); @@ -2019,12 +2020,6 @@ _public_ int sd_event_add_inotify( if (mask & IN_MASK_ADD) return -EINVAL; - 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; - if (fstat(fd, &st) < 0) return -errno; @@ -2044,14 +2039,24 @@ _public_ int sd_event_add_inotify( r = event_make_inode_data(e, inotify_data, st.st_dev, st.st_ino, &inode_data); if (r < 0) { - event_free_inotify_data(e, inotify_data); + event_gc_inotify_data(e, inotify_data); return r; } /* Keep the O_PATH fd around until the first iteration of the loop, so that we can still change the priority of * the event source, until then, for which we need the original inode. */ if (inode_data->fd < 0) { - inode_data->fd = TAKE_FD(fd); + if (donated_fd >= 0) + inode_data->fd = TAKE_FD(donated_fd); + else { + inode_data->fd = fcntl(fd, F_DUPFD_CLOEXEC, 3); + if (inode_data->fd < 0) { + r = -errno; + event_gc_inode_data(e, inode_data); + return r; + } + } + LIST_PREPEND(to_close, e->inode_data_to_close, inode_data); } @@ -2064,8 +2069,6 @@ _public_ int sd_event_add_inotify( if (r < 0) return r; - (void) sd_event_source_set_description(s, path); - if (ret) *ret = s; TAKE_PTR(s); @@ -2073,6 +2076,48 @@ _public_ int sd_event_add_inotify( return 0; } +_public_ int sd_event_add_inotify_fd( + sd_event *e, + sd_event_source **ret, + int fd, + uint32_t mask, + sd_event_inotify_handler_t callback, + void *userdata) { + + return event_add_inotify_fd_internal(e, ret, fd, /* donate= */ false, mask, callback, userdata); +} + +_public_ int sd_event_add_inotify( + sd_event *e, + sd_event_source **ret, + const char *path, + uint32_t mask, + sd_event_inotify_handler_t callback, + void *userdata) { + + sd_event_source *s; + 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)); + 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; +} + static sd_event_source* event_source_free(sd_event_source *s) { if (!s) return NULL; diff --git a/src/systemd/sd-event.h b/src/systemd/sd-event.h index 2ae2a0da48a..f4357aa5930 100644 --- a/src/systemd/sd-event.h +++ b/src/systemd/sd-event.h @@ -93,6 +93,7 @@ int sd_event_add_signal(sd_event *e, sd_event_source **s, int sig, sd_event_sign int sd_event_add_child(sd_event *e, sd_event_source **s, pid_t pid, int options, sd_event_child_handler_t callback, void *userdata); int sd_event_add_child_pidfd(sd_event *e, sd_event_source **s, int pidfd, int options, sd_event_child_handler_t callback, void *userdata); int sd_event_add_inotify(sd_event *e, sd_event_source **s, const char *path, uint32_t mask, sd_event_inotify_handler_t callback, void *userdata); +int sd_event_add_inotify_fd(sd_event *e, sd_event_source **s, int fd, uint32_t mask, sd_event_inotify_handler_t callback, void *userdata); int sd_event_add_defer(sd_event *e, sd_event_source **s, sd_event_handler_t callback, void *userdata); int sd_event_add_post(sd_event *e, sd_event_source **s, sd_event_handler_t callback, void *userdata); int sd_event_add_exit(sd_event *e, sd_event_source **s, sd_event_handler_t callback, void *userdata);