From: Zbigniew Jędrzejewski-Szmek Date: Thu, 26 Apr 2018 11:49:50 +0000 (+0200) Subject: analyze: add 'cat-config' verb X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=bc9e512db2edf97787aecdfafa922b1bbc142cc5;p=elogind.git analyze: add 'cat-config' verb This is used as 'systemd-analyze show-config systemd/logind.conf', which will dump /etc/systemd/system/user@.service /etc/systemd/system/user@.service.d/*.conf /run/systemd/system/user@.service.d/*.conf /usr/local/lib/systemd/system/user@.service.d/*.conf /usr/lib/systemd/system/user@.service.d/*.conf The idea is to make it easy to dump the configuration using the same locations and order that systemd programs use themselves (including masking, in the right order, etc.). This is the generic variant that works with any configuration scheme that follows the same general rules: $ systemd-analyze cat-config systemd/system.conf $ systemd-analyze cat-config systemd/user.conf $ systemd-analyze cat-config systemd/logind.conf $ systemd-analyze cat-config systemd/sleep.conf $ systemd-analyze cat-config systemd/journald.conf $ systemd-analyze cat-config systemd/journal-remote.conf $ systemd-analyze cat-config systemd/journal-upload.conf $ systemd-analyze cat-config systemd/coredump.conf $ systemd-analyze cat-config systemd/resolved.conf $ systemd-analyze cat-config systemd/timesyncd.conf $ systemd-analyze cat-config udev/udev.conf --- diff --git a/src/basic/conf-files.c b/src/basic/conf-files.c index b5cad5a6e..29c415aa7 100644 --- a/src/basic/conf-files.c +++ b/src/basic/conf-files.c @@ -13,6 +13,7 @@ #include #include "conf-files.h" +//#include "def.h" #include "dirent-util.h" #include "fd-util.h" #include "hashmap.h" @@ -23,6 +24,7 @@ #include "stat-util.h" #include "string-util.h" #include "strv.h" +//#include "terminal-util.h" #include "util.h" static int files_add(Hashmap *h, const char *suffix, const char *root, unsigned flags, const char *path) { @@ -152,8 +154,8 @@ int conf_files_insert(char ***strv, const char *root, char **dirs, const char *p * - do nothing if our new entry matches the existing entry, * - replace the existing entry if our new entry has higher priority. */ - size_t i; char *t; + unsigned i; int r; for (i = 0; i < strv_length(*strv); i++) { @@ -256,3 +258,33 @@ int conf_files_list_nulstr(char ***strv, const char *suffix, const char *root, u return conf_files_list_strv_internal(strv, suffix, root, flags, d); } + +int conf_files_cat(const char *name) { + _cleanup_strv_free_ char **dirs = NULL, **files = NULL; + const char *dir; + char **t; + int r; + + NULSTR_FOREACH(dir, CONF_PATHS_NULSTR("")) { + assert(endswith(dir, "/")); + r = strv_extendf(&dirs, "%s%s.d", dir, name); + if (r < 0) + return log_error("Failed to build directory list: %m"); + } + + r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char* const*) dirs); + if (r < 0) + return log_error_errno(r, "Failed to query file list: %m"); + + name = strjoina("/etc/", name); + + if (DEBUG_LOGGING) { + log_debug("Looking for configuration in:"); + log_debug(" %s", name); + STRV_FOREACH(t, dirs) + log_debug(" %s/*.conf", *t); + } + + /* show */ + return cat_files(name, files, CAT_FLAGS_MAIN_FILE_OPTIONAL); +} diff --git a/src/basic/conf-files.h b/src/basic/conf-files.h index b902c3a3b..3d1feadf0 100644 --- a/src/basic/conf-files.h +++ b/src/basic/conf-files.h @@ -17,3 +17,4 @@ int conf_files_list_strv(char ***ret, const char *suffix, const char *root, unsi int conf_files_list_nulstr(char ***ret, const char *suffix, const char *root, unsigned flags, const char *dirs); int conf_files_insert(char ***strv, const char *root, char **dirs, const char *path); int conf_files_insert_nulstr(char ***strv, const char *root, const char *dirs, const char *path); +int conf_files_cat(const char *name); diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c index abd125f8f..00462118e 100644 --- a/src/basic/terminal-util.c +++ b/src/basic/terminal-util.c @@ -1400,13 +1400,18 @@ static int cat_file(const char *filename, bool newline) { return copy_bytes(fd, STDOUT_FILENO, (uint64_t) -1, 0); } -int cat_files(const char *file, char **dropins) { +int cat_files(const char *file, char **dropins, CatFlags flags) { char **path; int r; if (file) { r = cat_file(file, false); - if (r < 0) + if (r == -ENOENT && (flags & CAT_FLAGS_MAIN_FILE_OPTIONAL)) + printf("%s# config file %s not found%s\n", + ansi_highlight_magenta(), + file, + ansi_normal()); + else if (r < 0) return log_warning_errno(r, "Failed to cat %s: %m", file); } diff --git a/src/basic/terminal-util.h b/src/basic/terminal-util.h index 4550e1fc0..e7b64c63d 100644 --- a/src/basic/terminal-util.h +++ b/src/basic/terminal-util.h @@ -149,6 +149,7 @@ DEFINE_ANSI_FUNC(highlight_red, HIGHLIGHT_RED); DEFINE_ANSI_FUNC(highlight_green, HIGHLIGHT_GREEN); DEFINE_ANSI_FUNC(highlight_yellow, HIGHLIGHT_YELLOW); DEFINE_ANSI_FUNC(highlight_blue, HIGHLIGHT_BLUE); +DEFINE_ANSI_FUNC(highlight_magenta, HIGHLIGHT_MAGENTA); DEFINE_ANSI_FUNC(normal, NORMAL); DEFINE_ANSI_FUNC_UNDERLINE(underline, UNDERLINE, NORMAL); @@ -178,4 +179,8 @@ int vt_reset_keyboard(int fd); int terminal_urlify(const char *url, const char *text, char **ret); int terminal_urlify_path(const char *path, const char *text, char **ret); -int cat_files(const char *file, char **files); +typedef enum CatFlags { + CAT_FLAGS_MAIN_FILE_OPTIONAL = 1 << 1, +} CatFlags; + +int cat_files(const char *file, char **dropins, CatFlags flags);