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 extern long init_module(void *, unsigned long, const char *);
55 extern long delete_module(const char *, unsigned int);
58 struct list_head list
;
68 mit_use_blacklist
= 8,
69 mit_ignore_commands
= 16,
70 mit_ignore_loaded
= 32,
72 mit_strip_vermagic
= 128,
73 mit_strip_modversion
= 256,
74 mit_resolve_alias
= 512
79 #define MODULE_DIR "/lib/modules"
82 static void print_usage(const char *progname
)
85 "Usage: %s [-v] [-V] [-C config-file] [-d <dirname> ] [-n] [-i] [-q] [-b] [-o <modname>] [ --dump-modversions ] <modname> [parameters...]\n"
86 "%s -r [-n] [-i] [-v] <modulename> ...\n"
87 "%s -l -t <dirname> [ -a <modulename> ...]\n",
88 progname
, progname
, progname
);
92 static struct module
*find_module(const char *filename
, struct list_head
*list
)
96 list_for_each_entry(i
, list
, list
) {
97 if (streq(i
->filename
, filename
))
103 /* We used to lock with a write flock but that allows regular users to block
104 * module load by having a read lock on the module file (no way to bust the
105 * existing locks without killing the offending process). Instead, we now
106 * do the system call/init_module and allow the kernel to fail us instead.
108 static int open_file(const char *filename
)
110 int fd
= open(filename
, O_RDONLY
, 0);
115 static void close_file(int fd
)
117 /* Valgrind is picky... */
121 static void add_module(char *filename
, int namelen
, struct list_head
*list
)
125 /* If it's a duplicate: move it to the end, so it gets
126 inserted where it is *first* required. */
127 mod
= find_module(filename
, list
);
129 list_del(&mod
->list
);
131 /* No match. Create a new module. */
132 mod
= NOFAIL(malloc(sizeof(struct module
) + namelen
+ 1));
133 memcpy(mod
->filename
, filename
, namelen
);
134 mod
->filename
[namelen
] = '\0';
135 mod
->modname
= NOFAIL(malloc(namelen
+ 1));
136 filename2modname(mod
->modname
, mod
->filename
);
139 list_add_tail(&mod
->list
, list
);
142 /* Compare len chars of a to b, with _ and - equivalent. */
143 static int modname_equal(const char *a
, const char *b
, unsigned int len
)
147 if (strlen(b
) != len
)
150 for (i
= 0; i
< len
; i
++) {
151 if ((a
[i
] == '_' || a
[i
] == '-')
152 && (b
[i
] == '_' || b
[i
] == '-'))
160 /* Fills in list of modules if this is the line we want. */
161 static int add_modules_dep_line(char *line
,
163 struct list_head
*list
,
168 char *modname
, *fullpath
;
170 /* Ignore lines without : or which start with a # */
171 ptr
= strchr(line
, ':');
172 if (ptr
== NULL
|| line
[strspn(line
, "\t ")] == '#')
175 /* Is this the module we are looking for? */
177 modname
= my_basename(line
);
179 len
= strlen(modname
);
180 if (strchr(modname
, '.'))
181 len
= strchr(modname
, '.') - modname
;
182 if (!modname_equal(modname
, name
, len
))
185 /* Create the list. */
186 if ('/' == line
[0]) { /* old style deps - absolute path specified */
187 add_module(line
, ptr
- line
, list
);
189 nofail_asprintf(&fullpath
, "%s/%s", dirname
, line
);
190 add_module(fullpath
, strlen(dirname
)+1+(ptr
- line
), list
);
197 ptr
+= strspn(ptr
, " \t");
201 ptr
+= strcspn(ptr
, " \t");
202 if ('/' == dep_start
[0]) { /* old style deps */
203 add_module(dep_start
, ptr
- dep_start
, list
);
205 nofail_asprintf(&fullpath
, "%s/%s", dirname
, dep_start
);
207 strlen(dirname
)+1+(ptr
- dep_start
), list
);
214 static int read_depends_file(const char *dirname
,
215 const char *start_name
,
216 struct list_head
*list
)
218 char *modules_dep_name
;
220 struct index_file
*modules_dep
;
222 nofail_asprintf(&modules_dep_name
, "%s/%s", dirname
, "modules.dep.bin");
223 modules_dep
= index_file_open(modules_dep_name
);
225 free(modules_dep_name
);
229 line
= index_search(modules_dep
, start_name
);
231 /* Value is standard dependency line format */
232 if (!add_modules_dep_line(line
, start_name
, list
, dirname
))
233 fatal("Module index is inconsistent\n");
237 index_file_close(modules_dep
);
238 free(modules_dep_name
);
243 static void read_depends(const char *dirname
,
244 const char *start_name
,
245 struct list_head
*list
)
247 char *modules_dep_name
;
252 if (use_binary_indexes
)
253 if (read_depends_file(dirname
, start_name
, list
))
256 nofail_asprintf(&modules_dep_name
, "%s/%s", dirname
, "modules.dep");
257 modules_dep
= fopen(modules_dep_name
, "r");
259 fatal("Could not load %s: %s\n",
260 modules_dep_name
, strerror(errno
));
262 /* Stop at first line, as we can have duplicates (eg. symlinks
264 while (!done
&& (line
= getline_wrapped(modules_dep
, NULL
)) != NULL
) {
265 done
= add_modules_dep_line(line
, start_name
, list
, dirname
);
269 free(modules_dep_name
);
272 /* We use error numbers in a loose translation... */
273 static const char *insert_moderror(int err
)
277 return "Invalid module format";
279 return "Unknown symbol in module, or unknown parameter (see dmesg)";
281 return "Kernel does not have module support";
283 return strerror(err
);
287 static const char *remove_moderror(int err
)
291 return "No such module";
293 return "Kernel does not have module unloading support";
295 return strerror(err
);
299 static void replace_modname(struct elf_file
*module
,
300 void *mem
, unsigned long len
,
301 const char *oldname
, const char *newname
)
305 /* 64 - sizeof(unsigned long) - 1 */
306 if (strlen(newname
) > 55)
307 fatal("New name %s is too long\n", newname
);
309 /* Find where it is in the module structure. Don't assume layout! */
310 for (p
= mem
; p
< (char *)mem
+ len
- strlen(oldname
); p
++) {
311 if (memcmp(p
, oldname
, strlen(oldname
)) == 0) {
317 warn("Could not find old name in %s to replace!\n", module
->pathname
);
320 static void rename_module(struct elf_file
*module
,
328 modstruct
= module
->ops
->load_section(module
,
329 ".gnu.linkonce.this_module", &len
);
332 modstruct
= module
->ops
->load_section(module
, "__module", &len
);
334 warn("Could not find module name to change in %s\n",
337 replace_modname(module
, modstruct
, len
, oldname
, newname
);
340 static void clear_magic(struct elf_file
*module
)
342 struct string_table
*tbl
;
345 /* Old-style: __vermagic section */
346 module
->ops
->strip_section(module
, "__vermagic");
348 /* New-style: in .modinfo section */
349 tbl
= module
->ops
->load_strings(module
, ".modinfo", NULL
, fatal
);
350 for (j
= 0; tbl
&& j
< tbl
->cnt
; j
++) {
351 const char *p
= tbl
->str
[j
];
352 if (strstarts(p
, "vermagic=")) {
353 memset((char *)p
, 0, strlen(p
));
359 struct module_options
361 struct module_options
*next
;
366 struct module_command
368 struct module_command
*next
;
375 struct module_alias
*next
;
379 struct module_blacklist
381 struct module_blacklist
*next
;
385 /* Link in a new option line from the config file. */
386 static struct module_options
*
387 add_options(const char *modname
,
389 struct module_options
*options
)
391 struct module_options
*new;
394 new = NOFAIL(malloc(sizeof(*new)));
395 new->modulename
= NOFAIL(strdup(modname
));
396 new->options
= NOFAIL(strdup(option
));
397 /* We can handle tabs, kernel can't. */
398 for (tab
= strchr(new->options
, '\t'); tab
; tab
= strchr(tab
, '\t'))
404 /* Link in a new install line from the config file. */
405 static struct module_command
*
406 add_command(const char *modname
,
408 struct module_command
*commands
)
410 struct module_command
*new;
412 new = NOFAIL(malloc(sizeof(*new)));
413 new->modulename
= NOFAIL(strdup(modname
));
414 new->command
= NOFAIL(strdup(command
));
415 new->next
= commands
;
419 /* Link in a new alias line from the config file. */
420 static struct module_alias
*
421 add_alias(const char *modname
, struct module_alias
*aliases
)
423 struct module_alias
*new;
425 new = NOFAIL(malloc(sizeof(*new)));
426 new->module
= NOFAIL(strdup(modname
));
431 /* Link in a new blacklist line from the config file. */
432 static struct module_blacklist
*
433 add_blacklist(const char *modname
, struct module_blacklist
*blacklist
)
435 struct module_blacklist
*new;
437 new = NOFAIL(malloc(sizeof(*new)));
438 new->modulename
= NOFAIL(strdup(modname
));
439 new->next
= blacklist
;
443 /* Find blacklist commands if any. */
445 find_blacklist(const char *modname
, const struct module_blacklist
*blacklist
)
448 if (streq(blacklist
->modulename
, modname
))
450 blacklist
= blacklist
->next
;
455 /* return a new alias list, with backlisted elems filtered out */
456 static struct module_alias
*
457 apply_blacklist(const struct module_alias
*aliases
,
458 const struct module_blacklist
*blacklist
)
460 struct module_alias
*result
= NULL
;
462 char *modname
= aliases
->module
;
463 if (!find_blacklist(modname
, blacklist
))
464 result
= add_alias(modname
, result
);
465 aliases
= aliases
->next
;
470 /* Find install commands if any. */
471 static const char *find_command(const char *modname
,
472 const struct module_command
*commands
)
475 if (fnmatch(commands
->modulename
, modname
, 0) == 0)
476 return commands
->command
;
477 commands
= commands
->next
;
482 static char *append_option(char *options
, const char *newoption
)
484 options
= NOFAIL(realloc(options
, strlen(options
) + 1
485 + strlen(newoption
) + 1));
486 if (strlen(options
)) strcat(options
, " ");
487 strcat(options
, newoption
);
491 static char *prepend_option(char *options
, const char *newoption
)
494 l1
= strlen(options
);
495 l2
= strlen(newoption
);
496 /* the resulting string will look like
497 * newoption + ' ' + options + '\0' */
499 options
= NOFAIL(realloc(options
, l2
+ 1 + l1
+ 1));
500 memmove(options
+ l2
+ 1, options
, l1
+ 1);
502 memcpy(options
, newoption
, l2
);
504 options
= NOFAIL(realloc(options
, l2
+ 1));
505 memcpy(options
, newoption
, l2
);
512 static char *add_extra_options(const char *modname
,
513 const char *optstring
,
514 const struct module_options
*options
)
516 char *opts
= NOFAIL(strdup(optstring
));
519 if (streq(options
->modulename
, modname
))
520 opts
= prepend_option(opts
, options
->options
);
521 options
= options
->next
;
526 /* Is module in /proc/modules? If so, fill in usecount if not NULL.
527 0 means no, 1 means yes, -1 means unknown.
529 static int module_in_procfs(const char *modname
, unsigned int *usecount
)
535 /* Might not be mounted yet. Don't fail. */
536 proc_modules
= fopen("/proc/modules", "r");
540 while ((line
= getline_wrapped(proc_modules
, NULL
)) != NULL
) {
541 char *entry
= strtok(line
, " \n");
543 if (entry
&& streq(entry
, modname
)) {
544 /* If it exists, usecount is the third entry. */
545 if (!strtok(NULL
, " \n"))
548 if (!(entry
= strtok(NULL
, " \n"))) /* usecount */
552 *usecount
= atoi(entry
);
554 /* Followed by - then status. */
555 if (strtok(NULL
, " \n")
556 && (entry
= strtok(NULL
, " \n")) != NULL
) {
557 /* No locking, we might hit cases
558 * where module is in flux. Spin. */
559 if (streq(entry
, "Loading")
560 || streq(entry
, "Unloading")) {
563 fclose(proc_modules
);
570 fclose(proc_modules
);
575 fclose(proc_modules
);
579 /* Read sysfs attribute into a buffer.
580 * returns: 1 = ok, 0 = attribute missing,
581 * -1 = file error (or empty file, but we don't care).
583 static int read_attribute(const char *filename
, char *buf
, size_t buflen
)
588 file
= fopen(filename
, "r");
590 return (errno
== ENOENT
) ? 0 : -1;
591 s
= fgets(buf
, buflen
, file
);
594 return (s
== NULL
) ? -1 : 1;
597 /* is this a built-in module?
598 * 0: no, 1: yes, -1: don't know
600 static int module_builtin(const char *dirname
, const char *modname
)
602 struct index_file
*index
;
603 char *filename
, *value
;
605 nofail_asprintf(&filename
, "%s/modules.builtin.bin", dirname
);
606 index
= index_file_open(filename
);
610 value
= index_search(index
, modname
);
612 return value
? 1 : 0;
615 /* Is module in /sys/module? If so, fill in usecount if not NULL.
616 0 means no, 1 means yes, -1 means unknown.
618 static int module_in_sysfs(const char *modname
, unsigned int *usecount
)
624 const int ATTR_LEN
= 16;
627 /* Check sysfs is mounted */
628 if (stat("/sys/module", &finfo
) < 0)
632 nofail_asprintf(&name
, "/sys/module/%s", modname
);
633 ret
= stat(name
, &finfo
);
636 return (errno
== ENOENT
) ? 0 : -1; /* Not found or unknown. */
638 nofail_asprintf(&name
, "/sys/module/%s/initstate", modname
);
639 ret
= read_attribute(name
, attr
, ATTR_LEN
);
642 nofail_asprintf(&name
, "/sys/module/%s", modname
);
643 if (stat(name
, &finfo
) < 0) {
644 /* module was removed before we could read initstate */
647 /* initstate not available (2.6.19 or earlier) */
654 /* Wait for the existing module to either go live or disappear. */
655 while (ret
== 1 && !streq(attr
, "live\n")) {
657 ret
= read_attribute(name
, attr
, ATTR_LEN
);
664 /* Get reference count, if it exists. */
665 if (usecount
!= NULL
) {
666 nofail_asprintf(&name
, "/sys/module/%s/refcnt", modname
);
667 ret
= read_attribute(name
, attr
, ATTR_LEN
);
670 *usecount
= atoi(attr
);
676 /* Is module loaded? If so, fill in usecount if not NULL.
677 0 means no, 1 means yes, -1 means unknown.
679 static int module_in_kernel(const char *modname
, unsigned int *usecount
)
683 result
= module_in_sysfs(modname
, usecount
);
687 /* /sys/module/%s/initstate is only available since 2.6.20,
688 fallback to /proc/modules to get module state on earlier kernels. */
689 return module_in_procfs(modname
, usecount
);
692 void dump_modversions(const char *filename
, errfn_t error
)
694 struct elf_file
*module
;
696 module
= grab_elf_file(filename
);
698 error("%s: %s\n", filename
, strerror(errno
));
701 if (module
->ops
->dump_modvers(module
) < 0)
702 error("Wrong section size in '%s'\n", filename
);
703 release_elf_file(module
);
706 /* Does path contain directory(s) subpath? */
707 static int type_matches(const char *path
, const char *subpath
)
709 char *subpath_with_slashes
;
712 nofail_asprintf(&subpath_with_slashes
, "/%s/", subpath
);
714 ret
= (strstr(path
, subpath_with_slashes
) != NULL
);
715 free(subpath_with_slashes
);
720 static int do_wildcard(const char *dirname
,
722 const char *wildcard
)
724 char *modules_dep_name
;
728 /* Canonicalize wildcard */
729 wcard
= strdup(wildcard
);
732 nofail_asprintf(&modules_dep_name
, "%s/%s", dirname
, "modules.dep");
733 modules_dep
= fopen(modules_dep_name
, "r");
735 fatal("Could not load %s: %s\n",
736 modules_dep_name
, strerror(errno
));
738 while ((line
= getline_wrapped(modules_dep
, NULL
)) != NULL
) {
741 /* Ignore lines without : or which start with a # */
742 ptr
= strchr(line
, ':');
743 if (ptr
== NULL
|| line
[strspn(line
, "\t ")] == '#')
747 /* "type" must match complete directory component(s). */
748 if (!type
|| type_matches(line
, type
)) {
749 char modname
[strlen(line
)+1];
751 filename2modname(modname
, line
);
752 if (fnmatch(wcard
, modname
, 0) == 0)
753 printf("%s\n", line
);
759 free(modules_dep_name
);
764 static char *strsep_skipspace(char **string
, char *delim
)
768 *string
+= strspn(*string
, delim
);
769 return strsep(string
, delim
);
772 static int parse_config_scan(const char *filename
,
776 struct module_options
**options
,
777 struct module_command
**commands
,
778 struct module_alias
**alias
,
779 struct module_blacklist
**blacklist
);
781 static int parse_config_file(const char *filename
,
785 struct module_options
**options
,
786 struct module_command
**commands
,
787 struct module_alias
**aliases
,
788 struct module_blacklist
**blacklist
)
791 unsigned int linenum
= 0;
794 cfile
= fopen(filename
, "r");
798 while ((line
= getline_wrapped(cfile
, &linenum
)) != NULL
) {
803 printf("%s\n", line
);
805 cmd
= strsep_skipspace(&ptr
, "\t ");
806 if (cmd
== NULL
|| cmd
[0] == '#' || cmd
[0] == '\0') {
811 if (streq(cmd
, "alias")) {
812 char *wildcard
= strsep_skipspace(&ptr
, "\t ");
813 char *realname
= strsep_skipspace(&ptr
, "\t ");
815 if (!wildcard
|| !realname
)
816 grammar(cmd
, filename
, linenum
);
817 else if (fnmatch(underscores(wildcard
),name
,0) == 0)
818 *aliases
= add_alias(underscores(realname
), *aliases
);
819 } else if (streq(cmd
, "include")) {
820 struct module_alias
*newalias
= NULL
;
823 newfilename
= strsep_skipspace(&ptr
, "\t ");
825 grammar(cmd
, filename
, linenum
);
827 warn("\"include %s\" is deprecated, "
828 "please use /etc/modprobe.d\n", newfilename
);
829 if (strstarts(newfilename
, "/etc/modprobe.d")) {
830 warn("\"include /etc/modprobe.d\" is "
831 "the default, ignored\n");
833 if (!parse_config_scan(newfilename
, name
,
835 options
, commands
, &newalias
,
837 warn("Failed to open included"
838 " config file %s: %s\n",
839 newfilename
, strerror(errno
));
841 /* Files included override aliases,
842 etc that was already set ... */
846 } else if (streq(cmd
, "options")) {
847 modname
= strsep_skipspace(&ptr
, "\t ");
848 if (!modname
|| !ptr
)
849 grammar(cmd
, filename
, linenum
);
851 ptr
+= strspn(ptr
, "\t ");
852 *options
= add_options(underscores(modname
),
855 } else if (streq(cmd
, "install")) {
856 modname
= strsep_skipspace(&ptr
, "\t ");
857 if (!modname
|| !ptr
)
858 grammar(cmd
, filename
, linenum
);
859 else if (!removing
) {
860 ptr
+= strspn(ptr
, "\t ");
861 *commands
= add_command(underscores(modname
),
864 } else if (streq(cmd
, "blacklist")) {
865 modname
= strsep_skipspace(&ptr
, "\t ");
867 grammar(cmd
, filename
, linenum
);
868 else if (!removing
) {
869 *blacklist
= add_blacklist(underscores(modname
),
872 } else if (streq(cmd
, "remove")) {
873 modname
= strsep_skipspace(&ptr
, "\t ");
874 if (!modname
|| !ptr
)
875 grammar(cmd
, filename
, linenum
);
877 ptr
+= strspn(ptr
, "\t ");
878 *commands
= add_command(underscores(modname
),
881 } else if (streq(cmd
, "config")) {
882 char *tmp
= strsep_skipspace(&ptr
, "\t ");
885 grammar(cmd
, filename
, linenum
);
886 else if (streq(tmp
, "binary_indexes")) {
887 tmp
= strsep_skipspace(&ptr
, "\t ");
888 if (streq(tmp
, "yes"))
889 use_binary_indexes
= 1;
890 if (streq(tmp
, "no"))
891 use_binary_indexes
= 0;
894 grammar(cmd
, filename
, linenum
);
902 /* fallback to plain-text aliases file as necessary */
903 static int read_aliases_file(const char *filename
,
907 struct module_options
**options
,
908 struct module_command
**commands
,
909 struct module_alias
**aliases
,
910 struct module_blacklist
**blacklist
)
912 struct index_value
*realnames
;
913 struct index_value
*realname
;
915 struct index_file
*index
;
917 if (!use_binary_indexes
)
920 nofail_asprintf(&binfile
, "%s.bin", filename
);
921 index
= index_file_open(binfile
);
928 index_dump(index
, stdout
, "alias ");
930 index_file_close(index
);
934 realnames
= index_searchwild(index
, name
);
935 for (realname
= realnames
; realname
; realname
= realname
->next
)
936 *aliases
= add_alias(realname
->value
, *aliases
);
937 index_values_free(realnames
);
940 index_file_close(index
);
944 return parse_config_file(filename
, name
, dump_only
, removing
,
945 options
, commands
, aliases
, blacklist
);
948 static int parse_config_scan(const char *filename
,
952 struct module_options
**options
,
953 struct module_command
**commands
,
954 struct module_alias
**aliases
,
955 struct module_blacklist
**blacklist
)
960 dir
= opendir(filename
);
963 struct list_head node
;
966 LIST_HEAD(files_list
);
967 struct file_entry
*fe
, *fe_tmp
;
970 /* sort files from directory into list */
971 while ((i
= readdir(dir
)) != NULL
) {
974 if (i
->d_name
[0] == '.')
976 if (!config_filter(i
->d_name
))
979 len
= strlen(i
->d_name
);
981 (strcmp(&i
->d_name
[len
-5], ".conf") != 0 &&
982 strcmp(&i
->d_name
[len
-6], ".alias") != 0))
983 warn("All config files need .conf: %s/%s, "
984 "it will be ignored in a future release.\n",
985 filename
, i
->d_name
);
986 fe
= malloc(sizeof(struct file_entry
) + len
+ 1);
989 strcpy(fe
->name
, i
->d_name
);
990 list_for_each_entry(fe_tmp
, &files_list
, node
)
991 if (strcmp(fe_tmp
->name
, fe
->name
) >= 0)
993 list_add_tail(&fe
->node
, &fe_tmp
->node
);
997 /* parse list of files */
998 list_for_each_entry_safe(fe
, fe_tmp
, &files_list
, node
) {
1001 nofail_asprintf(&cfgfile
, "%s/%s", filename
, fe
->name
);
1002 if (!parse_config_file(cfgfile
, name
,
1003 dump_only
, removing
,
1005 aliases
, blacklist
))
1006 warn("Failed to open config file "
1007 "%s: %s\n", fe
->name
, strerror(errno
));
1009 list_del(&fe
->node
);
1015 if (parse_config_file(filename
, name
, dump_only
, removing
,
1016 options
, commands
, aliases
, blacklist
))
1022 /* Read binary index file containing aliases only */
1023 static void parse_toplevel_config(const char *filename
,
1027 struct module_options
**options
,
1028 struct module_command
**commands
,
1029 struct module_alias
**aliases
,
1030 struct module_blacklist
**blacklist
)
1033 if (!parse_config_scan(filename
, name
, dump_only
, removing
,
1034 options
, commands
, aliases
, blacklist
))
1035 fatal("Failed to open config file %s: %s\n",
1036 filename
, strerror(errno
));
1040 /* deprecated config file */
1041 if (parse_config_file("/etc/modprobe.conf", name
, dump_only
, removing
,
1042 options
, commands
, aliases
, blacklist
) > 0)
1043 warn("Deprecated config file /etc/modprobe.conf, "
1044 "all config files belong into /etc/modprobe.d/.\n");
1046 /* default config */
1047 parse_config_scan("/etc/modprobe.d", name
, dump_only
, removing
,
1048 options
, commands
, aliases
, blacklist
);
1051 /* Read possible module arguments from the kernel command line. */
1052 static int parse_kcmdline(int dump_only
, struct module_options
**options
)
1055 unsigned int linenum
= 0;
1058 kcmdline
= fopen("/proc/cmdline", "r");
1062 while ((line
= getline_wrapped(kcmdline
, &linenum
)) != NULL
) {
1066 while ((arg
= strsep_skipspace(&ptr
, "\t ")) != NULL
) {
1067 char *sep
, *modname
, *opt
;
1069 sep
= strchr(arg
, '.');
1071 if (!strchr(sep
, '='))
1078 printf("options %s %s\n", modname
, opt
);
1080 *options
= add_options(underscores(modname
),
1091 static void add_to_env_var(const char *option
)
1095 if ((oldenv
= getenv("MODPROBE_OPTIONS")) != NULL
) {
1097 nofail_asprintf(&newenv
, "%s %s", oldenv
, option
);
1098 setenv("MODPROBE_OPTIONS", newenv
, 1);
1100 setenv("MODPROBE_OPTIONS", option
, 1);
1103 /* Prepend options from environment. */
1104 static char **merge_args(char *args
, char *argv
[], int *argc
)
1106 char *arg
, *argstring
;
1107 char **newargs
= NULL
;
1108 unsigned int i
, num_env
= 0;
1113 argstring
= NOFAIL(strdup(args
));
1114 for (arg
= strtok(argstring
, " "); arg
; arg
= strtok(NULL
, " ")) {
1116 newargs
= NOFAIL(realloc(newargs
,
1118 * (num_env
+ *argc
+ 1)));
1119 newargs
[num_env
] = arg
;
1125 /* Append commandline args */
1126 newargs
[0] = argv
[0];
1127 for (i
= 1; i
<= *argc
; i
++)
1128 newargs
[num_env
+i
] = argv
[i
];
1134 static char *gather_options(char *argv
[])
1136 char *optstring
= NOFAIL(strdup(""));
1138 /* Rest is module options */
1140 /* Quote value if it contains spaces. */
1141 unsigned int eq
= strcspn(*argv
, "=");
1143 if (strchr(*argv
+eq
, ' ') && !strchr(*argv
, '"')) {
1144 char quoted
[strlen(*argv
) + 3];
1146 sprintf(quoted
, "%s=\"%s\"", *argv
, *argv
+eq
+1);
1147 optstring
= append_option(optstring
, quoted
);
1149 optstring
= append_option(optstring
, *argv
);
1155 /* Do an install/remove command: replace $CMDLINE_OPTS if it's specified. */
1156 static void do_command(const char *modname
,
1157 const char *command
,
1161 const char *cmdline_opts
)
1164 char *p
, *replaced_cmd
= NOFAIL(strdup(command
));
1166 while ((p
= strstr(replaced_cmd
, "$CMDLINE_OPTS")) != NULL
) {
1168 nofail_asprintf(&new, "%.*s%s%s",
1169 (int)(p
- replaced_cmd
), replaced_cmd
, cmdline_opts
,
1170 p
+ strlen("$CMDLINE_OPTS"));
1175 info("%s %s\n", type
, replaced_cmd
);
1179 setenv("MODPROBE_MODULE", modname
, 1);
1180 ret
= system(replaced_cmd
);
1181 if (ret
== -1 || WEXITSTATUS(ret
))
1182 error("Error running %s command for %s\n", type
, modname
);
1186 /* Actually do the insert. */
1187 static int insmod(struct list_head
*list
,
1188 const char *optstring
,
1189 const char *newname
,
1190 const struct module_options
*options
,
1191 const struct module_command
*commands
,
1192 const char *cmdline_opts
,
1194 modprobe_flags_t flags
)
1197 struct elf_file
*module
;
1198 const char *command
;
1199 struct module
*mod
= list_entry(list
->next
, struct module
, list
);
1204 /* Take us off the list. */
1205 list_del(&mod
->list
);
1207 /* Do things we (or parent) depend on first. */
1208 if (!list_empty(list
)) {
1209 modprobe_flags_t f
= flags
;
1210 f
&= ~mit_first_time
;
1211 f
&= ~mit_ignore_commands
;
1212 if ((rc
= insmod(list
, "", NULL
,
1213 options
, commands
, "", warn
, f
)) != 0) {
1214 error("Error inserting %s (%s): %s\n",
1215 mod
->modname
, mod
->filename
,
1216 insert_moderror(errno
));
1221 fd
= open_file(mod
->filename
);
1223 error("Could not open '%s': %s\n",
1224 mod
->filename
, strerror(errno
));
1228 /* Don't do ANYTHING if already in kernel. */
1229 already_loaded
= module_in_kernel(newname
?: mod
->modname
, NULL
);
1231 if (!(flags
& mit_ignore_loaded
) && already_loaded
== 1) {
1232 if (flags
& mit_first_time
)
1233 error("Module %s already in kernel.\n",
1234 newname
?: mod
->modname
);
1238 command
= find_command(mod
->modname
, commands
);
1239 if (command
&& !(flags
& mit_ignore_commands
)) {
1240 if (already_loaded
== -1) {
1241 warn("/sys/module/ not present or too old,"
1242 " and /proc/modules does not exist.\n");
1243 warn("Ignoring install commands for %s"
1244 " in case it is already loaded.\n",
1245 newname
?: mod
->modname
);
1248 do_command(mod
->modname
, command
, flags
& mit_dry_run
,
1249 error
, "install", cmdline_opts
);
1254 module
= grab_elf_file_fd(mod
->filename
, fd
);
1256 error("Could not read '%s': %s\n", mod
->filename
,
1257 (errno
== ENOEXEC
) ? "Invalid module format" :
1262 rename_module(module
, mod
->modname
, newname
);
1263 if (flags
& mit_strip_modversion
)
1264 module
->ops
->strip_section(module
, "__versions");
1265 if (flags
& mit_strip_vermagic
)
1266 clear_magic(module
);
1268 /* Config file might have given more options */
1269 opts
= add_extra_options(mod
->modname
, optstring
, options
);
1271 info("insmod %s %s\n", mod
->filename
, opts
);
1273 if (flags
& mit_dry_run
)
1276 ret
= init_module(module
->data
, module
->len
, opts
);
1278 if (errno
== EEXIST
) {
1279 if (flags
& mit_first_time
)
1280 error("Module %s already in kernel.\n",
1281 newname
?: mod
->modname
);
1284 /* don't warn noisely if we're loading multiple aliases. */
1285 /* one of the aliases may try to use hardware we don't have. */
1286 if ((error
!= warn
) || (verbose
))
1287 error("Error inserting %s (%s): %s\n",
1288 mod
->modname
, mod
->filename
,
1289 insert_moderror(errno
));
1293 release_elf_file(module
);
1301 /* Do recursive removal. */
1302 static void rmmod(struct list_head
*list
,
1304 struct module_command
*commands
,
1305 const char *cmdline_opts
,
1307 modprobe_flags_t flags
)
1309 const char *command
;
1310 unsigned int usecount
= 0;
1311 struct module
*mod
= list_entry(list
->next
, struct module
, list
);
1314 /* Take first one off the list. */
1315 list_del(&mod
->list
);
1318 name
= mod
->modname
;
1320 /* Don't do ANYTHING if not loaded. */
1321 exists
= module_in_kernel(name
, &usecount
);
1323 goto nonexistent_module
;
1325 /* Even if renamed, find commands to orig. name. */
1326 command
= find_command(mod
->modname
, commands
);
1327 if (command
&& !(flags
& mit_ignore_commands
)) {
1329 warn("/sys/module/ not present or too old,"
1330 " and /proc/modules does not exist.\n");
1331 warn("Ignoring remove commands for %s"
1332 " in case it is not loaded.\n",
1335 do_command(mod
->modname
, command
, flags
& mit_dry_run
,
1336 error
, "remove", cmdline_opts
);
1341 if (usecount
!= 0) {
1342 if (!(flags
& mit_quiet_inuse
))
1343 error("Module %s is in use.\n", name
);
1347 info("rmmod %s\n", mod
->filename
);
1349 if (flags
& mit_dry_run
)
1352 if (delete_module(name
, O_EXCL
) != 0) {
1353 if (errno
== ENOENT
)
1354 goto nonexistent_module
;
1355 error("Error removing %s (%s): %s\n",
1356 name
, mod
->filename
,
1357 remove_moderror(errno
));
1361 /* Now do things we depend. */
1362 if (!list_empty(list
)) {
1363 flags
&= ~mit_first_time
;
1364 flags
&= ~mit_ignore_commands
;
1365 flags
|= mit_quiet_inuse
;
1367 rmmod(list
, NULL
, commands
, "", warn
, flags
);
1372 if (flags
& mit_first_time
)
1373 fatal("Module %s is not in kernel.\n", mod
->modname
);
1377 static int handle_module(const char *modname
,
1378 struct list_head
*todo_list
,
1379 const char *newname
,
1380 const char *options
,
1381 struct module_options
*modoptions
,
1382 struct module_command
*commands
,
1383 const char *cmdline_opts
,
1385 modprobe_flags_t flags
)
1387 if (list_empty(todo_list
)) {
1388 const char *command
;
1390 /* The dependencies have to be real modules, but
1391 handle case where the first is completely bogus. */
1392 command
= find_command(modname
, commands
);
1393 if (command
&& !(flags
& mit_ignore_commands
)) {
1394 do_command(modname
, command
, flags
& mit_dry_run
, error
,
1395 (flags
& mit_remove
) ? "remove":"install", cmdline_opts
);
1400 error("Module %s not found.\n", modname
);
1404 if (flags
& mit_remove
)
1405 rmmod(todo_list
, newname
, commands
, cmdline_opts
, error
, flags
);
1407 insmod(todo_list
, options
, newname
,
1408 modoptions
, commands
, cmdline_opts
, error
, flags
);
1413 int handle_builtin_module(const char *modname
,
1415 modprobe_flags_t flags
)
1417 if (flags
& mit_remove
) {
1418 error("Module %s is builtin\n", modname
);
1420 } else if (flags
& mit_first_time
) {
1421 error("Module %s already in kernel (builtin).\n", modname
);
1423 } else if (flags
& mit_ignore_loaded
) {
1424 /* --show-depends given */
1425 info("builtin %s\n", modname
);
1430 int do_modprobe(char *modname
,
1433 const char *configname
,
1434 const char *dirname
,
1435 const char *aliasfilename
,
1436 const char *symfilename
,
1438 modprobe_flags_t flags
)
1440 struct module_command
*commands
= NULL
;
1441 struct module_options
*modoptions
= NULL
;
1442 struct module_alias
*aliases
= NULL
;
1443 struct module_blacklist
*blacklist
= NULL
;
1447 /* Convert name we are looking for */
1448 underscores(modname
);
1450 /* Returns the resolved alias, options */
1451 parse_toplevel_config(configname
, modname
, 0,
1452 flags
& mit_remove
, &modoptions
, &commands
, &aliases
, &blacklist
);
1454 /* Read module options from kernel command line */
1455 parse_kcmdline(0, &modoptions
);
1457 /* No luck? Try symbol names, if starts with symbol:. */
1458 if (!aliases
&& strstarts(modname
, "symbol:")) {
1459 parse_config_file(symfilename
, modname
, 0,
1460 flags
& mit_remove
, &modoptions
, &commands
,
1461 &aliases
, &blacklist
);
1464 if(!strchr(modname
, ':'))
1465 read_depends(dirname
, modname
, &list
);
1467 /* We only use canned aliases as last resort. */
1468 if (list_empty(&list
)
1469 && !find_command(modname
, commands
))
1471 read_aliases_file(aliasfilename
,
1472 modname
, 0, flags
& mit_remove
,
1473 &modoptions
, &commands
,
1474 &aliases
, &blacklist
);
1475 /* builtin module? */
1476 if (!aliases
&& module_builtin(dirname
, modname
) > 0) {
1477 return handle_builtin_module(modname
, error
,
1483 aliases
= apply_blacklist(aliases
, blacklist
);
1484 if(flags
& mit_resolve_alias
) {
1485 for(; aliases
; aliases
=aliases
->next
)
1486 printf("%s\n", aliases
->module
);
1490 errfn_t err
= error
;
1492 /* More than one alias? Don't bail out on failure. */
1496 /* Add the options for this alias. */
1498 opts
= add_extra_options(modname
,
1499 cmdline_opts
, modoptions
);
1501 read_depends(dirname
, aliases
->module
, &list
);
1502 failed
|= handle_module(aliases
->module
,
1503 &list
, newname
, opts
, modoptions
,
1504 commands
, cmdline_opts
, err
, flags
);
1506 aliases
= aliases
->next
;
1508 INIT_LIST_HEAD(&list
);
1511 if (flags
& mit_use_blacklist
1512 && find_blacklist(modname
, blacklist
))
1515 failed
|= handle_module(modname
, &list
, newname
, cmdline_opts
,
1516 modoptions
, commands
, cmdline_opts
, error
, flags
);
1521 static struct option options
[] = { { "version", 0, NULL
, 'V' },
1522 { "verbose", 0, NULL
, 'v' },
1523 { "quiet", 0, NULL
, 'q' },
1524 { "syslog", 0, NULL
, 's' },
1525 { "show", 0, NULL
, 'n' },
1526 { "dry-run", 0, NULL
, 'n' },
1527 { "show-depends", 0, NULL
, 'D' },
1528 { "resolve-alias", 0, NULL
, 'R' },
1529 { "dirname", 1, NULL
, 'd' },
1530 { "set-version", 1, NULL
, 'S' },
1531 { "config", 1, NULL
, 'C' },
1532 { "name", 1, NULL
, 'o' },
1533 { "remove", 0, NULL
, 'r' },
1534 { "showconfig", 0, NULL
, 'c' },
1535 { "list", 0, NULL
, 'l' },
1536 { "type", 1, NULL
, 't' },
1537 { "all", 0, NULL
, 'a' },
1538 { "ignore-install", 0, NULL
, 'i' },
1539 { "ignore-remove", 0, NULL
, 'i' },
1540 { "use-blacklist", 0, NULL
, 'b' },
1541 { "force", 0, NULL
, 'f' },
1542 { "force-vermagic", 0, NULL
, 1 },
1543 { "force-modversion", 0, NULL
, 2 },
1544 { "first-time", 0, NULL
, 3 },
1545 { "dump-modversions", 0, NULL
, 4 },
1546 { NULL
, 0, NULL
, 0 } };
1548 int main(int argc
, char *argv
[])
1551 struct stat statbuf
;
1553 int dump_config
= 0;
1556 int dump_modver
= 0;
1557 unsigned int i
, num_modules
;
1559 const char *configname
= NULL
;
1561 char *cmdline_opts
= NULL
;
1562 char *newname
= NULL
;
1563 char *dirname
, *aliasfilename
, *symfilename
;
1564 errfn_t error
= fatal
;
1566 modprobe_flags_t flags
= 0;
1568 /* Prepend options from environment. */
1569 argv
= merge_args(getenv("MODPROBE_OPTIONS"), argv
, &argc
);
1572 while ((opt
= getopt_long(argc
, argv
, "Vvqsnd:C:o:rclt:aibf", options
, NULL
)) != -1){
1575 puts(PACKAGE
" version " VERSION
);
1578 add_to_env_var("-v");
1583 add_to_env_var("-q");
1586 add_to_env_var("-s");
1590 flags
|= mit_dry_run
;
1596 strncpy(buf
.release
, optarg
, sizeof(buf
.release
));
1597 buf
.release
[sizeof(buf
.release
)-1] = '\0';
1600 configname
= optarg
;
1601 add_to_env_var("-C");
1602 add_to_env_var(configname
);
1605 flags
|= mit_dry_run
;
1606 flags
|= mit_ignore_loaded
;
1610 flags
|= mit_resolve_alias
;
1616 flags
|= mit_remove
;
1632 flags
|= mit_ignore_commands
;
1635 flags
|= mit_use_blacklist
;
1638 flags
|= mit_strip_vermagic
;
1639 flags
|= mit_strip_modversion
;
1642 flags
|= mit_strip_vermagic
;
1645 flags
|= mit_strip_modversion
;
1648 flags
|= mit_first_time
;
1654 print_usage(argv
[0]);
1658 /* If stderr not open, go to syslog */
1659 if (logging
|| fstat(STDERR_FILENO
, &statbuf
) != 0) {
1660 openlog("modprobe", LOG_CONS
, LOG_DAEMON
);
1664 if (argc
< optind
+ 1 && !dump_config
&& !list_only
)
1665 print_usage(argv
[0]);
1667 nofail_asprintf(&dirname
, "%s%s/%s", basedir
, MODULE_DIR
, buf
.release
);
1668 nofail_asprintf(&aliasfilename
, "%s/modules.alias", dirname
);
1669 nofail_asprintf(&symfilename
, "%s/modules.symbols", dirname
);
1671 /* Old-style -t xxx wildcard? Only with -l. */
1673 if (optind
+1 < argc
)
1674 fatal("Can't have multiple wildcards\n");
1675 /* fprintf(stderr, "man find\n"); return 1; */
1676 return do_wildcard(dirname
, type
, argv
[optind
]?:"*");
1679 fatal("-t only supported with -l");
1682 struct module_command
*commands
= NULL
;
1683 struct module_options
*modoptions
= NULL
;
1684 struct module_alias
*aliases
= NULL
;
1685 struct module_blacklist
*blacklist
= NULL
;
1687 parse_toplevel_config(configname
, "", 1, 0, &modoptions
,
1688 &commands
, &aliases
, &blacklist
);
1689 /* Read module options from kernel command line */
1690 parse_kcmdline(1, &modoptions
);
1691 parse_config_file(aliasfilename
, "", 1, 0, &modoptions
,
1692 &commands
, &aliases
, &blacklist
);
1693 parse_config_file(symfilename
, "", 1, 0, &modoptions
,
1694 &commands
, &aliases
, &blacklist
);
1698 if ((flags
& mit_remove
) || all
) {
1699 num_modules
= argc
- optind
;
1700 cmdline_opts
= NOFAIL(strdup(""));
1703 cmdline_opts
= gather_options(argv
+optind
+1);
1706 /* num_modules is always 1 except for -r or -a. */
1707 for (i
= 0; i
< num_modules
; i
++) {
1708 char *modname
= argv
[optind
+ i
];
1711 dump_modversions(modname
, error
);
1713 failed
|= do_modprobe(modname
, newname
, cmdline_opts
,
1714 configname
, dirname
, aliasfilename
, symfilename
,
1722 free(aliasfilename
);