1 /* vi: set sw=4 ts=4: */
3 * Modprobe written from scratch for BusyBox
5 * Copyright (c) 2002 by Robert Griebl, griebl@gmx.de
6 * Copyright (c) 2003 by Andrew Dennison, andrew.dennison@motec.com.au
7 * Copyright (c) 2005 by Jim Bauer, jfbauer@nfr.com
9 * Portions Copyright (c) 2005 by Yann E. MORIN, yann.morin.1998@anciens.enib.fr
11 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
15 #include <sys/utsname.h>
18 #define line_buffer bb_common_bufsiz1
20 struct mod_opt_t { /* one-way list of options to pass to a module */
22 struct mod_opt_t * m_next;
25 struct dep_t { /* one-way list of dependency rules */
26 /* a dependency rule */
27 char * m_name; /* the module name*/
28 char * m_path; /* the module file path */
29 struct mod_opt_t * m_options; /* the module options */
31 int m_isalias : 1; /* the module is an alias */
32 int m_reserved : 15; /* stuffin' */
34 int m_depcnt : 16; /* the number of dependable module(s) */
35 char ** m_deparr; /* the list of dependable module(s) */
37 struct dep_t * m_next; /* the next dependency rule */
40 struct mod_list_t { /* two-way list of modules to process */
41 /* a module description */
44 struct mod_opt_t * m_options;
46 struct mod_list_t * m_prev;
47 struct mod_list_t * m_next;
51 static struct dep_t *depend;
53 #define MAIN_OPT_STR "acdklnqrst:vVC:"
54 #define INSERT_ALL 1 /* a */
55 #define DUMP_CONF_EXIT 2 /* c */
56 #define D_OPT_IGNORED 4 /* d */
57 #define AUTOCLEAN_FLG 8 /* k */
58 #define LIST_ALL 16 /* l */
59 #define SHOW_ONLY 32 /* n */
60 #define QUIET 64 /* q */
61 #define REMOVE_OPT 128 /* r */
62 #define DO_SYSLOG 256 /* s */
63 #define RESTRICT_DIR 512 /* t */
64 #define VERBOSE 1024 /* v */
65 #define VERSION_ONLY 2048 /* V */
66 #define CONFIG_FILE 4096 /* C */
68 #define autoclean (option_mask32 & AUTOCLEAN_FLG)
69 #define show_only (option_mask32 & SHOW_ONLY)
70 #define quiet (option_mask32 & QUIET)
71 #define remove_opt (option_mask32 & REMOVE_OPT)
72 #define do_syslog (option_mask32 & DO_SYSLOG)
73 #define verbose (option_mask32 & VERBOSE)
75 static int parse_tag_value(char *buffer, char **ptag, char **pvalue)
79 buffer = skip_whitespace(buffer);
81 while (!isspace(*value)) {
87 value = skip_whitespace(value);
98 * This function appends an option to a list
100 static struct mod_opt_t *append_option(struct mod_opt_t *opt_list, char *opt)
102 struct mod_opt_t *ol = opt_list;
108 ol->m_next = xzalloc(sizeof(struct mod_opt_t));
111 ol = opt_list = xzalloc(sizeof(struct mod_opt_t));
114 ol->m_opt_val = xstrdup(opt);
115 /*ol->m_next = NULL; - done by xzalloc*/
120 #if ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS
121 /* static char* parse_command_string(char* src, char **dst);
122 * src: pointer to string containing argument
123 * dst: pointer to where to store the parsed argument
124 * return value: the pointer to the first char after the parsed argument,
125 * NULL if there was no argument parsed (only trailing spaces).
126 * Note that memory is allocated with xstrdup when a new argument was
127 * parsed. Don't forget to free it!
129 #define ARG_EMPTY 0x00
130 #define ARG_IN_DQUOTES 0x01
131 #define ARG_IN_SQUOTES 0x02
132 static char *parse_command_string(char *src, char **dst)
134 int opt_status = ARG_EMPTY;
137 /* Dumb you, I have nothing to do... */
138 if (src == NULL) return src;
140 /* Skip leading spaces */
141 while (*src == ' ') {
144 /* Is the end of string reached? */
148 /* Reached the start of an argument
149 * By the way, we duplicate a little too much
150 * here but what is too much is freed later. */
151 *dst = tmp_str = xstrdup(src);
152 /* Get to the end of that argument */
153 while (*tmp_str != '\0'
154 && (*tmp_str != ' ' || (opt_status & (ARG_IN_DQUOTES | ARG_IN_SQUOTES)))
158 if (opt_status & ARG_IN_DQUOTES) {
159 /* Already in double quotes, keep current char as is */
161 /* shift left 1 char, until end of string: get rid of the opening/closing quotes */
162 memmove(tmp_str, tmp_str + 1, strlen(tmp_str));
163 /* mark me: we enter or leave single quotes */
164 opt_status ^= ARG_IN_SQUOTES;
165 /* Back one char, as we need to re-scan the new char there. */
170 if (opt_status & ARG_IN_SQUOTES) {
171 /* Already in single quotes, keep current char as is */
173 /* shift left 1 char, until end of string: get rid of the opening/closing quotes */
174 memmove(tmp_str, tmp_str + 1, strlen(tmp_str));
175 /* mark me: we enter or leave double quotes */
176 opt_status ^= ARG_IN_DQUOTES;
177 /* Back one char, as we need to re-scan the new char there. */
182 if (opt_status & ARG_IN_SQUOTES) {
183 /* Between single quotes: keep as is. */
185 switch (*(tmp_str+1)) {
194 /* We escaped a special character. For now, keep
195 * both the back-slash and the following char. */
200 /* We escaped a space or a single or double quote,
201 * or a back-slash, or a non-escapable char. Remove
202 * the '\' and keep the new current char as is. */
203 memmove(tmp_str, tmp_str + 1, strlen(tmp_str));
208 /* Any other char that is special shall appear here.
209 * Example: $ starts a variable
211 do_variable_expansion();
215 /* any other char is kept as is. */
218 tmp_str++; /* Go to next char */
219 src++; /* Go to next char to find the end of the argument. */
221 /* End of string, but still no ending quote */
222 if (opt_status & (ARG_IN_DQUOTES | ARG_IN_SQUOTES)) {
223 bb_error_msg_and_die("unterminated (single or double) quote in options list: %s", src);
226 *dst = xrealloc(*dst, (tmp_str - *dst));
230 #define parse_command_string(src, dst) (0)
231 #endif /* ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS */
234 * This function reads aliases and default module options from a configuration file
235 * (/etc/modprobe.conf syntax). It supports includes (only files, no directories).
237 static void include_conf(struct dep_t **first, struct dep_t **current, char *buffer, int buflen, int fd)
239 int continuation_line = 0;
241 // alias parsing is not 100% correct (no correct handling of continuation lines within an alias)!
243 while (reads(fd, buffer, buflen)) {
246 *strchrnul(buffer, '#') = '\0';
250 while (l && isspace(buffer[l-1])) {
256 continuation_line = 0;
260 if (continuation_line)
263 if ((strncmp(buffer, "alias", 5) == 0) && isspace(buffer[5])) {
266 if (parse_tag_value(buffer + 6, &alias, &mod)) {
267 /* handle alias as a module dependent on the aliased module */
269 (*first) = (*current) = xzalloc(sizeof(struct dep_t));
271 (*current)->m_next = xzalloc(sizeof(struct dep_t));
272 (*current) = (*current)->m_next;
274 (*current)->m_name = xstrdup(alias);
275 (*current)->m_isalias = 1;
277 if ((strcmp(mod, "off") == 0) || (strcmp(mod, "null") == 0)) {
278 /*(*current)->m_depcnt = 0; - done by xzalloc */
279 /*(*current)->m_deparr = 0;*/
281 (*current)->m_depcnt = 1;
282 (*current)->m_deparr = xmalloc(sizeof(char *));
283 (*current)->m_deparr[0] = xstrdup(mod);
285 /*(*current)->m_next = NULL; - done by xzalloc */
287 } else if ((strncmp(buffer, "options", 7) == 0) && isspace(buffer[7])) {
290 /* split the line in the module/alias name, and options */
291 if (parse_tag_value(buffer + 8, &mod, &opt)) {
294 /* find the corresponding module */
295 for (dt = *first; dt; dt = dt->m_next) {
296 if (strcmp(dt->m_name, mod) == 0)
300 if (ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS) {
301 char* new_opt = NULL;
302 while ((opt = parse_command_string(opt, &new_opt))) {
303 dt->m_options = append_option(dt->m_options, new_opt);
306 dt->m_options = append_option(dt->m_options, opt);
310 } else if ((strncmp(buffer, "include", 7) == 0) && isspace(buffer[7])) {
314 filename = skip_whitespace(buffer + 8);
315 fdi = open(filename, O_RDONLY);
317 include_conf(first, current, buffer, buflen, fdi);
321 } /* while (reads(...)) */
325 * This function builds a list of dependency rules from /lib/modules/`uname -r`/modules.dep.
326 * It then fills every modules and aliases with their default options, found by parsing
327 * modprobe.conf (or modules.conf, or conf.modules).
329 static struct dep_t *build_dep(void)
333 struct dep_t *first = NULL;
334 struct dep_t *current = NULL;
336 int continuation_line = 0;
340 bb_error_msg_and_die("can't determine kernel version");
343 if (un.release[0] == '2') {
344 k_version = un.release[2] - '0';
347 filename = xasprintf("/lib/modules/%s/modules.dep", un.release);
348 fd = open(filename, O_RDONLY);
349 if (ENABLE_FEATURE_CLEAN_UP)
352 /* Ok, that didn't work. Fall back to looking in /lib/modules */
353 fd = open("/lib/modules/modules.dep", O_RDONLY);
355 bb_error_msg_and_die("cannot parse modules.dep");
359 while (reads(fd, line_buffer, sizeof(line_buffer))) {
360 int l = strlen(line_buffer);
363 while (l > 0 && isspace(line_buffer[l-1])) {
364 line_buffer[l-1] = '\0';
369 continuation_line = 0;
373 /* Is this a new module dep description? */
374 if (!continuation_line) {
375 /* find the dep beginning */
376 char *col = strchr(line_buffer, ':');
380 /* This line is a dep description */
385 /* Find the beginning of the module file name */
387 mods = bb_basename(line_buffer);
389 /* find the path of the module */
390 modpath = strchr(line_buffer, '/'); /* ... and this is the path */
392 modpath = line_buffer; /* module with no path */
393 /* find the end of the module name in the file name */
394 if (ENABLE_FEATURE_2_6_MODULES &&
395 (k_version > 4) && (col[-3] == '.') &&
396 (col[-2] == 'k') && (col[-1] == 'o'))
398 else if ((col[-2] == '.') && (col[-1] == 'o'))
401 mod = xstrndup(mods, dot - mods);
403 /* enqueue new module */
405 first = current = xzalloc(sizeof(struct dep_t));
407 current->m_next = xzalloc(sizeof(struct dep_t));
408 current = current->m_next;
410 current->m_name = mod;
411 current->m_path = xstrdup(modpath);
412 /*current->m_options = NULL; - xzalloc did it*/
413 /*current->m_isalias = 0;*/
414 /*current->m_depcnt = 0;*/
415 /*current->m_deparr = 0;*/
416 /*current->m_next = 0;*/
420 /* this line is not a dep description */
423 /* It's a dep description continuation */
426 while (p && *p && isblank(*p))
429 /* p points to the first dependable module; if NULL, no dependable module */
431 char *end = &line_buffer[l-1];
437 while (isblank(*end) || (*end == '\\'))
441 /* search the end of the dependency */
442 next = strchr(p, ' ');
449 /* find the beginning of the module file name */
450 deps = bb_basename(p);
452 while (isblank(*deps))
456 /* find the end of the module name in the file name */
457 if (ENABLE_FEATURE_2_6_MODULES
458 && (k_version > 4) && (next[-2] == '.')
459 && (next[-1] == 'k') && (next[0] == 'o'))
461 else if ((next[-1] == '.') && (next[0] == 'o'))
464 /* Cope with blank lines */
465 if ((next-deps-ext+1) <= 0)
467 dep = xstrndup(deps, next - deps - ext + 1);
469 /* Add the new dependable module name */
471 current->m_deparr = xrealloc(current->m_deparr,
472 sizeof(char *) * current->m_depcnt);
473 current->m_deparr[current->m_depcnt - 1] = dep;
476 } while (next < end);
479 /* is there other dependable module(s) ? */
480 continuation_line = (line_buffer[l-1] == '\\');
481 } /* while (reads(...)) */
485 * First parse system-specific options and aliases
486 * as they take precedence over the kernel ones.
487 * >=2.6: we only care about modprobe.conf
488 * <=2.4: we care about modules.conf and conf.modules
490 if (ENABLE_FEATURE_2_6_MODULES
491 && (fd = open("/etc/modprobe.conf", O_RDONLY)) < 0)
492 if (ENABLE_FEATURE_2_4_MODULES
493 && (fd = open("/etc/modules.conf", O_RDONLY)) < 0)
494 if (ENABLE_FEATURE_2_4_MODULES)
495 fd = open("/etc/conf.modules", O_RDONLY);
498 include_conf(&first, ¤t, line_buffer, sizeof(line_buffer), fd);
502 /* Only 2.6 has a modules.alias file */
503 if (ENABLE_FEATURE_2_6_MODULES) {
504 /* Parse kernel-declared module aliases */
505 filename = xasprintf("/lib/modules/%s/modules.alias", un.release);
506 fd = open(filename, O_RDONLY);
508 /* Ok, that didn't work. Fall back to looking in /lib/modules */
509 fd = open("/lib/modules/modules.alias", O_RDONLY);
511 if (ENABLE_FEATURE_CLEAN_UP)
515 include_conf(&first, ¤t, line_buffer, sizeof(line_buffer), fd);
519 /* Parse kernel-declared symbol aliases */
520 filename = xasprintf("/lib/modules/%s/modules.symbols", un.release);
521 fd = open(filename, O_RDONLY);
523 /* Ok, that didn't work. Fall back to looking in /lib/modules */
524 fd = open("/lib/modules/modules.symbols", O_RDONLY);
526 if (ENABLE_FEATURE_CLEAN_UP)
530 include_conf(&first, ¤t, line_buffer, sizeof(line_buffer), fd);
538 /* return 1 = loaded, 0 = not loaded, -1 = can't tell */
539 static int already_loaded(const char *name)
543 fd = open("/proc/modules", O_RDONLY);
547 while (reads(fd, line_buffer, sizeof(line_buffer))) {
550 p = strchr(line_buffer, ' ');
554 // Truncate buffer at first space and check for matches, with
555 // the idiosyncrasy that _ and - are interchangeable because the
556 // 2.6 kernel does weird things.
559 for (p = line_buffer, n = name; ; p++, n++) {
561 if ((*p == '_' || *p == '-') && (*n == '_' || *n == '-'))
565 // If we made it to the end, that's a match.
578 static int mod_process(const struct mod_list_t *list, int do_insert)
582 struct mod_opt_t *opts;
583 int argc_malloc; /* never used when CONFIG_FEATURE_CLEAN_UP not defined */
588 if (ENABLE_FEATURE_CLEAN_UP)
590 /* If CONFIG_FEATURE_CLEAN_UP is not defined, then we leak memory
591 * each time we allocate memory for argv.
592 * But it is (quite) small amounts of memory that leak each
593 * time a module is loaded, and it is reclaimed when modprobe
594 * exits anyway (even when standalone shell?).
595 * This could become a problem when loading a module with LOTS of
596 * dependencies, with LOTS of options for each dependencies, with
597 * very little memory on the target... But in that case, the module
598 * would not load because there is no more memory, so there's no
600 /* enough for minimal insmod (5 args + NULL) or rmmod (3 args + NULL) */
601 argv = xmalloc(6 * sizeof(char*));
603 if (already_loaded(list->m_name) != 1) {
604 argv[argc++] = (char*)"insmod";
605 if (ENABLE_FEATURE_2_4_MODULES) {
607 argv[argc++] = (char*)"-s";
609 argv[argc++] = (char*)"-k";
611 argv[argc++] = (char*)"-q";
612 else if (verbose) /* verbose and quiet are mutually exclusive */
613 argv[argc++] = (char*)"-v";
615 argv[argc++] = list->m_path;
616 if (ENABLE_FEATURE_CLEAN_UP)
618 opts = list->m_options;
620 /* Add one more option */
622 argv = xrealloc(argv, (argc + 1) * sizeof(char*));
623 argv[argc-1] = opts->m_opt_val;
628 /* modutils uses short name for removal */
629 if (already_loaded(list->m_name) != 0) {
630 argv[argc++] = (char*)"rmmod";
632 argv[argc++] = (char*)"-s";
633 argv[argc++] = (char*)list->m_name;
634 if (ENABLE_FEATURE_CLEAN_UP)
642 printf("%s module %s\n", do_insert?"Loading":"Unloading", list->m_name);
645 int rc2 = wait4pid(spawn(argv));
648 rc = rc2; /* only last module matters */
650 rc = 0; /* success if remove any mod */
653 if (ENABLE_FEATURE_CLEAN_UP) {
654 /* the last value in the array has index == argc, but
655 * it is the terminating NULL, so we must not free it. */
656 while (argc_malloc < argc) {
657 free(argv[argc_malloc++]);
661 if (ENABLE_FEATURE_CLEAN_UP) {
665 list = do_insert ? list->m_prev : list->m_next;
667 return (show_only) ? 0 : rc;
671 * Check the matching between a pattern and a module name.
672 * We need this as *_* is equivalent to *-*, even in pattern matching.
674 static int check_pattern(const char* pat_src, const char* mod_src)
678 if (ENABLE_FEATURE_MODPROBE_FANCY_ALIAS) {
683 pat = xstrdup(pat_src);
684 mod = xstrdup(mod_src);
686 for (p = pat; (p = strchr(p, '-')); *p++ = '_');
687 for (p = mod; (p = strchr(p, '-')); *p++ = '_');
689 ret = fnmatch(pat, mod, 0);
691 if (ENABLE_FEATURE_CLEAN_UP) {
698 return fnmatch(pat_src, mod_src, 0);
702 * Builds the dependency list (aka stack) of a module.
703 * head: the highest module in the stack (last to insmod, first to rmmod)
704 * tail: the lowest module in the stack (first to insmod, last to rmmod)
706 static void check_dep(char *mod, struct mod_list_t **head, struct mod_list_t **tail)
708 struct mod_list_t *find;
710 struct mod_opt_t *opt = NULL;
713 /* Search for the given module name amongst all dependency rules.
714 * The module name in a dependency rule can be a shell pattern,
715 * so try to match the given module name against such a pattern.
716 * Of course if the name in the dependency rule is a plain string,
717 * then we consider it a pattern, and matching will still work. */
718 for (dt = depend; dt; dt = dt->m_next) {
719 if (check_pattern(dt->m_name, mod) == 0) {
725 bb_error_msg("module %s not found", mod);
729 // resolve alias names
730 while (dt->m_isalias) {
731 if (dt->m_depcnt == 1) {
734 for (adt = depend; adt; adt = adt->m_next) {
735 if (check_pattern(adt->m_name, dt->m_deparr[0]) == 0)
739 /* This is the module we are aliased to */
740 struct mod_opt_t *opts = dt->m_options;
741 /* Option of the alias are appended to the options of the module */
743 adt->m_options = append_option(adt->m_options, opts->m_opt_val);
748 bb_error_msg("module %s not found", mod);
752 bb_error_msg("bad alias %s", dt->m_name);
761 // search for duplicates
762 for (find = *head; find; find = find->m_next) {
763 if (strcmp(mod, find->m_name) == 0) {
764 // found -> dequeue it
767 find->m_prev->m_next = find->m_next;
769 *head = find->m_next;
772 find->m_next->m_prev = find->m_prev;
774 *tail = find->m_prev;
776 break; // there can be only one duplicate
780 if (!find) { // did not find a duplicate
781 find = xzalloc(sizeof(struct mod_list_t));
784 find->m_options = opt;
789 (*tail)->m_next = find;
790 find->m_prev = *tail;
791 find->m_next = NULL; /* possibly NOT done by xzalloc! */
800 /* Add all dependable module for that new module */
801 for (i = 0; i < dt->m_depcnt; i++)
802 check_dep(dt->m_deparr[i], head, tail);
806 static int mod_insert(char *mod, int argc, char **argv)
808 struct mod_list_t *tail = NULL;
809 struct mod_list_t *head = NULL;
812 // get dep list for module mod
813 check_dep(mod, &head, &tail);
819 // append module args
820 for (i = 0; i < argc; i++)
821 head->m_options = append_option(head->m_options, argv[i]);
824 // process tail ---> head
825 rc = mod_process(tail, 1);
828 * In case of using udev, multiple instances of modprobe can be
829 * spawned to load the same module (think of two same usb devices,
830 * for example; or cold-plugging at boot time). Thus we shouldn't
831 * fail if the module was loaded, and not by us.
833 if (already_loaded(mod))
840 static int mod_remove(char *mod)
843 static const struct mod_list_t rm_a_dummy = { "-a", NULL, NULL, NULL, NULL };
845 struct mod_list_t *head = NULL;
846 struct mod_list_t *tail = NULL;
849 check_dep(mod, &head, &tail);
851 head = tail = (struct mod_list_t*) &rm_a_dummy;
855 rc = mod_process(head, 0); // process head ---> tail
859 int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
860 int modprobe_main(int argc, char **argv)
862 int rc = EXIT_SUCCESS;
865 opt_complementary = "q-v:v-q";
866 getopt32(argv, MAIN_OPT_STR, &unused, &unused);
868 if (option_mask32 & (DUMP_CONF_EXIT | LIST_ALL))
870 if (option_mask32 & (RESTRICT_DIR | CONFIG_FILE))
871 bb_error_msg_and_die("-t and -C not supported");
873 depend = build_dep();
876 bb_error_msg_and_die("cannot parse modules.dep");
880 /* argv[optind] can be NULL here */
881 if (mod_remove(argv[optind])) {
882 bb_error_msg("failed to remove module %s",
886 } while (++optind < argc);
889 bb_error_msg_and_die("no module or pattern provided");
891 if (mod_insert(argv[optind], argc - optind - 1, argv + optind + 1))
892 bb_error_msg_and_die("failed to load module %s", argv[optind]);
895 /* Here would be a good place to free up memory allocated during the dependencies build. */