modprobe: make return values consistent for quiet/non-quiet
[mit.git] / modprobe.c
blob8bd7342d170131faf388f483a1730ee73c23c7d3
1 /* modprobe.c: add or remove a module from the kernel, intelligently.
2 Copyright (C) 2001 Rusty Russell.
3 Copyright (C) 2002, 2003 Rusty Russell, IBM Corporation.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #define _GNU_SOURCE /* asprintf */
21 #include <sys/utsname.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <sys/mman.h>
25 #include <fcntl.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <ctype.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <unistd.h>
33 #include <dirent.h>
34 #include <limits.h>
35 #include <elf.h>
36 #include <getopt.h>
37 #include <fnmatch.h>
38 #include <asm/unistd.h>
39 #include <sys/wait.h>
40 #include <syslog.h>
42 #define streq(a,b) (strcmp((a),(b)) == 0)
43 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
45 #include "zlibsupport.h"
46 #include "logging.h"
47 #include "index.h"
48 #include "list.h"
49 #include "config_filter.h"
51 #include "testing.h"
53 int use_binary_indexes = 1; /* default to enabled. */
55 extern long init_module(void *, unsigned long, const char *);
56 extern long delete_module(const char *, unsigned int);
58 struct module {
59 struct list_head list;
60 char *modname;
61 char filename[0];
64 #ifndef MODULE_DIR
65 #define MODULE_DIR "/lib/modules"
66 #endif
68 typedef void (*errfn_t)(const char *fmt, ...);
70 static void grammar(const char *cmd, const char *filename, unsigned int line)
72 warn("%s line %u: ignoring bad line starting with '%s'\n",
73 filename, line, cmd);
76 static void print_usage(const char *progname)
78 fprintf(stderr,
79 "Usage: %s [-v] [-V] [-C config-file] [-d <dirname> ] [-n] [-i] [-q] [-b] [-o <modname>] [ --dump-modversions ] <modname> [parameters...]\n"
80 "%s -r [-n] [-i] [-v] <modulename> ...\n"
81 "%s -l -t <dirname> [ -a <modulename> ...]\n",
82 progname, progname, progname);
83 exit(1);
86 static char *getline_wrapped(FILE *file, unsigned int *linenum)
88 int size = 256;
89 int i = 0;
90 char *buf = NOFAIL(malloc(size));
91 for(;;) {
92 int ch = getc_unlocked(file);
94 switch(ch) {
95 case EOF:
96 if (i == 0) {
97 free(buf);
98 return NULL;
100 /* else fall through */
102 case '\n':
103 if (linenum)
104 (*linenum)++;
105 if (i == size)
106 buf = NOFAIL(realloc(buf, size + 1));
107 buf[i] = '\0';
108 return buf;
110 case '\\':
111 ch = getc_unlocked(file);
113 if (ch == '\n') {
114 if (linenum)
115 (*linenum)++;
116 continue;
118 /* else fall through */
120 default:
121 buf[i++] = ch;
123 if (i == size) {
124 size *= 2;
125 buf = NOFAIL(realloc(buf, size));
131 static struct module *find_module(const char *filename, struct list_head *list)
133 struct module *i;
135 list_for_each_entry(i, list, list) {
136 if (strcmp(i->filename, filename) == 0)
137 return i;
139 return NULL;
142 /* Convert filename to the module name. Works if filename == modname, too. */
143 static void filename2modname(char *modname, const char *filename)
145 const char *afterslash;
146 unsigned int i;
148 afterslash = strrchr(filename, '/');
149 if (!afterslash)
150 afterslash = filename;
151 else
152 afterslash++;
154 /* Convert to underscores, stop at first . */
155 for (i = 0; afterslash[i] && afterslash[i] != '.'; i++) {
156 if (afterslash[i] == '-')
157 modname[i] = '_';
158 else
159 modname[i] = afterslash[i];
161 modname[i] = '\0';
164 /* We used to lock with a write flock but that allows regular users to block
165 * module load by having a read lock on the module file (no way to bust the
166 * existing locks without killing the offending process). Instead, we now
167 * do the system call/init_module and allow the kernel to fail us instead.
169 static int open_file(const char *filename)
171 int fd = open(filename, O_RDONLY, 0);
173 return fd;
176 static void close_file(int fd)
178 /* Valgrind is picky... */
179 close(fd);
182 static void add_module(char *filename, int namelen, struct list_head *list)
184 struct module *mod;
186 /* If it's a duplicate: move it to the end, so it gets
187 inserted where it is *first* required. */
188 mod = find_module(filename, list);
189 if (mod)
190 list_del(&mod->list);
191 else {
192 /* No match. Create a new module. */
193 mod = NOFAIL(malloc(sizeof(struct module) + namelen + 1));
194 memcpy(mod->filename, filename, namelen);
195 mod->filename[namelen] = '\0';
196 mod->modname = NOFAIL(malloc(namelen + 1));
197 filename2modname(mod->modname, mod->filename);
200 list_add_tail(&mod->list, list);
203 /* Compare len chars of a to b, with _ and - equivalent. */
204 static int modname_equal(const char *a, const char *b, unsigned int len)
206 unsigned int i;
208 if (strlen(b) != len)
209 return 0;
211 for (i = 0; i < len; i++) {
212 if ((a[i] == '_' || a[i] == '-')
213 && (b[i] == '_' || b[i] == '-'))
214 continue;
215 if (a[i] != b[i])
216 return 0;
218 return 1;
221 /* Fills in list of modules if this is the line we want. */
222 static int add_modules_dep_line(char *line,
223 const char *name,
224 struct list_head *list,
225 const char *dirname)
227 char *ptr;
228 int len;
229 char *modname, *fullpath;
231 /* Ignore lines without : or which start with a # */
232 ptr = strchr(line, ':');
233 if (ptr == NULL || line[strspn(line, "\t ")] == '#')
234 return 0;
236 /* Is this the module we are looking for? */
237 *ptr = '\0';
238 if (strrchr(line, '/'))
239 modname = strrchr(line, '/') + 1;
240 else
241 modname = line;
243 len = strlen(modname);
244 if (strchr(modname, '.'))
245 len = strchr(modname, '.') - modname;
246 if (!modname_equal(modname, name, len))
247 return 0;
249 /* Create the list. */
250 if ('/' == line[0]) { /* old style deps - absolute path specified */
251 add_module(line, ptr - line, list);
252 } else {
253 nofail_asprintf(&fullpath, "%s/%s", dirname, line);
254 add_module(fullpath, strlen(dirname)+1+(ptr - line), list);
255 free(fullpath);
258 ptr++;
259 for(;;) {
260 char *dep_start;
261 ptr += strspn(ptr, " \t");
262 if (*ptr == '\0')
263 break;
264 dep_start = ptr;
265 ptr += strcspn(ptr, " \t");
266 if ('/' == dep_start[0]) { /* old style deps */
267 add_module(dep_start, ptr - dep_start, list);
268 } else {
269 nofail_asprintf(&fullpath, "%s/%s", dirname, dep_start);
270 add_module(fullpath,
271 strlen(dirname)+1+(ptr - dep_start), list);
272 free(fullpath);
275 return 1;
278 static int read_depends_file(const char *dirname,
279 const char *start_name,
280 struct list_head *list)
282 char *modules_dep_name;
283 char *line;
284 struct index_file *modules_dep;
286 nofail_asprintf(&modules_dep_name, "%s/%s", dirname, "modules.dep.bin");
287 modules_dep = index_file_open(modules_dep_name);
288 if (!modules_dep) {
289 free(modules_dep_name);
290 return 0;
293 line = index_search(modules_dep, start_name);
294 if (line) {
295 /* Value is standard dependency line format */
296 if (!add_modules_dep_line(line, start_name, list, dirname))
297 fatal("Module index is inconsistent\n");
298 free(line);
301 index_file_close(modules_dep);
302 free(modules_dep_name);
304 return 1;
307 static void read_depends(const char *dirname,
308 const char *start_name,
309 struct list_head *list)
311 char *modules_dep_name;
312 char *line;
313 FILE *modules_dep;
314 int done = 0;
316 if (use_binary_indexes)
317 if (read_depends_file(dirname, start_name, list))
318 return;
320 nofail_asprintf(&modules_dep_name, "%s/%s", dirname, "modules.dep");
321 modules_dep = fopen(modules_dep_name, "r");
322 if (!modules_dep)
323 fatal("Could not load %s: %s\n",
324 modules_dep_name, strerror(errno));
326 /* Stop at first line, as we can have duplicates (eg. symlinks
327 from boot/ */
328 while (!done && (line = getline_wrapped(modules_dep, NULL)) != NULL) {
329 done = add_modules_dep_line(line, start_name, list, dirname);
330 free(line);
332 fclose(modules_dep);
333 free(modules_dep_name);
336 /* We use error numbers in a loose translation... */
337 static const char *insert_moderror(int err)
339 switch (err) {
340 case ENOEXEC:
341 return "Invalid module format";
342 case ENOENT:
343 return "Unknown symbol in module, or unknown parameter (see dmesg)";
344 case ENOSYS:
345 return "Kernel does not have module support";
346 default:
347 return strerror(err);
351 static const char *remove_moderror(int err)
353 switch (err) {
354 case ENOENT:
355 return "No such module";
356 case ENOSYS:
357 return "Kernel does not have module unloading support";
358 default:
359 return strerror(err);
363 static void replace_modname(struct module *module,
364 void *mem, unsigned long len,
365 const char *oldname, const char *newname)
367 char *p;
369 /* 64 - sizeof(unsigned long) - 1 */
370 if (strlen(newname) > 55)
371 fatal("New name %s is too long\n", newname);
373 /* Find where it is in the module structure. Don't assume layout! */
374 for (p = mem; p < (char *)mem + len - strlen(oldname); p++) {
375 if (memcmp(p, oldname, strlen(oldname)) == 0) {
376 strcpy(p, newname);
377 return;
381 warn("Could not find old name in %s to replace!\n", module->filename);
384 static void *get_section32(void *file,
385 unsigned long size,
386 const char *name,
387 unsigned long *secsize)
389 Elf32_Ehdr *hdr = file;
390 Elf32_Shdr *sechdrs = file + hdr->e_shoff;
391 const char *secnames;
392 unsigned int i;
394 /* Too short? */
395 if (size < sizeof(*hdr))
396 return NULL;
397 if (size < hdr->e_shoff + hdr->e_shnum * sizeof(sechdrs[0]))
398 return NULL;
399 if (size < sechdrs[hdr->e_shstrndx].sh_offset)
400 return NULL;
402 secnames = file + sechdrs[hdr->e_shstrndx].sh_offset;
403 for (i = 1; i < hdr->e_shnum; i++)
404 if (strcmp(secnames + sechdrs[i].sh_name, name) == 0) {
405 *secsize = sechdrs[i].sh_size;
406 return file + sechdrs[i].sh_offset;
408 return NULL;
411 static void *get_section64(void *file,
412 unsigned long size,
413 const char *name,
414 unsigned long *secsize)
416 Elf64_Ehdr *hdr = file;
417 Elf64_Shdr *sechdrs = file + hdr->e_shoff;
418 const char *secnames;
419 unsigned int i;
421 /* Too short? */
422 if (size < sizeof(*hdr))
423 return NULL;
424 if (size < hdr->e_shoff + hdr->e_shnum * sizeof(sechdrs[0]))
425 return NULL;
426 if (size < sechdrs[hdr->e_shstrndx].sh_offset)
427 return NULL;
429 secnames = file + sechdrs[hdr->e_shstrndx].sh_offset;
430 for (i = 1; i < hdr->e_shnum; i++)
431 if (strcmp(secnames + sechdrs[i].sh_name, name) == 0) {
432 *secsize = sechdrs[i].sh_size;
433 return file + sechdrs[i].sh_offset;
435 return NULL;
438 static int elf_ident(void *mod, unsigned long size)
440 /* "\177ELF" <byte> where byte = 001 for 32-bit, 002 for 64 */
441 char *ident = mod;
443 if (size < EI_CLASS || memcmp(mod, ELFMAG, SELFMAG) != 0)
444 return ELFCLASSNONE;
445 return ident[EI_CLASS];
448 static void *get_section(void *file,
449 unsigned long size,
450 const char *name,
451 unsigned long *secsize)
453 switch (elf_ident(file, size)) {
454 case ELFCLASS32:
455 return get_section32(file, size, name, secsize);
456 case ELFCLASS64:
457 return get_section64(file, size, name, secsize);
458 default:
459 return NULL;
463 static void rename_module(struct module *module,
464 void *mod,
465 unsigned long len,
466 const char *newname)
468 void *modstruct;
469 unsigned long modstruct_len;
471 /* Old-style */
472 modstruct = get_section(mod, len, ".gnu.linkonce.this_module",
473 &modstruct_len);
474 /* New-style */
475 if (!modstruct)
476 modstruct = get_section(mod, len, "__module", &modstruct_len);
477 if (!modstruct)
478 warn("Could not find module name to change in %s\n",
479 module->filename);
480 else
481 replace_modname(module, modstruct, modstruct_len,
482 module->modname, newname);
485 /* Kernel told to ignore these sections if SHF_ALLOC not set. */
486 static void invalidate_section32(void *mod, const char *secname)
488 Elf32_Ehdr *hdr = mod;
489 Elf32_Shdr *sechdrs = mod + hdr->e_shoff;
490 const char *secnames = mod + sechdrs[hdr->e_shstrndx].sh_offset;
491 unsigned int i;
493 for (i = 1; i < hdr->e_shnum; i++)
494 if (strcmp(secnames+sechdrs[i].sh_name, secname) == 0)
495 sechdrs[i].sh_flags &= ~SHF_ALLOC;
498 static void invalidate_section64(void *mod, const char *secname)
500 Elf64_Ehdr *hdr = mod;
501 Elf64_Shdr *sechdrs = mod + hdr->e_shoff;
502 const char *secnames = mod + sechdrs[hdr->e_shstrndx].sh_offset;
503 unsigned int i;
505 for (i = 1; i < hdr->e_shnum; i++)
506 if (strcmp(secnames+sechdrs[i].sh_name, secname) == 0)
507 sechdrs[i].sh_flags &= ~(unsigned long long)SHF_ALLOC;
510 static void strip_section(struct module *module,
511 void *mod,
512 unsigned long len,
513 const char *secname)
515 switch (elf_ident(mod, len)) {
516 case ELFCLASS32:
517 invalidate_section32(mod, secname);
518 break;
519 case ELFCLASS64:
520 invalidate_section64(mod, secname);
521 break;
522 default:
523 warn("Unknown module format in %s: not forcing version\n",
524 module->filename);
528 static const char *next_string(const char *string, unsigned long *secsize)
530 /* Skip non-zero chars */
531 while (string[0]) {
532 string++;
533 if ((*secsize)-- <= 1)
534 return NULL;
537 /* Skip any zero padding. */
538 while (!string[0]) {
539 string++;
540 if ((*secsize)-- <= 1)
541 return NULL;
543 return string;
546 static void clear_magic(struct module *module, void *mod, unsigned long len)
548 const char *p;
549 unsigned long modlen;
551 /* Old-style: __vermagic section */
552 strip_section(module, mod, len, "__vermagic");
554 /* New-style: in .modinfo section */
555 for (p = get_section(mod, len, ".modinfo", &modlen);
557 p = next_string(p, &modlen)) {
558 if (strncmp(p, "vermagic=", strlen("vermagic=")) == 0) {
559 memset((char *)p, 0, strlen(p));
560 return;
565 struct module_options
567 struct module_options *next;
568 char *modulename;
569 char *options;
572 struct module_command
574 struct module_command *next;
575 char *modulename;
576 char *command;
579 struct module_alias
581 struct module_alias *next;
582 char *module;
585 struct module_blacklist
587 struct module_blacklist *next;
588 char *modulename;
591 /* Link in a new option line from the config file. */
592 static struct module_options *
593 add_options(const char *modname,
594 const char *option,
595 struct module_options *options)
597 struct module_options *new;
598 char *tab;
600 new = NOFAIL(malloc(sizeof(*new)));
601 new->modulename = NOFAIL(strdup(modname));
602 new->options = NOFAIL(strdup(option));
603 /* We can handle tabs, kernel can't. */
604 for (tab = strchr(new->options, '\t'); tab; tab = strchr(tab, '\t'))
605 *tab = ' ';
606 new->next = options;
607 return new;
610 /* Link in a new install line from the config file. */
611 static struct module_command *
612 add_command(const char *modname,
613 const char *command,
614 struct module_command *commands)
616 struct module_command *new;
618 new = NOFAIL(malloc(sizeof(*new)));
619 new->modulename = NOFAIL(strdup(modname));
620 new->command = NOFAIL(strdup(command));
621 new->next = commands;
622 return new;
625 /* Link in a new alias line from the config file. */
626 static struct module_alias *
627 add_alias(const char *modname, struct module_alias *aliases)
629 struct module_alias *new;
631 new = NOFAIL(malloc(sizeof(*new)));
632 new->module = NOFAIL(strdup(modname));
633 new->next = aliases;
634 return new;
637 /* Link in a new blacklist line from the config file. */
638 static struct module_blacklist *
639 add_blacklist(const char *modname, struct module_blacklist *blacklist)
641 struct module_blacklist *new;
643 new = NOFAIL(malloc(sizeof(*new)));
644 new->modulename = NOFAIL(strdup(modname));
645 new->next = blacklist;
646 return new;
649 /* Find blacklist commands if any. */
650 static int
651 find_blacklist(const char *modname, const struct module_blacklist *blacklist)
653 while (blacklist) {
654 if (strcmp(blacklist->modulename, modname) == 0)
655 return 1;
656 blacklist = blacklist->next;
658 return 0;
661 /* return a new alias list, with backlisted elems filtered out */
662 static struct module_alias *
663 apply_blacklist(const struct module_alias *aliases,
664 const struct module_blacklist *blacklist)
666 struct module_alias *result = NULL;
667 while (aliases) {
668 char *modname = aliases->module;
669 if (!find_blacklist(modname, blacklist))
670 result = add_alias(modname, result);
671 aliases = aliases->next;
673 return result;
676 /* Find install commands if any. */
677 static const char *find_command(const char *modname,
678 const struct module_command *commands)
680 while (commands) {
681 if (fnmatch(commands->modulename, modname, 0) == 0)
682 return commands->command;
683 commands = commands->next;
685 return NULL;
688 static char *append_option(char *options, const char *newoption)
690 options = NOFAIL(realloc(options, strlen(options) + 1
691 + strlen(newoption) + 1));
692 if (strlen(options)) strcat(options, " ");
693 strcat(options, newoption);
694 return options;
697 static char *prepend_option(char *options, const char *newoption)
699 size_t l1, l2;
700 l1 = strlen(options);
701 l2 = strlen(newoption);
702 /* the resulting string will look like
703 * newoption + ' ' + options + '\0' */
704 if (l1) {
705 options = NOFAIL(realloc(options, l2 + 1 + l1 + 1));
706 memmove(options + l2 + 1, options, l1 + 1);
707 options[l2] = ' ';
708 memcpy(options, newoption, l2);
709 } else {
710 options = NOFAIL(realloc(options, l2 + 1));
711 memcpy(options, newoption, l2);
712 options[l2] = '\0';
714 return options;
717 /* Add to options */
718 static char *add_extra_options(const char *modname,
719 char *optstring,
720 const struct module_options *options)
722 while (options) {
723 if (strcmp(options->modulename, modname) == 0)
724 optstring = prepend_option(optstring, options->options);
725 options = options->next;
727 return optstring;
730 /* Read sysfs attribute into a buffer.
731 * returns: 1 = ok, 0 = attribute missing,
732 * -1 = file error (or empty file, but we don't care).
734 static int read_attribute(const char *filename, char *buf, size_t buflen)
736 FILE *file;
737 char *s;
739 file = fopen(filename, "r");
740 if (file == NULL)
741 return (errno == ENOENT) ? 0 : -1;
742 s = fgets(buf, buflen, file);
743 fclose(file);
745 return (s == NULL) ? -1 : 1;
748 /* Is module in /sys/module? If so, fill in usecount if not NULL.
749 0 means no, 1 means yes, -1 means unknown.
751 static int module_in_kernel(const char *modname, unsigned int *usecount)
753 int ret;
754 char *name;
755 struct stat finfo;
757 const int ATTR_LEN = 16;
758 char attr[ATTR_LEN];
760 /* Check sysfs is mounted */
761 if (stat("/sys/module", &finfo) < 0)
762 return -1;
764 /* Find module. */
765 nofail_asprintf(&name, "/sys/module/%s", modname);
766 ret = stat(name, &finfo);
767 free(name);
768 if (ret < 0)
769 return (errno == ENOENT) ? 0 : -1; /* Not found or unknown. */
771 /* Wait for the existing module to either go live or disappear. */
772 nofail_asprintf(&name, "/sys/module/%s/initstate", modname);
773 while (1) {
774 ret = read_attribute(name, attr, ATTR_LEN);
775 if (ret != 1 || streq(attr, "live\n"))
776 break;
778 usleep(100000);
780 free(name);
782 if (ret != 1)
783 return ret;
785 /* Get reference count, if it exists. */
786 if (usecount != NULL) {
787 nofail_asprintf(&name, "/sys/module/%s/refcnt", modname);
788 ret = read_attribute(name, attr, ATTR_LEN);
789 free(name);
790 if (ret == 1)
791 *usecount = atoi(attr);
794 return 1;
797 /* If we don't flush, then child processes print before we do */
798 static void verbose_printf(int verbose, const char *fmt, ...)
800 va_list arglist;
802 if (verbose) {
803 va_start(arglist, fmt);
804 vprintf(fmt, arglist);
805 fflush(stdout);
806 va_end(arglist);
810 /* Do an install/remove command: replace $CMDLINE_OPTS if it's specified. */
811 static void do_command(const char *modname,
812 const char *command,
813 int verbose, int dry_run,
814 errfn_t error,
815 const char *type,
816 const char *cmdline_opts)
818 int ret;
819 char *p, *replaced_cmd = NOFAIL(strdup(command));
821 while ((p = strstr(replaced_cmd, "$CMDLINE_OPTS")) != NULL) {
822 char *new;
823 nofail_asprintf(&new, "%.*s%s%s",
824 (int)(p - replaced_cmd), replaced_cmd, cmdline_opts,
825 p + strlen("$CMDLINE_OPTS"));
826 free(replaced_cmd);
827 replaced_cmd = new;
830 verbose_printf(verbose, "%s %s\n", type, replaced_cmd);
831 if (dry_run)
832 return;
834 setenv("MODPROBE_MODULE", modname, 1);
835 ret = system(replaced_cmd);
836 if (ret == -1 || WEXITSTATUS(ret))
837 error("Error running %s command for %s\n", type, modname);
838 free(replaced_cmd);
841 /* Actually do the insert. Frees second arg. */
842 static void insmod(struct list_head *list,
843 char *optstring,
844 const char *newname,
845 int first_time,
846 errfn_t error,
847 int dry_run,
848 int verbose,
849 const struct module_options *options,
850 const struct module_command *commands,
851 int ignore_commands,
852 int ignore_proc,
853 int strip_vermagic,
854 int strip_modversion,
855 const char *cmdline_opts)
857 int ret, fd;
858 unsigned long len;
859 void *map;
860 const char *command;
861 struct module *mod = list_entry(list->next, struct module, list);
863 /* Take us off the list. */
864 list_del(&mod->list);
866 /* Do things we (or parent) depend on first, but don't die if
867 * they fail. */
868 if (!list_empty(list)) {
869 insmod(list, NOFAIL(strdup("")), NULL, 0, warn,
870 dry_run, verbose, options, commands, 0, ignore_proc,
871 strip_vermagic, strip_modversion, "");
874 fd = open_file(mod->filename);
875 if (fd < 0) {
876 error("Could not open '%s': %s\n",
877 mod->filename, strerror(errno));
878 goto out_optstring;
881 /* Don't do ANYTHING if already in kernel. */
882 if (!ignore_proc
883 && module_in_kernel(newname ?: mod->modname, NULL) == 1) {
884 if (first_time)
885 error("Module %s already in kernel.\n",
886 newname ?: mod->modname);
887 goto out_unlock;
890 command = find_command(mod->modname, commands);
891 if (command && !ignore_commands) {
892 close_file(fd);
893 do_command(mod->modname, command, verbose, dry_run, error,
894 "install", cmdline_opts);
895 goto out_optstring;
898 map = grab_fd(fd, &len);
899 if (!map) {
900 error("Could not read '%s': %s\n",
901 mod->filename, strerror(errno));
902 goto out_unlock;
905 /* Rename it? */
906 if (newname)
907 rename_module(mod, map, len, newname);
909 if (strip_modversion)
910 strip_section(mod, map, len, "__versions");
911 if (strip_vermagic)
912 clear_magic(mod, map, len);
914 /* Config file might have given more options */
915 optstring = add_extra_options(mod->modname, optstring, options);
917 verbose_printf(verbose, "insmod %s %s\n", mod->filename, optstring);
919 if (dry_run)
920 goto out;
922 ret = init_module(map, len, optstring);
923 if (ret != 0) {
924 if (errno == EEXIST) {
925 if (first_time)
926 error("Module %s already in kernel.\n",
927 newname ?: mod->modname);
928 goto out_unlock;
930 /* don't warn noisely if we're loading multiple aliases. */
931 /* one of the aliases may try to use hardware we don't have. */
932 if ((error != warn) || (verbose))
933 error("Error inserting %s (%s): %s\n",
934 mod->modname, mod->filename,
935 insert_moderror(errno));
937 out:
938 release_file(map, len);
939 out_unlock:
940 close_file(fd);
941 out_optstring:
942 free(optstring);
943 return;
946 /* Do recursive removal. */
947 static void rmmod(struct list_head *list,
948 const char *name,
949 int first_time,
950 errfn_t error,
951 int dry_run,
952 int verbose,
953 struct module_command *commands,
954 int ignore_commands,
955 int ignore_inuse,
956 const char *cmdline_opts,
957 int flags)
959 const char *command;
960 unsigned int usecount = 0;
961 struct module *mod = list_entry(list->next, struct module, list);
963 /* Take first one off the list. */
964 list_del(&mod->list);
966 if (!name)
967 name = mod->modname;
969 /* Even if renamed, find commands to orig. name. */
970 command = find_command(mod->modname, commands);
971 if (command && !ignore_commands) {
972 do_command(mod->modname, command, verbose, dry_run, error,
973 "remove", cmdline_opts);
974 goto remove_rest;
977 if (module_in_kernel(name, &usecount) == 0)
978 goto nonexistent_module;
980 if (usecount != 0) {
981 if (!ignore_inuse)
982 error("Module %s is in use.\n", name);
983 goto remove_rest;
986 verbose_printf(verbose, "rmmod %s\n", mod->filename);
988 if (dry_run)
989 goto remove_rest;
991 if (delete_module(name, O_EXCL) != 0) {
992 if (errno == ENOENT)
993 goto nonexistent_module;
994 error("Error removing %s (%s): %s\n",
995 name, mod->filename,
996 remove_moderror(errno));
999 remove_rest:
1000 /* Now do things we depend. */
1001 if (!list_empty(list))
1002 rmmod(list, NULL, 0, warn, dry_run, verbose, commands,
1003 0, 1, "", flags);
1004 return;
1006 nonexistent_module:
1007 if (first_time)
1008 fatal("Module %s is not in kernel.\n", mod->modname);
1009 goto remove_rest;
1012 struct modver32_info
1014 uint32_t crc;
1015 char name[64 - sizeof(uint32_t)];
1018 struct modver64_info
1020 uint64_t crc;
1021 char name[64 - sizeof(uint64_t)];
1024 const char *skip_dot(const char *str)
1026 /* For our purposes, .foo matches foo. PPC64 needs this. */
1027 if (str && str[0] == '.')
1028 return str + 1;
1029 return str;
1032 void dump_modversions(const char *filename, errfn_t error)
1034 unsigned long size, secsize;
1035 void *file = grab_file(filename, &size);
1036 struct modver32_info *info32;
1037 struct modver64_info *info64;
1038 int n;
1040 if (!file) {
1041 error("%s: %s\n", filename, strerror(errno));
1042 return;
1044 switch (elf_ident(file, size)) {
1045 case ELFCLASS32:
1046 info32 = get_section32(file, size, "__versions", &secsize);
1047 if (!info32)
1048 return; /* Does not seem to be a kernel module */
1049 if (secsize % sizeof(struct modver32_info))
1050 error("Wrong section size in %s\n", filename);
1051 for (n = 0; n < secsize / sizeof(struct modver32_info); n++)
1052 printf("0x%08lx\t%s\n", (unsigned long)
1053 info32[n].crc, skip_dot(info32[n].name));
1054 break;
1056 case ELFCLASS64:
1057 info64 = get_section64(file, size, "__versions", &secsize);
1058 if (!info64)
1059 return; /* Does not seem to be a kernel module */
1060 if (secsize % sizeof(struct modver64_info))
1061 error("Wrong section size in %s\n", filename);
1062 for (n = 0; n < secsize / sizeof(struct modver64_info); n++)
1063 printf("0x%08llx\t%s\n", (unsigned long long)
1064 info64[n].crc, skip_dot(info64[n].name));
1065 break;
1067 default:
1068 error("%s: ELF class not recognized\n", filename);
1073 /* Does path contain directory(s) subpath? */
1074 static int type_matches(const char *path, const char *subpath)
1076 char *subpath_with_slashes;
1077 int ret;
1079 nofail_asprintf(&subpath_with_slashes, "/%s/", subpath);
1081 ret = (strstr(path, subpath_with_slashes) != NULL);
1082 free(subpath_with_slashes);
1083 return ret;
1086 /* Careful! Don't munge - in [ ] as per Debian Bug#350915 */
1087 static char *underscores(char *string)
1089 unsigned int i;
1091 if (!string)
1092 return NULL;
1094 for (i = 0; string[i]; i++) {
1095 switch (string[i]) {
1096 case '-':
1097 string[i] = '_';
1098 break;
1100 case ']':
1101 warn("Unmatched bracket in %s\n", string);
1102 break;
1104 case '[':
1105 i += strcspn(&string[i], "]");
1106 if (!string[i])
1107 warn("Unmatched bracket in %s\n", string);
1108 break;
1111 return string;
1114 static int do_wildcard(const char *dirname,
1115 const char *type,
1116 const char *wildcard)
1118 char *modules_dep_name;
1119 char *line, *wcard;
1120 FILE *modules_dep;
1122 /* Canonicalize wildcard */
1123 wcard = strdup(wildcard);
1124 underscores(wcard);
1126 nofail_asprintf(&modules_dep_name, "%s/%s", dirname, "modules.dep");
1127 modules_dep = fopen(modules_dep_name, "r");
1128 if (!modules_dep)
1129 fatal("Could not load %s: %s\n",
1130 modules_dep_name, strerror(errno));
1132 while ((line = getline_wrapped(modules_dep, NULL)) != NULL) {
1133 char *ptr;
1135 /* Ignore lines without : or which start with a # */
1136 ptr = strchr(line, ':');
1137 if (ptr == NULL || line[strspn(line, "\t ")] == '#')
1138 goto next;
1139 *ptr = '\0';
1141 /* "type" must match complete directory component(s). */
1142 if (!type || type_matches(line, type)) {
1143 char modname[strlen(line)+1];
1145 filename2modname(modname, line);
1146 if (fnmatch(wcard, modname, 0) == 0)
1147 printf("%s\n", line);
1149 next:
1150 free(line);
1153 free(modules_dep_name);
1154 free(wcard);
1155 return 0;
1158 static char *strsep_skipspace(char **string, char *delim)
1160 if (!*string)
1161 return NULL;
1162 *string += strspn(*string, delim);
1163 return strsep(string, delim);
1166 static int parse_config_scan(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 **alias,
1173 struct module_blacklist **blacklist);
1175 static int parse_config_file(const char *filename,
1176 const char *name,
1177 int dump_only,
1178 int removing,
1179 struct module_options **options,
1180 struct module_command **commands,
1181 struct module_alias **aliases,
1182 struct module_blacklist **blacklist)
1184 char *line;
1185 unsigned int linenum = 0;
1186 FILE *cfile;
1188 cfile = fopen(filename, "r");
1189 if (!cfile)
1190 return 0;
1192 while ((line = getline_wrapped(cfile, &linenum)) != NULL) {
1193 char *ptr = line;
1194 char *cmd, *modname;
1196 if (dump_only)
1197 printf("%s\n", line);
1199 cmd = strsep_skipspace(&ptr, "\t ");
1200 if (cmd == NULL || cmd[0] == '#' || cmd[0] == '\0') {
1201 free(line);
1202 continue;
1205 if (strcmp(cmd, "alias") == 0) {
1206 char *wildcard = strsep_skipspace(&ptr, "\t ");
1207 char *realname = strsep_skipspace(&ptr, "\t ");
1209 if (!wildcard || !realname)
1210 grammar(cmd, filename, linenum);
1211 else if (fnmatch(underscores(wildcard),name,0) == 0)
1212 *aliases = add_alias(underscores(realname), *aliases);
1213 } else if (strcmp(cmd, "include") == 0) {
1214 struct module_alias *newalias = NULL;
1215 char *newfilename;
1217 newfilename = strsep_skipspace(&ptr, "\t ");
1218 if (!newfilename) {
1219 grammar(cmd, filename, linenum);
1220 } else {
1221 warn("\"include %s\" is deprecated, "
1222 "please use /etc/modprobe.d\n", newfilename);
1223 if (strncmp(newfilename, "/etc/modprobe.d",
1224 strlen("/etc/modprobe.d")) == 0) {
1225 warn("\"include /etc/modprobe.d\" is "
1226 "the default, ignored\n");
1227 } else {
1228 if (!parse_config_scan(newfilename, name,
1229 dump_only, removing,
1230 options, commands, &newalias,
1231 blacklist))
1232 warn("Failed to open included"
1233 " config file %s: %s\n",
1234 newfilename, strerror(errno));
1236 /* Files included override aliases,
1237 etc that was already set ... */
1238 if (newalias)
1239 *aliases = newalias;
1241 } else if (strcmp(cmd, "options") == 0) {
1242 modname = strsep_skipspace(&ptr, "\t ");
1243 if (!modname || !ptr)
1244 grammar(cmd, filename, linenum);
1245 else {
1246 ptr += strspn(ptr, "\t ");
1247 *options = add_options(underscores(modname),
1248 ptr, *options);
1250 } else if (strcmp(cmd, "install") == 0) {
1251 modname = strsep_skipspace(&ptr, "\t ");
1252 if (!modname || !ptr)
1253 grammar(cmd, filename, linenum);
1254 else if (!removing) {
1255 ptr += strspn(ptr, "\t ");
1256 *commands = add_command(underscores(modname),
1257 ptr, *commands);
1259 } else if (strcmp(cmd, "blacklist") == 0) {
1260 modname = strsep_skipspace(&ptr, "\t ");
1261 if (!modname)
1262 grammar(cmd, filename, linenum);
1263 else if (!removing) {
1264 *blacklist = add_blacklist(underscores(modname),
1265 *blacklist);
1267 } else if (strcmp(cmd, "remove") == 0) {
1268 modname = strsep_skipspace(&ptr, "\t ");
1269 if (!modname || !ptr)
1270 grammar(cmd, filename, linenum);
1271 else if (removing) {
1272 ptr += strspn(ptr, "\t ");
1273 *commands = add_command(underscores(modname),
1274 ptr, *commands);
1276 } else if (strcmp(cmd, "config") == 0) {
1277 char *tmp = strsep_skipspace(&ptr, "\t ");
1278 if (strcmp(tmp, "binary_indexes") == 0) {
1279 tmp = strsep_skipspace(&ptr, "\t ");
1280 if (strcmp(tmp, "yes") == 0)
1281 use_binary_indexes = 1;
1282 if (strcmp(tmp, "no") == 0)
1283 use_binary_indexes = 0;
1285 } else
1286 grammar(cmd, filename, linenum);
1288 free(line);
1290 fclose(cfile);
1291 return 1;
1294 /* fallback to plain-text aliases file as necessary */
1295 static int read_aliases_file(const char *filename,
1296 const char *name,
1297 int dump_only,
1298 int removing,
1299 struct module_options **options,
1300 struct module_command **commands,
1301 struct module_alias **aliases,
1302 struct module_blacklist **blacklist)
1304 struct index_value *realnames;
1305 struct index_value *realname;
1306 char *binfile;
1307 struct index_file *index;
1309 if (!use_binary_indexes)
1310 goto plain_text;
1312 nofail_asprintf(&binfile, "%s.bin", filename);
1313 index = index_file_open(binfile);
1314 if (!index) {
1315 free(binfile);
1316 goto plain_text;
1319 if (dump_only) {
1320 index_dump(index, stdout, "alias ");
1321 free(binfile);
1322 index_file_close(index);
1323 return 1;
1326 realnames = index_searchwild(index, name);
1327 for (realname = realnames; realname; realname = realname->next)
1328 *aliases = add_alias(realname->value, *aliases);
1329 index_values_free(realnames);
1331 free(binfile);
1332 index_file_close(index);
1333 return 1;
1335 plain_text:
1336 return parse_config_file(filename, name, dump_only, removing,
1337 options, commands, aliases, blacklist);
1340 static int parse_config_scan(const char *filename,
1341 const char *name,
1342 int dump_only,
1343 int removing,
1344 struct module_options **options,
1345 struct module_command **commands,
1346 struct module_alias **aliases,
1347 struct module_blacklist **blacklist)
1349 DIR *dir;
1350 int ret = 0;
1352 dir = opendir(filename);
1353 if (dir) {
1354 struct file_entry {
1355 struct list_head node;
1356 char name[];
1358 LIST_HEAD(files_list);
1359 struct file_entry *fe, *fe_tmp;
1360 struct dirent *i;
1362 /* sort files from directory into list */
1363 while ((i = readdir(dir)) != NULL) {
1364 size_t len;
1366 if (i->d_name[0] == '.')
1367 continue;
1368 if (!config_filter(i->d_name))
1369 continue;
1371 len = strlen(i->d_name);
1372 if (len < 6 ||
1373 (strcmp(&i->d_name[len-5], ".conf") != 0 &&
1374 strcmp(&i->d_name[len-6], ".alias") != 0))
1375 warn("All config files need .conf: %s/%s, "
1376 "it will be ignored in a future release.\n",
1377 filename, i->d_name);
1378 fe = malloc(sizeof(struct file_entry) + len + 1);
1379 if (fe == NULL)
1380 continue;
1381 strcpy(fe->name, i->d_name);
1382 list_for_each_entry(fe_tmp, &files_list, node)
1383 if (strcmp(fe_tmp->name, fe->name) >= 0)
1384 break;
1385 list_add_tail(&fe->node, &fe_tmp->node);
1387 closedir(dir);
1389 /* parse list of files */
1390 list_for_each_entry_safe(fe, fe_tmp, &files_list, node) {
1391 char *cfgfile;
1393 nofail_asprintf(&cfgfile, "%s/%s", filename, fe->name);
1394 if (!parse_config_file(cfgfile, name,
1395 dump_only, removing,
1396 options, commands,
1397 aliases, blacklist))
1398 warn("Failed to open config file "
1399 "%s: %s\n", fe->name, strerror(errno));
1400 free(cfgfile);
1401 list_del(&fe->node);
1402 free(fe);
1405 ret = 1;
1406 } else {
1407 if (parse_config_file(filename, name, dump_only, removing,
1408 options, commands, aliases, blacklist))
1409 ret = 1;
1411 return ret;
1414 /* Read binary index file containing aliases only */
1415 static void parse_toplevel_config(const char *filename,
1416 const char *name,
1417 int dump_only,
1418 int removing,
1419 struct module_options **options,
1420 struct module_command **commands,
1421 struct module_alias **aliases,
1422 struct module_blacklist **blacklist)
1424 if (filename) {
1425 if (!parse_config_scan(filename, name, dump_only, removing,
1426 options, commands, aliases, blacklist))
1427 fatal("Failed to open config file %s: %s\n",
1428 filename, strerror(errno));
1429 return;
1432 /* deprecated config file */
1433 if (parse_config_file("/etc/modprobe.conf", name, dump_only, removing,
1434 options, commands, aliases, blacklist) > 0)
1435 warn("Deprecated config file /etc/modprobe.conf, "
1436 "all config files belong into /etc/modprobe.d/.\n");
1438 /* default config */
1439 parse_config_scan("/etc/modprobe.d", name, dump_only, removing,
1440 options, commands, aliases, blacklist);
1443 /* Read possible module arguments from the kernel command line. */
1444 static int parse_kcmdline(int dump_only, struct module_options **options)
1446 char *line;
1447 unsigned int linenum = 0;
1448 FILE *kcmdline;
1450 kcmdline = fopen("/proc/cmdline", "r");
1451 if (!kcmdline)
1452 return 0;
1454 while ((line = getline_wrapped(kcmdline, &linenum)) != NULL) {
1455 char *ptr = line;
1456 char *arg;
1458 while ((arg = strsep_skipspace(&ptr, "\t ")) != NULL) {
1459 char *sep, *modname, *opt;
1461 sep = strchr(arg, '.');
1462 if (sep) {
1463 if (!strchr(sep, '='))
1464 continue;
1465 modname = arg;
1466 *sep = '\0';
1467 opt = ++sep;
1469 if (dump_only)
1470 printf("options %s %s\n", modname, opt);
1472 *options = add_options(underscores(modname),
1473 opt, *options);
1477 free(line);
1479 fclose(kcmdline);
1480 return 1;
1483 static void add_to_env_var(const char *option)
1485 const char *oldenv;
1487 if ((oldenv = getenv("MODPROBE_OPTIONS")) != NULL) {
1488 char *newenv;
1489 nofail_asprintf(&newenv, "%s %s", oldenv, option);
1490 setenv("MODPROBE_OPTIONS", newenv, 1);
1491 } else
1492 setenv("MODPROBE_OPTIONS", option, 1);
1495 /* Prepend options from environment. */
1496 static char **merge_args(char *args, char *argv[], int *argc)
1498 char *arg, *argstring;
1499 char **newargs = NULL;
1500 unsigned int i, num_env = 0;
1502 if (!args)
1503 return argv;
1505 argstring = NOFAIL(strdup(args));
1506 for (arg = strtok(argstring, " "); arg; arg = strtok(NULL, " ")) {
1507 num_env++;
1508 newargs = NOFAIL(realloc(newargs,
1509 sizeof(newargs[0])
1510 * (num_env + *argc + 1)));
1511 newargs[num_env] = arg;
1514 if (!newargs)
1515 return argv;
1517 /* Append commandline args */
1518 newargs[0] = argv[0];
1519 for (i = 1; i <= *argc; i++)
1520 newargs[num_env+i] = argv[i];
1522 *argc += num_env;
1523 return newargs;
1526 static char *gather_options(char *argv[])
1528 char *optstring = NOFAIL(strdup(""));
1530 /* Rest is module options */
1531 while (*argv) {
1532 /* Quote value if it contains spaces. */
1533 unsigned int eq = strcspn(*argv, "=");
1535 if (strchr(*argv+eq, ' ') && !strchr(*argv, '"')) {
1536 char quoted[strlen(*argv) + 3];
1537 (*argv)[eq] = '\0';
1538 sprintf(quoted, "%s=\"%s\"", *argv, *argv+eq+1);
1539 optstring = append_option(optstring, quoted);
1540 } else
1541 optstring = append_option(optstring, *argv);
1542 argv++;
1544 return optstring;
1547 static void handle_module(const char *modname,
1548 struct list_head *todo_list,
1549 const char *newname,
1550 int remove,
1551 char *options,
1552 int first_time,
1553 errfn_t error,
1554 int dry_run,
1555 int verbose,
1556 struct module_options *modoptions,
1557 struct module_command *commands,
1558 int ignore_commands,
1559 int ignore_proc,
1560 int strip_vermagic,
1561 int strip_modversion,
1562 int unknown_silent,
1563 const char *cmdline_opts,
1564 int flags)
1566 if (list_empty(todo_list)) {
1567 const char *command;
1569 /* The dependencies have to be real modules, but
1570 handle case where the first is completely bogus. */
1571 command = find_command(modname, commands);
1572 if (command && !ignore_commands) {
1573 do_command(modname, command, verbose, dry_run, error,
1574 remove ? "remove":"install", cmdline_opts);
1575 return;
1578 if (!unknown_silent)
1579 error("Module %s not found.\n", modname);
1580 return;
1583 if (remove)
1584 rmmod(todo_list, newname, first_time, error, dry_run, verbose,
1585 commands, ignore_commands, 0, cmdline_opts, flags);
1586 else
1587 insmod(todo_list, NOFAIL(strdup(options)), newname,
1588 first_time, error, dry_run, verbose, modoptions,
1589 commands, ignore_commands, ignore_proc, strip_vermagic,
1590 strip_modversion, cmdline_opts);
1593 static struct option options[] = { { "verbose", 0, NULL, 'v' },
1594 { "version", 0, NULL, 'V' },
1595 { "config", 1, NULL, 'C' },
1596 { "name", 1, NULL, 'o' },
1597 { "remove", 0, NULL, 'r' },
1598 { "wait", 0, NULL, 'w' },
1599 { "showconfig", 0, NULL, 'c' },
1600 { "autoclean", 0, NULL, 'k' },
1601 { "quiet", 0, NULL, 'q' },
1602 { "show", 0, NULL, 'n' },
1603 { "dry-run", 0, NULL, 'n' },
1604 { "syslog", 0, NULL, 's' },
1605 { "type", 1, NULL, 't' },
1606 { "list", 0, NULL, 'l' },
1607 { "all", 0, NULL, 'a' },
1608 { "ignore-install", 0, NULL, 'i' },
1609 { "ignore-remove", 0, NULL, 'i' },
1610 { "force", 0, NULL, 'f' },
1611 { "force-vermagic", 0, NULL, 1 },
1612 { "force-modversion", 0, NULL, 2 },
1613 { "set-version", 1, NULL, 'S' },
1614 { "show-depends", 0, NULL, 'D' },
1615 { "dirname", 1, NULL, 'd' },
1616 { "first-time", 0, NULL, 3 },
1617 { "dump-modversions", 0, NULL, 4 },
1618 { "use-blacklist", 0, NULL, 'b' },
1619 { NULL, 0, NULL, 0 } };
1621 int main(int argc, char *argv[])
1623 struct utsname buf;
1624 struct stat statbuf;
1625 int opt;
1626 int dump_only = 0;
1627 int dry_run = 0;
1628 int remove = 0;
1629 int verbose = 0;
1630 int unknown_silent = 0;
1631 int list_only = 0;
1632 int all = 0;
1633 int ignore_commands = 0;
1634 int strip_vermagic = 0;
1635 int strip_modversion = 0;
1636 int ignore_proc = 0;
1637 int first_time = 0;
1638 int dump_modver = 0;
1639 int use_blacklist = 0;
1640 unsigned int i, num_modules;
1641 char *type = NULL;
1642 const char *config = NULL;
1643 char *dirname = NULL;
1644 char *optstring = NULL;
1645 char *newname = NULL;
1646 char *aliasfilename, *symfilename;
1647 errfn_t error = fatal;
1648 int flags = O_NONBLOCK|O_EXCL;
1650 /* Prepend options from environment. */
1651 argv = merge_args(getenv("MODPROBE_OPTIONS"), argv, &argc);
1653 uname(&buf);
1654 while ((opt = getopt_long(argc, argv, "vVC:o:rknqQsclt:aifbwd:", options, NULL)) != -1){
1655 switch (opt) {
1656 case 'v':
1657 add_to_env_var("-v");
1658 verbose = 1;
1659 break;
1660 case 'V':
1661 puts(PACKAGE " version " VERSION);
1662 exit(0);
1663 case 'S':
1664 strncpy(buf.release, optarg, sizeof(buf.release));
1665 buf.release[sizeof(buf.release)-1] = '\0';
1666 break;
1667 case 'C':
1668 config = optarg;
1669 add_to_env_var("-C");
1670 add_to_env_var(config);
1671 break;
1672 case 'q':
1673 unknown_silent = 1;
1674 add_to_env_var("-q");
1675 break;
1676 case 'D':
1677 dry_run = 1;
1678 ignore_proc = 1;
1679 verbose = 1;
1680 add_to_env_var("-D");
1681 break;
1682 case 'o':
1683 newname = optarg;
1684 break;
1685 case 'r':
1686 remove = 1;
1687 break;
1688 case 'c':
1689 dump_only = 1;
1690 break;
1691 case 't':
1692 type = optarg;
1693 break;
1694 case 'l':
1695 list_only = 1;
1696 break;
1697 case 'a':
1698 all = 1;
1699 error = warn;
1700 break;
1701 case 'k':
1702 /* FIXME: This should actually do something */
1703 break;
1704 case 'n':
1705 dry_run = 1;
1706 break;
1707 case 's':
1708 add_to_env_var("-s");
1709 logging = 1;
1710 break;
1711 case 'i':
1712 ignore_commands = 1;
1713 break;
1714 case 'f':
1715 strip_vermagic = 1;
1716 strip_modversion = 1;
1717 break;
1718 case 'b':
1719 use_blacklist = 1;
1720 break;
1721 case 'w':
1722 flags &= ~O_NONBLOCK;
1723 break;
1724 case 'd':
1725 nofail_asprintf(&dirname, "%s/%s/%s", optarg,
1726 MODULE_DIR, buf.release);
1727 break;
1728 case 1:
1729 strip_vermagic = 1;
1730 break;
1731 case 2:
1732 strip_modversion = 1;
1733 break;
1734 case 3:
1735 first_time = 1;
1736 break;
1737 case 4:
1738 dump_modver = 1;
1739 break;
1740 default:
1741 print_usage(argv[0]);
1745 /* If stderr not open, go to syslog */
1746 if (logging || fstat(STDERR_FILENO, &statbuf) != 0) {
1747 openlog("modprobe", LOG_CONS, LOG_DAEMON);
1748 logging = 1;
1751 if (argc < optind + 1 && !dump_only && !list_only && !remove)
1752 print_usage(argv[0]);
1754 if (!dirname)
1755 nofail_asprintf(&dirname, "%s/%s", MODULE_DIR, buf.release);
1756 nofail_asprintf(&aliasfilename, "%s/modules.alias", dirname);
1757 nofail_asprintf(&symfilename, "%s/modules.symbols", dirname);
1759 /* Old-style -t xxx wildcard? Only with -l. */
1760 if (list_only) {
1761 if (optind+1 < argc)
1762 fatal("Can't have multiple wildcards\n");
1763 /* fprintf(stderr, "man find\n"); return 1; */
1764 return do_wildcard(dirname, type, argv[optind]?:"*");
1766 if (type)
1767 fatal("-t only supported with -l");
1769 if (dump_only) {
1770 struct module_command *commands = NULL;
1771 struct module_options *modoptions = NULL;
1772 struct module_alias *aliases = NULL;
1773 struct module_blacklist *blacklist = NULL;
1775 parse_toplevel_config(config, "", 1, 0, &modoptions,
1776 &commands, &aliases, &blacklist);
1777 /* Read module options from kernel command line */
1778 parse_kcmdline(1, &modoptions);
1779 parse_config_file(aliasfilename, "", 1, 0, &modoptions,
1780 &commands, &aliases, &blacklist);
1781 parse_config_file(symfilename, "", 1, 0, &modoptions,
1782 &commands, &aliases, &blacklist);
1783 exit(0);
1786 if (remove || all) {
1787 num_modules = argc - optind;
1788 optstring = NOFAIL(strdup(""));
1789 } else {
1790 num_modules = 1;
1791 optstring = gather_options(argv+optind+1);
1794 /* num_modules is always 1 except for -r or -a. */
1795 for (i = 0; i < num_modules; i++) {
1796 struct module_command *commands = NULL;
1797 struct module_options *modoptions = NULL;
1798 struct module_alias *aliases = NULL;
1799 struct module_blacklist *blacklist = NULL;
1800 LIST_HEAD(list);
1801 char *modulearg = argv[optind + i];
1803 if (dump_modver) {
1804 dump_modversions(modulearg, error);
1805 continue;
1808 /* Convert name we are looking for */
1809 underscores(modulearg);
1811 /* Returns the resolved alias, options */
1812 parse_toplevel_config(config, modulearg, 0,
1813 remove, &modoptions, &commands, &aliases, &blacklist);
1815 /* Read module options from kernel command line */
1816 parse_kcmdline(0, &modoptions);
1818 /* No luck? Try symbol names, if starts with symbol:. */
1819 if (!aliases &&
1820 strncmp(modulearg, "symbol:", strlen("symbol:")) == 0) {
1821 parse_config_file(symfilename, modulearg, 0,
1822 remove, &modoptions, &commands,
1823 &aliases, &blacklist);
1825 if (!aliases) {
1826 if(!strchr(modulearg, ':'))
1827 read_depends(dirname, modulearg, &list);
1829 /* We only use canned aliases as last resort. */
1830 if (list_empty(&list)
1831 && !find_command(modulearg, commands))
1833 read_aliases_file(aliasfilename,
1834 modulearg, 0, remove,
1835 &modoptions, &commands,
1836 &aliases, &blacklist);
1840 aliases = apply_blacklist(aliases, blacklist);
1841 if (aliases) {
1842 errfn_t err = error;
1844 /* More than one alias? Don't bail out on failure. */
1845 if (aliases->next)
1846 err = warn;
1847 while (aliases) {
1848 /* Add the options for this alias. */
1849 char *opts = NOFAIL(strdup(optstring));
1850 opts = add_extra_options(modulearg,
1851 opts, modoptions);
1853 read_depends(dirname, aliases->module, &list);
1854 handle_module(aliases->module, &list, newname,
1855 remove, opts, first_time, err,
1856 dry_run, verbose, modoptions,
1857 commands, ignore_commands,
1858 ignore_proc, strip_vermagic,
1859 strip_modversion,
1860 unknown_silent,
1861 optstring, flags);
1863 aliases = aliases->next;
1864 INIT_LIST_HEAD(&list);
1866 } else {
1867 if (use_blacklist
1868 && find_blacklist(modulearg, blacklist))
1869 continue;
1871 handle_module(modulearg, &list, newname, remove,
1872 optstring, first_time, error, dry_run,
1873 verbose, modoptions, commands,
1874 ignore_commands, ignore_proc,
1875 strip_vermagic, strip_modversion,
1876 unknown_silent, optstring, flags);
1879 if (logging)
1880 closelog();
1882 free(dirname);
1883 free(aliasfilename);
1884 free(symfilename);
1885 free(optstring);
1887 if (warn)
1888 return 1;
1889 else
1890 return 0;