Request for work/inclusion: sysmodules

Ryan Finnie ryan at finnie.org
Sun Dec 2 16:41:04 UTC 2012


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Hello,

Below is a patch to add a modutils applet called sysmodules.  It scans
/sys/devices and compares it to entries in modules.alias, returning a
list of modules that could be loaded on the system.  This is similar to
the old pciutils program pcimodules, except 1) it works on all /sys
devices, not just PCI devices, and 2) compares it to modules.alias as
opposed to (the now removed from modern depmod) modules.pcimap.

It was originally written by a friend of mine, and I added the BusyBox
applet glue.  It works for my needs, and I would like to see it added to
BusyBox, but it needs some finishing work, and I Am Not A C Programmer
when it comes down to it.  Namely, right now you must pipe modules.alias
to it.  I would much rather it figure out `uname -r` and build the path
to it or accept a command line flag to specify modules.alias manually,
have it test against invalid names, etc.

Would someone be willing to help finish this up?

Thanks,
RF


diff --git a/modutils/Config.src b/modutils/Config.src
index 449ac65..8acabd3 100644
- --- a/modutils/Config.src
+++ b/modutils/Config.src
@@ -90,6 +90,13 @@ config FEATURE_LSMOD_PRETTY_2_6_OUTPUT
 	  the format of module-init-tools for Linux kernel 2.6.
 	  Increases size somewhat.
 
+config SYSMODULES
+	bool "sysmodules"
+	default n
+	select PLATFORM_LINUX
+	help
+	  sysmodules compares sysfs devices to modaliases.
+
 config MODPROBE
 	bool "modprobe"
 	default n
diff --git a/modutils/Kbuild.src b/modutils/Kbuild.src
index 1a7ac87..5ef7bbf 100644
- --- a/modutils/Kbuild.src
+++ b/modutils/Kbuild.src
@@ -11,6 +11,7 @@ lib-$(CONFIG_MODPROBE_SMALL)      += modprobe-small.o
 lib-$(CONFIG_DEPMOD)              += depmod.o modutils.o
 lib-$(CONFIG_INSMOD)              += insmod.o modutils.o
 lib-$(CONFIG_LSMOD)               += lsmod.o modutils.o
+lib-$(CONFIG_SYSMODULES)          += sysmodules.o
 lib-$(CONFIG_MODPROBE)            += modprobe.o modutils.o
 lib-$(CONFIG_RMMOD)               += rmmod.o modutils.o
 lib-$(CONFIG_FEATURE_2_4_MODULES) += modutils-24.o
