diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2023-07-05 02:49:45 +0200 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2023-07-05 17:06:20 +0200 |
commit | 4a45a2e0e3ac748bfe71be4ff32d1d85bfaeca1e (patch) | |
tree | 2c1bdff1822d6fe2c013520c0f86ba539795d9fa /src/libsystemd | |
parent | journal-upload: make --namespace=* work (diff) | |
download | systemd-4a45a2e0e3ac748bfe71be4ff32d1d85bfaeca1e.tar.xz systemd-4a45a2e0e3ac748bfe71be4ff32d1d85bfaeca1e.zip |
sd-journal: introduce SD_JOURNAL_TAKE_DIRECTORY_FD flag for sd_journal_open_directory_fd()
If it is called with the flag, then the provided file descriptor will be
owned by the sd_journal object, and will be closed in sd_journal_close().
Diffstat (limited to 'src/libsystemd')
-rw-r--r-- | src/libsystemd/sd-journal/sd-journal.c | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/src/libsystemd/sd-journal/sd-journal.c b/src/libsystemd/sd-journal/sd-journal.c index 0124900517..d84b117783 100644 --- a/src/libsystemd/sd-journal/sd-journal.c +++ b/src/libsystemd/sd-journal/sd-journal.c @@ -2177,13 +2177,16 @@ _public_ int sd_journal_open_files(sd_journal **ret, const char **paths, int fla return 0; } -#define OPEN_DIRECTORY_FD_ALLOWED_FLAGS \ +#define OPEN_DIRECTORY_FD_ALLOWED_FLAGS \ (SD_JOURNAL_OS_ROOT | \ - SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER ) + SD_JOURNAL_SYSTEM | \ + SD_JOURNAL_CURRENT_USER | \ + SD_JOURNAL_TAKE_DIRECTORY_FD) _public_ int sd_journal_open_directory_fd(sd_journal **ret, int fd, int flags) { _cleanup_(sd_journal_closep) sd_journal *j = NULL; struct stat st; + bool take_fd; int r; assert_return(ret, -EINVAL); @@ -2196,7 +2199,8 @@ _public_ int sd_journal_open_directory_fd(sd_journal **ret, int fd, int flags) { if (!S_ISDIR(st.st_mode)) return -EBADFD; - j = journal_new(flags, NULL, NULL); + take_fd = FLAGS_SET(flags, SD_JOURNAL_TAKE_DIRECTORY_FD); + j = journal_new(flags & ~SD_JOURNAL_TAKE_DIRECTORY_FD, NULL, NULL); if (!j) return -ENOMEM; @@ -2209,6 +2213,8 @@ _public_ int sd_journal_open_directory_fd(sd_journal **ret, int fd, int flags) { if (r < 0) return r; + SET_FLAG(j->flags, SD_JOURNAL_TAKE_DIRECTORY_FD, take_fd); + *ret = TAKE_PTR(j); return 0; } @@ -2288,6 +2294,9 @@ _public_ void sd_journal_close(sd_journal *j) { hashmap_free(j->directories_by_path); hashmap_free(j->directories_by_wd); + if (FLAGS_SET(j->flags, SD_JOURNAL_TAKE_DIRECTORY_FD)) + safe_close(j->toplevel_fd); + safe_close(j->inotify_fd); if (j->mmap) { |