ryantimwilson / rpms / systemd

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