[systemd-commits] 2 commits - Makefile.am TODO configure.ac src/kmod-setup.c src/modules-load.c

Lennart Poettering lennart at kemper.freedesktop.org
Wed Feb 8 15:50:00 PST 2012


 Makefile.am        |   12 ++++++---
 TODO               |    2 -
 configure.ac       |    2 +
 src/kmod-setup.c   |   68 ++++++++++++++++++++++++++++++---------------------
 src/modules-load.c |   70 ++++++++++++++++++++++++-----------------------------
 5 files changed, 84 insertions(+), 70 deletions(-)

New commits:
commit 83684a3553cd875d6873b3dc867e6cd1a7cc4173
Author: Tom Gundersen <teg at jklm.no>
Date:   Wed Feb 8 21:52:19 2012 +0100

    modules-load: use libkmod rather than modprobe

diff --git a/Makefile.am b/Makefile.am
index 2856fab..fbf2358 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -862,8 +862,12 @@ systemd_shutdown_LDADD = \
 systemd_modules_load_SOURCES = \
 	src/modules-load.c
 
+systemd_modules_load_CFLAGS = \
+	$(KMOD_CFLAGS)
+
 systemd_modules_load_LDADD = \
-	libsystemd-basic.la
+	libsystemd-basic.la \
+	$(KMOD_LIBS)
 
 systemd_tmpfiles_SOURCES = \
 	src/tmpfiles.c
diff --git a/TODO b/TODO
index 7d22ea0..b1c9ceb 100644
--- a/TODO
+++ b/TODO
@@ -23,8 +23,6 @@ Features:
 
 * if a journal file is corrupt, rotate it and create a new one
 
-* Port systemd-load-modules to libkmod
-
 * dbus: in fedora, make the machine a symlink to /etc/machine-id
 
 * journald: reuse XZ context
diff --git a/src/modules-load.c b/src/modules-load.c
index 7384f25..3adbd36 100644
--- a/src/modules-load.c
+++ b/src/modules-load.c
@@ -26,16 +26,23 @@
 #include <sys/stat.h>
 #include <limits.h>
 #include <dirent.h>
+#include <libkmod.h>
 
 #include "log.h"
 #include "util.h"
 #include "strv.h"
 
