modprobe: --dump-modversions takes one filename only
[mit.git] / modprobe.c
blob74e7d3aa8c47a52fcf4e04e34f7beb4acc673239
1 /* modprobe.c: add or remove a module from the kernel, intelligently.
2 Copyright (C) 2001 Rusty Russell.
3 Copyright (C) 2002, 2003 Rusty Russell, IBM Corporation.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #define _GNU_SOURCE /* asprintf */
21 #include <sys/utsname.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <sys/mman.h>
25 #include <fcntl.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <ctype.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <unistd.h>
33 #include <dirent.h>
34 #include <limits.h>
35 #include <elf.h>
36 #include <getopt.h>
37 #include <fnmatch.h>
38 #include <asm/unistd.h>
39 #include <sys/wait.h>
40 #include <syslog.h>
42 #include "util.h"
43 #include "elfops.h"
44 #include "zlibsupport.h"
45 #include "logging.h"
46 #include "index.h"
47 #include "list.h"
48 #include "config_filter.h"
50 #include "testing.h"
52 int use_binary_indexes = 1; /* default to enabled. */
54 /* Limit do_softdep/do_modprobe recursion.
55 * This is a simple way to handle dependency loops
56 * caused by poorly written softdep commands.
58 static int recursion_depth = 0;
59 const int MAX_RECURSION = 50; /* Arbitrary choice */
61 extern long init_module(void *, unsigned long, const char *);
62 extern long delete_module(const char *, unsigned int);
64 struct module {
65 struct list_head list;
66 char *modname;
67 char filename[0];
70 typedef enum
72 mit_remove = 1,
73 mit_dry_run = 2,
74 mit_first_time = 4,
75 mit_use_blacklist = 8,
76 mit_ignore_commands = 16,
77 mit_ignore_loaded = 32,
78 mit_quiet_inuse = 64,
79 mit_strip_vermagic = 128,
80 mit_strip_modversion = 256,
81 mit_resolve_alias = 512
83 } modprobe_flags_t;
85 #ifndef MODULE_DIR
86 #define MODULE_DIR "/lib/modules"
87 #endif
89 static void print_usage(const char *progname)
91 fprintf(stderr,
92 "Usage: %s [-v] [-V] [-C config-file] [-d <dirname> ] [-n] [-i] [-q] [-b] [-o <modname>] [ --dump-modversions ] <modname> [parameters...]\n"
93 "%s -r [-n] [-i] [-v] <modulename> ...\n"
94 "%s -l -t <dirname> [ -a <modulename> ...]\n",
95 progname, progname, progname);
96 exit(1);
99 static struct module *find_module(const char *filename, struct list_head *list)
101 struct module *i;
103 list_for_each_entry(i, list, list) {
104 if (streq(i->filename, filename))
105 return i;
107 return NULL;
110 static void add_module(char *filename, int namelen, struct list_head *list)
112 struct module *mod;
114 /* If it's a duplicate: move it to the end, so it gets
115 inserted where it is *first* required. */
116 mod = find_module(filename, list);
117 if (mod)
118 list_del(&mod->list);
119 else {
120 /* No match. Create a new module. */
121 mod = NOFAIL(malloc(sizeof(struct module) + namelen + 1));
122 memcpy(mod->filename, filename, namelen);
123 mod->filename[namelen] = '\0';
124 mod->modname = NOFAIL(malloc(namelen + 1));
125 filename2modname(mod->modname, mod->filename);
128 list_add_tail(&mod->list, list);
131 static void free_module(struct module *mod)
133 free(mod->modname);
134 free(mod);
137 /* Compare len chars of a to b, with _ and - equivalent. */
138 static int modname_equal(const char *a, const char *b, unsigned int len)
140 unsigned int i;
142 if (strlen(b) != len)
143 return 0;
145 for (i = 0; i < len; i++) {
146 if ((a[i] == '_' || a[i] == '-')
147 && (b[i] == '_' || b[i] == '-'))
148 continue;
149 if (a[i] != b[i])
150 return 0;
152 return 1;
155 /* Fills in list of modules if this is the line we want. */
156 static int add_modules_dep_line(char *line,
157 const char *name,
158 struct list_head *list,
159 const char *dirname)
161 char *ptr;
162 int len;
163 char *modname, *fullpath;
165 /* Ignore lines without : or which start with a # */
166 ptr = strchr(line, ':');
167 if (ptr == NULL || line[strspn(line, "\t ")] == '#')
168 return 0;
170 /* Is this the module we are looking for? */
171 *ptr = '\0';
172 modname = my_basename(line);
174 len = strlen(modname);
175 if (strchr(modname, '.'))
176 len = strchr(modname, '.') - modname;
177 if (!modname_equal(modname, name, len))
178 return 0;
180 /* Create the list. */
181 if ('/' == line[0]) { /* old style deps - absolute path specified */
182 add_module(line, ptr - line, list);
183 } else {
184 nofail_asprintf(&fullpath, "%s/%s", dirname, line);
185 add_module(fullpath, strlen(dirname)+1+(ptr - line), list);
186 free(fullpath);
189 ptr++;
190 for(;;) {
191 char *dep_start;
192 ptr += strspn(ptr, " \t");
193 if (*ptr == '\0')
194 break;
195 dep_start = ptr;
196 ptr += strcspn(ptr, " \t");
197 if ('/' == dep_start[0]) { /* old style deps */
198 add_module(dep_start, ptr - dep_start, list);
199 } else {
200 nofail_asprintf(&fullpath, "%s/%s", dirname, dep_start);
201 add_module(fullpath,
202 strlen(dirname)+1+(ptr - dep_start), list);
203 free(fullpath);
206 return 1;
209 static int read_depends_file(const char *dirname,
210 const char *start_name,
211 struct list_head *list)
213 char *modules_dep_name;
214 char *line;
215 struct index_file *modules_dep;
217 nofail_asprintf(&modules_dep_name, "%s/%s", dirname, "modules.dep.bin");
218 modules_dep = index_file_open(modules_dep_name);
219 if (!modules_dep) {
220 free(modules_dep_name);
221 return 0;
224 line = index_search(modules_dep, start_name);
225 if (line) {
226 /* Value is standard dependency line format */
227 if (!add_modules_dep_line(line, start_name, list, dirname))
228 fatal("Module index is inconsistent\n");
229 free(line);
232 index_file_close(modules_dep);
233 free(modules_dep_name);
235 return 1;
238 static void read_depends(const char *dirname,
239 const char *start_name,
240 struct list_head *list)
242 char *modules_dep_name;
243 char *line;
244 FILE *modules_dep;
245 int done = 0;
247 if (use_binary_indexes)
248 if (read_depends_file(dirname, start_name, list))
249 return;
251 nofail_asprintf(&modules_dep_name, "%s/%s", dirname, "modules.dep");
252 modules_dep = fopen(modules_dep_name, "r");
253 if (!modules_dep)
254 fatal("Could not load %s: %s\n",
255 modules_dep_name, strerror(errno));
257 /* Stop at first line, as we can have duplicates (eg. symlinks
258 from boot/ */
259 while (!done && (line = getline_wrapped(modules_dep, NULL)) != NULL) {
260 done = add_modules_dep_line(line, start_name, list, dirname);
261 free(line);
263 fclose(modules_dep);
264 free(modules_dep_name);
267 /* We use error numbers in a loose translation... */
268 static const char *insert_moderror(int err)
270 switch (err) {
271 case ENOEXEC:
272 return "Invalid module format";
273 case ENOENT:
274 return "Unknown symbol in module, or unknown parameter (see dmesg)";
275 case ENOSYS:
276 return "Kernel does not have module support";
277 default:
278 return strerror(err);
282 static const char *remove_moderror(int err)
284 switch (err) {
285 case ENOENT:
286 return "No such module";
287 case ENOSYS:
288 return "Kernel does not have module unloading support";
289 default:
290 return strerror(err);
294 static void clear_magic(struct elf_file *module)
296 struct string_table *tbl;
297 int j;
299 /* Old-style: __vermagic section */
300 module->ops->strip_section(module, "__vermagic");
302 /* New-style: in .modinfo section */
303 tbl = module->ops->load_strings(module, ".modinfo", NULL);
304 for (j = 0; tbl && j < tbl->cnt; j++) {
305 const char *p = tbl->str[j];
306 if (strstarts(p, "vermagic=")) {
307 memset((char *)p, 0, strlen(p));
308 return;
313 struct module_options
315 struct module_options *next;
316 char *modulename;
317 char *options;
320 struct module_command
322 struct module_command *next;
323 char *modulename;
324 char *command;
327 struct module_alias
329 struct module_alias *next;
330 char *aliasname;
331 char *module;
334 struct module_blacklist
336 struct module_blacklist *next;
337 char *modulename;
340 struct module_softdep
342 struct module_softdep *next;
343 char *buf;
344 /* The modname and string tables point to buf. */
345 char *modname;
346 struct string_table *pre;
347 struct string_table *post;
350 struct modprobe_conf
352 struct module_options *options;
353 struct module_command *commands;
354 struct module_alias *aliases;
355 struct module_blacklist *blacklist;
356 struct module_softdep *softdeps;
359 /* Link in a new option line from the config file. */
360 static struct module_options *
361 add_options(const char *modname,
362 const char *option,
363 struct module_options *options)
365 struct module_options *new;
366 char *tab;
368 new = NOFAIL(malloc(sizeof(*new)));
369 new->modulename = NOFAIL(strdup(modname));
370 new->options = NOFAIL(strdup(option));
371 /* We can handle tabs, kernel can't. */
372 for (tab = strchr(new->options, '\t'); tab; tab = strchr(tab, '\t'))
373 *tab = ' ';
374 new->next = options;
375 return new;
378 /* Link in a new install line from the config file. */
379 static struct module_command *
380 add_command(const char *modname,
381 const char *command,
382 struct module_command *commands)
384 struct module_command *new;
386 new = NOFAIL(malloc(sizeof(*new)));
387 new->modulename = NOFAIL(strdup(modname));
388 new->command = NOFAIL(strdup(command));
389 new->next = commands;
390 return new;
393 /* Link in a new alias line from the config file. */
394 static struct module_alias *
395 add_alias(const char *aliasname, const char *modname, struct module_alias *aliases)
397 struct module_alias *new;
399 new = NOFAIL(malloc(sizeof(*new)));
400 new->aliasname = NOFAIL(strdup(aliasname));
401 new->module = NOFAIL(strdup(modname));
402 new->next = aliases;
403 return new;
407 /* Return a list of matching aliases */
408 static struct module_alias *
409 find_aliases(const struct module_alias *aliases,
410 const char *name)
412 struct module_alias *result = NULL;
413 while (aliases) {
414 char *aliasname = aliases->aliasname;
415 char *modname = aliases->module;
416 if (fnmatch(aliasname, name, 0) == 0)
417 result = add_alias(aliasname, modname, result);
418 aliases = aliases->next;
420 return result;
423 static void free_aliases(struct module_alias *alias_list)
425 while (alias_list) {
426 struct module_alias *alias;
428 alias = alias_list;
429 alias_list = alias_list->next;
431 free(alias->aliasname);
432 free(alias->module);
433 free(alias);
437 /* Link in a new blacklist line from the config file. */
438 static struct module_blacklist *
439 add_blacklist(const char *modname, struct module_blacklist *blacklist)
441 struct module_blacklist *new;
443 new = NOFAIL(malloc(sizeof(*new)));
444 new->modulename = NOFAIL(strdup(modname));
445 new->next = blacklist;
446 return new;
449 /* Find blacklist commands if any. */
450 static int
451 find_blacklist(const char *modname, const struct module_blacklist *blacklist)
453 while (blacklist) {
454 if (streq(blacklist->modulename, modname))
455 return 1;
456 blacklist = blacklist->next;
458 return 0;
461 /* delete backlisted elems from a list of aliases */
462 static void
463 apply_blacklist(struct module_alias **aliases,
464 const struct module_blacklist *blacklist)
466 struct module_alias *result = NULL;
467 struct module_alias *alias = *aliases;
468 while (alias) {
469 char *modname = alias->module;
470 if (!find_blacklist(modname, blacklist))
471 result = add_alias(alias->aliasname, modname, result);
472 alias = alias->next;
474 free_aliases(*aliases);
475 *aliases = result;
478 /* Find install commands if any. */
479 static const char *find_command(const char *modname,
480 const struct module_command *commands)
482 while (commands) {
483 if (fnmatch(commands->modulename, modname, 0) == 0)
484 return commands->command;
485 commands = commands->next;
487 return NULL;
490 /* Find soft dependencies, if any. */
491 static const struct module_softdep *
492 find_softdep(const char *modname, const struct module_softdep *softdeps)
494 while (softdeps) {
495 if (fnmatch(softdeps->modname, modname, 0) == 0)
496 return softdeps;
497 softdeps = softdeps->next;
499 return NULL;
502 static char *append_option(char *options, const char *newoption)
504 options = NOFAIL(realloc(options, strlen(options) + 1
505 + strlen(newoption) + 1));
506 if (strlen(options)) strcat(options, " ");
507 strcat(options, newoption);
508 return options;
511 static char *prepend_option(char *options, const char *newoption)
513 size_t l1, l2;
514 l1 = strlen(options);
515 l2 = strlen(newoption);
516 /* the resulting string will look like
517 * newoption + ' ' + options + '\0' */
518 if (l1) {
519 options = NOFAIL(realloc(options, l2 + 1 + l1 + 1));
520 memmove(options + l2 + 1, options, l1 + 1);
521 options[l2] = ' ';
522 memcpy(options, newoption, l2);
523 } else {
524 options = NOFAIL(realloc(options, l2 + 1));
525 memcpy(options, newoption, l2);
526 options[l2] = '\0';
528 return options;
531 /* Add to options */
532 static char *add_extra_options(const char *modname,
533 const char *optstring,
534 const struct module_options *options)
536 char *opts = NOFAIL(strdup(optstring));
538 while (options) {
539 if (streq(options->modulename, modname))
540 opts = prepend_option(opts, options->options);
541 options = options->next;
543 return opts;
546 /* Is module in /proc/modules? If so, fill in usecount if not NULL.
547 0 means no, 1 means yes, -1 means unknown.
549 static int module_in_procfs(const char *modname, unsigned int *usecount)
551 FILE *proc_modules;
552 char *line;
554 again:
555 /* Might not be mounted yet. Don't fail. */
556 proc_modules = fopen("/proc/modules", "r");
557 if (!proc_modules)
558 return -1;
560 while ((line = getline_wrapped(proc_modules, NULL)) != NULL) {
561 char *entry = strtok(line, " \n");
563 if (entry && streq(entry, modname)) {
564 /* If it exists, usecount is the third entry. */
565 if (!strtok(NULL, " \n"))
566 goto out;
568 if (!(entry = strtok(NULL, " \n"))) /* usecount */
569 goto out;
570 else
571 if (usecount)
572 *usecount = atoi(entry);
574 /* Followed by - then status. */
575 if (strtok(NULL, " \n")
576 && (entry = strtok(NULL, " \n")) != NULL) {
577 /* No locking, we might hit cases
578 * where module is in flux. Spin. */
579 if (streq(entry, "Loading")
580 || streq(entry, "Unloading")) {
581 usleep(100000);
582 free(line);
583 fclose(proc_modules);
584 goto again;
588 out:
589 free(line);
590 fclose(proc_modules);
591 return 1;
593 free(line);
595 fclose(proc_modules);
596 return 0;
599 /* Read sysfs attribute into a buffer.
600 * returns: 1 = ok, 0 = attribute missing,
601 * -1 = file error (or empty file, but we don't care).
603 static int read_attribute(const char *filename, char *buf, size_t buflen)
605 FILE *file;
606 char *s;
608 file = fopen(filename, "r");
609 if (file == NULL)
610 return (errno == ENOENT) ? 0 : -1;
611 s = fgets(buf, buflen, file);
612 fclose(file);
614 return (s == NULL) ? -1 : 1;
617 /* is this a built-in module?
618 * 0: no, 1: yes, -1: don't know
620 static int module_builtin(const char *dirname, const char *modname)
622 struct index_file *index;
623 char *filename, *value;
625 nofail_asprintf(&filename, "%s/modules.builtin.bin", dirname);
626 index = index_file_open(filename);
627 free(filename);
628 if (!index)
629 return -1;
630 value = index_search(index, modname);
631 free(value);
632 return value ? 1 : 0;
635 /* Is module in /sys/module? If so, fill in usecount if not NULL.
636 0 means no, 1 means yes, -1 means unknown.
638 static int module_in_sysfs(const char *modname, unsigned int *usecount)
640 int ret;
641 char *name;
642 struct stat finfo;
644 const int ATTR_LEN = 16;
645 char attr[ATTR_LEN];
647 /* Check sysfs is mounted */
648 if (stat("/sys/module", &finfo) < 0)
649 return -1;
651 /* Find module. */
652 nofail_asprintf(&name, "/sys/module/%s", modname);
653 ret = stat(name, &finfo);
654 free(name);
655 if (ret < 0)
656 return (errno == ENOENT) ? 0 : -1; /* Not found or unknown. */
658 nofail_asprintf(&name, "/sys/module/%s/initstate", modname);
659 ret = read_attribute(name, attr, ATTR_LEN);
660 if (ret == 0) {
661 free(name);
662 nofail_asprintf(&name, "/sys/module/%s", modname);
663 if (stat(name, &finfo) < 0) {
664 /* module was removed before we could read initstate */
665 ret = 0;
666 } else {
667 /* initstate not available (2.6.19 or earlier) */
668 ret = -1;
670 free(name);
671 return ret;
674 /* Wait for the existing module to either go live or disappear. */
675 while (ret == 1 && !streq(attr, "live\n")) {
676 usleep(100000);
677 ret = read_attribute(name, attr, ATTR_LEN);
679 free(name);
681 if (ret != 1)
682 return ret;
684 /* Get reference count, if it exists. */
685 if (usecount != NULL) {
686 nofail_asprintf(&name, "/sys/module/%s/refcnt", modname);
687 ret = read_attribute(name, attr, ATTR_LEN);
688 free(name);
689 if (ret == 1)
690 *usecount = atoi(attr);
693 return 1;
696 /* Is module loaded? If so, fill in usecount if not NULL.
697 0 means no, 1 means yes, -1 means unknown.
699 static int module_in_kernel(const char *modname, unsigned int *usecount)
701 int result;
703 result = module_in_sysfs(modname, usecount);
704 if (result != -1)
705 return result;
707 /* /sys/module/%s/initstate is only available since 2.6.20,
708 fallback to /proc/modules to get module state on earlier kernels. */
709 return module_in_procfs(modname, usecount);
712 void dump_modversions(const char *filename, errfn_t error)
714 struct elf_file *module;
716 module = grab_elf_file(filename);
717 if (!module) {
718 error("%s: %s\n", filename, strerror(errno));
719 return;
721 if (module->ops->dump_modvers(module) < 0)
722 error("Wrong section size in '%s'\n", filename);
723 release_elf_file(module);
726 /* Does path contain directory(s) subpath? */
727 static int type_matches(const char *path, const char *subpath)
729 char *subpath_with_slashes;
730 int ret;
732 nofail_asprintf(&subpath_with_slashes, "/%s/", subpath);
734 ret = (strstr(path, subpath_with_slashes) != NULL);
735 free(subpath_with_slashes);
736 return ret;
740 static int do_wildcard(const char *dirname,
741 const char *type,
742 const char *wildcard)
744 char *modules_dep_name;
745 char *line, *wcard;
746 FILE *modules_dep;
748 /* Canonicalize wildcard */
749 wcard = strdup(wildcard);
750 underscores(wcard);
752 nofail_asprintf(&modules_dep_name, "%s/%s", dirname, "modules.dep");
753 modules_dep = fopen(modules_dep_name, "r");
754 if (!modules_dep)
755 fatal("Could not load %s: %s\n",
756 modules_dep_name, strerror(errno));
758 while ((line = getline_wrapped(modules_dep, NULL)) != NULL) {
759 char *ptr;
761 /* Ignore lines without : or which start with a # */
762 ptr = strchr(line, ':');
763 if (ptr == NULL || line[strspn(line, "\t ")] == '#')
764 goto next;
765 *ptr = '\0';
767 /* "type" must match complete directory component(s). */
768 if (!type || type_matches(line, type)) {
769 char modname[strlen(line)+1];
771 filename2modname(modname, line);
772 if (fnmatch(wcard, modname, 0) == 0)
773 printf("%s\n", line);
775 next:
776 free(line);
779 free(modules_dep_name);
780 free(wcard);
781 return 0;
784 static char *strsep_skipspace(char **string, char *delim)
786 if (!*string)
787 return NULL;
788 *string += strspn(*string, delim);
789 return strsep(string, delim);
792 static int parse_config_scan(const char *filename,
793 struct modprobe_conf *conf,
794 int dump_only,
795 int removing);
797 static int parse_config_file(const char *filename,
798 struct modprobe_conf *conf,
799 int dump_only,
800 int removing)
802 char *line;
803 unsigned int linenum = 0;
804 FILE *cfile;
806 struct module_options **options = &conf->options;
807 struct module_command **commands = &conf->commands;
808 struct module_alias **aliases = &conf->aliases;
809 struct module_blacklist **blacklist = &conf->blacklist;
811 cfile = fopen(filename, "r");
812 if (!cfile)
813 return 0;
815 while ((line = getline_wrapped(cfile, &linenum)) != NULL) {
816 char *ptr = line;
817 char *cmd, *modname;
819 if (dump_only)
820 printf("%s\n", line);
822 cmd = strsep_skipspace(&ptr, "\t ");
823 if (cmd == NULL || cmd[0] == '#' || cmd[0] == '\0') {
824 free(line);
825 continue;
828 if (streq(cmd, "alias")) {
829 char *wildcard = strsep_skipspace(&ptr, "\t ");
830 char *realname = strsep_skipspace(&ptr, "\t ");
831 if (!wildcard || !realname)
832 goto syntax_error;
833 *aliases = add_alias(underscores(wildcard),
834 underscores(realname),
835 *aliases);
836 } else if (streq(cmd, "include")) {
837 struct modprobe_conf newconf = *conf;
838 newconf.aliases = NULL;
839 char *newfilename;
840 newfilename = strsep_skipspace(&ptr, "\t ");
841 if (!newfilename)
842 goto syntax_error;
844 warn("\"include %s\" is deprecated, "
845 "please use /etc/modprobe.d\n", newfilename);
846 if (strstarts(newfilename, "/etc/modprobe.d")) {
847 warn("\"include /etc/modprobe.d\" is "
848 "the default, ignored\n");
849 } else {
850 if (!parse_config_scan(newfilename,
851 &newconf, dump_only,
852 removing))
853 warn("Failed to open included"
854 " config file %s: %s\n",
855 newfilename, strerror(errno));
857 /* Files included override aliases,
858 etc that was already set ... */
859 if (newconf.aliases)
860 *aliases = newconf.aliases;
862 } else if (streq(cmd, "options")) {
863 modname = strsep_skipspace(&ptr, "\t ");
864 if (!modname || !ptr)
865 goto syntax_error;
867 ptr += strspn(ptr, "\t ");
868 *options = add_options(underscores(modname),
869 ptr, *options);
871 } else if (streq(cmd, "install")) {
872 modname = strsep_skipspace(&ptr, "\t ");
873 if (!modname || !ptr)
874 goto syntax_error;
875 if (!removing) {
876 ptr += strspn(ptr, "\t ");
877 *commands = add_command(underscores(modname),
878 ptr, *commands);
880 } else if (streq(cmd, "blacklist")) {
881 modname = strsep_skipspace(&ptr, "\t ");
882 if (!modname)
883 goto syntax_error;
884 if (!removing) {
885 *blacklist = add_blacklist(underscores(modname),
886 *blacklist);
888 } else if (streq(cmd, "remove")) {
889 modname = strsep_skipspace(&ptr, "\t ");
890 if (!modname || !ptr)
891 goto syntax_error;
892 if (removing) {
893 ptr += strspn(ptr, "\t ");
894 *commands = add_command(underscores(modname),
895 ptr, *commands);
897 } else if (streq(cmd, "softdep")) {
898 char *tk;
899 int pre = 0, post = 0;
900 struct string_table *pre_modnames = NULL;
901 struct string_table *post_modnames = NULL;
902 struct module_softdep *new;
904 modname = underscores(strsep_skipspace(&ptr, "\t "));
905 if (!modname || !ptr)
906 goto syntax_error;
907 while ((tk = strsep_skipspace(&ptr, "\t ")) != NULL) {
908 if (streq(tk, "pre:")) {
909 pre = 1; post = 0;
910 } else if (streq(tk, "post:")) {
911 pre = 0; post = 1;
912 } else if (pre) {
913 pre_modnames = NOFAIL(
914 strtbl_add(tk, pre_modnames));
915 } else if (post) {
916 post_modnames = NOFAIL(
917 strtbl_add(tk, post_modnames));
918 } else {
919 strtbl_free(pre_modnames);
920 strtbl_free(post_modnames);
921 goto syntax_error;
924 new = NOFAIL(malloc(sizeof(*new)));
925 new->buf = line;
926 new->modname = modname;
927 new->pre = pre_modnames;
928 new->post = post_modnames;
929 new->next = conf->softdeps;
930 conf->softdeps = new;
932 line = NULL; /* Don't free() this line. */
934 } else if (streq(cmd, "config")) {
935 char *tmp = strsep_skipspace(&ptr, "\t ");
937 if (!tmp)
938 goto syntax_error;
939 if (streq(tmp, "binary_indexes")) {
940 tmp = strsep_skipspace(&ptr, "\t ");
941 if (streq(tmp, "yes"))
942 use_binary_indexes = 1;
943 if (streq(tmp, "no"))
944 use_binary_indexes = 0;
946 } else {
947 syntax_error:
948 grammar(cmd, filename, linenum);
951 free(line);
953 fclose(cfile);
954 return 1;
957 /* Read binary index file containing aliases only */
958 static int read_aliases_file(const char *filename,
959 const char *name,
960 int dump_only,
961 struct module_alias **aliases)
963 struct index_value *realnames;
964 struct index_value *realname;
965 char *binfile;
966 struct index_file *index;
968 nofail_asprintf(&binfile, "%s.bin", filename);
969 index = index_file_open(binfile);
970 if (!index) {
971 free(binfile);
972 return 0;
975 if (dump_only) {
976 index_dump(index, stdout, "alias ");
977 free(binfile);
978 index_file_close(index);
979 return 1;
982 realnames = index_searchwild(index, name);
983 for (realname = realnames; realname; realname = realname->next)
984 *aliases = add_alias("*", realname->value, *aliases);
985 index_values_free(realnames);
987 free(binfile);
988 index_file_close(index);
989 return 1;
992 /* fallback to plain-text aliases file if necessary */
993 static int read_aliases(const char *filename,
994 const char *name,
995 int dump_only,
996 struct module_alias **aliases)
998 char *line;
999 unsigned int linenum = 0;
1000 FILE *cfile;
1002 if (use_binary_indexes)
1003 if (read_aliases_file(filename, name, dump_only, aliases))
1004 return 1;
1006 cfile = fopen(filename, "r");
1007 if (!cfile)
1008 return 0;
1010 while ((line = getline_wrapped(cfile, &linenum)) != NULL) {
1011 char *ptr = line;
1012 char *cmd;
1014 if (dump_only)
1015 printf("%s\n", line);
1017 cmd = strsep_skipspace(&ptr, "\t ");
1018 if (cmd == NULL || cmd[0] == '#' || cmd[0] == '\0') {
1019 free(line);
1020 continue;
1023 if (streq(cmd, "alias")) {
1024 char *wildcard = strsep_skipspace(&ptr, "\t ");
1025 char *realname = strsep_skipspace(&ptr, "\t ");
1026 if (!wildcard || !realname)
1027 goto syntax_error;
1028 if (fnmatch(underscores(wildcard),name,0) == 0)
1029 *aliases = add_alias(wildcard,
1030 underscores(realname),
1031 *aliases);
1032 } else {
1033 syntax_error:
1034 grammar(cmd, filename, linenum);
1037 free(line);
1039 fclose(cfile);
1040 return 1;
1043 static int parse_config_scan(const char *filename,
1044 struct modprobe_conf *conf,
1045 int dump_only,
1046 int removing)
1048 DIR *dir;
1049 int ret = 0;
1051 dir = opendir(filename);
1052 if (dir) {
1053 struct file_entry {
1054 struct list_head node;
1055 char name[];
1057 LIST_HEAD(files_list);
1058 struct file_entry *fe, *fe_tmp;
1059 struct dirent *i;
1061 /* sort files from directory into list */
1062 while ((i = readdir(dir)) != NULL) {
1063 size_t len;
1065 if (i->d_name[0] == '.')
1066 continue;
1067 if (!config_filter(i->d_name))
1068 continue;
1070 len = strlen(i->d_name);
1071 if (len < 6 ||
1072 (strcmp(&i->d_name[len-5], ".conf") != 0 &&
1073 strcmp(&i->d_name[len-6], ".alias") != 0))
1074 warn("All config files need .conf: %s/%s, "
1075 "it will be ignored in a future release.\n",
1076 filename, i->d_name);
1077 fe = malloc(sizeof(struct file_entry) + len + 1);
1078 if (fe == NULL)
1079 continue;
1080 strcpy(fe->name, i->d_name);
1081 list_for_each_entry(fe_tmp, &files_list, node)
1082 if (strcmp(fe_tmp->name, fe->name) >= 0)
1083 break;
1084 list_add_tail(&fe->node, &fe_tmp->node);
1086 closedir(dir);
1088 /* parse list of files */
1089 list_for_each_entry_safe(fe, fe_tmp, &files_list, node) {
1090 char *cfgfile;
1092 nofail_asprintf(&cfgfile, "%s/%s", filename, fe->name);
1093 if (!parse_config_file(cfgfile, conf,
1094 dump_only, removing))
1095 warn("Failed to open config file "
1096 "%s: %s\n", fe->name, strerror(errno));
1097 free(cfgfile);
1098 list_del(&fe->node);
1099 free(fe);
1102 ret = 1;
1103 } else {
1104 if (parse_config_file(filename, conf, dump_only, removing))
1105 ret = 1;
1107 return ret;
1110 static void parse_toplevel_config(const char *filename,
1111 struct modprobe_conf *conf,
1112 int dump_only,
1113 int removing)
1115 if (filename) {
1116 if (!parse_config_scan(filename, conf, dump_only, removing))
1117 fatal("Failed to open config file %s: %s\n",
1118 filename, strerror(errno));
1119 return;
1122 /* deprecated config file */
1123 if (parse_config_file("/etc/modprobe.conf", conf,
1124 dump_only, removing) > 0)
1125 warn("Deprecated config file /etc/modprobe.conf, "
1126 "all config files belong into /etc/modprobe.d/.\n");
1128 /* default config */
1129 parse_config_scan("/etc/modprobe.d", conf, dump_only, removing);
1132 /* Read possible module arguments from the kernel command line. */
1133 static int parse_kcmdline(int dump_only, struct module_options **options)
1135 char *line;
1136 unsigned int linenum = 0;
1137 FILE *kcmdline;
1139 kcmdline = fopen("/proc/cmdline", "r");
1140 if (!kcmdline)
1141 return 0;
1143 while ((line = getline_wrapped(kcmdline, &linenum)) != NULL) {
1144 char *ptr = line;
1145 char *arg;
1147 while ((arg = strsep_skipspace(&ptr, "\t ")) != NULL) {
1148 char *sep, *modname, *opt;
1150 sep = strchr(arg, '.');
1151 if (sep) {
1152 if (!strchr(sep, '='))
1153 continue;
1154 modname = arg;
1155 *sep = '\0';
1156 opt = ++sep;
1158 if (dump_only)
1159 printf("options %s %s\n", modname, opt);
1161 *options = add_options(underscores(modname),
1162 opt, *options);
1166 free(line);
1168 fclose(kcmdline);
1169 return 1;
1172 static void add_to_env_var(const char *option)
1174 const char *oldenv;
1176 if ((oldenv = getenv("MODPROBE_OPTIONS")) != NULL) {
1177 char *newenv;
1178 nofail_asprintf(&newenv, "%s %s", oldenv, option);
1179 setenv("MODPROBE_OPTIONS", newenv, 1);
1180 free(newenv);
1181 } else
1182 setenv("MODPROBE_OPTIONS", option, 1);
1185 /* Prepend options from environment. */
1186 static char **merge_args(char *args, char *argv[], int *argc)
1188 char *arg, *argstring;
1189 char **newargs = NULL;
1190 unsigned int i, num_env = 0;
1192 if (!args)
1193 return argv;
1195 argstring = NOFAIL(strdup(args));
1196 for (arg = strtok(argstring, " "); arg; arg = strtok(NULL, " ")) {
1197 num_env++;
1198 newargs = NOFAIL(realloc(newargs,
1199 sizeof(newargs[0])
1200 * (num_env + *argc + 1)));
1201 newargs[num_env] = arg;
1204 if (!newargs)
1205 return argv;
1207 /* Append commandline args */
1208 newargs[0] = argv[0];
1209 for (i = 1; i <= *argc; i++)
1210 newargs[num_env+i] = argv[i];
1212 *argc += num_env;
1213 return newargs;
1216 static char *gather_options(char *argv[])
1218 char *optstring = NOFAIL(strdup(""));
1220 /* Rest is module options */
1221 while (*argv) {
1222 /* Quote value if it contains spaces. */
1223 unsigned int eq = strcspn(*argv, "=");
1225 if (strchr(*argv+eq, ' ') && !strchr(*argv, '"')) {
1226 char quoted[strlen(*argv) + 3];
1227 (*argv)[eq] = '\0';
1228 sprintf(quoted, "%s=\"%s\"", *argv, *argv+eq+1);
1229 optstring = append_option(optstring, quoted);
1230 } else
1231 optstring = append_option(optstring, *argv);
1232 argv++;
1234 return optstring;
1237 /* Do an install/remove command: replace $CMDLINE_OPTS if it's specified. */
1238 static void do_command(const char *modname,
1239 const char *command,
1240 int dry_run,
1241 errfn_t error,
1242 const char *type,
1243 const char *cmdline_opts)
1245 int ret;
1246 char *p, *replaced_cmd = NOFAIL(strdup(command));
1248 while ((p = strstr(replaced_cmd, "$CMDLINE_OPTS")) != NULL) {
1249 char *new;
1250 nofail_asprintf(&new, "%.*s%s%s",
1251 (int)(p - replaced_cmd), replaced_cmd, cmdline_opts,
1252 p + strlen("$CMDLINE_OPTS"));
1253 free(replaced_cmd);
1254 replaced_cmd = new;
1257 info("%s %s\n", type, replaced_cmd);
1258 if (dry_run)
1259 goto out;
1261 setenv("MODPROBE_MODULE", modname, 1);
1262 ret = system(replaced_cmd);
1263 if (ret == -1 || WEXITSTATUS(ret))
1264 error("Error running %s command for %s\n", type, modname);
1266 out:
1267 free(replaced_cmd);
1270 /* Forward declaration */
1271 int do_modprobe(const char *modname,
1272 const char *cmdline_opts,
1273 const struct modprobe_conf *conf,
1274 const char *dirname,
1275 errfn_t error,
1276 modprobe_flags_t flags);
1278 static void do_softdep(const struct module_softdep *softdep,
1279 const char *cmdline_opts,
1280 const struct modprobe_conf *conf,
1281 const char *dirname,
1282 errfn_t error,
1283 modprobe_flags_t flags)
1285 struct string_table *pre_modnames, *post_modnames;
1286 modprobe_flags_t softdep_flags = flags;
1287 int i, j;
1289 softdep_flags &= ~mit_first_time;
1290 softdep_flags &= ~mit_ignore_commands;
1291 if (flags & mit_remove)
1292 softdep_flags |= mit_quiet_inuse;
1294 if (++recursion_depth >= MAX_RECURSION)
1295 fatal("modprobe: softdep dependency loop encountered %s %s\n",
1296 (flags & mit_remove) ? "removing" : "inserting",
1297 softdep->modname);
1299 if (flags & mit_remove) {
1300 /* Reverse module order if removing. */
1301 pre_modnames = softdep->post;
1302 post_modnames = softdep->pre;
1303 } else {
1304 pre_modnames = softdep->pre;
1305 post_modnames = softdep->post;
1308 /* Modprobe pre_modnames */
1310 for (i = 0; pre_modnames && i < pre_modnames->cnt; i++) {
1311 /* Reverse module order if removing. */
1312 j = (flags & mit_remove) ? pre_modnames->cnt-1 - i : i;
1314 do_modprobe(pre_modnames->str[j], "",
1315 conf, dirname, warn, softdep_flags);
1318 /* Modprobe main module, passing cmdline_opts, ignoring softdep */
1320 do_modprobe(softdep->modname, cmdline_opts,
1321 conf, dirname, warn, flags | mit_ignore_commands);
1323 /* Modprobe post_modnames */
1325 for (i = 0; post_modnames && i < post_modnames->cnt; i++) {
1326 /* Reverse module order if removing. */
1327 j = (flags & mit_remove) ? post_modnames->cnt-1 - i : i;
1329 do_modprobe(post_modnames->str[j], "", conf,
1330 dirname, warn, softdep_flags);
1334 /* Actually do the insert. */
1335 static int insmod(struct list_head *list,
1336 const char *optstring,
1337 const char *cmdline_opts,
1338 const struct modprobe_conf *conf,
1339 const char *dirname,
1340 errfn_t error,
1341 modprobe_flags_t flags)
1343 int ret;
1344 struct elf_file *module;
1345 const struct module_softdep *softdep;
1346 const char *command;
1347 struct module *mod = list_entry(list->next, struct module, list);
1348 int rc = 0;
1349 int already_loaded;
1350 char *opts = NULL;
1352 /* Take us off the list. */
1353 list_del(&mod->list);
1355 /* Do things we (or parent) depend on first. */
1356 if (!list_empty(list)) {
1357 modprobe_flags_t f = flags;
1358 f &= ~mit_first_time;
1359 f &= ~mit_ignore_commands;
1360 if ((rc = insmod(list, "", "", conf, dirname, warn, f)) != 0)
1362 error("Error inserting %s (%s): %s\n",
1363 mod->modname, mod->filename,
1364 insert_moderror(errno));
1365 goto out;
1369 /* Don't do ANYTHING if already in kernel. */
1370 already_loaded = module_in_kernel(mod->modname, NULL);
1372 if (!(flags & mit_ignore_loaded) && already_loaded == 1) {
1373 if (flags & mit_first_time)
1374 error("Module %s already in kernel.\n", mod->modname);
1375 goto out;
1378 softdep = find_softdep(mod->modname, conf->softdeps);
1379 if (softdep && !(flags & mit_ignore_commands)) {
1380 do_softdep(softdep, cmdline_opts, conf, dirname, error, flags);
1381 goto out;
1384 command = find_command(mod->modname, conf->commands);
1385 if (command && !(flags & mit_ignore_commands)) {
1386 if (already_loaded == -1) {
1387 warn("/sys/module/ not present or too old,"
1388 " and /proc/modules does not exist.\n");
1389 warn("Ignoring install commands for %s"
1390 " in case it is already loaded.\n",
1391 mod->modname);
1392 } else {
1393 do_command(mod->modname, command, flags & mit_dry_run,
1394 error, "install", cmdline_opts);
1395 goto out;
1399 module = grab_elf_file(mod->filename);
1400 if (!module) {
1401 error("Could not read '%s': %s\n", mod->filename,
1402 (errno == ENOEXEC) ? "Invalid module format" :
1403 strerror(errno));
1404 goto out;
1406 if (flags & mit_strip_modversion)
1407 module->ops->strip_section(module, "__versions");
1408 if (flags & mit_strip_vermagic)
1409 clear_magic(module);
1411 /* Config file might have given more options */
1412 opts = add_extra_options(mod->modname, optstring, conf->options);
1414 info("insmod %s %s\n", mod->filename, opts);
1416 if (flags & mit_dry_run)
1417 goto out_elf_file;
1419 ret = init_module(module->data, module->len, opts);
1420 if (ret != 0) {
1421 if (errno == EEXIST) {
1422 if (flags & mit_first_time)
1423 error("Module %s already in kernel.\n",
1424 mod->modname);
1425 goto out_elf_file;
1427 /* don't warn noisely if we're loading multiple aliases. */
1428 /* one of the aliases may try to use hardware we don't have. */
1429 if ((error != warn) || (verbose))
1430 error("Error inserting %s (%s): %s\n",
1431 mod->modname, mod->filename,
1432 insert_moderror(errno));
1433 rc = 1;
1435 out_elf_file:
1436 release_elf_file(module);
1437 free(opts);
1438 out:
1439 free_module(mod);
1440 return rc;
1443 /* Do recursive removal. */
1444 static void rmmod(struct list_head *list,
1445 const char *cmdline_opts,
1446 const struct modprobe_conf *conf,
1447 const char *dirname,
1448 errfn_t error,
1449 modprobe_flags_t flags)
1451 const struct module_softdep *softdep;
1452 const char *command;
1453 unsigned int usecount = 0;
1454 struct module *mod = list_entry(list->next, struct module, list);
1455 int exists;
1457 /* Take first one off the list. */
1458 list_del(&mod->list);
1460 /* Don't do ANYTHING if not loaded. */
1461 exists = module_in_kernel(mod->modname, &usecount);
1462 if (exists == 0)
1463 goto nonexistent_module;
1465 /* Even if renamed, find commands/softdeps to orig. name. */
1467 softdep = find_softdep(mod->modname, conf->softdeps);
1468 if (softdep && !(flags & mit_ignore_commands)) {
1469 do_softdep(softdep, cmdline_opts, conf, dirname, error, flags);
1470 goto remove_rest;
1473 command = find_command(mod->modname, conf->commands);
1474 if (command && !(flags & mit_ignore_commands)) {
1475 if (exists == -1) {
1476 warn("/sys/module/ not present or too old,"
1477 " and /proc/modules does not exist.\n");
1478 warn("Ignoring remove commands for %s"
1479 " in case it is not loaded.\n",
1480 mod->modname);
1481 } else {
1482 do_command(mod->modname, command, flags & mit_dry_run,
1483 error, "remove", cmdline_opts);
1484 goto remove_rest;
1488 if (usecount != 0) {
1489 if (!(flags & mit_quiet_inuse))
1490 error("Module %s is in use.\n", mod->modname);
1491 goto remove_rest;
1494 info("rmmod %s\n", mod->filename);
1496 if (flags & mit_dry_run)
1497 goto remove_rest;
1499 if (delete_module(mod->modname, O_EXCL) != 0) {
1500 if (errno == ENOENT)
1501 goto nonexistent_module;
1502 error("Error removing %s (%s): %s\n",
1503 mod->modname, mod->filename,
1504 remove_moderror(errno));
1507 remove_rest:
1508 /* Now do things we depend. */
1509 if (!list_empty(list)) {
1510 flags &= ~mit_first_time;
1511 flags &= ~mit_ignore_commands;
1512 flags |= mit_quiet_inuse;
1514 rmmod(list, "", conf, dirname, warn, flags);
1516 free_module(mod);
1517 return;
1519 nonexistent_module:
1520 if (flags & mit_first_time)
1521 fatal("Module %s is not in kernel.\n", mod->modname);
1522 goto remove_rest;
1525 static int handle_module(const char *modname,
1526 struct list_head *todo_list,
1527 const char *options,
1528 const char *cmdline_opts,
1529 const struct modprobe_conf *conf,
1530 const char *dirname,
1531 errfn_t error,
1532 modprobe_flags_t flags)
1534 if (list_empty(todo_list)) {
1535 const char *command;
1537 /* The dependencies have to be real modules, but
1538 handle case where the first is completely bogus. */
1540 command = find_command(modname, conf->commands);
1541 if (command && !(flags & mit_ignore_commands)) {
1542 do_command(modname, command, flags & mit_dry_run, error,
1543 (flags & mit_remove) ? "remove":"install", cmdline_opts);
1544 return 0;
1547 if (!quiet)
1548 error("Module %s not found.\n", modname);
1549 return 1;
1552 if (flags & mit_remove)
1553 rmmod(todo_list, cmdline_opts,
1554 conf, dirname, error, flags);
1555 else
1556 insmod(todo_list, options,
1557 cmdline_opts, conf, dirname, error, flags);
1559 return 0;
1562 int handle_builtin_module(const char *modname,
1563 errfn_t error,
1564 modprobe_flags_t flags)
1566 if (flags & mit_remove) {
1567 error("Module %s is builtin\n", modname);
1568 return 1;
1569 } else if (flags & mit_first_time) {
1570 error("Module %s already in kernel (builtin).\n", modname);
1571 return 1;
1572 } else if (flags & mit_ignore_loaded) {
1573 /* --show-depends given */
1574 info("builtin %s\n", modname);
1576 return 0;
1579 int do_modprobe(const char *modulename,
1580 const char *cmdline_opts,
1581 const struct modprobe_conf *conf,
1582 const char *dirname,
1583 errfn_t error,
1584 modprobe_flags_t flags)
1586 char *modname;
1587 struct module_alias *matching_aliases;
1588 LIST_HEAD(list);
1589 int failed = 0;
1591 /* Convert name we are looking for */
1592 modname = underscores(NOFAIL(strdup(modulename)));
1594 matching_aliases = find_aliases(conf->aliases, modname);
1596 /* No luck? Try symbol names, if starts with symbol:. */
1597 if (!matching_aliases && strstarts(modname, "symbol:")) {
1598 char *symfilename;
1600 nofail_asprintf(&symfilename, "%s/modules.symbols", dirname);
1601 read_aliases(symfilename, modname, 0, &matching_aliases);
1602 free(symfilename);
1604 if (!matching_aliases) {
1605 if(!strchr(modname, ':'))
1606 read_depends(dirname, modname, &list);
1608 /* We only use canned aliases as last resort. */
1609 if (list_empty(&list)
1610 && !find_softdep(modname, conf->softdeps)
1611 && !find_command(modname, conf->commands))
1613 char *aliasfilename;
1615 nofail_asprintf(&aliasfilename, "%s/modules.alias",
1616 dirname);
1617 read_aliases(aliasfilename, modname, 0,
1618 &matching_aliases);
1619 free(aliasfilename);
1620 /* builtin module? */
1621 if (!matching_aliases && module_builtin(dirname, modname) > 0) {
1622 failed |= handle_builtin_module(modname, error,
1623 flags);
1624 goto out;
1629 apply_blacklist(&matching_aliases, conf->blacklist);
1630 if(flags & mit_resolve_alias) {
1631 struct module_alias *aliases = matching_aliases;
1633 for(; aliases; aliases=aliases->next)
1634 printf("%s\n", aliases->module);
1635 goto out;
1637 if (matching_aliases) {
1638 errfn_t err = error;
1639 struct module_alias *aliases = matching_aliases;
1641 /* More than one alias? Don't bail out on failure. */
1642 if (aliases->next)
1643 err = warn;
1644 while (aliases) {
1645 /* Add the options for this alias. */
1646 char *opts;
1647 opts = add_extra_options(modname,
1648 cmdline_opts, conf->options);
1650 read_depends(dirname, aliases->module, &list);
1651 failed |= handle_module(aliases->module,
1652 &list, opts, cmdline_opts,
1653 conf, dirname, err, flags);
1655 aliases = aliases->next;
1656 free(opts);
1657 INIT_LIST_HEAD(&list);
1659 } else {
1660 if (flags & mit_use_blacklist
1661 && find_blacklist(modname, conf->blacklist))
1662 goto out;
1664 failed |= handle_module(modname, &list, cmdline_opts,
1665 cmdline_opts, conf, dirname, error, flags);
1668 out:
1669 free(modname);
1670 free_aliases(matching_aliases);
1671 return failed;
1674 static struct option options[] = { { "version", 0, NULL, 'V' },
1675 { "verbose", 0, NULL, 'v' },
1676 { "quiet", 0, NULL, 'q' },
1677 { "syslog", 0, NULL, 's' },
1678 { "show", 0, NULL, 'n' },
1679 { "dry-run", 0, NULL, 'n' },
1680 { "show-depends", 0, NULL, 'D' },
1681 { "resolve-alias", 0, NULL, 'R' },
1682 { "dirname", 1, NULL, 'd' },
1683 { "set-version", 1, NULL, 'S' },
1684 { "config", 1, NULL, 'C' },
1685 { "remove", 0, NULL, 'r' },
1686 { "showconfig", 0, NULL, 'c' },
1687 { "list", 0, NULL, 'l' },
1688 { "type", 1, NULL, 't' },
1689 { "all", 0, NULL, 'a' },
1690 { "ignore-install", 0, NULL, 'i' },
1691 { "ignore-remove", 0, NULL, 'i' },
1692 { "use-blacklist", 0, NULL, 'b' },
1693 { "force", 0, NULL, 'f' },
1694 { "force-vermagic", 0, NULL, 1 },
1695 { "force-modversion", 0, NULL, 2 },
1696 { "first-time", 0, NULL, 3 },
1697 { "dump-modversions", 0, NULL, 4 },
1698 { NULL, 0, NULL, 0 } };
1700 int main(int argc, char *argv[])
1702 struct utsname buf;
1703 struct stat statbuf;
1704 int opt;
1705 int dump_config = 0;
1706 int list_only = 0;
1707 int all = 0;
1708 int dump_modver = 0;
1709 unsigned int i, num_modules;
1710 char *type = NULL;
1711 const char *configname = NULL;
1712 char *basedir = "";
1713 char *cmdline_opts = NULL;
1714 char *dirname;
1715 errfn_t error = fatal;
1716 int failed = 0;
1717 modprobe_flags_t flags = 0;
1718 struct modprobe_conf conf = {};
1720 recursion_depth = 0;
1722 /* Prepend options from environment. */
1723 argv = merge_args(getenv("MODPROBE_OPTIONS"), argv, &argc);
1725 uname(&buf);
1726 while ((opt = getopt_long(argc, argv, "Vvqsnd:S:C:DRrclt:aibf", options, NULL)) != -1){
1727 switch (opt) {
1728 case 'V':
1729 puts(PACKAGE " version " VERSION);
1730 exit(0);
1731 case 'v':
1732 add_to_env_var("-v");
1733 verbose = 1;
1734 break;
1735 case 'q':
1736 quiet = 1;
1737 add_to_env_var("-q");
1738 break;
1739 case 's':
1740 add_to_env_var("-s");
1741 logging = 1;
1742 break;
1743 case 'n':
1744 flags |= mit_dry_run;
1745 break;
1746 case 'd':
1747 basedir = optarg;
1748 break;
1749 case 'S':
1750 strncpy(buf.release, optarg, sizeof(buf.release));
1751 buf.release[sizeof(buf.release)-1] = '\0';
1752 break;
1753 case 'C':
1754 configname = optarg;
1755 add_to_env_var("-C");
1756 add_to_env_var(configname);
1757 break;
1758 case 'D':
1759 flags |= mit_dry_run;
1760 flags |= mit_ignore_loaded;
1761 verbose = 1;
1762 break;
1763 case 'R':
1764 flags |= mit_resolve_alias;
1765 break;
1766 case 'r':
1767 flags |= mit_remove;
1768 break;
1769 case 'c':
1770 dump_config = 1;
1771 break;
1772 case 'l':
1773 list_only = 1;
1774 break;
1775 case 't':
1776 type = optarg;
1777 break;
1778 case 'a':
1779 all = 1;
1780 error = warn;
1781 break;
1782 case 'i':
1783 flags |= mit_ignore_commands;
1784 break;
1785 case 'b':
1786 flags |= mit_use_blacklist;
1787 break;
1788 case 'f':
1789 flags |= mit_strip_vermagic;
1790 flags |= mit_strip_modversion;
1791 break;
1792 case 1:
1793 flags |= mit_strip_vermagic;
1794 break;
1795 case 2:
1796 flags |= mit_strip_modversion;
1797 break;
1798 case 3:
1799 flags |= mit_first_time;
1800 break;
1801 case 4:
1802 dump_modver = 1;
1803 break;
1804 default:
1805 print_usage(argv[0]);
1809 /* If stderr not open, go to syslog */
1810 if (logging || fstat(STDERR_FILENO, &statbuf) != 0) {
1811 openlog("modprobe", LOG_CONS, LOG_DAEMON);
1812 logging = 1;
1815 if (argc < optind + 1 && !dump_config && !list_only)
1816 print_usage(argv[0]);
1818 nofail_asprintf(&dirname, "%s%s/%s", basedir, MODULE_DIR, buf.release);
1820 /* Old-style -t xxx wildcard? Only with -l. */
1821 if (list_only) {
1822 if (optind+1 < argc)
1823 fatal("Can't have multiple wildcards\n");
1824 /* fprintf(stderr, "man find\n"); return 1; */
1825 failed = do_wildcard(dirname, type, argv[optind]?:"*");
1826 goto out;
1828 if (type)
1829 fatal("-t only supported with -l");
1831 if (dump_modver) {
1832 dump_modversions(argv[optind], error);
1833 goto out;
1836 /* Read aliases, options etc. */
1837 parse_toplevel_config(configname, &conf, dump_config, flags & mit_remove);
1839 /* Read module options from kernel command line */
1840 parse_kcmdline(dump_config, &conf.options);
1842 if (dump_config) {
1843 char *aliasfilename, *symfilename;
1844 struct modprobe_conf conf = {};
1846 nofail_asprintf(&aliasfilename, "%s/modules.alias", dirname);
1847 nofail_asprintf(&symfilename, "%s/modules.symbols", dirname);
1849 read_aliases(aliasfilename, "", 1, &conf.aliases);
1850 read_aliases(symfilename, "", 1, &conf.aliases);
1852 goto out;
1855 if ((flags & mit_remove) || all) {
1856 num_modules = argc - optind;
1857 cmdline_opts = NOFAIL(strdup(""));
1858 } else {
1859 num_modules = 1;
1860 cmdline_opts = gather_options(argv+optind+1);
1863 /* num_modules is always 1 except for -r or -a. */
1864 for (i = 0; i < num_modules; i++) {
1865 char *modname = argv[optind + i];
1867 failed |= do_modprobe(modname, cmdline_opts,
1868 &conf, dirname, error, flags);
1871 out:
1872 if (logging)
1873 closelog();
1874 free(dirname);
1875 free(cmdline_opts);
1876 /* Don't bother to free conf */
1878 exit(failed);