release: bump release to v3.7-pre3
[mit.git] / modprobe.c
blobe4b1c45d7adf6824ff53d81161b1f65b3e412cca
1 /* modprobe.c: add or remove a module from the kernel, intelligently.
2 Copyright (C) 2001 Rusty Russell.
3 Copyright (C) 2002, 2003 Rusty Russell, IBM Corporation.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #define _GNU_SOURCE /* asprintf */
21 #include <sys/utsname.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <sys/mman.h>
25 #include <fcntl.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <ctype.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <unistd.h>
33 #include <dirent.h>
34 #include <limits.h>
35 #include <elf.h>
36 #include <getopt.h>
37 #include <fnmatch.h>
38 #include <asm/unistd.h>
39 #include <sys/wait.h>
40 #include <syslog.h>
42 #define streq(a,b) (strcmp((a),(b)) == 0)
43 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
45 #include "zlibsupport.h"
46 #include "logging.h"
47 #include "index.h"
48 #include "list.h"
49 #include "config_filter.h"
51 #include "testing.h"
53 int use_binary_indexes = 1; /* default to enabled. */
55 extern long init_module(void *, unsigned long, const char *);
56 extern long delete_module(const char *, unsigned int);
58 struct module {
59 struct list_head list;
60 char *modname;
61 char filename[0];
64 #ifndef MODULE_DIR
65 #define MODULE_DIR "/lib/modules"
66 #endif
68 typedef void (*errfn_t)(const char *fmt, ...);
70 static void grammar(const char *cmd, const char *filename, unsigned int line)
72 warn("%s line %u: ignoring bad line starting with '%s'\n",
73 filename, line, cmd);
76 static void print_usage(const char *progname)
78 fprintf(stderr,
79 "Usage: %s [-v] [-V] [-C config-file] [-d <dirname> ] [-n] [-i] [-q] [-b] [-o <modname>] [ --dump-modversions ] <modname> [parameters...]\n"
80 "%s -r [-n] [-i] [-v] <modulename> ...\n"
81 "%s -l -t <dirname> [ -a <modulename> ...]\n",
82 progname, progname, progname);
83 exit(1);
86 static char *getline_wrapped(FILE *file, unsigned int *linenum)
88 int size = 256;
89 int i = 0;
90 char *buf = NOFAIL(malloc(size));
91 for(;;) {
92 int ch = getc_unlocked(file);
94 switch(ch) {
95 case EOF:
96 if (i == 0) {
97 free(buf);
98 return NULL;
100 /* else fall through */
102 case '\n':
103 if (linenum)
104 (*linenum)++;
105 if (i == size)
106 buf = NOFAIL(realloc(buf, size + 1));
107 buf[i] = '\0';
108 return buf;
110 case '\\':
111 ch = getc_unlocked(file);
113 if (ch == '\n') {
114 if (linenum)
115 (*linenum)++;
116 continue;
118 /* else fall through */
120 default:
121 buf[i++] = ch;
123 if (i == size) {
124 size *= 2;
125 buf = NOFAIL(realloc(buf, size));
131 static struct module *find_module(const char *filename, struct list_head *list)
133 struct module *i;
135 list_for_each_entry(i, list, list) {
136 if (strcmp(i->filename, filename) == 0)
137 return i;
139 return NULL;
142 /* Convert filename to the module name. Works if filename == modname, too. */
143 static void filename2modname(char *modname, const char *filename)
145 const char *afterslash;
146 unsigned int i;
148 afterslash = strrchr(filename, '/');
149 if (!afterslash)
150 afterslash = filename;
151 else
152 afterslash++;
154 /* Convert to underscores, stop at first . */
155 for (i = 0; afterslash[i] && afterslash[i] != '.'; i++) {
156 if (afterslash[i] == '-')
157 modname[i] = '_';
158 else
159 modname[i] = afterslash[i];
161 modname[i] = '\0';
164 static int lock_file(const char *filename)
166 int fd = open(filename, O_RDWR, 0);
168 if (fd >= 0) {
169 struct flock lock;
170 lock.l_type = F_WRLCK;
171 lock.l_whence = SEEK_SET;
172 lock.l_start = 0;
173 lock.l_len = 1;
174 fcntl(fd, F_SETLKW, &lock);
175 } else
176 /* Read-only filesystem? There goes locking... */
177 fd = open(filename, O_RDONLY, 0);
178 return fd;
181 static void unlock_file(int fd)
183 /* Valgrind is picky... */
184 close(fd);
187 static void add_module(char *filename, int namelen, struct list_head *list)
189 struct module *mod;
191 /* If it's a duplicate: move it to the end, so it gets
192 inserted where it is *first* required. */
193 mod = find_module(filename, list);
194 if (mod)
195 list_del(&mod->list);
196 else {
197 /* No match. Create a new module. */
198 mod = NOFAIL(malloc(sizeof(struct module) + namelen + 1));
199 memcpy(mod->filename, filename, namelen);
200 mod->filename[namelen] = '\0';
201 mod->modname = NOFAIL(malloc(namelen + 1));
202 filename2modname(mod->modname, mod->filename);
205 list_add_tail(&mod->list, list);
208 /* Compare len chars of a to b, with _ and - equivalent. */
209 static int modname_equal(const char *a, const char *b, unsigned int len)
211 unsigned int i;
213 if (strlen(b) != len)
214 return 0;
216 for (i = 0; i < len; i++) {
217 if ((a[i] == '_' || a[i] == '-')
218 && (b[i] == '_' || b[i] == '-'))
219 continue;
220 if (a[i] != b[i])
221 return 0;
223 return 1;
226 /* Fills in list of modules if this is the line we want. */
227 static int add_modules_dep_line(char *line,
228 const char *name,
229 struct list_head *list,
230 const char *dirname)
232 char *ptr;
233 int len;
234 char *modname, *fullpath;
236 /* Ignore lines without : or which start with a # */
237 ptr = strchr(line, ':');
238 if (ptr == NULL || line[strspn(line, "\t ")] == '#')
239 return 0;
241 /* Is this the module we are looking for? */
242 *ptr = '\0';
243 if (strrchr(line, '/'))
244 modname = strrchr(line, '/') + 1;
245 else
246 modname = line;
248 len = strlen(modname);
249 if (strchr(modname, '.'))
250 len = strchr(modname, '.') - modname;
251 if (!modname_equal(modname, name, len))
252 return 0;
254 /* Create the list. */
255 if ('/' == line[0]) { /* old style deps - absolute path specified */
256 add_module(line, ptr - line, list);
257 } else {
258 nofail_asprintf(&fullpath, "%s/%s", dirname, line);
259 add_module(fullpath, strlen(dirname)+1+(ptr - line), list);
260 free(fullpath);
263 ptr++;
264 for(;;) {
265 char *dep_start;
266 ptr += strspn(ptr, " \t");
267 if (*ptr == '\0')
268 break;
269 dep_start = ptr;
270 ptr += strcspn(ptr, " \t");
271 if ('/' == dep_start[0]) { /* old style deps */
272 add_module(dep_start, ptr - dep_start, list);
273 } else {
274 nofail_asprintf(&fullpath, "%s/%s", dirname, dep_start);
275 add_module(fullpath,
276 strlen(dirname)+1+(ptr - dep_start), list);
277 free(fullpath);
280 return 1;
283 static int read_depends_file(const char *dirname,
284 const char *start_name,
285 struct list_head *list)
287 char *modules_dep_name;
288 char *line;
289 struct index_file *modules_dep;
291 nofail_asprintf(&modules_dep_name, "%s/%s", dirname, "modules.dep.bin");
292 modules_dep = index_file_open(modules_dep_name);
293 if (!modules_dep) {
294 free(modules_dep_name);
295 return 0;
298 line = index_search(modules_dep, start_name);
299 if (line) {
300 /* Value is standard dependency line format */
301 if (!add_modules_dep_line(line, start_name, list, dirname))
302 fatal("Module index is inconsistent\n");
303 free(line);
306 index_file_close(modules_dep);
307 free(modules_dep_name);
309 return 1;
312 static void read_depends(const char *dirname,
313 const char *start_name,
314 struct list_head *list)
316 char *modules_dep_name;
317 char *line;
318 FILE *modules_dep;
319 int done = 0;
321 if (use_binary_indexes)
322 if (read_depends_file(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 static char *prepend_option(char *options, const char *newoption)
704 size_t l1, l2;
705 l1 = strlen(options);
706 l2 = strlen(newoption);
707 /* the resulting string will look like
708 * newoption + ' ' + options + '\0' */
709 if (l1) {
710 options = NOFAIL(realloc(options, l2 + 1 + l1 + 1));
711 memmove(options + l2 + 1, options, l1 + 1);
712 options[l2] = ' ';
713 memcpy(options, newoption, l2);
714 } else {
715 options = NOFAIL(realloc(options, l2 + 1));
716 memcpy(options, newoption, l2);
717 options[l2] = '\0';
719 return options;
722 /* Add to options */
723 static char *add_extra_options(const char *modname,
724 char *optstring,
725 const struct module_options *options)
727 while (options) {
728 if (strcmp(options->modulename, modname) == 0)
729 optstring = prepend_option(optstring, options->options);
730 options = options->next;
732 return optstring;
735 /* Read sysfs attribute into a buffer.
736 * returns: 1 = ok, 0 = attribute missing,
737 * -1 = file error (or empty file, but we don't care).
739 static int read_attribute(const char *filename, char *buf, size_t buflen)
741 FILE *file;
742 char *s;
744 file = fopen(filename, "r");
745 if (file == NULL)
746 return (errno == ENOENT) ? 0 : -1;
747 s = fgets(buf, buflen, file);
748 fclose(file);
750 return (s == NULL) ? -1 : 1;
753 /* Is module in /sys/module? If so, fill in usecount if not NULL.
754 0 means no, 1 means yes, -1 means unknown.
756 static int module_in_kernel(const char *modname, unsigned int *usecount)
758 int ret;
759 char *name;
760 struct stat finfo;
762 const int ATTR_LEN = 16;
763 char attr[ATTR_LEN];
765 /* Check sysfs is mounted */
766 if (stat("/sys/module", &finfo) < 0)
767 return -1;
769 /* Find module. */
770 nofail_asprintf(&name, "/sys/module/%s", modname);
771 ret = stat(name, &finfo);
772 free(name);
773 if (ret < 0)
774 return (errno == ENOENT) ? 0 : -1; /* Not found or unknown. */
776 /* Wait for the existing module to either go live or disappear. */
777 nofail_asprintf(&name, "/sys/module/%s/initstate", modname);
778 while (1) {
779 ret = read_attribute(name, attr, ATTR_LEN);
780 if (ret != 1 || streq(attr, "live\n"))
781 break;
783 usleep(100000);
785 free(name);
787 if (ret != 1)
788 return ret;
790 /* Get reference count, if it exists. */
791 if (usecount != NULL) {
792 nofail_asprintf(&name, "/sys/module/%s/refcnt", modname);
793 ret = read_attribute(name, attr, ATTR_LEN);
794 free(name);
795 if (ret == 1)
796 *usecount = atoi(attr);
799 return 1;
802 /* If we don't flush, then child processes print before we do */
803 static void verbose_printf(int verbose, const char *fmt, ...)
805 va_list arglist;
807 if (verbose) {
808 va_start(arglist, fmt);
809 vprintf(fmt, arglist);
810 fflush(stdout);
811 va_end(arglist);
815 /* Do an install/remove command: replace $CMDLINE_OPTS if it's specified. */
816 static void do_command(const char *modname,
817 const char *command,
818 int verbose, int dry_run,
819 errfn_t error,
820 const char *type,
821 const char *cmdline_opts)
823 int ret;
824 char *p, *replaced_cmd = NOFAIL(strdup(command));
826 while ((p = strstr(replaced_cmd, "$CMDLINE_OPTS")) != NULL) {
827 char *new;
828 nofail_asprintf(&new, "%.*s%s%s",
829 (int)(p - replaced_cmd), replaced_cmd, cmdline_opts,
830 p + strlen("$CMDLINE_OPTS"));
831 free(replaced_cmd);
832 replaced_cmd = new;
835 verbose_printf(verbose, "%s %s\n", type, replaced_cmd);
836 if (dry_run)
837 return;
839 setenv("MODPROBE_MODULE", modname, 1);
840 ret = system(replaced_cmd);
841 if (ret == -1 || WEXITSTATUS(ret))
842 error("Error running %s command for %s\n", type, modname);
843 free(replaced_cmd);
846 /* Actually do the insert. Frees second arg. */
847 static void insmod(struct list_head *list,
848 char *optstring,
849 const char *newname,
850 int first_time,
851 errfn_t error,
852 int dry_run,
853 int verbose,
854 const struct module_options *options,
855 const struct module_command *commands,
856 int ignore_commands,
857 int ignore_proc,
858 int strip_vermagic,
859 int strip_modversion,
860 const char *cmdline_opts)
862 int ret, fd;
863 unsigned long len;
864 void *map;
865 const char *command;
866 struct module *mod = list_entry(list->next, struct module, list);
868 /* Take us off the list. */
869 list_del(&mod->list);
871 /* Do things we (or parent) depend on first, but don't die if
872 * they fail. */
873 if (!list_empty(list)) {
874 insmod(list, NOFAIL(strdup("")), NULL, 0, warn,
875 dry_run, verbose, options, commands, 0, ignore_proc,
876 strip_vermagic, strip_modversion, "");
879 /* Lock before we look, in case it's initializing. */
880 fd = lock_file(mod->filename);
881 if (fd < 0) {
882 error("Could not open '%s': %s\n",
883 mod->filename, strerror(errno));
884 goto out_optstring;
887 /* Don't do ANYTHING if already in kernel. */
888 if (!ignore_proc
889 && module_in_kernel(newname ?: mod->modname, NULL) == 1) {
890 if (first_time)
891 error("Module %s already in kernel.\n",
892 newname ?: mod->modname);
893 goto out_unlock;
896 command = find_command(mod->modname, commands);
897 if (command && !ignore_commands) {
898 /* It might recurse: unlock. */
899 unlock_file(fd);
900 do_command(mod->modname, command, verbose, dry_run, error,
901 "install", cmdline_opts);
902 goto out_optstring;
905 map = grab_fd(fd, &len);
906 if (!map) {
907 error("Could not read '%s': %s\n",
908 mod->filename, strerror(errno));
909 goto out_unlock;
912 /* Rename it? */
913 if (newname)
914 rename_module(mod, map, len, newname);
916 if (strip_modversion)
917 strip_section(mod, map, len, "__versions");
918 if (strip_vermagic)
919 clear_magic(mod, map, len);
921 /* Config file might have given more options */
922 optstring = add_extra_options(mod->modname, optstring, options);
924 verbose_printf(verbose, "insmod %s %s\n", mod->filename, optstring);
926 if (dry_run)
927 goto out;
929 ret = init_module(map, len, optstring);
930 if (ret != 0) {
931 if (errno == EEXIST) {
932 if (first_time)
933 error("Module %s already in kernel.\n",
934 newname ?: mod->modname);
935 goto out_unlock;
937 /* don't warn noisely if we're loading multiple aliases. */
938 /* one of the aliases may try to use hardware we don't have. */
939 if ((error != warn) || (verbose))
940 error("Error inserting %s (%s): %s\n",
941 mod->modname, mod->filename,
942 insert_moderror(errno));
944 out:
945 release_file(map, len);
946 out_unlock:
947 unlock_file(fd);
948 out_optstring:
949 free(optstring);
950 return;
953 /* Do recursive removal. */
954 static void rmmod(struct list_head *list,
955 const char *name,
956 int first_time,
957 errfn_t error,
958 int dry_run,
959 int verbose,
960 struct module_command *commands,
961 int ignore_commands,
962 int ignore_inuse,
963 const char *cmdline_opts,
964 int flags)
966 const char *command;
967 unsigned int usecount = 0;
968 int lock;
969 struct module *mod = list_entry(list->next, struct module, list);
971 /* Take first one off the list. */
972 list_del(&mod->list);
974 /* Ignore failure; it's best effort here. */
975 lock = lock_file(mod->filename);
977 if (!name)
978 name = mod->modname;
980 /* Even if renamed, find commands to orig. name. */
981 command = find_command(mod->modname, commands);
982 if (command && !ignore_commands) {
983 /* It might recurse: unlock. */
984 unlock_file(lock);
985 do_command(mod->modname, command, verbose, dry_run, error,
986 "remove", cmdline_opts);
987 goto remove_rest_no_unlock;
990 if (module_in_kernel(name, &usecount) == 0)
991 goto nonexistent_module;
993 if (usecount != 0) {
994 if (!ignore_inuse)
995 error("Module %s is in use.\n", name);
996 goto remove_rest;
999 verbose_printf(verbose, "rmmod %s\n", mod->filename);
1001 if (dry_run)
1002 goto remove_rest;
1004 if (delete_module(name, O_EXCL) != 0) {
1005 if (errno == ENOENT)
1006 goto nonexistent_module;
1007 error("Error removing %s (%s): %s\n",
1008 name, mod->filename,
1009 remove_moderror(errno));
1012 remove_rest:
1013 unlock_file(lock);
1014 remove_rest_no_unlock:
1015 /* Now do things we depend. */
1016 if (!list_empty(list))
1017 rmmod(list, NULL, 0, warn, dry_run, verbose, commands,
1018 0, 1, "", flags);
1019 return;
1021 nonexistent_module:
1022 if (first_time)
1023 fatal("Module %s is not in kernel.\n", mod->modname);
1024 goto remove_rest;
1027 struct modver32_info
1029 uint32_t crc;
1030 char name[64 - sizeof(uint32_t)];
1033 struct modver64_info
1035 uint64_t crc;
1036 char name[64 - sizeof(uint64_t)];
1039 const char *skip_dot(const char *str)
1041 /* For our purposes, .foo matches foo. PPC64 needs this. */
1042 if (str && str[0] == '.')
1043 return str + 1;
1044 return str;
1047 void dump_modversions(const char *filename, errfn_t error)
1049 unsigned long size, secsize;
1050 void *file = grab_file(filename, &size);
1051 struct modver32_info *info32;
1052 struct modver64_info *info64;
1053 int n;
1055 if (!file) {
1056 error("%s: %s\n", filename, strerror(errno));
1057 return;
1059 switch (elf_ident(file, size)) {
1060 case ELFCLASS32:
1061 info32 = get_section32(file, size, "__versions", &secsize);
1062 if (!info32)
1063 return; /* Does not seem to be a kernel module */
1064 if (secsize % sizeof(struct modver32_info))
1065 error("Wrong section size in %s\n", filename);
1066 for (n = 0; n < secsize / sizeof(struct modver32_info); n++)
1067 printf("0x%08lx\t%s\n", (unsigned long)
1068 info32[n].crc, skip_dot(info32[n].name));
1069 break;
1071 case ELFCLASS64:
1072 info64 = get_section64(file, size, "__versions", &secsize);
1073 if (!info64)
1074 return; /* Does not seem to be a kernel module */
1075 if (secsize % sizeof(struct modver64_info))
1076 error("Wrong section size in %s\n", filename);
1077 for (n = 0; n < secsize / sizeof(struct modver64_info); n++)
1078 printf("0x%08llx\t%s\n", (unsigned long long)
1079 info64[n].crc, skip_dot(info64[n].name));
1080 break;
1082 default:
1083 error("%s: ELF class not recognized\n", filename);
1088 /* Does path contain directory(s) subpath? */
1089 static int type_matches(const char *path, const char *subpath)
1091 char *subpath_with_slashes;
1092 int ret;
1094 nofail_asprintf(&subpath_with_slashes, "/%s/", subpath);
1096 ret = (strstr(path, subpath_with_slashes) != NULL);
1097 free(subpath_with_slashes);
1098 return ret;
1101 /* Careful! Don't munge - in [ ] as per Debian Bug#350915 */
1102 static char *underscores(char *string)
1104 unsigned int i;
1106 if (!string)
1107 return NULL;
1109 for (i = 0; string[i]; i++) {
1110 switch (string[i]) {
1111 case '-':
1112 string[i] = '_';
1113 break;
1115 case ']':
1116 warn("Unmatched bracket in %s\n", string);
1117 break;
1119 case '[':
1120 i += strcspn(&string[i], "]");
1121 if (!string[i])
1122 warn("Unmatched bracket in %s\n", string);
1123 break;
1126 return string;
1129 static int do_wildcard(const char *dirname,
1130 const char *type,
1131 const char *wildcard)
1133 char *modules_dep_name;
1134 char *line, *wcard;
1135 FILE *modules_dep;
1137 /* Canonicalize wildcard */
1138 wcard = strdup(wildcard);
1139 underscores(wcard);
1141 nofail_asprintf(&modules_dep_name, "%s/%s", dirname, "modules.dep");
1142 modules_dep = fopen(modules_dep_name, "r");
1143 if (!modules_dep)
1144 fatal("Could not load %s: %s\n",
1145 modules_dep_name, strerror(errno));
1147 while ((line = getline_wrapped(modules_dep, NULL)) != NULL) {
1148 char *ptr;
1150 /* Ignore lines without : or which start with a # */
1151 ptr = strchr(line, ':');
1152 if (ptr == NULL || line[strspn(line, "\t ")] == '#')
1153 goto next;
1154 *ptr = '\0';
1156 /* "type" must match complete directory component(s). */
1157 if (!type || type_matches(line, type)) {
1158 char modname[strlen(line)+1];
1160 filename2modname(modname, line);
1161 if (fnmatch(wcard, modname, 0) == 0)
1162 printf("%s\n", line);
1164 next:
1165 free(line);
1168 free(modules_dep_name);
1169 free(wcard);
1170 return 0;
1173 static char *strsep_skipspace(char **string, char *delim)
1175 if (!*string)
1176 return NULL;
1177 *string += strspn(*string, delim);
1178 return strsep(string, delim);
1181 static int parse_config_scan(const char *filename,
1182 const char *name,
1183 int dump_only,
1184 int removing,
1185 struct module_options **options,
1186 struct module_command **commands,
1187 struct module_alias **alias,
1188 struct module_blacklist **blacklist);
1190 static int parse_config_file(const char *filename,
1191 const char *name,
1192 int dump_only,
1193 int removing,
1194 struct module_options **options,
1195 struct module_command **commands,
1196 struct module_alias **aliases,
1197 struct module_blacklist **blacklist)
1199 char *line;
1200 unsigned int linenum = 0;
1201 FILE *cfile;
1203 cfile = fopen(filename, "r");
1204 if (!cfile)
1205 return 0;
1207 while ((line = getline_wrapped(cfile, &linenum)) != NULL) {
1208 char *ptr = line;
1209 char *cmd, *modname;
1211 if (dump_only)
1212 printf("%s\n", line);
1214 cmd = strsep_skipspace(&ptr, "\t ");
1215 if (cmd == NULL || cmd[0] == '#' || cmd[0] == '\0') {
1216 free(line);
1217 continue;
1220 if (strcmp(cmd, "alias") == 0) {
1221 char *wildcard = strsep_skipspace(&ptr, "\t ");
1222 char *realname = strsep_skipspace(&ptr, "\t ");
1224 if (!wildcard || !realname)
1225 grammar(cmd, filename, linenum);
1226 else if (fnmatch(underscores(wildcard),name,0) == 0)
1227 *aliases = add_alias(underscores(realname), *aliases);
1228 } else if (strcmp(cmd, "include") == 0) {
1229 struct module_alias *newalias = NULL;
1230 char *newfilename;
1232 newfilename = strsep_skipspace(&ptr, "\t ");
1233 if (!newfilename) {
1234 grammar(cmd, filename, linenum);
1235 } else {
1236 warn("\"include %s\" is deprecated, "
1237 "please use /etc/modprobe.d\n", newfilename);
1238 if (strncmp(newfilename, "/etc/modprobe.d",
1239 strlen("/etc/modprobe.d")) == 0) {
1240 warn("\"include /etc/modprobe.d\" is "
1241 "the default, ignored\n");
1242 } else {
1243 if (!parse_config_scan(newfilename, name,
1244 dump_only, removing,
1245 options, commands, &newalias,
1246 blacklist))
1247 warn("Failed to open included"
1248 " config file %s: %s\n",
1249 newfilename, strerror(errno));
1251 /* Files included override aliases,
1252 etc that was already set ... */
1253 if (newalias)
1254 *aliases = newalias;
1256 } else if (strcmp(cmd, "options") == 0) {
1257 modname = strsep_skipspace(&ptr, "\t ");
1258 if (!modname || !ptr)
1259 grammar(cmd, filename, linenum);
1260 else {
1261 ptr += strspn(ptr, "\t ");
1262 *options = add_options(underscores(modname),
1263 ptr, *options);
1265 } else if (strcmp(cmd, "install") == 0) {
1266 modname = strsep_skipspace(&ptr, "\t ");
1267 if (!modname || !ptr)
1268 grammar(cmd, filename, linenum);
1269 else if (!removing) {
1270 ptr += strspn(ptr, "\t ");
1271 *commands = add_command(underscores(modname),
1272 ptr, *commands);
1274 } else if (strcmp(cmd, "blacklist") == 0) {
1275 modname = strsep_skipspace(&ptr, "\t ");
1276 if (!modname)
1277 grammar(cmd, filename, linenum);
1278 else if (!removing) {
1279 *blacklist = add_blacklist(underscores(modname),
1280 *blacklist);
1282 } else if (strcmp(cmd, "remove") == 0) {
1283 modname = strsep_skipspace(&ptr, "\t ");
1284 if (!modname || !ptr)
1285 grammar(cmd, filename, linenum);
1286 else if (removing) {
1287 ptr += strspn(ptr, "\t ");
1288 *commands = add_command(underscores(modname),
1289 ptr, *commands);
1291 } else if (strcmp(cmd, "config") == 0) {
1292 char *tmp = strsep_skipspace(&ptr, "\t ");
1293 if (strcmp(tmp, "binary_indexes") == 0) {
1294 tmp = strsep_skipspace(&ptr, "\t ");
1295 if (strcmp(tmp, "yes") == 0)
1296 use_binary_indexes = 1;
1297 if (strcmp(tmp, "no") == 0)
1298 use_binary_indexes = 0;
1300 } else
1301 grammar(cmd, filename, linenum);
1303 free(line);
1305 fclose(cfile);
1306 return 1;
1309 /* fallback to plain-text aliases file as necessary */
1310 static int read_aliases_file(const char *filename,
1311 const char *name,
1312 int dump_only,
1313 int removing,
1314 struct module_options **options,
1315 struct module_command **commands,
1316 struct module_alias **aliases,
1317 struct module_blacklist **blacklist)
1319 struct index_value *realnames;
1320 struct index_value *realname;
1321 char *binfile;
1322 struct index_file *index;
1324 if (!use_binary_indexes)
1325 goto plain_text;
1327 nofail_asprintf(&binfile, "%s.bin", filename);
1328 index = index_file_open(binfile);
1329 if (!index) {
1330 free(binfile);
1331 goto plain_text;
1334 if (dump_only) {
1335 index_dump(index, stdout, "alias ");
1336 free(binfile);
1337 index_file_close(index);
1338 return 1;
1341 realnames = index_searchwild(index, name);
1342 for (realname = realnames; realname; realname = realname->next)
1343 *aliases = add_alias(realname->value, *aliases);
1344 index_values_free(realnames);
1346 free(binfile);
1347 index_file_close(index);
1348 return 1;
1350 plain_text:
1351 return parse_config_file(filename, name, dump_only, removing,
1352 options, commands, aliases, blacklist);
1355 static int parse_config_scan(const char *filename,
1356 const char *name,
1357 int dump_only,
1358 int removing,
1359 struct module_options **options,
1360 struct module_command **commands,
1361 struct module_alias **aliases,
1362 struct module_blacklist **blacklist)
1364 DIR *dir;
1365 int ret = 0;
1367 dir = opendir(filename);
1368 if (dir) {
1369 struct file_entry {
1370 struct list_head node;
1371 char name[];
1373 LIST_HEAD(files_list);
1374 struct file_entry *fe, *fe_tmp;
1375 struct dirent *i;
1377 /* sort files from directory into list */
1378 while ((i = readdir(dir)) != NULL) {
1379 size_t len;
1381 if (i->d_name[0] == '.')
1382 continue;
1383 if (!config_filter(i->d_name))
1384 continue;
1386 len = strlen(i->d_name);
1387 if (len < 6 ||
1388 (strcmp(&i->d_name[len-5], ".conf") != 0 &&
1389 strcmp(&i->d_name[len-6], ".alias") != 0))
1390 warn("All config files need .conf: %s/%s, "
1391 "it will be ignored in a future release.\n",
1392 filename, i->d_name);
1393 fe = malloc(sizeof(struct file_entry) + len + 1);
1394 if (fe == NULL)
1395 continue;
1396 strcpy(fe->name, i->d_name);
1397 list_for_each_entry(fe_tmp, &files_list, node)
1398 if (strcmp(fe_tmp->name, fe->name) >= 0)
1399 break;
1400 list_add_tail(&fe->node, &fe_tmp->node);
1402 closedir(dir);
1404 /* parse list of files */
1405 list_for_each_entry_safe(fe, fe_tmp, &files_list, node) {
1406 char *cfgfile;
1408 nofail_asprintf(&cfgfile, "%s/%s", filename, fe->name);
1409 if (!parse_config_file(cfgfile, name,
1410 dump_only, removing,
1411 options, commands,
1412 aliases, blacklist))
1413 warn("Failed to open config file "
1414 "%s: %s\n", fe->name, strerror(errno));
1415 free(cfgfile);
1416 list_del(&fe->node);
1417 free(fe);
1420 ret = 1;
1421 } else {
1422 if (parse_config_file(filename, name, dump_only, removing,
1423 options, commands, aliases, blacklist))
1424 ret = 1;
1426 return ret;
1429 /* Read binary index file containing aliases only */
1430 static void parse_toplevel_config(const char *filename,
1431 const char *name,
1432 int dump_only,
1433 int removing,
1434 struct module_options **options,
1435 struct module_command **commands,
1436 struct module_alias **aliases,
1437 struct module_blacklist **blacklist)
1439 if (filename) {
1440 if (!parse_config_scan(filename, name, dump_only, removing,
1441 options, commands, aliases, blacklist))
1442 fatal("Failed to open config file %s: %s\n",
1443 filename, strerror(errno));
1444 return;
1447 /* deprecated config file */
1448 if (parse_config_file("/etc/modprobe.conf", name, dump_only, removing,
1449 options, commands, aliases, blacklist) > 0)
1450 warn("Deprecated config file /etc/modprobe.conf, "
1451 "all config files belong into /etc/modprobe.d/.\n");
1453 /* default config */
1454 parse_config_scan("/etc/modprobe.d", name, dump_only, removing,
1455 options, commands, aliases, blacklist);
1458 /* Read possible module arguments from the kernel command line. */
1459 static int parse_kcmdline(int dump_only, struct module_options **options)
1461 char *line;
1462 unsigned int linenum = 0;
1463 FILE *kcmdline;
1465 kcmdline = fopen("/proc/cmdline", "r");
1466 if (!kcmdline)
1467 return 0;
1469 while ((line = getline_wrapped(kcmdline, &linenum)) != NULL) {
1470 char *ptr = line;
1471 char *arg;
1473 while ((arg = strsep_skipspace(&ptr, "\t ")) != NULL) {
1474 char *sep, *modname, *opt;
1476 sep = strchr(arg, '.');
1477 if (sep) {
1478 if (!strchr(sep, '='))
1479 continue;
1480 modname = arg;
1481 *sep = '\0';
1482 opt = ++sep;
1484 if (dump_only)
1485 printf("options %s %s\n", modname, opt);
1487 *options = add_options(underscores(modname),
1488 opt, *options);
1492 free(line);
1494 fclose(kcmdline);
1495 return 1;
1498 static void add_to_env_var(const char *option)
1500 const char *oldenv;
1502 if ((oldenv = getenv("MODPROBE_OPTIONS")) != NULL) {
1503 char *newenv;
1504 nofail_asprintf(&newenv, "%s %s", oldenv, option);
1505 setenv("MODPROBE_OPTIONS", newenv, 1);
1506 } else
1507 setenv("MODPROBE_OPTIONS", option, 1);
1510 /* Prepend options from environment. */
1511 static char **merge_args(char *args, char *argv[], int *argc)
1513 char *arg, *argstring;
1514 char **newargs = NULL;
1515 unsigned int i, num_env = 0;
1517 if (!args)
1518 return argv;
1520 argstring = NOFAIL(strdup(args));
1521 for (arg = strtok(argstring, " "); arg; arg = strtok(NULL, " ")) {
1522 num_env++;
1523 newargs = NOFAIL(realloc(newargs,
1524 sizeof(newargs[0])
1525 * (num_env + *argc + 1)));
1526 newargs[num_env] = arg;
1529 if (!newargs)
1530 return argv;
1532 /* Append commandline args */
1533 newargs[0] = argv[0];
1534 for (i = 1; i <= *argc; i++)
1535 newargs[num_env+i] = argv[i];
1537 *argc += num_env;
1538 return newargs;
1541 static char *gather_options(char *argv[])
1543 char *optstring = NOFAIL(strdup(""));
1545 /* Rest is module options */
1546 while (*argv) {
1547 /* Quote value if it contains spaces. */
1548 unsigned int eq = strcspn(*argv, "=");
1550 if (strchr(*argv+eq, ' ') && !strchr(*argv, '"')) {
1551 char quoted[strlen(*argv) + 3];
1552 (*argv)[eq] = '\0';
1553 sprintf(quoted, "%s=\"%s\"", *argv, *argv+eq+1);
1554 optstring = append_option(optstring, quoted);
1555 } else
1556 optstring = append_option(optstring, *argv);
1557 argv++;
1559 return optstring;
1562 static void handle_module(const char *modname,
1563 struct list_head *todo_list,
1564 const char *newname,
1565 int remove,
1566 char *options,
1567 int first_time,
1568 errfn_t error,
1569 int dry_run,
1570 int verbose,
1571 struct module_options *modoptions,
1572 struct module_command *commands,
1573 int ignore_commands,
1574 int ignore_proc,
1575 int strip_vermagic,
1576 int strip_modversion,
1577 int unknown_silent,
1578 const char *cmdline_opts,
1579 int flags)
1581 if (list_empty(todo_list)) {
1582 const char *command;
1584 /* The dependencies have to be real modules, but
1585 handle case where the first is completely bogus. */
1586 command = find_command(modname, commands);
1587 if (command && !ignore_commands) {
1588 do_command(modname, command, verbose, dry_run, error,
1589 remove ? "remove":"install", cmdline_opts);
1590 return;
1593 if (unknown_silent)
1594 exit(1);
1595 error("Module %s not found.\n", modname);
1596 return;
1599 if (remove)
1600 rmmod(todo_list, newname, first_time, error, dry_run, verbose,
1601 commands, ignore_commands, 0, cmdline_opts, flags);
1602 else
1603 insmod(todo_list, NOFAIL(strdup(options)), newname,
1604 first_time, error, dry_run, verbose, modoptions,
1605 commands, ignore_commands, ignore_proc, strip_vermagic,
1606 strip_modversion, cmdline_opts);
1609 static struct option options[] = { { "verbose", 0, NULL, 'v' },
1610 { "version", 0, NULL, 'V' },
1611 { "config", 1, NULL, 'C' },
1612 { "name", 1, NULL, 'o' },
1613 { "remove", 0, NULL, 'r' },
1614 { "wait", 0, NULL, 'w' },
1615 { "showconfig", 0, NULL, 'c' },
1616 { "autoclean", 0, NULL, 'k' },
1617 { "quiet", 0, NULL, 'q' },
1618 { "show", 0, NULL, 'n' },
1619 { "dry-run", 0, NULL, 'n' },
1620 { "syslog", 0, NULL, 's' },
1621 { "type", 1, NULL, 't' },
1622 { "list", 0, NULL, 'l' },
1623 { "all", 0, NULL, 'a' },
1624 { "ignore-install", 0, NULL, 'i' },
1625 { "ignore-remove", 0, NULL, 'i' },
1626 { "force", 0, NULL, 'f' },
1627 { "force-vermagic", 0, NULL, 1 },
1628 { "force-modversion", 0, NULL, 2 },
1629 { "set-version", 1, NULL, 'S' },
1630 { "show-depends", 0, NULL, 'D' },
1631 { "dirname", 1, NULL, 'd' },
1632 { "first-time", 0, NULL, 3 },
1633 { "dump-modversions", 0, NULL, 4 },
1634 { "use-blacklist", 0, NULL, 'b' },
1635 { NULL, 0, NULL, 0 } };
1637 int main(int argc, char *argv[])
1639 struct utsname buf;
1640 struct stat statbuf;
1641 int opt;
1642 int dump_only = 0;
1643 int dry_run = 0;
1644 int remove = 0;
1645 int verbose = 0;
1646 int unknown_silent = 0;
1647 int list_only = 0;
1648 int all = 0;
1649 int ignore_commands = 0;
1650 int strip_vermagic = 0;
1651 int strip_modversion = 0;
1652 int ignore_proc = 0;
1653 int first_time = 0;
1654 int dump_modver = 0;
1655 int use_blacklist = 0;
1656 unsigned int i, num_modules;
1657 char *type = NULL;
1658 const char *config = NULL;
1659 char *dirname = NULL;
1660 char *optstring = NULL;
1661 char *newname = NULL;
1662 char *aliasfilename, *symfilename;
1663 errfn_t error = fatal;
1664 int flags = O_NONBLOCK|O_EXCL;
1666 /* Prepend options from environment. */
1667 argv = merge_args(getenv("MODPROBE_OPTIONS"), argv, &argc);
1669 uname(&buf);
1670 while ((opt = getopt_long(argc, argv, "vVC:o:rknqQsclt:aifbwd:", options, NULL)) != -1){
1671 switch (opt) {
1672 case 'v':
1673 add_to_env_var("-v");
1674 verbose = 1;
1675 break;
1676 case 'V':
1677 puts(PACKAGE " version " VERSION);
1678 exit(0);
1679 case 'S':
1680 strncpy(buf.release, optarg, sizeof(buf.release));
1681 buf.release[sizeof(buf.release)-1] = '\0';
1682 break;
1683 case 'C':
1684 config = optarg;
1685 add_to_env_var("-C");
1686 add_to_env_var(config);
1687 break;
1688 case 'q':
1689 unknown_silent = 1;
1690 add_to_env_var("-q");
1691 break;
1692 case 'D':
1693 dry_run = 1;
1694 ignore_proc = 1;
1695 verbose = 1;
1696 add_to_env_var("-D");
1697 break;
1698 case 'o':
1699 newname = optarg;
1700 break;
1701 case 'r':
1702 remove = 1;
1703 break;
1704 case 'c':
1705 dump_only = 1;
1706 break;
1707 case 't':
1708 type = optarg;
1709 break;
1710 case 'l':
1711 list_only = 1;
1712 break;
1713 case 'a':
1714 all = 1;
1715 error = warn;
1716 break;
1717 case 'k':
1718 /* FIXME: This should actually do something */
1719 break;
1720 case 'n':
1721 dry_run = 1;
1722 break;
1723 case 's':
1724 add_to_env_var("-s");
1725 logging = 1;
1726 break;
1727 case 'i':
1728 ignore_commands = 1;
1729 break;
1730 case 'f':
1731 strip_vermagic = 1;
1732 strip_modversion = 1;
1733 break;
1734 case 'b':
1735 use_blacklist = 1;
1736 break;
1737 case 'w':
1738 flags &= ~O_NONBLOCK;
1739 break;
1740 case 'd':
1741 nofail_asprintf(&dirname, "%s/%s/%s", optarg,
1742 MODULE_DIR, buf.release);
1743 break;
1744 case 1:
1745 strip_vermagic = 1;
1746 break;
1747 case 2:
1748 strip_modversion = 1;
1749 break;
1750 case 3:
1751 first_time = 1;
1752 break;
1753 case 4:
1754 dump_modver = 1;
1755 break;
1756 default:
1757 print_usage(argv[0]);
1761 /* If stderr not open, go to syslog */
1762 if (logging || fstat(STDERR_FILENO, &statbuf) != 0) {
1763 openlog("modprobe", LOG_CONS, LOG_DAEMON);
1764 logging = 1;
1767 if (argc < optind + 1 && !dump_only && !list_only && !remove)
1768 print_usage(argv[0]);
1770 if (!dirname)
1771 nofail_asprintf(&dirname, "%s/%s", MODULE_DIR, buf.release);
1772 nofail_asprintf(&aliasfilename, "%s/modules.alias", dirname);
1773 nofail_asprintf(&symfilename, "%s/modules.symbols", dirname);
1775 /* Old-style -t xxx wildcard? Only with -l. */
1776 if (list_only) {
1777 if (optind+1 < argc)
1778 fatal("Can't have multiple wildcards\n");
1779 /* fprintf(stderr, "man find\n"); return 1; */
1780 return do_wildcard(dirname, type, argv[optind]?:"*");
1782 if (type)
1783 fatal("-t only supported with -l");
1785 if (dump_only) {
1786 struct module_command *commands = NULL;
1787 struct module_options *modoptions = NULL;
1788 struct module_alias *aliases = NULL;
1789 struct module_blacklist *blacklist = NULL;
1791 parse_toplevel_config(config, "", 1, 0, &modoptions,
1792 &commands, &aliases, &blacklist);
1793 /* Read module options from kernel command line */
1794 parse_kcmdline(1, &modoptions);
1795 parse_config_file(aliasfilename, "", 1, 0, &modoptions,
1796 &commands, &aliases, &blacklist);
1797 parse_config_file(symfilename, "", 1, 0, &modoptions,
1798 &commands, &aliases, &blacklist);
1799 exit(0);
1802 if (remove || all) {
1803 num_modules = argc - optind;
1804 optstring = NOFAIL(strdup(""));
1805 } else {
1806 num_modules = 1;
1807 optstring = gather_options(argv+optind+1);
1810 /* num_modules is always 1 except for -r or -a. */
1811 for (i = 0; i < num_modules; i++) {
1812 struct module_command *commands = NULL;
1813 struct module_options *modoptions = NULL;
1814 struct module_alias *aliases = NULL;
1815 struct module_blacklist *blacklist = NULL;
1816 LIST_HEAD(list);
1817 char *modulearg = argv[optind + i];
1819 if (dump_modver) {
1820 dump_modversions(modulearg, error);
1821 continue;
1824 /* Convert name we are looking for */
1825 underscores(modulearg);
1827 /* Returns the resolved alias, options */
1828 parse_toplevel_config(config, modulearg, 0,
1829 remove, &modoptions, &commands, &aliases, &blacklist);
1831 /* Read module options from kernel command line */
1832 parse_kcmdline(0, &modoptions);
1834 /* No luck? Try symbol names, if starts with symbol:. */
1835 if (!aliases &&
1836 strncmp(modulearg, "symbol:", strlen("symbol:")) == 0) {
1837 parse_config_file(symfilename, modulearg, 0,
1838 remove, &modoptions, &commands,
1839 &aliases, &blacklist);
1841 if (!aliases) {
1842 if(!strchr(modulearg, ':'))
1843 read_depends(dirname, modulearg, &list);
1845 /* We only use canned aliases as last resort. */
1846 if (list_empty(&list)
1847 && !find_command(modulearg, commands))
1849 read_aliases_file(aliasfilename,
1850 modulearg, 0, remove,
1851 &modoptions, &commands,
1852 &aliases, &blacklist);
1856 aliases = apply_blacklist(aliases, blacklist);
1857 if (aliases) {
1858 errfn_t err = error;
1860 /* More than one alias? Don't bail out on failure. */
1861 if (aliases->next)
1862 err = warn;
1863 while (aliases) {
1864 /* Add the options for this alias. */
1865 char *opts = NOFAIL(strdup(optstring));
1866 opts = add_extra_options(modulearg,
1867 opts, modoptions);
1869 read_depends(dirname, aliases->module, &list);
1870 handle_module(aliases->module, &list, newname,
1871 remove, opts, first_time, err,
1872 dry_run, verbose, modoptions,
1873 commands, ignore_commands,
1874 ignore_proc, strip_vermagic,
1875 strip_modversion,
1876 unknown_silent,
1877 optstring, flags);
1879 aliases = aliases->next;
1880 INIT_LIST_HEAD(&list);
1882 } else {
1883 if (use_blacklist
1884 && find_blacklist(modulearg, blacklist))
1885 continue;
1887 handle_module(modulearg, &list, newname, remove,
1888 optstring, first_time, error, dry_run,
1889 verbose, modoptions, commands,
1890 ignore_commands, ignore_proc,
1891 strip_vermagic, strip_modversion,
1892 unknown_silent, optstring, flags);
1895 if (logging)
1896 closelog();
1898 free(dirname);
1899 free(aliasfilename);
1900 free(symfilename);
1901 free(optstring);
1903 return 0;