Take advantage of strstarts
[mit.git] / depmod.c
bloba2bf055230aba1c2c5c7f7351b45d3117f1a9f3e
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 exec_old_depmod(char *argv[])
219 char *sep;
220 char pathname[strlen(argv[0])+1];
221 char oldname[strlen("depmod") + strlen(argv[0]) + sizeof(".old")];
223 memset(pathname, 0, strlen(argv[0])+1);
224 sep = strrchr(argv[0], '/');
225 if (sep)
226 memcpy(pathname, argv[0], sep - argv[0]+1);
227 sprintf(oldname, "%s%s.old", pathname, "depmod");
229 /* Recursion detection: we need an env var since we can't
230 change argv[0] (as older modutils uses it to determine
231 behavior). */
232 if (getenv("MODULE_RECURSE"))
233 return;
234 setenv("MODULE_RECURSE", "y", 0);
236 execvp(oldname, argv);
237 fprintf(stderr,
238 "Version requires old depmod, but couldn't run %s: %s\n",
239 oldname, strerror(errno));
240 exit(2);
244 static void print_usage(const char *name)
246 fprintf(stderr,
247 "%s " VERSION " -- part of " PACKAGE "\n"
248 "%s -[aA] [-n -e -v -q -V -r -u -w -m]\n"
249 " [-b basedirectory] [forced_version]\n"
250 "depmod [-n -e -v -q -r -u -w] [-F kernelsyms] module1.ko module2.ko ...\n"
251 "If no arguments (except options) are given, \"depmod -a\" is assumed\n"
252 "\n"
253 "depmod will output a dependancy list suitable for the modprobe utility.\n"
254 "\n"
255 "\n"
256 "Options:\n"
257 "\t-a, --all Probe all modules\n"
258 "\t-A, --quick Only does the work if there's a new module\n"
259 "\t-e, --errsyms Report not supplied symbols\n"
260 "\t-m, --map Create the legacy map files\n"
261 "\t-n, --show Write the dependency file on stdout only\n"
262 "\t-V, --version Print the release version\n"
263 "\t-v, --verbose Enable verbose mode\n"
264 "\t-w, --warn Warn on duplicates\n"
265 "\t-h, --help Print this usage message\n"
266 "\n"
267 "The following options are useful for people managing distributions:\n"
268 "\t-b basedirectory\n"
269 "\t --basedir basedirectory Use an image of a module tree.\n"
270 "\t-F kernelsyms\n"
271 "\t --filesyms kernelsyms Use the file instead of the\n"
272 "\t current kernel symbols.\n",
273 "depmod", "depmod");
276 static int ends_in(const char *name, const char *ext)
278 unsigned int namelen, extlen;
280 /* Grab lengths */
281 namelen = strlen(name);
282 extlen = strlen(ext);
284 if (namelen < extlen) return 0;
286 if (streq(name + namelen - extlen, ext))
287 return 1;
288 return 0;
291 /* "\177ELF" <byte> where byte = 001 for 32-bit, 002 for 64 */
292 int needconv(const char *elfhdr)
294 union { short s; char c[2]; } endian_test;
296 endian_test.s = 1;
297 if (endian_test.c[1] == 1) return elfhdr[EI_DATA] != ELFDATA2MSB;
298 if (endian_test.c[0] == 1) return elfhdr[EI_DATA] != ELFDATA2LSB;
299 else
300 abort();
303 static char *my_basename(const char *name)
305 const char *base = strrchr(name, '/');
306 if (base) return (char *)base + 1;
307 return (char *)name;
310 static struct module *grab_module(const char *dirname, const char *filename)
312 struct module *new;
314 new = NOFAIL(malloc(sizeof(*new)
315 + strlen(dirname?:"") + 1 + strlen(filename) + 1));
316 if (dirname)
317 sprintf(new->pathname, "%s/%s", dirname, filename);
318 else
319 strcpy(new->pathname, filename);
320 new->basename = my_basename(new->pathname);
322 INIT_LIST_HEAD(&new->dep_list);
323 new->order = INDEX_PRIORITY_MIN;
325 new->data = grab_file(new->pathname, &new->len);
326 if (!new->data) {
327 warn("Can't read module %s: %s\n",
328 new->pathname, strerror(errno));
329 goto fail_data;
332 /* "\177ELF" <byte> where byte = 001 for 32-bit, 002 for 64 */
333 if (memcmp(new->data, ELFMAG, SELFMAG) != 0) {
334 warn("Module %s is not an elf object\n", new->pathname);
335 goto fail;
338 switch (((char *)new->data)[EI_CLASS]) {
339 case ELFCLASS32:
340 new->ops = &mod_ops32;
341 break;
342 case ELFCLASS64:
343 new->ops = &mod_ops64;
344 break;
345 default:
346 warn("Module %s has elf unknown identifier %i\n",
347 new->pathname, ((char *)new->data)[EI_CLASS]);
348 goto fail;
350 new->conv = needconv(new->data);
351 return new;
353 fail:
354 release_file(new->data, new->len);
355 fail_data:
356 free(new);
357 return NULL;
360 struct module_traverse
362 struct module_traverse *prev;
363 struct module *mod;
366 static int in_loop(struct module *mod, const struct module_traverse *traverse)
368 const struct module_traverse *i;
370 for (i = traverse; i; i = i->prev) {
371 if (i->mod == mod)
372 return 1;
374 return 0;
377 /* Assume we are doing all the modules, so only report each loop once. */
378 static void report_loop(const struct module *mod,
379 const struct module_traverse *traverse)
381 const struct module_traverse *i;
383 /* Check that start is least alphabetically. eg. a depends
384 on b depends on a will get reported for a, not b. */
385 for (i = traverse->prev; i->prev; i = i->prev) {
386 if (strcmp(mod->pathname, i->mod->pathname) > 0)
387 return;
390 /* Is start in the loop? If not, don't report now. eg. a
391 depends on b which depends on c which depends on b. Don't
392 report when generating depends for a. */
393 if (mod != i->mod)
394 return;
396 warn("Loop detected: %s ", mod->pathname);
397 for (i = traverse->prev; i->prev; i = i->prev)
398 fprintf(stderr, "needs %s ", i->mod->basename);
399 fprintf(stderr, "which needs %s again!\n", i->mod->basename);
402 /* This is damn slow, but loops actually happen, and we don't want to
403 just exit() and leave the user without any modules. */
404 static int has_dep_loop(struct module *module, struct module_traverse *prev)
406 unsigned int i;
407 struct module_traverse traverse = { .prev = prev, .mod = module };
409 if (in_loop(module, prev)) {
410 report_loop(module, &traverse);
411 return 1;
414 for (i = 0; i < module->num_deps; i++)
415 if (has_dep_loop(module->deps[i], &traverse))
416 return 1;
417 return 0;
420 /* Uniquifies and orders a dependency list. */
421 static void order_dep_list(struct module *start, struct module *mod)
423 unsigned int i;
425 for (i = 0; i < mod->num_deps; i++) {
426 /* If it was previously depended on, move it to the
427 tail. ie. if a needs b and c, and c needs b, we
428 must order b after c. */
429 list_del(&mod->deps[i]->dep_list);
430 list_add_tail(&mod->deps[i]->dep_list, &start->dep_list);
431 order_dep_list(start, mod->deps[i]);
435 static struct module *deleted = NULL;
437 static void del_module(struct module **modules, struct module *delme)
439 struct module **i;
441 /* Find pointer to it. */
442 if (modules) {
443 for (i = modules; *i != delme; i = &(*i)->next);
445 *i = delme->next;
448 /* Save on a list to quiet valgrind.
449 Can't free - other modules may depend on them */
450 delme->next = deleted;
451 deleted = delme;
454 /* convert to relative path if possible */
455 static const char *compress_path(const char *path, const char *basedir)
457 int len = strlen(basedir);
459 if (strncmp(path, basedir, len) == 0)
460 path += len + 1;
461 return path;
464 static void output_deps(struct module *modules,
465 FILE *out, char *dirname)
467 struct module *i;
469 for (i = modules; i; i = i->next) {
470 struct list_head *j, *tmp;
471 order_dep_list(i, i);
473 fprintf(out, "%s:", compress_path(i->pathname, dirname));
474 list_for_each_safe(j, tmp, &i->dep_list) {
475 struct module *dep
476 = list_entry(j, struct module, dep_list);
477 fprintf(out, " %s",
478 compress_path(dep->pathname, dirname));
479 list_del_init(j);
481 fprintf(out, "\n");
485 /* warn whenever duplicate module aliases, deps, or symbols are found. */
486 int warn_dups = 0;
488 static void output_deps_bin(struct module *modules,
489 FILE *out, char *dirname)
491 struct module *i;
492 struct index_node *index;
493 char *line;
494 char *p;
496 index = index_create();
498 for (i = modules; i; i = i->next) {
499 struct list_head *j, *tmp;
500 char modname[strlen(i->pathname)+1];
502 order_dep_list(i, i);
504 filename2modname(modname, i->pathname);
505 nofail_asprintf(&line, "%s:",
506 compress_path(i->pathname, dirname));
507 p = line;
508 list_for_each_safe(j, tmp, &i->dep_list) {
509 struct module *dep
510 = list_entry(j, struct module, dep_list);
511 nofail_asprintf(&line, "%s %s",
513 compress_path(dep->pathname, dirname));
514 free(p);
515 p = line;
516 list_del_init(j);
518 if (index_insert(index, modname, line, i->order) && warn_dups)
519 warn("duplicate module deps:\n%s\n",line);
520 free(line);
523 index_write(index, out);
524 index_destroy(index);
528 static int smells_like_module(const char *name)
530 return ends_in(name,".ko") || ends_in(name, ".ko.gz");
533 typedef struct module *(*do_module_t)(const char *dirname,
534 const char *filename,
535 struct module *next,
536 struct module_search *search,
537 struct module_overrides *overrides);
539 static int is_higher_priority(const char *newpath, const char *oldpath,
540 struct module_search *search,
541 struct module_overrides *overrides)
543 struct module_search *tmp;
544 struct module_overrides *ovtmp;
545 int i = 0;
546 int prio_builtin = -1;
547 int prio_new = -1;
548 int prio_old = -1;
550 /* The names already match, now we check for overrides and directory search
551 * order
553 for (ovtmp = overrides; ovtmp != NULL; ovtmp = ovtmp->next) {
554 if (streq(ovtmp->modfile, newpath))
555 return 1;
556 if (streq(ovtmp->modfile, oldpath))
557 return 0;
559 for (i = 0, tmp = search; tmp != NULL; tmp = tmp->next, i++) {
560 if (streq(tmp->search_path, MODULE_BUILTIN_KEY))
561 prio_builtin = i;
562 else if (strncmp(tmp->search_path, newpath, tmp->len) == 0)
563 prio_new = i;
564 else if (strncmp(tmp->search_path, oldpath, tmp->len) == 0)
565 prio_old = i;
567 if (prio_new < 0)
568 prio_new = prio_builtin;
569 if (prio_old < 0)
570 prio_old = prio_builtin;
572 return prio_new > prio_old;
576 static struct module *do_module(const char *dirname,
577 const char *filename,
578 struct module *list,
579 struct module_search *search,
580 struct module_overrides *overrides)
582 struct module *new, **i;
584 new = grab_module(dirname, filename);
585 if (!new)
586 return list;
588 /* Check if module is already in the list. */
589 for (i = &list; *i; i = &(*i)->next) {
591 if (streq((*i)->basename, filename)) {
592 char newpath[strlen(dirname) + strlen("/")
593 + strlen(filename) + 1];
595 sprintf(newpath, "%s/%s", dirname, filename);
597 if (is_higher_priority(newpath, (*i)->pathname,search,
598 overrides)) {
599 del_module(i, *i);
601 new->next = *i;
602 *i = new;
603 } else
604 del_module(NULL, new);
606 return list;
610 /* Not in the list already. Just prepend. */
611 new->next = list;
612 return new;
615 static struct module *grab_dir(const char *dirname,
616 DIR *dir,
617 struct module *next,
618 do_module_t do_mod,
619 struct module_search *search,
620 struct module_overrides *overrides)
622 struct dirent *dirent;
624 while ((dirent = readdir(dir)) != NULL) {
625 if (smells_like_module(dirent->d_name))
626 next = do_mod(dirname, dirent->d_name, next,
627 search, overrides);
628 else if (!streq(dirent->d_name, ".")
629 && !streq(dirent->d_name, "..")
630 && !streq(dirent->d_name, "source")
631 && !streq(dirent->d_name, "build")) {
633 DIR *sub;
634 char subdir[strlen(dirname) + 1
635 + strlen(dirent->d_name) + 1];
636 sprintf(subdir, "%s/%s", dirname, dirent->d_name);
637 sub = opendir(subdir);
638 if (sub) {
639 next = grab_dir(subdir, sub, next, do_mod,
640 search, overrides);
641 closedir(sub);
645 return next;
648 static struct module *grab_basedir(const char *dirname,
649 struct module_search *search,
650 struct module_overrides *overrides)
652 DIR *dir;
653 struct module *list;
655 dir = opendir(dirname);
656 if (!dir) {
657 warn("Couldn't open directory %s: %s\n",
658 dirname, strerror(errno));
659 return NULL;
661 list = grab_dir(dirname, dir, NULL, do_module, search, overrides);
662 closedir(dir);
664 return list;
667 static struct module *sort_modules(const char *dirname, struct module *list)
669 struct module *tlist = NULL, **tpos = &tlist;
670 FILE *modorder;
671 int dir_len = strlen(dirname) + 1;
672 char file_name[dir_len + strlen("modules.order") + 1];
673 char line[10240];
674 unsigned int linenum = 0;
676 sprintf(file_name, "%s/%s", dirname, "modules.order");
678 modorder = fopen(file_name, "r");
679 if (!modorder) {
680 /* Older kernels don't generate modules.order. Just
681 return if the file doesn't exist. */
682 if (errno == ENOENT)
683 return list;
684 fatal("Could not open '%s': %s\n", file_name, strerror(errno));
687 sprintf(line, "%s/", dirname);
689 /* move modules listed in modorder file to tlist in order */
690 while (fgets(line, sizeof(line), modorder)) {
691 struct module **pos, *mod;
692 int len = strlen(line);
694 linenum++;
695 if (line[len - 1] == '\n')
696 line[len - 1] = '\0';
698 for (pos = &list; (mod = *pos); pos = &(*pos)->next) {
699 if (streq(line, mod->pathname + dir_len)) {
700 mod->order = linenum;
701 *pos = mod->next;
702 mod->next = NULL;
703 *tpos = mod;
704 tpos = &mod->next;
705 break;
710 /* append the rest */
711 *tpos = list;
713 fclose(modorder);
715 return tlist;
718 static struct module *parse_modules(struct module *list)
720 struct module *i;
722 for (i = list; i; i = i->next) {
723 i->ops->load_symbols(i);
724 i->ops->fetch_tables(i);
727 for (i = list; i; i = i->next)
728 i->ops->calculate_deps(i);
730 /* Strip out modules with dependency loops. */
731 again:
732 for (i = list; i; i = i->next) {
733 if (has_dep_loop(i, NULL)) {
734 warn("Module %s ignored, due to loop\n",
735 i->pathname + skipchars);
736 del_module(&list, i);
737 goto again;
741 return list;
744 /* Simply dump hash table. */
745 static void output_symbols(struct module *unused, FILE *out, char *dirname)
747 unsigned int i;
749 fprintf(out, "# Aliases for symbols, used by symbol_request().\n");
750 for (i = 0; i < SYMBOL_HASH_SIZE; i++) {
751 struct symbol *s;
753 for (s = symbolhash[i]; s; s = s->next) {
754 if (s->owner) {
755 char modname[strlen(s->owner->pathname)+1];
756 filename2modname(modname, s->owner->pathname);
757 fprintf(out, "alias symbol:%s %s\n",
758 s->name, modname);
764 static void output_symbols_bin(struct module *unused, FILE *out, char *dirname)
766 struct index_node *index;
767 unsigned int i;
768 char *alias;
769 int duplicate;
771 index = index_create();
773 for (i = 0; i < SYMBOL_HASH_SIZE; i++) {
774 struct symbol *s;
776 for (s = symbolhash[i]; s; s = s->next) {
777 if (s->owner) {
778 char modname[strlen(s->owner->pathname)+1];
779 filename2modname(modname, s->owner->pathname);
780 nofail_asprintf(&alias, "symbol:%s", s->name);
781 duplicate = index_insert(index, alias, modname,
782 s->owner->order);
783 if (duplicate && warn_dups)
784 warn("duplicate module syms:\n%s %s\n",
785 alias, modname);
786 free(alias);
791 index_write(index, out);
792 index_destroy(index);
795 static void output_aliases(struct module *modules, FILE *out, char *dirname)
797 struct module *i;
798 const char *p;
799 unsigned long size;
801 fprintf(out, "# Aliases extracted from modules themselves.\n");
802 for (i = modules; i; i = i->next) {
803 char modname[strlen(i->pathname)+1];
805 filename2modname(modname, i->pathname);
807 /* Grab from old-style .modalias section. */
808 for (p = i->ops->get_aliases(i, &size);
810 p = next_string(p, &size))
811 fprintf(out, "alias %s %s\n", p, modname);
813 /* Grab form new-style .modinfo section. */
814 for (p = i->ops->get_modinfo(i, &size);
816 p = next_string(p, &size)) {
817 if (strstarts(p, "alias="))
818 fprintf(out, "alias %s %s\n",
819 p + strlen("alias="), modname);
824 static void output_aliases_bin(struct module *modules, FILE *out, char *dirname)
826 struct module *i;
827 const char *p;
828 char *alias;
829 unsigned long size;
830 struct index_node *index;
831 int duplicate;
833 index = index_create();
835 for (i = modules; i; i = i->next) {
836 char modname[strlen(i->pathname)+1];
838 filename2modname(modname, i->pathname);
840 /* Grab from old-style .modalias section. */
841 for (p = i->ops->get_aliases(i, &size);
843 p = next_string(p, &size)) {
844 alias = NOFAIL(strdup(p));
845 underscores(alias);
846 duplicate = index_insert(index, alias, modname, i->order);
847 if (duplicate && warn_dups)
848 warn("duplicate module alias:\n%s %s\n",
849 alias, modname);
850 free(alias);
853 /* Grab from new-style .modinfo section. */
854 for (p = i->ops->get_modinfo(i, &size);
856 p = next_string(p, &size)) {
857 if (strstarts(p, "alias=")) {
858 alias = NOFAIL(strdup(p + strlen("alias=")));
859 underscores(alias);
860 duplicate = index_insert(index, alias, modname, i->order);
861 if (duplicate && warn_dups)
862 warn("duplicate module alias:\n%s %s\n",
863 alias, modname);
864 free(alias);
869 index_write(index, out);
870 index_destroy(index);
873 struct depfile {
874 char *name;
875 void (*func)(struct module *, FILE *, char *dirname);
876 int map_file;
879 static struct depfile depfiles[] = {
880 { "modules.dep", output_deps, 0 }, /* This is what we check for '-A'. */
881 { "modules.dep.bin", output_deps_bin, 0 },
882 { "modules.pcimap", output_pci_table, 1 },
883 { "modules.usbmap", output_usb_table, 1 },
884 { "modules.ccwmap", output_ccw_table, 1 },
885 { "modules.ieee1394map", output_ieee1394_table, 1 },
886 { "modules.isapnpmap", output_isapnp_table, 1 },
887 { "modules.inputmap", output_input_table, 1 },
888 { "modules.ofmap", output_of_table, 1 },
889 { "modules.seriomap", output_serio_table, 1 },
890 { "modules.alias", output_aliases, 0 },
891 { "modules.alias.bin", output_aliases_bin, 0 },
892 { "modules.symbols", output_symbols, 0 },
893 { "modules.symbols.bin", output_symbols_bin, 0 }
896 /* If we can't figure it out, it's safe to say "true". */
897 static int any_modules_newer(const char *dirname, time_t mtime)
899 DIR *dir;
900 struct dirent *dirent;
902 dir = opendir(dirname);
903 if (!dir)
904 return 1;
906 while ((dirent = readdir(dir)) != NULL) {
907 struct stat st;
908 char file[strlen(dirname) + 1 + strlen(dirent->d_name) + 1];
910 if (streq(dirent->d_name, ".") || streq(dirent->d_name, ".."))
911 continue;
913 sprintf(file, "%s/%s", dirname, dirent->d_name);
914 if (lstat(file, &st) != 0)
915 goto ret_true;
917 if (smells_like_module(dirent->d_name)) {
918 if (st.st_mtime > mtime)
919 goto ret_true;
920 } else if (S_ISDIR(st.st_mode)) {
921 if (any_modules_newer(file, mtime))
922 goto ret_true;
925 closedir(dir);
926 return 0;
928 ret_true:
929 closedir(dir);
930 return 1;
933 static int depfile_out_of_date(const char *dirname)
935 struct stat st;
936 char depfile[strlen(dirname) + 1 + strlen(depfiles[0].name) + 1];
938 sprintf(depfile, "%s/%s", dirname, depfiles[0].name);
940 if (stat(depfile, &st) != 0)
941 return 1;
943 return any_modules_newer(dirname, st.st_mtime);
946 static char *strsep_skipspace(char **string, char *delim)
948 if (!*string)
949 return NULL;
950 *string += strspn(*string, delim);
951 return strsep(string, delim);
954 static struct module_search *add_search(const char *search_path,
955 size_t len,
956 struct module_search *search)
959 struct module_search *new;
961 new = NOFAIL(malloc(sizeof(*new)));
962 new->search_path = NOFAIL(strdup(search_path));
963 new->len = len;
964 new->next = search;
966 return new;
970 static struct module_overrides *add_override(const char *modfile,
971 struct module_overrides *overrides)
974 struct module_overrides *new;
976 new = NOFAIL(malloc(sizeof(*new)));
977 new->modfile = NOFAIL(strdup(modfile));
978 new->next = overrides;
980 return new;
984 static int parse_config_scan(const char *filename,
985 const char *basedir,
986 const char *kernelversion,
987 struct module_search **search,
988 struct module_overrides **overrides);
990 static int parse_config_file(const char *filename,
991 const char *basedir,
992 const char *kernelversion,
993 struct module_search **search,
994 struct module_overrides **overrides)
996 char *line;
997 unsigned int linenum = 0;
998 FILE *cfile;
1000 cfile = fopen(filename, "r");
1001 if (!cfile) {
1002 if (errno != ENOENT)
1003 fatal("could not open '%s', reason: %s\n", filename,
1004 strerror(errno));
1005 return 0;
1008 while ((line = getline_wrapped(cfile, &linenum)) != NULL) {
1009 char *ptr = line;
1010 char *cmd, *modname;
1012 cmd = strsep_skipspace(&ptr, "\t ");
1014 if (cmd == NULL || cmd[0] == '#' || cmd[0] == '\0') {
1015 free(line);
1016 continue;
1019 if (streq(cmd, "search")) {
1020 char *search_path;
1022 while ((search_path = strsep_skipspace(&ptr, "\t "))) {
1023 char *dirname;
1024 size_t len;
1026 if (strcmp(search_path,
1027 MODULE_BUILTIN_KEY) == 0) {
1028 *search = add_search(MODULE_BUILTIN_KEY,
1029 0, *search);
1030 continue;
1032 nofail_asprintf(&dirname, "%s%s%s/%s", basedir,
1033 MODULE_DIR, kernelversion, search_path);
1034 len = strlen(dirname);
1035 *search = add_search(dirname, len, *search);
1036 free(dirname);
1038 } else if (streq(cmd, "override")) {
1039 char *pathname = NULL, *version, *subdir;
1040 modname = strsep_skipspace(&ptr, "\t ");
1041 version = strsep_skipspace(&ptr, "\t ");
1042 subdir = strsep_skipspace(&ptr, "\t ");
1044 if (strcmp(version, kernelversion) != 0 &&
1045 strcmp(version, "*") != 0)
1046 continue;
1048 nofail_asprintf(&pathname, "%s%s%s/%s/%s.ko", basedir,
1049 MODULE_DIR, kernelversion, subdir, modname);
1051 *overrides = add_override(pathname, *overrides);
1052 free(pathname);
1053 } else if (streq(cmd, "include")) {
1054 char *newfilename;
1056 newfilename = strsep_skipspace(&ptr, "\t ");
1057 if (!newfilename) {
1058 grammar(cmd, filename, linenum);
1059 } else {
1060 warn("\"include %s\" is deprecated, "
1061 "please use /etc/depmod.d\n", newfilename);
1062 if (strstarts(newfilename, "/etc/depmod.d")) {
1063 warn("\"include /etc/depmod.d\" is "
1064 "the default, ignored\n");
1065 } else {
1066 if (!parse_config_scan(newfilename, basedir,
1067 kernelversion,
1068 search, overrides))
1069 warn("Failed to open included"
1070 " config file %s: %s\n",
1071 newfilename, strerror(errno));
1074 } else if (streq(cmd, "make_map_files")) {
1075 char *option;
1077 option = strsep_skipspace(&ptr, "\t ");
1078 if (!option)
1079 grammar(cmd, filename, linenum);
1080 else {
1081 if (streq(option, "yes"))
1082 make_map_files = 1;
1083 else if (streq(option, "no"))
1084 make_map_files = 0;
1085 else
1086 grammar(cmd, filename, linenum);
1088 } else
1089 grammar(cmd, filename, linenum);
1091 free(line);
1093 fclose(cfile);
1094 return 1;
1097 static int parse_config_scan(const char *filename,
1098 const char *basedir,
1099 const char *kernelversion,
1100 struct module_search **search,
1101 struct module_overrides **overrides)
1103 DIR *dir;
1104 int ret = 0;
1106 dir = opendir(filename);
1107 if (dir) {
1108 struct file_entry {
1109 struct list_head node;
1110 char name[];
1112 LIST_HEAD(files_list);
1113 struct file_entry *fe, *fe_tmp;
1114 struct dirent *i;
1116 /* sort files from directory into list */
1117 while ((i = readdir(dir)) != NULL) {
1118 size_t len;
1120 if (i->d_name[0] == '.')
1121 continue;
1122 if (!config_filter(i->d_name))
1123 continue;
1125 len = strlen(i->d_name);
1126 if (len < 6 || strcmp(&i->d_name[len-5], ".conf") != 0)
1127 warn("All config files need .conf: %s/%s, "
1128 "it will be ignored in a future release.\n",
1129 filename, i->d_name);
1130 fe = malloc(sizeof(struct file_entry) + len + 1);
1131 if (fe == NULL)
1132 continue;
1133 strcpy(fe->name, i->d_name);
1134 list_for_each_entry(fe_tmp, &files_list, node)
1135 if (strcmp(fe_tmp->name, fe->name) >= 0)
1136 break;
1137 list_add_tail(&fe->node, &fe_tmp->node);
1139 closedir(dir);
1141 /* parse list of files */
1142 list_for_each_entry_safe(fe, fe_tmp, &files_list, node) {
1143 char *cfgfile;
1145 nofail_asprintf(&cfgfile, "%s/%s", filename, fe->name);
1146 if (!parse_config_file(cfgfile, basedir, kernelversion,
1147 search, overrides))
1148 warn("Failed to open config file "
1149 "%s: %s\n", fe->name, strerror(errno));
1150 free(cfgfile);
1151 list_del(&fe->node);
1152 free(fe);
1155 ret = 1;
1156 } else {
1157 if (parse_config_file(filename, basedir, kernelversion, search,
1158 overrides))
1159 ret = 1;
1162 return ret;
1165 static void parse_toplevel_config(const char *filename,
1166 const char *basedir,
1167 const char *kernelversion,
1168 struct module_search **search,
1169 struct module_overrides **overrides)
1171 if (filename) {
1172 if (!parse_config_scan(filename, basedir, kernelversion, search,
1173 overrides))
1174 fatal("Failed to open config file %s: %s\n",
1175 filename, strerror(errno));
1176 return;
1179 /* deprecated config file */
1180 if (parse_config_file("/etc/depmod.conf", basedir, kernelversion,
1181 search, overrides) > 0)
1182 warn("Deprecated config file /etc/depmod.conf, "
1183 "all config files belong into /etc/depmod.d/.\n");
1185 /* default config */
1186 parse_config_scan("/etc/depmod.d", basedir, kernelversion,
1187 search, overrides);
1190 /* Local to main, but not freed on exit. Keep valgrind quiet. */
1191 struct module *list = NULL;
1192 struct module_search *search = NULL;
1193 struct module_overrides *overrides = NULL;
1195 int main(int argc, char *argv[])
1197 int opt, all = 0, maybe_all = 0, doing_stdout = 0;
1198 char *basedir = "", *dirname, *version, *badopt = NULL,
1199 *system_map = NULL;
1200 int i;
1201 const char *config = NULL;
1203 /* Don't print out any errors just yet, we might want to exec
1204 backwards compat version. */
1205 opterr = 0;
1206 while ((opt = getopt_long(argc, argv, "ab:ArehnqruvVF:C:wm", options, NULL))
1207 != -1) {
1208 switch (opt) {
1209 case 'a':
1210 all = 1;
1211 break;
1212 case 'b':
1213 basedir = optarg;
1214 skipchars = strlen(basedir);
1215 break;
1216 case 'A':
1217 maybe_all = 1;
1218 break;
1219 case 'F':
1220 system_map = optarg;
1221 break;
1222 case 'e':
1223 print_unknown = 1;
1224 break;
1225 case 'v':
1226 verbose = 1;
1227 break;
1228 case 'u':
1229 case 'q':
1230 case 'r':
1231 break;
1232 case 'C':
1233 config = optarg;
1234 break;
1235 case 'h':
1236 print_usage(argv[0]);
1237 exit(0);
1238 break;
1239 case 'n':
1240 doing_stdout = 1;
1241 break;
1242 case 'V':
1243 printf("%s %s\n", PACKAGE, VERSION);
1244 exit(0);
1245 case 'w':
1246 warn_dups = 1;
1247 break;
1248 case 'm':
1249 force_map_files = 1;
1250 break;
1251 default:
1252 badopt = argv[optind-1];
1256 /* We can't print unknowns without a System.map */
1257 if (!system_map)
1258 print_unknown = 0;
1259 else
1260 load_system_map(system_map);
1262 /* They can specify the version naked on the command line */
1263 if (optind < argc && is_version_number(argv[optind])) {
1264 version = NOFAIL(strdup(argv[optind]));
1265 optind++;
1266 } else {
1267 struct utsname buf;
1268 uname(&buf);
1269 version = NOFAIL(strdup(buf.release));
1272 /* Run old version if required. */
1273 if (old_module_version(version))
1274 exec_old_depmod(argv);
1276 if (badopt) {
1277 fprintf(stderr, "%s: malformed/unrecognized option '%s'\n",
1278 argv[0], badopt);
1279 print_usage(argv[0]);
1280 exit(1);
1283 /* Depmod -a by default if no names. */
1284 if (optind == argc)
1285 all = 1;
1287 nofail_asprintf(&dirname, "%s%s%s", basedir, MODULE_DIR, version);
1289 if (maybe_all) {
1290 if (!doing_stdout && !depfile_out_of_date(dirname))
1291 exit(0);
1292 all = 1;
1295 parse_toplevel_config(config, basedir, version, &search, &overrides);
1297 /* For backward compatibility add "updates" to the head of the search
1298 * list here. But only if there was no "search" option specified.
1300 if (!search) {
1301 char *dirname;
1302 size_t len;
1304 nofail_asprintf(&dirname, "%s%s%s/updates", basedir,
1305 MODULE_DIR, version);
1306 len = strlen(dirname);
1307 search = add_search(dirname, len, search);
1309 if (!all) {
1310 /* Do command line args. */
1311 for (opt = optind; opt < argc; opt++) {
1312 struct module *new;
1314 if (argv[opt][0] != '/')
1315 fatal("modules must be specified using absolute paths.\n"
1316 "\"%s\" is a relative path\n", argv[opt]);
1318 new = grab_module(NULL, argv[opt]);
1319 if (!new) {
1320 /* cmd-line specified modules must exist */
1321 fatal("grab_module() failed for module %s\n", argv[opt]);
1323 new->next = list;
1324 list = new;
1326 } else {
1327 list = grab_basedir(dirname,search,overrides);
1329 list = sort_modules(dirname,list);
1330 list = parse_modules(list);
1332 for (i = 0; i < sizeof(depfiles)/sizeof(depfiles[0]); i++) {
1333 FILE *out;
1334 struct depfile *d = &depfiles[i];
1335 char depname[strlen(dirname) + 1 + strlen(d->name) + 1];
1336 char tmpname[strlen(dirname) + 1 + strlen(d->name) +
1337 strlen(".temp") + 1];
1339 if (d->map_file && !make_map_files && !force_map_files)
1340 continue;
1342 sprintf(depname, "%s/%s", dirname, d->name);
1343 sprintf(tmpname, "%s/%s.temp", dirname, d->name);
1344 if (!doing_stdout) {
1345 out = fopen(tmpname, "w");
1346 if (!out)
1347 fatal("Could not open %s for writing: %s\n",
1348 tmpname, strerror(errno));
1349 } else {
1350 out = stdout;
1351 if (ends_in(depname, ".bin"))
1352 continue;
1354 d->func(list, out, dirname);
1355 if (!doing_stdout) {
1356 fclose(out);
1357 if (rename(tmpname, depname) < 0)
1358 fatal("Could not rename %s into %s: %s\n",
1359 tmpname, depname, strerror(errno));
1363 free(dirname);
1364 free(version);
1366 return 0;