From 5058bd7e1f0ae226d835253fcf333da3ab8d2806 Mon Sep 17 00:00:00 2001 From: Ludwig Nussel Date: Tue, 9 Jan 2024 17:31:01 +0100 Subject: [PATCH 1/3] strv: introduce strv_copy_unless_empty() --- src/basic/strv.c | 16 ++++++++++++++++ src/basic/strv.h | 2 ++ 2 files changed, 18 insertions(+) diff --git a/src/basic/strv.c b/src/basic/strv.c index 908e9e2513..fa17631aa0 100644 --- a/src/basic/strv.c +++ b/src/basic/strv.c @@ -123,6 +123,22 @@ char** strv_copy_n(char * const *l, size_t m) { return TAKE_PTR(result); } +int strv_copy_unless_empty(char * const *l, char ***ret) { + assert(ret); + + if (strv_isempty(l)) { + *ret = NULL; + return 0; + } + + char **copy = strv_copy(l); + if (!copy) + return -ENOMEM; + + *ret = TAKE_PTR(copy); + return 1; +} + size_t strv_length(char * const *l) { size_t n = 0; diff --git a/src/basic/strv.h b/src/basic/strv.h index f1a8bc4910..7361faed84 100644 --- a/src/basic/strv.h +++ b/src/basic/strv.h @@ -38,6 +38,8 @@ char** strv_copy_n(char * const *l, size_t n); static inline char** strv_copy(char * const *l) { return strv_copy_n(l, SIZE_MAX); } +int strv_copy_unless_empty(char * const *l, char ***ret); + size_t strv_length(char * const *l) _pure_; int strv_extend_strv(char ***a, char * const *b, bool filter_duplicates); From 27d420f46645ed584bdd66857eabc25f8c0118bb Mon Sep 17 00:00:00 2001 From: Ludwig Nussel Date: Tue, 9 Jan 2024 12:29:36 +0100 Subject: [PATCH 2/3] kernel-install: fix context_copy Don't reopen or dup values that weren't set before. Fixes add-all. --- src/kernel-install/kernel-install.c | 35 ++++++++++++++++------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/kernel-install/kernel-install.c b/src/kernel-install/kernel-install.c index a93b4e688f..882388b345 100644 --- a/src/kernel-install/kernel-install.c +++ b/src/kernel-install/kernel-install.c @@ -133,9 +133,10 @@ static int context_copy(const Context *source, Context *ret) { assert(source); assert(ret); + assert(source->rfd >= 0 || source->rfd == AT_FDCWD); _cleanup_(context_done) Context copy = (Context) { - .rfd = -EBADF, + .rfd = AT_FDCWD, .action = source->action, .machine_id = source->machine_id, .machine_id_is_random = source->machine_id_is_random, @@ -144,9 +145,11 @@ static int context_copy(const Context *source, Context *ret) { .entry_token_type = source->entry_token_type, }; - copy.rfd = fd_reopen(source->rfd, O_CLOEXEC|O_DIRECTORY|O_PATH); - if (copy.rfd < 0) - return copy.rfd; + if (source->rfd >= 0) { + copy.rfd = fd_reopen(source->rfd, O_CLOEXEC|O_DIRECTORY|O_PATH); + if (copy.rfd < 0) + return copy.rfd; + } r = strdup_or_null(source->layout_other, ©.layout_other); if (r < 0) @@ -169,9 +172,9 @@ static int context_copy(const Context *source, Context *ret) { r = strdup_or_null(source->kernel, ©.kernel); if (r < 0) return r; - copy.initrds = strv_copy(source->initrds); - if (!copy.initrds) - return -ENOMEM; + r = strv_copy_unless_empty(source->initrds, ©.initrds); + if (r < 0) + return r; r = strdup_or_null(source->initrd_generator, ©.initrd_generator); if (r < 0) return r; @@ -181,15 +184,15 @@ static int context_copy(const Context *source, Context *ret) { r = strdup_or_null(source->staging_area, ©.staging_area); if (r < 0) return r; - copy.plugins = strv_copy(source->plugins); - if (!copy.plugins) - return -ENOMEM; - copy.argv = strv_copy(source->argv); - if (!copy.argv) - return -ENOMEM; - copy.envp = strv_copy(source->envp); - if (!copy.envp) - return -ENOMEM; + r = strv_copy_unless_empty(source->plugins, ©.plugins); + if (r < 0) + return r; + r = strv_copy_unless_empty(source->argv, ©.argv); + if (r < 0) + return r; + r = strv_copy_unless_empty(source->envp, ©.envp); + if (r < 0) + return r; *ret = copy; copy = CONTEXT_NULL; From ec9ff6ea94417f83f45f563ae5375c0e090e6fb5 Mon Sep 17 00:00:00 2001 From: Ludwig Nussel Date: Tue, 9 Jan 2024 12:49:09 +0100 Subject: [PATCH 3/3] kernel-install: silence num kernels installed --- src/kernel-install/kernel-install.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/kernel-install/kernel-install.c b/src/kernel-install/kernel-install.c index 882388b345..2651ece294 100644 --- a/src/kernel-install/kernel-install.c +++ b/src/kernel-install/kernel-install.c @@ -1287,7 +1287,7 @@ static int verb_add_all(int argc, char *argv[], void *userdata) { } if (n > 0) - log_info("Installed %zu kernels.", n); + log_debug("Installed %zu kernel(s).", n); else if (ret == 0) ret = log_error_errno(SYNTHETIC_ERRNO(ENOENT), "No kernels to install found.");