Sort modules according to modules.order
[mit.git] / modprobe.c
blobfadc30d2dcff934cb4438a38954d9f5eced31ea0
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 == strstr(line, dirname)) { /* old style deps */
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 == strstr(dep_start, dirname)) {
272 /* old style deps */
273 add_module(dep_start, ptr - dep_start, list);
274 } else {
275 nofail_asprintf(&fullpath, "%s/%s", dirname, dep_start);
276 add_module(fullpath,
277 strlen(dirname)+1+(ptr - dep_start), list);
278 free(fullpath);
281 return 1;
284 static int read_depends_bin(const char *dirname,
285 const char *start_name,
286 struct list_head *list)
288 char *modules_dep_name;
289 char *line;
290 FILE *modules_dep;
292 nofail_asprintf(&modules_dep_name, "%s/%s", dirname, "modules.dep.bin");
293 modules_dep = fopen(modules_dep_name, "r");
294 if (!modules_dep) {
295 free(modules_dep_name);
296 return 0;
299 line = index_search(modules_dep, start_name);
300 if (line) {
301 /* Value is standard dependency line format */
302 if (!add_modules_dep_line(line, start_name, list, dirname))
303 fatal("Module index is inconsistent\n");
304 free(line);
307 fclose(modules_dep);
308 free(modules_dep_name);
310 return 1;
313 static void read_depends(const char *dirname,
314 const char *start_name,
315 struct list_head *list)
317 char *modules_dep_name;
318 char *line;
319 FILE *modules_dep;
320 int done = 0;
322 if (read_depends_bin(dirname, start_name, list))
323 return;
325 nofail_asprintf(&modules_dep_name, "%s/%s", dirname, "modules.dep");
326 modules_dep = fopen(modules_dep_name, "r");
327 if (!modules_dep)
328 fatal("Could not load %s: %s\n",
329 modules_dep_name, strerror(errno));
331 /* Stop at first line, as we can have duplicates (eg. symlinks
332 from boot/ */
333 while (!done && (line = getline_wrapped(modules_dep, NULL)) != NULL) {
334 done = add_modules_dep_line(line, start_name, list, dirname);
335 free(line);
337 fclose(modules_dep);
338 free(modules_dep_name);
341 /* We use error numbers in a loose translation... */
342 static const char *insert_moderror(int err)
344 switch (err) {
345 case ENOEXEC:
346 return "Invalid module format";
347 case ENOENT:
348 return "Unknown symbol in module, or unknown parameter (see dmesg)";
349 case ENOSYS:
350 return "Kernel does not have module support";
351 default:
352 return strerror(err);
356 static const char *remove_moderror(int err)
358 switch (err) {
359 case ENOENT:
360 return "No such module";
361 case ENOSYS:
362 return "Kernel does not have module unloading support";
363 default:
364 return strerror(err);
368 static void replace_modname(struct module *module,
369 void *mem, unsigned long len,
370 const char *oldname, const char *newname)
372 char *p;
374 /* 64 - sizeof(unsigned long) - 1 */
375 if (strlen(newname) > 55)
376 fatal("New name %s is too long\n", newname);
378 /* Find where it is in the module structure. Don't assume layout! */
379 for (p = mem; p < (char *)mem + len - strlen(oldname); p++) {
380 if (memcmp(p, oldname, strlen(oldname)) == 0) {
381 strcpy(p, newname);
382 return;
386 warn("Could not find old name in %s to replace!\n", module->filename);
389 static void *get_section32(void *file,
390 unsigned long size,
391 const char *name,
392 unsigned long *secsize)
394 Elf32_Ehdr *hdr = file;
395 Elf32_Shdr *sechdrs = file + hdr->e_shoff;
396 const char *secnames;
397 unsigned int i;
399 /* Too short? */
400 if (size < sizeof(*hdr))
401 return NULL;
402 if (size < hdr->e_shoff + hdr->e_shnum * sizeof(sechdrs[0]))
403 return NULL;
404 if (size < sechdrs[hdr->e_shstrndx].sh_offset)
405 return NULL;
407 secnames = file + sechdrs[hdr->e_shstrndx].sh_offset;
408 for (i = 1; i < hdr->e_shnum; i++)
409 if (strcmp(secnames + sechdrs[i].sh_name, name) == 0) {
410 *secsize = sechdrs[i].sh_size;
411 return file + sechdrs[i].sh_offset;
413 return NULL;
416 static void *get_section64(void *file,
417 unsigned long size,
418 const char *name,
419 unsigned long *secsize)
421 Elf64_Ehdr *hdr = file;
422 Elf64_Shdr *sechdrs = file + hdr->e_shoff;
423 const char *secnames;
424 unsigned int i;
426 /* Too short? */
427 if (size < sizeof(*hdr))
428 return NULL;
429 if (size < hdr->e_shoff + hdr->e_shnum * sizeof(sechdrs[0]))
430 return NULL;
431 if (size < sechdrs[hdr->e_shstrndx].sh_offset)
432 return NULL;
434 secnames = file + sechdrs[hdr->e_shstrndx].sh_offset;
435 for (i = 1; i < hdr->e_shnum; i++)
436 if (strcmp(secnames + sechdrs[i].sh_name, name) == 0) {
437 *secsize = sechdrs[i].sh_size;
438 return file + sechdrs[i].sh_offset;
440 return NULL;
443 static int elf_ident(void *mod, unsigned long size)
445 /* "\177ELF" <byte> where byte = 001 for 32-bit, 002 for 64 */
446 char *ident = mod;
448 if (size < EI_CLASS || memcmp(mod, ELFMAG, SELFMAG) != 0)
449 return ELFCLASSNONE;
450 return ident[EI_CLASS];
453 static void *get_section(void *file,
454 unsigned long size,
455 const char *name,
456 unsigned long *secsize)
458 switch (elf_ident(file, size)) {
459 case ELFCLASS32:
460 return get_section32(file, size, name, secsize);
461 case ELFCLASS64:
462 return get_section64(file, size, name, secsize);
463 default:
464 return NULL;
468 static void rename_module(struct module *module,
469 void *mod,
470 unsigned long len,
471 const char *newname)
473 void *modstruct;
474 unsigned long modstruct_len;
476 /* Old-style */
477 modstruct = get_section(mod, len, ".gnu.linkonce.this_module",
478 &modstruct_len);
479 /* New-style */
480 if (!modstruct)
481 modstruct = get_section(mod, len, "__module", &modstruct_len);
482 if (!modstruct)
483 warn("Could not find module name to change in %s\n",
484 module->filename);
485 else
486 replace_modname(module, modstruct, modstruct_len,
487 module->modname, newname);
490 /* Kernel told to ignore these sections if SHF_ALLOC not set. */
491 static void invalidate_section32(void *mod, const char *secname)
493 Elf32_Ehdr *hdr = mod;
494 Elf32_Shdr *sechdrs = mod + hdr->e_shoff;
495 const char *secnames = mod + sechdrs[hdr->e_shstrndx].sh_offset;
496 unsigned int i;
498 for (i = 1; i < hdr->e_shnum; i++)
499 if (strcmp(secnames+sechdrs[i].sh_name, secname) == 0)
500 sechdrs[i].sh_flags &= ~SHF_ALLOC;
503 static void invalidate_section64(void *mod, const char *secname)
505 Elf64_Ehdr *hdr = mod;
506 Elf64_Shdr *sechdrs = mod + hdr->e_shoff;
507 const char *secnames = mod + sechdrs[hdr->e_shstrndx].sh_offset;
508 unsigned int i;
510 for (i = 1; i < hdr->e_shnum; i++)
511 if (strcmp(secnames+sechdrs[i].sh_name, secname) == 0)
512 sechdrs[i].sh_flags &= ~(unsigned long long)SHF_ALLOC;
515 static void strip_section(struct module *module,
516 void *mod,
517 unsigned long len,
518 const char *secname)
520 switch (elf_ident(mod, len)) {
521 case ELFCLASS32:
522 invalidate_section32(mod, secname);
523 break;
524 case ELFCLASS64:
525 invalidate_section64(mod, secname);
526 break;
527 default:
528 warn("Unknown module format in %s: not forcing version\n",
529 module->filename);
533 static const char *next_string(const char *string, unsigned long *secsize)
535 /* Skip non-zero chars */
536 while (string[0]) {
537 string++;
538 if ((*secsize)-- <= 1)
539 return NULL;
542 /* Skip any zero padding. */
543 while (!string[0]) {
544 string++;
545 if ((*secsize)-- <= 1)
546 return NULL;
548 return string;
551 static void clear_magic(struct module *module, void *mod, unsigned long len)
553 const char *p;
554 unsigned long modlen;
556 /* Old-style: __vermagic section */
557 strip_section(module, mod, len, "__vermagic");
559 /* New-style: in .modinfo section */
560 for (p = get_section(mod, len, ".modinfo", &modlen);
562 p = next_string(p, &modlen)) {
563 if (strncmp(p, "vermagic=", strlen("vermagic=")) == 0) {
564 memset((char *)p, 0, strlen(p));
565 return;
570 struct module_options
572 struct module_options *next;
573 char *modulename;
574 char *options;
577 struct module_command
579 struct module_command *next;
580 char *modulename;
581 char *command;
584 struct module_alias
586 struct module_alias *next;
587 char *module;
590 struct module_blacklist
592 struct module_blacklist *next;
593 char *modulename;
596 /* Link in a new option line from the config file. */
597 static struct module_options *
598 add_options(const char *modname,
599 const char *option,
600 struct module_options *options)
602 struct module_options *new;
603 char *tab;
605 new = NOFAIL(malloc(sizeof(*new)));
606 new->modulename = NOFAIL(strdup(modname));
607 new->options = NOFAIL(strdup(option));
608 /* We can handle tabs, kernel can't. */
609 for (tab = strchr(new->options, '\t'); tab; tab = strchr(tab, '\t'))
610 *tab = ' ';
611 new->next = options;
612 return new;
615 /* Link in a new install line from the config file. */
616 static struct module_command *
617 add_command(const char *modname,
618 const char *command,
619 struct module_command *commands)
621 struct module_command *new;
623 new = NOFAIL(malloc(sizeof(*new)));
624 new->modulename = NOFAIL(strdup(modname));
625 new->command = NOFAIL(strdup(command));
626 new->next = commands;
627 return new;
630 /* Link in a new alias line from the config file. */
631 static struct module_alias *
632 add_alias(const char *modname, struct module_alias *aliases)
634 struct module_alias *new;
636 new = NOFAIL(malloc(sizeof(*new)));
637 new->module = NOFAIL(strdup(modname));
638 new->next = aliases;
639 return new;
642 /* Link in a new blacklist line from the config file. */
643 static struct module_blacklist *
644 add_blacklist(const char *modname, struct module_blacklist *blacklist)
646 struct module_blacklist *new;
648 new = NOFAIL(malloc(sizeof(*new)));
649 new->modulename = NOFAIL(strdup(modname));
650 new->next = blacklist;
651 return new;
654 /* Find blacklist commands if any. */
655 static int
656 find_blacklist(const char *modname, const struct module_blacklist *blacklist)
658 while (blacklist) {
659 if (strcmp(blacklist->modulename, modname) == 0)
660 return 1;
661 blacklist = blacklist->next;
663 return 0;
666 /* return a new alias list, with backlisted elems filtered out */
667 static struct module_alias *
668 apply_blacklist(const struct module_alias *aliases,
669 const struct module_blacklist *blacklist)
671 struct module_alias *result = NULL;
672 while (aliases) {
673 char *modname = aliases->module;
674 if (!find_blacklist(modname, blacklist))
675 result = add_alias(modname, result);
676 aliases = aliases->next;
678 return result;
681 /* Find install commands if any. */
682 static const char *find_command(const char *modname,
683 const struct module_command *commands)
685 while (commands) {
686 if (fnmatch(commands->modulename, modname, 0) == 0)
687 return commands->command;
688 commands = commands->next;
690 return NULL;
693 static char *append_option(char *options, const char *newoption)
695 options = NOFAIL(realloc(options, strlen(options) + 1
696 + strlen(newoption) + 1));
697 if (strlen(options)) strcat(options, " ");
698 strcat(options, newoption);
699 return options;
702 /* Add to options */
703 static char *add_extra_options(const char *modname,
704 char *optstring,
705 const struct module_options *options)
707 while (options) {
708 if (strcmp(options->modulename, modname) == 0)
709 optstring = append_option(optstring, options->options);
710 options = options->next;
712 return optstring;
715 /* Read sysfs attribute into a buffer.
716 * returns: 1 = ok, 0 = attribute missing,
717 * -1 = file error (or empty file, but we don't care).
719 static int read_attribute(const char *filename, char *buf, size_t buflen)
721 FILE *file;
722 char *s;
724 file = fopen(filename, "r");
725 if (file == NULL)
726 return (errno == ENOENT) ? 0 : -1;
727 s = fgets(buf, buflen, file);
728 fclose(file);
730 return (s == NULL) ? -1 : 1;
733 /* Is module in /sys/module? If so, fill in usecount if not NULL.
734 0 means no, 1 means yes, -1 means unknown.
736 static int module_in_kernel(const char *modname, unsigned int *usecount)
738 int ret;
739 char *name;
740 struct stat finfo;
742 const int ATTR_LEN = 16;
743 char attr[ATTR_LEN];
745 /* Find module. We assume sysfs is mounted. */
746 nofail_asprintf(&name, "/sys/module/%s", modname);
747 ret = stat(name, &finfo);
748 free(name);
749 if (ret < 0)
750 return (errno == ENOENT) ? 0 : -1; /* Not found or unknown. */
752 /* Wait for the existing module to either go live or disappear. */
753 nofail_asprintf(&name, "/sys/module/%s/initstate", modname);
754 while (1) {
755 ret = read_attribute(name, attr, ATTR_LEN);
756 if (ret != 1 || streq(attr, "live\n"))
757 break;
759 usleep(100000);
761 free(name);
763 if (ret != 1)
764 return ret;
766 /* Get reference count. */
767 if (usecount != NULL) {
768 nofail_asprintf(&name, "/sys/module/%s/refcnt", modname);
769 ret = read_attribute(name, attr, ATTR_LEN);
770 free(name);
771 if (ret != 1)
772 return ret;
773 *usecount = atoi(attr);
776 return 1;
779 /* If we don't flush, then child processes print before we do */
780 static void verbose_printf(int verbose, const char *fmt, ...)
782 va_list arglist;
784 if (verbose) {
785 va_start(arglist, fmt);
786 vprintf(fmt, arglist);
787 fflush(stdout);
788 va_end(arglist);
792 /* Do an install/remove command: replace $CMDLINE_OPTS if it's specified. */
793 static void do_command(const char *modname,
794 const char *command,
795 int verbose, int dry_run,
796 errfn_t error,
797 const char *type,
798 const char *cmdline_opts)
800 int ret;
801 char *p, *replaced_cmd = NOFAIL(strdup(command));
803 while ((p = strstr(replaced_cmd, "$CMDLINE_OPTS")) != NULL) {
804 char *new;
805 nofail_asprintf(&new, "%.*s%s%s",
806 (int)(p - replaced_cmd), replaced_cmd, cmdline_opts,
807 p + strlen("$CMDLINE_OPTS"));
808 free(replaced_cmd);
809 replaced_cmd = new;
812 verbose_printf(verbose, "%s %s\n", type, replaced_cmd);
813 if (dry_run)
814 return;
816 setenv("MODPROBE_MODULE", modname, 1);
817 ret = system(replaced_cmd);
818 if (ret == -1 || WEXITSTATUS(ret))
819 error("Error running %s command for %s\n", type, modname);
820 free(replaced_cmd);
823 /* Actually do the insert. Frees second arg. */
824 static void insmod(struct list_head *list,
825 char *optstring,
826 const char *newname,
827 int first_time,
828 errfn_t error,
829 int dry_run,
830 int verbose,
831 const struct module_options *options,
832 const struct module_command *commands,
833 int ignore_commands,
834 int ignore_proc,
835 int strip_vermagic,
836 int strip_modversion,
837 const char *cmdline_opts)
839 int ret, fd;
840 unsigned long len;
841 void *map;
842 const char *command;
843 struct module *mod = list_entry(list->next, struct module, list);
845 /* Take us off the list. */
846 list_del(&mod->list);
848 /* Do things we (or parent) depend on first, but don't die if
849 * they fail. */
850 if (!list_empty(list)) {
851 insmod(list, NOFAIL(strdup("")), NULL, 0, warn,
852 dry_run, verbose, options, commands, 0, ignore_proc,
853 strip_vermagic, strip_modversion, "");
856 /* Lock before we look, in case it's initializing. */
857 fd = lock_file(mod->filename);
858 if (fd < 0) {
859 error("Could not open '%s': %s\n",
860 mod->filename, strerror(errno));
861 goto out_optstring;
864 /* Don't do ANYTHING if already in kernel. */
865 if (!ignore_proc
866 && module_in_kernel(newname ?: mod->modname, NULL) == 1) {
867 if (first_time)
868 error("Module %s already in kernel.\n",
869 newname ?: mod->modname);
870 goto out_unlock;
873 command = find_command(mod->modname, commands);
874 if (command && !ignore_commands) {
875 /* It might recurse: unlock. */
876 unlock_file(fd);
877 do_command(mod->modname, command, verbose, dry_run, error,
878 "install", cmdline_opts);
879 goto out_optstring;
882 map = grab_fd(fd, &len);
883 if (!map) {
884 error("Could not read '%s': %s\n",
885 mod->filename, strerror(errno));
886 goto out_unlock;
889 /* Rename it? */
890 if (newname)
891 rename_module(mod, map, len, newname);
893 if (strip_modversion)
894 strip_section(mod, map, len, "__versions");
895 if (strip_vermagic)
896 clear_magic(mod, map, len);
898 /* Config file might have given more options */
899 optstring = add_extra_options(mod->modname, optstring, options);
901 verbose_printf(verbose, "insmod %s %s\n", mod->filename, optstring);
903 if (dry_run)
904 goto out;
906 ret = init_module(map, len, optstring);
907 if (ret != 0) {
908 if (errno == EEXIST) {
909 if (first_time)
910 error("Module %s already in kernel.\n",
911 newname ?: mod->modname);
912 goto out_unlock;
914 /* don't warn noisely if we're loading multiple aliases. */
915 /* one of the aliases may try to use hardware we don't have. */
916 if ((error != warn) || (verbose))
917 error("Error inserting %s (%s): %s\n",
918 mod->modname, mod->filename,
919 insert_moderror(errno));
921 out:
922 release_file(map, len);
923 out_unlock:
924 unlock_file(fd);
925 out_optstring:
926 free(optstring);
927 return;
930 /* Do recursive removal. */
931 static void rmmod(struct list_head *list,
932 const char *name,
933 int first_time,
934 errfn_t error,
935 int dry_run,
936 int verbose,
937 struct module_command *commands,
938 int ignore_commands,
939 int ignore_inuse,
940 const char *cmdline_opts,
941 int flags)
943 const char *command;
944 unsigned int usecount = 0;
945 int lock;
946 struct module *mod = list_entry(list->next, struct module, list);
948 /* Take first one off the list. */
949 list_del(&mod->list);
951 /* Ignore failure; it's best effort here. */
952 lock = lock_file(mod->filename);
954 if (!name)
955 name = mod->modname;
957 /* Even if renamed, find commands to orig. name. */
958 command = find_command(mod->modname, commands);
959 if (command && !ignore_commands) {
960 /* It might recurse: unlock. */
961 unlock_file(lock);
962 do_command(mod->modname, command, verbose, dry_run, error,
963 "remove", cmdline_opts);
964 goto remove_rest_no_unlock;
967 if (module_in_kernel(name, &usecount) == 0)
968 goto nonexistent_module;
970 if (usecount != 0) {
971 if (!ignore_inuse)
972 error("Module %s is in use.\n", name);
973 goto remove_rest;
976 verbose_printf(verbose, "rmmod %s\n", mod->filename);
978 if (dry_run)
979 goto remove_rest;
981 if (delete_module(name, O_EXCL) != 0) {
982 if (errno == ENOENT)
983 goto nonexistent_module;
984 error("Error removing %s (%s): %s\n",
985 name, mod->filename,
986 remove_moderror(errno));
989 remove_rest:
990 unlock_file(lock);
991 remove_rest_no_unlock:
992 /* Now do things we depend. */
993 if (!list_empty(list))
994 rmmod(list, NULL, 0, warn, dry_run, verbose, commands,
995 0, 1, "", flags);
996 return;
998 nonexistent_module:
999 if (first_time)
1000 fatal("Module %s is not in kernel.\n", mod->modname);
1001 goto remove_rest;
1004 struct modver32_info
1006 uint32_t crc;
1007 char name[64 - sizeof(uint32_t)];
1010 struct modver64_info
1012 uint64_t crc;
1013 char name[64 - sizeof(uint64_t)];
1016 const char *skip_dot(const char *str)
1018 /* For our purposes, .foo matches foo. PPC64 needs this. */
1019 if (str && str[0] == '.')
1020 return str + 1;
1021 return str;
1024 void dump_modversions(const char *filename, errfn_t error)
1026 unsigned long size, secsize;
1027 void *file = grab_file(filename, &size);
1028 struct modver32_info *info32;
1029 struct modver64_info *info64;
1030 int n;
1032 if (!file) {
1033 error("%s: %s\n", filename, strerror(errno));
1034 return;
1036 switch (elf_ident(file, size)) {
1037 case ELFCLASS32:
1038 info32 = get_section32(file, size, "__versions", &secsize);
1039 if (!info32)
1040 return; /* Does not seem to be a kernel module */
1041 if (secsize % sizeof(struct modver32_info))
1042 error("Wrong section size in %s\n", filename);
1043 for (n = 0; n < secsize / sizeof(struct modver32_info); n++)
1044 printf("0x%08lx\t%s\n", (unsigned long)
1045 info32[n].crc, skip_dot(info32[n].name));
1046 break;
1048 case ELFCLASS64:
1049 info64 = get_section64(file, size, "__versions", &secsize);
1050 if (!info64)
1051 return; /* Does not seem to be a kernel module */
1052 if (secsize % sizeof(struct modver64_info))
1053 error("Wrong section size in %s\n", filename);
1054 for (n = 0; n < secsize / sizeof(struct modver64_info); n++)
1055 printf("0x%08llx\t%s\n", (unsigned long long)
1056 info64[n].crc, skip_dot(info64[n].name));
1057 break;
1059 default:
1060 error("%s: ELF class not recognized\n", filename);
1065 /* Does path contain directory(s) subpath? */
1066 static int type_matches(const char *path, const char *subpath)
1068 char *subpath_with_slashes;
1069 int ret;
1071 nofail_asprintf(&subpath_with_slashes, "/%s/", subpath);
1073 ret = (strstr(path, subpath_with_slashes) != NULL);
1074 free(subpath_with_slashes);
1075 return ret;
1078 /* Careful! Don't munge - in [ ] as per Debian Bug#350915 */
1079 static char *underscores(char *string)
1081 if (string) {
1082 unsigned int i;
1083 int inbracket = 0;
1084 for (i = 0; string[i]; i++) {
1085 switch (string[i]) {
1086 case '[':
1087 inbracket++;
1088 break;
1089 case ']':
1090 inbracket--;
1091 break;
1092 case '-':
1093 if (!inbracket)
1094 string[i] = '_';
1097 if (inbracket)
1098 warn("Unmatched bracket in %s\n", string);
1100 return string;
1103 static int do_wildcard(const char *dirname,
1104 const char *type,
1105 const char *wildcard)
1107 char *modules_dep_name;
1108 char *line, *wcard;
1109 FILE *modules_dep;
1111 /* Canonicalize wildcard */
1112 wcard = strdup(wildcard);
1113 underscores(wcard);
1115 nofail_asprintf(&modules_dep_name, "%s/%s", dirname, "modules.dep");
1116 modules_dep = fopen(modules_dep_name, "r");
1117 if (!modules_dep)
1118 fatal("Could not load %s: %s\n",
1119 modules_dep_name, strerror(errno));
1121 while ((line = getline_wrapped(modules_dep, NULL)) != NULL) {
1122 char *ptr;
1124 /* Ignore lines without : or which start with a # */
1125 ptr = strchr(line, ':');
1126 if (ptr == NULL || line[strspn(line, "\t ")] == '#')
1127 goto next;
1128 *ptr = '\0';
1130 /* "type" must match complete directory component(s). */
1131 if (!type || type_matches(line, type)) {
1132 char modname[strlen(line)+1];
1134 filename2modname(modname, line);
1135 if (fnmatch(wcard, modname, 0) == 0)
1136 printf("%s\n", line);
1138 next:
1139 free(line);
1142 free(modules_dep_name);
1143 free(wcard);
1144 return 0;
1147 static char *strsep_skipspace(char **string, char *delim)
1149 if (!*string)
1150 return NULL;
1151 *string += strspn(*string, delim);
1152 return strsep(string, delim);
1155 /* Recursion */
1156 static int read_config(const char *filename,
1157 const char *name,
1158 int dump_only,
1159 int removing,
1160 struct module_options **options,
1161 struct module_command **commands,
1162 struct module_alias **alias,
1163 struct module_blacklist **blacklist);
1165 /* FIXME: Maybe should be extended to "alias a b [and|or c]...". --RR */
1166 static int read_config_file(const char *filename,
1167 const char *name,
1168 int dump_only,
1169 int removing,
1170 struct module_options **options,
1171 struct module_command **commands,
1172 struct module_alias **aliases,
1173 struct module_blacklist **blacklist)
1175 char *line;
1176 unsigned int linenum = 0;
1177 FILE *cfile;
1179 cfile = fopen(filename, "r");
1180 if (!cfile)
1181 return 0;
1183 while ((line = getline_wrapped(cfile, &linenum)) != NULL) {
1184 char *ptr = line;
1185 char *cmd, *modname;
1187 if (dump_only)
1188 printf("%s\n", line);
1190 cmd = strsep_skipspace(&ptr, "\t ");
1191 if (cmd == NULL || cmd[0] == '#' || cmd[0] == '\0') {
1192 free(line);
1193 continue;
1196 if (strcmp(cmd, "alias") == 0) {
1197 char *wildcard
1198 = underscores(strsep_skipspace(&ptr, "\t "));
1199 char *realname
1200 = underscores(strsep_skipspace(&ptr, "\t "));
1202 if (!wildcard || !realname)
1203 grammar(cmd, filename, linenum);
1204 else if (fnmatch(wildcard,name,0) == 0)
1205 *aliases = add_alias(realname, *aliases);
1206 } else if (strcmp(cmd, "include") == 0) {
1207 struct module_alias *newalias = NULL;
1208 char *newfilename;
1210 newfilename = strsep_skipspace(&ptr, "\t ");
1211 if (!newfilename)
1212 grammar(cmd, filename, linenum);
1213 else {
1214 if (!read_config(newfilename, name,
1215 dump_only, removing,
1216 options, commands, &newalias,
1217 blacklist))
1218 warn("Failed to open included"
1219 " config file %s: %s\n",
1220 newfilename, strerror(errno));
1222 /* Files included override aliases,
1223 etc that was already set ... */
1224 if (newalias)
1225 *aliases = newalias;
1227 } else if (strcmp(cmd, "options") == 0) {
1228 modname = strsep_skipspace(&ptr, "\t ");
1229 if (!modname || !ptr)
1230 grammar(cmd, filename, linenum);
1231 else {
1232 ptr += strspn(ptr, "\t ");
1233 *options = add_options(underscores(modname),
1234 ptr, *options);
1236 } else if (strcmp(cmd, "install") == 0) {
1237 modname = strsep_skipspace(&ptr, "\t ");
1238 if (!modname || !ptr)
1239 grammar(cmd, filename, linenum);
1240 else if (!removing) {
1241 ptr += strspn(ptr, "\t ");
1242 *commands = add_command(underscores(modname),
1243 ptr, *commands);
1245 } else if (strcmp(cmd, "blacklist") == 0) {
1246 modname = strsep_skipspace(&ptr, "\t ");
1247 if (!modname)
1248 grammar(cmd, filename, linenum);
1249 else if (!removing) {
1250 *blacklist = add_blacklist(underscores(modname),
1251 *blacklist);
1253 } else if (strcmp(cmd, "remove") == 0) {
1254 modname = strsep_skipspace(&ptr, "\t ");
1255 if (!modname || !ptr)
1256 grammar(cmd, filename, linenum);
1257 else if (removing) {
1258 ptr += strspn(ptr, "\t ");
1259 *commands = add_command(underscores(modname),
1260 ptr, *commands);
1262 } else if (strcmp(cmd, "config") == 0) {
1263 char *tmp = strsep_skipspace(&ptr, "\t ");
1264 if (strcmp(tmp, "binary_indexes") == 0) {
1265 tmp = strsep_skipspace(&ptr, "\t ");
1266 if (strcmp(tmp, "yes") == 0)
1267 use_binary_indexes = 1;
1268 if (strcmp(tmp, "no") == 0)
1269 use_binary_indexes = 0;
1271 } else
1272 grammar(cmd, filename, linenum);
1274 free(line);
1276 fclose(cfile);
1277 return 1;
1280 /* Simple format, ignore lines starting with #, one command per line.
1281 Returns true or false. */
1282 static int read_config(const char *filename,
1283 const char *name,
1284 int dump_only,
1285 int removing,
1286 struct module_options **options,
1287 struct module_command **commands,
1288 struct module_alias **aliases,
1289 struct module_blacklist **blacklist)
1291 DIR *dir;
1292 int ret = 0;
1294 /* Reiser4 has file/directory duality: treat it as both. */
1295 dir = opendir(filename);
1296 if (dir) {
1297 struct dirent *i;
1298 while ((i = readdir(dir)) != NULL) {
1299 if (!streq(i->d_name,".") && !streq(i->d_name,"..")
1300 && config_filter(i->d_name)) {
1301 char sub[strlen(filename) + 1
1302 + strlen(i->d_name) + 1];
1304 sprintf(sub, "%s/%s", filename, i->d_name);
1305 if (!read_config(sub, name,
1306 dump_only, removing, options,
1307 commands, aliases, blacklist))
1308 warn("Failed to open"
1309 " config file %s: %s\n",
1310 sub, strerror(errno));
1313 closedir(dir);
1314 ret = 1;
1317 if (read_config_file(filename, name, dump_only, removing,
1318 options, commands, aliases, blacklist))
1319 ret = 1;
1321 return ret;
1324 /* Read binary index file containing aliases only */
1325 /* fallback to legacy aliases file as necessary */
1326 static int read_config_file_bin(const char *filename,
1327 const char *name,
1328 int dump_only,
1329 int removing,
1330 struct module_options **options,
1331 struct module_command **commands,
1332 struct module_alias **aliases,
1333 struct module_blacklist **blacklist)
1335 struct index_value *realname;
1336 char *binfile;
1337 FILE *cfile;
1339 nofail_asprintf(&binfile, "%s.bin", filename);
1340 cfile = fopen(binfile, "r");
1341 if (!cfile) {
1342 free(binfile);
1344 return read_config_file(filename, name, dump_only, removing,
1345 options, commands, aliases, blacklist);
1348 if (dump_only) {
1349 index_dump(cfile, stdout, "alias ");
1350 free(binfile);
1351 fclose(cfile);
1352 return 1;
1355 realname = index_searchwild(cfile, name);
1356 while(realname) {
1357 struct index_value *next = realname->next;
1358 *aliases = add_alias(realname->value, *aliases);
1360 free(realname);
1361 realname = next;
1364 free(binfile);
1365 fclose(cfile);
1366 return 1;
1369 static const char *default_configs[] =
1371 "/etc/modprobe.conf",
1372 "/etc/modprobe.d",
1375 static void read_toplevel_config(const char *filename,
1376 const char *name,
1377 int dump_only,
1378 int removing,
1379 struct module_options **options,
1380 struct module_command **commands,
1381 struct module_alias **aliases,
1382 struct module_blacklist **blacklist)
1384 unsigned int i;
1386 if (filename) {
1387 if (!read_config(filename, name, dump_only, removing,
1388 options, commands, aliases, blacklist))
1389 fatal("Failed to open config file %s: %s\n",
1390 filename, strerror(errno));
1391 return;
1394 /* Try defaults. */
1395 for (i = 0; i < ARRAY_SIZE(default_configs); i++) {
1396 read_config(default_configs[i], name, dump_only, removing,
1397 options, commands, aliases, blacklist);
1401 /* Read possible module arguments from the kernel command line. */
1402 static int read_kcmdline(int dump_only, struct module_options **options)
1404 char *line;
1405 unsigned int linenum = 0;
1406 FILE *kcmdline;
1408 kcmdline = fopen("/proc/cmdline", "r");
1409 if (!kcmdline)
1410 return 0;
1412 while ((line = getline_wrapped(kcmdline, &linenum)) != NULL) {
1413 char *ptr = line;
1414 char *arg;
1416 while ((arg = strsep_skipspace(&ptr, "\t ")) != NULL) {
1417 char *sep, *modname, *opt;
1419 sep = strchr(arg, '.');
1420 if (sep) {
1421 if (!strchr(sep, '='))
1422 continue;
1423 modname = arg;
1424 *sep = '\0';
1425 opt = ++sep;
1427 if (dump_only)
1428 printf("options %s %s\n", modname, opt);
1430 *options = add_options(underscores(modname),
1431 opt, *options);
1435 free(line);
1437 fclose(kcmdline);
1438 return 1;
1441 static void add_to_env_var(const char *option)
1443 const char *oldenv;
1445 if ((oldenv = getenv("MODPROBE_OPTIONS")) != NULL) {
1446 char *newenv;
1447 nofail_asprintf(&newenv, "%s %s", oldenv, option);
1448 setenv("MODPROBE_OPTIONS", newenv, 1);
1449 } else
1450 setenv("MODPROBE_OPTIONS", option, 1);
1453 /* Prepend options from environment. */
1454 static char **merge_args(char *args, char *argv[], int *argc)
1456 char *arg, *argstring;
1457 char **newargs = NULL;
1458 unsigned int i, num_env = 0;
1460 if (!args)
1461 return argv;
1463 argstring = NOFAIL(strdup(args));
1464 for (arg = strtok(argstring, " "); arg; arg = strtok(NULL, " ")) {
1465 num_env++;
1466 newargs = NOFAIL(realloc(newargs,
1467 sizeof(newargs[0])
1468 * (num_env + *argc + 1)));
1469 newargs[num_env] = arg;
1472 /* Append commandline args */
1473 newargs[0] = argv[0];
1474 for (i = 1; i <= *argc; i++)
1475 newargs[num_env+i] = argv[i];
1477 *argc += num_env;
1478 return newargs;
1481 static char *gather_options(char *argv[])
1483 char *optstring = NOFAIL(strdup(""));
1485 /* Rest is module options */
1486 while (*argv) {
1487 /* Quote value if it contains spaces. */
1488 unsigned int eq = strcspn(*argv, "=");
1490 if (strchr(*argv+eq, ' ') && !strchr(*argv, '"')) {
1491 char quoted[strlen(*argv) + 3];
1492 (*argv)[eq] = '\0';
1493 sprintf(quoted, "%s=\"%s\"", *argv, *argv+eq+1);
1494 optstring = append_option(optstring, quoted);
1495 } else
1496 optstring = append_option(optstring, *argv);
1497 argv++;
1499 return optstring;
1502 static void handle_module(const char *modname,
1503 struct list_head *todo_list,
1504 const char *newname,
1505 int remove,
1506 char *options,
1507 int first_time,
1508 errfn_t error,
1509 int dry_run,
1510 int verbose,
1511 struct module_options *modoptions,
1512 struct module_command *commands,
1513 int ignore_commands,
1514 int ignore_proc,
1515 int strip_vermagic,
1516 int strip_modversion,
1517 int unknown_silent,
1518 const char *cmdline_opts,
1519 int flags)
1521 struct stat finfo;
1523 if (stat("/sys/module", &finfo) < 0)
1524 fatal("/sys is not mounted.\n");
1526 if (list_empty(todo_list)) {
1527 const char *command;
1529 /* The dependencies have to be real modules, but
1530 handle case where the first is completely bogus. */
1531 command = find_command(modname, commands);
1532 if (command && !ignore_commands) {
1533 do_command(modname, command, verbose, dry_run, error,
1534 remove ? "remove":"install", cmdline_opts);
1535 return;
1538 if (unknown_silent)
1539 exit(1);
1540 error("Module %s not found.\n", modname);
1541 return;
1544 if (remove)
1545 rmmod(todo_list, newname, first_time, error, dry_run, verbose,
1546 commands, ignore_commands, 0, cmdline_opts, flags);
1547 else
1548 insmod(todo_list, NOFAIL(strdup(options)), newname,
1549 first_time, error, dry_run, verbose, modoptions,
1550 commands, ignore_commands, ignore_proc, strip_vermagic,
1551 strip_modversion, cmdline_opts);
1554 static struct option options[] = { { "verbose", 0, NULL, 'v' },
1555 { "version", 0, NULL, 'V' },
1556 { "config", 1, NULL, 'C' },
1557 { "name", 1, NULL, 'o' },
1558 { "remove", 0, NULL, 'r' },
1559 { "wait", 0, NULL, 'w' },
1560 { "showconfig", 0, NULL, 'c' },
1561 { "autoclean", 0, NULL, 'k' },
1562 { "quiet", 0, NULL, 'q' },
1563 { "show", 0, NULL, 'n' },
1564 { "dry-run", 0, NULL, 'n' },
1565 { "syslog", 0, NULL, 's' },
1566 { "type", 1, NULL, 't' },
1567 { "list", 0, NULL, 'l' },
1568 { "all", 0, NULL, 'a' },
1569 { "ignore-install", 0, NULL, 'i' },
1570 { "ignore-remove", 0, NULL, 'i' },
1571 { "force", 0, NULL, 'f' },
1572 { "force-vermagic", 0, NULL, 1 },
1573 { "force-modversion", 0, NULL, 2 },
1574 { "set-version", 1, NULL, 'S' },
1575 { "show-depends", 0, NULL, 'D' },
1576 { "dirname", 1, NULL, 'd' },
1577 { "first-time", 0, NULL, 3 },
1578 { "dump-modversions", 0, NULL, 4 },
1579 { "use-blacklist", 0, NULL, 'b' },
1580 { NULL, 0, NULL, 0 } };
1582 int main(int argc, char *argv[])
1584 struct utsname buf;
1585 struct stat statbuf;
1586 int opt;
1587 int dump_only = 0;
1588 int dry_run = 0;
1589 int remove = 0;
1590 int verbose = 0;
1591 int unknown_silent = 0;
1592 int list_only = 0;
1593 int all = 0;
1594 int ignore_commands = 0;
1595 int strip_vermagic = 0;
1596 int strip_modversion = 0;
1597 int ignore_proc = 0;
1598 int first_time = 0;
1599 int dump_modver = 0;
1600 int use_blacklist = 0;
1601 unsigned int i, num_modules;
1602 char *type = NULL;
1603 const char *config = NULL;
1604 char *dirname = NULL;
1605 char *optstring = NULL;
1606 char *newname = NULL;
1607 char *aliasfilename, *symfilename;
1608 errfn_t error = fatal;
1609 int flags = O_NONBLOCK|O_EXCL;
1611 /* Prepend options from environment. */
1612 argv = merge_args(getenv("MODPROBE_OPTIONS"), argv, &argc);
1614 uname(&buf);
1615 while ((opt = getopt_long(argc, argv, "vVC:o:rknqQsclt:aifbwd:", options, NULL)) != -1){
1616 switch (opt) {
1617 case 'v':
1618 add_to_env_var("-v");
1619 verbose = 1;
1620 break;
1621 case 'V':
1622 puts(PACKAGE " version " VERSION);
1623 exit(0);
1624 case 'S':
1625 strncpy(buf.release, optarg, sizeof(buf.release));
1626 buf.release[sizeof(buf.release)-1] = '\0';
1627 break;
1628 case 'C':
1629 config = optarg;
1630 add_to_env_var("-C");
1631 add_to_env_var(config);
1632 break;
1633 case 'q':
1634 unknown_silent = 1;
1635 add_to_env_var("-q");
1636 break;
1637 case 'D':
1638 dry_run = 1;
1639 ignore_proc = 1;
1640 verbose = 1;
1641 add_to_env_var("-D");
1642 break;
1643 case 'o':
1644 newname = optarg;
1645 break;
1646 case 'r':
1647 remove = 1;
1648 break;
1649 case 'c':
1650 dump_only = 1;
1651 break;
1652 case 't':
1653 type = optarg;
1654 break;
1655 case 'l':
1656 list_only = 1;
1657 break;
1658 case 'a':
1659 all = 1;
1660 error = warn;
1661 break;
1662 case 'k':
1663 /* FIXME: This should actually do something */
1664 break;
1665 case 'n':
1666 dry_run = 1;
1667 break;
1668 case 's':
1669 add_to_env_var("-s");
1670 logging = 1;
1671 break;
1672 case 'i':
1673 ignore_commands = 1;
1674 break;
1675 case 'f':
1676 strip_vermagic = 1;
1677 strip_modversion = 1;
1678 break;
1679 case 'b':
1680 use_blacklist = 1;
1681 break;
1682 case 'w':
1683 flags &= ~O_NONBLOCK;
1684 break;
1685 case 'd':
1686 nofail_asprintf(&dirname, "%s/%s/%s", optarg,
1687 MODULE_DIR, buf.release);
1688 break;
1689 case 1:
1690 strip_vermagic = 1;
1691 break;
1692 case 2:
1693 strip_modversion = 1;
1694 break;
1695 case 3:
1696 first_time = 1;
1697 break;
1698 case 4:
1699 dump_modver = 1;
1700 break;
1701 default:
1702 print_usage(argv[0]);
1706 /* If stderr not open, go to syslog */
1707 if (logging || fstat(STDERR_FILENO, &statbuf) != 0) {
1708 openlog("modprobe", LOG_CONS, LOG_DAEMON);
1709 logging = 1;
1712 if (argc < optind + 1 && !dump_only && !list_only && !remove)
1713 print_usage(argv[0]);
1715 if (!dirname)
1716 nofail_asprintf(&dirname, "%s/%s", MODULE_DIR, buf.release);
1717 nofail_asprintf(&aliasfilename, "%s/modules.alias", dirname);
1718 nofail_asprintf(&symfilename, "%s/modules.symbols", dirname);
1720 /* Old-style -t xxx wildcard? Only with -l. */
1721 if (list_only) {
1722 if (optind+1 < argc)
1723 fatal("Can't have multiple wildcards\n");
1724 /* fprintf(stderr, "man find\n"); return 1; */
1725 return do_wildcard(dirname, type, argv[optind]?:"*");
1727 if (type)
1728 fatal("-t only supported with -l");
1730 if (dump_only) {
1731 struct module_command *commands = NULL;
1732 struct module_options *modoptions = NULL;
1733 struct module_alias *aliases = NULL;
1734 struct module_blacklist *blacklist = NULL;
1736 read_toplevel_config(config, "", 1, 0,
1737 &modoptions, &commands, &aliases, &blacklist);
1738 read_kcmdline(1, &modoptions);
1739 if (use_binary_indexes) {
1740 read_config_file_bin(aliasfilename, "", 1, 0,
1741 &modoptions, &commands, &aliases, &blacklist);
1742 read_config_file_bin(symfilename, "", 1, 0,
1743 &modoptions, &commands, &aliases, &blacklist);
1744 } else {
1745 read_config(aliasfilename, "", 1, 0,
1746 &modoptions, &commands, &aliases, &blacklist);
1747 read_config(symfilename, "", 1, 0,
1748 &modoptions, &commands, &aliases, &blacklist);
1750 exit(0);
1753 if (remove || all) {
1754 num_modules = argc - optind;
1755 optstring = NOFAIL(strdup(""));
1756 } else {
1757 num_modules = 1;
1758 optstring = gather_options(argv+optind+1);
1761 /* num_modules is always 1 except for -r or -a. */
1762 for (i = 0; i < num_modules; i++) {
1763 struct module_command *commands = NULL;
1764 struct module_options *modoptions = NULL;
1765 struct module_alias *aliases = NULL;
1766 struct module_blacklist *blacklist = NULL;
1767 LIST_HEAD(list);
1768 char *modulearg = argv[optind + i];
1770 if (dump_modver) {
1771 dump_modversions(modulearg, error);
1772 continue;
1775 /* Convert name we are looking for */
1776 underscores(modulearg);
1778 /* Returns the resolved alias, options */
1779 read_toplevel_config(config, modulearg, 0,
1780 remove, &modoptions, &commands, &aliases, &blacklist);
1782 /* Read module options from kernel command line */
1783 read_kcmdline(0, &modoptions);
1785 /* No luck? Try symbol names, if starts with symbol:. */
1786 if (!aliases
1787 && strncmp(modulearg, "symbol:", strlen("symbol:")) == 0) {
1788 if (use_binary_indexes)
1789 read_config_file_bin(symfilename, modulearg, 0,
1790 remove, &modoptions, &commands,
1791 &aliases, &blacklist);
1792 else
1793 read_config(symfilename, modulearg, 0,
1794 remove, &modoptions, &commands,
1795 &aliases, &blacklist);
1797 if (!aliases) {
1798 if(!strchr(modulearg, ':'))
1799 read_depends(dirname, modulearg, &list);
1801 /* We only use canned aliases as last resort. */
1802 if (list_empty(&list)
1803 && !find_command(modulearg, commands))
1805 if (use_binary_indexes)
1806 read_config_file_bin(aliasfilename,
1807 modulearg, 0, remove,
1808 &modoptions, &commands,
1809 &aliases, &blacklist);
1810 else
1811 read_config(aliasfilename,
1812 modulearg, 0, remove,
1813 &modoptions, &commands,
1814 &aliases, &blacklist);
1818 aliases = apply_blacklist(aliases, blacklist);
1819 if (aliases) {
1820 errfn_t err = error;
1822 /* More than one alias? Don't bail out on failure. */
1823 if (aliases->next)
1824 err = warn;
1825 while (aliases) {
1826 /* Add the options for this alias. */
1827 char *opts = NOFAIL(strdup(optstring));
1828 opts = add_extra_options(modulearg,
1829 opts, modoptions);
1831 read_depends(dirname, aliases->module, &list);
1832 handle_module(aliases->module, &list, newname,
1833 remove, opts, first_time, err,
1834 dry_run, verbose, modoptions,
1835 commands, ignore_commands,
1836 ignore_proc, strip_vermagic,
1837 strip_modversion,
1838 unknown_silent,
1839 optstring, flags);
1841 aliases = aliases->next;
1842 INIT_LIST_HEAD(&list);
1844 } else {
1845 if (use_blacklist
1846 && find_blacklist(modulearg, blacklist))
1847 continue;
1849 handle_module(modulearg, &list, newname, remove,
1850 optstring, first_time, error, dry_run,
1851 verbose, modoptions, commands,
1852 ignore_commands, ignore_proc,
1853 strip_vermagic, strip_modversion,
1854 unknown_silent, optstring, flags);
1857 if (logging)
1858 closelog();
1860 free(dirname);
1861 free(aliasfilename);
1862 free(symfilename);
1863 free(optstring);
1865 return 0;