Bump the release up to 3.6-pre1. This needs some testing still.
[mit.git] / depmod.c
blobc396c46e71fd82382849a23ac893708f424b8b26
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 "zlibsupport.h"
25 #include "depmod.h"
26 #include "logging.h"
27 #include "index.h"
28 #include "moduleops.h"
29 #include "tables.h"
31 #include "testing.h"
33 #ifndef MODULE_DIR
34 #define MODULE_DIR "/lib/modules/"
35 #endif
37 #ifndef MODULE_BUILTIN_KEY
38 #define MODULE_BUILTIN_KEY "built-in"
39 #endif
41 struct module_overrides
43 /* Next override */
44 struct module_overrides *next;
46 /* overridden module */
47 char *modfile;
50 struct module_search
52 /* Next search */
53 struct module_search *next;
55 /* search path */
56 char *search_path;
59 static int verbose;
60 static unsigned int skipchars;
61 static unsigned int make_map_files = 1; /* default to on */
62 static unsigned int force_map_files = 0; /* default to on */
64 #define SYMBOL_HASH_SIZE 1024
65 struct symbol
67 struct symbol *next;
68 struct module *owner;
69 char name[0];
72 static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
74 /* This is based on the hash agorithm from gdbm, via tdb */
75 static inline unsigned int tdb_hash(const char *name)
77 unsigned value; /* Used to compute the hash value. */
78 unsigned i; /* Used to cycle through random values. */
80 /* Set the initial value from the key size. */
81 for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
82 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
84 return (1103515243 * value + 12345);
87 void add_symbol(const char *name, struct module *owner)
89 unsigned int hash;
90 struct symbol *new = NOFAIL(malloc(sizeof *new + strlen(name) + 1));
92 new->owner = owner;
93 strcpy(new->name, name);
95 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
96 new->next = symbolhash[hash];
97 symbolhash[hash] = new;
100 static int print_unknown;
102 struct module *find_symbol(const char *name, const char *modname, int weak)
104 struct symbol *s;
106 /* For our purposes, .foo matches foo. PPC64 needs this. */
107 if (name[0] == '.')
108 name++;
110 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) {
111 if (streq(s->name, name))
112 return s->owner;
115 if (print_unknown && !weak)
116 warn("%s needs unknown symbol %s\n", modname, name);
118 return NULL;
121 void add_dep(struct module *mod, struct module *depends_on)
123 unsigned int i;
125 for (i = 0; i < mod->num_deps; i++)
126 if (mod->deps[i] == depends_on)
127 return;
129 mod->deps = NOFAIL(realloc(mod->deps, sizeof(mod->deps[0])*(mod->num_deps+1)));
130 mod->deps[mod->num_deps++] = depends_on;
133 static void load_system_map(const char *filename)
135 FILE *system_map;
136 char line[10240];
137 const char ksymstr[] = "__ksymtab_";
138 const int ksymstr_len = strlen(ksymstr);
140 system_map = fopen(filename, "r");
141 if (!system_map)
142 fatal("Could not open '%s': %s\n", filename, strerror(errno));
144 /* eg. c0294200 R __ksymtab_devfs_alloc_devnum */
145 while (fgets(line, sizeof(line)-1, system_map)) {
146 char *ptr;
148 /* Snip \n */
149 ptr = strchr(line, '\n');
150 if (ptr)
151 *ptr = '\0';
153 ptr = strchr(line, ' ');
154 if (!ptr || !(ptr = strchr(ptr + 1, ' ')))
155 continue;
157 /* Covers gpl-only and normal symbols. */
158 if (strncmp(ptr+1, ksymstr, ksymstr_len) == 0)
159 add_symbol(ptr+1+ksymstr_len, NULL);
162 fclose(system_map);
164 /* __this_module is magic inserted by kernel loader. */
165 add_symbol("__this_module", NULL);
166 /* On S390, this is faked up too */
167 add_symbol("_GLOBAL_OFFSET_TABLE_", NULL);
170 static struct option options[] = { { "all", 0, NULL, 'a' },
171 { "quick", 0, NULL, 'A' },
172 { "basedir", 1, NULL, 'b' },
173 { "errsyms", 0, NULL, 'e' },
174 { "filesyms", 1, NULL, 'F' },
175 { "help", 0, NULL, 'h' },
176 { "show", 0, NULL, 'n' },
177 { "dry-run", 0, NULL, 'n' },
178 { "quiet", 0, NULL, 'q' },
179 { "root", 0, NULL, 'r' },
180 { "unresolved-error", 0, NULL, 'u' },
181 { "verbose", 0, NULL, 'v' },
182 { "version", 0, NULL, 'V' },
183 { "config", 1, NULL, 'C' },
184 { "warn", 1, NULL, 'w' },
185 { "map", 0, NULL, 'm' },
186 { NULL, 0, NULL, 0 } };
188 /* Version number or module name? Don't assume extension. */
189 static int is_version_number(const char *version)
191 unsigned int dummy;
193 return (sscanf(version, "%u.%u.%u", &dummy, &dummy, &dummy) == 3);
196 static int old_module_version(const char *version)
198 /* Expect three part version. */
199 unsigned int major, sub, minor;
201 sscanf(version, "%u.%u.%u", &major, &sub, &minor);
203 if (major > 2) return 0;
204 if (major < 2) return 1;
206 /* 2.x */
207 if (sub > 5) return 0;
208 if (sub < 5) return 1;
210 /* 2.5.x */
211 if (minor >= 48) return 0;
212 return 1;
215 static void exec_old_depmod(char *argv[])
217 char *sep;
218 char pathname[strlen(argv[0])+1];
219 char oldname[strlen("depmod") + strlen(argv[0]) + sizeof(".old")];
221 memset(pathname, 0, strlen(argv[0])+1);
222 sep = strrchr(argv[0], '/');
223 if (sep)
224 memcpy(pathname, argv[0], sep - argv[0]+1);
225 sprintf(oldname, "%s%s.old", pathname, "depmod");
227 /* Recursion detection: we need an env var since we can't
228 change argv[0] (as older modutils uses it to determine
229 behavior). */
230 if (getenv("MODULE_RECURSE"))
231 return;
232 setenv("MODULE_RECURSE", "y", 0);
234 execvp(oldname, argv);
235 fprintf(stderr,
236 "Version requires old depmod, but couldn't run %s: %s\n",
237 oldname, strerror(errno));
238 exit(2);
241 static void grammar(const char *cmd, const char *filename, unsigned int line)
243 warn("%s line %u: ignoring bad line starting with '%s'\n",
244 filename, line, cmd);
248 static void print_usage(const char *name)
250 fprintf(stderr,
251 "%s " VERSION " -- part of " PACKAGE "\n"
252 "%s -[aA] [-n -e -v -q -V -r -u -w -m]\n"
253 " [-b basedirectory] [forced_version]\n"
254 "depmod [-n -e -v -q -r -u -w] [-F kernelsyms] module1.ko module2.ko ...\n"
255 "If no arguments (except options) are given, \"depmod -a\" is assumed\n"
256 "\n"
257 "depmod will output a dependancy list suitable for the modprobe utility.\n"
258 "\n"
259 "\n"
260 "Options:\n"
261 "\t-a, --all Probe all modules\n"
262 "\t-A, --quick Only does the work if there's a new module\n"
263 "\t-e, --errsyms Report not supplied symbols\n"
264 "\t-m, --map Create the legacy map files\n"
265 "\t-n, --show Write the dependency file on stdout only\n"
266 "\t-V, --version Print the release version\n"
267 "\t-v, --verbose Enable verbose mode\n"
268 "\t-w, --warn Warn on duplicates\n"
269 "\t-h, --help Print this usage message\n"
270 "\n"
271 "The following options are useful for people managing distributions:\n"
272 "\t-b basedirectory\n"
273 "\t --basedir basedirectory Use an image of a module tree.\n"
274 "\t-F kernelsyms\n"
275 "\t --filesyms kernelsyms Use the file instead of the\n"
276 "\t current kernel symbols.\n",
277 "depmod", "depmod");
280 static int ends_in(const char *name, const char *ext)
282 unsigned int namelen, extlen;
284 /* Grab lengths */
285 namelen = strlen(name);
286 extlen = strlen(ext);
288 if (namelen < extlen) return 0;
290 if (streq(name + namelen - extlen, ext))
291 return 1;
292 return 0;
295 /* "\177ELF" <byte> where byte = 001 for 32-bit, 002 for 64 */
296 int needconv(const char *elfhdr)
298 union { short s; char c[2]; } endian_test;
300 endian_test.s = 1;
301 if (endian_test.c[1] == 1) return elfhdr[EI_DATA] != ELFDATA2MSB;
302 if (endian_test.c[0] == 1) return elfhdr[EI_DATA] != ELFDATA2LSB;
303 else
304 abort();
307 static char *my_basename(const char *name)
309 const char *base = strrchr(name, '/');
310 if (base) return (char *)base + 1;
311 return (char *)name;
314 static struct module *grab_module(const char *dirname, const char *filename)
316 struct module *new;
318 new = NOFAIL(malloc(sizeof(*new)
319 + strlen(dirname?:"") + 1 + strlen(filename) + 1));
320 if (dirname)
321 sprintf(new->pathname, "%s/%s", dirname, filename);
322 else
323 strcpy(new->pathname, filename);
324 new->basename = my_basename(new->pathname);
326 INIT_LIST_HEAD(&new->dep_list);
328 new->data = grab_file(new->pathname, &new->len);
329 if (!new->data) {
330 warn("Can't read module %s: %s\n",
331 new->pathname, strerror(errno));
332 goto fail_data;
335 /* "\177ELF" <byte> where byte = 001 for 32-bit, 002 for 64 */
336 if (memcmp(new->data, ELFMAG, SELFMAG) != 0) {
337 warn("Module %s is not an elf object\n", new->pathname);
338 goto fail;
341 switch (((char *)new->data)[EI_CLASS]) {
342 case ELFCLASS32:
343 new->ops = &mod_ops32;
344 break;
345 case ELFCLASS64:
346 new->ops = &mod_ops64;
347 break;
348 default:
349 warn("Module %s has elf unknown identifier %i\n",
350 new->pathname, ((char *)new->data)[EI_CLASS]);
351 goto fail;
353 new->conv = needconv(new->data);
354 return new;
356 fail:
357 release_file(new->data, new->len);
358 fail_data:
359 free(new);
360 return NULL;
363 struct module_traverse
365 struct module_traverse *prev;
366 struct module *mod;
369 static int in_loop(struct module *mod, const struct module_traverse *traverse)
371 const struct module_traverse *i;
373 for (i = traverse; i; i = i->prev) {
374 if (i->mod == mod)
375 return 1;
377 return 0;
380 /* Assume we are doing all the modules, so only report each loop once. */
381 static void report_loop(const struct module *mod,
382 const struct module_traverse *traverse)
384 const struct module_traverse *i;
386 /* Check that start is least alphabetically. eg. a depends
387 on b depends on a will get reported for a, not b. */
388 for (i = traverse->prev; i->prev; i = i->prev) {
389 if (strcmp(mod->pathname, i->mod->pathname) > 0)
390 return;
393 /* Is start in the loop? If not, don't report now. eg. a
394 depends on b which depends on c which depends on b. Don't
395 report when generating depends for a. */
396 if (mod != i->mod)
397 return;
399 warn("Loop detected: %s ", mod->pathname);
400 for (i = traverse->prev; i->prev; i = i->prev)
401 fprintf(stderr, "needs %s ", i->mod->basename);
402 fprintf(stderr, "which needs %s again!\n", i->mod->basename);
405 /* This is damn slow, but loops actually happen, and we don't want to
406 just exit() and leave the user without any modules. */
407 static int has_dep_loop(struct module *module, struct module_traverse *prev)
409 unsigned int i;
410 struct module_traverse traverse = { .prev = prev, .mod = module };
412 if (in_loop(module, prev)) {
413 report_loop(module, &traverse);
414 return 1;
417 for (i = 0; i < module->num_deps; i++)
418 if (has_dep_loop(module->deps[i], &traverse))
419 return 1;
420 return 0;
423 /* Uniquifies and orders a dependency list. */
424 static void order_dep_list(struct module *start, struct module *mod)
426 unsigned int i;
428 for (i = 0; i < mod->num_deps; i++) {
429 /* If it was previously depended on, move it to the
430 tail. ie. if a needs b and c, and c needs b, we
431 must order b after c. */
432 list_del(&mod->deps[i]->dep_list);
433 list_add_tail(&mod->deps[i]->dep_list, &start->dep_list);
434 order_dep_list(start, mod->deps[i]);
438 static struct module *deleted = NULL;
440 static void del_module(struct module **modules, struct module *delme)
442 struct module **i;
444 /* Find pointer to it. */
445 if (modules) {
446 for (i = modules; *i != delme; i = &(*i)->next);
448 *i = delme->next;
451 /* Save on a list to quiet valgrind.
452 Can't free - other modules may depend on them */
453 delme->next = deleted;
454 deleted = delme;
457 /* Convert filename to the module name. Works if filename == modname, too. */
458 static void filename2modname(char *modname, const char *filename)
460 const char *afterslash;
461 unsigned int i;
463 afterslash = strrchr(filename, '/');
464 if (!afterslash)
465 afterslash = filename;
466 else
467 afterslash++;
469 /* Convert to underscores, stop at first . */
470 for (i = 0; afterslash[i] && afterslash[i] != '.'; i++) {
471 if (afterslash[i] == '-')
472 modname[i] = '_';
473 else
474 modname[i] = afterslash[i];
476 modname[i] = '\0';
479 static void output_deps(struct module *modules,
480 FILE *out, char *dirname)
482 struct module *i;
484 for (i = modules; i; i = i->next) {
485 struct list_head *j, *tmp;
486 order_dep_list(i, i);
488 fprintf(out, "%s:", i->pathname + strlen(dirname)+1);
489 list_for_each_safe(j, tmp, &i->dep_list) {
490 struct module *dep
491 = list_entry(j, struct module, dep_list);
492 fprintf(out, " %s", dep->pathname + strlen(dirname)+1);
493 list_del_init(j);
495 fprintf(out, "\n");
499 /* warn whenever duplicate module aliases, deps, or symbols are found. */
500 int warn_dups = 0;
502 static void output_deps_bin(struct module *modules,
503 FILE *out, char *dirname)
505 struct module *i;
506 struct index_node *index;
507 char *line;
508 char *p;
510 index = index_create();
512 for (i = modules; i; i = i->next) {
513 struct list_head *j, *tmp;
514 char modname[strlen(i->pathname)+1];
516 order_dep_list(i, i);
518 filename2modname(modname, i->pathname + strlen(dirname)+1);
519 nofail_asprintf(&line, "%s %s:", modname, i->pathname + strlen(dirname)+1);
520 p = line;
521 list_for_each_safe(j, tmp, &i->dep_list) {
522 struct module *dep
523 = list_entry(j, struct module, dep_list);
524 nofail_asprintf(&line, "%s %s", p, dep->pathname + strlen(dirname)+1);
525 free(p);
526 p = line;
527 list_del_init(j);
529 if (index_insert(index, line) && warn_dups)
530 warn("duplicate module deps:\n%s\n",line);
531 free(line);
534 index_write(index, out);
535 index_destroy(index);
539 static int smells_like_module(const char *name)
541 return ends_in(name,".ko") || ends_in(name, ".ko.gz");
544 typedef struct module *(*do_module_t)(const char *dirname,
545 const char *filename,
546 struct module *next,
547 struct module_search *search,
548 struct module_overrides *overrides);
550 static int is_higher_priority(const char *newpath, const char *oldpath,
551 struct module_search *search,
552 struct module_overrides *overrides)
554 char *p, *q, *s;
555 struct module_search *tmp;
556 struct module_overrides *ovtmp;
557 int i = 0;
558 int prio_builtin = -1;
559 int prio_new = -1;
560 int prio_old = -1;
562 /* The names already match, now we check for wildcard overrides and other
563 * high priority overrides we added to the config.
565 for (ovtmp=overrides;ovtmp!=NULL;ovtmp=ovtmp->next) {
567 p = strstr(ovtmp->modfile,newpath);
568 if (p)
569 return 1;
571 q = strstr(ovtmp->modfile,"*");
572 if (q) {
573 p = strstr(newpath, q+1);
574 if (p)
575 return 1;
577 p = strstr(oldpath, q+1);
578 if (p)
579 return 0;
582 p = strstr(ovtmp->modfile,oldpath);
583 if (p)
584 return 0;
587 for (i=0,tmp=search;tmp!=NULL;tmp=tmp->next,i++) {
589 s = NOFAIL(malloc(strlen(tmp->search_path)+2));
590 s[0] = '/';
591 strncpy(s+1,tmp->search_path, strlen(tmp->search_path)+1);
593 if (0 == strncmp(tmp->search_path,MODULE_BUILTIN_KEY,
594 strlen(MODULE_BUILTIN_KEY)))
595 prio_builtin = i;
597 p = strstr(newpath,s);
598 if ((p) && ((p[strlen(s)] == '/')
599 || (p[strlen(s)] == '\0')))
600 prio_new = i;
602 p = strstr(oldpath,s);
603 if ((p) && ((p[strlen(s)] == '/')
604 || (p[strlen(s)] == '\0')))
605 prio_old = i;
607 free(s);
611 if (prio_new < 0)
612 prio_new = prio_builtin;
614 if (prio_old < 0)
615 prio_old = prio_builtin;
617 return prio_new > prio_old;
622 static struct module *do_module(const char *dirname,
623 const char *filename,
624 struct module *list,
625 struct module_search *search,
626 struct module_overrides *overrides)
628 struct module *new, **i;
630 new = grab_module(dirname, filename);
631 if (!new)
632 return list;
634 /* Check if module is already in the list. */
635 for (i = &list; *i; i = &(*i)->next) {
637 if (streq((*i)->basename, filename)) {
638 char newpath[strlen(dirname) + strlen("/")
639 + strlen(filename) + 1];
641 sprintf(newpath, "%s/%s", dirname, filename);
643 if (is_higher_priority(newpath, (*i)->pathname,search,
644 overrides)) {
645 del_module(i, *i);
647 new->next = *i;
648 *i = new;
649 } else
650 del_module(NULL, new);
652 return list;
656 /* Not in the list already. Just prepend. */
657 new->next = list;
658 return new;
661 static struct module *grab_dir(const char *dirname,
662 DIR *dir,
663 struct module *next,
664 do_module_t do_mod,
665 struct module_search *search,
666 struct module_overrides *overrides)
668 struct dirent *dirent;
670 while ((dirent = readdir(dir)) != NULL) {
671 if (smells_like_module(dirent->d_name))
672 next = do_mod(dirname, dirent->d_name, next,
673 search, overrides);
674 else if (!streq(dirent->d_name, ".")
675 && !streq(dirent->d_name, "..")
676 && !streq(dirent->d_name, "source")
677 && !streq(dirent->d_name, "build")) {
679 DIR *sub;
680 char subdir[strlen(dirname) + 1
681 + strlen(dirent->d_name) + 1];
682 sprintf(subdir, "%s/%s", dirname, dirent->d_name);
683 sub = opendir(subdir);
684 if (sub) {
685 next = grab_dir(subdir, sub, next, do_mod,
686 search, overrides);
687 closedir(sub);
691 return next;
694 static struct module *grab_basedir(const char *dirname,
695 struct module_search *search,
696 struct module_overrides *overrides)
698 DIR *dir;
699 struct module *list;
701 dir = opendir(dirname);
702 if (!dir) {
703 warn("Couldn't open directory %s: %s\n",
704 dirname, strerror(errno));
705 return NULL;
707 list = grab_dir(dirname, dir, NULL, do_module, search, overrides);
708 closedir(dir);
710 return list;
713 static struct module *parse_modules(struct module *list)
715 struct module *i;
717 for (i = list; i; i = i->next) {
718 i->ops->load_symbols(i);
719 i->ops->fetch_tables(i);
722 for (i = list; i; i = i->next)
723 i->ops->calculate_deps(i, verbose);
725 /* Strip out modules with dependency loops. */
726 again:
727 for (i = list; i; i = i->next) {
728 if (has_dep_loop(i, NULL)) {
729 warn("Module %s ignored, due to loop\n",
730 i->pathname + skipchars);
731 del_module(&list, i);
732 goto again;
736 return list;
739 /* Simply dump hash table. */
740 static void output_symbols(struct module *unused, FILE *out, char *dirname)
742 unsigned int i;
744 fprintf(out, "# Aliases for symbols, used by symbol_request().\n");
745 for (i = 0; i < SYMBOL_HASH_SIZE; i++) {
746 struct symbol *s;
748 for (s = symbolhash[i]; s; s = s->next) {
749 if (s->owner) {
750 char modname[strlen(s->owner->pathname)+1];
751 filename2modname(modname, s->owner->pathname);
752 fprintf(out, "alias symbol:%s %s\n",
753 s->name, modname);
759 static void output_symbols_bin(struct module *unused, FILE *out, char *dirname)
761 struct index_node *index;
762 unsigned int i;
763 char *line;
765 index = index_create();
767 for (i = 0; i < SYMBOL_HASH_SIZE; i++) {
768 struct symbol *s;
770 for (s = symbolhash[i]; s; s = s->next) {
771 if (s->owner) {
772 char modname[strlen(s->owner->pathname)+1];
773 filename2modname(modname, s->owner->pathname);
774 nofail_asprintf(&line, "symbol:%s %s",
775 s->name, modname);
776 if (index_insert(index, line) && warn_dups)
777 warn("duplicate module syms:\n%s\n",
778 line);
779 free(line);
784 index_write(index, out);
785 index_destroy(index);
788 static const char *next_string(const char *string, unsigned long *secsize)
790 /* Skip non-zero chars */
791 while (string[0]) {
792 string++;
793 if ((*secsize)-- <= 1)
794 return NULL;
797 /* Skip any zero padding. */
798 while (!string[0]) {
799 string++;
800 if ((*secsize)-- <= 1)
801 return NULL;
803 return string;
806 /* Careful! Don't munge - in [ ] as per Debian Bug#350915 */
807 static char *underscores(char *string)
809 unsigned int i;
811 if (!string)
812 return NULL;
814 for (i = 0; string[i]; i++) {
815 switch (string[i]) {
816 case '-':
817 string[i] = '_';
818 break;
820 case '[':
821 i += strcspn(&string[i], "]");
822 if (!string[i])
823 warn("Unmatched bracket in %s\n", string);
826 return string;
829 static void output_aliases(struct module *modules, FILE *out, char *dirname)
831 struct module *i;
832 const char *p;
833 unsigned long size;
835 fprintf(out, "# Aliases extracted from modules themselves.\n");
836 for (i = modules; i; i = i->next) {
837 char modname[strlen(i->pathname)+1];
839 filename2modname(modname, i->pathname);
841 /* Grab from old-style .modalias section. */
842 for (p = i->ops->get_aliases(i, &size);
844 p = next_string(p, &size))
845 fprintf(out, "alias %s %s\n", p, modname);
847 /* Grab form new-style .modinfo section. */
848 for (p = i->ops->get_modinfo(i, &size);
850 p = next_string(p, &size)) {
851 if (strncmp(p, "alias=", strlen("alias=")) == 0)
852 fprintf(out, "alias %s %s\n",
853 p + strlen("alias="), modname);
858 static void output_aliases_bin(struct module *modules, FILE *out, char *dirname)
860 struct module *i;
861 const char *p;
862 char *line;
863 unsigned long size;
864 struct index_node *index;
866 index = index_create();
868 for (i = modules; i; i = i->next) {
869 char modname[strlen(i->pathname)+1];
871 filename2modname(modname, i->pathname);
873 /* Grab from old-style .modalias section. */
874 for (p = i->ops->get_aliases(i, &size);
876 p = next_string(p, &size)) {
877 nofail_asprintf(&line, "%s %s", p, modname);
878 underscores(line);
879 if (index_insert(index, line) && warn_dups)
880 warn("duplicate module alias:\n%s\n", line);
881 free(line);
884 /* Grab from new-style .modinfo section. */
885 for (p = i->ops->get_modinfo(i, &size);
887 p = next_string(p, &size)) {
888 if (strncmp(p, "alias=", strlen("alias=")) == 0) {
889 nofail_asprintf(&line, "%s %s",
890 p + strlen("alias="), modname);
891 underscores(line);
892 if (index_insert(index, line) && warn_dups)
893 warn("duplicate module alias:\n%s\n",
894 line);
895 free(line);
900 index_write(index, out);
901 index_destroy(index);
904 struct depfile {
905 char *name;
906 void (*func)(struct module *, FILE *, char *dirname);
907 int map_file;
910 static struct depfile depfiles[] = {
911 { "modules.dep", output_deps, 0 }, /* This is what we check for '-A'. */
912 { "modules.dep.bin", output_deps_bin, 0 },
913 { "modules.pcimap", output_pci_table, 1 },
914 { "modules.usbmap", output_usb_table, 1 },
915 { "modules.ccwmap", output_ccw_table, 1 },
916 { "modules.ieee1394map", output_ieee1394_table, 1 },
917 { "modules.isapnpmap", output_isapnp_table, 1 },
918 { "modules.inputmap", output_input_table, 1 },
919 { "modules.ofmap", output_of_table, 1 },
920 { "modules.seriomap", output_serio_table, 1 },
921 { "modules.alias", output_aliases, 0 },
922 { "modules.alias.bin", output_aliases_bin, 0 },
923 { "modules.symbols", output_symbols, 0 },
924 { "modules.symbols.bin", output_symbols_bin, 0 }
927 /* If we can't figure it out, it's safe to say "true". */
928 static int any_modules_newer(const char *dirname, time_t mtime)
930 DIR *dir;
931 struct dirent *dirent;
933 dir = opendir(dirname);
934 if (!dir)
935 return 1;
937 while ((dirent = readdir(dir)) != NULL) {
938 struct stat st;
939 char file[strlen(dirname) + 1 + strlen(dirent->d_name) + 1];
941 if (streq(dirent->d_name, ".") || streq(dirent->d_name, ".."))
942 continue;
944 sprintf(file, "%s/%s", dirname, dirent->d_name);
945 if (lstat(file, &st) != 0)
946 goto ret_true;
948 if (smells_like_module(dirent->d_name)) {
949 if (st.st_mtime > mtime)
950 goto ret_true;
951 } else if (S_ISDIR(st.st_mode)) {
952 if (any_modules_newer(file, mtime))
953 goto ret_true;
956 closedir(dir);
957 return 0;
959 ret_true:
960 closedir(dir);
961 return 1;
964 static int depfile_out_of_date(const char *dirname)
966 struct stat st;
967 char depfile[strlen(dirname) + 1 + strlen(depfiles[0].name) + 1];
969 sprintf(depfile, "%s/%s", dirname, depfiles[0].name);
971 if (stat(depfile, &st) != 0)
972 return 1;
974 return any_modules_newer(dirname, st.st_mtime);
977 static char *getline_wrapped(FILE *file, unsigned int *linenum)
979 int size = 256;
980 int i = 0;
981 char *buf = NOFAIL(malloc(size));
982 for(;;) {
983 int ch = getc_unlocked(file);
985 switch(ch) {
986 case EOF:
987 if (i == 0) {
988 free(buf);
989 return NULL;
991 /* else fall through */
993 case '\n':
994 if (linenum)
995 (*linenum)++;
996 if (i == size)
997 buf = NOFAIL(realloc(buf, size + 1));
998 buf[i] = '\0';
999 return buf;
1001 case '\\':
1002 ch = getc_unlocked(file);
1004 if (ch == '\n') {
1005 if (linenum)
1006 (*linenum)++;
1007 continue;
1009 /* else fall through */
1011 default:
1012 buf[i++] = ch;
1014 if (i == size) {
1015 size *= 2;
1016 buf = NOFAIL(realloc(buf, size));
1023 static char *strsep_skipspace(char **string, char *delim)
1025 if (!*string)
1026 return NULL;
1027 *string += strspn(*string, delim);
1028 return strsep(string, delim);
1031 static struct module_search *add_search(const char *search_path,
1032 struct module_search *search)
1035 struct module_search *new;
1037 new = NOFAIL(malloc(sizeof(*new)));
1038 new->search_path = NOFAIL(strdup(search_path));
1039 new->next = search;
1041 return new;
1045 static struct module_overrides *add_override(const char *modfile,
1046 struct module_overrides *overrides)
1049 struct module_overrides *new;
1051 new = NOFAIL(malloc(sizeof(*new)));
1052 new->modfile = NOFAIL(strdup(modfile));
1053 new->next = overrides;
1055 return new;
1059 /* Recursion */
1060 static int read_config(const char *filename,
1061 const char *basedir,
1062 struct module_search **search,
1063 struct module_overrides **overrides);
1065 static int read_config_file(const char *filename,
1066 const char *basedir,
1067 struct module_search **search,
1068 struct module_overrides **overrides)
1070 char *line;
1071 unsigned int linenum = 0;
1072 FILE *cfile;
1074 cfile = fopen(filename, "r");
1075 if (!cfile) {
1076 if (errno != ENOENT)
1077 fatal("could not open '%s', reason: %s\n", filename,
1078 strerror(errno));
1079 return 0;
1082 while ((line = getline_wrapped(cfile, &linenum)) != NULL) {
1083 char *ptr = line;
1084 char *cmd, *modname;
1086 cmd = strsep_skipspace(&ptr, "\t ");
1088 if (cmd == NULL || cmd[0] == '#' || cmd[0] == '\0') {
1089 free(line);
1090 continue;
1093 if (strcmp(cmd, "search") == 0) {
1094 char *search_path;
1096 while ((search_path = strsep_skipspace(&ptr, "\t ")))
1097 *search = add_search(search_path, *search);
1098 } else if (strcmp(cmd, "override") == 0) {
1099 char *pathname = NULL, *version, *subdir;
1100 modname = strsep_skipspace(&ptr, "\t ");
1101 version = strsep_skipspace(&ptr, "\t ");
1102 subdir = strsep_skipspace(&ptr, "\t ");
1104 pathname = NOFAIL(malloc(strlen(basedir)
1105 + strlen(MODULE_DIR)
1106 + strlen(version)
1107 + strlen(subdir)
1108 + strlen(modname)
1109 + strlen(".ko")
1110 + 3));
1111 sprintf(pathname, "%s%s%s/%s/%s.ko", basedir,
1112 MODULE_DIR, version, subdir, modname);
1114 *overrides = add_override(pathname, *overrides);
1115 } else if (strcmp(cmd, "include") == 0) {
1116 char *newfilename;
1118 newfilename = strsep_skipspace(&ptr, "\t ");
1119 if (!newfilename)
1120 grammar(cmd, filename, linenum);
1121 else {
1122 if (!read_config(newfilename, basedir,
1123 search, overrides))
1124 warn("Failed to open included"
1125 " config file %s: %s\n",
1126 newfilename, strerror(errno));
1129 } else if (strcmp(cmd, "make_map_files") == 0) {
1130 char *option;
1132 option = strsep_skipspace(&ptr, "\t ");
1133 if (!option)
1134 grammar(cmd, filename, linenum);
1135 else {
1136 if (0 == strncmp(option, "yes", 3))
1137 make_map_files = 1;
1138 else if (0 == strncmp(option, "no", 2))
1139 make_map_files = 0;
1140 else
1141 grammar(cmd, filename, linenum);
1143 } else
1144 grammar(cmd, filename, linenum);
1146 free(line);
1148 fclose(cfile);
1149 return 1;
1152 /* Simple format, ignore lines starting with #, one command per line.
1153 Returns true or false. */
1154 static int read_config(const char *filename,
1155 const char *basedir,
1156 struct module_search **search,
1157 struct module_overrides **overrides)
1159 DIR *dir;
1160 int ret = 0;
1162 dir = opendir(filename);
1163 if (dir) {
1164 struct dirent *i;
1165 while ((i = readdir(dir)) != NULL) {
1166 if (!streq(i->d_name,".") && !streq(i->d_name,"..")) {
1167 char sub[strlen(filename) + 1
1168 + strlen(i->d_name) + 1];
1170 sprintf(sub, "%s/%s", filename, i->d_name);
1171 if (!read_config(sub, basedir, search,
1172 overrides))
1173 warn("Failed to open"
1174 " config file %s: %s\n",
1175 sub, strerror(errno));
1178 closedir(dir);
1179 ret = 1;
1180 } else {
1181 if (read_config_file(filename, basedir, search, overrides))
1182 ret = 1;
1185 return ret;
1188 static const char *default_configs[] =
1190 "/etc/depmod.conf",
1191 "/etc/depmod.d",
1194 static void read_toplevel_config(const char *filename,
1195 const char *basedir,
1196 struct module_search **search,
1197 struct module_overrides **overrides)
1199 unsigned int i;
1201 if (filename) {
1202 if (!read_config(filename, basedir, search, overrides))
1203 fatal("Failed to open config file %s: %s\n",
1204 filename, strerror(errno));
1205 return;
1208 /* Try defaults. */
1209 for (i = 0; i < ARRAY_SIZE(default_configs); i++) {
1210 read_config(default_configs[i], basedir, search, overrides);
1214 /* Local to main, but not freed on exit. Keep valgrind quiet. */
1215 struct module *list = NULL;
1216 struct module_search *search = NULL;
1217 struct module_overrides *overrides = NULL;
1219 int main(int argc, char *argv[])
1221 int opt, all = 0, maybe_all = 0, doing_stdout = 0;
1222 char *basedir = "", *dirname, *version, *badopt = NULL,
1223 *system_map = NULL;
1224 int i;
1225 const char *config = NULL;
1227 /* Don't print out any errors just yet, we might want to exec
1228 backwards compat version. */
1229 opterr = 0;
1230 while ((opt = getopt_long(argc, argv, "ab:ArehnqruvVF:C:wm", options, NULL))
1231 != -1) {
1232 switch (opt) {
1233 case 'a':
1234 all = 1;
1235 break;
1236 case 'b':
1237 basedir = optarg;
1238 skipchars = strlen(basedir);
1239 break;
1240 case 'A':
1241 maybe_all = 1;
1242 break;
1243 case 'F':
1244 system_map = optarg;
1245 break;
1246 case 'e':
1247 print_unknown = 1;
1248 break;
1249 case 'v':
1250 verbose = 1;
1251 break;
1252 case 'u':
1253 case 'q':
1254 case 'r':
1255 break;
1256 case 'C':
1257 config = optarg;
1258 break;
1259 case 'h':
1260 print_usage(argv[0]);
1261 exit(0);
1262 break;
1263 case 'n':
1264 doing_stdout = 1;
1265 break;
1266 case 'V':
1267 printf("%s %s\n", PACKAGE, VERSION);
1268 exit(0);
1269 case 'w':
1270 warn_dups = 1;
1271 break;
1272 case 'm':
1273 force_map_files = 1;
1274 break;
1275 default:
1276 badopt = argv[optind-1];
1280 /* We can't print unknowns without a System.map */
1281 if (!system_map)
1282 print_unknown = 0;
1283 else
1284 load_system_map(system_map);
1286 /* They can specify the version naked on the command line */
1287 if (optind < argc && is_version_number(argv[optind])) {
1288 version = NOFAIL(strdup(argv[optind]));
1289 optind++;
1290 } else {
1291 struct utsname buf;
1292 uname(&buf);
1293 version = NOFAIL(strdup(buf.release));
1296 /* Run old version if required. */
1297 if (old_module_version(version))
1298 exec_old_depmod(argv);
1300 if (badopt) {
1301 fprintf(stderr, "%s: malformed/unrecognized option '%s'\n",
1302 argv[0], badopt);
1303 print_usage(argv[0]);
1304 exit(1);
1307 /* Depmod -a by default if no names. */
1308 if (optind == argc)
1309 all = 1;
1311 dirname = NOFAIL(malloc(strlen(basedir)
1312 + strlen(MODULE_DIR)
1313 + strlen(version) + 1));
1314 sprintf(dirname, "%s%s%s", basedir, MODULE_DIR, version);
1316 if (maybe_all) {
1317 if (!doing_stdout && !depfile_out_of_date(dirname))
1318 exit(0);
1319 all = 1;
1322 read_toplevel_config(config, basedir, &search, &overrides);
1324 /* For backward compatibility add "updates" to the head of the search
1325 * list here. But only if there was no "search" option specified.
1328 if (!search)
1329 search = add_search("updates",search);
1331 if (!all) {
1332 /* Do command line args. */
1333 for (opt = optind; opt < argc; opt++) {
1334 struct module *new = grab_module(NULL, argv[opt]);
1335 if (new) {
1336 new->next = list;
1337 list = new;
1340 } else {
1341 list = grab_basedir(dirname,search,overrides);
1343 list = parse_modules(list);
1345 for (i = 0; i < sizeof(depfiles)/sizeof(depfiles[0]); i++) {
1346 FILE *out;
1347 struct depfile *d = &depfiles[i];
1348 char depname[strlen(dirname) + 1 + strlen(d->name) + 1];
1349 char tmpname[strlen(dirname) + 1 + strlen(d->name) +
1350 strlen(".temp") + 1];
1352 if (d->map_file && !make_map_files && !force_map_files)
1353 continue;
1355 sprintf(depname, "%s/%s", dirname, d->name);
1356 sprintf(tmpname, "%s/%s.temp", dirname, d->name);
1357 if (!doing_stdout) {
1358 out = fopen(tmpname, "w");
1359 if (!out)
1360 fatal("Could not open %s for writing: %s\n",
1361 tmpname, strerror(errno));
1362 } else {
1363 out = stdout;
1364 if (ends_in(depname, ".bin"))
1365 continue;
1367 d->func(list, out, dirname);
1368 if (!doing_stdout) {
1369 fclose(out);
1370 if (rename(tmpname, depname) < 0)
1371 fatal("Could not rename %s into %s: %s\n",
1372 tmpname, depname, strerror(errno));
1376 free(dirname);
1377 free(version);
1379 return 0;