]> git.ipfire.org Git - thirdparty/kmod.git/blobdiff - tools/modprobe.c
libkmod: add weak dependecies
[thirdparty/kmod.git] / tools / modprobe.c
index d03bce655fa709b1a253f661d092120931d4b8c4..4328da602be3329c2f47be4f2534afa881f2cf55 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * kmod-modprobe - manage linux kernel modules using libkmod.
  *
- * Copyright (C) 2011-2012  ProFUSION embedded systems
+ * Copyright (C) 2011-2013  ProFUSION embedded systems
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  */
 
 #include <assert.h>
+#include <errno.h>
+#include <getopt.h>
+#include <limits.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <stdbool.h>
-#include <getopt.h>
-#include <errno.h>
 #include <string.h>
-#include <sys/types.h>
+#include <unistd.h>
 #include <sys/stat.h>
+#include <sys/types.h>
 #include <sys/utsname.h>
 #include <sys/wait.h>
-#include <unistd.h>
-#include <syslog.h>
-#include <limits.h>
 
-#include "libkmod.h"
-#include "libkmod-array.h"
-#include "macro.h"
+#include <shared/array.h>
+#include <shared/util.h>
+#include <shared/macro.h>
+
+#include <libkmod/libkmod.h>
+
+#include "kmod.h"
 
 static int log_priority = LOG_CRIT;
 static int use_syslog = 0;
+#define LOG(...) log_printf(log_priority, __VA_ARGS__)
 
 #define DEFAULT_VERBOSE LOG_WARNING
 static int verbose = DEFAULT_VERBOSE;
@@ -51,14 +55,19 @@ static int use_blacklist = 0;
 static int force = 0;
 static int strip_modversion = 0;
 static int strip_vermagic = 0;
-static int remove_dependencies = 0;
+static int remove_holders = 0;
+static unsigned long long wait_msec = 0;
 static int quiet_inuse = 0;
 
