modprobe: fix potential memory leak on failure path
[mit.git] / modprobe.c
blob9f92ae2f6b42875a72c9330f8669397d44071fd5
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 extern long init_module(void *, unsigned long, const char *);
55 extern long delete_module(const char *, unsigned int);
57 struct module {
58 struct list_head list;
59 char *modname;
60 char filename[0];
63 typedef enum
65 mit_remove = 1,
66 mit_dry_run = 2,
67 mit_first_time = 4,
68 mit_use_blacklist = 8,
69 mit_ignore_commands = 16,
70 mit_ignore_loaded = 32,
71 mit_quiet_inuse = 64,
72 mit_strip_vermagic = 128,
73 mit_strip_modversion = 256,
74 mit_resolve_alias = 512
76 } modprobe_flags_t;
78 #ifndef MODULE_DIR
79 #define MODULE_DIR "/lib/modules"
80 #endif
82 static void print_usage(const char *progname)
84 fprintf(stderr,
85 "Usage: %s [-v] [-V] [-C config-file] [-d <dirname> ] [-n] [-i] [-q] [-b] [-o <modname>] [ --dump-modversions ] <modname> [parameters...]\n"
86 "%s -r [-n] [-i] [-v] <modulename> ...\n"
87 "%s -l -t <dirname> [ -a <modulename> ...]\n",
88 progname, progname, progname);
89 exit(1);
92 static struct module *find_module(const char *filename, struct list_head *list)
94 struct module *i;
96 list_for_each_entry(i, list, list) {
97 if (streq(i->filename, filename))
98 return i;
100 return NULL;
103 /* We used to lock with a write flock but that allows regular users to block
104 * module load by having a read lock on the module file (no way to bust the
105 * existing locks without killing the offending process). Instead, we now
106 * do the system call/init_module and allow the kernel to fail us instead.
108 static int open_file(const char *filename)
110 int fd = open(filename, O_RDONLY, 0);
112 return fd;
115 static void close_file(int fd)
117 /* Valgrind is picky... */
118 close(fd);
121 static void add_module(char *filename, int namelen, struct list_head *list)
123 struct module *mod;
125 /* If it's a duplicate: move it to the end, so it gets
126 inserted where it is *first* required. */
127 mod = find_module(filename, list);
128 if (mod)
129 list_del(&mod->list);
130 else {
131 /* No match. Create a new module. */
132 mod = NOFAIL(malloc(sizeof(struct module) + namelen + 1));
133 memcpy(mod->filename, filename, namelen);
134 mod->filename[namelen] = '\0';
135 mod->modname = NOFAIL(malloc(namelen + 1));
136 filename2modname(mod->modname, mod->filename);
139 list_add_tail(&mod->list, list);
142 /* Compare len chars of a to b, with _ and - equivalent. */
143 static int modname_equal(const char *a, const char *b, unsigned int len)
145 unsigned int i;
147 if (strlen(b) != len)
148 return 0;
150 for (i = 0; i < len; i++) {
151 if ((a[i] == '_' || a[i] == '-')
152 && (b[i] == '_' || b[i] == '-'))
153 continue;
154 if (a[i] != b[i])
155 return 0;
157 return 1;
160 /* Fills in list of modules if this is the line we want. */
161 static int add_modules_dep_line(char *line,
162 const char *name,
163 struct list_head *list,
164 const char *dirname)
166 char *ptr;
167 int len;
168 char *modname, *fullpath;
170 /* Ignore lines without : or which start with a # */
171 ptr = strchr(line, ':');
172 if (ptr == NULL || line[strspn(line, "\t ")] == '#')
173 return 0;
175 /* Is this the module we are looking for? */
176 *ptr = '\0';
177 modname = my_basename(line);
179 len = strlen(modname);
180 if (strchr(modname, '.'))
181 len = strchr(modname, '.') - modname;
182 if (!modname_equal(modname, name, len))
183 return 0;
185 /* Create the list. */
186 if ('/' == line[0]) { /* old style deps - absolute path specified */
187 add_module(line, ptr - line, list);
188 } else {
189 nofail_asprintf(&fullpath, "%s/%s", dirname, line);
190 add_module(fullpath, strlen(dirname)+1+(ptr - line), list);
191 free(fullpath);
194 ptr++;
195 for(;;) {
196 char *dep_start;
197 ptr += strspn(ptr, " \t");
198 if (*ptr == '\0')
199 break;
200 dep_start = ptr;
201 ptr += strcspn(ptr, " \t");
202 if ('/' == dep_start[0]) { /* old style deps */
203 add_module(dep_start, ptr - dep_start, list);
204 } else {
205 nofail_asprintf(&fullpath, "%s/%s", dirname, dep_start);
206 add_module(fullpath,
207 strlen(dirname)+1+(ptr - dep_start), list);
208 free(fullpath);
211 return 1;
214 static int read_depends_file(const char *dirname,
215 const char *start_name,
216 struct list_head *list)
218 char *modules_dep_name;
219 char *line;
220 struct index_file *modules_dep;
222 nofail_asprintf(&modules_dep_name, "%s/%s", dirname, "modules.dep.bin");
223 modules_dep = index_file_open(modules_dep_name);
224 if (!modules_dep) {
225 free(modules_dep_name);
226 return 0;
229 line = index_search(modules_dep, start_name);
230 if (line) {
231 /* Value is standard dependency line format */
232 if (!add_modules_dep_line(line, start_name, list, dirname))
233 fatal("Module index is inconsistent\n");
234 free(line);
237 index_file_close(modules_dep);
238 free(modules_dep_name);
240 return 1;
243 static void read_depends(const char *dirname,
244 const char *start_name,
245 struct list_head *list)
247 char *modules_dep_name;
248 char *line;
249 FILE *modules_dep;
250 int done = 0;
252 if (use_binary_indexes)
253 if (read_depends_file(dirname, start_name, list))
254 return;
256 nofail_asprintf(&modules_dep_name, "%s/%s", dirname, "modules.dep");
257 modules_dep = fopen(modules_dep_name, "r");
258 if (!modules_dep)
259 fatal("Could not load %s: %s\n",
260 modules_dep_name, strerror(errno));
262 /* Stop at first line, as we can have duplicates (eg. symlinks
263 from boot/ */
264 while (!done && (line = getline_wrapped(modules_dep, NULL)) != NULL) {
265 done = add_modules_dep_line(line, start_name, list, dirname);
266 free(line);
268 fclose(modules_dep);
269 free(modules_dep_name);
272 /* We use error numbers in a loose translation... */
273 static const char *insert_moderror(int err)
275 switch (err) {
276 case ENOEXEC:
277 return "Invalid module format";
278 case ENOENT:
279 return "Unknown symbol in module, or unknown parameter (see dmesg)";
280 case ENOSYS:
281 return "Kernel does not have module support";
282 default:
283 return strerror(err);
287 static const char *remove_moderror(int err)
289 switch (err) {
290 case ENOENT:
291 return "No such module";
292 case ENOSYS:
293 return "Kernel does not have module unloading support";
294 default:
295 return strerror(err);
299 static void replace_modname(struct elf_file *module,
300 void *mem, unsigned long len,
301 const char *oldname, const char *newname)
303 char *p;
305 /* 64 - sizeof(unsigned long) - 1 */
306 if (strlen(newname) > 55)
307 fatal("New name %s is too long\n", newname);
309 /* Find where it is in the module structure. Don't assume layout! */
310 for (p = mem; p < (char *)mem + len - strlen(oldname); p++) {
311 if (memcmp(p, oldname, strlen(oldname)) == 0) {
312 strcpy(p, newname);
313 return;
317 warn("Could not find old name in %s to replace!\n", module->pathname);
320 static void rename_module(struct elf_file *module,
321 const char *oldname,
322 const char *newname)
324 void *modstruct;
325 unsigned long len;
327 /* Old-style */
328 modstruct = module->ops->load_section(module,
329 ".gnu.linkonce.this_module", &len);
330 /* New-style */
331 if (!modstruct)
332 modstruct = module->ops->load_section(module, "__module", &len);
333 if (!modstruct)
334 warn("Could not find module name to change in %s\n",
335 module->pathname);
336 else
337 replace_modname(module, modstruct, len, oldname, newname);
340 static void clear_magic(struct elf_file *module)
342 struct string_table *tbl;
343 int j;
345 /* Old-style: __vermagic section */
346 module->ops->strip_section(module, "__vermagic");
348 /* New-style: in .modinfo section */
349 tbl = module->ops->load_strings(module, ".modinfo", NULL, fatal);
350 for (j = 0; tbl && j < tbl->cnt; j++) {
351 const char *p = tbl->str[j];
352 if (strstarts(p, "vermagic=")) {
353 memset((char *)p, 0, strlen(p));
354 return;
359 struct module_options
361 struct module_options *next;
362 char *modulename;
363 char *options;
366 struct module_command
368 struct module_command *next;
369 char *modulename;
370 char *command;
373 struct module_alias
375 struct module_alias *next;
376 char *module;
379 struct module_blacklist
381 struct module_blacklist *next;
382 char *modulename;
385 /* Link in a new option line from the config file. */
386 static struct module_options *
387 add_options(const char *modname,
388 const char *option,
389 struct module_options *options)
391 struct module_options *new;
392 char *tab;
394 new = NOFAIL(malloc(sizeof(*new)));
395 new->modulename = NOFAIL(strdup(modname));
396 new->options = NOFAIL(strdup(option));
397 /* We can handle tabs, kernel can't. */
398 for (tab = strchr(new->options, '\t'); tab; tab = strchr(tab, '\t'))
399 *tab = ' ';
400 new->next = options;
401 return new;
404 /* Link in a new install line from the config file. */
405 static struct module_command *
406 add_command(const char *modname,
407 const char *command,
408 struct module_command *commands)
410 struct module_command *new;
412 new = NOFAIL(malloc(sizeof(*new)));
413 new->modulename = NOFAIL(strdup(modname));
414 new->command = NOFAIL(strdup(command));
415 new->next = commands;
416 return new;
419 /* Link in a new alias line from the config file. */
420 static struct module_alias *
421 add_alias(const char *modname, struct module_alias *aliases)
423 struct module_alias *new;
425 new = NOFAIL(malloc(sizeof(*new)));
426 new->module = NOFAIL(strdup(modname));
427 new->next = aliases;
428 return new;
431 /* Link in a new blacklist line from the config file. */
432 static struct module_blacklist *
433 add_blacklist(const char *modname, struct module_blacklist *blacklist)
435 struct module_blacklist *new;
437 new = NOFAIL(malloc(sizeof(*new)));
438 new->modulename = NOFAIL(strdup(modname));
439 new->next = blacklist;
440 return new;
443 /* Find blacklist commands if any. */
444 static int
445 find_blacklist(const char *modname, const struct module_blacklist *blacklist)
447 while (blacklist) {
448 if (streq(blacklist->modulename, modname))
449 return 1;
450 blacklist = blacklist->next;
452 return 0;
455 /* return a new alias list, with backlisted elems filtered out */
456 static struct module_alias *
457 apply_blacklist(const struct module_alias *aliases,
458 const struct module_blacklist *blacklist)
460 struct module_alias *result = NULL;
461 while (aliases) {
462 char *modname = aliases->module;
463 if (!find_blacklist(modname, blacklist))
464 result = add_alias(modname, result);
465 aliases = aliases->next;
467 return result;
470 /* Find install commands if any. */
471 static const char *find_command(const char *modname,
472 const struct module_command *commands)
474 while (commands) {
475 if (fnmatch(commands->modulename, modname, 0) == 0)
476 return commands->command;
477 commands = commands->next;
479 return NULL;
482 static char *append_option(char *options, const char *newoption)
484 options = NOFAIL(realloc(options, strlen(options) + 1
485 + strlen(newoption) + 1));
486 if (strlen(options)) strcat(options, " ");
487 strcat(options, newoption);
488 return options;
491 static char *prepend_option(char *options, const char *newoption)
493 size_t l1, l2;
494 l1 = strlen(options);
495 l2 = strlen(newoption);
496 /* the resulting string will look like
497 * newoption + ' ' + options + '\0' */
498 if (l1) {
499 options = NOFAIL(realloc(options, l2 + 1 + l1 + 1));
500 memmove(options + l2 + 1, options, l1 + 1);
501 options[l2] = ' ';
502 memcpy(options, newoption, l2);
503 } else {
504 options = NOFAIL(realloc(options, l2 + 1));
505 memcpy(options, newoption, l2);
506 options[l2] = '\0';
508 return options;
511 /* Add to options */
512 static char *add_extra_options(const char *modname,
513 const char *optstring,
514 const struct module_options *options)
516 char *opts = NOFAIL(strdup(optstring));
518 while (options) {
519 if (streq(options->modulename, modname))
520 opts = prepend_option(opts, options->options);
521 options = options->next;
523 return opts;
526 /* Is module in /proc/modules? If so, fill in usecount if not NULL.
527 0 means no, 1 means yes, -1 means unknown.
529 static int module_in_procfs(const char *modname, unsigned int *usecount)
531 FILE *proc_modules;
532 char *line;
534 again:
535 /* Might not be mounted yet. Don't fail. */
536 proc_modules = fopen("/proc/modules", "r");
537 if (!proc_modules)
538 return -1;
540 while ((line = getline_wrapped(proc_modules, NULL)) != NULL) {
541 char *entry = strtok(line, " \n");
543 if (entry && streq(entry, modname)) {
544 /* If it exists, usecount is the third entry. */
545 if (!strtok(NULL, " \n"))
546 goto out;
548 if (!(entry = strtok(NULL, " \n"))) /* usecount */
549 goto out;
550 else
551 if (usecount)
552 *usecount = atoi(entry);
554 /* Followed by - then status. */
555 if (strtok(NULL, " \n")
556 && (entry = strtok(NULL, " \n")) != NULL) {
557 /* No locking, we might hit cases
558 * where module is in flux. Spin. */
559 if (streq(entry, "Loading")
560 || streq(entry, "Unloading")) {
561 usleep(100000);
562 free(line);
563 fclose(proc_modules);
564 goto again;
568 out:
569 free(line);
570 fclose(proc_modules);
571 return 1;
573 free(line);
575 fclose(proc_modules);
576 return 0;
579 /* Read sysfs attribute into a buffer.
580 * returns: 1 = ok, 0 = attribute missing,
581 * -1 = file error (or empty file, but we don't care).
583 static int read_attribute(const char *filename, char *buf, size_t buflen)
585 FILE *file;
586 char *s;
588 file = fopen(filename, "r");
589 if (file == NULL)
590 return (errno == ENOENT) ? 0 : -1;
591 s = fgets(buf, buflen, file);
592 fclose(file);
594 return (s == NULL) ? -1 : 1;
597 /* is this a built-in module?
598 * 0: no, 1: yes, -1: don't know
600 static int module_builtin(const char *dirname, const char *modname)
602 struct index_file *index;
603 char *filename, *value;
605 nofail_asprintf(&filename, "%s/modules.builtin.bin", dirname);
606 index = index_file_open(filename);
607 free(filename);
608 if (!index)
609 return -1;
610 value = index_search(index, modname);
611 free(value);
612 return value ? 1 : 0;
615 /* Is module in /sys/module? If so, fill in usecount if not NULL.
616 0 means no, 1 means yes, -1 means unknown.
618 static int module_in_sysfs(const char *modname, unsigned int *usecount)
620 int ret;
621 char *name;
622 struct stat finfo;
624 const int ATTR_LEN = 16;
625 char attr[ATTR_LEN];
627 /* Check sysfs is mounted */
628 if (stat("/sys/module", &finfo) < 0)
629 return -1;
631 /* Find module. */
632 nofail_asprintf(&name, "/sys/module/%s", modname);
633 ret = stat(name, &finfo);
634 free(name);
635 if (ret < 0)
636 return (errno == ENOENT) ? 0 : -1; /* Not found or unknown. */
638 nofail_asprintf(&name, "/sys/module/%s/initstate", modname);
639 ret = read_attribute(name, attr, ATTR_LEN);
640 if (ret == 0) {
641 free(name);
642 nofail_asprintf(&name, "/sys/module/%s", modname);
643 if (stat(name, &finfo) < 0) {
644 /* module was removed before we could read initstate */
645 ret = 0;
646 } else {
647 /* initstate not available (2.6.19 or earlier) */
648 ret = -1;
650 free(name);
651 return ret;
654 /* Wait for the existing module to either go live or disappear. */
655 while (ret == 1 && !streq(attr, "live\n")) {
656 usleep(100000);
657 ret = read_attribute(name, attr, ATTR_LEN);
659 free(name);
661 if (ret != 1)
662 return ret;
664 /* Get reference count, if it exists. */
665 if (usecount != NULL) {
666 nofail_asprintf(&name, "/sys/module/%s/refcnt", modname);
667 ret = read_attribute(name, attr, ATTR_LEN);
668 free(name);
669 if (ret == 1)
670 *usecount = atoi(attr);
673 return 1;
676 /* Is module loaded? If so, fill in usecount if not NULL.
677 0 means no, 1 means yes, -1 means unknown.
679 static int module_in_kernel(const char *modname, unsigned int *usecount)
681 int result;
683 result = module_in_sysfs(modname, usecount);
684 if (result != -1)
685 return result;
687 /* /sys/module/%s/initstate is only available since 2.6.20,
688 fallback to /proc/modules to get module state on earlier kernels. */
689 return module_in_procfs(modname, usecount);
692 void dump_modversions(const char *filename, errfn_t error)
694 struct elf_file *module;
696 module = grab_elf_file(filename);
697 if (!module) {
698 error("%s: %s\n", filename, strerror(errno));
699 return;
701 if (module->ops->dump_modvers(module) < 0)
702 error("Wrong section size in '%s'\n", filename);
703 release_elf_file(module);
706 /* Does path contain directory(s) subpath? */
707 static int type_matches(const char *path, const char *subpath)
709 char *subpath_with_slashes;
710 int ret;
712 nofail_asprintf(&subpath_with_slashes, "/%s/", subpath);
714 ret = (strstr(path, subpath_with_slashes) != NULL);
715 free(subpath_with_slashes);
716 return ret;
720 static int do_wildcard(const char *dirname,
721 const char *type,
722 const char *wildcard)
724 char *modules_dep_name;
725 char *line, *wcard;
726 FILE *modules_dep;
728 /* Canonicalize wildcard */
729 wcard = strdup(wildcard);
730 underscores(wcard);
732 nofail_asprintf(&modules_dep_name, "%s/%s", dirname, "modules.dep");
733 modules_dep = fopen(modules_dep_name, "r");
734 if (!modules_dep)
735 fatal("Could not load %s: %s\n",
736 modules_dep_name, strerror(errno));
738 while ((line = getline_wrapped(modules_dep, NULL)) != NULL) {
739 char *ptr;
741 /* Ignore lines without : or which start with a # */
742 ptr = strchr(line, ':');
743 if (ptr == NULL || line[strspn(line, "\t ")] == '#')
744 goto next;
745 *ptr = '\0';
747 /* "type" must match complete directory component(s). */
748 if (!type || type_matches(line, type)) {
749 char modname[strlen(line)+1];
751 filename2modname(modname, line);
752 if (fnmatch(wcard, modname, 0) == 0)
753 printf("%s\n", line);
755 next:
756 free(line);
759 free(modules_dep_name);
760 free(wcard);
761 return 0;
764 static char *strsep_skipspace(char **string, char *delim)
766 if (!*string)
767 return NULL;
768 *string += strspn(*string, delim);
769 return strsep(string, delim);
772 static int parse_config_scan(const char *filename,
773 const char *name,
774 int dump_only,
775 int removing,
776 struct module_options **options,
777 struct module_command **commands,
778 struct module_alias **alias,
779 struct module_blacklist **blacklist);
781 static int parse_config_file(const char *filename,
782 const char *name,
783 int dump_only,
784 int removing,
785 struct module_options **options,
786 struct module_command **commands,
787 struct module_alias **aliases,
788 struct module_blacklist **blacklist)
790 char *line;
791 unsigned int linenum = 0;
792 FILE *cfile;
794 cfile = fopen(filename, "r");
795 if (!cfile)
796 return 0;
798 while ((line = getline_wrapped(cfile, &linenum)) != NULL) {
799 char *ptr = line;
800 char *cmd, *modname;
802 if (dump_only)
803 printf("%s\n", line);
805 cmd = strsep_skipspace(&ptr, "\t ");
806 if (cmd == NULL || cmd[0] == '#' || cmd[0] == '\0') {
807 free(line);
808 continue;
811 if (streq(cmd, "alias")) {
812 char *wildcard = strsep_skipspace(&ptr, "\t ");
813 char *realname = strsep_skipspace(&ptr, "\t ");
815 if (!wildcard || !realname)
816 grammar(cmd, filename, linenum);
817 else if (fnmatch(underscores(wildcard),name,0) == 0)
818 *aliases = add_alias(underscores(realname), *aliases);
819 } else if (streq(cmd, "include")) {
820 struct module_alias *newalias = NULL;
821 char *newfilename;
823 newfilename = strsep_skipspace(&ptr, "\t ");
824 if (!newfilename) {
825 grammar(cmd, filename, linenum);
826 } else {
827 warn("\"include %s\" is deprecated, "
828 "please use /etc/modprobe.d\n", newfilename);
829 if (strstarts(newfilename, "/etc/modprobe.d")) {
830 warn("\"include /etc/modprobe.d\" is "
831 "the default, ignored\n");
832 } else {
833 if (!parse_config_scan(newfilename, name,
834 dump_only, removing,
835 options, commands, &newalias,
836 blacklist))
837 warn("Failed to open included"
838 " config file %s: %s\n",
839 newfilename, strerror(errno));
841 /* Files included override aliases,
842 etc that was already set ... */
843 if (newalias)
844 *aliases = newalias;
846 } else if (streq(cmd, "options")) {
847 modname = strsep_skipspace(&ptr, "\t ");
848 if (!modname || !ptr)
849 grammar(cmd, filename, linenum);
850 else {
851 ptr += strspn(ptr, "\t ");
852 *options = add_options(underscores(modname),
853 ptr, *options);
855 } else if (streq(cmd, "install")) {
856 modname = strsep_skipspace(&ptr, "\t ");
857 if (!modname || !ptr)
858 grammar(cmd, filename, linenum);
859 else if (!removing) {
860 ptr += strspn(ptr, "\t ");
861 *commands = add_command(underscores(modname),
862 ptr, *commands);
864 } else if (streq(cmd, "blacklist")) {
865 modname = strsep_skipspace(&ptr, "\t ");
866 if (!modname)
867 grammar(cmd, filename, linenum);
868 else if (!removing) {
869 *blacklist = add_blacklist(underscores(modname),
870 *blacklist);
872 } else if (streq(cmd, "remove")) {
873 modname = strsep_skipspace(&ptr, "\t ");
874 if (!modname || !ptr)
875 grammar(cmd, filename, linenum);
876 else if (removing) {
877 ptr += strspn(ptr, "\t ");
878 *commands = add_command(underscores(modname),
879 ptr, *commands);
881 } else if (streq(cmd, "config")) {
882 char *tmp = strsep_skipspace(&ptr, "\t ");
884 if (!tmp)
885 grammar(cmd, filename, linenum);
886 else if (streq(tmp, "binary_indexes")) {
887 tmp = strsep_skipspace(&ptr, "\t ");
888 if (streq(tmp, "yes"))
889 use_binary_indexes = 1;
890 if (streq(tmp, "no"))
891 use_binary_indexes = 0;
893 } else
894 grammar(cmd, filename, linenum);
896 free(line);
898 fclose(cfile);
899 return 1;
902 /* fallback to plain-text aliases file as necessary */
903 static int read_aliases_file(const char *filename,
904 const char *name,
905 int dump_only,
906 int removing,
907 struct module_options **options,
908 struct module_command **commands,
909 struct module_alias **aliases,
910 struct module_blacklist **blacklist)
912 struct index_value *realnames;
913 struct index_value *realname;
914 char *binfile;
915 struct index_file *index;
917 if (!use_binary_indexes)
918 goto plain_text;
920 nofail_asprintf(&binfile, "%s.bin", filename);
921 index = index_file_open(binfile);
922 if (!index) {
923 free(binfile);
924 goto plain_text;
927 if (dump_only) {
928 index_dump(index, stdout, "alias ");
929 free(binfile);
930 index_file_close(index);
931 return 1;
934 realnames = index_searchwild(index, name);
935 for (realname = realnames; realname; realname = realname->next)
936 *aliases = add_alias(realname->value, *aliases);
937 index_values_free(realnames);
939 free(binfile);
940 index_file_close(index);
941 return 1;
943 plain_text:
944 return parse_config_file(filename, name, dump_only, removing,
945 options, commands, aliases, blacklist);
948 static int parse_config_scan(const char *filename,
949 const char *name,
950 int dump_only,
951 int removing,
952 struct module_options **options,
953 struct module_command **commands,
954 struct module_alias **aliases,
955 struct module_blacklist **blacklist)
957 DIR *dir;
958 int ret = 0;
960 dir = opendir(filename);
961 if (dir) {
962 struct file_entry {
963 struct list_head node;
964 char name[];
966 LIST_HEAD(files_list);
967 struct file_entry *fe, *fe_tmp;
968 struct dirent *i;
970 /* sort files from directory into list */
971 while ((i = readdir(dir)) != NULL) {
972 size_t len;
974 if (i->d_name[0] == '.')
975 continue;
976 if (!config_filter(i->d_name))
977 continue;
979 len = strlen(i->d_name);
980 if (len < 6 ||
981 (strcmp(&i->d_name[len-5], ".conf") != 0 &&
982 strcmp(&i->d_name[len-6], ".alias") != 0))
983 warn("All config files need .conf: %s/%s, "
984 "it will be ignored in a future release.\n",
985 filename, i->d_name);
986 fe = malloc(sizeof(struct file_entry) + len + 1);
987 if (fe == NULL)
988 continue;
989 strcpy(fe->name, i->d_name);
990 list_for_each_entry(fe_tmp, &files_list, node)
991 if (strcmp(fe_tmp->name, fe->name) >= 0)
992 break;
993 list_add_tail(&fe->node, &fe_tmp->node);
995 closedir(dir);
997 /* parse list of files */
998 list_for_each_entry_safe(fe, fe_tmp, &files_list, node) {
999 char *cfgfile;
1001 nofail_asprintf(&cfgfile, "%s/%s", filename, fe->name);
1002 if (!parse_config_file(cfgfile, name,
1003 dump_only, removing,
1004 options, commands,
1005 aliases, blacklist))
1006 warn("Failed to open config file "
1007 "%s: %s\n", fe->name, strerror(errno));
1008 free(cfgfile);
1009 list_del(&fe->node);
1010 free(fe);
1013 ret = 1;
1014 } else {
1015 if (parse_config_file(filename, name, dump_only, removing,
1016 options, commands, aliases, blacklist))
1017 ret = 1;
1019 return ret;
1022 /* Read binary index file containing aliases only */
1023 static void parse_toplevel_config(const char *filename,
1024 const char *name,
1025 int dump_only,
1026 int removing,
1027 struct module_options **options,
1028 struct module_command **commands,
1029 struct module_alias **aliases,
1030 struct module_blacklist **blacklist)
1032 if (filename) {
1033 if (!parse_config_scan(filename, name, dump_only, removing,
1034 options, commands, aliases, blacklist))
1035 fatal("Failed to open config file %s: %s\n",
1036 filename, strerror(errno));
1037 return;
1040 /* deprecated config file */
1041 if (parse_config_file("/etc/modprobe.conf", name, dump_only, removing,
1042 options, commands, aliases, blacklist) > 0)
1043 warn("Deprecated config file /etc/modprobe.conf, "
1044 "all config files belong into /etc/modprobe.d/.\n");
1046 /* default config */
1047 parse_config_scan("/etc/modprobe.d", name, dump_only, removing,
1048 options, commands, aliases, blacklist);
1051 /* Read possible module arguments from the kernel command line. */
1052 static int parse_kcmdline(int dump_only, struct module_options **options)
1054 char *line;
1055 unsigned int linenum = 0;
1056 FILE *kcmdline;
1058 kcmdline = fopen("/proc/cmdline", "r");
1059 if (!kcmdline)
1060 return 0;
1062 while ((line = getline_wrapped(kcmdline, &linenum)) != NULL) {
1063 char *ptr = line;
1064 char *arg;
1066 while ((arg = strsep_skipspace(&ptr, "\t ")) != NULL) {
1067 char *sep, *modname, *opt;
1069 sep = strchr(arg, '.');
1070 if (sep) {
1071 if (!strchr(sep, '='))
1072 continue;
1073 modname = arg;
1074 *sep = '\0';
1075 opt = ++sep;
1077 if (dump_only)
1078 printf("options %s %s\n", modname, opt);
1080 *options = add_options(underscores(modname),
1081 opt, *options);
1085 free(line);
1087 fclose(kcmdline);
1088 return 1;
1091 static void add_to_env_var(const char *option)
1093 const char *oldenv;
1095 if ((oldenv = getenv("MODPROBE_OPTIONS")) != NULL) {
1096 char *newenv;
1097 nofail_asprintf(&newenv, "%s %s", oldenv, option);
1098 setenv("MODPROBE_OPTIONS", newenv, 1);
1099 } else
1100 setenv("MODPROBE_OPTIONS", option, 1);
1103 /* Prepend options from environment. */
1104 static char **merge_args(char *args, char *argv[], int *argc)
1106 char *arg, *argstring;
1107 char **newargs = NULL;
1108 unsigned int i, num_env = 0;
1110 if (!args)
1111 return argv;
1113 argstring = NOFAIL(strdup(args));
1114 for (arg = strtok(argstring, " "); arg; arg = strtok(NULL, " ")) {
1115 num_env++;
1116 newargs = NOFAIL(realloc(newargs,
1117 sizeof(newargs[0])
1118 * (num_env + *argc + 1)));
1119 newargs[num_env] = arg;
1122 if (!newargs)
1123 return argv;
1125 /* Append commandline args */
1126 newargs[0] = argv[0];
1127 for (i = 1; i <= *argc; i++)
1128 newargs[num_env+i] = argv[i];
1130 *argc += num_env;
1131 return newargs;
1134 static char *gather_options(char *argv[])
1136 char *optstring = NOFAIL(strdup(""));
1138 /* Rest is module options */
1139 while (*argv) {
1140 /* Quote value if it contains spaces. */
1141 unsigned int eq = strcspn(*argv, "=");
1143 if (strchr(*argv+eq, ' ') && !strchr(*argv, '"')) {
1144 char quoted[strlen(*argv) + 3];
1145 (*argv)[eq] = '\0';
1146 sprintf(quoted, "%s=\"%s\"", *argv, *argv+eq+1);
1147 optstring = append_option(optstring, quoted);
1148 } else
1149 optstring = append_option(optstring, *argv);
1150 argv++;
1152 return optstring;
1155 /* Do an install/remove command: replace $CMDLINE_OPTS if it's specified. */
1156 static void do_command(const char *modname,
1157 const char *command,
1158 int dry_run,
1159 errfn_t error,
1160 const char *type,
1161 const char *cmdline_opts)
1163 int ret;
1164 char *p, *replaced_cmd = NOFAIL(strdup(command));
1166 while ((p = strstr(replaced_cmd, "$CMDLINE_OPTS")) != NULL) {
1167 char *new;
1168 nofail_asprintf(&new, "%.*s%s%s",
1169 (int)(p - replaced_cmd), replaced_cmd, cmdline_opts,
1170 p + strlen("$CMDLINE_OPTS"));
1171 free(replaced_cmd);
1172 replaced_cmd = new;
1175 info("%s %s\n", type, replaced_cmd);
1176 if (dry_run)
1177 return;
1179 setenv("MODPROBE_MODULE", modname, 1);
1180 ret = system(replaced_cmd);
1181 if (ret == -1 || WEXITSTATUS(ret))
1182 error("Error running %s command for %s\n", type, modname);
1183 free(replaced_cmd);
1186 /* Actually do the insert. */
1187 static int insmod(struct list_head *list,
1188 const char *optstring,
1189 const char *newname,
1190 const struct module_options *options,
1191 const struct module_command *commands,
1192 const char *cmdline_opts,
1193 errfn_t error,
1194 modprobe_flags_t flags)
1196 int ret, fd;
1197 struct elf_file *module;
1198 const char *command;
1199 struct module *mod = list_entry(list->next, struct module, list);
1200 int rc = 0;
1201 int already_loaded;
1202 char *opts = NULL;
1204 /* Take us off the list. */
1205 list_del(&mod->list);
1207 /* Do things we (or parent) depend on first. */
1208 if (!list_empty(list)) {
1209 modprobe_flags_t f = flags;
1210 f &= ~mit_first_time;
1211 f &= ~mit_ignore_commands;
1212 if ((rc = insmod(list, "", NULL,
1213 options, commands, "", warn, f)) != 0) {
1214 error("Error inserting %s (%s): %s\n",
1215 mod->modname, mod->filename,
1216 insert_moderror(errno));
1217 goto out;
1221 fd = open_file(mod->filename);
1222 if (fd < 0) {
1223 error("Could not open '%s': %s\n",
1224 mod->filename, strerror(errno));
1225 goto out;
1228 /* Don't do ANYTHING if already in kernel. */
1229 already_loaded = module_in_kernel(newname ?: mod->modname, NULL);
1231 if (!(flags & mit_ignore_loaded) && already_loaded == 1) {
1232 if (flags & mit_first_time)
1233 error("Module %s already in kernel.\n",
1234 newname ?: mod->modname);
1235 goto out_unlock;
1238 command = find_command(mod->modname, commands);
1239 if (command && !(flags & mit_ignore_commands)) {
1240 if (already_loaded == -1) {
1241 warn("/sys/module/ not present or too old,"
1242 " and /proc/modules does not exist.\n");
1243 warn("Ignoring install commands for %s"
1244 " in case it is already loaded.\n",
1245 newname ?: mod->modname);
1246 } else {
1247 close_file(fd);
1248 do_command(mod->modname, command, flags & mit_dry_run,
1249 error, "install", cmdline_opts);
1250 goto out;
1254 module = grab_elf_file_fd(mod->filename, fd);
1255 if (!module) {
1256 error("Could not read '%s': %s\n", mod->filename,
1257 (errno == ENOEXEC) ? "Invalid module format" :
1258 strerror(errno));
1259 goto out_unlock;
1261 if (newname)
1262 rename_module(module, mod->modname, newname);
1263 if (flags & mit_strip_modversion)
1264 module->ops->strip_section(module, "__versions");
1265 if (flags & mit_strip_vermagic)
1266 clear_magic(module);
1268 /* Config file might have given more options */
1269 opts = add_extra_options(mod->modname, optstring, options);
1271 info("insmod %s %s\n", mod->filename, opts);
1273 if (flags & mit_dry_run)
1274 goto out_elf_file;
1276 ret = init_module(module->data, module->len, opts);
1277 if (ret != 0) {
1278 if (errno == EEXIST) {
1279 if (flags & mit_first_time)
1280 error("Module %s already in kernel.\n",
1281 newname ?: mod->modname);
1282 goto out_elf_file;
1284 /* don't warn noisely if we're loading multiple aliases. */
1285 /* one of the aliases may try to use hardware we don't have. */
1286 if ((error != warn) || (verbose))
1287 error("Error inserting %s (%s): %s\n",
1288 mod->modname, mod->filename,
1289 insert_moderror(errno));
1290 rc = 1;
1292 out_elf_file:
1293 release_elf_file(module);
1294 out_unlock:
1295 close_file(fd);
1296 free(opts);
1297 out:
1298 return rc;
1301 /* Do recursive removal. */
1302 static void rmmod(struct list_head *list,
1303 const char *name,
1304 struct module_command *commands,
1305 const char *cmdline_opts,
1306 errfn_t error,
1307 modprobe_flags_t flags)
1309 const char *command;
1310 unsigned int usecount = 0;
1311 struct module *mod = list_entry(list->next, struct module, list);
1312 int exists;
1314 /* Take first one off the list. */
1315 list_del(&mod->list);
1317 if (!name)
1318 name = mod->modname;
1320 /* Don't do ANYTHING if not loaded. */
1321 exists = module_in_kernel(name, &usecount);
1322 if (exists == 0)
1323 goto nonexistent_module;
1325 /* Even if renamed, find commands to orig. name. */
1326 command = find_command(mod->modname, commands);
1327 if (command && !(flags & mit_ignore_commands)) {
1328 if (exists == -1) {
1329 warn("/sys/module/ not present or too old,"
1330 " and /proc/modules does not exist.\n");
1331 warn("Ignoring remove commands for %s"
1332 " in case it is not loaded.\n",
1333 mod->modname);
1334 } else {
1335 do_command(mod->modname, command, flags & mit_dry_run,
1336 error, "remove", cmdline_opts);
1337 goto remove_rest;
1341 if (usecount != 0) {
1342 if (!(flags & mit_quiet_inuse))
1343 error("Module %s is in use.\n", name);
1344 goto remove_rest;
1347 info("rmmod %s\n", mod->filename);
1349 if (flags & mit_dry_run)
1350 goto remove_rest;
1352 if (delete_module(name, O_EXCL) != 0) {
1353 if (errno == ENOENT)
1354 goto nonexistent_module;
1355 error("Error removing %s (%s): %s\n",
1356 name, mod->filename,
1357 remove_moderror(errno));
1360 remove_rest:
1361 /* Now do things we depend. */
1362 if (!list_empty(list)) {
1363 flags &= ~mit_first_time;
1364 flags &= ~mit_ignore_commands;
1365 flags |= mit_quiet_inuse;
1367 rmmod(list, NULL, commands, "", warn, flags);
1369 return;
1371 nonexistent_module:
1372 if (flags & mit_first_time)
1373 fatal("Module %s is not in kernel.\n", mod->modname);
1374 goto remove_rest;
1377 static int handle_module(const char *modname,
1378 struct list_head *todo_list,
1379 const char *newname,
1380 const char *options,
1381 struct module_options *modoptions,
1382 struct module_command *commands,
1383 const char *cmdline_opts,
1384 errfn_t error,
1385 modprobe_flags_t flags)
1387 if (list_empty(todo_list)) {
1388 const char *command;
1390 /* The dependencies have to be real modules, but
1391 handle case where the first is completely bogus. */
1392 command = find_command(modname, commands);
1393 if (command && !(flags & mit_ignore_commands)) {
1394 do_command(modname, command, flags & mit_dry_run, error,
1395 (flags & mit_remove) ? "remove":"install", cmdline_opts);
1396 return 0;
1399 if (!quiet)
1400 error("Module %s not found.\n", modname);
1401 return 1;
1404 if (flags & mit_remove)
1405 rmmod(todo_list, newname, commands, cmdline_opts, error, flags);
1406 else
1407 insmod(todo_list, options, newname,
1408 modoptions, commands, cmdline_opts, error, flags);
1410 return 0;
1413 int handle_builtin_module(const char *modname,
1414 errfn_t error,
1415 modprobe_flags_t flags)
1417 if (flags & mit_remove) {
1418 error("Module %s is builtin\n", modname);
1419 return 1;
1420 } else if (flags & mit_first_time) {
1421 error("Module %s already in kernel (builtin).\n", modname);
1422 return 1;
1423 } else if (flags & mit_ignore_loaded) {
1424 /* --show-depends given */
1425 info("builtin %s\n", modname);
1427 return 0;
1430 int do_modprobe(char *modname,
1431 char *newname,
1432 char *cmdline_opts,
1433 const char *configname,
1434 const char *dirname,
1435 const char *aliasfilename,
1436 const char *symfilename,
1437 errfn_t error,
1438 modprobe_flags_t flags)
1440 struct module_command *commands = NULL;
1441 struct module_options *modoptions = NULL;
1442 struct module_alias *aliases = NULL;
1443 struct module_blacklist *blacklist = NULL;
1444 LIST_HEAD(list);
1445 int failed = 0;
1447 /* Convert name we are looking for */
1448 underscores(modname);
1450 /* Returns the resolved alias, options */
1451 parse_toplevel_config(configname, modname, 0,
1452 flags & mit_remove, &modoptions, &commands, &aliases, &blacklist);
1454 /* Read module options from kernel command line */
1455 parse_kcmdline(0, &modoptions);
1457 /* No luck? Try symbol names, if starts with symbol:. */
1458 if (!aliases && strstarts(modname, "symbol:")) {
1459 parse_config_file(symfilename, modname, 0,
1460 flags & mit_remove, &modoptions, &commands,
1461 &aliases, &blacklist);
1463 if (!aliases) {
1464 if(!strchr(modname, ':'))
1465 read_depends(dirname, modname, &list);
1467 /* We only use canned aliases as last resort. */
1468 if (list_empty(&list)
1469 && !find_command(modname, commands))
1471 read_aliases_file(aliasfilename,
1472 modname, 0, flags & mit_remove,
1473 &modoptions, &commands,
1474 &aliases, &blacklist);
1475 /* builtin module? */
1476 if (!aliases && module_builtin(dirname, modname) > 0) {
1477 return handle_builtin_module(modname, error,
1478 flags);
1483 aliases = apply_blacklist(aliases, blacklist);
1484 if(flags & mit_resolve_alias) {
1485 for(; aliases; aliases=aliases->next)
1486 printf("%s\n", aliases->module);
1487 return 0;
1489 if (aliases) {
1490 errfn_t err = error;
1492 /* More than one alias? Don't bail out on failure. */
1493 if (aliases->next)
1494 err = warn;
1495 while (aliases) {
1496 /* Add the options for this alias. */
1497 char *opts;
1498 opts = add_extra_options(modname,
1499 cmdline_opts, modoptions);
1501 read_depends(dirname, aliases->module, &list);
1502 failed |= handle_module(aliases->module,
1503 &list, newname, opts, modoptions,
1504 commands, cmdline_opts, err, flags);
1506 aliases = aliases->next;
1507 free(opts);
1508 INIT_LIST_HEAD(&list);
1510 } else {
1511 if (flags & mit_use_blacklist
1512 && find_blacklist(modname, blacklist))
1513 return failed;
1515 failed |= handle_module(modname, &list, newname, cmdline_opts,
1516 modoptions, commands, cmdline_opts, error, flags);
1518 return failed;
1521 static struct option options[] = { { "version", 0, NULL, 'V' },
1522 { "verbose", 0, NULL, 'v' },
1523 { "quiet", 0, NULL, 'q' },
1524 { "syslog", 0, NULL, 's' },
1525 { "show", 0, NULL, 'n' },
1526 { "dry-run", 0, NULL, 'n' },
1527 { "show-depends", 0, NULL, 'D' },
1528 { "resolve-alias", 0, NULL, 'R' },
1529 { "dirname", 1, NULL, 'd' },
1530 { "set-version", 1, NULL, 'S' },
1531 { "config", 1, NULL, 'C' },
1532 { "name", 1, NULL, 'o' },
1533 { "remove", 0, NULL, 'r' },
1534 { "showconfig", 0, NULL, 'c' },
1535 { "list", 0, NULL, 'l' },
1536 { "type", 1, NULL, 't' },
1537 { "all", 0, NULL, 'a' },
1538 { "ignore-install", 0, NULL, 'i' },
1539 { "ignore-remove", 0, NULL, 'i' },
1540 { "use-blacklist", 0, NULL, 'b' },
1541 { "force", 0, NULL, 'f' },
1542 { "force-vermagic", 0, NULL, 1 },
1543 { "force-modversion", 0, NULL, 2 },
1544 { "first-time", 0, NULL, 3 },
1545 { "dump-modversions", 0, NULL, 4 },
1546 { NULL, 0, NULL, 0 } };
1548 int main(int argc, char *argv[])
1550 struct utsname buf;
1551 struct stat statbuf;
1552 int opt;
1553 int dump_config = 0;
1554 int list_only = 0;
1555 int all = 0;
1556 int dump_modver = 0;
1557 unsigned int i, num_modules;
1558 char *type = NULL;
1559 const char *configname = NULL;
1560 char *basedir = "";
1561 char *cmdline_opts = NULL;
1562 char *newname = NULL;
1563 char *dirname, *aliasfilename, *symfilename;
1564 errfn_t error = fatal;
1565 int failed = 0;
1566 modprobe_flags_t flags = 0;
1568 /* Prepend options from environment. */
1569 argv = merge_args(getenv("MODPROBE_OPTIONS"), argv, &argc);
1571 uname(&buf);
1572 while ((opt = getopt_long(argc, argv, "Vvqsnd:C:o:rclt:aibf", options, NULL)) != -1){
1573 switch (opt) {
1574 case 'V':
1575 puts(PACKAGE " version " VERSION);
1576 exit(0);
1577 case 'v':
1578 add_to_env_var("-v");
1579 verbose = 1;
1580 break;
1581 case 'q':
1582 quiet = 1;
1583 add_to_env_var("-q");
1584 break;
1585 case 's':
1586 add_to_env_var("-s");
1587 logging = 1;
1588 break;
1589 case 'n':
1590 flags |= mit_dry_run;
1591 break;
1592 case 'd':
1593 basedir = optarg;
1594 break;
1595 case 'S':
1596 strncpy(buf.release, optarg, sizeof(buf.release));
1597 buf.release[sizeof(buf.release)-1] = '\0';
1598 break;
1599 case 'C':
1600 configname = optarg;
1601 add_to_env_var("-C");
1602 add_to_env_var(configname);
1603 break;
1604 case 'D':
1605 flags |= mit_dry_run;
1606 flags |= mit_ignore_loaded;
1607 verbose = 1;
1608 break;
1609 case 'R':
1610 flags |= mit_resolve_alias;
1611 break;
1612 case 'o':
1613 newname = optarg;
1614 break;
1615 case 'r':
1616 flags |= mit_remove;
1617 break;
1618 case 'c':
1619 dump_config = 1;
1620 break;
1621 case 'l':
1622 list_only = 1;
1623 break;
1624 case 't':
1625 type = optarg;
1626 break;
1627 case 'a':
1628 all = 1;
1629 error = warn;
1630 break;
1631 case 'i':
1632 flags |= mit_ignore_commands;
1633 break;
1634 case 'b':
1635 flags |= mit_use_blacklist;
1636 break;
1637 case 'f':
1638 flags |= mit_strip_vermagic;
1639 flags |= mit_strip_modversion;
1640 break;
1641 case 1:
1642 flags |= mit_strip_vermagic;
1643 break;
1644 case 2:
1645 flags |= mit_strip_modversion;
1646 break;
1647 case 3:
1648 flags |= mit_first_time;
1649 break;
1650 case 4:
1651 dump_modver = 1;
1652 break;
1653 default:
1654 print_usage(argv[0]);
1658 /* If stderr not open, go to syslog */
1659 if (logging || fstat(STDERR_FILENO, &statbuf) != 0) {
1660 openlog("modprobe", LOG_CONS, LOG_DAEMON);
1661 logging = 1;
1664 if (argc < optind + 1 && !dump_config && !list_only)
1665 print_usage(argv[0]);
1667 nofail_asprintf(&dirname, "%s%s/%s", basedir, MODULE_DIR, buf.release);
1668 nofail_asprintf(&aliasfilename, "%s/modules.alias", dirname);
1669 nofail_asprintf(&symfilename, "%s/modules.symbols", dirname);
1671 /* Old-style -t xxx wildcard? Only with -l. */
1672 if (list_only) {
1673 if (optind+1 < argc)
1674 fatal("Can't have multiple wildcards\n");
1675 /* fprintf(stderr, "man find\n"); return 1; */
1676 return do_wildcard(dirname, type, argv[optind]?:"*");
1678 if (type)
1679 fatal("-t only supported with -l");
1681 if (dump_config) {
1682 struct module_command *commands = NULL;
1683 struct module_options *modoptions = NULL;
1684 struct module_alias *aliases = NULL;
1685 struct module_blacklist *blacklist = NULL;
1687 parse_toplevel_config(configname, "", 1, 0, &modoptions,
1688 &commands, &aliases, &blacklist);
1689 /* Read module options from kernel command line */
1690 parse_kcmdline(1, &modoptions);
1691 parse_config_file(aliasfilename, "", 1, 0, &modoptions,
1692 &commands, &aliases, &blacklist);
1693 parse_config_file(symfilename, "", 1, 0, &modoptions,
1694 &commands, &aliases, &blacklist);
1695 exit(0);
1698 if ((flags & mit_remove) || all) {
1699 num_modules = argc - optind;
1700 cmdline_opts = NOFAIL(strdup(""));
1701 } else {
1702 num_modules = 1;
1703 cmdline_opts = gather_options(argv+optind+1);
1706 /* num_modules is always 1 except for -r or -a. */
1707 for (i = 0; i < num_modules; i++) {
1708 char *modname = argv[optind + i];
1710 if (dump_modver)
1711 dump_modversions(modname, error);
1712 else
1713 failed |= do_modprobe(modname, newname, cmdline_opts,
1714 configname, dirname, aliasfilename, symfilename,
1715 error, flags);
1718 if (logging)
1719 closelog();
1721 free(dirname);
1722 free(aliasfilename);
1723 free(symfilename);
1724 free(cmdline_opts);
1726 exit(failed);