From 70f50a4731dc6372f9925705faa278f3e9cb5be2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Tue, 7 Jun 2022 11:12:48 +0200 Subject: [PATCH] sd-journal: use _cleanup_ --- src/libsystemd/sd-journal/sd-journal.c | 47 ++++++++++---------------- 1 file changed, 18 insertions(+), 29 deletions(-) diff --git a/src/libsystemd/sd-journal/sd-journal.c b/src/libsystemd/sd-journal/sd-journal.c index 5a94a51377..96d0430a00 100644 --- a/src/libsystemd/sd-journal/sd-journal.c +++ b/src/libsystemd/sd-journal/sd-journal.c @@ -1268,10 +1268,10 @@ static int add_any_file( int fd, const char *path) { - bool close_fd = false; + _cleanup_close_ int our_fd = -1; JournalFile *f; struct stat st; - int r, k; + int r; assert(j); assert(fd >= 0 || path); @@ -1282,32 +1282,30 @@ static int add_any_file( /* If there's a top-level fd defined make the path relative, explicitly, since otherwise * openat() ignores the first argument. */ - fd = openat(j->toplevel_fd, skip_slash(path), O_RDONLY|O_CLOEXEC|O_NONBLOCK); + fd = our_fd = openat(j->toplevel_fd, skip_slash(path), O_RDONLY|O_CLOEXEC|O_NONBLOCK); else - fd = open(path, O_RDONLY|O_CLOEXEC|O_NONBLOCK); + fd = our_fd = open(path, O_RDONLY|O_CLOEXEC|O_NONBLOCK); if (fd < 0) { r = log_debug_errno(errno, "Failed to open journal file %s: %m", path); - goto finish; + goto error; } - close_fd = true; - r = fd_nonblock(fd, false); if (r < 0) { r = log_debug_errno(errno, "Failed to turn off O_NONBLOCK for %s: %m", path); - goto finish; + goto error; } } if (fstat(fd, &st) < 0) { r = log_debug_errno(errno, "Failed to fstat %s: %m", path ?: "fd"); - goto finish; + goto error; } r = stat_verify_regular(&st); if (r < 0) { log_debug_errno(r, "Refusing to open %s: %m", path ?: "fd"); - goto finish; + goto error; } if (path) { @@ -1321,8 +1319,7 @@ static int add_any_file( * which are gone. */ f->last_seen_generation = j->generation; - r = 0; - goto finish; + return 0; } /* So we tracked a file under this name, but it has a different inode/device. In that @@ -1334,15 +1331,15 @@ static int add_any_file( } if (ordered_hashmap_size(j->files) >= JOURNAL_FILES_MAX) { - log_debug("Too many open journal files, not adding %s.", path ?: "fd"); - r = -ETOOMANYREFS; - goto finish; + r = log_debug_errno(SYNTHETIC_ERRNO(ETOOMANYREFS), + "Too many open journal files, not adding %s.", path ?: "fd"); + goto error; } r = journal_file_open(fd, path, O_RDONLY, 0, 0, 0, NULL, j->mmap, NULL, &f); if (r < 0) { log_debug_errno(r, "Failed to open journal file %s: %m", path ?: "from fd"); - goto finish; + goto error; } /* journal_file_dump(f); */ @@ -1353,10 +1350,10 @@ static int add_any_file( f->close_fd = false; /* Make sure journal_file_close() doesn't close the caller's fd * (or our own). The caller or we will do that ourselves. */ (void) journal_file_close(f); - goto finish; + goto error; } - close_fd = false; /* the fd is now owned by the JournalFile object */ + TAKE_FD(our_fd); /* the fd is now owned by the JournalFile object */ f->last_seen_generation = j->generation; @@ -1367,18 +1364,10 @@ static int add_any_file( log_debug("File %s added.", f->path); - r = 0; - -finish: - if (close_fd) - safe_close(fd); - - if (r < 0) { - k = journal_put_error(j, r, path); /* path==NULL is OK. */ - if (k < 0) - return k; - } + return 0; +error: + (void) journal_put_error(j, r, path); /* path==NULL is OK. */ return r; } -- 2.25.1