depmod: fix return in sort_modules
[mit.git] / modprobe.c
blob4b5189981044ab89d8ec0754c47cdadbbb5aa732
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 static int lock_file(const char *filename)
166 int fd = open(filename, O_RDWR, 0);
168 if (fd >= 0) {
169 struct flock lock;
170 lock.l_type = F_WRLCK;
171 lock.l_whence = SEEK_SET;
172 lock.l_start = 0;
173 lock.l_len = 1;
174 fcntl(fd, F_SETLKW, &lock);
175 } else
176 /* Read-only filesystem? There goes locking... */
177 fd = open(filename, O_RDONLY, 0);
178 return fd;
181 static void unlock_file(int fd)
183 /* Valgrind is picky... */
184 close(fd);
187 static void add_module(char *filename, int namelen, struct list_head *list)
189 struct module *mod;
191 /* If it's a duplicate: move it to the end, so it gets
192 inserted where it is *first* required. */
193 mod = find_module(filename, list);
194 if (mod)
195 list_del(&mod->list);
196 else {
197 /* No match. Create a new module. */
198 mod = NOFAIL(malloc(sizeof(struct module) + namelen + 1));
199 memcpy(mod->filename, filename, namelen);
200 mod->filename[namelen] = '\0';
201 mod->modname = NOFAIL(malloc(namelen + 1));
202 filename2modname(mod->modname, mod->filename);
205 list_add_tail(&mod->list, list);
208 /* Compare len chars of a to b, with _ and - equivalent. */
209 static int modname_equal(const char *a, const char *b, unsigned int len)
211 unsigned int i;
213 if (strlen(b) != len)
214 return 0;
216 for (i = 0; i < len; i++) {
217 if ((a[i] == '_' || a[i] == '-')
218 && (b[i] == '_' || b[i] == '-'))
219 continue;
220 if (a[i] != b[i])
221 return 0;
223 return 1;
226 /* Fills in list of modules if this is the line we want. */
227 static int add_modules_dep_line(char *line,
228 const char *name,
229 struct list_head *list,
230 const char *dirname)
232 char *ptr;
233 int len;
234 char *modname, *fullpath;
236 /* Ignore lines without : or which start with a # */
237 ptr = strchr(line, ':');
238 if (ptr == NULL || line[strspn(line, "\t ")] == '#')
239 return 0;
241 /* Is this the module we are looking for? */
242 *ptr = '\0';
243 if (strrchr(line, '/'))
244 modname = strrchr(line, '/') + 1;
245 else
246 modname = line;
248 len = strlen(modname);
249 if (strchr(modname, '.'))
250 len = strchr(modname, '.') - modname;
251 if (!modname_equal(modname, name, len))
252 return 0;
254 /* Create the list. */
255 if ('/' == line[0]) { /* old style deps - absolute path specified */
256 add_module(line, ptr - line, list);
257 } else {
258 nofail_asprintf(&fullpath, "%s/%s", dirname, line);
259 add_module(fullpath, strlen(dirname)+1+(ptr - line), list);
260 free(fullpath);
263 ptr++;
264 for(;;) {
265 char *dep_start;
266 ptr += strspn(ptr, " \t");
267 if (*ptr == '\0')
268 break;
269 dep_start = ptr;
270 ptr += strcspn(ptr, " \t");
271 if ('/' == dep_start[0]) { /* old style deps */
272 add_module(dep_start, ptr - dep_start, list);
273 } else {
274 nofail_asprintf(&fullpath, "%s/%s", dirname, dep_start);
275 add_module(fullpath,
276 strlen(dirname)+1+(ptr - dep_start), list);
277 free(fullpath);
280 return 1;
283 static int read_depends_bin(const char *dirname,
284 const char *start_name,
285 struct list_head *list)
287 char *modules_dep_name;
288 char *line;
289 FILE *modules_dep;
291 nofail_asprintf(&modules_dep_name, "%s/%s", dirname, "modules.dep.bin");
292 modules_dep = fopen(modules_dep_name, "r");
293 if (!modules_dep) {
294 free(modules_dep_name);
295 return 0;
298 line = index_search(modules_dep, start_name);
299 if (line) {
300 /* Value is standard dependency line format */
301 if (!add_modules_dep_line(line, start_name, list, dirname))
302 fatal("Module index is inconsistent\n");
303 free(line);
306 fclose(modules_dep);
307 free(modules_dep_name);
309 return 1;
312 static void read_depends(const char *dirname,
313 const char *start_name,
314 struct list_head *list)
316 char *modules_dep_name;
317 char *line;
318 FILE *modules_dep;
319 int done = 0;
321 if (read_depends_bin(dirname, start_name, list))
322 return;
324 nofail_asprintf(&modules_dep_name, "%s/%s", dirname, "modules.dep");
325 modules_dep = fopen(modules_dep_name, "r");
326 if (!modules_dep)
327 fatal("Could not load %s: %s\n",
328 modules_dep_name, strerror(errno));
330 /* Stop at first line, as we can have duplicates (eg. symlinks
331 from boot/ */
332 while (!done && (line = getline_wrapped(modules_dep, NULL)) != NULL) {
333 done = add_modules_dep_line(line, start_name, list, dirname);
334 free(line);
336 fclose(modules_dep);
337 free(modules_dep_name);
340 /* We use error numbers in a loose translation... */
341 static const char *insert_moderror(int err)
343 switch (err) {
344 case ENOEXEC:
345 return "Invalid module format";
346 case ENOENT:
347 return "Unknown symbol in module, or unknown parameter (see dmesg)";
348 case ENOSYS:
349 return "Kernel does not have module support";
350 default:
351 return strerror(err);
355 static const char *remove_moderror(int err)
357 switch (err) {
358 case ENOENT:
359 return "No such module";
360 case ENOSYS:
361 return "Kernel does not have module unloading support";
362 default:
363 return strerror(err);
367 static void replace_modname(struct module *module,
368 void *mem, unsigned long len,
369 const char *oldname, const char *newname)
371 char *p;
373 /* 64 - sizeof(unsigned long) - 1 */
374 if (strlen(newname) > 55)
375 fatal("New name %s is too long\n", newname);
377 /* Find where it is in the module structure. Don't assume layout! */
378 for (p = mem; p < (char *)mem + len - strlen(oldname); p++) {
379 if (memcmp(p, oldname, strlen(oldname)) == 0) {
380 strcpy(p, newname);
381 return;
385 warn("Could not find old name in %s to replace!\n", module->filename);
388 static void *get_section32(void *file,
389 unsigned long size,
390 const char *name,
391 unsigned long *secsize)
393 Elf32_Ehdr *hdr = file;
394 Elf32_Shdr *sechdrs = file + hdr->e_shoff;
395 const char *secnames;
396 unsigned int i;
398 /* Too short? */
399 if (size < sizeof(*hdr))
400 return NULL;
401 if (size < hdr->e_shoff + hdr->e_shnum * sizeof(sechdrs[0]))
402 return NULL;
403 if (size < sechdrs[hdr->e_shstrndx].sh_offset)
404 return NULL;
406 secnames = file + sechdrs[hdr->e_shstrndx].sh_offset;
407 for (i = 1; i < hdr->e_shnum; i++)
408 if (strcmp(secnames + sechdrs[i].sh_name, name) == 0) {
409 *secsize = sechdrs[i].sh_size;
410 return file + sechdrs[i].sh_offset;
412 return NULL;
415 static void *get_section64(void *file,
416 unsigned long size,
417 const char *name,
418 unsigned long *secsize)
420 Elf64_Ehdr *hdr = file;
421 Elf64_Shdr *sechdrs = file + hdr->e_shoff;
422 const char *secnames;
423 unsigned int i;
425 /* Too short? */
426 if (size < sizeof(*hdr))
427 return NULL;
428 if (size < hdr->e_shoff + hdr->e_shnum * sizeof(sechdrs[0]))
429 return NULL;
430 if (size < sechdrs[hdr->e_shstrndx].sh_offset)
431 return NULL;
433 secnames = file + sechdrs[hdr->e_shstrndx].sh_offset;
434 for (i = 1; i < hdr->e_shnum; i++)
435 if (strcmp(secnames + sechdrs[i].sh_name, name) == 0) {
436 *secsize = sechdrs[i].sh_size;
437 return file + sechdrs[i].sh_offset;
439 return NULL;
442 static int elf_ident(void *mod, unsigned long size)
444 /* "\177ELF" <byte> where byte = 001 for 32-bit, 002 for 64 */
445 char *ident = mod;
447 if (size < EI_CLASS || memcmp(mod, ELFMAG, SELFMAG) != 0)
448 return ELFCLASSNONE;
449 return ident[EI_CLASS];
452 static void *get_section(void *file,
453 unsigned long size,
454 const char *name,
455 unsigned long *secsize)
457 switch (elf_ident(file, size)) {
458 case ELFCLASS32:
459 return get_section32(file, size, name, secsize);
460 case ELFCLASS64:
461 return get_section64(file, size, name, secsize);
462 default:
463 return NULL;
467 static void rename_module(struct module *module,
468 void *mod,
469 unsigned long len,
470 const char *newname)
472 void *modstruct;
473 unsigned long modstruct_len;
475 /* Old-style */
476 modstruct = get_section(mod, len, ".gnu.linkonce.this_module",
477 &modstruct_len);
478 /* New-style */
479 if (!modstruct)
480 modstruct = get_section(mod, len, "__module", &modstruct_len);
481 if (!modstruct)
482 warn("Could not find module name to change in %s\n",
483 module->filename);
484 else
485 replace_modname(module, modstruct, modstruct_len,
486 module->modname, newname);
489 /* Kernel told to ignore these sections if SHF_ALLOC not set. */
490 static void invalidate_section32(void *mod, const char *secname)
492 Elf32_Ehdr *hdr = mod;
493 Elf32_Shdr *sechdrs = mod + hdr->e_shoff;
494 const char *secnames = mod + sechdrs[hdr->e_shstrndx].sh_offset;
495 unsigned int i;
497 for (i = 1; i < hdr->e_shnum; i++)
498 if (strcmp(secnames+sechdrs[i].sh_name, secname) == 0)
499 sechdrs[i].sh_flags &= ~SHF_ALLOC;
502 static void invalidate_section64(void *mod, const char *secname)
504 Elf64_Ehdr *hdr = mod;
505 Elf64_Shdr *sechdrs = mod + hdr->e_shoff;
506 const char *secnames = mod + sechdrs[hdr->e_shstrndx].sh_offset;
507 unsigned int i;
509 for (i = 1; i < hdr->e_shnum; i++)
510 if (strcmp(secnames+sechdrs[i].sh_name, secname) == 0)
511 sechdrs[i].sh_flags &= ~(unsigned long long)SHF_ALLOC;
514 static void strip_section(struct module *module,
515 void *mod,
516 unsigned long len,
517 const char *secname)
519 switch (elf_ident(mod, len)) {
520 case ELFCLASS32:
521 invalidate_section32(mod, secname);
522 break;
523 case ELFCLASS64:
524 invalidate_section64(mod, secname);
525 break;
526 default:
527 warn("Unknown module format in %s: not forcing version\n",
528 module->filename);
532 static const char *next_string(const char *string, unsigned long *secsize)
534 /* Skip non-zero chars */
535 while (string[0]) {
536 string++;
537 if ((*secsize)-- <= 1)
538 return NULL;
541 /* Skip any zero padding. */
542 while (!string[0]) {
543 string++;
544 if ((*secsize)-- <= 1)
545 return NULL;
547 return string;
550 static void clear_magic(struct module *module, void *mod, unsigned long len)
552 const char *p;
553 unsigned long modlen;
555 /* Old-style: __vermagic section */
556 strip_section(module, mod, len, "__vermagic");
558 /* New-style: in .modinfo section */
559 for (p = get_section(mod, len, ".modinfo", &modlen);
561 p = next_string(p, &modlen)) {
562 if (strncmp(p, "vermagic=", strlen("vermagic=")) == 0) {
563 memset((char *)p, 0, strlen(p));
564 return;
569 struct module_options
571 struct module_options *next;
572 char *modulename;
573 char *options;
576 struct module_command
578 struct module_command *next;
579 char *modulename;
580 char *command;
583 struct module_alias
585 struct module_alias *next;
586 char *module;
589 struct module_blacklist
591 struct module_blacklist *next;
592 char *modulename;
595 /* Link in a new option line from the config file. */
596 static struct module_options *
597 add_options(const char *modname,
598 const char *option,
599 struct module_options *options)
601 struct module_options *new;
602 char *tab;
604 new = NOFAIL(malloc(sizeof(*new)));
605 new->modulename = NOFAIL(strdup(modname));
606 new->options = NOFAIL(strdup(option));
607 /* We can handle tabs, kernel can't. */
608 for (tab = strchr(new->options, '\t'); tab; tab = strchr(tab, '\t'))
609 *tab = ' ';
610 new->next = options;
611 return new;
614 /* Link in a new install line from the config file. */
615 static struct module_command *
616 add_command(const char *modname,
617 const char *command,
618 struct module_command *commands)
620 struct module_command *new;
622 new = NOFAIL(malloc(sizeof(*new)));
623 new->modulename = NOFAIL(strdup(modname));
624 new->command = NOFAIL(strdup(command));
625 new->next = commands;
626 return new;
629 /* Link in a new alias line from the config file. */
630 static struct module_alias *
631 add_alias(const char *modname, struct module_alias *aliases)
633 struct module_alias *new;
635 new = NOFAIL(malloc(sizeof(*new)));
636 new->module = NOFAIL(strdup(modname));
637 new->next = aliases;
638 return new;
641 /* Link in a new blacklist line from the config file. */
642 static struct module_blacklist *
643 add_blacklist(const char *modname, struct module_blacklist *blacklist)
645 struct module_blacklist *new;
647 new = NOFAIL(malloc(sizeof(*new)));
648 new->modulename = NOFAIL(strdup(modname));
649 new->next = blacklist;
650 return new;
653 /* Find blacklist commands if any. */
654 static int
655 find_blacklist(const char *modname, const struct module_blacklist *blacklist)
657 while (blacklist) {
658 if (strcmp(blacklist->modulename, modname) == 0)
659 return 1;
660 blacklist = blacklist->next;
662 return 0;
665 /* return a new alias list, with backlisted elems filtered out */
666 static struct module_alias *
667 apply_blacklist(const struct module_alias *aliases,
668 const struct module_blacklist *blacklist)
670 struct module_alias *result = NULL;
671 while (aliases) {
672 char *modname = aliases->module;
673 if (!find_blacklist(modname, blacklist))
674 result = add_alias(modname, result);
675 aliases = aliases->next;
677 return result;
680 /* Find install commands if any. */
681 static const char *find_command(const char *modname,
682 const struct module_command *commands)
684 while (commands) {
685 if (fnmatch(commands->modulename, modname, 0) == 0)
686 return commands->command;
687 commands = commands->next;
689 return NULL;
692 static char *append_option(char *options, const char *newoption)
694 options = NOFAIL(realloc(options, strlen(options) + 1
695 + strlen(newoption) + 1));
696 if (strlen(options)) strcat(options, " ");
697 strcat(options, newoption);
698 return options;
701 /* Add to options */
702 static char *add_extra_options(const char *modname,
703 char *optstring,
704 const struct module_options *options)
706 while (options) {
707 if (strcmp(options->modulename, modname) == 0)
708 optstring = append_option(optstring, options->options);
709 options = options->next;
711 return optstring;
714 /* Read sysfs attribute into a buffer.
715 * returns: 1 = ok, 0 = attribute missing,
716 * -1 = file error (or empty file, but we don't care).
718 static int read_attribute(const char *filename, char *buf, size_t buflen)
720 FILE *file;
721 char *s;
723 file = fopen(filename, "r");
724 if (file == NULL)
725 return (errno == ENOENT) ? 0 : -1;
726 s = fgets(buf, buflen, file);
727 fclose(file);
729 return (s == NULL) ? -1 : 1;
732 /* Is module in /sys/module? If so, fill in usecount if not NULL.
733 0 means no, 1 means yes, -1 means unknown.
735 static int module_in_kernel(const char *modname, unsigned int *usecount)
737 int ret;
738 char *name;
739 struct stat finfo;
741 const int ATTR_LEN = 16;
742 char attr[ATTR_LEN];
744 /* Find module. We assume sysfs is mounted. */
745 nofail_asprintf(&name, "/sys/module/%s", modname);
746 ret = stat(name, &finfo);
747 free(name);
748 if (ret < 0)
749 return (errno == ENOENT) ? 0 : -1; /* Not found or unknown. */
751 /* Wait for the existing module to either go live or disappear. */
752 nofail_asprintf(&name, "/sys/module/%s/initstate", modname);
753 while (1) {
754 ret = read_attribute(name, attr, ATTR_LEN);
755 if (ret != 1 || streq(attr, "live\n"))
756 break;
758 usleep(100000);
760 free(name);
762 if (ret != 1)
763 return ret;
765 /* Get reference count, if it exists. */
766 if (usecount != NULL) {
767 nofail_asprintf(&name, "/sys/module/%s/refcnt", modname);
768 ret = read_attribute(name, attr, ATTR_LEN);
769 free(name);
770 if (ret == 1)
771 *usecount = atoi(attr);
774 return 1;
777 /* If we don't flush, then child processes print before we do */
778 static void verbose_printf(int verbose, const char *fmt, ...)
780 va_list arglist;
782 if (verbose) {
783 va_start(arglist, fmt);
784 vprintf(fmt, arglist);
785 fflush(stdout);
786 va_end(arglist);
790 /* Do an install/remove command: replace $CMDLINE_OPTS if it's specified. */
791 static void do_command(const char *modname,
792 const char *command,
793 int verbose, int dry_run,
794 errfn_t error,
795 const char *type,
796 const char *cmdline_opts)
798 int ret;
799 char *p, *replaced_cmd = NOFAIL(strdup(command));
801 while ((p = strstr(replaced_cmd, "$CMDLINE_OPTS")) != NULL) {
802 char *new;
803 nofail_asprintf(&new, "%.*s%s%s",
804 (int)(p - replaced_cmd), replaced_cmd, cmdline_opts,
805 p + strlen("$CMDLINE_OPTS"));
806 free(replaced_cmd);
807 replaced_cmd = new;
810 verbose_printf(verbose, "%s %s\n", type, replaced_cmd);
811 if (dry_run)
812 return;
814 setenv("MODPROBE_MODULE", modname, 1);
815 ret = system(replaced_cmd);
816 if (ret == -1 || WEXITSTATUS(ret))
817 error("Error running %s command for %s\n", type, modname);
818 free(replaced_cmd);
821 /* Actually do the insert. Frees second arg. */
822 static void insmod(struct list_head *list,
823 char *optstring,
824 const char *newname,
825 int first_time,
826 errfn_t error,
827 int dry_run,
828 int verbose,
829 const struct module_options *options,
830 const struct module_command *commands,
831 int ignore_commands,
832 int ignore_proc,
833 int strip_vermagic,
834 int strip_modversion,
835 const char *cmdline_opts)
837 int ret, fd;
838 unsigned long len;
839 void *map;
840 const char *command;
841 struct module *mod = list_entry(list->next, struct module, list);
843 /* Take us off the list. */
844 list_del(&mod->list);
846 /* Do things we (or parent) depend on first, but don't die if
847 * they fail. */
848 if (!list_empty(list)) {
849 insmod(list, NOFAIL(strdup("")), NULL, 0, warn,
850 dry_run, verbose, options, commands, 0, ignore_proc,
851 strip_vermagic, strip_modversion, "");
854 /* Lock before we look, in case it's initializing. */
855 fd = lock_file(mod->filename);
856 if (fd < 0) {
857 error("Could not open '%s': %s\n",
858 mod->filename, strerror(errno));
859 goto out_optstring;
862 /* Don't do ANYTHING if already in kernel. */
863 if (!ignore_proc
864 && module_in_kernel(newname ?: mod->modname, NULL) == 1) {
865 if (first_time)
866 error("Module %s already in kernel.\n",
867 newname ?: mod->modname);
868 goto out_unlock;
871 command = find_command(mod->modname, commands);
872 if (command && !ignore_commands) {
873 /* It might recurse: unlock. */
874 unlock_file(fd);
875 do_command(mod->modname, command, verbose, dry_run, error,
876 "install", cmdline_opts);
877 goto out_optstring;
880 map = grab_fd(fd, &len);
881 if (!map) {
882 error("Could not read '%s': %s\n",
883 mod->filename, strerror(errno));
884 goto out_unlock;
887 /* Rename it? */
888 if (newname)
889 rename_module(mod, map, len, newname);
891 if (strip_modversion)
892 strip_section(mod, map, len, "__versions");
893 if (strip_vermagic)
894 clear_magic(mod, map, len);
896 /* Config file might have given more options */
897 optstring = add_extra_options(mod->modname, optstring, options);
899 verbose_printf(verbose, "insmod %s %s\n", mod->filename, optstring);
901 if (dry_run)
902 goto out;
904 ret = init_module(map, len, optstring);
905 if (ret != 0) {
906 if (errno == EEXIST) {
907 if (first_time)
908 error("Module %s already in kernel.\n",
909 newname ?: mod->modname);
910 goto out_unlock;
912 /* don't warn noisely if we're loading multiple aliases. */
913 /* one of the aliases may try to use hardware we don't have. */
914 if ((error != warn) || (verbose))
915 error("Error inserting %s (%s): %s\n",
916 mod->modname, mod->filename,
917 insert_moderror(errno));
919 out:
920 release_file(map, len);
921 out_unlock:
922 unlock_file(fd);
923 out_optstring:
924 free(optstring);
925 return;
928 /* Do recursive removal. */
929 static void rmmod(struct list_head *list,
930 const char *name,
931 int first_time,
932 errfn_t error,
933 int dry_run,
934 int verbose,
935 struct module_command *commands,
936 int ignore_commands,
937 int ignore_inuse,
938 const char *cmdline_opts,
939 int flags)
941 const char *command;
942 unsigned int usecount = 0;
943 int lock;
944 struct module *mod = list_entry(list->next, struct module, list);
946 /* Take first one off the list. */
947 list_del(&mod->list);
949 /* Ignore failure; it's best effort here. */
950 lock = lock_file(mod->filename);
952 if (!name)
953 name = mod->modname;
955 /* Even if renamed, find commands to orig. name. */
956 command = find_command(mod->modname, commands);
957 if (command && !ignore_commands) {
958 /* It might recurse: unlock. */
959 unlock_file(lock);
960 do_command(mod->modname, command, verbose, dry_run, error,
961 "remove", cmdline_opts);
962 goto remove_rest_no_unlock;
965 if (module_in_kernel(name, &usecount) == 0)
966 goto nonexistent_module;
968 if (usecount != 0) {
969 if (!ignore_inuse)
970 error("Module %s is in use.\n", name);
971 goto remove_rest;
974 verbose_printf(verbose, "rmmod %s\n", mod->filename);
976 if (dry_run)
977 goto remove_rest;
979 if (delete_module(name, O_EXCL) != 0) {
980 if (errno == ENOENT)
981 goto nonexistent_module;
982 error("Error removing %s (%s): %s\n",
983 name, mod->filename,
984 remove_moderror(errno));
987 remove_rest:
988 unlock_file(lock);
989 remove_rest_no_unlock:
990 /* Now do things we depend. */
991 if (!list_empty(list))
992 rmmod(list, NULL, 0, warn, dry_run, verbose, commands,
993 0, 1, "", flags);
994 return;
996 nonexistent_module:
997 if (first_time)
998 fatal("Module %s is not in kernel.\n", mod->modname);
999 goto remove_rest;
1002 struct modver32_info
1004 uint32_t crc;
1005 char name[64 - sizeof(uint32_t)];
1008 struct modver64_info
1010 uint64_t crc;
1011 char name[64 - sizeof(uint64_t)];
1014 const char *skip_dot(const char *str)
1016 /* For our purposes, .foo matches foo. PPC64 needs this. */
1017 if (str && str[0] == '.')
1018 return str + 1;
1019 return str;
1022 void dump_modversions(const char *filename, errfn_t error)
1024 unsigned long size, secsize;
1025 void *file = grab_file(filename, &size);
1026 struct modver32_info *info32;
1027 struct modver64_info *info64;
1028 int n;
1030 if (!file) {
1031 error("%s: %s\n", filename, strerror(errno));
1032 return;
1034 switch (elf_ident(file, size)) {
1035 case ELFCLASS32:
1036 info32 = get_section32(file, size, "__versions", &secsize);
1037 if (!info32)
1038 return; /* Does not seem to be a kernel module */
1039 if (secsize % sizeof(struct modver32_info))
1040 error("Wrong section size in %s\n", filename);
1041 for (n = 0; n < secsize / sizeof(struct modver32_info); n++)
1042 printf("0x%08lx\t%s\n", (unsigned long)
1043 info32[n].crc, skip_dot(info32[n].name));
1044 break;
1046 case ELFCLASS64:
1047 info64 = get_section64(file, size, "__versions", &secsize);
1048 if (!info64)
1049 return; /* Does not seem to be a kernel module */
1050 if (secsize % sizeof(struct modver64_info))
1051 error("Wrong section size in %s\n", filename);
1052 for (n = 0; n < secsize / sizeof(struct modver64_info); n++)
1053 printf("0x%08llx\t%s\n", (unsigned long long)
1054 info64[n].crc, skip_dot(info64[n].name));
1055 break;
1057 default:
1058 error("%s: ELF class not recognized\n", filename);
1063 /* Does path contain directory(s) subpath? */
1064 static int type_matches(const char *path, const char *subpath)
1066 char *subpath_with_slashes;
1067 int ret;
1069 nofail_asprintf(&subpath_with_slashes, "/%s/", subpath);
1071 ret = (strstr(path, subpath_with_slashes) != NULL);
1072 free(subpath_with_slashes);
1073 return ret;
1076 /* Careful! Don't munge - in [ ] as per Debian Bug#350915 */
1077 static char *underscores(char *string)
1079 if (string) {
1080 unsigned int i;
1081 int inbracket = 0;
1082 for (i = 0; string[i]; i++) {
1083 switch (string[i]) {
1084 case '[':
1085 inbracket++;
1086 break;
1087 case ']':
1088 inbracket--;
1089 break;
1090 case '-':
1091 if (!inbracket)
1092 string[i] = '_';
1095 if (inbracket)
1096 warn("Unmatched bracket in %s\n", string);
1098 return string;
1101 static int do_wildcard(const char *dirname,
1102 const char *type,
1103 const char *wildcard)
1105 char *modules_dep_name;
1106 char *line, *wcard;
1107 FILE *modules_dep;
1109 /* Canonicalize wildcard */
1110 wcard = strdup(wildcard);
1111 underscores(wcard);
1113 nofail_asprintf(&modules_dep_name, "%s/%s", dirname, "modules.dep");
1114 modules_dep = fopen(modules_dep_name, "r");
1115 if (!modules_dep)
1116 fatal("Could not load %s: %s\n",
1117 modules_dep_name, strerror(errno));
1119 while ((line = getline_wrapped(modules_dep, NULL)) != NULL) {
1120 char *ptr;
1122 /* Ignore lines without : or which start with a # */
1123 ptr = strchr(line, ':');
1124 if (ptr == NULL || line[strspn(line, "\t ")] == '#')
1125 goto next;
1126 *ptr = '\0';
1128 /* "type" must match complete directory component(s). */
1129 if (!type || type_matches(line, type)) {
1130 char modname[strlen(line)+1];
1132 filename2modname(modname, line);
1133 if (fnmatch(wcard, modname, 0) == 0)
1134 printf("%s\n", line);
1136 next:
1137 free(line);
1140 free(modules_dep_name);
1141 free(wcard);
1142 return 0;
1145 static char *strsep_skipspace(char **string, char *delim)
1147 if (!*string)
1148 return NULL;
1149 *string += strspn(*string, delim);
1150 return strsep(string, delim);
1153 /* Recursion */
1154 static int read_config(const char *filename,
1155 const char *name,
1156 int dump_only,
1157 int removing,
1158 struct module_options **options,
1159 struct module_command **commands,
1160 struct module_alias **alias,
1161 struct module_blacklist **blacklist);
1163 /* FIXME: Maybe should be extended to "alias a b [and|or c]...". --RR */
1164 static int read_config_file(const char *filename,
1165 const char *name,
1166 int dump_only,
1167 int removing,
1168 struct module_options **options,
1169 struct module_command **commands,
1170 struct module_alias **aliases,
1171 struct module_blacklist **blacklist)
1173 char *line;
1174 unsigned int linenum = 0;
1175 FILE *cfile;
1177 cfile = fopen(filename, "r");
1178 if (!cfile)
1179 return 0;
1181 while ((line = getline_wrapped(cfile, &linenum)) != NULL) {
1182 char *ptr = line;
1183 char *cmd, *modname;
1185 if (dump_only)
1186 printf("%s\n", line);
1188 cmd = strsep_skipspace(&ptr, "\t ");
1189 if (cmd == NULL || cmd[0] == '#' || cmd[0] == '\0') {
1190 free(line);
1191 continue;
1194 if (strcmp(cmd, "alias") == 0) {
1195 char *wildcard
1196 = underscores(strsep_skipspace(&ptr, "\t "));
1197 char *realname
1198 = underscores(strsep_skipspace(&ptr, "\t "));
1200 if (!wildcard || !realname)
1201 grammar(cmd, filename, linenum);
1202 else if (fnmatch(wildcard,name,0) == 0)
1203 *aliases = add_alias(realname, *aliases);
1204 } else if (strcmp(cmd, "include") == 0) {
1205 struct module_alias *newalias = NULL;
1206 char *newfilename;
1208 newfilename = strsep_skipspace(&ptr, "\t ");
1209 if (!newfilename)
1210 grammar(cmd, filename, linenum);
1211 else {
1212 if (!read_config(newfilename, name,
1213 dump_only, removing,
1214 options, commands, &newalias,
1215 blacklist))
1216 warn("Failed to open included"
1217 " config file %s: %s\n",
1218 newfilename, strerror(errno));
1220 /* Files included override aliases,
1221 etc that was already set ... */
1222 if (newalias)
1223 *aliases = newalias;
1225 } else if (strcmp(cmd, "options") == 0) {
1226 modname = strsep_skipspace(&ptr, "\t ");
1227 if (!modname || !ptr)
1228 grammar(cmd, filename, linenum);
1229 else {
1230 ptr += strspn(ptr, "\t ");
1231 *options = add_options(underscores(modname),
1232 ptr, *options);
1234 } else if (strcmp(cmd, "install") == 0) {
1235 modname = strsep_skipspace(&ptr, "\t ");
1236 if (!modname || !ptr)
1237 grammar(cmd, filename, linenum);
1238 else if (!removing) {
1239 ptr += strspn(ptr, "\t ");
1240 *commands = add_command(underscores(modname),
1241 ptr, *commands);
1243 } else if (strcmp(cmd, "blacklist") == 0) {
1244 modname = strsep_skipspace(&ptr, "\t ");
1245 if (!modname)
1246 grammar(cmd, filename, linenum);
1247 else if (!removing) {
1248 *blacklist = add_blacklist(underscores(modname),
1249 *blacklist);
1251 } else if (strcmp(cmd, "remove") == 0) {
1252 modname = strsep_skipspace(&ptr, "\t ");
1253 if (!modname || !ptr)
1254 grammar(cmd, filename, linenum);
1255 else if (removing) {
1256 ptr += strspn(ptr, "\t ");
1257 *commands = add_command(underscores(modname),
1258 ptr, *commands);
1260 } else if (strcmp(cmd, "config") == 0) {
1261 char *tmp = strsep_skipspace(&ptr, "\t ");
1262 if (strcmp(tmp, "binary_indexes") == 0) {
1263 tmp = strsep_skipspace(&ptr, "\t ");
1264 if (strcmp(tmp, "yes") == 0)
1265 use_binary_indexes = 1;
1266 if (strcmp(tmp, "no") == 0)
1267 use_binary_indexes = 0;
1269 } else
1270 grammar(cmd, filename, linenum);
1272 free(line);
1274 fclose(cfile);
1275 return 1;
1278 /* Simple format, ignore lines starting with #, one command per line.
1279 Returns true or false. */
1280 static int read_config(const char *filename,
1281 const char *name,
1282 int dump_only,
1283 int removing,
1284 struct module_options **options,
1285 struct module_command **commands,
1286 struct module_alias **aliases,
1287 struct module_blacklist **blacklist)
1289 DIR *dir;
1290 int ret = 0;
1292 /* Reiser4 has file/directory duality: treat it as both. */
1293 dir = opendir(filename);
1294 if (dir) {
1295 struct dirent *i;
1296 while ((i = readdir(dir)) != NULL) {
1297 if (!streq(i->d_name,".") && !streq(i->d_name,"..")
1298 && config_filter(i->d_name)) {
1299 char sub[strlen(filename) + 1
1300 + strlen(i->d_name) + 1];
1302 sprintf(sub, "%s/%s", filename, i->d_name);
1303 if (!read_config(sub, name,
1304 dump_only, removing, options,
1305 commands, aliases, blacklist))
1306 warn("Failed to open"
1307 " config file %s: %s\n",
1308 sub, strerror(errno));
1311 closedir(dir);
1312 ret = 1;
1315 if (read_config_file(filename, name, dump_only, removing,
1316 options, commands, aliases, blacklist))
1317 ret = 1;
1319 return ret;
1322 /* Read binary index file containing aliases only */
1323 /* fallback to legacy aliases file as necessary */
1324 static int read_config_file_bin(const char *filename,
1325 const char *name,
1326 int dump_only,
1327 int removing,
1328 struct module_options **options,
1329 struct module_command **commands,
1330 struct module_alias **aliases,
1331 struct module_blacklist **blacklist)
1333 struct index_value *realname;
1334 char *binfile;
1335 FILE *cfile;
1337 nofail_asprintf(&binfile, "%s.bin", filename);
1338 cfile = fopen(binfile, "r");
1339 if (!cfile) {
1340 free(binfile);
1342 return read_config_file(filename, name, dump_only, removing,
1343 options, commands, aliases, blacklist);
1346 if (dump_only) {
1347 index_dump(cfile, stdout, "alias ");
1348 free(binfile);
1349 fclose(cfile);
1350 return 1;
1353 realname = index_searchwild(cfile, name);
1354 while(realname) {
1355 struct index_value *next = realname->next;
1356 *aliases = add_alias(realname->value, *aliases);
1358 free(realname);
1359 realname = next;
1362 free(binfile);
1363 fclose(cfile);
1364 return 1;
1367 static const char *default_configs[] =
1369 "/etc/modprobe.conf",
1370 "/etc/modprobe.d",
1373 static void read_toplevel_config(const char *filename,
1374 const char *name,
1375 int dump_only,
1376 int removing,
1377 struct module_options **options,
1378 struct module_command **commands,
1379 struct module_alias **aliases,
1380 struct module_blacklist **blacklist)
1382 unsigned int i;
1384 if (filename) {
1385 if (!read_config(filename, name, dump_only, removing,
1386 options, commands, aliases, blacklist))
1387 fatal("Failed to open config file %s: %s\n",
1388 filename, strerror(errno));
1389 return;
1392 /* Try defaults. */
1393 for (i = 0; i < ARRAY_SIZE(default_configs); i++) {
1394 read_config(default_configs[i], name, dump_only, removing,
1395 options, commands, aliases, blacklist);
1399 /* Read possible module arguments from the kernel command line. */
1400 static int read_kcmdline(int dump_only, struct module_options **options)
1402 char *line;
1403 unsigned int linenum = 0;
1404 FILE *kcmdline;
1406 kcmdline = fopen("/proc/cmdline", "r");
1407 if (!kcmdline)
1408 return 0;
1410 while ((line = getline_wrapped(kcmdline, &linenum)) != NULL) {
1411 char *ptr = line;
1412 char *arg;
1414 while ((arg = strsep_skipspace(&ptr, "\t ")) != NULL) {
1415 char *sep, *modname, *opt;
1417 sep = strchr(arg, '.');
1418 if (sep) {
1419 if (!strchr(sep, '='))
1420 continue;
1421 modname = arg;
1422 *sep = '\0';
1423 opt = ++sep;
1425 if (dump_only)
1426 printf("options %s %s\n", modname, opt);
1428 *options = add_options(underscores(modname),
1429 opt, *options);
1433 free(line);
1435 fclose(kcmdline);
1436 return 1;
1439 static void add_to_env_var(const char *option)
1441 const char *oldenv;
1443 if ((oldenv = getenv("MODPROBE_OPTIONS")) != NULL) {
1444 char *newenv;
1445 nofail_asprintf(&newenv, "%s %s", oldenv, option);
1446 setenv("MODPROBE_OPTIONS", newenv, 1);
1447 } else
1448 setenv("MODPROBE_OPTIONS", option, 1);
1451 /* Prepend options from environment. */
1452 static char **merge_args(char *args, char *argv[], int *argc)
1454 char *arg, *argstring;
1455 char **newargs = NULL;
1456 unsigned int i, num_env = 0;
1458 if (!args)
1459 return argv;
1461 argstring = NOFAIL(strdup(args));
1462 for (arg = strtok(argstring, " "); arg; arg = strtok(NULL, " ")) {
1463 num_env++;
1464 newargs = NOFAIL(realloc(newargs,
1465 sizeof(newargs[0])
1466 * (num_env + *argc + 1)));
1467 newargs[num_env] = arg;
1470 /* Append commandline args */
1471 newargs[0] = argv[0];
1472 for (i = 1; i <= *argc; i++)
1473 newargs[num_env+i] = argv[i];
1475 *argc += num_env;
1476 return newargs;
1479 static char *gather_options(char *argv[])
1481 char *optstring = NOFAIL(strdup(""));
1483 /* Rest is module options */
1484 while (*argv) {
1485 /* Quote value if it contains spaces. */
1486 unsigned int eq = strcspn(*argv, "=");
1488 if (strchr(*argv+eq, ' ') && !strchr(*argv, '"')) {
1489 char quoted[strlen(*argv) + 3];
1490 (*argv)[eq] = '\0';
1491 sprintf(quoted, "%s=\"%s\"", *argv, *argv+eq+1);
1492 optstring = append_option(optstring, quoted);
1493 } else
1494 optstring = append_option(optstring, *argv);
1495 argv++;
1497 return optstring;
1500 static void handle_module(const char *modname,
1501 struct list_head *todo_list,
1502 const char *newname,
1503 int remove,
1504 char *options,
1505 int first_time,
1506 errfn_t error,
1507 int dry_run,
1508 int verbose,
1509 struct module_options *modoptions,
1510 struct module_command *commands,
1511 int ignore_commands,
1512 int ignore_proc,
1513 int strip_vermagic,
1514 int strip_modversion,
1515 int unknown_silent,
1516 const char *cmdline_opts,
1517 int flags)
1519 struct stat finfo;
1521 if (stat("/sys/module", &finfo) < 0)
1522 fatal("/sys is not mounted.\n");
1524 if (list_empty(todo_list)) {
1525 const char *command;
1527 /* The dependencies have to be real modules, but
1528 handle case where the first is completely bogus. */
1529 command = find_command(modname, commands);
1530 if (command && !ignore_commands) {
1531 do_command(modname, command, verbose, dry_run, error,
1532 remove ? "remove":"install", cmdline_opts);
1533 return;
1536 if (unknown_silent)
1537 exit(1);
1538 error("Module %s not found.\n", modname);
1539 return;
1542 if (remove)
1543 rmmod(todo_list, newname, first_time, error, dry_run, verbose,
1544 commands, ignore_commands, 0, cmdline_opts, flags);
1545 else
1546 insmod(todo_list, NOFAIL(strdup(options)), newname,
1547 first_time, error, dry_run, verbose, modoptions,
1548 commands, ignore_commands, ignore_proc, strip_vermagic,
1549 strip_modversion, cmdline_opts);
1552 static struct option options[] = { { "verbose", 0, NULL, 'v' },
1553 { "version", 0, NULL, 'V' },
1554 { "config", 1, NULL, 'C' },
1555 { "name", 1, NULL, 'o' },
1556 { "remove", 0, NULL, 'r' },
1557 { "wait", 0, NULL, 'w' },
1558 { "showconfig", 0, NULL, 'c' },
1559 { "autoclean", 0, NULL, 'k' },
1560 { "quiet", 0, NULL, 'q' },
1561 { "show", 0, NULL, 'n' },
1562 { "dry-run", 0, NULL, 'n' },
1563 { "syslog", 0, NULL, 's' },
1564 { "type", 1, NULL, 't' },
1565 { "list", 0, NULL, 'l' },
1566 { "all", 0, NULL, 'a' },
1567 { "ignore-install", 0, NULL, 'i' },
1568 { "ignore-remove", 0, NULL, 'i' },
1569 { "force", 0, NULL, 'f' },
1570 { "force-vermagic", 0, NULL, 1 },
1571 { "force-modversion", 0, NULL, 2 },
1572 { "set-version", 1, NULL, 'S' },
1573 { "show-depends", 0, NULL, 'D' },
1574 { "dirname", 1, NULL, 'd' },
1575 { "first-time", 0, NULL, 3 },
1576 { "dump-modversions", 0, NULL, 4 },
1577 { "use-blacklist", 0, NULL, 'b' },
1578 { NULL, 0, NULL, 0 } };
1580 int main(int argc, char *argv[])
1582 struct utsname buf;
1583 struct stat statbuf;
1584 int opt;
1585 int dump_only = 0;
1586 int dry_run = 0;
1587 int remove = 0;
1588 int verbose = 0;
1589 int unknown_silent = 0;
1590 int list_only = 0;
1591 int all = 0;
1592 int ignore_commands = 0;
1593 int strip_vermagic = 0;
1594 int strip_modversion = 0;
1595 int ignore_proc = 0;
1596 int first_time = 0;
1597 int dump_modver = 0;
1598 int use_blacklist = 0;
1599 unsigned int i, num_modules;
1600 char *type = NULL;
1601 const char *config = NULL;
1602 char *dirname = NULL;
1603 char *optstring = NULL;
1604 char *newname = NULL;
1605 char *aliasfilename, *symfilename;
1606 errfn_t error = fatal;
1607 int flags = O_NONBLOCK|O_EXCL;
1609 /* Prepend options from environment. */
1610 argv = merge_args(getenv("MODPROBE_OPTIONS"), argv, &argc);
1612 uname(&buf);
1613 while ((opt = getopt_long(argc, argv, "vVC:o:rknqQsclt:aifbwd:", options, NULL)) != -1){
1614 switch (opt) {
1615 case 'v':
1616 add_to_env_var("-v");
1617 verbose = 1;
1618 break;
1619 case 'V':
1620 puts(PACKAGE " version " VERSION);
1621 exit(0);
1622 case 'S':
1623 strncpy(buf.release, optarg, sizeof(buf.release));
1624 buf.release[sizeof(buf.release)-1] = '\0';
1625 break;
1626 case 'C':
1627 config = optarg;
1628 add_to_env_var("-C");
1629 add_to_env_var(config);
1630 break;
1631 case 'q':
1632 unknown_silent = 1;
1633 add_to_env_var("-q");
1634 break;
1635 case 'D':
1636 dry_run = 1;
1637 ignore_proc = 1;
1638 verbose = 1;
1639 add_to_env_var("-D");
1640 break;
1641 case 'o':
1642 newname = optarg;
1643 break;
1644 case 'r':
1645 remove = 1;
1646 break;
1647 case 'c':
1648 dump_only = 1;
1649 break;
1650 case 't':
1651 type = optarg;
1652 break;
1653 case 'l':
1654 list_only = 1;
1655 break;
1656 case 'a':
1657 all = 1;
1658 error = warn;
1659 break;
1660 case 'k':
1661 /* FIXME: This should actually do something */
1662 break;
1663 case 'n':
1664 dry_run = 1;
1665 break;
1666 case 's':
1667 add_to_env_var("-s");
1668 logging = 1;
1669 break;
1670 case 'i':
1671 ignore_commands = 1;
1672 break;
1673 case 'f':
1674 strip_vermagic = 1;
1675 strip_modversion = 1;
1676 break;
1677 case 'b':
1678 use_blacklist = 1;
1679 break;
1680 case 'w':
1681 flags &= ~O_NONBLOCK;
1682 break;
1683 case 'd':
1684 nofail_asprintf(&dirname, "%s/%s/%s", optarg,
1685 MODULE_DIR, buf.release);
1686 break;
1687 case 1:
1688 strip_vermagic = 1;
1689 break;
1690 case 2:
1691 strip_modversion = 1;
1692 break;
1693 case 3:
1694 first_time = 1;
1695 break;
1696 case 4:
1697 dump_modver = 1;
1698 break;
1699 default:
1700 print_usage(argv[0]);
1704 /* If stderr not open, go to syslog */
1705 if (logging || fstat(STDERR_FILENO, &statbuf) != 0) {
1706 openlog("modprobe", LOG_CONS, LOG_DAEMON);
1707 logging = 1;
1710 if (argc < optind + 1 && !dump_only && !list_only && !remove)
1711 print_usage(argv[0]);
1713 if (!dirname)
1714 nofail_asprintf(&dirname, "%s/%s", MODULE_DIR, buf.release);
1715 nofail_asprintf(&aliasfilename, "%s/modules.alias", dirname);
1716 nofail_asprintf(&symfilename, "%s/modules.symbols", dirname);
1718 /* Old-style -t xxx wildcard? Only with -l. */
1719 if (list_only) {
1720 if (optind+1 < argc)
1721 fatal("Can't have multiple wildcards\n");
1722 /* fprintf(stderr, "man find\n"); return 1; */
1723 return do_wildcard(dirname, type, argv[optind]?:"*");
1725 if (type)
1726 fatal("-t only supported with -l");
1728 if (dump_only) {
1729 struct module_command *commands = NULL;
1730 struct module_options *modoptions = NULL;
1731 struct module_alias *aliases = NULL;
1732 struct module_blacklist *blacklist = NULL;
1734 read_toplevel_config(config, "", 1, 0,
1735 &modoptions, &commands, &aliases, &blacklist);
1736 read_kcmdline(1, &modoptions);
1737 if (use_binary_indexes) {
1738 read_config_file_bin(aliasfilename, "", 1, 0,
1739 &modoptions, &commands, &aliases, &blacklist);
1740 read_config_file_bin(symfilename, "", 1, 0,
1741 &modoptions, &commands, &aliases, &blacklist);
1742 } else {
1743 read_config(aliasfilename, "", 1, 0,
1744 &modoptions, &commands, &aliases, &blacklist);
1745 read_config(symfilename, "", 1, 0,
1746 &modoptions, &commands, &aliases, &blacklist);
1748 exit(0);
1751 if (remove || all) {
1752 num_modules = argc - optind;
1753 optstring = NOFAIL(strdup(""));
1754 } else {
1755 num_modules = 1;
1756 optstring = gather_options(argv+optind+1);
1759 /* num_modules is always 1 except for -r or -a. */
1760 for (i = 0; i < num_modules; i++) {
1761 struct module_command *commands = NULL;
1762 struct module_options *modoptions = NULL;
1763 struct module_alias *aliases = NULL;
1764 struct module_blacklist *blacklist = NULL;
1765 LIST_HEAD(list);
1766 char *modulearg = argv[optind + i];
1768 if (dump_modver) {
1769 dump_modversions(modulearg, error);
1770 continue;
1773 /* Convert name we are looking for */
1774 underscores(modulearg);
1776 /* Returns the resolved alias, options */
1777 read_toplevel_config(config, modulearg, 0,
1778 remove, &modoptions, &commands, &aliases, &blacklist);
1780 /* Read module options from kernel command line */
1781 read_kcmdline(0, &modoptions);
1783 /* No luck? Try symbol names, if starts with symbol:. */
1784 if (!aliases
1785 && strncmp(modulearg, "symbol:", strlen("symbol:")) == 0) {
1786 if (use_binary_indexes)
1787 read_config_file_bin(symfilename, modulearg, 0,
1788 remove, &modoptions, &commands,
1789 &aliases, &blacklist);
1790 else
1791 read_config(symfilename, modulearg, 0,
1792 remove, &modoptions, &commands,
1793 &aliases, &blacklist);
1795 if (!aliases) {
1796 if(!strchr(modulearg, ':'))
1797 read_depends(dirname, modulearg, &list);
1799 /* We only use canned aliases as last resort. */
1800 if (list_empty(&list)
1801 && !find_command(modulearg, commands))
1803 if (use_binary_indexes)
1804 read_config_file_bin(aliasfilename,
1805 modulearg, 0, remove,
1806 &modoptions, &commands,
1807 &aliases, &blacklist);
1808 else
1809 read_config(aliasfilename,
1810 modulearg, 0, remove,
1811 &modoptions, &commands,
1812 &aliases, &blacklist);
1816 aliases = apply_blacklist(aliases, blacklist);
1817 if (aliases) {
1818 errfn_t err = error;
1820 /* More than one alias? Don't bail out on failure. */
1821 if (aliases->next)
1822 err = warn;
1823 while (aliases) {
1824 /* Add the options for this alias. */
1825 char *opts = NOFAIL(strdup(optstring));
1826 opts = add_extra_options(modulearg,
1827 opts, modoptions);
1829 read_depends(dirname, aliases->module, &list);
1830 handle_module(aliases->module, &list, newname,
1831 remove, opts, first_time, err,
1832 dry_run, verbose, modoptions,
1833 commands, ignore_commands,
1834 ignore_proc, strip_vermagic,
1835 strip_modversion,
1836 unknown_silent,
1837 optstring, flags);
1839 aliases = aliases->next;
1840 INIT_LIST_HEAD(&list);
1842 } else {
1843 if (use_blacklist
1844 && find_blacklist(modulearg, blacklist))
1845 continue;
1847 handle_module(modulearg, &list, newname, remove,
1848 optstring, first_time, error, dry_run,
1849 verbose, modoptions, commands,
1850 ignore_commands, ignore_proc,
1851 strip_vermagic, strip_modversion,
1852 unknown_silent, optstring, flags);
1855 if (logging)
1856 closelog();
1858 free(dirname);
1859 free(aliasfilename);
1860 free(symfilename);
1861 free(optstring);
1863 return 0;