Merge pull request #8824 from keszybz/analyze-show-config
systemd-analyze show-config
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
#include <string.h>
|
||||
|
||||
#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) {
|
||||
@@ -256,3 +258,71 @@ 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_list_with_replacement(
|
||||
const char *root,
|
||||
char **config_dirs,
|
||||
const char *replacement,
|
||||
char ***files,
|
||||
char **replace_file) {
|
||||
|
||||
_cleanup_strv_free_ char **f = NULL;
|
||||
_cleanup_free_ char *p = NULL;
|
||||
int r;
|
||||
|
||||
assert(config_dirs);
|
||||
assert(files);
|
||||
assert(replace_file || !replacement);
|
||||
|
||||
r = conf_files_list_strv(&f, ".conf", root, 0, (const char* const*) config_dirs);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to enumerate config files: %m");
|
||||
|
||||
if (replacement) {
|
||||
r = conf_files_insert(&f, root, config_dirs, replacement);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to extend config file list: %m");
|
||||
|
||||
p = path_join(root, replacement, NULL);
|
||||
if (!p)
|
||||
return log_oom();
|
||||
}
|
||||
|
||||
*files = TAKE_PTR(f);
|
||||
if (replace_file)
|
||||
*replace_file = TAKE_PTR(p);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int conf_files_cat(const char *root, const char *name) {
|
||||
_cleanup_strv_free_ char **dirs = NULL, **files = NULL;
|
||||
_cleanup_free_ char *path = 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", root, 0, (const char* const*) dirs);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to query file list: %m");
|
||||
|
||||
path = path_join(root, "/etc", name);
|
||||
if (!path)
|
||||
return log_oom();
|
||||
|
||||
if (DEBUG_LOGGING) {
|
||||
log_debug("Looking for configuration in:");
|
||||
log_debug(" %s", path);
|
||||
STRV_FOREACH(t, dirs)
|
||||
log_debug(" %s/*.conf", *t);
|
||||
}
|
||||
|
||||
/* show */
|
||||
return cat_files(path, files, CAT_FLAGS_MAIN_FILE_OPTIONAL);
|
||||
}
|
||||
|
||||
@@ -17,3 +17,10 @@ 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_list_with_replacement(
|
||||
const char *root,
|
||||
char **config_dirs,
|
||||
const char *replacement,
|
||||
char ***files,
|
||||
char **replace_file);
|
||||
int conf_files_cat(const char *root, const char *name);
|
||||
|
||||
+12
-2
@@ -54,8 +54,10 @@
|
||||
#define NOTIFY_BUFFER_MAX PIPE_BUF
|
||||
|
||||
#if HAVE_SPLIT_USR
|
||||
# define _CONF_PATHS_SPLIT_USR(n) "/lib/" n "\0"
|
||||
# define _CONF_PATHS_SPLIT_USR_NULSTR(n) "/lib/" n "\0"
|
||||
# define _CONF_PATHS_SPLIT_USR(n) , "/lib/" n
|
||||
#else
|
||||
# define _CONF_PATHS_SPLIT_USR_NULSTR(n)
|
||||
# define _CONF_PATHS_SPLIT_USR(n)
|
||||
#endif
|
||||
|
||||
@@ -68,6 +70,14 @@
|
||||
"/run/" n "\0" \
|
||||
"/usr/local/lib/" n "\0" \
|
||||
"/usr/lib/" n "\0" \
|
||||
_CONF_PATHS_SPLIT_USR(n)
|
||||
_CONF_PATHS_SPLIT_USR_NULSTR(n)
|
||||
|
||||
#define CONF_PATHS_STRV(n) \
|
||||
STRV_MAKE( \
|
||||
"/etc/" n, \
|
||||
"/run/" n, \
|
||||
"/usr/local/lib/" n, \
|
||||
"/usr/lib/" n \
|
||||
_CONF_PATHS_SPLIT_USR(n))
|
||||
|
||||
#define LONG_LINE_MAX (1U*1024U*1024U)
|
||||
|
||||
@@ -28,6 +28,8 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "copy.h"
|
||||
#include "def.h"
|
||||
#include "env-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "fileio.h"
|
||||
@@ -1362,3 +1364,57 @@ int terminal_urlify_path(const char *path, const char *text, char **ret) {
|
||||
|
||||
return terminal_urlify(url, text, ret);
|
||||
}
|
||||
|
||||
static int cat_file(const char *filename, bool newline) {
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
int r;
|
||||
|
||||
f = fopen(filename, "re");
|
||||
if (!f)
|
||||
return -errno;
|
||||
|
||||
printf("%s%s# %s%s\n",
|
||||
newline ? "\n" : "",
|
||||
ansi_highlight_blue(),
|
||||
filename,
|
||||
ansi_normal());
|
||||
fflush(stdout);
|
||||
|
||||
for (;;) {
|
||||
_cleanup_free_ char *line = NULL;
|
||||
|
||||
r = read_line(f, LONG_LINE_MAX, &line);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to read \"%s\": %m", filename);
|
||||
if (r == 0)
|
||||
break;
|
||||
|
||||
puts(line);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cat_files(const char *file, char **dropins, CatFlags flags) {
|
||||
char **path;
|
||||
int r;
|
||||
|
||||
if (file) {
|
||||
r = cat_file(file, false);
|
||||
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);
|
||||
}
|
||||
|
||||
STRV_FOREACH(path, dropins) {
|
||||
r = cat_file(*path, file || path != dropins);
|
||||
if (r < 0)
|
||||
return log_warning_errno(r, "Failed to cat %s: %m", *path);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -133,6 +133,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);
|
||||
@@ -159,3 +160,9 @@ 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);
|
||||
|
||||
typedef enum CatFlags {
|
||||
CAT_FLAGS_MAIN_FILE_OPTIONAL = 1 << 1,
|
||||
} CatFlags;
|
||||
|
||||
int cat_files(const char *file, char **dropins, CatFlags flags);
|
||||
|
||||
Reference in New Issue
Block a user