Merge pull request #10408 from keszybz/analyze-cat-presets
systemd-analyze cat-presets
This commit is contained in:
+25
-27
@@ -15,6 +15,7 @@
|
||||
#include "alloc-util.h"
|
||||
#include "conf-files.h"
|
||||
#include "conf-parser.h"
|
||||
#include "def.h"
|
||||
#include "dirent-util.h"
|
||||
#include "extract-word.h"
|
||||
#include "fd-util.h"
|
||||
@@ -2820,6 +2821,29 @@ static int split_pattern_into_name_and_instances(const char *pattern, char **out
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int presets_find_config(UnitFileScope scope, const char *root_dir, char ***files) {
|
||||
const char* const* dirs;
|
||||
|
||||
assert(scope >= 0);
|
||||
assert(scope < _UNIT_FILE_SCOPE_MAX);
|
||||
|
||||
switch (scope) {
|
||||
case UNIT_FILE_SYSTEM:
|
||||
dirs = (const char* const*) CONF_PATHS_STRV("systemd/system-preset");
|
||||
break;
|
||||
|
||||
case UNIT_FILE_GLOBAL:
|
||||
case UNIT_FILE_USER:
|
||||
dirs = (const char* const*) CONF_PATHS_USR_STRV("systemd/user-preset");
|
||||
break;
|
||||
|
||||
default:
|
||||
assert_not_reached("Invalid unit file scope");
|
||||
}
|
||||
|
||||
return conf_files_list_strv(files, ".preset", root_dir, 0, dirs);
|
||||
}
|
||||
|
||||
static int read_presets(UnitFileScope scope, const char *root_dir, Presets *presets) {
|
||||
_cleanup_(presets_freep) Presets ps = {};
|
||||
size_t n_allocated = 0;
|
||||
@@ -2831,33 +2855,7 @@ static int read_presets(UnitFileScope scope, const char *root_dir, Presets *pres
|
||||
assert(scope < _UNIT_FILE_SCOPE_MAX);
|
||||
assert(presets);
|
||||
|
||||
switch (scope) {
|
||||
case UNIT_FILE_SYSTEM:
|
||||
r = conf_files_list(&files, ".preset", root_dir, 0,
|
||||
"/etc/systemd/system-preset",
|
||||
"/run/systemd/system-preset",
|
||||
"/usr/local/lib/systemd/system-preset",
|
||||
"/usr/lib/systemd/system-preset",
|
||||
#if HAVE_SPLIT_USR
|
||||
"/lib/systemd/system-preset",
|
||||
#endif
|
||||
NULL);
|
||||
break;
|
||||
|
||||
case UNIT_FILE_GLOBAL:
|
||||
case UNIT_FILE_USER:
|
||||
r = conf_files_list(&files, ".preset", root_dir, 0,
|
||||
"/etc/systemd/user-preset",
|
||||
"/run/systemd/user-preset",
|
||||
"/usr/local/lib/systemd/user-preset",
|
||||
"/usr/lib/systemd/user-preset",
|
||||
NULL);
|
||||
break;
|
||||
|
||||
default:
|
||||
assert_not_reached("Invalid unit file scope");
|
||||
}
|
||||
|
||||
r = presets_find_config(scope, root_dir, &files);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
|
||||
+65
-10
@@ -213,33 +213,88 @@ void print_separator(void) {
|
||||
fputs("\n\n", stdout);
|
||||
}
|
||||
|
||||
static int guess_type(const char **name, bool *is_usr, bool *is_collection, const char **extension) {
|
||||
/* Try to figure out if name is like tmpfiles.d/ or systemd/system-presets/,
|
||||
* i.e. a collection of directories without a main config file. */
|
||||
|
||||
_cleanup_free_ char *n = NULL;
|
||||
bool usr = false, coll = false;
|
||||
const char *ext = ".conf";
|
||||
|
||||
if (path_equal(*name, "environment.d"))
|
||||
/* Special case: we need to include /etc/environment in the search path, even
|
||||
* though the whole concept is called environment.d. */
|
||||
*name = "environment";
|
||||
|
||||
n = strdup(*name);
|
||||
if (!n)
|
||||
return log_oom();
|
||||
|
||||
delete_trailing_chars(n, "/");
|
||||
|
||||
if (endswith(n, ".d"))
|
||||
coll = true;
|
||||
|
||||
if (path_equal(n, "environment"))
|
||||
usr = true;
|
||||
|
||||
if (path_equal(n, "udev/hwdb.d"))
|
||||
ext = ".hwdb";
|
||||
|
||||
if (path_equal(n, "udev/rules.d"))
|
||||
ext = ".rules";
|
||||
|
||||
if (PATH_IN_SET(n, "systemd/system-preset", "systemd/user-preset")) {
|
||||
coll = true;
|
||||
ext = ".preset";
|
||||
}
|
||||
|
||||
if (path_equal(n, "systemd/user-preset"))
|
||||
usr = true;
|
||||
|
||||
*is_usr = usr;
|
||||
*is_collection = coll;
|
||||
*extension = ext;
|
||||
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 **dir;
|
||||
bool is_usr, is_collection;
|
||||
const char *extension;
|
||||
char **t;
|
||||
int r;
|
||||
|
||||
NULSTR_FOREACH(dir, CONF_PATHS_NULSTR("")) {
|
||||
assert(endswith(dir, "/"));
|
||||
r = strv_extendf(&dirs, "%s%s.d", dir, name);
|
||||
r = guess_type(&name, &is_usr, &is_collection, &extension);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
STRV_FOREACH(dir, is_usr ? CONF_PATHS_USR_STRV("") : CONF_PATHS_STRV("")) {
|
||||
assert(endswith(*dir, "/"));
|
||||
r = strv_extendf(&dirs, "%s%s%s", *dir, name,
|
||||
is_collection ? "" : ".d");
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to build directory list: %m");
|
||||
}
|
||||
|
||||
r = conf_files_list_strv(&files, ".conf", root, 0, (const char* const*) dirs);
|
||||
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");
|
||||
|
||||
path = path_join(root, "/etc", name);
|
||||
if (!path)
|
||||
return log_oom();
|
||||
if (!is_collection) {
|
||||
path = path_join(root, "/etc", name);
|
||||
if (!path)
|
||||
return log_oom();
|
||||
}
|
||||
|
||||
if (DEBUG_LOGGING) {
|
||||
log_debug("Looking for configuration in:");
|
||||
log_debug(" %s", path);
|
||||
if (path)
|
||||
log_debug(" %s", path);
|
||||
STRV_FOREACH(t, dirs)
|
||||
log_debug(" %s/*.conf", *t);
|
||||
log_debug(" %s/*%s", *t, extension);
|
||||
}
|
||||
|
||||
/* show */
|
||||
|
||||
Reference in New Issue
Block a user