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 /* We used to lock with a write flock but that allows regular users to block
165 * module load by having a read lock on the module file (no way to bust the
166 * existing locks without killing the offending process). Instead, we now
167 * do the system call/init_module and allow the kernel to fail us instead.
169 static int open_file(const char *filename
)
171 int fd
= open(filename
, O_RDONLY
, 0);
176 static void close_file(int fd
)
178 /* Valgrind is picky... */
182 static void add_module(char *filename
, int namelen
, struct list_head
*list
)
186 /* If it's a duplicate: move it to the end, so it gets
187 inserted where it is *first* required. */
188 mod
= find_module(filename
, list
);
190 list_del(&mod
->list
);
192 /* No match. Create a new module. */
193 mod
= NOFAIL(malloc(sizeof(struct module
) + namelen
+ 1));
194 memcpy(mod
->filename
, filename
, namelen
);
195 mod
->filename
[namelen
] = '\0';
196 mod
->modname
= NOFAIL(malloc(namelen
+ 1));
197 filename2modname(mod
->modname
, mod
->filename
);
200 list_add_tail(&mod
->list
, list
);
203 /* Compare len chars of a to b, with _ and - equivalent. */
204 static int modname_equal(const char *a
, const char *b
, unsigned int len
)
208 if (strlen(b
) != len
)
211 for (i
= 0; i
< len
; i
++) {
212 if ((a
[i
] == '_' || a
[i
] == '-')
213 && (b
[i
] == '_' || b
[i
] == '-'))
221 /* Fills in list of modules if this is the line we want. */
222 static int add_modules_dep_line(char *line
,
224 struct list_head
*list
,
229 char *modname
, *fullpath
;
231 /* Ignore lines without : or which start with a # */
232 ptr
= strchr(line
, ':');
233 if (ptr
== NULL
|| line
[strspn(line
, "\t ")] == '#')
236 /* Is this the module we are looking for? */
238 if (strrchr(line
, '/'))
239 modname
= strrchr(line
, '/') + 1;
243 len
= strlen(modname
);
244 if (strchr(modname
, '.'))
245 len
= strchr(modname
, '.') - modname
;
246 if (!modname_equal(modname
, name
, len
))
249 /* Create the list. */
250 if ('/' == line
[0]) { /* old style deps - absolute path specified */
251 add_module(line
, ptr
- line
, list
);
253 nofail_asprintf(&fullpath
, "%s/%s", dirname
, line
);
254 add_module(fullpath
, strlen(dirname
)+1+(ptr
- line
), list
);
261 ptr
+= strspn(ptr
, " \t");
265 ptr
+= strcspn(ptr
, " \t");
266 if ('/' == dep_start
[0]) { /* old style deps */
267 add_module(dep_start
, ptr
- dep_start
, list
);
269 nofail_asprintf(&fullpath
, "%s/%s", dirname
, dep_start
);
271 strlen(dirname
)+1+(ptr
- dep_start
), list
);
278 static int read_depends_file(const char *dirname
,
279 const char *start_name
,
280 struct list_head
*list
)
282 char *modules_dep_name
;
284 struct index_file
*modules_dep
;
286 nofail_asprintf(&modules_dep_name
, "%s/%s", dirname
, "modules.dep.bin");
287 modules_dep
= index_file_open(modules_dep_name
);
289 free(modules_dep_name
);
293 line
= index_search(modules_dep
, start_name
);
295 /* Value is standard dependency line format */
296 if (!add_modules_dep_line(line
, start_name
, list
, dirname
))
297 fatal("Module index is inconsistent\n");
301 index_file_close(modules_dep
);
302 free(modules_dep_name
);
307 static void read_depends(const char *dirname
,
308 const char *start_name
,
309 struct list_head
*list
)
311 char *modules_dep_name
;
316 if (use_binary_indexes
)
317 if (read_depends_file(dirname
, start_name
, list
))
320 nofail_asprintf(&modules_dep_name
, "%s/%s", dirname
, "modules.dep");
321 modules_dep
= fopen(modules_dep_name
, "r");
323 fatal("Could not load %s: %s\n",
324 modules_dep_name
, strerror(errno
));
326 /* Stop at first line, as we can have duplicates (eg. symlinks
328 while (!done
&& (line
= getline_wrapped(modules_dep
, NULL
)) != NULL
) {
329 done
= add_modules_dep_line(line
, start_name
, list
, dirname
);
333 free(modules_dep_name
);
336 /* We use error numbers in a loose translation... */
337 static const char *insert_moderror(int err
)
341 return "Invalid module format";
343 return "Unknown symbol in module, or unknown parameter (see dmesg)";
345 return "Kernel does not have module support";
347 return strerror(err
);
351 static const char *remove_moderror(int err
)
355 return "No such module";
357 return "Kernel does not have module unloading support";
359 return strerror(err
);
363 static void replace_modname(struct module
*module
,
364 void *mem
, unsigned long len
,
365 const char *oldname
, const char *newname
)
369 /* 64 - sizeof(unsigned long) - 1 */
370 if (strlen(newname
) > 55)
371 fatal("New name %s is too long\n", newname
);
373 /* Find where it is in the module structure. Don't assume layout! */
374 for (p
= mem
; p
< (char *)mem
+ len
- strlen(oldname
); p
++) {
375 if (memcmp(p
, oldname
, strlen(oldname
)) == 0) {
381 warn("Could not find old name in %s to replace!\n", module
->filename
);
384 static void *get_section32(void *file
,
387 unsigned long *secsize
)
389 Elf32_Ehdr
*hdr
= file
;
390 Elf32_Shdr
*sechdrs
= file
+ hdr
->e_shoff
;
391 const char *secnames
;
395 if (size
< sizeof(*hdr
))
397 if (size
< hdr
->e_shoff
+ hdr
->e_shnum
* sizeof(sechdrs
[0]))
399 if (size
< sechdrs
[hdr
->e_shstrndx
].sh_offset
)
402 secnames
= file
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
403 for (i
= 1; i
< hdr
->e_shnum
; i
++)
404 if (strcmp(secnames
+ sechdrs
[i
].sh_name
, name
) == 0) {
405 *secsize
= sechdrs
[i
].sh_size
;
406 return file
+ sechdrs
[i
].sh_offset
;
411 static void *get_section64(void *file
,
414 unsigned long *secsize
)
416 Elf64_Ehdr
*hdr
= file
;
417 Elf64_Shdr
*sechdrs
= file
+ hdr
->e_shoff
;
418 const char *secnames
;
422 if (size
< sizeof(*hdr
))
424 if (size
< hdr
->e_shoff
+ hdr
->e_shnum
* sizeof(sechdrs
[0]))
426 if (size
< sechdrs
[hdr
->e_shstrndx
].sh_offset
)
429 secnames
= file
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
430 for (i
= 1; i
< hdr
->e_shnum
; i
++)
431 if (strcmp(secnames
+ sechdrs
[i
].sh_name
, name
) == 0) {
432 *secsize
= sechdrs
[i
].sh_size
;
433 return file
+ sechdrs
[i
].sh_offset
;
438 static int elf_ident(void *mod
, unsigned long size
)
440 /* "\177ELF" <byte> where byte = 001 for 32-bit, 002 for 64 */
443 if (size
< EI_CLASS
|| memcmp(mod
, ELFMAG
, SELFMAG
) != 0)
445 return ident
[EI_CLASS
];
448 static void *get_section(void *file
,
451 unsigned long *secsize
)
453 switch (elf_ident(file
, size
)) {
455 return get_section32(file
, size
, name
, secsize
);
457 return get_section64(file
, size
, name
, secsize
);
463 static void rename_module(struct module
*module
,
469 unsigned long modstruct_len
;
472 modstruct
= get_section(mod
, len
, ".gnu.linkonce.this_module",
476 modstruct
= get_section(mod
, len
, "__module", &modstruct_len
);
478 warn("Could not find module name to change in %s\n",
481 replace_modname(module
, modstruct
, modstruct_len
,
482 module
->modname
, newname
);
485 /* Kernel told to ignore these sections if SHF_ALLOC not set. */
486 static void invalidate_section32(void *mod
, const char *secname
)
488 Elf32_Ehdr
*hdr
= mod
;
489 Elf32_Shdr
*sechdrs
= mod
+ hdr
->e_shoff
;
490 const char *secnames
= mod
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
493 for (i
= 1; i
< hdr
->e_shnum
; i
++)
494 if (strcmp(secnames
+sechdrs
[i
].sh_name
, secname
) == 0)
495 sechdrs
[i
].sh_flags
&= ~SHF_ALLOC
;
498 static void invalidate_section64(void *mod
, const char *secname
)
500 Elf64_Ehdr
*hdr
= mod
;
501 Elf64_Shdr
*sechdrs
= mod
+ hdr
->e_shoff
;
502 const char *secnames
= mod
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
505 for (i
= 1; i
< hdr
->e_shnum
; i
++)
506 if (strcmp(secnames
+sechdrs
[i
].sh_name
, secname
) == 0)
507 sechdrs
[i
].sh_flags
&= ~(unsigned long long)SHF_ALLOC
;
510 static void strip_section(struct module
*module
,
515 switch (elf_ident(mod
, len
)) {
517 invalidate_section32(mod
, secname
);
520 invalidate_section64(mod
, secname
);
523 warn("Unknown module format in %s: not forcing version\n",
528 static const char *next_string(const char *string
, unsigned long *secsize
)
530 /* Skip non-zero chars */
533 if ((*secsize
)-- <= 1)
537 /* Skip any zero padding. */
540 if ((*secsize
)-- <= 1)
546 static void clear_magic(struct module
*module
, void *mod
, unsigned long len
)
549 unsigned long modlen
;
551 /* Old-style: __vermagic section */
552 strip_section(module
, mod
, len
, "__vermagic");
554 /* New-style: in .modinfo section */
555 for (p
= get_section(mod
, len
, ".modinfo", &modlen
);
557 p
= next_string(p
, &modlen
)) {
558 if (strncmp(p
, "vermagic=", strlen("vermagic=")) == 0) {
559 memset((char *)p
, 0, strlen(p
));
565 struct module_options
567 struct module_options
*next
;
572 struct module_command
574 struct module_command
*next
;
581 struct module_alias
*next
;
585 struct module_blacklist
587 struct module_blacklist
*next
;
591 /* Link in a new option line from the config file. */
592 static struct module_options
*
593 add_options(const char *modname
,
595 struct module_options
*options
)
597 struct module_options
*new;
600 new = NOFAIL(malloc(sizeof(*new)));
601 new->modulename
= NOFAIL(strdup(modname
));
602 new->options
= NOFAIL(strdup(option
));
603 /* We can handle tabs, kernel can't. */
604 for (tab
= strchr(new->options
, '\t'); tab
; tab
= strchr(tab
, '\t'))
610 /* Link in a new install line from the config file. */
611 static struct module_command
*
612 add_command(const char *modname
,
614 struct module_command
*commands
)
616 struct module_command
*new;
618 new = NOFAIL(malloc(sizeof(*new)));
619 new->modulename
= NOFAIL(strdup(modname
));
620 new->command
= NOFAIL(strdup(command
));
621 new->next
= commands
;
625 /* Link in a new alias line from the config file. */
626 static struct module_alias
*
627 add_alias(const char *modname
, struct module_alias
*aliases
)
629 struct module_alias
*new;
631 new = NOFAIL(malloc(sizeof(*new)));
632 new->module
= NOFAIL(strdup(modname
));
637 /* Link in a new blacklist line from the config file. */
638 static struct module_blacklist
*
639 add_blacklist(const char *modname
, struct module_blacklist
*blacklist
)
641 struct module_blacklist
*new;
643 new = NOFAIL(malloc(sizeof(*new)));
644 new->modulename
= NOFAIL(strdup(modname
));
645 new->next
= blacklist
;
649 /* Find blacklist commands if any. */
651 find_blacklist(const char *modname
, const struct module_blacklist
*blacklist
)
654 if (strcmp(blacklist
->modulename
, modname
) == 0)
656 blacklist
= blacklist
->next
;
661 /* return a new alias list, with backlisted elems filtered out */
662 static struct module_alias
*
663 apply_blacklist(const struct module_alias
*aliases
,
664 const struct module_blacklist
*blacklist
)
666 struct module_alias
*result
= NULL
;
668 char *modname
= aliases
->module
;
669 if (!find_blacklist(modname
, blacklist
))
670 result
= add_alias(modname
, result
);
671 aliases
= aliases
->next
;
676 /* Find install commands if any. */
677 static const char *find_command(const char *modname
,
678 const struct module_command
*commands
)
681 if (fnmatch(commands
->modulename
, modname
, 0) == 0)
682 return commands
->command
;
683 commands
= commands
->next
;
688 static char *append_option(char *options
, const char *newoption
)
690 options
= NOFAIL(realloc(options
, strlen(options
) + 1
691 + strlen(newoption
) + 1));
692 if (strlen(options
)) strcat(options
, " ");
693 strcat(options
, newoption
);
697 static char *prepend_option(char *options
, const char *newoption
)
700 l1
= strlen(options
);
701 l2
= strlen(newoption
);
702 /* the resulting string will look like
703 * newoption + ' ' + options + '\0' */
705 options
= NOFAIL(realloc(options
, l2
+ 1 + l1
+ 1));
706 memmove(options
+ l2
+ 1, options
, l1
+ 1);
708 memcpy(options
, newoption
, l2
);
710 options
= NOFAIL(realloc(options
, l2
+ 1));
711 memcpy(options
, newoption
, l2
);
718 static char *add_extra_options(const char *modname
,
720 const struct module_options
*options
)
723 if (strcmp(options
->modulename
, modname
) == 0)
724 optstring
= prepend_option(optstring
, options
->options
);
725 options
= options
->next
;
730 /* Read sysfs attribute into a buffer.
731 * returns: 1 = ok, 0 = attribute missing,
732 * -1 = file error (or empty file, but we don't care).
734 static int read_attribute(const char *filename
, char *buf
, size_t buflen
)
739 file
= fopen(filename
, "r");
741 return (errno
== ENOENT
) ? 0 : -1;
742 s
= fgets(buf
, buflen
, file
);
745 return (s
== NULL
) ? -1 : 1;
748 /* Is module in /sys/module? If so, fill in usecount if not NULL.
749 0 means no, 1 means yes, -1 means unknown.
751 static int module_in_kernel(const char *modname
, unsigned int *usecount
)
757 const int ATTR_LEN
= 16;
760 /* Check sysfs is mounted */
761 if (stat("/sys/module", &finfo
) < 0)
765 nofail_asprintf(&name
, "/sys/module/%s", modname
);
766 ret
= stat(name
, &finfo
);
769 return (errno
== ENOENT
) ? 0 : -1; /* Not found or unknown. */
771 /* Wait for the existing module to either go live or disappear. */
772 nofail_asprintf(&name
, "/sys/module/%s/initstate", modname
);
774 ret
= read_attribute(name
, attr
, ATTR_LEN
);
775 if (ret
!= 1 || streq(attr
, "live\n"))
785 /* Get reference count, if it exists. */
786 if (usecount
!= NULL
) {
787 nofail_asprintf(&name
, "/sys/module/%s/refcnt", modname
);
788 ret
= read_attribute(name
, attr
, ATTR_LEN
);
791 *usecount
= atoi(attr
);
797 /* If we don't flush, then child processes print before we do */
798 static void verbose_printf(int verbose
, const char *fmt
, ...)
803 va_start(arglist
, fmt
);
804 vprintf(fmt
, arglist
);
810 /* Do an install/remove command: replace $CMDLINE_OPTS if it's specified. */
811 static void do_command(const char *modname
,
813 int verbose
, int dry_run
,
816 const char *cmdline_opts
)
819 char *p
, *replaced_cmd
= NOFAIL(strdup(command
));
821 while ((p
= strstr(replaced_cmd
, "$CMDLINE_OPTS")) != NULL
) {
823 nofail_asprintf(&new, "%.*s%s%s",
824 (int)(p
- replaced_cmd
), replaced_cmd
, cmdline_opts
,
825 p
+ strlen("$CMDLINE_OPTS"));
830 verbose_printf(verbose
, "%s %s\n", type
, replaced_cmd
);
834 setenv("MODPROBE_MODULE", modname
, 1);
835 ret
= system(replaced_cmd
);
836 if (ret
== -1 || WEXITSTATUS(ret
))
837 error("Error running %s command for %s\n", type
, modname
);
841 /* Actually do the insert. Frees second arg. */
842 static void insmod(struct list_head
*list
,
849 const struct module_options
*options
,
850 const struct module_command
*commands
,
854 int strip_modversion
,
855 const char *cmdline_opts
)
861 struct module
*mod
= list_entry(list
->next
, struct module
, list
);
863 /* Take us off the list. */
864 list_del(&mod
->list
);
866 /* Do things we (or parent) depend on first, but don't die if
868 if (!list_empty(list
)) {
869 insmod(list
, NOFAIL(strdup("")), NULL
, 0, warn
,
870 dry_run
, verbose
, options
, commands
, 0, ignore_proc
,
871 strip_vermagic
, strip_modversion
, "");
874 fd
= open_file(mod
->filename
);
876 error("Could not open '%s': %s\n",
877 mod
->filename
, strerror(errno
));
881 /* Don't do ANYTHING if already in kernel. */
883 && module_in_kernel(newname
?: mod
->modname
, NULL
) == 1) {
885 error("Module %s already in kernel.\n",
886 newname
?: mod
->modname
);
890 command
= find_command(mod
->modname
, commands
);
891 if (command
&& !ignore_commands
) {
893 do_command(mod
->modname
, command
, verbose
, dry_run
, error
,
894 "install", cmdline_opts
);
898 map
= grab_fd(fd
, &len
);
900 error("Could not read '%s': %s\n",
901 mod
->filename
, strerror(errno
));
907 rename_module(mod
, map
, len
, newname
);
909 if (strip_modversion
)
910 strip_section(mod
, map
, len
, "__versions");
912 clear_magic(mod
, map
, len
);
914 /* Config file might have given more options */
915 optstring
= add_extra_options(mod
->modname
, optstring
, options
);
917 verbose_printf(verbose
, "insmod %s %s\n", mod
->filename
, optstring
);
922 ret
= init_module(map
, len
, optstring
);
924 if (errno
== EEXIST
) {
926 error("Module %s already in kernel.\n",
927 newname
?: mod
->modname
);
930 /* don't warn noisely if we're loading multiple aliases. */
931 /* one of the aliases may try to use hardware we don't have. */
932 if ((error
!= warn
) || (verbose
))
933 error("Error inserting %s (%s): %s\n",
934 mod
->modname
, mod
->filename
,
935 insert_moderror(errno
));
938 release_file(map
, len
);
946 /* Do recursive removal. */
947 static void rmmod(struct list_head
*list
,
953 struct module_command
*commands
,
956 const char *cmdline_opts
,
960 unsigned int usecount
= 0;
961 struct module
*mod
= list_entry(list
->next
, struct module
, list
);
963 /* Take first one off the list. */
964 list_del(&mod
->list
);
969 /* Even if renamed, find commands to orig. name. */
970 command
= find_command(mod
->modname
, commands
);
971 if (command
&& !ignore_commands
) {
972 do_command(mod
->modname
, command
, verbose
, dry_run
, error
,
973 "remove", cmdline_opts
);
977 if (module_in_kernel(name
, &usecount
) == 0)
978 goto nonexistent_module
;
982 error("Module %s is in use.\n", name
);
986 verbose_printf(verbose
, "rmmod %s\n", mod
->filename
);
991 if (delete_module(name
, O_EXCL
) != 0) {
993 goto nonexistent_module
;
994 error("Error removing %s (%s): %s\n",
996 remove_moderror(errno
));
1000 /* Now do things we depend. */
1001 if (!list_empty(list
))
1002 rmmod(list
, NULL
, 0, warn
, dry_run
, verbose
, commands
,
1008 fatal("Module %s is not in kernel.\n", mod
->modname
);
1012 struct modver32_info
1015 char name
[64 - sizeof(uint32_t)];
1018 struct modver64_info
1021 char name
[64 - sizeof(uint64_t)];
1024 const char *skip_dot(const char *str
)
1026 /* For our purposes, .foo matches foo. PPC64 needs this. */
1027 if (str
&& str
[0] == '.')
1032 void dump_modversions(const char *filename
, errfn_t error
)
1034 unsigned long size
, secsize
;
1035 void *file
= grab_file(filename
, &size
);
1036 struct modver32_info
*info32
;
1037 struct modver64_info
*info64
;
1041 error("%s: %s\n", filename
, strerror(errno
));
1044 switch (elf_ident(file
, size
)) {
1046 info32
= get_section32(file
, size
, "__versions", &secsize
);
1048 return; /* Does not seem to be a kernel module */
1049 if (secsize
% sizeof(struct modver32_info
))
1050 error("Wrong section size in %s\n", filename
);
1051 for (n
= 0; n
< secsize
/ sizeof(struct modver32_info
); n
++)
1052 printf("0x%08lx\t%s\n", (unsigned long)
1053 info32
[n
].crc
, skip_dot(info32
[n
].name
));
1057 info64
= get_section64(file
, size
, "__versions", &secsize
);
1059 return; /* Does not seem to be a kernel module */
1060 if (secsize
% sizeof(struct modver64_info
))
1061 error("Wrong section size in %s\n", filename
);
1062 for (n
= 0; n
< secsize
/ sizeof(struct modver64_info
); n
++)
1063 printf("0x%08llx\t%s\n", (unsigned long long)
1064 info64
[n
].crc
, skip_dot(info64
[n
].name
));
1068 error("%s: ELF class not recognized\n", filename
);
1073 /* Does path contain directory(s) subpath? */
1074 static int type_matches(const char *path
, const char *subpath
)
1076 char *subpath_with_slashes
;
1079 nofail_asprintf(&subpath_with_slashes
, "/%s/", subpath
);
1081 ret
= (strstr(path
, subpath_with_slashes
) != NULL
);
1082 free(subpath_with_slashes
);
1086 /* Careful! Don't munge - in [ ] as per Debian Bug#350915 */
1087 static char *underscores(char *string
)
1094 for (i
= 0; string
[i
]; i
++) {
1095 switch (string
[i
]) {
1101 warn("Unmatched bracket in %s\n", string
);
1105 i
+= strcspn(&string
[i
], "]");
1107 warn("Unmatched bracket in %s\n", string
);
1114 static int do_wildcard(const char *dirname
,
1116 const char *wildcard
)
1118 char *modules_dep_name
;
1122 /* Canonicalize wildcard */
1123 wcard
= strdup(wildcard
);
1126 nofail_asprintf(&modules_dep_name
, "%s/%s", dirname
, "modules.dep");
1127 modules_dep
= fopen(modules_dep_name
, "r");
1129 fatal("Could not load %s: %s\n",
1130 modules_dep_name
, strerror(errno
));
1132 while ((line
= getline_wrapped(modules_dep
, NULL
)) != NULL
) {
1135 /* Ignore lines without : or which start with a # */
1136 ptr
= strchr(line
, ':');
1137 if (ptr
== NULL
|| line
[strspn(line
, "\t ")] == '#')
1141 /* "type" must match complete directory component(s). */
1142 if (!type
|| type_matches(line
, type
)) {
1143 char modname
[strlen(line
)+1];
1145 filename2modname(modname
, line
);
1146 if (fnmatch(wcard
, modname
, 0) == 0)
1147 printf("%s\n", line
);
1153 free(modules_dep_name
);
1158 static char *strsep_skipspace(char **string
, char *delim
)
1162 *string
+= strspn(*string
, delim
);
1163 return strsep(string
, delim
);
1166 static int parse_config_scan(const char *filename
,
1170 struct module_options
**options
,
1171 struct module_command
**commands
,
1172 struct module_alias
**alias
,
1173 struct module_blacklist
**blacklist
);
1175 static int parse_config_file(const char *filename
,
1179 struct module_options
**options
,
1180 struct module_command
**commands
,
1181 struct module_alias
**aliases
,
1182 struct module_blacklist
**blacklist
)
1185 unsigned int linenum
= 0;
1188 cfile
= fopen(filename
, "r");
1192 while ((line
= getline_wrapped(cfile
, &linenum
)) != NULL
) {
1194 char *cmd
, *modname
;
1197 printf("%s\n", line
);
1199 cmd
= strsep_skipspace(&ptr
, "\t ");
1200 if (cmd
== NULL
|| cmd
[0] == '#' || cmd
[0] == '\0') {
1205 if (strcmp(cmd
, "alias") == 0) {
1206 char *wildcard
= strsep_skipspace(&ptr
, "\t ");
1207 char *realname
= strsep_skipspace(&ptr
, "\t ");
1209 if (!wildcard
|| !realname
)
1210 grammar(cmd
, filename
, linenum
);
1211 else if (fnmatch(underscores(wildcard
),name
,0) == 0)
1212 *aliases
= add_alias(underscores(realname
), *aliases
);
1213 } else if (strcmp(cmd
, "include") == 0) {
1214 struct module_alias
*newalias
= NULL
;
1217 newfilename
= strsep_skipspace(&ptr
, "\t ");
1219 grammar(cmd
, filename
, linenum
);
1221 warn("\"include %s\" is deprecated, "
1222 "please use /etc/modprobe.d\n", newfilename
);
1223 if (strncmp(newfilename
, "/etc/modprobe.d",
1224 strlen("/etc/modprobe.d")) == 0) {
1225 warn("\"include /etc/modprobe.d\" is "
1226 "the default, ignored\n");
1228 if (!parse_config_scan(newfilename
, name
,
1229 dump_only
, removing
,
1230 options
, commands
, &newalias
,
1232 warn("Failed to open included"
1233 " config file %s: %s\n",
1234 newfilename
, strerror(errno
));
1236 /* Files included override aliases,
1237 etc that was already set ... */
1239 *aliases
= newalias
;
1241 } else if (strcmp(cmd
, "options") == 0) {
1242 modname
= strsep_skipspace(&ptr
, "\t ");
1243 if (!modname
|| !ptr
)
1244 grammar(cmd
, filename
, linenum
);
1246 ptr
+= strspn(ptr
, "\t ");
1247 *options
= add_options(underscores(modname
),
1250 } else if (strcmp(cmd
, "install") == 0) {
1251 modname
= strsep_skipspace(&ptr
, "\t ");
1252 if (!modname
|| !ptr
)
1253 grammar(cmd
, filename
, linenum
);
1254 else if (!removing
) {
1255 ptr
+= strspn(ptr
, "\t ");
1256 *commands
= add_command(underscores(modname
),
1259 } else if (strcmp(cmd
, "blacklist") == 0) {
1260 modname
= strsep_skipspace(&ptr
, "\t ");
1262 grammar(cmd
, filename
, linenum
);
1263 else if (!removing
) {
1264 *blacklist
= add_blacklist(underscores(modname
),
1267 } else if (strcmp(cmd
, "remove") == 0) {
1268 modname
= strsep_skipspace(&ptr
, "\t ");
1269 if (!modname
|| !ptr
)
1270 grammar(cmd
, filename
, linenum
);
1271 else if (removing
) {
1272 ptr
+= strspn(ptr
, "\t ");
1273 *commands
= add_command(underscores(modname
),
1276 } else if (strcmp(cmd
, "config") == 0) {
1277 char *tmp
= strsep_skipspace(&ptr
, "\t ");
1278 if (strcmp(tmp
, "binary_indexes") == 0) {
1279 tmp
= strsep_skipspace(&ptr
, "\t ");
1280 if (strcmp(tmp
, "yes") == 0)
1281 use_binary_indexes
= 1;
1282 if (strcmp(tmp
, "no") == 0)
1283 use_binary_indexes
= 0;
1286 grammar(cmd
, filename
, linenum
);
1294 /* fallback to plain-text aliases file as necessary */
1295 static int read_aliases_file(const char *filename
,
1299 struct module_options
**options
,
1300 struct module_command
**commands
,
1301 struct module_alias
**aliases
,
1302 struct module_blacklist
**blacklist
)
1304 struct index_value
*realnames
;
1305 struct index_value
*realname
;
1307 struct index_file
*index
;
1309 if (!use_binary_indexes
)
1312 nofail_asprintf(&binfile
, "%s.bin", filename
);
1313 index
= index_file_open(binfile
);
1320 index_dump(index
, stdout
, "alias ");
1322 index_file_close(index
);
1326 realnames
= index_searchwild(index
, name
);
1327 for (realname
= realnames
; realname
; realname
= realname
->next
)
1328 *aliases
= add_alias(realname
->value
, *aliases
);
1329 index_values_free(realnames
);
1332 index_file_close(index
);
1336 return parse_config_file(filename
, name
, dump_only
, removing
,
1337 options
, commands
, aliases
, blacklist
);
1340 static int parse_config_scan(const char *filename
,
1344 struct module_options
**options
,
1345 struct module_command
**commands
,
1346 struct module_alias
**aliases
,
1347 struct module_blacklist
**blacklist
)
1352 dir
= opendir(filename
);
1355 struct list_head node
;
1358 LIST_HEAD(files_list
);
1359 struct file_entry
*fe
, *fe_tmp
;
1362 /* sort files from directory into list */
1363 while ((i
= readdir(dir
)) != NULL
) {
1366 if (i
->d_name
[0] == '.')
1368 if (!config_filter(i
->d_name
))
1371 len
= strlen(i
->d_name
);
1373 (strcmp(&i
->d_name
[len
-5], ".conf") != 0 &&
1374 strcmp(&i
->d_name
[len
-6], ".alias") != 0))
1375 warn("All config files need .conf: %s/%s, "
1376 "it will be ignored in a future release.\n",
1377 filename
, i
->d_name
);
1378 fe
= malloc(sizeof(struct file_entry
) + len
+ 1);
1381 strcpy(fe
->name
, i
->d_name
);
1382 list_for_each_entry(fe_tmp
, &files_list
, node
)
1383 if (strcmp(fe_tmp
->name
, fe
->name
) >= 0)
1385 list_add_tail(&fe
->node
, &fe_tmp
->node
);
1389 /* parse list of files */
1390 list_for_each_entry_safe(fe
, fe_tmp
, &files_list
, node
) {
1393 nofail_asprintf(&cfgfile
, "%s/%s", filename
, fe
->name
);
1394 if (!parse_config_file(cfgfile
, name
,
1395 dump_only
, removing
,
1397 aliases
, blacklist
))
1398 warn("Failed to open config file "
1399 "%s: %s\n", fe
->name
, strerror(errno
));
1401 list_del(&fe
->node
);
1407 if (parse_config_file(filename
, name
, dump_only
, removing
,
1408 options
, commands
, aliases
, blacklist
))
1414 /* Read binary index file containing aliases only */
1415 static void parse_toplevel_config(const char *filename
,
1419 struct module_options
**options
,
1420 struct module_command
**commands
,
1421 struct module_alias
**aliases
,
1422 struct module_blacklist
**blacklist
)
1425 if (!parse_config_scan(filename
, name
, dump_only
, removing
,
1426 options
, commands
, aliases
, blacklist
))
1427 fatal("Failed to open config file %s: %s\n",
1428 filename
, strerror(errno
));
1432 /* deprecated config file */
1433 if (parse_config_file("/etc/modprobe.conf", name
, dump_only
, removing
,
1434 options
, commands
, aliases
, blacklist
) > 0)
1435 warn("Deprecated config file /etc/modprobe.conf, "
1436 "all config files belong into /etc/modprobe.d/.\n");
1438 /* default config */
1439 parse_config_scan("/etc/modprobe.d", name
, dump_only
, removing
,
1440 options
, commands
, aliases
, blacklist
);
1443 /* Read possible module arguments from the kernel command line. */
1444 static int parse_kcmdline(int dump_only
, struct module_options
**options
)
1447 unsigned int linenum
= 0;
1450 kcmdline
= fopen("/proc/cmdline", "r");
1454 while ((line
= getline_wrapped(kcmdline
, &linenum
)) != NULL
) {
1458 while ((arg
= strsep_skipspace(&ptr
, "\t ")) != NULL
) {
1459 char *sep
, *modname
, *opt
;
1461 sep
= strchr(arg
, '.');
1463 if (!strchr(sep
, '='))
1470 printf("options %s %s\n", modname
, opt
);
1472 *options
= add_options(underscores(modname
),
1483 static void add_to_env_var(const char *option
)
1487 if ((oldenv
= getenv("MODPROBE_OPTIONS")) != NULL
) {
1489 nofail_asprintf(&newenv
, "%s %s", oldenv
, option
);
1490 setenv("MODPROBE_OPTIONS", newenv
, 1);
1492 setenv("MODPROBE_OPTIONS", option
, 1);
1495 /* Prepend options from environment. */
1496 static char **merge_args(char *args
, char *argv
[], int *argc
)
1498 char *arg
, *argstring
;
1499 char **newargs
= NULL
;
1500 unsigned int i
, num_env
= 0;
1505 argstring
= NOFAIL(strdup(args
));
1506 for (arg
= strtok(argstring
, " "); arg
; arg
= strtok(NULL
, " ")) {
1508 newargs
= NOFAIL(realloc(newargs
,
1510 * (num_env
+ *argc
+ 1)));
1511 newargs
[num_env
] = arg
;
1517 /* Append commandline args */
1518 newargs
[0] = argv
[0];
1519 for (i
= 1; i
<= *argc
; i
++)
1520 newargs
[num_env
+i
] = argv
[i
];
1526 static char *gather_options(char *argv
[])
1528 char *optstring
= NOFAIL(strdup(""));
1530 /* Rest is module options */
1532 /* Quote value if it contains spaces. */
1533 unsigned int eq
= strcspn(*argv
, "=");
1535 if (strchr(*argv
+eq
, ' ') && !strchr(*argv
, '"')) {
1536 char quoted
[strlen(*argv
) + 3];
1538 sprintf(quoted
, "%s=\"%s\"", *argv
, *argv
+eq
+1);
1539 optstring
= append_option(optstring
, quoted
);
1541 optstring
= append_option(optstring
, *argv
);
1547 static int handle_module(const char *modname
,
1548 struct list_head
*todo_list
,
1549 const char *newname
,
1556 struct module_options
*modoptions
,
1557 struct module_command
*commands
,
1558 int ignore_commands
,
1561 int strip_modversion
,
1562 const char *cmdline_opts
,
1565 if (list_empty(todo_list
)) {
1566 const char *command
;
1568 /* The dependencies have to be real modules, but
1569 handle case where the first is completely bogus. */
1570 command
= find_command(modname
, commands
);
1571 if (command
&& !ignore_commands
) {
1572 do_command(modname
, command
, verbose
, dry_run
, error
,
1573 remove
? "remove":"install", cmdline_opts
);
1578 error("Module %s not found.\n", modname
);
1583 rmmod(todo_list
, newname
, first_time
, error
, dry_run
, verbose
,
1584 commands
, ignore_commands
, 0, cmdline_opts
, flags
);
1586 insmod(todo_list
, NOFAIL(strdup(options
)), newname
,
1587 first_time
, error
, dry_run
, verbose
, modoptions
,
1588 commands
, ignore_commands
, ignore_proc
, strip_vermagic
,
1589 strip_modversion
, cmdline_opts
);
1594 static struct option options
[] = { { "verbose", 0, NULL
, 'v' },
1595 { "version", 0, NULL
, 'V' },
1596 { "config", 1, NULL
, 'C' },
1597 { "name", 1, NULL
, 'o' },
1598 { "remove", 0, NULL
, 'r' },
1599 { "wait", 0, NULL
, 'w' },
1600 { "showconfig", 0, NULL
, 'c' },
1601 { "autoclean", 0, NULL
, 'k' },
1602 { "quiet", 0, NULL
, 'q' },
1603 { "show", 0, NULL
, 'n' },
1604 { "dry-run", 0, NULL
, 'n' },
1605 { "syslog", 0, NULL
, 's' },
1606 { "type", 1, NULL
, 't' },
1607 { "list", 0, NULL
, 'l' },
1608 { "all", 0, NULL
, 'a' },
1609 { "ignore-install", 0, NULL
, 'i' },
1610 { "ignore-remove", 0, NULL
, 'i' },
1611 { "force", 0, NULL
, 'f' },
1612 { "force-vermagic", 0, NULL
, 1 },
1613 { "force-modversion", 0, NULL
, 2 },
1614 { "set-version", 1, NULL
, 'S' },
1615 { "show-depends", 0, NULL
, 'D' },
1616 { "dirname", 1, NULL
, 'd' },
1617 { "first-time", 0, NULL
, 3 },
1618 { "dump-modversions", 0, NULL
, 4 },
1619 { "use-blacklist", 0, NULL
, 'b' },
1620 { NULL
, 0, NULL
, 0 } };
1622 int main(int argc
, char *argv
[])
1625 struct stat statbuf
;
1633 int ignore_commands
= 0;
1634 int strip_vermagic
= 0;
1635 int strip_modversion
= 0;
1636 int ignore_proc
= 0;
1638 int dump_modver
= 0;
1639 int use_blacklist
= 0;
1640 unsigned int i
, num_modules
;
1642 const char *config
= NULL
;
1643 char *dirname
= NULL
;
1644 char *optstring
= NULL
;
1645 char *newname
= NULL
;
1646 char *aliasfilename
, *symfilename
;
1647 errfn_t error
= fatal
;
1648 int flags
= O_NONBLOCK
|O_EXCL
;
1651 /* Prepend options from environment. */
1652 argv
= merge_args(getenv("MODPROBE_OPTIONS"), argv
, &argc
);
1655 while ((opt
= getopt_long(argc
, argv
, "vVC:o:rknqQsclt:aifbwd:", options
, NULL
)) != -1){
1658 add_to_env_var("-v");
1662 puts(PACKAGE
" version " VERSION
);
1665 strncpy(buf
.release
, optarg
, sizeof(buf
.release
));
1666 buf
.release
[sizeof(buf
.release
)-1] = '\0';
1670 add_to_env_var("-C");
1671 add_to_env_var(config
);
1675 add_to_env_var("-q");
1681 add_to_env_var("-D");
1703 /* FIXME: This should actually do something */
1709 add_to_env_var("-s");
1713 ignore_commands
= 1;
1717 strip_modversion
= 1;
1723 flags
&= ~O_NONBLOCK
;
1726 nofail_asprintf(&dirname
, "%s/%s/%s", optarg
,
1727 MODULE_DIR
, buf
.release
);
1733 strip_modversion
= 1;
1742 print_usage(argv
[0]);
1746 /* If stderr not open, go to syslog */
1747 if (logging
|| fstat(STDERR_FILENO
, &statbuf
) != 0) {
1748 openlog("modprobe", LOG_CONS
, LOG_DAEMON
);
1752 if (argc
< optind
+ 1 && !dump_only
&& !list_only
&& !remove
)
1753 print_usage(argv
[0]);
1756 nofail_asprintf(&dirname
, "%s/%s", MODULE_DIR
, buf
.release
);
1757 nofail_asprintf(&aliasfilename
, "%s/modules.alias", dirname
);
1758 nofail_asprintf(&symfilename
, "%s/modules.symbols", dirname
);
1760 /* Old-style -t xxx wildcard? Only with -l. */
1762 if (optind
+1 < argc
)
1763 fatal("Can't have multiple wildcards\n");
1764 /* fprintf(stderr, "man find\n"); return 1; */
1765 return do_wildcard(dirname
, type
, argv
[optind
]?:"*");
1768 fatal("-t only supported with -l");
1771 struct module_command
*commands
= NULL
;
1772 struct module_options
*modoptions
= NULL
;
1773 struct module_alias
*aliases
= NULL
;
1774 struct module_blacklist
*blacklist
= NULL
;
1776 parse_toplevel_config(config
, "", 1, 0, &modoptions
,
1777 &commands
, &aliases
, &blacklist
);
1778 /* Read module options from kernel command line */
1779 parse_kcmdline(1, &modoptions
);
1780 parse_config_file(aliasfilename
, "", 1, 0, &modoptions
,
1781 &commands
, &aliases
, &blacklist
);
1782 parse_config_file(symfilename
, "", 1, 0, &modoptions
,
1783 &commands
, &aliases
, &blacklist
);
1787 if (remove
|| all
) {
1788 num_modules
= argc
- optind
;
1789 optstring
= NOFAIL(strdup(""));
1792 optstring
= gather_options(argv
+optind
+1);
1795 /* num_modules is always 1 except for -r or -a. */
1796 for (i
= 0; i
< num_modules
; i
++) {
1797 struct module_command
*commands
= NULL
;
1798 struct module_options
*modoptions
= NULL
;
1799 struct module_alias
*aliases
= NULL
;
1800 struct module_blacklist
*blacklist
= NULL
;
1802 char *modulearg
= argv
[optind
+ i
];
1805 dump_modversions(modulearg
, error
);
1809 /* Convert name we are looking for */
1810 underscores(modulearg
);
1812 /* Returns the resolved alias, options */
1813 parse_toplevel_config(config
, modulearg
, 0,
1814 remove
, &modoptions
, &commands
, &aliases
, &blacklist
);
1816 /* Read module options from kernel command line */
1817 parse_kcmdline(0, &modoptions
);
1819 /* No luck? Try symbol names, if starts with symbol:. */
1821 strncmp(modulearg
, "symbol:", strlen("symbol:")) == 0) {
1822 parse_config_file(symfilename
, modulearg
, 0,
1823 remove
, &modoptions
, &commands
,
1824 &aliases
, &blacklist
);
1827 if(!strchr(modulearg
, ':'))
1828 read_depends(dirname
, modulearg
, &list
);
1830 /* We only use canned aliases as last resort. */
1831 if (list_empty(&list
)
1832 && !find_command(modulearg
, commands
))
1834 read_aliases_file(aliasfilename
,
1835 modulearg
, 0, remove
,
1836 &modoptions
, &commands
,
1837 &aliases
, &blacklist
);
1841 aliases
= apply_blacklist(aliases
, blacklist
);
1843 errfn_t err
= error
;
1845 /* More than one alias? Don't bail out on failure. */
1849 /* Add the options for this alias. */
1850 char *opts
= NOFAIL(strdup(optstring
));
1851 opts
= add_extra_options(modulearg
,
1854 read_depends(dirname
, aliases
->module
, &list
);
1855 if (handle_module(aliases
->module
, &list
,
1856 newname
, remove
, opts
,
1858 dry_run
, verbose
, modoptions
,
1859 commands
, ignore_commands
,
1860 ignore_proc
, strip_vermagic
,
1865 aliases
= aliases
->next
;
1866 INIT_LIST_HEAD(&list
);
1870 && find_blacklist(modulearg
, blacklist
))
1873 if (handle_module(modulearg
, &list
, newname
, remove
,
1874 optstring
, first_time
, error
, dry_run
,
1875 verbose
, modoptions
, commands
,
1876 ignore_commands
, ignore_proc
,
1877 strip_vermagic
, strip_modversion
,
1886 free(aliasfilename
);