1 /* vi: set sw=4 ts=4: */
5 * Copyright (c) 2008 Vladimir Dronnikov
6 * Copyright (c) 2008 Bernhard Reutner-Fischer (initial depmod code)
8 * Licensed under GPLv2, see file LICENSE in this source tree.
11 /* modprobe-small configs are defined in Config.src to ensure better
12 * "make config" order */
14 //applet:IF_LSMOD( IF_MODPROBE_SMALL(APPLET_NOEXEC( lsmod, lsmod, BB_DIR_SBIN, BB_SUID_DROP, lsmod )))
15 //applet:IF_MODPROBE(IF_MODPROBE_SMALL(APPLET_NOEXEC( modprobe, modprobe, BB_DIR_SBIN, BB_SUID_DROP, modprobe)))
16 // APPLET_ODDNAME:name main location suid_type help
17 //applet:IF_DEPMOD( IF_MODPROBE_SMALL(APPLET_ODDNAME(depmod, modprobe, BB_DIR_SBIN, BB_SUID_DROP, depmod )))
18 //applet:IF_INSMOD( IF_MODPROBE_SMALL(APPLET_NOEXEC( insmod, modprobe, BB_DIR_SBIN, BB_SUID_DROP, insmod )))
19 //applet:IF_RMMOD( IF_MODPROBE_SMALL(APPLET_NOEXEC( rmmod, modprobe, BB_DIR_SBIN, BB_SUID_DROP, rmmod )))
20 /* noexec speeds up boot with many modules loaded (need SH_STANDALONE=y) */
21 /* I measured about ~5 times faster insmod */
22 /* depmod is not noexec, it runs longer and benefits from memory trimming via exec */
24 //kbuild:lib-$(CONFIG_MODPROBE_SMALL) += modprobe-small.o
27 /* After libbb.h, since it needs sys/types.h on some systems */
28 #include <sys/utsname.h> /* uname() */
30 #include <sys/syscall.h>
32 #define init_module(mod, len, opts) syscall(__NR_init_module, mod, len, opts)
33 #define delete_module(mod, flags) syscall(__NR_delete_module, mod, flags)
34 #ifdef __NR_finit_module
35 # define finit_module(fd, uargs, flags) syscall(__NR_finit_module, fd, uargs, flags)
37 /* linux/include/linux/module.h has limit of 64 chars on module names */
38 #undef MODULE_NAME_LEN
39 #define MODULE_NAME_LEN 64
43 # define dbg1_error_msg(...) ((void)0)
44 # define dbg2_error_msg(...) ((void)0)
46 # define dbg1_error_msg(...) bb_error_msg(__VA_ARGS__)
47 # define dbg2_error_msg(...) bb_error_msg(__VA_ARGS__)
50 #define DEPFILE_BB CONFIG_DEFAULT_DEPMOD_FILE".bb"
52 //usage:#if ENABLE_MODPROBE_SMALL
54 //usage:#define lsmod_trivial_usage
56 //usage:#define lsmod_full_usage "\n\n"
57 //usage: "List loaded kernel modules"
62 int lsmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
63 int lsmod_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
65 xprint_and_close_file(xfopen_for_read("/proc/modules"));
70 /* Num of applets that use modprobe_main() entry point. */
71 /* lsmod is not here. */
72 #define MOD_APPLET_CNT (ENABLE_MODPROBE + ENABLE_DEPMOD + ENABLE_INSMOD + ENABLE_RMMOD)
74 /* Do not bother if MODPROBE_SMALL=y but no applets selected. */
75 /* The rest of the file is in this if block. */
76 #if MOD_APPLET_CNT > 0
78 #define ONLY_APPLET (MOD_APPLET_CNT == 1)
79 #define is_modprobe (ENABLE_MODPROBE && (ONLY_APPLET || applet_name[0] == 'm'))
80 #define is_depmod (ENABLE_DEPMOD && (ONLY_APPLET || applet_name[0] == 'd'))
81 #define is_insmod (ENABLE_INSMOD && (ONLY_APPLET || applet_name[0] == 'i'))
82 #define is_rmmod (ENABLE_RMMOD && (ONLY_APPLET || applet_name[0] == 'r'))
85 DEPMOD_OPT_n = (1 << 0), /* dry-run, print to stdout */
86 OPT_q = (1 << 0), /* be quiet */
87 OPT_r = (1 << 1), /* module removal instead of loading */
90 typedef struct module_info {
94 smallint open_read_failed;
101 module_info *modinfo;
102 char *module_load_options;
103 smallint dep_bb_seen;
104 smallint wrote_dep_bb_ok;
105 unsigned module_count;
106 int module_found_idx;
107 unsigned stringbuf_idx;
108 unsigned stringbuf_size;
109 char *stringbuf; /* some modules have lots of stuff */
110 /* for example, drivers/media/video/saa7134/saa7134.ko */
111 /* therefore having a fixed biggish buffer is not wise */
113 #define G (*ptr_to_globals)
114 #define modinfo (G.modinfo )
115 #define dep_bb_seen (G.dep_bb_seen )
116 #define wrote_dep_bb_ok (G.wrote_dep_bb_ok )
117 #define module_count (G.module_count )
118 #define module_found_idx (G.module_found_idx )
119 #define module_load_options (G.module_load_options)
120 #define stringbuf_idx (G.stringbuf_idx )
121 #define stringbuf_size (G.stringbuf_size )
122 #define stringbuf (G.stringbuf )
123 #define INIT_G() do { \
124 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
127 static void append(const char *s)
129 unsigned len = strlen(s);
130 if (stringbuf_idx + len + 15 > stringbuf_size) {
131 stringbuf_size = stringbuf_idx + len + 127;
132 dbg2_error_msg("grow stringbuf to %u", stringbuf_size);
133 stringbuf = xrealloc(stringbuf, stringbuf_size);
135 memcpy(stringbuf + stringbuf_idx, s, len);
136 stringbuf_idx += len;
139 static void appendc(char c)
141 /* We appendc() only after append(), + 15 trick in append()
142 * makes it unnecessary to check for overflow here */
143 stringbuf[stringbuf_idx++] = c;
146 static void bksp(void)
152 static void reset_stringbuf(void)
157 static char* copy_stringbuf(void)
159 char *copy = xzalloc(stringbuf_idx + 1); /* terminating NUL */
160 return memcpy(copy, stringbuf, stringbuf_idx);
163 static char* find_keyword(char *ptr, size_t len, const char *word)
165 if (!ptr) /* happens if xmalloc_open_zipped_read_close cannot read it */
168 len -= strlen(word) - 1;
169 while ((ssize_t)len > 0) {
173 /* search for the first char in word */
174 ptr = memchr(ptr, word[0], len);
175 if (ptr == NULL) /* no occurrence left, done */
177 after_word = is_prefixed_with(ptr, word);
179 return after_word; /* found, return ptr past it */
186 static void replace(char *s, char what, char with)
195 static char *filename2modname(const char *filename, char *modname)
200 // Disabled since otherwise "modprobe dir/name" would work
201 // as if it is "modprobe name". It is unclear why
202 // 'basenamization' was here in the first place.
203 //from = bb_get_last_path_component_nostrip(filename);
205 for (i = 0; i < (MODULE_NAME_LEN-1) && from[i] != '\0' && from[i] != '.'; i++)
206 modname[i] = (from[i] == '-') ? '_' : from[i];
212 static int pathname_matches_modname(const char *pathname, const char *modname)
215 char name[MODULE_NAME_LEN];
216 filename2modname(bb_get_last_path_component_nostrip(pathname), name);
217 r = (strcmp(name, modname) == 0);
221 /* Take "word word", return malloced "word",NUL,"word",NUL,NUL */
222 static char* str_2_list(const char *str)
224 int len = strlen(str) + 1;
225 char *dst = xmalloc(len + 1);
228 memcpy(dst, str, len);
229 //TODO: protect against 2+ spaces: "word word"
230 replace(dst, ' ', '\0');
234 /* We use error numbers in a loose translation... */
235 static const char *moderror(int err)
239 return "invalid module format";
241 return "unknown symbol in module or invalid parameter";
243 return "module has wrong symbol version";
244 case EINVAL: /* "invalid parameter" */
245 return "unknown symbol in module or invalid parameter"
246 + sizeof("unknown symbol in module or");
248 return strerror(err);
252 static int load_module(const char *fname, const char *options)
256 size_t len = MAXINT(ssize_t);
262 dbg1_error_msg("load_module('%s','%s')", fname, options);
265 * First we try finit_module if available. Some kernels are configured
266 * to only allow loading of modules off of secure storage (like a read-
267 * only rootfs) which needs the finit_module call. If it fails, we fall
268 * back to normal module loading to support compressed modules.
271 # ifdef __NR_finit_module
273 int fd = open(fname, O_RDONLY | O_CLOEXEC);
275 r = finit_module(fd, options, 0) != 0;
281 module_image = xmalloc_open_zipped_read_close(fname, &len);
282 r = (!module_image || init_module(module_image, len, options) != 0);
286 dbg1_error_msg("load_module:%d", r);
287 return r; /* 0 = success */
290 dbg1_error_msg("load_module('%s','%s')", fname, options);
295 /* Returns !0 if open/read was unsuccessful */
296 static int parse_module(module_info *info, const char *pathname)
302 dbg1_error_msg("parse_module('%s')", pathname);
304 /* Read (possibly compressed) module */
306 len = 64 * 1024 * 1024; /* 64 Mb at most */
307 module_image = xmalloc_open_zipped_read_close(pathname, &len);
308 /* module_image == NULL is ok here, find_keyword handles it */
309 //TODO: optimize redundant module body reads
311 /* "alias1 symbol:sym1 alias2 symbol:sym2" */
315 unsigned start = stringbuf_idx;
316 ptr = find_keyword(module_image + pos, len - pos, "alias=");
318 ptr = find_keyword(module_image + pos, len - pos, "__ksymtab_");
321 /* DOCME: __ksymtab_gpl and __ksymtab_strings occur
322 * in many modules. What do they mean? */
323 if (strcmp(ptr, "gpl") == 0 || strcmp(ptr, "strings") == 0)
325 dbg2_error_msg("alias:'symbol:%s'", ptr);
328 dbg2_error_msg("alias:'%s'", ptr);
333 * Don't add redundant aliases, such as:
334 * libcrc32c.ko symbol:crc32c symbol:crc32c
336 if (start) { /* "if we aren't the first alias" */
338 stringbuf[stringbuf_idx] = '\0';
339 last = stringbuf + start;
341 * String at last-1 is " symbol:crc32c "
342 * (with both leading and trailing spaces).
344 if (strncmp(stringbuf, last, stringbuf_idx - start) == 0)
345 /* First alias matches us */
348 /* Does any other alias match? */
349 found = strstr(stringbuf, last-1);
350 if (found < last-1) {
351 /* There is absolutely the same string before us */
352 dbg2_error_msg("redundant:'%s'", last);
353 stringbuf_idx = start;
358 pos = (ptr - module_image);
360 bksp(); /* remove last ' ' */
361 info->aliases = copy_stringbuf();
362 replace(info->aliases, '-', '_');
364 /* "dependency1 depandency2" */
366 ptr = find_keyword(module_image, len, "depends=");
368 replace(ptr, ',', ' ');
369 replace(ptr, '-', '_');
370 dbg2_error_msg("dep:'%s'", ptr);
374 info->deps = copy_stringbuf();
376 info->open_read_failed = (module_image == NULL);
377 return info->open_read_failed;
380 static FAST_FUNC int fileAction(const char *pathname,
381 struct stat *sb UNUSED_PARAM,
382 void *modname_to_match,
383 int depth UNUSED_PARAM)
387 bool is_remove = (ENABLE_RMMOD && ONLY_APPLET)
388 || ((ENABLE_RMMOD || ENABLE_MODPROBE) && (option_mask32 & OPT_r));
390 pathname += 2; /* skip "./" */
391 fname = bb_get_last_path_component_nostrip(pathname);
392 if (!strrstr(fname, ".ko")) {
393 dbg1_error_msg("'%s' is not a module", pathname);
394 return TRUE; /* not a module, continue search */
397 cur = module_count++;
398 modinfo = xrealloc_vector(modinfo, 12, cur);
399 modinfo[cur].pathname = xstrdup(pathname);
400 /*modinfo[cur].aliases = NULL; - xrealloc_vector did it */
401 /*modinfo[cur+1].pathname = NULL;*/
403 if (!pathname_matches_modname(fname, modname_to_match)) {
404 dbg1_error_msg("'%s' module name doesn't match", pathname);
405 return TRUE; /* module name doesn't match, continue search */
408 dbg1_error_msg("'%s' module name matches", pathname);
409 module_found_idx = cur;
410 if (parse_module(&modinfo[cur], pathname) != 0)
411 return TRUE; /* failed to open/read it, no point in trying loading */
414 if (load_module(pathname, module_load_options) == 0) {
415 /* Load was successful, there is nothing else to do.
416 * This can happen ONLY for "top-level" module load,
417 * not a dep, because deps don't do dirscan. */
425 static int load_dep_bb(void)
428 FILE *fp = fopen_for_read(DEPFILE_BB);
434 dbg1_error_msg("loading "DEPFILE_BB);
436 /* Why? There is a rare scenario: we did not find modprobe.dep.bb,
437 * we scanned the dir and found no module by name, then we search
438 * for alias (full scan), and we decided to generate modprobe.dep.bb.
439 * But we see modprobe.dep.bb.new! Other modprobe is at work!
440 * We wait and other modprobe renames it to modprobe.dep.bb.
442 * But we already have modinfo[] filled, and "module_count = 0"
443 * makes us start anew. Yes, we leak modinfo[].xxx pointers -
444 * there is not much of data there anyway. */
446 memset(&modinfo[0], 0, sizeof(modinfo[0]));
448 while ((line = xmalloc_fgetline(fp)) != NULL) {
457 space = strchrnul(line, ' ');
458 cur = module_count++;
459 modinfo = xrealloc_vector(modinfo, 12, cur);
460 /*modinfo[cur+1].pathname = NULL; - xrealloc_vector did it */
461 modinfo[cur].pathname = line; /* we take ownership of malloced block here */
464 modinfo[cur].aliases = space;
465 linebuf = xmalloc_fgetline(fp);
466 modinfo[cur].deps = linebuf ? linebuf : xzalloc(1);
467 if (modinfo[cur].deps[0]) {
468 /* deps are not "", so next line must be empty */
469 line = xmalloc_fgetline(fp);
470 /* Refuse to work with damaged config file */
472 bb_error_msg_and_die("error in %s at '%s'", DEPFILE_BB, line);
479 static int start_dep_bb_writeout(void)
483 /* depmod -n: write result to stdout */
484 if (is_depmod && (option_mask32 & DEPMOD_OPT_n))
485 return STDOUT_FILENO;
487 fd = open(DEPFILE_BB".new", O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0644);
489 if (errno == EEXIST) {
491 dbg1_error_msg(DEPFILE_BB".new exists, waiting for "DEPFILE_BB);
493 usleep(1000*1000 / 20);
495 dbg1_error_msg(DEPFILE_BB" appeared");
496 return -2; /* magic number */
501 bb_error_msg("deleting stale %s", DEPFILE_BB".new");
502 fd = open_or_warn(DEPFILE_BB".new", O_WRONLY | O_CREAT | O_TRUNC);
505 dbg1_error_msg("opened "DEPFILE_BB".new:%d", fd);
509 static void write_out_dep_bb(int fd)
514 /* We want good error reporting. fdprintf is not good enough. */
515 fp = xfdopen_for_write(fd);
517 while (modinfo[i].pathname) {
518 fprintf(fp, "%s%s%s\n" "%s%s\n",
519 modinfo[i].pathname, modinfo[i].aliases[0] ? " " : "", modinfo[i].aliases,
520 modinfo[i].deps, modinfo[i].deps[0] ? "\n" : "");
523 /* Badly formatted depfile is a no-no. Be paranoid. */
525 if (ferror(fp) | fclose(fp)) /* | instead of || is intended */
528 if (fd == STDOUT_FILENO) /* it was depmod -n */
531 if (rename(DEPFILE_BB".new", DEPFILE_BB) != 0) {
533 bb_perror_msg("can't create '%s'", DEPFILE_BB);
534 unlink(DEPFILE_BB".new");
538 dbg1_error_msg("created "DEPFILE_BB);
542 static module_info** find_alias(const char *alias)
547 module_info **infovec;
548 dbg1_error_msg("find_alias('%s')", alias);
551 /* First try to find by name (cheaper) */
553 while (modinfo[i].pathname) {
554 if (pathname_matches_modname(modinfo[i].pathname, alias)) {
555 dbg1_error_msg("found '%s' in module '%s'",
556 alias, modinfo[i].pathname);
557 if (!modinfo[i].aliases) {
558 parse_module(&modinfo[i], modinfo[i].pathname);
560 infovec = xzalloc(2 * sizeof(infovec[0]));
561 infovec[0] = &modinfo[i];
567 /* Ok, we definitely have to scan module bodies. This is a good
568 * moment to generate modprobe.dep.bb, if it does not exist yet */
569 dep_bb_fd = dep_bb_seen ? -1 : start_dep_bb_writeout();
570 if (dep_bb_fd == -2) /* modprobe.dep.bb appeared? */
573 /* Scan all module bodies, extract modinfo (it contains aliases) */
577 while (modinfo[i].pathname) {
579 if (!modinfo[i].aliases) {
580 parse_module(&modinfo[i], modinfo[i].pathname);
582 /* "alias1 symbol:sym1 alias2 symbol:sym2" */
583 desc = str_2_list(modinfo[i].aliases);
584 /* Does matching substring exist? */
585 for (s = desc; *s; s += strlen(s) + 1) {
586 /* Aliases in module bodies can be defined with
587 * shell patterns. Example:
588 * "pci:v000010DEd000000D9sv*sd*bc*sc*i*".
589 * Plain strcmp() won't catch that */
590 if (fnmatch(s, alias, 0) == 0) {
591 dbg1_error_msg("found alias '%s' in module '%s'",
592 alias, modinfo[i].pathname);
593 infovec = xrealloc_vector(infovec, 1, infoidx);
594 infovec[infoidx++] = &modinfo[i];
602 /* Create module.dep.bb if needed */
603 if (dep_bb_fd >= 0) {
604 write_out_dep_bb(dep_bb_fd);
607 dbg1_error_msg("find_alias '%s' returns %d results", alias, infoidx);
611 #if ENABLE_FEATURE_MODPROBE_SMALL_CHECK_ALREADY_LOADED
612 // TODO: open only once, invent config_rewind()
613 static int already_loaded(const char *name)
621 fp = fopen_for_read("/proc/modules");
624 while ((line = xmalloc_fgetline(fp)) != NULL) {
628 // Examples from kernel 3.14.6:
629 //pcspkr 12718 0 - Live 0xffffffffa017e000
630 //snd_timer 28690 2 snd_seq,snd_pcm, Live 0xffffffffa025e000
631 //i915 801405 2 - Live 0xffffffffa0096000
632 after_name = is_prefixed_with(line, name);
633 if (!after_name || *after_name != ' ') {
637 live = strstr(line, " Live");
640 /* State can be Unloading, Loading, or Live.
641 * modprobe must not return prematurely if we see "Loading":
642 * it can cause further programs to assume load completed,
643 * but it did not (yet)!
644 * Wait up to 5*20 ms for it to resolve.
648 break; /* huh? report as "not loaded" */
661 #define already_loaded(name) 0
664 static int rmmod(const char *filename)
667 char modname[MODULE_NAME_LEN];
669 filename2modname(filename, modname);
670 r = delete_module(modname, O_NONBLOCK | O_EXCL);
671 dbg1_error_msg("delete_module('%s', O_NONBLOCK | O_EXCL):%d", modname, r);
672 if (r != 0 && !(option_mask32 & OPT_q)) {
673 bb_perror_msg("remove '%s'", modname);
679 * Given modules definition and module name (or alias, or symbol)
680 * load/remove the module respecting dependencies.
681 * NB: also called by depmod with bogus name "/",
682 * just in order to force modprobe.dep.bb creation.
684 #if !ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS
685 #define process_module(a,b) process_module(a)
686 #define cmdline_options ""
688 static int process_module(char *name, const char *cmdline_options)
690 char *s, *deps, *options;
691 module_info **infovec;
694 bool is_remove = (ENABLE_RMMOD && ONLY_APPLET)
695 || ((ENABLE_RMMOD || ENABLE_MODPROBE) && (option_mask32 & OPT_r));
696 int exitcode = EXIT_SUCCESS;
698 dbg1_error_msg("process_module('%s','%s')", name, cmdline_options);
700 replace(name, '-', '_');
702 dbg1_error_msg("already_loaded:%d is_remove:%d", already_loaded(name), is_remove);
705 /* Does not remove dependencies, no need to scan, just remove.
706 * (compat note: this allows and strips .ko suffix)
713 * We used to have "is_remove != already_loaded(name)" check here, but
714 * modprobe -r pci:v00008086d00007010sv00000000sd00000000bc01sc01i80
715 * won't unload modules (there are more than one)
716 * which have this alias.
718 if (!is_remove && already_loaded(name)) {
719 dbg1_error_msg("nothing to do for '%s'", name);
725 char *opt_filename = xasprintf("/etc/modules/%s", name);
726 options = xmalloc_open_read_close(opt_filename, NULL);
728 replace(options, '\n', ' ');
729 #if ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS
730 if (cmdline_options) {
731 /* NB: cmdline_options always have one leading ' '
732 * (see main()), we remove it here */
733 char *op = xasprintf(options ? "%s %s" : "%s %s" + 3,
734 cmdline_options + 1, options);
740 module_load_options = options;
741 dbg1_error_msg("process_module('%s'): options:'%s'", name, options);
745 /* Scan module directory. This is done only once.
746 * It will attempt module load, and will exit(EXIT_SUCCESS)
749 module_found_idx = -1;
750 recursive_action(".",
751 ACTION_RECURSE, /* flags */
752 fileAction, /* file action */
753 NULL, /* dir action */
754 name, /* user data */
757 dbg1_error_msg("dirscan complete");
758 /* Module was not found, or load failed, or is_remove */
759 if (module_found_idx >= 0) { /* module was found */
760 infovec = xzalloc(2 * sizeof(infovec[0]));
761 infovec[0] = &modinfo[module_found_idx];
762 } else { /* search for alias, not a plain module name */
763 infovec = find_alias(name);
766 infovec = find_alias(name);
770 /* both dirscan and find_alias found nothing */
771 if (!is_remove && !is_depmod) { /* it wasn't rmmod or depmod */
772 bb_error_msg("module '%s' not found", name);
773 //TODO: _and_die()? or should we continue (un)loading modules listed on cmdline?
774 /* "modprobe non-existing-module; echo $?" must print 1 */
775 exitcode = EXIT_FAILURE;
780 /* There can be more than one module for the given alias. For example,
781 * "pci:v00008086d00007010sv00000000sd00000000bc01sc01i80" matches
782 * ata_piix because it has alias "pci:v00008086d00007010sv*sd*bc*sc*i*"
783 * and ata_generic, it has alias "pci:v*d*sv*sd*bc01sc01i*"
784 * Standard modprobe loads them both. We achieve it by returning
785 * a *list* of modinfo pointers from find_alias().
788 /* modprobe -r? unload module(s) */
791 while ((info = infovec[infoidx++]) != NULL) {
792 int r = rmmod(bb_get_last_path_component_nostrip(info->pathname));
794 goto ret; /* error */
797 /* modprobe -r: we do not stop here -
798 * continue to unload modules on which the module depends:
799 * "-r --remove: option causes modprobe to remove a module.
800 * If the modules it depends on are also unused, modprobe
801 * will try to remove them, too."
806 while ((info = infovec[infoidx++]) != NULL) {
807 /* Iterate thru dependencies, trying to (un)load them */
808 deps = str_2_list(info->deps);
809 for (s = deps; *s; s += strlen(s) + 1) {
810 //if (strcmp(name, s) != 0) // N.B. do loops exist?
811 dbg1_error_msg("recurse on dep '%s'", s);
812 process_module(s, NULL);
813 dbg1_error_msg("recurse on dep '%s' done", s);
820 /* We are modprobe: load it */
821 if (options && strstr(options, "blacklist")) {
822 dbg1_error_msg("'%s': blacklisted", info->pathname);
825 if (info->open_read_failed) {
826 /* We already tried it, didn't work. Don't try load again */
827 exitcode = EXIT_FAILURE;
831 if (load_module(info->pathname, options) != 0) {
832 if (EEXIST != errno) {
833 bb_error_msg("'%s': %s",
837 dbg1_error_msg("'%s': %s",
841 exitcode = EXIT_FAILURE;
850 #undef cmdline_options
853 /* For reference, module-init-tools v3.4 options:
856 Usage: insmod filename [args]
859 Usage: rmmod [-fhswvV] modulename ...
860 -f (or --force) forces a module unload, and may crash your
861 machine. This requires the Forced Module Removal option
862 when the kernel was compiled.
863 -h (or --help) prints this help text
864 -s (or --syslog) says use syslog, not stderr
865 -v (or --verbose) enables more messages
866 -V (or --version) prints the version code
867 -w (or --wait) begins module removal even if it is used
868 and will stop new users from accessing the module (so it
869 should eventually fall to zero).
872 Usage: modprobe [-v] [-V] [-C config-file] [-d <dirname> ] [-n] [-i] [-q]
873 [-b] [-o <modname>] [ --dump-modversions ] <modname> [parameters...]
874 modprobe -r [-n] [-i] [-v] <modulename> ...
875 modprobe -l -t <dirname> [ -a <modulename> ...]
878 depmod 3.13 -- part of module-init-tools
879 depmod -[aA] [-n -e -v -q -V -r -u -w -m]
880 [-b basedirectory] [forced_version]
881 depmod [-n -e -v -q -r -u -w] [-F kernelsyms] module1.ko module2.ko ...
882 If no arguments (except options) are given, "depmod -a" is assumed.
883 depmod will output a dependency list suitable for the modprobe utility.
885 -a, --all Probe all modules
886 -A, --quick Only does the work if there's a new module
887 -e, --errsyms Report not supplied symbols
888 -m, --map Create the legacy map files
889 -n, --show Write the dependency file on stdout only
890 -P, --symbol-prefix Architecture symbol prefix
891 -V, --version Print the release version
892 -v, --verbose Enable verbose mode
893 -w, --warn Warn on duplicates
894 -h, --help Print this usage message
895 The following options are useful for people managing distributions:
897 --basedir basedirectory
898 Use an image of a module tree
900 --filesyms kernelsyms
901 Use the file instead of the current kernel symbols
903 --symvers Module.symvers
904 Use Module.symvers file to check symbol versions
907 //usage:#if ENABLE_MODPROBE_SMALL
909 //usage:#define depmod_trivial_usage "[-n]"
910 //usage:#define depmod_full_usage "\n\n"
911 //usage: "Generate modules.dep.bb"
913 //usage: "\n -n Dry run: print file to stdout"
915 //usage:#define insmod_trivial_usage
916 //usage: "FILE" IF_FEATURE_CMDLINE_MODULE_OPTIONS(" [SYMBOL=VALUE]...")
917 //usage:#define insmod_full_usage "\n\n"
918 //usage: "Load kernel module"
920 //usage:#define rmmod_trivial_usage
922 //usage:#define rmmod_full_usage "\n\n"
923 //usage: "Unload kernel modules"
925 //usage:#define modprobe_trivial_usage
926 //usage: "[-rq] MODULE" IF_FEATURE_CMDLINE_MODULE_OPTIONS(" [SYMBOL=VALUE]...")
927 //usage:#define modprobe_full_usage "\n\n"
928 //usage: " -r Remove MODULE"
929 //usage: "\n -q Quiet"
933 int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
934 int modprobe_main(int argc UNUSED_PARAM, char **argv)
936 #if ENABLE_MODPROBE || ENABLE_INSMOD || ENABLE_RMMOD
940 IF_FEATURE_CMDLINE_MODULE_OPTIONS(char *options = NULL;)
944 /* Prevent ugly corner cases with no modules at all */
945 modinfo = xzalloc(sizeof(modinfo[0]));
947 if ((MOD_APPLET_CNT == 2 && ENABLE_DEPMOD && ENABLE_MODPROBE)
948 || is_depmod || is_modprobe
950 /* Goto modules directory */
951 xchdir(CONFIG_DEFAULT_MODULES_DIR);
952 uname(&uts); /* never fails */
958 * -n: print result to stdout
959 * -a: process all modules (default)
960 * optional VERSION parameter
962 * -A: do work only if a module is newer than depfile
963 * -e: report any symbols which a module needs
964 * which are not supplied by other modules or the kernel
965 * -F FILE: System.map (symbols for -e)
968 * -b BASEDIR: (TODO!) modules are in
969 * $BASEDIR/lib/modules/$VERSION
970 * -m: create legacy "modules.*map" files (deprecated; in
971 * kmod's depmod, prints a warning message and continues)
972 * -v: human readable deps to stdout
973 * -V: version (don't want to support it - people may depend
974 * on it as an indicator of "standard" depmod)
975 * -h: help (well duh)
976 * module1.o module2.o parameters (just ignored for now)
978 getopt32(argv, "na" "AeF:qru" /* "b:vV", NULL */, NULL);
980 /* if (argv[0] && argv[1]) bb_show_usage(); */
981 /* Goto $VERSION directory */
982 xchdir(argv[0] ? argv[0] : uts.release);
983 /* Force full module scan by asking to find a bogus module.
984 * This will generate modules.dep.bb as a side effect. */
985 process_module((char*)"/", NULL);
986 return !wrote_dep_bb_ok;
989 #if ENABLE_MODPROBE || ENABLE_INSMOD || ENABLE_RMMOD
990 /* modprobe, insmod, rmmod require at least one argument */
991 /* only -q (quiet) and -r (rmmod),
992 * the rest are accepted and ignored (compat) */
993 getopt32(argv, "^" "qrfsvwb" "\0" "-1");
997 /* Goto $VERSION directory */
1001 /* are we rmmod? -> simulate modprobe -r, but don't bother the flag if
1002 * there're no other applets here */
1005 option_mask32 |= OPT_r;
1006 } else if (!ENABLE_MODPROBE || !(option_mask32 & OPT_r)) {
1007 # if ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS
1008 /* If not rmmod/-r, parse possible module options given on command line.
1009 * insmod/modprobe takes one module name, the rest are parameters. */
1012 /* Enclose options in quotes */
1014 options = xasprintf("%s \"%s\"", s ? s : "", *arg);
1027 len = MAXINT(ssize_t);
1028 map = xmalloc_open_zipped_read_close(*argv, &len);
1030 bb_perror_msg_and_die("can't read '%s'", *argv);
1031 if (init_module(map, len,
1032 (IF_FEATURE_CMDLINE_MODULE_OPTIONS(options ? options : ) "")
1035 bb_error_msg_and_die("can't insert '%s': %s",
1036 *argv, moderror(errno));
1038 return EXIT_SUCCESS;
1041 /* Try to load modprobe.dep.bb */
1046 /* Load/remove modules.
1047 * Only rmmod/modprobe -r loops here, insmod/modprobe has only argv[0] */
1048 exitcode = EXIT_SUCCESS;
1050 exitcode |= process_module(*argv, options);
1053 if (ENABLE_FEATURE_CLEAN_UP) {
1054 IF_FEATURE_CMDLINE_MODULE_OPTIONS(free(options);)
1057 #endif /* MODPROBE || INSMOD || RMMOD */
1060 #endif /* MOD_APPLET_CNT > 0 */