depmod: export static device node information to modules.devname
[mit.git] / depmod.c
blob647e5e69392d231057f80ef962963dddbf9e4966
1 /*
2 * New simplified depmod without backwards compat stuff and not
3 * requiring ksyms.
5 * (C) 2010 Jon Masters <jcm@jonmasters.org>, and others.
6 * (C) 2002 Rusty Russell IBM Corporation
7 */
8 #define _GNU_SOURCE /* asprintf */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <getopt.h>
13 #include <string.h>
14 #include <errno.h>
15 #include <unistd.h>
16 #include <elf.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20 #include <dirent.h>
21 #include <sys/utsname.h>
22 #include <sys/mman.h>
24 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
26 #include "util.h"
27 #include "zlibsupport.h"
28 #include "depmod.h"
29 #include "logging.h"
30 #include "index.h"
31 #include "elfops.h"
32 #include "tables.h"
33 #include "config_filter.h"
35 #include "testing.h"
37 #ifndef MODULE_DIR
38 #define MODULE_DIR "/lib/modules/"
39 #endif
41 #ifndef MODULE_BUILTIN_KEY
42 #define MODULE_BUILTIN_KEY "built-in"
43 #endif
45 struct module_overrides
47 /* Next override */
48 struct module_overrides *next;
50 /* overridden module */
51 char *modfile;
54 struct module_search
56 /* Next search */
57 struct module_search *next;
59 /* search path */
60 char *search_path;
61 size_t len;
64 static unsigned int skipchars;
65 static unsigned int make_map_files = 1; /* default to on */
66 static unsigned int force_map_files = 0; /* default to on */
68 #define SYMBOL_HASH_SIZE 1024
69 struct symbol
71 struct symbol *next;
72 struct module *owner;
73 uint64_t ver;
74 char name[0];
77 static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
79 /* This is based on the hash agorithm from gdbm, via tdb */
80 static inline unsigned int tdb_hash(const char *name)
82 unsigned value; /* Used to compute the hash value. */
83 unsigned i; /* Used to cycle through random values. */
85 /* Set the initial value from the key size. */
86 for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
87 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
89 return (1103515243 * value + 12345);
92 void add_symbol(const char *name, uint64_t ver, struct module *owner)
94 unsigned int hash;
95 struct symbol *new = NOFAIL(malloc(sizeof *new + strlen(name) + 1));
97 new->owner = owner;
98 new->ver = ver;
99 strcpy(new->name, name);
101 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
102 new->next = symbolhash[hash];
103 symbolhash[hash] = new;
106 static int print_unknown, check_symvers;
108 struct module *find_symbol(const char *name, uint64_t ver,
109 const char *modname, int weak)
111 struct symbol *s;
113 /* For our purposes, .foo matches foo. PPC64 needs this. */
114 if (name[0] == '.')
115 name++;
117 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) {
118 if (streq(s->name, name))
119 break;
121 if (s) {
122 if (ver && s->ver && s->ver != ver && print_unknown && !weak)
123 warn("%s disagrees about version of symbol %s\n",
124 modname, name);
125 return s->owner;
128 if (print_unknown && !weak)
129 warn("%s needs unknown symbol %s\n", modname, name);
131 return NULL;
134 void add_dep(struct module *mod, struct module *depends_on)
136 unsigned int i;
138 for (i = 0; i < mod->num_deps; i++)
139 if (mod->deps[i] == depends_on)
140 return;
142 mod->deps = NOFAIL(realloc(mod->deps, sizeof(mod->deps[0])*(mod->num_deps+1)));
143 mod->deps[mod->num_deps++] = depends_on;
146 static void add_fake_syms(void)
148 /* __this_module is magic inserted by kernel loader. */
149 add_symbol("__this_module", 0, NULL);
150 /* On S390, this is faked up too */
151 add_symbol("_GLOBAL_OFFSET_TABLE_", 0, NULL);
154 static void load_system_map(const char *filename)
156 FILE *system_map;
157 char line[10240];
158 const char ksymstr[] = "__ksymtab_";
159 const int ksymstr_len = strlen(ksymstr);
161 system_map = fopen(filename, "r");
162 if (!system_map)
163 fatal("Could not open '%s': %s\n", filename, strerror(errno));
165 /* eg. c0294200 R __ksymtab_devfs_alloc_devnum */
166 while (fgets(line, sizeof(line)-1, system_map)) {
167 char *ptr;
169 /* Snip \n */
170 ptr = strchr(line, '\n');
171 if (ptr)
172 *ptr = '\0';
174 ptr = strchr(line, ' ');
175 if (!ptr || !(ptr = strchr(ptr + 1, ' ')))
176 continue;
178 /* Covers gpl-only and normal symbols. */
179 if (strstarts(ptr+1, ksymstr))
180 add_symbol(ptr+1+ksymstr_len, 0, NULL);
183 fclose(system_map);
184 add_fake_syms();
187 static void load_module_symvers(const char *filename)
189 FILE *module_symvers;
190 char line[10240];
192 module_symvers = fopen(filename, "r");
193 if (!module_symvers)
194 fatal("Could not open '%s': %s\n", filename, strerror(errno));
196 /* eg. "0xb352177e\tfind_first_bit\tvmlinux\tEXPORT_SYMBOL" */
197 while (fgets(line, sizeof(line)-1, module_symvers)) {
198 const char *ver, *sym, *where;
200 ver = strtok(line, " \t");
201 sym = strtok(NULL, " \t");
202 where = strtok(NULL, " \t");
203 if (!ver || !sym || !where)
204 continue;
206 if (streq(where, "vmlinux"))
207 add_symbol(sym, strtoull(ver, NULL, 16), NULL);
210 fclose(module_symvers);
211 add_fake_syms();
214 static struct option options[] = { { "all", 0, NULL, 'a' },
215 { "quick", 0, NULL, 'A' },
216 { "basedir", 1, NULL, 'b' },
217 { "config", 1, NULL, 'C' },
218 { "symvers", 1, NULL, 'E' },
219 { "filesyms", 1, NULL, 'F' },
220 { "errsyms", 0, NULL, 'e' },
221 { "unresolved-error", 0, NULL, 'u' },
222 { "quiet", 0, NULL, 'q' },
223 { "root", 0, NULL, 'r' },
224 { "verbose", 0, NULL, 'v' },
225 { "show", 0, NULL, 'n' },
226 { "dry-run", 0, NULL, 'n' },
227 { "help", 0, NULL, 'h' },
228 { "version", 0, NULL, 'V' },
229 { "warn", 0, NULL, 'w' },
230 { "map", 0, NULL, 'm' },
231 { NULL, 0, NULL, 0 } };
233 /* Version number or module name? Don't assume extension. */
234 static int is_version_number(const char *version)
236 unsigned int dummy;
238 return (sscanf(version, "%u.%u.%u", &dummy, &dummy, &dummy) == 3);
241 static int old_module_version(const char *version)
243 /* Expect three part version. */
244 unsigned int major, sub, minor;
246 sscanf(version, "%u.%u.%u", &major, &sub, &minor);
248 if (major > 2) return 0;
249 if (major < 2) return 1;
251 /* 2.x */
252 if (sub > 5) return 0;
253 if (sub < 5) return 1;
255 /* 2.5.x */
256 if (minor >= 48) return 0;
257 return 1;
260 static void print_usage(const char *name)
262 fprintf(stderr,
263 "%s " VERSION " -- part of " PACKAGE "\n"
264 "%s -[aA] [-n -e -v -q -V -r -u -w -m]\n"
265 " [-b basedirectory] [forced_version]\n"
266 "depmod [-n -e -v -q -r -u -w] [-F kernelsyms] module1.ko module2.ko ...\n"
267 "If no arguments (except options) are given, \"depmod -a\" is assumed\n"
268 "\n"
269 "depmod will output a dependancy list suitable for the modprobe utility.\n"
270 "\n"
271 "\n"
272 "Options:\n"
273 "\t-a, --all Probe all modules\n"
274 "\t-A, --quick Only does the work if there's a new module\n"
275 "\t-e, --errsyms Report not supplied symbols\n"
276 "\t-m, --map Create the legacy map files\n"
277 "\t-n, --show Write the dependency file on stdout only\n"
278 "\t-V, --version Print the release version\n"
279 "\t-v, --verbose Enable verbose mode\n"
280 "\t-w, --warn Warn on duplicates\n"
281 "\t-h, --help Print this usage message\n"
282 "\n"
283 "The following options are useful for people managing distributions:\n"
284 "\t-b basedirectory\n"
285 "\t --basedir basedirectory Use an image of a module tree.\n"
286 "\t-F kernelsyms\n"
287 "\t --filesyms kernelsyms Use the file instead of the\n"
288 "\t current kernel symbols.\n"
289 "\t-E Module.symvers\n"
290 "\t --symvers Module.symvers Use Module.symvers file to check\n"
291 "\t symbol versions.\n",
292 "depmod", "depmod");
295 static int ends_in(const char *name, const char *ext)
297 unsigned int namelen, extlen;
299 /* Grab lengths */
300 namelen = strlen(name);
301 extlen = strlen(ext);
303 if (namelen < extlen) return 0;
305 if (streq(name + namelen - extlen, ext))
306 return 1;
307 return 0;
310 static struct module *grab_module(const char *dirname, const char *filename)
312 struct module *new;
314 new = NOFAIL(malloc(sizeof(*new)
315 + strlen(dirname?:"") + 1 + strlen(filename) + 1));
316 if (dirname)
317 sprintf(new->pathname, "%s/%s", dirname, filename);
318 else
319 strcpy(new->pathname, filename);
320 new->basename = my_basename(new->pathname);
322 INIT_LIST_HEAD(&new->dep_list);
323 new->order = INDEX_PRIORITY_MIN;
325 new->file = grab_elf_file(new->pathname);
326 if (!new->file) {
327 warn("Can't read module %s: %s\n",
328 new->pathname, strerror(errno));
329 free(new);
330 return NULL;
332 return new;
335 struct module_traverse
337 struct module_traverse *prev;
338 struct module *mod;
341 static int in_loop(struct module *mod, const struct module_traverse *traverse)
343 const struct module_traverse *i;
345 for (i = traverse; i; i = i->prev) {
346 if (i->mod == mod)
347 return 1;
349 return 0;
352 /* Assume we are doing all the modules, so only report each loop once. */
353 static void report_loop(const struct module *mod,
354 const struct module_traverse *traverse)
356 const struct module_traverse *i;
358 /* Check that start is least alphabetically. eg. a depends
359 on b depends on a will get reported for a, not b. */
360 for (i = traverse->prev; i->prev; i = i->prev) {
361 if (strcmp(mod->pathname, i->mod->pathname) > 0)
362 return;
365 /* Is start in the loop? If not, don't report now. eg. a
366 depends on b which depends on c which depends on b. Don't
367 report when generating depends for a. */
368 if (mod != i->mod)
369 return;
371 warn("Loop detected: %s ", mod->pathname);
372 for (i = traverse->prev; i->prev; i = i->prev)
373 fprintf(stderr, "needs %s ", i->mod->basename);
374 fprintf(stderr, "which needs %s again!\n", i->mod->basename);
377 /* This is damn slow, but loops actually happen, and we don't want to
378 just exit() and leave the user without any modules. */
379 static int has_dep_loop(struct module *module, struct module_traverse *prev)
381 unsigned int i;
382 struct module_traverse traverse = { .prev = prev, .mod = module };
384 if (in_loop(module, prev)) {
385 report_loop(module, &traverse);
386 return 1;
389 for (i = 0; i < module->num_deps; i++)
390 if (has_dep_loop(module->deps[i], &traverse))
391 return 1;
392 return 0;
395 /* Uniquifies and orders a dependency list. */
396 static void order_dep_list(struct module *start, struct module *mod)
398 unsigned int i;
400 for (i = 0; i < mod->num_deps; i++) {
401 /* If it was previously depended on, move it to the
402 tail. ie. if a needs b and c, and c needs b, we
403 must order b after c. */
404 list_del(&mod->deps[i]->dep_list);
405 list_add_tail(&mod->deps[i]->dep_list, &start->dep_list);
406 order_dep_list(start, mod->deps[i]);
410 static struct module *deleted = NULL;
412 static void del_module(struct module **modules, struct module *delme)
414 struct module **i;
416 /* Find pointer to it. */
417 if (modules) {
418 for (i = modules; *i != delme; i = &(*i)->next);
420 *i = delme->next;
423 /* Save on a list to quiet valgrind.
424 Can't free - other modules may depend on them */
425 delme->next = deleted;
426 deleted = delme;
429 /* convert to relative path if possible */
430 static const char *compress_path(const char *path, const char *basedir)
432 int len = strlen(basedir);
434 if (strncmp(path, basedir, len) == 0)
435 path += len + 1;
436 return path;
439 static int output_deps(struct module *modules,
440 FILE *out, char *dirname)
442 struct module *i;
444 for (i = modules; i; i = i->next) {
445 struct list_head *j, *tmp;
446 order_dep_list(i, i);
448 fprintf(out, "%s:", compress_path(i->pathname, dirname));
449 list_for_each_safe(j, tmp, &i->dep_list) {
450 struct module *dep
451 = list_entry(j, struct module, dep_list);
452 fprintf(out, " %s",
453 compress_path(dep->pathname, dirname));
454 list_del_init(j);
456 fprintf(out, "\n");
458 return 1;
461 /* warn whenever duplicate module aliases, deps, or symbols are found. */
462 int warn_dups = 0;
464 static int output_deps_bin(struct module *modules,
465 FILE *out, char *dirname)
467 struct module *i;
468 struct index_node *index;
469 char *line;
470 char *p;
472 index = index_create();
474 for (i = modules; i; i = i->next) {
475 struct list_head *j, *tmp;
476 char modname[strlen(i->pathname)+1];
478 order_dep_list(i, i);
480 filename2modname(modname, i->pathname);
481 nofail_asprintf(&line, "%s:",
482 compress_path(i->pathname, dirname));
483 p = line;
484 list_for_each_safe(j, tmp, &i->dep_list) {
485 struct module *dep
486 = list_entry(j, struct module, dep_list);
487 nofail_asprintf(&line, "%s %s",
489 compress_path(dep->pathname, dirname));
490 free(p);
491 p = line;
492 list_del_init(j);
494 if (index_insert(index, modname, line, i->order) && warn_dups)
495 warn("duplicate module deps:\n%s\n",line);
496 free(line);
499 index_write(index, out);
500 index_destroy(index);
502 return 1;
506 static int smells_like_module(const char *name)
508 return ends_in(name,".ko") || ends_in(name, ".ko.gz");
511 typedef struct module *(*do_module_t)(const char *dirname,
512 const char *filename,
513 struct module *next,
514 struct module_search *search,
515 struct module_overrides *overrides);
517 static int is_higher_priority(const char *newpath, const char *oldpath,
518 struct module_search *search,
519 struct module_overrides *overrides)
521 struct module_search *tmp;
522 struct module_overrides *ovtmp;
523 int i = 0;
524 int prio_builtin = -1;
525 int prio_new = -1;
526 int prio_old = -1;
528 /* The names already match, now we check for overrides and directory search
529 * order
531 for (ovtmp = overrides; ovtmp != NULL; ovtmp = ovtmp->next) {
532 if (streq(ovtmp->modfile, newpath))
533 return 1;
534 if (streq(ovtmp->modfile, oldpath))
535 return 0;
537 for (i = 0, tmp = search; tmp != NULL; tmp = tmp->next, i++) {
538 if (streq(tmp->search_path, MODULE_BUILTIN_KEY))
539 prio_builtin = i;
540 else if (strncmp(tmp->search_path, newpath, tmp->len) == 0)
541 prio_new = i;
542 else if (strncmp(tmp->search_path, oldpath, tmp->len) == 0)
543 prio_old = i;
545 if (prio_new < 0)
546 prio_new = prio_builtin;
547 if (prio_old < 0)
548 prio_old = prio_builtin;
550 return prio_new > prio_old;
554 static struct module *do_module(const char *dirname,
555 const char *filename,
556 struct module *list,
557 struct module_search *search,
558 struct module_overrides *overrides)
560 struct module *new, **i;
562 new = grab_module(dirname, filename);
563 if (!new)
564 return list;
566 /* Check if module is already in the list. */
567 for (i = &list; *i; i = &(*i)->next) {
569 if (streq((*i)->basename, filename)) {
570 char newpath[strlen(dirname) + strlen("/")
571 + strlen(filename) + 1];
573 sprintf(newpath, "%s/%s", dirname, filename);
575 if (is_higher_priority(newpath, (*i)->pathname,search,
576 overrides)) {
577 del_module(i, *i);
579 new->next = *i;
580 *i = new;
581 } else
582 del_module(NULL, new);
584 return list;
588 /* Not in the list already. Just prepend. */
589 new->next = list;
590 return new;
593 static struct module *grab_dir(const char *dirname,
594 DIR *dir,
595 struct module *next,
596 do_module_t do_mod,
597 struct module_search *search,
598 struct module_overrides *overrides)
600 struct dirent *dirent;
602 while ((dirent = readdir(dir)) != NULL) {
603 if (smells_like_module(dirent->d_name))
604 next = do_mod(dirname, dirent->d_name, next,
605 search, overrides);
606 else if (!streq(dirent->d_name, ".")
607 && !streq(dirent->d_name, "..")
608 && !streq(dirent->d_name, "source")
609 && !streq(dirent->d_name, "build")) {
611 DIR *sub;
612 char subdir[strlen(dirname) + 1
613 + strlen(dirent->d_name) + 1];
614 sprintf(subdir, "%s/%s", dirname, dirent->d_name);
615 sub = opendir(subdir);
616 if (sub) {
617 next = grab_dir(subdir, sub, next, do_mod,
618 search, overrides);
619 closedir(sub);
623 return next;
626 static struct module *grab_basedir(const char *dirname,
627 struct module_search *search,
628 struct module_overrides *overrides)
630 DIR *dir;
631 struct module *list;
633 dir = opendir(dirname);
634 if (!dir) {
635 warn("Couldn't open directory %s: %s\n",
636 dirname, strerror(errno));
637 return NULL;
639 list = grab_dir(dirname, dir, NULL, do_module, search, overrides);
640 closedir(dir);
642 return list;
645 static struct module *sort_modules(const char *dirname, struct module *list)
647 struct module *tlist = NULL, **tpos = &tlist;
648 FILE *modorder;
649 int dir_len = strlen(dirname) + 1;
650 char file_name[dir_len + strlen("modules.order") + 1];
651 char line[10240];
652 unsigned int linenum = 0;
654 sprintf(file_name, "%s/%s", dirname, "modules.order");
656 modorder = fopen(file_name, "r");
657 if (!modorder) {
658 /* Older kernels don't generate modules.order. Just
659 return if the file doesn't exist. */
660 if (errno == ENOENT)
661 return list;
662 fatal("Could not open '%s': %s\n", file_name, strerror(errno));
665 sprintf(line, "%s/", dirname);
667 /* move modules listed in modorder file to tlist in order */
668 while (fgets(line, sizeof(line), modorder)) {
669 struct module **pos, *mod;
670 int len = strlen(line);
672 linenum++;
673 if (line[len - 1] == '\n')
674 line[len - 1] = '\0';
676 for (pos = &list; (mod = *pos); pos = &(*pos)->next) {
677 if (streq(line, mod->pathname + dir_len)) {
678 mod->order = linenum;
679 *pos = mod->next;
680 mod->next = NULL;
681 *tpos = mod;
682 tpos = &mod->next;
683 break;
688 /* append the rest */
689 *tpos = list;
691 fclose(modorder);
693 return tlist;
696 /* Calculate the dependencies for this module */
697 static void calculate_deps(struct module *module)
699 unsigned int i;
700 struct string_table *symnames;
701 struct string_table *symtypes;
702 uint64_t *symvers = NULL;
703 struct elf_file *file;
705 module->num_deps = 0;
706 module->deps = NULL;
707 file = module->file;
709 symnames = file->ops->load_dep_syms(file, &symtypes,
710 check_symvers ? &symvers : NULL);
711 if (!symnames || !symtypes)
712 return;
714 for (i = 0; i < symnames->cnt; i++) {
715 const char *name;
716 uint64_t ver;
717 struct module *owner;
718 int weak;
720 name = symnames->str[i];
721 ver = symvers ? symvers[i] : 0;
722 weak = (*(symtypes->str[i]) == 'W');
723 owner = find_symbol(name, ver, module->pathname, weak);
724 if (owner) {
725 info("%s needs \"%s\": %s\n",
726 module->pathname, name,
727 owner->pathname);
728 add_dep(module, owner);
732 free(symnames);
733 free(symtypes);
734 free(symvers);
737 static struct module *parse_modules(struct module *list)
739 struct module *i;
740 struct elf_file *file;
741 struct string_table *syms;
742 int j;
744 for (i = list; i; i = i->next) {
745 uint64_t *symvers = NULL;
746 file = i->file;
747 syms = file->ops->load_symbols(file,
748 check_symvers ? &symvers : NULL);
749 if (syms) {
750 for (j = 0; j < syms->cnt; j++)
751 add_symbol(syms->str[j],
752 symvers ? symvers[j] : 0, i);
753 strtbl_free(syms);
755 free(symvers);
756 file->ops->fetch_tables(file, &i->tables);
759 for (i = list; i; i = i->next)
760 calculate_deps(i);
762 /* Strip out modules with dependency loops. */
763 again:
764 for (i = list; i; i = i->next) {
765 if (has_dep_loop(i, NULL)) {
766 warn("Module %s ignored, due to loop\n",
767 i->pathname + skipchars);
768 del_module(&list, i);
769 goto again;
773 return list;
776 /* Simply dump hash table. */
777 static int output_symbols(struct module *unused, FILE *out, char *dirname)
779 unsigned int i;
781 fprintf(out, "# Aliases for symbols, used by symbol_request().\n");
782 for (i = 0; i < SYMBOL_HASH_SIZE; i++) {
783 struct symbol *s;
785 for (s = symbolhash[i]; s; s = s->next) {
786 if (s->owner) {
787 char modname[strlen(s->owner->pathname)+1];
788 filename2modname(modname, s->owner->pathname);
789 fprintf(out, "alias symbol:%s %s\n",
790 s->name, modname);
794 return 1;
797 static int output_symbols_bin(struct module *unused, FILE *out, char *dirname)
799 struct index_node *index;
800 unsigned int i;
801 char *alias;
802 int duplicate;
804 index = index_create();
806 for (i = 0; i < SYMBOL_HASH_SIZE; i++) {
807 struct symbol *s;
809 for (s = symbolhash[i]; s; s = s->next) {
810 if (s->owner) {
811 char modname[strlen(s->owner->pathname)+1];
812 filename2modname(modname, s->owner->pathname);
813 nofail_asprintf(&alias, "symbol:%s", s->name);
814 duplicate = index_insert(index, alias, modname,
815 s->owner->order);
816 if (duplicate && warn_dups)
817 warn("duplicate module syms:\n%s %s\n",
818 alias, modname);
819 free(alias);
824 index_write(index, out);
825 index_destroy(index);
827 return 1;
830 static int output_builtin_bin(struct module *unused, FILE *out, char *dirname)
832 struct index_node *index;
833 char *textfile, *line;
834 unsigned int linenum;
835 FILE *f;
837 nofail_asprintf(&textfile, "%s/modules.builtin", dirname);
838 if (!(f = fopen(textfile, "r"))) {
839 if (errno != ENOENT)
840 fatal("Could not open '%s': %s\n",
841 textfile, strerror(errno));
842 free(textfile);
843 return 0;
845 free(textfile);
846 index = index_create();
848 while ((line = getline_wrapped(f, &linenum)) != NULL) {
849 char *module = line;
851 if (!*line || *line == '#') {
852 free(line);
853 continue;
855 filename2modname(module, module);
856 index_insert(index, module, "", 0);
857 free(line);
859 fclose(f);
860 index_write(index, out);
861 index_destroy(index);
863 return 1;
866 static int output_aliases(struct module *modules, FILE *out, char *dirname)
868 struct module *i;
869 struct elf_file *file;
870 struct string_table *tbl;
871 int j;
873 fprintf(out, "# Aliases extracted from modules themselves.\n");
874 for (i = modules; i; i = i->next) {
875 char modname[strlen(i->pathname)+1];
877 file = i->file;
878 filename2modname(modname, i->pathname);
880 /* Grab from old-style .modalias section. */
881 tbl = file->ops->load_strings(file, ".modalias", NULL);
882 for (j = 0; tbl && j < tbl->cnt; j++)
883 fprintf(out, "alias %s %s\n", tbl->str[j], modname);
884 strtbl_free(tbl);
886 /* Grab from new-style .modinfo section. */
887 tbl = file->ops->load_strings(file, ".modinfo", NULL);
888 for (j = 0; tbl && j < tbl->cnt; j++) {
889 const char *p = tbl->str[j];
890 if (strstarts(p, "alias="))
891 fprintf(out, "alias %s %s\n",
892 p + strlen("alias="), modname);
894 strtbl_free(tbl);
896 return 1;
899 static int output_aliases_bin(struct module *modules, FILE *out, char *dirname)
901 struct module *i;
902 struct elf_file *file;
903 struct string_table *tbl;
904 int j;
905 char *alias;
906 struct index_node *index;
907 int duplicate;
909 index = index_create();
911 for (i = modules; i; i = i->next) {
912 char modname[strlen(i->pathname)+1];
914 file = i->file;
915 filename2modname(modname, i->pathname);
917 /* Grab from old-style .modalias section. */
918 tbl = file->ops->load_strings(file, ".modalias", NULL);
919 for (j = 0; tbl && j < tbl->cnt; j++) {
920 alias = NOFAIL(strdup(tbl->str[j]));
921 underscores(alias);
922 duplicate = index_insert(index, alias, modname, i->order);
923 if (duplicate && warn_dups)
924 warn("duplicate module alias:\n%s %s\n",
925 alias, modname);
926 free(alias);
928 strtbl_free(tbl);
930 /* Grab from new-style .modinfo section. */
931 tbl = file->ops->load_strings(file, ".modinfo", NULL);
932 for (j = 0; tbl && j < tbl->cnt; j++) {
933 const char *p = tbl->str[j];
934 if (strstarts(p, "alias=")) {
935 alias = NOFAIL(strdup(p + strlen("alias=")));
936 underscores(alias);
937 duplicate = index_insert(index, alias, modname, i->order);
938 if (duplicate && warn_dups)
939 warn("duplicate module alias:\n%s %s\n",
940 alias, modname);
941 free(alias);
944 strtbl_free(tbl);
947 index_write(index, out);
948 index_destroy(index);
950 return 1;
953 static int output_softdeps(struct module *modules, FILE *out, char *dirname)
955 struct module *i;
956 struct elf_file *file;
957 struct string_table *tbl;
958 int j;
960 fprintf(out, "# Soft dependencies extracted from modules themselves.\n");
961 fprintf(out, "# Copy, with a .conf extension, to /etc/modprobe.d to use "
962 "it with modprobe.\n");
963 for (i = modules; i; i = i->next) {
964 char modname[strlen(i->pathname)+1];
966 file = i->file;
967 filename2modname(modname, i->pathname);
969 /* Grab from new-style .modinfo section. */
970 tbl = file->ops->load_strings(file, ".modinfo", NULL);
971 for (j = 0; tbl && j < tbl->cnt; j++) {
972 const char *p = tbl->str[j];
973 if (strstarts(p, "softdep="))
974 fprintf(out, "softdep %s %s\n",
975 modname, p + strlen("softdep="));
977 strtbl_free(tbl);
979 return 1;
982 static int output_devname(struct module *modules, FILE *out, char *dirname)
984 struct module *m;
986 fprintf(out, "# Device nodes to trigger on-demand module loading.\n");
987 for (m = modules; m != NULL; m = m->next) {
988 struct string_table *tbl;
989 int i;
990 char type = '\0';
991 const char *devname = NULL;
993 tbl = m->file->ops->load_strings(m->file, ".modinfo", NULL);
994 for (i = 0; tbl && i < tbl->cnt; i++) {
995 const char *p = tbl->str[i];
996 unsigned int maj, min;
998 if (sscanf(p, "alias=char-major-%u-%u", &maj, &min) == 2)
999 type = 'c';
1000 else if (sscanf(p, "alias=block-major-%u-%u", &maj, &min) == 2)
1001 type = 'b';
1002 else if (strstarts(p, "alias=devname:"))
1003 devname = &p[strlen("alias=devname:")];
1005 if (type && devname) {
1006 char modname[strlen(m->pathname)+1];
1008 filename2modname(modname, m->pathname);
1009 fprintf(out, "%s %s %c%u:%u\n",
1010 modname, devname, type, maj, min);
1011 break;
1014 strtbl_free(tbl);
1016 return 1;
1019 struct depfile {
1020 char *name;
1021 int (*func)(struct module *, FILE *, char *dirname);
1022 int map_file;
1025 static struct depfile depfiles[] = {
1026 { "modules.dep", output_deps, 0 }, /* This is what we check for '-A'. */
1027 { "modules.dep.bin", output_deps_bin, 0 },
1028 { "modules.pcimap", output_pci_table, 1 },
1029 { "modules.usbmap", output_usb_table, 1 },
1030 { "modules.ccwmap", output_ccw_table, 1 },
1031 { "modules.ieee1394map", output_ieee1394_table, 1 },
1032 { "modules.isapnpmap", output_isapnp_table, 1 },
1033 { "modules.inputmap", output_input_table, 1 },
1034 { "modules.ofmap", output_of_table, 1 },
1035 { "modules.seriomap", output_serio_table, 1 },
1036 { "modules.alias", output_aliases, 0 },
1037 { "modules.alias.bin", output_aliases_bin, 0 },
1038 { "modules.softdep", output_softdeps, 0 },
1039 { "modules.symbols", output_symbols, 0 },
1040 { "modules.symbols.bin", output_symbols_bin, 0 },
1041 { "modules.builtin.bin", output_builtin_bin, 0 },
1042 { "modules.devname", output_devname, 0 },
1045 /* If we can't figure it out, it's safe to say "true". */
1046 static int any_modules_newer(const char *dirname, time_t mtime)
1048 DIR *dir;
1049 struct dirent *dirent;
1051 dir = opendir(dirname);
1052 if (!dir)
1053 return 1;
1055 while ((dirent = readdir(dir)) != NULL) {
1056 struct stat st;
1057 char file[strlen(dirname) + 1 + strlen(dirent->d_name) + 1];
1059 if (streq(dirent->d_name, ".") || streq(dirent->d_name, ".."))
1060 continue;
1062 sprintf(file, "%s/%s", dirname, dirent->d_name);
1063 if (lstat(file, &st) != 0)
1064 goto ret_true;
1066 if (smells_like_module(dirent->d_name)) {
1067 if (st.st_mtime > mtime)
1068 goto ret_true;
1069 } else if (S_ISDIR(st.st_mode)) {
1070 if (any_modules_newer(file, mtime))
1071 goto ret_true;
1074 closedir(dir);
1075 return 0;
1077 ret_true:
1078 closedir(dir);
1079 return 1;
1082 static int depfile_out_of_date(const char *dirname)
1084 struct stat st;
1085 char depfile[strlen(dirname) + 1 + strlen(depfiles[0].name) + 1];
1087 sprintf(depfile, "%s/%s", dirname, depfiles[0].name);
1089 if (stat(depfile, &st) != 0)
1090 return 1;
1092 return any_modules_newer(dirname, st.st_mtime);
1095 static char *strsep_skipspace(char **string, char *delim)
1097 if (!*string)
1098 return NULL;
1099 *string += strspn(*string, delim);
1100 return strsep(string, delim);
1103 static struct module_search *add_search(const char *search_path,
1104 size_t len,
1105 struct module_search *search)
1108 struct module_search *new;
1110 new = NOFAIL(malloc(sizeof(*new)));
1111 new->search_path = NOFAIL(strdup(search_path));
1112 new->len = len;
1113 new->next = search;
1115 return new;
1119 static struct module_overrides *add_override(const char *modfile,
1120 struct module_overrides *overrides)
1123 struct module_overrides *new;
1125 new = NOFAIL(malloc(sizeof(*new)));
1126 new->modfile = NOFAIL(strdup(modfile));
1127 new->next = overrides;
1129 return new;
1133 static int parse_config_scan(const char *filename,
1134 const char *basedir,
1135 const char *kernelversion,
1136 struct module_search **search,
1137 struct module_overrides **overrides);
1139 static int parse_config_file(const char *filename,
1140 const char *basedir,
1141 const char *kernelversion,
1142 struct module_search **search,
1143 struct module_overrides **overrides)
1145 char *line;
1146 unsigned int linenum = 0;
1147 FILE *cfile;
1149 cfile = fopen(filename, "r");
1150 if (!cfile) {
1151 if (errno != ENOENT)
1152 fatal("could not open '%s', reason: %s\n", filename,
1153 strerror(errno));
1154 return 0;
1157 while ((line = getline_wrapped(cfile, &linenum)) != NULL) {
1158 char *ptr = line;
1159 char *cmd, *modname;
1161 cmd = strsep_skipspace(&ptr, "\t ");
1163 if (cmd == NULL || cmd[0] == '#' || cmd[0] == '\0') {
1164 free(line);
1165 continue;
1168 if (streq(cmd, "search")) {
1169 char *search_path;
1171 while ((search_path = strsep_skipspace(&ptr, "\t "))) {
1172 char *dirname;
1173 size_t len;
1175 if (strcmp(search_path,
1176 MODULE_BUILTIN_KEY) == 0) {
1177 *search = add_search(MODULE_BUILTIN_KEY,
1178 0, *search);
1179 continue;
1181 nofail_asprintf(&dirname, "%s%s%s/%s", basedir,
1182 MODULE_DIR, kernelversion, search_path);
1183 len = strlen(dirname);
1184 *search = add_search(dirname, len, *search);
1185 free(dirname);
1187 } else if (streq(cmd, "override")) {
1188 char *pathname = NULL, *version, *subdir;
1189 modname = strsep_skipspace(&ptr, "\t ");
1190 version = strsep_skipspace(&ptr, "\t ");
1191 subdir = strsep_skipspace(&ptr, "\t ");
1193 if (!regex_match(kernelversion, (const char *)version))
1194 continue;
1196 nofail_asprintf(&pathname, "%s%s%s/%s/%s.ko", basedir,
1197 MODULE_DIR, kernelversion, subdir, modname);
1199 *overrides = add_override(pathname, *overrides);
1200 free(pathname);
1201 } else if (streq(cmd, "include")) {
1202 char *newfilename;
1204 newfilename = strsep_skipspace(&ptr, "\t ");
1205 if (!newfilename) {
1206 grammar(cmd, filename, linenum);
1207 } else {
1208 warn("\"include %s\" is deprecated, "
1209 "please use /etc/depmod.d\n", newfilename);
1210 if (strstarts(newfilename, "/etc/depmod.d")) {
1211 warn("\"include /etc/depmod.d\" is "
1212 "the default, ignored\n");
1213 } else {
1214 if (!parse_config_scan(newfilename, basedir,
1215 kernelversion,
1216 search, overrides))
1217 warn("Failed to open included"
1218 " config file %s: %s\n",
1219 newfilename, strerror(errno));
1222 } else if (streq(cmd, "make_map_files")) {
1223 char *option;
1225 option = strsep_skipspace(&ptr, "\t ");
1226 if (!option)
1227 grammar(cmd, filename, linenum);
1228 else {
1229 if (streq(option, "yes"))
1230 make_map_files = 1;
1231 else if (streq(option, "no"))
1232 make_map_files = 0;
1233 else
1234 grammar(cmd, filename, linenum);
1236 } else
1237 grammar(cmd, filename, linenum);
1239 free(line);
1241 fclose(cfile);
1242 return 1;
1245 static int parse_config_scan(const char *filename,
1246 const char *basedir,
1247 const char *kernelversion,
1248 struct module_search **search,
1249 struct module_overrides **overrides)
1251 DIR *dir;
1252 int ret = 0;
1254 dir = opendir(filename);
1255 if (dir) {
1256 struct file_entry {
1257 struct list_head node;
1258 char name[];
1260 LIST_HEAD(files_list);
1261 struct file_entry *fe, *fe_tmp;
1262 struct dirent *i;
1264 /* sort files from directory into list */
1265 while ((i = readdir(dir)) != NULL) {
1266 size_t len;
1268 if (i->d_name[0] == '.')
1269 continue;
1270 if (!config_filter(i->d_name))
1271 continue;
1273 len = strlen(i->d_name);
1274 if (len < 6 || strcmp(&i->d_name[len-5], ".conf") != 0)
1275 warn("All config files need .conf: %s/%s, "
1276 "it will be ignored in a future release.\n",
1277 filename, i->d_name);
1278 fe = malloc(sizeof(struct file_entry) + len + 1);
1279 if (fe == NULL)
1280 continue;
1281 strcpy(fe->name, i->d_name);
1282 list_for_each_entry(fe_tmp, &files_list, node)
1283 if (strcmp(fe_tmp->name, fe->name) >= 0)
1284 break;
1285 list_add_tail(&fe->node, &fe_tmp->node);
1287 closedir(dir);
1289 /* parse list of files */
1290 list_for_each_entry_safe(fe, fe_tmp, &files_list, node) {
1291 char *cfgfile;
1293 nofail_asprintf(&cfgfile, "%s/%s", filename, fe->name);
1294 if (!parse_config_file(cfgfile, basedir, kernelversion,
1295 search, overrides))
1296 warn("Failed to open config file "
1297 "%s: %s\n", fe->name, strerror(errno));
1298 free(cfgfile);
1299 list_del(&fe->node);
1300 free(fe);
1303 ret = 1;
1304 } else {
1305 if (parse_config_file(filename, basedir, kernelversion, search,
1306 overrides))
1307 ret = 1;
1310 return ret;
1313 static void parse_toplevel_config(const char *filename,
1314 const char *basedir,
1315 const char *kernelversion,
1316 struct module_search **search,
1317 struct module_overrides **overrides)
1319 if (filename) {
1320 if (!parse_config_scan(filename, basedir, kernelversion, search,
1321 overrides))
1322 fatal("Failed to open config file %s: %s\n",
1323 filename, strerror(errno));
1324 return;
1327 /* deprecated config file */
1328 if (parse_config_file("/etc/depmod.conf", basedir, kernelversion,
1329 search, overrides) > 0)
1330 warn("Deprecated config file /etc/depmod.conf, "
1331 "all config files belong into /etc/depmod.d/.\n");
1333 /* default config */
1334 parse_config_scan("/etc/depmod.d", basedir, kernelversion,
1335 search, overrides);
1338 /* Local to main, but not freed on exit. Keep valgrind quiet. */
1339 struct module *list = NULL;
1340 struct module_search *search = NULL;
1341 struct module_overrides *overrides = NULL;
1343 int main(int argc, char *argv[])
1345 int opt, all = 0, maybe_all = 0, doing_stdout = 0;
1346 char *basedir = "", *dirname, *version;
1347 char *system_map = NULL, *module_symvers = NULL;
1348 int i;
1349 const char *config = NULL;
1351 if (native_endianness() == 0)
1352 abort();
1354 while ((opt = getopt_long(argc, argv, "aAb:C:E:F:euqrvnhVwm", options, NULL))
1355 != -1) {
1356 switch (opt) {
1357 case 'a':
1358 all = 1;
1359 break;
1360 case 'A':
1361 maybe_all = 1;
1362 break;
1363 case 'b':
1364 basedir = optarg;
1365 skipchars = strlen(basedir);
1366 break;
1367 case 'C':
1368 config = optarg;
1369 break;
1370 case 'E':
1371 module_symvers = optarg;
1372 check_symvers = 1;
1373 break;
1374 case 'F':
1375 system_map = optarg;
1376 break;
1377 case 'e':
1378 print_unknown = 1;
1379 break;
1380 case 'u':
1381 case 'q':
1382 case 'r':
1383 break;
1384 case 'v':
1385 verbose = 1;
1386 break;
1387 case 'n':
1388 doing_stdout = 1;
1389 break;
1390 case 'h':
1391 print_usage(argv[0]);
1392 exit(0);
1393 break;
1394 case 'V':
1395 printf("%s %s\n", PACKAGE, VERSION);
1396 exit(0);
1397 case 'w':
1398 warn_dups = 1;
1399 break;
1400 case 'm':
1401 force_map_files = 1;
1402 break;
1403 default:
1404 print_usage(argv[0]);
1405 exit(1);
1409 if (module_symvers)
1410 load_module_symvers(module_symvers);
1411 else if (system_map)
1412 load_system_map(system_map);
1413 else if (print_unknown) {
1414 warn("-e needs -E or -F\n");
1415 print_unknown = 0;
1418 /* They can specify the version naked on the command line */
1419 if (optind < argc && is_version_number(argv[optind])) {
1420 version = NOFAIL(strdup(argv[optind]));
1421 optind++;
1422 } else {
1423 struct utsname buf;
1424 uname(&buf);
1425 version = NOFAIL(strdup(buf.release));
1428 /* Check for old version. */
1429 if (old_module_version(version)) {
1430 fprintf(stderr, "Kernel version %s requires old depmod\n",
1431 version);
1432 exit(2);
1435 /* Depmod -a by default if no names. */
1436 if (optind == argc)
1437 all = 1;
1439 nofail_asprintf(&dirname, "%s%s%s", basedir, MODULE_DIR, version);
1441 if (maybe_all) {
1442 if (!doing_stdout && !depfile_out_of_date(dirname))
1443 exit(0);
1444 all = 1;
1447 parse_toplevel_config(config, basedir, version, &search, &overrides);
1449 /* For backward compatibility add "updates" to the head of the search
1450 * list here. But only if there was no "search" option specified.
1452 if (!search) {
1453 char *dirname;
1454 size_t len;
1456 nofail_asprintf(&dirname, "%s%s%s/updates", basedir,
1457 MODULE_DIR, version);
1458 len = strlen(dirname);
1459 search = add_search(dirname, len, search);
1461 if (!all) {
1462 /* Do command line args. */
1463 for (opt = optind; opt < argc; opt++) {
1464 struct module *new;
1466 if (argv[opt][0] != '/')
1467 fatal("modules must be specified using absolute paths.\n"
1468 "\"%s\" is a relative path\n", argv[opt]);
1470 new = grab_module(NULL, argv[opt]);
1471 if (!new) {
1472 /* cmd-line specified modules must exist */
1473 fatal("grab_module() failed for module %s\n", argv[opt]);
1475 new->next = list;
1476 list = new;
1478 } else {
1479 list = grab_basedir(dirname,search,overrides);
1481 list = sort_modules(dirname,list);
1482 list = parse_modules(list);
1484 for (i = 0; i < sizeof(depfiles)/sizeof(depfiles[0]); i++) {
1485 FILE *out;
1486 int res;
1487 struct depfile *d = &depfiles[i];
1488 char depname[strlen(dirname) + 1 + strlen(d->name) + 1];
1489 char tmpname[strlen(dirname) + 1 + strlen(d->name) +
1490 strlen(".temp") + 1];
1492 if (d->map_file && !make_map_files && !force_map_files)
1493 continue;
1495 sprintf(depname, "%s/%s", dirname, d->name);
1496 sprintf(tmpname, "%s/%s.temp", dirname, d->name);
1497 if (!doing_stdout) {
1498 out = fopen(tmpname, "w");
1499 if (!out)
1500 fatal("Could not open %s for writing: %s\n",
1501 tmpname, strerror(errno));
1502 } else {
1503 out = stdout;
1504 if (ends_in(depname, ".bin"))
1505 continue;
1507 res = d->func(list, out, dirname);
1508 if (doing_stdout)
1509 continue;
1510 fclose(out);
1511 if (res) {
1512 if (rename(tmpname, depname) < 0)
1513 fatal("Could not rename %s into %s: %s\n",
1514 tmpname, depname, strerror(errno));
1515 } else {
1516 if (unlink(tmpname) < 0)
1517 warn("Could not delete %s: %s\n",
1518 tmpname, strerror(errno));
1522 free(dirname);
1523 free(version);
1525 return 0;