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
= underscores(strsep_skipspace(&ptr
, "\t "));
905 if (!modname
|| !ptr
)
907 while ((tk
= strsep_skipspace(&ptr
, "\t ")) != NULL
) {
908 if (streq(tk
, "pre:")) {
910 } else if (streq(tk
, "post:")) {
913 pre_modnames
= NOFAIL(
914 strtbl_add(tk
, pre_modnames
));
916 post_modnames
= NOFAIL(
917 strtbl_add(tk
, post_modnames
));
919 strtbl_free(pre_modnames
);
920 strtbl_free(post_modnames
);
924 new = NOFAIL(malloc(sizeof(*new)));
926 new->modname
= modname
;
927 new->pre
= pre_modnames
;
928 new->post
= post_modnames
;
929 new->next
= conf
->softdeps
;
930 conf
->softdeps
= new;
932 line
= NULL
; /* Don't free() this line. */
934 } else if (streq(cmd
, "config")) {
935 char *tmp
= strsep_skipspace(&ptr
, "\t ");
939 if (streq(tmp
, "binary_indexes")) {
940 tmp
= strsep_skipspace(&ptr
, "\t ");
941 if (streq(tmp
, "yes"))
942 use_binary_indexes
= 1;
943 if (streq(tmp
, "no"))
944 use_binary_indexes
= 0;
948 grammar(cmd
, filename
, linenum
);
957 /* Read binary index file containing aliases only */
958 static int read_aliases_file(const char *filename
,
961 struct module_alias
**aliases
)
963 struct index_value
*realnames
;
964 struct index_value
*realname
;
966 struct index_file
*index
;
968 nofail_asprintf(&binfile
, "%s.bin", filename
);
969 index
= index_file_open(binfile
);
976 index_dump(index
, stdout
, "alias ");
978 index_file_close(index
);
982 realnames
= index_searchwild(index
, name
);
983 for (realname
= realnames
; realname
; realname
= realname
->next
)
984 *aliases
= add_alias("*", realname
->value
, *aliases
);
985 index_values_free(realnames
);
988 index_file_close(index
);
992 /* fallback to plain-text aliases file if necessary */
993 static int read_aliases(const char *filename
,
996 struct module_alias
**aliases
)
999 unsigned int linenum
= 0;
1002 if (use_binary_indexes
)
1003 if (read_aliases_file(filename
, name
, dump_only
, aliases
))
1006 cfile
= fopen(filename
, "r");
1010 while ((line
= getline_wrapped(cfile
, &linenum
)) != NULL
) {
1015 printf("%s\n", line
);
1017 cmd
= strsep_skipspace(&ptr
, "\t ");
1018 if (cmd
== NULL
|| cmd
[0] == '#' || cmd
[0] == '\0') {
1023 if (streq(cmd
, "alias")) {
1024 char *wildcard
= strsep_skipspace(&ptr
, "\t ");
1025 char *realname
= strsep_skipspace(&ptr
, "\t ");
1026 if (!wildcard
|| !realname
)
1028 if (fnmatch(underscores(wildcard
),name
,0) == 0)
1029 *aliases
= add_alias(wildcard
,
1030 underscores(realname
),
1034 grammar(cmd
, filename
, linenum
);
1043 static int parse_config_scan(const char *filename
,
1044 struct modprobe_conf
*conf
,
1051 dir
= opendir(filename
);
1054 struct list_head node
;
1057 LIST_HEAD(files_list
);
1058 struct file_entry
*fe
, *fe_tmp
;
1061 /* sort files from directory into list */
1062 while ((i
= readdir(dir
)) != NULL
) {
1065 if (i
->d_name
[0] == '.')
1067 if (!config_filter(i
->d_name
))
1070 len
= strlen(i
->d_name
);
1072 (strcmp(&i
->d_name
[len
-5], ".conf") != 0 &&
1073 strcmp(&i
->d_name
[len
-6], ".alias") != 0))
1074 warn("All config files need .conf: %s/%s, "
1075 "it will be ignored in a future release.\n",
1076 filename
, i
->d_name
);
1077 fe
= malloc(sizeof(struct file_entry
) + len
+ 1);
1080 strcpy(fe
->name
, i
->d_name
);
1081 list_for_each_entry(fe_tmp
, &files_list
, node
)
1082 if (strcmp(fe_tmp
->name
, fe
->name
) >= 0)
1084 list_add_tail(&fe
->node
, &fe_tmp
->node
);
1088 /* parse list of files */
1089 list_for_each_entry_safe(fe
, fe_tmp
, &files_list
, node
) {
1092 nofail_asprintf(&cfgfile
, "%s/%s", filename
, fe
->name
);
1093 if (!parse_config_file(cfgfile
, conf
,
1094 dump_only
, removing
))
1095 warn("Failed to open config file "
1096 "%s: %s\n", fe
->name
, strerror(errno
));
1098 list_del(&fe
->node
);
1104 if (parse_config_file(filename
, conf
, dump_only
, removing
))
1110 static void parse_toplevel_config(const char *filename
,
1111 struct modprobe_conf
*conf
,
1116 if (!parse_config_scan(filename
, conf
, dump_only
, removing
))
1117 fatal("Failed to open config file %s: %s\n",
1118 filename
, strerror(errno
));
1122 /* deprecated config file */
1123 if (parse_config_file("/etc/modprobe.conf", conf
,
1124 dump_only
, removing
) > 0)
1125 warn("Deprecated config file /etc/modprobe.conf, "
1126 "all config files belong into /etc/modprobe.d/.\n");
1128 /* default config */
1129 parse_config_scan("/etc/modprobe.d", conf
, dump_only
, removing
);
1132 /* Read possible module arguments from the kernel command line. */
1133 static int parse_kcmdline(int dump_only
, struct module_options
**options
)
1136 unsigned int linenum
= 0;
1139 kcmdline
= fopen("/proc/cmdline", "r");
1143 while ((line
= getline_wrapped(kcmdline
, &linenum
)) != NULL
) {
1147 while ((arg
= strsep_skipspace(&ptr
, "\t ")) != NULL
) {
1148 char *sep
, *modname
, *opt
;
1150 sep
= strchr(arg
, '.');
1152 if (!strchr(sep
, '='))
1159 printf("options %s %s\n", modname
, opt
);
1161 *options
= add_options(underscores(modname
),
1172 static void add_to_env_var(const char *option
)
1176 if ((oldenv
= getenv("MODPROBE_OPTIONS")) != NULL
) {
1178 nofail_asprintf(&newenv
, "%s %s", oldenv
, option
);
1179 setenv("MODPROBE_OPTIONS", newenv
, 1);
1182 setenv("MODPROBE_OPTIONS", option
, 1);
1185 /* Prepend options from environment. */
1186 static char **merge_args(char *args
, char *argv
[], int *argc
)
1188 char *arg
, *argstring
;
1189 char **newargs
= NULL
;
1190 unsigned int i
, num_env
= 0;
1195 argstring
= NOFAIL(strdup(args
));
1196 for (arg
= strtok(argstring
, " "); arg
; arg
= strtok(NULL
, " ")) {
1198 newargs
= NOFAIL(realloc(newargs
,
1200 * (num_env
+ *argc
+ 1)));
1201 newargs
[num_env
] = arg
;
1207 /* Append commandline args */
1208 newargs
[0] = argv
[0];
1209 for (i
= 1; i
<= *argc
; i
++)
1210 newargs
[num_env
+i
] = argv
[i
];
1216 static char *gather_options(char *argv
[])
1218 char *optstring
= NOFAIL(strdup(""));
1220 /* Rest is module options */
1222 /* Quote value if it contains spaces. */
1223 unsigned int eq
= strcspn(*argv
, "=");
1225 if (strchr(*argv
+eq
, ' ') && !strchr(*argv
, '"')) {
1226 char quoted
[strlen(*argv
) + 3];
1228 sprintf(quoted
, "%s=\"%s\"", *argv
, *argv
+eq
+1);
1229 optstring
= append_option(optstring
, quoted
);
1231 optstring
= append_option(optstring
, *argv
);
1237 /* Do an install/remove command: replace $CMDLINE_OPTS if it's specified. */
1238 static void do_command(const char *modname
,
1239 const char *command
,
1243 const char *cmdline_opts
)
1246 char *p
, *replaced_cmd
= NOFAIL(strdup(command
));
1248 while ((p
= strstr(replaced_cmd
, "$CMDLINE_OPTS")) != NULL
) {
1250 nofail_asprintf(&new, "%.*s%s%s",
1251 (int)(p
- replaced_cmd
), replaced_cmd
, cmdline_opts
,
1252 p
+ strlen("$CMDLINE_OPTS"));
1257 info("%s %s\n", type
, replaced_cmd
);
1261 setenv("MODPROBE_MODULE", modname
, 1);
1262 ret
= system(replaced_cmd
);
1263 if (ret
== -1 || WEXITSTATUS(ret
))
1264 error("Error running %s command for %s\n", type
, modname
);
1270 /* Forward declaration */
1271 int do_modprobe(const char *modname
,
1272 const char *cmdline_opts
,
1273 const struct modprobe_conf
*conf
,
1274 const char *dirname
,
1276 modprobe_flags_t flags
);
1278 static void do_softdep(const struct module_softdep
*softdep
,
1279 const char *cmdline_opts
,
1280 const struct modprobe_conf
*conf
,
1281 const char *dirname
,
1283 modprobe_flags_t flags
)
1285 struct string_table
*pre_modnames
, *post_modnames
;
1286 modprobe_flags_t softdep_flags
= flags
;
1289 softdep_flags
&= ~mit_first_time
;
1290 softdep_flags
&= ~mit_ignore_commands
;
1291 if (flags
& mit_remove
)
1292 softdep_flags
|= mit_quiet_inuse
;
1294 if (++recursion_depth
>= MAX_RECURSION
)
1295 fatal("modprobe: softdep dependency loop encountered %s %s\n",
1296 (flags
& mit_remove
) ? "removing" : "inserting",
1299 if (flags
& mit_remove
) {
1300 /* Reverse module order if removing. */
1301 pre_modnames
= softdep
->post
;
1302 post_modnames
= softdep
->pre
;
1304 pre_modnames
= softdep
->pre
;
1305 post_modnames
= softdep
->post
;
1308 /* Modprobe pre_modnames */
1310 for (i
= 0; pre_modnames
&& i
< pre_modnames
->cnt
; i
++) {
1311 /* Reverse module order if removing. */
1312 j
= (flags
& mit_remove
) ? pre_modnames
->cnt
-1 - i
: i
;
1314 do_modprobe(pre_modnames
->str
[j
], "",
1315 conf
, dirname
, warn
, softdep_flags
);
1318 /* Modprobe main module, passing cmdline_opts, ignoring softdep */
1320 do_modprobe(softdep
->modname
, cmdline_opts
,
1321 conf
, dirname
, warn
, flags
| mit_ignore_commands
);
1323 /* Modprobe post_modnames */
1325 for (i
= 0; post_modnames
&& i
< post_modnames
->cnt
; i
++) {
1326 /* Reverse module order if removing. */
1327 j
= (flags
& mit_remove
) ? post_modnames
->cnt
-1 - i
: i
;
1329 do_modprobe(post_modnames
->str
[j
], "", conf
,
1330 dirname
, warn
, softdep_flags
);
1334 /* Actually do the insert. */
1335 static int insmod(struct list_head
*list
,
1336 const char *optstring
,
1337 const char *cmdline_opts
,
1338 const struct modprobe_conf
*conf
,
1339 const char *dirname
,
1341 modprobe_flags_t flags
)
1344 struct elf_file
*module
;
1345 const struct module_softdep
*softdep
;
1346 const char *command
;
1347 struct module
*mod
= list_entry(list
->next
, struct module
, list
);
1352 /* Take us off the list. */
1353 list_del(&mod
->list
);
1355 /* Do things we (or parent) depend on first. */
1356 if (!list_empty(list
)) {
1357 modprobe_flags_t f
= flags
;
1358 f
&= ~mit_first_time
;
1359 f
&= ~mit_ignore_commands
;
1360 if ((rc
= insmod(list
, "", "", conf
, dirname
, warn
, f
)) != 0)
1362 error("Error inserting %s (%s): %s\n",
1363 mod
->modname
, mod
->filename
,
1364 insert_moderror(errno
));
1369 /* Don't do ANYTHING if already in kernel. */
1370 already_loaded
= module_in_kernel(mod
->modname
, NULL
);
1372 if (!(flags
& mit_ignore_loaded
) && already_loaded
== 1) {
1373 if (flags
& mit_first_time
)
1374 error("Module %s already in kernel.\n", mod
->modname
);
1378 softdep
= find_softdep(mod
->modname
, conf
->softdeps
);
1379 if (softdep
&& !(flags
& mit_ignore_commands
)) {
1380 do_softdep(softdep
, cmdline_opts
, conf
, dirname
, error
, flags
);
1384 command
= find_command(mod
->modname
, conf
->commands
);
1385 if (command
&& !(flags
& mit_ignore_commands
)) {
1386 if (already_loaded
== -1) {
1387 warn("/sys/module/ not present or too old,"
1388 " and /proc/modules does not exist.\n");
1389 warn("Ignoring install commands for %s"
1390 " in case it is already loaded.\n",
1393 do_command(mod
->modname
, command
, flags
& mit_dry_run
,
1394 error
, "install", cmdline_opts
);
1399 module
= grab_elf_file(mod
->filename
);
1401 error("Could not read '%s': %s\n", mod
->filename
,
1402 (errno
== ENOEXEC
) ? "Invalid module format" :
1406 if (flags
& mit_strip_modversion
)
1407 module
->ops
->strip_section(module
, "__versions");
1408 if (flags
& mit_strip_vermagic
)
1409 clear_magic(module
);
1411 /* Config file might have given more options */
1412 opts
= add_extra_options(mod
->modname
, optstring
, conf
->options
);
1414 info("insmod %s %s\n", mod
->filename
, opts
);
1416 if (flags
& mit_dry_run
)
1419 ret
= init_module(module
->data
, module
->len
, opts
);
1421 if (errno
== EEXIST
) {
1422 if (flags
& mit_first_time
)
1423 error("Module %s already in kernel.\n",
1427 /* don't warn noisely if we're loading multiple aliases. */
1428 /* one of the aliases may try to use hardware we don't have. */
1429 if ((error
!= warn
) || (verbose
))
1430 error("Error inserting %s (%s): %s\n",
1431 mod
->modname
, mod
->filename
,
1432 insert_moderror(errno
));
1436 release_elf_file(module
);
1443 /* Do recursive removal. */
1444 static void rmmod(struct list_head
*list
,
1445 const char *cmdline_opts
,
1446 const struct modprobe_conf
*conf
,
1447 const char *dirname
,
1449 modprobe_flags_t flags
)
1451 const struct module_softdep
*softdep
;
1452 const char *command
;
1453 unsigned int usecount
= 0;
1454 struct module
*mod
= list_entry(list
->next
, struct module
, list
);
1457 /* Take first one off the list. */
1458 list_del(&mod
->list
);
1460 /* Don't do ANYTHING if not loaded. */
1461 exists
= module_in_kernel(mod
->modname
, &usecount
);
1463 goto nonexistent_module
;
1465 /* Even if renamed, find commands/softdeps to orig. name. */
1467 softdep
= find_softdep(mod
->modname
, conf
->softdeps
);
1468 if (softdep
&& !(flags
& mit_ignore_commands
)) {
1469 do_softdep(softdep
, cmdline_opts
, conf
, dirname
, error
, flags
);
1473 command
= find_command(mod
->modname
, conf
->commands
);
1474 if (command
&& !(flags
& mit_ignore_commands
)) {
1476 warn("/sys/module/ not present or too old,"
1477 " and /proc/modules does not exist.\n");
1478 warn("Ignoring remove commands for %s"
1479 " in case it is not loaded.\n",
1482 do_command(mod
->modname
, command
, flags
& mit_dry_run
,
1483 error
, "remove", cmdline_opts
);
1488 if (usecount
!= 0) {
1489 if (!(flags
& mit_quiet_inuse
))
1490 error("Module %s is in use.\n", mod
->modname
);
1494 info("rmmod %s\n", mod
->filename
);
1496 if (flags
& mit_dry_run
)
1499 if (delete_module(mod
->modname
, O_EXCL
) != 0) {
1500 if (errno
== ENOENT
)
1501 goto nonexistent_module
;
1502 error("Error removing %s (%s): %s\n",
1503 mod
->modname
, mod
->filename
,
1504 remove_moderror(errno
));
1508 /* Now do things we depend. */
1509 if (!list_empty(list
)) {
1510 flags
&= ~mit_first_time
;
1511 flags
&= ~mit_ignore_commands
;
1512 flags
|= mit_quiet_inuse
;
1514 rmmod(list
, "", conf
, dirname
, warn
, flags
);
1520 if (flags
& mit_first_time
)
1521 fatal("Module %s is not in kernel.\n", mod
->modname
);
1525 static int handle_module(const char *modname
,
1526 struct list_head
*todo_list
,
1527 const char *options
,
1528 const char *cmdline_opts
,
1529 const struct modprobe_conf
*conf
,
1530 const char *dirname
,
1532 modprobe_flags_t flags
)
1534 if (list_empty(todo_list
)) {
1535 const char *command
;
1537 /* The dependencies have to be real modules, but
1538 handle case where the first is completely bogus. */
1540 command
= find_command(modname
, conf
->commands
);
1541 if (command
&& !(flags
& mit_ignore_commands
)) {
1542 do_command(modname
, command
, flags
& mit_dry_run
, error
,
1543 (flags
& mit_remove
) ? "remove":"install", cmdline_opts
);
1548 error("Module %s not found.\n", modname
);
1552 if (flags
& mit_remove
)
1553 rmmod(todo_list
, cmdline_opts
,
1554 conf
, dirname
, error
, flags
);
1556 insmod(todo_list
, options
,
1557 cmdline_opts
, conf
, dirname
, error
, flags
);
1562 int handle_builtin_module(const char *modname
,
1564 modprobe_flags_t flags
)
1566 if (flags
& mit_remove
) {
1567 error("Module %s is builtin\n", modname
);
1569 } else if (flags
& mit_first_time
) {
1570 error("Module %s already in kernel (builtin).\n", modname
);
1572 } else if (flags
& mit_ignore_loaded
) {
1573 /* --show-depends given */
1574 info("builtin %s\n", modname
);
1579 int do_modprobe(const char *modulename
,
1580 const char *cmdline_opts
,
1581 const struct modprobe_conf
*conf
,
1582 const char *dirname
,
1584 modprobe_flags_t flags
)
1587 struct module_alias
*matching_aliases
;
1591 /* Convert name we are looking for */
1592 modname
= underscores(NOFAIL(strdup(modulename
)));
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
);
1670 free_aliases(matching_aliases
);
1674 static struct option options
[] = { { "version", 0, NULL
, 'V' },
1675 { "verbose", 0, NULL
, 'v' },
1676 { "quiet", 0, NULL
, 'q' },
1677 { "syslog", 0, NULL
, 's' },
1678 { "show", 0, NULL
, 'n' },
1679 { "dry-run", 0, NULL
, 'n' },
1680 { "show-depends", 0, NULL
, 'D' },
1681 { "resolve-alias", 0, NULL
, 'R' },
1682 { "dirname", 1, NULL
, 'd' },
1683 { "set-version", 1, NULL
, 'S' },
1684 { "config", 1, NULL
, 'C' },
1685 { "remove", 0, NULL
, 'r' },
1686 { "showconfig", 0, NULL
, 'c' },
1687 { "list", 0, NULL
, 'l' },
1688 { "type", 1, NULL
, 't' },
1689 { "all", 0, NULL
, 'a' },
1690 { "ignore-install", 0, NULL
, 'i' },
1691 { "ignore-remove", 0, NULL
, 'i' },
1692 { "use-blacklist", 0, NULL
, 'b' },
1693 { "force", 0, NULL
, 'f' },
1694 { "force-vermagic", 0, NULL
, 1 },
1695 { "force-modversion", 0, NULL
, 2 },
1696 { "first-time", 0, NULL
, 3 },
1697 { "dump-modversions", 0, NULL
, 4 },
1698 { NULL
, 0, NULL
, 0 } };
1700 int main(int argc
, char *argv
[])
1703 struct stat statbuf
;
1705 int dump_config
= 0;
1708 int dump_modver
= 0;
1709 unsigned int i
, num_modules
;
1711 const char *configname
= NULL
;
1713 char *cmdline_opts
= NULL
;
1715 errfn_t error
= fatal
;
1717 modprobe_flags_t flags
= 0;
1718 struct modprobe_conf conf
= {};
1720 recursion_depth
= 0;
1722 /* Prepend options from environment. */
1723 argv
= merge_args(getenv("MODPROBE_OPTIONS"), argv
, &argc
);
1726 while ((opt
= getopt_long(argc
, argv
, "Vvqsnd:S:C:DRrclt:aibf", options
, NULL
)) != -1){
1729 puts(PACKAGE
" version " VERSION
);
1732 add_to_env_var("-v");
1737 add_to_env_var("-q");
1740 add_to_env_var("-s");
1744 flags
|= mit_dry_run
;
1750 strncpy(buf
.release
, optarg
, sizeof(buf
.release
));
1751 buf
.release
[sizeof(buf
.release
)-1] = '\0';
1754 configname
= optarg
;
1755 add_to_env_var("-C");
1756 add_to_env_var(configname
);
1759 flags
|= mit_dry_run
;
1760 flags
|= mit_ignore_loaded
;
1764 flags
|= mit_resolve_alias
;
1767 flags
|= mit_remove
;
1783 flags
|= mit_ignore_commands
;
1786 flags
|= mit_use_blacklist
;
1789 flags
|= mit_strip_vermagic
;
1790 flags
|= mit_strip_modversion
;
1793 flags
|= mit_strip_vermagic
;
1796 flags
|= mit_strip_modversion
;
1799 flags
|= mit_first_time
;
1805 print_usage(argv
[0]);
1809 /* If stderr not open, go to syslog */
1810 if (logging
|| fstat(STDERR_FILENO
, &statbuf
) != 0) {
1811 openlog("modprobe", LOG_CONS
, LOG_DAEMON
);
1815 if (argc
< optind
+ 1 && !dump_config
&& !list_only
)
1816 print_usage(argv
[0]);
1818 nofail_asprintf(&dirname
, "%s%s/%s", basedir
, MODULE_DIR
, buf
.release
);
1820 /* Old-style -t xxx wildcard? Only with -l. */
1822 if (optind
+1 < argc
)
1823 fatal("Can't have multiple wildcards\n");
1824 /* fprintf(stderr, "man find\n"); return 1; */
1825 failed
= do_wildcard(dirname
, type
, argv
[optind
]?:"*");
1829 fatal("-t only supported with -l");
1832 dump_modversions(argv
[optind
], error
);
1836 /* Read aliases, options etc. */
1837 parse_toplevel_config(configname
, &conf
, dump_config
, flags
& mit_remove
);
1839 /* Read module options from kernel command line */
1840 parse_kcmdline(dump_config
, &conf
.options
);
1843 char *aliasfilename
, *symfilename
;
1844 struct modprobe_conf conf
= {};
1846 nofail_asprintf(&aliasfilename
, "%s/modules.alias", dirname
);
1847 nofail_asprintf(&symfilename
, "%s/modules.symbols", dirname
);
1849 read_aliases(aliasfilename
, "", 1, &conf
.aliases
);
1850 read_aliases(symfilename
, "", 1, &conf
.aliases
);
1855 if ((flags
& mit_remove
) || all
) {
1856 num_modules
= argc
- optind
;
1857 cmdline_opts
= NOFAIL(strdup(""));
1860 cmdline_opts
= gather_options(argv
+optind
+1);
1863 /* num_modules is always 1 except for -r or -a. */
1864 for (i
= 0; i
< num_modules
; i
++) {
1865 char *modname
= argv
[optind
+ i
];
1867 failed
|= do_modprobe(modname
, cmdline_opts
,
1868 &conf
, dirname
, error
, flags
);
1876 /* Don't bother to free conf */