diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2015-02-07 11:35:37 -0500 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2015-02-23 23:38:31 -0500 |
commit | 8527b07be1c5211b50a1a6496585952857a25c73 (patch) | |
tree | 1d22d5a2d267ac1377e8c2741f12150541a0f12c /src/systemctl | |
parent | d028e01814a405e83c400c60545785d35dba2a17 (diff) |
systemctl: support auditd.service better
We would print the filename header before trying to open the file. But since
the header was printed to stdout, and the error to stderr, the error would appear
on the terminal before the header. It is cleaner to open the file first, then
and only then print the header.
Also exit on first error. We shouldn't report success if we were unable to open
a file.
Diffstat (limited to 'src/systemctl')
-rw-r--r-- | src/systemctl/systemctl.c | 46 |
1 files changed, 23 insertions, 23 deletions
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 85c50004f8..4da4113e3b 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -4514,6 +4514,23 @@ static int init_home_and_lookup_paths(char **user_home, char **user_runtime, Loo return 0; } +static int cat_file(const char *filename, bool newline) { + _cleanup_close_ int fd; + + fd = open(filename, O_RDONLY|O_CLOEXEC|O_NOCTTY); + if (fd < 0) + return -errno; + + printf("%s%s# %s%s\n", + newline ? "\n" : "", + ansi_highlight_blue(), + filename, + ansi_highlight_off()); + fflush(stdout); + + return copy_bytes(fd, STDOUT_FILENO, (off_t) -1, false); +} + static int cat(sd_bus *bus, char **args) { _cleanup_free_ char *user_home = NULL; _cleanup_free_ char *user_runtime = NULL; @@ -4559,32 +4576,15 @@ static int cat(sd_bus *bus, char **args) { puts(""); if (fragment_path) { - printf("%s# %s%s\n", - ansi_highlight_blue(), - fragment_path, - ansi_highlight_off()); - fflush(stdout); - - r = copy_file_fd(fragment_path, STDOUT_FILENO, false); - if (r < 0) { - log_warning_errno(r, "Failed to cat %s: %m", fragment_path); - continue; - } + r = cat_file(fragment_path, false); + if (r < 0) + return log_warning_errno(r, "Failed to cat %s: %m", fragment_path); } STRV_FOREACH(path, dropin_paths) { - printf("%s%s# %s%s\n", - isempty(fragment_path) && path == dropin_paths ? "" : "\n", - ansi_highlight_blue(), - *path, - ansi_highlight_off()); - fflush(stdout); - - r = copy_file_fd(*path, STDOUT_FILENO, false); - if (r < 0) { - log_warning_errno(r, "Failed to cat %s: %m", *path); - continue; - } + r = cat_file(*path, path == dropin_paths); + if (r < 0) + return log_warning_errno(r, "Failed to cat %s: %m", *path); } } |