systemctl: fix silent failure when --root is not found

Some calls to lookup_path_init() were not followed by any log emission.
E.g.:
$ SYSTEMD_LOG_LEVEL=debug systemctl --root=/missing enable unit; echo $?
1

Let's add a helper function and use it in various places.

$ SYSTEMD_LOG_LEVEL=debug build/systemctl --root=/missing enable unit; echo $?
Failed to initialize unit search paths for root directory /missing: No such file or directory
1
$ SYSTEMCTL_SKIP_SYSV=1 build/systemctl --root=/missing enable unit; echo $?
Failed to initialize unit search paths for root directory /missing: No such file or directory
Failed to enable: No such file or directory.
1

The repeated error in the second case is not very nice, but this is a niche
case and I don't think it's worth the trouble to trying to avoid it.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2022-03-29 16:17:56 +02:00
parent 0d11db5982
commit 99aad9a2b9
14 changed files with 96 additions and 113 deletions
+29 -56
View File
@@ -16,9 +16,8 @@ static int parse_env_file_internal(
FILE *f,
const char *fname,
int (*push) (const char *filename, unsigned line,
const char *key, char *value, void *userdata, int *n_pushed),
void *userdata,
int *n_pushed) {
const char *key, char *value, void *userdata),
void *userdata) {
size_t n_key = 0, n_value = 0, last_value_whitespace = SIZE_MAX, last_key_whitespace = SIZE_MAX;
_cleanup_free_ char *contents = NULL, *key = NULL, *value = NULL;
@@ -99,7 +98,7 @@ static int parse_env_file_internal(
if (last_key_whitespace != SIZE_MAX)
key[last_key_whitespace] = 0;
r = push(fname, line, key, value, userdata, n_pushed);
r = push(fname, line, key, value, userdata);
if (r < 0)
return r;
@@ -142,7 +141,7 @@ static int parse_env_file_internal(
if (last_key_whitespace != SIZE_MAX)
key[last_key_whitespace] = 0;
r = push(fname, line, key, value, userdata, n_pushed);
r = push(fname, line, key, value, userdata);
if (r < 0)
return r;
@@ -261,7 +260,7 @@ static int parse_env_file_internal(
if (last_key_whitespace != SIZE_MAX)
key[last_key_whitespace] = 0;
r = push(fname, line, key, value, userdata, n_pushed);
r = push(fname, line, key, value, userdata);
if (r < 0)
return r;
@@ -299,8 +298,7 @@ static int check_utf8ness_and_warn(
static int parse_env_file_push(
const char *filename, unsigned line,
const char *key, char *value,
void *userdata,
int *n_pushed) {
void *userdata) {
const char *k;
va_list aq, *ap = userdata;
@@ -322,9 +320,6 @@ static int parse_env_file_push(
free(*v);
*v = value;
if (n_pushed)
(*n_pushed)++;
return 1;
}
}
@@ -340,16 +335,13 @@ int parse_env_filev(
const char *fname,
va_list ap) {
int r, n_pushed = 0;
int r;
va_list aq;
va_copy(aq, ap);
r = parse_env_file_internal(f, fname, parse_env_file_push, &aq, &n_pushed);
r = parse_env_file_internal(f, fname, parse_env_file_push, &aq);
va_end(aq);
if (r < 0)
return r;
return n_pushed;
return r;
}
int parse_env_file_sentinel(
@@ -370,8 +362,7 @@ int parse_env_file_sentinel(
static int load_env_file_push(
const char *filename, unsigned line,
const char *key, char *value,
void *userdata,
int *n_pushed) {
void *userdata) {
char ***m = userdata;
char *p;
int r;
@@ -388,34 +379,28 @@ static int load_env_file_push(
if (r < 0)
return r;
if (n_pushed)
(*n_pushed)++;
free(value);
return 0;
}
int load_env_file(FILE *f, const char *fname, char ***rl) {
char **m = NULL;
_cleanup_strv_free_ char **m = NULL;
int r;
r = parse_env_file_internal(f, fname, load_env_file_push, &m, NULL);
if (r < 0) {
strv_free(m);
r = parse_env_file_internal(f, fname, load_env_file_push, &m);
if (r < 0)
return r;
}
*rl = m;
*rl = TAKE_PTR(m);
return 0;
}
static int load_env_file_push_pairs(
const char *filename, unsigned line,
const char *key, char *value,
void *userdata,
int *n_pushed) {
void *userdata) {
char ***m = ASSERT_PTR(userdata);
bool added = false;
int r;
r = check_utf8ness_and_warn(filename, line, key, value);
@@ -426,49 +411,37 @@ static int load_env_file_push_pairs(
for (char **t = *m; t && *t; t += 2)
if (streq(t[0], key)) {
if (value)
r = free_and_replace(t[1], value);
return free_and_replace(t[1], value);
else
r = free_and_strdup(t+1, "");
goto finish;
return free_and_strdup(t+1, "");
}
r = strv_extend(m, key);
if (r < 0)
return -ENOMEM;
if (value)
r = strv_push(m, value);
else
r = strv_extend(m, "");
added = true;
finish:
if (r < 0)
return r;
if (n_pushed && added)
(*n_pushed)++;
return 0;
if (value)
return strv_push(m, value);
else
return strv_extend(m, "");
}
int load_env_file_pairs(FILE *f, const char *fname, char ***rl) {
char **m = NULL;
_cleanup_strv_free_ char **m = NULL;
int r;
r = parse_env_file_internal(f, fname, load_env_file_push_pairs, &m, NULL);
if (r < 0) {
strv_free(m);
r = parse_env_file_internal(f, fname, load_env_file_push_pairs, &m);
if (r < 0)
return r;
}
*rl = m;
*rl = TAKE_PTR(m);
return 0;
}
static int merge_env_file_push(
const char *filename, unsigned line,
const char *key, char *value,
void *userdata,
int *n_pushed) {
void *userdata) {
char ***env = userdata;
char *expanded_value;
@@ -497,7 +470,7 @@ static int merge_env_file_push(
log_debug("%s:%u: setting %s=%s", filename, line, key, value);
return load_env_file_push(filename, line, key, value, env, n_pushed);
return load_env_file_push(filename, line, key, value, env);
}
int merge_env_file(
@@ -509,7 +482,7 @@ int merge_env_file(
* plus "extended" substitutions, unlike other exported parsing functions.
*/
return parse_env_file_internal(f, fname, merge_env_file_push, env, NULL);
return parse_env_file_internal(f, fname, merge_env_file_push, env);
}
static void write_env_var(FILE *f, const char *v) {
+40 -30
View File
@@ -508,7 +508,7 @@ static int get_paths_from_environ(const char *var, char ***paths, bool *append)
}
int lookup_paths_init(
LookupPaths *p,
LookupPaths *lp,
UnitFileScope scope,
LookupPathsFlags flags,
const char *root_dir) {
@@ -526,7 +526,7 @@ int lookup_paths_init(
_cleanup_strv_free_ char **paths = NULL;
int r;
assert(p);
assert(lp);
assert(scope >= 0);
assert(scope < _UNIT_FILE_SCOPE_MAX);
@@ -716,7 +716,7 @@ int lookup_paths_init(
if (r < 0)
return -ENOMEM;
*p = (LookupPaths) {
*lp = (LookupPaths) {
.search_path = strv_uniq(TAKE_PTR(paths)),
.persistent_config = TAKE_PTR(persistent_config),
@@ -741,41 +741,51 @@ int lookup_paths_init(
return 0;
}
void lookup_paths_free(LookupPaths *p) {
if (!p)
return;
int lookup_paths_init_or_warn(LookupPaths *lp, UnitFileScope scope, LookupPathsFlags flags, const char *root_dir) {
int r;
p->search_path = strv_free(p->search_path);
p->persistent_config = mfree(p->persistent_config);
p->runtime_config = mfree(p->runtime_config);
p->persistent_attached = mfree(p->persistent_attached);
p->runtime_attached = mfree(p->runtime_attached);
p->generator = mfree(p->generator);
p->generator_early = mfree(p->generator_early);
p->generator_late = mfree(p->generator_late);
p->transient = mfree(p->transient);
p->persistent_control = mfree(p->persistent_control);
p->runtime_control = mfree(p->runtime_control);
p->root_dir = mfree(p->root_dir);
p->temporary_dir = mfree(p->temporary_dir);
r = lookup_paths_init(lp, scope, flags, root_dir);
if (r < 0)
return log_error_errno(r, "Failed to initialize unit search paths%s%s: %m",
isempty(root_dir) ? "" : " for root directory ", strempty(root_dir));
return r;
}
void lookup_paths_log(LookupPaths *p) {
assert(p);
void lookup_paths_free(LookupPaths *lp) {
if (!lp)
return;
if (strv_isempty(p->search_path)) {
lp->search_path = strv_free(lp->search_path);
lp->persistent_config = mfree(lp->persistent_config);
lp->runtime_config = mfree(lp->runtime_config);
lp->persistent_attached = mfree(lp->persistent_attached);
lp->runtime_attached = mfree(lp->runtime_attached);
lp->generator = mfree(lp->generator);
lp->generator_early = mfree(lp->generator_early);
lp->generator_late = mfree(lp->generator_late);
lp->transient = mfree(lp->transient);
lp->persistent_control = mfree(lp->persistent_control);
lp->runtime_control = mfree(lp->runtime_control);
lp->root_dir = mfree(lp->root_dir);
lp->temporary_dir = mfree(lp->temporary_dir);
}
void lookup_paths_log(LookupPaths *lp) {
assert(lp);
if (strv_isempty(lp->search_path)) {
log_debug("Ignoring unit files.");
p->search_path = strv_free(p->search_path);
lp->search_path = strv_free(lp->search_path);
} else {
_cleanup_free_ char *t = NULL;
t = strv_join(p->search_path, "\n\t");
t = strv_join(lp->search_path, "\n\t");
log_debug("Looking for unit files in (higher priority first):\n\t%s", strna(t));
}
}
+2 -1
View File
@@ -54,7 +54,8 @@ struct LookupPaths {
char *temporary_dir;
};
int lookup_paths_init(LookupPaths *p, UnitFileScope scope, LookupPathsFlags flags, const char *root_dir);
int lookup_paths_init(LookupPaths *lp, UnitFileScope scope, LookupPathsFlags flags, const char *root_dir);
int lookup_paths_init_or_warn(LookupPaths *lp, UnitFileScope scope, LookupPathsFlags flags, const char *root_dir);
int xdg_user_dirs(char ***ret_config_dirs, char ***ret_data_dirs);
int xdg_user_runtime_dir(char **ret, const char *suffix);