Add elf_core.c into the EXTRA_SOURCES for the static library
[mit.git] / depmod.c
blob3544b895ce147310bf6aa15373c8839c24065855
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 "moduleops.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 char name[0];
74 static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
76 /* This is based on the hash agorithm from gdbm, via tdb */
77 static inline unsigned int tdb_hash(const char *name)
79 unsigned value; /* Used to compute the hash value. */
80 unsigned i; /* Used to cycle through random values. */
82 /* Set the initial value from the key size. */
83 for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
84 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
86 return (1103515243 * value + 12345);
89 void add_symbol(const char *name, struct module *owner)
91 unsigned int hash;
92 struct symbol *new = NOFAIL(malloc(sizeof *new + strlen(name) + 1));
94 new->owner = owner;
95 strcpy(new->name, name);
97 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
98 new->next = symbolhash[hash];
99 symbolhash[hash] = new;
102 static int print_unknown;
104 struct module *find_symbol(const char *name, const char *modname, int weak)
106 struct symbol *s;
108 /* For our purposes, .foo matches foo. PPC64 needs this. */
109 if (name[0] == '.')
110 name++;
112 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) {
113 if (streq(s->name, name))
114 return s->owner;
117 if (print_unknown && !weak)
118 warn("%s needs unknown symbol %s\n", modname, name);
120 return NULL;
123 void add_dep(struct module *mod, struct module *depends_on)
125 unsigned int i;
127 for (i = 0; i < mod->num_deps; i++)
128 if (mod->deps[i] == depends_on)
129 return;
131 mod->deps = NOFAIL(realloc(mod->deps, sizeof(mod->deps[0])*(mod->num_deps+1)));
132 mod->deps[mod->num_deps++] = depends_on;
135 static void load_system_map(const char *filename)
137 FILE *system_map;
138 char line[10240];
139 const char ksymstr[] = "__ksymtab_";
140 const int ksymstr_len = strlen(ksymstr);
142 system_map = fopen(filename, "r");
143 if (!system_map)
144 fatal("Could not open '%s': %s\n", filename, strerror(errno));
146 /* eg. c0294200 R __ksymtab_devfs_alloc_devnum */
147 while (fgets(line, sizeof(line)-1, system_map)) {
148 char *ptr;
150 /* Snip \n */
151 ptr = strchr(line, '\n');
152 if (ptr)
153 *ptr = '\0';
155 ptr = strchr(line, ' ');
156 if (!ptr || !(ptr = strchr(ptr + 1, ' ')))
157 continue;
159 /* Covers gpl-only and normal symbols. */
160 if (strstarts(ptr+1, ksymstr))
161 add_symbol(ptr+1+ksymstr_len, NULL);
164 fclose(system_map);
166 /* __this_module is magic inserted by kernel loader. */
167 add_symbol("__this_module", NULL);
168 /* On S390, this is faked up too */
169 add_symbol("_GLOBAL_OFFSET_TABLE_", NULL);
172 static struct option options[] = { { "all", 0, NULL, 'a' },
173 { "quick", 0, NULL, 'A' },
174 { "basedir", 1, NULL, 'b' },
175 { "errsyms", 0, NULL, 'e' },
176 { "filesyms", 1, NULL, 'F' },
177 { "help", 0, NULL, 'h' },
178 { "show", 0, NULL, 'n' },
179 { "dry-run", 0, NULL, 'n' },
180 { "quiet", 0, NULL, 'q' },
181 { "root", 0, NULL, 'r' },
182 { "unresolved-error", 0, NULL, 'u' },
183 { "verbose", 0, NULL, 'v' },
184 { "version", 0, NULL, 'V' },
185 { "config", 1, NULL, 'C' },
186 { "warn", 1, NULL, 'w' },
187 { "map", 0, NULL, 'm' },
188 { NULL, 0, NULL, 0 } };
190 /* Version number or module name? Don't assume extension. */
191 static int is_version_number(const char *version)
193 unsigned int dummy;
195 return (sscanf(version, "%u.%u.%u", &dummy, &dummy, &dummy) == 3);
198 static int old_module_version(const char *version)
200 /* Expect three part version. */
201 unsigned int major, sub, minor;
203 sscanf(version, "%u.%u.%u", &major, &sub, &minor);
205 if (major > 2) return 0;
206 if (major < 2) return 1;
208 /* 2.x */
209 if (sub > 5) return 0;
210 if (sub < 5) return 1;
212 /* 2.5.x */
213 if (minor >= 48) return 0;
214 return 1;
217 static void print_usage(const char *name)
219 fprintf(stderr,
220 "%s " VERSION " -- part of " PACKAGE "\n"
221 "%s -[aA] [-n -e -v -q -V -r -u -w -m]\n"
222 " [-b basedirectory] [forced_version]\n"
223 "depmod [-n -e -v -q -r -u -w] [-F kernelsyms] module1.ko module2.ko ...\n"
224 "If no arguments (except options) are given, \"depmod -a\" is assumed\n"
225 "\n"
226 "depmod will output a dependancy list suitable for the modprobe utility.\n"
227 "\n"
228 "\n"
229 "Options:\n"
230 "\t-a, --all Probe all modules\n"
231 "\t-A, --quick Only does the work if there's a new module\n"
232 "\t-e, --errsyms Report not supplied symbols\n"
233 "\t-m, --map Create the legacy map files\n"
234 "\t-n, --show Write the dependency file on stdout only\n"
235 "\t-V, --version Print the release version\n"
236 "\t-v, --verbose Enable verbose mode\n"
237 "\t-w, --warn Warn on duplicates\n"
238 "\t-h, --help Print this usage message\n"
239 "\n"
240 "The following options are useful for people managing distributions:\n"
241 "\t-b basedirectory\n"
242 "\t --basedir basedirectory Use an image of a module tree.\n"
243 "\t-F kernelsyms\n"
244 "\t --filesyms kernelsyms Use the file instead of the\n"
245 "\t current kernel symbols.\n",
246 "depmod", "depmod");
249 static int ends_in(const char *name, const char *ext)
251 unsigned int namelen, extlen;
253 /* Grab lengths */
254 namelen = strlen(name);
255 extlen = strlen(ext);
257 if (namelen < extlen) return 0;
259 if (streq(name + namelen - extlen, ext))
260 return 1;
261 return 0;
264 static struct module *grab_module(const char *dirname, const char *filename)
266 struct module *new;
268 new = NOFAIL(malloc(sizeof(*new)
269 + strlen(dirname?:"") + 1 + strlen(filename) + 1));
270 if (dirname)
271 sprintf(new->pathname, "%s/%s", dirname, filename);
272 else
273 strcpy(new->pathname, filename);
274 new->basename = my_basename(new->pathname);
276 INIT_LIST_HEAD(&new->dep_list);
277 new->order = INDEX_PRIORITY_MIN;
279 new->data = grab_file(new->pathname, &new->len);
280 if (!new->data) {
281 warn("Can't read module %s: %s\n",
282 new->pathname, strerror(errno));
283 goto fail_data;
286 switch (elf_ident(new->data, new->len, &new->conv)) {
287 case ELFCLASS32:
288 new->ops = &mod_ops32;
289 break;
290 case ELFCLASS64:
291 new->ops = &mod_ops64;
292 break;
293 case -ENOEXEC:
294 warn("Module %s is not an elf object\n", new->pathname);
295 goto fail;
296 case -EINVAL:
297 warn("Module %s has unknown endianness\n", new->pathname);
298 goto fail;
299 default:
300 warn("Module %s has unknown word size\n", new->pathname);
301 goto fail;
303 return new;
305 fail:
306 release_file(new->data, new->len);
307 fail_data:
308 free(new);
309 return NULL;
312 struct module_traverse
314 struct module_traverse *prev;
315 struct module *mod;
318 static int in_loop(struct module *mod, const struct module_traverse *traverse)
320 const struct module_traverse *i;
322 for (i = traverse; i; i = i->prev) {
323 if (i->mod == mod)
324 return 1;
326 return 0;
329 /* Assume we are doing all the modules, so only report each loop once. */
330 static void report_loop(const struct module *mod,
331 const struct module_traverse *traverse)
333 const struct module_traverse *i;
335 /* Check that start is least alphabetically. eg. a depends
336 on b depends on a will get reported for a, not b. */
337 for (i = traverse->prev; i->prev; i = i->prev) {
338 if (strcmp(mod->pathname, i->mod->pathname) > 0)
339 return;
342 /* Is start in the loop? If not, don't report now. eg. a
343 depends on b which depends on c which depends on b. Don't
344 report when generating depends for a. */
345 if (mod != i->mod)
346 return;
348 warn("Loop detected: %s ", mod->pathname);
349 for (i = traverse->prev; i->prev; i = i->prev)
350 fprintf(stderr, "needs %s ", i->mod->basename);
351 fprintf(stderr, "which needs %s again!\n", i->mod->basename);
354 /* This is damn slow, but loops actually happen, and we don't want to
355 just exit() and leave the user without any modules. */
356 static int has_dep_loop(struct module *module, struct module_traverse *prev)
358 unsigned int i;
359 struct module_traverse traverse = { .prev = prev, .mod = module };
361 if (in_loop(module, prev)) {
362 report_loop(module, &traverse);
363 return 1;
366 for (i = 0; i < module->num_deps; i++)
367 if (has_dep_loop(module->deps[i], &traverse))
368 return 1;
369 return 0;
372 /* Uniquifies and orders a dependency list. */
373 static void order_dep_list(struct module *start, struct module *mod)
375 unsigned int i;
377 for (i = 0; i < mod->num_deps; i++) {
378 /* If it was previously depended on, move it to the
379 tail. ie. if a needs b and c, and c needs b, we
380 must order b after c. */
381 list_del(&mod->deps[i]->dep_list);
382 list_add_tail(&mod->deps[i]->dep_list, &start->dep_list);
383 order_dep_list(start, mod->deps[i]);
387 static struct module *deleted = NULL;
389 static void del_module(struct module **modules, struct module *delme)
391 struct module **i;
393 /* Find pointer to it. */
394 if (modules) {
395 for (i = modules; *i != delme; i = &(*i)->next);
397 *i = delme->next;
400 /* Save on a list to quiet valgrind.
401 Can't free - other modules may depend on them */
402 delme->next = deleted;
403 deleted = delme;
406 /* convert to relative path if possible */
407 static const char *compress_path(const char *path, const char *basedir)
409 int len = strlen(basedir);
411 if (strncmp(path, basedir, len) == 0)
412 path += len + 1;
413 return path;
416 static void output_deps(struct module *modules,
417 FILE *out, char *dirname)
419 struct module *i;
421 for (i = modules; i; i = i->next) {
422 struct list_head *j, *tmp;
423 order_dep_list(i, i);
425 fprintf(out, "%s:", compress_path(i->pathname, dirname));
426 list_for_each_safe(j, tmp, &i->dep_list) {
427 struct module *dep
428 = list_entry(j, struct module, dep_list);
429 fprintf(out, " %s",
430 compress_path(dep->pathname, dirname));
431 list_del_init(j);
433 fprintf(out, "\n");
437 /* warn whenever duplicate module aliases, deps, or symbols are found. */
438 int warn_dups = 0;
440 static void output_deps_bin(struct module *modules,
441 FILE *out, char *dirname)
443 struct module *i;
444 struct index_node *index;
445 char *line;
446 char *p;
448 index = index_create();
450 for (i = modules; i; i = i->next) {
451 struct list_head *j, *tmp;
452 char modname[strlen(i->pathname)+1];
454 order_dep_list(i, i);
456 filename2modname(modname, i->pathname);
457 nofail_asprintf(&line, "%s:",
458 compress_path(i->pathname, dirname));
459 p = line;
460 list_for_each_safe(j, tmp, &i->dep_list) {
461 struct module *dep
462 = list_entry(j, struct module, dep_list);
463 nofail_asprintf(&line, "%s %s",
465 compress_path(dep->pathname, dirname));
466 free(p);
467 p = line;
468 list_del_init(j);
470 if (index_insert(index, modname, line, i->order) && warn_dups)
471 warn("duplicate module deps:\n%s\n",line);
472 free(line);
475 index_write(index, out);
476 index_destroy(index);
480 static int smells_like_module(const char *name)
482 return ends_in(name,".ko") || ends_in(name, ".ko.gz");
485 typedef struct module *(*do_module_t)(const char *dirname,
486 const char *filename,
487 struct module *next,
488 struct module_search *search,
489 struct module_overrides *overrides);
491 static int is_higher_priority(const char *newpath, const char *oldpath,
492 struct module_search *search,
493 struct module_overrides *overrides)
495 struct module_search *tmp;
496 struct module_overrides *ovtmp;
497 int i = 0;
498 int prio_builtin = -1;
499 int prio_new = -1;
500 int prio_old = -1;
502 /* The names already match, now we check for overrides and directory search
503 * order
505 for (ovtmp = overrides; ovtmp != NULL; ovtmp = ovtmp->next) {
506 if (streq(ovtmp->modfile, newpath))
507 return 1;
508 if (streq(ovtmp->modfile, oldpath))
509 return 0;
511 for (i = 0, tmp = search; tmp != NULL; tmp = tmp->next, i++) {
512 if (streq(tmp->search_path, MODULE_BUILTIN_KEY))
513 prio_builtin = i;
514 else if (strncmp(tmp->search_path, newpath, tmp->len) == 0)
515 prio_new = i;
516 else if (strncmp(tmp->search_path, oldpath, tmp->len) == 0)
517 prio_old = i;
519 if (prio_new < 0)
520 prio_new = prio_builtin;
521 if (prio_old < 0)
522 prio_old = prio_builtin;
524 return prio_new > prio_old;
528 static struct module *do_module(const char *dirname,
529 const char *filename,
530 struct module *list,
531 struct module_search *search,
532 struct module_overrides *overrides)
534 struct module *new, **i;
536 new = grab_module(dirname, filename);
537 if (!new)
538 return list;
540 /* Check if module is already in the list. */
541 for (i = &list; *i; i = &(*i)->next) {
543 if (streq((*i)->basename, filename)) {
544 char newpath[strlen(dirname) + strlen("/")
545 + strlen(filename) + 1];
547 sprintf(newpath, "%s/%s", dirname, filename);
549 if (is_higher_priority(newpath, (*i)->pathname,search,
550 overrides)) {
551 del_module(i, *i);
553 new->next = *i;
554 *i = new;
555 } else
556 del_module(NULL, new);
558 return list;
562 /* Not in the list already. Just prepend. */
563 new->next = list;
564 return new;
567 static struct module *grab_dir(const char *dirname,
568 DIR *dir,
569 struct module *next,
570 do_module_t do_mod,
571 struct module_search *search,
572 struct module_overrides *overrides)
574 struct dirent *dirent;
576 while ((dirent = readdir(dir)) != NULL) {
577 if (smells_like_module(dirent->d_name))
578 next = do_mod(dirname, dirent->d_name, next,
579 search, overrides);
580 else if (!streq(dirent->d_name, ".")
581 && !streq(dirent->d_name, "..")
582 && !streq(dirent->d_name, "source")
583 && !streq(dirent->d_name, "build")) {
585 DIR *sub;
586 char subdir[strlen(dirname) + 1
587 + strlen(dirent->d_name) + 1];
588 sprintf(subdir, "%s/%s", dirname, dirent->d_name);
589 sub = opendir(subdir);
590 if (sub) {
591 next = grab_dir(subdir, sub, next, do_mod,
592 search, overrides);
593 closedir(sub);
597 return next;
600 static struct module *grab_basedir(const char *dirname,
601 struct module_search *search,
602 struct module_overrides *overrides)
604 DIR *dir;
605 struct module *list;
607 dir = opendir(dirname);
608 if (!dir) {
609 warn("Couldn't open directory %s: %s\n",
610 dirname, strerror(errno));
611 return NULL;
613 list = grab_dir(dirname, dir, NULL, do_module, search, overrides);
614 closedir(dir);
616 return list;
619 static struct module *sort_modules(const char *dirname, struct module *list)
621 struct module *tlist = NULL, **tpos = &tlist;
622 FILE *modorder;
623 int dir_len = strlen(dirname) + 1;
624 char file_name[dir_len + strlen("modules.order") + 1];
625 char line[10240];
626 unsigned int linenum = 0;
628 sprintf(file_name, "%s/%s", dirname, "modules.order");
630 modorder = fopen(file_name, "r");
631 if (!modorder) {
632 /* Older kernels don't generate modules.order. Just
633 return if the file doesn't exist. */
634 if (errno == ENOENT)
635 return list;
636 fatal("Could not open '%s': %s\n", file_name, strerror(errno));
639 sprintf(line, "%s/", dirname);
641 /* move modules listed in modorder file to tlist in order */
642 while (fgets(line, sizeof(line), modorder)) {
643 struct module **pos, *mod;
644 int len = strlen(line);
646 linenum++;
647 if (line[len - 1] == '\n')
648 line[len - 1] = '\0';
650 for (pos = &list; (mod = *pos); pos = &(*pos)->next) {
651 if (streq(line, mod->pathname + dir_len)) {
652 mod->order = linenum;
653 *pos = mod->next;
654 mod->next = NULL;
655 *tpos = mod;
656 tpos = &mod->next;
657 break;
662 /* append the rest */
663 *tpos = list;
665 fclose(modorder);
667 return tlist;
670 static struct module *parse_modules(struct module *list)
672 struct module *i;
674 for (i = list; i; i = i->next) {
675 i->ops->load_symbols(i);
676 i->ops->fetch_tables(i);
679 for (i = list; i; i = i->next)
680 i->ops->calculate_deps(i);
682 /* Strip out modules with dependency loops. */
683 again:
684 for (i = list; i; i = i->next) {
685 if (has_dep_loop(i, NULL)) {
686 warn("Module %s ignored, due to loop\n",
687 i->pathname + skipchars);
688 del_module(&list, i);
689 goto again;
693 return list;
696 /* Simply dump hash table. */
697 static void output_symbols(struct module *unused, FILE *out, char *dirname)
699 unsigned int i;
701 fprintf(out, "# Aliases for symbols, used by symbol_request().\n");
702 for (i = 0; i < SYMBOL_HASH_SIZE; i++) {
703 struct symbol *s;
705 for (s = symbolhash[i]; s; s = s->next) {
706 if (s->owner) {
707 char modname[strlen(s->owner->pathname)+1];
708 filename2modname(modname, s->owner->pathname);
709 fprintf(out, "alias symbol:%s %s\n",
710 s->name, modname);
716 static void output_symbols_bin(struct module *unused, FILE *out, char *dirname)
718 struct index_node *index;
719 unsigned int i;
720 char *alias;
721 int duplicate;
723 index = index_create();
725 for (i = 0; i < SYMBOL_HASH_SIZE; i++) {
726 struct symbol *s;
728 for (s = symbolhash[i]; s; s = s->next) {
729 if (s->owner) {
730 char modname[strlen(s->owner->pathname)+1];
731 filename2modname(modname, s->owner->pathname);
732 nofail_asprintf(&alias, "symbol:%s", s->name);
733 duplicate = index_insert(index, alias, modname,
734 s->owner->order);
735 if (duplicate && warn_dups)
736 warn("duplicate module syms:\n%s %s\n",
737 alias, modname);
738 free(alias);
743 index_write(index, out);
744 index_destroy(index);
747 static void output_aliases(struct module *modules, FILE *out, char *dirname)
749 struct module *i;
750 const char *p;
751 unsigned long size;
753 fprintf(out, "# Aliases extracted from modules themselves.\n");
754 for (i = modules; i; i = i->next) {
755 char modname[strlen(i->pathname)+1];
757 filename2modname(modname, i->pathname);
759 /* Grab from old-style .modalias section. */
760 for (p = i->ops->get_aliases(i, &size);
762 p = next_string(p, &size))
763 fprintf(out, "alias %s %s\n", p, modname);
765 /* Grab form new-style .modinfo section. */
766 for (p = i->ops->get_modinfo(i, &size);
768 p = next_string(p, &size)) {
769 if (strstarts(p, "alias="))
770 fprintf(out, "alias %s %s\n",
771 p + strlen("alias="), modname);
776 static void output_aliases_bin(struct module *modules, FILE *out, char *dirname)
778 struct module *i;
779 const char *p;
780 char *alias;
781 unsigned long size;
782 struct index_node *index;
783 int duplicate;
785 index = index_create();
787 for (i = modules; i; i = i->next) {
788 char modname[strlen(i->pathname)+1];
790 filename2modname(modname, i->pathname);
792 /* Grab from old-style .modalias section. */
793 for (p = i->ops->get_aliases(i, &size);
795 p = next_string(p, &size)) {
796 alias = NOFAIL(strdup(p));
797 underscores(alias);
798 duplicate = index_insert(index, alias, modname, i->order);
799 if (duplicate && warn_dups)
800 warn("duplicate module alias:\n%s %s\n",
801 alias, modname);
802 free(alias);
805 /* Grab from new-style .modinfo section. */
806 for (p = i->ops->get_modinfo(i, &size);
808 p = next_string(p, &size)) {
809 if (strstarts(p, "alias=")) {
810 alias = NOFAIL(strdup(p + strlen("alias=")));
811 underscores(alias);
812 duplicate = index_insert(index, alias, modname, i->order);
813 if (duplicate && warn_dups)
814 warn("duplicate module alias:\n%s %s\n",
815 alias, modname);
816 free(alias);
821 index_write(index, out);
822 index_destroy(index);
825 struct depfile {
826 char *name;
827 void (*func)(struct module *, FILE *, char *dirname);
828 int map_file;
831 static struct depfile depfiles[] = {
832 { "modules.dep", output_deps, 0 }, /* This is what we check for '-A'. */
833 { "modules.dep.bin", output_deps_bin, 0 },
834 { "modules.pcimap", output_pci_table, 1 },
835 { "modules.usbmap", output_usb_table, 1 },
836 { "modules.ccwmap", output_ccw_table, 1 },
837 { "modules.ieee1394map", output_ieee1394_table, 1 },
838 { "modules.isapnpmap", output_isapnp_table, 1 },
839 { "modules.inputmap", output_input_table, 1 },
840 { "modules.ofmap", output_of_table, 1 },
841 { "modules.seriomap", output_serio_table, 1 },
842 { "modules.alias", output_aliases, 0 },
843 { "modules.alias.bin", output_aliases_bin, 0 },
844 { "modules.symbols", output_symbols, 0 },
845 { "modules.symbols.bin", output_symbols_bin, 0 }
848 /* If we can't figure it out, it's safe to say "true". */
849 static int any_modules_newer(const char *dirname, time_t mtime)
851 DIR *dir;
852 struct dirent *dirent;
854 dir = opendir(dirname);
855 if (!dir)
856 return 1;
858 while ((dirent = readdir(dir)) != NULL) {
859 struct stat st;
860 char file[strlen(dirname) + 1 + strlen(dirent->d_name) + 1];
862 if (streq(dirent->d_name, ".") || streq(dirent->d_name, ".."))
863 continue;
865 sprintf(file, "%s/%s", dirname, dirent->d_name);
866 if (lstat(file, &st) != 0)
867 goto ret_true;
869 if (smells_like_module(dirent->d_name)) {
870 if (st.st_mtime > mtime)
871 goto ret_true;
872 } else if (S_ISDIR(st.st_mode)) {
873 if (any_modules_newer(file, mtime))
874 goto ret_true;
877 closedir(dir);
878 return 0;
880 ret_true:
881 closedir(dir);
882 return 1;
885 static int depfile_out_of_date(const char *dirname)
887 struct stat st;
888 char depfile[strlen(dirname) + 1 + strlen(depfiles[0].name) + 1];
890 sprintf(depfile, "%s/%s", dirname, depfiles[0].name);
892 if (stat(depfile, &st) != 0)
893 return 1;
895 return any_modules_newer(dirname, st.st_mtime);
898 static char *strsep_skipspace(char **string, char *delim)
900 if (!*string)
901 return NULL;
902 *string += strspn(*string, delim);
903 return strsep(string, delim);
906 static struct module_search *add_search(const char *search_path,
907 size_t len,
908 struct module_search *search)
911 struct module_search *new;
913 new = NOFAIL(malloc(sizeof(*new)));
914 new->search_path = NOFAIL(strdup(search_path));
915 new->len = len;
916 new->next = search;
918 return new;
922 static struct module_overrides *add_override(const char *modfile,
923 struct module_overrides *overrides)
926 struct module_overrides *new;
928 new = NOFAIL(malloc(sizeof(*new)));
929 new->modfile = NOFAIL(strdup(modfile));
930 new->next = overrides;
932 return new;
936 static int parse_config_scan(const char *filename,
937 const char *basedir,
938 const char *kernelversion,
939 struct module_search **search,
940 struct module_overrides **overrides);
942 static int parse_config_file(const char *filename,
943 const char *basedir,
944 const char *kernelversion,
945 struct module_search **search,
946 struct module_overrides **overrides)
948 char *line;
949 unsigned int linenum = 0;
950 FILE *cfile;
952 cfile = fopen(filename, "r");
953 if (!cfile) {
954 if (errno != ENOENT)
955 fatal("could not open '%s', reason: %s\n", filename,
956 strerror(errno));
957 return 0;
960 while ((line = getline_wrapped(cfile, &linenum)) != NULL) {
961 char *ptr = line;
962 char *cmd, *modname;
964 cmd = strsep_skipspace(&ptr, "\t ");
966 if (cmd == NULL || cmd[0] == '#' || cmd[0] == '\0') {
967 free(line);
968 continue;
971 if (streq(cmd, "search")) {
972 char *search_path;
974 while ((search_path = strsep_skipspace(&ptr, "\t "))) {
975 char *dirname;
976 size_t len;
978 if (strcmp(search_path,
979 MODULE_BUILTIN_KEY) == 0) {
980 *search = add_search(MODULE_BUILTIN_KEY,
981 0, *search);
982 continue;
984 nofail_asprintf(&dirname, "%s%s%s/%s", basedir,
985 MODULE_DIR, kernelversion, search_path);
986 len = strlen(dirname);
987 *search = add_search(dirname, len, *search);
988 free(dirname);
990 } else if (streq(cmd, "override")) {
991 char *pathname = NULL, *version, *subdir;
992 modname = strsep_skipspace(&ptr, "\t ");
993 version = strsep_skipspace(&ptr, "\t ");
994 subdir = strsep_skipspace(&ptr, "\t ");
996 if (strcmp(version, kernelversion) != 0 &&
997 strcmp(version, "*") != 0)
998 continue;
1000 nofail_asprintf(&pathname, "%s%s%s/%s/%s.ko", basedir,
1001 MODULE_DIR, kernelversion, subdir, modname);
1003 *overrides = add_override(pathname, *overrides);
1004 free(pathname);
1005 } else if (streq(cmd, "include")) {
1006 char *newfilename;
1008 newfilename = strsep_skipspace(&ptr, "\t ");
1009 if (!newfilename) {
1010 grammar(cmd, filename, linenum);
1011 } else {
1012 warn("\"include %s\" is deprecated, "
1013 "please use /etc/depmod.d\n", newfilename);
1014 if (strstarts(newfilename, "/etc/depmod.d")) {
1015 warn("\"include /etc/depmod.d\" is "
1016 "the default, ignored\n");
1017 } else {
1018 if (!parse_config_scan(newfilename, basedir,
1019 kernelversion,
1020 search, overrides))
1021 warn("Failed to open included"
1022 " config file %s: %s\n",
1023 newfilename, strerror(errno));
1026 } else if (streq(cmd, "make_map_files")) {
1027 char *option;
1029 option = strsep_skipspace(&ptr, "\t ");
1030 if (!option)
1031 grammar(cmd, filename, linenum);
1032 else {
1033 if (streq(option, "yes"))
1034 make_map_files = 1;
1035 else if (streq(option, "no"))
1036 make_map_files = 0;
1037 else
1038 grammar(cmd, filename, linenum);
1040 } else
1041 grammar(cmd, filename, linenum);
1043 free(line);
1045 fclose(cfile);
1046 return 1;
1049 static int parse_config_scan(const char *filename,
1050 const char *basedir,
1051 const char *kernelversion,
1052 struct module_search **search,
1053 struct module_overrides **overrides)
1055 DIR *dir;
1056 int ret = 0;
1058 dir = opendir(filename);
1059 if (dir) {
1060 struct file_entry {
1061 struct list_head node;
1062 char name[];
1064 LIST_HEAD(files_list);
1065 struct file_entry *fe, *fe_tmp;
1066 struct dirent *i;
1068 /* sort files from directory into list */
1069 while ((i = readdir(dir)) != NULL) {
1070 size_t len;
1072 if (i->d_name[0] == '.')
1073 continue;
1074 if (!config_filter(i->d_name))
1075 continue;
1077 len = strlen(i->d_name);
1078 if (len < 6 || strcmp(&i->d_name[len-5], ".conf") != 0)
1079 warn("All config files need .conf: %s/%s, "
1080 "it will be ignored in a future release.\n",
1081 filename, i->d_name);
1082 fe = malloc(sizeof(struct file_entry) + len + 1);
1083 if (fe == NULL)
1084 continue;
1085 strcpy(fe->name, i->d_name);
1086 list_for_each_entry(fe_tmp, &files_list, node)
1087 if (strcmp(fe_tmp->name, fe->name) >= 0)
1088 break;
1089 list_add_tail(&fe->node, &fe_tmp->node);
1091 closedir(dir);
1093 /* parse list of files */
1094 list_for_each_entry_safe(fe, fe_tmp, &files_list, node) {
1095 char *cfgfile;
1097 nofail_asprintf(&cfgfile, "%s/%s", filename, fe->name);
1098 if (!parse_config_file(cfgfile, basedir, kernelversion,
1099 search, overrides))
1100 warn("Failed to open config file "
1101 "%s: %s\n", fe->name, strerror(errno));
1102 free(cfgfile);
1103 list_del(&fe->node);
1104 free(fe);
1107 ret = 1;
1108 } else {
1109 if (parse_config_file(filename, basedir, kernelversion, search,
1110 overrides))
1111 ret = 1;
1114 return ret;
1117 static void parse_toplevel_config(const char *filename,
1118 const char *basedir,
1119 const char *kernelversion,
1120 struct module_search **search,
1121 struct module_overrides **overrides)
1123 if (filename) {
1124 if (!parse_config_scan(filename, basedir, kernelversion, search,
1125 overrides))
1126 fatal("Failed to open config file %s: %s\n",
1127 filename, strerror(errno));
1128 return;
1131 /* deprecated config file */
1132 if (parse_config_file("/etc/depmod.conf", basedir, kernelversion,
1133 search, overrides) > 0)
1134 warn("Deprecated config file /etc/depmod.conf, "
1135 "all config files belong into /etc/depmod.d/.\n");
1137 /* default config */
1138 parse_config_scan("/etc/depmod.d", basedir, kernelversion,
1139 search, overrides);
1142 /* Local to main, but not freed on exit. Keep valgrind quiet. */
1143 struct module *list = NULL;
1144 struct module_search *search = NULL;
1145 struct module_overrides *overrides = NULL;
1147 int main(int argc, char *argv[])
1149 int opt, all = 0, maybe_all = 0, doing_stdout = 0;
1150 char *basedir = "", *dirname, *version, *badopt = NULL,
1151 *system_map = NULL;
1152 int i;
1153 const char *config = NULL;
1155 if (native_endianness() == 0)
1156 abort();
1158 /* Don't print out any errors just yet, we might want to exec
1159 backwards compat version. */
1160 opterr = 0;
1161 while ((opt = getopt_long(argc, argv, "ab:ArehnqruvVF:C:wm", options, NULL))
1162 != -1) {
1163 switch (opt) {
1164 case 'a':
1165 all = 1;
1166 break;
1167 case 'b':
1168 basedir = optarg;
1169 skipchars = strlen(basedir);
1170 break;
1171 case 'A':
1172 maybe_all = 1;
1173 break;
1174 case 'F':
1175 system_map = optarg;
1176 break;
1177 case 'e':
1178 print_unknown = 1;
1179 break;
1180 case 'v':
1181 verbose = 1;
1182 break;
1183 case 'u':
1184 case 'q':
1185 case 'r':
1186 break;
1187 case 'C':
1188 config = optarg;
1189 break;
1190 case 'h':
1191 print_usage(argv[0]);
1192 exit(0);
1193 break;
1194 case 'n':
1195 doing_stdout = 1;
1196 break;
1197 case 'V':
1198 printf("%s %s\n", PACKAGE, VERSION);
1199 exit(0);
1200 case 'w':
1201 warn_dups = 1;
1202 break;
1203 case 'm':
1204 force_map_files = 1;
1205 break;
1206 default:
1207 badopt = argv[optind-1];
1211 /* We can't print unknowns without a System.map */
1212 if (!system_map)
1213 print_unknown = 0;
1214 else
1215 load_system_map(system_map);
1217 /* They can specify the version naked on the command line */
1218 if (optind < argc && is_version_number(argv[optind])) {
1219 version = NOFAIL(strdup(argv[optind]));
1220 optind++;
1221 } else {
1222 struct utsname buf;
1223 uname(&buf);
1224 version = NOFAIL(strdup(buf.release));
1227 /* Check for old version. */
1228 if (old_module_version(version)) {
1229 fprintf(stderr, "Kernel version %s requires old depmod\n",
1230 version);
1231 exit(2);
1234 if (badopt) {
1235 fprintf(stderr, "%s: malformed/unrecognized option '%s'\n",
1236 argv[0], badopt);
1237 print_usage(argv[0]);
1238 exit(1);
1241 /* Depmod -a by default if no names. */
1242 if (optind == argc)
1243 all = 1;
1245 nofail_asprintf(&dirname, "%s%s%s", basedir, MODULE_DIR, version);
1247 if (maybe_all) {
1248 if (!doing_stdout && !depfile_out_of_date(dirname))
1249 exit(0);
1250 all = 1;
1253 parse_toplevel_config(config, basedir, version, &search, &overrides);
1255 /* For backward compatibility add "updates" to the head of the search
1256 * list here. But only if there was no "search" option specified.
1258 if (!search) {
1259 char *dirname;
1260 size_t len;
1262 nofail_asprintf(&dirname, "%s%s%s/updates", basedir,
1263 MODULE_DIR, version);
1264 len = strlen(dirname);
1265 search = add_search(dirname, len, search);
1267 if (!all) {
1268 /* Do command line args. */
1269 for (opt = optind; opt < argc; opt++) {
1270 struct module *new;
1272 if (argv[opt][0] != '/')
1273 fatal("modules must be specified using absolute paths.\n"
1274 "\"%s\" is a relative path\n", argv[opt]);
1276 new = grab_module(NULL, argv[opt]);
1277 if (!new) {
1278 /* cmd-line specified modules must exist */
1279 fatal("grab_module() failed for module %s\n", argv[opt]);
1281 new->next = list;
1282 list = new;
1284 } else {
1285 list = grab_basedir(dirname,search,overrides);
1287 list = sort_modules(dirname,list);
1288 list = parse_modules(list);
1290 for (i = 0; i < sizeof(depfiles)/sizeof(depfiles[0]); i++) {
1291 FILE *out;
1292 struct depfile *d = &depfiles[i];
1293 char depname[strlen(dirname) + 1 + strlen(d->name) + 1];
1294 char tmpname[strlen(dirname) + 1 + strlen(d->name) +
1295 strlen(".temp") + 1];
1297 if (d->map_file && !make_map_files && !force_map_files)
1298 continue;
1300 sprintf(depname, "%s/%s", dirname, d->name);
1301 sprintf(tmpname, "%s/%s.temp", dirname, d->name);
1302 if (!doing_stdout) {
1303 out = fopen(tmpname, "w");
1304 if (!out)
1305 fatal("Could not open %s for writing: %s\n",
1306 tmpname, strerror(errno));
1307 } else {
1308 out = stdout;
1309 if (ends_in(depname, ".bin"))
1310 continue;
1312 d->func(list, out, dirname);
1313 if (!doing_stdout) {
1314 fclose(out);
1315 if (rename(tmpname, depname) < 0)
1316 fatal("Could not rename %s into %s: %s\n",
1317 tmpname, depname, strerror(errno));
1321 free(dirname);
1322 free(version);
1324 return 0;