binfmt,tmpfiles,modules-load,sysctl: rework the various early-boot services that work on .d/ directories
This unifies much of the logic behind them: - All four will now ofllow the rule that the earlier file and earlier assignment in the .d/ directories wins. Before, sysctl was the only outlier, where the later setting always won. - All four now support getopt() and --help on the command line. - All four can now handle specification of configuration file names on the command line to apply. The tools will automatically find them, and apply them. Previously only tmpfiles could do that. This is useful for %post scripts in RPMs and suchlike. - This fixes various error path issues in conf_files_list()
This commit is contained in:
+71
-43
@@ -38,7 +38,7 @@
|
||||
#include "conf-files.h"
|
||||
|
||||
static int files_add(Hashmap *h, const char *root, const char *path, const char *suffix) {
|
||||
_cleanup_closedir_ DIR *dir;
|
||||
_cleanup_closedir_ DIR *dir = NULL;
|
||||
_cleanup_free_ char *dirpath = NULL;
|
||||
|
||||
if (asprintf(&dirpath, "%s%s", root ? root : "", path) < 0)
|
||||
@@ -55,11 +55,11 @@ static int files_add(Hashmap *h, const char *root, const char *path, const char
|
||||
struct dirent *de;
|
||||
union dirent_storage buf;
|
||||
char *p;
|
||||
int err;
|
||||
int r;
|
||||
|
||||
err = readdir_r(dir, &buf.de, &de);
|
||||
if (err != 0)
|
||||
return err;
|
||||
r = readdir_r(dir, &buf.de, &de);
|
||||
if (r != 0)
|
||||
return -r;
|
||||
|
||||
if (!de)
|
||||
break;
|
||||
@@ -67,11 +67,19 @@ static int files_add(Hashmap *h, const char *root, const char *path, const char
|
||||
if (!dirent_is_file_with_suffix(de, suffix))
|
||||
continue;
|
||||
|
||||
if (asprintf(&p, "%s/%s", dirpath, de->d_name) < 0)
|
||||
p = strjoin(dirpath, "/", de->d_name, NULL);
|
||||
if (!p)
|
||||
return -ENOMEM;
|
||||
|
||||
if (hashmap_put(h, path_get_file_name(p), p) <= 0) {
|
||||
log_debug("Skip overridden file: %s.", p);
|
||||
r = hashmap_put(h, path_get_file_name(p), p);
|
||||
if (r == -EEXIST) {
|
||||
log_debug("Skipping overridden file: %s.", p);
|
||||
free(p);
|
||||
} else if (r < 0) {
|
||||
free(p);
|
||||
return r;
|
||||
} else if (r == 0) {
|
||||
log_debug("Duplicate file %s", p);
|
||||
free(p);
|
||||
}
|
||||
}
|
||||
@@ -87,64 +95,84 @@ static int base_cmp(const void *a, const void *b) {
|
||||
return strcmp(path_get_file_name(s1), path_get_file_name(s2));
|
||||
}
|
||||
|
||||
int conf_files_list_strv(char ***strv, const char *suffix, const char *root, const char **dirs) {
|
||||
Hashmap *fh = NULL;
|
||||
char **files = NULL;
|
||||
const char **p;
|
||||
static int conf_files_list_strv_internal(char ***strv, const char *suffix, const char *root, char **dirs) {
|
||||
Hashmap *fh;
|
||||
char **files, **p;
|
||||
int r;
|
||||
|
||||
assert(dirs);
|
||||
assert(strv);
|
||||
assert(suffix);
|
||||
|
||||
/* This alters the dirs string array */
|
||||
if (!path_strv_canonicalize_uniq(dirs))
|
||||
return -ENOMEM;
|
||||
|
||||
fh = hashmap_new(string_hash_func, string_compare_func);
|
||||
if (!fh) {
|
||||
r = -ENOMEM;
|
||||
goto finish;
|
||||
}
|
||||
if (!fh)
|
||||
return -ENOMEM;
|
||||
|
||||
STRV_FOREACH(p, dirs) {
|
||||
r = files_add(fh, root, *p, suffix);
|
||||
if (r < 0)
|
||||
log_warning("Failed to search for files in %s: %s",
|
||||
*p, strerror(-r));
|
||||
if (r == -ENOMEM) {
|
||||
hashmap_free_free(fh);
|
||||
return r;
|
||||
} else if (r < 0)
|
||||
log_debug("Failed to search for files in %s: %s",
|
||||
*p, strerror(-r));
|
||||
}
|
||||
|
||||
files = hashmap_get_strv(fh);
|
||||
if (files == NULL) {
|
||||
log_error("Failed to compose list of files.");
|
||||
r = -ENOMEM;
|
||||
goto finish;
|
||||
hashmap_free_free(fh);
|
||||
return -ENOMEM;
|
||||
}
|
||||
qsort(files, hashmap_size(fh), sizeof(char *), base_cmp);
|
||||
r = 0;
|
||||
|
||||
finish:
|
||||
hashmap_free(fh);
|
||||
qsort(files, hashmap_size(fh), sizeof(char *), base_cmp);
|
||||
*strv = files;
|
||||
return r;
|
||||
|
||||
hashmap_free(fh);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int conf_files_list_strv(char ***strv, const char *suffix, const char *root, const char **dirs) {
|
||||
_cleanup_strv_free_ char **copy = NULL;
|
||||
|
||||
assert(strv);
|
||||
assert(suffix);
|
||||
|
||||
copy = strv_copy((char**) dirs);
|
||||
if (!copy)
|
||||
return -ENOMEM;
|
||||
|
||||
return conf_files_list_strv_internal(strv, suffix, root, copy);
|
||||
}
|
||||
|
||||
int conf_files_list(char ***strv, const char *suffix, const char *root, const char *dir, ...) {
|
||||
char **dirs = NULL;
|
||||
_cleanup_strv_free_ char **dirs = NULL;
|
||||
va_list ap;
|
||||
int r;
|
||||
|
||||
assert(strv);
|
||||
assert(suffix);
|
||||
|
||||
va_start(ap, dir);
|
||||
dirs = strv_new_ap(dir, ap);
|
||||
va_end(ap);
|
||||
if (!dirs) {
|
||||
r = -ENOMEM;
|
||||
goto finish;
|
||||
}
|
||||
|
||||
if (!path_strv_canonicalize(dirs)) {
|
||||
r = -ENOMEM;
|
||||
goto finish;
|
||||
}
|
||||
strv_uniq(dirs);
|
||||
if (!dirs)
|
||||
return -ENOMEM;
|
||||
|
||||
r = conf_files_list_strv(strv, suffix, root, (const char **)dirs);
|
||||
return conf_files_list_strv_internal(strv, suffix, root, dirs);
|
||||
}
|
||||
|
||||
finish:
|
||||
strv_free(dirs);
|
||||
return r;
|
||||
int conf_files_list_nulstr(char ***strv, const char *suffix, const char *root, const char *d) {
|
||||
_cleanup_strv_free_ char **dirs = NULL;
|
||||
|
||||
assert(strv);
|
||||
assert(suffix);
|
||||
|
||||
dirs = strv_split_nulstr(d);
|
||||
if (!dirs)
|
||||
return -ENOMEM;
|
||||
|
||||
return conf_files_list_strv_internal(strv, suffix, root, dirs);
|
||||
}
|
||||
|
||||
@@ -27,5 +27,6 @@
|
||||
|
||||
int conf_files_list(char ***strv, const char *suffix, const char *root, const char *dir, ...);
|
||||
int conf_files_list_strv(char ***strv, const char *suffix, const char *root, const char **dirs);
|
||||
int conf_files_list_nulstr(char ***strv, const char *suffix, const char *root, const char *dirs);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -309,6 +309,17 @@ void hashmap_free_free(Hashmap *h) {
|
||||
hashmap_free(h);
|
||||
}
|
||||
|
||||
void hashmap_free_free_free(Hashmap *h) {
|
||||
|
||||
/* Free the hashmap and all data and key objects in it */
|
||||
|
||||
if (!h)
|
||||
return;
|
||||
|
||||
hashmap_clear_free_free(h);
|
||||
hashmap_free(h);
|
||||
}
|
||||
|
||||
void hashmap_clear(Hashmap *h) {
|
||||
if (!h)
|
||||
return;
|
||||
@@ -327,6 +338,22 @@ void hashmap_clear_free(Hashmap *h) {
|
||||
free(p);
|
||||
}
|
||||
|
||||
void hashmap_clear_free_free(Hashmap *h) {
|
||||
if (!h)
|
||||
return;
|
||||
|
||||
while (h->iterate_list_head) {
|
||||
void *a, *b;
|
||||
|
||||
a = h->iterate_list_head->value;
|
||||
b = (void*) h->iterate_list_head->key;
|
||||
remove_entry(h, h->iterate_list_head);
|
||||
free(a);
|
||||
free(b);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static struct hashmap_entry *hash_scan(Hashmap *h, unsigned hash, const void *key) {
|
||||
struct hashmap_entry *e;
|
||||
assert(h);
|
||||
|
||||
@@ -50,6 +50,7 @@ int uint64_compare_func(const void *a, const void *b);
|
||||
Hashmap *hashmap_new(hash_func_t hash_func, compare_func_t compare_func);
|
||||
void hashmap_free(Hashmap *h);
|
||||
void hashmap_free_free(Hashmap *h);
|
||||
void hashmap_free_free_free(Hashmap *h);
|
||||
Hashmap *hashmap_copy(Hashmap *h);
|
||||
int hashmap_ensure_allocated(Hashmap **h, hash_func_t hash_func, compare_func_t compare_func);
|
||||
|
||||
@@ -77,6 +78,7 @@ void *hashmap_iterate_skip(Hashmap *h, const void *key, Iterator *i);
|
||||
|
||||
void hashmap_clear(Hashmap *h);
|
||||
void hashmap_clear_free(Hashmap *h);
|
||||
void hashmap_clear_free_free(Hashmap *h);
|
||||
|
||||
void *hashmap_steal_first(Hashmap *h);
|
||||
void *hashmap_steal_first_key(Hashmap *h);
|
||||
|
||||
@@ -215,6 +215,16 @@ char **path_strv_canonicalize(char **l) {
|
||||
return l;
|
||||
}
|
||||
|
||||
char **path_strv_canonicalize_uniq(char **l) {
|
||||
if (strv_isempty(l))
|
||||
return l;
|
||||
|
||||
if (!path_strv_canonicalize(l))
|
||||
return NULL;
|
||||
|
||||
return strv_uniq(l);
|
||||
}
|
||||
|
||||
char *path_kill_slashes(char *path) {
|
||||
char *f, *t;
|
||||
bool slash = false;
|
||||
|
||||
@@ -37,6 +37,7 @@ bool path_equal(const char *a, const char *b);
|
||||
|
||||
char **path_strv_make_absolute_cwd(char **l);
|
||||
char **path_strv_canonicalize(char **l);
|
||||
char **path_strv_canonicalize_uniq(char **l);
|
||||
|
||||
int path_is_mount_point(const char *path, bool allow_symlink);
|
||||
int path_is_read_only_fs(const char *path);
|
||||
|
||||
@@ -504,6 +504,22 @@ char **strv_parse_nulstr(const char *s, size_t l) {
|
||||
return v;
|
||||
}
|
||||
|
||||
char **strv_split_nulstr(const char *s) {
|
||||
const char *i;
|
||||
char **r = NULL;
|
||||
|
||||
NULSTR_FOREACH(i, s)
|
||||
if (strv_extend(&r, i) < 0) {
|
||||
strv_free(r);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!r)
|
||||
return strv_new(NULL, NULL);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
bool strv_overlap(char **a, char **b) {
|
||||
char **i, **j;
|
||||
|
||||
|
||||
@@ -62,6 +62,7 @@ char **strv_split_quoted(const char *s) _malloc_;
|
||||
char *strv_join(char **l, const char *separator) _malloc_;
|
||||
|
||||
char **strv_parse_nulstr(const char *s, size_t l);
|
||||
char **strv_split_nulstr(const char *s);
|
||||
|
||||
bool strv_overlap(char **a, char **b);
|
||||
|
||||
|
||||
@@ -5909,3 +5909,82 @@ int on_ac_power(void) {
|
||||
|
||||
return found_online || !found_offline;
|
||||
}
|
||||
|
||||
static int search_and_fopen_internal(const char *path, const char *mode, char **search, FILE **_f) {
|
||||
char **i;
|
||||
|
||||
assert(path);
|
||||
assert(mode);
|
||||
assert(_f);
|
||||
|
||||
if (!path_strv_canonicalize_uniq(search))
|
||||
return -ENOMEM;
|
||||
|
||||
STRV_FOREACH(i, search) {
|
||||
_cleanup_free_ char *p = NULL;
|
||||
FILE *f;
|
||||
|
||||
p = strjoin(*i, "/", path, NULL);
|
||||
if (!p)
|
||||
return -ENOMEM;
|
||||
|
||||
f = fopen(p, mode);
|
||||
if (f) {
|
||||
*_f = f;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (errno != ENOENT)
|
||||
return -errno;
|
||||
}
|
||||
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
int search_and_fopen(const char *path, const char *mode, const char **search, FILE **_f) {
|
||||
_cleanup_strv_free_ char **copy = NULL;
|
||||
|
||||
assert(path);
|
||||
assert(mode);
|
||||
assert(_f);
|
||||
|
||||
if (path_is_absolute(path)) {
|
||||
FILE *f;
|
||||
|
||||
f = fopen(path, mode);
|
||||
if (f) {
|
||||
*_f = f;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -errno;
|
||||
}
|
||||
|
||||
copy = strv_copy((char**) search);
|
||||
if (!copy)
|
||||
return -ENOMEM;
|
||||
|
||||
return search_and_fopen_internal(path, mode, copy, _f);
|
||||
}
|
||||
|
||||
int search_and_fopen_nulstr(const char *path, const char *mode, const char *search, FILE **_f) {
|
||||
_cleanup_strv_free_ char **s = NULL;
|
||||
|
||||
if (path_is_absolute(path)) {
|
||||
FILE *f;
|
||||
|
||||
f = fopen(path, mode);
|
||||
if (f) {
|
||||
*_f = f;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -errno;
|
||||
}
|
||||
|
||||
s = strv_split_nulstr(search);
|
||||
if (!s)
|
||||
return -ENOMEM;
|
||||
|
||||
return search_and_fopen_internal(path, mode, s, _f);
|
||||
}
|
||||
|
||||
@@ -568,6 +568,9 @@ char *strip_tab_ansi(char **p, size_t *l);
|
||||
|
||||
int on_ac_power(void);
|
||||
|
||||
int search_and_fopen(const char *path, const char *mode, const char **search, FILE **_f);
|
||||
int search_and_fopen_nulstr(const char *path, const char *mode, const char *search, FILE **_f);
|
||||
|
||||
#define FOREACH_LINE(f, line, on_error) \
|
||||
for (char line[LINE_MAX]; !feof(f); ) \
|
||||
if (!fgets(line, sizeof(line), f)) { \
|
||||
|
||||
Reference in New Issue
Block a user