1 /* modprobe.c: add or remove a module from the kernel, intelligently.
3 * Copyright (C) 2001 Rusty Russell.
4 * Copyright (C) 2002, 2003 Rusty Russell, IBM Corporation.
5 * Copyright (C) 2006-2011 Jon Masters <jcm@jonmasters.org>, and others.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
22 #define _GNU_SOURCE /* asprintf */
24 #include <sys/utsname.h>
25 #include <sys/types.h>
41 #include <asm/unistd.h>
47 #include "zlibsupport.h"
51 #include "config_filter.h"
55 static int use_binary_indexes
= 1; /* default to enabled. */
57 /* Limit do_softdep/do_modprobe recursion.
58 * This is a simple way to handle dependency loops
59 * caused by poorly written softdep commands.
61 static int recursion_depth
= 0;
62 static const int MAX_RECURSION
= 50; /* Arbitrary choice */
64 extern long init_module(void *, unsigned long, const char *);
65 extern long delete_module(const char *, unsigned int);
68 struct list_head list
;
78 mit_use_blacklist
= 8,
79 mit_ignore_commands
= 16,
80 mit_ignore_loaded
= 32,
82 mit_strip_vermagic
= 128,
83 mit_strip_modversion
= 256,
84 mit_resolve_alias
= 512
89 #define MODULE_DIR "/lib/modules"
92 static void print_usage(const char *progname
)
95 "Usage: %s [-v] [-V] [-C config-file] [-d <dirname> ] [-n] [-i] [-q] [-b] [-o <modname>] [ --dump-modversions ] <modname> [parameters...]\n"
96 "%s -r [-n] [-i] [-v] <modulename> ...\n"
97 "%s -l -t <dirname> [ -a <modulename> ...]\n",
98 progname
, progname
, progname
);
102 static struct module
*find_module(const char *filename
, struct list_head
*list
)
106 list_for_each_entry(i
, list
, list
) {
107 if (streq(i
->filename
, filename
))
113 static void add_module(char *filename
, int namelen
, struct list_head
*list
)
117 /* If it's a duplicate: move it to the end, so it gets
118 inserted where it is *first* required. */
119 mod
= find_module(filename
, list
);
121 list_del(&mod
->list
);
123 /* No match. Create a new module. */
124 mod
= NOFAIL(malloc(sizeof(struct module
) + namelen
+ 1));
125 memcpy(mod
->filename
, filename
, namelen
);
126 mod
->filename
[namelen
] = '\0';
127 mod
->modname
= NOFAIL(malloc(namelen
+ 1));
128 filename2modname(mod
->modname
, mod
->filename
);
131 list_add_tail(&mod
->list
, list
);
134 static void free_module(struct module
*mod
)
140 /* Compare len chars of a to b, with _ and - equivalent. */
141 static int modname_equal(const char *a
, const char *b
, unsigned int len
)
145 if (strlen(b
) != len
)
148 for (i
= 0; i
< len
; i
++) {
149 if ((a
[i
] == '_' || a
[i
] == '-')
150 && (b
[i
] == '_' || b
[i
] == '-'))
158 /* Fills in list of modules if this is the line we want. */
159 static int add_modules_dep_line(char *line
,
161 struct list_head
*list
,
166 char *modname
, *fullpath
;
168 /* Ignore lines without : or which start with a # */
169 ptr
= strchr(line
, ':');
170 if (ptr
== NULL
|| line
[strspn(line
, "\t ")] == '#')
173 /* Is this the module we are looking for? */
175 modname
= my_basename(line
);
177 len
= strlen(modname
);
178 if (strchr(modname
, '.'))
179 len
= strchr(modname
, '.') - modname
;
180 if (!modname_equal(modname
, name
, len
))
183 /* Create the list. */
184 if ('/' == line
[0]) { /* old style deps - absolute path specified */
185 add_module(line
, ptr
- line
, list
);
187 nofail_asprintf(&fullpath
, "%s/%s", dirname
, line
);
188 add_module(fullpath
, strlen(dirname
)+1+(ptr
- line
), list
);
195 ptr
+= strspn(ptr
, " \t");
199 ptr
+= strcspn(ptr
, " \t");
200 if ('/' == dep_start
[0]) { /* old style deps */
201 add_module(dep_start
, ptr
- dep_start
, list
);
203 nofail_asprintf(&fullpath
, "%s/%s", dirname
, dep_start
);
205 strlen(dirname
)+1+(ptr
- dep_start
), list
);
212 static int read_depends_file(const char *dirname
,
213 const char *start_name
,
214 struct list_head
*list
)
216 char *modules_dep_name
;
218 struct index_file
*modules_dep
;
220 nofail_asprintf(&modules_dep_name
, "%s/%s", dirname
, "modules.dep.bin");
221 modules_dep
= index_file_open(modules_dep_name
);
223 free(modules_dep_name
);
227 line
= index_search(modules_dep
, start_name
);
229 /* Value is standard dependency line format */
230 if (!add_modules_dep_line(line
, start_name
, list
, dirname
))
231 fatal("Module index is inconsistent\n");
235 index_file_close(modules_dep
);
236 free(modules_dep_name
);
241 static void read_depends(const char *dirname
,
242 const char *start_name
,
243 struct list_head
*list
)
245 char *modules_dep_name
;
250 if (use_binary_indexes
)
251 if (read_depends_file(dirname
, start_name
, list
))
254 nofail_asprintf(&modules_dep_name
, "%s/%s", dirname
, "modules.dep");
255 modules_dep
= fopen(modules_dep_name
, "r");
257 fatal("Could not load %s: %s\n",
258 modules_dep_name
, strerror(errno
));
260 /* Stop at first line, as we can have duplicates (eg. symlinks
262 while (!done
&& (line
= getline_wrapped(modules_dep
, NULL
)) != NULL
) {
263 done
= add_modules_dep_line(line
, start_name
, list
, dirname
);
267 free(modules_dep_name
);
270 /* We use error numbers in a loose translation... */
271 static const char *insert_moderror(int err
)
275 return "Invalid module format";
277 return "Unknown symbol in module, or unknown parameter (see dmesg)";
279 return "Kernel does not have module support";
281 return strerror(err
);
285 static const char *remove_moderror(int err
)
289 return "No such module";
291 return "Kernel does not have module unloading support";
293 return strerror(err
);
297 static void clear_magic(struct elf_file
*module
)
299 struct string_table
*tbl
;
302 /* Old-style: __vermagic section */
303 module
->ops
->strip_section(module
, "__vermagic");
305 /* New-style: in .modinfo section */
306 tbl
= module
->ops
->load_strings(module
, ".modinfo", NULL
);
307 for (j
= 0; tbl
&& j
< tbl
->cnt
; j
++) {
308 const char *p
= tbl
->str
[j
];
309 if (strstarts(p
, "vermagic=")) {
310 memset((char *)p
, 0, strlen(p
));
316 struct module_options
318 struct module_options
*next
;
323 struct module_command
325 struct module_command
*next
;
332 struct module_alias
*next
;
337 struct module_blacklist
339 struct module_blacklist
*next
;
343 struct module_softdep
345 struct module_softdep
*next
;
347 /* The modname and string tables point to buf. */
349 struct string_table
*pre
;
350 struct string_table
*post
;
355 struct module_options
*options
;
356 struct module_command
*commands
;
357 struct module_alias
*aliases
;
358 struct module_blacklist
*blacklist
;
359 struct module_softdep
*softdeps
;
362 /* Link in a new option line from the config file. */
363 static struct module_options
*
364 add_options(const char *modname
,
366 struct module_options
*options
)
368 struct module_options
*new;
371 new = NOFAIL(malloc(sizeof(*new)));
372 new->modulename
= NOFAIL(strdup(modname
));
373 new->options
= NOFAIL(strdup(option
));
374 /* We can handle tabs, kernel can't. */
375 for (tab
= strchr(new->options
, '\t'); tab
; tab
= strchr(tab
, '\t'))
381 /* Link in a new install line from the config file. */
382 static struct module_command
*
383 add_command(const char *modname
,
385 struct module_command
*commands
)
387 struct module_command
*new;
389 new = NOFAIL(malloc(sizeof(*new)));
390 new->modulename
= NOFAIL(strdup(modname
));
391 new->command
= NOFAIL(strdup(command
));
392 new->next
= commands
;
396 /* Link in a new alias line from the config file. */
397 static struct module_alias
*
398 add_alias(const char *aliasname
, const char *modname
, struct module_alias
*aliases
)
400 struct module_alias
*new;
402 new = NOFAIL(malloc(sizeof(*new)));
403 new->aliasname
= NOFAIL(strdup(aliasname
));
404 new->module
= NOFAIL(strdup(modname
));
410 /* Return a list of matching aliases */
411 static struct module_alias
*
412 find_aliases(const struct module_alias
*aliases
,
415 struct module_alias
*result
= NULL
;
417 char *aliasname
= aliases
->aliasname
;
418 char *modname
= aliases
->module
;
419 if (fnmatch(aliasname
, name
, 0) == 0)
420 result
= add_alias(aliasname
, modname
, result
);
421 aliases
= aliases
->next
;
426 static void free_aliases(struct module_alias
*alias_list
)
429 struct module_alias
*alias
;
432 alias_list
= alias_list
->next
;
434 free(alias
->aliasname
);
440 /* Link in a new blacklist line from the config file. */
441 static struct module_blacklist
*
442 add_blacklist(const char *modname
, struct module_blacklist
*blacklist
)
444 struct module_blacklist
*new;
446 new = NOFAIL(malloc(sizeof(*new)));
447 new->modulename
= NOFAIL(strdup(modname
));
448 new->next
= blacklist
;
452 /* Find blacklist commands if any. */
454 find_blacklist(const char *modname
, const struct module_blacklist
*blacklist
)
457 if (streq(blacklist
->modulename
, modname
))
459 blacklist
= blacklist
->next
;
464 /* delete backlisted elems from a list of aliases */
466 apply_blacklist(struct module_alias
**aliases
,
467 const struct module_blacklist
*blacklist
)
469 struct module_alias
*result
= NULL
;
470 struct module_alias
*alias
= *aliases
;
472 char *modname
= alias
->module
;
473 if (!find_blacklist(modname
, blacklist
))
474 result
= add_alias(alias
->aliasname
, modname
, result
);
477 free_aliases(*aliases
);
481 /* Find install commands if any. */
482 static const char *find_command(const char *modname
,
483 const struct module_command
*commands
)
486 if (fnmatch(commands
->modulename
, modname
, 0) == 0)
487 return commands
->command
;
488 commands
= commands
->next
;
493 /* Find soft dependencies, if any. */
494 static const struct module_softdep
*
495 find_softdep(const char *modname
, const struct module_softdep
*softdeps
)
498 if (fnmatch(softdeps
->modname
, modname
, 0) == 0)
500 softdeps
= softdeps
->next
;
505 static char *append_option(char *options
, const char *newoption
)
507 options
= NOFAIL(realloc(options
, strlen(options
) + 1
508 + strlen(newoption
) + 1));
509 if (strlen(options
)) strcat(options
, " ");
510 strcat(options
, newoption
);
514 static char *prepend_option(char *options
, const char *newoption
)
517 l1
= strlen(options
);
518 l2
= strlen(newoption
);
519 /* the resulting string will look like
520 * newoption + ' ' + options + '\0' */
522 options
= NOFAIL(realloc(options
, l2
+ 1 + l1
+ 1));
523 memmove(options
+ l2
+ 1, options
, l1
+ 1);
525 memcpy(options
, newoption
, l2
);
527 options
= NOFAIL(realloc(options
, l2
+ 1));
528 memcpy(options
, newoption
, l2
);
535 static char *add_extra_options(const char *modname
,
536 const char *optstring
,
537 const struct module_options
*options
)
539 char *opts
= NOFAIL(strdup(optstring
));
542 if (streq(options
->modulename
, modname
))
543 opts
= prepend_option(opts
, options
->options
);
544 options
= options
->next
;
549 /* Is module in /proc/modules? If so, fill in usecount if not NULL.
550 0 means no, 1 means yes, -1 means unknown.
552 static int module_in_procfs(const char *modname
, unsigned int *usecount
)
558 /* Might not be mounted yet. Don't fail. */
559 proc_modules
= fopen("/proc/modules", "r");
563 while ((line
= getline_wrapped(proc_modules
, NULL
)) != NULL
) {
564 char *entry
= strtok(line
, " \n");
566 if (entry
&& streq(entry
, modname
)) {
567 /* If it exists, usecount is the third entry. */
568 if (!strtok(NULL
, " \n"))
571 if (!(entry
= strtok(NULL
, " \n"))) /* usecount */
575 *usecount
= atoi(entry
);
577 /* Followed by - then status. */
578 if (strtok(NULL
, " \n")
579 && (entry
= strtok(NULL
, " \n")) != NULL
) {
580 /* No locking, we might hit cases
581 * where module is in flux. Spin. */
582 if (streq(entry
, "Loading")
583 || streq(entry
, "Unloading")) {
586 fclose(proc_modules
);
593 fclose(proc_modules
);
598 fclose(proc_modules
);
602 /* Read sysfs attribute into a buffer.
603 * returns: 1 = ok, 0 = attribute missing,
604 * -1 = file error (or empty file, but we don't care).
606 static int read_attribute(const char *filename
, char *buf
, size_t buflen
)
611 file
= fopen(filename
, "r");
613 return (errno
== ENOENT
) ? 0 : -1;
614 s
= fgets(buf
, buflen
, file
);
617 return (s
== NULL
) ? -1 : 1;
620 /* is this a built-in module?
621 * 0: no, 1: yes, -1: don't know
623 static int module_builtin(const char *dirname
, const char *modname
)
625 struct index_file
*index
;
626 char *filename
, *value
;
628 nofail_asprintf(&filename
, "%s/modules.builtin.bin", dirname
);
629 index
= index_file_open(filename
);
633 value
= index_search(index
, modname
);
635 return value
? 1 : 0;
638 /* Is module in /sys/module? If so, fill in usecount if not NULL.
639 0 means no, 1 means yes, -1 means unknown.
641 static int module_in_sysfs(const char *modname
, unsigned int *usecount
)
647 const int ATTR_LEN
= 16;
650 /* Check sysfs is mounted */
651 if (stat("/sys/module", &finfo
) < 0)
655 nofail_asprintf(&name
, "/sys/module/%s", modname
);
656 ret
= stat(name
, &finfo
);
659 return (errno
== ENOENT
) ? 0 : -1; /* Not found or unknown. */
661 nofail_asprintf(&name
, "/sys/module/%s/initstate", modname
);
662 ret
= read_attribute(name
, attr
, ATTR_LEN
);
665 nofail_asprintf(&name
, "/sys/module/%s", modname
);
666 if (stat(name
, &finfo
) < 0) {
667 /* module was removed before we could read initstate */
670 /* initstate not available (2.6.19 or earlier) */
677 /* Wait for the existing module to either go live or disappear. */
678 while (ret
== 1 && !streq(attr
, "live\n")) {
680 ret
= read_attribute(name
, attr
, ATTR_LEN
);
687 /* Get reference count, if it exists. */
688 if (usecount
!= NULL
) {
689 nofail_asprintf(&name
, "/sys/module/%s/refcnt", modname
);
690 ret
= read_attribute(name
, attr
, ATTR_LEN
);
693 *usecount
= atoi(attr
);
699 /* Is module loaded? If so, fill in usecount if not NULL.
700 0 means no, 1 means yes, -1 means unknown.
702 static int module_in_kernel(const char *modname
, unsigned int *usecount
)
706 result
= module_in_sysfs(modname
, usecount
);
710 /* /sys/module/%s/initstate is only available since 2.6.20,
711 fallback to /proc/modules to get module state on earlier kernels. */
712 return module_in_procfs(modname
, usecount
);
715 static void dump_modversions(const char *filename
, errfn_t error
)
717 struct elf_file
*module
;
719 module
= grab_elf_file(filename
);
721 error("%s: %s\n", filename
, strerror(errno
));
724 if (module
->ops
->dump_modvers(module
) < 0)
725 error("Wrong section size in '%s'\n", filename
);
726 release_elf_file(module
);
729 /* Does path contain directory(s) subpath? */
730 static int type_matches(const char *path
, const char *subpath
)
732 char *subpath_with_slashes
;
735 nofail_asprintf(&subpath_with_slashes
, "/%s/", subpath
);
737 ret
= (strstr(path
, subpath_with_slashes
) != NULL
);
738 free(subpath_with_slashes
);
743 static int do_wildcard(const char *dirname
,
745 const char *wildcard
)
747 char *modules_dep_name
;
751 /* Canonicalize wildcard */
752 wcard
= strdup(wildcard
);
755 nofail_asprintf(&modules_dep_name
, "%s/%s", dirname
, "modules.dep");
756 modules_dep
= fopen(modules_dep_name
, "r");
758 fatal("Could not load %s: %s\n",
759 modules_dep_name
, strerror(errno
));
761 while ((line
= getline_wrapped(modules_dep
, NULL
)) != NULL
) {
764 /* Ignore lines without : or which start with a # */
765 ptr
= strchr(line
, ':');
766 if (ptr
== NULL
|| line
[strspn(line
, "\t ")] == '#')
770 /* "type" must match complete directory component(s). */
771 if (!type
|| type_matches(line
, type
)) {
772 char modname
[strlen(line
)+1];
774 filename2modname(modname
, line
);
775 if (fnmatch(wcard
, modname
, 0) == 0)
776 printf("%s\n", line
);
782 free(modules_dep_name
);
787 static char *strsep_skipspace(char **string
, char *delim
)
791 *string
+= strspn(*string
, delim
);
792 return strsep(string
, delim
);
795 static int parse_config_scan(struct modprobe_conf
*conf
,
799 static int parse_config_file(const char *filename
,
800 struct modprobe_conf
*conf
,
805 unsigned int linenum
= 0;
808 struct module_options
**options
= &conf
->options
;
809 struct module_command
**commands
= &conf
->commands
;
810 struct module_alias
**aliases
= &conf
->aliases
;
811 struct module_blacklist
**blacklist
= &conf
->blacklist
;
813 cfile
= fopen(filename
, "r");
817 while ((line
= getline_wrapped(cfile
, &linenum
)) != NULL
) {
822 printf("%s\n", line
);
824 cmd
= strsep_skipspace(&ptr
, "\t ");
825 if (cmd
== NULL
|| cmd
[0] == '#' || cmd
[0] == '\0') {
830 if (streq(cmd
, "alias")) {
831 char *wildcard
= strsep_skipspace(&ptr
, "\t ");
832 char *realname
= strsep_skipspace(&ptr
, "\t ");
833 if (!wildcard
|| !realname
)
835 *aliases
= add_alias(underscores(wildcard
),
836 underscores(realname
),
838 } else if (streq(cmd
, "include")) {
839 struct modprobe_conf newconf
= *conf
;
840 newconf
.aliases
= NULL
;
842 newfilename
= strsep_skipspace(&ptr
, "\t ");
846 warn("\"include %s\" is deprecated, "
847 "please use /etc/modprobe.d\n", newfilename
);
848 if (strstarts(newfilename
, "/etc/modprobe.d")) {
849 warn("\"include /etc/modprobe.d\" is "
850 "the default, ignored\n");
852 if (!parse_config_scan(&newconf
, dump_only
,
853 removing
, newfilename
,
855 warn("Failed to open included"
856 " config file %s: %s\n",
857 newfilename
, strerror(errno
));
859 /* Files included override aliases,
860 etc that was already set ... */
862 *aliases
= newconf
.aliases
;
864 } else if (streq(cmd
, "options")) {
865 modname
= strsep_skipspace(&ptr
, "\t ");
866 if (!modname
|| !ptr
)
869 ptr
+= strspn(ptr
, "\t ");
870 *options
= add_options(underscores(modname
),
873 } else if (streq(cmd
, "install")) {
874 modname
= strsep_skipspace(&ptr
, "\t ");
875 if (!modname
|| !ptr
)
878 ptr
+= strspn(ptr
, "\t ");
879 *commands
= add_command(underscores(modname
),
882 } else if (streq(cmd
, "blacklist")) {
883 modname
= strsep_skipspace(&ptr
, "\t ");
887 *blacklist
= add_blacklist(underscores(modname
),
890 } else if (streq(cmd
, "remove")) {
891 modname
= strsep_skipspace(&ptr
, "\t ");
892 if (!modname
|| !ptr
)
895 ptr
+= strspn(ptr
, "\t ");
896 *commands
= add_command(underscores(modname
),
899 } else if (streq(cmd
, "softdep")) {
901 int pre
= 0, post
= 0;
902 struct string_table
*pre_modnames
= NULL
;
903 struct string_table
*post_modnames
= NULL
;
904 struct module_softdep
*new;
906 modname
= strsep_skipspace(&ptr
, "\t ");
907 if (!modname
|| !ptr
)
909 modname
= underscores(modname
);
911 while ((tk
= strsep_skipspace(&ptr
, "\t ")) != NULL
) {
912 tk
= underscores(tk
);
914 if (streq(tk
, "pre:")) {
916 } else if (streq(tk
, "post:")) {
919 pre_modnames
= NOFAIL(
920 strtbl_add(tk
, pre_modnames
));
922 post_modnames
= NOFAIL(
923 strtbl_add(tk
, post_modnames
));
925 strtbl_free(pre_modnames
);
926 strtbl_free(post_modnames
);
930 new = NOFAIL(malloc(sizeof(*new)));
932 new->modname
= modname
;
933 new->pre
= pre_modnames
;
934 new->post
= post_modnames
;
935 new->next
= conf
->softdeps
;
936 conf
->softdeps
= new;
938 line
= NULL
; /* Don't free() this line. */
940 } else if (streq(cmd
, "config")) {
941 char *tmp
= strsep_skipspace(&ptr
, "\t ");
945 if (streq(tmp
, "binary_indexes")) {
946 tmp
= strsep_skipspace(&ptr
, "\t ");
947 if (streq(tmp
, "yes"))
948 use_binary_indexes
= 1;
949 if (streq(tmp
, "no"))
950 use_binary_indexes
= 0;
954 grammar(cmd
, filename
, linenum
);
963 /* Read binary index file containing aliases only */
964 static int read_aliases_file(const char *filename
,
967 struct module_alias
**aliases
)
969 struct index_value
*realnames
;
970 struct index_value
*realname
;
972 struct index_file
*index
;
974 nofail_asprintf(&binfile
, "%s.bin", filename
);
975 index
= index_file_open(binfile
);
982 index_dump(index
, stdout
, "alias ");
984 index_file_close(index
);
988 realnames
= index_searchwild(index
, name
);
989 for (realname
= realnames
; realname
; realname
= realname
->next
)
990 *aliases
= add_alias("*", realname
->value
, *aliases
);
991 index_values_free(realnames
);
994 index_file_close(index
);
998 /* fallback to plain-text aliases file if necessary */
999 static int read_aliases(const char *filename
,
1002 struct module_alias
**aliases
)
1005 unsigned int linenum
= 0;
1008 if (use_binary_indexes
)
1009 if (read_aliases_file(filename
, name
, dump_only
, aliases
))
1012 cfile
= fopen(filename
, "r");
1016 while ((line
= getline_wrapped(cfile
, &linenum
)) != NULL
) {
1021 printf("%s\n", line
);
1023 cmd
= strsep_skipspace(&ptr
, "\t ");
1024 if (cmd
== NULL
|| cmd
[0] == '#' || cmd
[0] == '\0') {
1029 if (streq(cmd
, "alias")) {
1030 char *wildcard
= strsep_skipspace(&ptr
, "\t ");
1031 char *realname
= strsep_skipspace(&ptr
, "\t ");
1032 if (!wildcard
|| !realname
)
1034 if (fnmatch(underscores(wildcard
),name
,0) == 0)
1035 *aliases
= add_alias(wildcard
,
1036 underscores(realname
),
1040 grammar(cmd
, filename
, linenum
);
1049 static int parse_config_scan(struct modprobe_conf
*conf
,
1057 struct list_head node
;
1061 struct file_entry
*fe
, *fe_tmp
;
1062 LIST_HEAD(files_list
);
1065 va_start(filelist
, removing
);
1067 while ((filename
= va_arg(filelist
, char*))) {
1068 dir
= opendir(filename
);
1072 /* sort files from directories into list, ignoring duplicates */
1073 while ((i
= readdir(dir
)) != NULL
) {
1077 if (i
->d_name
[0] == '.')
1079 if (!config_filter(i
->d_name
))
1082 len
= strlen(i
->d_name
);
1084 (strcmp(&i
->d_name
[len
-5], ".conf") != 0 &&
1085 strcmp(&i
->d_name
[len
-6], ".alias") != 0))
1086 warn("All config files need .conf: %s/%s, "
1087 "it will be ignored in a future release.\n",
1088 filename
, i
->d_name
);
1089 fe
= malloc(sizeof(struct file_entry
));
1093 list_for_each_entry(fe_tmp
, &files_list
, node
)
1094 if ((cmp
= strcmp(fe_tmp
->name
, i
->d_name
)) >= 0)
1098 fe
->name
= malloc(len
+ 1);
1099 fe
->path
= malloc(strlen(filename
) + 1);
1100 strcpy(fe
->name
, i
->d_name
);
1101 strcpy(fe
->path
, filename
);
1104 list_add_tail(&fe
->node
, &files_list
);
1106 list_add_tail(&fe
->node
, &fe_tmp
->node
);
1108 info("Ignoring config file %s/%s\n", filename
, i
->d_name
);
1115 if (parse_config_file(filename
, conf
, dump_only
, removing
))
1120 /* parse list of files */
1121 list_for_each_entry_safe(fe
, fe_tmp
, &files_list
, node
) {
1124 nofail_asprintf(&cfgfile
, "%s/%s", fe
->path
, fe
->name
);
1125 if (!parse_config_file(cfgfile
, conf
,
1126 dump_only
, removing
))
1127 warn("Failed to open config file %s: %s\n",
1128 cfgfile
, strerror(errno
));
1130 list_del(&fe
->node
);
1140 static void parse_toplevel_config(const char *filename
,
1141 struct modprobe_conf
*conf
,
1146 if (!parse_config_scan(conf
, dump_only
, removing
, filename
,
1148 fatal("Failed to open config file %s: %s\n",
1149 filename
, strerror(errno
));
1153 /* deprecated config file */
1154 if (parse_config_file("/etc/modprobe.conf", conf
,
1155 dump_only
, removing
) > 0)
1156 warn("Deprecated config file /etc/modprobe.conf, "
1157 "all config files belong into /etc/modprobe.d/.\n");
1159 /* default config */
1160 parse_config_scan(conf
, dump_only
, removing
, "/run/modprobe.d",
1161 "/etc/modprobe.d", "/usr/local/lib/modprobe.d",
1162 "/lib/modprobe.d", NULL
);
1165 /* Read possible module arguments from the kernel command line. */
1166 static int parse_kcmdline(int dump_only
, struct modprobe_conf
*conf
)
1169 unsigned int linenum
= 0;
1171 struct module_options
**options
= &conf
->options
;
1172 struct module_blacklist
**blacklist
= &conf
->blacklist
;
1174 kcmdline
= fopen("/proc/cmdline", "r");
1178 while ((line
= getline_wrapped(kcmdline
, &linenum
)) != NULL
) {
1182 while ((arg
= strsep_skipspace(&ptr
, "\t ")) != NULL
) {
1183 char *sep
, *modname
, *opt
;
1185 if (strstr(arg
, "modprobe.blacklist=") != NULL
) {
1186 ptr
= strchr(arg
,'=') + 1;
1188 while ((modname
= strsep(&ptr
, ",")) != NULL
) {
1190 printf("blacklist %s\n", modname
);
1192 *blacklist
= add_blacklist(underscores(modname
), *blacklist
);
1196 sep
= strchr(arg
, '.');
1198 if (!strchr(sep
, '='))
1205 printf("options %s %s\n", modname
, opt
);
1207 *options
= add_options(underscores(modname
),
1218 static void add_to_env_var(const char *option
)
1222 if ((oldenv
= getenv("MODPROBE_OPTIONS")) != NULL
) {
1224 nofail_asprintf(&newenv
, "%s %s", oldenv
, option
);
1225 setenv("MODPROBE_OPTIONS", newenv
, 1);
1228 setenv("MODPROBE_OPTIONS", option
, 1);
1231 /* Prepend options from environment. */
1232 static char **merge_args(char *args
, char *argv
[], int *argc
)
1234 char *arg
, *argstring
;
1235 char **newargs
= NULL
;
1236 unsigned int i
, num_env
= 0;
1241 argstring
= NOFAIL(strdup(args
));
1242 for (arg
= strtok(argstring
, " "); arg
; arg
= strtok(NULL
, " ")) {
1244 newargs
= NOFAIL(realloc(newargs
,
1246 * (num_env
+ *argc
+ 1)));
1247 newargs
[num_env
] = arg
;
1253 /* Append commandline args */
1254 newargs
[0] = argv
[0];
1255 for (i
= 1; i
<= *argc
; i
++)
1256 newargs
[num_env
+i
] = argv
[i
];
1262 static char *gather_options(char *argv
[])
1264 char *optstring
= NOFAIL(strdup(""));
1266 /* Rest is module options */
1268 /* Quote value if it contains spaces. */
1269 unsigned int eq
= strcspn(*argv
, "=");
1271 if (strchr(*argv
+eq
, ' ') && !strchr(*argv
, '"')) {
1272 char quoted
[strlen(*argv
) + 3];
1274 sprintf(quoted
, "%s=\"%s\"", *argv
, *argv
+eq
+1);
1275 optstring
= append_option(optstring
, quoted
);
1277 optstring
= append_option(optstring
, *argv
);
1283 /* Do an install/remove command: replace $CMDLINE_OPTS if it's specified. */
1284 static void do_command(const char *modname
,
1285 const char *command
,
1289 const char *cmdline_opts
)
1292 char *p
, *replaced_cmd
= NOFAIL(strdup(command
));
1294 while ((p
= strstr(replaced_cmd
, "$CMDLINE_OPTS")) != NULL
) {
1296 nofail_asprintf(&new, "%.*s%s%s",
1297 (int)(p
- replaced_cmd
), replaced_cmd
, cmdline_opts
,
1298 p
+ strlen("$CMDLINE_OPTS"));
1303 info("%s %s\n", type
, replaced_cmd
);
1307 setenv("MODPROBE_MODULE", modname
, 1);
1308 ret
= system(replaced_cmd
);
1309 if (ret
== -1 || WEXITSTATUS(ret
))
1310 error("Error running %s command for %s\n", type
, modname
);
1316 /* Forward declaration */
1317 static int do_modprobe(const char *modname
,
1318 const char *cmdline_opts
,
1319 const struct modprobe_conf
*conf
,
1320 const char *dirname
,
1322 modprobe_flags_t flags
);
1324 static void do_softdep(const struct module_softdep
*softdep
,
1325 const char *cmdline_opts
,
1326 const struct modprobe_conf
*conf
,
1327 const char *dirname
,
1329 modprobe_flags_t flags
)
1331 struct string_table
*pre_modnames
, *post_modnames
;
1332 modprobe_flags_t softdep_flags
= flags
;
1335 softdep_flags
&= ~mit_first_time
;
1336 softdep_flags
&= ~mit_ignore_commands
;
1337 if (flags
& mit_remove
)
1338 softdep_flags
|= mit_quiet_inuse
;
1340 if (++recursion_depth
>= MAX_RECURSION
)
1341 fatal("modprobe: softdep dependency loop encountered %s %s\n",
1342 (flags
& mit_remove
) ? "removing" : "inserting",
1345 if (flags
& mit_remove
) {
1346 /* Reverse module order if removing. */
1347 pre_modnames
= softdep
->post
;
1348 post_modnames
= softdep
->pre
;
1350 pre_modnames
= softdep
->pre
;
1351 post_modnames
= softdep
->post
;
1354 /* Modprobe pre_modnames */
1356 for (i
= 0; pre_modnames
&& i
< pre_modnames
->cnt
; i
++) {
1357 /* Reverse module order if removing. */
1358 j
= (flags
& mit_remove
) ? pre_modnames
->cnt
-1 - i
: i
;
1360 do_modprobe(pre_modnames
->str
[j
], "",
1361 conf
, dirname
, warn
, softdep_flags
);
1364 /* Modprobe main module, passing cmdline_opts, ignoring softdep */
1366 do_modprobe(softdep
->modname
, cmdline_opts
,
1367 conf
, dirname
, warn
, flags
| mit_ignore_commands
);
1369 /* Modprobe post_modnames */
1371 for (i
= 0; post_modnames
&& i
< post_modnames
->cnt
; i
++) {
1372 /* Reverse module order if removing. */
1373 j
= (flags
& mit_remove
) ? post_modnames
->cnt
-1 - i
: i
;
1375 do_modprobe(post_modnames
->str
[j
], "", conf
,
1376 dirname
, warn
, softdep_flags
);
1380 /* Actually do the insert. */
1381 static int insmod(struct list_head
*list
,
1382 const char *optstring
,
1383 const char *cmdline_opts
,
1384 const struct modprobe_conf
*conf
,
1385 const char *dirname
,
1387 modprobe_flags_t flags
)
1390 struct elf_file
*module
;
1391 const struct module_softdep
*softdep
;
1392 const char *command
;
1393 struct module
*mod
= list_entry(list
->next
, struct module
, list
);
1398 /* Take us off the list. */
1399 list_del(&mod
->list
);
1401 /* Do things we (or parent) depend on first. */
1402 if (!list_empty(list
)) {
1403 modprobe_flags_t f
= flags
;
1404 f
&= ~mit_first_time
;
1405 f
&= ~mit_ignore_commands
;
1406 if ((rc
= insmod(list
, "", "", conf
, dirname
, warn
, f
)) != 0)
1408 error("Error inserting %s (%s): %s\n",
1409 mod
->modname
, mod
->filename
,
1410 insert_moderror(errno
));
1415 /* Don't do ANYTHING if already in kernel. */
1416 already_loaded
= module_in_kernel(mod
->modname
, NULL
);
1418 if (!(flags
& mit_ignore_loaded
) && already_loaded
== 1) {
1419 if (flags
& mit_first_time
)
1420 error("Module %s already in kernel.\n", mod
->modname
);
1424 softdep
= find_softdep(mod
->modname
, conf
->softdeps
);
1425 if (softdep
&& !(flags
& mit_ignore_commands
)) {
1426 do_softdep(softdep
, cmdline_opts
, conf
, dirname
, error
, flags
);
1430 command
= find_command(mod
->modname
, conf
->commands
);
1431 if (command
&& !(flags
& mit_ignore_commands
)) {
1432 if (already_loaded
== -1) {
1433 warn("/sys/module/ not present or too old,"
1434 " and /proc/modules does not exist.\n");
1435 warn("Ignoring install commands for %s"
1436 " in case it is already loaded.\n",
1439 do_command(mod
->modname
, command
, flags
& mit_dry_run
,
1440 error
, "install", cmdline_opts
);
1445 module
= grab_elf_file(mod
->filename
);
1447 error("Could not read '%s': %s\n", mod
->filename
,
1448 (errno
== ENOEXEC
) ? "Invalid module format" :
1452 if (flags
& mit_strip_modversion
)
1453 module
->ops
->strip_section(module
, "__versions");
1454 if (flags
& mit_strip_vermagic
)
1455 clear_magic(module
);
1457 /* Config file might have given more options */
1458 opts
= add_extra_options(mod
->modname
, optstring
, conf
->options
);
1460 info("insmod %s %s\n", mod
->filename
, opts
);
1462 if (flags
& mit_dry_run
)
1465 ret
= init_module(module
->data
, module
->len
, opts
);
1467 if (errno
== EEXIST
) {
1468 if (flags
& mit_first_time
)
1469 error("Module %s already in kernel.\n",
1473 /* don't warn noisely if we're loading multiple aliases. */
1474 /* one of the aliases may try to use hardware we don't have. */
1475 if ((error
!= warn
) || (verbose
))
1476 error("Error inserting %s (%s): %s\n",
1477 mod
->modname
, mod
->filename
,
1478 insert_moderror(errno
));
1482 release_elf_file(module
);
1489 /* Do recursive removal. */
1490 static void rmmod(struct list_head
*list
,
1491 const char *cmdline_opts
,
1492 const struct modprobe_conf
*conf
,
1493 const char *dirname
,
1495 modprobe_flags_t flags
)
1497 const struct module_softdep
*softdep
;
1498 const char *command
;
1499 unsigned int usecount
= 0;
1500 struct module
*mod
= list_entry(list
->next
, struct module
, list
);
1503 /* Take first one off the list. */
1504 list_del(&mod
->list
);
1506 /* Don't do ANYTHING if not loaded. */
1507 exists
= module_in_kernel(mod
->modname
, &usecount
);
1509 goto nonexistent_module
;
1511 /* Even if renamed, find commands/softdeps to orig. name. */
1513 softdep
= find_softdep(mod
->modname
, conf
->softdeps
);
1514 if (softdep
&& !(flags
& mit_ignore_commands
)) {
1515 do_softdep(softdep
, cmdline_opts
, conf
, dirname
, error
, flags
);
1519 command
= find_command(mod
->modname
, conf
->commands
);
1520 if (command
&& !(flags
& mit_ignore_commands
)) {
1522 warn("/sys/module/ not present or too old,"
1523 " and /proc/modules does not exist.\n");
1524 warn("Ignoring remove commands for %s"
1525 " in case it is not loaded.\n",
1528 do_command(mod
->modname
, command
, flags
& mit_dry_run
,
1529 error
, "remove", cmdline_opts
);
1534 if (usecount
!= 0) {
1535 if (!(flags
& mit_quiet_inuse
))
1536 error("Module %s is in use.\n", mod
->modname
);
1540 info("rmmod %s\n", mod
->filename
);
1542 if (flags
& mit_dry_run
)
1545 if (delete_module(mod
->modname
, O_EXCL
) != 0) {
1546 if (errno
== ENOENT
)
1547 goto nonexistent_module
;
1548 error("Error removing %s (%s): %s\n",
1549 mod
->modname
, mod
->filename
,
1550 remove_moderror(errno
));
1554 /* Now do things we depend. */
1555 if (!list_empty(list
)) {
1556 flags
&= ~mit_first_time
;
1557 flags
&= ~mit_ignore_commands
;
1558 flags
|= mit_quiet_inuse
;
1560 rmmod(list
, "", conf
, dirname
, warn
, flags
);
1566 if (flags
& mit_first_time
)
1567 fatal("Module %s is not in kernel.\n", mod
->modname
);
1571 static int handle_module(const char *modname
,
1572 struct list_head
*todo_list
,
1573 const char *options
,
1574 const char *cmdline_opts
,
1575 const struct modprobe_conf
*conf
,
1576 const char *dirname
,
1578 modprobe_flags_t flags
)
1580 if (list_empty(todo_list
)) {
1581 const char *command
;
1583 /* The dependencies have to be real modules, but
1584 handle case where the first is completely bogus. */
1586 command
= find_command(modname
, conf
->commands
);
1587 if (command
&& !(flags
& mit_ignore_commands
)) {
1588 do_command(modname
, command
, flags
& mit_dry_run
, error
,
1589 (flags
& mit_remove
) ? "remove":"install", cmdline_opts
);
1594 error("Module %s not found.\n", modname
);
1598 if (flags
& mit_remove
)
1599 rmmod(todo_list
, cmdline_opts
,
1600 conf
, dirname
, error
, flags
);
1602 insmod(todo_list
, options
,
1603 cmdline_opts
, conf
, dirname
, error
, flags
);
1608 static int handle_builtin_module(const char *modname
,
1610 modprobe_flags_t flags
)
1612 if (flags
& mit_remove
) {
1613 error("Module %s is builtin\n", modname
);
1615 } else if (flags
& mit_first_time
) {
1616 error("Module %s already in kernel (builtin).\n", modname
);
1618 } else if (flags
& mit_ignore_loaded
) {
1619 /* --show-depends given */
1620 info("builtin %s\n", modname
);
1625 static int do_modprobe(const char *modname
,
1626 const char *cmdline_opts
,
1627 const struct modprobe_conf
*conf
,
1628 const char *dirname
,
1630 modprobe_flags_t flags
)
1632 struct module_alias
*matching_aliases
;
1636 matching_aliases
= find_aliases(conf
->aliases
, modname
);
1638 /* No luck? Try symbol names, if starts with symbol:. */
1639 if (!matching_aliases
&& strstarts(modname
, "symbol:")) {
1642 nofail_asprintf(&symfilename
, "%s/modules.symbols", dirname
);
1643 read_aliases(symfilename
, modname
, 0, &matching_aliases
);
1646 if (!matching_aliases
) {
1647 if(!strchr(modname
, ':'))
1648 read_depends(dirname
, modname
, &list
);
1650 /* We only use canned aliases as last resort. */
1651 if (list_empty(&list
)
1652 && !find_softdep(modname
, conf
->softdeps
)
1653 && !find_command(modname
, conf
->commands
))
1655 char *aliasfilename
;
1657 nofail_asprintf(&aliasfilename
, "%s/modules.alias",
1659 read_aliases(aliasfilename
, modname
, 0,
1661 free(aliasfilename
);
1662 /* builtin module? */
1663 if (!matching_aliases
&& module_builtin(dirname
, modname
) > 0) {
1664 failed
|= handle_builtin_module(modname
, error
,
1671 apply_blacklist(&matching_aliases
, conf
->blacklist
);
1672 if(flags
& mit_resolve_alias
) {
1673 struct module_alias
*aliases
= matching_aliases
;
1675 for(; aliases
; aliases
=aliases
->next
)
1676 printf("%s\n", aliases
->module
);
1679 if (matching_aliases
) {
1680 errfn_t err
= error
;
1681 struct module_alias
*aliases
= matching_aliases
;
1683 /* More than one alias? Don't bail out on failure. */
1687 /* Add the options for this alias. */
1689 opts
= add_extra_options(modname
,
1690 cmdline_opts
, conf
->options
);
1692 read_depends(dirname
, aliases
->module
, &list
);
1693 failed
|= handle_module(aliases
->module
,
1694 &list
, opts
, cmdline_opts
,
1695 conf
, dirname
, err
, flags
);
1697 aliases
= aliases
->next
;
1699 INIT_LIST_HEAD(&list
);
1702 if (flags
& mit_use_blacklist
1703 && find_blacklist(modname
, conf
->blacklist
))
1706 failed
|= handle_module(modname
, &list
, cmdline_opts
,
1707 cmdline_opts
, conf
, dirname
, error
, flags
);
1711 free_aliases(matching_aliases
);
1715 static const struct option options
[] = { { "version", 0, NULL
, 'V' },
1716 { "verbose", 0, NULL
, 'v' },
1717 { "quiet", 0, NULL
, 'q' },
1718 { "syslog", 0, NULL
, 's' },
1719 { "show", 0, NULL
, 'n' },
1720 { "dry-run", 0, NULL
, 'n' },
1721 { "show-depends", 0, NULL
, 'D' },
1722 { "resolve-alias", 0, NULL
, 'R' },
1723 { "dirname", 1, NULL
, 'd' },
1724 { "set-version", 1, NULL
, 'S' },
1725 { "config", 1, NULL
, 'C' },
1726 { "remove", 0, NULL
, 'r' },
1727 { "showconfig", 0, NULL
, 'c' },
1728 { "list", 0, NULL
, 'l' },
1729 { "type", 1, NULL
, 't' },
1730 { "all", 0, NULL
, 'a' },
1731 { "ignore-install", 0, NULL
, 'i' },
1732 { "ignore-remove", 0, NULL
, 'i' },
1733 { "use-blacklist", 0, NULL
, 'b' },
1734 { "force", 0, NULL
, 'f' },
1735 { "force-vermagic", 0, NULL
, 1 },
1736 { "force-modversion", 0, NULL
, 2 },
1737 { "first-time", 0, NULL
, 3 },
1738 { "dump-modversions", 0, NULL
, 4 },
1739 { NULL
, 0, NULL
, 0 } };
1741 int main(int argc
, char *argv
[])
1744 struct stat statbuf
;
1746 int dump_config
= 0;
1749 int dump_modver
= 0;
1750 unsigned int i
, num_modules
;
1752 const char *configname
= NULL
;
1754 char *cmdline_opts
= NULL
;
1756 errfn_t error
= fatal
;
1758 modprobe_flags_t flags
= 0;
1759 struct modprobe_conf conf
= {};
1761 recursion_depth
= 0;
1763 /* Prepend options from environment. */
1764 argv
= merge_args(getenv("MODPROBE_OPTIONS"), argv
, &argc
);
1767 while ((opt
= getopt_long(argc
, argv
, "Vvqsnd:S:C:DRrclt:aibf", options
, NULL
)) != -1){
1770 puts(PACKAGE
" version " VERSION
);
1773 add_to_env_var("-v");
1778 add_to_env_var("-q");
1781 add_to_env_var("-s");
1785 flags
|= mit_dry_run
;
1791 strncpy(buf
.release
, optarg
, sizeof(buf
.release
));
1792 buf
.release
[sizeof(buf
.release
)-1] = '\0';
1795 configname
= optarg
;
1796 add_to_env_var("-C");
1797 add_to_env_var(configname
);
1800 flags
|= mit_dry_run
;
1801 flags
|= mit_ignore_loaded
;
1805 flags
|= mit_resolve_alias
;
1808 flags
|= mit_remove
;
1824 flags
|= mit_ignore_commands
;
1827 flags
|= mit_use_blacklist
;
1830 flags
|= mit_strip_vermagic
;
1831 flags
|= mit_strip_modversion
;
1834 flags
|= mit_strip_vermagic
;
1837 flags
|= mit_strip_modversion
;
1840 flags
|= mit_first_time
;
1846 print_usage(argv
[0]);
1850 /* If stderr not open, go to syslog */
1851 if (logging
|| fstat(STDERR_FILENO
, &statbuf
) != 0) {
1852 openlog("modprobe", LOG_CONS
, LOG_DAEMON
);
1856 if (argc
< optind
+ 1 && !dump_config
&& !list_only
)
1857 print_usage(argv
[0]);
1859 nofail_asprintf(&dirname
, "%s%s/%s", basedir
, MODULE_DIR
, buf
.release
);
1861 /* Old-style -t xxx wildcard? Only with -l. */
1863 if (optind
+1 < argc
)
1864 fatal("Can't have multiple wildcards\n");
1865 /* fprintf(stderr, "man find\n"); return 1; */
1866 failed
= do_wildcard(dirname
, type
, argv
[optind
]?:"*");
1870 fatal("-t only supported with -l");
1873 dump_modversions(argv
[optind
], error
);
1877 /* Read aliases, options etc. */
1878 parse_toplevel_config(configname
, &conf
, dump_config
, flags
& mit_remove
);
1880 /* Read module options from kernel command line */
1881 parse_kcmdline(dump_config
, &conf
);
1884 char *aliasfilename
, *symfilename
;
1885 struct modprobe_conf conf
= {};
1887 nofail_asprintf(&aliasfilename
, "%s/modules.alias", dirname
);
1888 nofail_asprintf(&symfilename
, "%s/modules.symbols", dirname
);
1890 read_aliases(aliasfilename
, "", 1, &conf
.aliases
);
1891 read_aliases(symfilename
, "", 1, &conf
.aliases
);
1896 if ((flags
& mit_remove
) || all
) {
1897 num_modules
= argc
- optind
;
1898 cmdline_opts
= NOFAIL(strdup(""));
1901 cmdline_opts
= gather_options(argv
+optind
+1);
1904 /* Convert names we are looking for */
1905 for (i
= 0; i
< num_modules
; i
++)
1906 underscores(argv
[optind
+ i
]);
1908 /* If we have a list of modules to remove, try the unused ones first.
1909 Aliases and modules which don't seem to exist are handled later. */
1910 if (flags
& mit_remove
) {
1915 for (i
= 0; i
< num_modules
; i
++) {
1916 const char *modname
;
1920 modname
= argv
[optind
+ i
];
1923 if (module_in_kernel(modname
, &usecount
) != 1)
1928 read_depends(dirname
, modname
, &list
);
1930 failed
|= handle_module(modname
, &list
,
1931 cmdline_opts
, cmdline_opts
,
1932 &conf
, dirname
, error
, flags
);
1934 argv
[optind
+ i
] = NULL
;
1935 INIT_LIST_HEAD(&list
);
1937 } while (progress
> 0);
1940 /* num_modules is always 1 except for -r or -a. */
1941 for (i
= 0; i
< num_modules
; i
++) {
1942 const char *modname
= argv
[optind
+ i
];
1947 failed
|= do_modprobe(modname
, cmdline_opts
,
1948 &conf
, dirname
, error
, flags
);
1956 /* Don't bother to free conf */