123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725 |
- #include "libbb.h"
- #include "modutils.h"
- #include <sys/utsname.h>
- #include <fnmatch.h>
- #if 1
- #define DBG(...) ((void)0)
- #else
- #define DBG(fmt, ...) bb_error_msg("%s: " fmt, __func__, ## __VA_ARGS__)
- #endif
- #define MODPROBE_OPTS "alrDb"
- #define MODPROBE_COMPLEMENTARY "q-v:v-q:l--arD:r--alD:a--lr:D--rl"
- enum {
- OPT_INSERT_ALL = (INSMOD_OPT_UNUSED << 0),
-
-
- OPT_LIST_ONLY = (INSMOD_OPT_UNUSED << 1),
-
- OPT_REMOVE = (INSMOD_OPT_UNUSED << 2),
-
-
-
- OPT_SHOW_DEPS = (INSMOD_OPT_UNUSED << 3),
- OPT_BLACKLIST = (INSMOD_OPT_UNUSED << 4) * ENABLE_FEATURE_MODPROBE_BLACKLIST,
- };
- #if ENABLE_LONG_OPTS
- static const char modprobe_longopts[] ALIGN1 =
-
-
-
-
-
-
-
-
- "show-depends\0" No_argument "D"
-
- ;
- #endif
- #define MODULE_FLAG_LOADED 0x0001
- #define MODULE_FLAG_NEED_DEPS 0x0002
- #define MODULE_FLAG_FOUND_IN_MODDEP 0x0004
- #define MODULE_FLAG_BLACKLISTED 0x0008
- #define MODULE_FLAG_BUILTIN 0x0010
- struct globals {
- llist_t *probes;
- #if ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS
- char *cmdline_mopts;
- #endif
- int num_unresolved_deps;
-
- smallint need_symbols;
- struct utsname uts;
- module_db db;
- } FIX_ALIASING;
- #define G (*ptr_to_globals)
- #define INIT_G() do { \
- SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
- } while (0)
- static int read_config(const char *path);
- static char *gather_options_str(char *opts, const char *append)
- {
-
- if (append) {
- if (opts == NULL) {
- opts = xstrdup(append);
- } else {
- int optlen = strlen(opts);
- opts = xrealloc(opts, optlen + strlen(append) + 2);
- sprintf(opts + optlen, " %s", append);
- }
- }
- return opts;
- }
- static struct module_entry *get_or_add_modentry(const char *module)
- {
- return moddb_get_or_create(&G.db, module);
- }
- static void add_probe(const char *name)
- {
- struct module_entry *m;
- m = get_or_add_modentry(name);
- if (!(option_mask32 & (OPT_REMOVE | OPT_SHOW_DEPS))
- && (m->flags & (MODULE_FLAG_LOADED | MODULE_FLAG_BUILTIN))
- ) {
- DBG("skipping %s, it is already loaded", name);
- return;
- }
- DBG("queuing %s", name);
- m->probed_name = name;
- m->flags |= MODULE_FLAG_NEED_DEPS;
- llist_add_to_end(&G.probes, m);
- G.num_unresolved_deps++;
- if (ENABLE_FEATURE_MODUTILS_SYMBOLS
- && is_prefixed_with(m->modname, "symbol:")
- ) {
- G.need_symbols = 1;
- }
- }
- static int FAST_FUNC config_file_action(struct recursive_state *state,
- const char *filename,
- struct stat *statbuf UNUSED_PARAM)
- {
- char *tokens[3];
- parser_t *p;
- struct module_entry *m;
- int rc = TRUE;
- const char *base;
-
- base = bb_basename(filename);
- if (base[0] == '.')
- goto error;
-
- if (state->depth > 1)
- return SKIP;
-
- if (state->depth != 0) {
- if (!is_suffixed_with(base, ".conf"))
- goto error;
- }
- p = config_open2(filename, fopen_for_read);
- if (p == NULL) {
- rc = FALSE;
- goto error;
- }
- while (config_read(p, tokens, 3, 2, "# \t", PARSE_NORMAL)) {
- if (strcmp(tokens[0], "alias") == 0) {
-
- llist_t *l;
- char wildcard[MODULE_NAME_LEN];
- char *rmod;
- if (tokens[2] == NULL)
- continue;
- filename2modname(tokens[1], wildcard);
- for (l = G.probes; l; l = l->link) {
- m = (struct module_entry *) l->data;
- if (fnmatch(wildcard, m->modname, 0) != 0)
- continue;
- rmod = filename2modname(tokens[2], NULL);
- llist_add_to(&m->realnames, rmod);
- if (m->flags & MODULE_FLAG_NEED_DEPS) {
- m->flags &= ~MODULE_FLAG_NEED_DEPS;
- G.num_unresolved_deps--;
- }
- m = get_or_add_modentry(rmod);
- if (!(m->flags & MODULE_FLAG_NEED_DEPS)) {
- m->flags |= MODULE_FLAG_NEED_DEPS;
- G.num_unresolved_deps++;
- }
- }
- } else if (strcmp(tokens[0], "options") == 0) {
-
- if (tokens[2] == NULL)
- continue;
- m = get_or_add_modentry(tokens[1]);
- m->options = gather_options_str(m->options, tokens[2]);
- } else if (strcmp(tokens[0], "include") == 0) {
-
- read_config(tokens[1]);
- } else if (ENABLE_FEATURE_MODPROBE_BLACKLIST
- && strcmp(tokens[0], "blacklist") == 0
- ) {
-
- get_or_add_modentry(tokens[1])->flags |= MODULE_FLAG_BLACKLISTED;
- }
- }
- config_close(p);
- error:
- return rc;
- }
- static int read_config(const char *path)
- {
- return recursive_action(path, ACTION_RECURSE | ACTION_QUIET,
- config_file_action, NULL, NULL);
- }
- static const char *humanly_readable_name(struct module_entry *m)
- {
-
- return m->probed_name ? m->probed_name : m->modname;
- }
- static char *strsep_quotes(char **stringp)
- {
- char *s, *start = *stringp;
- if (!start)
- return NULL;
- for (s = start; ; s++) {
- switch (*s) {
- case '"':
- s = strchrnul(s + 1, '"');
- if (*s != '\0')
- s++;
-
- case '\0':
- case '\n':
- case '\t':
- case ' ':
- if (*s != '\0') {
- *s = '\0';
- *stringp = s + 1;
- } else {
- *stringp = NULL;
- }
- return start;
- }
- }
- }
- static char *parse_and_add_kcmdline_module_options(char *options, const char *modulename)
- {
- char *kcmdline_buf;
- char *kcmdline;
- char *kptr;
- kcmdline_buf = xmalloc_open_read_close("/proc/cmdline", NULL);
- if (!kcmdline_buf)
- return options;
- kcmdline = kcmdline_buf;
- while ((kptr = strsep_quotes(&kcmdline)) != NULL) {
- char *after_modulename = is_prefixed_with(kptr, modulename);
- if (!after_modulename || *after_modulename != '.')
- continue;
-
- kptr = after_modulename + 1;
- if (strchr(kptr, '=') != NULL) {
-
- options = gather_options_str(options, kptr);
- }
- }
- free(kcmdline_buf);
- return options;
- }
- static int do_modprobe(struct module_entry *m)
- {
- int rc, first;
- if (!(m->flags & MODULE_FLAG_FOUND_IN_MODDEP)) {
- if (!(option_mask32 & INSMOD_OPT_SILENT))
- bb_error_msg((m->flags & MODULE_FLAG_BUILTIN) ?
- "module %s is builtin" :
- "module %s not found in modules.dep",
- humanly_readable_name(m));
- return -ENOENT;
- }
- DBG("do_modprob'ing %s", m->modname);
- if (!(option_mask32 & OPT_REMOVE))
- m->deps = llist_rev(m->deps);
- if (0) {
- llist_t *l;
- for (l = m->deps; l; l = l->link)
- DBG("dep: %s", l->data);
- }
- first = 1;
- rc = 0;
- while (m->deps) {
- struct module_entry *m2;
- char *fn, *options;
- rc = 0;
- fn = llist_pop(&m->deps);
- m2 = get_or_add_modentry(bb_get_last_path_component_nostrip(fn));
- if (option_mask32 & OPT_REMOVE) {
-
- if (m2->flags & MODULE_FLAG_LOADED) {
- rc = bb_delete_module(m2->modname, O_EXCL);
- if (rc) {
- if (first) {
- bb_perror_msg("can't unload module '%s'",
- humanly_readable_name(m2));
- break;
- }
- } else {
- m2->flags &= ~MODULE_FLAG_LOADED;
- }
- }
-
- first = 0;
- continue;
- }
- options = m2->options;
- m2->options = NULL;
- options = parse_and_add_kcmdline_module_options(options, m2->modname);
- #if ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS
- if (m == m2)
- options = gather_options_str(options, G.cmdline_mopts);
- #endif
- if (option_mask32 & OPT_SHOW_DEPS) {
- printf(options ? "insmod %s/%s/%s %s\n"
- : "insmod %s/%s/%s\n",
- CONFIG_DEFAULT_MODULES_DIR, G.uts.release, fn,
- options);
- free(options);
- continue;
- }
- if (m2->flags & MODULE_FLAG_LOADED) {
- DBG("%s is already loaded, skipping", fn);
- free(options);
- continue;
- }
- rc = bb_init_module(fn, options);
- DBG("loaded %s '%s', rc:%d", fn, options, rc);
- if (rc == EEXIST)
- rc = 0;
- free(options);
- if (rc) {
- bb_error_msg("can't load module %s (%s): %s",
- humanly_readable_name(m2),
- fn,
- moderror(rc)
- );
- break;
- }
- m2->flags |= MODULE_FLAG_LOADED;
- }
- return rc;
- }
- static void load_modules_dep(void)
- {
- struct module_entry *m;
- char *colon, *tokens[2];
- parser_t *p;
-
- p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
- while (G.num_unresolved_deps
- && config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)
- ) {
- colon = last_char_is(tokens[0], ':');
- if (colon == NULL)
- continue;
- *colon = '\0';
- m = moddb_get(&G.db, bb_get_last_path_component_nostrip(tokens[0]));
- if (m == NULL)
- continue;
-
- if ((m->flags & MODULE_FLAG_LOADED)
- && !(option_mask32 & (OPT_REMOVE | OPT_SHOW_DEPS))
- ) {
- DBG("skip deps of %s, it's already loaded", tokens[0]);
- continue;
- }
- m->flags |= MODULE_FLAG_FOUND_IN_MODDEP;
- if ((m->flags & MODULE_FLAG_NEED_DEPS) && (m->deps == NULL)) {
- G.num_unresolved_deps--;
- llist_add_to(&m->deps, xstrdup(tokens[0]));
- if (tokens[1])
- string_to_llist(tokens[1], &m->deps, " \t");
- } else
- DBG("skipping dep line");
- }
- config_close(p);
- }
- int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int modprobe_main(int argc UNUSED_PARAM, char **argv)
- {
- int rc;
- unsigned opt;
- struct module_entry *me;
- INIT_G();
- opt = getopt32long(argv, "^" INSMOD_OPTS MODPROBE_OPTS "\0" MODPROBE_COMPLEMENTARY,
- modprobe_longopts
- INSMOD_ARGS
- );
- argv += optind;
-
- xchdir(CONFIG_DEFAULT_MODULES_DIR);
- uname(&G.uts);
- xchdir(G.uts.release);
- if (opt & OPT_LIST_ONLY) {
- int i;
- char *colon, *tokens[2];
- parser_t *p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
- for (i = 0; argv[i]; i++)
- replace(argv[i], '-', '_');
- while (config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)) {
- colon = last_char_is(tokens[0], ':');
- if (!colon)
- continue;
- *colon = '\0';
- if (!argv[0])
- puts(tokens[0]);
- else {
- char name[MODULE_NAME_LEN];
- filename2modname(
- bb_get_last_path_component_nostrip(tokens[0]),
- name
- );
- for (i = 0; argv[i]; i++) {
- if (fnmatch(argv[i], name, 0) == 0) {
- puts(tokens[0]);
- }
- }
- }
- }
- return EXIT_SUCCESS;
- }
-
- if (opt & INSMOD_OPT_SYSLOG)
- logmode = LOGMODE_SYSLOG;
- if (!argv[0]) {
- if (opt & OPT_REMOVE) {
-
- if (bb_delete_module(NULL, O_NONBLOCK | O_EXCL) != 0)
- bb_perror_nomsg_and_die();
- }
- return EXIT_SUCCESS;
- }
-
- {
- char *s;
- parser_t *parser = config_open2("/proc/modules", fopen_for_read);
- while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY))
- get_or_add_modentry(s)->flags |= MODULE_FLAG_LOADED;
- config_close(parser);
- parser = config_open2("modules.builtin", fopen_for_read);
-
- while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL))
- get_or_add_modentry(bb_basename(s))->flags |= MODULE_FLAG_BUILTIN;
- config_close(parser);
- }
- if (opt & (OPT_INSERT_ALL | OPT_REMOVE)) {
-
- do {
- DBG("adding module %s", *argv);
- add_probe(*argv++);
- } while (*argv);
- } else {
-
- DBG("probing just module %s", *argv);
- add_probe(argv[0]);
- #if ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS
- G.cmdline_mopts = parse_cmdline_module_options(argv, 1);
- #endif
- }
-
- if (G.probes == NULL)
- return EXIT_SUCCESS;
- read_config("/etc/modprobe.conf");
- read_config("/etc/modprobe.d");
- if (ENABLE_FEATURE_MODUTILS_SYMBOLS && G.need_symbols)
- read_config("modules.symbols");
- load_modules_dep();
- if (ENABLE_FEATURE_MODUTILS_ALIAS && G.num_unresolved_deps) {
- read_config("modules.alias");
- load_modules_dep();
- }
-
- if (ENABLE_FEATURE_MODPROBE_BLACKLIST) {
- char *options;
- char *substr;
- options = parse_and_add_kcmdline_module_options(NULL, "modprobe");
- while ((substr = strsep(&options, " ")) != NULL) {
- char *fn = is_prefixed_with(substr, "blacklist=");
- if (!fn)
- continue;
- while ((substr = strsep(&fn, ",")) != NULL) {
-
- get_or_add_modentry(substr)->flags |= MODULE_FLAG_BLACKLISTED;
- DBG("blacklist: %s", substr);
- }
- }
-
- }
- rc = 0;
- while ((me = llist_pop(&G.probes)) != NULL) {
- if (me->realnames == NULL) {
- DBG("probing by module name");
-
- if (!(opt & OPT_BLACKLIST)
- || !(me->flags & MODULE_FLAG_BLACKLISTED)
- ) {
- rc |= do_modprobe(me);
- }
- continue;
- }
-
- do {
- char *realname = llist_pop(&me->realnames);
- struct module_entry *m2;
- DBG("probing alias %s by realname %s", me->modname, realname);
- m2 = get_or_add_modentry(realname);
- if (!(m2->flags & MODULE_FLAG_BLACKLISTED)
- && (!(m2->flags & MODULE_FLAG_LOADED)
- || (opt & (OPT_REMOVE | OPT_SHOW_DEPS)))
- ) {
- rc |= do_modprobe(m2);
- }
- free(realname);
- } while (me->realnames != NULL);
- }
- if (ENABLE_FEATURE_CLEAN_UP)
- moddb_free(&G.db);
- return (rc != 0);
- }
|