pre-release: update to v3.7-pre7
[mit.git] / modprobe.c
blob7c3b4eca9ceb271b78dd4aaef108aa98bb79e719
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>
23 #include <sys/stat.h>
24 #include <sys/mman.h>
25 #include <fcntl.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <ctype.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <unistd.h>
33 #include <dirent.h>
34 #include <limits.h>
35 #include <elf.h>
36 #include <getopt.h>
37 #include <fnmatch.h>
38 #include <asm/unistd.h>
39 #include <sys/wait.h>
40 #include <syslog.h>
42 #define streq(a,b) (strcmp((a),(b)) == 0)
43 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
45 #include "zlibsupport.h"
46 #include "logging.h"
47 #include "index.h"
48 #include "list.h"
49 #include "config_filter.h"
51 #include "testing.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);
58 struct module {
59 struct list_head list;
60 char *modname;
61 char filename[0];
64 #ifndef MODULE_DIR
65 #define MODULE_DIR "/lib/modules"
66 #endif
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",
73 filename, line, cmd);
76 static void print_usage(const char *progname)
78 fprintf(stderr,
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);
83 exit(1);
86 static char *getline_wrapped(FILE *file, unsigned int *linenum)
88 int size = 256;
89 int i = 0;
90 char *buf = NOFAIL(malloc(size));
91 for(;;) {
92 int ch = getc_unlocked(file);
94 switch(ch) {
95 case EOF:
96 if (i == 0) {
97 free(buf);
98 return NULL;
100 /* else fall through */
102 case '\n':
103 if (linenum)
104 (*linenum)++;
105 if (i == size)
106 buf = NOFAIL(realloc(buf, size + 1));
107 buf[i] = '\0';
108 return buf;
110 case '\\':
111 ch = getc_unlocked(file);
113 if (ch == '\n') {
114 if (linenum)
115 (*linenum)++;
116 continue;
118 /* else fall through */
120 default:
121 buf[i++] = ch;
123 if (i == size) {
124 size *= 2;
125 buf = NOFAIL(realloc(buf, size));
131 static struct module *find_module(const char *filename, struct list_head *list)
133 struct module *i;
135 list_for_each_entry(i, list, list) {
136 if (strcmp(i->filename, filename) == 0)
137 return i;
139 return NULL;
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;
146 unsigned int i;
148 afterslash = strrchr(filename, '/');
149 if (!afterslash)
150 afterslash = filename;
151 else
152 afterslash++;
154 /* Convert to underscores, stop at first . */
155 for (i = 0; afterslash[i] && afterslash[i] != '.'; i++) {
156 if (afterslash[i] == '-')
157 modname[i] = '_';
158 else
159 modname[i] = afterslash[i];
161 modname[i] = '\0';
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);
173 return fd;
176 static void close_file(int fd)
178 /* Valgrind is picky... */
179 close(fd);
182 static void add_module(char *filename, int namelen, struct list_head *list)
184 struct module *mod;
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);
189 if (mod)
190 list_del(&mod->list);
191 else {
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)
206 unsigned int i;
208 if (strlen(b) != len)
209 return 0;
211 for (i = 0; i < len; i++) {
212 if ((a[i] == '_' || a[i] == '-')
213 && (b[i] == '_' || b[i] == '-'))
214 continue;
215 if (a[i] != b[i])
216 return 0;
218 return 1;
221 /* Fills in list of modules if this is the line we want. */
222 static int add_modules_dep_line(char *line,
223 const char *name,
224 struct list_head *list,
225 const char *dirname)
227 char *ptr;
228 int len;
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 ")] == '#')
234 return 0;
236 /* Is this the module we are looking for? */
237 *ptr = '\0';
238 if (strrchr(line, '/'))
239 modname = strrchr(line, '/') + 1;
240 else
241 modname = line;
243 len = strlen(modname);
244 if (strchr(modname, '.'))
245 len = strchr(modname, '.') - modname;
246 if (!modname_equal(modname, name, len))
247 return 0;
249 /* Create the list. */
250 if ('/' == line[0]) { /* old style deps - absolute path specified */
251 add_module(line, ptr - line, list);
252 } else {
253 nofail_asprintf(&fullpath, "%s/%s", dirname, line);
254 add_module(fullpath, strlen(dirname)+1+(ptr - line), list);
255 free(fullpath);
258 ptr++;
259 for(;;) {
260 char *dep_start;
261 ptr += strspn(ptr, " \t");
262 if (*ptr == '\0')
263 break;
264 dep_start = ptr;
265 ptr += strcspn(ptr, " \t");
266 if ('/' == dep_start[0]) { /* old style deps */
267 add_module(dep_start, ptr - dep_start, list);
268 } else {
269 nofail_asprintf(&fullpath, "%s/%s", dirname, dep_start);
270 add_module(fullpath,
271 strlen(dirname)+1+(ptr - dep_start), list);
272 free(fullpath);
275 return 1;
278 static int read_depends_file(const char *dirname,
279 const char *start_name,
280 struct list_head *list)
282 char *modules_dep_name;
283 char *line;
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);
288 if (!modules_dep) {
289 free(modules_dep_name);
290 return 0;
293 line = index_search(modules_dep, start_name);
294 if (line) {
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");
298 free(line);
301 index_file_close(modules_dep);
302 free(modules_dep_name);
304 return 1;
307 static void read_depends(const char *dirname,
308 const char *start_name,
309 struct list_head *list)
311 char *modules_dep_name;
312 char *line;
313 FILE *modules_dep;
314 int done = 0;
316 if (use_binary_indexes)
317 if (read_depends_file(dirname, start_name, list))
318 return;
320 nofail_asprintf(&modules_dep_name, "%s/%s", dirname, "modules.dep");
321 modules_dep = fopen(modules_dep_name, "r");
322 if (!modules_dep)
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
327 from boot/ */
328 while (!done && (line = getline_wrapped(modules_dep, NULL)) != NULL) {
329 done = add_modules_dep_line(line, start_name, list, dirname);
330 free(line);
332 fclose(modules_dep);
333 free(modules_dep_name);
336 /* We use error numbers in a loose translation... */
337 static const char *insert_moderror(int err)
339 switch (err) {
340 case ENOEXEC:
341 return "Invalid module format";
342 case ENOENT:
343 return "Unknown symbol in module, or unknown parameter (see dmesg)";
344 case ENOSYS:
345 return "Kernel does not have module support";
346 default:
347 return strerror(err);
351 static const char *remove_moderror(int err)
353 switch (err) {
354 case ENOENT:
355 return "No such module";
356 case ENOSYS:
357 return "Kernel does not have module unloading support";
358 default:
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)
367 char *p;
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) {
376 strcpy(p, newname);
377 return;
381 warn("Could not find old name in %s to replace!\n", module->filename);
384 static void *get_section32(void *file,
385 unsigned long size,
386 const char *name,
387 unsigned long *secsize)
389 Elf32_Ehdr *hdr = file;
390 Elf32_Shdr *sechdrs = file + hdr->e_shoff;
391 const char *secnames;
392 unsigned int i;
394 /* Too short? */
395 if (size < sizeof(*hdr))
396 return NULL;
397 if (size < hdr->e_shoff + hdr->e_shnum * sizeof(sechdrs[0]))
398 return NULL;
399 if (size < sechdrs[hdr->e_shstrndx].sh_offset)
400 return NULL;
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;
408 return NULL;
411 static void *get_section64(void *file,
412 unsigned long size,
413 const char *name,
414 unsigned long *secsize)
416 Elf64_Ehdr *hdr = file;
417 Elf64_Shdr *sechdrs = file + hdr->e_shoff;
418 const char *secnames;
419 unsigned int i;
421 /* Too short? */
422 if (size < sizeof(*hdr))
423 return NULL;
424 if (size < hdr->e_shoff + hdr->e_shnum * sizeof(sechdrs[0]))
425 return NULL;
426 if (size < sechdrs[hdr->e_shstrndx].sh_offset)
427 return NULL;
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;
435 return NULL;
438 static int elf_ident(void *mod, unsigned long size)
440 /* "\177ELF" <byte> where byte = 001 for 32-bit, 002 for 64 */
441 char *ident = mod;
443 if (size < EI_CLASS || memcmp(mod, ELFMAG, SELFMAG) != 0)
444 return ELFCLASSNONE;
445 return ident[EI_CLASS];
448 static void *get_section(void *file,
449 unsigned long size,
450 const char *name,
451 unsigned long *secsize)
453 switch (elf_ident(file, size)) {
454 case ELFCLASS32:
455 return get_section32(file, size, name, secsize);
456 case ELFCLASS64:
457 return get_section64(file, size, name, secsize);
458 default:
459 return NULL;
463 static void rename_module(struct module *module,
464 void *mod,
465 unsigned long len,
466 const char *newname)
468 void *modstruct;
469 unsigned long modstruct_len;
471 /* Old-style */
472 modstruct = get_section(mod, len, ".gnu.linkonce.this_module",
473 &modstruct_len);
474 /* New-style */
475 if (!modstruct)
476 modstruct = get_section(mod, len, "__module", &modstruct_len);
477 if (!modstruct)
478 warn("Could not find module name to change in %s\n",
479 module->filename);
480 else
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;
491 unsigned int i;
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;
503 unsigned int i;
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,
511 void *mod,
512 unsigned long len,
513 const char *secname)
515 switch (elf_ident(mod, len)) {
516 case ELFCLASS32:
517 invalidate_section32(mod, secname);
518 break;
519 case ELFCLASS64:
520 invalidate_section64(mod, secname);
521 break;
522 default:
523 warn("Unknown module format in %s: not forcing version\n",
524 module->filename);
528 static const char *next_string(const char *string, unsigned long *secsize)
530 /* Skip non-zero chars */
531 while (string[0]) {
532 string++;
533 if ((*secsize)-- <= 1)
534 return NULL;
537 /* Skip any zero padding. */
538 while (!string[0]) {
539 string++;
540 if ((*secsize)-- <= 1)
541 return NULL;
543 return string;
546 static void clear_magic(struct module *module, void *mod, unsigned long len)
548 const char *p;
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));
560 return;
565 struct module_options
567 struct module_options *next;
568 char *modulename;
569 char *options;
572 struct module_command
574 struct module_command *next;
575 char *modulename;
576 char *command;
579 struct module_alias
581 struct module_alias *next;
582 char *module;
585 struct module_blacklist
587 struct module_blacklist *next;
588 char *modulename;
591 /* Link in a new option line from the config file. */
592 static struct module_options *
593 add_options(const char *modname,
594 const char *option,
595 struct module_options *options)
597 struct module_options *new;
598 char *tab;
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'))
605 *tab = ' ';
606 new->next = options;
607 return new;
610 /* Link in a new install line from the config file. */
611 static struct module_command *
612 add_command(const char *modname,
613 const char *command,
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;
622 return new;
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));
633 new->next = aliases;
634 return new;
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;
646 return new;
649 /* Find blacklist commands if any. */
650 static int
651 find_blacklist(const char *modname, const struct module_blacklist *blacklist)
653 while (blacklist) {
654 if (strcmp(blacklist->modulename, modname) == 0)
655 return 1;
656 blacklist = blacklist->next;
658 return 0;
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;
667 while (aliases) {
668 char *modname = aliases->module;
669 if (!find_blacklist(modname, blacklist))
670 result = add_alias(modname, result);
671 aliases = aliases->next;
673 return result;
676 /* Find install commands if any. */
677 static const char *find_command(const char *modname,
678 const struct module_command *commands)
680 while (commands) {
681 if (fnmatch(commands->modulename, modname, 0) == 0)
682 return commands->command;
683 commands = commands->next;
685 return NULL;
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);
694 return options;
697 static char *prepend_option(char *options, const char *newoption)
699 size_t l1, l2;
700 l1 = strlen(options);
701 l2 = strlen(newoption);
702 /* the resulting string will look like
703 * newoption + ' ' + options + '\0' */
704 if (l1) {
705 options = NOFAIL(realloc(options, l2 + 1 + l1 + 1));
706 memmove(options + l2 + 1, options, l1 + 1);
707 options[l2] = ' ';
708 memcpy(options, newoption, l2);
709 } else {
710 options = NOFAIL(realloc(options, l2 + 1));
711 memcpy(options, newoption, l2);
712 options[l2] = '\0';
714 return options;
717 /* Add to options */
718 static char *add_extra_options(const char *modname,
719 char *optstring,
720 const struct module_options *options)
722 while (options) {
723 if (strcmp(options->modulename, modname) == 0)
724 optstring = prepend_option(optstring, options->options);
725 options = options->next;
727 return optstring;
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)
736 FILE *file;
737 char *s;
739 file = fopen(filename, "r");
740 if (file == NULL)
741 return (errno == ENOENT) ? 0 : -1;
742 s = fgets(buf, buflen, file);
743 fclose(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)
753 int ret;
754 char *name;
755 struct stat finfo;
757 const int ATTR_LEN = 16;
758 char attr[ATTR_LEN];
760 /* Check sysfs is mounted */
761 if (stat("/sys/module", &finfo) < 0)
762 return -1;
764 /* Find module. */
765 nofail_asprintf(&name, "/sys/module/%s", modname);
766 ret = stat(name, &finfo);
767 free(name);
768 if (ret < 0)
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);
773 while (1) {
774 ret = read_attribute(name, attr, ATTR_LEN);
775 if (ret != 1 || streq(attr, "live\n"))
776 break;
778 usleep(100000);
780 free(name);
782 if (ret != 1)
783 return ret;
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);
789 free(name);
790 if (ret == 1)
791 *usecount = atoi(attr);
794 return 1;
797 /* If we don't flush, then child processes print before we do */
798 static void verbose_printf(int verbose, const char *fmt, ...)
800 va_list arglist;
802 if (verbose) {
803 va_start(arglist, fmt);
804 vprintf(fmt, arglist);
805 fflush(stdout);
806 va_end(arglist);
810 /* Do an install/remove command: replace $CMDLINE_OPTS if it's specified. */
811 static void do_command(const char *modname,
812 const char *command,
813 int verbose, int dry_run,
814 errfn_t error,
815 const char *type,
816 const char *cmdline_opts)
818 int ret;
819 char *p, *replaced_cmd = NOFAIL(strdup(command));
821 while ((p = strstr(replaced_cmd, "$CMDLINE_OPTS")) != NULL) {
822 char *new;
823 nofail_asprintf(&new, "%.*s%s%s",
824 (int)(p - replaced_cmd), replaced_cmd, cmdline_opts,
825 p + strlen("$CMDLINE_OPTS"));
826 free(replaced_cmd);
827 replaced_cmd = new;
830 verbose_printf(verbose, "%s %s\n", type, replaced_cmd);
831 if (dry_run)
832 return;
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);
838 free(replaced_cmd);
841 /* Actually do the insert. Frees second arg. */
842 static int insmod(struct list_head *list,
843 char *optstring,
844 const char *newname,
845 int first_time,
846 errfn_t error,
847 int dry_run,
848 int verbose,
849 const struct module_options *options,
850 const struct module_command *commands,
851 int ignore_commands,
852 int ignore_proc,
853 int strip_vermagic,
854 int strip_modversion,
855 const char *cmdline_opts)
857 int ret, fd;
858 unsigned long len;
859 void *map;
860 const char *command;
861 struct module *mod = list_entry(list->next, struct module, list);
862 int rc = 0;
864 /* Take us off the list. */
865 list_del(&mod->list);
867 /* Do things we (or parent) depend on first, but don't die if
868 * they fail. */
869 if (!list_empty(list)) {
870 if ((rc = insmod(list, NOFAIL(strdup("")), NULL, 0, warn,
871 dry_run, verbose, options, commands, 0, ignore_proc,
872 strip_vermagic, strip_modversion, "")) != 0) {
873 error("Error inserting %s (%s): %s\n",
874 mod->modname, mod->filename,
875 insert_moderror(errno));
876 goto out_optstring;
880 fd = open_file(mod->filename);
881 if (fd < 0) {
882 error("Could not open '%s': %s\n",
883 mod->filename, strerror(errno));
884 goto out_optstring;
887 /* Don't do ANYTHING if already in kernel. */
888 if (!ignore_proc
889 && module_in_kernel(newname ?: mod->modname, NULL) == 1) {
890 if (first_time)
891 error("Module %s already in kernel.\n",
892 newname ?: mod->modname);
893 goto out_unlock;
896 command = find_command(mod->modname, commands);
897 if (command && !ignore_commands) {
898 close_file(fd);
899 do_command(mod->modname, command, verbose, dry_run, error,
900 "install", cmdline_opts);
901 goto out_optstring;
904 map = grab_fd(fd, &len);
905 if (!map) {
906 error("Could not read '%s': %s\n",
907 mod->filename, strerror(errno));
908 goto out_unlock;
911 /* Rename it? */
912 if (newname)
913 rename_module(mod, map, len, newname);
915 if (strip_modversion)
916 strip_section(mod, map, len, "__versions");
917 if (strip_vermagic)
918 clear_magic(mod, map, len);
920 /* Config file might have given more options */
921 optstring = add_extra_options(mod->modname, optstring, options);
923 verbose_printf(verbose, "insmod %s %s\n", mod->filename, optstring);
925 if (dry_run)
926 goto out;
928 ret = init_module(map, len, optstring);
929 if (ret != 0) {
930 if (errno == EEXIST) {
931 if (first_time)
932 error("Module %s already in kernel.\n",
933 newname ?: mod->modname);
934 goto out_unlock;
936 /* don't warn noisely if we're loading multiple aliases. */
937 /* one of the aliases may try to use hardware we don't have. */
938 if ((error != warn) || (verbose))
939 error("Error inserting %s (%s): %s\n",
940 mod->modname, mod->filename,
941 insert_moderror(errno));
942 rc = 1;
944 out:
945 release_file(map, len);
946 out_unlock:
947 close_file(fd);
948 out_optstring:
949 free(optstring);
950 return rc;
953 /* Do recursive removal. */
954 static void rmmod(struct list_head *list,
955 const char *name,
956 int first_time,
957 errfn_t error,
958 int dry_run,
959 int verbose,
960 struct module_command *commands,
961 int ignore_commands,
962 int ignore_inuse,
963 const char *cmdline_opts,
964 int flags)
966 const char *command;
967 unsigned int usecount = 0;
968 struct module *mod = list_entry(list->next, struct module, list);
970 /* Take first one off the list. */
971 list_del(&mod->list);
973 if (!name)
974 name = mod->modname;
976 /* Even if renamed, find commands to orig. name. */
977 command = find_command(mod->modname, commands);
978 if (command && !ignore_commands) {
979 do_command(mod->modname, command, verbose, dry_run, error,
980 "remove", cmdline_opts);
981 goto remove_rest;
984 if (module_in_kernel(name, &usecount) == 0)
985 goto nonexistent_module;
987 if (usecount != 0) {
988 if (!ignore_inuse)
989 error("Module %s is in use.\n", name);
990 goto remove_rest;
993 verbose_printf(verbose, "rmmod %s\n", mod->filename);
995 if (dry_run)
996 goto remove_rest;
998 if (delete_module(name, O_EXCL) != 0) {
999 if (errno == ENOENT)
1000 goto nonexistent_module;
1001 error("Error removing %s (%s): %s\n",
1002 name, mod->filename,
1003 remove_moderror(errno));
1006 remove_rest:
1007 /* Now do things we depend. */
1008 if (!list_empty(list))
1009 rmmod(list, NULL, 0, warn, dry_run, verbose, commands,
1010 0, 1, "", flags);
1011 return;
1013 nonexistent_module:
1014 if (first_time)
1015 fatal("Module %s is not in kernel.\n", mod->modname);
1016 goto remove_rest;
1019 struct modver32_info
1021 uint32_t crc;
1022 char name[64 - sizeof(uint32_t)];
1025 struct modver64_info
1027 uint64_t crc;
1028 char name[64 - sizeof(uint64_t)];
1031 const char *skip_dot(const char *str)
1033 /* For our purposes, .foo matches foo. PPC64 needs this. */
1034 if (str && str[0] == '.')
1035 return str + 1;
1036 return str;
1039 void dump_modversions(const char *filename, errfn_t error)
1041 unsigned long size, secsize;
1042 void *file = grab_file(filename, &size);
1043 struct modver32_info *info32;
1044 struct modver64_info *info64;
1045 int n;
1047 if (!file) {
1048 error("%s: %s\n", filename, strerror(errno));
1049 return;
1051 switch (elf_ident(file, size)) {
1052 case ELFCLASS32:
1053 info32 = get_section32(file, size, "__versions", &secsize);
1054 if (!info32)
1055 return; /* Does not seem to be a kernel module */
1056 if (secsize % sizeof(struct modver32_info))
1057 error("Wrong section size in %s\n", filename);
1058 for (n = 0; n < secsize / sizeof(struct modver32_info); n++)
1059 printf("0x%08lx\t%s\n", (unsigned long)
1060 info32[n].crc, skip_dot(info32[n].name));
1061 break;
1063 case ELFCLASS64:
1064 info64 = get_section64(file, size, "__versions", &secsize);
1065 if (!info64)
1066 return; /* Does not seem to be a kernel module */
1067 if (secsize % sizeof(struct modver64_info))
1068 error("Wrong section size in %s\n", filename);
1069 for (n = 0; n < secsize / sizeof(struct modver64_info); n++)
1070 printf("0x%08llx\t%s\n", (unsigned long long)
1071 info64[n].crc, skip_dot(info64[n].name));
1072 break;
1074 default:
1075 error("%s: ELF class not recognized\n", filename);
1080 /* Does path contain directory(s) subpath? */
1081 static int type_matches(const char *path, const char *subpath)
1083 char *subpath_with_slashes;
1084 int ret;
1086 nofail_asprintf(&subpath_with_slashes, "/%s/", subpath);
1088 ret = (strstr(path, subpath_with_slashes) != NULL);
1089 free(subpath_with_slashes);
1090 return ret;
1093 /* Careful! Don't munge - in [ ] as per Debian Bug#350915 */
1094 static char *underscores(char *string)
1096 unsigned int i;
1098 if (!string)
1099 return NULL;
1101 for (i = 0; string[i]; i++) {
1102 switch (string[i]) {
1103 case '-':
1104 string[i] = '_';
1105 break;
1107 case ']':
1108 warn("Unmatched bracket in %s\n", string);
1109 break;
1111 case '[':
1112 i += strcspn(&string[i], "]");
1113 if (!string[i])
1114 warn("Unmatched bracket in %s\n", string);
1115 break;
1118 return string;
1121 static int do_wildcard(const char *dirname,
1122 const char *type,
1123 const char *wildcard)
1125 char *modules_dep_name;
1126 char *line, *wcard;
1127 FILE *modules_dep;
1129 /* Canonicalize wildcard */
1130 wcard = strdup(wildcard);
1131 underscores(wcard);
1133 nofail_asprintf(&modules_dep_name, "%s/%s", dirname, "modules.dep");
1134 modules_dep = fopen(modules_dep_name, "r");
1135 if (!modules_dep)
1136 fatal("Could not load %s: %s\n",
1137 modules_dep_name, strerror(errno));
1139 while ((line = getline_wrapped(modules_dep, NULL)) != NULL) {
1140 char *ptr;
1142 /* Ignore lines without : or which start with a # */
1143 ptr = strchr(line, ':');
1144 if (ptr == NULL || line[strspn(line, "\t ")] == '#')
1145 goto next;
1146 *ptr = '\0';
1148 /* "type" must match complete directory component(s). */
1149 if (!type || type_matches(line, type)) {
1150 char modname[strlen(line)+1];
1152 filename2modname(modname, line);
1153 if (fnmatch(wcard, modname, 0) == 0)
1154 printf("%s\n", line);
1156 next:
1157 free(line);
1160 free(modules_dep_name);
1161 free(wcard);
1162 return 0;
1165 static char *strsep_skipspace(char **string, char *delim)
1167 if (!*string)
1168 return NULL;
1169 *string += strspn(*string, delim);
1170 return strsep(string, delim);
1173 static int parse_config_scan(const char *filename,
1174 const char *name,
1175 int dump_only,
1176 int removing,
1177 struct module_options **options,
1178 struct module_command **commands,
1179 struct module_alias **alias,
1180 struct module_blacklist **blacklist);
1182 static int parse_config_file(const char *filename,
1183 const char *name,
1184 int dump_only,
1185 int removing,
1186 struct module_options **options,
1187 struct module_command **commands,
1188 struct module_alias **aliases,
1189 struct module_blacklist **blacklist)
1191 char *line;
1192 unsigned int linenum = 0;
1193 FILE *cfile;
1195 cfile = fopen(filename, "r");
1196 if (!cfile)
1197 return 0;
1199 while ((line = getline_wrapped(cfile, &linenum)) != NULL) {
1200 char *ptr = line;
1201 char *cmd, *modname;
1203 if (dump_only)
1204 printf("%s\n", line);
1206 cmd = strsep_skipspace(&ptr, "\t ");
1207 if (cmd == NULL || cmd[0] == '#' || cmd[0] == '\0') {
1208 free(line);
1209 continue;
1212 if (strcmp(cmd, "alias") == 0) {
1213 char *wildcard = strsep_skipspace(&ptr, "\t ");
1214 char *realname = strsep_skipspace(&ptr, "\t ");
1216 if (!wildcard || !realname)
1217 grammar(cmd, filename, linenum);
1218 else if (fnmatch(underscores(wildcard),name,0) == 0)
1219 *aliases = add_alias(underscores(realname), *aliases);
1220 } else if (strcmp(cmd, "include") == 0) {
1221 struct module_alias *newalias = NULL;
1222 char *newfilename;
1224 newfilename = strsep_skipspace(&ptr, "\t ");
1225 if (!newfilename) {
1226 grammar(cmd, filename, linenum);
1227 } else {
1228 warn("\"include %s\" is deprecated, "
1229 "please use /etc/modprobe.d\n", newfilename);
1230 if (strncmp(newfilename, "/etc/modprobe.d",
1231 strlen("/etc/modprobe.d")) == 0) {
1232 warn("\"include /etc/modprobe.d\" is "
1233 "the default, ignored\n");
1234 } else {
1235 if (!parse_config_scan(newfilename, name,
1236 dump_only, removing,
1237 options, commands, &newalias,
1238 blacklist))
1239 warn("Failed to open included"
1240 " config file %s: %s\n",
1241 newfilename, strerror(errno));
1243 /* Files included override aliases,
1244 etc that was already set ... */
1245 if (newalias)
1246 *aliases = newalias;
1248 } else if (strcmp(cmd, "options") == 0) {
1249 modname = strsep_skipspace(&ptr, "\t ");
1250 if (!modname || !ptr)
1251 grammar(cmd, filename, linenum);
1252 else {
1253 ptr += strspn(ptr, "\t ");
1254 *options = add_options(underscores(modname),
1255 ptr, *options);
1257 } else if (strcmp(cmd, "install") == 0) {
1258 modname = strsep_skipspace(&ptr, "\t ");
1259 if (!modname || !ptr)
1260 grammar(cmd, filename, linenum);
1261 else if (!removing) {
1262 ptr += strspn(ptr, "\t ");
1263 *commands = add_command(underscores(modname),
1264 ptr, *commands);
1266 } else if (strcmp(cmd, "blacklist") == 0) {
1267 modname = strsep_skipspace(&ptr, "\t ");
1268 if (!modname)
1269 grammar(cmd, filename, linenum);
1270 else if (!removing) {
1271 *blacklist = add_blacklist(underscores(modname),
1272 *blacklist);
1274 } else if (strcmp(cmd, "remove") == 0) {
1275 modname = strsep_skipspace(&ptr, "\t ");
1276 if (!modname || !ptr)
1277 grammar(cmd, filename, linenum);
1278 else if (removing) {
1279 ptr += strspn(ptr, "\t ");
1280 *commands = add_command(underscores(modname),
1281 ptr, *commands);
1283 } else if (strcmp(cmd, "config") == 0) {
1284 char *tmp = strsep_skipspace(&ptr, "\t ");
1285 if (strcmp(tmp, "binary_indexes") == 0) {
1286 tmp = strsep_skipspace(&ptr, "\t ");
1287 if (strcmp(tmp, "yes") == 0)
1288 use_binary_indexes = 1;
1289 if (strcmp(tmp, "no") == 0)
1290 use_binary_indexes = 0;
1292 } else
1293 grammar(cmd, filename, linenum);
1295 free(line);
1297 fclose(cfile);
1298 return 1;
1301 /* fallback to plain-text aliases file as necessary */
1302 static int read_aliases_file(const char *filename,
1303 const char *name,
1304 int dump_only,
1305 int removing,
1306 struct module_options **options,
1307 struct module_command **commands,
1308 struct module_alias **aliases,
1309 struct module_blacklist **blacklist)
1311 struct index_value *realnames;
1312 struct index_value *realname;
1313 char *binfile;
1314 struct index_file *index;
1316 if (!use_binary_indexes)
1317 goto plain_text;
1319 nofail_asprintf(&binfile, "%s.bin", filename);
1320 index = index_file_open(binfile);
1321 if (!index) {
1322 free(binfile);
1323 goto plain_text;
1326 if (dump_only) {
1327 index_dump(index, stdout, "alias ");
1328 free(binfile);
1329 index_file_close(index);
1330 return 1;
1333 realnames = index_searchwild(index, name);
1334 for (realname = realnames; realname; realname = realname->next)
1335 *aliases = add_alias(realname->value, *aliases);
1336 index_values_free(realnames);
1338 free(binfile);
1339 index_file_close(index);
1340 return 1;
1342 plain_text:
1343 return parse_config_file(filename, name, dump_only, removing,
1344 options, commands, aliases, blacklist);
1347 static int parse_config_scan(const char *filename,
1348 const char *name,
1349 int dump_only,
1350 int removing,
1351 struct module_options **options,
1352 struct module_command **commands,
1353 struct module_alias **aliases,
1354 struct module_blacklist **blacklist)
1356 DIR *dir;
1357 int ret = 0;
1359 dir = opendir(filename);
1360 if (dir) {
1361 struct file_entry {
1362 struct list_head node;
1363 char name[];
1365 LIST_HEAD(files_list);
1366 struct file_entry *fe, *fe_tmp;
1367 struct dirent *i;
1369 /* sort files from directory into list */
1370 while ((i = readdir(dir)) != NULL) {
1371 size_t len;
1373 if (i->d_name[0] == '.')
1374 continue;
1375 if (!config_filter(i->d_name))
1376 continue;
1378 len = strlen(i->d_name);
1379 if (len < 6 ||
1380 (strcmp(&i->d_name[len-5], ".conf") != 0 &&
1381 strcmp(&i->d_name[len-6], ".alias") != 0))
1382 warn("All config files need .conf: %s/%s, "
1383 "it will be ignored in a future release.\n",
1384 filename, i->d_name);
1385 fe = malloc(sizeof(struct file_entry) + len + 1);
1386 if (fe == NULL)
1387 continue;
1388 strcpy(fe->name, i->d_name);
1389 list_for_each_entry(fe_tmp, &files_list, node)
1390 if (strcmp(fe_tmp->name, fe->name) >= 0)
1391 break;
1392 list_add_tail(&fe->node, &fe_tmp->node);
1394 closedir(dir);
1396 /* parse list of files */
1397 list_for_each_entry_safe(fe, fe_tmp, &files_list, node) {
1398 char *cfgfile;
1400 nofail_asprintf(&cfgfile, "%s/%s", filename, fe->name);
1401 if (!parse_config_file(cfgfile, name,
1402 dump_only, removing,
1403 options, commands,
1404 aliases, blacklist))
1405 warn("Failed to open config file "
1406 "%s: %s\n", fe->name, strerror(errno));
1407 free(cfgfile);
1408 list_del(&fe->node);
1409 free(fe);
1412 ret = 1;
1413 } else {
1414 if (parse_config_file(filename, name, dump_only, removing,
1415 options, commands, aliases, blacklist))
1416 ret = 1;
1418 return ret;
1421 /* Read binary index file containing aliases only */
1422 static void parse_toplevel_config(const char *filename,
1423 const char *name,
1424 int dump_only,
1425 int removing,
1426 struct module_options **options,
1427 struct module_command **commands,
1428 struct module_alias **aliases,
1429 struct module_blacklist **blacklist)
1431 if (filename) {
1432 if (!parse_config_scan(filename, name, dump_only, removing,
1433 options, commands, aliases, blacklist))
1434 fatal("Failed to open config file %s: %s\n",
1435 filename, strerror(errno));
1436 return;
1439 /* deprecated config file */
1440 if (parse_config_file("/etc/modprobe.conf", name, dump_only, removing,
1441 options, commands, aliases, blacklist) > 0)
1442 warn("Deprecated config file /etc/modprobe.conf, "
1443 "all config files belong into /etc/modprobe.d/.\n");
1445 /* default config */
1446 parse_config_scan("/etc/modprobe.d", name, dump_only, removing,
1447 options, commands, aliases, blacklist);
1450 /* Read possible module arguments from the kernel command line. */
1451 static int parse_kcmdline(int dump_only, struct module_options **options)
1453 char *line;
1454 unsigned int linenum = 0;
1455 FILE *kcmdline;
1457 kcmdline = fopen("/proc/cmdline", "r");
1458 if (!kcmdline)
1459 return 0;
1461 while ((line = getline_wrapped(kcmdline, &linenum)) != NULL) {
1462 char *ptr = line;
1463 char *arg;
1465 while ((arg = strsep_skipspace(&ptr, "\t ")) != NULL) {
1466 char *sep, *modname, *opt;
1468 sep = strchr(arg, '.');
1469 if (sep) {
1470 if (!strchr(sep, '='))
1471 continue;
1472 modname = arg;
1473 *sep = '\0';
1474 opt = ++sep;
1476 if (dump_only)
1477 printf("options %s %s\n", modname, opt);
1479 *options = add_options(underscores(modname),
1480 opt, *options);
1484 free(line);
1486 fclose(kcmdline);
1487 return 1;
1490 static void add_to_env_var(const char *option)
1492 const char *oldenv;
1494 if ((oldenv = getenv("MODPROBE_OPTIONS")) != NULL) {
1495 char *newenv;
1496 nofail_asprintf(&newenv, "%s %s", oldenv, option);
1497 setenv("MODPROBE_OPTIONS", newenv, 1);
1498 } else
1499 setenv("MODPROBE_OPTIONS", option, 1);
1502 /* Prepend options from environment. */
1503 static char **merge_args(char *args, char *argv[], int *argc)
1505 char *arg, *argstring;
1506 char **newargs = NULL;
1507 unsigned int i, num_env = 0;
1509 if (!args)
1510 return argv;
1512 argstring = NOFAIL(strdup(args));
1513 for (arg = strtok(argstring, " "); arg; arg = strtok(NULL, " ")) {
1514 num_env++;
1515 newargs = NOFAIL(realloc(newargs,
1516 sizeof(newargs[0])
1517 * (num_env + *argc + 1)));
1518 newargs[num_env] = arg;
1521 if (!newargs)
1522 return argv;
1524 /* Append commandline args */
1525 newargs[0] = argv[0];
1526 for (i = 1; i <= *argc; i++)
1527 newargs[num_env+i] = argv[i];
1529 *argc += num_env;
1530 return newargs;
1533 static char *gather_options(char *argv[])
1535 char *optstring = NOFAIL(strdup(""));
1537 /* Rest is module options */
1538 while (*argv) {
1539 /* Quote value if it contains spaces. */
1540 unsigned int eq = strcspn(*argv, "=");
1542 if (strchr(*argv+eq, ' ') && !strchr(*argv, '"')) {
1543 char quoted[strlen(*argv) + 3];
1544 (*argv)[eq] = '\0';
1545 sprintf(quoted, "%s=\"%s\"", *argv, *argv+eq+1);
1546 optstring = append_option(optstring, quoted);
1547 } else
1548 optstring = append_option(optstring, *argv);
1549 argv++;
1551 return optstring;
1554 static int handle_module(const char *modname,
1555 struct list_head *todo_list,
1556 const char *newname,
1557 int remove,
1558 char *options,
1559 int first_time,
1560 errfn_t error,
1561 int dry_run,
1562 int verbose,
1563 struct module_options *modoptions,
1564 struct module_command *commands,
1565 int ignore_commands,
1566 int ignore_proc,
1567 int strip_vermagic,
1568 int strip_modversion,
1569 const char *cmdline_opts,
1570 int flags)
1572 if (list_empty(todo_list)) {
1573 const char *command;
1575 /* The dependencies have to be real modules, but
1576 handle case where the first is completely bogus. */
1577 command = find_command(modname, commands);
1578 if (command && !ignore_commands) {
1579 do_command(modname, command, verbose, dry_run, error,
1580 remove ? "remove":"install", cmdline_opts);
1581 return 0;
1584 if (!quiet)
1585 error("Module %s not found.\n", modname);
1586 return 1;
1589 if (remove)
1590 rmmod(todo_list, newname, first_time, error, dry_run, verbose,
1591 commands, ignore_commands, 0, cmdline_opts, flags);
1592 else
1593 insmod(todo_list, NOFAIL(strdup(options)), newname,
1594 first_time, error, dry_run, verbose, modoptions,
1595 commands, ignore_commands, ignore_proc, strip_vermagic,
1596 strip_modversion, cmdline_opts);
1598 return 0;
1601 static struct option options[] = { { "verbose", 0, NULL, 'v' },
1602 { "version", 0, NULL, 'V' },
1603 { "config", 1, NULL, 'C' },
1604 { "name", 1, NULL, 'o' },
1605 { "remove", 0, NULL, 'r' },
1606 { "wait", 0, NULL, 'w' },
1607 { "showconfig", 0, NULL, 'c' },
1608 { "quiet", 0, NULL, 'q' },
1609 { "show", 0, NULL, 'n' },
1610 { "dry-run", 0, NULL, 'n' },
1611 { "syslog", 0, NULL, 's' },
1612 { "type", 1, NULL, 't' },
1613 { "list", 0, NULL, 'l' },
1614 { "all", 0, NULL, 'a' },
1615 { "ignore-install", 0, NULL, 'i' },
1616 { "ignore-remove", 0, NULL, 'i' },
1617 { "force", 0, NULL, 'f' },
1618 { "force-vermagic", 0, NULL, 1 },
1619 { "force-modversion", 0, NULL, 2 },
1620 { "set-version", 1, NULL, 'S' },
1621 { "show-depends", 0, NULL, 'D' },
1622 { "dirname", 1, NULL, 'd' },
1623 { "first-time", 0, NULL, 3 },
1624 { "dump-modversions", 0, NULL, 4 },
1625 { "use-blacklist", 0, NULL, 'b' },
1626 { NULL, 0, NULL, 0 } };
1628 int main(int argc, char *argv[])
1630 struct utsname buf;
1631 struct stat statbuf;
1632 int opt;
1633 int dump_only = 0;
1634 int dry_run = 0;
1635 int remove = 0;
1636 int verbose = 0;
1637 int list_only = 0;
1638 int all = 0;
1639 int ignore_commands = 0;
1640 int strip_vermagic = 0;
1641 int strip_modversion = 0;
1642 int ignore_proc = 0;
1643 int first_time = 0;
1644 int dump_modver = 0;
1645 int use_blacklist = 0;
1646 unsigned int i, num_modules;
1647 char *type = NULL;
1648 const char *config = NULL;
1649 char *dirname = NULL;
1650 char *optstring = NULL;
1651 char *newname = NULL;
1652 char *aliasfilename, *symfilename;
1653 errfn_t error = fatal;
1654 int flags = O_NONBLOCK|O_EXCL;
1655 int was_error = 0;
1657 /* Prepend options from environment. */
1658 argv = merge_args(getenv("MODPROBE_OPTIONS"), argv, &argc);
1660 uname(&buf);
1661 while ((opt = getopt_long(argc, argv, "vVC:o:rnqQsclt:aifbwd:", options, NULL)) != -1){
1662 switch (opt) {
1663 case 'v':
1664 add_to_env_var("-v");
1665 verbose = 1;
1666 break;
1667 case 'V':
1668 puts(PACKAGE " version " VERSION);
1669 exit(0);
1670 case 'S':
1671 strncpy(buf.release, optarg, sizeof(buf.release));
1672 buf.release[sizeof(buf.release)-1] = '\0';
1673 break;
1674 case 'C':
1675 config = optarg;
1676 add_to_env_var("-C");
1677 add_to_env_var(config);
1678 break;
1679 case 'q':
1680 quiet = 1;
1681 add_to_env_var("-q");
1682 break;
1683 case 'D':
1684 dry_run = 1;
1685 ignore_proc = 1;
1686 verbose = 1;
1687 add_to_env_var("-D");
1688 break;
1689 case 'o':
1690 newname = optarg;
1691 break;
1692 case 'r':
1693 remove = 1;
1694 break;
1695 case 'c':
1696 dump_only = 1;
1697 break;
1698 case 't':
1699 type = optarg;
1700 break;
1701 case 'l':
1702 list_only = 1;
1703 break;
1704 case 'a':
1705 all = 1;
1706 error = warn;
1707 break;
1708 case 'n':
1709 dry_run = 1;
1710 break;
1711 case 's':
1712 add_to_env_var("-s");
1713 logging = 1;
1714 break;
1715 case 'i':
1716 ignore_commands = 1;
1717 break;
1718 case 'f':
1719 strip_vermagic = 1;
1720 strip_modversion = 1;
1721 break;
1722 case 'b':
1723 use_blacklist = 1;
1724 break;
1725 case 'w':
1726 flags &= ~O_NONBLOCK;
1727 break;
1728 case 'd':
1729 nofail_asprintf(&dirname, "%s/%s/%s", optarg,
1730 MODULE_DIR, buf.release);
1731 break;
1732 case 1:
1733 strip_vermagic = 1;
1734 break;
1735 case 2:
1736 strip_modversion = 1;
1737 break;
1738 case 3:
1739 first_time = 1;
1740 break;
1741 case 4:
1742 dump_modver = 1;
1743 break;
1744 default:
1745 print_usage(argv[0]);
1749 /* If stderr not open, go to syslog */
1750 if (logging || fstat(STDERR_FILENO, &statbuf) != 0) {
1751 openlog("modprobe", LOG_CONS, LOG_DAEMON);
1752 logging = 1;
1755 if (argc < optind + 1 && !dump_only && !list_only && !remove)
1756 print_usage(argv[0]);
1758 if (!dirname)
1759 nofail_asprintf(&dirname, "%s/%s", MODULE_DIR, buf.release);
1760 nofail_asprintf(&aliasfilename, "%s/modules.alias", dirname);
1761 nofail_asprintf(&symfilename, "%s/modules.symbols", dirname);
1763 /* Old-style -t xxx wildcard? Only with -l. */
1764 if (list_only) {
1765 if (optind+1 < argc)
1766 fatal("Can't have multiple wildcards\n");
1767 /* fprintf(stderr, "man find\n"); return 1; */
1768 return do_wildcard(dirname, type, argv[optind]?:"*");
1770 if (type)
1771 fatal("-t only supported with -l");
1773 if (dump_only) {
1774 struct module_command *commands = NULL;
1775 struct module_options *modoptions = NULL;
1776 struct module_alias *aliases = NULL;
1777 struct module_blacklist *blacklist = NULL;
1779 parse_toplevel_config(config, "", 1, 0, &modoptions,
1780 &commands, &aliases, &blacklist);
1781 /* Read module options from kernel command line */
1782 parse_kcmdline(1, &modoptions);
1783 parse_config_file(aliasfilename, "", 1, 0, &modoptions,
1784 &commands, &aliases, &blacklist);
1785 parse_config_file(symfilename, "", 1, 0, &modoptions,
1786 &commands, &aliases, &blacklist);
1787 exit(0);
1790 if (remove || all) {
1791 num_modules = argc - optind;
1792 optstring = NOFAIL(strdup(""));
1793 } else {
1794 num_modules = 1;
1795 optstring = gather_options(argv+optind+1);
1798 /* num_modules is always 1 except for -r or -a. */
1799 for (i = 0; i < num_modules; i++) {
1800 struct module_command *commands = NULL;
1801 struct module_options *modoptions = NULL;
1802 struct module_alias *aliases = NULL;
1803 struct module_blacklist *blacklist = NULL;
1804 LIST_HEAD(list);
1805 char *modulearg = argv[optind + i];
1807 if (dump_modver) {
1808 dump_modversions(modulearg, error);
1809 continue;
1812 /* Convert name we are looking for */
1813 underscores(modulearg);
1815 /* Returns the resolved alias, options */
1816 parse_toplevel_config(config, modulearg, 0,
1817 remove, &modoptions, &commands, &aliases, &blacklist);
1819 /* Read module options from kernel command line */
1820 parse_kcmdline(0, &modoptions);
1822 /* No luck? Try symbol names, if starts with symbol:. */
1823 if (!aliases &&
1824 strncmp(modulearg, "symbol:", strlen("symbol:")) == 0) {
1825 parse_config_file(symfilename, modulearg, 0,
1826 remove, &modoptions, &commands,
1827 &aliases, &blacklist);
1829 if (!aliases) {
1830 if(!strchr(modulearg, ':'))
1831 read_depends(dirname, modulearg, &list);
1833 /* We only use canned aliases as last resort. */
1834 if (list_empty(&list)
1835 && !find_command(modulearg, commands))
1837 read_aliases_file(aliasfilename,
1838 modulearg, 0, remove,
1839 &modoptions, &commands,
1840 &aliases, &blacklist);
1844 aliases = apply_blacklist(aliases, blacklist);
1845 if (aliases) {
1846 errfn_t err = error;
1848 /* More than one alias? Don't bail out on failure. */
1849 if (aliases->next)
1850 err = warn;
1851 while (aliases) {
1852 /* Add the options for this alias. */
1853 char *opts = NOFAIL(strdup(optstring));
1854 opts = add_extra_options(modulearg,
1855 opts, modoptions);
1857 read_depends(dirname, aliases->module, &list);
1858 if (handle_module(aliases->module, &list,
1859 newname, remove, opts,
1860 first_time, err,
1861 dry_run, verbose, modoptions,
1862 commands, ignore_commands,
1863 ignore_proc, strip_vermagic,
1864 strip_modversion,
1865 optstring, flags))
1866 was_error = 1;
1868 aliases = aliases->next;
1869 INIT_LIST_HEAD(&list);
1871 } else {
1872 if (use_blacklist
1873 && find_blacklist(modulearg, blacklist))
1874 continue;
1876 if (handle_module(modulearg, &list, newname, remove,
1877 optstring, first_time, error, dry_run,
1878 verbose, modoptions, commands,
1879 ignore_commands, ignore_proc,
1880 strip_vermagic, strip_modversion,
1881 optstring, flags))
1882 was_error = 1;
1885 if (logging)
1886 closelog();
1888 free(dirname);
1889 free(aliasfilename);
1890 free(symfilename);
1891 free(optstring);
1893 if (was_error)
1894 exit(1);
1895 else
1896 exit(0);