From d1806141880e24a06f08644804da4ba007b7ce4f Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 21 Aug 2026 23:27:18 +0900 Subject: [PATCH] coredumpctl: check earlier if stdout is not connected to a tty --- src/coredump/coredumpctl-core.c | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/src/coredump/coredumpctl-core.c b/src/coredump/coredumpctl-core.c index 5a69d1fc20..f7d96d8ff7 100644 --- a/src/coredump/coredumpctl-core.c +++ b/src/coredump/coredumpctl-core.c @@ -29,7 +29,7 @@ int save_core(sd_journal *j, FILE *file, char **path, bool *unlink_temp) { _cleanup_close_ int fdt = -EBADF; char *temp = NULL; - assert(!(file && path)); /* At most one can be specified */ + assert(!!file == !path); /* file or path must be specified. They must not be specified together. */ assert(!!path == !!unlink_temp); /* Those must be specified together */ /* Look for a coredump on disk first. */ @@ -87,22 +87,8 @@ int save_core(sd_journal *j, FILE *file, char **path, bool *unlink_temp) { log_debug("Created temporary file %s", temp); fd = fdt; - } else { - /* If neither path or file are specified, we will write to stdout. Let's now check if stdout - * is connected to a tty. We checked that the file exists, or that the core might be stored - * in the journal. In this second case, if we found the entry, in all likelihood we will be - * able to access the COREDUMP= field. In either case, we stop before doing any "real" work, - * i.e. before starting decompression or reading from the file or creating temporary files. */ - if (!file) { - if (on_tty()) - return log_error_errno(SYNTHETIC_ERRNO(ENOTTY), - "Refusing to dump core to tty" - " (use shell redirection or specify --output)."); - file = stdout; - } - + } else fd = fileno(file); - } if (filename) { #if HAVE_COMPRESSION @@ -178,11 +164,14 @@ int verb_dump_core(int argc, char *argv[], uintptr_t _data, void *userdata) { f = fopen(arg_output, "we"); if (!f) return log_error_errno(errno, "Failed to open \"%s\" for writing: %m", arg_output); - } + } else if (on_tty()) + /* We will write core to stdout. Let's refuse if stdout is connected to a tty. */ + return log_error_errno(SYNTHETIC_ERRNO(ENOTTY), + "Refusing to dump core to tty (use shell redirection or specify --output)."); (void) print_entry(f ? stdout : stderr, j, /* need_space= */ false, /* table= */ NULL); - r = save_core(j, f, NULL, NULL); + r = save_core(j, f ?: stdout, NULL, NULL); if (r < 0) return r;