release: bump release to v3.7
[mit.git] / modprobe.c
blob1a977145c5e8a51117675e58ba5861cd0ea264b8
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 #include "util.h"
43 #include "zlibsupport.h"
44 #include "logging.h"
45 #include "index.h"
46 #include "list.h"
47 #include "config_filter.h"
49 #include "testing.h"
51 int use_binary_indexes = 1; /* default to enabled. */
53 extern long init_module(void *, unsigned long, const char *);
54 extern long delete_module(const char *, unsigned int);
56 struct module {
57 struct list_head list;
58 char *modname;
59 char filename[0];
62 #ifndef MODULE_DIR
63 #define MODULE_DIR "/lib/modules"
64 #endif
66 typedef void (*errfn_t)(const char *fmt, ...);
68 static void print_usage(const char *progname)
70 fprintf(stderr,
71 "Usage: %s [-v] [-V] [-C config-file] [-d <dirname> ] [-n] [-i] [-q] [-b] [-o <modname>] [ --dump-modversions ] <modname> [parameters...]\n"
72 "%s -r [-n] [-i] [-v] <modulename> ...\n"
73 "%s -l -t <dirname> [ -a <modulename> ...]\n",
74 progname, progname, progname);
75 exit(1);
78 static struct module *find_module(const char *filename, struct list_head *list)
80 struct module *i;
82 list_for_each_entry(i, list, list) {
83 if (streq(i->filename, filename))
84 return i;
86 return NULL;
89 /* We used to lock with a write flock but that allows regular users to block
90 * module load by having a read lock on the module file (no way to bust the
91 * existing locks without killing the offending process). Instead, we now
92 * do the system call/init_module and allow the kernel to fail us instead.
94 static int open_file(const char *filename)
96 int fd = open(filename, O_RDONLY, 0);
98 return fd;
101 static void close_file(int fd)
103 /* Valgrind is picky... */
104 close(fd);
107 static void add_module(char *filename, int namelen, struct list_head *list)
109 struct module *mod;
111 /* If it's a duplicate: move it to the end, so it gets
112 inserted where it is *first* required. */
113 mod = find_module(filename, list);
114 if (mod)
115 list_del(&mod->list);
116 else {
117 /* No match. Create a new module. */
118 mod = NOFAIL(malloc(sizeof(struct module) + namelen + 1));
119 memcpy(mod->filename, filename, namelen);
120 mod->filename[namelen] = '\0';
121 mod->modname = NOFAIL(malloc(namelen + 1));
122 filename2modname(mod->modname, mod->filename);
125 list_add_tail(&mod->list, list);
128 /* Compare len chars of a to b, with _ and - equivalent. */
129 static int modname_equal(const char *a, const char *b, unsigned int len)
131 unsigned int i;
133 if (strlen(b) != len)
134 return 0;
136 for (i = 0; i < len; i++) {
137 if ((a[i] == '_' || a[i] == '-')
138 && (b[i] == '_' || b[i] == '-'))
139 continue;
140 if (a[i] != b[i])
141 return 0;
143 return 1;
146 /* Fills in list of modules if this is the line we want. */
147 static int add_modules_dep_line(char *line,
148 const char *name,
149 struct list_head *list,
150 const char *dirname)
152 char *ptr;
153 int len;
154 char *modname, *fullpath;
156 /* Ignore lines without : or which start with a # */
157 ptr = strchr(line, ':');
158 if (ptr == NULL || line[strspn(line, "\t ")] == '#')
159 return 0;
161 /* Is this the module we are looking for? */
162 *ptr = '\0';
163 if (strrchr(line, '/'))
164 modname = strrchr(line, '/') + 1;
165 else
166 modname = line;
168 len = strlen(modname);
169 if (strchr(modname, '.'))
170 len = strchr(modname, '.') - modname;
171 if (!modname_equal(modname, name, len))
172 return 0;
174 /* Create the list. */
175 if ('/' == line[0]) { /* old style deps - absolute path specified */
176 add_module(line, ptr - line, list);
177 } else {
178 nofail_asprintf(&fullpath, "%s/%s", dirname, line);
179 add_module(fullpath, strlen(dirname)+1+(ptr - line), list);
180 free(fullpath);
183 ptr++;
184 for(;;) {
185 char *dep_start;
186 ptr += strspn(ptr, " \t");
187 if (*ptr == '\0')
188 break;
189 dep_start = ptr;
190 ptr += strcspn(ptr, " \t");
191 if ('/' == dep_start[0]) { /* old style deps */
192 add_module(dep_start, ptr - dep_start, list);
193 } else {
194 nofail_asprintf(&fullpath, "%s/%s", dirname, dep_start);
195 add_module(fullpath,
196 strlen(dirname)+1+(ptr - dep_start), list);
197 free(fullpath);
200 return 1;
203 static int read_depends_file(const char *dirname,
204 const char *start_name,
205 struct list_head *list)
207 char *modules_dep_name;
208 char *line;
209 struct index_file *modules_dep;
211 nofail_asprintf(&modules_dep_name, "%s/%s", dirname, "modules.dep.bin");
212 modules_dep = index_file_open(modules_dep_name);
213 if (!modules_dep) {
214 free(modules_dep_name);
215 return 0;
218 line = index_search(modules_dep, start_name);
219 if (line) {
220 /* Value is standard dependency line format */
221 if (!add_modules_dep_line(line, start_name, list, dirname))
222 fatal("Module index is inconsistent\n");
223 free(line);
226 index_file_close(modules_dep);
227 free(modules_dep_name);
229 return 1;
232 static void read_depends(const char *dirname,
233 const char *start_name,
234 struct list_head *list)
236 char *modules_dep_name;
237 char *line;
238 FILE *modules_dep;
239 int done = 0;
241 if (use_binary_indexes)
242 if (read_depends_file(dirname, start_name, list))
243 return;
245 nofail_asprintf(&modules_dep_name, "%s/%s", dirname, "modules.dep");
246 modules_dep = fopen(modules_dep_name, "r");
247 if (!modules_dep)
248 fatal("Could not load %s: %s\n",
249 modules_dep_name, strerror(errno));
251 /* Stop at first line, as we can have duplicates (eg. symlinks
252 from boot/ */
253 while (!done && (line = getline_wrapped(modules_dep, NULL)) != NULL) {
254 done = add_modules_dep_line(line, start_name, list, dirname);
255 free(line);
257 fclose(modules_dep);
258 free(modules_dep_name);
261 /* We use error numbers in a loose translation... */
262 static const char *insert_moderror(int err)
264 switch (err) {
265 case ENOEXEC:
266 return "Invalid module format";
267 case ENOENT:
268 return "Unknown symbol in module, or unknown parameter (see dmesg)";
269 case ENOSYS:
270 return "Kernel does not have module support";
271 default:
272 return strerror(err);
276 static const char *remove_moderror(int err)
278 switch (err) {
279 case ENOENT:
280 return "No such module";
281 case ENOSYS:
282 return "Kernel does not have module unloading support";
283 default:
284 return strerror(err);
288 static void replace_modname(struct module *module,
289 void *mem, unsigned long len,
290 const char *oldname, const char *newname)
292 char *p;
294 /* 64 - sizeof(unsigned long) - 1 */
295 if (strlen(newname) > 55)
296 fatal("New name %s is too long\n", newname);
298 /* Find where it is in the module structure. Don't assume layout! */
299 for (p = mem; p < (char *)mem + len - strlen(oldname); p++) {
300 if (memcmp(p, oldname, strlen(oldname)) == 0) {
301 strcpy(p, newname);
302 return;
306 warn("Could not find old name in %s to replace!\n", module->filename);
309 static void *get_section32(void *file,
310 unsigned long size,
311 const char *name,
312 unsigned long *secsize)
314 Elf32_Ehdr *hdr = file;
315 Elf32_Shdr *sechdrs = file + hdr->e_shoff;
316 const char *secnames;
317 unsigned int i;
319 /* Too short? */
320 if (size < sizeof(*hdr))
321 return NULL;
322 if (size < hdr->e_shoff + hdr->e_shnum * sizeof(sechdrs[0]))
323 return NULL;
324 if (size < sechdrs[hdr->e_shstrndx].sh_offset)
325 return NULL;
327 secnames = file + sechdrs[hdr->e_shstrndx].sh_offset;
328 for (i = 1; i < hdr->e_shnum; i++)
329 if (streq(secnames + sechdrs[i].sh_name, name)) {
330 *secsize = sechdrs[i].sh_size;
331 return file + sechdrs[i].sh_offset;
333 return NULL;
336 static void *get_section64(void *file,
337 unsigned long size,
338 const char *name,
339 unsigned long *secsize)
341 Elf64_Ehdr *hdr = file;
342 Elf64_Shdr *sechdrs = file + hdr->e_shoff;
343 const char *secnames;
344 unsigned int i;
346 /* Too short? */
347 if (size < sizeof(*hdr))
348 return NULL;
349 if (size < hdr->e_shoff + hdr->e_shnum * sizeof(sechdrs[0]))
350 return NULL;
351 if (size < sechdrs[hdr->e_shstrndx].sh_offset)
352 return NULL;
354 secnames = file + sechdrs[hdr->e_shstrndx].sh_offset;
355 for (i = 1; i < hdr->e_shnum; i++)
356 if (streq(secnames + sechdrs[i].sh_name, name)) {
357 *secsize = sechdrs[i].sh_size;
358 return file + sechdrs[i].sh_offset;
360 return NULL;
363 static int elf_ident(void *mod, unsigned long size)
365 /* "\177ELF" <byte> where byte = 001 for 32-bit, 002 for 64 */
366 char *ident = mod;
368 if (size < EI_CLASS || memcmp(mod, ELFMAG, SELFMAG) != 0)
369 return ELFCLASSNONE;
370 return ident[EI_CLASS];
373 static void *get_section(void *file,
374 unsigned long size,
375 const char *name,
376 unsigned long *secsize)
378 switch (elf_ident(file, size)) {
379 case ELFCLASS32:
380 return get_section32(file, size, name, secsize);
381 case ELFCLASS64:
382 return get_section64(file, size, name, secsize);
383 default:
384 return NULL;
388 static void rename_module(struct module *module,
389 void *mod,
390 unsigned long len,
391 const char *newname)
393 void *modstruct;
394 unsigned long modstruct_len;
396 /* Old-style */
397 modstruct = get_section(mod, len, ".gnu.linkonce.this_module",
398 &modstruct_len);
399 /* New-style */
400 if (!modstruct)
401 modstruct = get_section(mod, len, "__module", &modstruct_len);
402 if (!modstruct)
403 warn("Could not find module name to change in %s\n",
404 module->filename);
405 else
406 replace_modname(module, modstruct, modstruct_len,
407 module->modname, newname);
410 /* Kernel told to ignore these sections if SHF_ALLOC not set. */
411 static void invalidate_section32(void *mod, const char *secname)
413 Elf32_Ehdr *hdr = mod;
414 Elf32_Shdr *sechdrs = mod + hdr->e_shoff;
415 const char *secnames = mod + sechdrs[hdr->e_shstrndx].sh_offset;
416 unsigned int i;
418 for (i = 1; i < hdr->e_shnum; i++)
419 if (streq(secnames+sechdrs[i].sh_name, secname))
420 sechdrs[i].sh_flags &= ~SHF_ALLOC;
423 static void invalidate_section64(void *mod, const char *secname)
425 Elf64_Ehdr *hdr = mod;
426 Elf64_Shdr *sechdrs = mod + hdr->e_shoff;
427 const char *secnames = mod + sechdrs[hdr->e_shstrndx].sh_offset;
428 unsigned int i;
430 for (i = 1; i < hdr->e_shnum; i++)
431 if (streq(secnames+sechdrs[i].sh_name, secname))
432 sechdrs[i].sh_flags &= ~(unsigned long long)SHF_ALLOC;
435 static void strip_section(struct module *module,
436 void *mod,
437 unsigned long len,
438 const char *secname)
440 switch (elf_ident(mod, len)) {
441 case ELFCLASS32:
442 invalidate_section32(mod, secname);
443 break;
444 case ELFCLASS64:
445 invalidate_section64(mod, secname);
446 break;
447 default:
448 warn("Unknown module format in %s: not forcing version\n",
449 module->filename);
453 static void clear_magic(struct module *module, void *mod, unsigned long len)
455 const char *p;
456 unsigned long modlen;
458 /* Old-style: __vermagic section */
459 strip_section(module, mod, len, "__vermagic");
461 /* New-style: in .modinfo section */
462 for (p = get_section(mod, len, ".modinfo", &modlen);
464 p = next_string(p, &modlen)) {
465 if (strstarts(p, "vermagic=")) {
466 memset((char *)p, 0, strlen(p));
467 return;
472 struct module_options
474 struct module_options *next;
475 char *modulename;
476 char *options;
479 struct module_command
481 struct module_command *next;
482 char *modulename;
483 char *command;
486 struct module_alias
488 struct module_alias *next;
489 char *module;
492 struct module_blacklist
494 struct module_blacklist *next;
495 char *modulename;
498 /* Link in a new option line from the config file. */
499 static struct module_options *
500 add_options(const char *modname,
501 const char *option,
502 struct module_options *options)
504 struct module_options *new;
505 char *tab;
507 new = NOFAIL(malloc(sizeof(*new)));
508 new->modulename = NOFAIL(strdup(modname));
509 new->options = NOFAIL(strdup(option));
510 /* We can handle tabs, kernel can't. */
511 for (tab = strchr(new->options, '\t'); tab; tab = strchr(tab, '\t'))
512 *tab = ' ';
513 new->next = options;
514 return new;
517 /* Link in a new install line from the config file. */
518 static struct module_command *
519 add_command(const char *modname,
520 const char *command,
521 struct module_command *commands)
523 struct module_command *new;
525 new = NOFAIL(malloc(sizeof(*new)));
526 new->modulename = NOFAIL(strdup(modname));
527 new->command = NOFAIL(strdup(command));
528 new->next = commands;
529 return new;
532 /* Link in a new alias line from the config file. */
533 static struct module_alias *
534 add_alias(const char *modname, struct module_alias *aliases)
536 struct module_alias *new;
538 new = NOFAIL(malloc(sizeof(*new)));
539 new->module = NOFAIL(strdup(modname));
540 new->next = aliases;
541 return new;
544 /* Link in a new blacklist line from the config file. */
545 static struct module_blacklist *
546 add_blacklist(const char *modname, struct module_blacklist *blacklist)
548 struct module_blacklist *new;
550 new = NOFAIL(malloc(sizeof(*new)));
551 new->modulename = NOFAIL(strdup(modname));
552 new->next = blacklist;
553 return new;
556 /* Find blacklist commands if any. */
557 static int
558 find_blacklist(const char *modname, const struct module_blacklist *blacklist)
560 while (blacklist) {
561 if (streq(blacklist->modulename, modname))
562 return 1;
563 blacklist = blacklist->next;
565 return 0;
568 /* return a new alias list, with backlisted elems filtered out */
569 static struct module_alias *
570 apply_blacklist(const struct module_alias *aliases,
571 const struct module_blacklist *blacklist)
573 struct module_alias *result = NULL;
574 while (aliases) {
575 char *modname = aliases->module;
576 if (!find_blacklist(modname, blacklist))
577 result = add_alias(modname, result);
578 aliases = aliases->next;
580 return result;
583 /* Find install commands if any. */
584 static const char *find_command(const char *modname,
585 const struct module_command *commands)
587 while (commands) {
588 if (fnmatch(commands->modulename, modname, 0) == 0)
589 return commands->command;
590 commands = commands->next;
592 return NULL;
595 static char *append_option(char *options, const char *newoption)
597 options = NOFAIL(realloc(options, strlen(options) + 1
598 + strlen(newoption) + 1));
599 if (strlen(options)) strcat(options, " ");
600 strcat(options, newoption);
601 return options;
604 static char *prepend_option(char *options, const char *newoption)
606 size_t l1, l2;
607 l1 = strlen(options);
608 l2 = strlen(newoption);
609 /* the resulting string will look like
610 * newoption + ' ' + options + '\0' */
611 if (l1) {
612 options = NOFAIL(realloc(options, l2 + 1 + l1 + 1));
613 memmove(options + l2 + 1, options, l1 + 1);
614 options[l2] = ' ';
615 memcpy(options, newoption, l2);
616 } else {
617 options = NOFAIL(realloc(options, l2 + 1));
618 memcpy(options, newoption, l2);
619 options[l2] = '\0';
621 return options;
624 /* Add to options */
625 static char *add_extra_options(const char *modname,
626 char *optstring,
627 const struct module_options *options)
629 while (options) {
630 if (streq(options->modulename, modname))
631 optstring = prepend_option(optstring, options->options);
632 options = options->next;
634 return optstring;
637 /* Read sysfs attribute into a buffer.
638 * returns: 1 = ok, 0 = attribute missing,
639 * -1 = file error (or empty file, but we don't care).
641 static int read_attribute(const char *filename, char *buf, size_t buflen)
643 FILE *file;
644 char *s;
646 file = fopen(filename, "r");
647 if (file == NULL)
648 return (errno == ENOENT) ? 0 : -1;
649 s = fgets(buf, buflen, file);
650 fclose(file);
652 return (s == NULL) ? -1 : 1;
655 /* Is module in /sys/module? If so, fill in usecount if not NULL.
656 0 means no, 1 means yes, -1 means unknown.
658 static int module_in_kernel(const char *modname, unsigned int *usecount)
660 int ret;
661 char *name;
662 struct stat finfo;
664 const int ATTR_LEN = 16;
665 char attr[ATTR_LEN];
667 /* Check sysfs is mounted */
668 if (stat("/sys/module", &finfo) < 0)
669 return -1;
671 /* Find module. */
672 nofail_asprintf(&name, "/sys/module/%s", modname);
673 ret = stat(name, &finfo);
674 free(name);
675 if (ret < 0)
676 return (errno == ENOENT) ? 0 : -1; /* Not found or unknown. */
678 /* Wait for the existing module to either go live or disappear. */
679 nofail_asprintf(&name, "/sys/module/%s/initstate", modname);
680 while (1) {
681 ret = read_attribute(name, attr, ATTR_LEN);
682 if (ret != 1 || streq(attr, "live\n"))
683 break;
685 usleep(100000);
687 free(name);
689 if (ret != 1)
690 return ret;
692 /* Get reference count, if it exists. */
693 if (usecount != NULL) {
694 nofail_asprintf(&name, "/sys/module/%s/refcnt", modname);
695 ret = read_attribute(name, attr, ATTR_LEN);
696 free(name);
697 if (ret == 1)
698 *usecount = atoi(attr);
701 return 1;
704 /* Do an install/remove command: replace $CMDLINE_OPTS if it's specified. */
705 static void do_command(const char *modname,
706 const char *command,
707 int dry_run,
708 errfn_t error,
709 const char *type,
710 const char *cmdline_opts)
712 int ret;
713 char *p, *replaced_cmd = NOFAIL(strdup(command));
715 while ((p = strstr(replaced_cmd, "$CMDLINE_OPTS")) != NULL) {
716 char *new;
717 nofail_asprintf(&new, "%.*s%s%s",
718 (int)(p - replaced_cmd), replaced_cmd, cmdline_opts,
719 p + strlen("$CMDLINE_OPTS"));
720 free(replaced_cmd);
721 replaced_cmd = new;
724 info("%s %s\n", type, replaced_cmd);
725 if (dry_run)
726 return;
728 setenv("MODPROBE_MODULE", modname, 1);
729 ret = system(replaced_cmd);
730 if (ret == -1 || WEXITSTATUS(ret))
731 error("Error running %s command for %s\n", type, modname);
732 free(replaced_cmd);
735 /* Actually do the insert. Frees second arg. */
736 static int insmod(struct list_head *list,
737 char *optstring,
738 const char *newname,
739 int first_time,
740 errfn_t error,
741 int dry_run,
742 const struct module_options *options,
743 const struct module_command *commands,
744 int ignore_commands,
745 int ignore_proc,
746 int strip_vermagic,
747 int strip_modversion,
748 const char *cmdline_opts)
750 int ret, fd;
751 unsigned long len;
752 void *map;
753 const char *command;
754 struct module *mod = list_entry(list->next, struct module, list);
755 int rc = 0;
757 /* Take us off the list. */
758 list_del(&mod->list);
760 /* Do things we (or parent) depend on first. */
761 if (!list_empty(list)) {
762 if ((rc = insmod(list, NOFAIL(strdup("")), NULL, 0, warn,
763 dry_run, options, commands, 0, ignore_proc,
764 strip_vermagic, strip_modversion, "")) != 0) {
765 error("Error inserting %s (%s): %s\n",
766 mod->modname, mod->filename,
767 insert_moderror(errno));
768 goto out_optstring;
772 fd = open_file(mod->filename);
773 if (fd < 0) {
774 error("Could not open '%s': %s\n",
775 mod->filename, strerror(errno));
776 goto out_optstring;
779 /* Don't do ANYTHING if already in kernel. */
780 if (!ignore_proc
781 && module_in_kernel(newname ?: mod->modname, NULL) == 1) {
782 if (first_time)
783 error("Module %s already in kernel.\n",
784 newname ?: mod->modname);
785 goto out_unlock;
788 command = find_command(mod->modname, commands);
789 if (command && !ignore_commands) {
790 close_file(fd);
791 do_command(mod->modname, command, dry_run, error,
792 "install", cmdline_opts);
793 goto out_optstring;
796 map = grab_fd(fd, &len);
797 if (!map) {
798 error("Could not read '%s': %s\n",
799 mod->filename, strerror(errno));
800 goto out_unlock;
803 /* Rename it? */
804 if (newname)
805 rename_module(mod, map, len, newname);
807 if (strip_modversion)
808 strip_section(mod, map, len, "__versions");
809 if (strip_vermagic)
810 clear_magic(mod, map, len);
812 /* Config file might have given more options */
813 optstring = add_extra_options(mod->modname, optstring, options);
815 info("insmod %s %s\n", mod->filename, optstring);
817 if (dry_run)
818 goto out;
820 ret = init_module(map, len, optstring);
821 if (ret != 0) {
822 if (errno == EEXIST) {
823 if (first_time)
824 error("Module %s already in kernel.\n",
825 newname ?: mod->modname);
826 goto out_unlock;
828 /* don't warn noisely if we're loading multiple aliases. */
829 /* one of the aliases may try to use hardware we don't have. */
830 if ((error != warn) || (verbose))
831 error("Error inserting %s (%s): %s\n",
832 mod->modname, mod->filename,
833 insert_moderror(errno));
834 rc = 1;
836 out:
837 release_file(map, len);
838 out_unlock:
839 close_file(fd);
840 out_optstring:
841 free(optstring);
842 return rc;
845 /* Do recursive removal. */
846 static void rmmod(struct list_head *list,
847 const char *name,
848 int first_time,
849 errfn_t error,
850 int dry_run,
851 struct module_command *commands,
852 int ignore_commands,
853 int ignore_inuse,
854 const char *cmdline_opts,
855 int flags)
857 const char *command;
858 unsigned int usecount = 0;
859 struct module *mod = list_entry(list->next, struct module, list);
861 /* Take first one off the list. */
862 list_del(&mod->list);
864 if (!name)
865 name = mod->modname;
867 /* Even if renamed, find commands to orig. name. */
868 command = find_command(mod->modname, commands);
869 if (command && !ignore_commands) {
870 do_command(mod->modname, command, dry_run, error,
871 "remove", cmdline_opts);
872 goto remove_rest;
875 if (module_in_kernel(name, &usecount) == 0)
876 goto nonexistent_module;
878 if (usecount != 0) {
879 if (!ignore_inuse)
880 error("Module %s is in use.\n", name);
881 goto remove_rest;
884 info("rmmod %s\n", mod->filename);
886 if (dry_run)
887 goto remove_rest;
889 if (delete_module(name, O_EXCL) != 0) {
890 if (errno == ENOENT)
891 goto nonexistent_module;
892 error("Error removing %s (%s): %s\n",
893 name, mod->filename,
894 remove_moderror(errno));
897 remove_rest:
898 /* Now do things we depend. */
899 if (!list_empty(list))
900 rmmod(list, NULL, 0, warn, dry_run, commands,
901 0, 1, "", flags);
902 return;
904 nonexistent_module:
905 if (first_time)
906 fatal("Module %s is not in kernel.\n", mod->modname);
907 goto remove_rest;
910 struct modver32_info
912 uint32_t crc;
913 char name[64 - sizeof(uint32_t)];
916 struct modver64_info
918 uint64_t crc;
919 char name[64 - sizeof(uint64_t)];
922 const char *skip_dot(const char *str)
924 /* For our purposes, .foo matches foo. PPC64 needs this. */
925 if (str && str[0] == '.')
926 return str + 1;
927 return str;
930 void dump_modversions(const char *filename, errfn_t error)
932 unsigned long size, secsize;
933 void *file = grab_file(filename, &size);
934 struct modver32_info *info32;
935 struct modver64_info *info64;
936 int n;
938 if (!file) {
939 error("%s: %s\n", filename, strerror(errno));
940 return;
942 switch (elf_ident(file, size)) {
943 case ELFCLASS32:
944 info32 = get_section32(file, size, "__versions", &secsize);
945 if (!info32)
946 return; /* Does not seem to be a kernel module */
947 if (secsize % sizeof(struct modver32_info))
948 error("Wrong section size in %s\n", filename);
949 for (n = 0; n < secsize / sizeof(struct modver32_info); n++)
950 printf("0x%08lx\t%s\n", (unsigned long)
951 info32[n].crc, skip_dot(info32[n].name));
952 break;
954 case ELFCLASS64:
955 info64 = get_section64(file, size, "__versions", &secsize);
956 if (!info64)
957 return; /* Does not seem to be a kernel module */
958 if (secsize % sizeof(struct modver64_info))
959 error("Wrong section size in %s\n", filename);
960 for (n = 0; n < secsize / sizeof(struct modver64_info); n++)
961 printf("0x%08llx\t%s\n", (unsigned long long)
962 info64[n].crc, skip_dot(info64[n].name));
963 break;
965 default:
966 error("%s: ELF class not recognized\n", filename);
971 /* Does path contain directory(s) subpath? */
972 static int type_matches(const char *path, const char *subpath)
974 char *subpath_with_slashes;
975 int ret;
977 nofail_asprintf(&subpath_with_slashes, "/%s/", subpath);
979 ret = (strstr(path, subpath_with_slashes) != NULL);
980 free(subpath_with_slashes);
981 return ret;
985 static int do_wildcard(const char *dirname,
986 const char *type,
987 const char *wildcard)
989 char *modules_dep_name;
990 char *line, *wcard;
991 FILE *modules_dep;
993 /* Canonicalize wildcard */
994 wcard = strdup(wildcard);
995 underscores(wcard);
997 nofail_asprintf(&modules_dep_name, "%s/%s", dirname, "modules.dep");
998 modules_dep = fopen(modules_dep_name, "r");
999 if (!modules_dep)
1000 fatal("Could not load %s: %s\n",
1001 modules_dep_name, strerror(errno));
1003 while ((line = getline_wrapped(modules_dep, NULL)) != NULL) {
1004 char *ptr;
1006 /* Ignore lines without : or which start with a # */
1007 ptr = strchr(line, ':');
1008 if (ptr == NULL || line[strspn(line, "\t ")] == '#')
1009 goto next;
1010 *ptr = '\0';
1012 /* "type" must match complete directory component(s). */
1013 if (!type || type_matches(line, type)) {
1014 char modname[strlen(line)+1];
1016 filename2modname(modname, line);
1017 if (fnmatch(wcard, modname, 0) == 0)
1018 printf("%s\n", line);
1020 next:
1021 free(line);
1024 free(modules_dep_name);
1025 free(wcard);
1026 return 0;
1029 static char *strsep_skipspace(char **string, char *delim)
1031 if (!*string)
1032 return NULL;
1033 *string += strspn(*string, delim);
1034 return strsep(string, delim);
1037 static int parse_config_scan(const char *filename,
1038 const char *name,
1039 int dump_only,
1040 int removing,
1041 struct module_options **options,
1042 struct module_command **commands,
1043 struct module_alias **alias,
1044 struct module_blacklist **blacklist);
1046 static int parse_config_file(const char *filename,
1047 const char *name,
1048 int dump_only,
1049 int removing,
1050 struct module_options **options,
1051 struct module_command **commands,
1052 struct module_alias **aliases,
1053 struct module_blacklist **blacklist)
1055 char *line;
1056 unsigned int linenum = 0;
1057 FILE *cfile;
1059 cfile = fopen(filename, "r");
1060 if (!cfile)
1061 return 0;
1063 while ((line = getline_wrapped(cfile, &linenum)) != NULL) {
1064 char *ptr = line;
1065 char *cmd, *modname;
1067 if (dump_only)
1068 printf("%s\n", line);
1070 cmd = strsep_skipspace(&ptr, "\t ");
1071 if (cmd == NULL || cmd[0] == '#' || cmd[0] == '\0') {
1072 free(line);
1073 continue;
1076 if (streq(cmd, "alias")) {
1077 char *wildcard = strsep_skipspace(&ptr, "\t ");
1078 char *realname = strsep_skipspace(&ptr, "\t ");
1080 if (!wildcard || !realname)
1081 grammar(cmd, filename, linenum);
1082 else if (fnmatch(underscores(wildcard),name,0) == 0)
1083 *aliases = add_alias(underscores(realname), *aliases);
1084 } else if (streq(cmd, "include")) {
1085 struct module_alias *newalias = NULL;
1086 char *newfilename;
1088 newfilename = strsep_skipspace(&ptr, "\t ");
1089 if (!newfilename) {
1090 grammar(cmd, filename, linenum);
1091 } else {
1092 warn("\"include %s\" is deprecated, "
1093 "please use /etc/modprobe.d\n", newfilename);
1094 if (strstarts(newfilename, "/etc/modprobe.d")) {
1095 warn("\"include /etc/modprobe.d\" is "
1096 "the default, ignored\n");
1097 } else {
1098 if (!parse_config_scan(newfilename, name,
1099 dump_only, removing,
1100 options, commands, &newalias,
1101 blacklist))
1102 warn("Failed to open included"
1103 " config file %s: %s\n",
1104 newfilename, strerror(errno));
1106 /* Files included override aliases,
1107 etc that was already set ... */
1108 if (newalias)
1109 *aliases = newalias;
1111 } else if (streq(cmd, "options")) {
1112 modname = strsep_skipspace(&ptr, "\t ");
1113 if (!modname || !ptr)
1114 grammar(cmd, filename, linenum);
1115 else {
1116 ptr += strspn(ptr, "\t ");
1117 *options = add_options(underscores(modname),
1118 ptr, *options);
1120 } else if (streq(cmd, "install")) {
1121 modname = strsep_skipspace(&ptr, "\t ");
1122 if (!modname || !ptr)
1123 grammar(cmd, filename, linenum);
1124 else if (!removing) {
1125 ptr += strspn(ptr, "\t ");
1126 *commands = add_command(underscores(modname),
1127 ptr, *commands);
1129 } else if (streq(cmd, "blacklist")) {
1130 modname = strsep_skipspace(&ptr, "\t ");
1131 if (!modname)
1132 grammar(cmd, filename, linenum);
1133 else if (!removing) {
1134 *blacklist = add_blacklist(underscores(modname),
1135 *blacklist);
1137 } else if (streq(cmd, "remove")) {
1138 modname = strsep_skipspace(&ptr, "\t ");
1139 if (!modname || !ptr)
1140 grammar(cmd, filename, linenum);
1141 else if (removing) {
1142 ptr += strspn(ptr, "\t ");
1143 *commands = add_command(underscores(modname),
1144 ptr, *commands);
1146 } else if (streq(cmd, "config")) {
1147 char *tmp = strsep_skipspace(&ptr, "\t ");
1149 if (!tmp)
1150 grammar(cmd, filename, linenum);
1151 else if (streq(tmp, "binary_indexes")) {
1152 tmp = strsep_skipspace(&ptr, "\t ");
1153 if (streq(tmp, "yes"))
1154 use_binary_indexes = 1;
1155 if (streq(tmp, "no"))
1156 use_binary_indexes = 0;
1158 } else
1159 grammar(cmd, filename, linenum);
1161 free(line);
1163 fclose(cfile);
1164 return 1;
1167 /* fallback to plain-text aliases file as necessary */
1168 static int read_aliases_file(const char *filename,
1169 const char *name,
1170 int dump_only,
1171 int removing,
1172 struct module_options **options,
1173 struct module_command **commands,
1174 struct module_alias **aliases,
1175 struct module_blacklist **blacklist)
1177 struct index_value *realnames;
1178 struct index_value *realname;
1179 char *binfile;
1180 struct index_file *index;
1182 if (!use_binary_indexes)
1183 goto plain_text;
1185 nofail_asprintf(&binfile, "%s.bin", filename);
1186 index = index_file_open(binfile);
1187 if (!index) {
1188 free(binfile);
1189 goto plain_text;
1192 if (dump_only) {
1193 index_dump(index, stdout, "alias ");
1194 free(binfile);
1195 index_file_close(index);
1196 return 1;
1199 realnames = index_searchwild(index, name);
1200 for (realname = realnames; realname; realname = realname->next)
1201 *aliases = add_alias(realname->value, *aliases);
1202 index_values_free(realnames);
1204 free(binfile);
1205 index_file_close(index);
1206 return 1;
1208 plain_text:
1209 return parse_config_file(filename, name, dump_only, removing,
1210 options, commands, aliases, blacklist);
1213 static int parse_config_scan(const char *filename,
1214 const char *name,
1215 int dump_only,
1216 int removing,
1217 struct module_options **options,
1218 struct module_command **commands,
1219 struct module_alias **aliases,
1220 struct module_blacklist **blacklist)
1222 DIR *dir;
1223 int ret = 0;
1225 dir = opendir(filename);
1226 if (dir) {
1227 struct file_entry {
1228 struct list_head node;
1229 char name[];
1231 LIST_HEAD(files_list);
1232 struct file_entry *fe, *fe_tmp;
1233 struct dirent *i;
1235 /* sort files from directory into list */
1236 while ((i = readdir(dir)) != NULL) {
1237 size_t len;
1239 if (i->d_name[0] == '.')
1240 continue;
1241 if (!config_filter(i->d_name))
1242 continue;
1244 len = strlen(i->d_name);
1245 if (len < 6 ||
1246 (strcmp(&i->d_name[len-5], ".conf") != 0 &&
1247 strcmp(&i->d_name[len-6], ".alias") != 0))
1248 warn("All config files need .conf: %s/%s, "
1249 "it will be ignored in a future release.\n",
1250 filename, i->d_name);
1251 fe = malloc(sizeof(struct file_entry) + len + 1);
1252 if (fe == NULL)
1253 continue;
1254 strcpy(fe->name, i->d_name);
1255 list_for_each_entry(fe_tmp, &files_list, node)
1256 if (strcmp(fe_tmp->name, fe->name) >= 0)
1257 break;
1258 list_add_tail(&fe->node, &fe_tmp->node);
1260 closedir(dir);
1262 /* parse list of files */
1263 list_for_each_entry_safe(fe, fe_tmp, &files_list, node) {
1264 char *cfgfile;
1266 nofail_asprintf(&cfgfile, "%s/%s", filename, fe->name);
1267 if (!parse_config_file(cfgfile, name,
1268 dump_only, removing,
1269 options, commands,
1270 aliases, blacklist))
1271 warn("Failed to open config file "
1272 "%s: %s\n", fe->name, strerror(errno));
1273 free(cfgfile);
1274 list_del(&fe->node);
1275 free(fe);
1278 ret = 1;
1279 } else {
1280 if (parse_config_file(filename, name, dump_only, removing,
1281 options, commands, aliases, blacklist))
1282 ret = 1;
1284 return ret;
1287 /* Read binary index file containing aliases only */
1288 static void parse_toplevel_config(const char *filename,
1289 const char *name,
1290 int dump_only,
1291 int removing,
1292 struct module_options **options,
1293 struct module_command **commands,
1294 struct module_alias **aliases,
1295 struct module_blacklist **blacklist)
1297 if (filename) {
1298 if (!parse_config_scan(filename, name, dump_only, removing,
1299 options, commands, aliases, blacklist))
1300 fatal("Failed to open config file %s: %s\n",
1301 filename, strerror(errno));
1302 return;
1305 /* deprecated config file */
1306 if (parse_config_file("/etc/modprobe.conf", name, dump_only, removing,
1307 options, commands, aliases, blacklist) > 0)
1308 warn("Deprecated config file /etc/modprobe.conf, "
1309 "all config files belong into /etc/modprobe.d/.\n");
1311 /* default config */
1312 parse_config_scan("/etc/modprobe.d", name, dump_only, removing,
1313 options, commands, aliases, blacklist);
1316 /* Read possible module arguments from the kernel command line. */
1317 static int parse_kcmdline(int dump_only, struct module_options **options)
1319 char *line;
1320 unsigned int linenum = 0;
1321 FILE *kcmdline;
1323 kcmdline = fopen("/proc/cmdline", "r");
1324 if (!kcmdline)
1325 return 0;
1327 while ((line = getline_wrapped(kcmdline, &linenum)) != NULL) {
1328 char *ptr = line;
1329 char *arg;
1331 while ((arg = strsep_skipspace(&ptr, "\t ")) != NULL) {
1332 char *sep, *modname, *opt;
1334 sep = strchr(arg, '.');
1335 if (sep) {
1336 if (!strchr(sep, '='))
1337 continue;
1338 modname = arg;
1339 *sep = '\0';
1340 opt = ++sep;
1342 if (dump_only)
1343 printf("options %s %s\n", modname, opt);
1345 *options = add_options(underscores(modname),
1346 opt, *options);
1350 free(line);
1352 fclose(kcmdline);
1353 return 1;
1356 static void add_to_env_var(const char *option)
1358 const char *oldenv;
1360 if ((oldenv = getenv("MODPROBE_OPTIONS")) != NULL) {
1361 char *newenv;
1362 nofail_asprintf(&newenv, "%s %s", oldenv, option);
1363 setenv("MODPROBE_OPTIONS", newenv, 1);
1364 } else
1365 setenv("MODPROBE_OPTIONS", option, 1);
1368 /* Prepend options from environment. */
1369 static char **merge_args(char *args, char *argv[], int *argc)
1371 char *arg, *argstring;
1372 char **newargs = NULL;
1373 unsigned int i, num_env = 0;
1375 if (!args)
1376 return argv;
1378 argstring = NOFAIL(strdup(args));
1379 for (arg = strtok(argstring, " "); arg; arg = strtok(NULL, " ")) {
1380 num_env++;
1381 newargs = NOFAIL(realloc(newargs,
1382 sizeof(newargs[0])
1383 * (num_env + *argc + 1)));
1384 newargs[num_env] = arg;
1387 if (!newargs)
1388 return argv;
1390 /* Append commandline args */
1391 newargs[0] = argv[0];
1392 for (i = 1; i <= *argc; i++)
1393 newargs[num_env+i] = argv[i];
1395 *argc += num_env;
1396 return newargs;
1399 static char *gather_options(char *argv[])
1401 char *optstring = NOFAIL(strdup(""));
1403 /* Rest is module options */
1404 while (*argv) {
1405 /* Quote value if it contains spaces. */
1406 unsigned int eq = strcspn(*argv, "=");
1408 if (strchr(*argv+eq, ' ') && !strchr(*argv, '"')) {
1409 char quoted[strlen(*argv) + 3];
1410 (*argv)[eq] = '\0';
1411 sprintf(quoted, "%s=\"%s\"", *argv, *argv+eq+1);
1412 optstring = append_option(optstring, quoted);
1413 } else
1414 optstring = append_option(optstring, *argv);
1415 argv++;
1417 return optstring;
1420 static int handle_module(const char *modname,
1421 struct list_head *todo_list,
1422 const char *newname,
1423 int remove,
1424 char *options,
1425 int first_time,
1426 errfn_t error,
1427 int dry_run,
1428 struct module_options *modoptions,
1429 struct module_command *commands,
1430 int ignore_commands,
1431 int ignore_proc,
1432 int strip_vermagic,
1433 int strip_modversion,
1434 const char *cmdline_opts,
1435 int flags)
1437 if (list_empty(todo_list)) {
1438 const char *command;
1440 /* The dependencies have to be real modules, but
1441 handle case where the first is completely bogus. */
1442 command = find_command(modname, commands);
1443 if (command && !ignore_commands) {
1444 do_command(modname, command, dry_run, error,
1445 remove ? "remove":"install", cmdline_opts);
1446 return 0;
1449 if (!quiet)
1450 error("Module %s not found.\n", modname);
1451 return 1;
1454 if (remove)
1455 rmmod(todo_list, newname, first_time, error, dry_run,
1456 commands, ignore_commands, 0, cmdline_opts, flags);
1457 else
1458 insmod(todo_list, NOFAIL(strdup(options)), newname,
1459 first_time, error, dry_run, modoptions,
1460 commands, ignore_commands, ignore_proc, strip_vermagic,
1461 strip_modversion, cmdline_opts);
1463 return 0;
1466 static struct option options[] = { { "verbose", 0, NULL, 'v' },
1467 { "version", 0, NULL, 'V' },
1468 { "config", 1, NULL, 'C' },
1469 { "name", 1, NULL, 'o' },
1470 { "remove", 0, NULL, 'r' },
1471 { "wait", 0, NULL, 'w' },
1472 { "showconfig", 0, NULL, 'c' },
1473 { "quiet", 0, NULL, 'q' },
1474 { "show", 0, NULL, 'n' },
1475 { "dry-run", 0, NULL, 'n' },
1476 { "syslog", 0, NULL, 's' },
1477 { "type", 1, NULL, 't' },
1478 { "list", 0, NULL, 'l' },
1479 { "all", 0, NULL, 'a' },
1480 { "ignore-install", 0, NULL, 'i' },
1481 { "ignore-remove", 0, NULL, 'i' },
1482 { "force", 0, NULL, 'f' },
1483 { "force-vermagic", 0, NULL, 1 },
1484 { "force-modversion", 0, NULL, 2 },
1485 { "set-version", 1, NULL, 'S' },
1486 { "show-depends", 0, NULL, 'D' },
1487 { "dirname", 1, NULL, 'd' },
1488 { "first-time", 0, NULL, 3 },
1489 { "dump-modversions", 0, NULL, 4 },
1490 { "use-blacklist", 0, NULL, 'b' },
1491 { NULL, 0, NULL, 0 } };
1493 int main(int argc, char *argv[])
1495 struct utsname buf;
1496 struct stat statbuf;
1497 int opt;
1498 int dump_only = 0;
1499 int dry_run = 0;
1500 int remove = 0;
1501 int list_only = 0;
1502 int all = 0;
1503 int ignore_commands = 0;
1504 int strip_vermagic = 0;
1505 int strip_modversion = 0;
1506 int ignore_proc = 0;
1507 int first_time = 0;
1508 int dump_modver = 0;
1509 int use_blacklist = 0;
1510 unsigned int i, num_modules;
1511 char *type = NULL;
1512 const char *config = NULL;
1513 char *dirname = NULL;
1514 char *optstring = NULL;
1515 char *newname = NULL;
1516 char *aliasfilename, *symfilename;
1517 errfn_t error = fatal;
1518 int flags = O_NONBLOCK|O_EXCL;
1519 int was_error = 0;
1521 /* Prepend options from environment. */
1522 argv = merge_args(getenv("MODPROBE_OPTIONS"), argv, &argc);
1524 uname(&buf);
1525 while ((opt = getopt_long(argc, argv, "vVC:o:rnqQsclt:aifbwd:", options, NULL)) != -1){
1526 switch (opt) {
1527 case 'v':
1528 add_to_env_var("-v");
1529 verbose = 1;
1530 break;
1531 case 'V':
1532 puts(PACKAGE " version " VERSION);
1533 exit(0);
1534 case 'S':
1535 strncpy(buf.release, optarg, sizeof(buf.release));
1536 buf.release[sizeof(buf.release)-1] = '\0';
1537 break;
1538 case 'C':
1539 config = optarg;
1540 add_to_env_var("-C");
1541 add_to_env_var(config);
1542 break;
1543 case 'q':
1544 quiet = 1;
1545 add_to_env_var("-q");
1546 break;
1547 case 'D':
1548 dry_run = 1;
1549 ignore_proc = 1;
1550 verbose = 1;
1551 add_to_env_var("-D");
1552 break;
1553 case 'o':
1554 newname = optarg;
1555 break;
1556 case 'r':
1557 remove = 1;
1558 break;
1559 case 'c':
1560 dump_only = 1;
1561 break;
1562 case 't':
1563 type = optarg;
1564 break;
1565 case 'l':
1566 list_only = 1;
1567 break;
1568 case 'a':
1569 all = 1;
1570 error = warn;
1571 break;
1572 case 'n':
1573 dry_run = 1;
1574 break;
1575 case 's':
1576 add_to_env_var("-s");
1577 logging = 1;
1578 break;
1579 case 'i':
1580 ignore_commands = 1;
1581 break;
1582 case 'f':
1583 strip_vermagic = 1;
1584 strip_modversion = 1;
1585 break;
1586 case 'b':
1587 use_blacklist = 1;
1588 break;
1589 case 'w':
1590 flags &= ~O_NONBLOCK;
1591 break;
1592 case 'd':
1593 nofail_asprintf(&dirname, "%s/%s/%s", optarg,
1594 MODULE_DIR, buf.release);
1595 break;
1596 case 1:
1597 strip_vermagic = 1;
1598 break;
1599 case 2:
1600 strip_modversion = 1;
1601 break;
1602 case 3:
1603 first_time = 1;
1604 break;
1605 case 4:
1606 dump_modver = 1;
1607 break;
1608 default:
1609 print_usage(argv[0]);
1613 /* If stderr not open, go to syslog */
1614 if (logging || fstat(STDERR_FILENO, &statbuf) != 0) {
1615 openlog("modprobe", LOG_CONS, LOG_DAEMON);
1616 logging = 1;
1619 if (argc < optind + 1 && !dump_only && !list_only && !remove)
1620 print_usage(argv[0]);
1622 if (!dirname)
1623 nofail_asprintf(&dirname, "%s/%s", MODULE_DIR, buf.release);
1624 nofail_asprintf(&aliasfilename, "%s/modules.alias", dirname);
1625 nofail_asprintf(&symfilename, "%s/modules.symbols", dirname);
1627 /* Old-style -t xxx wildcard? Only with -l. */
1628 if (list_only) {
1629 if (optind+1 < argc)
1630 fatal("Can't have multiple wildcards\n");
1631 /* fprintf(stderr, "man find\n"); return 1; */
1632 return do_wildcard(dirname, type, argv[optind]?:"*");
1634 if (type)
1635 fatal("-t only supported with -l");
1637 if (dump_only) {
1638 struct module_command *commands = NULL;
1639 struct module_options *modoptions = NULL;
1640 struct module_alias *aliases = NULL;
1641 struct module_blacklist *blacklist = NULL;
1643 parse_toplevel_config(config, "", 1, 0, &modoptions,
1644 &commands, &aliases, &blacklist);
1645 /* Read module options from kernel command line */
1646 parse_kcmdline(1, &modoptions);
1647 parse_config_file(aliasfilename, "", 1, 0, &modoptions,
1648 &commands, &aliases, &blacklist);
1649 parse_config_file(symfilename, "", 1, 0, &modoptions,
1650 &commands, &aliases, &blacklist);
1651 exit(0);
1654 if (remove || all) {
1655 num_modules = argc - optind;
1656 optstring = NOFAIL(strdup(""));
1657 } else {
1658 num_modules = 1;
1659 optstring = gather_options(argv+optind+1);
1662 /* num_modules is always 1 except for -r or -a. */
1663 for (i = 0; i < num_modules; i++) {
1664 struct module_command *commands = NULL;
1665 struct module_options *modoptions = NULL;
1666 struct module_alias *aliases = NULL;
1667 struct module_blacklist *blacklist = NULL;
1668 LIST_HEAD(list);
1669 char *modulearg = argv[optind + i];
1671 if (dump_modver) {
1672 dump_modversions(modulearg, error);
1673 continue;
1676 /* Convert name we are looking for */
1677 underscores(modulearg);
1679 /* Returns the resolved alias, options */
1680 parse_toplevel_config(config, modulearg, 0,
1681 remove, &modoptions, &commands, &aliases, &blacklist);
1683 /* Read module options from kernel command line */
1684 parse_kcmdline(0, &modoptions);
1686 /* No luck? Try symbol names, if starts with symbol:. */
1687 if (!aliases && strstarts(modulearg, "symbol:")) {
1688 parse_config_file(symfilename, modulearg, 0,
1689 remove, &modoptions, &commands,
1690 &aliases, &blacklist);
1692 if (!aliases) {
1693 if(!strchr(modulearg, ':'))
1694 read_depends(dirname, modulearg, &list);
1696 /* We only use canned aliases as last resort. */
1697 if (list_empty(&list)
1698 && !find_command(modulearg, commands))
1700 read_aliases_file(aliasfilename,
1701 modulearg, 0, remove,
1702 &modoptions, &commands,
1703 &aliases, &blacklist);
1707 aliases = apply_blacklist(aliases, blacklist);
1708 if (aliases) {
1709 errfn_t err = error;
1711 /* More than one alias? Don't bail out on failure. */
1712 if (aliases->next)
1713 err = warn;
1714 while (aliases) {
1715 /* Add the options for this alias. */
1716 char *opts = NOFAIL(strdup(optstring));
1717 opts = add_extra_options(modulearg,
1718 opts, modoptions);
1720 read_depends(dirname, aliases->module, &list);
1721 if (handle_module(aliases->module, &list,
1722 newname, remove, opts,
1723 first_time, err,
1724 dry_run, modoptions,
1725 commands, ignore_commands,
1726 ignore_proc, strip_vermagic,
1727 strip_modversion,
1728 optstring, flags))
1729 was_error = 1;
1731 aliases = aliases->next;
1732 INIT_LIST_HEAD(&list);
1734 } else {
1735 if (use_blacklist
1736 && find_blacklist(modulearg, blacklist))
1737 continue;
1739 if (handle_module(modulearg, &list, newname, remove,
1740 optstring, first_time, error, dry_run,
1741 modoptions, commands,
1742 ignore_commands, ignore_proc,
1743 strip_vermagic, strip_modversion,
1744 optstring, flags))
1745 was_error = 1;
1748 if (logging)
1749 closelog();
1751 free(dirname);
1752 free(aliasfilename);
1753 free(symfilename);
1754 free(optstring);
1756 if (was_error)
1757 exit(1);
1758 else
1759 exit(0);