ryantimwilson / rpms / systemd

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