From: Zbigniew Jędrzejewski-Szmek Date: Tue, 20 Nov 2018 08:27:19 +0000 (+0100) Subject: coredumpctl: open output file only before writing X-Git-Tag: v240~258^2~34 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=158ecef56b63aff23bc6fc04b7a7fce08a56b55c;p=thirdparty%2Fsystemd.git coredumpctl: open output file only before writing We would open the file very early, which is not nice, if we e.g. fail when parsing later options. Let's do the usual thing and just open it just before writing, and close immediately after writing. --- diff --git a/src/coredump/coredumpctl.c b/src/coredump/coredumpctl.c index 6160b4bf7a3..906b45488a0 100644 --- a/src/coredump/coredumpctl.c +++ b/src/coredump/coredumpctl.c @@ -46,7 +46,7 @@ static const char *arg_directory = NULL; static PagerFlags arg_pager_flags = 0; static int arg_no_legend = false; static int arg_one = false; -static FILE* arg_output = NULL; +static const char* arg_output = NULL; static bool arg_reverse = false; static bool arg_quiet = false; @@ -228,10 +228,7 @@ static int parse_argv(int argc, char *argv[]) { return -EINVAL; } - arg_output = fopen(optarg, "we"); - if (!arg_output) - return log_error_errno(errno, "writing to '%s': %m", optarg); - + arg_output = optarg; break; case 'S': @@ -871,6 +868,7 @@ error: static int dump_core(int argc, char **argv, void *userdata) { _cleanup_(sd_journal_closep) sd_journal *j = NULL; + _cleanup_fclose_ FILE *f = NULL; int r; if (arg_field) { @@ -886,9 +884,15 @@ static int dump_core(int argc, char **argv, void *userdata) { if (r < 0) return r; - print_info(arg_output ? stdout : stderr, j, false); + if (arg_output) { + f = fopen(arg_output, "we"); + if (!f) + return log_error_errno(errno, "Failed to open \"%s\" for writing: %m", arg_output); + } + + print_info(f ? stdout : stderr, j, false); - r = save_core(j, arg_output, NULL, NULL); + r = save_core(j, f, NULL, NULL); if (r < 0) return r; @@ -1090,7 +1094,5 @@ int main(int argc, char *argv[]) { end: pager_close(); - safe_fclose(arg_output); - return r >= 0 ? r : EXIT_FAILURE; }