Blame SOURCES/0054-systemctl-support-auditd.service-better.patch

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