modprobe: cleanup source, Free Software Foundation mailing address.
[module-init-tools.git] / modprobe.c
blob966809ae7b05fa1b0cf43491a1de8169250ee976
1 /* modprobe.c: add or remove a module from the kernel, intelligently.
3 * Copyright (C) 2001 Rusty Russell.
4 * Copyright (C) 2002, 2003 Rusty Russell, IBM Corporation.
5 * Copyright (C) 2006-2011 Jon Masters <jcm@jonmasters.org>, and others.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
22 #define _GNU_SOURCE /* asprintf */
24 #include <sys/utsname.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/mman.h>
28 #include <fcntl.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <ctype.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <unistd.h>
36 #include <dirent.h>
37 #include <limits.h>
38 #include <elf.h>
39 #include <getopt.h>
40 #include <fnmatch.h>
41 #include <asm/unistd.h>
42 #include <sys/wait.h>
43 #include <syslog.h>
45 #include "util.h"
46 #include "elfops.h"
47 #include "zlibsupport.h"
48 #include "logging.h"
49 #include "index.h"
50 #include "list.h"
51 #include "config_filter.h"
53 #include "testing.h"
55 static int use_binary_indexes = 1; /* default to enabled. */
57 /* Limit do_softdep/do_modprobe recursion.
58 * This is a simple way to handle dependency loops
59 * caused by poorly written softdep commands.
61 static int recursion_depth = 0;
62 static const int MAX_RECURSION = 50; /* Arbitrary choice */
64 extern long init_module(void *, unsigned long, const char *);
65 extern long delete_module(const char *, unsigned int);
67 struct module {
68 struct list_head list;
69 char *modname;
70 char filename[0];
73 typedef enum
75 mit_remove = 1,
76 mit_dry_run = 2,
77 mit_first_time = 4,
78 mit_use_blacklist = 8,
79 mit_ignore_commands = 16,
80 mit_ignore_loaded = 32,
81 mit_quiet_inuse = 64,
82 mit_strip_vermagic = 128,
83 mit_strip_modversion = 256,
84 mit_resolve_alias = 512
86 } modprobe_flags_t;
88 #ifndef MODULE_DIR
89 #define MODULE_DIR "/lib/modules"
90 #endif
92 static void print_usage(const char *progname)
94 fprintf(stderr,
95 "Usage: %s [-v] [-V] [-C config-file] [-d <dirname> ] [-n] [-i] [-q] [-b] [-o <modname>] [ --dump-modversions ] <modname> [parameters...]\n"
96 "%s -r [-n] [-i] [-v] <modulename> ...\n"
97 "%s -l -t <dirname> [ -a <modulename> ...]\n",
98 progname, progname, progname);
99 exit(1);
102 static struct module *find_module(const char *filename, struct list_head *list)
104 struct module *i;
106 list_for_each_entry(i, list, list) {
107 if (streq(i->filename, filename))
108 return i;
110 return NULL;
113 static void add_module(char *filename, int namelen, struct list_head *list)
115 struct module *mod;
117 /* If it's a duplicate: move it to the end, so it gets
118 inserted where it is *first* required. */
119 mod = find_module(filename, list);
120 if (mod)
121 list_del(&mod->list);
122 else {
123 /* No match. Create a new module. */
124 mod = NOFAIL(malloc(sizeof(struct module) + namelen + 1));
125 memcpy(mod->filename, filename, namelen);
126 mod->filename[namelen] = '\0';
127 mod->modname = NOFAIL(malloc(namelen + 1));
128 filename2modname(mod->modname, mod->filename);
131 list_add_tail(&mod->list, list);
134 static void free_module(struct module *mod)
136 free(mod->modname);
137 free(mod);
140 /* Compare len chars of a to b, with _ and - equivalent. */
141 static int modname_equal(const char *a, const char *b, unsigned int len)
143 unsigned int i;
145 if (strlen(b) != len)
146 return 0;
148 for (i = 0; i < len; i++) {
149 if ((a[i] == '_' || a[i] == '-')
150 && (b[i] == '_' || b[i] == '-'))
151 continue;
152 if (a[i] != b[i])
153 return 0;
155 return 1;
158 /* Fills in list of modules if this is the line we want. */
159 static int add_modules_dep_line(char *line,
160 const char *name,
161 struct list_head *list,
162 const char *dirname)
164 char *ptr;
165 int len;
166 char *modname, *fullpath;
168 /* Ignore lines without : or which start with a # */
169 ptr = strchr(line, ':');
170 if (ptr == NULL || line[strspn(line, "\t ")] == '#')
171 return 0;
173 /* Is this the module we are looking for? */
174 *ptr = '\0';
175 modname = my_basename(line);
177 len = strlen(modname);
178 if (strchr(modname, '.'))
179 len = strchr(modname, '.') - modname;
180 if (!modname_equal(modname, name, len))
181 return 0;
183 /* Create the list. */
184 if ('/' == line[0]) { /* old style deps - absolute path specified */
185 add_module(line, ptr - line, list);
186 } else {
187 nofail_asprintf(&fullpath, "%s/%s", dirname, line);
188 add_module(fullpath, strlen(dirname)+1+(ptr - line), list);
189 free(fullpath);
192 ptr++;
193 for(;;) {
194 char *dep_start;
195 ptr += strspn(ptr, " \t");
196 if (*ptr == '\0')
197 break;
198 dep_start = ptr;
199 ptr += strcspn(ptr, " \t");
200 if ('/' == dep_start[0]) { /* old style deps */
201 add_module(dep_start, ptr - dep_start, list);
202 } else {
203 nofail_asprintf(&fullpath, "%s/%s", dirname, dep_start);
204 add_module(fullpath,
205 strlen(dirname)+1+(ptr - dep_start), list);
206 free(fullpath);
209 return 1;
212 static int read_depends_file(const char *dirname,
213 const char *start_name,
214 struct list_head *list)
216 char *modules_dep_name;
217 char *line;
218 struct index_file *modules_dep;
220 nofail_asprintf(&modules_dep_name, "%s/%s", dirname, "modules.dep.bin");
221 modules_dep = index_file_open(modules_dep_name);
222 if (!modules_dep) {
223 free(modules_dep_name);
224 return 0;
227 line = index_search(modules_dep, start_name);
228 if (line) {
229 /* Value is standard dependency line format */
230 if (!add_modules_dep_line(line, start_name, list, dirname))
231 fatal("Module index is inconsistent\n");
232 free(line);
235 index_file_close(modules_dep);
236 free(modules_dep_name);
238 return 1;
241 static void read_depends(const char *dirname,
242 const char *start_name,
243 struct list_head *list)
245 char *modules_dep_name;
246 char *line;
247 FILE *modules_dep;
248 int done = 0;
250 if (use_binary_indexes)
251 if (read_depends_file(dirname, start_name, list))
252 return;
254 nofail_asprintf(&modules_dep_name, "%s/%s", dirname, "modules.dep");
255 modules_dep = fopen(modules_dep_name, "r");
256 if (!modules_dep)
257 fatal("Could not load %s: %s\n",
258 modules_dep_name, strerror(errno));
260 /* Stop at first line, as we can have duplicates (eg. symlinks
261 from boot/ */
262 while (!done && (line = getline_wrapped(modules_dep, NULL)) != NULL) {
263 done = add_modules_dep_line(line, start_name, list, dirname);
264 free(line);
266 fclose(modules_dep);
267 free(modules_dep_name);
270 /* We use error numbers in a loose translation... */
271 static const char *insert_moderror(int err)
273 switch (err) {
274 case ENOEXEC:
275 return "Invalid module format";
276 case ENOENT:
277 return "Unknown symbol in module, or unknown parameter (see dmesg)";
278 case ENOSYS:
279 return "Kernel does not have module support";
280 default:
281 return strerror(err);
285 static const char *remove_moderror(int err)
287 switch (err) {
288 case ENOENT:
289 return "No such module";
290 case ENOSYS:
291 return "Kernel does not have module unloading support";
292 default:
293 return strerror(err);
297 static void clear_magic(struct elf_file *module)
299 struct string_table *tbl;
300 int j;
302 /* Old-style: __vermagic section */
303 module->ops->strip_section(module, "__vermagic");
305 /* New-style: in .modinfo section */
306 tbl = module->ops->load_strings(module, ".modinfo", NULL);
307 for (j = 0; tbl && j < tbl->cnt; j++) {
308 const char *p = tbl->str[j];
309 if (strstarts(p, "vermagic=")) {
310 memset((char *)p, 0, strlen(p));
311 return;
316 struct module_options
318 struct module_options *next;
319 char *modulename;
320 char *options;
323 struct module_command
325 struct module_command *next;
326 char *modulename;
327 char *command;
330 struct module_alias
332 struct module_alias *next;
333 char *aliasname;
334 char *module;
337 struct module_blacklist
339 struct module_blacklist *next;
340 char *modulename;
343 struct module_softdep
345 struct module_softdep *next;
346 char *buf;
347 /* The modname and string tables point to buf. */
348 char *modname;
349 struct string_table *pre;
350 struct string_table *post;
353 struct modprobe_conf
355 struct module_options *options;
356 struct module_command *commands;
357 struct module_alias *aliases;
358 struct module_blacklist *blacklist;
359 struct module_softdep *softdeps;
362 /* Link in a new option line from the config file. */
363 static struct module_options *
364 add_options(const char *modname,
365 const char *option,
366 struct module_options *options)
368 struct module_options *new;
369 char *tab;
371 new = NOFAIL(malloc(sizeof(*new)));
372 new->modulename = NOFAIL(strdup(modname));
373 new->options = NOFAIL(strdup(option));
374 /* We can handle tabs, kernel can't. */
375 for (tab = strchr(new->options, '\t'); tab; tab = strchr(tab, '\t'))
376 *tab = ' ';
377 new->next = options;
378 return new;
381 /* Link in a new install line from the config file. */
382 static struct module_command *
383 add_command(const char *modname,
384 const char *command,
385 struct module_command *commands)
387 struct module_command *new;
389 new = NOFAIL(malloc(sizeof(*new)));
390 new->modulename = NOFAIL(strdup(modname));
391 new->command = NOFAIL(strdup(command));
392 new->next = commands;
393 return new;
396 /* Link in a new alias line from the config file. */
397 static struct module_alias *
398 add_alias(const char *aliasname, const char *modname, struct module_alias *aliases)
400 struct module_alias *new;
402 new = NOFAIL(malloc(sizeof(*new)));
403 new->aliasname = NOFAIL(strdup(aliasname));
404 new->module = NOFAIL(strdup(modname));
405 new->next = aliases;
406 return new;
410 /* Return a list of matching aliases */
411 static struct module_alias *
412 find_aliases(const struct module_alias *aliases,
413 const char *name)
415 struct module_alias *result = NULL;
416 while (aliases) {
417 char *aliasname = aliases->aliasname;
418 char *modname = aliases->module;
419 if (fnmatch(aliasname, name, 0) == 0)
420 result = add_alias(aliasname, modname, result);
421 aliases = aliases->next;
423 return result;
426 static void free_aliases(struct module_alias *alias_list)
428 while (alias_list) {
429 struct module_alias *alias;
431 alias = alias_list;
432 alias_list = alias_list->next;
434 free(alias->aliasname);
435 free(alias->module);
436 free(alias);
440 /* Link in a new blacklist line from the config file. */
441 static struct module_blacklist *
442 add_blacklist(const char *modname, struct module_blacklist *blacklist)
444 struct module_blacklist *new;
446 new = NOFAIL(malloc(sizeof(*new)));
447 new->modulename = NOFAIL(strdup(modname));
448 new->next = blacklist;
449 return new;
452 /* Find blacklist commands if any. */
453 static int
454 find_blacklist(const char *modname, const struct module_blacklist *blacklist)
456 while (blacklist) {
457 if (streq(blacklist->modulename, modname))
458 return 1;
459 blacklist = blacklist->next;
461 return 0;
464 /* delete backlisted elems from a list of aliases */
465 static void
466 apply_blacklist(struct module_alias **aliases,
467 const struct module_blacklist *blacklist)
469 struct module_alias *result = NULL;
470 struct module_alias *alias = *aliases;
471 while (alias) {
472 char *modname = alias->module;
473 if (!find_blacklist(modname, blacklist))
474 result = add_alias(alias->aliasname, modname, result);
475 alias = alias->next;
477 free_aliases(*aliases);
478 *aliases = result;
481 /* Find install commands if any. */
482 static const char *find_command(const char *modname,
483 const struct module_command *commands)
485 while (commands) {
486 if (fnmatch(commands->modulename, modname, 0) == 0)
487 return commands->command;
488 commands = commands->next;
490 return NULL;
493 /* Find soft dependencies, if any. */
494 static const struct module_softdep *
495 find_softdep(const char *modname, const struct module_softdep *softdeps)
497 while (softdeps) {
498 if (fnmatch(softdeps->modname, modname, 0) == 0)
499 return softdeps;
500 softdeps = softdeps->next;
502 return NULL;
505 static char *append_option(char *options, const char *newoption)
507 options = NOFAIL(realloc(options, strlen(options) + 1
508 + strlen(newoption) + 1));
509 if (strlen(options)) strcat(options, " ");
510 strcat(options, newoption);
511 return options;
514 static char *prepend_option(char *options, const char *newoption)
516 size_t l1, l2;
517 l1 = strlen(options);
518 l2 = strlen(newoption);
519 /* the resulting string will look like
520 * newoption + ' ' + options + '\0' */
521 if (l1) {
522 options = NOFAIL(realloc(options, l2 + 1 + l1 + 1));
523 memmove(options + l2 + 1, options, l1 + 1);
524 options[l2] = ' ';
525 memcpy(options, newoption, l2);
526 } else {
527 options = NOFAIL(realloc(options, l2 + 1));
528 memcpy(options, newoption, l2);
529 options[l2] = '\0';
531 return options;
534 /* Add to options */
535 static char *add_extra_options(const char *modname,
536 const char *optstring,
537 const struct module_options *options)
539 char *opts = NOFAIL(strdup(optstring));
541 while (options) {
542 if (streq(options->modulename, modname))
543 opts = prepend_option(opts, options->options);
544 options = options->next;
546 return opts;
549 /* Is module in /proc/modules? If so, fill in usecount if not NULL.
550 0 means no, 1 means yes, -1 means unknown.
552 static int module_in_procfs(const char *modname, unsigned int *usecount)
554 FILE *proc_modules;
555 char *line;
557 again:
558 /* Might not be mounted yet. Don't fail. */
559 proc_modules = fopen("/proc/modules", "r");
560 if (!proc_modules)
561 return -1;
563 while ((line = getline_wrapped(proc_modules, NULL)) != NULL) {
564 char *entry = strtok(line, " \n");
566 if (entry && streq(entry, modname)) {
567 /* If it exists, usecount is the third entry. */
568 if (!strtok(NULL, " \n"))
569 goto out;
571 if (!(entry = strtok(NULL, " \n"))) /* usecount */
572 goto out;
573 else
574 if (usecount)
575 *usecount = atoi(entry);
577 /* Followed by - then status. */
578 if (strtok(NULL, " \n")
579 && (entry = strtok(NULL, " \n")) != NULL) {
580 /* No locking, we might hit cases
581 * where module is in flux. Spin. */
582 if (streq(entry, "Loading")
583 || streq(entry, "Unloading")) {
584 usleep(100000);
585 free(line);
586 fclose(proc_modules);
587 goto again;
591 out:
592 free(line);
593 fclose(proc_modules);
594 return 1;
596 free(line);
598 fclose(proc_modules);
599 return 0;
602 /* Read sysfs attribute into a buffer.
603 * returns: 1 = ok, 0 = attribute missing,
604 * -1 = file error (or empty file, but we don't care).
606 static int read_attribute(const char *filename, char *buf, size_t buflen)
608 FILE *file;
609 char *s;
611 file = fopen(filename, "r");
612 if (file == NULL)
613 return (errno == ENOENT) ? 0 : -1;
614 s = fgets(buf, buflen, file);
615 fclose(file);
617 return (s == NULL) ? -1 : 1;
620 /* is this a built-in module?
621 * 0: no, 1: yes, -1: don't know
623 static int module_builtin(const char *dirname, const char *modname)
625 struct index_file *index;
626 char *filename, *value;
628 nofail_asprintf(&filename, "%s/modules.builtin.bin", dirname);
629 index = index_file_open(filename);
630 free(filename);
631 if (!index)
632 return -1;
633 value = index_search(index, modname);
634 free(value);
635 return value ? 1 : 0;
638 /* Is module in /sys/module? If so, fill in usecount if not NULL.
639 0 means no, 1 means yes, -1 means unknown.
641 static int module_in_sysfs(const char *modname, unsigned int *usecount)
643 int ret;
644 char *name;
645 struct stat finfo;
647 const int ATTR_LEN = 16;
648 char attr[ATTR_LEN];
650 /* Check sysfs is mounted */
651 if (stat("/sys/module", &finfo) < 0)
652 return -1;
654 /* Find module. */
655 nofail_asprintf(&name, "/sys/module/%s", modname);
656 ret = stat(name, &finfo);
657 free(name);
658 if (ret < 0)
659 return (errno == ENOENT) ? 0 : -1; /* Not found or unknown. */
661 nofail_asprintf(&name, "/sys/module/%s/initstate", modname);
662 ret = read_attribute(name, attr, ATTR_LEN);
663 if (ret == 0) {
664 free(name);
665 nofail_asprintf(&name, "/sys/module/%s", modname);
666 if (stat(name, &finfo) < 0) {
667 /* module was removed before we could read initstate */
668 ret = 0;
669 } else {
670 /* initstate not available (2.6.19 or earlier) */
671 ret = -1;
673 free(name);
674 return ret;
677 /* Wait for the existing module to either go live or disappear. */
678 while (ret == 1 && !streq(attr, "live\n")) {
679 usleep(100000);
680 ret = read_attribute(name, attr, ATTR_LEN);
682 free(name);
684 if (ret != 1)
685 return ret;
687 /* Get reference count, if it exists. */
688 if (usecount != NULL) {
689 nofail_asprintf(&name, "/sys/module/%s/refcnt", modname);
690 ret = read_attribute(name, attr, ATTR_LEN);
691 free(name);
692 if (ret == 1)
693 *usecount = atoi(attr);
696 return 1;
699 /* Is module loaded? If so, fill in usecount if not NULL.
700 0 means no, 1 means yes, -1 means unknown.
702 static int module_in_kernel(const char *modname, unsigned int *usecount)
704 int result;
706 result = module_in_sysfs(modname, usecount);
707 if (result != -1)
708 return result;
710 /* /sys/module/%s/initstate is only available since 2.6.20,
711 fallback to /proc/modules to get module state on earlier kernels. */
712 return module_in_procfs(modname, usecount);
715 static void dump_modversions(const char *filename, errfn_t error)
717 struct elf_file *module;
719 module = grab_elf_file(filename);
720 if (!module) {
721 error("%s: %s\n", filename, strerror(errno));
722 return;
724 if (module->ops->dump_modvers(module) < 0)
725 error("Wrong section size in '%s'\n", filename);
726 release_elf_file(module);
729 /* Does path contain directory(s) subpath? */
730 static int type_matches(const char *path, const char *subpath)
732 char *subpath_with_slashes;
733 int ret;
735 nofail_asprintf(&subpath_with_slashes, "/%s/", subpath);
737 ret = (strstr(path, subpath_with_slashes) != NULL);
738 free(subpath_with_slashes);
739 return ret;
743 static int do_wildcard(const char *dirname,
744 const char *type,
745 const char *wildcard)
747 char *modules_dep_name;
748 char *line, *wcard;
749 FILE *modules_dep;
751 /* Canonicalize wildcard */
752 wcard = strdup(wildcard);
753 underscores(wcard);
755 nofail_asprintf(&modules_dep_name, "%s/%s", dirname, "modules.dep");
756 modules_dep = fopen(modules_dep_name, "r");
757 if (!modules_dep)
758 fatal("Could not load %s: %s\n",
759 modules_dep_name, strerror(errno));
761 while ((line = getline_wrapped(modules_dep, NULL)) != NULL) {
762 char *ptr;
764 /* Ignore lines without : or which start with a # */
765 ptr = strchr(line, ':');
766 if (ptr == NULL || line[strspn(line, "\t ")] == '#')
767 goto next;
768 *ptr = '\0';
770 /* "type" must match complete directory component(s). */
771 if (!type || type_matches(line, type)) {
772 char modname[strlen(line)+1];
774 filename2modname(modname, line);
775 if (fnmatch(wcard, modname, 0) == 0)
776 printf("%s\n", line);
778 next:
779 free(line);
782 free(modules_dep_name);
783 free(wcard);
784 return 0;
787 static char *strsep_skipspace(char **string, char *delim)
789 if (!*string)
790 return NULL;
791 *string += strspn(*string, delim);
792 return strsep(string, delim);
795 static int parse_config_scan(struct modprobe_conf *conf,
796 int dump_only,
797 int removing, ...);
799 static int parse_config_file(const char *filename,
800 struct modprobe_conf *conf,
801 int dump_only,
802 int removing)
804 char *line;
805 unsigned int linenum = 0;
806 FILE *cfile;
808 struct module_options **options = &conf->options;
809 struct module_command **commands = &conf->commands;
810 struct module_alias **aliases = &conf->aliases;
811 struct module_blacklist **blacklist = &conf->blacklist;
813 cfile = fopen(filename, "r");
814 if (!cfile)
815 return 0;
817 while ((line = getline_wrapped(cfile, &linenum)) != NULL) {
818 char *ptr = line;
819 char *cmd, *modname;
821 if (dump_only)
822 printf("%s\n", line);
824 cmd = strsep_skipspace(&ptr, "\t ");
825 if (cmd == NULL || cmd[0] == '#' || cmd[0] == '\0') {
826 free(line);
827 continue;
830 if (streq(cmd, "alias")) {
831 char *wildcard = strsep_skipspace(&ptr, "\t ");
832 char *realname = strsep_skipspace(&ptr, "\t ");
833 if (!wildcard || !realname)
834 goto syntax_error;
835 *aliases = add_alias(underscores(wildcard),
836 underscores(realname),
837 *aliases);
838 } else if (streq(cmd, "include")) {
839 struct modprobe_conf newconf = *conf;
840 newconf.aliases = NULL;
841 char *newfilename;
842 newfilename = strsep_skipspace(&ptr, "\t ");
843 if (!newfilename)
844 goto syntax_error;
846 warn("\"include %s\" is deprecated, "
847 "please use /etc/modprobe.d\n", newfilename);
848 if (strstarts(newfilename, "/etc/modprobe.d")) {
849 warn("\"include /etc/modprobe.d\" is "
850 "the default, ignored\n");
851 } else {
852 if (!parse_config_scan(&newconf, dump_only,
853 removing, newfilename,
854 NULL))
855 warn("Failed to open included"
856 " config file %s: %s\n",
857 newfilename, strerror(errno));
859 /* Files included override aliases,
860 etc that was already set ... */
861 if (newconf.aliases)
862 *aliases = newconf.aliases;
864 } else if (streq(cmd, "options")) {
865 modname = strsep_skipspace(&ptr, "\t ");
866 if (!modname || !ptr)
867 goto syntax_error;
869 ptr += strspn(ptr, "\t ");
870 *options = add_options(underscores(modname),
871 ptr, *options);
873 } else if (streq(cmd, "install")) {
874 modname = strsep_skipspace(&ptr, "\t ");
875 if (!modname || !ptr)
876 goto syntax_error;
877 if (!removing) {
878 ptr += strspn(ptr, "\t ");
879 *commands = add_command(underscores(modname),
880 ptr, *commands);
882 } else if (streq(cmd, "blacklist")) {
883 modname = strsep_skipspace(&ptr, "\t ");
884 if (!modname)
885 goto syntax_error;
886 if (!removing) {
887 *blacklist = add_blacklist(underscores(modname),
888 *blacklist);
890 } else if (streq(cmd, "remove")) {
891 modname = strsep_skipspace(&ptr, "\t ");
892 if (!modname || !ptr)
893 goto syntax_error;
894 if (removing) {
895 ptr += strspn(ptr, "\t ");
896 *commands = add_command(underscores(modname),
897 ptr, *commands);
899 } else if (streq(cmd, "softdep")) {
900 char *tk;
901 int pre = 0, post = 0;
902 struct string_table *pre_modnames = NULL;
903 struct string_table *post_modnames = NULL;
904 struct module_softdep *new;
906 modname = strsep_skipspace(&ptr, "\t ");
907 if (!modname || !ptr)
908 goto syntax_error;
909 modname = underscores(modname);
911 while ((tk = strsep_skipspace(&ptr, "\t ")) != NULL) {
912 tk = underscores(tk);
914 if (streq(tk, "pre:")) {
915 pre = 1; post = 0;
916 } else if (streq(tk, "post:")) {
917 pre = 0; post = 1;
918 } else if (pre) {
919 pre_modnames = NOFAIL(
920 strtbl_add(tk, pre_modnames));
921 } else if (post) {
922 post_modnames = NOFAIL(
923 strtbl_add(tk, post_modnames));
924 } else {
925 strtbl_free(pre_modnames);
926 strtbl_free(post_modnames);
927 goto syntax_error;
930 new = NOFAIL(malloc(sizeof(*new)));
931 new->buf = line;
932 new->modname = modname;
933 new->pre = pre_modnames;
934 new->post = post_modnames;
935 new->next = conf->softdeps;
936 conf->softdeps = new;
938 line = NULL; /* Don't free() this line. */
940 } else if (streq(cmd, "config")) {
941 char *tmp = strsep_skipspace(&ptr, "\t ");
943 if (!tmp)
944 goto syntax_error;
945 if (streq(tmp, "binary_indexes")) {
946 tmp = strsep_skipspace(&ptr, "\t ");
947 if (streq(tmp, "yes"))
948 use_binary_indexes = 1;
949 if (streq(tmp, "no"))
950 use_binary_indexes = 0;
952 } else {
953 syntax_error:
954 grammar(cmd, filename, linenum);
957 free(line);
959 fclose(cfile);
960 return 1;
963 /* Read binary index file containing aliases only */
964 static int read_aliases_file(const char *filename,
965 const char *name,
966 int dump_only,
967 struct module_alias **aliases)
969 struct index_value *realnames;
970 struct index_value *realname;
971 char *binfile;
972 struct index_file *index;
974 nofail_asprintf(&binfile, "%s.bin", filename);
975 index = index_file_open(binfile);
976 if (!index) {
977 free(binfile);
978 return 0;
981 if (dump_only) {
982 index_dump(index, stdout, "alias ");
983 free(binfile);
984 index_file_close(index);
985 return 1;
988 realnames = index_searchwild(index, name);
989 for (realname = realnames; realname; realname = realname->next)
990 *aliases = add_alias("*", realname->value, *aliases);
991 index_values_free(realnames);
993 free(binfile);
994 index_file_close(index);
995 return 1;
998 /* fallback to plain-text aliases file if necessary */
999 static int read_aliases(const char *filename,
1000 const char *name,
1001 int dump_only,
1002 struct module_alias **aliases)
1004 char *line;
1005 unsigned int linenum = 0;
1006 FILE *cfile;
1008 if (use_binary_indexes)
1009 if (read_aliases_file(filename, name, dump_only, aliases))
1010 return 1;
1012 cfile = fopen(filename, "r");
1013 if (!cfile)
1014 return 0;
1016 while ((line = getline_wrapped(cfile, &linenum)) != NULL) {
1017 char *ptr = line;
1018 char *cmd;
1020 if (dump_only)
1021 printf("%s\n", line);
1023 cmd = strsep_skipspace(&ptr, "\t ");
1024 if (cmd == NULL || cmd[0] == '#' || cmd[0] == '\0') {
1025 free(line);
1026 continue;
1029 if (streq(cmd, "alias")) {
1030 char *wildcard = strsep_skipspace(&ptr, "\t ");
1031 char *realname = strsep_skipspace(&ptr, "\t ");
1032 if (!wildcard || !realname)
1033 goto syntax_error;
1034 if (fnmatch(underscores(wildcard),name,0) == 0)
1035 *aliases = add_alias(wildcard,
1036 underscores(realname),
1037 *aliases);
1038 } else {
1039 syntax_error:
1040 grammar(cmd, filename, linenum);
1043 free(line);
1045 fclose(cfile);
1046 return 1;
1049 static int parse_config_scan(struct modprobe_conf *conf,
1050 int dump_only,
1051 int removing, ...)
1053 va_list filelist;
1054 char *filename;
1055 DIR *dir;
1056 struct file_entry {
1057 struct list_head node;
1058 char *name;
1059 char *path;
1061 struct file_entry *fe, *fe_tmp;
1062 LIST_HEAD(files_list);
1063 int ret = 0;
1065 va_start(filelist, removing);
1067 while ((filename = va_arg(filelist, char*))) {
1068 dir = opendir(filename);
1069 if (dir) {
1070 struct dirent *i;
1072 /* sort files from directories into list, ignoring duplicates */
1073 while ((i = readdir(dir)) != NULL) {
1074 size_t len;
1075 int cmp = -1;
1077 if (i->d_name[0] == '.')
1078 continue;
1079 if (!config_filter(i->d_name))
1080 continue;
1082 len = strlen(i->d_name);
1083 if (len < 6 ||
1084 (strcmp(&i->d_name[len-5], ".conf") != 0 &&
1085 strcmp(&i->d_name[len-6], ".alias") != 0))
1086 warn("All config files need .conf: %s/%s, "
1087 "it will be ignored in a future release.\n",
1088 filename, i->d_name);
1089 fe = malloc(sizeof(struct file_entry));
1090 if (fe == NULL)
1091 continue;
1093 list_for_each_entry(fe_tmp, &files_list, node)
1094 if ((cmp = strcmp(fe_tmp->name, i->d_name)) >= 0)
1095 break;
1097 if (cmp != 0) {
1098 fe->name = malloc(len + 1);
1099 fe->path = malloc(strlen(filename) + 1);
1100 strcpy(fe->name, i->d_name);
1101 strcpy(fe->path, filename);
1103 if (cmp < 0)
1104 list_add_tail(&fe->node, &files_list);
1105 else
1106 list_add_tail(&fe->node, &fe_tmp->node);
1107 } else
1108 info("Ignoring config file %s/%s\n", filename, i->d_name);
1111 closedir(dir);
1113 ret = 1;
1114 } else {
1115 if (parse_config_file(filename, conf, dump_only, removing))
1116 ret = 1;
1120 /* parse list of files */
1121 list_for_each_entry_safe(fe, fe_tmp, &files_list, node) {
1122 char *cfgfile;
1124 nofail_asprintf(&cfgfile, "%s/%s", fe->path, fe->name);
1125 if (!parse_config_file(cfgfile, conf,
1126 dump_only, removing))
1127 warn("Failed to open config file %s: %s\n",
1128 cfgfile, strerror(errno));
1129 free(cfgfile);
1130 list_del(&fe->node);
1131 free(fe->name);
1132 free(fe->path);
1133 free(fe);
1136 va_end(filelist);
1137 return ret;
1140 static void parse_toplevel_config(const char *filename,
1141 struct modprobe_conf *conf,
1142 int dump_only,
1143 int removing)
1145 if (filename) {
1146 if (!parse_config_scan(conf, dump_only, removing, filename,
1147 NULL))
1148 fatal("Failed to open config file %s: %s\n",
1149 filename, strerror(errno));
1150 return;
1153 /* deprecated config file */
1154 if (parse_config_file("/etc/modprobe.conf", conf,
1155 dump_only, removing) > 0)
1156 warn("Deprecated config file /etc/modprobe.conf, "
1157 "all config files belong into /etc/modprobe.d/.\n");
1159 /* default config */
1160 parse_config_scan(conf, dump_only, removing, "/run/modprobe.d",
1161 "/etc/modprobe.d", "/usr/local/lib/modprobe.d",
1162 "/lib/modprobe.d", NULL);
1165 /* Read possible module arguments from the kernel command line. */
1166 static int parse_kcmdline(int dump_only, struct modprobe_conf *conf)
1168 char *line;
1169 unsigned int linenum = 0;
1170 FILE *kcmdline;
1171 struct module_options **options = &conf->options;
1172 struct module_blacklist **blacklist = &conf->blacklist;
1174 kcmdline = fopen("/proc/cmdline", "r");
1175 if (!kcmdline)
1176 return 0;
1178 while ((line = getline_wrapped(kcmdline, &linenum)) != NULL) {
1179 char *ptr = line;
1180 char *arg;
1182 while ((arg = strsep_skipspace(&ptr, "\t ")) != NULL) {
1183 char *sep, *modname, *opt;
1185 if (strstr(arg, "modprobe.blacklist=") != NULL) {
1186 ptr = strchr(arg,'=') + 1;
1188 while ((modname = strsep(&ptr, ",")) != NULL) {
1189 if (dump_only)
1190 printf("blacklist %s\n", modname);
1192 *blacklist = add_blacklist(underscores(modname), *blacklist);
1196 sep = strchr(arg, '.');
1197 if (sep) {
1198 if (!strchr(sep, '='))
1199 continue;
1200 modname = arg;
1201 *sep = '\0';
1202 opt = ++sep;
1204 if (dump_only)
1205 printf("options %s %s\n", modname, opt);
1207 *options = add_options(underscores(modname),
1208 opt, *options);
1212 free(line);
1214 fclose(kcmdline);
1215 return 1;
1218 static void add_to_env_var(const char *option)
1220 const char *oldenv;
1222 if ((oldenv = getenv("MODPROBE_OPTIONS")) != NULL) {
1223 char *newenv;
1224 nofail_asprintf(&newenv, "%s %s", oldenv, option);
1225 setenv("MODPROBE_OPTIONS", newenv, 1);
1226 free(newenv);
1227 } else
1228 setenv("MODPROBE_OPTIONS", option, 1);
1231 /* Prepend options from environment. */
1232 static char **merge_args(char *args, char *argv[], int *argc)
1234 char *arg, *argstring;
1235 char **newargs = NULL;
1236 unsigned int i, num_env = 0;
1238 if (!args)
1239 return argv;
1241 argstring = NOFAIL(strdup(args));
1242 for (arg = strtok(argstring, " "); arg; arg = strtok(NULL, " ")) {
1243 num_env++;
1244 newargs = NOFAIL(realloc(newargs,
1245 sizeof(newargs[0])
1246 * (num_env + *argc + 1)));
1247 newargs[num_env] = arg;
1250 if (!newargs)
1251 return argv;
1253 /* Append commandline args */
1254 newargs[0] = argv[0];
1255 for (i = 1; i <= *argc; i++)
1256 newargs[num_env+i] = argv[i];
1258 *argc += num_env;
1259 return newargs;
1262 static char *gather_options(char *argv[])
1264 char *optstring = NOFAIL(strdup(""));
1266 /* Rest is module options */
1267 while (*argv) {
1268 /* Quote value if it contains spaces. */
1269 unsigned int eq = strcspn(*argv, "=");
1271 if (strchr(*argv+eq, ' ') && !strchr(*argv, '"')) {
1272 char quoted[strlen(*argv) + 3];
1273 (*argv)[eq] = '\0';
1274 sprintf(quoted, "%s=\"%s\"", *argv, *argv+eq+1);
1275 optstring = append_option(optstring, quoted);
1276 } else
1277 optstring = append_option(optstring, *argv);
1278 argv++;
1280 return optstring;
1283 /* Do an install/remove command: replace $CMDLINE_OPTS if it's specified. */
1284 static void do_command(const char *modname,
1285 const char *command,
1286 int dry_run,
1287 errfn_t error,
1288 const char *type,
1289 const char *cmdline_opts)
1291 int ret;
1292 char *p, *replaced_cmd = NOFAIL(strdup(command));
1294 while ((p = strstr(replaced_cmd, "$CMDLINE_OPTS")) != NULL) {
1295 char *new;
1296 nofail_asprintf(&new, "%.*s%s%s",
1297 (int)(p - replaced_cmd), replaced_cmd, cmdline_opts,
1298 p + strlen("$CMDLINE_OPTS"));
1299 free(replaced_cmd);
1300 replaced_cmd = new;
1303 info("%s %s\n", type, replaced_cmd);
1304 if (dry_run)
1305 goto out;
1307 setenv("MODPROBE_MODULE", modname, 1);
1308 ret = system(replaced_cmd);
1309 if (ret == -1 || WEXITSTATUS(ret))
1310 error("Error running %s command for %s\n", type, modname);
1312 out:
1313 free(replaced_cmd);
1316 /* Forward declaration */
1317 static int do_modprobe(const char *modname,
1318 const char *cmdline_opts,
1319 const struct modprobe_conf *conf,
1320 const char *dirname,
1321 errfn_t error,
1322 modprobe_flags_t flags);
1324 static void do_softdep(const struct module_softdep *softdep,
1325 const char *cmdline_opts,
1326 const struct modprobe_conf *conf,
1327 const char *dirname,
1328 errfn_t error,
1329 modprobe_flags_t flags)
1331 struct string_table *pre_modnames, *post_modnames;
1332 modprobe_flags_t softdep_flags = flags;
1333 int i, j;
1335 softdep_flags &= ~mit_first_time;
1336 softdep_flags &= ~mit_ignore_commands;
1337 if (flags & mit_remove)
1338 softdep_flags |= mit_quiet_inuse;
1340 if (++recursion_depth >= MAX_RECURSION)
1341 fatal("modprobe: softdep dependency loop encountered %s %s\n",
1342 (flags & mit_remove) ? "removing" : "inserting",
1343 softdep->modname);
1345 if (flags & mit_remove) {
1346 /* Reverse module order if removing. */
1347 pre_modnames = softdep->post;
1348 post_modnames = softdep->pre;
1349 } else {
1350 pre_modnames = softdep->pre;
1351 post_modnames = softdep->post;
1354 /* Modprobe pre_modnames */
1356 for (i = 0; pre_modnames && i < pre_modnames->cnt; i++) {
1357 /* Reverse module order if removing. */
1358 j = (flags & mit_remove) ? pre_modnames->cnt-1 - i : i;
1360 do_modprobe(pre_modnames->str[j], "",
1361 conf, dirname, warn, softdep_flags);
1364 /* Modprobe main module, passing cmdline_opts, ignoring softdep */
1366 do_modprobe(softdep->modname, cmdline_opts,
1367 conf, dirname, warn, flags | mit_ignore_commands);
1369 /* Modprobe post_modnames */
1371 for (i = 0; post_modnames && i < post_modnames->cnt; i++) {
1372 /* Reverse module order if removing. */
1373 j = (flags & mit_remove) ? post_modnames->cnt-1 - i : i;
1375 do_modprobe(post_modnames->str[j], "", conf,
1376 dirname, warn, softdep_flags);
1380 /* Actually do the insert. */
1381 static int insmod(struct list_head *list,
1382 const char *optstring,
1383 const char *cmdline_opts,
1384 const struct modprobe_conf *conf,
1385 const char *dirname,
1386 errfn_t error,
1387 modprobe_flags_t flags)
1389 int ret;
1390 struct elf_file *module;
1391 const struct module_softdep *softdep;
1392 const char *command;
1393 struct module *mod = list_entry(list->next, struct module, list);
1394 int rc = 0;
1395 int already_loaded;
1396 char *opts = NULL;
1398 /* Take us off the list. */
1399 list_del(&mod->list);
1401 /* Do things we (or parent) depend on first. */
1402 if (!list_empty(list)) {
1403 modprobe_flags_t f = flags;
1404 f &= ~mit_first_time;
1405 f &= ~mit_ignore_commands;
1406 if ((rc = insmod(list, "", "", conf, dirname, warn, f)) != 0)
1408 error("Error inserting %s (%s): %s\n",
1409 mod->modname, mod->filename,
1410 insert_moderror(errno));
1411 goto out;
1415 /* Don't do ANYTHING if already in kernel. */
1416 already_loaded = module_in_kernel(mod->modname, NULL);
1418 if (!(flags & mit_ignore_loaded) && already_loaded == 1) {
1419 if (flags & mit_first_time)
1420 error("Module %s already in kernel.\n", mod->modname);
1421 goto out;
1424 softdep = find_softdep(mod->modname, conf->softdeps);
1425 if (softdep && !(flags & mit_ignore_commands)) {
1426 do_softdep(softdep, cmdline_opts, conf, dirname, error, flags);
1427 goto out;
1430 command = find_command(mod->modname, conf->commands);
1431 if (command && !(flags & mit_ignore_commands)) {
1432 if (already_loaded == -1) {
1433 warn("/sys/module/ not present or too old,"
1434 " and /proc/modules does not exist.\n");
1435 warn("Ignoring install commands for %s"
1436 " in case it is already loaded.\n",
1437 mod->modname);
1438 } else {
1439 do_command(mod->modname, command, flags & mit_dry_run,
1440 error, "install", cmdline_opts);
1441 goto out;
1445 module = grab_elf_file(mod->filename);
1446 if (!module) {
1447 error("Could not read '%s': %s\n", mod->filename,
1448 (errno == ENOEXEC) ? "Invalid module format" :
1449 strerror(errno));
1450 goto out;
1452 if (flags & mit_strip_modversion)
1453 module->ops->strip_section(module, "__versions");
1454 if (flags & mit_strip_vermagic)
1455 clear_magic(module);
1457 /* Config file might have given more options */
1458 opts = add_extra_options(mod->modname, optstring, conf->options);
1460 info("insmod %s %s\n", mod->filename, opts);
1462 if (flags & mit_dry_run)
1463 goto out_elf_file;
1465 ret = init_module(module->data, module->len, opts);
1466 if (ret != 0) {
1467 if (errno == EEXIST) {
1468 if (flags & mit_first_time)
1469 error("Module %s already in kernel.\n",
1470 mod->modname);
1471 goto out_elf_file;
1473 /* don't warn noisely if we're loading multiple aliases. */
1474 /* one of the aliases may try to use hardware we don't have. */
1475 if ((error != warn) || (verbose))
1476 error("Error inserting %s (%s): %s\n",
1477 mod->modname, mod->filename,
1478 insert_moderror(errno));
1479 rc = 1;
1481 out_elf_file:
1482 release_elf_file(module);
1483 free(opts);
1484 out:
1485 free_module(mod);
1486 return rc;
1489 /* Do recursive removal. */
1490 static void rmmod(struct list_head *list,
1491 const char *cmdline_opts,
1492 const struct modprobe_conf *conf,
1493 const char *dirname,
1494 errfn_t error,
1495 modprobe_flags_t flags)
1497 const struct module_softdep *softdep;
1498 const char *command;
1499 unsigned int usecount = 0;
1500 struct module *mod = list_entry(list->next, struct module, list);
1501 int exists;
1503 /* Take first one off the list. */
1504 list_del(&mod->list);
1506 /* Don't do ANYTHING if not loaded. */
1507 exists = module_in_kernel(mod->modname, &usecount);
1508 if (exists == 0)
1509 goto nonexistent_module;
1511 /* Even if renamed, find commands/softdeps to orig. name. */
1513 softdep = find_softdep(mod->modname, conf->softdeps);
1514 if (softdep && !(flags & mit_ignore_commands)) {
1515 do_softdep(softdep, cmdline_opts, conf, dirname, error, flags);
1516 goto remove_rest;
1519 command = find_command(mod->modname, conf->commands);
1520 if (command && !(flags & mit_ignore_commands)) {
1521 if (exists == -1) {
1522 warn("/sys/module/ not present or too old,"
1523 " and /proc/modules does not exist.\n");
1524 warn("Ignoring remove commands for %s"
1525 " in case it is not loaded.\n",
1526 mod->modname);
1527 } else {
1528 do_command(mod->modname, command, flags & mit_dry_run,
1529 error, "remove", cmdline_opts);
1530 goto remove_rest;
1534 if (usecount != 0) {
1535 if (!(flags & mit_quiet_inuse))
1536 error("Module %s is in use.\n", mod->modname);
1537 goto remove_rest;
1540 info("rmmod %s\n", mod->filename);
1542 if (flags & mit_dry_run)
1543 goto remove_rest;
1545 if (delete_module(mod->modname, O_EXCL) != 0) {
1546 if (errno == ENOENT)
1547 goto nonexistent_module;
1548 error("Error removing %s (%s): %s\n",
1549 mod->modname, mod->filename,
1550 remove_moderror(errno));
1553 remove_rest:
1554 /* Now do things we depend. */
1555 if (!list_empty(list)) {
1556 flags &= ~mit_first_time;
1557 flags &= ~mit_ignore_commands;
1558 flags |= mit_quiet_inuse;
1560 rmmod(list, "", conf, dirname, warn, flags);
1562 free_module(mod);
1563 return;
1565 nonexistent_module:
1566 if (flags & mit_first_time)
1567 fatal("Module %s is not in kernel.\n", mod->modname);
1568 goto remove_rest;
1571 static int handle_module(const char *modname,
1572 struct list_head *todo_list,
1573 const char *options,
1574 const char *cmdline_opts,
1575 const struct modprobe_conf *conf,
1576 const char *dirname,
1577 errfn_t error,
1578 modprobe_flags_t flags)
1580 if (list_empty(todo_list)) {
1581 const char *command;
1583 /* The dependencies have to be real modules, but
1584 handle case where the first is completely bogus. */
1586 command = find_command(modname, conf->commands);
1587 if (command && !(flags & mit_ignore_commands)) {
1588 do_command(modname, command, flags & mit_dry_run, error,
1589 (flags & mit_remove) ? "remove":"install", cmdline_opts);
1590 return 0;
1593 if (!quiet)
1594 error("Module %s not found.\n", modname);
1595 return 1;
1598 if (flags & mit_remove)
1599 rmmod(todo_list, cmdline_opts,
1600 conf, dirname, error, flags);
1601 else
1602 insmod(todo_list, options,
1603 cmdline_opts, conf, dirname, error, flags);
1605 return 0;
1608 static int handle_builtin_module(const char *modname,
1609 errfn_t error,
1610 modprobe_flags_t flags)
1612 if (flags & mit_remove) {
1613 error("Module %s is builtin\n", modname);
1614 return 1;
1615 } else if (flags & mit_first_time) {
1616 error("Module %s already in kernel (builtin).\n", modname);
1617 return 1;
1618 } else if (flags & mit_ignore_loaded) {
1619 /* --show-depends given */
1620 info("builtin %s\n", modname);
1622 return 0;
1625 static int do_modprobe(const char *modname,
1626 const char *cmdline_opts,
1627 const struct modprobe_conf *conf,
1628 const char *dirname,
1629 errfn_t error,
1630 modprobe_flags_t flags)
1632 struct module_alias *matching_aliases;
1633 LIST_HEAD(list);
1634 int failed = 0;
1636 matching_aliases = find_aliases(conf->aliases, modname);
1638 /* No luck? Try symbol names, if starts with symbol:. */
1639 if (!matching_aliases && strstarts(modname, "symbol:")) {
1640 char *symfilename;
1642 nofail_asprintf(&symfilename, "%s/modules.symbols", dirname);
1643 read_aliases(symfilename, modname, 0, &matching_aliases);
1644 free(symfilename);
1646 if (!matching_aliases) {
1647 if(!strchr(modname, ':'))
1648 read_depends(dirname, modname, &list);
1650 /* We only use canned aliases as last resort. */
1651 if (list_empty(&list)
1652 && !find_softdep(modname, conf->softdeps)
1653 && !find_command(modname, conf->commands))
1655 char *aliasfilename;
1657 nofail_asprintf(&aliasfilename, "%s/modules.alias",
1658 dirname);
1659 read_aliases(aliasfilename, modname, 0,
1660 &matching_aliases);
1661 free(aliasfilename);
1662 /* builtin module? */
1663 if (!matching_aliases && module_builtin(dirname, modname) > 0) {
1664 failed |= handle_builtin_module(modname, error,
1665 flags);
1666 goto out;
1671 apply_blacklist(&matching_aliases, conf->blacklist);
1672 if(flags & mit_resolve_alias) {
1673 struct module_alias *aliases = matching_aliases;
1675 for(; aliases; aliases=aliases->next)
1676 printf("%s\n", aliases->module);
1677 goto out;
1679 if (matching_aliases) {
1680 errfn_t err = error;
1681 struct module_alias *aliases = matching_aliases;
1683 /* More than one alias? Don't bail out on failure. */
1684 if (aliases->next)
1685 err = warn;
1686 while (aliases) {
1687 /* Add the options for this alias. */
1688 char *opts;
1689 opts = add_extra_options(modname,
1690 cmdline_opts, conf->options);
1692 read_depends(dirname, aliases->module, &list);
1693 failed |= handle_module(aliases->module,
1694 &list, opts, cmdline_opts,
1695 conf, dirname, err, flags);
1697 aliases = aliases->next;
1698 free(opts);
1699 INIT_LIST_HEAD(&list);
1701 } else {
1702 if (flags & mit_use_blacklist
1703 && find_blacklist(modname, conf->blacklist))
1704 goto out;
1706 failed |= handle_module(modname, &list, cmdline_opts,
1707 cmdline_opts, conf, dirname, error, flags);
1710 out:
1711 free_aliases(matching_aliases);
1712 return failed;
1715 static const struct option options[] = { { "version", 0, NULL, 'V' },
1716 { "verbose", 0, NULL, 'v' },
1717 { "quiet", 0, NULL, 'q' },
1718 { "syslog", 0, NULL, 's' },
1719 { "show", 0, NULL, 'n' },
1720 { "dry-run", 0, NULL, 'n' },
1721 { "show-depends", 0, NULL, 'D' },
1722 { "resolve-alias", 0, NULL, 'R' },
1723 { "dirname", 1, NULL, 'd' },
1724 { "set-version", 1, NULL, 'S' },
1725 { "config", 1, NULL, 'C' },
1726 { "remove", 0, NULL, 'r' },
1727 { "showconfig", 0, NULL, 'c' },
1728 { "list", 0, NULL, 'l' },
1729 { "type", 1, NULL, 't' },
1730 { "all", 0, NULL, 'a' },
1731 { "ignore-install", 0, NULL, 'i' },
1732 { "ignore-remove", 0, NULL, 'i' },
1733 { "use-blacklist", 0, NULL, 'b' },
1734 { "force", 0, NULL, 'f' },
1735 { "force-vermagic", 0, NULL, 1 },
1736 { "force-modversion", 0, NULL, 2 },
1737 { "first-time", 0, NULL, 3 },
1738 { "dump-modversions", 0, NULL, 4 },
1739 { NULL, 0, NULL, 0 } };
1741 int main(int argc, char *argv[])
1743 struct utsname buf;
1744 struct stat statbuf;
1745 int opt;
1746 int dump_config = 0;
1747 int list_only = 0;
1748 int all = 0;
1749 int dump_modver = 0;
1750 unsigned int i, num_modules;
1751 char *type = NULL;
1752 const char *configname = NULL;
1753 char *basedir = "";
1754 char *cmdline_opts = NULL;
1755 char *dirname;
1756 errfn_t error = fatal;
1757 int failed = 0;
1758 modprobe_flags_t flags = 0;
1759 struct modprobe_conf conf = {};
1761 recursion_depth = 0;
1763 /* Prepend options from environment. */
1764 argv = merge_args(getenv("MODPROBE_OPTIONS"), argv, &argc);
1766 uname(&buf);
1767 while ((opt = getopt_long(argc, argv, "Vvqsnd:S:C:DRrclt:aibf", options, NULL)) != -1){
1768 switch (opt) {
1769 case 'V':
1770 puts(PACKAGE " version " VERSION);
1771 exit(0);
1772 case 'v':
1773 add_to_env_var("-v");
1774 verbose = 1;
1775 break;
1776 case 'q':
1777 quiet = 1;
1778 add_to_env_var("-q");
1779 break;
1780 case 's':
1781 add_to_env_var("-s");
1782 logging = 1;
1783 break;
1784 case 'n':
1785 flags |= mit_dry_run;
1786 break;
1787 case 'd':
1788 basedir = optarg;
1789 break;
1790 case 'S':
1791 strncpy(buf.release, optarg, sizeof(buf.release));
1792 buf.release[sizeof(buf.release)-1] = '\0';
1793 break;
1794 case 'C':
1795 configname = optarg;
1796 add_to_env_var("-C");
1797 add_to_env_var(configname);
1798 break;
1799 case 'D':
1800 flags |= mit_dry_run;
1801 flags |= mit_ignore_loaded;
1802 verbose = 1;
1803 break;
1804 case 'R':
1805 flags |= mit_resolve_alias;
1806 break;
1807 case 'r':
1808 flags |= mit_remove;
1809 break;
1810 case 'c':
1811 dump_config = 1;
1812 break;
1813 case 'l':
1814 list_only = 1;
1815 break;
1816 case 't':
1817 type = optarg;
1818 break;
1819 case 'a':
1820 all = 1;
1821 error = warn;
1822 break;
1823 case 'i':
1824 flags |= mit_ignore_commands;
1825 break;
1826 case 'b':
1827 flags |= mit_use_blacklist;
1828 break;
1829 case 'f':
1830 flags |= mit_strip_vermagic;
1831 flags |= mit_strip_modversion;
1832 break;
1833 case 1:
1834 flags |= mit_strip_vermagic;
1835 break;
1836 case 2:
1837 flags |= mit_strip_modversion;
1838 break;
1839 case 3:
1840 flags |= mit_first_time;
1841 break;
1842 case 4:
1843 dump_modver = 1;
1844 break;
1845 default:
1846 print_usage(argv[0]);
1850 /* If stderr not open, go to syslog */
1851 if (logging || fstat(STDERR_FILENO, &statbuf) != 0) {
1852 openlog("modprobe", LOG_CONS, LOG_DAEMON);
1853 logging = 1;
1856 if (argc < optind + 1 && !dump_config && !list_only)
1857 print_usage(argv[0]);
1859 nofail_asprintf(&dirname, "%s%s/%s", basedir, MODULE_DIR, buf.release);
1861 /* Old-style -t xxx wildcard? Only with -l. */
1862 if (list_only) {
1863 if (optind+1 < argc)
1864 fatal("Can't have multiple wildcards\n");
1865 /* fprintf(stderr, "man find\n"); return 1; */
1866 failed = do_wildcard(dirname, type, argv[optind]?:"*");
1867 goto out;
1869 if (type)
1870 fatal("-t only supported with -l");
1872 if (dump_modver) {
1873 dump_modversions(argv[optind], error);
1874 goto out;
1877 /* Read aliases, options etc. */
1878 parse_toplevel_config(configname, &conf, dump_config, flags & mit_remove);
1880 /* Read module options from kernel command line */
1881 parse_kcmdline(dump_config, &conf);
1883 if (dump_config) {
1884 char *aliasfilename, *symfilename;
1885 struct modprobe_conf conf = {};
1887 nofail_asprintf(&aliasfilename, "%s/modules.alias", dirname);
1888 nofail_asprintf(&symfilename, "%s/modules.symbols", dirname);
1890 read_aliases(aliasfilename, "", 1, &conf.aliases);
1891 read_aliases(symfilename, "", 1, &conf.aliases);
1893 goto out;
1896 if ((flags & mit_remove) || all) {
1897 num_modules = argc - optind;
1898 cmdline_opts = NOFAIL(strdup(""));
1899 } else {
1900 num_modules = 1;
1901 cmdline_opts = gather_options(argv+optind+1);
1904 /* Convert names we are looking for */
1905 for (i = 0; i < num_modules; i++)
1906 underscores(argv[optind + i]);
1908 /* If we have a list of modules to remove, try the unused ones first.
1909 Aliases and modules which don't seem to exist are handled later. */
1910 if (flags & mit_remove) {
1911 int progress;
1912 do {
1913 progress = 0;
1915 for (i = 0; i < num_modules; i++) {
1916 const char *modname;
1917 unsigned usecount;
1918 LIST_HEAD(list);
1920 modname = argv[optind + i];
1921 if (!modname)
1922 continue;
1923 if (module_in_kernel(modname, &usecount) != 1)
1924 continue;
1925 if (usecount != 0)
1926 continue;
1928 read_depends(dirname, modname, &list);
1930 failed |= handle_module(modname, &list,
1931 cmdline_opts, cmdline_opts,
1932 &conf, dirname, error, flags);
1933 progress++;
1934 argv[optind + i] = NULL;
1935 INIT_LIST_HEAD(&list);
1937 } while (progress > 0);
1940 /* num_modules is always 1 except for -r or -a. */
1941 for (i = 0; i < num_modules; i++) {
1942 const char *modname = argv[optind + i];
1944 if (!modname)
1945 continue;
1947 failed |= do_modprobe(modname, cmdline_opts,
1948 &conf, dirname, error, flags);
1951 out:
1952 if (logging)
1953 closelog();
1954 free(dirname);
1955 free(cmdline_opts);
1956 /* Don't bother to free conf */
1958 exit(failed);