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