]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Move module-util.h to src/shared/ and load_module() to libshared
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 6 Jul 2018 09:57:54 +0000 (11:57 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 16 Jul 2018 11:08:40 +0000 (13:08 +0200)
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
src/modules-load/modules-load.c
src/shared/meson.build
src/shared/module-util.c [new file with mode: 0644]
src/shared/module-util.h [moved from src/basic/module-util.h with 81% similarity]

index 31625b1785dbf588fe3669a3059e5f2ac82029ec..2fa2681b477dabb4dfce2f23f6b5382722695332 100644 (file)
@@ -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
index c49a6b7a76e484d04f0487f788c412ff36b027b9..b3a4e818b608feb4a9cde9317807f16520e13e6c 100644 (file)
@@ -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;
                 }
index 54e77e9af639b1be5d29da153379deec0b2e8a4c..9c80f2b85533f0008298a17a29570cdd2a4cc3c0 100644 (file)
@@ -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 (file)
index 0000000..36f4f36
--- /dev/null
@@ -0,0 +1,64 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
+
+#include <errno.h>
+
+#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;
+}
similarity index 81%
rename from src/basic/module-util.h
rename to src/shared/module-util.h
index 8fa121ed986e7a03c8f875137c45fabecf416642..16cac90258f54bfa802d17618b0e129cc4b696bf 100644 (file)
@@ -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);