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>
43 #include "zlibsupport.h"
47 #include "config_filter.h"
51 int use_binary_indexes
= 1; /* default to enabled. */
53 extern long init_module(void *, unsigned long, const char *);
54 extern long delete_module(const char *, unsigned int);
57 struct list_head list
;
63 #define MODULE_DIR "/lib/modules"
66 typedef void (*errfn_t
)(const char *fmt
, ...);
68 static void print_usage(const char *progname
)
71 "Usage: %s [-v] [-V] [-C config-file] [-d <dirname> ] [-n] [-i] [-q] [-b] [-o <modname>] [ --dump-modversions ] <modname> [parameters...]\n"
72 "%s -r [-n] [-i] [-v] <modulename> ...\n"
73 "%s -l -t <dirname> [ -a <modulename> ...]\n",
74 progname
, progname
, progname
);
78 static struct module
*find_module(const char *filename
, struct list_head
*list
)
82 list_for_each_entry(i
, list
, list
) {
83 if (streq(i
->filename
, filename
))
89 /* We used to lock with a write flock but that allows regular users to block
90 * module load by having a read lock on the module file (no way to bust the
91 * existing locks without killing the offending process). Instead, we now
92 * do the system call/init_module and allow the kernel to fail us instead.
94 static int open_file(const char *filename
)
96 int fd
= open(filename
, O_RDONLY
, 0);
101 static void close_file(int fd
)
103 /* Valgrind is picky... */
107 static void add_module(char *filename
, int namelen
, struct list_head
*list
)
111 /* If it's a duplicate: move it to the end, so it gets
112 inserted where it is *first* required. */
113 mod
= find_module(filename
, list
);
115 list_del(&mod
->list
);
117 /* No match. Create a new module. */
118 mod
= NOFAIL(malloc(sizeof(struct module
) + namelen
+ 1));
119 memcpy(mod
->filename
, filename
, namelen
);
120 mod
->filename
[namelen
] = '\0';
121 mod
->modname
= NOFAIL(malloc(namelen
+ 1));
122 filename2modname(mod
->modname
, mod
->filename
);
125 list_add_tail(&mod
->list
, list
);
128 /* Compare len chars of a to b, with _ and - equivalent. */
129 static int modname_equal(const char *a
, const char *b
, unsigned int len
)
133 if (strlen(b
) != len
)
136 for (i
= 0; i
< len
; i
++) {
137 if ((a
[i
] == '_' || a
[i
] == '-')
138 && (b
[i
] == '_' || b
[i
] == '-'))
146 /* Fills in list of modules if this is the line we want. */
147 static int add_modules_dep_line(char *line
,
149 struct list_head
*list
,
154 char *modname
, *fullpath
;
156 /* Ignore lines without : or which start with a # */
157 ptr
= strchr(line
, ':');
158 if (ptr
== NULL
|| line
[strspn(line
, "\t ")] == '#')
161 /* Is this the module we are looking for? */
163 if (strrchr(line
, '/'))
164 modname
= strrchr(line
, '/') + 1;
168 len
= strlen(modname
);
169 if (strchr(modname
, '.'))
170 len
= strchr(modname
, '.') - modname
;
171 if (!modname_equal(modname
, name
, len
))
174 /* Create the list. */
175 if ('/' == line
[0]) { /* old style deps - absolute path specified */
176 add_module(line
, ptr
- line
, list
);
178 nofail_asprintf(&fullpath
, "%s/%s", dirname
, line
);
179 add_module(fullpath
, strlen(dirname
)+1+(ptr
- line
), list
);
186 ptr
+= strspn(ptr
, " \t");
190 ptr
+= strcspn(ptr
, " \t");
191 if ('/' == dep_start
[0]) { /* old style deps */
192 add_module(dep_start
, ptr
- dep_start
, list
);
194 nofail_asprintf(&fullpath
, "%s/%s", dirname
, dep_start
);
196 strlen(dirname
)+1+(ptr
- dep_start
), list
);
203 static int read_depends_file(const char *dirname
,
204 const char *start_name
,
205 struct list_head
*list
)
207 char *modules_dep_name
;
209 struct index_file
*modules_dep
;
211 nofail_asprintf(&modules_dep_name
, "%s/%s", dirname
, "modules.dep.bin");
212 modules_dep
= index_file_open(modules_dep_name
);
214 free(modules_dep_name
);
218 line
= index_search(modules_dep
, start_name
);
220 /* Value is standard dependency line format */
221 if (!add_modules_dep_line(line
, start_name
, list
, dirname
))
222 fatal("Module index is inconsistent\n");
226 index_file_close(modules_dep
);
227 free(modules_dep_name
);
232 static void read_depends(const char *dirname
,
233 const char *start_name
,
234 struct list_head
*list
)
236 char *modules_dep_name
;
241 if (use_binary_indexes
)
242 if (read_depends_file(dirname
, start_name
, list
))
245 nofail_asprintf(&modules_dep_name
, "%s/%s", dirname
, "modules.dep");
246 modules_dep
= fopen(modules_dep_name
, "r");
248 fatal("Could not load %s: %s\n",
249 modules_dep_name
, strerror(errno
));
251 /* Stop at first line, as we can have duplicates (eg. symlinks
253 while (!done
&& (line
= getline_wrapped(modules_dep
, NULL
)) != NULL
) {
254 done
= add_modules_dep_line(line
, start_name
, list
, dirname
);
258 free(modules_dep_name
);
261 /* We use error numbers in a loose translation... */
262 static const char *insert_moderror(int err
)
266 return "Invalid module format";
268 return "Unknown symbol in module, or unknown parameter (see dmesg)";
270 return "Kernel does not have module support";
272 return strerror(err
);
276 static const char *remove_moderror(int err
)
280 return "No such module";
282 return "Kernel does not have module unloading support";
284 return strerror(err
);
288 static void replace_modname(struct module
*module
,
289 void *mem
, unsigned long len
,
290 const char *oldname
, const char *newname
)
294 /* 64 - sizeof(unsigned long) - 1 */
295 if (strlen(newname
) > 55)
296 fatal("New name %s is too long\n", newname
);
298 /* Find where it is in the module structure. Don't assume layout! */
299 for (p
= mem
; p
< (char *)mem
+ len
- strlen(oldname
); p
++) {
300 if (memcmp(p
, oldname
, strlen(oldname
)) == 0) {
306 warn("Could not find old name in %s to replace!\n", module
->filename
);
309 static void *get_section32(void *file
,
312 unsigned long *secsize
)
314 Elf32_Ehdr
*hdr
= file
;
315 Elf32_Shdr
*sechdrs
= file
+ hdr
->e_shoff
;
316 const char *secnames
;
320 if (size
< sizeof(*hdr
))
322 if (size
< hdr
->e_shoff
+ hdr
->e_shnum
* sizeof(sechdrs
[0]))
324 if (size
< sechdrs
[hdr
->e_shstrndx
].sh_offset
)
327 secnames
= file
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
328 for (i
= 1; i
< hdr
->e_shnum
; i
++)
329 if (streq(secnames
+ sechdrs
[i
].sh_name
, name
)) {
330 *secsize
= sechdrs
[i
].sh_size
;
331 return file
+ sechdrs
[i
].sh_offset
;
336 static void *get_section64(void *file
,
339 unsigned long *secsize
)
341 Elf64_Ehdr
*hdr
= file
;
342 Elf64_Shdr
*sechdrs
= file
+ hdr
->e_shoff
;
343 const char *secnames
;
347 if (size
< sizeof(*hdr
))
349 if (size
< hdr
->e_shoff
+ hdr
->e_shnum
* sizeof(sechdrs
[0]))
351 if (size
< sechdrs
[hdr
->e_shstrndx
].sh_offset
)
354 secnames
= file
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
355 for (i
= 1; i
< hdr
->e_shnum
; i
++)
356 if (streq(secnames
+ sechdrs
[i
].sh_name
, name
)) {
357 *secsize
= sechdrs
[i
].sh_size
;
358 return file
+ sechdrs
[i
].sh_offset
;
363 static int elf_ident(void *mod
, unsigned long size
)
365 /* "\177ELF" <byte> where byte = 001 for 32-bit, 002 for 64 */
368 if (size
< EI_CLASS
|| memcmp(mod
, ELFMAG
, SELFMAG
) != 0)
370 return ident
[EI_CLASS
];
373 static void *get_section(void *file
,
376 unsigned long *secsize
)
378 switch (elf_ident(file
, size
)) {
380 return get_section32(file
, size
, name
, secsize
);
382 return get_section64(file
, size
, name
, secsize
);
388 static void rename_module(struct module
*module
,
394 unsigned long modstruct_len
;
397 modstruct
= get_section(mod
, len
, ".gnu.linkonce.this_module",
401 modstruct
= get_section(mod
, len
, "__module", &modstruct_len
);
403 warn("Could not find module name to change in %s\n",
406 replace_modname(module
, modstruct
, modstruct_len
,
407 module
->modname
, newname
);
410 /* Kernel told to ignore these sections if SHF_ALLOC not set. */
411 static void invalidate_section32(void *mod
, const char *secname
)
413 Elf32_Ehdr
*hdr
= mod
;
414 Elf32_Shdr
*sechdrs
= mod
+ hdr
->e_shoff
;
415 const char *secnames
= mod
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
418 for (i
= 1; i
< hdr
->e_shnum
; i
++)
419 if (streq(secnames
+sechdrs
[i
].sh_name
, secname
))
420 sechdrs
[i
].sh_flags
&= ~SHF_ALLOC
;
423 static void invalidate_section64(void *mod
, const char *secname
)
425 Elf64_Ehdr
*hdr
= mod
;
426 Elf64_Shdr
*sechdrs
= mod
+ hdr
->e_shoff
;
427 const char *secnames
= mod
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
430 for (i
= 1; i
< hdr
->e_shnum
; i
++)
431 if (streq(secnames
+sechdrs
[i
].sh_name
, secname
))
432 sechdrs
[i
].sh_flags
&= ~(unsigned long long)SHF_ALLOC
;
435 static void strip_section(struct module
*module
,
440 switch (elf_ident(mod
, len
)) {
442 invalidate_section32(mod
, secname
);
445 invalidate_section64(mod
, secname
);
448 warn("Unknown module format in %s: not forcing version\n",
453 static void clear_magic(struct module
*module
, void *mod
, unsigned long len
)
456 unsigned long modlen
;
458 /* Old-style: __vermagic section */
459 strip_section(module
, mod
, len
, "__vermagic");
461 /* New-style: in .modinfo section */
462 for (p
= get_section(mod
, len
, ".modinfo", &modlen
);
464 p
= next_string(p
, &modlen
)) {
465 if (strstarts(p
, "vermagic=")) {
466 memset((char *)p
, 0, strlen(p
));
472 struct module_options
474 struct module_options
*next
;
479 struct module_command
481 struct module_command
*next
;
488 struct module_alias
*next
;
492 struct module_blacklist
494 struct module_blacklist
*next
;
498 /* Link in a new option line from the config file. */
499 static struct module_options
*
500 add_options(const char *modname
,
502 struct module_options
*options
)
504 struct module_options
*new;
507 new = NOFAIL(malloc(sizeof(*new)));
508 new->modulename
= NOFAIL(strdup(modname
));
509 new->options
= NOFAIL(strdup(option
));
510 /* We can handle tabs, kernel can't. */
511 for (tab
= strchr(new->options
, '\t'); tab
; tab
= strchr(tab
, '\t'))
517 /* Link in a new install line from the config file. */
518 static struct module_command
*
519 add_command(const char *modname
,
521 struct module_command
*commands
)
523 struct module_command
*new;
525 new = NOFAIL(malloc(sizeof(*new)));
526 new->modulename
= NOFAIL(strdup(modname
));
527 new->command
= NOFAIL(strdup(command
));
528 new->next
= commands
;
532 /* Link in a new alias line from the config file. */
533 static struct module_alias
*
534 add_alias(const char *modname
, struct module_alias
*aliases
)
536 struct module_alias
*new;
538 new = NOFAIL(malloc(sizeof(*new)));
539 new->module
= NOFAIL(strdup(modname
));
544 /* Link in a new blacklist line from the config file. */
545 static struct module_blacklist
*
546 add_blacklist(const char *modname
, struct module_blacklist
*blacklist
)
548 struct module_blacklist
*new;
550 new = NOFAIL(malloc(sizeof(*new)));
551 new->modulename
= NOFAIL(strdup(modname
));
552 new->next
= blacklist
;
556 /* Find blacklist commands if any. */
558 find_blacklist(const char *modname
, const struct module_blacklist
*blacklist
)
561 if (streq(blacklist
->modulename
, modname
))
563 blacklist
= blacklist
->next
;
568 /* return a new alias list, with backlisted elems filtered out */
569 static struct module_alias
*
570 apply_blacklist(const struct module_alias
*aliases
,
571 const struct module_blacklist
*blacklist
)
573 struct module_alias
*result
= NULL
;
575 char *modname
= aliases
->module
;
576 if (!find_blacklist(modname
, blacklist
))
577 result
= add_alias(modname
, result
);
578 aliases
= aliases
->next
;
583 /* Find install commands if any. */
584 static const char *find_command(const char *modname
,
585 const struct module_command
*commands
)
588 if (fnmatch(commands
->modulename
, modname
, 0) == 0)
589 return commands
->command
;
590 commands
= commands
->next
;
595 static char *append_option(char *options
, const char *newoption
)
597 options
= NOFAIL(realloc(options
, strlen(options
) + 1
598 + strlen(newoption
) + 1));
599 if (strlen(options
)) strcat(options
, " ");
600 strcat(options
, newoption
);
604 static char *prepend_option(char *options
, const char *newoption
)
607 l1
= strlen(options
);
608 l2
= strlen(newoption
);
609 /* the resulting string will look like
610 * newoption + ' ' + options + '\0' */
612 options
= NOFAIL(realloc(options
, l2
+ 1 + l1
+ 1));
613 memmove(options
+ l2
+ 1, options
, l1
+ 1);
615 memcpy(options
, newoption
, l2
);
617 options
= NOFAIL(realloc(options
, l2
+ 1));
618 memcpy(options
, newoption
, l2
);
625 static char *add_extra_options(const char *modname
,
627 const struct module_options
*options
)
630 if (streq(options
->modulename
, modname
))
631 optstring
= prepend_option(optstring
, options
->options
);
632 options
= options
->next
;
637 /* Read sysfs attribute into a buffer.
638 * returns: 1 = ok, 0 = attribute missing,
639 * -1 = file error (or empty file, but we don't care).
641 static int read_attribute(const char *filename
, char *buf
, size_t buflen
)
646 file
= fopen(filename
, "r");
648 return (errno
== ENOENT
) ? 0 : -1;
649 s
= fgets(buf
, buflen
, file
);
652 return (s
== NULL
) ? -1 : 1;
655 /* Is module in /sys/module? If so, fill in usecount if not NULL.
656 0 means no, 1 means yes, -1 means unknown.
658 static int module_in_kernel(const char *modname
, unsigned int *usecount
)
664 const int ATTR_LEN
= 16;
667 /* Check sysfs is mounted */
668 if (stat("/sys/module", &finfo
) < 0)
672 nofail_asprintf(&name
, "/sys/module/%s", modname
);
673 ret
= stat(name
, &finfo
);
676 return (errno
== ENOENT
) ? 0 : -1; /* Not found or unknown. */
678 /* Wait for the existing module to either go live or disappear. */
679 nofail_asprintf(&name
, "/sys/module/%s/initstate", modname
);
681 ret
= read_attribute(name
, attr
, ATTR_LEN
);
682 if (ret
!= 1 || streq(attr
, "live\n"))
692 /* Get reference count, if it exists. */
693 if (usecount
!= NULL
) {
694 nofail_asprintf(&name
, "/sys/module/%s/refcnt", modname
);
695 ret
= read_attribute(name
, attr
, ATTR_LEN
);
698 *usecount
= atoi(attr
);
704 /* Do an install/remove command: replace $CMDLINE_OPTS if it's specified. */
705 static void do_command(const char *modname
,
710 const char *cmdline_opts
)
713 char *p
, *replaced_cmd
= NOFAIL(strdup(command
));
715 while ((p
= strstr(replaced_cmd
, "$CMDLINE_OPTS")) != NULL
) {
717 nofail_asprintf(&new, "%.*s%s%s",
718 (int)(p
- replaced_cmd
), replaced_cmd
, cmdline_opts
,
719 p
+ strlen("$CMDLINE_OPTS"));
724 info("%s %s\n", type
, replaced_cmd
);
728 setenv("MODPROBE_MODULE", modname
, 1);
729 ret
= system(replaced_cmd
);
730 if (ret
== -1 || WEXITSTATUS(ret
))
731 error("Error running %s command for %s\n", type
, modname
);
735 /* Actually do the insert. Frees second arg. */
736 static int insmod(struct list_head
*list
,
742 const struct module_options
*options
,
743 const struct module_command
*commands
,
747 int strip_modversion
,
748 const char *cmdline_opts
)
754 struct module
*mod
= list_entry(list
->next
, struct module
, list
);
757 /* Take us off the list. */
758 list_del(&mod
->list
);
760 /* Do things we (or parent) depend on first. */
761 if (!list_empty(list
)) {
762 if ((rc
= insmod(list
, NOFAIL(strdup("")), NULL
, 0, warn
,
763 dry_run
, options
, commands
, 0, ignore_proc
,
764 strip_vermagic
, strip_modversion
, "")) != 0) {
765 error("Error inserting %s (%s): %s\n",
766 mod
->modname
, mod
->filename
,
767 insert_moderror(errno
));
772 fd
= open_file(mod
->filename
);
774 error("Could not open '%s': %s\n",
775 mod
->filename
, strerror(errno
));
779 /* Don't do ANYTHING if already in kernel. */
781 && module_in_kernel(newname
?: mod
->modname
, NULL
) == 1) {
783 error("Module %s already in kernel.\n",
784 newname
?: mod
->modname
);
788 command
= find_command(mod
->modname
, commands
);
789 if (command
&& !ignore_commands
) {
791 do_command(mod
->modname
, command
, dry_run
, error
,
792 "install", cmdline_opts
);
796 map
= grab_fd(fd
, &len
);
798 error("Could not read '%s': %s\n",
799 mod
->filename
, strerror(errno
));
805 rename_module(mod
, map
, len
, newname
);
807 if (strip_modversion
)
808 strip_section(mod
, map
, len
, "__versions");
810 clear_magic(mod
, map
, len
);
812 /* Config file might have given more options */
813 optstring
= add_extra_options(mod
->modname
, optstring
, options
);
815 info("insmod %s %s\n", mod
->filename
, optstring
);
820 ret
= init_module(map
, len
, optstring
);
822 if (errno
== EEXIST
) {
824 error("Module %s already in kernel.\n",
825 newname
?: mod
->modname
);
828 /* don't warn noisely if we're loading multiple aliases. */
829 /* one of the aliases may try to use hardware we don't have. */
830 if ((error
!= warn
) || (verbose
))
831 error("Error inserting %s (%s): %s\n",
832 mod
->modname
, mod
->filename
,
833 insert_moderror(errno
));
837 release_file(map
, len
);
845 /* Do recursive removal. */
846 static void rmmod(struct list_head
*list
,
851 struct module_command
*commands
,
854 const char *cmdline_opts
,
858 unsigned int usecount
= 0;
859 struct module
*mod
= list_entry(list
->next
, struct module
, list
);
861 /* Take first one off the list. */
862 list_del(&mod
->list
);
867 /* Even if renamed, find commands to orig. name. */
868 command
= find_command(mod
->modname
, commands
);
869 if (command
&& !ignore_commands
) {
870 do_command(mod
->modname
, command
, dry_run
, error
,
871 "remove", cmdline_opts
);
875 if (module_in_kernel(name
, &usecount
) == 0)
876 goto nonexistent_module
;
880 error("Module %s is in use.\n", name
);
884 info("rmmod %s\n", mod
->filename
);
889 if (delete_module(name
, O_EXCL
) != 0) {
891 goto nonexistent_module
;
892 error("Error removing %s (%s): %s\n",
894 remove_moderror(errno
));
898 /* Now do things we depend. */
899 if (!list_empty(list
))
900 rmmod(list
, NULL
, 0, warn
, dry_run
, commands
,
906 fatal("Module %s is not in kernel.\n", mod
->modname
);
913 char name
[64 - sizeof(uint32_t)];
919 char name
[64 - sizeof(uint64_t)];
922 const char *skip_dot(const char *str
)
924 /* For our purposes, .foo matches foo. PPC64 needs this. */
925 if (str
&& str
[0] == '.')
930 void dump_modversions(const char *filename
, errfn_t error
)
932 unsigned long size
, secsize
;
933 void *file
= grab_file(filename
, &size
);
934 struct modver32_info
*info32
;
935 struct modver64_info
*info64
;
939 error("%s: %s\n", filename
, strerror(errno
));
942 switch (elf_ident(file
, size
)) {
944 info32
= get_section32(file
, size
, "__versions", &secsize
);
946 return; /* Does not seem to be a kernel module */
947 if (secsize
% sizeof(struct modver32_info
))
948 error("Wrong section size in %s\n", filename
);
949 for (n
= 0; n
< secsize
/ sizeof(struct modver32_info
); n
++)
950 printf("0x%08lx\t%s\n", (unsigned long)
951 info32
[n
].crc
, skip_dot(info32
[n
].name
));
955 info64
= get_section64(file
, size
, "__versions", &secsize
);
957 return; /* Does not seem to be a kernel module */
958 if (secsize
% sizeof(struct modver64_info
))
959 error("Wrong section size in %s\n", filename
);
960 for (n
= 0; n
< secsize
/ sizeof(struct modver64_info
); n
++)
961 printf("0x%08llx\t%s\n", (unsigned long long)
962 info64
[n
].crc
, skip_dot(info64
[n
].name
));
966 error("%s: ELF class not recognized\n", filename
);
971 /* Does path contain directory(s) subpath? */
972 static int type_matches(const char *path
, const char *subpath
)
974 char *subpath_with_slashes
;
977 nofail_asprintf(&subpath_with_slashes
, "/%s/", subpath
);
979 ret
= (strstr(path
, subpath_with_slashes
) != NULL
);
980 free(subpath_with_slashes
);
985 static int do_wildcard(const char *dirname
,
987 const char *wildcard
)
989 char *modules_dep_name
;
993 /* Canonicalize wildcard */
994 wcard
= strdup(wildcard
);
997 nofail_asprintf(&modules_dep_name
, "%s/%s", dirname
, "modules.dep");
998 modules_dep
= fopen(modules_dep_name
, "r");
1000 fatal("Could not load %s: %s\n",
1001 modules_dep_name
, strerror(errno
));
1003 while ((line
= getline_wrapped(modules_dep
, NULL
)) != NULL
) {
1006 /* Ignore lines without : or which start with a # */
1007 ptr
= strchr(line
, ':');
1008 if (ptr
== NULL
|| line
[strspn(line
, "\t ")] == '#')
1012 /* "type" must match complete directory component(s). */
1013 if (!type
|| type_matches(line
, type
)) {
1014 char modname
[strlen(line
)+1];
1016 filename2modname(modname
, line
);
1017 if (fnmatch(wcard
, modname
, 0) == 0)
1018 printf("%s\n", line
);
1024 free(modules_dep_name
);
1029 static char *strsep_skipspace(char **string
, char *delim
)
1033 *string
+= strspn(*string
, delim
);
1034 return strsep(string
, delim
);
1037 static int parse_config_scan(const char *filename
,
1041 struct module_options
**options
,
1042 struct module_command
**commands
,
1043 struct module_alias
**alias
,
1044 struct module_blacklist
**blacklist
);
1046 static int parse_config_file(const char *filename
,
1050 struct module_options
**options
,
1051 struct module_command
**commands
,
1052 struct module_alias
**aliases
,
1053 struct module_blacklist
**blacklist
)
1056 unsigned int linenum
= 0;
1059 cfile
= fopen(filename
, "r");
1063 while ((line
= getline_wrapped(cfile
, &linenum
)) != NULL
) {
1065 char *cmd
, *modname
;
1068 printf("%s\n", line
);
1070 cmd
= strsep_skipspace(&ptr
, "\t ");
1071 if (cmd
== NULL
|| cmd
[0] == '#' || cmd
[0] == '\0') {
1076 if (streq(cmd
, "alias")) {
1077 char *wildcard
= strsep_skipspace(&ptr
, "\t ");
1078 char *realname
= strsep_skipspace(&ptr
, "\t ");
1080 if (!wildcard
|| !realname
)
1081 grammar(cmd
, filename
, linenum
);
1082 else if (fnmatch(underscores(wildcard
),name
,0) == 0)
1083 *aliases
= add_alias(underscores(realname
), *aliases
);
1084 } else if (streq(cmd
, "include")) {
1085 struct module_alias
*newalias
= NULL
;
1088 newfilename
= strsep_skipspace(&ptr
, "\t ");
1090 grammar(cmd
, filename
, linenum
);
1092 warn("\"include %s\" is deprecated, "
1093 "please use /etc/modprobe.d\n", newfilename
);
1094 if (strstarts(newfilename
, "/etc/modprobe.d")) {
1095 warn("\"include /etc/modprobe.d\" is "
1096 "the default, ignored\n");
1098 if (!parse_config_scan(newfilename
, name
,
1099 dump_only
, removing
,
1100 options
, commands
, &newalias
,
1102 warn("Failed to open included"
1103 " config file %s: %s\n",
1104 newfilename
, strerror(errno
));
1106 /* Files included override aliases,
1107 etc that was already set ... */
1109 *aliases
= newalias
;
1111 } else if (streq(cmd
, "options")) {
1112 modname
= strsep_skipspace(&ptr
, "\t ");
1113 if (!modname
|| !ptr
)
1114 grammar(cmd
, filename
, linenum
);
1116 ptr
+= strspn(ptr
, "\t ");
1117 *options
= add_options(underscores(modname
),
1120 } else if (streq(cmd
, "install")) {
1121 modname
= strsep_skipspace(&ptr
, "\t ");
1122 if (!modname
|| !ptr
)
1123 grammar(cmd
, filename
, linenum
);
1124 else if (!removing
) {
1125 ptr
+= strspn(ptr
, "\t ");
1126 *commands
= add_command(underscores(modname
),
1129 } else if (streq(cmd
, "blacklist")) {
1130 modname
= strsep_skipspace(&ptr
, "\t ");
1132 grammar(cmd
, filename
, linenum
);
1133 else if (!removing
) {
1134 *blacklist
= add_blacklist(underscores(modname
),
1137 } else if (streq(cmd
, "remove")) {
1138 modname
= strsep_skipspace(&ptr
, "\t ");
1139 if (!modname
|| !ptr
)
1140 grammar(cmd
, filename
, linenum
);
1141 else if (removing
) {
1142 ptr
+= strspn(ptr
, "\t ");
1143 *commands
= add_command(underscores(modname
),
1146 } else if (streq(cmd
, "config")) {
1147 char *tmp
= strsep_skipspace(&ptr
, "\t ");
1150 grammar(cmd
, filename
, linenum
);
1151 else if (streq(tmp
, "binary_indexes")) {
1152 tmp
= strsep_skipspace(&ptr
, "\t ");
1153 if (streq(tmp
, "yes"))
1154 use_binary_indexes
= 1;
1155 if (streq(tmp
, "no"))
1156 use_binary_indexes
= 0;
1159 grammar(cmd
, filename
, linenum
);
1167 /* fallback to plain-text aliases file as necessary */
1168 static int read_aliases_file(const char *filename
,
1172 struct module_options
**options
,
1173 struct module_command
**commands
,
1174 struct module_alias
**aliases
,
1175 struct module_blacklist
**blacklist
)
1177 struct index_value
*realnames
;
1178 struct index_value
*realname
;
1180 struct index_file
*index
;
1182 if (!use_binary_indexes
)
1185 nofail_asprintf(&binfile
, "%s.bin", filename
);
1186 index
= index_file_open(binfile
);
1193 index_dump(index
, stdout
, "alias ");
1195 index_file_close(index
);
1199 realnames
= index_searchwild(index
, name
);
1200 for (realname
= realnames
; realname
; realname
= realname
->next
)
1201 *aliases
= add_alias(realname
->value
, *aliases
);
1202 index_values_free(realnames
);
1205 index_file_close(index
);
1209 return parse_config_file(filename
, name
, dump_only
, removing
,
1210 options
, commands
, aliases
, blacklist
);
1213 static int parse_config_scan(const char *filename
,
1217 struct module_options
**options
,
1218 struct module_command
**commands
,
1219 struct module_alias
**aliases
,
1220 struct module_blacklist
**blacklist
)
1225 dir
= opendir(filename
);
1228 struct list_head node
;
1231 LIST_HEAD(files_list
);
1232 struct file_entry
*fe
, *fe_tmp
;
1235 /* sort files from directory into list */
1236 while ((i
= readdir(dir
)) != NULL
) {
1239 if (i
->d_name
[0] == '.')
1241 if (!config_filter(i
->d_name
))
1244 len
= strlen(i
->d_name
);
1246 (strcmp(&i
->d_name
[len
-5], ".conf") != 0 &&
1247 strcmp(&i
->d_name
[len
-6], ".alias") != 0))
1248 warn("All config files need .conf: %s/%s, "
1249 "it will be ignored in a future release.\n",
1250 filename
, i
->d_name
);
1251 fe
= malloc(sizeof(struct file_entry
) + len
+ 1);
1254 strcpy(fe
->name
, i
->d_name
);
1255 list_for_each_entry(fe_tmp
, &files_list
, node
)
1256 if (strcmp(fe_tmp
->name
, fe
->name
) >= 0)
1258 list_add_tail(&fe
->node
, &fe_tmp
->node
);
1262 /* parse list of files */
1263 list_for_each_entry_safe(fe
, fe_tmp
, &files_list
, node
) {
1266 nofail_asprintf(&cfgfile
, "%s/%s", filename
, fe
->name
);
1267 if (!parse_config_file(cfgfile
, name
,
1268 dump_only
, removing
,
1270 aliases
, blacklist
))
1271 warn("Failed to open config file "
1272 "%s: %s\n", fe
->name
, strerror(errno
));
1274 list_del(&fe
->node
);
1280 if (parse_config_file(filename
, name
, dump_only
, removing
,
1281 options
, commands
, aliases
, blacklist
))
1287 /* Read binary index file containing aliases only */
1288 static void parse_toplevel_config(const char *filename
,
1292 struct module_options
**options
,
1293 struct module_command
**commands
,
1294 struct module_alias
**aliases
,
1295 struct module_blacklist
**blacklist
)
1298 if (!parse_config_scan(filename
, name
, dump_only
, removing
,
1299 options
, commands
, aliases
, blacklist
))
1300 fatal("Failed to open config file %s: %s\n",
1301 filename
, strerror(errno
));
1305 /* deprecated config file */
1306 if (parse_config_file("/etc/modprobe.conf", name
, dump_only
, removing
,
1307 options
, commands
, aliases
, blacklist
) > 0)
1308 warn("Deprecated config file /etc/modprobe.conf, "
1309 "all config files belong into /etc/modprobe.d/.\n");
1311 /* default config */
1312 parse_config_scan("/etc/modprobe.d", name
, dump_only
, removing
,
1313 options
, commands
, aliases
, blacklist
);
1316 /* Read possible module arguments from the kernel command line. */
1317 static int parse_kcmdline(int dump_only
, struct module_options
**options
)
1320 unsigned int linenum
= 0;
1323 kcmdline
= fopen("/proc/cmdline", "r");
1327 while ((line
= getline_wrapped(kcmdline
, &linenum
)) != NULL
) {
1331 while ((arg
= strsep_skipspace(&ptr
, "\t ")) != NULL
) {
1332 char *sep
, *modname
, *opt
;
1334 sep
= strchr(arg
, '.');
1336 if (!strchr(sep
, '='))
1343 printf("options %s %s\n", modname
, opt
);
1345 *options
= add_options(underscores(modname
),
1356 static void add_to_env_var(const char *option
)
1360 if ((oldenv
= getenv("MODPROBE_OPTIONS")) != NULL
) {
1362 nofail_asprintf(&newenv
, "%s %s", oldenv
, option
);
1363 setenv("MODPROBE_OPTIONS", newenv
, 1);
1365 setenv("MODPROBE_OPTIONS", option
, 1);
1368 /* Prepend options from environment. */
1369 static char **merge_args(char *args
, char *argv
[], int *argc
)
1371 char *arg
, *argstring
;
1372 char **newargs
= NULL
;
1373 unsigned int i
, num_env
= 0;
1378 argstring
= NOFAIL(strdup(args
));
1379 for (arg
= strtok(argstring
, " "); arg
; arg
= strtok(NULL
, " ")) {
1381 newargs
= NOFAIL(realloc(newargs
,
1383 * (num_env
+ *argc
+ 1)));
1384 newargs
[num_env
] = arg
;
1390 /* Append commandline args */
1391 newargs
[0] = argv
[0];
1392 for (i
= 1; i
<= *argc
; i
++)
1393 newargs
[num_env
+i
] = argv
[i
];
1399 static char *gather_options(char *argv
[])
1401 char *optstring
= NOFAIL(strdup(""));
1403 /* Rest is module options */
1405 /* Quote value if it contains spaces. */
1406 unsigned int eq
= strcspn(*argv
, "=");
1408 if (strchr(*argv
+eq
, ' ') && !strchr(*argv
, '"')) {
1409 char quoted
[strlen(*argv
) + 3];
1411 sprintf(quoted
, "%s=\"%s\"", *argv
, *argv
+eq
+1);
1412 optstring
= append_option(optstring
, quoted
);
1414 optstring
= append_option(optstring
, *argv
);
1420 static int handle_module(const char *modname
,
1421 struct list_head
*todo_list
,
1422 const char *newname
,
1428 struct module_options
*modoptions
,
1429 struct module_command
*commands
,
1430 int ignore_commands
,
1433 int strip_modversion
,
1434 const char *cmdline_opts
,
1437 if (list_empty(todo_list
)) {
1438 const char *command
;
1440 /* The dependencies have to be real modules, but
1441 handle case where the first is completely bogus. */
1442 command
= find_command(modname
, commands
);
1443 if (command
&& !ignore_commands
) {
1444 do_command(modname
, command
, dry_run
, error
,
1445 remove
? "remove":"install", cmdline_opts
);
1450 error("Module %s not found.\n", modname
);
1455 rmmod(todo_list
, newname
, first_time
, error
, dry_run
,
1456 commands
, ignore_commands
, 0, cmdline_opts
, flags
);
1458 insmod(todo_list
, NOFAIL(strdup(options
)), newname
,
1459 first_time
, error
, dry_run
, modoptions
,
1460 commands
, ignore_commands
, ignore_proc
, strip_vermagic
,
1461 strip_modversion
, cmdline_opts
);
1466 static struct option options
[] = { { "verbose", 0, NULL
, 'v' },
1467 { "version", 0, NULL
, 'V' },
1468 { "config", 1, NULL
, 'C' },
1469 { "name", 1, NULL
, 'o' },
1470 { "remove", 0, NULL
, 'r' },
1471 { "wait", 0, NULL
, 'w' },
1472 { "showconfig", 0, NULL
, 'c' },
1473 { "quiet", 0, NULL
, 'q' },
1474 { "show", 0, NULL
, 'n' },
1475 { "dry-run", 0, NULL
, 'n' },
1476 { "syslog", 0, NULL
, 's' },
1477 { "type", 1, NULL
, 't' },
1478 { "list", 0, NULL
, 'l' },
1479 { "all", 0, NULL
, 'a' },
1480 { "ignore-install", 0, NULL
, 'i' },
1481 { "ignore-remove", 0, NULL
, 'i' },
1482 { "force", 0, NULL
, 'f' },
1483 { "force-vermagic", 0, NULL
, 1 },
1484 { "force-modversion", 0, NULL
, 2 },
1485 { "set-version", 1, NULL
, 'S' },
1486 { "show-depends", 0, NULL
, 'D' },
1487 { "dirname", 1, NULL
, 'd' },
1488 { "first-time", 0, NULL
, 3 },
1489 { "dump-modversions", 0, NULL
, 4 },
1490 { "use-blacklist", 0, NULL
, 'b' },
1491 { NULL
, 0, NULL
, 0 } };
1493 int main(int argc
, char *argv
[])
1496 struct stat statbuf
;
1503 int ignore_commands
= 0;
1504 int strip_vermagic
= 0;
1505 int strip_modversion
= 0;
1506 int ignore_proc
= 0;
1508 int dump_modver
= 0;
1509 int use_blacklist
= 0;
1510 unsigned int i
, num_modules
;
1512 const char *config
= NULL
;
1513 char *dirname
= NULL
;
1514 char *optstring
= NULL
;
1515 char *newname
= NULL
;
1516 char *aliasfilename
, *symfilename
;
1517 errfn_t error
= fatal
;
1518 int flags
= O_NONBLOCK
|O_EXCL
;
1521 /* Prepend options from environment. */
1522 argv
= merge_args(getenv("MODPROBE_OPTIONS"), argv
, &argc
);
1525 while ((opt
= getopt_long(argc
, argv
, "vVC:o:rnqQsclt:aifbwd:", options
, NULL
)) != -1){
1528 add_to_env_var("-v");
1532 puts(PACKAGE
" version " VERSION
);
1535 strncpy(buf
.release
, optarg
, sizeof(buf
.release
));
1536 buf
.release
[sizeof(buf
.release
)-1] = '\0';
1540 add_to_env_var("-C");
1541 add_to_env_var(config
);
1545 add_to_env_var("-q");
1551 add_to_env_var("-D");
1576 add_to_env_var("-s");
1580 ignore_commands
= 1;
1584 strip_modversion
= 1;
1590 flags
&= ~O_NONBLOCK
;
1593 nofail_asprintf(&dirname
, "%s/%s/%s", optarg
,
1594 MODULE_DIR
, buf
.release
);
1600 strip_modversion
= 1;
1609 print_usage(argv
[0]);
1613 /* If stderr not open, go to syslog */
1614 if (logging
|| fstat(STDERR_FILENO
, &statbuf
) != 0) {
1615 openlog("modprobe", LOG_CONS
, LOG_DAEMON
);
1619 if (argc
< optind
+ 1 && !dump_only
&& !list_only
&& !remove
)
1620 print_usage(argv
[0]);
1623 nofail_asprintf(&dirname
, "%s/%s", MODULE_DIR
, buf
.release
);
1624 nofail_asprintf(&aliasfilename
, "%s/modules.alias", dirname
);
1625 nofail_asprintf(&symfilename
, "%s/modules.symbols", dirname
);
1627 /* Old-style -t xxx wildcard? Only with -l. */
1629 if (optind
+1 < argc
)
1630 fatal("Can't have multiple wildcards\n");
1631 /* fprintf(stderr, "man find\n"); return 1; */
1632 return do_wildcard(dirname
, type
, argv
[optind
]?:"*");
1635 fatal("-t only supported with -l");
1638 struct module_command
*commands
= NULL
;
1639 struct module_options
*modoptions
= NULL
;
1640 struct module_alias
*aliases
= NULL
;
1641 struct module_blacklist
*blacklist
= NULL
;
1643 parse_toplevel_config(config
, "", 1, 0, &modoptions
,
1644 &commands
, &aliases
, &blacklist
);
1645 /* Read module options from kernel command line */
1646 parse_kcmdline(1, &modoptions
);
1647 parse_config_file(aliasfilename
, "", 1, 0, &modoptions
,
1648 &commands
, &aliases
, &blacklist
);
1649 parse_config_file(symfilename
, "", 1, 0, &modoptions
,
1650 &commands
, &aliases
, &blacklist
);
1654 if (remove
|| all
) {
1655 num_modules
= argc
- optind
;
1656 optstring
= NOFAIL(strdup(""));
1659 optstring
= gather_options(argv
+optind
+1);
1662 /* num_modules is always 1 except for -r or -a. */
1663 for (i
= 0; i
< num_modules
; i
++) {
1664 struct module_command
*commands
= NULL
;
1665 struct module_options
*modoptions
= NULL
;
1666 struct module_alias
*aliases
= NULL
;
1667 struct module_blacklist
*blacklist
= NULL
;
1669 char *modulearg
= argv
[optind
+ i
];
1672 dump_modversions(modulearg
, error
);
1676 /* Convert name we are looking for */
1677 underscores(modulearg
);
1679 /* Returns the resolved alias, options */
1680 parse_toplevel_config(config
, modulearg
, 0,
1681 remove
, &modoptions
, &commands
, &aliases
, &blacklist
);
1683 /* Read module options from kernel command line */
1684 parse_kcmdline(0, &modoptions
);
1686 /* No luck? Try symbol names, if starts with symbol:. */
1687 if (!aliases
&& strstarts(modulearg
, "symbol:")) {
1688 parse_config_file(symfilename
, modulearg
, 0,
1689 remove
, &modoptions
, &commands
,
1690 &aliases
, &blacklist
);
1693 if(!strchr(modulearg
, ':'))
1694 read_depends(dirname
, modulearg
, &list
);
1696 /* We only use canned aliases as last resort. */
1697 if (list_empty(&list
)
1698 && !find_command(modulearg
, commands
))
1700 read_aliases_file(aliasfilename
,
1701 modulearg
, 0, remove
,
1702 &modoptions
, &commands
,
1703 &aliases
, &blacklist
);
1707 aliases
= apply_blacklist(aliases
, blacklist
);
1709 errfn_t err
= error
;
1711 /* More than one alias? Don't bail out on failure. */
1715 /* Add the options for this alias. */
1716 char *opts
= NOFAIL(strdup(optstring
));
1717 opts
= add_extra_options(modulearg
,
1720 read_depends(dirname
, aliases
->module
, &list
);
1721 if (handle_module(aliases
->module
, &list
,
1722 newname
, remove
, opts
,
1724 dry_run
, modoptions
,
1725 commands
, ignore_commands
,
1726 ignore_proc
, strip_vermagic
,
1731 aliases
= aliases
->next
;
1732 INIT_LIST_HEAD(&list
);
1736 && find_blacklist(modulearg
, blacklist
))
1739 if (handle_module(modulearg
, &list
, newname
, remove
,
1740 optstring
, first_time
, error
, dry_run
,
1741 modoptions
, commands
,
1742 ignore_commands
, ignore_proc
,
1743 strip_vermagic
, strip_modversion
,
1752 free(aliasfilename
);