-static const char cmdopts_s[] = "arRibfDcnC:d:S:sqvVh";
+static const char cmdopts_s[] = "arw:RibfDcnC:d:S:sqvVh";
 static const struct option cmdopts[] = {
        {"all", no_argument, 0, 'a'},
+
        {"remove", no_argument, 0, 'r'},
        {"remove-dependencies", no_argument, 0, 5},
+       {"remove-holders", no_argument, 0, 5},
+       {"wait", required_argument, 0, 'w'},
+
        {"resolve-alias", no_argument, 0, 'R'},
        {"first-time", no_argument, 0, 3},
        {"ignore-install", no_argument, 0, 'i'},
@@ -73,6 +82,7 @@ static const struct option cmdopts[] = {
        {"show-config", no_argument, 0, 'c'},
        {"show-modversions", no_argument, 0, 4},
        {"dump-modversions", no_argument, 0, 4},
+       {"show-exports", no_argument, 0, 6},
 
        {"dry-run", no_argument, 0, 'n'},
        {"show", no_argument, 0, 'n'},
@@ -89,10 +99,9 @@ static const struct option cmdopts[] = {
        {NULL, 0, 0, 0}
 };
 
-static void help(const char *progname)
+static void help(void)
 {
-       fprintf(stderr,
-               "Usage:\n"
+       printf("Usage:\n"
                "\t%s [options] [-i] [-b] modulename\n"
                "\t%s [options] -a [-i] [-b] modulename [modulename...]\n"
                "\t%s [options] -r [-i] modulename\n"
@@ -104,8 +113,11 @@ static void help(const char *progname)
                "\t                            be a module name to be inserted\n"
                "\t                            or removed (-r)\n"
                "\t-r, --remove                Remove modules instead of inserting\n"
-               "\t    --remove-dependencies   Also remove modules depending on it\n"
-               "\t-R, --resolve-alias         Only lookup and print alias and exit\n"
+               "\t    --remove-dependencies   Deprecated: use --remove-holders\n"
+               "\t    --remove-holders        Also remove module holders (use together with -r)\n"
+               "\t-w, --wait <MSEC>           When removing a module, wait up to MSEC for\n"
+               "\t                            module's refcount to become 0 so it can be\n"
+               "\t                            removed (use together with -r)\n"
                "\t    --first-time            Fail if module already inserted or removed\n"
                "\t-i, --ignore-install        Ignore install commands\n"
                "\t-i, --ignore-remove         Ignore remove commands\n"
@@ -117,18 +129,20 @@ static void help(const char *progname)
                "\t    --force-vermagic        Ignore module's version magic\n"
                "\n"
                "Query Options:\n"
+               "\t-R, --resolve-alias         Only lookup and print alias and exit\n"
                "\t-D, --show-depends          Only print module dependencies and exit\n"
                "\t-c, --showconfig            Print out known configuration and exit\n"
                "\t-c, --show-config           Same as --showconfig\n"
                "\t    --show-modversions      Dump module symbol version and exit\n"
                "\t    --dump-modversions      Same as --show-modversions\n"
+               "\t    --show-exports          Only print module exported symbol versions and exit\n"
                "\n"
                "General Options:\n"
                "\t-n, --dry-run               Do not execute operations, just print out\n"
                "\t-n, --show                  Same as --dry-run\n"
 
                "\t-C, --config=FILE           Use FILE instead of default search paths\n"
-               "\t-d, --dirname=DIR           Use DIR as filesystem root for /lib/modules\n"
+               "\t-d, --dirname=DIR           Use DIR as filesystem root for " MODULE_DIRECTORY "\n"
                "\t-S, --set-version=VERSION   Use VERSION instead of `uname -r`\n"
 
                "\t-s, --syslog                print to syslog, not stderr\n"
@@ -136,9 +150,12 @@ static void help(const char *progname)
                "\t-v, --verbose               enables more messages\n"
                "\t-V, --version               show version\n"
                "\t-h, --help                  show this help\n",
-               progname, progname, progname, progname, progname, progname);
+               program_invocation_short_name, program_invocation_short_name,
+               program_invocation_short_name, program_invocation_short_name,
+               program_invocation_short_name, program_invocation_short_name);
 }
 
+_printf_format_(1, 2)
 static inline void _show(const char *fmt, ...)
 {
        va_list args;
@@ -151,61 +168,6 @@ static inline void _show(const char *fmt, ...)
        fflush(stdout);
        va_end(args);
 }
-
-static inline void _log(int prio, const char *fmt, ...)
-{
-       const char *prioname;
-       char buf[32], *msg;
-       va_list args;
-
-       if (prio > verbose)
-               return;
-
-       va_start(args, fmt);
-       if (vasprintf(&msg, fmt, args) < 0)
-               msg = NULL;
-       va_end(args);
-       if (msg == NULL)
-               return;
-
-       switch (prio) {
-       case LOG_CRIT:
-               prioname = "FATAL";
-               break;
-       case LOG_ERR:
-               prioname = "ERROR";
-               break;
-       case LOG_WARNING:
-               prioname = "WARNING";
-               break;
-       case LOG_NOTICE:
-               prioname = "NOTICE";
-               break;
-       case LOG_INFO:
-               prioname = "INFO";
-               break;
-       case LOG_DEBUG:
-               prioname = "DEBUG";
-               break;
-       default:
-               snprintf(buf, sizeof(buf), "LOG-%03d", prio);
-               prioname = buf;
-       }
-
-       if (use_syslog)
-               syslog(LOG_NOTICE, "%s: %s", prioname, msg);
-       else
-               fprintf(stderr, "%s: %s", prioname, msg);
-       free(msg);
-
-       if (prio <= LOG_CRIT)
-               exit(EXIT_FAILURE);
-}
-#define ERR(...) _log(LOG_ERR, __VA_ARGS__)
-#define WRN(...) _log(LOG_WARNING, __VA_ARGS__)
-#define INF(...) _log(LOG_INFO, __VA_ARGS__)
-#define DBG(...) _log(LOG_DEBUG, __VA_ARGS__)
-#define LOG(...) _log(log_priority, __VA_ARGS__)
 #define SHOW(...) _show(__VA_ARGS__)
 
 static int show_config(struct kmod_ctx *ctx)
@@ -220,6 +182,7 @@ static int show_config(struct kmod_ctx *ctx)
                { "alias", kmod_config_get_aliases },
                { "options", kmod_config_get_options },
                { "softdep", kmod_config_get_softdeps },
+               { "weakdep", kmod_config_get_weakdeps },
        };
        size_t i;
 
@@ -282,6 +245,34 @@ static int show_modversions(struct kmod_ctx *ctx, const char *filename)
        return 0;
 }
 
