meta: update various documentation, logs, authors, etc.
[mit.git] / depmod.c
blobb739fa99a5329dc39f5f1ad28547e875e5160916
1 /* New simplified depmod without backwards compat stuff and not
2 requiring ksyms.
4 (C) 2010 Jon Masters <jcm@jonmasters.org>, and others.
5 (C) 2002 Rusty Russell IBM Corporation
6 */
7 #define _GNU_SOURCE /* asprintf */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <getopt.h>
12 #include <string.h>
13 #include <errno.h>
14 #include <unistd.h>
15 #include <elf.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 #include <dirent.h>
20 #include <sys/utsname.h>
21 #include <sys/mman.h>
23 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
25 #include "util.h"
26 #include "zlibsupport.h"
27 #include "depmod.h"
28 #include "logging.h"
29 #include "index.h"
30 #include "elfops.h"
31 #include "tables.h"
32 #include "config_filter.h"
34 #include "testing.h"
36 #ifndef MODULE_DIR
37 #define MODULE_DIR "/lib/modules/"
38 #endif
40 #ifndef MODULE_BUILTIN_KEY
41 #define MODULE_BUILTIN_KEY "built-in"
42 #endif
44 struct module_overrides
46 /* Next override */
47 struct module_overrides *next;
49 /* overridden module */
50 char *modfile;
53 struct module_search
55 /* Next search */
56 struct module_search *next;
58 /* search path */
59 char *search_path;
60 size_t len;
63 static unsigned int skipchars;
64 static unsigned int make_map_files = 1; /* default to on */
65 static unsigned int force_map_files = 0; /* default to on */
67 #define SYMBOL_HASH_SIZE 1024
68 struct symbol
70 struct symbol *next;
71 struct module *owner;
72 uint64_t ver;
73 char name[0];
76 static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
78 /* This is based on the hash agorithm from gdbm, via tdb */
79 static inline unsigned int tdb_hash(const char *name)
81 unsigned value; /* Used to compute the hash value. */
82 unsigned i; /* Used to cycle through random values. */
84 /* Set the initial value from the key size. */
85 for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
86 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
88 return (1103515243 * value + 12345);
91 void add_symbol(const char *name, uint64_t ver, struct module *owner)
93 unsigned int hash;
94 struct symbol *new = NOFAIL(malloc(sizeof *new + strlen(name) + 1));
96 new->owner = owner;
97 new->ver = ver;
98 strcpy(new->name, name);
100 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
101 new->next = symbolhash[hash];
102 symbolhash[hash] = new;
105 static int print_unknown, check_symvers;
107 struct module *find_symbol(const char *name, uint64_t ver,
108 const char *modname, int weak)
110 struct symbol *s;
112 /* For our purposes, .foo matches foo. PPC64 needs this. */
113 if (name[0] == '.')
114 name++;
116 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) {
117 if (streq(s->name, name))
118 break;
120 if (s) {
121 if (ver && s->ver && s->ver != ver && print_unknown && !weak)
122 warn("%s disagrees about version of symbol %s\n",
123 modname, name);
124 return s->owner;
127 if (print_unknown && !weak)
128 warn("%s needs unknown symbol %s\n", modname, name);
130 return NULL;
133 void add_dep(struct module *mod, struct module *depends_on)
135 unsigned int i;
137 for (i = 0; i < mod->num_deps; i++)
138 if (mod->deps[i] == depends_on)
139 return;
141 mod->deps = NOFAIL(realloc(mod->deps, sizeof(mod->deps[0])*(mod->num_deps+1)));
142 mod->deps[mod->num_deps++] = depends_on;
145 static void add_fake_syms(void)
147 /* __this_module is magic inserted by kernel loader. */
148 add_symbol("__this_module", 0, NULL);
149 /* On S390, this is faked up too */
150 add_symbol("_GLOBAL_OFFSET_TABLE_", 0, NULL);
153 static void load_system_map(const char *filename)
155 FILE *system_map;
156 char line[10240];
157 const char ksymstr[] = "__ksymtab_";
158 const int ksymstr_len = strlen(ksymstr);
160 system_map = fopen(filename, "r");
161 if (!system_map)
162 fatal("Could not open '%s': %s\n", filename, strerror(errno));
164 /* eg. c0294200 R __ksymtab_devfs_alloc_devnum */
165 while (fgets(line, sizeof(line)-1, system_map)) {
166 char *ptr;
168 /* Snip \n */
169 ptr = strchr(line, '\n');
170 if (ptr)
171 *ptr = '\0';
173 ptr = strchr(line, ' ');
174 if (!ptr || !(ptr = strchr(ptr + 1, ' ')))
175 continue;
177 /* Covers gpl-only and normal symbols. */
178 if (strstarts(ptr+1, ksymstr))
179 add_symbol(ptr+1+ksymstr_len, 0, NULL);
182 fclose(system_map);
183 add_fake_syms();
186 static void load_module_symvers(const char *filename)
188 FILE *module_symvers;
189 char line[10240];
191 module_symvers = fopen(filename, "r");
192 if (!module_symvers)
193 fatal("Could not open '%s': %s\n", filename, strerror(errno));
195 /* eg. "0xb352177e\tfind_first_bit\tvmlinux\tEXPORT_SYMBOL" */
196 while (fgets(line, sizeof(line)-1, module_symvers)) {
197 const char *ver, *sym, *where;
199 ver = strtok(line, " \t");
200 sym = strtok(NULL, " \t");
201 where = strtok(NULL, " \t");
202 if (!ver || !sym || !where)
203 continue;
205 if (streq(where, "vmlinux"))
206 add_symbol(sym, strtoull(ver, NULL, 16), NULL);
209 fclose(module_symvers);
210 add_fake_syms();
213 static struct option options[] = { { "all", 0, NULL, 'a' },
214 { "quick", 0, NULL, 'A' },
215 { "basedir", 1, NULL, 'b' },
216 { "config", 1, NULL, 'C' },
217 { "symvers", 1, NULL, 'E' },
218 { "filesyms", 1, NULL, 'F' },
219 { "errsyms", 0, NULL, 'e' },
220 { "unresolved-error", 0, NULL, 'u' },
221 { "quiet", 0, NULL, 'q' },
222 { "root", 0, NULL, 'r' },
223 { "verbose", 0, NULL, 'v' },
224 { "show", 0, NULL, 'n' },
225 { "dry-run", 0, NULL, 'n' },
226 { "help", 0, NULL, 'h' },
227 { "version", 0, NULL, 'V' },
228 { "warn", 0, NULL, 'w' },
229 { "map", 0, NULL, 'm' },
230 { NULL, 0, NULL, 0 } };
232 /* Version number or module name? Don't assume extension. */
233 static int is_version_number(const char *version)
235 unsigned int dummy;
237 return (sscanf(version, "%u.%u.%u", &dummy, &dummy, &dummy) == 3);
240 static int old_module_version(const char *version)
242 /* Expect three part version. */
243 unsigned int major, sub, minor;
245 sscanf(version, "%u.%u.%u", &major, &sub, &minor);
247 if (major > 2) return 0;
248 if (major < 2) return 1;
250 /* 2.x */
251 if (sub > 5) return 0;
252 if (sub < 5) return 1;
254 /* 2.5.x */
255 if (minor >= 48) return 0;
256 return 1;
259 static void print_usage(const char *name)
261 fprintf(stderr,
262 "%s " VERSION " -- part of " PACKAGE "\n"
263 "%s -[aA] [-n -e -v -q -V -r -u -w -m]\n"
264 " [-b basedirectory] [forced_version]\n"
265 "depmod [-n -e -v -q -r -u -w] [-F kernelsyms] module1.ko module2.ko ...\n"
266 "If no arguments (except options) are given, \"depmod -a\" is assumed\n"
267 "\n"
268 "depmod will output a dependancy list suitable for the modprobe utility.\n"
269 "\n"
270 "\n"
271 "Options:\n"
272 "\t-a, --all Probe all modules\n"
273 "\t-A, --quick Only does the work if there's a new module\n"
274 "\t-e, --errsyms Report not supplied symbols\n"
275 "\t-m, --map Create the legacy map files\n"
276 "\t-n, --show Write the dependency file on stdout only\n"
277 "\t-V, --version Print the release version\n"
278 "\t-v, --verbose Enable verbose mode\n"
279 "\t-w, --warn Warn on duplicates\n"
280 "\t-h, --help Print this usage message\n"
281 "\n"
282 "The following options are useful for people managing distributions:\n"
283 "\t-b basedirectory\n"
284 "\t --basedir basedirectory Use an image of a module tree.\n"
285 "\t-F kernelsyms\n"
286 "\t --filesyms kernelsyms Use the file instead of the\n"
287 "\t current kernel symbols.\n"
288 "\t-E Module.symvers\n"
289 "\t --symvers Module.symvers Use Module.symvers file to check\n"
290 "\t symbol versions.\n",
291 "depmod", "depmod");
294 static int ends_in(const char *name, const char *ext)
296 unsigned int namelen, extlen;
298 /* Grab lengths */
299 namelen = strlen(name);
300 extlen = strlen(ext);
302 if (namelen < extlen) return 0;
304 if (streq(name + namelen - extlen, ext))
305 return 1;
306 return 0;
309 static struct module *grab_module(const char *dirname, const char *filename)
311 struct module *new;
313 new = NOFAIL(malloc(sizeof(*new)
314 + strlen(dirname?:"") + 1 + strlen(filename) + 1));
315 if (dirname)
316 sprintf(new->pathname, "%s/%s", dirname, filename);
317 else
318 strcpy(new->pathname, filename);
319 new->basename = my_basename(new->pathname);
321 INIT_LIST_HEAD(&new->dep_list);
322 new->order = INDEX_PRIORITY_MIN;
324 new->file = grab_elf_file(new->pathname);
325 if (!new->file) {
326 warn("Can't read module %s: %s\n",
327 new->pathname, strerror(errno));
328 free(new);
329 return NULL;
331 return new;
334 struct module_traverse
336 struct module_traverse *prev;
337 struct module *mod;
340 static int in_loop(struct module *mod, const struct module_traverse *traverse)
342 const struct module_traverse *i;
344 for (i = traverse; i; i = i->prev) {
345 if (i->mod == mod)
346 return 1;
348 return 0;
351 /* Assume we are doing all the modules, so only report each loop once. */
352 static void report_loop(const struct module *mod,
353 const struct module_traverse *traverse)
355 const struct module_traverse *i;
357 /* Check that start is least alphabetically. eg. a depends
358 on b depends on a will get reported for a, not b. */
359 for (i = traverse->prev; i->prev; i = i->prev) {
360 if (strcmp(mod->pathname, i->mod->pathname) > 0)
361 return;
364 /* Is start in the loop? If not, don't report now. eg. a
365 depends on b which depends on c which depends on b. Don't
366 report when generating depends for a. */
367 if (mod != i->mod)
368 return;
370 warn("Loop detected: %s ", mod->pathname);
371 for (i = traverse->prev; i->prev; i = i->prev)
372 fprintf(stderr, "needs %s ", i->mod->basename);
373 fprintf(stderr, "which needs %s again!\n", i->mod->basename);
376 /* This is damn slow, but loops actually happen, and we don't want to
377 just exit() and leave the user without any modules. */
378 static int has_dep_loop(struct module *module, struct module_traverse *prev)
380 unsigned int i;
381 struct module_traverse traverse = { .prev = prev, .mod = module };
383 if (in_loop(module, prev)) {
384 report_loop(module, &traverse);
385 return 1;
388 for (i = 0; i < module->num_deps; i++)
389 if (has_dep_loop(module->deps[i], &traverse))
390 return 1;
391 return 0;
394 /* Uniquifies and orders a dependency list. */
395 static void order_dep_list(struct module *start, struct module *mod)
397 unsigned int i;
399 for (i = 0; i < mod->num_deps; i++) {
400 /* If it was previously depended on, move it to the
401 tail. ie. if a needs b and c, and c needs b, we
402 must order b after c. */
403 list_del(&mod->deps[i]->dep_list);
404 list_add_tail(&mod->deps[i]->dep_list, &start->dep_list);
405 order_dep_list(start, mod->deps[i]);
409 static struct module *deleted = NULL;
411 static void del_module(struct module **modules, struct module *delme)
413 struct module **i;
415 /* Find pointer to it. */
416 if (modules) {
417 for (i = modules; *i != delme; i = &(*i)->next);
419 *i = delme->next;
422 /* Save on a list to quiet valgrind.
423 Can't free - other modules may depend on them */
424 delme->next = deleted;
425 deleted = delme;
428 /* convert to relative path if possible */
429 static const char *compress_path(const char *path, const char *basedir)
431 int len = strlen(basedir);
433 if (strncmp(path, basedir, len) == 0)
434 path += len + 1;
435 return path;
438 static int output_deps(struct module *modules,
439 FILE *out, char *dirname)
441 struct module *i;
443 for (i = modules; i; i = i->next) {
444 struct list_head *j, *tmp;
445 order_dep_list(i, i);
447 fprintf(out, "%s:", compress_path(i->pathname, dirname));
448 list_for_each_safe(j, tmp, &i->dep_list) {
449 struct module *dep
450 = list_entry(j, struct module, dep_list);
451 fprintf(out, " %s",
452 compress_path(dep->pathname, dirname));
453 list_del_init(j);
455 fprintf(out, "\n");
457 return 1;
460 /* warn whenever duplicate module aliases, deps, or symbols are found. */
461 int warn_dups = 0;
463 static int output_deps_bin(struct module *modules,
464 FILE *out, char *dirname)
466 struct module *i;
467 struct index_node *index;
468 char *line;
469 char *p;
471 index = index_create();
473 for (i = modules; i; i = i->next) {
474 struct list_head *j, *tmp;
475 char modname[strlen(i->pathname)+1];
477 order_dep_list(i, i);
479 filename2modname(modname, i->pathname);
480 nofail_asprintf(&line, "%s:",
481 compress_path(i->pathname, dirname));
482 p = line;
483 list_for_each_safe(j, tmp, &i->dep_list) {
484 struct module *dep
485 = list_entry(j, struct module, dep_list);
486 nofail_asprintf(&line, "%s %s",
488 compress_path(dep->pathname, dirname));
489 free(p);
490 p = line;
491 list_del_init(j);
493 if (index_insert(index, modname, line, i->order) && warn_dups)
494 warn("duplicate module deps:\n%s\n",line);
495 free(line);
498 index_write(index, out);
499 index_destroy(index);
501 return 1;
505 static int smells_like_module(const char *name)
507 return ends_in(name,".ko") || ends_in(name, ".ko.gz");
510 typedef struct module *(*do_module_t)(const char *dirname,
511 const char *filename,
512 struct module *next,
513 struct module_search *search,
514 struct module_overrides *overrides);
516 static int is_higher_priority(const char *newpath, const char *oldpath,
517 struct module_search *search,
518 struct module_overrides *overrides)
520 struct module_search *tmp;
521 struct module_overrides *ovtmp;
522 int i = 0;
523 int prio_builtin = -1;
524 int prio_new = -1;
525 int prio_old = -1;
527 /* The names already match, now we check for overrides and directory search
528 * order
530 for (ovtmp = overrides; ovtmp != NULL; ovtmp = ovtmp->next) {
531 if (streq(ovtmp->modfile, newpath))
532 return 1;
533 if (streq(ovtmp->modfile, oldpath))
534 return 0;
536 for (i = 0, tmp = search; tmp != NULL; tmp = tmp->next, i++) {
537 if (streq(tmp->search_path, MODULE_BUILTIN_KEY))
538 prio_builtin = i;
539 else if (strncmp(tmp->search_path, newpath, tmp->len) == 0)
540 prio_new = i;
541 else if (strncmp(tmp->search_path, oldpath, tmp->len) == 0)
542 prio_old = i;
544 if (prio_new < 0)
545 prio_new = prio_builtin;
546 if (prio_old < 0)
547 prio_old = prio_builtin;
549 return prio_new > prio_old;
553 static struct module *do_module(const char *dirname,
554 const char *filename,
555 struct module *list,
556 struct module_search *search,
557 struct module_overrides *overrides)
559 struct module *new, **i;
561 new = grab_module(dirname, filename);
562 if (!new)
563 return list;
565 /* Check if module is already in the list. */
566 for (i = &list; *i; i = &(*i)->next) {
568 if (streq((*i)->basename, filename)) {
569 char newpath[strlen(dirname) + strlen("/")
570 + strlen(filename) + 1];
572 sprintf(newpath, "%s/%s", dirname, filename);
574 if (is_higher_priority(newpath, (*i)->pathname,search,
575 overrides)) {
576 del_module(i, *i);
578 new->next = *i;
579 *i = new;
580 } else
581 del_module(NULL, new);
583 return list;
587 /* Not in the list already. Just prepend. */
588 new->next = list;
589 return new;
592 static struct module *grab_dir(const char *dirname,
593 DIR *dir,
594 struct module *next,
595 do_module_t do_mod,
596 struct module_search *search,
597 struct module_overrides *overrides)
599 struct dirent *dirent;
601 while ((dirent = readdir(dir)) != NULL) {
602 if (smells_like_module(dirent->d_name))
603 next = do_mod(dirname, dirent->d_name, next,
604 search, overrides);
605 else if (!streq(dirent->d_name, ".")
606 && !streq(dirent->d_name, "..")
607 && !streq(dirent->d_name, "source")
608 && !streq(dirent->d_name, "build")) {
610 DIR *sub;
611 char subdir[strlen(dirname) + 1
612 + strlen(dirent->d_name) + 1];
613 sprintf(subdir, "%s/%s", dirname, dirent->d_name);
614 sub = opendir(subdir);
615 if (sub) {
616 next = grab_dir(subdir, sub, next, do_mod,
617 search, overrides);
618 closedir(sub);
622 return next;
625 static struct module *grab_basedir(const char *dirname,
626 struct module_search *search,
627 struct module_overrides *overrides)
629 DIR *dir;
630 struct module *list;
632 dir = opendir(dirname);
633 if (!dir) {
634 warn("Couldn't open directory %s: %s\n",
635 dirname, strerror(errno));
636 return NULL;
638 list = grab_dir(dirname, dir, NULL, do_module, search, overrides);
639 closedir(dir);
641 return list;
644 static struct module *sort_modules(const char *dirname, struct module *list)
646 struct module *tlist = NULL, **tpos = &tlist;
647 FILE *modorder;
648 int dir_len = strlen(dirname) + 1;
649 char file_name[dir_len + strlen("modules.order") + 1];
650 char line[10240];
651 unsigned int linenum = 0;
653 sprintf(file_name, "%s/%s", dirname, "modules.order");
655 modorder = fopen(file_name, "r");
656 if (!modorder) {
657 /* Older kernels don't generate modules.order. Just
658 return if the file doesn't exist. */
659 if (errno == ENOENT)
660 return list;
661 fatal("Could not open '%s': %s\n", file_name, strerror(errno));
664 sprintf(line, "%s/", dirname);
666 /* move modules listed in modorder file to tlist in order */
667 while (fgets(line, sizeof(line), modorder)) {
668 struct module **pos, *mod;
669 int len = strlen(line);
671 linenum++;
672 if (line[len - 1] == '\n')
673 line[len - 1] = '\0';
675 for (pos = &list; (mod = *pos); pos = &(*pos)->next) {
676 if (streq(line, mod->pathname + dir_len)) {
677 mod->order = linenum;
678 *pos = mod->next;
679 mod->next = NULL;
680 *tpos = mod;
681 tpos = &mod->next;
682 break;
687 /* append the rest */
688 *tpos = list;
690 fclose(modorder);
692 return tlist;
695 /* Calculate the dependencies for this module */
696 static void calculate_deps(struct module *module)
698 unsigned int i;
699 struct string_table *symnames;
700 struct string_table *symtypes;
701 uint64_t *symvers = NULL;
702 struct elf_file *file;
704 module->num_deps = 0;
705 module->deps = NULL;
706 file = module->file;
708 symnames = file->ops->load_dep_syms(file, &symtypes,
709 check_symvers ? &symvers : NULL);
710 if (!symnames || !symtypes)
711 return;
713 for (i = 0; i < symnames->cnt; i++) {
714 const char *name;
715 uint64_t ver;
716 struct module *owner;
717 int weak;
719 name = symnames->str[i];
720 ver = symvers ? symvers[i] : 0;
721 weak = (*(symtypes->str[i]) == 'W');
722 owner = find_symbol(name, ver, module->pathname, weak);
723 if (owner) {
724 info("%s needs \"%s\": %s\n",
725 module->pathname, name,
726 owner->pathname);
727 add_dep(module, owner);
731 free(symnames);
732 free(symtypes);
733 free(symvers);
736 static struct module *parse_modules(struct module *list)
738 struct module *i;
739 struct elf_file *file;
740 struct string_table *syms;
741 int j;
743 for (i = list; i; i = i->next) {
744 uint64_t *symvers = NULL;
745 file = i->file;
746 syms = file->ops->load_symbols(file,
747 check_symvers ? &symvers : NULL);
748 if (syms) {
749 for (j = 0; j < syms->cnt; j++)
750 add_symbol(syms->str[j],
751 symvers ? symvers[j] : 0, i);
752 strtbl_free(syms);
754 free(symvers);
755 file->ops->fetch_tables(file, &i->tables);
758 for (i = list; i; i = i->next)
759 calculate_deps(i);
761 /* Strip out modules with dependency loops. */
762 again:
763 for (i = list; i; i = i->next) {
764 if (has_dep_loop(i, NULL)) {
765 warn("Module %s ignored, due to loop\n",
766 i->pathname + skipchars);
767 del_module(&list, i);
768 goto again;
772 return list;
775 /* Simply dump hash table. */
776 static int output_symbols(struct module *unused, FILE *out, char *dirname)
778 unsigned int i;
780 fprintf(out, "# Aliases for symbols, used by symbol_request().\n");
781 for (i = 0; i < SYMBOL_HASH_SIZE; i++) {
782 struct symbol *s;
784 for (s = symbolhash[i]; s; s = s->next) {
785 if (s->owner) {
786 char modname[strlen(s->owner->pathname)+1];
787 filename2modname(modname, s->owner->pathname);
788 fprintf(out, "alias symbol:%s %s\n",
789 s->name, modname);
793 return 1;
796 static int output_symbols_bin(struct module *unused, FILE *out, char *dirname)
798 struct index_node *index;
799 unsigned int i;
800 char *alias;
801 int duplicate;
803 index = index_create();
805 for (i = 0; i < SYMBOL_HASH_SIZE; i++) {
806 struct symbol *s;
808 for (s = symbolhash[i]; s; s = s->next) {
809 if (s->owner) {
810 char modname[strlen(s->owner->pathname)+1];
811 filename2modname(modname, s->owner->pathname);
812 nofail_asprintf(&alias, "symbol:%s", s->name);
813 duplicate = index_insert(index, alias, modname,
814 s->owner->order);
815 if (duplicate && warn_dups)
816 warn("duplicate module syms:\n%s %s\n",
817 alias, modname);
818 free(alias);
823 index_write(index, out);
824 index_destroy(index);
826 return 1;
829 static int output_builtin_bin(struct module *unused, FILE *out, char *dirname)
831 struct index_node *index;
832 char *textfile, *line;
833 unsigned int linenum;
834 FILE *f;
836 nofail_asprintf(&textfile, "%s/modules.builtin", dirname);
837 if (!(f = fopen(textfile, "r"))) {
838 if (errno != ENOENT)
839 fatal("Could not open '%s': %s\n",
840 textfile, strerror(errno));
841 free(textfile);
842 return 0;
844 free(textfile);
845 index = index_create();
847 while ((line = getline_wrapped(f, &linenum)) != NULL) {
848 char *module = line;
850 if (!*line || *line == '#') {
851 free(line);
852 continue;
854 filename2modname(module, module);
855 index_insert(index, module, "", 0);
856 free(line);
858 fclose(f);
859 index_write(index, out);
860 index_destroy(index);
862 return 1;
865 static int output_aliases(struct module *modules, FILE *out, char *dirname)
867 struct module *i;
868 struct elf_file *file;
869 struct string_table *tbl;
870 int j;
872 fprintf(out, "# Aliases extracted from modules themselves.\n");
873 for (i = modules; i; i = i->next) {
874 char modname[strlen(i->pathname)+1];
876 file = i->file;
877 filename2modname(modname, i->pathname);
879 /* Grab from old-style .modalias section. */
880 tbl = file->ops->load_strings(file, ".modalias", NULL, fatal);
881 for (j = 0; tbl && j < tbl->cnt; j++)
882 fprintf(out, "alias %s %s\n", tbl->str[j], modname);
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 fprintf(out, "alias %s %s\n",
891 p + strlen("alias="), modname);
893 strtbl_free(tbl);
895 return 1;
898 static int output_aliases_bin(struct module *modules, FILE *out, char *dirname)
900 struct module *i;
901 struct elf_file *file;
902 struct string_table *tbl;
903 int j;
904 char *alias;
905 struct index_node *index;
906 int duplicate;
908 index = index_create();
910 for (i = modules; i; i = i->next) {
911 char modname[strlen(i->pathname)+1];
913 file = i->file;
914 filename2modname(modname, i->pathname);
916 /* Grab from old-style .modalias section. */
917 tbl = file->ops->load_strings(file, ".modalias", NULL, fatal);
918 for (j = 0; tbl && j < tbl->cnt; j++) {
919 alias = NOFAIL(strdup(tbl->str[j]));
920 underscores(alias);
921 duplicate = index_insert(index, alias, modname, i->order);
922 if (duplicate && warn_dups)
923 warn("duplicate module alias:\n%s %s\n",
924 alias, modname);
925 free(alias);
927 strtbl_free(tbl);
929 /* Grab from new-style .modinfo section. */
930 tbl = file->ops->load_strings(file, ".modinfo", NULL, fatal);
931 for (j = 0; tbl && j < tbl->cnt; j++) {
932 const char *p = tbl->str[j];
933 if (strstarts(p, "alias=")) {
934 alias = NOFAIL(strdup(p + strlen("alias=")));
935 underscores(alias);
936 duplicate = index_insert(index, alias, modname, i->order);
937 if (duplicate && warn_dups)
938 warn("duplicate module alias:\n%s %s\n",
939 alias, modname);
940 free(alias);
943 strtbl_free(tbl);
946 index_write(index, out);
947 index_destroy(index);
949 return 1;
952 struct depfile {
953 char *name;
954 int (*func)(struct module *, FILE *, char *dirname);
955 int map_file;
958 static struct depfile depfiles[] = {
959 { "modules.dep", output_deps, 0 }, /* This is what we check for '-A'. */
960 { "modules.dep.bin", output_deps_bin, 0 },
961 { "modules.pcimap", output_pci_table, 1 },
962 { "modules.usbmap", output_usb_table, 1 },
963 { "modules.ccwmap", output_ccw_table, 1 },
964 { "modules.ieee1394map", output_ieee1394_table, 1 },
965 { "modules.isapnpmap", output_isapnp_table, 1 },
966 { "modules.inputmap", output_input_table, 1 },
967 { "modules.ofmap", output_of_table, 1 },
968 { "modules.seriomap", output_serio_table, 1 },
969 { "modules.alias", output_aliases, 0 },
970 { "modules.alias.bin", output_aliases_bin, 0 },
971 { "modules.symbols", output_symbols, 0 },
972 { "modules.symbols.bin", output_symbols_bin, 0 },
973 { "modules.builtin.bin", output_builtin_bin, 0 },
976 /* If we can't figure it out, it's safe to say "true". */
977 static int any_modules_newer(const char *dirname, time_t mtime)
979 DIR *dir;
980 struct dirent *dirent;
982 dir = opendir(dirname);
983 if (!dir)
984 return 1;
986 while ((dirent = readdir(dir)) != NULL) {
987 struct stat st;
988 char file[strlen(dirname) + 1 + strlen(dirent->d_name) + 1];
990 if (streq(dirent->d_name, ".") || streq(dirent->d_name, ".."))
991 continue;
993 sprintf(file, "%s/%s", dirname, dirent->d_name);
994 if (lstat(file, &st) != 0)
995 goto ret_true;
997 if (smells_like_module(dirent->d_name)) {
998 if (st.st_mtime > mtime)
999 goto ret_true;
1000 } else if (S_ISDIR(st.st_mode)) {
1001 if (any_modules_newer(file, mtime))
1002 goto ret_true;
1005 closedir(dir);
1006 return 0;
1008 ret_true:
1009 closedir(dir);
1010 return 1;
1013 static int depfile_out_of_date(const char *dirname)
1015 struct stat st;
1016 char depfile[strlen(dirname) + 1 + strlen(depfiles[0].name) + 1];
1018 sprintf(depfile, "%s/%s", dirname, depfiles[0].name);
1020 if (stat(depfile, &st) != 0)
1021 return 1;
1023 return any_modules_newer(dirname, st.st_mtime);
1026 static char *strsep_skipspace(char **string, char *delim)
1028 if (!*string)
1029 return NULL;
1030 *string += strspn(*string, delim);
1031 return strsep(string, delim);
1034 static struct module_search *add_search(const char *search_path,
1035 size_t len,
1036 struct module_search *search)
1039 struct module_search *new;
1041 new = NOFAIL(malloc(sizeof(*new)));
1042 new->search_path = NOFAIL(strdup(search_path));
1043 new->len = len;
1044 new->next = search;
1046 return new;
1050 static struct module_overrides *add_override(const char *modfile,
1051 struct module_overrides *overrides)
1054 struct module_overrides *new;
1056 new = NOFAIL(malloc(sizeof(*new)));
1057 new->modfile = NOFAIL(strdup(modfile));
1058 new->next = overrides;
1060 return new;
1064 static int parse_config_scan(const char *filename,
1065 const char *basedir,
1066 const char *kernelversion,
1067 struct module_search **search,
1068 struct module_overrides **overrides);
1070 static int parse_config_file(const char *filename,
1071 const char *basedir,
1072 const char *kernelversion,
1073 struct module_search **search,
1074 struct module_overrides **overrides)
1076 char *line;
1077 unsigned int linenum = 0;
1078 FILE *cfile;
1080 cfile = fopen(filename, "r");
1081 if (!cfile) {
1082 if (errno != ENOENT)
1083 fatal("could not open '%s', reason: %s\n", filename,
1084 strerror(errno));
1085 return 0;
1088 while ((line = getline_wrapped(cfile, &linenum)) != NULL) {
1089 char *ptr = line;
1090 char *cmd, *modname;
1092 cmd = strsep_skipspace(&ptr, "\t ");
1094 if (cmd == NULL || cmd[0] == '#' || cmd[0] == '\0') {
1095 free(line);
1096 continue;
1099 if (streq(cmd, "search")) {
1100 char *search_path;
1102 while ((search_path = strsep_skipspace(&ptr, "\t "))) {
1103 char *dirname;
1104 size_t len;
1106 if (strcmp(search_path,
1107 MODULE_BUILTIN_KEY) == 0) {
1108 *search = add_search(MODULE_BUILTIN_KEY,
1109 0, *search);
1110 continue;
1112 nofail_asprintf(&dirname, "%s%s%s/%s", basedir,
1113 MODULE_DIR, kernelversion, search_path);
1114 len = strlen(dirname);
1115 *search = add_search(dirname, len, *search);
1116 free(dirname);
1118 } else if (streq(cmd, "override")) {
1119 char *pathname = NULL, *version, *subdir;
1120 modname = strsep_skipspace(&ptr, "\t ");
1121 version = strsep_skipspace(&ptr, "\t ");
1122 subdir = strsep_skipspace(&ptr, "\t ");
1124 if (!regex_match(kernelversion, (const char *)version))
1125 continue;
1127 nofail_asprintf(&pathname, "%s%s%s/%s/%s.ko", basedir,
1128 MODULE_DIR, kernelversion, subdir, modname);
1130 *overrides = add_override(pathname, *overrides);
1131 free(pathname);
1132 } else if (streq(cmd, "include")) {
1133 char *newfilename;
1135 newfilename = strsep_skipspace(&ptr, "\t ");
1136 if (!newfilename) {
1137 grammar(cmd, filename, linenum);
1138 } else {
1139 warn("\"include %s\" is deprecated, "
1140 "please use /etc/depmod.d\n", newfilename);
1141 if (strstarts(newfilename, "/etc/depmod.d")) {
1142 warn("\"include /etc/depmod.d\" is "
1143 "the default, ignored\n");
1144 } else {
1145 if (!parse_config_scan(newfilename, basedir,
1146 kernelversion,
1147 search, overrides))
1148 warn("Failed to open included"
1149 " config file %s: %s\n",
1150 newfilename, strerror(errno));
1153 } else if (streq(cmd, "make_map_files")) {
1154 char *option;
1156 option = strsep_skipspace(&ptr, "\t ");
1157 if (!option)
1158 grammar(cmd, filename, linenum);
1159 else {
1160 if (streq(option, "yes"))
1161 make_map_files = 1;
1162 else if (streq(option, "no"))
1163 make_map_files = 0;
1164 else
1165 grammar(cmd, filename, linenum);
1167 } else
1168 grammar(cmd, filename, linenum);
1170 free(line);
1172 fclose(cfile);
1173 return 1;
1176 static int parse_config_scan(const char *filename,
1177 const char *basedir,
1178 const char *kernelversion,
1179 struct module_search **search,
1180 struct module_overrides **overrides)
1182 DIR *dir;
1183 int ret = 0;
1185 dir = opendir(filename);
1186 if (dir) {
1187 struct file_entry {
1188 struct list_head node;
1189 char name[];
1191 LIST_HEAD(files_list);
1192 struct file_entry *fe, *fe_tmp;
1193 struct dirent *i;
1195 /* sort files from directory into list */
1196 while ((i = readdir(dir)) != NULL) {
1197 size_t len;
1199 if (i->d_name[0] == '.')
1200 continue;
1201 if (!config_filter(i->d_name))
1202 continue;
1204 len = strlen(i->d_name);
1205 if (len < 6 || strcmp(&i->d_name[len-5], ".conf") != 0)
1206 warn("All config files need .conf: %s/%s, "
1207 "it will be ignored in a future release.\n",
1208 filename, i->d_name);
1209 fe = malloc(sizeof(struct file_entry) + len + 1);
1210 if (fe == NULL)
1211 continue;
1212 strcpy(fe->name, i->d_name);
1213 list_for_each_entry(fe_tmp, &files_list, node)
1214 if (strcmp(fe_tmp->name, fe->name) >= 0)
1215 break;
1216 list_add_tail(&fe->node, &fe_tmp->node);
1218 closedir(dir);
1220 /* parse list of files */
1221 list_for_each_entry_safe(fe, fe_tmp, &files_list, node) {
1222 char *cfgfile;
1224 nofail_asprintf(&cfgfile, "%s/%s", filename, fe->name);
1225 if (!parse_config_file(cfgfile, basedir, kernelversion,
1226 search, overrides))
1227 warn("Failed to open config file "
1228 "%s: %s\n", fe->name, strerror(errno));
1229 free(cfgfile);
1230 list_del(&fe->node);
1231 free(fe);
1234 ret = 1;
1235 } else {
1236 if (parse_config_file(filename, basedir, kernelversion, search,
1237 overrides))
1238 ret = 1;
1241 return ret;
1244 static void parse_toplevel_config(const char *filename,
1245 const char *basedir,
1246 const char *kernelversion,
1247 struct module_search **search,
1248 struct module_overrides **overrides)
1250 if (filename) {
1251 if (!parse_config_scan(filename, basedir, kernelversion, search,
1252 overrides))
1253 fatal("Failed to open config file %s: %s\n",
1254 filename, strerror(errno));
1255 return;
1258 /* deprecated config file */
1259 if (parse_config_file("/etc/depmod.conf", basedir, kernelversion,
1260 search, overrides) > 0)
1261 warn("Deprecated config file /etc/depmod.conf, "
1262 "all config files belong into /etc/depmod.d/.\n");
1264 /* default config */
1265 parse_config_scan("/etc/depmod.d", basedir, kernelversion,
1266 search, overrides);
1269 /* Local to main, but not freed on exit. Keep valgrind quiet. */
1270 struct module *list = NULL;
1271 struct module_search *search = NULL;
1272 struct module_overrides *overrides = NULL;
1274 int main(int argc, char *argv[])
1276 int opt, all = 0, maybe_all = 0, doing_stdout = 0;
1277 char *basedir = "", *dirname, *version;
1278 char *system_map = NULL, *module_symvers = NULL;
1279 int i;
1280 const char *config = NULL;
1282 if (native_endianness() == 0)
1283 abort();
1285 while ((opt = getopt_long(argc, argv, "aAb:C:E:F:euqrvnhVwm", options, NULL))
1286 != -1) {
1287 switch (opt) {
1288 case 'a':
1289 all = 1;
1290 break;
1291 case 'A':
1292 maybe_all = 1;
1293 break;
1294 case 'b':
1295 basedir = optarg;
1296 skipchars = strlen(basedir);
1297 break;
1298 case 'C':
1299 config = optarg;
1300 break;
1301 case 'E':
1302 module_symvers = optarg;
1303 check_symvers = 1;
1304 break;
1305 case 'F':
1306 system_map = optarg;
1307 break;
1308 case 'e':
1309 print_unknown = 1;
1310 break;
1311 case 'u':
1312 case 'q':
1313 case 'r':
1314 break;
1315 case 'v':
1316 verbose = 1;
1317 break;
1318 case 'n':
1319 doing_stdout = 1;
1320 break;
1321 case 'h':
1322 print_usage(argv[0]);
1323 exit(0);
1324 break;
1325 case 'V':
1326 printf("%s %s\n", PACKAGE, VERSION);
1327 exit(0);
1328 case 'w':
1329 warn_dups = 1;
1330 break;
1331 case 'm':
1332 force_map_files = 1;
1333 break;
1334 default:
1335 print_usage(argv[0]);
1336 exit(1);
1340 if (module_symvers)
1341 load_module_symvers(module_symvers);
1342 else if (system_map)
1343 load_system_map(system_map);
1344 else if (print_unknown) {
1345 warn("-e needs -E or -F\n");
1346 print_unknown = 0;
1349 /* They can specify the version naked on the command line */
1350 if (optind < argc && is_version_number(argv[optind])) {
1351 version = NOFAIL(strdup(argv[optind]));
1352 optind++;
1353 } else {
1354 struct utsname buf;
1355 uname(&buf);
1356 version = NOFAIL(strdup(buf.release));
1359 /* Check for old version. */
1360 if (old_module_version(version)) {
1361 fprintf(stderr, "Kernel version %s requires old depmod\n",
1362 version);
1363 exit(2);
1366 /* Depmod -a by default if no names. */
1367 if (optind == argc)
1368 all = 1;
1370 nofail_asprintf(&dirname, "%s%s%s", basedir, MODULE_DIR, version);
1372 if (maybe_all) {
1373 if (!doing_stdout && !depfile_out_of_date(dirname))
1374 exit(0);
1375 all = 1;
1378 parse_toplevel_config(config, basedir, version, &search, &overrides);
1380 /* For backward compatibility add "updates" to the head of the search
1381 * list here. But only if there was no "search" option specified.
1383 if (!search) {
1384 char *dirname;
1385 size_t len;
1387 nofail_asprintf(&dirname, "%s%s%s/updates", basedir,
1388 MODULE_DIR, version);
1389 len = strlen(dirname);
1390 search = add_search(dirname, len, search);
1392 if (!all) {
1393 /* Do command line args. */
1394 for (opt = optind; opt < argc; opt++) {
1395 struct module *new;
1397 if (argv[opt][0] != '/')
1398 fatal("modules must be specified using absolute paths.\n"
1399 "\"%s\" is a relative path\n", argv[opt]);
1401 new = grab_module(NULL, argv[opt]);
1402 if (!new) {
1403 /* cmd-line specified modules must exist */
1404 fatal("grab_module() failed for module %s\n", argv[opt]);
1406 new->next = list;
1407 list = new;
1409 } else {
1410 list = grab_basedir(dirname,search,overrides);
1412 list = sort_modules(dirname,list);
1413 list = parse_modules(list);
1415 for (i = 0; i < sizeof(depfiles)/sizeof(depfiles[0]); i++) {
1416 FILE *out;
1417 int res;
1418 struct depfile *d = &depfiles[i];
1419 char depname[strlen(dirname) + 1 + strlen(d->name) + 1];
1420 char tmpname[strlen(dirname) + 1 + strlen(d->name) +
1421 strlen(".temp") + 1];
1423 if (d->map_file && !make_map_files && !force_map_files)
1424 continue;
1426 sprintf(depname, "%s/%s", dirname, d->name);
1427 sprintf(tmpname, "%s/%s.temp", dirname, d->name);
1428 if (!doing_stdout) {
1429 out = fopen(tmpname, "w");
1430 if (!out)
1431 fatal("Could not open %s for writing: %s\n",
1432 tmpname, strerror(errno));
1433 } else {
1434 out = stdout;
1435 if (ends_in(depname, ".bin"))
1436 continue;
1438 res = d->func(list, out, dirname);
1439 if (doing_stdout)
1440 continue;
1441 fclose(out);
1442 if (res) {
1443 if (rename(tmpname, depname) < 0)
1444 fatal("Could not rename %s into %s: %s\n",
1445 tmpname, depname, strerror(errno));
1446 } else {
1447 if (unlink(tmpname) < 0)
1448 warn("Could not delete %s: %s\n",
1449 tmpname, strerror(errno));
1453 free(dirname);
1454 free(version);
1456 return 0;