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>
42 #define streq(a,b) (strcmp((a),(b)) == 0)
43 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
45 #include "zlibsupport.h"
49 #include "config_filter.h"
53 int use_binary_indexes
= 1; /* default to enabled. */
55 extern long init_module(void *, unsigned long, const char *);
56 extern long delete_module(const char *, unsigned int);
59 struct list_head list
;
65 #define MODULE_DIR "/lib/modules"
68 typedef void (*errfn_t
)(const char *fmt
, ...);
70 static void grammar(const char *cmd
, const char *filename
, unsigned int line
)
72 warn("%s line %u: ignoring bad line starting with '%s'\n",
76 static void print_usage(const char *progname
)
79 "Usage: %s [-v] [-V] [-C config-file] [-d <dirname> ] [-n] [-i] [-q] [-b] [-o <modname>] [ --dump-modversions ] <modname> [parameters...]\n"
80 "%s -r [-n] [-i] [-v] <modulename> ...\n"
81 "%s -l -t <dirname> [ -a <modulename> ...]\n",
82 progname
, progname
, progname
);
86 static char *getline_wrapped(FILE *file
, unsigned int *linenum
)
90 char *buf
= NOFAIL(malloc(size
));
92 int ch
= getc_unlocked(file
);
100 /* else fall through */
106 buf
= NOFAIL(realloc(buf
, size
+ 1));
111 ch
= getc_unlocked(file
);
118 /* else fall through */
125 buf
= NOFAIL(realloc(buf
, size
));
131 static struct module
*find_module(const char *filename
, struct list_head
*list
)
135 list_for_each_entry(i
, list
, list
) {
136 if (strcmp(i
->filename
, filename
) == 0)
142 /* Convert filename to the module name. Works if filename == modname, too. */
143 static void filename2modname(char *modname
, const char *filename
)
145 const char *afterslash
;
148 afterslash
= strrchr(filename
, '/');
150 afterslash
= filename
;
154 /* Convert to underscores, stop at first . */
155 for (i
= 0; afterslash
[i
] && afterslash
[i
] != '.'; i
++) {
156 if (afterslash
[i
] == '-')
159 modname
[i
] = afterslash
[i
];
164 static int lock_file(const char *filename
)
166 int fd
= open(filename
, O_RDWR
, 0);
170 lock
.l_type
= F_WRLCK
;
171 lock
.l_whence
= SEEK_SET
;
174 fcntl(fd
, F_SETLKW
, &lock
);
176 /* Read-only filesystem? There goes locking... */
177 fd
= open(filename
, O_RDONLY
, 0);
181 static void unlock_file(int fd
)
183 /* Valgrind is picky... */
187 static void add_module(char *filename
, int namelen
, struct list_head
*list
)
191 /* If it's a duplicate: move it to the end, so it gets
192 inserted where it is *first* required. */
193 mod
= find_module(filename
, list
);
195 list_del(&mod
->list
);
197 /* No match. Create a new module. */
198 mod
= NOFAIL(malloc(sizeof(struct module
) + namelen
+ 1));
199 memcpy(mod
->filename
, filename
, namelen
);
200 mod
->filename
[namelen
] = '\0';
201 mod
->modname
= NOFAIL(malloc(namelen
+ 1));
202 filename2modname(mod
->modname
, mod
->filename
);
205 list_add_tail(&mod
->list
, list
);
208 /* Compare len chars of a to b, with _ and - equivalent. */
209 static int modname_equal(const char *a
, const char *b
, unsigned int len
)
213 if (strlen(b
) != len
)
216 for (i
= 0; i
< len
; i
++) {
217 if ((a
[i
] == '_' || a
[i
] == '-')
218 && (b
[i
] == '_' || b
[i
] == '-'))
226 /* Fills in list of modules if this is the line we want. */
227 static int add_modules_dep_line(char *line
,
229 struct list_head
*list
,
234 char *modname
, *fullpath
;
236 /* Ignore lines without : or which start with a # */
237 ptr
= strchr(line
, ':');
238 if (ptr
== NULL
|| line
[strspn(line
, "\t ")] == '#')
241 /* Is this the module we are looking for? */
243 if (strrchr(line
, '/'))
244 modname
= strrchr(line
, '/') + 1;
248 len
= strlen(modname
);
249 if (strchr(modname
, '.'))
250 len
= strchr(modname
, '.') - modname
;
251 if (!modname_equal(modname
, name
, len
))
254 /* Create the list. */
255 if ('/' == line
[0]) { /* old style deps - absolute path specified */
256 add_module(line
, ptr
- line
, list
);
258 nofail_asprintf(&fullpath
, "%s/%s", dirname
, line
);
259 add_module(fullpath
, strlen(dirname
)+1+(ptr
- line
), list
);
266 ptr
+= strspn(ptr
, " \t");
270 ptr
+= strcspn(ptr
, " \t");
271 if ('/' == dep_start
[0]) { /* old style deps */
272 add_module(dep_start
, ptr
- dep_start
, list
);
274 nofail_asprintf(&fullpath
, "%s/%s", dirname
, dep_start
);
276 strlen(dirname
)+1+(ptr
- dep_start
), list
);
283 static int read_depends_file(const char *dirname
,
284 const char *start_name
,
285 struct list_head
*list
)
287 char *modules_dep_name
;
289 struct index_file
*modules_dep
;
291 nofail_asprintf(&modules_dep_name
, "%s/%s", dirname
, "modules.dep.bin");
292 modules_dep
= index_file_open(modules_dep_name
);
294 free(modules_dep_name
);
298 line
= index_search(modules_dep
, start_name
);
300 /* Value is standard dependency line format */
301 if (!add_modules_dep_line(line
, start_name
, list
, dirname
))
302 fatal("Module index is inconsistent\n");
306 index_file_close(modules_dep
);
307 free(modules_dep_name
);
312 static void read_depends(const char *dirname
,
313 const char *start_name
,
314 struct list_head
*list
)
316 char *modules_dep_name
;
321 if (use_binary_indexes
)
322 if (read_depends_file(dirname
, start_name
, list
))
325 nofail_asprintf(&modules_dep_name
, "%s/%s", dirname
, "modules.dep");
326 modules_dep
= fopen(modules_dep_name
, "r");
328 fatal("Could not load %s: %s\n",
329 modules_dep_name
, strerror(errno
));
331 /* Stop at first line, as we can have duplicates (eg. symlinks
333 while (!done
&& (line
= getline_wrapped(modules_dep
, NULL
)) != NULL
) {
334 done
= add_modules_dep_line(line
, start_name
, list
, dirname
);
338 free(modules_dep_name
);
341 /* We use error numbers in a loose translation... */
342 static const char *insert_moderror(int err
)
346 return "Invalid module format";
348 return "Unknown symbol in module, or unknown parameter (see dmesg)";
350 return "Kernel does not have module support";
352 return strerror(err
);
356 static const char *remove_moderror(int err
)
360 return "No such module";
362 return "Kernel does not have module unloading support";
364 return strerror(err
);
368 static void replace_modname(struct module
*module
,
369 void *mem
, unsigned long len
,
370 const char *oldname
, const char *newname
)
374 /* 64 - sizeof(unsigned long) - 1 */
375 if (strlen(newname
) > 55)
376 fatal("New name %s is too long\n", newname
);
378 /* Find where it is in the module structure. Don't assume layout! */
379 for (p
= mem
; p
< (char *)mem
+ len
- strlen(oldname
); p
++) {
380 if (memcmp(p
, oldname
, strlen(oldname
)) == 0) {
386 warn("Could not find old name in %s to replace!\n", module
->filename
);
389 static void *get_section32(void *file
,
392 unsigned long *secsize
)
394 Elf32_Ehdr
*hdr
= file
;
395 Elf32_Shdr
*sechdrs
= file
+ hdr
->e_shoff
;
396 const char *secnames
;
400 if (size
< sizeof(*hdr
))
402 if (size
< hdr
->e_shoff
+ hdr
->e_shnum
* sizeof(sechdrs
[0]))
404 if (size
< sechdrs
[hdr
->e_shstrndx
].sh_offset
)
407 secnames
= file
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
408 for (i
= 1; i
< hdr
->e_shnum
; i
++)
409 if (strcmp(secnames
+ sechdrs
[i
].sh_name
, name
) == 0) {
410 *secsize
= sechdrs
[i
].sh_size
;
411 return file
+ sechdrs
[i
].sh_offset
;
416 static void *get_section64(void *file
,
419 unsigned long *secsize
)
421 Elf64_Ehdr
*hdr
= file
;
422 Elf64_Shdr
*sechdrs
= file
+ hdr
->e_shoff
;
423 const char *secnames
;
427 if (size
< sizeof(*hdr
))
429 if (size
< hdr
->e_shoff
+ hdr
->e_shnum
* sizeof(sechdrs
[0]))
431 if (size
< sechdrs
[hdr
->e_shstrndx
].sh_offset
)
434 secnames
= file
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
435 for (i
= 1; i
< hdr
->e_shnum
; i
++)
436 if (strcmp(secnames
+ sechdrs
[i
].sh_name
, name
) == 0) {
437 *secsize
= sechdrs
[i
].sh_size
;
438 return file
+ sechdrs
[i
].sh_offset
;
443 static int elf_ident(void *mod
, unsigned long size
)
445 /* "\177ELF" <byte> where byte = 001 for 32-bit, 002 for 64 */
448 if (size
< EI_CLASS
|| memcmp(mod
, ELFMAG
, SELFMAG
) != 0)
450 return ident
[EI_CLASS
];
453 static void *get_section(void *file
,
456 unsigned long *secsize
)
458 switch (elf_ident(file
, size
)) {
460 return get_section32(file
, size
, name
, secsize
);
462 return get_section64(file
, size
, name
, secsize
);
468 static void rename_module(struct module
*module
,
474 unsigned long modstruct_len
;
477 modstruct
= get_section(mod
, len
, ".gnu.linkonce.this_module",
481 modstruct
= get_section(mod
, len
, "__module", &modstruct_len
);
483 warn("Could not find module name to change in %s\n",
486 replace_modname(module
, modstruct
, modstruct_len
,
487 module
->modname
, newname
);
490 /* Kernel told to ignore these sections if SHF_ALLOC not set. */
491 static void invalidate_section32(void *mod
, const char *secname
)
493 Elf32_Ehdr
*hdr
= mod
;
494 Elf32_Shdr
*sechdrs
= mod
+ hdr
->e_shoff
;
495 const char *secnames
= mod
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
498 for (i
= 1; i
< hdr
->e_shnum
; i
++)
499 if (strcmp(secnames
+sechdrs
[i
].sh_name
, secname
) == 0)
500 sechdrs
[i
].sh_flags
&= ~SHF_ALLOC
;
503 static void invalidate_section64(void *mod
, const char *secname
)
505 Elf64_Ehdr
*hdr
= mod
;
506 Elf64_Shdr
*sechdrs
= mod
+ hdr
->e_shoff
;
507 const char *secnames
= mod
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
510 for (i
= 1; i
< hdr
->e_shnum
; i
++)
511 if (strcmp(secnames
+sechdrs
[i
].sh_name
, secname
) == 0)
512 sechdrs
[i
].sh_flags
&= ~(unsigned long long)SHF_ALLOC
;
515 static void strip_section(struct module
*module
,
520 switch (elf_ident(mod
, len
)) {
522 invalidate_section32(mod
, secname
);
525 invalidate_section64(mod
, secname
);
528 warn("Unknown module format in %s: not forcing version\n",
533 static const char *next_string(const char *string
, unsigned long *secsize
)
535 /* Skip non-zero chars */
538 if ((*secsize
)-- <= 1)
542 /* Skip any zero padding. */
545 if ((*secsize
)-- <= 1)
551 static void clear_magic(struct module
*module
, void *mod
, unsigned long len
)
554 unsigned long modlen
;
556 /* Old-style: __vermagic section */
557 strip_section(module
, mod
, len
, "__vermagic");
559 /* New-style: in .modinfo section */
560 for (p
= get_section(mod
, len
, ".modinfo", &modlen
);
562 p
= next_string(p
, &modlen
)) {
563 if (strncmp(p
, "vermagic=", strlen("vermagic=")) == 0) {
564 memset((char *)p
, 0, strlen(p
));
570 struct module_options
572 struct module_options
*next
;
577 struct module_command
579 struct module_command
*next
;
586 struct module_alias
*next
;
590 struct module_blacklist
592 struct module_blacklist
*next
;
596 /* Link in a new option line from the config file. */
597 static struct module_options
*
598 add_options(const char *modname
,
600 struct module_options
*options
)
602 struct module_options
*new;
605 new = NOFAIL(malloc(sizeof(*new)));
606 new->modulename
= NOFAIL(strdup(modname
));
607 new->options
= NOFAIL(strdup(option
));
608 /* We can handle tabs, kernel can't. */
609 for (tab
= strchr(new->options
, '\t'); tab
; tab
= strchr(tab
, '\t'))
615 /* Link in a new install line from the config file. */
616 static struct module_command
*
617 add_command(const char *modname
,
619 struct module_command
*commands
)
621 struct module_command
*new;
623 new = NOFAIL(malloc(sizeof(*new)));
624 new->modulename
= NOFAIL(strdup(modname
));
625 new->command
= NOFAIL(strdup(command
));
626 new->next
= commands
;
630 /* Link in a new alias line from the config file. */
631 static struct module_alias
*
632 add_alias(const char *modname
, struct module_alias
*aliases
)
634 struct module_alias
*new;
636 new = NOFAIL(malloc(sizeof(*new)));
637 new->module
= NOFAIL(strdup(modname
));
642 /* Link in a new blacklist line from the config file. */
643 static struct module_blacklist
*
644 add_blacklist(const char *modname
, struct module_blacklist
*blacklist
)
646 struct module_blacklist
*new;
648 new = NOFAIL(malloc(sizeof(*new)));
649 new->modulename
= NOFAIL(strdup(modname
));
650 new->next
= blacklist
;
654 /* Find blacklist commands if any. */
656 find_blacklist(const char *modname
, const struct module_blacklist
*blacklist
)
659 if (strcmp(blacklist
->modulename
, modname
) == 0)
661 blacklist
= blacklist
->next
;
666 /* return a new alias list, with backlisted elems filtered out */
667 static struct module_alias
*
668 apply_blacklist(const struct module_alias
*aliases
,
669 const struct module_blacklist
*blacklist
)
671 struct module_alias
*result
= NULL
;
673 char *modname
= aliases
->module
;
674 if (!find_blacklist(modname
, blacklist
))
675 result
= add_alias(modname
, result
);
676 aliases
= aliases
->next
;
681 /* Find install commands if any. */
682 static const char *find_command(const char *modname
,
683 const struct module_command
*commands
)
686 if (fnmatch(commands
->modulename
, modname
, 0) == 0)
687 return commands
->command
;
688 commands
= commands
->next
;
693 static char *append_option(char *options
, const char *newoption
)
695 options
= NOFAIL(realloc(options
, strlen(options
) + 1
696 + strlen(newoption
) + 1));
697 if (strlen(options
)) strcat(options
, " ");
698 strcat(options
, newoption
);
702 static char *prepend_option(char *options
, const char *newoption
)
705 l1
= strlen(options
);
706 l2
= strlen(newoption
);
707 /* the resulting string will look like
708 * newoption + ' ' + options + '\0' */
710 options
= NOFAIL(realloc(options
, l2
+ 1 + l1
+ 1));
711 memmove(options
+ l2
+ 1, options
, l1
+ 1);
713 memcpy(options
, newoption
, l2
);
715 options
= NOFAIL(realloc(options
, l2
+ 1));
716 memcpy(options
, newoption
, l2
);
723 static char *add_extra_options(const char *modname
,
725 const struct module_options
*options
)
728 if (strcmp(options
->modulename
, modname
) == 0)
729 optstring
= prepend_option(optstring
, options
->options
);
730 options
= options
->next
;
735 /* Read sysfs attribute into a buffer.
736 * returns: 1 = ok, 0 = attribute missing,
737 * -1 = file error (or empty file, but we don't care).
739 static int read_attribute(const char *filename
, char *buf
, size_t buflen
)
744 file
= fopen(filename
, "r");
746 return (errno
== ENOENT
) ? 0 : -1;
747 s
= fgets(buf
, buflen
, file
);
750 return (s
== NULL
) ? -1 : 1;
753 /* Is module in /sys/module? If so, fill in usecount if not NULL.
754 0 means no, 1 means yes, -1 means unknown.
756 static int module_in_kernel(const char *modname
, unsigned int *usecount
)
762 const int ATTR_LEN
= 16;
765 /* Check sysfs is mounted */
766 if (stat("/sys/module", &finfo
) < 0)
770 nofail_asprintf(&name
, "/sys/module/%s", modname
);
771 ret
= stat(name
, &finfo
);
774 return (errno
== ENOENT
) ? 0 : -1; /* Not found or unknown. */
776 /* Wait for the existing module to either go live or disappear. */
777 nofail_asprintf(&name
, "/sys/module/%s/initstate", modname
);
779 ret
= read_attribute(name
, attr
, ATTR_LEN
);
780 if (ret
!= 1 || streq(attr
, "live\n"))
790 /* Get reference count, if it exists. */
791 if (usecount
!= NULL
) {
792 nofail_asprintf(&name
, "/sys/module/%s/refcnt", modname
);
793 ret
= read_attribute(name
, attr
, ATTR_LEN
);
796 *usecount
= atoi(attr
);
802 /* If we don't flush, then child processes print before we do */
803 static void verbose_printf(int verbose
, const char *fmt
, ...)
808 va_start(arglist
, fmt
);
809 vprintf(fmt
, arglist
);
815 /* Do an install/remove command: replace $CMDLINE_OPTS if it's specified. */
816 static void do_command(const char *modname
,
818 int verbose
, int dry_run
,
821 const char *cmdline_opts
)
824 char *p
, *replaced_cmd
= NOFAIL(strdup(command
));
826 while ((p
= strstr(replaced_cmd
, "$CMDLINE_OPTS")) != NULL
) {
828 nofail_asprintf(&new, "%.*s%s%s",
829 (int)(p
- replaced_cmd
), replaced_cmd
, cmdline_opts
,
830 p
+ strlen("$CMDLINE_OPTS"));
835 verbose_printf(verbose
, "%s %s\n", type
, replaced_cmd
);
839 setenv("MODPROBE_MODULE", modname
, 1);
840 ret
= system(replaced_cmd
);
841 if (ret
== -1 || WEXITSTATUS(ret
))
842 error("Error running %s command for %s\n", type
, modname
);
846 /* Actually do the insert. Frees second arg. */
847 static void insmod(struct list_head
*list
,
854 const struct module_options
*options
,
855 const struct module_command
*commands
,
859 int strip_modversion
,
860 const char *cmdline_opts
)
866 struct module
*mod
= list_entry(list
->next
, struct module
, list
);
868 /* Take us off the list. */
869 list_del(&mod
->list
);
871 /* Do things we (or parent) depend on first, but don't die if
873 if (!list_empty(list
)) {
874 insmod(list
, NOFAIL(strdup("")), NULL
, 0, warn
,
875 dry_run
, verbose
, options
, commands
, 0, ignore_proc
,
876 strip_vermagic
, strip_modversion
, "");
879 /* Lock before we look, in case it's initializing. */
880 fd
= lock_file(mod
->filename
);
882 error("Could not open '%s': %s\n",
883 mod
->filename
, strerror(errno
));
887 /* Don't do ANYTHING if already in kernel. */
889 && module_in_kernel(newname
?: mod
->modname
, NULL
) == 1) {
891 error("Module %s already in kernel.\n",
892 newname
?: mod
->modname
);
896 command
= find_command(mod
->modname
, commands
);
897 if (command
&& !ignore_commands
) {
898 /* It might recurse: unlock. */
900 do_command(mod
->modname
, command
, verbose
, dry_run
, error
,
901 "install", cmdline_opts
);
905 map
= grab_fd(fd
, &len
);
907 error("Could not read '%s': %s\n",
908 mod
->filename
, strerror(errno
));
914 rename_module(mod
, map
, len
, newname
);
916 if (strip_modversion
)
917 strip_section(mod
, map
, len
, "__versions");
919 clear_magic(mod
, map
, len
);
921 /* Config file might have given more options */
922 optstring
= add_extra_options(mod
->modname
, optstring
, options
);
924 verbose_printf(verbose
, "insmod %s %s\n", mod
->filename
, optstring
);
929 ret
= init_module(map
, len
, optstring
);
931 if (errno
== EEXIST
) {
933 error("Module %s already in kernel.\n",
934 newname
?: mod
->modname
);
937 /* don't warn noisely if we're loading multiple aliases. */
938 /* one of the aliases may try to use hardware we don't have. */
939 if ((error
!= warn
) || (verbose
))
940 error("Error inserting %s (%s): %s\n",
941 mod
->modname
, mod
->filename
,
942 insert_moderror(errno
));
945 release_file(map
, len
);
953 /* Do recursive removal. */
954 static void rmmod(struct list_head
*list
,
960 struct module_command
*commands
,
963 const char *cmdline_opts
,
967 unsigned int usecount
= 0;
969 struct module
*mod
= list_entry(list
->next
, struct module
, list
);
971 /* Take first one off the list. */
972 list_del(&mod
->list
);
974 /* Ignore failure; it's best effort here. */
975 lock
= lock_file(mod
->filename
);
980 /* Even if renamed, find commands to orig. name. */
981 command
= find_command(mod
->modname
, commands
);
982 if (command
&& !ignore_commands
) {
983 /* It might recurse: unlock. */
985 do_command(mod
->modname
, command
, verbose
, dry_run
, error
,
986 "remove", cmdline_opts
);
987 goto remove_rest_no_unlock
;
990 if (module_in_kernel(name
, &usecount
) == 0)
991 goto nonexistent_module
;
995 error("Module %s is in use.\n", name
);
999 verbose_printf(verbose
, "rmmod %s\n", mod
->filename
);
1004 if (delete_module(name
, O_EXCL
) != 0) {
1005 if (errno
== ENOENT
)
1006 goto nonexistent_module
;
1007 error("Error removing %s (%s): %s\n",
1008 name
, mod
->filename
,
1009 remove_moderror(errno
));
1014 remove_rest_no_unlock
:
1015 /* Now do things we depend. */
1016 if (!list_empty(list
))
1017 rmmod(list
, NULL
, 0, warn
, dry_run
, verbose
, commands
,
1023 fatal("Module %s is not in kernel.\n", mod
->modname
);
1027 struct modver32_info
1030 char name
[64 - sizeof(uint32_t)];
1033 struct modver64_info
1036 char name
[64 - sizeof(uint64_t)];
1039 const char *skip_dot(const char *str
)
1041 /* For our purposes, .foo matches foo. PPC64 needs this. */
1042 if (str
&& str
[0] == '.')
1047 void dump_modversions(const char *filename
, errfn_t error
)
1049 unsigned long size
, secsize
;
1050 void *file
= grab_file(filename
, &size
);
1051 struct modver32_info
*info32
;
1052 struct modver64_info
*info64
;
1056 error("%s: %s\n", filename
, strerror(errno
));
1059 switch (elf_ident(file
, size
)) {
1061 info32
= get_section32(file
, size
, "__versions", &secsize
);
1063 return; /* Does not seem to be a kernel module */
1064 if (secsize
% sizeof(struct modver32_info
))
1065 error("Wrong section size in %s\n", filename
);
1066 for (n
= 0; n
< secsize
/ sizeof(struct modver32_info
); n
++)
1067 printf("0x%08lx\t%s\n", (unsigned long)
1068 info32
[n
].crc
, skip_dot(info32
[n
].name
));
1072 info64
= get_section64(file
, size
, "__versions", &secsize
);
1074 return; /* Does not seem to be a kernel module */
1075 if (secsize
% sizeof(struct modver64_info
))
1076 error("Wrong section size in %s\n", filename
);
1077 for (n
= 0; n
< secsize
/ sizeof(struct modver64_info
); n
++)
1078 printf("0x%08llx\t%s\n", (unsigned long long)
1079 info64
[n
].crc
, skip_dot(info64
[n
].name
));
1083 error("%s: ELF class not recognized\n", filename
);
1088 /* Does path contain directory(s) subpath? */
1089 static int type_matches(const char *path
, const char *subpath
)
1091 char *subpath_with_slashes
;
1094 nofail_asprintf(&subpath_with_slashes
, "/%s/", subpath
);
1096 ret
= (strstr(path
, subpath_with_slashes
) != NULL
);
1097 free(subpath_with_slashes
);
1101 /* Careful! Don't munge - in [ ] as per Debian Bug#350915 */
1102 static char *underscores(char *string
)
1109 for (i
= 0; string
[i
]; i
++) {
1110 switch (string
[i
]) {
1116 warn("Unmatched bracket in %s\n", string
);
1120 i
+= strcspn(&string
[i
], "]");
1122 warn("Unmatched bracket in %s\n", string
);
1129 static int do_wildcard(const char *dirname
,
1131 const char *wildcard
)
1133 char *modules_dep_name
;
1137 /* Canonicalize wildcard */
1138 wcard
= strdup(wildcard
);
1141 nofail_asprintf(&modules_dep_name
, "%s/%s", dirname
, "modules.dep");
1142 modules_dep
= fopen(modules_dep_name
, "r");
1144 fatal("Could not load %s: %s\n",
1145 modules_dep_name
, strerror(errno
));
1147 while ((line
= getline_wrapped(modules_dep
, NULL
)) != NULL
) {
1150 /* Ignore lines without : or which start with a # */
1151 ptr
= strchr(line
, ':');
1152 if (ptr
== NULL
|| line
[strspn(line
, "\t ")] == '#')
1156 /* "type" must match complete directory component(s). */
1157 if (!type
|| type_matches(line
, type
)) {
1158 char modname
[strlen(line
)+1];
1160 filename2modname(modname
, line
);
1161 if (fnmatch(wcard
, modname
, 0) == 0)
1162 printf("%s\n", line
);
1168 free(modules_dep_name
);
1173 static char *strsep_skipspace(char **string
, char *delim
)
1177 *string
+= strspn(*string
, delim
);
1178 return strsep(string
, delim
);
1181 static int parse_config_scan(const char *filename
,
1185 struct module_options
**options
,
1186 struct module_command
**commands
,
1187 struct module_alias
**alias
,
1188 struct module_blacklist
**blacklist
);
1190 static int parse_config_file(const char *filename
,
1194 struct module_options
**options
,
1195 struct module_command
**commands
,
1196 struct module_alias
**aliases
,
1197 struct module_blacklist
**blacklist
)
1200 unsigned int linenum
= 0;
1203 cfile
= fopen(filename
, "r");
1207 while ((line
= getline_wrapped(cfile
, &linenum
)) != NULL
) {
1209 char *cmd
, *modname
;
1212 printf("%s\n", line
);
1214 cmd
= strsep_skipspace(&ptr
, "\t ");
1215 if (cmd
== NULL
|| cmd
[0] == '#' || cmd
[0] == '\0') {
1220 if (strcmp(cmd
, "alias") == 0) {
1221 char *wildcard
= strsep_skipspace(&ptr
, "\t ");
1222 char *realname
= strsep_skipspace(&ptr
, "\t ");
1224 if (!wildcard
|| !realname
)
1225 grammar(cmd
, filename
, linenum
);
1226 else if (fnmatch(underscores(wildcard
),name
,0) == 0)
1227 *aliases
= add_alias(underscores(realname
), *aliases
);
1228 } else if (strcmp(cmd
, "include") == 0) {
1229 struct module_alias
*newalias
= NULL
;
1232 newfilename
= strsep_skipspace(&ptr
, "\t ");
1234 grammar(cmd
, filename
, linenum
);
1236 warn("\"include %s\" is deprecated, "
1237 "please use /etc/modprobe.d\n", newfilename
);
1238 if (strncmp(newfilename
, "/etc/modprobe.d",
1239 strlen("/etc/modprobe.d")) == 0) {
1240 warn("\"include /etc/modprobe.d\" is "
1241 "the default, ignored\n");
1243 if (!parse_config_scan(newfilename
, name
,
1244 dump_only
, removing
,
1245 options
, commands
, &newalias
,
1247 warn("Failed to open included"
1248 " config file %s: %s\n",
1249 newfilename
, strerror(errno
));
1251 /* Files included override aliases,
1252 etc that was already set ... */
1254 *aliases
= newalias
;
1256 } else if (strcmp(cmd
, "options") == 0) {
1257 modname
= strsep_skipspace(&ptr
, "\t ");
1258 if (!modname
|| !ptr
)
1259 grammar(cmd
, filename
, linenum
);
1261 ptr
+= strspn(ptr
, "\t ");
1262 *options
= add_options(underscores(modname
),
1265 } else if (strcmp(cmd
, "install") == 0) {
1266 modname
= strsep_skipspace(&ptr
, "\t ");
1267 if (!modname
|| !ptr
)
1268 grammar(cmd
, filename
, linenum
);
1269 else if (!removing
) {
1270 ptr
+= strspn(ptr
, "\t ");
1271 *commands
= add_command(underscores(modname
),
1274 } else if (strcmp(cmd
, "blacklist") == 0) {
1275 modname
= strsep_skipspace(&ptr
, "\t ");
1277 grammar(cmd
, filename
, linenum
);
1278 else if (!removing
) {
1279 *blacklist
= add_blacklist(underscores(modname
),
1282 } else if (strcmp(cmd
, "remove") == 0) {
1283 modname
= strsep_skipspace(&ptr
, "\t ");
1284 if (!modname
|| !ptr
)
1285 grammar(cmd
, filename
, linenum
);
1286 else if (removing
) {
1287 ptr
+= strspn(ptr
, "\t ");
1288 *commands
= add_command(underscores(modname
),
1291 } else if (strcmp(cmd
, "config") == 0) {
1292 char *tmp
= strsep_skipspace(&ptr
, "\t ");
1293 if (strcmp(tmp
, "binary_indexes") == 0) {
1294 tmp
= strsep_skipspace(&ptr
, "\t ");
1295 if (strcmp(tmp
, "yes") == 0)
1296 use_binary_indexes
= 1;
1297 if (strcmp(tmp
, "no") == 0)
1298 use_binary_indexes
= 0;
1301 grammar(cmd
, filename
, linenum
);
1309 /* fallback to plain-text aliases file as necessary */
1310 static int read_aliases_file(const char *filename
,
1314 struct module_options
**options
,
1315 struct module_command
**commands
,
1316 struct module_alias
**aliases
,
1317 struct module_blacklist
**blacklist
)
1319 struct index_value
*realnames
;
1320 struct index_value
*realname
;
1322 struct index_file
*index
;
1324 if (!use_binary_indexes
)
1327 nofail_asprintf(&binfile
, "%s.bin", filename
);
1328 index
= index_file_open(binfile
);
1335 index_dump(index
, stdout
, "alias ");
1337 index_file_close(index
);
1341 realnames
= index_searchwild(index
, name
);
1342 for (realname
= realnames
; realname
; realname
= realname
->next
)
1343 *aliases
= add_alias(realname
->value
, *aliases
);
1344 index_values_free(realnames
);
1347 index_file_close(index
);
1351 return parse_config_file(filename
, name
, dump_only
, removing
,
1352 options
, commands
, aliases
, blacklist
);
1355 static int parse_config_scan(const char *filename
,
1359 struct module_options
**options
,
1360 struct module_command
**commands
,
1361 struct module_alias
**aliases
,
1362 struct module_blacklist
**blacklist
)
1367 dir
= opendir(filename
);
1370 struct list_head node
;
1373 LIST_HEAD(files_list
);
1374 struct file_entry
*fe
, *fe_tmp
;
1377 /* sort files from directory into list */
1378 while ((i
= readdir(dir
)) != NULL
) {
1381 if (i
->d_name
[0] == '.')
1383 if (!config_filter(i
->d_name
))
1386 len
= strlen(i
->d_name
);
1388 (strcmp(&i
->d_name
[len
-5], ".conf") != 0 &&
1389 strcmp(&i
->d_name
[len
-6], ".alias") != 0))
1390 warn("All config files need .conf: %s/%s, "
1391 "it will be ignored in a future release.\n",
1392 filename
, i
->d_name
);
1393 fe
= malloc(sizeof(struct file_entry
) + len
+ 1);
1396 strcpy(fe
->name
, i
->d_name
);
1397 list_for_each_entry(fe_tmp
, &files_list
, node
)
1398 if (strcmp(fe_tmp
->name
, fe
->name
) >= 0)
1400 list_add_tail(&fe
->node
, &fe_tmp
->node
);
1404 /* parse list of files */
1405 list_for_each_entry_safe(fe
, fe_tmp
, &files_list
, node
) {
1408 nofail_asprintf(&cfgfile
, "%s/%s", filename
, fe
->name
);
1409 if (!parse_config_file(cfgfile
, name
,
1410 dump_only
, removing
,
1412 aliases
, blacklist
))
1413 warn("Failed to open config file "
1414 "%s: %s\n", fe
->name
, strerror(errno
));
1416 list_del(&fe
->node
);
1422 if (parse_config_file(filename
, name
, dump_only
, removing
,
1423 options
, commands
, aliases
, blacklist
))
1429 /* Read binary index file containing aliases only */
1430 static void parse_toplevel_config(const char *filename
,
1434 struct module_options
**options
,
1435 struct module_command
**commands
,
1436 struct module_alias
**aliases
,
1437 struct module_blacklist
**blacklist
)
1440 if (!parse_config_scan(filename
, name
, dump_only
, removing
,
1441 options
, commands
, aliases
, blacklist
))
1442 fatal("Failed to open config file %s: %s\n",
1443 filename
, strerror(errno
));
1447 /* deprecated config file */
1448 if (parse_config_file("/etc/modprobe.conf", name
, dump_only
, removing
,
1449 options
, commands
, aliases
, blacklist
) > 0)
1450 warn("Deprecated config file /etc/modprobe.conf, "
1451 "all config files belong into /etc/modprobe.d/.\n");
1453 /* default config */
1454 parse_config_scan("/etc/modprobe.d", name
, dump_only
, removing
,
1455 options
, commands
, aliases
, blacklist
);
1458 /* Read possible module arguments from the kernel command line. */
1459 static int parse_kcmdline(int dump_only
, struct module_options
**options
)
1462 unsigned int linenum
= 0;
1465 kcmdline
= fopen("/proc/cmdline", "r");
1469 while ((line
= getline_wrapped(kcmdline
, &linenum
)) != NULL
) {
1473 while ((arg
= strsep_skipspace(&ptr
, "\t ")) != NULL
) {
1474 char *sep
, *modname
, *opt
;
1476 sep
= strchr(arg
, '.');
1478 if (!strchr(sep
, '='))
1485 printf("options %s %s\n", modname
, opt
);
1487 *options
= add_options(underscores(modname
),
1498 static void add_to_env_var(const char *option
)
1502 if ((oldenv
= getenv("MODPROBE_OPTIONS")) != NULL
) {
1504 nofail_asprintf(&newenv
, "%s %s", oldenv
, option
);
1505 setenv("MODPROBE_OPTIONS", newenv
, 1);
1507 setenv("MODPROBE_OPTIONS", option
, 1);
1510 /* Prepend options from environment. */
1511 static char **merge_args(char *args
, char *argv
[], int *argc
)
1513 char *arg
, *argstring
;
1514 char **newargs
= NULL
;
1515 unsigned int i
, num_env
= 0;
1520 argstring
= NOFAIL(strdup(args
));
1521 for (arg
= strtok(argstring
, " "); arg
; arg
= strtok(NULL
, " ")) {
1523 newargs
= NOFAIL(realloc(newargs
,
1525 * (num_env
+ *argc
+ 1)));
1526 newargs
[num_env
] = arg
;
1532 /* Append commandline args */
1533 newargs
[0] = argv
[0];
1534 for (i
= 1; i
<= *argc
; i
++)
1535 newargs
[num_env
+i
] = argv
[i
];
1541 static char *gather_options(char *argv
[])
1543 char *optstring
= NOFAIL(strdup(""));
1545 /* Rest is module options */
1547 /* Quote value if it contains spaces. */
1548 unsigned int eq
= strcspn(*argv
, "=");
1550 if (strchr(*argv
+eq
, ' ') && !strchr(*argv
, '"')) {
1551 char quoted
[strlen(*argv
) + 3];
1553 sprintf(quoted
, "%s=\"%s\"", *argv
, *argv
+eq
+1);
1554 optstring
= append_option(optstring
, quoted
);
1556 optstring
= append_option(optstring
, *argv
);
1562 static void handle_module(const char *modname
,
1563 struct list_head
*todo_list
,
1564 const char *newname
,
1571 struct module_options
*modoptions
,
1572 struct module_command
*commands
,
1573 int ignore_commands
,
1576 int strip_modversion
,
1578 const char *cmdline_opts
,
1581 if (list_empty(todo_list
)) {
1582 const char *command
;
1584 /* The dependencies have to be real modules, but
1585 handle case where the first is completely bogus. */
1586 command
= find_command(modname
, commands
);
1587 if (command
&& !ignore_commands
) {
1588 do_command(modname
, command
, verbose
, dry_run
, error
,
1589 remove
? "remove":"install", cmdline_opts
);
1595 error("Module %s not found.\n", modname
);
1600 rmmod(todo_list
, newname
, first_time
, error
, dry_run
, verbose
,
1601 commands
, ignore_commands
, 0, cmdline_opts
, flags
);
1603 insmod(todo_list
, NOFAIL(strdup(options
)), newname
,
1604 first_time
, error
, dry_run
, verbose
, modoptions
,
1605 commands
, ignore_commands
, ignore_proc
, strip_vermagic
,
1606 strip_modversion
, cmdline_opts
);
1609 static struct option options
[] = { { "verbose", 0, NULL
, 'v' },
1610 { "version", 0, NULL
, 'V' },
1611 { "config", 1, NULL
, 'C' },
1612 { "name", 1, NULL
, 'o' },
1613 { "remove", 0, NULL
, 'r' },
1614 { "wait", 0, NULL
, 'w' },
1615 { "showconfig", 0, NULL
, 'c' },
1616 { "autoclean", 0, NULL
, 'k' },
1617 { "quiet", 0, NULL
, 'q' },
1618 { "show", 0, NULL
, 'n' },
1619 { "dry-run", 0, NULL
, 'n' },
1620 { "syslog", 0, NULL
, 's' },
1621 { "type", 1, NULL
, 't' },
1622 { "list", 0, NULL
, 'l' },
1623 { "all", 0, NULL
, 'a' },
1624 { "ignore-install", 0, NULL
, 'i' },
1625 { "ignore-remove", 0, NULL
, 'i' },
1626 { "force", 0, NULL
, 'f' },
1627 { "force-vermagic", 0, NULL
, 1 },
1628 { "force-modversion", 0, NULL
, 2 },
1629 { "set-version", 1, NULL
, 'S' },
1630 { "show-depends", 0, NULL
, 'D' },
1631 { "dirname", 1, NULL
, 'd' },
1632 { "first-time", 0, NULL
, 3 },
1633 { "dump-modversions", 0, NULL
, 4 },
1634 { "use-blacklist", 0, NULL
, 'b' },
1635 { NULL
, 0, NULL
, 0 } };
1637 int main(int argc
, char *argv
[])
1640 struct stat statbuf
;
1646 int unknown_silent
= 0;
1649 int ignore_commands
= 0;
1650 int strip_vermagic
= 0;
1651 int strip_modversion
= 0;
1652 int ignore_proc
= 0;
1654 int dump_modver
= 0;
1655 int use_blacklist
= 0;
1656 unsigned int i
, num_modules
;
1658 const char *config
= NULL
;
1659 char *dirname
= NULL
;
1660 char *optstring
= NULL
;
1661 char *newname
= NULL
;
1662 char *aliasfilename
, *symfilename
;
1663 errfn_t error
= fatal
;
1664 int flags
= O_NONBLOCK
|O_EXCL
;
1666 /* Prepend options from environment. */
1667 argv
= merge_args(getenv("MODPROBE_OPTIONS"), argv
, &argc
);
1670 while ((opt
= getopt_long(argc
, argv
, "vVC:o:rknqQsclt:aifbwd:", options
, NULL
)) != -1){
1673 add_to_env_var("-v");
1677 puts(PACKAGE
" version " VERSION
);
1680 strncpy(buf
.release
, optarg
, sizeof(buf
.release
));
1681 buf
.release
[sizeof(buf
.release
)-1] = '\0';
1685 add_to_env_var("-C");
1686 add_to_env_var(config
);
1690 add_to_env_var("-q");
1696 add_to_env_var("-D");
1718 /* FIXME: This should actually do something */
1724 add_to_env_var("-s");
1728 ignore_commands
= 1;
1732 strip_modversion
= 1;
1738 flags
&= ~O_NONBLOCK
;
1741 nofail_asprintf(&dirname
, "%s/%s/%s", optarg
,
1742 MODULE_DIR
, buf
.release
);
1748 strip_modversion
= 1;
1757 print_usage(argv
[0]);
1761 /* If stderr not open, go to syslog */
1762 if (logging
|| fstat(STDERR_FILENO
, &statbuf
) != 0) {
1763 openlog("modprobe", LOG_CONS
, LOG_DAEMON
);
1767 if (argc
< optind
+ 1 && !dump_only
&& !list_only
&& !remove
)
1768 print_usage(argv
[0]);
1771 nofail_asprintf(&dirname
, "%s/%s", MODULE_DIR
, buf
.release
);
1772 nofail_asprintf(&aliasfilename
, "%s/modules.alias", dirname
);
1773 nofail_asprintf(&symfilename
, "%s/modules.symbols", dirname
);
1775 /* Old-style -t xxx wildcard? Only with -l. */
1777 if (optind
+1 < argc
)
1778 fatal("Can't have multiple wildcards\n");
1779 /* fprintf(stderr, "man find\n"); return 1; */
1780 return do_wildcard(dirname
, type
, argv
[optind
]?:"*");
1783 fatal("-t only supported with -l");
1786 struct module_command
*commands
= NULL
;
1787 struct module_options
*modoptions
= NULL
;
1788 struct module_alias
*aliases
= NULL
;
1789 struct module_blacklist
*blacklist
= NULL
;
1791 parse_toplevel_config(config
, "", 1, 0, &modoptions
,
1792 &commands
, &aliases
, &blacklist
);
1793 /* Read module options from kernel command line */
1794 parse_kcmdline(1, &modoptions
);
1795 parse_config_file(aliasfilename
, "", 1, 0, &modoptions
,
1796 &commands
, &aliases
, &blacklist
);
1797 parse_config_file(symfilename
, "", 1, 0, &modoptions
,
1798 &commands
, &aliases
, &blacklist
);
1802 if (remove
|| all
) {
1803 num_modules
= argc
- optind
;
1804 optstring
= NOFAIL(strdup(""));
1807 optstring
= gather_options(argv
+optind
+1);
1810 /* num_modules is always 1 except for -r or -a. */
1811 for (i
= 0; i
< num_modules
; i
++) {
1812 struct module_command
*commands
= NULL
;
1813 struct module_options
*modoptions
= NULL
;
1814 struct module_alias
*aliases
= NULL
;
1815 struct module_blacklist
*blacklist
= NULL
;
1817 char *modulearg
= argv
[optind
+ i
];
1820 dump_modversions(modulearg
, error
);
1824 /* Convert name we are looking for */
1825 underscores(modulearg
);
1827 /* Returns the resolved alias, options */
1828 parse_toplevel_config(config
, modulearg
, 0,
1829 remove
, &modoptions
, &commands
, &aliases
, &blacklist
);
1831 /* Read module options from kernel command line */
1832 parse_kcmdline(0, &modoptions
);
1834 /* No luck? Try symbol names, if starts with symbol:. */
1836 strncmp(modulearg
, "symbol:", strlen("symbol:")) == 0) {
1837 parse_config_file(symfilename
, modulearg
, 0,
1838 remove
, &modoptions
, &commands
,
1839 &aliases
, &blacklist
);
1842 if(!strchr(modulearg
, ':'))
1843 read_depends(dirname
, modulearg
, &list
);
1845 /* We only use canned aliases as last resort. */
1846 if (list_empty(&list
)
1847 && !find_command(modulearg
, commands
))
1849 read_aliases_file(aliasfilename
,
1850 modulearg
, 0, remove
,
1851 &modoptions
, &commands
,
1852 &aliases
, &blacklist
);
1856 aliases
= apply_blacklist(aliases
, blacklist
);
1858 errfn_t err
= error
;
1860 /* More than one alias? Don't bail out on failure. */
1864 /* Add the options for this alias. */
1865 char *opts
= NOFAIL(strdup(optstring
));
1866 opts
= add_extra_options(modulearg
,
1869 read_depends(dirname
, aliases
->module
, &list
);
1870 handle_module(aliases
->module
, &list
, newname
,
1871 remove
, opts
, first_time
, err
,
1872 dry_run
, verbose
, modoptions
,
1873 commands
, ignore_commands
,
1874 ignore_proc
, strip_vermagic
,
1879 aliases
= aliases
->next
;
1880 INIT_LIST_HEAD(&list
);
1884 && find_blacklist(modulearg
, blacklist
))
1887 handle_module(modulearg
, &list
, newname
, remove
,
1888 optstring
, first_time
, error
, dry_run
,
1889 verbose
, modoptions
, commands
,
1890 ignore_commands
, ignore_proc
,
1891 strip_vermagic
, strip_modversion
,
1892 unknown_silent
, optstring
, flags
);
1899 free(aliasfilename
);