bump release to v3.6-pre5
[mit.git] / depmod.c
blob8c8997ecad925c07426f3b10560552b84532c3dc
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"
30 #include "config_filter.h"
32 #include "testing.h"
34 #ifndef MODULE_DIR
35 #define MODULE_DIR "/lib/modules/"
36 #endif
38 #ifndef MODULE_BUILTIN_KEY
39 #define MODULE_BUILTIN_KEY "built-in"
40 #endif
42 struct module_overrides
44 /* Next override */
45 struct module_overrides *next;
47 /* overridden module */
48 char *modfile;
51 struct module_search
53 /* Next search */
54 struct module_search *next;
56 /* search path */
57 char *search_path;
60 static int verbose;
61 static unsigned int skipchars;
62 static unsigned int make_map_files = 1; /* default to on */
63 static unsigned int force_map_files = 0; /* default to on */
65 #define SYMBOL_HASH_SIZE 1024
66 struct symbol
68 struct symbol *next;
69 struct module *owner;
70 char name[0];
73 static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
75 /* This is based on the hash agorithm from gdbm, via tdb */
76 static inline unsigned int tdb_hash(const char *name)
78 unsigned value; /* Used to compute the hash value. */
79 unsigned i; /* Used to cycle through random values. */
81 /* Set the initial value from the key size. */
82 for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
83 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
85 return (1103515243 * value + 12345);
88 void add_symbol(const char *name, struct module *owner)
90 unsigned int hash;
91 struct symbol *new = NOFAIL(malloc(sizeof *new + strlen(name) + 1));
93 new->owner = owner;
94 strcpy(new->name, name);
96 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
97 new->next = symbolhash[hash];
98 symbolhash[hash] = new;
101 static int print_unknown;
103 struct module *find_symbol(const char *name, const char *modname, int weak)
105 struct symbol *s;
107 /* For our purposes, .foo matches foo. PPC64 needs this. */
108 if (name[0] == '.')
109 name++;
111 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) {
112 if (streq(s->name, name))
113 return s->owner;
116 if (print_unknown && !weak)
117 warn("%s needs unknown symbol %s\n", modname, name);
119 return NULL;
122 void add_dep(struct module *mod, struct module *depends_on)
124 unsigned int i;
126 for (i = 0; i < mod->num_deps; i++)
127 if (mod->deps[i] == depends_on)
128 return;
130 mod->deps = NOFAIL(realloc(mod->deps, sizeof(mod->deps[0])*(mod->num_deps+1)));
131 mod->deps[mod->num_deps++] = depends_on;
134 static void load_system_map(const char *filename)
136 FILE *system_map;
137 char line[10240];
138 const char ksymstr[] = "__ksymtab_";
139 const int ksymstr_len = strlen(ksymstr);
141 system_map = fopen(filename, "r");
142 if (!system_map)
143 fatal("Could not open '%s': %s\n", filename, strerror(errno));
145 /* eg. c0294200 R __ksymtab_devfs_alloc_devnum */
146 while (fgets(line, sizeof(line)-1, system_map)) {
147 char *ptr;
149 /* Snip \n */
150 ptr = strchr(line, '\n');
151 if (ptr)
152 *ptr = '\0';
154 ptr = strchr(line, ' ');
155 if (!ptr || !(ptr = strchr(ptr + 1, ' ')))
156 continue;
158 /* Covers gpl-only and normal symbols. */
159 if (strncmp(ptr+1, ksymstr, ksymstr_len) == 0)
160 add_symbol(ptr+1+ksymstr_len, NULL);
163 fclose(system_map);
165 /* __this_module is magic inserted by kernel loader. */
166 add_symbol("__this_module", NULL);
167 /* On S390, this is faked up too */
168 add_symbol("_GLOBAL_OFFSET_TABLE_", NULL);
171 static struct option options[] = { { "all", 0, NULL, 'a' },
172 { "quick", 0, NULL, 'A' },
173 { "basedir", 1, NULL, 'b' },
174 { "errsyms", 0, NULL, 'e' },
175 { "filesyms", 1, NULL, 'F' },
176 { "help", 0, NULL, 'h' },
177 { "show", 0, NULL, 'n' },
178 { "dry-run", 0, NULL, 'n' },
179 { "quiet", 0, NULL, 'q' },
180 { "root", 0, NULL, 'r' },
181 { "unresolved-error", 0, NULL, 'u' },
182 { "verbose", 0, NULL, 'v' },
183 { "version", 0, NULL, 'V' },
184 { "config", 1, NULL, 'C' },
185 { "warn", 1, NULL, 'w' },
186 { "map", 0, NULL, 'm' },
187 { NULL, 0, NULL, 0 } };
189 /* Version number or module name? Don't assume extension. */
190 static int is_version_number(const char *version)
192 unsigned int dummy;
194 return (sscanf(version, "%u.%u.%u", &dummy, &dummy, &dummy) == 3);
197 static int old_module_version(const char *version)
199 /* Expect three part version. */
200 unsigned int major, sub, minor;
202 sscanf(version, "%u.%u.%u", &major, &sub, &minor);
204 if (major > 2) return 0;
205 if (major < 2) return 1;
207 /* 2.x */
208 if (sub > 5) return 0;
209 if (sub < 5) return 1;
211 /* 2.5.x */
212 if (minor >= 48) return 0;
213 return 1;
216 static void exec_old_depmod(char *argv[])
218 char *sep;
219 char pathname[strlen(argv[0])+1];
220 char oldname[strlen("depmod") + strlen(argv[0]) + sizeof(".old")];
222 memset(pathname, 0, strlen(argv[0])+1);
223 sep = strrchr(argv[0], '/');
224 if (sep)
225 memcpy(pathname, argv[0], sep - argv[0]+1);
226 sprintf(oldname, "%s%s.old", pathname, "depmod");
228 /* Recursion detection: we need an env var since we can't
229 change argv[0] (as older modutils uses it to determine
230 behavior). */
231 if (getenv("MODULE_RECURSE"))
232 return;
233 setenv("MODULE_RECURSE", "y", 0);
235 execvp(oldname, argv);
236 fprintf(stderr,
237 "Version requires old depmod, but couldn't run %s: %s\n",
238 oldname, strerror(errno));
239 exit(2);
242 static void grammar(const char *cmd, const char *filename, unsigned int line)
244 warn("%s line %u: ignoring bad line starting with '%s'\n",
245 filename, line, cmd);
249 static void print_usage(const char *name)
251 fprintf(stderr,
252 "%s " VERSION " -- part of " PACKAGE "\n"
253 "%s -[aA] [-n -e -v -q -V -r -u -w -m]\n"
254 " [-b basedirectory] [forced_version]\n"
255 "depmod [-n -e -v -q -r -u -w] [-F kernelsyms] module1.ko module2.ko ...\n"
256 "If no arguments (except options) are given, \"depmod -a\" is assumed\n"
257 "\n"
258 "depmod will output a dependancy list suitable for the modprobe utility.\n"
259 "\n"
260 "\n"
261 "Options:\n"
262 "\t-a, --all Probe all modules\n"
263 "\t-A, --quick Only does the work if there's a new module\n"
264 "\t-e, --errsyms Report not supplied symbols\n"
265 "\t-m, --map Create the legacy map files\n"
266 "\t-n, --show Write the dependency file on stdout only\n"
267 "\t-V, --version Print the release version\n"
268 "\t-v, --verbose Enable verbose mode\n"
269 "\t-w, --warn Warn on duplicates\n"
270 "\t-h, --help Print this usage message\n"
271 "\n"
272 "The following options are useful for people managing distributions:\n"
273 "\t-b basedirectory\n"
274 "\t --basedir basedirectory Use an image of a module tree.\n"
275 "\t-F kernelsyms\n"
276 "\t --filesyms kernelsyms Use the file instead of the\n"
277 "\t current kernel symbols.\n",
278 "depmod", "depmod");
281 static int ends_in(const char *name, const char *ext)
283 unsigned int namelen, extlen;
285 /* Grab lengths */
286 namelen = strlen(name);
287 extlen = strlen(ext);
289 if (namelen < extlen) return 0;
291 if (streq(name + namelen - extlen, ext))
292 return 1;
293 return 0;
296 /* "\177ELF" <byte> where byte = 001 for 32-bit, 002 for 64 */
297 int needconv(const char *elfhdr)
299 union { short s; char c[2]; } endian_test;
301 endian_test.s = 1;
302 if (endian_test.c[1] == 1) return elfhdr[EI_DATA] != ELFDATA2MSB;
303 if (endian_test.c[0] == 1) return elfhdr[EI_DATA] != ELFDATA2LSB;
304 else
305 abort();
308 static char *my_basename(const char *name)
310 const char *base = strrchr(name, '/');
311 if (base) return (char *)base + 1;
312 return (char *)name;
315 static struct module *grab_module(const char *dirname, const char *filename)
317 struct module *new;
319 new = NOFAIL(malloc(sizeof(*new)
320 + strlen(dirname?:"") + 1 + strlen(filename) + 1));
321 if (dirname)
322 sprintf(new->pathname, "%s/%s", dirname, filename);
323 else
324 strcpy(new->pathname, filename);
325 new->basename = my_basename(new->pathname);
327 INIT_LIST_HEAD(&new->dep_list);
329 new->data = grab_file(new->pathname, &new->len);
330 if (!new->data) {
331 warn("Can't read module %s: %s\n",
332 new->pathname, strerror(errno));
333 goto fail_data;
336 /* "\177ELF" <byte> where byte = 001 for 32-bit, 002 for 64 */
337 if (memcmp(new->data, ELFMAG, SELFMAG) != 0) {
338 warn("Module %s is not an elf object\n", new->pathname);
339 goto fail;
342 switch (((char *)new->data)[EI_CLASS]) {
343 case ELFCLASS32:
344 new->ops = &mod_ops32;
345 break;
346 case ELFCLASS64:
347 new->ops = &mod_ops64;
348 break;
349 default:
350 warn("Module %s has elf unknown identifier %i\n",
351 new->pathname, ((char *)new->data)[EI_CLASS]);
352 goto fail;
354 new->conv = needconv(new->data);
355 return new;
357 fail:
358 release_file(new->data, new->len);
359 fail_data:
360 free(new);
361 return NULL;
364 struct module_traverse
366 struct module_traverse *prev;
367 struct module *mod;
370 static int in_loop(struct module *mod, const struct module_traverse *traverse)
372 const struct module_traverse *i;
374 for (i = traverse; i; i = i->prev) {
375 if (i->mod == mod)
376 return 1;
378 return 0;
381 /* Assume we are doing all the modules, so only report each loop once. */
382 static void report_loop(const struct module *mod,
383 const struct module_traverse *traverse)
385 const struct module_traverse *i;
387 /* Check that start is least alphabetically. eg. a depends
388 on b depends on a will get reported for a, not b. */
389 for (i = traverse->prev; i->prev; i = i->prev) {
390 if (strcmp(mod->pathname, i->mod->pathname) > 0)
391 return;
394 /* Is start in the loop? If not, don't report now. eg. a
395 depends on b which depends on c which depends on b. Don't
396 report when generating depends for a. */
397 if (mod != i->mod)
398 return;
400 warn("Loop detected: %s ", mod->pathname);
401 for (i = traverse->prev; i->prev; i = i->prev)
402 fprintf(stderr, "needs %s ", i->mod->basename);
403 fprintf(stderr, "which needs %s again!\n", i->mod->basename);
406 /* This is damn slow, but loops actually happen, and we don't want to
407 just exit() and leave the user without any modules. */
408 static int has_dep_loop(struct module *module, struct module_traverse *prev)
410 unsigned int i;
411 struct module_traverse traverse = { .prev = prev, .mod = module };
413 if (in_loop(module, prev)) {
414 report_loop(module, &traverse);
415 return 1;
418 for (i = 0; i < module->num_deps; i++)
419 if (has_dep_loop(module->deps[i], &traverse))
420 return 1;
421 return 0;
424 /* Uniquifies and orders a dependency list. */
425 static void order_dep_list(struct module *start, struct module *mod)
427 unsigned int i;
429 for (i = 0; i < mod->num_deps; i++) {
430 /* If it was previously depended on, move it to the
431 tail. ie. if a needs b and c, and c needs b, we
432 must order b after c. */
433 list_del(&mod->deps[i]->dep_list);
434 list_add_tail(&mod->deps[i]->dep_list, &start->dep_list);
435 order_dep_list(start, mod->deps[i]);
439 static struct module *deleted = NULL;
441 static void del_module(struct module **modules, struct module *delme)
443 struct module **i;
445 /* Find pointer to it. */
446 if (modules) {
447 for (i = modules; *i != delme; i = &(*i)->next);
449 *i = delme->next;
452 /* Save on a list to quiet valgrind.
453 Can't free - other modules may depend on them */
454 delme->next = deleted;
455 deleted = delme;
458 /* Convert filename to the module name. Works if filename == modname, too. */
459 static void filename2modname(char *modname, const char *filename)
461 const char *afterslash;
462 unsigned int i;
464 afterslash = strrchr(filename, '/');
465 if (!afterslash)
466 afterslash = filename;
467 else
468 afterslash++;
470 /* Convert to underscores, stop at first . */
471 for (i = 0; afterslash[i] && afterslash[i] != '.'; i++) {
472 if (afterslash[i] == '-')
473 modname[i] = '_';
474 else
475 modname[i] = afterslash[i];
477 modname[i] = '\0';
480 static void output_deps(struct module *modules,
481 FILE *out, char *dirname)
483 struct module *i;
485 for (i = modules; i; i = i->next) {
486 struct list_head *j, *tmp;
487 order_dep_list(i, i);
489 fprintf(out, "%s:", i->pathname + strlen(dirname)+1);
490 list_for_each_safe(j, tmp, &i->dep_list) {
491 struct module *dep
492 = list_entry(j, struct module, dep_list);
493 fprintf(out, " %s", dep->pathname + strlen(dirname)+1);
494 list_del_init(j);
496 fprintf(out, "\n");
500 /* warn whenever duplicate module aliases, deps, or symbols are found. */
501 int warn_dups = 0;
503 static void output_deps_bin(struct module *modules,
504 FILE *out, char *dirname)
506 struct module *i;
507 struct index_node *index;
508 char *line;
509 char *p;
511 index = index_create();
513 for (i = modules; i; i = i->next) {
514 struct list_head *j, *tmp;
515 char modname[strlen(i->pathname)+1];
517 order_dep_list(i, i);
519 filename2modname(modname, i->pathname + strlen(dirname)+1);
520 nofail_asprintf(&line, "%s %s:", modname, i->pathname + strlen(dirname)+1);
521 p = line;
522 list_for_each_safe(j, tmp, &i->dep_list) {
523 struct module *dep
524 = list_entry(j, struct module, dep_list);
525 nofail_asprintf(&line, "%s %s", p, dep->pathname + strlen(dirname)+1);
526 free(p);
527 p = line;
528 list_del_init(j);
530 if (index_insert(index, line) && warn_dups)
531 warn("duplicate module deps:\n%s\n",line);
532 free(line);
535 index_write(index, out);
536 index_destroy(index);
540 static int smells_like_module(const char *name)
542 return ends_in(name,".ko") || ends_in(name, ".ko.gz");
545 typedef struct module *(*do_module_t)(const char *dirname,
546 const char *filename,
547 struct module *next,
548 struct module_search *search,
549 struct module_overrides *overrides);
551 static int is_higher_priority(const char *newpath, const char *oldpath,
552 struct module_search *search,
553 struct module_overrides *overrides)
555 char *p, *q, *s;
556 struct module_search *tmp;
557 struct module_overrides *ovtmp;
558 int i = 0;
559 int prio_builtin = -1;
560 int prio_new = -1;
561 int prio_old = -1;
563 /* The names already match, now we check for wildcard overrides and other
564 * high priority overrides we added to the config.
566 for (ovtmp=overrides;ovtmp!=NULL;ovtmp=ovtmp->next) {
568 p = strstr(ovtmp->modfile,newpath);
569 if (p)
570 return 1;
572 q = strstr(ovtmp->modfile,"*");
573 if (q) {
574 p = strstr(newpath, q+1);
575 if (p)
576 return 1;
578 p = strstr(oldpath, q+1);
579 if (p)
580 return 0;
583 p = strstr(ovtmp->modfile,oldpath);
584 if (p)
585 return 0;
588 for (i=0,tmp=search;tmp!=NULL;tmp=tmp->next,i++) {
590 s = NOFAIL(malloc(strlen(tmp->search_path)+2));
591 s[0] = '/';
592 strncpy(s+1,tmp->search_path, strlen(tmp->search_path)+1);
594 if (0 == strncmp(tmp->search_path,MODULE_BUILTIN_KEY,
595 strlen(MODULE_BUILTIN_KEY)))
596 prio_builtin = i;
598 p = strstr(newpath,s);
599 if ((p) && ((p[strlen(s)] == '/')
600 || (p[strlen(s)] == '\0')))
601 prio_new = i;
603 p = strstr(oldpath,s);
604 if ((p) && ((p[strlen(s)] == '/')
605 || (p[strlen(s)] == '\0')))
606 prio_old = i;
608 free(s);
612 if (prio_new < 0)
613 prio_new = prio_builtin;
615 if (prio_old < 0)
616 prio_old = prio_builtin;
618 return prio_new > prio_old;
623 static struct module *do_module(const char *dirname,
624 const char *filename,
625 struct module *list,
626 struct module_search *search,
627 struct module_overrides *overrides)
629 struct module *new, **i;
631 new = grab_module(dirname, filename);
632 if (!new)
633 return list;
635 /* Check if module is already in the list. */
636 for (i = &list; *i; i = &(*i)->next) {
638 if (streq((*i)->basename, filename)) {
639 char newpath[strlen(dirname) + strlen("/")
640 + strlen(filename) + 1];
642 sprintf(newpath, "%s/%s", dirname, filename);
644 if (is_higher_priority(newpath, (*i)->pathname,search,
645 overrides)) {
646 del_module(i, *i);
648 new->next = *i;
649 *i = new;
650 } else
651 del_module(NULL, new);
653 return list;
657 /* Not in the list already. Just prepend. */
658 new->next = list;
659 return new;
662 static struct module *grab_dir(const char *dirname,
663 DIR *dir,
664 struct module *next,
665 do_module_t do_mod,
666 struct module_search *search,
667 struct module_overrides *overrides)
669 struct dirent *dirent;
671 while ((dirent = readdir(dir)) != NULL) {
672 if (smells_like_module(dirent->d_name))
673 next = do_mod(dirname, dirent->d_name, next,
674 search, overrides);
675 else if (!streq(dirent->d_name, ".")
676 && !streq(dirent->d_name, "..")
677 && !streq(dirent->d_name, "source")
678 && !streq(dirent->d_name, "build")) {
680 DIR *sub;
681 char subdir[strlen(dirname) + 1
682 + strlen(dirent->d_name) + 1];
683 sprintf(subdir, "%s/%s", dirname, dirent->d_name);
684 sub = opendir(subdir);
685 if (sub) {
686 next = grab_dir(subdir, sub, next, do_mod,
687 search, overrides);
688 closedir(sub);
692 return next;
695 static struct module *grab_basedir(const char *dirname,
696 struct module_search *search,
697 struct module_overrides *overrides)
699 DIR *dir;
700 struct module *list;
702 dir = opendir(dirname);
703 if (!dir) {
704 warn("Couldn't open directory %s: %s\n",
705 dirname, strerror(errno));
706 return NULL;
708 list = grab_dir(dirname, dir, NULL, do_module, search, overrides);
709 closedir(dir);
711 return list;
714 static struct module *sort_modules(const char *dirname, struct module *list)
716 struct module *tlist = NULL, **tpos = &tlist;
717 FILE *modorder;
718 int dir_len = strlen(dirname) + 1;
719 char file_name[dir_len + strlen("modules.order") + 1];
720 char line[10240];
722 sprintf(file_name, "%s/%s", dirname, "modules.order");
724 modorder = fopen(file_name, "r");
725 if (!modorder) {
726 /* Older kernels don't generate modules.order. Just
727 return if the file doesn't exist. */
728 if (errno == ENOENT)
729 return list;
730 fatal("Could not open '%s': %s\n", file_name, strerror(errno));
733 sprintf(line, "%s/", dirname);
735 /* move modules listed in modorder file to tlist in order */
736 while (fgets(line, sizeof(line), modorder)) {
737 struct module **pos, *mod;
738 int len = strlen(line);
740 if (line[len - 1] == '\n')
741 line[len - 1] = '\0';
743 for (pos = &list; (mod = *pos); pos = &(*pos)->next) {
744 if (strcmp(line, mod->pathname + dir_len) == 0) {
745 *pos = mod->next;
746 mod->next = NULL;
747 *tpos = mod;
748 tpos = &mod->next;
749 break;
754 /* append the rest */
755 *tpos = list;
757 fclose(modorder);
759 return tlist;
762 static struct module *parse_modules(struct module *list)
764 struct module *i;
766 for (i = list; i; i = i->next) {
767 i->ops->load_symbols(i);
768 i->ops->fetch_tables(i);
771 for (i = list; i; i = i->next)
772 i->ops->calculate_deps(i, verbose);
774 /* Strip out modules with dependency loops. */
775 again:
776 for (i = list; i; i = i->next) {
777 if (has_dep_loop(i, NULL)) {
778 warn("Module %s ignored, due to loop\n",
779 i->pathname + skipchars);
780 del_module(&list, i);
781 goto again;
785 return list;
788 /* Simply dump hash table. */
789 static void output_symbols(struct module *unused, FILE *out, char *dirname)
791 unsigned int i;
793 fprintf(out, "# Aliases for symbols, used by symbol_request().\n");
794 for (i = 0; i < SYMBOL_HASH_SIZE; i++) {
795 struct symbol *s;
797 for (s = symbolhash[i]; s; s = s->next) {
798 if (s->owner) {
799 char modname[strlen(s->owner->pathname)+1];
800 filename2modname(modname, s->owner->pathname);
801 fprintf(out, "alias symbol:%s %s\n",
802 s->name, modname);
808 static void output_symbols_bin(struct module *unused, FILE *out, char *dirname)
810 struct index_node *index;
811 unsigned int i;
812 char *line;
814 index = index_create();
816 for (i = 0; i < SYMBOL_HASH_SIZE; i++) {
817 struct symbol *s;
819 for (s = symbolhash[i]; s; s = s->next) {
820 if (s->owner) {
821 char modname[strlen(s->owner->pathname)+1];
822 filename2modname(modname, s->owner->pathname);
823 nofail_asprintf(&line, "symbol:%s %s",
824 s->name, modname);
825 if (index_insert(index, line) && warn_dups)
826 warn("duplicate module syms:\n%s\n",
827 line);
828 free(line);
833 index_write(index, out);
834 index_destroy(index);
837 static const char *next_string(const char *string, unsigned long *secsize)
839 /* Skip non-zero chars */
840 while (string[0]) {
841 string++;
842 if ((*secsize)-- <= 1)
843 return NULL;
846 /* Skip any zero padding. */
847 while (!string[0]) {
848 string++;
849 if ((*secsize)-- <= 1)
850 return NULL;
852 return string;
855 /* Careful! Don't munge - in [ ] as per Debian Bug#350915 */
856 static char *underscores(char *string)
858 unsigned int i;
860 if (!string)
861 return NULL;
863 for (i = 0; string[i]; i++) {
864 switch (string[i]) {
865 case '-':
866 string[i] = '_';
867 break;
869 case '[':
870 i += strcspn(&string[i], "]");
871 if (!string[i])
872 warn("Unmatched bracket in %s\n", string);
875 return string;
878 static void output_aliases(struct module *modules, FILE *out, char *dirname)
880 struct module *i;
881 const char *p;
882 unsigned long size;
884 fprintf(out, "# Aliases extracted from modules themselves.\n");
885 for (i = modules; i; i = i->next) {
886 char modname[strlen(i->pathname)+1];
888 filename2modname(modname, i->pathname);
890 /* Grab from old-style .modalias section. */
891 for (p = i->ops->get_aliases(i, &size);
893 p = next_string(p, &size))
894 fprintf(out, "alias %s %s\n", p, modname);
896 /* Grab form new-style .modinfo section. */
897 for (p = i->ops->get_modinfo(i, &size);
899 p = next_string(p, &size)) {
900 if (strncmp(p, "alias=", strlen("alias=")) == 0)
901 fprintf(out, "alias %s %s\n",
902 p + strlen("alias="), modname);
907 static void output_aliases_bin(struct module *modules, FILE *out, char *dirname)
909 struct module *i;
910 const char *p;
911 char *line;
912 unsigned long size;
913 struct index_node *index;
915 index = index_create();
917 for (i = modules; i; i = i->next) {
918 char modname[strlen(i->pathname)+1];
920 filename2modname(modname, i->pathname);
922 /* Grab from old-style .modalias section. */
923 for (p = i->ops->get_aliases(i, &size);
925 p = next_string(p, &size)) {
926 nofail_asprintf(&line, "%s %s", p, modname);
927 underscores(line);
928 if (index_insert(index, line) && warn_dups)
929 warn("duplicate module alias:\n%s\n", line);
930 free(line);
933 /* Grab from new-style .modinfo section. */
934 for (p = i->ops->get_modinfo(i, &size);
936 p = next_string(p, &size)) {
937 if (strncmp(p, "alias=", strlen("alias=")) == 0) {
938 nofail_asprintf(&line, "%s %s",
939 p + strlen("alias="), modname);
940 underscores(line);
941 if (index_insert(index, line) && warn_dups)
942 warn("duplicate module alias:\n%s\n",
943 line);
944 free(line);
949 index_write(index, out);
950 index_destroy(index);
953 struct depfile {
954 char *name;
955 void (*func)(struct module *, FILE *, char *dirname);
956 int map_file;
959 static struct depfile depfiles[] = {
960 { "modules.dep", output_deps, 0 }, /* This is what we check for '-A'. */
961 { "modules.dep.bin", output_deps_bin, 0 },
962 { "modules.pcimap", output_pci_table, 1 },
963 { "modules.usbmap", output_usb_table, 1 },
964 { "modules.ccwmap", output_ccw_table, 1 },
965 { "modules.ieee1394map", output_ieee1394_table, 1 },
966 { "modules.isapnpmap", output_isapnp_table, 1 },
967 { "modules.inputmap", output_input_table, 1 },
968 { "modules.ofmap", output_of_table, 1 },
969 { "modules.seriomap", output_serio_table, 1 },
970 { "modules.alias", output_aliases, 0 },
971 { "modules.alias.bin", output_aliases_bin, 0 },
972 { "modules.symbols", output_symbols, 0 },
973 { "modules.symbols.bin", output_symbols_bin, 0 }
976 /* If we can't figure it out, it's safe to say "true". */
977 static int any_modules_newer(const char *dirname, time_t mtime)
979 DIR *dir;
980 struct dirent *dirent;
982 dir = opendir(dirname);
983 if (!dir)
984 return 1;
986 while ((dirent = readdir(dir)) != NULL) {
987 struct stat st;
988 char file[strlen(dirname) + 1 + strlen(dirent->d_name) + 1];
990 if (streq(dirent->d_name, ".") || streq(dirent->d_name, ".."))
991 continue;
993 sprintf(file, "%s/%s", dirname, dirent->d_name);
994 if (lstat(file, &st) != 0)
995 goto ret_true;
997 if (smells_like_module(dirent->d_name)) {
998 if (st.st_mtime > mtime)
999 goto ret_true;
1000 } else if (S_ISDIR(st.st_mode)) {
1001 if (any_modules_newer(file, mtime))
1002 goto ret_true;
1005 closedir(dir);
1006 return 0;
1008 ret_true:
1009 closedir(dir);
1010 return 1;
1013 static int depfile_out_of_date(const char *dirname)
1015 struct stat st;
1016 char depfile[strlen(dirname) + 1 + strlen(depfiles[0].name) + 1];
1018 sprintf(depfile, "%s/%s", dirname, depfiles[0].name);
1020 if (stat(depfile, &st) != 0)
1021 return 1;
1023 return any_modules_newer(dirname, st.st_mtime);
1026 static char *getline_wrapped(FILE *file, unsigned int *linenum)
1028 int size = 256;
1029 int i = 0;
1030 char *buf = NOFAIL(malloc(size));
1031 for(;;) {
1032 int ch = getc_unlocked(file);
1034 switch(ch) {
1035 case EOF:
1036 if (i == 0) {
1037 free(buf);
1038 return NULL;
1040 /* else fall through */
1042 case '\n':
1043 if (linenum)
1044 (*linenum)++;
1045 if (i == size)
1046 buf = NOFAIL(realloc(buf, size + 1));
1047 buf[i] = '\0';
1048 return buf;
1050 case '\\':
1051 ch = getc_unlocked(file);
1053 if (ch == '\n') {
1054 if (linenum)
1055 (*linenum)++;
1056 continue;
1058 /* else fall through */
1060 default:
1061 buf[i++] = ch;
1063 if (i == size) {
1064 size *= 2;
1065 buf = NOFAIL(realloc(buf, size));
1072 static char *strsep_skipspace(char **string, char *delim)
1074 if (!*string)
1075 return NULL;
1076 *string += strspn(*string, delim);
1077 return strsep(string, delim);
1080 static struct module_search *add_search(const char *search_path,
1081 struct module_search *search)
1084 struct module_search *new;
1086 new = NOFAIL(malloc(sizeof(*new)));
1087 new->search_path = NOFAIL(strdup(search_path));
1088 new->next = search;
1090 return new;
1094 static struct module_overrides *add_override(const char *modfile,
1095 struct module_overrides *overrides)
1098 struct module_overrides *new;
1100 new = NOFAIL(malloc(sizeof(*new)));
1101 new->modfile = NOFAIL(strdup(modfile));
1102 new->next = overrides;
1104 return new;
1108 /* Recursion */
1109 static int read_config(const char *filename,
1110 const char *basedir,
1111 struct module_search **search,
1112 struct module_overrides **overrides);
1114 static int read_config_file(const char *filename,
1115 const char *basedir,
1116 struct module_search **search,
1117 struct module_overrides **overrides)
1119 char *line;
1120 unsigned int linenum = 0;
1121 FILE *cfile;
1123 cfile = fopen(filename, "r");
1124 if (!cfile) {
1125 if (errno != ENOENT)
1126 fatal("could not open '%s', reason: %s\n", filename,
1127 strerror(errno));
1128 return 0;
1131 while ((line = getline_wrapped(cfile, &linenum)) != NULL) {
1132 char *ptr = line;
1133 char *cmd, *modname;
1135 cmd = strsep_skipspace(&ptr, "\t ");
1137 if (cmd == NULL || cmd[0] == '#' || cmd[0] == '\0') {
1138 free(line);
1139 continue;
1142 if (strcmp(cmd, "search") == 0) {
1143 char *search_path;
1145 while ((search_path = strsep_skipspace(&ptr, "\t ")))
1146 *search = add_search(search_path, *search);
1147 } else if (strcmp(cmd, "override") == 0) {
1148 char *pathname = NULL, *version, *subdir;
1149 modname = strsep_skipspace(&ptr, "\t ");
1150 version = strsep_skipspace(&ptr, "\t ");
1151 subdir = strsep_skipspace(&ptr, "\t ");
1153 pathname = NOFAIL(malloc(strlen(basedir)
1154 + strlen(MODULE_DIR)
1155 + strlen(version)
1156 + strlen(subdir)
1157 + strlen(modname)
1158 + strlen(".ko")
1159 + 3));
1160 sprintf(pathname, "%s%s%s/%s/%s.ko", basedir,
1161 MODULE_DIR, version, subdir, modname);
1163 *overrides = add_override(pathname, *overrides);
1164 } else if (strcmp(cmd, "include") == 0) {
1165 char *newfilename;
1167 newfilename = strsep_skipspace(&ptr, "\t ");
1168 if (!newfilename)
1169 grammar(cmd, filename, linenum);
1170 else {
1171 if (!read_config(newfilename, basedir,
1172 search, overrides))
1173 warn("Failed to open included"
1174 " config file %s: %s\n",
1175 newfilename, strerror(errno));
1178 } else if (strcmp(cmd, "make_map_files") == 0) {
1179 char *option;
1181 option = strsep_skipspace(&ptr, "\t ");
1182 if (!option)
1183 grammar(cmd, filename, linenum);
1184 else {
1185 if (0 == strncmp(option, "yes", 3))
1186 make_map_files = 1;
1187 else if (0 == strncmp(option, "no", 2))
1188 make_map_files = 0;
1189 else
1190 grammar(cmd, filename, linenum);
1192 } else
1193 grammar(cmd, filename, linenum);
1195 free(line);
1197 fclose(cfile);
1198 return 1;
1201 /* Simple format, ignore lines starting with #, one command per line.
1202 Returns true or false. */
1203 static int read_config(const char *filename,
1204 const char *basedir,
1205 struct module_search **search,
1206 struct module_overrides **overrides)
1208 DIR *dir;
1209 int ret = 0;
1211 dir = opendir(filename);
1212 if (dir) {
1213 struct dirent *i;
1214 while ((i = readdir(dir)) != NULL) {
1215 if (!streq(i->d_name,".") && !streq(i->d_name,"..")
1216 && config_filter(i->d_name)) {
1217 char sub[strlen(filename) + 1
1218 + strlen(i->d_name) + 1];
1220 sprintf(sub, "%s/%s", filename, i->d_name);
1221 if (!read_config(sub, basedir, search,
1222 overrides))
1223 warn("Failed to open"
1224 " config file %s: %s\n",
1225 sub, strerror(errno));
1228 closedir(dir);
1229 ret = 1;
1230 } else {
1231 if (read_config_file(filename, basedir, search, overrides))
1232 ret = 1;
1235 return ret;
1238 static const char *default_configs[] =
1240 "/etc/depmod.conf",
1241 "/etc/depmod.d",
1244 static void read_toplevel_config(const char *filename,
1245 const char *basedir,
1246 struct module_search **search,
1247 struct module_overrides **overrides)
1249 unsigned int i;
1251 if (filename) {
1252 if (!read_config(filename, basedir, search, overrides))
1253 fatal("Failed to open config file %s: %s\n",
1254 filename, strerror(errno));
1255 return;
1258 /* Try defaults. */
1259 for (i = 0; i < ARRAY_SIZE(default_configs); i++) {
1260 read_config(default_configs[i], basedir, search, overrides);
1264 /* Local to main, but not freed on exit. Keep valgrind quiet. */
1265 struct module *list = NULL;
1266 struct module_search *search = NULL;
1267 struct module_overrides *overrides = NULL;
1269 int main(int argc, char *argv[])
1271 int opt, all = 0, maybe_all = 0, doing_stdout = 0;
1272 char *basedir = "", *dirname, *version, *badopt = NULL,
1273 *system_map = NULL;
1274 int i;
1275 const char *config = NULL;
1277 /* Don't print out any errors just yet, we might want to exec
1278 backwards compat version. */
1279 opterr = 0;
1280 while ((opt = getopt_long(argc, argv, "ab:ArehnqruvVF:C:wm", options, NULL))
1281 != -1) {
1282 switch (opt) {
1283 case 'a':
1284 all = 1;
1285 break;
1286 case 'b':
1287 basedir = optarg;
1288 skipchars = strlen(basedir);
1289 break;
1290 case 'A':
1291 maybe_all = 1;
1292 break;
1293 case 'F':
1294 system_map = optarg;
1295 break;
1296 case 'e':
1297 print_unknown = 1;
1298 break;
1299 case 'v':
1300 verbose = 1;
1301 break;
1302 case 'u':
1303 case 'q':
1304 case 'r':
1305 break;
1306 case 'C':
1307 config = optarg;
1308 break;
1309 case 'h':
1310 print_usage(argv[0]);
1311 exit(0);
1312 break;
1313 case 'n':
1314 doing_stdout = 1;
1315 break;
1316 case 'V':
1317 printf("%s %s\n", PACKAGE, VERSION);
1318 exit(0);
1319 case 'w':
1320 warn_dups = 1;
1321 break;
1322 case 'm':
1323 force_map_files = 1;
1324 break;
1325 default:
1326 badopt = argv[optind-1];
1330 /* We can't print unknowns without a System.map */
1331 if (!system_map)
1332 print_unknown = 0;
1333 else
1334 load_system_map(system_map);
1336 /* They can specify the version naked on the command line */
1337 if (optind < argc && is_version_number(argv[optind])) {
1338 version = NOFAIL(strdup(argv[optind]));
1339 optind++;
1340 } else {
1341 struct utsname buf;
1342 uname(&buf);
1343 version = NOFAIL(strdup(buf.release));
1346 /* Run old version if required. */
1347 if (old_module_version(version))
1348 exec_old_depmod(argv);
1350 if (badopt) {
1351 fprintf(stderr, "%s: malformed/unrecognized option '%s'\n",
1352 argv[0], badopt);
1353 print_usage(argv[0]);
1354 exit(1);
1357 /* Depmod -a by default if no names. */
1358 if (optind == argc)
1359 all = 1;
1361 dirname = NOFAIL(malloc(strlen(basedir)
1362 + strlen(MODULE_DIR)
1363 + strlen(version) + 1));
1364 sprintf(dirname, "%s%s%s", basedir, MODULE_DIR, version);
1366 if (maybe_all) {
1367 if (!doing_stdout && !depfile_out_of_date(dirname))
1368 exit(0);
1369 all = 1;
1372 read_toplevel_config(config, basedir, &search, &overrides);
1374 /* For backward compatibility add "updates" to the head of the search
1375 * list here. But only if there was no "search" option specified.
1378 if (!search)
1379 search = add_search("updates",search);
1381 if (!all) {
1382 /* Do command line args. */
1383 for (opt = optind; opt < argc; opt++) {
1384 struct module *new = grab_module(NULL, argv[opt]);
1385 if (!new) {
1386 /* cmd-line specified modules must exist */
1387 fatal("grab_module() failed for module %s\n", argv[opt]);
1389 new->next = list;
1390 list = new;
1392 } else {
1393 list = grab_basedir(dirname,search,overrides);
1395 list = sort_modules(dirname,list);
1396 list = parse_modules(list);
1398 for (i = 0; i < sizeof(depfiles)/sizeof(depfiles[0]); i++) {
1399 FILE *out;
1400 struct depfile *d = &depfiles[i];
1401 char depname[strlen(dirname) + 1 + strlen(d->name) + 1];
1402 char tmpname[strlen(dirname) + 1 + strlen(d->name) +
1403 strlen(".temp") + 1];
1405 if (d->map_file && !make_map_files && !force_map_files)
1406 continue;
1408 sprintf(depname, "%s/%s", dirname, d->name);
1409 sprintf(tmpname, "%s/%s.temp", dirname, d->name);
1410 if (!doing_stdout) {
1411 out = fopen(tmpname, "w");
1412 if (!out)
1413 fatal("Could not open %s for writing: %s\n",
1414 tmpname, strerror(errno));
1415 } else {
1416 out = stdout;
1417 if (ends_in(depname, ".bin"))
1418 continue;
1420 d->func(list, out, dirname);
1421 if (!doing_stdout) {
1422 fclose(out);
1423 if (rename(tmpname, depname) < 0)
1424 fatal("Could not rename %s into %s: %s\n",
1425 tmpname, depname, strerror(errno));
1429 free(dirname);
1430 free(version);
1432 return 0;