Skip to content

Commit

Permalink
Use libkmod only when requested
Browse files Browse the repository at this point in the history
We support module loading through modprobe when libkmod is neither
available nor wanted.

Signed-off-by: Richard Yao <ryao@gentoo.org>
  • Loading branch information
ryao committed Nov 23, 2012
1 parent 135d61d commit c4d1ce9
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 7 deletions.
29 changes: 28 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ if test -z "$GPERF" ; then
AC_MSG_ERROR([*** gperf not found])
fi

PKG_CHECK_MODULES(KMOD, [libkmod >= 5])
PKG_CHECK_MODULES(BLKID,[blkid >= 2.20])

# Checks for header files.
Expand Down Expand Up @@ -278,6 +277,34 @@ AC_SUBST([udevlibexecdir], [${with_rootlibdir}/udev])

# ------------------------------------------------------------------------------

AC_ARG_ENABLE([libkmod], [AS_HELP_STRING([--enable-libkmod], [Enable module loading through kmod @<:@default=disabled@:>@])], [], [with_libkmod=no])

LIBKMOD=
AS_IF([test "x$with_libkmod" != xno],
[AC_CHECK_LIB([kmod], [main],
[PKG_CHECK_MODULES(KMOD, [libkmod >= 5])
AC_SUBST([LIBKMOD], ["-lkmod"])
AC_DEFINE([HAVE_LIBKMOD], [1],
[Define if you have libkmod])
],
[AC_MSG_FAILURE(
[--with-libkmod was given, but test for kmod failed])],
[-lkmod])])

AM_CONDITIONAL([HAVE_LIBKMOD], [test "$with_kmod" = "yes"])

# ------------------------------------------------------------------------------

AC_ARG_WITH([modprobe],
[AS_HELP_STRING([--with-modprobe=modprobe],
[specify location of modprobe when -- @<:@default=$sbindir/modprobe@:>@])],
[],
[with_modprobe="${sbindir}/modprobe"])

AC_SUBST([MODPROBE], ["${with_modprobe}"])

# ------------------------------------------------------------------------------

AC_CONFIG_FILES([Makefile
docs/Makefile
docs/gudev/Makefile
Expand Down
5 changes: 4 additions & 1 deletion src/test/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ test_udev_LDADD = \
$(top_builddir)/src/libudev/libudev-private.la \
$(top_builddir)/src/udev/libudev-core.la \
$(BLKID_LIBS) \
$(KMOD_LIBS) \
$(SELINUX_LIBS)

if HAVE_LIBKMOD
test_udev_LDADD += $(KMOD_LIBS)
endif

test_udev_CPPFLAGS = \
-I $(top_srcdir)/src/udev \
$(AM_CPPFLAGS)
15 changes: 11 additions & 4 deletions src/udev/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ udevhomedir = $(udevlibexecdir)

AM_CPPFLAGS = \
-include $(top_builddir)/config.h \
-DMODPROBE=\"$(MODPROBE)\" \
-DROOTPREFIX=\"$(rootprefix)\" \
-DUDEVLIBEXECDIR=\"$(udevlibexecdir)\" \
-I $(top_srcdir)/src/libudev
Expand Down Expand Up @@ -101,13 +102,19 @@ include_HEADERS = \

libudev_core_la_CFLAGS = \
$(AM_CFLAGS) \
$(BLKID_CFLAGS) \
$(KMOD_CFLAGS)
$(BLKID_CFLAGS)

if HAVE_LIBKMOD
libudev_core_la_CFLAGS += $(KMOD_CFLAGS)
endif

libudev_core_la_LIBADD = \
$(top_builddir)/src/libudev/libudev-private.la \
$(BLKID_LIBS) \
$(KMOD_LIBS)
$(BLKID_LIBS)

if HAVE_LIBKMOD
libudev_core_la_LIBADD += $(KMOD_LIBS)
endif

if HAVE_ACL
libudev_core_la_SOURCES += \
Expand Down
21 changes: 20 additions & 1 deletion src/udev/udev-builtin-kmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,19 @@
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/wait.h>

#ifdef HAVE_LIBKMOD
#include <libkmod.h>
#endif

#include "udev.h"
#include "util.h"

static struct kmod_ctx *ctx;

static int load_module(struct udev *udev, const char *alias)
static int load_module(struct udev *udev, char *const alias)
{
#ifdef HAVE_LIBKMOD
struct kmod_list *list = NULL;
struct kmod_list *l;
int err;
Expand Down Expand Up @@ -62,6 +67,10 @@ static int load_module(struct udev *udev, const char *alias)

kmod_module_unref_list(list);
return err;
#else
char * const argv[] = { "-bq", alias, 0 };
return execute_command(MODPROBE, argv);
#endif
}

static void udev_kmod_log(void *data, int priority, const char *file, int line,
Expand All @@ -75,8 +84,10 @@ static int builtin_kmod(struct udev_device *dev, int argc, char *argv[], bool te
struct udev *udev = udev_device_get_udev(dev);
int i;

#ifdef HAVE_LIBKMOD
if (!ctx)
return 0;
#endif

if (argc < 3 || strcmp(argv[1], "load")) {
log_error("expect: %s load <module>\n", argv[0]);
Expand All @@ -94,6 +105,7 @@ static int builtin_kmod(struct udev_device *dev, int argc, char *argv[], bool te
/* called at udev startup and reload */
static int builtin_kmod_init(struct udev *udev)
{
#ifdef HAVE_LIBKMOD
if (ctx)
return 0;

Expand All @@ -104,16 +116,20 @@ static int builtin_kmod_init(struct udev *udev)
log_debug("load module index\n");
kmod_set_log_fn(ctx, udev_kmod_log, udev);
kmod_load_resources(ctx);
#endif
return 0;
}

/* called on udev shutdown and reload request */
static void builtin_kmod_exit(struct udev *udev)
{
#ifdef HAVE_LIBKMOD
log_debug("unload module index\n");
ctx = kmod_unref(ctx);
#endif
}

#ifdef HAVE_LIBKMOD
/* called every couple of seconds during event activity; 'true' if config has changed */
static bool builtin_kmod_validate(struct udev *udev)
{
Expand All @@ -122,13 +138,16 @@ static bool builtin_kmod_validate(struct udev *udev)
return false;
return (kmod_validate_resources(ctx) != KMOD_RESOURCES_OK);
}
#endif

const struct udev_builtin udev_builtin_kmod = {
.name = "kmod",
.cmd = builtin_kmod,
.init = builtin_kmod_init,
.exit = builtin_kmod_exit,
#ifdef HAVE_LIBKMOD
.validate = builtin_kmod_validate,
#endif
.help = "kernel module loader",
.run_once = false,
};

0 comments on commit c4d1ce9

Please sign in to comment.