From 3cb9b42af3b205fba176ebf51ce0e07739698278 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Fri, 6 Jul 2018 11:57:54 +0200 Subject: [PATCH] Move module-util.h to src/shared/ and load_module() to libshared Unfortunately this needs libshared to link to libkmod. Before it was linked into systemd-udevd, udevadm, and systemd each seperately. On most systems this doesn't make much difference, because at least systemd would be installed, but it might not be in small chroots. It is a small library, so I hope this is not a big issue. --- src/basic/meson.build | 1 - src/modules-load/modules-load.c | 63 +--------------------------- src/shared/meson.build | 3 ++ src/shared/module-util.c | 64 +++++++++++++++++++++++++++++ src/{basic => shared}/module-util.h | 2 + 5 files changed, 71 insertions(+), 62 deletions(-) create mode 100644 src/shared/module-util.c rename src/{basic => shared}/module-util.h (81%) diff --git a/src/basic/meson.build b/src/basic/meson.build index 31625b1785..2fa2681b47 100644 --- a/src/basic/meson.build +++ b/src/basic/meson.build @@ -121,7 +121,6 @@ basic_sources = files(''' mkdir-label.c mkdir.c mkdir.h - module-util.h mount-util.c mount-util.h nss-util.h diff --git a/src/modules-load/modules-load.c b/src/modules-load/modules-load.c index c49a6b7a76..b3a4e818b6 100644 --- a/src/modules-load/modules-load.c +++ b/src/modules-load/modules-load.c @@ -59,65 +59,6 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat return 0; } -static int load_module(struct kmod_ctx *ctx, const char *m) { - const int probe_flags = KMOD_PROBE_APPLY_BLACKLIST; - struct kmod_list *itr; - _cleanup_(kmod_module_unref_listp) struct kmod_list *modlist = NULL; - int r = 0; - - log_debug("load: %s", m); - - r = kmod_module_new_from_lookup(ctx, m, &modlist); - if (r < 0) - return log_error_errno(r, "Failed to lookup alias '%s': %m", m); - - if (!modlist) { - log_error("Failed to find module '%s'", m); - return -ENOENT; - } - - kmod_list_foreach(itr, modlist) { - _cleanup_(kmod_module_unrefp) struct kmod_module *mod = NULL; - int state, err; - - mod = kmod_module_get_module(itr); - state = kmod_module_get_initstate(mod); - - switch (state) { - case KMOD_MODULE_BUILTIN: - log_info("Module '%s' is builtin", kmod_module_get_name(mod)); - break; - - case KMOD_MODULE_LIVE: - log_debug("Module '%s' is already loaded", kmod_module_get_name(mod)); - break; - - default: - err = kmod_module_probe_insert_module(mod, probe_flags, - NULL, NULL, NULL, NULL); - - if (err == 0) - log_info("Inserted module '%s'", kmod_module_get_name(mod)); - else if (err == KMOD_PROBE_APPLY_BLACKLIST) - log_info("Module '%s' is blacklisted", kmod_module_get_name(mod)); - else { - assert(err < 0); - - log_full_errno(err == ENODEV ? LOG_NOTICE : - err == ENOENT ? LOG_WARNING : - LOG_ERR, - err, - "Failed to insert '%s': %m", - kmod_module_get_name(mod)); - if (!IN_SET(err, ENODEV, ENOENT)) - r = err; - } - } - } - - return r; -} - static int apply_file(struct kmod_ctx *ctx, const char *path, bool ignore_enoent) { _cleanup_fclose_ FILE *f = NULL; int r; @@ -151,7 +92,7 @@ static int apply_file(struct kmod_ctx *ctx, const char *path, bool ignore_enoent if (strchr(COMMENTS "\n", *l)) continue; - k = load_module(ctx, l); + k = module_load_and_warn(ctx, l); if (k < 0 && r == 0) r = k; } @@ -248,7 +189,7 @@ int main(int argc, char *argv[]) { char **fn, **i; STRV_FOREACH(i, arg_proc_cmdline_modules) { - k = load_module(ctx, *i); + k = module_load_and_warn(ctx, *i); if (k < 0 && r == 0) r = k; } diff --git a/src/shared/meson.build b/src/shared/meson.build index 54e77e9af6..9c80f2b855 100644 --- a/src/shared/meson.build +++ b/src/shared/meson.build @@ -63,6 +63,8 @@ shared_sources = files(''' machine-image.h machine-pool.c machine-pool.h + module-util.h + module-util.c nsflags.c nsflags.h output-mode.c @@ -132,6 +134,7 @@ libshared_deps = [threads, libcryptsetup, libgcrypt, libiptc, + libkmod, libseccomp, libselinux, libidn, diff --git a/src/shared/module-util.c b/src/shared/module-util.c new file mode 100644 index 0000000000..36f4f364c1 --- /dev/null +++ b/src/shared/module-util.c @@ -0,0 +1,64 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ + +#include + +#include "module-util.h" + +int module_load_and_warn(struct kmod_ctx *ctx, const char *module) { + const int probe_flags = KMOD_PROBE_APPLY_BLACKLIST; + struct kmod_list *itr; + _cleanup_(kmod_module_unref_listp) struct kmod_list *modlist = NULL; + int r = 0; + + log_debug("Loading module: %s", module); + + r = kmod_module_new_from_lookup(ctx, module, &modlist); + if (r < 0) + return log_error_errno(r, "Failed to lookup module alias '%s': %m", module); + + if (!modlist) { + log_error("Failed to find module '%s'", module); + return -ENOENT; + } + + kmod_list_foreach(itr, modlist) { + _cleanup_(kmod_module_unrefp) struct kmod_module *mod = NULL; + int state, err; + + mod = kmod_module_get_module(itr); + state = kmod_module_get_initstate(mod); + + switch (state) { + case KMOD_MODULE_BUILTIN: + log_info("Module '%s' is builtin", kmod_module_get_name(mod)); + break; + + case KMOD_MODULE_LIVE: + log_debug("Module '%s' is already loaded", kmod_module_get_name(mod)); + break; + + default: + err = kmod_module_probe_insert_module(mod, probe_flags, + NULL, NULL, NULL, NULL); + + if (err == 0) + log_info("Inserted module '%s'", kmod_module_get_name(mod)); + else if (err == KMOD_PROBE_APPLY_BLACKLIST) + log_info("Module '%s' is blacklisted", kmod_module_get_name(mod)); + else { + assert(err < 0); + + log_full_errno(err == ENODEV ? LOG_NOTICE : + err == ENOENT ? LOG_WARNING : + LOG_ERR, + err, + "Failed to insert module '%s': %m", + kmod_module_get_name(mod)); + if (!IN_SET(err, ENODEV, ENOENT)) + r = err; + } + } + } + + return r; +} diff --git a/src/basic/module-util.h b/src/shared/module-util.h similarity index 81% rename from src/basic/module-util.h rename to src/shared/module-util.h index 8fa121ed98..16cac90258 100644 --- a/src/basic/module-util.h +++ b/src/shared/module-util.h @@ -8,3 +8,5 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(struct kmod_ctx*, kmod_unref); DEFINE_TRIVIAL_CLEANUP_FUNC(struct kmod_module*, kmod_module_unref); DEFINE_TRIVIAL_CLEANUP_FUNC(struct kmod_list*, kmod_module_unref_list); + +int module_load_and_warn(struct kmod_ctx *ctx, const char *module); -- 2.25.1