Clean up missing options
[mit.git] / depmod.c
blob116893de5a2412971a13b80584f4f891303ad0d6
1 /* New simplified depmod without backwards compat stuff and not
2 requiring ksyms.
4 (C) 2002 Rusty Russell IBM Corporation
5 */
6 #define _GNU_SOURCE /* asprintf */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <getopt.h>
11 #include <string.h>
12 #include <errno.h>
13 #include <unistd.h>
14 #include <elf.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <dirent.h>
19 #include <sys/utsname.h>
20 #include <sys/mman.h>
22 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
24 #include "util.h"
25 #include "zlibsupport.h"
26 #include "depmod.h"
27 #include "logging.h"
28 #include "index.h"
29 #include "elfops.h"
30 #include "tables.h"
31 #include "config_filter.h"
33 #include "testing.h"
35 #ifndef MODULE_DIR
36 #define MODULE_DIR "/lib/modules/"
37 #endif
39 #ifndef MODULE_BUILTIN_KEY
40 #define MODULE_BUILTIN_KEY "built-in"
41 #endif
43 struct module_overrides
45 /* Next override */
46 struct module_overrides *next;
48 /* overridden module */
49 char *modfile;
52 struct module_search
54 /* Next search */
55 struct module_search *next;
57 /* search path */
58 char *search_path;
59 size_t len;
62 static unsigned int skipchars;
63 static unsigned int make_map_files = 1; /* default to on */
64 static unsigned int force_map_files = 0; /* default to on */
66 #define SYMBOL_HASH_SIZE 1024
67 struct symbol
69 struct symbol *next;
70 struct module *owner;
71 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 { "config", 1, NULL, 'C' },
176 { "filesyms", 1, NULL, 'F' },
177 { "errsyms", 0, NULL, 'e' },
178 { "unresolved-error", 0, NULL, 'u' },
179 { "quiet", 0, NULL, 'q' },
180 { "root", 0, NULL, 'r' },
181 { "verbose", 0, NULL, 'v' },
182 { "show", 0, NULL, 'n' },
183 { "dry-run", 0, NULL, 'n' },
184 { "help", 0, NULL, 'h' },
185 { "version", 0, NULL, 'V' },
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->file = grab_elf_file(new->pathname);
280 if (!new->file) {
281 warn("Can't read module %s: %s\n",
282 new->pathname, strerror(errno));
283 free(new);
284 return NULL;
286 return new;
289 struct module_traverse
291 struct module_traverse *prev;
292 struct module *mod;
295 static int in_loop(struct module *mod, const struct module_traverse *traverse)
297 const struct module_traverse *i;
299 for (i = traverse; i; i = i->prev) {
300 if (i->mod == mod)
301 return 1;
303 return 0;
306 /* Assume we are doing all the modules, so only report each loop once. */
307 static void report_loop(const struct module *mod,
308 const struct module_traverse *traverse)
310 const struct module_traverse *i;
312 /* Check that start is least alphabetically. eg. a depends
313 on b depends on a will get reported for a, not b. */
314 for (i = traverse->prev; i->prev; i = i->prev) {
315 if (strcmp(mod->pathname, i->mod->pathname) > 0)
316 return;
319 /* Is start in the loop? If not, don't report now. eg. a
320 depends on b which depends on c which depends on b. Don't
321 report when generating depends for a. */
322 if (mod != i->mod)
323 return;
325 warn("Loop detected: %s ", mod->pathname);
326 for (i = traverse->prev; i->prev; i = i->prev)
327 fprintf(stderr, "needs %s ", i->mod->basename);
328 fprintf(stderr, "which needs %s again!\n", i->mod->basename);
331 /* This is damn slow, but loops actually happen, and we don't want to
332 just exit() and leave the user without any modules. */
333 static int has_dep_loop(struct module *module, struct module_traverse *prev)
335 unsigned int i;
336 struct module_traverse traverse = { .prev = prev, .mod = module };
338 if (in_loop(module, prev)) {
339 report_loop(module, &traverse);
340 return 1;
343 for (i = 0; i < module->num_deps; i++)
344 if (has_dep_loop(module->deps[i], &traverse))
345 return 1;
346 return 0;
349 /* Uniquifies and orders a dependency list. */
350 static void order_dep_list(struct module *start, struct module *mod)
352 unsigned int i;
354 for (i = 0; i < mod->num_deps; i++) {
355 /* If it was previously depended on, move it to the
356 tail. ie. if a needs b and c, and c needs b, we
357 must order b after c. */
358 list_del(&mod->deps[i]->dep_list);
359 list_add_tail(&mod->deps[i]->dep_list, &start->dep_list);
360 order_dep_list(start, mod->deps[i]);
364 static struct module *deleted = NULL;
366 static void del_module(struct module **modules, struct module *delme)
368 struct module **i;
370 /* Find pointer to it. */
371 if (modules) {
372 for (i = modules; *i != delme; i = &(*i)->next);
374 *i = delme->next;
377 /* Save on a list to quiet valgrind.
378 Can't free - other modules may depend on them */
379 delme->next = deleted;
380 deleted = delme;
383 /* convert to relative path if possible */
384 static const char *compress_path(const char *path, const char *basedir)
386 int len = strlen(basedir);
388 if (strncmp(path, basedir, len) == 0)
389 path += len + 1;
390 return path;
393 static void output_deps(struct module *modules,
394 FILE *out, char *dirname)
396 struct module *i;
398 for (i = modules; i; i = i->next) {
399 struct list_head *j, *tmp;
400 order_dep_list(i, i);
402 fprintf(out, "%s:", compress_path(i->pathname, dirname));
403 list_for_each_safe(j, tmp, &i->dep_list) {
404 struct module *dep
405 = list_entry(j, struct module, dep_list);
406 fprintf(out, " %s",
407 compress_path(dep->pathname, dirname));
408 list_del_init(j);
410 fprintf(out, "\n");
414 /* warn whenever duplicate module aliases, deps, or symbols are found. */
415 int warn_dups = 0;
417 static void output_deps_bin(struct module *modules,
418 FILE *out, char *dirname)
420 struct module *i;
421 struct index_node *index;
422 char *line;
423 char *p;
425 index = index_create();
427 for (i = modules; i; i = i->next) {
428 struct list_head *j, *tmp;
429 char modname[strlen(i->pathname)+1];
431 order_dep_list(i, i);
433 filename2modname(modname, i->pathname);
434 nofail_asprintf(&line, "%s:",
435 compress_path(i->pathname, dirname));
436 p = line;
437 list_for_each_safe(j, tmp, &i->dep_list) {
438 struct module *dep
439 = list_entry(j, struct module, dep_list);
440 nofail_asprintf(&line, "%s %s",
442 compress_path(dep->pathname, dirname));
443 free(p);
444 p = line;
445 list_del_init(j);
447 if (index_insert(index, modname, line, i->order) && warn_dups)
448 warn("duplicate module deps:\n%s\n",line);
449 free(line);
452 index_write(index, out);
453 index_destroy(index);
457 static int smells_like_module(const char *name)
459 return ends_in(name,".ko") || ends_in(name, ".ko.gz");
462 typedef struct module *(*do_module_t)(const char *dirname,
463 const char *filename,
464 struct module *next,
465 struct module_search *search,
466 struct module_overrides *overrides);
468 static int is_higher_priority(const char *newpath, const char *oldpath,
469 struct module_search *search,
470 struct module_overrides *overrides)
472 struct module_search *tmp;
473 struct module_overrides *ovtmp;
474 int i = 0;
475 int prio_builtin = -1;
476 int prio_new = -1;
477 int prio_old = -1;
479 /* The names already match, now we check for overrides and directory search
480 * order
482 for (ovtmp = overrides; ovtmp != NULL; ovtmp = ovtmp->next) {
483 if (streq(ovtmp->modfile, newpath))
484 return 1;
485 if (streq(ovtmp->modfile, oldpath))
486 return 0;
488 for (i = 0, tmp = search; tmp != NULL; tmp = tmp->next, i++) {
489 if (streq(tmp->search_path, MODULE_BUILTIN_KEY))
490 prio_builtin = i;
491 else if (strncmp(tmp->search_path, newpath, tmp->len) == 0)
492 prio_new = i;
493 else if (strncmp(tmp->search_path, oldpath, tmp->len) == 0)
494 prio_old = i;
496 if (prio_new < 0)
497 prio_new = prio_builtin;
498 if (prio_old < 0)
499 prio_old = prio_builtin;
501 return prio_new > prio_old;
505 static struct module *do_module(const char *dirname,
506 const char *filename,
507 struct module *list,
508 struct module_search *search,
509 struct module_overrides *overrides)
511 struct module *new, **i;
513 new = grab_module(dirname, filename);
514 if (!new)
515 return list;
517 /* Check if module is already in the list. */
518 for (i = &list; *i; i = &(*i)->next) {
520 if (streq((*i)->basename, filename)) {
521 char newpath[strlen(dirname) + strlen("/")
522 + strlen(filename) + 1];
524 sprintf(newpath, "%s/%s", dirname, filename);
526 if (is_higher_priority(newpath, (*i)->pathname,search,
527 overrides)) {
528 del_module(i, *i);
530 new->next = *i;
531 *i = new;
532 } else
533 del_module(NULL, new);
535 return list;
539 /* Not in the list already. Just prepend. */
540 new->next = list;
541 return new;
544 static struct module *grab_dir(const char *dirname,
545 DIR *dir,
546 struct module *next,
547 do_module_t do_mod,
548 struct module_search *search,
549 struct module_overrides *overrides)
551 struct dirent *dirent;
553 while ((dirent = readdir(dir)) != NULL) {
554 if (smells_like_module(dirent->d_name))
555 next = do_mod(dirname, dirent->d_name, next,
556 search, overrides);
557 else if (!streq(dirent->d_name, ".")
558 && !streq(dirent->d_name, "..")
559 && !streq(dirent->d_name, "source")
560 && !streq(dirent->d_name, "build")) {
562 DIR *sub;
563 char subdir[strlen(dirname) + 1
564 + strlen(dirent->d_name) + 1];
565 sprintf(subdir, "%s/%s", dirname, dirent->d_name);
566 sub = opendir(subdir);
567 if (sub) {
568 next = grab_dir(subdir, sub, next, do_mod,
569 search, overrides);
570 closedir(sub);
574 return next;
577 static struct module *grab_basedir(const char *dirname,
578 struct module_search *search,
579 struct module_overrides *overrides)
581 DIR *dir;
582 struct module *list;
584 dir = opendir(dirname);
585 if (!dir) {
586 warn("Couldn't open directory %s: %s\n",
587 dirname, strerror(errno));
588 return NULL;
590 list = grab_dir(dirname, dir, NULL, do_module, search, overrides);
591 closedir(dir);
593 return list;
596 static struct module *sort_modules(const char *dirname, struct module *list)
598 struct module *tlist = NULL, **tpos = &tlist;
599 FILE *modorder;
600 int dir_len = strlen(dirname) + 1;
601 char file_name[dir_len + strlen("modules.order") + 1];
602 char line[10240];
603 unsigned int linenum = 0;
605 sprintf(file_name, "%s/%s", dirname, "modules.order");
607 modorder = fopen(file_name, "r");
608 if (!modorder) {
609 /* Older kernels don't generate modules.order. Just
610 return if the file doesn't exist. */
611 if (errno == ENOENT)
612 return list;
613 fatal("Could not open '%s': %s\n", file_name, strerror(errno));
616 sprintf(line, "%s/", dirname);
618 /* move modules listed in modorder file to tlist in order */
619 while (fgets(line, sizeof(line), modorder)) {
620 struct module **pos, *mod;
621 int len = strlen(line);
623 linenum++;
624 if (line[len - 1] == '\n')
625 line[len - 1] = '\0';
627 for (pos = &list; (mod = *pos); pos = &(*pos)->next) {
628 if (streq(line, mod->pathname + dir_len)) {
629 mod->order = linenum;
630 *pos = mod->next;
631 mod->next = NULL;
632 *tpos = mod;
633 tpos = &mod->next;
634 break;
639 /* append the rest */
640 *tpos = list;
642 fclose(modorder);
644 return tlist;
647 /* Calculate the dependencies for this module */
648 static void calculate_deps(struct module *module)
650 unsigned int i;
651 struct string_table *symnames;
652 struct string_table *symtypes;
653 struct elf_file *file;
655 module->num_deps = 0;
656 module->deps = NULL;
657 file = module->file;
659 symnames = file->ops->load_dep_syms(module->pathname, file, &symtypes);
660 if (!symnames || !symtypes)
661 return;
663 for (i = 0; i < symnames->cnt; i++) {
664 const char *name;
665 struct module *owner;
666 int weak;
668 name = symnames->str[i];
669 weak = (*(symtypes->str[i]) == 'W');
670 owner = find_symbol(name, module->pathname, weak);
671 if (owner) {
672 info("%s needs \"%s\": %s\n",
673 module->pathname, name,
674 owner->pathname);
675 add_dep(module, owner);
679 free(symnames);
680 free(symtypes);
683 static struct module *parse_modules(struct module *list)
685 struct module *i;
686 struct elf_file *file;
687 struct string_table *syms;
688 int j;
690 for (i = list; i; i = i->next) {
691 file = i->file;
692 syms = file->ops->load_symbols(file);
693 if (syms) {
694 for (j = 0; j < syms->cnt; j++)
695 add_symbol(syms->str[j], i);
696 strtbl_free(syms);
698 file->ops->fetch_tables(file, &i->tables);
701 for (i = list; i; i = i->next)
702 calculate_deps(i);
704 /* Strip out modules with dependency loops. */
705 again:
706 for (i = list; i; i = i->next) {
707 if (has_dep_loop(i, NULL)) {
708 warn("Module %s ignored, due to loop\n",
709 i->pathname + skipchars);
710 del_module(&list, i);
711 goto again;
715 return list;
718 /* Simply dump hash table. */
719 static void output_symbols(struct module *unused, FILE *out, char *dirname)
721 unsigned int i;
723 fprintf(out, "# Aliases for symbols, used by symbol_request().\n");
724 for (i = 0; i < SYMBOL_HASH_SIZE; i++) {
725 struct symbol *s;
727 for (s = symbolhash[i]; s; s = s->next) {
728 if (s->owner) {
729 char modname[strlen(s->owner->pathname)+1];
730 filename2modname(modname, s->owner->pathname);
731 fprintf(out, "alias symbol:%s %s\n",
732 s->name, modname);
738 static void output_symbols_bin(struct module *unused, FILE *out, char *dirname)
740 struct index_node *index;
741 unsigned int i;
742 char *alias;
743 int duplicate;
745 index = index_create();
747 for (i = 0; i < SYMBOL_HASH_SIZE; i++) {
748 struct symbol *s;
750 for (s = symbolhash[i]; s; s = s->next) {
751 if (s->owner) {
752 char modname[strlen(s->owner->pathname)+1];
753 filename2modname(modname, s->owner->pathname);
754 nofail_asprintf(&alias, "symbol:%s", s->name);
755 duplicate = index_insert(index, alias, modname,
756 s->owner->order);
757 if (duplicate && warn_dups)
758 warn("duplicate module syms:\n%s %s\n",
759 alias, modname);
760 free(alias);
765 index_write(index, out);
766 index_destroy(index);
769 static void output_aliases(struct module *modules, FILE *out, char *dirname)
771 struct module *i;
772 struct elf_file *file;
773 struct string_table *tbl;
774 int j;
776 fprintf(out, "# Aliases extracted from modules themselves.\n");
777 for (i = modules; i; i = i->next) {
778 char modname[strlen(i->pathname)+1];
780 file = i->file;
781 filename2modname(modname, i->pathname);
783 /* Grab from old-style .modalias section. */
784 tbl = file->ops->load_strings(file, ".modalias", NULL, fatal);
785 for (j = 0; tbl && j < tbl->cnt; j++)
786 fprintf(out, "alias %s %s\n", tbl->str[j], modname);
787 strtbl_free(tbl);
789 /* Grab from new-style .modinfo section. */
790 tbl = file->ops->load_strings(file, ".modinfo", NULL, fatal);
791 for (j = 0; tbl && j < tbl->cnt; j++) {
792 const char *p = tbl->str[j];
793 if (strstarts(p, "alias="))
794 fprintf(out, "alias %s %s\n",
795 p + strlen("alias="), modname);
797 strtbl_free(tbl);
801 static void output_aliases_bin(struct module *modules, FILE *out, char *dirname)
803 struct module *i;
804 struct elf_file *file;
805 struct string_table *tbl;
806 int j;
807 char *alias;
808 struct index_node *index;
809 int duplicate;
811 index = index_create();
813 for (i = modules; i; i = i->next) {
814 char modname[strlen(i->pathname)+1];
816 file = i->file;
817 filename2modname(modname, i->pathname);
819 /* Grab from old-style .modalias section. */
820 tbl = file->ops->load_strings(file, ".modalias", NULL, fatal);
821 for (j = 0; tbl && j < tbl->cnt; j++) {
822 alias = NOFAIL(strdup(tbl->str[j]));
823 underscores(alias);
824 duplicate = index_insert(index, alias, modname, i->order);
825 if (duplicate && warn_dups)
826 warn("duplicate module alias:\n%s %s\n",
827 alias, modname);
828 free(alias);
830 strtbl_free(tbl);
832 /* Grab from new-style .modinfo section. */
833 tbl = file->ops->load_strings(file, ".modinfo", NULL, fatal);
834 for (j = 0; tbl && j < tbl->cnt; j++) {
835 const char *p = tbl->str[j];
836 if (strstarts(p, "alias=")) {
837 alias = NOFAIL(strdup(p + strlen("alias=")));
838 underscores(alias);
839 duplicate = index_insert(index, alias, modname, i->order);
840 if (duplicate && warn_dups)
841 warn("duplicate module alias:\n%s %s\n",
842 alias, modname);
843 free(alias);
846 strtbl_free(tbl);
849 index_write(index, out);
850 index_destroy(index);
853 struct depfile {
854 char *name;
855 void (*func)(struct module *, FILE *, char *dirname);
856 int map_file;
859 static struct depfile depfiles[] = {
860 { "modules.dep", output_deps, 0 }, /* This is what we check for '-A'. */
861 { "modules.dep.bin", output_deps_bin, 0 },
862 { "modules.pcimap", output_pci_table, 1 },
863 { "modules.usbmap", output_usb_table, 1 },
864 { "modules.ccwmap", output_ccw_table, 1 },
865 { "modules.ieee1394map", output_ieee1394_table, 1 },
866 { "modules.isapnpmap", output_isapnp_table, 1 },
867 { "modules.inputmap", output_input_table, 1 },
868 { "modules.ofmap", output_of_table, 1 },
869 { "modules.seriomap", output_serio_table, 1 },
870 { "modules.alias", output_aliases, 0 },
871 { "modules.alias.bin", output_aliases_bin, 0 },
872 { "modules.symbols", output_symbols, 0 },
873 { "modules.symbols.bin", output_symbols_bin, 0 }
876 /* If we can't figure it out, it's safe to say "true". */
877 static int any_modules_newer(const char *dirname, time_t mtime)
879 DIR *dir;
880 struct dirent *dirent;
882 dir = opendir(dirname);
883 if (!dir)
884 return 1;
886 while ((dirent = readdir(dir)) != NULL) {
887 struct stat st;
888 char file[strlen(dirname) + 1 + strlen(dirent->d_name) + 1];
890 if (streq(dirent->d_name, ".") || streq(dirent->d_name, ".."))
891 continue;
893 sprintf(file, "%s/%s", dirname, dirent->d_name);
894 if (lstat(file, &st) != 0)
895 goto ret_true;
897 if (smells_like_module(dirent->d_name)) {
898 if (st.st_mtime > mtime)
899 goto ret_true;
900 } else if (S_ISDIR(st.st_mode)) {
901 if (any_modules_newer(file, mtime))
902 goto ret_true;
905 closedir(dir);
906 return 0;
908 ret_true:
909 closedir(dir);
910 return 1;
913 static int depfile_out_of_date(const char *dirname)
915 struct stat st;
916 char depfile[strlen(dirname) + 1 + strlen(depfiles[0].name) + 1];
918 sprintf(depfile, "%s/%s", dirname, depfiles[0].name);
920 if (stat(depfile, &st) != 0)
921 return 1;
923 return any_modules_newer(dirname, st.st_mtime);
926 static char *strsep_skipspace(char **string, char *delim)
928 if (!*string)
929 return NULL;
930 *string += strspn(*string, delim);
931 return strsep(string, delim);
934 static struct module_search *add_search(const char *search_path,
935 size_t len,
936 struct module_search *search)
939 struct module_search *new;
941 new = NOFAIL(malloc(sizeof(*new)));
942 new->search_path = NOFAIL(strdup(search_path));
943 new->len = len;
944 new->next = search;
946 return new;
950 static struct module_overrides *add_override(const char *modfile,
951 struct module_overrides *overrides)
954 struct module_overrides *new;
956 new = NOFAIL(malloc(sizeof(*new)));
957 new->modfile = NOFAIL(strdup(modfile));
958 new->next = overrides;
960 return new;
964 static int parse_config_scan(const char *filename,
965 const char *basedir,
966 const char *kernelversion,
967 struct module_search **search,
968 struct module_overrides **overrides);
970 static int parse_config_file(const char *filename,
971 const char *basedir,
972 const char *kernelversion,
973 struct module_search **search,
974 struct module_overrides **overrides)
976 char *line;
977 unsigned int linenum = 0;
978 FILE *cfile;
980 cfile = fopen(filename, "r");
981 if (!cfile) {
982 if (errno != ENOENT)
983 fatal("could not open '%s', reason: %s\n", filename,
984 strerror(errno));
985 return 0;
988 while ((line = getline_wrapped(cfile, &linenum)) != NULL) {
989 char *ptr = line;
990 char *cmd, *modname;
992 cmd = strsep_skipspace(&ptr, "\t ");
994 if (cmd == NULL || cmd[0] == '#' || cmd[0] == '\0') {
995 free(line);
996 continue;
999 if (streq(cmd, "search")) {
1000 char *search_path;
1002 while ((search_path = strsep_skipspace(&ptr, "\t "))) {
1003 char *dirname;
1004 size_t len;
1006 if (strcmp(search_path,
1007 MODULE_BUILTIN_KEY) == 0) {
1008 *search = add_search(MODULE_BUILTIN_KEY,
1009 0, *search);
1010 continue;
1012 nofail_asprintf(&dirname, "%s%s%s/%s", basedir,
1013 MODULE_DIR, kernelversion, search_path);
1014 len = strlen(dirname);
1015 *search = add_search(dirname, len, *search);
1016 free(dirname);
1018 } else if (streq(cmd, "override")) {
1019 char *pathname = NULL, *version, *subdir;
1020 modname = strsep_skipspace(&ptr, "\t ");
1021 version = strsep_skipspace(&ptr, "\t ");
1022 subdir = strsep_skipspace(&ptr, "\t ");
1024 if (strcmp(version, kernelversion) != 0 &&
1025 strcmp(version, "*") != 0)
1026 continue;
1028 nofail_asprintf(&pathname, "%s%s%s/%s/%s.ko", basedir,
1029 MODULE_DIR, kernelversion, subdir, modname);
1031 *overrides = add_override(pathname, *overrides);
1032 free(pathname);
1033 } else if (streq(cmd, "include")) {
1034 char *newfilename;
1036 newfilename = strsep_skipspace(&ptr, "\t ");
1037 if (!newfilename) {
1038 grammar(cmd, filename, linenum);
1039 } else {
1040 warn("\"include %s\" is deprecated, "
1041 "please use /etc/depmod.d\n", newfilename);
1042 if (strstarts(newfilename, "/etc/depmod.d")) {
1043 warn("\"include /etc/depmod.d\" is "
1044 "the default, ignored\n");
1045 } else {
1046 if (!parse_config_scan(newfilename, basedir,
1047 kernelversion,
1048 search, overrides))
1049 warn("Failed to open included"
1050 " config file %s: %s\n",
1051 newfilename, strerror(errno));
1054 } else if (streq(cmd, "make_map_files")) {
1055 char *option;
1057 option = strsep_skipspace(&ptr, "\t ");
1058 if (!option)
1059 grammar(cmd, filename, linenum);
1060 else {
1061 if (streq(option, "yes"))
1062 make_map_files = 1;
1063 else if (streq(option, "no"))
1064 make_map_files = 0;
1065 else
1066 grammar(cmd, filename, linenum);
1068 } else
1069 grammar(cmd, filename, linenum);
1071 free(line);
1073 fclose(cfile);
1074 return 1;
1077 static int parse_config_scan(const char *filename,
1078 const char *basedir,
1079 const char *kernelversion,
1080 struct module_search **search,
1081 struct module_overrides **overrides)
1083 DIR *dir;
1084 int ret = 0;
1086 dir = opendir(filename);
1087 if (dir) {
1088 struct file_entry {
1089 struct list_head node;
1090 char name[];
1092 LIST_HEAD(files_list);
1093 struct file_entry *fe, *fe_tmp;
1094 struct dirent *i;
1096 /* sort files from directory into list */
1097 while ((i = readdir(dir)) != NULL) {
1098 size_t len;
1100 if (i->d_name[0] == '.')
1101 continue;
1102 if (!config_filter(i->d_name))
1103 continue;
1105 len = strlen(i->d_name);
1106 if (len < 6 || strcmp(&i->d_name[len-5], ".conf") != 0)
1107 warn("All config files need .conf: %s/%s, "
1108 "it will be ignored in a future release.\n",
1109 filename, i->d_name);
1110 fe = malloc(sizeof(struct file_entry) + len + 1);
1111 if (fe == NULL)
1112 continue;
1113 strcpy(fe->name, i->d_name);
1114 list_for_each_entry(fe_tmp, &files_list, node)
1115 if (strcmp(fe_tmp->name, fe->name) >= 0)
1116 break;
1117 list_add_tail(&fe->node, &fe_tmp->node);
1119 closedir(dir);
1121 /* parse list of files */
1122 list_for_each_entry_safe(fe, fe_tmp, &files_list, node) {
1123 char *cfgfile;
1125 nofail_asprintf(&cfgfile, "%s/%s", filename, fe->name);
1126 if (!parse_config_file(cfgfile, basedir, kernelversion,
1127 search, overrides))
1128 warn("Failed to open config file "
1129 "%s: %s\n", fe->name, strerror(errno));
1130 free(cfgfile);
1131 list_del(&fe->node);
1132 free(fe);
1135 ret = 1;
1136 } else {
1137 if (parse_config_file(filename, basedir, kernelversion, search,
1138 overrides))
1139 ret = 1;
1142 return ret;
1145 static void parse_toplevel_config(const char *filename,
1146 const char *basedir,
1147 const char *kernelversion,
1148 struct module_search **search,
1149 struct module_overrides **overrides)
1151 if (filename) {
1152 if (!parse_config_scan(filename, basedir, kernelversion, search,
1153 overrides))
1154 fatal("Failed to open config file %s: %s\n",
1155 filename, strerror(errno));
1156 return;
1159 /* deprecated config file */
1160 if (parse_config_file("/etc/depmod.conf", basedir, kernelversion,
1161 search, overrides) > 0)
1162 warn("Deprecated config file /etc/depmod.conf, "
1163 "all config files belong into /etc/depmod.d/.\n");
1165 /* default config */
1166 parse_config_scan("/etc/depmod.d", basedir, kernelversion,
1167 search, overrides);
1170 /* Local to main, but not freed on exit. Keep valgrind quiet. */
1171 struct module *list = NULL;
1172 struct module_search *search = NULL;
1173 struct module_overrides *overrides = NULL;
1175 int main(int argc, char *argv[])
1177 int opt, all = 0, maybe_all = 0, doing_stdout = 0;
1178 char *basedir = "", *dirname, *version, *badopt = NULL,
1179 *system_map = NULL;
1180 int i;
1181 const char *config = NULL;
1183 if (native_endianness() == 0)
1184 abort();
1186 /* Don't print out any errors just yet, we might want to exec
1187 backwards compat version. */
1188 opterr = 0;
1189 while ((opt = getopt_long(argc, argv, "aAb:C:F:euqrvnhVwm", options, NULL))
1190 != -1) {
1191 switch (opt) {
1192 case 'a':
1193 all = 1;
1194 break;
1195 case 'A':
1196 maybe_all = 1;
1197 break;
1198 case 'b':
1199 basedir = optarg;
1200 skipchars = strlen(basedir);
1201 break;
1202 case 'C':
1203 config = optarg;
1204 break;
1205 case 'F':
1206 system_map = optarg;
1207 break;
1208 case 'e':
1209 print_unknown = 1;
1210 break;
1211 case 'u':
1212 case 'q':
1213 case 'r':
1214 break;
1215 case 'v':
1216 verbose = 1;
1217 break;
1218 case 'n':
1219 doing_stdout = 1;
1220 break;
1221 case 'h':
1222 print_usage(argv[0]);
1223 exit(0);
1224 break;
1225 case 'V':
1226 printf("%s %s\n", PACKAGE, VERSION);
1227 exit(0);
1228 case 'w':
1229 warn_dups = 1;
1230 break;
1231 case 'm':
1232 force_map_files = 1;
1233 break;
1234 default:
1235 badopt = argv[optind-1];
1239 /* We can't print unknowns without a System.map */
1240 if (!system_map)
1241 print_unknown = 0;
1242 else
1243 load_system_map(system_map);
1245 /* They can specify the version naked on the command line */
1246 if (optind < argc && is_version_number(argv[optind])) {
1247 version = NOFAIL(strdup(argv[optind]));
1248 optind++;
1249 } else {
1250 struct utsname buf;
1251 uname(&buf);
1252 version = NOFAIL(strdup(buf.release));
1255 /* Check for old version. */
1256 if (old_module_version(version)) {
1257 fprintf(stderr, "Kernel version %s requires old depmod\n",
1258 version);
1259 exit(2);
1262 if (badopt) {
1263 fprintf(stderr, "%s: malformed/unrecognized option '%s'\n",
1264 argv[0], badopt);
1265 print_usage(argv[0]);
1266 exit(1);
1269 /* Depmod -a by default if no names. */
1270 if (optind == argc)
1271 all = 1;
1273 nofail_asprintf(&dirname, "%s%s%s", basedir, MODULE_DIR, version);
1275 if (maybe_all) {
1276 if (!doing_stdout && !depfile_out_of_date(dirname))
1277 exit(0);
1278 all = 1;
1281 parse_toplevel_config(config, basedir, version, &search, &overrides);
1283 /* For backward compatibility add "updates" to the head of the search
1284 * list here. But only if there was no "search" option specified.
1286 if (!search) {
1287 char *dirname;
1288 size_t len;
1290 nofail_asprintf(&dirname, "%s%s%s/updates", basedir,
1291 MODULE_DIR, version);
1292 len = strlen(dirname);
1293 search = add_search(dirname, len, search);
1295 if (!all) {
1296 /* Do command line args. */
1297 for (opt = optind; opt < argc; opt++) {
1298 struct module *new;
1300 if (argv[opt][0] != '/')
1301 fatal("modules must be specified using absolute paths.\n"
1302 "\"%s\" is a relative path\n", argv[opt]);
1304 new = grab_module(NULL, argv[opt]);
1305 if (!new) {
1306 /* cmd-line specified modules must exist */
1307 fatal("grab_module() failed for module %s\n", argv[opt]);
1309 new->next = list;
1310 list = new;
1312 } else {
1313 list = grab_basedir(dirname,search,overrides);
1315 list = sort_modules(dirname,list);
1316 list = parse_modules(list);
1318 for (i = 0; i < sizeof(depfiles)/sizeof(depfiles[0]); i++) {
1319 FILE *out;
1320 struct depfile *d = &depfiles[i];
1321 char depname[strlen(dirname) + 1 + strlen(d->name) + 1];
1322 char tmpname[strlen(dirname) + 1 + strlen(d->name) +
1323 strlen(".temp") + 1];
1325 if (d->map_file && !make_map_files && !force_map_files)
1326 continue;
1328 sprintf(depname, "%s/%s", dirname, d->name);
1329 sprintf(tmpname, "%s/%s.temp", dirname, d->name);
1330 if (!doing_stdout) {
1331 out = fopen(tmpname, "w");
1332 if (!out)
1333 fatal("Could not open %s for writing: %s\n",
1334 tmpname, strerror(errno));
1335 } else {
1336 out = stdout;
1337 if (ends_in(depname, ".bin"))
1338 continue;
1340 d->func(list, out, dirname);
1341 if (!doing_stdout) {
1342 fclose(out);
1343 if (rename(tmpname, depname) < 0)
1344 fatal("Could not rename %s into %s: %s\n",
1345 tmpname, depname, strerror(errno));
1349 free(dirname);
1350 free(version);
1352 return 0;