+static int show_exports(struct kmod_ctx *ctx, const char *filename)
+{
+       struct kmod_list *l, *list = NULL;
+       struct kmod_module *mod;
+       int err = kmod_module_new_from_path(ctx, filename, &mod);
+       if (err < 0) {
+               LOG("Module %s not found.\n", filename);
+               return err;
+       }
+
+       err = kmod_module_get_symbols(mod, &list);
+       if (err < 0) {
+               LOG("could not get symbols of %s: %s\n",
+                       filename, strerror(-err));
+               kmod_module_unref(mod);
+               return err;
+       }
+
+       kmod_list_foreach(l, list) {
+               const char *symbol = kmod_module_symbol_get_symbol(l);
+               uint64_t crc = kmod_module_symbol_get_crc(l);
+               printf("0x%08"PRIx64"\t%s\n", crc, symbol);
+       }
+       kmod_module_symbols_free_list(list);
+       kmod_module_unref(mod);
+       return 0;
+}
+
 static int command_do(struct kmod_module *module, const char *type,
                                const char *command, const char *cmdline_opts)
 {
@@ -340,10 +331,11 @@ end:
 static int rmmod_do_remove_module(struct kmod_module *mod)
 {
        const char *modname = kmod_module_get_name(mod);
-       struct kmod_list *deps, *itr;
+       unsigned long long interval_msec = 0, t0_msec = 0,
+                     tend_msec = 0;
        int flags = 0, err;
 
-       SHOW("rmmod %s\n", kmod_module_get_name(mod));
+       SHOW("rmmod %s\n", modname);
 
        if (dry_run)
                return 0;
@@ -351,37 +343,61 @@ static int rmmod_do_remove_module(struct kmod_module *mod)
        if (force)
                flags |= KMOD_REMOVE_FORCE;
 
-       err = kmod_module_remove_module(mod, flags);
-       if (err == -EEXIST) {
-               if (!first_time)
-                       err = 0;
-               else
-                       LOG("Module %s is not in kernel.\n", modname);
-       }
+       if (wait_msec)
+               flags |= KMOD_REMOVE_NOLOG;
 
-       deps = kmod_module_get_dependencies(mod);
-       if (deps != NULL) {
-               kmod_list_foreach(itr, deps) {
-                       struct kmod_module *dep = kmod_module_get_module(itr);
-                       if (kmod_module_get_refcnt(dep) == 0)
-                               rmmod_do_remove_module(dep);
-                       kmod_module_unref(dep);
+       do {
+               err = kmod_module_remove_module(mod, flags);
+               if (err == -EEXIST) {
+                       if (!first_time)
+                               err = 0;
+                       else
+                               LOG("Module %s is not in kernel.\n", modname);
+                       break;
+               } else if (err == -EAGAIN && wait_msec) {
+                       unsigned long long until_msec;
+
+                       if (!t0_msec) {
+                               t0_msec = now_msec();
+                               tend_msec = t0_msec + wait_msec;
+                               interval_msec = 1;
+                       }
+
+                       until_msec = get_backoff_delta_msec(t0_msec, tend_msec,
+                                                         &interval_msec);
+                       err = sleep_until_msec(until_msec);
+
+                       if (!t0_msec)
+                               err = -ENOTSUP;
+
+                       if (err < 0) {
+                               ERR("Failed to sleep: %s\n", strerror(-err));
+                               err = -EAGAIN;
+                               break;
+                       }
+               } else {
+                       break;
                }
-               kmod_module_unref_list(deps);
-       }
+       } while (interval_msec);
+
+       if (err < 0 && wait_msec)
+               ERR("could not remove '%s': %s\n", modname, strerror(-err));
 
        return err;
 }
 
-static int rmmod_do_module(struct kmod_module *mod, bool do_dependencies);
+#define RMMOD_FLAG_REMOVE_HOLDERS      0x1
+#define RMMOD_FLAG_IGNORE_BUILTIN      0x2
+static int rmmod_do_module(struct kmod_module *mod, int flags);
 
-static int rmmod_do_deps_list(struct kmod_list *list, bool stop_on_errors)
+/* Remove modules in reverse order */
+static int rmmod_do_modlist(struct kmod_list *list, bool stop_on_errors)
 {
        struct kmod_list *l;
 
        kmod_list_foreach_reverse(l, list) {
                struct kmod_module *m = kmod_module_get_module(l);
-               int r = rmmod_do_module(m, false);
+               int r = rmmod_do_module(m, RMMOD_FLAG_IGNORE_BUILTIN);
                kmod_module_unref(m);
 
                if (r < 0 && stop_on_errors)
@@ -391,7 +407,7 @@ static int rmmod_do_deps_list(struct kmod_list *list, bool stop_on_errors)
        return 0;
 }
 
-static int rmmod_do_module(struct kmod_module *mod, bool do_dependencies)
+static int rmmod_do_module(struct kmod_module *mod, int flags)
 {
        const char *modname = kmod_module_get_name(mod);
        struct kmod_list *pre = NULL, *post = NULL;
@@ -409,7 +425,8 @@ static int rmmod_do_module(struct kmod_module *mod, bool do_dependencies)
                cmd = kmod_module_get_remove_commands(mod);
        }
 
-       if (cmd == NULL && !ignore_loaded) {
+       /* Quick check if module is loaded, otherwise there's nothing to do */
+       if (!cmd && !ignore_loaded) {
                int state = kmod_module_get_initstate(mod);
 
                if (state < 0) {
@@ -421,23 +438,31 @@ static int rmmod_do_module(struct kmod_module *mod, bool do_dependencies)
                        }
                        goto error;
                } else if (state == KMOD_MODULE_BUILTIN) {
-                       LOG("Module %s is builtin.\n", modname);
-                       err = -ENOENT;
+                       if (flags & RMMOD_FLAG_IGNORE_BUILTIN) {
+                               err = 0;
+                       } else {
+                               LOG("Module %s is builtin.\n", modname);
+                               err = -ENOENT;
+                       }
                        goto error;
                }
        }
 
-       rmmod_do_deps_list(post, false);
+       /* 1. @mod's post-softdeps in reverse order */
+       rmmod_do_modlist(post, false);
 
-       if (do_dependencies && remove_dependencies) {
-               struct kmod_list *deps = kmod_module_get_dependencies(mod);
+       /* 2. Other modules holding @mod */
+       if (flags & RMMOD_FLAG_REMOVE_HOLDERS) {
+               struct kmod_list *holders = kmod_module_get_holders(mod);
 
-               err = rmmod_do_deps_list(deps, true);
+               err = rmmod_do_modlist(holders, true);
+               kmod_module_unref_list(holders);
                if (err < 0)
                        goto error;
        }
 
-       if (!ignore_loaded) {
+       /* 3. @mod itself, but check for refcnt first */
+       if (!cmd && !ignore_loaded && !wait_msec) {
                int usage = kmod_module_get_refcnt(mod);
 
                if (usage > 0) {
@@ -449,7 +474,7 @@ static int rmmod_do_module(struct kmod_module *mod, bool do_dependencies)
                }
        }
 
-       if (cmd == NULL)
+       if (!cmd)
                err = rmmod_do_remove_module(mod);
        else
                err = command_do(mod, "remove", cmd, NULL);
@@ -457,7 +482,22 @@ static int rmmod_do_module(struct kmod_module *mod, bool do_dependencies)
        if (err < 0)
                goto error;
 
-       rmmod_do_deps_list(pre, false);
+       /* 4. Other modules that became unused: errors are non-fatal */
+       if (!cmd) {
+               struct kmod_list *deps, *itr;
+
+               deps = kmod_module_get_dependencies(mod);
+               kmod_list_foreach(itr, deps) {
+                       struct kmod_module *dep = kmod_module_get_module(itr);
+                       if (kmod_module_get_refcnt(dep) == 0)
+                               rmmod_do_remove_module(dep);
+                       kmod_module_unref(dep);
+               }
+               kmod_module_unref_list(deps);
+       }
+
+       /* 5. @mod's pre-softdeps in reverse order: errors are non-fatal */
+       rmmod_do_modlist(pre, false);
 
 error:
        kmod_module_unref_list(pre);
@@ -482,7 +522,9 @@ static int rmmod(struct kmod_ctx *ctx, const char *alias)
 
        kmod_list_foreach(l, list) {
                struct kmod_module *mod = kmod_module_get_module(l);
-               err = rmmod_do_module(mod, true);
+               int flags = remove_holders ? RMMOD_FLAG_REMOVE_HOLDERS : 0;
+
+               err = rmmod_do_module(mod, flags);
                kmod_module_unref(mod);
                if (err < 0)
                        break;
@@ -505,34 +547,6 @@ static int rmmod_all(struct kmod_ctx *ctx, char **args, int nargs)
        return err;
 }
 
-static int handle_failed_lookup(struct kmod_ctx *ctx, const char *alias)
-{
-       struct kmod_module *mod;
-       int state, err;
-
-       DBG("lookup failed - trying to check if it's builtin\n");
-
-       err = kmod_module_new_from_name(ctx, alias, &mod);
-       if (err < 0)
-               return err;
-
-       state = kmod_module_get_initstate(mod);
-       kmod_module_unref(mod);
-
-       if (state != KMOD_MODULE_BUILTIN) {
-               LOG("Module %s not found.\n", alias);
-               return -ENOENT;
-       }
-
-       if (first_time) {
-               LOG("Module %s already in kernel (builtin).\n", alias);
-               return -ENOENT;
-       }
-
-       SHOW("builtin %s\n", alias);
-       return 0;
-}
-
 static void print_action(struct kmod_module *m, bool install,
                                                        const char *options)
 {
@@ -547,27 +561,79 @@ static void print_action(struct kmod_module *m, bool install,
        path = kmod_module_get_path(m);
 
        if (path == NULL) {
-               assert(kmod_module_get_initstate(m) == KMOD_MODULE_BUILTIN);
-               printf("builtin %s\n", kmod_module_get_name(m));
+               /*
+                * Either a builtin module, or an alias, print only for
+                * builtin
+                */
+               if (kmod_module_get_initstate(m) == KMOD_MODULE_BUILTIN)
+                       printf("builtin %s\n", kmod_module_get_name(m));
        } else
                printf("insmod %s %s\n", kmod_module_get_path(m), options);
 }
 
+static int insmod_insert(struct kmod_module *mod, int flags,
+                               const char *extra_options)
+{
+       int err = 0;
+       void (*show)(struct kmod_module *m, bool install,
+                                               const char *options) = NULL;
+
+       if (do_show || verbose > DEFAULT_VERBOSE)
+               show = &print_action;
+
+       if (lookup_only)
+               printf("%s\n", kmod_module_get_name(mod));
+       else
+               err = kmod_module_probe_insert_module(mod, flags,
+                               extra_options, NULL, NULL, show);
+
+       if (err >= 0)
+               /* ignore flag return values such as a mod being blacklisted */
+               err = 0;
+       else {
+               switch (err) {
+               case -EEXIST:
+                       ERR("could not insert '%s': Module already in kernel\n",
+                                               kmod_module_get_name(mod));
+                       break;
+               case -ENOENT:
+                       ERR("could not insert '%s': Unknown symbol in module, "
+                                       "or unknown parameter (see dmesg)\n",
+                                       kmod_module_get_name(mod));
+                       break;
+               default:
+                       ERR("could not insert '%s': %s\n",
+                                       kmod_module_get_name(mod),
+                                       strerror(-err));
+                       break;
+               }
+       }
+
+       return err;
+}
+
 static int insmod(struct kmod_ctx *ctx, const char *alias,
                                                const char *extra_options)
 {
        struct kmod_list *l, *list = NULL;
+       struct kmod_module *mod = NULL;
        int err, flags = 0;
 
-       void (*show)(struct kmod_module *m, bool install,
-                                               const char *options) = NULL;
-
-       err = kmod_module_new_from_lookup(ctx, alias, &list);
-       if (err < 0)
-               return err;
-
-       if (list == NULL)
-               return handle_failed_lookup(ctx, alias);
+       if (strncmp(alias, "/", 1) == 0 || strncmp(alias, "./", 2) == 0) {
+               err = kmod_module_new_from_path(ctx, alias, &mod);
+               if (err < 0) {
+                       LOG("Failed to get module from path %s: %s\n", alias,
+                               strerror(-err));
+                       return -ENOENT;
+               }
+       } else {
+               err = kmod_module_new_from_lookup(ctx, alias, &list);
+               if (list == NULL || err < 0) {
+                       LOG("Module %s not found in directory %s\n", alias,
+                               ctx ? kmod_get_dirname(ctx) : "(missing)");
+                       return -ENOENT;
+               }
+       }
 
        if (strip_modversion || force)
                flags |= KMOD_PROBE_FORCE_MODVERSION;
@@ -579,8 +645,6 @@ static int insmod(struct kmod_ctx *ctx, const char *alias,
                flags |= KMOD_PROBE_IGNORE_LOADED;
        if (dry_run)
                flags |= KMOD_PROBE_DRY_RUN;
-       if (do_show || verbose > DEFAULT_VERBOSE)
-               show = &print_action;
 
        flags |= KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY;
 
@@ -589,42 +653,18 @@ static int insmod(struct kmod_ctx *ctx, const char *alias,
        if (first_time)
                flags |= KMOD_PROBE_FAIL_ON_LOADED;
 
-       kmod_list_foreach(l, list) {
-               struct kmod_module *mod = kmod_module_get_module(l);
-
-               if (lookup_only)
-                       printf("%s\n", kmod_module_get_name(mod));
-               else {
-                       err = kmod_module_probe_insert_module(mod, flags,
-                                       extra_options, NULL, NULL, show);
-               }
-
-               if (err >= 0)
-                       /* ignore flag return values such as a mod being blacklisted */
-                       err = 0;
-               else {
-                       switch (err) {
-                       case -EEXIST:
-                               ERR("could not insert '%s': Module already in kernel\n",
-                                                       kmod_module_get_name(mod));
-                               break;
-                       case -ENOENT:
-                               ERR("could not insert '%s': Unknown symbol in module, "
-                                               "or unknown parameter (see dmesg)\n",
-                                               kmod_module_get_name(mod));
-                               break;
-                       default:
-                               ERR("could not insert '%s': %s\n",
-                                               kmod_module_get_name(mod),
-                                               strerror(-err));
-                               break;
-                       }
-               }
-
+       /* If module is loaded from path */
+       if (mod != NULL) {
+               err = insmod_insert(mod, flags, extra_options);
                kmod_module_unref(mod);
+       } else {
+               kmod_list_foreach(l, list) {
+                       mod = kmod_module_get_module(l);
+                       err = insmod_insert(mod, flags, extra_options);
+                       kmod_module_unref(mod);
+               }
+               kmod_module_unref_list(list);
        }
-
-       kmod_module_unref_list(list);
        return err;
 }
 
@@ -720,7 +760,7 @@ static int options_from_array(char **args, int nargs, char **output)
 static char **prepend_options_from_env(int *p_argc, char **orig_argv)
 {
        const char *p, *env = getenv("MODPROBE_OPTIONS");
-       char **new_argv, *str_start, *str_end, *str, *s, *quote;
+       char **new_argv, *str_end, *str, *s, *quote;
        int i, argc = *p_argc;
        size_t envlen, space_count = 0;
 
@@ -738,10 +778,10 @@ static char **prepend_options_from_env(int *p_argc, char **orig_argv)
                return NULL;
 
        new_argv[0] = orig_argv[0];
-       str_start = str = (char *) (new_argv + argc + space_count + 3);
+       str = (char *) (new_argv + argc + space_count + 3);
        memcpy(str, env, envlen + 1);
 
-       str_end = str_start + envlen;
+       str_end = str + envlen;
 
        quote = NULL;
        for (i = 1, s = str; *s != '\0'; s++) {
@@ -780,53 +820,12 @@ static char **prepend_options_from_env(int *p_argc, char **orig_argv)
        }
 
        memcpy(new_argv + i, orig_argv + 1, sizeof(char *) * (argc - 1));
-       new_argv[i + argc] = NULL;
+       new_argv[i + argc - 1] = NULL;
        *p_argc = i + argc - 1;
 
        return new_argv;
 }
 
-static void log_syslog(void *data, int priority, const char *file, int line,
-                       const char *fn, const char *format, va_list args)
-{
-       char *str, buf[32];
-       const char *prioname;
-
-       switch (priority) {
-       case LOG_CRIT:
-               prioname = "FATAL";
-               break;
-       case LOG_ERR:
-               prioname = "ERROR";
-               break;
-       case LOG_WARNING:
-               prioname = "WARNING";
-               break;
-       case LOG_NOTICE:
-               prioname = "NOTICE";
-               break;
-       case LOG_INFO:
-               prioname = "INFO";
-               break;
-       case LOG_DEBUG:
-               prioname = "DEBUG";
-               break;
-       default:
-               snprintf(buf, sizeof(buf), "LOG-%03d", priority);
-               prioname = buf;
-       }
-
-       if (vasprintf(&str, format, args) < 0)
-               return;
-#ifdef ENABLE_DEBUG
-       syslog(LOG_NOTICE, "%s: %s:%d %s() %s", prioname, file, line, fn, str);
-#else
-       syslog(LOG_NOTICE, "%s: %s", prioname, str);
-#endif
-       free(str);
-       (void)data;
-}
-
 static int do_modprobe(int argc, char **orig_argv)
 {
        struct kmod_ctx *ctx;
@@ -841,12 +840,13 @@ static int do_modprobe(int argc, char **orig_argv)
        int do_remove = 0;
        int do_show_config = 0;
        int do_show_modversions = 0;
+       int do_show_exports = 0;
        int err;
+       struct stat stat_buf;
 
        argv = prepend_options_from_env(&argc, orig_argv);
        if (argv == NULL) {
-               fputs("Error: could not prepend options from command line\n",
-                       stderr);
+               ERR("Could not prepend options from command line\n");
                return EXIT_FAILURE;
        }
 
@@ -864,11 +864,18 @@ static int do_modprobe(int argc, char **orig_argv)
                        do_remove = 1;
                        break;
                case 5:
-                       remove_dependencies = 1;
+                       remove_holders = 1;
                        break;
-               case 'R':
-                       lookup_only = 1;
+               case 'w': {
+                       char *endptr = NULL;
+                       wait_msec = strtoul(optarg, &endptr, 0);
+                       if (!*optarg || *endptr) {
+                               ERR("unexpected wait value '%s'.\n", optarg);
+                               err = -1;
+                               goto done;
+                       }
                        break;
+               }
                case 3:
                        first_time = 1;
                        break;
@@ -892,12 +899,18 @@ static int do_modprobe(int argc, char **orig_argv)
                        dry_run = 1;
                        do_show = 1;
                        break;
+               case 'R':
+                       lookup_only = 1;
+                       break;
                case 'c':
                        do_show_config = 1;
                        break;
                case 4:
                        do_show_modversions = 1;
                        break;
+               case 6:
+                       do_show_exports = 1;
+                       break;
                case 'n':
                        dry_run = 1;
                        break;
@@ -905,8 +918,9 @@ static int do_modprobe(int argc, char **orig_argv)
                        size_t bytes = sizeof(char *) * (n_config_paths + 2);
                        void *tmp = realloc(config_paths, bytes);
                        if (!tmp) {
-                               fputs("Error: out-of-memory\n", stderr);
-                               goto cmdline_failed;
+                               ERR("out-of-memory\n");
+                               err = -1;
+                               goto done;
                        }
                        config_paths = tmp;
                        config_paths[n_config_paths] = optarg;
@@ -937,28 +951,39 @@ static int do_modprobe(int argc, char **orig_argv)
                        break;
                case 'V':
                        puts(PACKAGE " version " VERSION);
-                       err = EXIT_SUCCESS;
+                       puts(KMOD_FEATURES);
+                       err = 0;
                        goto done;
                case 'h':
-                       help(basename(argv[0]));
-                       err = EXIT_SUCCESS;
+                       help();
+                       err = 0;
                        goto done;
                case '?':
-                       goto cmdline_failed;
+                       err = -1;
+                       goto done;
                default:
-                       fprintf(stderr, "Error: unexpected getopt_long() value '%c'.\n",
-                                                                       c);
-                       goto cmdline_failed;
+                       ERR("unexpected getopt_long() value '%c'.\n", c);
+                       err = -1;
+                       goto done;
                }
        }
 
        args = argv + optind;
        nargs = argc - optind;
 
+       if (!use_syslog &&
+           (!stderr ||
+            fileno(stderr) == -1 ||
+            fstat(fileno(stderr), &stat_buf)))
+               use_syslog = 1;
+
+       log_open(use_syslog);
+
        if (!do_show_config) {
                if (nargs == 0) {
-                       fputs("Error: missing parameters. See -h.\n", stderr);
-                       goto cmdline_failed;
+                       ERR("missing parameters. See -h.\n");
+                       err = -1;
+                       goto done;
                }
        }
 
@@ -968,35 +993,35 @@ static int do_modprobe(int argc, char **orig_argv)
                        root = "";
                if (kversion == NULL) {
                        if (uname(&u) < 0) {
-                               fprintf(stderr, "Error: uname() failed: %s\n",
-                                       strerror(errno));
-                               goto cmdline_failed;
+                               ERR("uname() failed: %m\n");
+                               err = -1;
+                               goto done;
                        }
                        kversion = u.release;
                }
                snprintf(dirname_buf, sizeof(dirname_buf),
-                               "%s/lib/modules/%s", root,
+                               "%s" MODULE_DIRECTORY "/%s", root,
                                kversion);
                dirname = dirname_buf;
        }
 
        ctx = kmod_new(dirname, config_paths);
        if (!ctx) {
-               fputs("Error: kmod_new() failed!\n", stderr);
-               goto cmdline_failed;
+               ERR("kmod_new() failed!\n");
+               err = -1;
+               goto done;
        }
-       kmod_load_resources(ctx);
 
-       kmod_set_log_priority(ctx, verbose);
-       if (use_syslog) {
-               openlog("modprobe", LOG_CONS, LOG_DAEMON);
-               kmod_set_log_fn(ctx, log_syslog, NULL);
-       }
+       log_setup_kmod_log(ctx, verbose);
+
+       kmod_load_resources(ctx);
 
        if (do_show_config)
                err = show_config(ctx);
        else if (do_show_modversions)
                err = show_modversions(ctx, args[0]);
+       else if (do_show_exports)
+               err = show_exports(ctx, args[0]);
        else if (do_remove)
                err = rmmod_all(ctx, args, nargs);
        else if (use_all)
@@ -1012,25 +1037,17 @@ static int do_modprobe(int argc, char **orig_argv)
 
        kmod_unref(ctx);
 
-       if (use_syslog)
-               closelog();
-
 done:
+       log_close();
+
        if (argv != orig_argv)
                free(argv);
 
        free(config_paths);
-       return err >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
 
-cmdline_failed:
-       if (argv != orig_argv)
-               free(argv);
-       free(config_paths);
-       return EXIT_FAILURE;
+       return err >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
 }
 
-#include "kmod.h"
-
 const struct kmod_cmd kmod_cmd_compat_modprobe = {
        .name = "modprobe",
        .cmd = do_modprobe,