git: ignore libmodtools.a
[mit.git] / depmod.c
blob4c8caa0f59336721492354d5006a5689bc902d4c
1 /* New simplified depmod without backwards compat stuff and not
2 requiring ksyms.
4 (C) 2002 Rusty Russell IBM Corporation
5 */
6 #define _GNU_SOURCE /* asprintf */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <getopt.h>
11 #include <string.h>
12 #include <errno.h>
13 #include <unistd.h>
14 #include <elf.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <dirent.h>
19 #include <sys/utsname.h>
20 #include <sys/mman.h>
22 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
24 #include "util.h"
25 #include "zlibsupport.h"
26 #include "depmod.h"
27 #include "logging.h"
28 #include "index.h"
29 #include "elfops.h"
30 #include "tables.h"
31 #include "config_filter.h"
33 #include "testing.h"
35 #ifndef MODULE_DIR
36 #define MODULE_DIR "/lib/modules/"
37 #endif
39 #ifndef MODULE_BUILTIN_KEY
40 #define MODULE_BUILTIN_KEY "built-in"
41 #endif
43 struct module_overrides
45 /* Next override */
46 struct module_overrides *next;
48 /* overridden module */
49 char *modfile;
52 struct module_search
54 /* Next search */
55 struct module_search *next;
57 /* search path */
58 char *search_path;
59 size_t len;
62 static unsigned int skipchars;
63 static unsigned int make_map_files = 1; /* default to on */
64 static unsigned int force_map_files = 0; /* default to on */
66 #define SYMBOL_HASH_SIZE 1024
67 struct symbol
69 struct symbol *next;
70 struct module *owner;
71 uint64_t ver;
72 char name[0];
75 static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
77 /* This is based on the hash agorithm from gdbm, via tdb */
78 static inline unsigned int tdb_hash(const char *name)
80 unsigned value; /* Used to compute the hash value. */
81 unsigned i; /* Used to cycle through random values. */
83 /* Set the initial value from the key size. */
84 for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
85 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
87 return (1103515243 * value + 12345);
90 void add_symbol(const char *name, uint64_t ver, struct module *owner)
92 unsigned int hash;
93 struct symbol *new = NOFAIL(malloc(sizeof *new + strlen(name) + 1));
95 new->owner = owner;
96 new->ver = ver;
97 strcpy(new->name, name);
99 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
100 new->next = symbolhash[hash];
101 symbolhash[hash] = new;
104 static int print_unknown, check_symvers;
106 struct module *find_symbol(const char *name, uint64_t ver,
107 const char *modname, int weak)
109 struct symbol *s;
111 /* For our purposes, .foo matches foo. PPC64 needs this. */
112 if (name[0] == '.')
113 name++;
115 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) {
116 if (streq(s->name, name))
117 break;
119 if (s) {
120 if (ver && s->ver && s->ver != ver && print_unknown && !weak)
121 warn("%s disagrees about version of symbol %s\n",
122 modname, name);
123 return s->owner;
126 if (print_unknown && !weak)
127 warn("%s needs unknown symbol %s\n", modname, name);
129 return NULL;
132 void add_dep(struct module *mod, struct module *depends_on)
134 unsigned int i;
136 for (i = 0; i < mod->num_deps; i++)
137 if (mod->deps[i] == depends_on)
138 return;
140 mod->deps = NOFAIL(realloc(mod->deps, sizeof(mod->deps[0])*(mod->num_deps+1)));
141 mod->deps[mod->num_deps++] = depends_on;
144 static void add_fake_syms(void)
146 /* __this_module is magic inserted by kernel loader. */
147 add_symbol("__this_module", 0, NULL);
148 /* On S390, this is faked up too */
149 add_symbol("_GLOBAL_OFFSET_TABLE_", 0, NULL);
152 static void load_system_map(const char *filename)
154 FILE *system_map;
155 char line[10240];
156 const char ksymstr[] = "__ksymtab_";
157 const int ksymstr_len = strlen(ksymstr);
159 system_map = fopen(filename, "r");
160 if (!system_map)
161 fatal("Could not open '%s': %s\n", filename, strerror(errno));
163 /* eg. c0294200 R __ksymtab_devfs_alloc_devnum */
164 while (fgets(line, sizeof(line)-1, system_map)) {
165 char *ptr;
167 /* Snip \n */
168 ptr = strchr(line, '\n');
169 if (ptr)
170 *ptr = '\0';
172 ptr = strchr(line, ' ');
173 if (!ptr || !(ptr = strchr(ptr + 1, ' ')))
174 continue;
176 /* Covers gpl-only and normal symbols. */
177 if (strstarts(ptr+1, ksymstr))
178 add_symbol(ptr+1+ksymstr_len, 0, NULL);
181 fclose(system_map);
182 add_fake_syms();
185 static void load_module_symvers(const char *filename)
187 FILE *module_symvers;
188 char line[10240];
190 module_symvers = fopen(filename, "r");
191 if (!module_symvers)
192 fatal("Could not open '%s': %s\n", filename, strerror(errno));
194 /* eg. "0xb352177e\tfind_first_bit\tvmlinux\tEXPORT_SYMBOL" */
195 while (fgets(line, sizeof(line)-1, module_symvers)) {
196 const char *ver, *sym, *where;
198 ver = strtok(line, " \t");
199 sym = strtok(NULL, " \t");
200 where = strtok(NULL, " \t");
201 if (!ver || !sym || !where)
202 continue;
204 if (streq(where, "vmlinux"))
205 add_symbol(sym, strtoull(ver, NULL, 16), NULL);
208 fclose(module_symvers);
209 add_fake_syms();
212 static struct option options[] = { { "all", 0, NULL, 'a' },
213 { "quick", 0, NULL, 'A' },
214 { "basedir", 1, NULL, 'b' },
215 { "config", 1, NULL, 'C' },
216 { "symvers", 1, NULL, 'E' },
217 { "filesyms", 1, NULL, 'F' },
218 { "errsyms", 0, NULL, 'e' },
219 { "unresolved-error", 0, NULL, 'u' },
220 { "quiet", 0, NULL, 'q' },
221 { "root", 0, NULL, 'r' },
222 { "verbose", 0, NULL, 'v' },
223 { "show", 0, NULL, 'n' },
224 { "dry-run", 0, NULL, 'n' },
225 { "help", 0, NULL, 'h' },
226 { "version", 0, NULL, 'V' },
227 { "warn", 0, NULL, 'w' },
228 { "map", 0, NULL, 'm' },
229 { NULL, 0, NULL, 0 } };
231 /* Version number or module name? Don't assume extension. */
232 static int is_version_number(const char *version)
234 unsigned int dummy;
236 return (sscanf(version, "%u.%u.%u", &dummy, &dummy, &dummy) == 3);
239 static int old_module_version(const char *version)
241 /* Expect three part version. */
242 unsigned int major, sub, minor;
244 sscanf(version, "%u.%u.%u", &major, &sub, &minor);
246 if (major > 2) return 0;
247 if (major < 2) return 1;
249 /* 2.x */
250 if (sub > 5) return 0;
251 if (sub < 5) return 1;
253 /* 2.5.x */
254 if (minor >= 48) return 0;
255 return 1;
258 static void print_usage(const char *name)
260 fprintf(stderr,
261 "%s " VERSION " -- part of " PACKAGE "\n"
262 "%s -[aA] [-n -e -v -q -V -r -u -w -m]\n"
263 " [-b basedirectory] [forced_version]\n"
264 "depmod [-n -e -v -q -r -u -w] [-F kernelsyms] module1.ko module2.ko ...\n"
265 "If no arguments (except options) are given, \"depmod -a\" is assumed\n"
266 "\n"
267 "depmod will output a dependancy list suitable for the modprobe utility.\n"
268 "\n"
269 "\n"
270 "Options:\n"
271 "\t-a, --all Probe all modules\n"
272 "\t-A, --quick Only does the work if there's a new module\n"
273 "\t-e, --errsyms Report not supplied symbols\n"
274 "\t-m, --map Create the legacy map files\n"
275 "\t-n, --show Write the dependency file on stdout only\n"
276 "\t-V, --version Print the release version\n"
277 "\t-v, --verbose Enable verbose mode\n"
278 "\t-w, --warn Warn on duplicates\n"
279 "\t-h, --help Print this usage message\n"
280 "\n"
281 "The following options are useful for people managing distributions:\n"
282 "\t-b basedirectory\n"
283 "\t --basedir basedirectory Use an image of a module tree.\n"
284 "\t-F kernelsyms\n"
285 "\t --filesyms kernelsyms Use the file instead of the\n"
286 "\t current kernel symbols.\n"
287 "\t-E Module.symvers\n"
288 "\t --symvers Module.symvers Use Module.symvers file to check\n"
289 "\t symbol versions.\n",
290 "depmod", "depmod");
293 static int ends_in(const char *name, const char *ext)
295 unsigned int namelen, extlen;
297 /* Grab lengths */
298 namelen = strlen(name);
299 extlen = strlen(ext);
301 if (namelen < extlen) return 0;
303 if (streq(name + namelen - extlen, ext))
304 return 1;
305 return 0;
308 static struct module *grab_module(const char *dirname, const char *filename)
310 struct module *new;
312 new = NOFAIL(malloc(sizeof(*new)
313 + strlen(dirname?:"") + 1 + strlen(filename) + 1));
314 if (dirname)
315 sprintf(new->pathname, "%s/%s", dirname, filename);
316 else
317 strcpy(new->pathname, filename);
318 new->basename = my_basename(new->pathname);
320 INIT_LIST_HEAD(&new->dep_list);
321 new->order = INDEX_PRIORITY_MIN;
323 new->file = grab_elf_file(new->pathname);
324 if (!new->file) {
325 warn("Can't read module %s: %s\n",
326 new->pathname, strerror(errno));
327 free(new);
328 return NULL;
330 return new;
333 struct module_traverse
335 struct module_traverse *prev;
336 struct module *mod;
339 static int in_loop(struct module *mod, const struct module_traverse *traverse)
341 const struct module_traverse *i;
343 for (i = traverse; i; i = i->prev) {
344 if (i->mod == mod)
345 return 1;
347 return 0;
350 /* Assume we are doing all the modules, so only report each loop once. */
351 static void report_loop(const struct module *mod,
352 const struct module_traverse *traverse)
354 const struct module_traverse *i;
356 /* Check that start is least alphabetically. eg. a depends
357 on b depends on a will get reported for a, not b. */
358 for (i = traverse->prev; i->prev; i = i->prev) {
359 if (strcmp(mod->pathname, i->mod->pathname) > 0)
360 return;
363 /* Is start in the loop? If not, don't report now. eg. a
364 depends on b which depends on c which depends on b. Don't
365 report when generating depends for a. */
366 if (mod != i->mod)
367 return;
369 warn("Loop detected: %s ", mod->pathname);
370 for (i = traverse->prev; i->prev; i = i->prev)
371 fprintf(stderr, "needs %s ", i->mod->basename);
372 fprintf(stderr, "which needs %s again!\n", i->mod->basename);
375 /* This is damn slow, but loops actually happen, and we don't want to
376 just exit() and leave the user without any modules. */
377 static int has_dep_loop(struct module *module, struct module_traverse *prev)
379 unsigned int i;
380 struct module_traverse traverse = { .prev = prev, .mod = module };
382 if (in_loop(module, prev)) {
383 report_loop(module, &traverse);
384 return 1;
387 for (i = 0; i < module->num_deps; i++)
388 if (has_dep_loop(module->deps[i], &traverse))
389 return 1;
390 return 0;
393 /* Uniquifies and orders a dependency list. */
394 static void order_dep_list(struct module *start, struct module *mod)
396 unsigned int i;
398 for (i = 0; i < mod->num_deps; i++) {
399 /* If it was previously depended on, move it to the
400 tail. ie. if a needs b and c, and c needs b, we
401 must order b after c. */
402 list_del(&mod->deps[i]->dep_list);
403 list_add_tail(&mod->deps[i]->dep_list, &start->dep_list);
404 order_dep_list(start, mod->deps[i]);
408 static struct module *deleted = NULL;
410 static void del_module(struct module **modules, struct module *delme)
412 struct module **i;
414 /* Find pointer to it. */
415 if (modules) {
416 for (i = modules; *i != delme; i = &(*i)->next);
418 *i = delme->next;
421 /* Save on a list to quiet valgrind.
422 Can't free - other modules may depend on them */
423 delme->next = deleted;
424 deleted = delme;
427 /* convert to relative path if possible */
428 static const char *compress_path(const char *path, const char *basedir)
430 int len = strlen(basedir);
432 if (strncmp(path, basedir, len) == 0)
433 path += len + 1;
434 return path;
437 static void output_deps(struct module *modules,
438 FILE *out, char *dirname)
440 struct module *i;
442 for (i = modules; i; i = i->next) {
443 struct list_head *j, *tmp;
444 order_dep_list(i, i);
446 fprintf(out, "%s:", compress_path(i->pathname, dirname));
447 list_for_each_safe(j, tmp, &i->dep_list) {
448 struct module *dep
449 = list_entry(j, struct module, dep_list);
450 fprintf(out, " %s",
451 compress_path(dep->pathname, dirname));
452 list_del_init(j);
454 fprintf(out, "\n");
458 /* warn whenever duplicate module aliases, deps, or symbols are found. */
459 int warn_dups = 0;
461 static void output_deps_bin(struct module *modules,
462 FILE *out, char *dirname)
464 struct module *i;
465 struct index_node *index;
466 char *line;
467 char *p;
469 index = index_create();
471 for (i = modules; i; i = i->next) {
472 struct list_head *j, *tmp;
473 char modname[strlen(i->pathname)+1];
475 order_dep_list(i, i);
477 filename2modname(modname, i->pathname);
478 nofail_asprintf(&line, "%s:",
479 compress_path(i->pathname, dirname));
480 p = line;
481 list_for_each_safe(j, tmp, &i->dep_list) {
482 struct module *dep
483 = list_entry(j, struct module, dep_list);
484 nofail_asprintf(&line, "%s %s",
486 compress_path(dep->pathname, dirname));
487 free(p);
488 p = line;
489 list_del_init(j);
491 if (index_insert(index, modname, line, i->order) && warn_dups)
492 warn("duplicate module deps:\n%s\n",line);
493 free(line);
496 index_write(index, out);
497 index_destroy(index);
501 static int smells_like_module(const char *name)
503 return ends_in(name,".ko") || ends_in(name, ".ko.gz");
506 typedef struct module *(*do_module_t)(const char *dirname,
507 const char *filename,
508 struct module *next,
509 struct module_search *search,
510 struct module_overrides *overrides);
512 static int is_higher_priority(const char *newpath, const char *oldpath,
513 struct module_search *search,
514 struct module_overrides *overrides)
516 struct module_search *tmp;
517 struct module_overrides *ovtmp;
518 int i = 0;
519 int prio_builtin = -1;
520 int prio_new = -1;
521 int prio_old = -1;
523 /* The names already match, now we check for overrides and directory search
524 * order
526 for (ovtmp = overrides; ovtmp != NULL; ovtmp = ovtmp->next) {
527 if (streq(ovtmp->modfile, newpath))
528 return 1;
529 if (streq(ovtmp->modfile, oldpath))
530 return 0;
532 for (i = 0, tmp = search; tmp != NULL; tmp = tmp->next, i++) {
533 if (streq(tmp->search_path, MODULE_BUILTIN_KEY))
534 prio_builtin = i;
535 else if (strncmp(tmp->search_path, newpath, tmp->len) == 0)
536 prio_new = i;
537 else if (strncmp(tmp->search_path, oldpath, tmp->len) == 0)
538 prio_old = i;
540 if (prio_new < 0)
541 prio_new = prio_builtin;
542 if (prio_old < 0)
543 prio_old = prio_builtin;
545 return prio_new > prio_old;
549 static struct module *do_module(const char *dirname,
550 const char *filename,
551 struct module *list,
552 struct module_search *search,
553 struct module_overrides *overrides)
555 struct module *new, **i;
557 new = grab_module(dirname, filename);
558 if (!new)
559 return list;
561 /* Check if module is already in the list. */
562 for (i = &list; *i; i = &(*i)->next) {
564 if (streq((*i)->basename, filename)) {
565 char newpath[strlen(dirname) + strlen("/")
566 + strlen(filename) + 1];
568 sprintf(newpath, "%s/%s", dirname, filename);
570 if (is_higher_priority(newpath, (*i)->pathname,search,
571 overrides)) {
572 del_module(i, *i);
574 new->next = *i;
575 *i = new;
576 } else
577 del_module(NULL, new);
579 return list;
583 /* Not in the list already. Just prepend. */
584 new->next = list;
585 return new;
588 static struct module *grab_dir(const char *dirname,
589 DIR *dir,
590 struct module *next,
591 do_module_t do_mod,
592 struct module_search *search,
593 struct module_overrides *overrides)
595 struct dirent *dirent;
597 while ((dirent = readdir(dir)) != NULL) {
598 if (smells_like_module(dirent->d_name))
599 next = do_mod(dirname, dirent->d_name, next,
600 search, overrides);
601 else if (!streq(dirent->d_name, ".")
602 && !streq(dirent->d_name, "..")
603 && !streq(dirent->d_name, "source")
604 && !streq(dirent->d_name, "build")) {
606 DIR *sub;
607 char subdir[strlen(dirname) + 1
608 + strlen(dirent->d_name) + 1];
609 sprintf(subdir, "%s/%s", dirname, dirent->d_name);
610 sub = opendir(subdir);
611 if (sub) {
612 next = grab_dir(subdir, sub, next, do_mod,
613 search, overrides);
614 closedir(sub);
618 return next;
621 static struct module *grab_basedir(const char *dirname,
622 struct module_search *search,
623 struct module_overrides *overrides)
625 DIR *dir;
626 struct module *list;
628 dir = opendir(dirname);
629 if (!dir) {
630 warn("Couldn't open directory %s: %s\n",
631 dirname, strerror(errno));
632 return NULL;
634 list = grab_dir(dirname, dir, NULL, do_module, search, overrides);
635 closedir(dir);
637 return list;
640 static struct module *sort_modules(const char *dirname, struct module *list)
642 struct module *tlist = NULL, **tpos = &tlist;
643 FILE *modorder;
644 int dir_len = strlen(dirname) + 1;
645 char file_name[dir_len + strlen("modules.order") + 1];
646 char line[10240];
647 unsigned int linenum = 0;
649 sprintf(file_name, "%s/%s", dirname, "modules.order");
651 modorder = fopen(file_name, "r");
652 if (!modorder) {
653 /* Older kernels don't generate modules.order. Just
654 return if the file doesn't exist. */
655 if (errno == ENOENT)
656 return list;
657 fatal("Could not open '%s': %s\n", file_name, strerror(errno));
660 sprintf(line, "%s/", dirname);
662 /* move modules listed in modorder file to tlist in order */
663 while (fgets(line, sizeof(line), modorder)) {
664 struct module **pos, *mod;
665 int len = strlen(line);
667 linenum++;
668 if (line[len - 1] == '\n')
669 line[len - 1] = '\0';
671 for (pos = &list; (mod = *pos); pos = &(*pos)->next) {
672 if (streq(line, mod->pathname + dir_len)) {
673 mod->order = linenum;
674 *pos = mod->next;
675 mod->next = NULL;
676 *tpos = mod;
677 tpos = &mod->next;
678 break;
683 /* append the rest */
684 *tpos = list;
686 fclose(modorder);
688 return tlist;
691 /* Calculate the dependencies for this module */
692 static void calculate_deps(struct module *module)
694 unsigned int i;
695 struct string_table *symnames;
696 struct string_table *symtypes;
697 uint64_t *symvers = NULL;
698 struct elf_file *file;
700 module->num_deps = 0;
701 module->deps = NULL;
702 file = module->file;
704 symnames = file->ops->load_dep_syms(file, &symtypes,
705 check_symvers ? &symvers : NULL);
706 if (!symnames || !symtypes)
707 return;
709 for (i = 0; i < symnames->cnt; i++) {
710 const char *name;
711 uint64_t ver;
712 struct module *owner;
713 int weak;
715 name = symnames->str[i];
716 ver = symvers ? symvers[i] : 0;
717 weak = (*(symtypes->str[i]) == 'W');
718 owner = find_symbol(name, ver, module->pathname, weak);
719 if (owner) {
720 info("%s needs \"%s\": %s\n",
721 module->pathname, name,
722 owner->pathname);
723 add_dep(module, owner);
727 free(symnames);
728 free(symtypes);
729 free(symvers);
732 static struct module *parse_modules(struct module *list)
734 struct module *i;
735 struct elf_file *file;
736 struct string_table *syms;
737 int j;
739 for (i = list; i; i = i->next) {
740 uint64_t *symvers = NULL;
741 file = i->file;
742 syms = file->ops->load_symbols(file,
743 check_symvers ? &symvers : NULL);
744 if (syms) {
745 for (j = 0; j < syms->cnt; j++)
746 add_symbol(syms->str[j],
747 symvers ? symvers[j] : 0, i);
748 strtbl_free(syms);
750 free(symvers);
751 file->ops->fetch_tables(file, &i->tables);
754 for (i = list; i; i = i->next)
755 calculate_deps(i);
757 /* Strip out modules with dependency loops. */
758 again:
759 for (i = list; i; i = i->next) {
760 if (has_dep_loop(i, NULL)) {
761 warn("Module %s ignored, due to loop\n",
762 i->pathname + skipchars);
763 del_module(&list, i);
764 goto again;
768 return list;
771 /* Simply dump hash table. */
772 static void output_symbols(struct module *unused, FILE *out, char *dirname)
774 unsigned int i;
776 fprintf(out, "# Aliases for symbols, used by symbol_request().\n");
777 for (i = 0; i < SYMBOL_HASH_SIZE; i++) {
778 struct symbol *s;
780 for (s = symbolhash[i]; s; s = s->next) {
781 if (s->owner) {
782 char modname[strlen(s->owner->pathname)+1];
783 filename2modname(modname, s->owner->pathname);
784 fprintf(out, "alias symbol:%s %s\n",
785 s->name, modname);
791 static void output_symbols_bin(struct module *unused, FILE *out, char *dirname)
793 struct index_node *index;
794 unsigned int i;
795 char *alias;
796 int duplicate;
798 index = index_create();
800 for (i = 0; i < SYMBOL_HASH_SIZE; i++) {
801 struct symbol *s;
803 for (s = symbolhash[i]; s; s = s->next) {
804 if (s->owner) {
805 char modname[strlen(s->owner->pathname)+1];
806 filename2modname(modname, s->owner->pathname);
807 nofail_asprintf(&alias, "symbol:%s", s->name);
808 duplicate = index_insert(index, alias, modname,
809 s->owner->order);
810 if (duplicate && warn_dups)
811 warn("duplicate module syms:\n%s %s\n",
812 alias, modname);
813 free(alias);
818 index_write(index, out);
819 index_destroy(index);
822 static void output_aliases(struct module *modules, FILE *out, char *dirname)
824 struct module *i;
825 struct elf_file *file;
826 struct string_table *tbl;
827 int j;
829 fprintf(out, "# Aliases extracted from modules themselves.\n");
830 for (i = modules; i; i = i->next) {
831 char modname[strlen(i->pathname)+1];
833 file = i->file;
834 filename2modname(modname, i->pathname);
836 /* Grab from old-style .modalias section. */
837 tbl = file->ops->load_strings(file, ".modalias", NULL, fatal);
838 for (j = 0; tbl && j < tbl->cnt; j++)
839 fprintf(out, "alias %s %s\n", tbl->str[j], modname);
840 strtbl_free(tbl);
842 /* Grab from new-style .modinfo section. */
843 tbl = file->ops->load_strings(file, ".modinfo", NULL, fatal);
844 for (j = 0; tbl && j < tbl->cnt; j++) {
845 const char *p = tbl->str[j];
846 if (strstarts(p, "alias="))
847 fprintf(out, "alias %s %s\n",
848 p + strlen("alias="), modname);
850 strtbl_free(tbl);
854 static void output_aliases_bin(struct module *modules, FILE *out, char *dirname)
856 struct module *i;
857 struct elf_file *file;
858 struct string_table *tbl;
859 int j;
860 char *alias;
861 struct index_node *index;
862 int duplicate;
864 index = index_create();
866 for (i = modules; i; i = i->next) {
867 char modname[strlen(i->pathname)+1];
869 file = i->file;
870 filename2modname(modname, i->pathname);
872 /* Grab from old-style .modalias section. */
873 tbl = file->ops->load_strings(file, ".modalias", NULL, fatal);
874 for (j = 0; tbl && j < tbl->cnt; j++) {
875 alias = NOFAIL(strdup(tbl->str[j]));
876 underscores(alias);
877 duplicate = index_insert(index, alias, modname, i->order);
878 if (duplicate && warn_dups)
879 warn("duplicate module alias:\n%s %s\n",
880 alias, modname);
881 free(alias);
883 strtbl_free(tbl);
885 /* Grab from new-style .modinfo section. */
886 tbl = file->ops->load_strings(file, ".modinfo", NULL, fatal);
887 for (j = 0; tbl && j < tbl->cnt; j++) {
888 const char *p = tbl->str[j];
889 if (strstarts(p, "alias=")) {
890 alias = NOFAIL(strdup(p + strlen("alias=")));
891 underscores(alias);
892 duplicate = index_insert(index, alias, modname, i->order);
893 if (duplicate && warn_dups)
894 warn("duplicate module alias:\n%s %s\n",
895 alias, modname);
896 free(alias);
899 strtbl_free(tbl);
902 index_write(index, out);
903 index_destroy(index);
906 struct depfile {
907 char *name;
908 void (*func)(struct module *, FILE *, char *dirname);
909 int map_file;
912 static struct depfile depfiles[] = {
913 { "modules.dep", output_deps, 0 }, /* This is what we check for '-A'. */
914 { "modules.dep.bin", output_deps_bin, 0 },
915 { "modules.pcimap", output_pci_table, 1 },
916 { "modules.usbmap", output_usb_table, 1 },
917 { "modules.ccwmap", output_ccw_table, 1 },
918 { "modules.ieee1394map", output_ieee1394_table, 1 },
919 { "modules.isapnpmap", output_isapnp_table, 1 },
920 { "modules.inputmap", output_input_table, 1 },
921 { "modules.ofmap", output_of_table, 1 },
922 { "modules.seriomap", output_serio_table, 1 },
923 { "modules.alias", output_aliases, 0 },
924 { "modules.alias.bin", output_aliases_bin, 0 },
925 { "modules.symbols", output_symbols, 0 },
926 { "modules.symbols.bin", output_symbols_bin, 0 }
929 /* If we can't figure it out, it's safe to say "true". */
930 static int any_modules_newer(const char *dirname, time_t mtime)
932 DIR *dir;
933 struct dirent *dirent;
935 dir = opendir(dirname);
936 if (!dir)
937 return 1;
939 while ((dirent = readdir(dir)) != NULL) {
940 struct stat st;
941 char file[strlen(dirname) + 1 + strlen(dirent->d_name) + 1];
943 if (streq(dirent->d_name, ".") || streq(dirent->d_name, ".."))
944 continue;
946 sprintf(file, "%s/%s", dirname, dirent->d_name);
947 if (lstat(file, &st) != 0)
948 goto ret_true;
950 if (smells_like_module(dirent->d_name)) {
951 if (st.st_mtime > mtime)
952 goto ret_true;
953 } else if (S_ISDIR(st.st_mode)) {
954 if (any_modules_newer(file, mtime))
955 goto ret_true;
958 closedir(dir);
959 return 0;
961 ret_true:
962 closedir(dir);
963 return 1;
966 static int depfile_out_of_date(const char *dirname)
968 struct stat st;
969 char depfile[strlen(dirname) + 1 + strlen(depfiles[0].name) + 1];
971 sprintf(depfile, "%s/%s", dirname, depfiles[0].name);
973 if (stat(depfile, &st) != 0)
974 return 1;
976 return any_modules_newer(dirname, st.st_mtime);
979 static char *strsep_skipspace(char **string, char *delim)
981 if (!*string)
982 return NULL;
983 *string += strspn(*string, delim);
984 return strsep(string, delim);
987 static struct module_search *add_search(const char *search_path,
988 size_t len,
989 struct module_search *search)
992 struct module_search *new;
994 new = NOFAIL(malloc(sizeof(*new)));
995 new->search_path = NOFAIL(strdup(search_path));
996 new->len = len;
997 new->next = search;
999 return new;
1003 static struct module_overrides *add_override(const char *modfile,
1004 struct module_overrides *overrides)
1007 struct module_overrides *new;
1009 new = NOFAIL(malloc(sizeof(*new)));
1010 new->modfile = NOFAIL(strdup(modfile));
1011 new->next = overrides;
1013 return new;
1017 static int parse_config_scan(const char *filename,
1018 const char *basedir,
1019 const char *kernelversion,
1020 struct module_search **search,
1021 struct module_overrides **overrides);
1023 static int parse_config_file(const char *filename,
1024 const char *basedir,
1025 const char *kernelversion,
1026 struct module_search **search,
1027 struct module_overrides **overrides)
1029 char *line;
1030 unsigned int linenum = 0;
1031 FILE *cfile;
1033 cfile = fopen(filename, "r");
1034 if (!cfile) {
1035 if (errno != ENOENT)
1036 fatal("could not open '%s', reason: %s\n", filename,
1037 strerror(errno));
1038 return 0;
1041 while ((line = getline_wrapped(cfile, &linenum)) != NULL) {
1042 char *ptr = line;
1043 char *cmd, *modname;
1045 cmd = strsep_skipspace(&ptr, "\t ");
1047 if (cmd == NULL || cmd[0] == '#' || cmd[0] == '\0') {
1048 free(line);
1049 continue;
1052 if (streq(cmd, "search")) {
1053 char *search_path;
1055 while ((search_path = strsep_skipspace(&ptr, "\t "))) {
1056 char *dirname;
1057 size_t len;
1059 if (strcmp(search_path,
1060 MODULE_BUILTIN_KEY) == 0) {
1061 *search = add_search(MODULE_BUILTIN_KEY,
1062 0, *search);
1063 continue;
1065 nofail_asprintf(&dirname, "%s%s%s/%s", basedir,
1066 MODULE_DIR, kernelversion, search_path);
1067 len = strlen(dirname);
1068 *search = add_search(dirname, len, *search);
1069 free(dirname);
1071 } else if (streq(cmd, "override")) {
1072 char *pathname = NULL, *version, *subdir;
1073 modname = strsep_skipspace(&ptr, "\t ");
1074 version = strsep_skipspace(&ptr, "\t ");
1075 subdir = strsep_skipspace(&ptr, "\t ");
1077 if (strcmp(version, kernelversion) != 0 &&
1078 strcmp(version, "*") != 0)
1079 continue;
1081 nofail_asprintf(&pathname, "%s%s%s/%s/%s.ko", basedir,
1082 MODULE_DIR, kernelversion, subdir, modname);
1084 *overrides = add_override(pathname, *overrides);
1085 free(pathname);
1086 } else if (streq(cmd, "include")) {
1087 char *newfilename;
1089 newfilename = strsep_skipspace(&ptr, "\t ");
1090 if (!newfilename) {
1091 grammar(cmd, filename, linenum);
1092 } else {
1093 warn("\"include %s\" is deprecated, "
1094 "please use /etc/depmod.d\n", newfilename);
1095 if (strstarts(newfilename, "/etc/depmod.d")) {
1096 warn("\"include /etc/depmod.d\" is "
1097 "the default, ignored\n");
1098 } else {
1099 if (!parse_config_scan(newfilename, basedir,
1100 kernelversion,
1101 search, overrides))
1102 warn("Failed to open included"
1103 " config file %s: %s\n",
1104 newfilename, strerror(errno));
1107 } else if (streq(cmd, "make_map_files")) {
1108 char *option;
1110 option = strsep_skipspace(&ptr, "\t ");
1111 if (!option)
1112 grammar(cmd, filename, linenum);
1113 else {
1114 if (streq(option, "yes"))
1115 make_map_files = 1;
1116 else if (streq(option, "no"))
1117 make_map_files = 0;
1118 else
1119 grammar(cmd, filename, linenum);
1121 } else
1122 grammar(cmd, filename, linenum);
1124 free(line);
1126 fclose(cfile);
1127 return 1;
1130 static int parse_config_scan(const char *filename,
1131 const char *basedir,
1132 const char *kernelversion,
1133 struct module_search **search,
1134 struct module_overrides **overrides)
1136 DIR *dir;
1137 int ret = 0;
1139 dir = opendir(filename);
1140 if (dir) {
1141 struct file_entry {
1142 struct list_head node;
1143 char name[];
1145 LIST_HEAD(files_list);
1146 struct file_entry *fe, *fe_tmp;
1147 struct dirent *i;
1149 /* sort files from directory into list */
1150 while ((i = readdir(dir)) != NULL) {
1151 size_t len;
1153 if (i->d_name[0] == '.')
1154 continue;
1155 if (!config_filter(i->d_name))
1156 continue;
1158 len = strlen(i->d_name);
1159 if (len < 6 || strcmp(&i->d_name[len-5], ".conf") != 0)
1160 warn("All config files need .conf: %s/%s, "
1161 "it will be ignored in a future release.\n",
1162 filename, i->d_name);
1163 fe = malloc(sizeof(struct file_entry) + len + 1);
1164 if (fe == NULL)
1165 continue;
1166 strcpy(fe->name, i->d_name);
1167 list_for_each_entry(fe_tmp, &files_list, node)
1168 if (strcmp(fe_tmp->name, fe->name) >= 0)
1169 break;
1170 list_add_tail(&fe->node, &fe_tmp->node);
1172 closedir(dir);
1174 /* parse list of files */
1175 list_for_each_entry_safe(fe, fe_tmp, &files_list, node) {
1176 char *cfgfile;
1178 nofail_asprintf(&cfgfile, "%s/%s", filename, fe->name);
1179 if (!parse_config_file(cfgfile, basedir, kernelversion,
1180 search, overrides))
1181 warn("Failed to open config file "
1182 "%s: %s\n", fe->name, strerror(errno));
1183 free(cfgfile);
1184 list_del(&fe->node);
1185 free(fe);
1188 ret = 1;
1189 } else {
1190 if (parse_config_file(filename, basedir, kernelversion, search,
1191 overrides))
1192 ret = 1;
1195 return ret;
1198 static void parse_toplevel_config(const char *filename,
1199 const char *basedir,
1200 const char *kernelversion,
1201 struct module_search **search,
1202 struct module_overrides **overrides)
1204 if (filename) {
1205 if (!parse_config_scan(filename, basedir, kernelversion, search,
1206 overrides))
1207 fatal("Failed to open config file %s: %s\n",
1208 filename, strerror(errno));
1209 return;
1212 /* deprecated config file */
1213 if (parse_config_file("/etc/depmod.conf", basedir, kernelversion,
1214 search, overrides) > 0)
1215 warn("Deprecated config file /etc/depmod.conf, "
1216 "all config files belong into /etc/depmod.d/.\n");
1218 /* default config */
1219 parse_config_scan("/etc/depmod.d", basedir, kernelversion,
1220 search, overrides);
1223 /* Local to main, but not freed on exit. Keep valgrind quiet. */
1224 struct module *list = NULL;
1225 struct module_search *search = NULL;
1226 struct module_overrides *overrides = NULL;
1228 int main(int argc, char *argv[])
1230 int opt, all = 0, maybe_all = 0, doing_stdout = 0;
1231 char *basedir = "", *dirname, *version;
1232 char *system_map = NULL, *module_symvers = NULL;
1233 int i;
1234 const char *config = NULL;
1236 if (native_endianness() == 0)
1237 abort();
1239 while ((opt = getopt_long(argc, argv, "aAb:C:E:F:euqrvnhVwm", options, NULL))
1240 != -1) {
1241 switch (opt) {
1242 case 'a':
1243 all = 1;
1244 break;
1245 case 'A':
1246 maybe_all = 1;
1247 break;
1248 case 'b':
1249 basedir = optarg;
1250 skipchars = strlen(basedir);
1251 break;
1252 case 'C':
1253 config = optarg;
1254 break;
1255 case 'E':
1256 module_symvers = optarg;
1257 check_symvers = 1;
1258 break;
1259 case 'F':
1260 system_map = optarg;
1261 break;
1262 case 'e':
1263 print_unknown = 1;
1264 break;
1265 case 'u':
1266 case 'q':
1267 case 'r':
1268 break;
1269 case 'v':
1270 verbose = 1;
1271 break;
1272 case 'n':
1273 doing_stdout = 1;
1274 break;
1275 case 'h':
1276 print_usage(argv[0]);
1277 exit(0);
1278 break;
1279 case 'V':
1280 printf("%s %s\n", PACKAGE, VERSION);
1281 exit(0);
1282 case 'w':
1283 warn_dups = 1;
1284 break;
1285 case 'm':
1286 force_map_files = 1;
1287 break;
1288 default:
1289 print_usage(argv[0]);
1290 exit(1);
1294 if (module_symvers)
1295 load_module_symvers(module_symvers);
1296 else if (system_map)
1297 load_system_map(system_map);
1298 else if (print_unknown) {
1299 warn("-e needs -E or -F");
1300 print_unknown = 0;
1303 /* They can specify the version naked on the command line */
1304 if (optind < argc && is_version_number(argv[optind])) {
1305 version = NOFAIL(strdup(argv[optind]));
1306 optind++;
1307 } else {
1308 struct utsname buf;
1309 uname(&buf);
1310 version = NOFAIL(strdup(buf.release));
1313 /* Check for old version. */
1314 if (old_module_version(version)) {
1315 fprintf(stderr, "Kernel version %s requires old depmod\n",
1316 version);
1317 exit(2);
1320 /* Depmod -a by default if no names. */
1321 if (optind == argc)
1322 all = 1;
1324 nofail_asprintf(&dirname, "%s%s%s", basedir, MODULE_DIR, version);
1326 if (maybe_all) {
1327 if (!doing_stdout && !depfile_out_of_date(dirname))
1328 exit(0);
1329 all = 1;
1332 parse_toplevel_config(config, basedir, version, &search, &overrides);
1334 /* For backward compatibility add "updates" to the head of the search
1335 * list here. But only if there was no "search" option specified.
1337 if (!search) {
1338 char *dirname;
1339 size_t len;
1341 nofail_asprintf(&dirname, "%s%s%s/updates", basedir,
1342 MODULE_DIR, version);
1343 len = strlen(dirname);
1344 search = add_search(dirname, len, search);
1346 if (!all) {
1347 /* Do command line args. */
1348 for (opt = optind; opt < argc; opt++) {
1349 struct module *new;
1351 if (argv[opt][0] != '/')
1352 fatal("modules must be specified using absolute paths.\n"
1353 "\"%s\" is a relative path\n", argv[opt]);
1355 new = grab_module(NULL, argv[opt]);
1356 if (!new) {
1357 /* cmd-line specified modules must exist */
1358 fatal("grab_module() failed for module %s\n", argv[opt]);
1360 new->next = list;
1361 list = new;
1363 } else {
1364 list = grab_basedir(dirname,search,overrides);
1366 list = sort_modules(dirname,list);
1367 list = parse_modules(list);
1369 for (i = 0; i < sizeof(depfiles)/sizeof(depfiles[0]); i++) {
1370 FILE *out;
1371 struct depfile *d = &depfiles[i];
1372 char depname[strlen(dirname) + 1 + strlen(d->name) + 1];
1373 char tmpname[strlen(dirname) + 1 + strlen(d->name) +
1374 strlen(".temp") + 1];
1376 if (d->map_file && !make_map_files && !force_map_files)
1377 continue;
1379 sprintf(depname, "%s/%s", dirname, d->name);
1380 sprintf(tmpname, "%s/%s.temp", dirname, d->name);
1381 if (!doing_stdout) {
1382 out = fopen(tmpname, "w");
1383 if (!out)
1384 fatal("Could not open %s for writing: %s\n",
1385 tmpname, strerror(errno));
1386 } else {
1387 out = stdout;
1388 if (ends_in(depname, ".bin"))
1389 continue;
1391 d->func(list, out, dirname);
1392 if (!doing_stdout) {
1393 fclose(out);
1394 if (rename(tmpname, depname) < 0)
1395 fatal("Could not rename %s into %s: %s\n",
1396 tmpname, depname, strerror(errno));
1400 free(dirname);
1401 free(version);
1403 return 0;