depmod: export static device node information to modules.devname
[mit.git] / modprobe.c
blob26a71630ca8f15448a855c10a3505eb88c8424ea
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 = strsep_skipspace(&ptr, "\t ");
905 if (!modname || !ptr)
906 goto syntax_error;
907 modname = underscores(modname);
909 while ((tk = strsep_skipspace(&ptr, "\t ")) != NULL) {
910 tk = underscores(tk);
912 if (streq(tk, "pre:")) {
913 pre = 1; post = 0;
914 } else if (streq(tk, "post:")) {
915 pre = 0; post = 1;
916 } else if (pre) {
917 pre_modnames = NOFAIL(
918 strtbl_add(tk, pre_modnames));
919 } else if (post) {
920 post_modnames = NOFAIL(
921 strtbl_add(tk, post_modnames));
922 } else {
923 strtbl_free(pre_modnames);
924 strtbl_free(post_modnames);
925 goto syntax_error;
928 new = NOFAIL(malloc(sizeof(*new)));
929 new->buf = line;
930 new->modname = modname;
931 new->pre = pre_modnames;
932 new->post = post_modnames;
933 new->next = conf->softdeps;
934 conf->softdeps = new;
936 line = NULL; /* Don't free() this line. */
938 } else if (streq(cmd, "config")) {
939 char *tmp = strsep_skipspace(&ptr, "\t ");
941 if (!tmp)
942 goto syntax_error;
943 if (streq(tmp, "binary_indexes")) {
944 tmp = strsep_skipspace(&ptr, "\t ");
945 if (streq(tmp, "yes"))
946 use_binary_indexes = 1;
947 if (streq(tmp, "no"))
948 use_binary_indexes = 0;
950 } else {
951 syntax_error:
952 grammar(cmd, filename, linenum);
955 free(line);
957 fclose(cfile);
958 return 1;
961 /* Read binary index file containing aliases only */
962 static int read_aliases_file(const char *filename,
963 const char *name,
964 int dump_only,
965 struct module_alias **aliases)
967 struct index_value *realnames;
968 struct index_value *realname;
969 char *binfile;
970 struct index_file *index;
972 nofail_asprintf(&binfile, "%s.bin", filename);
973 index = index_file_open(binfile);
974 if (!index) {
975 free(binfile);
976 return 0;
979 if (dump_only) {
980 index_dump(index, stdout, "alias ");
981 free(binfile);
982 index_file_close(index);
983 return 1;
986 realnames = index_searchwild(index, name);
987 for (realname = realnames; realname; realname = realname->next)
988 *aliases = add_alias("*", realname->value, *aliases);
989 index_values_free(realnames);
991 free(binfile);
992 index_file_close(index);
993 return 1;
996 /* fallback to plain-text aliases file if necessary */
997 static int read_aliases(const char *filename,
998 const char *name,
999 int dump_only,
1000 struct module_alias **aliases)
1002 char *line;
1003 unsigned int linenum = 0;
1004 FILE *cfile;
1006 if (use_binary_indexes)
1007 if (read_aliases_file(filename, name, dump_only, aliases))
1008 return 1;
1010 cfile = fopen(filename, "r");
1011 if (!cfile)
1012 return 0;
1014 while ((line = getline_wrapped(cfile, &linenum)) != NULL) {
1015 char *ptr = line;
1016 char *cmd;
1018 if (dump_only)
1019 printf("%s\n", line);
1021 cmd = strsep_skipspace(&ptr, "\t ");
1022 if (cmd == NULL || cmd[0] == '#' || cmd[0] == '\0') {
1023 free(line);
1024 continue;
1027 if (streq(cmd, "alias")) {
1028 char *wildcard = strsep_skipspace(&ptr, "\t ");
1029 char *realname = strsep_skipspace(&ptr, "\t ");
1030 if (!wildcard || !realname)
1031 goto syntax_error;
1032 if (fnmatch(underscores(wildcard),name,0) == 0)
1033 *aliases = add_alias(wildcard,
1034 underscores(realname),
1035 *aliases);
1036 } else {
1037 syntax_error:
1038 grammar(cmd, filename, linenum);
1041 free(line);
1043 fclose(cfile);
1044 return 1;
1047 static int parse_config_scan(const char *filename,
1048 struct modprobe_conf *conf,
1049 int dump_only,
1050 int removing)
1052 DIR *dir;
1053 int ret = 0;
1055 dir = opendir(filename);
1056 if (dir) {
1057 struct file_entry {
1058 struct list_head node;
1059 char name[];
1061 LIST_HEAD(files_list);
1062 struct file_entry *fe, *fe_tmp;
1063 struct dirent *i;
1065 /* sort files from directory into list */
1066 while ((i = readdir(dir)) != NULL) {
1067 size_t len;
1069 if (i->d_name[0] == '.')
1070 continue;
1071 if (!config_filter(i->d_name))
1072 continue;
1074 len = strlen(i->d_name);
1075 if (len < 6 ||
1076 (strcmp(&i->d_name[len-5], ".conf") != 0 &&
1077 strcmp(&i->d_name[len-6], ".alias") != 0))
1078 warn("All config files need .conf: %s/%s, "
1079 "it will be ignored in a future release.\n",
1080 filename, i->d_name);
1081 fe = malloc(sizeof(struct file_entry) + len + 1);
1082 if (fe == NULL)
1083 continue;
1084 strcpy(fe->name, i->d_name);
1085 list_for_each_entry(fe_tmp, &files_list, node)
1086 if (strcmp(fe_tmp->name, fe->name) >= 0)
1087 break;
1088 list_add_tail(&fe->node, &fe_tmp->node);
1090 closedir(dir);
1092 /* parse list of files */
1093 list_for_each_entry_safe(fe, fe_tmp, &files_list, node) {
1094 char *cfgfile;
1096 nofail_asprintf(&cfgfile, "%s/%s", filename, fe->name);
1097 if (!parse_config_file(cfgfile, conf,
1098 dump_only, removing))
1099 warn("Failed to open config file "
1100 "%s: %s\n", fe->name, strerror(errno));
1101 free(cfgfile);
1102 list_del(&fe->node);
1103 free(fe);
1106 ret = 1;
1107 } else {
1108 if (parse_config_file(filename, conf, dump_only, removing))
1109 ret = 1;
1111 return ret;
1114 static void parse_toplevel_config(const char *filename,
1115 struct modprobe_conf *conf,
1116 int dump_only,
1117 int removing)
1119 if (filename) {
1120 if (!parse_config_scan(filename, conf, dump_only, removing))
1121 fatal("Failed to open config file %s: %s\n",
1122 filename, strerror(errno));
1123 return;
1126 /* deprecated config file */
1127 if (parse_config_file("/etc/modprobe.conf", conf,
1128 dump_only, removing) > 0)
1129 warn("Deprecated config file /etc/modprobe.conf, "
1130 "all config files belong into /etc/modprobe.d/.\n");
1132 /* default config */
1133 parse_config_scan("/etc/modprobe.d", conf, dump_only, removing);
1136 /* Read possible module arguments from the kernel command line. */
1137 static int parse_kcmdline(int dump_only, struct module_options **options)
1139 char *line;
1140 unsigned int linenum = 0;
1141 FILE *kcmdline;
1143 kcmdline = fopen("/proc/cmdline", "r");
1144 if (!kcmdline)
1145 return 0;
1147 while ((line = getline_wrapped(kcmdline, &linenum)) != NULL) {
1148 char *ptr = line;
1149 char *arg;
1151 while ((arg = strsep_skipspace(&ptr, "\t ")) != NULL) {
1152 char *sep, *modname, *opt;
1154 sep = strchr(arg, '.');
1155 if (sep) {
1156 if (!strchr(sep, '='))
1157 continue;
1158 modname = arg;
1159 *sep = '\0';
1160 opt = ++sep;
1162 if (dump_only)
1163 printf("options %s %s\n", modname, opt);
1165 *options = add_options(underscores(modname),
1166 opt, *options);
1170 free(line);
1172 fclose(kcmdline);
1173 return 1;
1176 static void add_to_env_var(const char *option)
1178 const char *oldenv;
1180 if ((oldenv = getenv("MODPROBE_OPTIONS")) != NULL) {
1181 char *newenv;
1182 nofail_asprintf(&newenv, "%s %s", oldenv, option);
1183 setenv("MODPROBE_OPTIONS", newenv, 1);
1184 free(newenv);
1185 } else
1186 setenv("MODPROBE_OPTIONS", option, 1);
1189 /* Prepend options from environment. */
1190 static char **merge_args(char *args, char *argv[], int *argc)
1192 char *arg, *argstring;
1193 char **newargs = NULL;
1194 unsigned int i, num_env = 0;
1196 if (!args)
1197 return argv;
1199 argstring = NOFAIL(strdup(args));
1200 for (arg = strtok(argstring, " "); arg; arg = strtok(NULL, " ")) {
1201 num_env++;
1202 newargs = NOFAIL(realloc(newargs,
1203 sizeof(newargs[0])
1204 * (num_env + *argc + 1)));
1205 newargs[num_env] = arg;
1208 if (!newargs)
1209 return argv;
1211 /* Append commandline args */
1212 newargs[0] = argv[0];
1213 for (i = 1; i <= *argc; i++)
1214 newargs[num_env+i] = argv[i];
1216 *argc += num_env;
1217 return newargs;
1220 static char *gather_options(char *argv[])
1222 char *optstring = NOFAIL(strdup(""));
1224 /* Rest is module options */
1225 while (*argv) {
1226 /* Quote value if it contains spaces. */
1227 unsigned int eq = strcspn(*argv, "=");
1229 if (strchr(*argv+eq, ' ') && !strchr(*argv, '"')) {
1230 char quoted[strlen(*argv) + 3];
1231 (*argv)[eq] = '\0';
1232 sprintf(quoted, "%s=\"%s\"", *argv, *argv+eq+1);
1233 optstring = append_option(optstring, quoted);
1234 } else
1235 optstring = append_option(optstring, *argv);
1236 argv++;
1238 return optstring;
1241 /* Do an install/remove command: replace $CMDLINE_OPTS if it's specified. */
1242 static void do_command(const char *modname,
1243 const char *command,
1244 int dry_run,
1245 errfn_t error,
1246 const char *type,
1247 const char *cmdline_opts)
1249 int ret;
1250 char *p, *replaced_cmd = NOFAIL(strdup(command));
1252 while ((p = strstr(replaced_cmd, "$CMDLINE_OPTS")) != NULL) {
1253 char *new;
1254 nofail_asprintf(&new, "%.*s%s%s",
1255 (int)(p - replaced_cmd), replaced_cmd, cmdline_opts,
1256 p + strlen("$CMDLINE_OPTS"));
1257 free(replaced_cmd);
1258 replaced_cmd = new;
1261 info("%s %s\n", type, replaced_cmd);
1262 if (dry_run)
1263 goto out;
1265 setenv("MODPROBE_MODULE", modname, 1);
1266 ret = system(replaced_cmd);
1267 if (ret == -1 || WEXITSTATUS(ret))
1268 error("Error running %s command for %s\n", type, modname);
1270 out:
1271 free(replaced_cmd);
1274 /* Forward declaration */
1275 int do_modprobe(const char *modname,
1276 const char *cmdline_opts,
1277 const struct modprobe_conf *conf,
1278 const char *dirname,
1279 errfn_t error,
1280 modprobe_flags_t flags);
1282 static void do_softdep(const struct module_softdep *softdep,
1283 const char *cmdline_opts,
1284 const struct modprobe_conf *conf,
1285 const char *dirname,
1286 errfn_t error,
1287 modprobe_flags_t flags)
1289 struct string_table *pre_modnames, *post_modnames;
1290 modprobe_flags_t softdep_flags = flags;
1291 int i, j;
1293 softdep_flags &= ~mit_first_time;
1294 softdep_flags &= ~mit_ignore_commands;
1295 if (flags & mit_remove)
1296 softdep_flags |= mit_quiet_inuse;
1298 if (++recursion_depth >= MAX_RECURSION)
1299 fatal("modprobe: softdep dependency loop encountered %s %s\n",
1300 (flags & mit_remove) ? "removing" : "inserting",
1301 softdep->modname);
1303 if (flags & mit_remove) {
1304 /* Reverse module order if removing. */
1305 pre_modnames = softdep->post;
1306 post_modnames = softdep->pre;
1307 } else {
1308 pre_modnames = softdep->pre;
1309 post_modnames = softdep->post;
1312 /* Modprobe pre_modnames */
1314 for (i = 0; pre_modnames && i < pre_modnames->cnt; i++) {
1315 /* Reverse module order if removing. */
1316 j = (flags & mit_remove) ? pre_modnames->cnt-1 - i : i;
1318 do_modprobe(pre_modnames->str[j], "",
1319 conf, dirname, warn, softdep_flags);
1322 /* Modprobe main module, passing cmdline_opts, ignoring softdep */
1324 do_modprobe(softdep->modname, cmdline_opts,
1325 conf, dirname, warn, flags | mit_ignore_commands);
1327 /* Modprobe post_modnames */
1329 for (i = 0; post_modnames && i < post_modnames->cnt; i++) {
1330 /* Reverse module order if removing. */
1331 j = (flags & mit_remove) ? post_modnames->cnt-1 - i : i;
1333 do_modprobe(post_modnames->str[j], "", conf,
1334 dirname, warn, softdep_flags);
1338 /* Actually do the insert. */
1339 static int insmod(struct list_head *list,
1340 const char *optstring,
1341 const char *cmdline_opts,
1342 const struct modprobe_conf *conf,
1343 const char *dirname,
1344 errfn_t error,
1345 modprobe_flags_t flags)
1347 int ret;
1348 struct elf_file *module;
1349 const struct module_softdep *softdep;
1350 const char *command;
1351 struct module *mod = list_entry(list->next, struct module, list);
1352 int rc = 0;
1353 int already_loaded;
1354 char *opts = NULL;
1356 /* Take us off the list. */
1357 list_del(&mod->list);
1359 /* Do things we (or parent) depend on first. */
1360 if (!list_empty(list)) {
1361 modprobe_flags_t f = flags;
1362 f &= ~mit_first_time;
1363 f &= ~mit_ignore_commands;
1364 if ((rc = insmod(list, "", "", conf, dirname, warn, f)) != 0)
1366 error("Error inserting %s (%s): %s\n",
1367 mod->modname, mod->filename,
1368 insert_moderror(errno));
1369 goto out;
1373 /* Don't do ANYTHING if already in kernel. */
1374 already_loaded = module_in_kernel(mod->modname, NULL);
1376 if (!(flags & mit_ignore_loaded) && already_loaded == 1) {
1377 if (flags & mit_first_time)
1378 error("Module %s already in kernel.\n", mod->modname);
1379 goto out;
1382 softdep = find_softdep(mod->modname, conf->softdeps);
1383 if (softdep && !(flags & mit_ignore_commands)) {
1384 do_softdep(softdep, cmdline_opts, conf, dirname, error, flags);
1385 goto out;
1388 command = find_command(mod->modname, conf->commands);
1389 if (command && !(flags & mit_ignore_commands)) {
1390 if (already_loaded == -1) {
1391 warn("/sys/module/ not present or too old,"
1392 " and /proc/modules does not exist.\n");
1393 warn("Ignoring install commands for %s"
1394 " in case it is already loaded.\n",
1395 mod->modname);
1396 } else {
1397 do_command(mod->modname, command, flags & mit_dry_run,
1398 error, "install", cmdline_opts);
1399 goto out;
1403 module = grab_elf_file(mod->filename);
1404 if (!module) {
1405 error("Could not read '%s': %s\n", mod->filename,
1406 (errno == ENOEXEC) ? "Invalid module format" :
1407 strerror(errno));
1408 goto out;
1410 if (flags & mit_strip_modversion)
1411 module->ops->strip_section(module, "__versions");
1412 if (flags & mit_strip_vermagic)
1413 clear_magic(module);
1415 /* Config file might have given more options */
1416 opts = add_extra_options(mod->modname, optstring, conf->options);
1418 info("insmod %s %s\n", mod->filename, opts);
1420 if (flags & mit_dry_run)
1421 goto out_elf_file;
1423 ret = init_module(module->data, module->len, opts);
1424 if (ret != 0) {
1425 if (errno == EEXIST) {
1426 if (flags & mit_first_time)
1427 error("Module %s already in kernel.\n",
1428 mod->modname);
1429 goto out_elf_file;
1431 /* don't warn noisely if we're loading multiple aliases. */
1432 /* one of the aliases may try to use hardware we don't have. */
1433 if ((error != warn) || (verbose))
1434 error("Error inserting %s (%s): %s\n",
1435 mod->modname, mod->filename,
1436 insert_moderror(errno));
1437 rc = 1;
1439 out_elf_file:
1440 release_elf_file(module);
1441 free(opts);
1442 out:
1443 free_module(mod);
1444 return rc;
1447 /* Do recursive removal. */
1448 static void rmmod(struct list_head *list,
1449 const char *cmdline_opts,
1450 const struct modprobe_conf *conf,
1451 const char *dirname,
1452 errfn_t error,
1453 modprobe_flags_t flags)
1455 const struct module_softdep *softdep;
1456 const char *command;
1457 unsigned int usecount = 0;
1458 struct module *mod = list_entry(list->next, struct module, list);
1459 int exists;
1461 /* Take first one off the list. */
1462 list_del(&mod->list);
1464 /* Don't do ANYTHING if not loaded. */
1465 exists = module_in_kernel(mod->modname, &usecount);
1466 if (exists == 0)
1467 goto nonexistent_module;
1469 /* Even if renamed, find commands/softdeps to orig. name. */
1471 softdep = find_softdep(mod->modname, conf->softdeps);
1472 if (softdep && !(flags & mit_ignore_commands)) {
1473 do_softdep(softdep, cmdline_opts, conf, dirname, error, flags);
1474 goto remove_rest;
1477 command = find_command(mod->modname, conf->commands);
1478 if (command && !(flags & mit_ignore_commands)) {
1479 if (exists == -1) {
1480 warn("/sys/module/ not present or too old,"
1481 " and /proc/modules does not exist.\n");
1482 warn("Ignoring remove commands for %s"
1483 " in case it is not loaded.\n",
1484 mod->modname);
1485 } else {
1486 do_command(mod->modname, command, flags & mit_dry_run,
1487 error, "remove", cmdline_opts);
1488 goto remove_rest;
1492 if (usecount != 0) {
1493 if (!(flags & mit_quiet_inuse))
1494 error("Module %s is in use.\n", mod->modname);
1495 goto remove_rest;
1498 info("rmmod %s\n", mod->filename);
1500 if (flags & mit_dry_run)
1501 goto remove_rest;
1503 if (delete_module(mod->modname, O_EXCL) != 0) {
1504 if (errno == ENOENT)
1505 goto nonexistent_module;
1506 error("Error removing %s (%s): %s\n",
1507 mod->modname, mod->filename,
1508 remove_moderror(errno));
1511 remove_rest:
1512 /* Now do things we depend. */
1513 if (!list_empty(list)) {
1514 flags &= ~mit_first_time;
1515 flags &= ~mit_ignore_commands;
1516 flags |= mit_quiet_inuse;
1518 rmmod(list, "", conf, dirname, warn, flags);
1520 free_module(mod);
1521 return;
1523 nonexistent_module:
1524 if (flags & mit_first_time)
1525 fatal("Module %s is not in kernel.\n", mod->modname);
1526 goto remove_rest;
1529 static int handle_module(const char *modname,
1530 struct list_head *todo_list,
1531 const char *options,
1532 const char *cmdline_opts,
1533 const struct modprobe_conf *conf,
1534 const char *dirname,
1535 errfn_t error,
1536 modprobe_flags_t flags)
1538 if (list_empty(todo_list)) {
1539 const char *command;
1541 /* The dependencies have to be real modules, but
1542 handle case where the first is completely bogus. */
1544 command = find_command(modname, conf->commands);
1545 if (command && !(flags & mit_ignore_commands)) {
1546 do_command(modname, command, flags & mit_dry_run, error,
1547 (flags & mit_remove) ? "remove":"install", cmdline_opts);
1548 return 0;
1551 if (!quiet)
1552 error("Module %s not found.\n", modname);
1553 return 1;
1556 if (flags & mit_remove)
1557 rmmod(todo_list, cmdline_opts,
1558 conf, dirname, error, flags);
1559 else
1560 insmod(todo_list, options,
1561 cmdline_opts, conf, dirname, error, flags);
1563 return 0;
1566 int handle_builtin_module(const char *modname,
1567 errfn_t error,
1568 modprobe_flags_t flags)
1570 if (flags & mit_remove) {
1571 error("Module %s is builtin\n", modname);
1572 return 1;
1573 } else if (flags & mit_first_time) {
1574 error("Module %s already in kernel (builtin).\n", modname);
1575 return 1;
1576 } else if (flags & mit_ignore_loaded) {
1577 /* --show-depends given */
1578 info("builtin %s\n", modname);
1580 return 0;
1583 int do_modprobe(const char *modname,
1584 const char *cmdline_opts,
1585 const struct modprobe_conf *conf,
1586 const char *dirname,
1587 errfn_t error,
1588 modprobe_flags_t flags)
1590 struct module_alias *matching_aliases;
1591 LIST_HEAD(list);
1592 int failed = 0;
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_aliases(matching_aliases);
1670 return failed;
1673 static struct option options[] = { { "version", 0, NULL, 'V' },
1674 { "verbose", 0, NULL, 'v' },
1675 { "quiet", 0, NULL, 'q' },
1676 { "syslog", 0, NULL, 's' },
1677 { "show", 0, NULL, 'n' },
1678 { "dry-run", 0, NULL, 'n' },
1679 { "show-depends", 0, NULL, 'D' },
1680 { "resolve-alias", 0, NULL, 'R' },
1681 { "dirname", 1, NULL, 'd' },
1682 { "set-version", 1, NULL, 'S' },
1683 { "config", 1, NULL, 'C' },
1684 { "remove", 0, NULL, 'r' },
1685 { "showconfig", 0, NULL, 'c' },
1686 { "list", 0, NULL, 'l' },
1687 { "type", 1, NULL, 't' },
1688 { "all", 0, NULL, 'a' },
1689 { "ignore-install", 0, NULL, 'i' },
1690 { "ignore-remove", 0, NULL, 'i' },
1691 { "use-blacklist", 0, NULL, 'b' },
1692 { "force", 0, NULL, 'f' },
1693 { "force-vermagic", 0, NULL, 1 },
1694 { "force-modversion", 0, NULL, 2 },
1695 { "first-time", 0, NULL, 3 },
1696 { "dump-modversions", 0, NULL, 4 },
1697 { NULL, 0, NULL, 0 } };
1699 int main(int argc, char *argv[])
1701 struct utsname buf;
1702 struct stat statbuf;
1703 int opt;
1704 int dump_config = 0;
1705 int list_only = 0;
1706 int all = 0;
1707 int dump_modver = 0;
1708 unsigned int i, num_modules;
1709 char *type = NULL;
1710 const char *configname = NULL;
1711 char *basedir = "";
1712 char *cmdline_opts = NULL;
1713 char *dirname;
1714 errfn_t error = fatal;
1715 int failed = 0;
1716 modprobe_flags_t flags = 0;
1717 struct modprobe_conf conf = {};
1719 recursion_depth = 0;
1721 /* Prepend options from environment. */
1722 argv = merge_args(getenv("MODPROBE_OPTIONS"), argv, &argc);
1724 uname(&buf);
1725 while ((opt = getopt_long(argc, argv, "Vvqsnd:S:C:DRrclt:aibf", options, NULL)) != -1){
1726 switch (opt) {
1727 case 'V':
1728 puts(PACKAGE " version " VERSION);
1729 exit(0);
1730 case 'v':
1731 add_to_env_var("-v");
1732 verbose = 1;
1733 break;
1734 case 'q':
1735 quiet = 1;
1736 add_to_env_var("-q");
1737 break;
1738 case 's':
1739 add_to_env_var("-s");
1740 logging = 1;
1741 break;
1742 case 'n':
1743 flags |= mit_dry_run;
1744 break;
1745 case 'd':
1746 basedir = optarg;
1747 break;
1748 case 'S':
1749 strncpy(buf.release, optarg, sizeof(buf.release));
1750 buf.release[sizeof(buf.release)-1] = '\0';
1751 break;
1752 case 'C':
1753 configname = optarg;
1754 add_to_env_var("-C");
1755 add_to_env_var(configname);
1756 break;
1757 case 'D':
1758 flags |= mit_dry_run;
1759 flags |= mit_ignore_loaded;
1760 verbose = 1;
1761 break;
1762 case 'R':
1763 flags |= mit_resolve_alias;
1764 break;
1765 case 'r':
1766 flags |= mit_remove;
1767 break;
1768 case 'c':
1769 dump_config = 1;
1770 break;
1771 case 'l':
1772 list_only = 1;
1773 break;
1774 case 't':
1775 type = optarg;
1776 break;
1777 case 'a':
1778 all = 1;
1779 error = warn;
1780 break;
1781 case 'i':
1782 flags |= mit_ignore_commands;
1783 break;
1784 case 'b':
1785 flags |= mit_use_blacklist;
1786 break;
1787 case 'f':
1788 flags |= mit_strip_vermagic;
1789 flags |= mit_strip_modversion;
1790 break;
1791 case 1:
1792 flags |= mit_strip_vermagic;
1793 break;
1794 case 2:
1795 flags |= mit_strip_modversion;
1796 break;
1797 case 3:
1798 flags |= mit_first_time;
1799 break;
1800 case 4:
1801 dump_modver = 1;
1802 break;
1803 default:
1804 print_usage(argv[0]);
1808 /* If stderr not open, go to syslog */
1809 if (logging || fstat(STDERR_FILENO, &statbuf) != 0) {
1810 openlog("modprobe", LOG_CONS, LOG_DAEMON);
1811 logging = 1;
1814 if (argc < optind + 1 && !dump_config && !list_only)
1815 print_usage(argv[0]);
1817 nofail_asprintf(&dirname, "%s%s/%s", basedir, MODULE_DIR, buf.release);
1819 /* Old-style -t xxx wildcard? Only with -l. */
1820 if (list_only) {
1821 if (optind+1 < argc)
1822 fatal("Can't have multiple wildcards\n");
1823 /* fprintf(stderr, "man find\n"); return 1; */
1824 failed = do_wildcard(dirname, type, argv[optind]?:"*");
1825 goto out;
1827 if (type)
1828 fatal("-t only supported with -l");
1830 if (dump_modver) {
1831 dump_modversions(argv[optind], error);
1832 goto out;
1835 /* Read aliases, options etc. */
1836 parse_toplevel_config(configname, &conf, dump_config, flags & mit_remove);
1838 /* Read module options from kernel command line */
1839 parse_kcmdline(dump_config, &conf.options);
1841 if (dump_config) {
1842 char *aliasfilename, *symfilename;
1843 struct modprobe_conf conf = {};
1845 nofail_asprintf(&aliasfilename, "%s/modules.alias", dirname);
1846 nofail_asprintf(&symfilename, "%s/modules.symbols", dirname);
1848 read_aliases(aliasfilename, "", 1, &conf.aliases);
1849 read_aliases(symfilename, "", 1, &conf.aliases);
1851 goto out;
1854 if ((flags & mit_remove) || all) {
1855 num_modules = argc - optind;
1856 cmdline_opts = NOFAIL(strdup(""));
1857 } else {
1858 num_modules = 1;
1859 cmdline_opts = gather_options(argv+optind+1);
1862 /* Convert names we are looking for */
1863 for (i = 0; i < num_modules; i++)
1864 underscores(argv[optind + i]);
1866 /* If we have a list of modules to remove, try the unused ones first.
1867 Aliases and modules which don't seem to exist are handled later. */
1868 if (flags & mit_remove) {
1869 int progress;
1870 do {
1871 progress = 0;
1873 for (i = 0; i < num_modules; i++) {
1874 const char *modname;
1875 unsigned usecount;
1876 LIST_HEAD(list);
1878 modname = argv[optind + i];
1879 if (!modname)
1880 continue;
1881 if (module_in_kernel(modname, &usecount) != 1)
1882 continue;
1883 if (usecount != 0)
1884 continue;
1886 read_depends(dirname, modname, &list);
1888 failed |= handle_module(modname, &list,
1889 cmdline_opts, cmdline_opts,
1890 &conf, dirname, error, flags);
1891 progress++;
1892 argv[optind + i] = NULL;
1893 INIT_LIST_HEAD(&list);
1895 } while (progress > 0);
1898 /* num_modules is always 1 except for -r or -a. */
1899 for (i = 0; i < num_modules; i++) {
1900 const char *modname = argv[optind + i];
1902 if (!modname)
1903 continue;
1905 failed |= do_modprobe(modname, cmdline_opts,
1906 &conf, dirname, error, flags);
1909 out:
1910 if (logging)
1911 closelog();
1912 free(dirname);
1913 free(cmdline_opts);
1914 /* Don't bother to free conf */
1916 exit(failed);