From: Yu Watanabe Date: Fri, 17 Feb 2023 00:11:28 +0000 (+0900) Subject: journalctl: fix fd leak X-Git-Tag: v254-rc1~1258^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F26452%2Fhead;p=thirdparty%2Fsystemd.git journalctl: fix fd leak --- diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index 30a843d64f8..0267fc6506c 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -2100,6 +2100,7 @@ static int run(int argc, char *argv[]) { _cleanup_(sd_journal_closep) sd_journal *j = NULL; sd_id128_t previous_boot_id = SD_ID128_NULL, previous_boot_id_output = SD_ID128_NULL; dual_timestamp previous_ts_output = DUAL_TIMESTAMP_NULL; + _cleanup_close_ int machine_fd = -EBADF; int n_shown = 0, r, poll_fd = -EBADF; setlocale(LC_ALL, ""); @@ -2240,13 +2241,11 @@ static int run(int argc, char *argv[]) { if (r < 0) return bus_log_parse_error(r); - fd = fcntl(fd, F_DUPFD_CLOEXEC, 3); - if (fd < 0) + machine_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3); + if (machine_fd < 0) return log_error_errno(errno, "Failed to duplicate file descriptor: %m"); - r = sd_journal_open_directory_fd(&j, fd, SD_JOURNAL_OS_ROOT); - if (r < 0) - safe_close(fd); + r = sd_journal_open_directory_fd(&j, machine_fd, SD_JOURNAL_OS_ROOT); } else r = sd_journal_open_namespace( &j,