diff --git a/modutils/sysmodules.c b/modutils/sysmodules.c
new file mode 100644
index 0000000..15c0177
- --- /dev/null
+++ b/modutils/sysmodules.c
@@ -0,0 +1,153 @@
+/*
+ * sysmodules
+ *
+ * This software determines which modules can be loaded based on a copy
+ * of modules.alias, comparing to sysfs devies that publish a modalias.
+ *
+ * Usage: sysmodules < /lib/modules/`uname -r`/modules.alias
+ *
+ * This software contains code originally written by Neale Pickett, and
+ * included the following disclaimer of copyright:
+ *
+ *   This software has been authored by an employee or employees of Los
+ *   Alamos National Security, LLC, operator of the Los Alamos National
+ *   Laboratory (LANL) under Contract No. DE-AC52-06NA25396 with the
+ *   U.S.  Department of Energy.  The U.S. Government has rights to use,
+ *   reproduce, and distribute this software.  The public may copy,
+ *   distribute, prepare derivative works and publicly display this
+ *   software without charge, provided that this Notice and any
+ *   statement of authorship are reproduced on all copies.  Neither the
+ *   Government nor LANS makes any warranty, express or implied, or
+ *   assumes any liability or responsibility for the use of this
+ *   software.  If software is modified to produce derivative works,
+ *   such modified software should be clearly marked, so as not to
+ *   confuse it with the version available from LANL.
+ */
+
+//applet:IF_SYSMODULES(APPLET(sysmodules, BB_DIR_SBIN, BB_SUID_DROP))
+
+//usage:#define sysmodules_trivial_usage
+//usage:       ""
+//usage:#define sysmodules_full_usage "\n\n"
+//usage:       "Compares sysfs devices to modaliases"
+
+#include "libbb.h"
+#include "sysmodules.h"
+
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <dirent.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fnmatch.h>
+
+#define LISTLEN 1024
+
+char *mas[LISTLEN];
+int malen = 0;
+
+void sysmodules_cat(char *path)
+{
+    FILE *f = fopen(path, "r");
+    char line[512];
+
+    if (! f) {
+        return;
+    }
+    while (NULL != fgets(line, sizeof line, f)) {
+        char *p;
+
+        if (malen == LISTLEN) {
+            abort();
+        }
+        for (p = line; *p && (*p != '\n'); p += 1);
+        *p = 0;
+
+        mas[malen++] = strdup(line);
+    }
+    fclose(f);
+}
+
+void sysmodules_find(char *path)
+{
+    struct dirent *e;
+    DIR *d = opendir(path);
+
+    while ((e = readdir(d))) {
+        char fn[PATH_MAX];
+        struct stat st;
+
+        if (e->d_name[0] == '.') {
+            continue;
+        }
+
+        snprintf(fn, sizeof fn, "%s/%s", path, e->d_name);
+        if (lstat(fn, &st)) {
+            continue;
+        }
+
+        if (S_ISLNK(st.st_mode)) {
+            continue;
+        } else if (S_ISDIR(st.st_mode)) {
+            sysmodules_find(fn);
+        } else if (0 == strcmp(e->d_name, "modalias")) {
+            sysmodules_cat(fn);
+        }
+    }
+    closedir(d);
+}
+
+
+char *written[LISTLEN] = {0};
+void sysmodules_uwrite(char *s)
+{
+    int i;
+
+    for (i = 0; written[i]; i += 1) {
+        if (0 == strcmp(written[i], s)) {
+            return;
+        }
+    }
+    written[i++] = strdup(s);
+    printf("%s", s);
+}
+
+const char *syspath = "/sys/devices";
+int sysmodules_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int sysmodules_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
+{
+    char l[512];
+
+    sysmodules_find((char *)syspath);
+
+    while (NULL != fgets(l, sizeof l, stdin)) {
+        int i;
+        char *matcher;
+        char *module;
+        char *p;
+
+        if (0 != strncmp(l, "alias ", 6)) {
+            continue;
+        }
+
+        matcher = l + 6;
+        for (p = matcher; *p; p += 1) {
+            if (*p == ' ') {
+                *p++ = 0;
+                break;
+            }
+        }
+
+        module = p;
+
+        for (i = 0; i < malen; i += 1) {
+            if (0 == fnmatch(matcher, mas[i], FNM_NOESCAPE)) {
+                sysmodules_uwrite(module);
+            }
+        }
+    }
+
+    return 0;
+}
diff --git a/modutils/sysmodules.h b/modutils/sysmodules.h
new file mode 100644
index 0000000..fdcb0c2
- --- /dev/null
+++ b/modutils/sysmodules.h
@@ -0,0 +1,3 @@
+void sysmodules_cat(char *path);
+void sysmodules_find(char *path);
+void sysmodules_uwrite(char *s);
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://www.enigmail.net/

iQIcBAEBCAAGBQJQu4SgAAoJEH5go6aGro2Ywk4P/0NmRr0F79zlDUnBtrfhc4nG
4fFH0eX/OBj4qvVX/WU/xbH/E3NaYWtlgMO1lxyaW954vmzUihOnYjCwcuzQJ0Zb
KubnCNX40QtE6Q+rL4uCD9vnk0M9XlHB4CeizadLtzFMaGMsDzAflgG7Su2+9dyW
2dDKz/nUeCLBlIi82si7QK1t+nCxA3+Cs6QuZDZlZkB8GOTCcdrSgJjtQ0Ntx9T5
lskerK6wnuP0UtdB6WAYAxfpq9csMYsjRAaqk57tjemhHd9Tpdyo7LXb7ZXS/00e
qZVv9II6ThusOsAGRXtW8j29PWzJ0ZEhz5FvkQmYEjC1y9YjGsVJUNxnB+if6DjT
HeGWGvN27uDjokfQ8XCUu+FHPiXCR+6sBVqoCmlXE1EOUD5bK9inEECgl3o9GRTc
3ZqQNMIUxeh/D69NCb+aczulh7LTA9N9Z62/Nn17iFVKNFyU3z+7mLoD7hsvgO4r
yg7DteC9n7kLeoDcXjwKbu7sAUWMZqEQrHypLRoczoTLLNuU3ucYNa1n0P4M39of
RKyWjU48MBcusEl3eaC4fGr+Zpk9tUvOGfUJNtf8aeoM60CjfGnKxQ6nZF5o8qAt
3GjBRCRE20eYiOpPQ2MHfHhS1fC3egsp2xNTab8ehmt+8gIM5koH4GjbSnDv1Igl
G2Vie+ye9VnGY+OQBRZZ
=jFp9
-----END PGP SIGNATURE-----


More information about the busybox mailing list