summaryrefslogtreecommitdiff
path: root/src/coredump
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2018-11-20 09:27:19 +0100
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2018-11-20 16:48:21 +0100
commit158ecef56b63aff23bc6fc04b7a7fce08a56b55c (patch)
treeb4e09bcf672cfcf4406b9ffa5019ea9463432572 /src/coredump
parentcac0b957906f282b4a54e28187f24d4789aa2bad (diff)
downloadsystemd-158ecef56b63aff23bc6fc04b7a7fce08a56b55c.tar.gz
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.
Diffstat (limited to 'src/coredump')
-rw-r--r--src/coredump/coredumpctl.c20
1 files changed, 11 insertions, 9 deletions
diff --git a/src/coredump/coredumpctl.c b/src/coredump/coredumpctl.c
index 6160b4bf7a..906b45488a 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;
}