399a2a8
From bddb25b6d9f5b1cf7f7219b3f23e3668f9563d6e Mon Sep 17 00:00:00 2001
399a2a8
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
399a2a8
Date: Sat, 7 Feb 2015 11:35:37 -0500
399a2a8
Subject: [PATCH] systemctl: support auditd.service better
399a2a8
399a2a8
We would print the filename header before trying to open the file. But since
399a2a8
the header was printed to stdout, and the error to stderr, the error would appear
399a2a8
on the terminal before the header. It is cleaner to open the file first, then
399a2a8
and only then print the header.
399a2a8
399a2a8
Also exit on first error. We shouldn't report success if we were unable to open
399a2a8
a file.
399a2a8
399a2a8
(cherry picked from commit 8527b07be1c5211b50a1a6496585952857a25c73)
399a2a8
---
399a2a8
 src/systemctl/systemctl.c | 46 +++++++++++++++++++++++-----------------------
399a2a8
 1 file changed, 23 insertions(+), 23 deletions(-)
399a2a8
399a2a8
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
399a2a8
index 3da4d3d4f1..4ec0cff21d 100644
399a2a8
--- a/src/systemctl/systemctl.c
399a2a8
+++ b/src/systemctl/systemctl.c
399a2a8
@@ -4555,6 +4555,23 @@ static int init_home_and_lookup_paths(char **user_home, char **user_runtime, Loo
399a2a8
         return 0;
399a2a8
 }
399a2a8
 
399a2a8
+static int cat_file(const char *filename, bool newline) {
399a2a8
+        _cleanup_close_ int fd;
399a2a8
+
399a2a8
+        fd = open(filename, O_RDONLY|O_CLOEXEC|O_NOCTTY);
399a2a8
+        if (fd < 0)
399a2a8
+                return -errno;
399a2a8
+
399a2a8
+        printf("%s%s# %s%s\n",
399a2a8
+               newline ? "\n" : "",
399a2a8
+               ansi_highlight_blue(),
399a2a8
+               filename,
399a2a8
+               ansi_highlight_off());
399a2a8
+        fflush(stdout);
399a2a8
+
399a2a8
+        return copy_bytes(fd, STDOUT_FILENO, (off_t) -1, false);
399a2a8
+}
399a2a8
+
399a2a8
 static int cat(sd_bus *bus, char **args) {
399a2a8
         _cleanup_free_ char *user_home = NULL;
399a2a8
         _cleanup_free_ char *user_runtime = NULL;
399a2a8
@@ -4600,32 +4617,15 @@ static int cat(sd_bus *bus, char **args) {
399a2a8
                         puts("");
399a2a8
 
399a2a8
                 if (fragment_path) {
399a2a8
-                        printf("%s# %s%s\n",
399a2a8
-                               ansi_highlight_blue(),
399a2a8
-                               fragment_path,
399a2a8
-                               ansi_highlight_off());
399a2a8
-                        fflush(stdout);
399a2a8
-
399a2a8
-                        r = copy_file_fd(fragment_path, STDOUT_FILENO, false);
399a2a8
-                        if (r < 0) {
399a2a8
-                                log_warning_errno(r, "Failed to cat %s: %m", fragment_path);
399a2a8
-                                continue;
399a2a8
-                        }
399a2a8
+                        r = cat_file(fragment_path, false);
399a2a8
+                        if (r < 0)
399a2a8
+                                return log_warning_errno(r, "Failed to cat %s: %m", fragment_path);
399a2a8
                 }
399a2a8
 
399a2a8
                 STRV_FOREACH(path, dropin_paths) {
399a2a8
-                        printf("%s%s# %s%s\n",
399a2a8
-                               isempty(fragment_path) && path == dropin_paths ? "" : "\n",
399a2a8
-                               ansi_highlight_blue(),
399a2a8
-                               *path,
399a2a8
-                               ansi_highlight_off());
399a2a8
-                        fflush(stdout);
399a2a8
-
399a2a8
-                        r = copy_file_fd(*path, STDOUT_FILENO, false);
399a2a8
-                        if (r < 0) {
399a2a8
-                                log_warning_errno(r, "Failed to cat %s: %m", *path);
399a2a8
-                                continue;
399a2a8
-                        }
399a2a8
+                        r = cat_file(*path, path == dropin_paths);
399a2a8
+                        if (r < 0)
399a2a8
+                                return log_warning_errno(r, "Failed to cat %s: %m", *path);
399a2a8
                 }
399a2a8
         }
399a2a8