1 diff --git a/Makefile.am b/Makefile.am
2 index d4eeb7e..5c9f603 100644
5 @@ -19,6 +19,7 @@ AM_CPPFLAGS = \
6 -include $(top_builddir)/config.h \
8 -DSYSCONFDIR=\""$(sysconfdir)"\" \
9 + -DMODULESDIRS=\""$(shell echo $(modulesdirs) | $(SED) 's|:|\\",\\"|g')"\" \
12 AM_CFLAGS = $(OUR_CFLAGS)
13 diff --git a/configure.ac b/configure.ac
14 index 23510c8..66490cf 100644
17 @@ -202,6 +202,12 @@ GTK_DOC_CHECK([1.14],[--flavour no-tmpl-flat])
19 AM_CONDITIONAL([ENABLE_GTK_DOC], false)])
21 +AC_ARG_WITH([modulesdirs],
22 + AS_HELP_STRING([--with-modulesdirs=DIRS], [Kernel modules directories, separated by :]),
24 + [with_modulesdirs=/lib/modules])
25 +AC_SUBST([modulesdirs], [$with_modulesdirs])
28 #####################################################################
29 # Default CFLAGS and LDFLAGS
30 diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c
31 index 69fe431..d37da32 100644
32 --- a/libkmod/libkmod.c
33 +++ b/libkmod/libkmod.c
34 @@ -206,12 +206,15 @@ static int log_priority(const char *priority)
38 -static const char *dirname_default_prefix = "/lib/modules";
39 +static const char *dirname_default_prefixes[] = {
44 static char *get_kernel_release(const char *dirname)
48 + char *p, *dirname_prefix;
51 return path_make_absolute_cwd(dirname);
52 @@ -219,8 +222,42 @@ static char *get_kernel_release(const char *dirname)
56 - if (asprintf(&p, "%s/%s", dirname_default_prefix, u.release) < 0)
58 + if ((dirname_prefix = getenv("MODULE_DIR")) != NULL) {
59 + if(asprintf(&p, "%s/%s", dirname_prefix, u.release) < 0)
65 + for (i = 0; dirname_default_prefixes[i] != NULL; i++) {
67 + struct stat dirstat;
69 + plen = snprintf(buf, sizeof(buf), "%s/%s", dirname_default_prefixes[i], u.release);
72 + else if (plen >= PATH_MAX)
75 + if (dirname_default_prefixes[i + 1] != NULL) {
76 + if (stat(buf, &dirstat) < 0) {
77 + if (errno == ENOENT)
83 + if (!S_ISDIR(dirstat.st_mode))
87 + p = malloc(plen + 1);
90 + memcpy(p, buf, plen + 1);
97 diff --git a/tools/static-nodes.c b/tools/static-nodes.c
98 index 8d2356d..2ed306d 100644
99 --- a/tools/static-nodes.c
100 +++ b/tools/static-nodes.c
103 #include <sys/stat.h>
104 #include <sys/types.h>
105 -#include <sys/utsname.h>
107 #include <shared/util.h>
109 +#include <libkmod/libkmod.h>
113 struct static_nodes_format {
114 @@ -154,8 +155,8 @@ static void help(void)
116 static int do_static_nodes(int argc, char *argv[])
118 - struct utsname kernel;
119 char modules[PATH_MAX], buf[4096];
120 + struct kmod_ctx *ctx;
121 const char *output = "/dev/stdout";
122 FILE *in = NULL, *out = NULL;
123 const struct static_nodes_format *format = &static_nodes_format_human;
124 @@ -206,22 +207,25 @@ static int do_static_nodes(int argc, char *argv[])
128 - if (uname(&kernel) < 0) {
129 - fputs("Error: uname failed!\n", stderr);
130 + ctx = kmod_new(NULL, NULL);
132 + fprintf(stderr, "Error: failed to create kmod context\n");
137 - snprintf(modules, sizeof(modules), "/lib/modules/%s/modules.devname", kernel.release);
138 + if (snprintf(modules, sizeof(modules), "%s/modules.devname", kmod_get_dirname(ctx)) < 0) {
139 + fprintf(stderr, "Error: path to modules.devname is too long\n");
140 + ret = EXIT_FAILURE;
144 in = fopen(modules, "re");
146 if (errno == ENOENT) {
147 - fprintf(stderr, "Warning: /lib/modules/%s/modules.devname not found - ignoring\n",
149 + fprintf(stderr, "Warning: %s not found - ignoring\n", modules);
152 - fprintf(stderr, "Error: could not open /lib/modules/%s/modules.devname - %m\n",
154 + fprintf(stderr, "Error: could not open %s - %m\n", modules);