+static void systemd_kmod_log(void *data, int priority, const char *file, int line,
+                             const char *fn, const char *format, va_list args)
+{
+        log_meta(priority, file, line, fn, format, args);
+}
+
 int main(int argc, char *argv[]) {
         int r = EXIT_FAILURE;
-        char **arguments = NULL;
-        unsigned n_arguments = 0, n_allocated = 0;
         char **files, **fn;
+        struct kmod_ctx *ctx;
+        struct kmod_module *mod;
 
         if (argc > 1) {
                 log_error("This program takes no argument.");
@@ -48,12 +55,14 @@ int main(int argc, char *argv[]) {
 
         umask(0022);
 
-        if (!(arguments = strv_new("/sbin/modprobe", "-sab", "--", NULL))) {
-                log_error("Failed to allocate string array");
+        if (!(ctx = kmod_new(NULL, NULL))) {
+                log_error("Failed to allocate memory for kmod.");
                 goto finish;
         }
 
-        n_arguments = n_allocated = 3;
+        kmod_load_resources(ctx);
+
+        kmod_set_log_fn(ctx, systemd_kmod_log, NULL);
 
         if (conf_files_list(&files, ".conf",
                             "/run/modules-load.d",
@@ -83,7 +92,8 @@ int main(int argc, char *argv[]) {
 
                 log_debug("apply: %s\n", *fn);
                 for (;;) {
-                        char line[LINE_MAX], *l, *t;
+                        char line[LINE_MAX], *l;
+                        int err;
 
                         if (!(fgets(line, sizeof(line), f)))
                                 break;
@@ -92,52 +102,38 @@ int main(int argc, char *argv[]) {
                         if (*l == '#' || *l == 0)
                                 continue;
 
-                        if (!(t = strdup(l))) {
-                                log_error("Failed to allocate module name.");
+                        err = kmod_module_new_from_name(ctx, l, &mod);
+                        if (err < 0) {
+                                log_error("Failed to load module '%s'", l);
+                                r = EXIT_FAILURE;
                                 continue;
                         }
 
-                        if (n_arguments >= n_allocated) {
-                                char **a;
-                                unsigned m;
-
-                                m = MAX(16U, n_arguments*2);
-
-                                if (!(a = realloc(arguments, sizeof(char*) * (m+1)))) {
-                                        log_error("Failed to increase module array size.");
-                                        free(t);
-                                        r = EXIT_FAILURE;
-                                        continue;
-                                }
-
-                                arguments = a;
-                                n_allocated = m;
+                        err = kmod_module_probe_insert_module(mod, KMOD_PROBE_APPLY_BLACKLIST,
+                                                              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 {
+                                log_error("Failed to insert '%s'", kmod_module_get_name(mod));
+                                r = EXIT_FAILURE;
                         }
 
-                        arguments[n_arguments++] = t;
+                        kmod_module_unref(mod);
                 }
 
                 if (ferror(f)) {
-                        r = EXIT_FAILURE;
                         log_error("Failed to read from file: %m");
+                        r = EXIT_FAILURE;
                 }
 
                 fclose(f);
         }
 
-        strv_free(files);
 finish:
-
-        if (n_arguments > 3) {
-                arguments[n_arguments] = NULL;
-                strv_uniq(arguments);
-                execv("/sbin/modprobe", arguments);
-
-                log_error("Failed to execute /sbin/modprobe: %m");
-                r = EXIT_FAILURE;
-        }
-
-        strv_free(arguments);
+        strv_free(files);
+        kmod_unref(ctx);
 
         return r;
 }

commit 728beb28a713709f521d374c9f8f3da781969d26
Author: Tom Gundersen <teg at jklm.no>
Date:   Wed Feb 8 21:52:18 2012 +0100

    kmod-setup: use libkmod rather than modprobe

diff --git a/Makefile.am b/Makefile.am
index e4e3510..2856fab 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -728,10 +728,12 @@ systemd_SOURCES = \
 systemd_CFLAGS = \
 	$(AM_CFLAGS) \
 	$(DBUS_CFLAGS) \
-	$(UDEV_CFLAGS)
+	$(UDEV_CFLAGS) \
+	$(KMOD_CFLAGS)
 
 systemd_LDADD = \
-	libsystemd-core.la
+	libsystemd-core.la \
+	$(KMOD_LIBS)
 
 test_engine_SOURCES = \
 	src/test-engine.c
diff --git a/TODO b/TODO
index c59a3e5..7d22ea0 100644
--- a/TODO
+++ b/TODO
@@ -23,7 +23,7 @@ Features:
 
 * if a journal file is corrupt, rotate it and create a new one
 
-* Port systemd-load-modules and setup-kmod.c to libkmod
+* Port systemd-load-modules to libkmod
 
 * dbus: in fedora, make the machine a symlink to /etc/machine-id
 
diff --git a/configure.ac b/configure.ac
index 2d04ee9..d540d95 100644
--- a/configure.ac
+++ b/configure.ac
@@ -130,6 +130,8 @@ PKG_CHECK_MODULES(DBUS, [ dbus-1 >= 1.3.2 ])
 AC_SUBST(DBUS_CFLAGS)
 AC_SUBST(DBUS_LIBS)
 
+PKG_CHECK_MODULES(KMOD, [ libkmod >= 5 ])
+
 have_selinux=no
 AC_ARG_ENABLE(selinux, AS_HELP_STRING([--disable-selinux], [Disable optional SELINUX support]))
 if test "x$enable_selinux" != "xno"; then
diff --git a/src/kmod-setup.c b/src/kmod-setup.c
index 7bd7dcb..7042de4 100644
--- a/src/kmod-setup.c
+++ b/src/kmod-setup.c
@@ -23,6 +23,7 @@
 #include <unistd.h>
 #include <string.h>
 #include <errno.h>
+#include <libkmod.h>
 
 #include "macro.h"
 #include "execute.h"
@@ -35,13 +36,18 @@ static const char * const kmod_table[] = {
         "unix",    "/proc/net/unix"
 };
 
+static void systemd_kmod_log(void *data, int priority, const char *file, int line,
+                             const char *fn, const char *format, va_list args)
+{
+        log_meta(priority, file, line, fn, format, args);
+}
+
+
 int kmod_setup(void) {
-        unsigned i, n = 0;
-        const char * cmdline[3 + ELEMENTSOF(kmod_table) + 1];
-        ExecCommand command;
-        ExecContext context;
-        pid_t pid;
-        int r;
+        unsigned i;
+        struct kmod_ctx *ctx = NULL;
+        struct kmod_module *mod;
+        int err;
 
         for (i = 0; i < ELEMENTSOF(kmod_table); i += 2) {
 
@@ -49,34 +55,40 @@ int kmod_setup(void) {
                         continue;
 
                 log_debug("Your kernel apparently lacks built-in %s support. Might be a good idea to compile it in. "
-                          "We'll now try to work around this by calling '/sbin/modprobe %s'...",
-                          kmod_table[i], kmod_table[i]);
+                          "We'll now try to work around this by loading the module...",
+                          kmod_table[i]);
 
-                cmdline[3 + n++] = kmod_table[i];
-        }
+                if (!ctx) {
+                        ctx = kmod_new(NULL, NULL);
+                        if (!ctx) {
+                                log_error("Failed to allocate memory for kmod");
+                                return -ENOMEM;
+                        }
 
-        if (n <= 0)
-                return 0;
+                        kmod_set_log_fn(ctx, systemd_kmod_log, NULL);
 
-        cmdline[0] = "/sbin/modprobe";
-        cmdline[1] = "-qab";
-        cmdline[2] = "--";
-        cmdline[3 + n] = NULL;
+                        kmod_load_resources(ctx);
+                }
 
-        zero(command);
-        zero(context);
-
-        command.path = (char*) cmdline[0];
-        command.argv = (char**) cmdline;
+                err = kmod_module_new_from_name(ctx, kmod_table[i], &mod);
+                if (err < 0) {
+                        log_error("Failed to load module '%s'", kmod_table[i]);
+                        continue;
+                }
 
-        exec_context_init(&context);
-        r = exec_spawn(&command, NULL, &context, NULL, 0, NULL, false, false, false, false, NULL, NULL, &pid);
-        exec_context_done(&context);
+                err = kmod_module_probe_insert_module(mod, KMOD_PROBE_APPLY_BLACKLIST, 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
+                        log_error("Failed to insert '%s'", kmod_module_get_name(mod));
 
-        if (r < 0) {
-                log_error("Failed to spawn %s: %s", cmdline[0], strerror(-r));
-                return r;
+                kmod_module_unref(mod);
         }
 
-        return wait_for_terminate_and_warn(cmdline[0], pid);
+        if (ctx)
+                ctx = kmod_unref(ctx);
+
+        return 0;
 }



More information about the systemd-commits mailing list