1 /* modprobe.c: add or remove a module from the kernel, intelligently.
2 Copyright (C) 2001 Rusty Russell.
3 Copyright (C) 2002, 2003 Rusty Russell, IBM Corporation.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #define _GNU_SOURCE /* asprintf */
21 #include <sys/utsname.h>
22 #include <sys/types.h>
38 #include <asm/unistd.h>
44 #include "zlibsupport.h"
48 #include "config_filter.h"
52 int use_binary_indexes
= 1; /* default to enabled. */
54 /* Limit do_softdep/do_modprobe recursion.
55 * This is a simple way to handle dependency loops
56 * caused by poorly written softdep commands.
58 static int recursion_depth
= 0;
59 const int MAX_RECURSION
= 50; /* Arbitrary choice */
61 extern long init_module(void *, unsigned long, const char *);
62 extern long delete_module(const char *, unsigned int);
65 struct list_head list
;
75 mit_use_blacklist
= 8,
76 mit_ignore_commands
= 16,
77 mit_ignore_loaded
= 32,
79 mit_strip_vermagic
= 128,
80 mit_strip_modversion
= 256,
81 mit_resolve_alias
= 512
86 #define MODULE_DIR "/lib/modules"
89 static void print_usage(const char *progname
)
92 "Usage: %s [-v] [-V] [-C config-file] [-d <dirname> ] [-n] [-i] [-q] [-b] [-o <modname>] [ --dump-modversions ] <modname> [parameters...]\n"
93 "%s -r [-n] [-i] [-v] <modulename> ...\n"
94 "%s -l -t <dirname> [ -a <modulename> ...]\n",
95 progname
, progname
, progname
);
99 static struct module
*find_module(const char *filename
, struct list_head
*list
)
103 list_for_each_entry(i
, list
, list
) {
104 if (streq(i
->filename
, filename
))
110 static void add_module(char *filename
, int namelen
, struct list_head
*list
)
114 /* If it's a duplicate: move it to the end, so it gets
115 inserted where it is *first* required. */
116 mod
= find_module(filename
, list
);
118 list_del(&mod
->list
);
120 /* No match. Create a new module. */
121 mod
= NOFAIL(malloc(sizeof(struct module
) + namelen
+ 1));
122 memcpy(mod
->filename
, filename
, namelen
);
123 mod
->filename
[namelen
] = '\0';
124 mod
->modname
= NOFAIL(malloc(namelen
+ 1));
125 filename2modname(mod
->modname
, mod
->filename
);
128 list_add_tail(&mod
->list
, list
);
131 static void free_module(struct module
*mod
)
137 /* Compare len chars of a to b, with _ and - equivalent. */
138 static int modname_equal(const char *a
, const char *b
, unsigned int len
)
142 if (strlen(b
) != len
)
145 for (i
= 0; i
< len
; i
++) {
146 if ((a
[i
] == '_' || a
[i
] == '-')
147 && (b
[i
] == '_' || b
[i
] == '-'))
155 /* Fills in list of modules if this is the line we want. */
156 static int add_modules_dep_line(char *line
,
158 struct list_head
*list
,
163 char *modname
, *fullpath
;
165 /* Ignore lines without : or which start with a # */
166 ptr
= strchr(line
, ':');
167 if (ptr
== NULL
|| line
[strspn(line
, "\t ")] == '#')
170 /* Is this the module we are looking for? */
172 modname
= my_basename(line
);
174 len
= strlen(modname
);
175 if (strchr(modname
, '.'))
176 len
= strchr(modname
, '.') - modname
;
177 if (!modname_equal(modname
, name
, len
))
180 /* Create the list. */
181 if ('/' == line
[0]) { /* old style deps - absolute path specified */
182 add_module(line
, ptr
- line
, list
);
184 nofail_asprintf(&fullpath
, "%s/%s", dirname
, line
);
185 add_module(fullpath
, strlen(dirname
)+1+(ptr
- line
), list
);
192 ptr
+= strspn(ptr
, " \t");
196 ptr
+= strcspn(ptr
, " \t");
197 if ('/' == dep_start
[0]) { /* old style deps */
198 add_module(dep_start
, ptr
- dep_start
, list
);
200 nofail_asprintf(&fullpath
, "%s/%s", dirname
, dep_start
);
202 strlen(dirname
)+1+(ptr
- dep_start
), list
);
209 static int read_depends_file(const char *dirname
,
210 const char *start_name
,
211 struct list_head
*list
)
213 char *modules_dep_name
;
215 struct index_file
*modules_dep
;
217 nofail_asprintf(&modules_dep_name
, "%s/%s", dirname
, "modules.dep.bin");
218 modules_dep
= index_file_open(modules_dep_name
);
220 free(modules_dep_name
);
224 line
= index_search(modules_dep
, start_name
);
226 /* Value is standard dependency line format */
227 if (!add_modules_dep_line(line
, start_name
, list
, dirname
))
228 fatal("Module index is inconsistent\n");
232 index_file_close(modules_dep
);
233 free(modules_dep_name
);
238 static void read_depends(const char *dirname
,
239 const char *start_name
,
240 struct list_head
*list
)
242 char *modules_dep_name
;
247 if (use_binary_indexes
)
248 if (read_depends_file(dirname
, start_name
, list
))
251 nofail_asprintf(&modules_dep_name
, "%s/%s", dirname
, "modules.dep");
252 modules_dep
= fopen(modules_dep_name
, "r");
254 fatal("Could not load %s: %s\n",
255 modules_dep_name
, strerror(errno
));
257 /* Stop at first line, as we can have duplicates (eg. symlinks
259 while (!done
&& (line
= getline_wrapped(modules_dep
, NULL
)) != NULL
) {
260 done
= add_modules_dep_line(line
, start_name
, list
, dirname
);
264 free(modules_dep_name
);
267 /* We use error numbers in a loose translation... */
268 static const char *insert_moderror(int err
)
272 return "Invalid module format";
274 return "Unknown symbol in module, or unknown parameter (see dmesg)";
276 return "Kernel does not have module support";
278 return strerror(err
);
282 static const char *remove_moderror(int err
)
286 return "No such module";
288 return "Kernel does not have module unloading support";
290 return strerror(err
);
294 static void clear_magic(struct elf_file
*module
)
296 struct string_table
*tbl
;
299 /* Old-style: __vermagic section */
300 module
->ops
->strip_section(module
, "__vermagic");
302 /* New-style: in .modinfo section */
303 tbl
= module
->ops
->load_strings(module
, ".modinfo", NULL
);
304 for (j
= 0; tbl
&& j
< tbl
->cnt
; j
++) {
305 const char *p
= tbl
->str
[j
];
306 if (strstarts(p
, "vermagic=")) {
307 memset((char *)p
, 0, strlen(p
));
313 struct module_options
315 struct module_options
*next
;
320 struct module_command
322 struct module_command
*next
;
329 struct module_alias
*next
;
334 struct module_blacklist
336 struct module_blacklist
*next
;
340 struct module_softdep
342 struct module_softdep
*next
;
344 /* The modname and string tables point to buf. */
346 struct string_table
*pre
;
347 struct string_table
*post
;
352 struct module_options
*options
;
353 struct module_command
*commands
;
354 struct module_alias
*aliases
;
355 struct module_blacklist
*blacklist
;
356 struct module_softdep
*softdeps
;
359 /* Link in a new option line from the config file. */
360 static struct module_options
*
361 add_options(const char *modname
,
363 struct module_options
*options
)
365 struct module_options
*new;
368 new = NOFAIL(malloc(sizeof(*new)));
369 new->modulename
= NOFAIL(strdup(modname
));
370 new->options
= NOFAIL(strdup(option
));
371 /* We can handle tabs, kernel can't. */
372 for (tab
= strchr(new->options
, '\t'); tab
; tab
= strchr(tab
, '\t'))
378 /* Link in a new install line from the config file. */
379 static struct module_command
*
380 add_command(const char *modname
,
382 struct module_command
*commands
)
384 struct module_command
*new;
386 new = NOFAIL(malloc(sizeof(*new)));
387 new->modulename
= NOFAIL(strdup(modname
));
388 new->command
= NOFAIL(strdup(command
));
389 new->next
= commands
;
393 /* Link in a new alias line from the config file. */
394 static struct module_alias
*
395 add_alias(const char *aliasname
, const char *modname
, struct module_alias
*aliases
)
397 struct module_alias
*new;
399 new = NOFAIL(malloc(sizeof(*new)));
400 new->aliasname
= NOFAIL(strdup(aliasname
));
401 new->module
= NOFAIL(strdup(modname
));
407 /* Return a list of matching aliases */
408 static struct module_alias
*
409 find_aliases(const struct module_alias
*aliases
,
412 struct module_alias
*result
= NULL
;
414 char *aliasname
= aliases
->aliasname
;
415 char *modname
= aliases
->module
;
416 if (fnmatch(aliasname
, name
, 0) == 0)
417 result
= add_alias(aliasname
, modname
, result
);
418 aliases
= aliases
->next
;
423 static void free_aliases(struct module_alias
*alias_list
)
426 struct module_alias
*alias
;
429 alias_list
= alias_list
->next
;
431 free(alias
->aliasname
);
437 /* Link in a new blacklist line from the config file. */
438 static struct module_blacklist
*
439 add_blacklist(const char *modname
, struct module_blacklist
*blacklist
)
441 struct module_blacklist
*new;
443 new = NOFAIL(malloc(sizeof(*new)));
444 new->modulename
= NOFAIL(strdup(modname
));
445 new->next
= blacklist
;
449 /* Find blacklist commands if any. */
451 find_blacklist(const char *modname
, const struct module_blacklist
*blacklist
)
454 if (streq(blacklist
->modulename
, modname
))
456 blacklist
= blacklist
->next
;
461 /* delete backlisted elems from a list of aliases */
463 apply_blacklist(struct module_alias
**aliases
,
464 const struct module_blacklist
*blacklist
)
466 struct module_alias
*result
= NULL
;
467 struct module_alias
*alias
= *aliases
;
469 char *modname
= alias
->module
;
470 if (!find_blacklist(modname
, blacklist
))
471 result
= add_alias(alias
->aliasname
, modname
, result
);
474 free_aliases(*aliases
);
478 /* Find install commands if any. */
479 static const char *find_command(const char *modname
,
480 const struct module_command
*commands
)
483 if (fnmatch(commands
->modulename
, modname
, 0) == 0)
484 return commands
->command
;
485 commands
= commands
->next
;
490 /* Find soft dependencies, if any. */
491 static const struct module_softdep
*
492 find_softdep(const char *modname
, const struct module_softdep
*softdeps
)
495 if (fnmatch(softdeps
->modname
, modname
, 0) == 0)
497 softdeps
= softdeps
->next
;
502 static char *append_option(char *options
, const char *newoption
)
504 options
= NOFAIL(realloc(options
, strlen(options
) + 1
505 + strlen(newoption
) + 1));
506 if (strlen(options
)) strcat(options
, " ");
507 strcat(options
, newoption
);
511 static char *prepend_option(char *options
, const char *newoption
)
514 l1
= strlen(options
);
515 l2
= strlen(newoption
);
516 /* the resulting string will look like
517 * newoption + ' ' + options + '\0' */
519 options
= NOFAIL(realloc(options
, l2
+ 1 + l1
+ 1));
520 memmove(options
+ l2
+ 1, options
, l1
+ 1);
522 memcpy(options
, newoption
, l2
);
524 options
= NOFAIL(realloc(options
, l2
+ 1));
525 memcpy(options
, newoption
, l2
);
532 static char *add_extra_options(const char *modname
,
533 const char *optstring
,
534 const struct module_options
*options
)
536 char *opts
= NOFAIL(strdup(optstring
));
539 if (streq(options
->modulename
, modname
))
540 opts
= prepend_option(opts
, options
->options
);
541 options
= options
->next
;
546 /* Is module in /proc/modules? If so, fill in usecount if not NULL.
547 0 means no, 1 means yes, -1 means unknown.
549 static int module_in_procfs(const char *modname
, unsigned int *usecount
)
555 /* Might not be mounted yet. Don't fail. */
556 proc_modules
= fopen("/proc/modules", "r");
560 while ((line
= getline_wrapped(proc_modules
, NULL
)) != NULL
) {
561 char *entry
= strtok(line
, " \n");
563 if (entry
&& streq(entry
, modname
)) {
564 /* If it exists, usecount is the third entry. */
565 if (!strtok(NULL
, " \n"))
568 if (!(entry
= strtok(NULL
, " \n"))) /* usecount */
572 *usecount
= atoi(entry
);
574 /* Followed by - then status. */
575 if (strtok(NULL
, " \n")
576 && (entry
= strtok(NULL
, " \n")) != NULL
) {
577 /* No locking, we might hit cases
578 * where module is in flux. Spin. */
579 if (streq(entry
, "Loading")
580 || streq(entry
, "Unloading")) {
583 fclose(proc_modules
);
590 fclose(proc_modules
);
595 fclose(proc_modules
);
599 /* Read sysfs attribute into a buffer.
600 * returns: 1 = ok, 0 = attribute missing,
601 * -1 = file error (or empty file, but we don't care).
603 static int read_attribute(const char *filename
, char *buf
, size_t buflen
)
608 file
= fopen(filename
, "r");
610 return (errno
== ENOENT
) ? 0 : -1;
611 s
= fgets(buf
, buflen
, file
);
614 return (s
== NULL
) ? -1 : 1;
617 /* is this a built-in module?
618 * 0: no, 1: yes, -1: don't know
620 static int module_builtin(const char *dirname
, const char *modname
)
622 struct index_file
*index
;
623 char *filename
, *value
;
625 nofail_asprintf(&filename
, "%s/modules.builtin.bin", dirname
);
626 index
= index_file_open(filename
);
630 value
= index_search(index
, modname
);
632 return value
? 1 : 0;
635 /* Is module in /sys/module? If so, fill in usecount if not NULL.
636 0 means no, 1 means yes, -1 means unknown.
638 static int module_in_sysfs(const char *modname
, unsigned int *usecount
)
644 const int ATTR_LEN
= 16;
647 /* Check sysfs is mounted */
648 if (stat("/sys/module", &finfo
) < 0)
652 nofail_asprintf(&name
, "/sys/module/%s", modname
);
653 ret
= stat(name
, &finfo
);
656 return (errno
== ENOENT
) ? 0 : -1; /* Not found or unknown. */
658 nofail_asprintf(&name
, "/sys/module/%s/initstate", modname
);
659 ret
= read_attribute(name
, attr
, ATTR_LEN
);
662 nofail_asprintf(&name
, "/sys/module/%s", modname
);
663 if (stat(name
, &finfo
) < 0) {
664 /* module was removed before we could read initstate */
667 /* initstate not available (2.6.19 or earlier) */
674 /* Wait for the existing module to either go live or disappear. */
675 while (ret
== 1 && !streq(attr
, "live\n")) {
677 ret
= read_attribute(name
, attr
, ATTR_LEN
);
684 /* Get reference count, if it exists. */
685 if (usecount
!= NULL
) {
686 nofail_asprintf(&name
, "/sys/module/%s/refcnt", modname
);
687 ret
= read_attribute(name
, attr
, ATTR_LEN
);
690 *usecount
= atoi(attr
);
696 /* Is module loaded? If so, fill in usecount if not NULL.
697 0 means no, 1 means yes, -1 means unknown.
699 static int module_in_kernel(const char *modname
, unsigned int *usecount
)
703 result
= module_in_sysfs(modname
, usecount
);
707 /* /sys/module/%s/initstate is only available since 2.6.20,
708 fallback to /proc/modules to get module state on earlier kernels. */
709 return module_in_procfs(modname
, usecount
);
712 void dump_modversions(const char *filename
, errfn_t error
)
714 struct elf_file
*module
;
716 module
= grab_elf_file(filename
);
718 error("%s: %s\n", filename
, strerror(errno
));
721 if (module
->ops
->dump_modvers(module
) < 0)
722 error("Wrong section size in '%s'\n", filename
);
723 release_elf_file(module
);
726 /* Does path contain directory(s) subpath? */
727 static int type_matches(const char *path
, const char *subpath
)
729 char *subpath_with_slashes
;
732 nofail_asprintf(&subpath_with_slashes
, "/%s/", subpath
);
734 ret
= (strstr(path
, subpath_with_slashes
) != NULL
);
735 free(subpath_with_slashes
);
740 static int do_wildcard(const char *dirname
,
742 const char *wildcard
)
744 char *modules_dep_name
;
748 /* Canonicalize wildcard */
749 wcard
= strdup(wildcard
);
752 nofail_asprintf(&modules_dep_name
, "%s/%s", dirname
, "modules.dep");
753 modules_dep
= fopen(modules_dep_name
, "r");
755 fatal("Could not load %s: %s\n",
756 modules_dep_name
, strerror(errno
));
758 while ((line
= getline_wrapped(modules_dep
, NULL
)) != NULL
) {
761 /* Ignore lines without : or which start with a # */
762 ptr
= strchr(line
, ':');
763 if (ptr
== NULL
|| line
[strspn(line
, "\t ")] == '#')
767 /* "type" must match complete directory component(s). */
768 if (!type
|| type_matches(line
, type
)) {
769 char modname
[strlen(line
)+1];
771 filename2modname(modname
, line
);
772 if (fnmatch(wcard
, modname
, 0) == 0)
773 printf("%s\n", line
);
779 free(modules_dep_name
);
784 static char *strsep_skipspace(char **string
, char *delim
)
788 *string
+= strspn(*string
, delim
);
789 return strsep(string
, delim
);
792 static int parse_config_scan(const char *filename
,
793 struct modprobe_conf
*conf
,
797 static int parse_config_file(const char *filename
,
798 struct modprobe_conf
*conf
,
803 unsigned int linenum
= 0;
806 struct module_options
**options
= &conf
->options
;
807 struct module_command
**commands
= &conf
->commands
;
808 struct module_alias
**aliases
= &conf
->aliases
;
809 struct module_blacklist
**blacklist
= &conf
->blacklist
;
811 cfile
= fopen(filename
, "r");
815 while ((line
= getline_wrapped(cfile
, &linenum
)) != NULL
) {
820 printf("%s\n", line
);
822 cmd
= strsep_skipspace(&ptr
, "\t ");
823 if (cmd
== NULL
|| cmd
[0] == '#' || cmd
[0] == '\0') {
828 if (streq(cmd
, "alias")) {
829 char *wildcard
= strsep_skipspace(&ptr
, "\t ");
830 char *realname
= strsep_skipspace(&ptr
, "\t ");
831 if (!wildcard
|| !realname
)
833 *aliases
= add_alias(underscores(wildcard
),
834 underscores(realname
),
836 } else if (streq(cmd
, "include")) {
837 struct modprobe_conf newconf
= *conf
;
838 newconf
.aliases
= NULL
;
840 newfilename
= strsep_skipspace(&ptr
, "\t ");
844 warn("\"include %s\" is deprecated, "
845 "please use /etc/modprobe.d\n", newfilename
);
846 if (strstarts(newfilename
, "/etc/modprobe.d")) {
847 warn("\"include /etc/modprobe.d\" is "
848 "the default, ignored\n");
850 if (!parse_config_scan(newfilename
,
853 warn("Failed to open included"
854 " config file %s: %s\n",
855 newfilename
, strerror(errno
));
857 /* Files included override aliases,
858 etc that was already set ... */
860 *aliases
= newconf
.aliases
;
862 } else if (streq(cmd
, "options")) {
863 modname
= strsep_skipspace(&ptr
, "\t ");
864 if (!modname
|| !ptr
)
867 ptr
+= strspn(ptr
, "\t ");
868 *options
= add_options(underscores(modname
),
871 } else if (streq(cmd
, "install")) {
872 modname
= strsep_skipspace(&ptr
, "\t ");
873 if (!modname
|| !ptr
)
876 ptr
+= strspn(ptr
, "\t ");
877 *commands
= add_command(underscores(modname
),
880 } else if (streq(cmd
, "blacklist")) {
881 modname
= strsep_skipspace(&ptr
, "\t ");
885 *blacklist
= add_blacklist(underscores(modname
),
888 } else if (streq(cmd
, "remove")) {
889 modname
= strsep_skipspace(&ptr
, "\t ");
890 if (!modname
|| !ptr
)
893 ptr
+= strspn(ptr
, "\t ");
894 *commands
= add_command(underscores(modname
),
897 } else if (streq(cmd
, "softdep")) {
899 int pre
= 0, post
= 0;
900 struct string_table
*pre_modnames
= NULL
;
901 struct string_table
*post_modnames
= NULL
;
902 struct module_softdep
*new;
904 modname
= strsep_skipspace(&ptr
, "\t ");
905 if (!modname
|| !ptr
)
907 modname
= underscores(modname
);
909 while ((tk
= strsep_skipspace(&ptr
, "\t ")) != NULL
) {
910 tk
= underscores(tk
);
912 if (streq(tk
, "pre:")) {
914 } else if (streq(tk
, "post:")) {
917 pre_modnames
= NOFAIL(
918 strtbl_add(tk
, pre_modnames
));
920 post_modnames
= NOFAIL(
921 strtbl_add(tk
, post_modnames
));
923 strtbl_free(pre_modnames
);
924 strtbl_free(post_modnames
);
928 new = NOFAIL(malloc(sizeof(*new)));
930 new->modname
= modname
;
931 new->pre
= pre_modnames
;
932 new->post
= post_modnames
;
933 new->next
= conf
->softdeps
;
934 conf
->softdeps
= new;
936 line
= NULL
; /* Don't free() this line. */
938 } else if (streq(cmd
, "config")) {
939 char *tmp
= strsep_skipspace(&ptr
, "\t ");
943 if (streq(tmp
, "binary_indexes")) {
944 tmp
= strsep_skipspace(&ptr
, "\t ");
945 if (streq(tmp
, "yes"))
946 use_binary_indexes
= 1;
947 if (streq(tmp
, "no"))
948 use_binary_indexes
= 0;
952 grammar(cmd
, filename
, linenum
);
961 /* Read binary index file containing aliases only */
962 static int read_aliases_file(const char *filename
,
965 struct module_alias
**aliases
)
967 struct index_value
*realnames
;
968 struct index_value
*realname
;
970 struct index_file
*index
;
972 nofail_asprintf(&binfile
, "%s.bin", filename
);
973 index
= index_file_open(binfile
);
980 index_dump(index
, stdout
, "alias ");
982 index_file_close(index
);
986 realnames
= index_searchwild(index
, name
);
987 for (realname
= realnames
; realname
; realname
= realname
->next
)
988 *aliases
= add_alias("*", realname
->value
, *aliases
);
989 index_values_free(realnames
);
992 index_file_close(index
);
996 /* fallback to plain-text aliases file if necessary */
997 static int read_aliases(const char *filename
,
1000 struct module_alias
**aliases
)
1003 unsigned int linenum
= 0;
1006 if (use_binary_indexes
)
1007 if (read_aliases_file(filename
, name
, dump_only
, aliases
))
1010 cfile
= fopen(filename
, "r");
1014 while ((line
= getline_wrapped(cfile
, &linenum
)) != NULL
) {
1019 printf("%s\n", line
);
1021 cmd
= strsep_skipspace(&ptr
, "\t ");
1022 if (cmd
== NULL
|| cmd
[0] == '#' || cmd
[0] == '\0') {
1027 if (streq(cmd
, "alias")) {
1028 char *wildcard
= strsep_skipspace(&ptr
, "\t ");
1029 char *realname
= strsep_skipspace(&ptr
, "\t ");
1030 if (!wildcard
|| !realname
)
1032 if (fnmatch(underscores(wildcard
),name
,0) == 0)
1033 *aliases
= add_alias(wildcard
,
1034 underscores(realname
),
1038 grammar(cmd
, filename
, linenum
);
1047 static int parse_config_scan(const char *filename
,
1048 struct modprobe_conf
*conf
,
1055 dir
= opendir(filename
);
1058 struct list_head node
;
1061 LIST_HEAD(files_list
);
1062 struct file_entry
*fe
, *fe_tmp
;
1065 /* sort files from directory into list */
1066 while ((i
= readdir(dir
)) != NULL
) {
1069 if (i
->d_name
[0] == '.')
1071 if (!config_filter(i
->d_name
))
1074 len
= strlen(i
->d_name
);
1076 (strcmp(&i
->d_name
[len
-5], ".conf") != 0 &&
1077 strcmp(&i
->d_name
[len
-6], ".alias") != 0))
1078 warn("All config files need .conf: %s/%s, "
1079 "it will be ignored in a future release.\n",
1080 filename
, i
->d_name
);
1081 fe
= malloc(sizeof(struct file_entry
) + len
+ 1);
1084 strcpy(fe
->name
, i
->d_name
);
1085 list_for_each_entry(fe_tmp
, &files_list
, node
)
1086 if (strcmp(fe_tmp
->name
, fe
->name
) >= 0)
1088 list_add_tail(&fe
->node
, &fe_tmp
->node
);
1092 /* parse list of files */
1093 list_for_each_entry_safe(fe
, fe_tmp
, &files_list
, node
) {
1096 nofail_asprintf(&cfgfile
, "%s/%s", filename
, fe
->name
);
1097 if (!parse_config_file(cfgfile
, conf
,
1098 dump_only
, removing
))
1099 warn("Failed to open config file "
1100 "%s: %s\n", fe
->name
, strerror(errno
));
1102 list_del(&fe
->node
);
1108 if (parse_config_file(filename
, conf
, dump_only
, removing
))
1114 static void parse_toplevel_config(const char *filename
,
1115 struct modprobe_conf
*conf
,
1120 if (!parse_config_scan(filename
, conf
, dump_only
, removing
))
1121 fatal("Failed to open config file %s: %s\n",
1122 filename
, strerror(errno
));
1126 /* deprecated config file */
1127 if (parse_config_file("/etc/modprobe.conf", conf
,
1128 dump_only
, removing
) > 0)
1129 warn("Deprecated config file /etc/modprobe.conf, "
1130 "all config files belong into /etc/modprobe.d/.\n");
1132 /* default config */
1133 parse_config_scan("/etc/modprobe.d", conf
, dump_only
, removing
);
1136 /* Read possible module arguments from the kernel command line. */
1137 static int parse_kcmdline(int dump_only
, struct module_options
**options
)
1140 unsigned int linenum
= 0;
1143 kcmdline
= fopen("/proc/cmdline", "r");
1147 while ((line
= getline_wrapped(kcmdline
, &linenum
)) != NULL
) {
1151 while ((arg
= strsep_skipspace(&ptr
, "\t ")) != NULL
) {
1152 char *sep
, *modname
, *opt
;
1154 sep
= strchr(arg
, '.');
1156 if (!strchr(sep
, '='))
1163 printf("options %s %s\n", modname
, opt
);
1165 *options
= add_options(underscores(modname
),
1176 static void add_to_env_var(const char *option
)
1180 if ((oldenv
= getenv("MODPROBE_OPTIONS")) != NULL
) {
1182 nofail_asprintf(&newenv
, "%s %s", oldenv
, option
);
1183 setenv("MODPROBE_OPTIONS", newenv
, 1);
1186 setenv("MODPROBE_OPTIONS", option
, 1);
1189 /* Prepend options from environment. */
1190 static char **merge_args(char *args
, char *argv
[], int *argc
)
1192 char *arg
, *argstring
;
1193 char **newargs
= NULL
;
1194 unsigned int i
, num_env
= 0;
1199 argstring
= NOFAIL(strdup(args
));
1200 for (arg
= strtok(argstring
, " "); arg
; arg
= strtok(NULL
, " ")) {
1202 newargs
= NOFAIL(realloc(newargs
,
1204 * (num_env
+ *argc
+ 1)));
1205 newargs
[num_env
] = arg
;
1211 /* Append commandline args */
1212 newargs
[0] = argv
[0];
1213 for (i
= 1; i
<= *argc
; i
++)
1214 newargs
[num_env
+i
] = argv
[i
];
1220 static char *gather_options(char *argv
[])
1222 char *optstring
= NOFAIL(strdup(""));
1224 /* Rest is module options */
1226 /* Quote value if it contains spaces. */
1227 unsigned int eq
= strcspn(*argv
, "=");
1229 if (strchr(*argv
+eq
, ' ') && !strchr(*argv
, '"')) {
1230 char quoted
[strlen(*argv
) + 3];
1232 sprintf(quoted
, "%s=\"%s\"", *argv
, *argv
+eq
+1);
1233 optstring
= append_option(optstring
, quoted
);
1235 optstring
= append_option(optstring
, *argv
);
1241 /* Do an install/remove command: replace $CMDLINE_OPTS if it's specified. */
1242 static void do_command(const char *modname
,
1243 const char *command
,
1247 const char *cmdline_opts
)
1250 char *p
, *replaced_cmd
= NOFAIL(strdup(command
));
1252 while ((p
= strstr(replaced_cmd
, "$CMDLINE_OPTS")) != NULL
) {
1254 nofail_asprintf(&new, "%.*s%s%s",
1255 (int)(p
- replaced_cmd
), replaced_cmd
, cmdline_opts
,
1256 p
+ strlen("$CMDLINE_OPTS"));
1261 info("%s %s\n", type
, replaced_cmd
);
1265 setenv("MODPROBE_MODULE", modname
, 1);
1266 ret
= system(replaced_cmd
);
1267 if (ret
== -1 || WEXITSTATUS(ret
))
1268 error("Error running %s command for %s\n", type
, modname
);
1274 /* Forward declaration */
1275 int do_modprobe(const char *modname
,
1276 const char *cmdline_opts
,
1277 const struct modprobe_conf
*conf
,
1278 const char *dirname
,
1280 modprobe_flags_t flags
);
1282 static void do_softdep(const struct module_softdep
*softdep
,
1283 const char *cmdline_opts
,
1284 const struct modprobe_conf
*conf
,
1285 const char *dirname
,
1287 modprobe_flags_t flags
)
1289 struct string_table
*pre_modnames
, *post_modnames
;
1290 modprobe_flags_t softdep_flags
= flags
;
1293 softdep_flags
&= ~mit_first_time
;
1294 softdep_flags
&= ~mit_ignore_commands
;
1295 if (flags
& mit_remove
)
1296 softdep_flags
|= mit_quiet_inuse
;
1298 if (++recursion_depth
>= MAX_RECURSION
)
1299 fatal("modprobe: softdep dependency loop encountered %s %s\n",
1300 (flags
& mit_remove
) ? "removing" : "inserting",
1303 if (flags
& mit_remove
) {
1304 /* Reverse module order if removing. */
1305 pre_modnames
= softdep
->post
;
1306 post_modnames
= softdep
->pre
;
1308 pre_modnames
= softdep
->pre
;
1309 post_modnames
= softdep
->post
;
1312 /* Modprobe pre_modnames */
1314 for (i
= 0; pre_modnames
&& i
< pre_modnames
->cnt
; i
++) {
1315 /* Reverse module order if removing. */
1316 j
= (flags
& mit_remove
) ? pre_modnames
->cnt
-1 - i
: i
;
1318 do_modprobe(pre_modnames
->str
[j
], "",
1319 conf
, dirname
, warn
, softdep_flags
);
1322 /* Modprobe main module, passing cmdline_opts, ignoring softdep */
1324 do_modprobe(softdep
->modname
, cmdline_opts
,
1325 conf
, dirname
, warn
, flags
| mit_ignore_commands
);
1327 /* Modprobe post_modnames */
1329 for (i
= 0; post_modnames
&& i
< post_modnames
->cnt
; i
++) {
1330 /* Reverse module order if removing. */
1331 j
= (flags
& mit_remove
) ? post_modnames
->cnt
-1 - i
: i
;
1333 do_modprobe(post_modnames
->str
[j
], "", conf
,
1334 dirname
, warn
, softdep_flags
);
1338 /* Actually do the insert. */
1339 static int insmod(struct list_head
*list
,
1340 const char *optstring
,
1341 const char *cmdline_opts
,
1342 const struct modprobe_conf
*conf
,
1343 const char *dirname
,
1345 modprobe_flags_t flags
)
1348 struct elf_file
*module
;
1349 const struct module_softdep
*softdep
;
1350 const char *command
;
1351 struct module
*mod
= list_entry(list
->next
, struct module
, list
);
1356 /* Take us off the list. */
1357 list_del(&mod
->list
);
1359 /* Do things we (or parent) depend on first. */
1360 if (!list_empty(list
)) {
1361 modprobe_flags_t f
= flags
;
1362 f
&= ~mit_first_time
;
1363 f
&= ~mit_ignore_commands
;
1364 if ((rc
= insmod(list
, "", "", conf
, dirname
, warn
, f
)) != 0)
1366 error("Error inserting %s (%s): %s\n",
1367 mod
->modname
, mod
->filename
,
1368 insert_moderror(errno
));
1373 /* Don't do ANYTHING if already in kernel. */
1374 already_loaded
= module_in_kernel(mod
->modname
, NULL
);
1376 if (!(flags
& mit_ignore_loaded
) && already_loaded
== 1) {
1377 if (flags
& mit_first_time
)
1378 error("Module %s already in kernel.\n", mod
->modname
);
1382 softdep
= find_softdep(mod
->modname
, conf
->softdeps
);
1383 if (softdep
&& !(flags
& mit_ignore_commands
)) {
1384 do_softdep(softdep
, cmdline_opts
, conf
, dirname
, error
, flags
);
1388 command
= find_command(mod
->modname
, conf
->commands
);
1389 if (command
&& !(flags
& mit_ignore_commands
)) {
1390 if (already_loaded
== -1) {
1391 warn("/sys/module/ not present or too old,"
1392 " and /proc/modules does not exist.\n");
1393 warn("Ignoring install commands for %s"
1394 " in case it is already loaded.\n",
1397 do_command(mod
->modname
, command
, flags
& mit_dry_run
,
1398 error
, "install", cmdline_opts
);
1403 module
= grab_elf_file(mod
->filename
);
1405 error("Could not read '%s': %s\n", mod
->filename
,
1406 (errno
== ENOEXEC
) ? "Invalid module format" :
1410 if (flags
& mit_strip_modversion
)
1411 module
->ops
->strip_section(module
, "__versions");
1412 if (flags
& mit_strip_vermagic
)
1413 clear_magic(module
);
1415 /* Config file might have given more options */
1416 opts
= add_extra_options(mod
->modname
, optstring
, conf
->options
);
1418 info("insmod %s %s\n", mod
->filename
, opts
);
1420 if (flags
& mit_dry_run
)
1423 ret
= init_module(module
->data
, module
->len
, opts
);
1425 if (errno
== EEXIST
) {
1426 if (flags
& mit_first_time
)
1427 error("Module %s already in kernel.\n",
1431 /* don't warn noisely if we're loading multiple aliases. */
1432 /* one of the aliases may try to use hardware we don't have. */
1433 if ((error
!= warn
) || (verbose
))
1434 error("Error inserting %s (%s): %s\n",
1435 mod
->modname
, mod
->filename
,
1436 insert_moderror(errno
));
1440 release_elf_file(module
);
1447 /* Do recursive removal. */
1448 static void rmmod(struct list_head
*list
,
1449 const char *cmdline_opts
,
1450 const struct modprobe_conf
*conf
,
1451 const char *dirname
,
1453 modprobe_flags_t flags
)
1455 const struct module_softdep
*softdep
;
1456 const char *command
;
1457 unsigned int usecount
= 0;
1458 struct module
*mod
= list_entry(list
->next
, struct module
, list
);
1461 /* Take first one off the list. */
1462 list_del(&mod
->list
);
1464 /* Don't do ANYTHING if not loaded. */
1465 exists
= module_in_kernel(mod
->modname
, &usecount
);
1467 goto nonexistent_module
;
1469 /* Even if renamed, find commands/softdeps to orig. name. */
1471 softdep
= find_softdep(mod
->modname
, conf
->softdeps
);
1472 if (softdep
&& !(flags
& mit_ignore_commands
)) {
1473 do_softdep(softdep
, cmdline_opts
, conf
, dirname
, error
, flags
);
1477 command
= find_command(mod
->modname
, conf
->commands
);
1478 if (command
&& !(flags
& mit_ignore_commands
)) {
1480 warn("/sys/module/ not present or too old,"
1481 " and /proc/modules does not exist.\n");
1482 warn("Ignoring remove commands for %s"
1483 " in case it is not loaded.\n",
1486 do_command(mod
->modname
, command
, flags
& mit_dry_run
,
1487 error
, "remove", cmdline_opts
);
1492 if (usecount
!= 0) {
1493 if (!(flags
& mit_quiet_inuse
))
1494 error("Module %s is in use.\n", mod
->modname
);
1498 info("rmmod %s\n", mod
->filename
);
1500 if (flags
& mit_dry_run
)
1503 if (delete_module(mod
->modname
, O_EXCL
) != 0) {
1504 if (errno
== ENOENT
)
1505 goto nonexistent_module
;
1506 error("Error removing %s (%s): %s\n",
1507 mod
->modname
, mod
->filename
,
1508 remove_moderror(errno
));
1512 /* Now do things we depend. */
1513 if (!list_empty(list
)) {
1514 flags
&= ~mit_first_time
;
1515 flags
&= ~mit_ignore_commands
;
1516 flags
|= mit_quiet_inuse
;
1518 rmmod(list
, "", conf
, dirname
, warn
, flags
);
1524 if (flags
& mit_first_time
)
1525 fatal("Module %s is not in kernel.\n", mod
->modname
);
1529 static int handle_module(const char *modname
,
1530 struct list_head
*todo_list
,
1531 const char *options
,
1532 const char *cmdline_opts
,
1533 const struct modprobe_conf
*conf
,
1534 const char *dirname
,
1536 modprobe_flags_t flags
)
1538 if (list_empty(todo_list
)) {
1539 const char *command
;
1541 /* The dependencies have to be real modules, but
1542 handle case where the first is completely bogus. */
1544 command
= find_command(modname
, conf
->commands
);
1545 if (command
&& !(flags
& mit_ignore_commands
)) {
1546 do_command(modname
, command
, flags
& mit_dry_run
, error
,
1547 (flags
& mit_remove
) ? "remove":"install", cmdline_opts
);
1552 error("Module %s not found.\n", modname
);
1556 if (flags
& mit_remove
)
1557 rmmod(todo_list
, cmdline_opts
,
1558 conf
, dirname
, error
, flags
);
1560 insmod(todo_list
, options
,
1561 cmdline_opts
, conf
, dirname
, error
, flags
);
1566 int handle_builtin_module(const char *modname
,
1568 modprobe_flags_t flags
)
1570 if (flags
& mit_remove
) {
1571 error("Module %s is builtin\n", modname
);
1573 } else if (flags
& mit_first_time
) {
1574 error("Module %s already in kernel (builtin).\n", modname
);
1576 } else if (flags
& mit_ignore_loaded
) {
1577 /* --show-depends given */
1578 info("builtin %s\n", modname
);
1583 int do_modprobe(const char *modname
,
1584 const char *cmdline_opts
,
1585 const struct modprobe_conf
*conf
,
1586 const char *dirname
,
1588 modprobe_flags_t flags
)
1590 struct module_alias
*matching_aliases
;
1594 matching_aliases
= find_aliases(conf
->aliases
, modname
);
1596 /* No luck? Try symbol names, if starts with symbol:. */
1597 if (!matching_aliases
&& strstarts(modname
, "symbol:")) {
1600 nofail_asprintf(&symfilename
, "%s/modules.symbols", dirname
);
1601 read_aliases(symfilename
, modname
, 0, &matching_aliases
);
1604 if (!matching_aliases
) {
1605 if(!strchr(modname
, ':'))
1606 read_depends(dirname
, modname
, &list
);
1608 /* We only use canned aliases as last resort. */
1609 if (list_empty(&list
)
1610 && !find_softdep(modname
, conf
->softdeps
)
1611 && !find_command(modname
, conf
->commands
))
1613 char *aliasfilename
;
1615 nofail_asprintf(&aliasfilename
, "%s/modules.alias",
1617 read_aliases(aliasfilename
, modname
, 0,
1619 free(aliasfilename
);
1620 /* builtin module? */
1621 if (!matching_aliases
&& module_builtin(dirname
, modname
) > 0) {
1622 failed
|= handle_builtin_module(modname
, error
,
1629 apply_blacklist(&matching_aliases
, conf
->blacklist
);
1630 if(flags
& mit_resolve_alias
) {
1631 struct module_alias
*aliases
= matching_aliases
;
1633 for(; aliases
; aliases
=aliases
->next
)
1634 printf("%s\n", aliases
->module
);
1637 if (matching_aliases
) {
1638 errfn_t err
= error
;
1639 struct module_alias
*aliases
= matching_aliases
;
1641 /* More than one alias? Don't bail out on failure. */
1645 /* Add the options for this alias. */
1647 opts
= add_extra_options(modname
,
1648 cmdline_opts
, conf
->options
);
1650 read_depends(dirname
, aliases
->module
, &list
);
1651 failed
|= handle_module(aliases
->module
,
1652 &list
, opts
, cmdline_opts
,
1653 conf
, dirname
, err
, flags
);
1655 aliases
= aliases
->next
;
1657 INIT_LIST_HEAD(&list
);
1660 if (flags
& mit_use_blacklist
1661 && find_blacklist(modname
, conf
->blacklist
))
1664 failed
|= handle_module(modname
, &list
, cmdline_opts
,
1665 cmdline_opts
, conf
, dirname
, error
, flags
);
1669 free_aliases(matching_aliases
);
1673 static struct option options
[] = { { "version", 0, NULL
, 'V' },
1674 { "verbose", 0, NULL
, 'v' },
1675 { "quiet", 0, NULL
, 'q' },
1676 { "syslog", 0, NULL
, 's' },
1677 { "show", 0, NULL
, 'n' },
1678 { "dry-run", 0, NULL
, 'n' },
1679 { "show-depends", 0, NULL
, 'D' },
1680 { "resolve-alias", 0, NULL
, 'R' },
1681 { "dirname", 1, NULL
, 'd' },
1682 { "set-version", 1, NULL
, 'S' },
1683 { "config", 1, NULL
, 'C' },
1684 { "remove", 0, NULL
, 'r' },
1685 { "showconfig", 0, NULL
, 'c' },
1686 { "list", 0, NULL
, 'l' },
1687 { "type", 1, NULL
, 't' },
1688 { "all", 0, NULL
, 'a' },
1689 { "ignore-install", 0, NULL
, 'i' },
1690 { "ignore-remove", 0, NULL
, 'i' },
1691 { "use-blacklist", 0, NULL
, 'b' },
1692 { "force", 0, NULL
, 'f' },
1693 { "force-vermagic", 0, NULL
, 1 },
1694 { "force-modversion", 0, NULL
, 2 },
1695 { "first-time", 0, NULL
, 3 },
1696 { "dump-modversions", 0, NULL
, 4 },
1697 { NULL
, 0, NULL
, 0 } };
1699 int main(int argc
, char *argv
[])
1702 struct stat statbuf
;
1704 int dump_config
= 0;
1707 int dump_modver
= 0;
1708 unsigned int i
, num_modules
;
1710 const char *configname
= NULL
;
1712 char *cmdline_opts
= NULL
;
1714 errfn_t error
= fatal
;
1716 modprobe_flags_t flags
= 0;
1717 struct modprobe_conf conf
= {};
1719 recursion_depth
= 0;
1721 /* Prepend options from environment. */
1722 argv
= merge_args(getenv("MODPROBE_OPTIONS"), argv
, &argc
);
1725 while ((opt
= getopt_long(argc
, argv
, "Vvqsnd:S:C:DRrclt:aibf", options
, NULL
)) != -1){
1728 puts(PACKAGE
" version " VERSION
);
1731 add_to_env_var("-v");
1736 add_to_env_var("-q");
1739 add_to_env_var("-s");
1743 flags
|= mit_dry_run
;
1749 strncpy(buf
.release
, optarg
, sizeof(buf
.release
));
1750 buf
.release
[sizeof(buf
.release
)-1] = '\0';
1753 configname
= optarg
;
1754 add_to_env_var("-C");
1755 add_to_env_var(configname
);
1758 flags
|= mit_dry_run
;
1759 flags
|= mit_ignore_loaded
;
1763 flags
|= mit_resolve_alias
;
1766 flags
|= mit_remove
;
1782 flags
|= mit_ignore_commands
;
1785 flags
|= mit_use_blacklist
;
1788 flags
|= mit_strip_vermagic
;
1789 flags
|= mit_strip_modversion
;
1792 flags
|= mit_strip_vermagic
;
1795 flags
|= mit_strip_modversion
;
1798 flags
|= mit_first_time
;
1804 print_usage(argv
[0]);
1808 /* If stderr not open, go to syslog */
1809 if (logging
|| fstat(STDERR_FILENO
, &statbuf
) != 0) {
1810 openlog("modprobe", LOG_CONS
, LOG_DAEMON
);
1814 if (argc
< optind
+ 1 && !dump_config
&& !list_only
)
1815 print_usage(argv
[0]);
1817 nofail_asprintf(&dirname
, "%s%s/%s", basedir
, MODULE_DIR
, buf
.release
);
1819 /* Old-style -t xxx wildcard? Only with -l. */
1821 if (optind
+1 < argc
)
1822 fatal("Can't have multiple wildcards\n");
1823 /* fprintf(stderr, "man find\n"); return 1; */
1824 failed
= do_wildcard(dirname
, type
, argv
[optind
]?:"*");
1828 fatal("-t only supported with -l");
1831 dump_modversions(argv
[optind
], error
);
1835 /* Read aliases, options etc. */
1836 parse_toplevel_config(configname
, &conf
, dump_config
, flags
& mit_remove
);
1838 /* Read module options from kernel command line */
1839 parse_kcmdline(dump_config
, &conf
.options
);
1842 char *aliasfilename
, *symfilename
;
1843 struct modprobe_conf conf
= {};
1845 nofail_asprintf(&aliasfilename
, "%s/modules.alias", dirname
);
1846 nofail_asprintf(&symfilename
, "%s/modules.symbols", dirname
);
1848 read_aliases(aliasfilename
, "", 1, &conf
.aliases
);
1849 read_aliases(symfilename
, "", 1, &conf
.aliases
);
1854 if ((flags
& mit_remove
) || all
) {
1855 num_modules
= argc
- optind
;
1856 cmdline_opts
= NOFAIL(strdup(""));
1859 cmdline_opts
= gather_options(argv
+optind
+1);
1862 /* Convert names we are looking for */
1863 for (i
= 0; i
< num_modules
; i
++)
1864 underscores(argv
[optind
+ i
]);
1866 /* If we have a list of modules to remove, try the unused ones first.
1867 Aliases and modules which don't seem to exist are handled later. */
1868 if (flags
& mit_remove
) {
1873 for (i
= 0; i
< num_modules
; i
++) {
1874 const char *modname
;
1878 modname
= argv
[optind
+ i
];
1881 if (module_in_kernel(modname
, &usecount
) != 1)
1886 read_depends(dirname
, modname
, &list
);
1888 failed
|= handle_module(modname
, &list
,
1889 cmdline_opts
, cmdline_opts
,
1890 &conf
, dirname
, error
, flags
);
1892 argv
[optind
+ i
] = NULL
;
1893 INIT_LIST_HEAD(&list
);
1895 } while (progress
> 0);
1898 /* num_modules is always 1 except for -r or -a. */
1899 for (i
= 0; i
< num_modules
; i
++) {
1900 const char *modname
= argv
[optind
+ i
];
1905 failed
|= do_modprobe(modname
, cmdline_opts
,
1906 &conf
, dirname
, error
, flags
);
1914 /* Don't bother to free conf */