Merge pull request #32710 from YHNdnzj/debug-generator-cleanup

debug-generator: several cleanups
This commit is contained in:
Daan De Meyer 2024-05-08 14:09:56 +02:00 committed by GitHub
commit 1b35ea0cdf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 33 additions and 31 deletions

View file

@ -49,8 +49,7 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
if (r < 0)
return log_error_errno(r, "Failed to glob unit name: %m");
r = strv_consume(&arg_mask, n);
if (r < 0)
if (strv_consume(&arg_mask, n) < 0)
return log_oom();
} else if (streq(key, "systemd.wants")) {
@ -63,11 +62,11 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
if (r < 0)
return log_error_errno(r, "Failed to glob unit name: %m");
r = strv_consume(&arg_wants, n);
if (r < 0)
if (strv_consume(&arg_wants, n) < 0)
return log_oom();
} else if (proc_cmdline_key_streq(key, "systemd.debug_shell")) {
r = value ? parse_boolean(value) : 1;
arg_debug_shell = r != 0;
if (r >= 0)
@ -76,6 +75,7 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
return free_and_strdup_warn(&arg_debug_tty, skip_dev_prefix(value));
} else if (proc_cmdline_key_streq(key, "systemd.default_debug_tty")) {
if (proc_cmdline_value_missing(key, value))
return 0;
@ -105,14 +105,12 @@ static int generate_mask_symlinks(void) {
STRV_FOREACH(u, arg_mask) {
_cleanup_free_ char *p = NULL;
p = path_join(empty_to_root(arg_dest), *u);
p = path_join(arg_dest, *u);
if (!p)
return log_oom();
if (symlink("/dev/null", p) < 0)
r = log_error_errno(errno,
"Failed to create mask symlink %s: %m",
p);
RET_GATHER(r, log_error_errno(errno, "Failed to create mask symlink '%s': %m", p));
}
return r;
@ -137,44 +135,44 @@ static int generate_wants_symlinks(void) {
if (!f)
return log_oom();
r = generator_add_symlink(arg_dest, target, "wants", f);
if (r < 0)
return r;
RET_GATHER(r, generator_add_symlink(arg_dest, target, "wants", f));
}
return r;
}
static void install_debug_shell_dropin(void) {
static int install_debug_shell_dropin(void) {
const char *tty = arg_debug_tty ?: arg_default_debug_tty;
int r;
if (!tty || path_equal(tty, skip_dev_prefix(DEBUGTTY)))
return;
return 0;
r = write_drop_in_format(arg_dest, "debug-shell.service", 50, "tty",
"[Unit]\n"
"Description=Early root shell on /dev/%s FOR DEBUGGING ONLY\n"
"ConditionPathExists=\n"
"[Service]\n"
"TTYPath=/dev/%s",
tty, tty);
"# Automatically generated by systemd-debug-generator\n\n"
"[Unit]\n"
"Description=Early root shell on /dev/%s FOR DEBUGGING ONLY\n"
"ConditionPathExists=\n"
"\n[Service]\n"
"TTYPath=/dev/%s\n",
tty, tty);
if (r < 0)
log_warning_errno(r, "Failed to write drop-in for debug-shell.service, ignoring: %m");
return log_warning_errno(r, "Failed to write drop-in for debug-shell.service: %m");
return 1;
}
static int process_unit_credentials(const char *credentials_dir) {
_cleanup_free_ DirectoryEntries *des = NULL;
int r;
assert(credentials_dir);
_cleanup_free_ DirectoryEntries *des = NULL;
r = readdir_all_at(AT_FDCWD, credentials_dir, RECURSE_DIR_SORT|RECURSE_DIR_IGNORE_DOT|RECURSE_DIR_ENSURE_TYPE, &des);
if (r < 0)
return log_error_errno(r, "Failed to enumerate credentials from credentials directory '%s': %m", credentials_dir);
FOREACH_ARRAY(i, des->entries, des->n_entries) {
_cleanup_free_ void *d = NULL;
struct dirent *de = *i;
const char *unit, *dropin;
@ -193,9 +191,13 @@ static int process_unit_credentials(const char *credentials_dir) {
continue;
}
r = read_credential_with_decryption(de->d_name, &d, NULL);
if (r < 0)
_cleanup_free_ char *d = NULL;
r = read_credential_with_decryption(de->d_name, (void**) &d, NULL);
if (r < 0) {
log_warning_errno(r, "Failed to read credential '%s', ignoring: %m", de->d_name);
continue;
}
if (unit) {
_cleanup_free_ char *p = NULL;
@ -213,7 +215,7 @@ static int process_unit_credentials(const char *credentials_dir) {
log_debug("Wrote unit file '%s' from credential '%s'", unit, de->d_name);
} else {
} else if (dropin) {
r = write_drop_in(arg_dest, dropin, 50, "credential", d);
if (r < 0) {
log_warning_errno(r, "Failed to write drop-in for unit '%s' from credential '%s', ignoring: %m",
@ -222,7 +224,8 @@ static int process_unit_credentials(const char *credentials_dir) {
}
log_debug("Wrote drop-in for unit '%s' from credential '%s'", dropin, de->d_name);
}
} else
assert_not_reached();
}
return 0;
@ -230,7 +233,7 @@ static int process_unit_credentials(const char *credentials_dir) {
static int run(const char *dest, const char *dest_early, const char *dest_late) {
const char *credentials_dir;
int r = 0;
int r;
assert_se(arg_dest = dest_early);
@ -239,11 +242,10 @@ static int run(const char *dest, const char *dest_early, const char *dest_late)
log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
if (arg_debug_shell) {
r = strv_extend(&arg_wants, "debug-shell.service");
if (r < 0)
if (strv_extend(&arg_wants, "debug-shell.service") < 0)
return log_oom();
install_debug_shell_dropin();
RET_GATHER(r, install_debug_shell_dropin());
}
if (get_credentials_dir(&credentials_dir) >= 0)

View file

@ -400,7 +400,7 @@ static int parse_credentials(void) {
size_t sz = 0;
int r;
r = read_credential_with_decryption("ssh.listen", (void*) &b, &sz);
r = read_credential_with_decryption("ssh.listen", (void**) &b, &sz);
if (r <= 0)
return r;