depmod: fix tabs in help output
[module-init-tools.git] / depmod.c
blob90c24e473effd14d0a845c8ac9da4742bce8f46e
1 /*
2 * depmod.c: generate module dependency meta-data (aliases, etc.)
4 * (C) 2002 Rusty Russell IBM Corporation
5 * (C) 2006-2011 Jon Masters <jcm@jonmasters.org>, and others.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
22 #define _GNU_SOURCE /* asprintf */
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <getopt.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <elf.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <dirent.h>
35 #include <sys/utsname.h>
36 #include <sys/mman.h>
38 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
40 #include "util.h"
41 #include "zlibsupport.h"
42 #include "depmod.h"
43 #include "logging.h"
44 #include "index.h"
45 #include "elfops.h"
46 #include "tables.h"
47 #include "config_filter.h"
49 #include "testing.h"
51 #ifndef MODULE_DIR
52 #define MODULE_DIR "/lib/modules/"
53 #endif
55 #ifndef MODULE_BUILTIN_KEY
56 #define MODULE_BUILTIN_KEY "built-in"
57 #endif
59 /* used to replace one module with another based on conf override entries */
60 struct module_overrides
62 /* Next override */
63 struct module_overrides *next;
65 /* overridden module */
66 char *modfile;
69 /* used to specify the order in which /lib/modules subdirs are searched */
70 struct module_search
72 /* Next search */
73 struct module_search *next;
75 /* search path */
76 char *search_path;
77 size_t len;
80 static char sym_prefix; /* used for -P option */
81 static unsigned int skipchars; /* prefix target part of basedir to skip over */
82 static unsigned int make_map_files = 1; /* default to on */
83 static unsigned int force_map_files = 0; /* default to on */
85 #define SYMBOL_HASH_SIZE 1024
86 struct symbol
88 struct symbol *next;
89 struct module *owner;
90 uint64_t ver;
91 char name[0];
94 static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
96 /**
97 * tdb_hash - calculate hash entry for a symbol (algorithm from gdbm, via tdb)
99 * @name: symbol name
102 static inline unsigned int tdb_hash(const char *name)
104 unsigned value; /* Used to compute the hash value. */
105 unsigned i; /* Used to cycle through random values. */
107 /* Set the initial value from the key size. */
108 for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
109 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
111 return (1103515243 * value + 12345);
115 * skip_symprefix - remove extraneous prefix character on some architectures
117 * @symname: symbol name to process for prefix character removal
120 static const char *skip_symprefix(const char *symname)
122 return symname + (symname[0] == sym_prefix ? 1 : 0);
126 * add_symbol - add a symbol to the symbol hash list
128 * @name: symbol name
129 * @ver: symbol version (checksum)
130 * @owner: module owning this symbol
133 static void add_symbol(const char *name, uint64_t ver, struct module *owner)
135 unsigned int hash;
136 struct symbol *new = NOFAIL(malloc(sizeof *new + strlen(name) + 1));
138 new->owner = owner;
139 new->ver = ver;
140 strcpy(new->name, name);
142 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
143 new->next = symbolhash[hash];
144 symbolhash[hash] = new;
147 static int print_unknown, check_symvers;
150 * find_symbol - lookup module owning a symbol in the symbol hash list
152 * @name: symbol name
153 * @ver: symbol version
154 * @modname: pathname of module requesting lookup (being processed)
155 * @weak: whether the symbol is weakly defined or not
157 * This function is used during dependency calculation to match the
158 * dependencies a module says it requires with symbols we have seen.
159 * calculate_deps calls us after it has load_dep_syms on a module.
162 static struct module *find_symbol(const char *name, uint64_t ver,
163 const char *modname, int weak)
165 struct symbol *s;
167 /* For our purposes, .foo matches foo. PPC64 needs this. */
168 if (name[0] == '.')
169 name++;
170 name = skip_symprefix(name);
172 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) {
173 if (streq(s->name, name))
174 break;
176 if (s) {
177 if (ver && s->ver && s->ver != ver && print_unknown && !weak)
178 warn("%s disagrees about version of symbol %s\n",
179 modname, name);
180 return s->owner;
183 if (print_unknown && !weak)
184 warn("%s needs unknown symbol %s\n", modname, name);
186 return NULL;
190 * add_dep - add a module dependency
192 * @mod: module name
193 * @depends_on: new module dependency
196 static void add_dep(struct module *mod, struct module *depends_on)
198 unsigned int i;
200 for (i = 0; i < mod->num_deps; i++)
201 if (mod->deps[i] == depends_on)
202 return;
204 mod->deps = NOFAIL(realloc(mod->deps, sizeof(mod->deps[0])*(mod->num_deps+1)));
205 mod->deps[mod->num_deps++] = depends_on;
209 * add_fake_syms - symbols not explicitly otherwise provided
211 * The kernel provides a few dependencies within the module loader.
214 static void add_fake_syms(void)
216 /* __this_module is magic inserted by kernel loader. */
217 add_symbol("__this_module", 0, NULL);
218 /* On S390, this is faked up too */
219 add_symbol("_GLOBAL_OFFSET_TABLE_", 0, NULL);
223 * load_system_map - load list of symbols from the System.map
225 * @filename: path to the System.map-like file
228 static void load_system_map(const char *filename)
230 FILE *system_map;
231 char line[10240];
232 const char ksymstr[] = "__ksymtab_";
233 const int ksymstr_len = strlen(ksymstr);
235 system_map = fopen(filename, "r");
236 if (!system_map)
237 fatal("Could not open '%s': %s\n", filename, strerror(errno));
239 /* eg. c0294200 R __ksymtab_devfs_alloc_devnum */
240 while (fgets(line, sizeof(line)-1, system_map)) {
241 char *ptr;
242 const char *cptr;
244 /* Snip \n */
245 ptr = strchr(line, '\n');
246 if (ptr)
247 *ptr = '\0';
249 ptr = strchr(line, ' ');
250 if (!ptr || !(ptr = strchr(ptr + 1, ' ')))
251 continue;
253 /* Skip the space before symbol name */
254 cptr = skip_symprefix(ptr + 1);
256 /* Covers gpl-only and normal symbols. */
257 if (strstarts(cptr, ksymstr))
258 add_symbol(cptr + ksymstr_len, 0, NULL);
261 fclose(system_map);
262 add_fake_syms();
266 * load_module_symvers - load list of symbol versions from the module.symvers
268 * @filename: path to the module.symvers-like file
271 static void load_module_symvers(const char *filename)
273 FILE *module_symvers;
274 char line[10240];
276 module_symvers = fopen(filename, "r");
277 if (!module_symvers)
278 fatal("Could not open '%s': %s\n", filename, strerror(errno));
280 /* eg. "0xb352177e\tfind_first_bit\tvmlinux\tEXPORT_SYMBOL" */
281 while (fgets(line, sizeof(line)-1, module_symvers)) {
282 const char *ver, *sym, *where;
284 ver = strtok(line, " \t");
285 sym = strtok(NULL, " \t");
286 where = strtok(NULL, " \t");
287 if (!ver || !sym || !where)
288 continue;
290 if (streq(where, "vmlinux"))
291 add_symbol(skip_symprefix(sym), strtoull(ver, NULL, 16), NULL);
294 fclose(module_symvers);
295 add_fake_syms();
298 static const struct option options[] = { { "all", 0, NULL, 'a' },
299 { "quick", 0, NULL, 'A' },
300 { "basedir", 1, NULL, 'b' },
301 { "config", 1, NULL, 'C' },
302 { "symvers", 1, NULL, 'E' },
303 { "filesyms", 1, NULL, 'F' },
304 { "errsyms", 0, NULL, 'e' },
305 { "unresolved-error", 0, NULL, 'u' },
306 { "quiet", 0, NULL, 'q' },
307 { "root", 0, NULL, 'r' },
308 { "verbose", 0, NULL, 'v' },
309 { "show", 0, NULL, 'n' },
310 { "dry-run", 0, NULL, 'n' },
311 { "symbol-prefix", 0, NULL, 'P' },
312 { "help", 0, NULL, 'h' },
313 { "version", 0, NULL, 'V' },
314 { "warn", 0, NULL, 'w' },
315 { "map", 0, NULL, 'm' },
316 { NULL, 0, NULL, 0 } };
319 * is_version_number - is the option a kernel version or module name
321 * @version: possible version number
324 static int is_version_number(const char *version)
326 unsigned int dummy;
328 return (sscanf(version, "%u.%u", &dummy, &dummy) == 2);
332 * old_module_version - is the kernel version too old for these tools
334 * @version: version number
337 static int old_module_version(const char *version)
339 /* Expect three part version (but won't fail it only two part). */
340 unsigned int major, sub, minor;
342 sscanf(version, "%u.%u.%u", &major, &sub, &minor);
344 if (major > 2) return 0;
345 if (major < 2) return 1;
347 /* 2.x */
348 if (sub > 5) return 0;
349 if (sub < 5) return 1;
351 /* 2.5.x */
352 if (minor >= 48) return 0;
353 return 1;
357 * print_usage - output a list of all possible options
359 * @name: not currently used
362 static void print_usage(const char *name)
364 fprintf(stderr,
365 "%s " VERSION " -- part of " PACKAGE "\n"
366 "%s -[aA] [-n -e -v -q -V -r -u -w -m]\n"
367 " [-b basedirectory] [forced_version]\n"
368 "depmod [-n -e -v -q -r -u -w] [-F kernelsyms] module1.ko module2.ko ...\n"
369 "If no arguments (except options) are given, \"depmod -a\" is assumed\n"
370 "\n"
371 "depmod will output a dependancy list suitable for the modprobe utility.\n"
372 "\n"
373 "\n"
374 "Options:\n"
375 "\t-a, --all Probe all modules\n"
376 "\t-A, --quick Only does the work if there's a new module\n"
377 "\t-e, --errsyms Report not supplied symbols\n"
378 "\t-m, --map Create the legacy map files\n"
379 "\t-n, --show Write the dependency file on stdout only\n"
380 "\t-P, --symbol-prefix Architecture symbol prefix\n"
381 "\t-V, --version Print the release version\n"
382 "\t-v, --verbose Enable verbose mode\n"
383 "\t-w, --warn Warn on duplicates\n"
384 "\t-h, --help Print this usage message\n"
385 "\n"
386 "The following options are useful for people managing distributions:\n"
387 "\t-b basedirectory\n"
388 "\t --basedir basedirectory Use an image of a module tree.\n"
389 "\t-F kernelsyms\n"
390 "\t --filesyms kernelsyms Use the file instead of the\n"
391 "\t current kernel symbols.\n"
392 "\t-E Module.symvers\n"
393 "\t --symvers Module.symvers Use Module.symvers file to check\n"
394 "\t symbol versions.\n",
395 "depmod", "depmod");
399 * ends_in - check file extension
401 * @name: filename
402 * @ext: extension to check
405 static int ends_in(const char *name, const char *ext)
407 unsigned int namelen, extlen;
409 /* Grab lengths */
410 namelen = strlen(name);
411 extlen = strlen(ext);
413 if (namelen < extlen) return 0;
415 if (streq(name + namelen - extlen, ext))
416 return 1;
417 return 0;
421 * grab_module - open module ELF file and load symbol data
423 * @dirname: path prefix
424 * @filename: filename within path
427 static struct module *grab_module(const char *dirname, const char *filename)
429 struct module *new;
431 new = NOFAIL(malloc(sizeof(*new)
432 + strlen(dirname?:"") + 1 + strlen(filename) + 1));
433 if (dirname)
434 sprintf(new->pathname, "%s/%s", dirname, filename);
435 else
436 strcpy(new->pathname, filename);
437 new->basename = my_basename(new->pathname);
439 INIT_LIST_HEAD(&new->dep_list);
440 new->order = INDEX_PRIORITY_MIN;
442 new->file = grab_elf_file(new->pathname);
443 if (!new->file) {
444 warn("Can't read module %s: %s\n",
445 new->pathname, strerror(errno));
446 free(new);
447 return NULL;
449 return new;
452 /* We use this on-stack structure to track recursive calls to has_dep_loop */
453 struct module_traverse
455 struct module_traverse *prev;
456 struct module *mod;
460 * in_loop - determine if a module dependency loop exists
462 * @mod: current module
463 * @traverse: on-stack structure created as module deps were processed
466 static int in_loop(struct module *mod, const struct module_traverse *traverse)
468 const struct module_traverse *i;
470 for (i = traverse; i; i = i->prev) {
471 if (i->mod == mod)
472 return 1;
474 return 0;
478 * report_loop - report (once) that a dependency loop exists for a module
480 * @mod: module with dep loop
481 * @traverse: on-stack structure created as module deps were processed
484 static void report_loop(const struct module *mod,
485 const struct module_traverse *traverse)
487 const struct module_traverse *i;
489 /* Check that start is least alphabetically. eg. a depends
490 on b depends on a will get reported for a, not b. */
491 for (i = traverse->prev; i->prev; i = i->prev) {
492 if (strcmp(mod->pathname, i->mod->pathname) > 0)
493 return;
496 /* Is start in the loop? If not, don't report now. eg. a
497 depends on b which depends on c which depends on b. Don't
498 report when generating depends for a. */
499 if (mod != i->mod)
500 return;
502 warn("Loop detected: %s ", mod->pathname);
503 for (i = traverse->prev; i->prev; i = i->prev)
504 fprintf(stderr, "needs %s ", i->mod->basename);
505 fprintf(stderr, "which needs %s again!\n", i->mod->basename);
509 * has_dep_loop - iterate over all module deps and check for loops
511 * @module: module to process
512 * @prev: previously processed dependency
514 * This function is called recursively, following every dep and creating
515 * a module_traverse on the stack describing each dependency encountered.
516 * Determining a loop is as simple (and slow) as finding repetitions.
518 * This is slow, but we can't leave the user without any modules, so we
519 * need to detect loops and just fail those modules that cause loops.
522 static int has_dep_loop(struct module *module, struct module_traverse *prev)
524 unsigned int i;
525 struct module_traverse traverse = { .prev = prev, .mod = module };
527 if (in_loop(module, prev)) {
528 report_loop(module, &traverse);
529 return 1;
532 for (i = 0; i < module->num_deps; i++)
533 if (has_dep_loop(module->deps[i], &traverse))
534 return 1;
535 return 0;
539 * order_dep_list - expand all module deps recursively and in order
541 * @start: module being processed
542 * @mod: recursive dep
544 * We expand all of the dependencies of the dependencies of a module
545 * and ensure that the lowest dependency is loaded first, etc.
548 static void order_dep_list(struct module *start, struct module *mod)
550 unsigned int i;
552 for (i = 0; i < mod->num_deps; i++) {
553 /* If it was previously depended on, move it to the
554 tail. ie. if a needs b and c, and c needs b, we
555 must order b after c. */
556 list_del(&mod->deps[i]->dep_list);
557 list_add_tail(&mod->deps[i]->dep_list, &start->dep_list);
558 order_dep_list(start, mod->deps[i]);
562 static struct module *deleted = NULL;
565 * del_module - remove from list of modules
567 * @modules: list of modules
568 * @delme: module to remove
571 static void del_module(struct module **modules, struct module *delme)
573 struct module **i;
575 /* Find pointer to it. */
576 if (modules) {
577 for (i = modules; *i != delme; i = &(*i)->next);
579 *i = delme->next;
582 /* Save on a list to quiet valgrind.
583 Can't free - other modules may depend on them */
584 delme->next = deleted;
585 deleted = delme;
589 * compress_path - strip out common path prefix for modules
591 * @path: path to module
592 * @basedir: top level modules directory
594 * Modules are typically located in /lib/modules. There is no need to
595 * store the same common path prefix for all modules - make paths
596 * relative to directory that contains the dep information files.
599 static const char *compress_path(const char *path, const char *basedir)
601 int len = strlen(basedir);
603 if (strncmp(path, basedir, len) == 0)
604 path += len + 1;
605 return path;
609 * output_deps - create ascii text file representation of module deps
611 * @modules: list of modules
612 * @out: output file
613 * @dirname: output directory
616 static int output_deps(struct module *modules,
617 FILE *out, char *dirname)
619 struct module *i;
621 for (i = modules; i; i = i->next) {
622 struct list_head *j, *tmp;
623 order_dep_list(i, i);
625 fprintf(out, "%s:", compress_path(i->pathname, dirname));
626 list_for_each_safe(j, tmp, &i->dep_list) {
627 struct module *dep
628 = list_entry(j, struct module, dep_list);
629 fprintf(out, " %s",
630 compress_path(dep->pathname, dirname));
631 list_del_init(j);
633 fprintf(out, "\n");
635 return 1;
638 /* warn whenever duplicate module aliases, deps, or symbols are found. */
639 static int warn_dups = 0;
642 * output_deps_bin - create binary trie representation of module deps
644 * @modules: list of modules
645 * @out: output binary file
646 * @dirname: output directory
648 * This optimized dependency file contains an ordered structure that is
649 * more easily processed by modprobe in a time sensitive manner.
652 static int output_deps_bin(struct module *modules,
653 FILE *out, char *dirname)
655 struct module *i;
656 struct index_node *index;
657 char *line;
658 char *p;
660 index = index_create();
662 for (i = modules; i; i = i->next) {
663 struct list_head *j, *tmp;
664 char modname[strlen(i->pathname)+1];
666 order_dep_list(i, i);
668 filename2modname(modname, i->pathname);
669 nofail_asprintf(&line, "%s:",
670 compress_path(i->pathname, dirname));
671 p = line;
672 list_for_each_safe(j, tmp, &i->dep_list) {
673 struct module *dep
674 = list_entry(j, struct module, dep_list);
675 nofail_asprintf(&line, "%s %s",
677 compress_path(dep->pathname, dirname));
678 free(p);
679 p = line;
680 list_del_init(j);
682 if (index_insert(index, modname, line, i->order) && warn_dups)
683 warn("duplicate module deps:\n%s\n",line);
684 free(line);
687 index_write(index, out);
688 index_destroy(index);
690 return 1;
694 * smells_like_module - detect common module extensions
696 * @name: filename
699 static int smells_like_module(const char *name)
701 return ends_in(name,".ko") || ends_in(name, ".ko.gz");
704 typedef struct module *(*do_module_t)(const char *dirname,
705 const char *filename,
706 struct module *next,
707 struct module_search *search,
708 struct module_overrides *overrides);
711 * is_higher_priority - find modules replacing other modules
713 * @newpath: new module path
714 * @oldpath: old module path
715 * @search: path search order
716 * @overrides: module override directives
718 * Compares one module (path) to another module (path) and determines
719 * whether the new module should replace the existing module of the
720 * same name. Overriding is handled very coarsely for the moment.
723 static int is_higher_priority(const char *newpath, const char *oldpath,
724 struct module_search *search,
725 struct module_overrides *overrides)
727 struct module_search *tmp;
728 struct module_overrides *ovtmp;
729 int i = 0;
730 int prio_builtin = -1;
731 int prio_new = -1;
732 int prio_old = -1;
734 /* The names already match, now we check for overrides and directory search
735 * order
737 for (ovtmp = overrides; ovtmp != NULL; ovtmp = ovtmp->next) {
738 if (streq(ovtmp->modfile, newpath))
739 return 1;
740 if (streq(ovtmp->modfile, oldpath))
741 return 0;
743 for (i = 0, tmp = search; tmp != NULL; tmp = tmp->next, i++) {
744 if (streq(tmp->search_path, MODULE_BUILTIN_KEY))
745 prio_builtin = i;
746 else if (strncmp(tmp->search_path, newpath, tmp->len) == 0)
747 prio_new = i;
748 else if (strncmp(tmp->search_path, oldpath, tmp->len) == 0)
749 prio_old = i;
751 if (prio_new < 0)
752 prio_new = prio_builtin;
753 if (prio_old < 0)
754 prio_old = prio_builtin;
756 return prio_new > prio_old;
760 * do_module - process a module file
762 * @dirname: directory containing module
763 * @filename: module disk file
764 * @list: list of modules
765 * @search: path search order
766 * @overrides: module override directives
769 static struct module *do_module(const char *dirname,
770 const char *filename,
771 struct module *list,
772 struct module_search *search,
773 struct module_overrides *overrides)
775 struct module *new, **i;
777 new = grab_module(dirname, filename);
778 if (!new)
779 return list;
781 /* Check if module is already in the list. */
782 for (i = &list; *i; i = &(*i)->next) {
784 if (streq((*i)->basename, filename)) {
785 char newpath[strlen(dirname) + strlen("/")
786 + strlen(filename) + 1];
788 sprintf(newpath, "%s/%s", dirname, filename);
790 /* if module matches an existing entry (name) but */
791 /* has a higher priority, replace existing entry. */
792 if (is_higher_priority(newpath, (*i)->pathname,search,
793 overrides)) {
794 del_module(i, *i);
796 new->next = *i;
797 *i = new;
798 } else
799 del_module(NULL, new);
801 return list;
805 /* Not in the list already. Just prepend. */
806 new->next = list;
807 return new;
811 * grab_dir - process a directory of modules
813 * @dirname: directory name
814 * @dir: open directory reference
815 * @do_mod: do_module function to use
816 * @search: path search order
817 * @overrides: module overrides directives
820 static struct module *grab_dir(const char *dirname,
821 DIR *dir,
822 struct module *next,
823 do_module_t do_mod,
824 struct module_search *search,
825 struct module_overrides *overrides)
827 struct dirent *dirent;
829 while ((dirent = readdir(dir)) != NULL) {
830 if (smells_like_module(dirent->d_name))
831 next = do_mod(dirname, dirent->d_name, next,
832 search, overrides);
833 else if (!streq(dirent->d_name, ".")
834 && !streq(dirent->d_name, "..")
835 && !streq(dirent->d_name, "source")
836 && !streq(dirent->d_name, "build")) {
838 DIR *sub;
839 char subdir[strlen(dirname) + 1
840 + strlen(dirent->d_name) + 1];
841 sprintf(subdir, "%s/%s", dirname, dirent->d_name);
842 sub = opendir(subdir);
843 if (sub) {
844 next = grab_dir(subdir, sub, next, do_mod,
845 search, overrides);
846 closedir(sub);
850 return next;
854 * grab_basedir - top-level module processing
856 * @dirname: top-level directory name
857 * @search: path search order
858 * @overrides: module overrides directives
861 static struct module *grab_basedir(const char *dirname,
862 struct module_search *search,
863 struct module_overrides *overrides)
865 DIR *dir;
866 struct module *list;
868 dir = opendir(dirname);
869 if (!dir) {
870 warn("Couldn't open directory %s: %s\n",
871 dirname, strerror(errno));
872 return NULL;
874 list = grab_dir(dirname, dir, NULL, do_module, search, overrides);
875 closedir(dir);
877 return list;
881 * sort_modules - order modules in list on modules.order if available
883 * @dirname: directory name
884 * @list: module list
886 * Using the modules.order file (if available), give every module an index
887 * based on its position in the file, and order list based on the index. If
888 * no ordering data is available, fallback to existing unordered list.
891 static struct module *sort_modules(const char *dirname, struct module *list)
893 struct module *tlist = NULL, **tpos = &tlist;
894 FILE *modorder;
895 int dir_len = strlen(dirname) + 1;
896 char file_name[dir_len + strlen("modules.order") + 1];
897 char line[10240];
898 unsigned int linenum = 0;
900 sprintf(file_name, "%s/%s", dirname, "modules.order");
902 modorder = fopen(file_name, "r");
903 if (!modorder) {
904 /* Older kernels don't generate modules.order. Just
905 return if the file doesn't exist. */
906 if (errno == ENOENT)
907 return list;
908 fatal("Could not open '%s': %s\n", file_name, strerror(errno));
911 sprintf(line, "%s/", dirname);
913 /* move modules listed in modorder file to tlist in order */
914 while (fgets(line, sizeof(line), modorder)) {
915 struct module **pos, *mod;
916 int len = strlen(line);
918 linenum++;
919 if (line[len - 1] == '\n')
920 line[len - 1] = '\0';
922 for (pos = &list; (mod = *pos); pos = &(*pos)->next) {
923 if (streq(line, mod->pathname + dir_len)) {
924 mod->order = linenum;
925 *pos = mod->next;
926 mod->next = NULL;
927 *tpos = mod;
928 tpos = &mod->next;
929 break;
934 /* append the rest */
935 *tpos = list;
937 fclose(modorder);
939 return tlist;
943 * calculate_deps - calculate deps for module
945 * @module: module to process
948 static void calculate_deps(struct module *module)
950 unsigned int i;
951 struct string_table *symnames;
952 struct string_table *symtypes;
953 uint64_t *symvers = NULL;
954 struct elf_file *file;
956 module->num_deps = 0;
957 module->deps = NULL;
958 file = module->file;
960 symnames = file->ops->load_dep_syms(file, &symtypes,
961 check_symvers ? &symvers : NULL);
962 if (!symnames || !symtypes)
963 return;
965 for (i = 0; i < symnames->cnt; i++) {
966 const char *name;
967 uint64_t ver;
968 struct module *owner;
969 int weak;
971 name = symnames->str[i];
972 ver = symvers ? symvers[i] : 0;
973 weak = (*(symtypes->str[i]) == 'W');
974 owner = find_symbol(name, ver, module->pathname, weak);
975 if (owner) {
976 info("%s needs \"%s\": %s\n",
977 module->pathname, name,
978 owner->pathname);
979 add_dep(module, owner);
983 free(symnames);
984 free(symtypes);
985 free(symvers);
989 * parse_modules - process the modules list
991 * @module: module list
993 * Process each module in the (sorted by sort_modules) list for symbols,
994 * dependencies, and other meta-data that will be output later.
997 static struct module *parse_modules(struct module *list)
999 struct module *i;
1000 struct elf_file *file;
1001 struct string_table *syms;
1002 int j;
1004 for (i = list; i; i = i->next) {
1005 uint64_t *symvers = NULL;
1006 file = i->file;
1007 syms = file->ops->load_symbols(file,
1008 check_symvers ? &symvers : NULL);
1009 if (syms) {
1010 for (j = 0; j < syms->cnt; j++)
1011 add_symbol(skip_symprefix(syms->str[j]),
1012 symvers ? symvers[j] : 0, i);
1013 strtbl_free(syms);
1015 free(symvers);
1016 file->ops->fetch_tables(file, &i->tables);
1019 for (i = list; i; i = i->next)
1020 calculate_deps(i);
1022 /* Strip out modules with dependency loops. */
1023 again:
1024 for (i = list; i; i = i->next) {
1025 if (has_dep_loop(i, NULL)) {
1026 warn("Module %s ignored, due to loop\n",
1027 i->pathname + skipchars);
1028 del_module(&list, i);
1029 goto again;
1033 return list;
1037 * output_symbols - output symbol alias information
1039 * @unused: unused
1040 * @out: output file reference
1041 * @dirname: output directory
1043 * Output the symbol hash table, in the form of symbol alias entries, to
1044 * an ascii text file of the form modules.symbols (depending on file).
1047 static int output_symbols(struct module *unused, FILE *out, char *dirname)
1049 unsigned int i;
1051 fprintf(out, "# Aliases for symbols, used by symbol_request().\n");
1052 for (i = 0; i < SYMBOL_HASH_SIZE; i++) {
1053 struct symbol *s;
1055 for (s = symbolhash[i]; s; s = s->next) {
1056 if (s->owner) {
1057 char modname[strlen(s->owner->pathname)+1];
1058 filename2modname(modname, s->owner->pathname);
1059 fprintf(out, "alias symbol:%s %s\n",
1060 s->name, modname);
1064 return 1;
1068 * output_symbols_bin - output symbol alias information in binary format
1070 * @unused: unused
1071 * @out: output file reference
1072 * @dirname: output directory
1074 * Output the symbol hash table, in the form of symbol alias entries, to
1075 * a trie ordered output file e.g. of the form modules.symbols.bin.
1078 static int output_symbols_bin(struct module *unused, FILE *out, char *dirname)
1080 struct index_node *index;
1081 unsigned int i;
1082 char *alias;
1083 int duplicate;
1085 index = index_create();
1087 for (i = 0; i < SYMBOL_HASH_SIZE; i++) {
1088 struct symbol *s;
1090 for (s = symbolhash[i]; s; s = s->next) {
1091 if (s->owner) {
1092 char modname[strlen(s->owner->pathname)+1];
1093 filename2modname(modname, s->owner->pathname);
1094 nofail_asprintf(&alias, "symbol:%s", s->name);
1095 duplicate = index_insert(index, alias, modname,
1096 s->owner->order);
1097 if (duplicate && warn_dups)
1098 warn("duplicate module syms:\n%s %s\n",
1099 alias, modname);
1100 free(alias);
1105 index_write(index, out);
1106 index_destroy(index);
1108 return 1;
1112 * output_builtin_bin - output list of built-in modules in binary format
1114 * @unused: unused
1115 * @out: output file reference
1116 * @dirname: output directory
1119 static int output_builtin_bin(struct module *unused, FILE *out, char *dirname)
1121 struct index_node *index;
1122 char *textfile, *line;
1123 unsigned int linenum;
1124 FILE *f;
1126 nofail_asprintf(&textfile, "%s/modules.builtin", dirname);
1127 if (!(f = fopen(textfile, "r"))) {
1128 if (errno != ENOENT)
1129 fatal("Could not open '%s': %s\n",
1130 textfile, strerror(errno));
1131 free(textfile);
1132 return 0;
1134 free(textfile);
1135 index = index_create();
1137 while ((line = getline_wrapped(f, &linenum)) != NULL) {
1138 char *module = line;
1140 if (!*line || *line == '#') {
1141 free(line);
1142 continue;
1144 filename2modname(module, module);
1145 index_insert(index, module, "", 0);
1146 free(line);
1148 fclose(f);
1149 index_write(index, out);
1150 index_destroy(index);
1152 return 1;
1156 * output_aliases - output list of module aliases
1158 * @modules: list of modules
1159 * @out: output file reference
1160 * @dirname: output directory
1163 static int output_aliases(struct module *modules, FILE *out, char *dirname)
1165 struct module *i;
1166 struct elf_file *file;
1167 struct string_table *tbl;
1168 int j;
1170 fprintf(out, "# Aliases extracted from modules themselves.\n");
1171 for (i = modules; i; i = i->next) {
1172 char modname[strlen(i->pathname)+1];
1174 file = i->file;
1175 filename2modname(modname, i->pathname);
1177 /* Grab from old-style .modalias section. */
1178 tbl = file->ops->load_strings(file, ".modalias", NULL);
1179 for (j = 0; tbl && j < tbl->cnt; j++)
1180 fprintf(out, "alias %s %s\n", tbl->str[j], modname);
1181 strtbl_free(tbl);
1183 /* Grab from new-style .modinfo section. */
1184 tbl = file->ops->load_strings(file, ".modinfo", NULL);
1185 for (j = 0; tbl && j < tbl->cnt; j++) {
1186 const char *p = tbl->str[j];
1187 if (strstarts(p, "alias="))
1188 fprintf(out, "alias %s %s\n",
1189 p + strlen("alias="), modname);
1191 strtbl_free(tbl);
1193 return 1;
1197 * output_aliases_bin - output list of module aliases in binary format
1199 * @modules: list of modules
1200 * @out: outout file reference
1201 * @dirname: output directory
1204 static int output_aliases_bin(struct module *modules, FILE *out, char *dirname)
1206 struct module *i;
1207 struct elf_file *file;
1208 struct string_table *tbl;
1209 int j;
1210 char *alias;
1211 struct index_node *index;
1212 int duplicate;
1214 index = index_create();
1216 for (i = modules; i; i = i->next) {
1217 char modname[strlen(i->pathname)+1];
1219 file = i->file;
1220 filename2modname(modname, i->pathname);
1222 /* Grab from old-style .modalias section. */
1223 tbl = file->ops->load_strings(file, ".modalias", NULL);
1224 for (j = 0; tbl && j < tbl->cnt; j++) {
1225 alias = NOFAIL(strdup(tbl->str[j]));
1226 underscores(alias);
1227 duplicate = index_insert(index, alias, modname, i->order);
1228 if (duplicate && warn_dups)
1229 warn("duplicate module alias:\n%s %s\n",
1230 alias, modname);
1231 free(alias);
1233 strtbl_free(tbl);
1235 /* Grab from new-style .modinfo section. */
1236 tbl = file->ops->load_strings(file, ".modinfo", NULL);
1237 for (j = 0; tbl && j < tbl->cnt; j++) {
1238 const char *p = tbl->str[j];
1239 if (strstarts(p, "alias=")) {
1240 alias = NOFAIL(strdup(p + strlen("alias=")));
1241 underscores(alias);
1242 duplicate = index_insert(index, alias, modname, i->order);
1243 if (duplicate && warn_dups)
1244 warn("duplicate module alias:\n%s %s\n",
1245 alias, modname);
1246 free(alias);
1249 strtbl_free(tbl);
1252 index_write(index, out);
1253 index_destroy(index);
1255 return 1;
1259 * output_softdeps - output module softdeps (non-implicit dependencies)
1261 * @modules: list of modules
1262 * @out: output file reference
1263 * @dirname: output directory
1266 static int output_softdeps(struct module *modules, FILE *out, char *dirname)
1268 struct module *i;
1269 struct elf_file *file;
1270 struct string_table *tbl;
1271 int j;
1273 fprintf(out, "# Soft dependencies extracted from modules themselves.\n");
1274 fprintf(out, "# Copy, with a .conf extension, to /etc/modprobe.d to use "
1275 "it with modprobe.\n");
1276 for (i = modules; i; i = i->next) {
1277 char modname[strlen(i->pathname)+1];
1279 file = i->file;
1280 filename2modname(modname, i->pathname);
1282 /* Grab from new-style .modinfo section. */
1283 tbl = file->ops->load_strings(file, ".modinfo", NULL);
1284 for (j = 0; tbl && j < tbl->cnt; j++) {
1285 const char *p = tbl->str[j];
1286 if (strstarts(p, "softdep="))
1287 fprintf(out, "softdep %s %s\n",
1288 modname, p + strlen("softdep="));
1290 strtbl_free(tbl);
1292 return 1;
1296 * output_devname - output device names required by modules
1298 * @modules: list of modules
1299 * @out: output file reference
1300 * @dirname: output directory
1303 static int output_devname(struct module *modules, FILE *out, char *dirname)
1305 struct module *m;
1307 fprintf(out, "# Device nodes to trigger on-demand module loading.\n");
1308 for (m = modules; m != NULL; m = m->next) {
1309 struct string_table *tbl;
1310 int i;
1311 char type = '\0';
1312 const char *devname = NULL;
1314 tbl = m->file->ops->load_strings(m->file, ".modinfo", NULL);
1315 for (i = 0; tbl && i < tbl->cnt; i++) {
1316 const char *p = tbl->str[i];
1317 unsigned int maj, min;
1319 if (sscanf(p, "alias=char-major-%u-%u", &maj, &min) == 2)
1320 type = 'c';
1321 else if (sscanf(p, "alias=block-major-%u-%u", &maj, &min) == 2)
1322 type = 'b';
1323 else if (strstarts(p, "alias=devname:"))
1324 devname = &p[strlen("alias=devname:")];
1326 if (type && devname) {
1327 char modname[strlen(m->pathname)+1];
1329 filename2modname(modname, m->pathname);
1330 fprintf(out, "%s %s %c%u:%u\n",
1331 modname, devname, type, maj, min);
1332 break;
1335 strtbl_free(tbl);
1337 return 1;
1340 struct depfile {
1341 const char *name;
1342 int (*func)(struct module *, FILE *, char *dirname);
1343 int map_file;
1346 /* The possible output files - those with map_file unset typically not made */
1347 static const struct depfile depfiles[] = {
1348 { "modules.dep", output_deps, 0 }, /* This is what we check for '-A'. */
1349 { "modules.dep.bin", output_deps_bin, 0 },
1350 { "modules.pcimap", output_pci_table, 1 },
1351 { "modules.usbmap", output_usb_table, 1 },
1352 { "modules.ccwmap", output_ccw_table, 1 },
1353 { "modules.ieee1394map", output_ieee1394_table, 1 },
1354 { "modules.isapnpmap", output_isapnp_table, 1 },
1355 { "modules.inputmap", output_input_table, 1 },
1356 { "modules.ofmap", output_of_table, 1 },
1357 { "modules.seriomap", output_serio_table, 1 },
1358 { "modules.alias", output_aliases, 0 },
1359 { "modules.alias.bin", output_aliases_bin, 0 },
1360 { "modules.softdep", output_softdeps, 0 },
1361 { "modules.symbols", output_symbols, 0 },
1362 { "modules.symbols.bin", output_symbols_bin, 0 },
1363 { "modules.builtin.bin", output_builtin_bin, 0 },
1364 { "modules.devname", output_devname, 0 },
1368 * any_modules_newer - determine if modules are newer than ref time
1370 * @dirname: directory to process
1371 * @mtime: comparison time
1373 * The worst case is that we process modules we didn't need to. It is
1374 * therefore safer to go with "true" if we can't figure it out.
1377 static int any_modules_newer(const char *dirname, time_t mtime)
1379 DIR *dir;
1380 struct dirent *dirent;
1382 dir = opendir(dirname);
1383 if (!dir)
1384 return 1;
1386 while ((dirent = readdir(dir)) != NULL) {
1387 struct stat st;
1388 char file[strlen(dirname) + 1 + strlen(dirent->d_name) + 1];
1390 if (streq(dirent->d_name, ".") || streq(dirent->d_name, ".."))
1391 continue;
1393 sprintf(file, "%s/%s", dirname, dirent->d_name);
1394 if (lstat(file, &st) != 0)
1395 goto ret_true;
1397 if (smells_like_module(dirent->d_name)) {
1398 if (st.st_mtime > mtime)
1399 goto ret_true;
1400 } else if (S_ISDIR(st.st_mode)) {
1401 if (any_modules_newer(file, mtime))
1402 goto ret_true;
1405 closedir(dir);
1406 return 0;
1408 ret_true:
1409 closedir(dir);
1410 return 1;
1414 * depfile_out_of_date - check if module dep files are older than any modules
1416 * @dirname: directory to process
1418 * Use any_modules_newer to determine if the dep files are up to date.
1421 static int depfile_out_of_date(const char *dirname)
1423 struct stat st;
1424 char depfile[strlen(dirname) + 1 + strlen(depfiles[0].name) + 1];
1426 sprintf(depfile, "%s/%s", dirname, depfiles[0].name);
1428 if (stat(depfile, &st) != 0)
1429 return 1;
1431 return any_modules_newer(dirname, st.st_mtime);
1435 * strsep_skipspace - skip over delimitors in strings
1437 * @string: string to process
1438 * @delim: delimitor (e.g. ' ')
1441 static char *strsep_skipspace(char **string, char *delim)
1443 if (!*string)
1444 return NULL;
1445 *string += strspn(*string, delim);
1446 return strsep(string, delim);
1450 * add_search - add a new module search path
1452 * @search_path: path to search
1453 * @len: length of path
1454 * @search: list of search paths
1457 static struct module_search *add_search(const char *search_path,
1458 size_t len,
1459 struct module_search *search)
1462 struct module_search *new;
1464 new = NOFAIL(malloc(sizeof(*new)));
1465 new->search_path = NOFAIL(strdup(search_path));
1466 new->len = len;
1467 new->next = search;
1469 return new;
1474 * add_override - add a new module override entry
1476 * @modfile: name of module file
1477 * @overrides: list of override entries
1480 static struct module_overrides *add_override(const char *modfile,
1481 struct module_overrides *overrides)
1484 struct module_overrides *new;
1486 new = NOFAIL(malloc(sizeof(*new)));
1487 new->modfile = NOFAIL(strdup(modfile));
1488 new->next = overrides;
1490 return new;
1494 static int parse_config_scan(const char *filename,
1495 const char *basedir,
1496 const char *kernelversion,
1497 struct module_search **search,
1498 struct module_overrides **overrides);
1501 * parse_config_file - process an individual configuration file
1503 * @filename: name of config file
1504 * @basedir: module base directory
1505 * @kernelversion: kernel version to process
1506 * @search: search path order
1507 * @overrides: module override entries
1510 static int parse_config_file(const char *filename,
1511 const char *basedir,
1512 const char *kernelversion,
1513 struct module_search **search,
1514 struct module_overrides **overrides)
1516 char *line;
1517 unsigned int linenum = 0;
1518 FILE *cfile;
1520 cfile = fopen(filename, "r");
1521 if (!cfile) {
1522 if (errno != ENOENT)
1523 fatal("could not open '%s', reason: %s\n", filename,
1524 strerror(errno));
1525 return 0;
1528 while ((line = getline_wrapped(cfile, &linenum)) != NULL) {
1529 char *ptr = line;
1530 char *cmd, *modname;
1532 cmd = strsep_skipspace(&ptr, "\t ");
1534 if (cmd == NULL || cmd[0] == '#' || cmd[0] == '\0') {
1535 free(line);
1536 continue;
1539 if (streq(cmd, "search")) {
1540 char *search_path;
1542 while ((search_path = strsep_skipspace(&ptr, "\t "))) {
1543 char *dirname;
1544 size_t len;
1546 if (strcmp(search_path,
1547 MODULE_BUILTIN_KEY) == 0) {
1548 *search = add_search(MODULE_BUILTIN_KEY,
1549 0, *search);
1550 continue;
1552 nofail_asprintf(&dirname, "%s%s%s/%s", basedir,
1553 MODULE_DIR, kernelversion, search_path);
1554 len = strlen(dirname);
1555 *search = add_search(dirname, len, *search);
1556 free(dirname);
1558 } else if (streq(cmd, "override")) {
1559 char *pathname = NULL, *version, *subdir;
1560 modname = strsep_skipspace(&ptr, "\t ");
1561 version = strsep_skipspace(&ptr, "\t ");
1562 subdir = strsep_skipspace(&ptr, "\t ");
1564 if (!regex_match(kernelversion, (const char *)version))
1565 continue;
1567 nofail_asprintf(&pathname, "%s%s%s/%s/%s.ko", basedir,
1568 MODULE_DIR, kernelversion, subdir, modname);
1570 *overrides = add_override(pathname, *overrides);
1571 free(pathname);
1572 } else if (streq(cmd, "include")) {
1573 char *newfilename;
1575 newfilename = strsep_skipspace(&ptr, "\t ");
1576 if (!newfilename) {
1577 grammar(cmd, filename, linenum);
1578 } else {
1579 warn("\"include %s\" is deprecated, "
1580 "please use /etc/depmod.d\n", newfilename);
1581 if (strstarts(newfilename, "/etc/depmod.d")) {
1582 warn("\"include /etc/depmod.d\" is "
1583 "the default, ignored\n");
1584 } else {
1585 if (!parse_config_scan(newfilename, basedir,
1586 kernelversion,
1587 search, overrides))
1588 warn("Failed to open included"
1589 " config file %s: %s\n",
1590 newfilename, strerror(errno));
1593 } else if (streq(cmd, "make_map_files")) {
1594 char *option;
1596 option = strsep_skipspace(&ptr, "\t ");
1597 if (!option)
1598 grammar(cmd, filename, linenum);
1599 else {
1600 if (streq(option, "yes"))
1601 make_map_files = 1;
1602 else if (streq(option, "no"))
1603 make_map_files = 0;
1604 else
1605 grammar(cmd, filename, linenum);
1607 } else
1608 grammar(cmd, filename, linenum);
1610 free(line);
1612 fclose(cfile);
1613 return 1;
1617 * parse_config_scan - handle a directory of config files
1619 * @filename: name of directory
1620 * @basedir: module base directory
1621 * @kernelversion: kernel version to process
1622 * @search: search path order
1623 * @overrides: module override entries
1626 static int parse_config_scan(const char *filename,
1627 const char *basedir,
1628 const char *kernelversion,
1629 struct module_search **search,
1630 struct module_overrides **overrides)
1632 DIR *dir;
1633 int ret = 0;
1635 dir = opendir(filename);
1636 if (dir) {
1637 struct file_entry {
1638 struct list_head node;
1639 char name[];
1641 LIST_HEAD(files_list);
1642 struct file_entry *fe, *fe_tmp;
1643 struct dirent *i;
1645 /* sort files from directory into list */
1646 while ((i = readdir(dir)) != NULL) {
1647 size_t len;
1649 if (i->d_name[0] == '.')
1650 continue;
1651 if (!config_filter(i->d_name))
1652 continue;
1654 len = strlen(i->d_name);
1655 if (len < 6 || strcmp(&i->d_name[len-5], ".conf") != 0)
1656 warn("All config files need .conf: %s/%s, "
1657 "it will be ignored in a future release.\n",
1658 filename, i->d_name);
1659 fe = malloc(sizeof(struct file_entry) + len + 1);
1660 if (fe == NULL)
1661 continue;
1662 strcpy(fe->name, i->d_name);
1663 list_for_each_entry(fe_tmp, &files_list, node)
1664 if (strcmp(fe_tmp->name, fe->name) >= 0)
1665 break;
1666 list_add_tail(&fe->node, &fe_tmp->node);
1668 closedir(dir);
1670 /* parse list of files */
1671 list_for_each_entry_safe(fe, fe_tmp, &files_list, node) {
1672 char *cfgfile;
1674 nofail_asprintf(&cfgfile, "%s/%s", filename, fe->name);
1675 if (!parse_config_file(cfgfile, basedir, kernelversion,
1676 search, overrides))
1677 warn("Failed to open config file "
1678 "%s: %s\n", fe->name, strerror(errno));
1679 free(cfgfile);
1680 list_del(&fe->node);
1681 free(fe);
1684 ret = 1;
1685 } else {
1686 if (parse_config_file(filename, basedir, kernelversion, search,
1687 overrides))
1688 ret = 1;
1691 return ret;
1695 * parse_toplevel_config - handle top-level depmod.conf, depmod.d
1697 * @filename: possibly overridden config
1698 * @basedir: module base directory
1699 * @kernelversion: kernel version to process
1700 * @search: search path order
1701 * @overrides: module override entries
1704 static void parse_toplevel_config(const char *filename,
1705 const char *basedir,
1706 const char *kernelversion,
1707 struct module_search **search,
1708 struct module_overrides **overrides)
1710 if (filename) {
1711 if (!parse_config_scan(filename, basedir, kernelversion, search,
1712 overrides))
1713 fatal("Failed to open config file %s: %s\n",
1714 filename, strerror(errno));
1715 return;
1718 /* deprecated config file */
1719 if (parse_config_file("/etc/depmod.conf", basedir, kernelversion,
1720 search, overrides) > 0)
1721 warn("Deprecated config file /etc/depmod.conf, "
1722 "all config files belong into /etc/depmod.d/.\n");
1724 /* default config */
1725 parse_config_scan("/etc/depmod.d", basedir, kernelversion,
1726 search, overrides);
1729 /* Local to main, but not freed on exit. Keep valgrind quiet. */
1730 static struct module *list = NULL;
1731 static struct module_search *search = NULL;
1732 static struct module_overrides *overrides = NULL;
1734 int main(int argc, char *argv[])
1736 int opt, all = 0, maybe_all = 0, doing_stdout = 0;
1737 char *basedir = "", *dirname, *version;
1738 char *system_map = NULL, *module_symvers = NULL;
1739 int i;
1740 const char *config = NULL;
1742 if (native_endianness() == 0)
1743 abort();
1745 while ((opt = getopt_long(argc, argv, "aAb:C:E:F:euqrvnP:hVwm", options, NULL))
1746 != -1) {
1747 switch (opt) {
1748 case 'a':
1749 all = 1;
1750 break;
1751 case 'A':
1752 maybe_all = 1;
1753 break;
1754 case 'b':
1755 basedir = optarg;
1756 skipchars = strlen(basedir);
1757 break;
1758 case 'C':
1759 config = optarg;
1760 break;
1761 case 'E':
1762 module_symvers = optarg;
1763 check_symvers = 1;
1764 break;
1765 case 'F':
1766 system_map = optarg;
1767 break;
1768 case 'e':
1769 print_unknown = 1;
1770 break;
1771 case 'u':
1772 case 'q':
1773 case 'r':
1774 break;
1775 case 'v':
1776 verbose = 1;
1777 break;
1778 case 'n':
1779 doing_stdout = 1;
1780 break;
1781 case 'P':
1782 if (optarg[1] != '\0')
1783 fatal("-P only takes a single char\n");
1784 sym_prefix = optarg[0];
1785 break;
1786 case 'h':
1787 print_usage(argv[0]);
1788 exit(0);
1789 break;
1790 case 'V':
1791 printf("%s %s\n", PACKAGE, VERSION);
1792 exit(0);
1793 case 'w':
1794 warn_dups = 1;
1795 break;
1796 case 'm':
1797 force_map_files = 1;
1798 break;
1799 default:
1800 print_usage(argv[0]);
1801 exit(1);
1805 if (module_symvers)
1806 load_module_symvers(module_symvers);
1807 else if (system_map)
1808 load_system_map(system_map);
1809 else if (print_unknown) {
1810 warn("-e needs -E or -F\n");
1811 print_unknown = 0;
1814 /* They can specify the version naked on the command line */
1815 if (optind < argc && is_version_number(argv[optind])) {
1816 version = NOFAIL(strdup(argv[optind]));
1817 optind++;
1818 } else {
1819 struct utsname buf;
1820 uname(&buf);
1821 version = NOFAIL(strdup(buf.release));
1824 /* Check for old version. */
1825 if (old_module_version(version)) {
1826 fprintf(stderr, "Kernel version %s requires old depmod\n",
1827 version);
1828 exit(2);
1831 /* Depmod -a by default if no names. */
1832 if (optind == argc)
1833 all = 1;
1835 nofail_asprintf(&dirname, "%s%s%s", basedir, MODULE_DIR, version);
1837 if (maybe_all) {
1838 if (!doing_stdout && !depfile_out_of_date(dirname))
1839 exit(0);
1840 all = 1;
1843 parse_toplevel_config(config, basedir, version, &search, &overrides);
1845 /* For backward compatibility add "updates" to the head of the search
1846 * list here. But only if there was no "search" option specified.
1848 if (!search) {
1849 char *dirname;
1850 size_t len;
1852 nofail_asprintf(&dirname, "%s%s%s/updates", basedir,
1853 MODULE_DIR, version);
1854 len = strlen(dirname);
1855 search = add_search(dirname, len, search);
1857 if (!all) {
1858 /* Do command line args. */
1859 for (opt = optind; opt < argc; opt++) {
1860 struct module *new;
1862 if (argv[opt][0] != '/')
1863 fatal("modules must be specified using absolute paths.\n"
1864 "\"%s\" is a relative path\n", argv[opt]);
1866 new = grab_module(NULL, argv[opt]);
1867 if (!new) {
1868 /* cmd-line specified modules must exist */
1869 fatal("grab_module() failed for module %s\n", argv[opt]);
1871 new->next = list;
1872 list = new;
1874 } else {
1875 list = grab_basedir(dirname,search,overrides);
1877 list = sort_modules(dirname,list);
1878 list = parse_modules(list);
1880 for (i = 0; i < ARRAY_SIZE(depfiles); i++) {
1881 FILE *out;
1882 int res;
1883 const struct depfile *d = &depfiles[i];
1884 char depname[strlen(dirname) + 1 + strlen(d->name) + 1];
1885 char tmpname[strlen(dirname) + 1 + strlen(d->name) +
1886 strlen(".temp") + 1];
1888 if (d->map_file && !make_map_files && !force_map_files)
1889 continue;
1891 sprintf(depname, "%s/%s", dirname, d->name);
1892 sprintf(tmpname, "%s/%s.temp", dirname, d->name);
1893 if (!doing_stdout) {
1894 out = fopen(tmpname, "w");
1895 if (!out)
1896 fatal("Could not open %s for writing: %s\n",
1897 tmpname, strerror(errno));
1898 } else {
1899 out = stdout;
1900 if (ends_in(depname, ".bin"))
1901 continue;
1903 res = d->func(list, out, dirname);
1904 if (doing_stdout)
1905 continue;
1906 fclose(out);
1907 if (res) {
1908 if (rename(tmpname, depname) < 0)
1909 fatal("Could not rename %s into %s: %s\n",
1910 tmpname, depname, strerror(errno));
1911 } else {
1912 if (unlink(tmpname) < 0)
1913 warn("Could not delete %s: %s\n",
1914 tmpname, strerror(errno));
1918 free(dirname);
1919 free(version);
1921 return 0;