From 3aeea37d88b8a5d6703a34bc310baf925c6066bf Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Tue, 31 Mar 2020 00:29:26 -0700 Subject: [PATCH 1/7] home: narrow scope of 'size_t n' trivial cosmetic cleanup --- src/home/homework.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/home/homework.c b/src/home/homework.c index ecf07ffb48d..591e9a19c8b 100644 --- a/src/home/homework.c +++ b/src/home/homework.c @@ -38,7 +38,6 @@ int user_record_authenticate( bool need_password = false, need_token = false, need_pin = false, need_protected_authentication_path_permitted = false, pin_locked = false, pin_incorrect = false, pin_incorrect_few_tries_left = false, pin_incorrect_one_try_left = false; - size_t n; int r; assert(h); @@ -70,7 +69,7 @@ int user_record_authenticate( } /* Second, let's see if any of the PKCS#11 security tokens are plugged in and help us */ - for (n = 0; n < h->n_pkcs11_encrypted_key; n++) { + for (size_t n = 0; n < h->n_pkcs11_encrypted_key; n++) { #if HAVE_P11KIT _cleanup_(pkcs11_callback_data_release) struct pkcs11_callback_data data = { .user_record = h, From 3ebbb6cb394f8a328a9bba782e3b1cbeac4d9a7b Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Tue, 31 Mar 2020 00:49:07 -0700 Subject: [PATCH 2/7] fileio: introduce take_fdopen{_unlocked}() variant With the addition of _cleanup_close_ there's a repetitious pattern of assigning -1 to the fd after a successful fdopen to prevent its close on cleanup now that the FILE * owns the fd. This introduces a wrapper that instead takes a pointer to the fd being opened, and always overwrites the fd with -1 on success. A future commit will cleanup all the fdopen call sites to use the wrapper and elide the manual -1 fd assignment. --- src/basic/fileio.c | 26 ++++++++++++++++++++++++++ src/basic/fileio.h | 2 ++ 2 files changed, 28 insertions(+) diff --git a/src/basic/fileio.c b/src/basic/fileio.c index fe0c4f47071..c82a9d2c01d 100644 --- a/src/basic/fileio.c +++ b/src/basic/fileio.c @@ -54,6 +54,32 @@ int fdopen_unlocked(int fd, const char *options, FILE **ret) { return 0; } +int take_fdopen_unlocked(int *fd, const char *options, FILE **ret) { + int r; + + assert(fd); + + r = fdopen_unlocked(*fd, options, ret); + if (r < 0) + return r; + + *fd = -1; + + return 0; +} + +FILE* take_fdopen(int *fd, const char *options) { + assert(fd); + + FILE *f = fdopen(*fd, options); + if (!f) + return NULL; + + *fd = -1; + + return f; +} + FILE* open_memstream_unlocked(char **ptr, size_t *sizeloc) { FILE *f = open_memstream(ptr, sizeloc); if (!f) diff --git a/src/basic/fileio.h b/src/basic/fileio.h index e6fea2afd4b..525f6ac814a 100644 --- a/src/basic/fileio.h +++ b/src/basic/fileio.h @@ -39,6 +39,8 @@ typedef enum { int fopen_unlocked(const char *path, const char *options, FILE **ret); int fdopen_unlocked(int fd, const char *options, FILE **ret); +int take_fdopen_unlocked(int *fd, const char *options, FILE **ret); +FILE* take_fdopen(int *fd, const char *options); FILE* open_memstream_unlocked(char **ptr, size_t *sizeloc); FILE* fmemopen_unlocked(void *buf, size_t size, const char *mode); From 4fa744a35cd5f6dd18cd38f79a1b6a1c9547fd16 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Tue, 31 Mar 2020 01:07:21 -0700 Subject: [PATCH 3/7] *: convert amenable fdopen calls to take_fdopen Mechanical change to eliminate some cruft by using the new take_fdopen{_unlocked}() wrappers where trivial. --- src/basic/tmpfile-util.c | 4 +--- src/home/homed-home.c | 4 +--- src/home/homework.c | 8 ++------ src/machine/machine-dbus.c | 5 ++--- src/machine/machined-dbus.c | 4 +--- src/nspawn/nspawn-setuid.c | 6 ++---- src/portable/portable.c | 3 +-- src/portable/portabled-image-bus.c | 4 +--- src/shared/ask-password-api.c | 4 +--- src/shared/dissect-image.c | 4 +--- src/shared/install.c | 3 +-- src/shared/os-util.c | 4 ++-- 12 files changed, 16 insertions(+), 37 deletions(-) diff --git a/src/basic/tmpfile-util.c b/src/basic/tmpfile-util.c index decdafb9c95..2a2ffb21a51 100644 --- a/src/basic/tmpfile-util.c +++ b/src/basic/tmpfile-util.c @@ -48,14 +48,12 @@ int fopen_temporary(const char *path, FILE **ret_f, char **ret_temp_path) { /* This assumes that returned FILE object is short-lived and used within the same single-threaded * context and never shared externally, hence locking is not necessary. */ - r = fdopen_unlocked(fd, "w", &f); + r = take_fdopen_unlocked(&fd, "w", &f); if (r < 0) { (void) unlink(t); return r; } - TAKE_FD(fd); - if (ret_f) *ret_f = TAKE_PTR(f); diff --git a/src/home/homed-home.c b/src/home/homed-home.c index f50de267227..f553419c413 100644 --- a/src/home/homed-home.c +++ b/src/home/homed-home.c @@ -360,12 +360,10 @@ static int home_parse_worker_stdout(int _fd, UserRecord **ret) { if (lseek(fd, SEEK_SET, 0) == (off_t) -1) return log_error_errno(errno, "Failed to seek to beginning of memfd: %m"); - f = fdopen(fd, "r"); + f = take_fdopen(&fd, "r"); if (!f) return log_error_errno(errno, "Failed to reopen memfd: %m"); - TAKE_FD(fd); - if (DEBUG_LOGGING) { _cleanup_free_ char *text = NULL; diff --git a/src/home/homework.c b/src/home/homework.c index 591e9a19c8b..58fa0870973 100644 --- a/src/home/homework.c +++ b/src/home/homework.c @@ -279,12 +279,10 @@ static int read_identity_file(int root_fd, JsonVariant **ret) { if (r < 0) return log_error_errno(r, "Embedded identity file is not a regular file, refusing: %m"); - identity_file = fdopen(identity_fd, "r"); + identity_file = take_fdopen(&identity_fd, "r"); if (!identity_file) return log_oom(); - identity_fd = -1; - r = json_parse_file(identity_file, ".identity", JSON_PARSE_SENSITIVE, ret, &line, &column); if (r < 0) return log_error_errno(r, "[.identity:%u:%u] Failed to parse JSON data: %m", line, column); @@ -318,14 +316,12 @@ static int write_identity_file(int root_fd, JsonVariant *v, uid_t uid) { if (identity_fd < 0) return log_error_errno(errno, "Failed to create .identity file in home directory: %m"); - identity_file = fdopen(identity_fd, "w"); + identity_file = take_fdopen(&identity_fd, "w"); if (!identity_file) { r = log_oom(); goto fail; } - identity_fd = -1; - json_variant_dump(normalized, JSON_FORMAT_PRETTY, identity_file, NULL); r = fflush_and_check(identity_file); diff --git a/src/machine/machine-dbus.c b/src/machine/machine-dbus.c index a2990452af1..5c0f7141840 100644 --- a/src/machine/machine-dbus.c +++ b/src/machine/machine-dbus.c @@ -20,6 +20,7 @@ #include "env-file.h" #include "env-util.h" #include "fd-util.h" +#include "fileio.h" #include "format-util.h" #include "fs-util.h" #include "in-addr-util.h" @@ -399,12 +400,10 @@ int bus_machine_method_get_os_release(sd_bus_message *message, void *userdata, s pair[1] = safe_close(pair[1]); - f = fdopen(pair[0], "r"); + f = take_fdopen(&pair[0], "r"); if (!f) return -errno; - pair[0] = -1; - r = load_env_file_pairs(f, "/etc/os-release", &l); if (r < 0) return r; diff --git a/src/machine/machined-dbus.c b/src/machine/machined-dbus.c index d0cc07678fa..97dfb942788 100644 --- a/src/machine/machined-dbus.c +++ b/src/machine/machined-dbus.c @@ -620,12 +620,10 @@ static int clean_pool_done(Operation *operation, int ret, sd_bus_error *error) { if (lseek(operation->extra_fd, 0, SEEK_SET) == (off_t) -1) return -errno; - f = fdopen(operation->extra_fd, "r"); + f = take_fdopen(&operation->extra_fd, "r"); if (!f) return -errno; - operation->extra_fd = -1; - /* The resulting temporary file starts with a boolean value that indicates success or not. */ errno = 0; n = fread(&success, 1, sizeof(success), f); diff --git a/src/nspawn/nspawn-setuid.c b/src/nspawn/nspawn-setuid.c index cb2b2272b6b..d0e575fef23 100644 --- a/src/nspawn/nspawn-setuid.c +++ b/src/nspawn/nspawn-setuid.c @@ -118,10 +118,9 @@ int change_uid_gid(const char *user, char **_home) { if (fd < 0) return fd; - f = fdopen(fd, "r"); + f = take_fdopen(&fd, "r"); if (!f) return log_oom(); - fd = -1; r = read_line(f, LONG_LINE_MAX, &line); if (r == 0) @@ -191,10 +190,9 @@ int change_uid_gid(const char *user, char **_home) { if (fd < 0) return fd; - f = fdopen(fd, "r"); + f = take_fdopen(&fd, "r"); if (!f) return log_oom(); - fd = -1; r = read_line(f, LONG_LINE_MAX, &line); if (r == 0) diff --git a/src/portable/portable.c b/src/portable/portable.c index e18826ab268..58e4a6670d8 100644 --- a/src/portable/portable.c +++ b/src/portable/portable.c @@ -1092,10 +1092,9 @@ static int test_chroot_dropin( return log_debug_errno(errno, "Failed to open %s/%s: %m", where, p); } - r = fdopen_unlocked(fd, "r", &f); + r = take_fdopen_unlocked(&fd, "r", &f); if (r < 0) return log_debug_errno(r, "Failed to convert file handle: %m"); - TAKE_FD(fd); r = read_line(f, LONG_LINE_MAX, &line); if (r < 0) diff --git a/src/portable/portabled-image-bus.c b/src/portable/portabled-image-bus.c index 2bd1c495e4e..5988a210374 100644 --- a/src/portable/portabled-image-bus.c +++ b/src/portable/portabled-image-bus.c @@ -79,12 +79,10 @@ static int append_fd(sd_bus_message *m, PortableMetadata *d) { assert(d); assert(d->fd >= 0); - f = fdopen(d->fd, "r"); + f = take_fdopen(&d->fd, "r"); if (!f) return -errno; - d->fd = -1; - r = read_full_stream(f, &buf, &n); if (r < 0) return r; diff --git a/src/shared/ask-password-api.c b/src/shared/ask-password-api.c index 0fc5501e621..b64663bd1e0 100644 --- a/src/shared/ask-password-api.c +++ b/src/shared/ask-password-api.c @@ -791,14 +791,12 @@ int ask_password_agent( (void) fchmod(fd, 0644); - f = fdopen(fd, "w"); + f = take_fdopen(&fd, "w"); if (!f) { r = -errno; goto finish; } - fd = -1; - signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC); if (signal_fd < 0) { r = -errno; diff --git a/src/shared/dissect-image.c b/src/shared/dissect-image.c index 1ef69fdf4c8..f914473a1d3 100644 --- a/src/shared/dissect-image.c +++ b/src/shared/dissect-image.c @@ -1541,14 +1541,12 @@ int dissected_image_acquire_metadata(DissectedImage *m) { fds[2*k+1] = safe_close(fds[2*k+1]); - f = fdopen(fds[2*k], "r"); + f = take_fdopen(&fds[2*k], "r"); if (!f) { r = -errno; goto finish; } - fds[2*k] = -1; - switch (k) { case META_HOSTNAME: diff --git a/src/shared/install.c b/src/shared/install.c index 48cb6aca7e8..0f18b6bee50 100644 --- a/src/shared/install.c +++ b/src/shared/install.c @@ -1280,10 +1280,9 @@ static int unit_file_load( if (r < 0) return r; - f = fdopen(fd, "r"); + f = take_fdopen(&fd, "r"); if (!f) return -errno; - fd = -1; /* c is only needed if we actually load the file (it's referenced from items[] btw, in case you wonder.) */ assert(c); diff --git a/src/shared/os-util.c b/src/shared/os-util.c index b2af8535f9c..eacebc1ea5d 100644 --- a/src/shared/os-util.c +++ b/src/shared/os-util.c @@ -3,6 +3,7 @@ #include "alloc-util.h" #include "env-file.h" #include "fd-util.h" +#include "fileio.h" #include "fs-util.h" #include "macro.h" #include "os-util.h" @@ -76,10 +77,9 @@ int fopen_os_release(const char *root, char **ret_path, FILE **ret_file) { if (r < 0) return r; - f = fdopen(fd, "r"); + f = take_fdopen(&fd, "r"); if (!f) return -errno; - fd = -1; *ret_file = f; From f61457b0fee7326ead5b2cd8df8515a814d922e0 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Tue, 31 Mar 2020 01:59:33 -0700 Subject: [PATCH 4/7] fileio: add take_fdopendir() variant fdopendir() wrapper analogous to take_fdopen() --- src/basic/fileio.c | 12 ++++++++++++ src/basic/fileio.h | 1 + 2 files changed, 13 insertions(+) diff --git a/src/basic/fileio.c b/src/basic/fileio.c index c82a9d2c01d..4c365ad6fa9 100644 --- a/src/basic/fileio.c +++ b/src/basic/fileio.c @@ -80,6 +80,18 @@ FILE* take_fdopen(int *fd, const char *options) { return f; } +DIR* take_fdopendir(int *dfd) { + assert(dfd); + + DIR *d = fdopendir(*dfd); + if (!d) + return NULL; + + *dfd = -1; + + return d; +} + FILE* open_memstream_unlocked(char **ptr, size_t *sizeloc) { FILE *f = open_memstream(ptr, sizeloc); if (!f) diff --git a/src/basic/fileio.h b/src/basic/fileio.h index 525f6ac814a..58daabaa8ff 100644 --- a/src/basic/fileio.h +++ b/src/basic/fileio.h @@ -41,6 +41,7 @@ int fopen_unlocked(const char *path, const char *options, FILE **ret); int fdopen_unlocked(int fd, const char *options, FILE **ret); int take_fdopen_unlocked(int *fd, const char *options, FILE **ret); FILE* take_fdopen(int *fd, const char *options); +DIR* take_fdopendir(int *dfd); FILE* open_memstream_unlocked(char **ptr, size_t *sizeloc); FILE* fmemopen_unlocked(void *buf, size_t size, const char *mode); From 9f81a592c19a7390eefaca99e8323f13d69c2044 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Tue, 31 Mar 2020 02:00:44 -0700 Subject: [PATCH 5/7] *: convert amenable fdopendir() calls to take_fdopendir() Some fdopendir() calls remain where safe_close() is manually performed, those could be simplified as well by converting to use the _cleanup_close_ machinery, but makes things less trivial to review so left for a future cleanup. --- src/basic/copy.c | 4 ++-- src/basic/stat-util.c | 4 ++-- src/nspawn/nspawn-patch-uid.c | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/basic/copy.c b/src/basic/copy.c index 97d566c5b93..b384010ae32 100644 --- a/src/basic/copy.c +++ b/src/basic/copy.c @@ -15,6 +15,7 @@ #include "copy.h" #include "dirent-util.h" #include "fd-util.h" +#include "fileio.h" #include "fs-util.h" #include "io-util.h" #include "macro.h" @@ -569,10 +570,9 @@ static int fd_copy_directory( if (fdf < 0) return -errno; - d = fdopendir(fdf); + d = take_fdopendir(&fdf); if (!d) return -errno; - fdf = -1; exists = false; if (copy_flags & COPY_MERGE_EMPTY) { diff --git a/src/basic/stat-util.c b/src/basic/stat-util.c index 8ef90e96b7e..5412ccbf7df 100644 --- a/src/basic/stat-util.c +++ b/src/basic/stat-util.c @@ -10,6 +10,7 @@ #include "alloc-util.h" #include "dirent-util.h" #include "fd-util.h" +#include "fileio.h" #include "fs-util.h" #include "macro.h" #include "missing_fs.h" @@ -77,10 +78,9 @@ int dir_is_empty_at(int dir_fd, const char *path) { if (fd < 0) return -errno; - d = fdopendir(fd); + d = take_fdopendir(&fd); if (!d) return -errno; - fd = -1; FOREACH_DIRENT(de, d, return -errno) return 0; diff --git a/src/nspawn/nspawn-patch-uid.c b/src/nspawn/nspawn-patch-uid.c index fc591e27253..112c3562ace 100644 --- a/src/nspawn/nspawn-patch-uid.c +++ b/src/nspawn/nspawn-patch-uid.c @@ -8,6 +8,7 @@ #include "acl-util.h" #include "dirent-util.h" #include "fd-util.h" +#include "fileio.h" #include "fs-util.h" #include "missing_magic.h" #include "nspawn-def.h" @@ -335,12 +336,11 @@ static int recurse_fd(int fd, bool donate_fd, const struct stat *st, uid_t shift donate_fd = true; } - d = fdopendir(fd); + d = take_fdopendir(&fd); if (!d) { r = -errno; goto finish; } - fd = -1; FOREACH_DIRENT_ALL(de, d, r = -errno; goto finish) { struct stat fst; From b46c3e4913f7f234039ebd8104446450917cab8d Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Tue, 31 Mar 2020 02:29:37 -0700 Subject: [PATCH 6/7] *: use _cleanup_close_ with fdopen() where trivial Also convert these to use take_fdopen(). --- src/basic/tmpfile-util.c | 8 +++----- src/boot/bootctl.c | 9 ++++----- src/core/manager.c | 8 +++----- src/coredump/coredump.c | 8 +++----- src/journal-remote/journal-gatewayd.c | 8 +++----- 5 files changed, 16 insertions(+), 25 deletions(-) diff --git a/src/basic/tmpfile-util.c b/src/basic/tmpfile-util.c index 2a2ffb21a51..9cbca312fcf 100644 --- a/src/basic/tmpfile-util.c +++ b/src/basic/tmpfile-util.c @@ -78,18 +78,16 @@ int mkostemp_safe(char *pattern) { } int fmkostemp_safe(char *pattern, const char *mode, FILE **ret_f) { - int fd; + _cleanup_close_ int fd = -1; FILE *f; fd = mkostemp_safe(pattern); if (fd < 0) return fd; - f = fdopen(fd, mode); - if (!f) { - safe_close(fd); + f = take_fdopen(&fd, mode); + if (!f) return -errno; - } *ret_f = f; return 0; diff --git a/src/boot/bootctl.c b/src/boot/bootctl.c index 5874b9ec68c..1a2ea0ae65d 100644 --- a/src/boot/bootctl.c +++ b/src/boot/bootctl.c @@ -982,8 +982,9 @@ static int install_loader_config(const char *esp_path, sd_id128_t machine_id) { char machine_string[SD_ID128_STRING_MAX]; _cleanup_(unlink_and_freep) char *t = NULL; _cleanup_fclose_ FILE *f = NULL; + _cleanup_close_ int fd = -1; const char *p; - int r, fd; + int r; p = prefix_roota(esp_path, "/loader/loader.conf"); if (access(p, F_OK) >= 0) /* Silently skip creation if the file already exists (early check) */ @@ -993,11 +994,9 @@ static int install_loader_config(const char *esp_path, sd_id128_t machine_id) { if (fd < 0) return log_error_errno(fd, "Failed to open \"%s\" for writing: %m", p); - f = fdopen(fd, "w"); - if (!f) { - safe_close(fd); + f = take_fdopen(&fd, "w"); + if (!f) return log_oom(); - } fprintf(f, "#timeout 3\n" "#console-mode keep\n" diff --git a/src/core/manager.c b/src/core/manager.c index 148df8d523b..2ae1e3f4d93 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -3107,7 +3107,7 @@ void manager_send_unit_plymouth(Manager *m, Unit *u) { } int manager_open_serialization(Manager *m, FILE **_f) { - int fd; + _cleanup_close_ int fd = -1; FILE *f; assert(_f); @@ -3116,11 +3116,9 @@ int manager_open_serialization(Manager *m, FILE **_f) { if (fd < 0) return fd; - f = fdopen(fd, "w+"); - if (!f) { - safe_close(fd); + f = take_fdopen(&fd, "w+"); + if (!f) return -errno; - } *_f = f; return 0; diff --git a/src/coredump/coredump.c b/src/coredump/coredump.c index 0e9a3c023c7..ee4268b9657 100644 --- a/src/coredump/coredump.c +++ b/src/coredump/coredump.c @@ -560,7 +560,7 @@ static int compose_open_fds(pid_t pid, char **open_fds) { FOREACH_DIRENT(dent, proc_fd_dir, return -errno) { _cleanup_fclose_ FILE *fdinfo = NULL; _cleanup_free_ char *fdname = NULL; - int fd; + _cleanup_close_ int fd = -1; r = readlinkat_malloc(dirfd(proc_fd_dir), dent->d_name, &fdname); if (r < 0) @@ -574,11 +574,9 @@ static int compose_open_fds(pid_t pid, char **open_fds) { if (fd < 0) continue; - fdinfo = fdopen(fd, "r"); - if (!fdinfo) { - safe_close(fd); + fdinfo = take_fdopen(&fd, "r"); + if (!fdinfo) continue; - } for (;;) { _cleanup_free_ char *line = NULL; diff --git a/src/journal-remote/journal-gatewayd.c b/src/journal-remote/journal-gatewayd.c index 459d8e86a1c..5177e0d1577 100644 --- a/src/journal-remote/journal-gatewayd.c +++ b/src/journal-remote/journal-gatewayd.c @@ -123,17 +123,15 @@ static int request_meta_ensure_tmp(RequestMeta *m) { if (m->tmp) rewind(m->tmp); else { - int fd; + _cleanup_close_ int fd = -1; fd = open_tmpfile_unlinkable("/tmp", O_RDWR|O_CLOEXEC); if (fd < 0) return fd; - m->tmp = fdopen(fd, "w+"); - if (!m->tmp) { - safe_close(fd); + m->tmp = take_fdopen(&fd, "w+"); + if (!m->tmp) return -errno; - } } return 0; From 8e06af804b125a035156bac24008a7112e9ce448 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Tue, 31 Mar 2020 02:35:00 -0700 Subject: [PATCH 7/7] *: use _cleanup_close_ with fdopendir() where trivial Also convert these to use take_fdopendir(). --- src/home/homework-cifs.c | 9 ++++----- src/journal/sd-journal.c | 5 ++--- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/home/homework-cifs.c b/src/home/homework-cifs.c index 27f298144a0..1510031a38a 100644 --- a/src/home/homework-cifs.c +++ b/src/home/homework-cifs.c @@ -142,7 +142,8 @@ int home_create_cifs(UserRecord *h, UserRecord **ret_home) { _cleanup_(home_setup_undo) HomeSetup setup = HOME_SETUP_INIT; _cleanup_(user_record_unrefp) UserRecord *new_home = NULL; _cleanup_(closedirp) DIR *d = NULL; - int r, copy; + _cleanup_close_ int copy = -1; + int r; assert(h); assert(user_record_storage(h) == USER_CIFS); @@ -166,11 +167,9 @@ int home_create_cifs(UserRecord *h, UserRecord **ret_home) { if (copy < 0) return -errno; - d = fdopendir(copy); - if (!d) { - safe_close(copy); + d = take_fdopendir(©); + if (!d) return -errno; - } errno = 0; if (readdir_no_dot(d)) diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c index 36f65ad6883..a739fa8aafc 100644 --- a/src/journal/sd-journal.c +++ b/src/journal/sd-journal.c @@ -1718,7 +1718,7 @@ static int add_root_directory(sd_journal *j, const char *p, bool missing_ok) { goto fail; } } else { - int dfd; + _cleanup_close_ int dfd = -1; /* If there's no path specified, then we use the top-level fd itself. We duplicate the fd here, since * opendir() will take possession of the fd, and close it, which we don't want. */ @@ -1731,10 +1731,9 @@ static int add_root_directory(sd_journal *j, const char *p, bool missing_ok) { goto fail; } - d = fdopendir(dfd); + d = take_fdopendir(&dfd); if (!d) { r = -errno; - safe_close(dfd); goto fail; }