2008-07-04 15:55:44 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* simplified modprobe
|
|
|
|
*
|
|
|
|
* Copyright (c) 2008 Vladimir Dronnikov
|
2008-09-25 17:43:34 +05:30
|
|
|
* Copyright (c) 2008 Bernhard Reutner-Fischer (initial depmod code)
|
2008-07-04 15:55:44 +05:30
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
2008-07-04 15:55:44 +05:30
|
|
|
*/
|
2016-12-22 19:06:49 +05:30
|
|
|
|
2017-02-01 17:13:54 +05:30
|
|
|
/* modprobe-small configs are defined in Config.src to ensure better
|
|
|
|
* "make config" order */
|
2008-07-04 15:55:44 +05:30
|
|
|
|
2017-08-04 06:26:39 +05:30
|
|
|
//applet:IF_LSMOD( IF_MODPROBE_SMALL(APPLET_NOEXEC( lsmod, lsmod, BB_DIR_SBIN, BB_SUID_DROP, lsmod )))
|
|
|
|
//applet:IF_MODPROBE(IF_MODPROBE_SMALL(APPLET_NOEXEC( modprobe, modprobe, BB_DIR_SBIN, BB_SUID_DROP, modprobe)))
|
|
|
|
// APPLET_ODDNAME:name main location suid_type help
|
|
|
|
//applet:IF_DEPMOD( IF_MODPROBE_SMALL(APPLET_ODDNAME(depmod, modprobe, BB_DIR_SBIN, BB_SUID_DROP, depmod )))
|
|
|
|
//applet:IF_INSMOD( IF_MODPROBE_SMALL(APPLET_NOEXEC( insmod, modprobe, BB_DIR_SBIN, BB_SUID_DROP, insmod )))
|
|
|
|
//applet:IF_RMMOD( IF_MODPROBE_SMALL(APPLET_NOEXEC( rmmod, modprobe, BB_DIR_SBIN, BB_SUID_DROP, rmmod )))
|
|
|
|
/* noexec speeds up boot with many modules loaded (need SH_STANDALONE=y) */
|
|
|
|
/* I measured about ~5 times faster insmod */
|
|
|
|
/* depmod is not noexec, it runs longer and benefits from memory trimming via exec */
|
2010-10-15 14:59:02 +05:30
|
|
|
|
2016-11-23 12:24:52 +05:30
|
|
|
//kbuild:lib-$(CONFIG_MODPROBE_SMALL) += modprobe-small.o
|
|
|
|
|
2008-07-04 15:55:44 +05:30
|
|
|
#include "libbb.h"
|
2009-09-06 16:17:55 +05:30
|
|
|
/* After libbb.h, since it needs sys/types.h on some systems */
|
2008-07-04 15:55:44 +05:30
|
|
|
#include <sys/utsname.h> /* uname() */
|
|
|
|
#include <fnmatch.h>
|
2016-09-15 15:46:33 +05:30
|
|
|
#include <sys/syscall.h>
|
2008-07-04 15:55:44 +05:30
|
|
|
|
2016-12-27 00:37:59 +05:30
|
|
|
#define init_module(mod, len, opts) syscall(__NR_init_module, mod, len, opts)
|
|
|
|
#define delete_module(mod, flags) syscall(__NR_delete_module, mod, flags)
|
2016-09-15 15:46:33 +05:30
|
|
|
#ifdef __NR_finit_module
|
|
|
|
# define finit_module(fd, uargs, flags) syscall(__NR_finit_module, fd, uargs, flags)
|
|
|
|
#endif
|
2014-04-21 20:29:36 +05:30
|
|
|
/* linux/include/linux/module.h has limit of 64 chars on module names */
|
|
|
|
#undef MODULE_NAME_LEN
|
|
|
|
#define MODULE_NAME_LEN 64
|
2008-07-09 21:00:57 +05:30
|
|
|
|
|
|
|
|
2010-09-25 18:07:06 +05:30
|
|
|
#if 1
|
|
|
|
# define dbg1_error_msg(...) ((void)0)
|
|
|
|
# define dbg2_error_msg(...) ((void)0)
|
|
|
|
#else
|
|
|
|
# define dbg1_error_msg(...) bb_error_msg(__VA_ARGS__)
|
|
|
|
# define dbg2_error_msg(...) bb_error_msg(__VA_ARGS__)
|
|
|
|
#endif
|
2008-07-04 15:55:44 +05:30
|
|
|
|
2008-07-09 21:00:57 +05:30
|
|
|
#define DEPFILE_BB CONFIG_DEFAULT_DEPMOD_FILE".bb"
|
2008-07-04 15:55:44 +05:30
|
|
|
|
2017-01-31 14:36:43 +05:30
|
|
|
//usage:#if ENABLE_MODPROBE_SMALL
|
|
|
|
|
|
|
|
//usage:#define lsmod_trivial_usage
|
|
|
|
//usage: ""
|
|
|
|
//usage:#define lsmod_full_usage "\n\n"
|
|
|
|
//usage: "List loaded kernel modules"
|
|
|
|
|
|
|
|
//usage:#endif
|
|
|
|
|
|
|
|
#if ENABLE_LSMOD
|
|
|
|
int lsmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
|
|
|
int lsmod_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
|
|
|
|
{
|
|
|
|
xprint_and_close_file(xfopen_for_read("/proc/modules"));
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Num of applets that use modprobe_main() entry point. */
|
|
|
|
/* lsmod is not here. */
|
|
|
|
#define MOD_APPLET_CNT (ENABLE_MODPROBE + ENABLE_DEPMOD + ENABLE_INSMOD + ENABLE_RMMOD)
|
2017-01-09 21:01:21 +05:30
|
|
|
|
|
|
|
/* Do not bother if MODPROBE_SMALL=y but no applets selected. */
|
|
|
|
/* The rest of the file is in this if block. */
|
|
|
|
#if MOD_APPLET_CNT > 0
|
|
|
|
|
|
|
|
#define ONLY_APPLET (MOD_APPLET_CNT == 1)
|
2017-01-09 12:34:43 +05:30
|
|
|
#define is_modprobe (ENABLE_MODPROBE && (ONLY_APPLET || applet_name[0] == 'm'))
|
|
|
|
#define is_depmod (ENABLE_DEPMOD && (ONLY_APPLET || applet_name[0] == 'd'))
|
|
|
|
#define is_insmod (ENABLE_INSMOD && (ONLY_APPLET || applet_name[0] == 'i'))
|
|
|
|
#define is_rmmod (ENABLE_RMMOD && (ONLY_APPLET || applet_name[0] == 'r'))
|
|
|
|
|
2008-07-04 15:55:44 +05:30
|
|
|
enum {
|
2017-02-09 20:18:52 +05:30
|
|
|
DEPMOD_OPT_n = (1 << 0), /* dry-run, print to stdout */
|
2008-07-04 15:55:44 +05:30
|
|
|
OPT_q = (1 << 0), /* be quiet */
|
|
|
|
OPT_r = (1 << 1), /* module removal instead of loading */
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct module_info {
|
|
|
|
char *pathname;
|
2008-07-10 19:44:20 +05:30
|
|
|
char *aliases;
|
|
|
|
char *deps;
|
2016-06-20 14:29:06 +05:30
|
|
|
smallint open_read_failed;
|
2008-07-04 15:55:44 +05:30
|
|
|
} module_info;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* GLOBALS
|
|
|
|
*/
|
|
|
|
struct globals {
|
|
|
|
module_info *modinfo;
|
|
|
|
char *module_load_options;
|
2008-07-10 19:44:45 +05:30
|
|
|
smallint dep_bb_seen;
|
2008-07-10 19:46:11 +05:30
|
|
|
smallint wrote_dep_bb_ok;
|
2009-09-07 06:08:26 +05:30
|
|
|
unsigned module_count;
|
2008-07-04 15:55:44 +05:30
|
|
|
int module_found_idx;
|
2009-09-07 06:08:26 +05:30
|
|
|
unsigned stringbuf_idx;
|
|
|
|
unsigned stringbuf_size;
|
|
|
|
char *stringbuf; /* some modules have lots of stuff */
|
2008-07-04 15:55:44 +05:30
|
|
|
/* for example, drivers/media/video/saa7134/saa7134.ko */
|
2009-09-07 06:08:26 +05:30
|
|
|
/* therefore having a fixed biggish buffer is not wise */
|
2008-07-04 15:55:44 +05:30
|
|
|
};
|
|
|
|
#define G (*ptr_to_globals)
|
|
|
|
#define modinfo (G.modinfo )
|
2008-07-10 19:44:45 +05:30
|
|
|
#define dep_bb_seen (G.dep_bb_seen )
|
2008-07-10 19:46:11 +05:30
|
|
|
#define wrote_dep_bb_ok (G.wrote_dep_bb_ok )
|
2008-07-04 15:55:44 +05:30
|
|
|
#define module_count (G.module_count )
|
|
|
|
#define module_found_idx (G.module_found_idx )
|
|
|
|
#define module_load_options (G.module_load_options)
|
|
|
|
#define stringbuf_idx (G.stringbuf_idx )
|
2009-09-07 06:08:26 +05:30
|
|
|
#define stringbuf_size (G.stringbuf_size )
|
2008-07-04 15:55:44 +05:30
|
|
|
#define stringbuf (G.stringbuf )
|
|
|
|
#define INIT_G() do { \
|
|
|
|
SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
|
|
|
|
} while (0)
|
|
|
|
|
2009-09-07 06:08:26 +05:30
|
|
|
static void append(const char *s)
|
|
|
|
{
|
|
|
|
unsigned len = strlen(s);
|
|
|
|
if (stringbuf_idx + len + 15 > stringbuf_size) {
|
|
|
|
stringbuf_size = stringbuf_idx + len + 127;
|
|
|
|
dbg2_error_msg("grow stringbuf to %u", stringbuf_size);
|
|
|
|
stringbuf = xrealloc(stringbuf, stringbuf_size);
|
|
|
|
}
|
|
|
|
memcpy(stringbuf + stringbuf_idx, s, len);
|
|
|
|
stringbuf_idx += len;
|
|
|
|
}
|
2008-07-04 15:55:44 +05:30
|
|
|
|
|
|
|
static void appendc(char c)
|
|
|
|
{
|
2009-09-07 06:08:26 +05:30
|
|
|
/* We appendc() only after append(), + 15 trick in append()
|
|
|
|
* makes it unnecessary to check for overflow here */
|
|
|
|
stringbuf[stringbuf_idx++] = c;
|
2008-07-04 15:55:44 +05:30
|
|
|
}
|
|
|
|
|
2008-07-09 21:00:57 +05:30
|
|
|
static void bksp(void)
|
|
|
|
{
|
|
|
|
if (stringbuf_idx)
|
|
|
|
stringbuf_idx--;
|
|
|
|
}
|
|
|
|
|
2008-07-04 15:55:44 +05:30
|
|
|
static void reset_stringbuf(void)
|
|
|
|
{
|
|
|
|
stringbuf_idx = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char* copy_stringbuf(void)
|
|
|
|
{
|
2009-09-07 06:08:26 +05:30
|
|
|
char *copy = xzalloc(stringbuf_idx + 1); /* terminating NUL */
|
2008-07-04 15:55:44 +05:30
|
|
|
return memcpy(copy, stringbuf, stringbuf_idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char* find_keyword(char *ptr, size_t len, const char *word)
|
|
|
|
{
|
2008-08-05 18:40:34 +05:30
|
|
|
if (!ptr) /* happens if xmalloc_open_zipped_read_close cannot read it */
|
2008-07-04 15:55:44 +05:30
|
|
|
return NULL;
|
|
|
|
|
2015-03-12 22:18:34 +05:30
|
|
|
len -= strlen(word) - 1;
|
2008-07-04 15:55:44 +05:30
|
|
|
while ((ssize_t)len > 0) {
|
|
|
|
char *old = ptr;
|
2015-03-12 22:18:34 +05:30
|
|
|
char *after_word;
|
|
|
|
|
2008-07-04 15:55:44 +05:30
|
|
|
/* search for the first char in word */
|
2015-03-12 22:18:34 +05:30
|
|
|
ptr = memchr(ptr, word[0], len);
|
2017-04-17 19:43:32 +05:30
|
|
|
if (ptr == NULL) /* no occurrence left, done */
|
2008-07-04 15:55:44 +05:30
|
|
|
break;
|
2015-03-12 22:18:34 +05:30
|
|
|
after_word = is_prefixed_with(ptr, word);
|
|
|
|
if (after_word)
|
|
|
|
return after_word; /* found, return ptr past it */
|
2008-07-04 15:55:44 +05:30
|
|
|
++ptr;
|
|
|
|
len -= (ptr - old);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void replace(char *s, char what, char with)
|
|
|
|
{
|
|
|
|
while (*s) {
|
|
|
|
if (what == *s)
|
|
|
|
*s = with;
|
|
|
|
++s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-21 20:29:36 +05:30
|
|
|
static char *filename2modname(const char *filename, char *modname)
|
|
|
|
{
|
|
|
|
int i;
|
2015-01-01 23:32:40 +05:30
|
|
|
const char *from;
|
2014-04-21 20:29:36 +05:30
|
|
|
|
2015-01-01 23:32:40 +05:30
|
|
|
// Disabled since otherwise "modprobe dir/name" would work
|
|
|
|
// as if it is "modprobe name". It is unclear why
|
|
|
|
// 'basenamization' was here in the first place.
|
|
|
|
//from = bb_get_last_path_component_nostrip(filename);
|
|
|
|
from = filename;
|
2014-04-21 20:29:36 +05:30
|
|
|
for (i = 0; i < (MODULE_NAME_LEN-1) && from[i] != '\0' && from[i] != '.'; i++)
|
|
|
|
modname[i] = (from[i] == '-') ? '_' : from[i];
|
|
|
|
modname[i] = '\0';
|
|
|
|
|
|
|
|
return modname;
|
|
|
|
}
|
|
|
|
|
2015-01-11 22:16:56 +05:30
|
|
|
static int pathname_matches_modname(const char *pathname, const char *modname)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
char name[MODULE_NAME_LEN];
|
|
|
|
filename2modname(bb_get_last_path_component_nostrip(pathname), name);
|
|
|
|
r = (strcmp(name, modname) == 0);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2008-07-09 21:00:57 +05:30
|
|
|
/* Take "word word", return malloced "word",NUL,"word",NUL,NUL */
|
|
|
|
static char* str_2_list(const char *str)
|
|
|
|
{
|
|
|
|
int len = strlen(str) + 1;
|
|
|
|
char *dst = xmalloc(len + 1);
|
|
|
|
|
|
|
|
dst[len] = '\0';
|
|
|
|
memcpy(dst, str, len);
|
|
|
|
//TODO: protect against 2+ spaces: "word word"
|
|
|
|
replace(dst, ' ', '\0');
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2008-07-04 15:55:44 +05:30
|
|
|
/* We use error numbers in a loose translation... */
|
|
|
|
static const char *moderror(int err)
|
|
|
|
{
|
|
|
|
switch (err) {
|
|
|
|
case ENOEXEC:
|
|
|
|
return "invalid module format";
|
|
|
|
case ENOENT:
|
|
|
|
return "unknown symbol in module or invalid parameter";
|
|
|
|
case ESRCH:
|
|
|
|
return "module has wrong symbol version";
|
|
|
|
case EINVAL: /* "invalid parameter" */
|
|
|
|
return "unknown symbol in module or invalid parameter"
|
|
|
|
+ sizeof("unknown symbol in module or");
|
|
|
|
default:
|
|
|
|
return strerror(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int load_module(const char *fname, const char *options)
|
|
|
|
{
|
|
|
|
#if 1
|
|
|
|
int r;
|
|
|
|
size_t len = MAXINT(ssize_t);
|
|
|
|
char *module_image;
|
2016-09-15 15:46:33 +05:30
|
|
|
|
|
|
|
if (!options)
|
|
|
|
options = "";
|
|
|
|
|
2008-07-04 15:55:44 +05:30
|
|
|
dbg1_error_msg("load_module('%s','%s')", fname, options);
|
|
|
|
|
2016-09-15 15:46:33 +05:30
|
|
|
/*
|
|
|
|
* First we try finit_module if available. Some kernels are configured
|
|
|
|
* to only allow loading of modules off of secure storage (like a read-
|
|
|
|
* only rootfs) which needs the finit_module call. If it fails, we fall
|
|
|
|
* back to normal module loading to support compressed modules.
|
|
|
|
*/
|
|
|
|
r = 1;
|
|
|
|
# ifdef __NR_finit_module
|
|
|
|
{
|
|
|
|
int fd = open(fname, O_RDONLY | O_CLOEXEC);
|
|
|
|
if (fd >= 0) {
|
|
|
|
r = finit_module(fd, options, 0) != 0;
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
if (r != 0) {
|
|
|
|
module_image = xmalloc_open_zipped_read_close(fname, &len);
|
|
|
|
r = (!module_image || init_module(module_image, len, options) != 0);
|
|
|
|
free(module_image);
|
|
|
|
}
|
|
|
|
|
2008-07-04 15:55:44 +05:30
|
|
|
dbg1_error_msg("load_module:%d", r);
|
|
|
|
return r; /* 0 = success */
|
|
|
|
#else
|
|
|
|
/* For testing */
|
|
|
|
dbg1_error_msg("load_module('%s','%s')", fname, options);
|
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-06-20 14:29:06 +05:30
|
|
|
/* Returns !0 if open/read was unsuccessful */
|
|
|
|
static int parse_module(module_info *info, const char *pathname)
|
2008-07-04 15:55:44 +05:30
|
|
|
{
|
|
|
|
char *module_image;
|
|
|
|
char *ptr;
|
|
|
|
size_t len;
|
|
|
|
size_t pos;
|
2008-07-09 21:00:57 +05:30
|
|
|
dbg1_error_msg("parse_module('%s')", pathname);
|
2008-07-04 15:55:44 +05:30
|
|
|
|
|
|
|
/* Read (possibly compressed) module */
|
2016-06-20 14:29:06 +05:30
|
|
|
errno = 0;
|
2008-07-04 15:55:44 +05:30
|
|
|
len = 64 * 1024 * 1024; /* 64 Mb at most */
|
2008-08-05 18:40:34 +05:30
|
|
|
module_image = xmalloc_open_zipped_read_close(pathname, &len);
|
2011-01-17 19:28:27 +05:30
|
|
|
/* module_image == NULL is ok here, find_keyword handles it */
|
2008-07-10 19:44:20 +05:30
|
|
|
//TODO: optimize redundant module body reads
|
2008-07-04 15:55:44 +05:30
|
|
|
|
2008-07-10 19:44:20 +05:30
|
|
|
/* "alias1 symbol:sym1 alias2 symbol:sym2" */
|
2008-07-04 15:55:44 +05:30
|
|
|
reset_stringbuf();
|
|
|
|
pos = 0;
|
|
|
|
while (1) {
|
2014-04-19 19:47:27 +05:30
|
|
|
unsigned start = stringbuf_idx;
|
2008-07-04 15:55:44 +05:30
|
|
|
ptr = find_keyword(module_image + pos, len - pos, "alias=");
|
|
|
|
if (!ptr) {
|
|
|
|
ptr = find_keyword(module_image + pos, len - pos, "__ksymtab_");
|
|
|
|
if (!ptr)
|
|
|
|
break;
|
|
|
|
/* DOCME: __ksymtab_gpl and __ksymtab_strings occur
|
|
|
|
* in many modules. What do they mean? */
|
2008-07-10 19:46:11 +05:30
|
|
|
if (strcmp(ptr, "gpl") == 0 || strcmp(ptr, "strings") == 0)
|
|
|
|
goto skip;
|
|
|
|
dbg2_error_msg("alias:'symbol:%s'", ptr);
|
|
|
|
append("symbol:");
|
2008-07-04 15:55:44 +05:30
|
|
|
} else {
|
2008-07-09 21:00:57 +05:30
|
|
|
dbg2_error_msg("alias:'%s'", ptr);
|
2008-07-04 15:55:44 +05:30
|
|
|
}
|
|
|
|
append(ptr);
|
|
|
|
appendc(' ');
|
2014-04-19 19:47:27 +05:30
|
|
|
/*
|
|
|
|
* Don't add redundant aliases, such as:
|
|
|
|
* libcrc32c.ko symbol:crc32c symbol:crc32c
|
|
|
|
*/
|
|
|
|
if (start) { /* "if we aren't the first alias" */
|
|
|
|
char *found, *last;
|
|
|
|
stringbuf[stringbuf_idx] = '\0';
|
|
|
|
last = stringbuf + start;
|
|
|
|
/*
|
|
|
|
* String at last-1 is " symbol:crc32c "
|
|
|
|
* (with both leading and trailing spaces).
|
|
|
|
*/
|
|
|
|
if (strncmp(stringbuf, last, stringbuf_idx - start) == 0)
|
|
|
|
/* First alias matches us */
|
|
|
|
found = stringbuf;
|
|
|
|
else
|
|
|
|
/* Does any other alias match? */
|
|
|
|
found = strstr(stringbuf, last-1);
|
|
|
|
if (found < last-1) {
|
|
|
|
/* There is absolutely the same string before us */
|
|
|
|
dbg2_error_msg("redundant:'%s'", last);
|
|
|
|
stringbuf_idx = start;
|
|
|
|
goto skip;
|
|
|
|
}
|
|
|
|
}
|
2008-07-10 19:46:11 +05:30
|
|
|
skip:
|
2008-07-04 15:55:44 +05:30
|
|
|
pos = (ptr - module_image);
|
|
|
|
}
|
2008-07-09 21:00:57 +05:30
|
|
|
bksp(); /* remove last ' ' */
|
2008-07-10 19:44:20 +05:30
|
|
|
info->aliases = copy_stringbuf();
|
2009-09-07 06:07:19 +05:30
|
|
|
replace(info->aliases, '-', '_');
|
2008-07-04 15:55:44 +05:30
|
|
|
|
2008-07-10 19:44:20 +05:30
|
|
|
/* "dependency1 depandency2" */
|
|
|
|
reset_stringbuf();
|
2008-07-04 15:55:44 +05:30
|
|
|
ptr = find_keyword(module_image, len, "depends=");
|
|
|
|
if (ptr && *ptr) {
|
|
|
|
replace(ptr, ',', ' ');
|
|
|
|
replace(ptr, '-', '_');
|
|
|
|
dbg2_error_msg("dep:'%s'", ptr);
|
|
|
|
append(ptr);
|
|
|
|
}
|
2016-06-20 14:29:06 +05:30
|
|
|
free(module_image);
|
2008-07-10 19:44:20 +05:30
|
|
|
info->deps = copy_stringbuf();
|
2008-07-04 15:55:44 +05:30
|
|
|
|
2016-06-20 14:29:06 +05:30
|
|
|
info->open_read_failed = (module_image == NULL);
|
|
|
|
return info->open_read_failed;
|
2008-07-04 15:55:44 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static FAST_FUNC int fileAction(const char *pathname,
|
2008-07-05 14:48:54 +05:30
|
|
|
struct stat *sb UNUSED_PARAM,
|
2008-07-09 21:00:57 +05:30
|
|
|
void *modname_to_match,
|
2008-07-05 14:48:54 +05:30
|
|
|
int depth UNUSED_PARAM)
|
2008-07-04 15:55:44 +05:30
|
|
|
{
|
|
|
|
int cur;
|
|
|
|
const char *fname;
|
2017-01-09 12:34:43 +05:30
|
|
|
bool is_remove = (ENABLE_RMMOD && ONLY_APPLET)
|
|
|
|
|| ((ENABLE_RMMOD || ENABLE_MODPROBE) && (option_mask32 & OPT_r));
|
2008-07-04 15:55:44 +05:30
|
|
|
|
|
|
|
pathname += 2; /* skip "./" */
|
|
|
|
fname = bb_get_last_path_component_nostrip(pathname);
|
|
|
|
if (!strrstr(fname, ".ko")) {
|
|
|
|
dbg1_error_msg("'%s' is not a module", pathname);
|
|
|
|
return TRUE; /* not a module, continue search */
|
|
|
|
}
|
|
|
|
|
|
|
|
cur = module_count++;
|
2008-07-08 10:44:36 +05:30
|
|
|
modinfo = xrealloc_vector(modinfo, 12, cur);
|
2008-07-04 15:55:44 +05:30
|
|
|
modinfo[cur].pathname = xstrdup(pathname);
|
2008-08-04 18:50:36 +05:30
|
|
|
/*modinfo[cur].aliases = NULL; - xrealloc_vector did it */
|
|
|
|
/*modinfo[cur+1].pathname = NULL;*/
|
2008-07-04 15:55:44 +05:30
|
|
|
|
2008-07-09 21:00:57 +05:30
|
|
|
if (!pathname_matches_modname(fname, modname_to_match)) {
|
2008-07-04 15:55:44 +05:30
|
|
|
dbg1_error_msg("'%s' module name doesn't match", pathname);
|
|
|
|
return TRUE; /* module name doesn't match, continue search */
|
|
|
|
}
|
|
|
|
|
|
|
|
dbg1_error_msg("'%s' module name matches", pathname);
|
|
|
|
module_found_idx = cur;
|
2016-06-20 14:29:06 +05:30
|
|
|
if (parse_module(&modinfo[cur], pathname) != 0)
|
|
|
|
return TRUE; /* failed to open/read it, no point in trying loading */
|
2008-07-04 15:55:44 +05:30
|
|
|
|
2017-01-09 12:34:43 +05:30
|
|
|
if (!is_remove) {
|
2008-07-04 15:55:44 +05:30
|
|
|
if (load_module(pathname, module_load_options) == 0) {
|
|
|
|
/* Load was successful, there is nothing else to do.
|
|
|
|
* This can happen ONLY for "top-level" module load,
|
2017-04-17 19:43:32 +05:30
|
|
|
* not a dep, because deps don't do dirscan. */
|
2008-07-04 15:55:44 +05:30
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2008-07-10 19:44:45 +05:30
|
|
|
static int load_dep_bb(void)
|
2008-07-10 19:44:20 +05:30
|
|
|
{
|
|
|
|
char *line;
|
2008-07-22 04:35:26 +05:30
|
|
|
FILE *fp = fopen_for_read(DEPFILE_BB);
|
2008-07-10 19:44:20 +05:30
|
|
|
|
|
|
|
if (!fp)
|
2008-07-10 19:44:45 +05:30
|
|
|
return 0;
|
|
|
|
|
|
|
|
dep_bb_seen = 1;
|
|
|
|
dbg1_error_msg("loading "DEPFILE_BB);
|
|
|
|
|
|
|
|
/* Why? There is a rare scenario: we did not find modprobe.dep.bb,
|
|
|
|
* we scanned the dir and found no module by name, then we search
|
|
|
|
* for alias (full scan), and we decided to generate modprobe.dep.bb.
|
|
|
|
* But we see modprobe.dep.bb.new! Other modprobe is at work!
|
|
|
|
* We wait and other modprobe renames it to modprobe.dep.bb.
|
|
|
|
* Now we can use it.
|
|
|
|
* But we already have modinfo[] filled, and "module_count = 0"
|
|
|
|
* makes us start anew. Yes, we leak modinfo[].xxx pointers -
|
|
|
|
* there is not much of data there anyway. */
|
|
|
|
module_count = 0;
|
|
|
|
memset(&modinfo[0], 0, sizeof(modinfo[0]));
|
2008-07-10 19:44:20 +05:30
|
|
|
|
|
|
|
while ((line = xmalloc_fgetline(fp)) != NULL) {
|
|
|
|
char* space;
|
2009-09-06 06:06:23 +05:30
|
|
|
char* linebuf;
|
2008-07-10 19:44:20 +05:30
|
|
|
int cur;
|
|
|
|
|
|
|
|
if (!line[0]) {
|
|
|
|
free(line);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
space = strchrnul(line, ' ');
|
|
|
|
cur = module_count++;
|
|
|
|
modinfo = xrealloc_vector(modinfo, 12, cur);
|
2008-08-04 18:50:36 +05:30
|
|
|
/*modinfo[cur+1].pathname = NULL; - xrealloc_vector did it */
|
2008-07-10 19:44:20 +05:30
|
|
|
modinfo[cur].pathname = line; /* we take ownership of malloced block here */
|
|
|
|
if (*space)
|
|
|
|
*space++ = '\0';
|
|
|
|
modinfo[cur].aliases = space;
|
2009-09-06 06:06:23 +05:30
|
|
|
linebuf = xmalloc_fgetline(fp);
|
|
|
|
modinfo[cur].deps = linebuf ? linebuf : xzalloc(1);
|
2008-07-10 19:44:20 +05:30
|
|
|
if (modinfo[cur].deps[0]) {
|
|
|
|
/* deps are not "", so next line must be empty */
|
|
|
|
line = xmalloc_fgetline(fp);
|
|
|
|
/* Refuse to work with damaged config file */
|
|
|
|
if (line && line[0])
|
|
|
|
bb_error_msg_and_die("error in %s at '%s'", DEPFILE_BB, line);
|
|
|
|
free(line);
|
|
|
|
}
|
|
|
|
}
|
2008-07-10 19:44:45 +05:30
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int start_dep_bb_writeout(void)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2008-07-10 19:46:11 +05:30
|
|
|
/* depmod -n: write result to stdout */
|
2017-02-09 20:18:52 +05:30
|
|
|
if (is_depmod && (option_mask32 & DEPMOD_OPT_n))
|
2008-07-10 19:46:11 +05:30
|
|
|
return STDOUT_FILENO;
|
|
|
|
|
2008-07-10 19:44:45 +05:30
|
|
|
fd = open(DEPFILE_BB".new", O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0644);
|
|
|
|
if (fd < 0) {
|
|
|
|
if (errno == EEXIST) {
|
|
|
|
int count = 5 * 20;
|
|
|
|
dbg1_error_msg(DEPFILE_BB".new exists, waiting for "DEPFILE_BB);
|
|
|
|
while (1) {
|
|
|
|
usleep(1000*1000 / 20);
|
|
|
|
if (load_dep_bb()) {
|
|
|
|
dbg1_error_msg(DEPFILE_BB" appeared");
|
|
|
|
return -2; /* magic number */
|
|
|
|
}
|
|
|
|
if (!--count)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
bb_error_msg("deleting stale %s", DEPFILE_BB".new");
|
|
|
|
fd = open_or_warn(DEPFILE_BB".new", O_WRONLY | O_CREAT | O_TRUNC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dbg1_error_msg("opened "DEPFILE_BB".new:%d", fd);
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void write_out_dep_bb(int fd)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
/* We want good error reporting. fdprintf is not good enough. */
|
2009-11-16 03:58:11 +05:30
|
|
|
fp = xfdopen_for_write(fd);
|
2008-07-10 19:44:45 +05:30
|
|
|
i = 0;
|
|
|
|
while (modinfo[i].pathname) {
|
|
|
|
fprintf(fp, "%s%s%s\n" "%s%s\n",
|
|
|
|
modinfo[i].pathname, modinfo[i].aliases[0] ? " " : "", modinfo[i].aliases,
|
|
|
|
modinfo[i].deps, modinfo[i].deps[0] ? "\n" : "");
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
/* Badly formatted depfile is a no-no. Be paranoid. */
|
|
|
|
errno = 0;
|
2008-07-10 19:46:11 +05:30
|
|
|
if (ferror(fp) | fclose(fp)) /* | instead of || is intended */
|
2008-07-10 19:44:45 +05:30
|
|
|
goto err;
|
2008-07-10 19:46:11 +05:30
|
|
|
|
|
|
|
if (fd == STDOUT_FILENO) /* it was depmod -n */
|
|
|
|
goto ok;
|
|
|
|
|
2008-07-10 19:44:45 +05:30
|
|
|
if (rename(DEPFILE_BB".new", DEPFILE_BB) != 0) {
|
|
|
|
err:
|
2010-03-23 20:55:17 +05:30
|
|
|
bb_perror_msg("can't create '%s'", DEPFILE_BB);
|
2008-07-10 19:44:45 +05:30
|
|
|
unlink(DEPFILE_BB".new");
|
|
|
|
} else {
|
2008-07-10 19:46:11 +05:30
|
|
|
ok:
|
|
|
|
wrote_dep_bb_ok = 1;
|
2008-07-10 19:44:45 +05:30
|
|
|
dbg1_error_msg("created "DEPFILE_BB);
|
|
|
|
}
|
2008-07-10 19:44:20 +05:30
|
|
|
}
|
|
|
|
|
2014-04-21 20:29:36 +05:30
|
|
|
static module_info** find_alias(const char *alias)
|
2008-07-04 15:55:44 +05:30
|
|
|
{
|
|
|
|
int i;
|
2008-07-10 19:44:45 +05:30
|
|
|
int dep_bb_fd;
|
2014-04-21 20:29:36 +05:30
|
|
|
int infoidx;
|
|
|
|
module_info **infovec;
|
2008-07-04 15:55:44 +05:30
|
|
|
dbg1_error_msg("find_alias('%s')", alias);
|
|
|
|
|
2008-07-10 19:44:45 +05:30
|
|
|
try_again:
|
2008-07-04 15:55:44 +05:30
|
|
|
/* First try to find by name (cheaper) */
|
|
|
|
i = 0;
|
|
|
|
while (modinfo[i].pathname) {
|
2008-07-09 21:00:57 +05:30
|
|
|
if (pathname_matches_modname(modinfo[i].pathname, alias)) {
|
2008-07-04 15:55:44 +05:30
|
|
|
dbg1_error_msg("found '%s' in module '%s'",
|
|
|
|
alias, modinfo[i].pathname);
|
2008-07-10 19:44:20 +05:30
|
|
|
if (!modinfo[i].aliases) {
|
|
|
|
parse_module(&modinfo[i], modinfo[i].pathname);
|
|
|
|
}
|
2014-04-21 20:29:36 +05:30
|
|
|
infovec = xzalloc(2 * sizeof(infovec[0]));
|
|
|
|
infovec[0] = &modinfo[i];
|
|
|
|
return infovec;
|
2008-07-04 15:55:44 +05:30
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2008-07-10 19:44:45 +05:30
|
|
|
/* Ok, we definitely have to scan module bodies. This is a good
|
|
|
|
* moment to generate modprobe.dep.bb, if it does not exist yet */
|
|
|
|
dep_bb_fd = dep_bb_seen ? -1 : start_dep_bb_writeout();
|
|
|
|
if (dep_bb_fd == -2) /* modprobe.dep.bb appeared? */
|
|
|
|
goto try_again;
|
|
|
|
|
2008-07-04 15:55:44 +05:30
|
|
|
/* Scan all module bodies, extract modinfo (it contains aliases) */
|
|
|
|
i = 0;
|
2014-04-21 20:29:36 +05:30
|
|
|
infoidx = 0;
|
|
|
|
infovec = NULL;
|
2008-07-04 15:55:44 +05:30
|
|
|
while (modinfo[i].pathname) {
|
|
|
|
char *desc, *s;
|
2008-07-10 19:44:20 +05:30
|
|
|
if (!modinfo[i].aliases) {
|
|
|
|
parse_module(&modinfo[i], modinfo[i].pathname);
|
2008-07-04 15:55:44 +05:30
|
|
|
}
|
2008-07-09 21:00:57 +05:30
|
|
|
/* "alias1 symbol:sym1 alias2 symbol:sym2" */
|
2008-07-10 19:44:20 +05:30
|
|
|
desc = str_2_list(modinfo[i].aliases);
|
2008-07-04 15:55:44 +05:30
|
|
|
/* Does matching substring exist? */
|
|
|
|
for (s = desc; *s; s += strlen(s) + 1) {
|
2008-07-09 21:00:57 +05:30
|
|
|
/* Aliases in module bodies can be defined with
|
2008-07-06 17:22:23 +05:30
|
|
|
* shell patterns. Example:
|
|
|
|
* "pci:v000010DEd000000D9sv*sd*bc*sc*i*".
|
|
|
|
* Plain strcmp() won't catch that */
|
|
|
|
if (fnmatch(s, alias, 0) == 0) {
|
2008-07-04 15:55:44 +05:30
|
|
|
dbg1_error_msg("found alias '%s' in module '%s'",
|
|
|
|
alias, modinfo[i].pathname);
|
2014-04-21 20:29:36 +05:30
|
|
|
infovec = xrealloc_vector(infovec, 1, infoidx);
|
|
|
|
infovec[infoidx++] = &modinfo[i];
|
2008-07-10 19:44:45 +05:30
|
|
|
break;
|
2008-07-04 15:55:44 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
free(desc);
|
|
|
|
i++;
|
|
|
|
}
|
2008-07-10 19:44:45 +05:30
|
|
|
|
|
|
|
/* Create module.dep.bb if needed */
|
|
|
|
if (dep_bb_fd >= 0) {
|
|
|
|
write_out_dep_bb(dep_bb_fd);
|
|
|
|
}
|
|
|
|
|
2014-04-21 20:29:36 +05:30
|
|
|
dbg1_error_msg("find_alias '%s' returns %d results", alias, infoidx);
|
|
|
|
return infovec;
|
2008-07-04 15:55:44 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#if ENABLE_FEATURE_MODPROBE_SMALL_CHECK_ALREADY_LOADED
|
2008-07-25 05:08:04 +05:30
|
|
|
// TODO: open only once, invent config_rewind()
|
2008-07-04 15:55:44 +05:30
|
|
|
static int already_loaded(const char *name)
|
|
|
|
{
|
2015-03-12 22:18:34 +05:30
|
|
|
int ret;
|
2015-02-12 20:48:39 +05:30
|
|
|
char *line;
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
ret = 5 * 2;
|
|
|
|
again:
|
|
|
|
fp = fopen_for_read("/proc/modules");
|
|
|
|
if (!fp)
|
|
|
|
return 0;
|
|
|
|
while ((line = xmalloc_fgetline(fp)) != NULL) {
|
|
|
|
char *live;
|
2015-03-12 22:18:34 +05:30
|
|
|
char *after_name;
|
2015-02-12 20:48:39 +05:30
|
|
|
|
|
|
|
// Examples from kernel 3.14.6:
|
|
|
|
//pcspkr 12718 0 - Live 0xffffffffa017e000
|
|
|
|
//snd_timer 28690 2 snd_seq,snd_pcm, Live 0xffffffffa025e000
|
|
|
|
//i915 801405 2 - Live 0xffffffffa0096000
|
2015-03-12 22:18:34 +05:30
|
|
|
after_name = is_prefixed_with(line, name);
|
|
|
|
if (!after_name || *after_name != ' ') {
|
2015-02-12 20:48:39 +05:30
|
|
|
free(line);
|
|
|
|
continue;
|
2008-07-04 15:55:44 +05:30
|
|
|
}
|
2015-02-12 20:48:39 +05:30
|
|
|
live = strstr(line, " Live");
|
|
|
|
free(line);
|
|
|
|
if (!live) {
|
|
|
|
/* State can be Unloading, Loading, or Live.
|
|
|
|
* modprobe must not return prematurely if we see "Loading":
|
|
|
|
* it can cause further programs to assume load completed,
|
|
|
|
* but it did not (yet)!
|
|
|
|
* Wait up to 5*20 ms for it to resolve.
|
|
|
|
*/
|
|
|
|
ret -= 2;
|
|
|
|
if (ret == 0)
|
|
|
|
break; /* huh? report as "not loaded" */
|
|
|
|
fclose(fp);
|
|
|
|
usleep(20*1000);
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
ret = 1;
|
|
|
|
break;
|
2008-07-04 15:55:44 +05:30
|
|
|
}
|
2015-02-12 20:48:39 +05:30
|
|
|
fclose(fp);
|
|
|
|
|
2015-02-13 16:23:33 +05:30
|
|
|
return ret & 1;
|
2008-07-04 15:55:44 +05:30
|
|
|
}
|
|
|
|
#else
|
2015-02-08 01:14:46 +05:30
|
|
|
#define already_loaded(name) 0
|
2008-07-04 15:55:44 +05:30
|
|
|
#endif
|
|
|
|
|
2015-02-08 01:14:46 +05:30
|
|
|
static int rmmod(const char *filename)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
char modname[MODULE_NAME_LEN];
|
|
|
|
|
|
|
|
filename2modname(filename, modname);
|
|
|
|
r = delete_module(modname, O_NONBLOCK | O_EXCL);
|
|
|
|
dbg1_error_msg("delete_module('%s', O_NONBLOCK | O_EXCL):%d", modname, r);
|
|
|
|
if (r != 0 && !(option_mask32 & OPT_q)) {
|
|
|
|
bb_perror_msg("remove '%s'", modname);
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2008-07-04 15:55:44 +05:30
|
|
|
/*
|
2008-07-10 19:46:11 +05:30
|
|
|
* Given modules definition and module name (or alias, or symbol)
|
|
|
|
* load/remove the module respecting dependencies.
|
|
|
|
* NB: also called by depmod with bogus name "/",
|
|
|
|
* just in order to force modprobe.dep.bb creation.
|
2008-07-04 15:55:44 +05:30
|
|
|
*/
|
2017-01-31 18:39:17 +05:30
|
|
|
#if !ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS
|
2008-07-04 15:55:44 +05:30
|
|
|
#define process_module(a,b) process_module(a)
|
|
|
|
#define cmdline_options ""
|
|
|
|
#endif
|
2016-06-20 14:29:06 +05:30
|
|
|
static int process_module(char *name, const char *cmdline_options)
|
2008-07-04 15:55:44 +05:30
|
|
|
{
|
|
|
|
char *s, *deps, *options;
|
2014-04-21 20:29:36 +05:30
|
|
|
module_info **infovec;
|
2008-07-04 15:55:44 +05:30
|
|
|
module_info *info;
|
2014-04-21 20:29:36 +05:30
|
|
|
int infoidx;
|
2017-01-09 12:34:43 +05:30
|
|
|
bool is_remove = (ENABLE_RMMOD && ONLY_APPLET)
|
|
|
|
|| ((ENABLE_RMMOD || ENABLE_MODPROBE) && (option_mask32 & OPT_r));
|
2016-06-20 14:29:06 +05:30
|
|
|
int exitcode = EXIT_SUCCESS;
|
2014-04-21 20:29:36 +05:30
|
|
|
|
2008-07-04 15:55:44 +05:30
|
|
|
dbg1_error_msg("process_module('%s','%s')", name, cmdline_options);
|
|
|
|
|
|
|
|
replace(name, '-', '_');
|
|
|
|
|
2015-02-08 01:14:46 +05:30
|
|
|
dbg1_error_msg("already_loaded:%d is_remove:%d", already_loaded(name), is_remove);
|
|
|
|
|
2017-01-09 12:34:43 +05:30
|
|
|
if (is_rmmod) {
|
|
|
|
/* Does not remove dependencies, no need to scan, just remove.
|
2015-02-08 01:14:46 +05:30
|
|
|
* (compat note: this allows and strips .ko suffix)
|
|
|
|
*/
|
|
|
|
rmmod(name);
|
2016-06-20 14:29:06 +05:30
|
|
|
return EXIT_SUCCESS;
|
2015-02-08 01:14:46 +05:30
|
|
|
}
|
|
|
|
|
2014-04-21 20:29:36 +05:30
|
|
|
/*
|
2015-02-08 01:14:46 +05:30
|
|
|
* We used to have "is_remove != already_loaded(name)" check here, but
|
2014-04-21 20:29:36 +05:30
|
|
|
* modprobe -r pci:v00008086d00007010sv00000000sd00000000bc01sc01i80
|
|
|
|
* won't unload modules (there are more than one)
|
|
|
|
* which have this alias.
|
|
|
|
*/
|
2015-02-08 01:14:46 +05:30
|
|
|
if (!is_remove && already_loaded(name)) {
|
2008-07-04 15:55:44 +05:30
|
|
|
dbg1_error_msg("nothing to do for '%s'", name);
|
2016-06-20 14:29:06 +05:30
|
|
|
return EXIT_SUCCESS;
|
2008-07-04 15:55:44 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
options = NULL;
|
2015-02-08 01:14:46 +05:30
|
|
|
if (!is_remove) {
|
2008-07-04 15:55:44 +05:30
|
|
|
char *opt_filename = xasprintf("/etc/modules/%s", name);
|
|
|
|
options = xmalloc_open_read_close(opt_filename, NULL);
|
|
|
|
if (options)
|
|
|
|
replace(options, '\n', ' ');
|
2017-01-31 18:39:17 +05:30
|
|
|
#if ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS
|
2008-07-04 15:55:44 +05:30
|
|
|
if (cmdline_options) {
|
|
|
|
/* NB: cmdline_options always have one leading ' '
|
|
|
|
* (see main()), we remove it here */
|
|
|
|
char *op = xasprintf(options ? "%s %s" : "%s %s" + 3,
|
|
|
|
cmdline_options + 1, options);
|
|
|
|
free(options);
|
|
|
|
options = op;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
free(opt_filename);
|
|
|
|
module_load_options = options;
|
|
|
|
dbg1_error_msg("process_module('%s'): options:'%s'", name, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!module_count) {
|
|
|
|
/* Scan module directory. This is done only once.
|
|
|
|
* It will attempt module load, and will exit(EXIT_SUCCESS)
|
2014-04-21 20:29:36 +05:30
|
|
|
* on success.
|
|
|
|
*/
|
2008-07-04 15:55:44 +05:30
|
|
|
module_found_idx = -1;
|
|
|
|
recursive_action(".",
|
|
|
|
ACTION_RECURSE, /* flags */
|
|
|
|
fileAction, /* file action */
|
|
|
|
NULL, /* dir action */
|
2020-10-02 01:22:16 +05:30
|
|
|
name /* user data */
|
2014-04-21 20:29:36 +05:30
|
|
|
);
|
2008-07-04 15:55:44 +05:30
|
|
|
dbg1_error_msg("dirscan complete");
|
2015-02-08 01:14:46 +05:30
|
|
|
/* Module was not found, or load failed, or is_remove */
|
2008-07-04 15:55:44 +05:30
|
|
|
if (module_found_idx >= 0) { /* module was found */
|
2014-04-21 20:29:36 +05:30
|
|
|
infovec = xzalloc(2 * sizeof(infovec[0]));
|
|
|
|
infovec[0] = &modinfo[module_found_idx];
|
2008-07-04 15:55:44 +05:30
|
|
|
} else { /* search for alias, not a plain module name */
|
2014-04-21 20:29:36 +05:30
|
|
|
infovec = find_alias(name);
|
2008-07-04 15:55:44 +05:30
|
|
|
}
|
|
|
|
} else {
|
2014-04-21 20:29:36 +05:30
|
|
|
infovec = find_alias(name);
|
2008-07-04 15:55:44 +05:30
|
|
|
}
|
|
|
|
|
2015-01-11 22:10:30 +05:30
|
|
|
if (!infovec) {
|
|
|
|
/* both dirscan and find_alias found nothing */
|
2017-04-06 17:15:00 +05:30
|
|
|
if (!is_remove && !is_depmod) { /* it wasn't rmmod or depmod */
|
2015-01-11 22:10:30 +05:30
|
|
|
bb_error_msg("module '%s' not found", name);
|
|
|
|
//TODO: _and_die()? or should we continue (un)loading modules listed on cmdline?
|
2017-04-06 17:15:00 +05:30
|
|
|
/* "modprobe non-existing-module; echo $?" must print 1 */
|
|
|
|
exitcode = EXIT_FAILURE;
|
|
|
|
}
|
2015-01-11 22:10:30 +05:30
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
|
2014-04-21 20:29:36 +05:30
|
|
|
/* There can be more than one module for the given alias. For example,
|
|
|
|
* "pci:v00008086d00007010sv00000000sd00000000bc01sc01i80" matches
|
|
|
|
* ata_piix because it has alias "pci:v00008086d00007010sv*sd*bc*sc*i*"
|
|
|
|
* and ata_generic, it has alias "pci:v*d*sv*sd*bc01sc01i*"
|
|
|
|
* Standard modprobe loads them both. We achieve it by returning
|
|
|
|
* a *list* of modinfo pointers from find_alias().
|
|
|
|
*/
|
2009-10-09 02:24:41 +05:30
|
|
|
|
2015-02-08 01:14:46 +05:30
|
|
|
/* modprobe -r? unload module(s) */
|
|
|
|
if (is_remove) {
|
2014-04-21 20:29:36 +05:30
|
|
|
infoidx = 0;
|
|
|
|
while ((info = infovec[infoidx++]) != NULL) {
|
2015-02-08 01:14:46 +05:30
|
|
|
int r = rmmod(bb_get_last_path_component_nostrip(info->pathname));
|
2014-04-21 20:29:36 +05:30
|
|
|
if (r != 0) {
|
2015-02-08 01:14:46 +05:30
|
|
|
goto ret; /* error */
|
2014-04-21 20:29:36 +05:30
|
|
|
}
|
2008-07-04 15:55:44 +05:30
|
|
|
}
|
2012-09-26 00:07:38 +05:30
|
|
|
/* modprobe -r: we do not stop here -
|
2008-07-04 15:55:44 +05:30
|
|
|
* continue to unload modules on which the module depends:
|
|
|
|
* "-r --remove: option causes modprobe to remove a module.
|
|
|
|
* If the modules it depends on are also unused, modprobe
|
2012-09-26 00:07:38 +05:30
|
|
|
* will try to remove them, too."
|
|
|
|
*/
|
2008-07-04 15:55:44 +05:30
|
|
|
}
|
|
|
|
|
2014-04-21 20:29:36 +05:30
|
|
|
infoidx = 0;
|
|
|
|
while ((info = infovec[infoidx++]) != NULL) {
|
|
|
|
/* Iterate thru dependencies, trying to (un)load them */
|
|
|
|
deps = str_2_list(info->deps);
|
|
|
|
for (s = deps; *s; s += strlen(s) + 1) {
|
|
|
|
//if (strcmp(name, s) != 0) // N.B. do loops exist?
|
|
|
|
dbg1_error_msg("recurse on dep '%s'", s);
|
|
|
|
process_module(s, NULL);
|
|
|
|
dbg1_error_msg("recurse on dep '%s' done", s);
|
|
|
|
}
|
|
|
|
free(deps);
|
2008-07-04 15:55:44 +05:30
|
|
|
|
2015-02-08 01:14:46 +05:30
|
|
|
if (is_remove)
|
2014-04-21 20:29:36 +05:30
|
|
|
continue;
|
|
|
|
|
|
|
|
/* We are modprobe: load it */
|
|
|
|
if (options && strstr(options, "blacklist")) {
|
2008-11-12 05:39:58 +05:30
|
|
|
dbg1_error_msg("'%s': blacklisted", info->pathname);
|
2014-04-21 20:29:36 +05:30
|
|
|
continue;
|
|
|
|
}
|
2016-06-20 14:29:06 +05:30
|
|
|
if (info->open_read_failed) {
|
|
|
|
/* We already tried it, didn't work. Don't try load again */
|
|
|
|
exitcode = EXIT_FAILURE;
|
|
|
|
continue;
|
|
|
|
}
|
2014-04-21 20:29:36 +05:30
|
|
|
errno = 0;
|
|
|
|
if (load_module(info->pathname, options) != 0) {
|
|
|
|
if (EEXIST != errno) {
|
|
|
|
bb_error_msg("'%s': %s",
|
|
|
|
info->pathname,
|
|
|
|
moderror(errno));
|
|
|
|
} else {
|
|
|
|
dbg1_error_msg("'%s': %s",
|
|
|
|
info->pathname,
|
|
|
|
moderror(errno));
|
|
|
|
}
|
2016-06-20 14:29:06 +05:30
|
|
|
exitcode = EXIT_FAILURE;
|
2008-07-04 15:55:44 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
ret:
|
2014-04-21 20:29:36 +05:30
|
|
|
free(infovec);
|
2008-07-04 15:55:44 +05:30
|
|
|
free(options);
|
2016-06-20 14:29:06 +05:30
|
|
|
|
|
|
|
return exitcode;
|
2008-07-04 15:55:44 +05:30
|
|
|
}
|
|
|
|
#undef cmdline_options
|
|
|
|
|
|
|
|
|
2008-07-10 19:46:11 +05:30
|
|
|
/* For reference, module-init-tools v3.4 options:
|
2008-07-04 15:55:44 +05:30
|
|
|
|
|
|
|
# insmod
|
|
|
|
Usage: insmod filename [args]
|
|
|
|
|
|
|
|
# rmmod --help
|
|
|
|
Usage: rmmod [-fhswvV] modulename ...
|
2008-07-10 19:46:11 +05:30
|
|
|
-f (or --force) forces a module unload, and may crash your
|
|
|
|
machine. This requires the Forced Module Removal option
|
|
|
|
when the kernel was compiled.
|
|
|
|
-h (or --help) prints this help text
|
2008-07-04 15:55:44 +05:30
|
|
|
-s (or --syslog) says use syslog, not stderr
|
|
|
|
-v (or --verbose) enables more messages
|
2008-07-10 19:46:11 +05:30
|
|
|
-V (or --version) prints the version code
|
2008-08-05 13:29:25 +05:30
|
|
|
-w (or --wait) begins module removal even if it is used
|
2008-07-04 15:55:44 +05:30
|
|
|
and will stop new users from accessing the module (so it
|
|
|
|
should eventually fall to zero).
|
|
|
|
|
|
|
|
# modprobe
|
2017-02-09 20:18:51 +05:30
|
|
|
Usage: modprobe [-v] [-V] [-C config-file] [-d <dirname> ] [-n] [-i] [-q]
|
|
|
|
[-b] [-o <modname>] [ --dump-modversions ] <modname> [parameters...]
|
2008-07-10 19:46:11 +05:30
|
|
|
modprobe -r [-n] [-i] [-v] <modulename> ...
|
|
|
|
modprobe -l -t <dirname> [ -a <modulename> ...]
|
2008-07-04 15:55:44 +05:30
|
|
|
|
|
|
|
# depmod --help
|
2017-02-09 20:18:51 +05:30
|
|
|
depmod 3.13 -- part of module-init-tools
|
|
|
|
depmod -[aA] [-n -e -v -q -V -r -u -w -m]
|
2008-07-10 19:46:11 +05:30
|
|
|
[-b basedirectory] [forced_version]
|
2017-02-09 20:18:51 +05:30
|
|
|
depmod [-n -e -v -q -r -u -w] [-F kernelsyms] module1.ko module2.ko ...
|
2008-07-10 19:46:11 +05:30
|
|
|
If no arguments (except options) are given, "depmod -a" is assumed.
|
2009-05-25 07:45:37 +05:30
|
|
|
depmod will output a dependency list suitable for the modprobe utility.
|
2008-07-04 15:55:44 +05:30
|
|
|
Options:
|
2008-08-05 13:29:25 +05:30
|
|
|
-a, --all Probe all modules
|
|
|
|
-A, --quick Only does the work if there's a new module
|
|
|
|
-e, --errsyms Report not supplied symbols
|
2017-02-09 20:18:51 +05:30
|
|
|
-m, --map Create the legacy map files
|
|
|
|
-n, --show Write the dependency file on stdout only
|
|
|
|
-P, --symbol-prefix Architecture symbol prefix
|
2008-08-05 13:29:25 +05:30
|
|
|
-V, --version Print the release version
|
|
|
|
-v, --verbose Enable verbose mode
|
2017-02-09 20:18:51 +05:30
|
|
|
-w, --warn Warn on duplicates
|
2008-08-05 13:29:25 +05:30
|
|
|
-h, --help Print this usage message
|
2008-07-10 19:46:11 +05:30
|
|
|
The following options are useful for people managing distributions:
|
|
|
|
-b basedirectory
|
2008-08-05 13:29:25 +05:30
|
|
|
--basedir basedirectory
|
|
|
|
Use an image of a module tree
|
2008-07-10 19:46:11 +05:30
|
|
|
-F kernelsyms
|
2008-08-05 13:29:25 +05:30
|
|
|
--filesyms kernelsyms
|
|
|
|
Use the file instead of the current kernel symbols
|
2017-02-09 20:18:51 +05:30
|
|
|
-E Module.symvers
|
|
|
|
--symvers Module.symvers
|
|
|
|
Use Module.symvers file to check symbol versions
|
2008-07-04 15:55:44 +05:30
|
|
|
*/
|
|
|
|
|
2010-06-07 04:46:45 +05:30
|
|
|
//usage:#if ENABLE_MODPROBE_SMALL
|
2010-10-16 05:26:41 +05:30
|
|
|
|
2017-02-09 20:18:50 +05:30
|
|
|
//usage:#define depmod_trivial_usage "[-n]"
|
|
|
|
//usage:#define depmod_full_usage "\n\n"
|
|
|
|
//usage: "Generate modules.dep.bb"
|
|
|
|
//usage: "\n"
|
|
|
|
//usage: "\n -n Dry run: print file to stdout"
|
2010-10-16 05:26:41 +05:30
|
|
|
|
|
|
|
//usage:#define insmod_trivial_usage
|
2017-01-31 18:39:17 +05:30
|
|
|
//usage: "FILE" IF_FEATURE_CMDLINE_MODULE_OPTIONS(" [SYMBOL=VALUE]...")
|
2010-10-16 05:26:41 +05:30
|
|
|
//usage:#define insmod_full_usage "\n\n"
|
2014-04-19 18:34:39 +05:30
|
|
|
//usage: "Load kernel module"
|
2010-10-16 05:26:41 +05:30
|
|
|
|
|
|
|
//usage:#define rmmod_trivial_usage
|
2017-01-30 23:51:12 +05:30
|
|
|
//usage: "MODULE..."
|
2010-10-16 05:26:41 +05:30
|
|
|
//usage:#define rmmod_full_usage "\n\n"
|
2017-01-30 23:51:12 +05:30
|
|
|
//usage: "Unload kernel modules"
|
2010-10-16 05:26:41 +05:30
|
|
|
|
2010-06-07 04:46:45 +05:30
|
|
|
//usage:#define modprobe_trivial_usage
|
2017-01-31 18:39:17 +05:30
|
|
|
//usage: "[-rq] MODULE" IF_FEATURE_CMDLINE_MODULE_OPTIONS(" [SYMBOL=VALUE]...")
|
2010-06-07 04:46:45 +05:30
|
|
|
//usage:#define modprobe_full_usage "\n\n"
|
2017-01-30 23:51:12 +05:30
|
|
|
//usage: " -r Remove MODULE"
|
2010-06-07 04:46:45 +05:30
|
|
|
//usage: "\n -q Quiet"
|
2010-10-16 05:26:41 +05:30
|
|
|
|
|
|
|
//usage:#endif
|
2010-06-07 04:46:45 +05:30
|
|
|
|
2008-07-04 15:55:44 +05:30
|
|
|
int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int modprobe_main(int argc UNUSED_PARAM, char **argv)
|
2008-07-04 15:55:44 +05:30
|
|
|
{
|
2017-01-09 14:18:58 +05:30
|
|
|
#if ENABLE_MODPROBE || ENABLE_INSMOD || ENABLE_RMMOD
|
2016-06-20 14:29:06 +05:30
|
|
|
int exitcode;
|
2017-01-09 14:18:58 +05:30
|
|
|
#endif
|
2008-07-04 15:55:44 +05:30
|
|
|
struct utsname uts;
|
2017-01-31 18:39:17 +05:30
|
|
|
IF_FEATURE_CMDLINE_MODULE_OPTIONS(char *options = NULL;)
|
2008-07-04 15:55:44 +05:30
|
|
|
|
|
|
|
INIT_G();
|
|
|
|
|
2008-07-10 19:46:11 +05:30
|
|
|
/* Prevent ugly corner cases with no modules at all */
|
|
|
|
modinfo = xzalloc(sizeof(modinfo[0]));
|
|
|
|
|
2017-02-07 21:11:25 +05:30
|
|
|
if ((MOD_APPLET_CNT == 2 && ENABLE_DEPMOD && ENABLE_MODPROBE)
|
|
|
|
|| is_depmod || is_modprobe
|
|
|
|
) {
|
2008-09-06 19:44:01 +05:30
|
|
|
/* Goto modules directory */
|
|
|
|
xchdir(CONFIG_DEFAULT_MODULES_DIR);
|
2017-02-05 23:32:34 +05:30
|
|
|
uname(&uts); /* never fails */
|
2008-09-06 19:44:01 +05:30
|
|
|
}
|
2008-07-10 19:46:11 +05:30
|
|
|
|
2017-01-09 14:18:58 +05:30
|
|
|
/* depmod? */
|
2017-01-31 14:36:43 +05:30
|
|
|
if (is_depmod) {
|
2008-07-10 19:46:11 +05:30
|
|
|
/* Supported:
|
|
|
|
* -n: print result to stdout
|
|
|
|
* -a: process all modules (default)
|
|
|
|
* optional VERSION parameter
|
|
|
|
* Ignored:
|
|
|
|
* -A: do work only if a module is newer than depfile
|
|
|
|
* -e: report any symbols which a module needs
|
|
|
|
* which are not supplied by other modules or the kernel
|
|
|
|
* -F FILE: System.map (symbols for -e)
|
2017-02-09 20:18:51 +05:30
|
|
|
* -q, -r, -u: noop
|
2008-07-10 19:46:11 +05:30
|
|
|
* Not supported:
|
|
|
|
* -b BASEDIR: (TODO!) modules are in
|
|
|
|
* $BASEDIR/lib/modules/$VERSION
|
2017-02-09 20:18:51 +05:30
|
|
|
* -m: create legacy "modules.*map" files (deprecated; in
|
|
|
|
* kmod's depmod, prints a warning message and continues)
|
2008-07-10 19:46:11 +05:30
|
|
|
* -v: human readable deps to stdout
|
|
|
|
* -V: version (don't want to support it - people may depend
|
|
|
|
* on it as an indicator of "standard" depmod)
|
|
|
|
* -h: help (well duh)
|
|
|
|
* module1.o module2.o parameters (just ignored for now)
|
|
|
|
*/
|
|
|
|
getopt32(argv, "na" "AeF:qru" /* "b:vV", NULL */, NULL);
|
|
|
|
argv += optind;
|
|
|
|
/* if (argv[0] && argv[1]) bb_show_usage(); */
|
|
|
|
/* Goto $VERSION directory */
|
|
|
|
xchdir(argv[0] ? argv[0] : uts.release);
|
|
|
|
/* Force full module scan by asking to find a bogus module.
|
|
|
|
* This will generate modules.dep.bb as a side effect. */
|
|
|
|
process_module((char*)"/", NULL);
|
|
|
|
return !wrote_dep_bb_ok;
|
|
|
|
}
|
|
|
|
|
2017-01-09 12:34:43 +05:30
|
|
|
#if ENABLE_MODPROBE || ENABLE_INSMOD || ENABLE_RMMOD
|
|
|
|
/* modprobe, insmod, rmmod require at least one argument */
|
2008-07-04 15:55:44 +05:30
|
|
|
/* only -q (quiet) and -r (rmmod),
|
|
|
|
* the rest are accepted and ignored (compat) */
|
2017-08-09 01:25:02 +05:30
|
|
|
getopt32(argv, "^" "qrfsvwb" "\0" "-1");
|
2008-07-04 15:55:44 +05:30
|
|
|
argv += optind;
|
|
|
|
|
2017-02-05 23:32:34 +05:30
|
|
|
if (is_modprobe) {
|
2008-09-06 19:44:01 +05:30
|
|
|
/* Goto $VERSION directory */
|
|
|
|
xchdir(uts.release);
|
|
|
|
}
|
2008-07-04 15:55:44 +05:30
|
|
|
|
2017-01-09 12:34:43 +05:30
|
|
|
/* are we rmmod? -> simulate modprobe -r, but don't bother the flag if
|
|
|
|
* there're no other applets here */
|
|
|
|
if (is_rmmod) {
|
|
|
|
if (!ONLY_APPLET)
|
|
|
|
option_mask32 |= OPT_r;
|
|
|
|
} else if (!ENABLE_MODPROBE || !(option_mask32 & OPT_r)) {
|
2017-01-31 18:39:17 +05:30
|
|
|
# if ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS
|
2015-02-08 01:14:46 +05:30
|
|
|
/* If not rmmod/-r, parse possible module options given on command line.
|
2008-07-04 15:55:44 +05:30
|
|
|
* insmod/modprobe takes one module name, the rest are parameters. */
|
|
|
|
char **arg = argv;
|
|
|
|
while (*++arg) {
|
|
|
|
/* Enclose options in quotes */
|
|
|
|
char *s = options;
|
|
|
|
options = xasprintf("%s \"%s\"", s ? s : "", *arg);
|
|
|
|
free(s);
|
|
|
|
*arg = NULL;
|
|
|
|
}
|
2017-01-09 12:34:43 +05:30
|
|
|
# else
|
2008-07-04 15:55:44 +05:30
|
|
|
argv[1] = NULL;
|
2017-01-09 12:34:43 +05:30
|
|
|
# endif
|
|
|
|
}
|
2008-07-04 15:55:44 +05:30
|
|
|
|
2017-01-09 12:34:43 +05:30
|
|
|
if (is_insmod) {
|
2008-09-06 19:44:01 +05:30
|
|
|
size_t len;
|
|
|
|
void *map;
|
|
|
|
|
|
|
|
len = MAXINT(ssize_t);
|
2011-01-10 01:27:52 +05:30
|
|
|
map = xmalloc_open_zipped_read_close(*argv, &len);
|
2011-01-17 19:28:27 +05:30
|
|
|
if (!map)
|
|
|
|
bb_perror_msg_and_die("can't read '%s'", *argv);
|
2008-09-06 19:44:01 +05:30
|
|
|
if (init_module(map, len,
|
2017-01-31 18:39:17 +05:30
|
|
|
(IF_FEATURE_CMDLINE_MODULE_OPTIONS(options ? options : ) "")
|
2011-01-10 01:27:52 +05:30
|
|
|
) != 0
|
|
|
|
) {
|
2009-04-13 07:55:40 +05:30
|
|
|
bb_error_msg_and_die("can't insert '%s': %s",
|
2008-09-06 19:44:01 +05:30
|
|
|
*argv, moderror(errno));
|
2011-01-10 01:27:52 +05:30
|
|
|
}
|
2016-06-20 14:29:06 +05:30
|
|
|
return EXIT_SUCCESS;
|
2008-09-06 19:44:01 +05:30
|
|
|
}
|
|
|
|
|
2008-07-10 19:44:45 +05:30
|
|
|
/* Try to load modprobe.dep.bb */
|
2017-01-09 12:34:43 +05:30
|
|
|
if (!is_rmmod) {
|
2015-02-08 01:14:46 +05:30
|
|
|
load_dep_bb();
|
2016-06-20 14:29:06 +05:30
|
|
|
}
|
2008-07-10 19:44:20 +05:30
|
|
|
|
2008-07-04 15:55:44 +05:30
|
|
|
/* Load/remove modules.
|
2015-02-08 01:14:46 +05:30
|
|
|
* Only rmmod/modprobe -r loops here, insmod/modprobe has only argv[0] */
|
2016-06-20 14:29:06 +05:30
|
|
|
exitcode = EXIT_SUCCESS;
|
2008-07-04 15:55:44 +05:30
|
|
|
do {
|
2016-06-20 14:29:06 +05:30
|
|
|
exitcode |= process_module(*argv, options);
|
2010-09-25 18:07:06 +05:30
|
|
|
} while (*++argv);
|
2008-07-04 15:55:44 +05:30
|
|
|
|
|
|
|
if (ENABLE_FEATURE_CLEAN_UP) {
|
2017-01-31 18:39:17 +05:30
|
|
|
IF_FEATURE_CMDLINE_MODULE_OPTIONS(free(options);)
|
2008-07-04 15:55:44 +05:30
|
|
|
}
|
2016-06-20 14:29:06 +05:30
|
|
|
return exitcode;
|
2017-01-09 12:34:43 +05:30
|
|
|
#endif /* MODPROBE || INSMOD || RMMOD */
|
2008-07-04 15:55:44 +05:30
|
|
|
}
|
2017-01-09 21:01:21 +05:30
|
|
|
|
|
|
|
#endif /* MOD_APPLET_CNT > 0 */
|