]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journalctl: fix fd leak 26452/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 17 Feb 2023 00:11:28 +0000 (09:11 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 17 Feb 2023 00:13:22 +0000 (09:13 +0900)
src/journal/journalctl.c

index 30a843d64f8f81552535d4d836d4f8add3df0d74..0267fc6506cfc315a9b8af61db916eb690a82421 100644 (file)
@@ -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,