From 8621f957b6e3a7eed1c5965d332ad1c4c594f26e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
Date: Sat, 20 Nov 2021 11:42:31 +0100
Subject: [PATCH] analyze: fix printing config when there is no main config
 file

Since 8b8024f1c231c166f5c450905c8fd91d11704ae7 and the follow-up commits, the
main config file may be located in /usr or in other paths. But the code in
analyze.c was still assuming that it must be in /etc. Things mostly worked for
our own config files because we usually install a comments-only file in /etc,
but was not correct in the general case.

This fixes in particular 'systemd-analyze cat-config systemd/zram-generator.conf'.
In Fedora we distribute a config file in zram-generator-defaults.rpm that is in
/usr/lib, and 'cat-config' would refuse to show it because
/etc/systemd/zram-generator.conf does not exist.

The main config file is optional, but let's print an informative message
because this is a slightly unusual case.

The file paths that we printed were missing the root prefix.

(cherry picked from commit 0895e87348e5fc02f50498cad5922eb3eb172323)

Conflict:NA
Reference:https://github.com/systemd/systemd/commit/8621f957b6e3a7eed1c5965d332ad1c4c594f26e
---
 src/shared/pretty-print.c | 43 ++++++++++++++++++++++++++-------------
 1 file changed, 29 insertions(+), 14 deletions(-)

diff --git a/src/shared/pretty-print.c b/src/shared/pretty-print.c
index 137ba77b3a..97baeda401 100644
--- a/src/shared/pretty-print.c
+++ b/src/shared/pretty-print.c
@@ -300,24 +300,39 @@ int conf_files_cat(const char *root, const char *name) {
                         return log_error_errno(r, "Failed to build directory list: %m");
         }
 
-        r = conf_files_list_strv(&files, extension, root, 0, (const char* const*) dirs);
-        if (r < 0)
-                return log_error_errno(r, "Failed to query file list: %m");
+        if (DEBUG_LOGGING) {
+                log_debug("Looking for configuration in:");
+                if (!is_collection)
+                        STRV_FOREACH(prefix, prefixes)
+                                log_debug("   %s%s%s", strempty(root), *prefix, name);
 
+                STRV_FOREACH(t, dirs)
+                        log_debug("   %s%s/*%s", strempty(root), *t, extension);
+        }
+
+        /* First locate the main config file, if any */
         if (!is_collection) {
-                path = path_join(root, "/etc", name);
+                STRV_FOREACH(prefix, prefixes) {
+                        path = path_join(root, *prefix, name);
+                        if (!path)
+                                return log_oom();
+                        if (access(path, F_OK) == 0)
+                                break;
+                        path = mfree(path);
+                }
+
                 if (!path)
-                        return log_oom();
+                        printf("%s# Main configuration file %s not found%s\n",
+                               ansi_highlight_magenta(),
+                               name,
+                               ansi_normal());
         }
 
-        if (DEBUG_LOGGING) {
-                log_debug("Looking for configuration in:");
-                if (path)
-                        log_debug("   %s", path);
-                STRV_FOREACH(t, dirs)
-                        log_debug("   %s/*%s", *t, extension);
-        }
+        /* Then locate the drop-ins, if any */
+        r = conf_files_list_strv(&files, extension, root, 0, (const char* const*) dirs);
+        if (r < 0)
+                return log_error_errno(r, "Failed to query file list: %m");
 
-        /* show */
-        return cat_files(path, files, CAT_FLAGS_MAIN_FILE_OPTIONAL);
+        /* Show */
+        return cat_files(path, files, 0);
 }
-- 
2.33.0

