1 /* New simplified depmod without backwards compat stuff and not
4 (C) 2002 Rusty Russell IBM Corporation
14 #include <sys/types.h>
18 #include <sys/utsname.h>
21 #include "zlibsupport.h"
23 #include "moduleops.h"
29 #define MODULE_DIR "/lib/modules/"
33 static unsigned int skipchars;
35 void fatal(const char *fmt, ...)
39 fprintf(stderr, "FATAL: ");
41 va_start(arglist, fmt);
42 vfprintf(stderr, fmt, arglist);
48 void warn(const char *fmt, ...)
52 fprintf(stderr, "WARNING: ");
54 va_start(arglist, fmt);
55 vfprintf(stderr, fmt, arglist);
59 void *do_nofail(void *ptr, const char *file, int line, const char *expr)
62 fatal("Memory allocation failure %s line %d: %s.\n",
68 #define SYMBOL_HASH_SIZE 1024
76 static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
78 /* This is based on the hash agorithm from gdbm, via tdb */
79 static inline unsigned int tdb_hash(const char *name)
81 unsigned value; /* Used to compute the hash value. */
82 unsigned i; /* Used to cycle through random values. */
84 /* Set the initial value from the key size. */
85 for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
86 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
88 return (1103515243 * value + 12345);
91 void add_symbol(const char *name, struct module *owner)
94 struct symbol *new = NOFAIL(malloc(sizeof *new + strlen(name) + 1));
97 strcpy(new->name, name);
99 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
100 new->next = symbolhash[hash];
101 symbolhash[hash] = new;
104 static int print_unknown;
106 struct module *find_symbol(const char *name, const char *modname, int weak)
110 /* For our purposes, .foo matches foo. PPC64 needs this. */
114 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) {
115 if (streq(s->name, name))
119 if (print_unknown && !weak)
120 warn("%s needs unknown symbol %s\n", modname, name);
125 void add_dep(struct module *mod, struct module *depends_on)
129 for (i = 0; i < mod->num_deps; i++)
130 if (mod->deps[i] == depends_on)
133 mod->deps = NOFAIL(realloc(mod->deps, sizeof(mod->deps[0])*(mod->num_deps+1)));
134 mod->deps[mod->num_deps++] = depends_on;
137 static void load_system_map(const char *filename)
142 system_map = fopen(filename, "r");
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)) {
151 ptr = strchr(line, '\n');
154 ptr = strchr(line, ' ');
155 if (!ptr || !(ptr = strchr(ptr + 1, ' ')))
158 /* Covers gpl-only and normal symbols. */
159 if (strncmp(ptr+1, "__ksymtab_", strlen("__ksymtab_")) == 0)
160 add_symbol(ptr+1+strlen("__ksymtab_"), NULL);
163 /* __this_module is magic inserted by kernel loader. */
164 add_symbol("__this_module", NULL);
165 /* On S390, this is faked up too */
166 add_symbol("_GLOBAL_OFFSET_TABLE_", NULL);
169 static struct option options[] = { { "all", 0, NULL, 'a' },
170 { "quick", 0, NULL, 'A' },
171 { "basedir", 1, NULL, 'b' },
172 { "errsyms", 0, NULL, 'e' },
173 { "filesyms", 1, NULL, 'F' },
174 { "help", 0, NULL, 'h' },
175 { "show", 0, NULL, 'n' },
176 { "dry-run", 0, NULL, 'n' },
177 { "quiet", 0, NULL, 'q' },
178 { "root", 0, NULL, 'r' },
179 { "unresolved-error", 0, NULL, 'u' },
180 { "verbose", 0, NULL, 'v' },
181 { "version", 0, NULL, 'V' },
182 /* Obsolete, but we need to parse it. */
183 { "config", 1, NULL, 'C' },
184 { NULL, 0, NULL, 0 } };
186 /* Version number or module name? Don't assume extension. */
187 static int is_version_number(const char *version)
191 return (sscanf(version, "%u.%u.%u", &dummy, &dummy, &dummy) == 3);
194 static int old_module_version(const char *version)
196 /* Expect three part version. */
197 unsigned int major, sub, minor;
199 sscanf(version, "%u.%u.%u", &major, &sub, &minor);
201 if (major > 2) return 0;
202 if (major < 2) return 1;
205 if (sub > 5) return 0;
206 if (sub < 5) return 1;
209 if (minor >= 48) return 0;
213 static void exec_old_depmod(char *argv[])
216 char pathname[strlen(argv[0])+1];
217 char oldname[strlen("depmod") + strlen(argv[0]) + sizeof(".old")];
219 memset(pathname, 0, strlen(argv[0])+1);
220 sep = strrchr(argv[0], '/');
222 memcpy(pathname, argv[0], sep - argv[0]+1);
223 sprintf(oldname, "%s%s.old", pathname, "depmod");
225 /* Recursion detection: we need an env var since we can't
226 change argv[0] (as older modutils uses it to determine
228 if (getenv("MODULE_RECURSE"))
230 setenv("MODULE_RECURSE", "y", 0);
232 execvp(oldname, argv);
234 "Version requires old depmod, but couldn't run %s: %s\n",
235 oldname, strerror(errno));
239 static void print_usage(const char *name)
242 "%s " VERSION " -- part of " PACKAGE "\n"
243 "%s -[aA] [-n -e -v -q -V -r -u]\n"
244 " [-b basedirectory] [forced_version]\n"
245 "depmod [-n -e -v -q -r -u] [-F kernelsyms] module1.o module2.o ...\n"
246 "If no arguments (except options) are given, \"depmod -a\" is assumed\n"
248 "depmod will output a dependancy list suitable for the modprobe utility.\n"
252 "\t-a, --all Probe all modules\n"
253 "\t-n, --show Write the dependency file on stdout only\n"
254 "\t-V, --version Print the release version\n"
255 "\t-h, --help Print this usage message\n"
257 "The following options are useful for people managing distributions:\n"
258 "\t-b basedirectory\n"
259 "\t --basedir basedirectory Use an image of a module tree.\n"
261 "\t --filesyms kernelsyms Use the file instead of the\n"
262 "\t current kernel symbols.\n",
266 static int ends_in(const char *name, const char *ext)
268 unsigned int namelen, extlen;
271 namelen = strlen(name);
272 extlen = strlen(ext);
274 if (namelen < extlen) return 0;
276 if (streq(name + namelen - extlen, ext))
281 /* "\177ELF" <byte> where byte = 001 for 32-bit, 002 for 64 */
282 int needconv(const char *elfhdr)
284 union { short s; char c[2]; } endian_test;
287 if (endian_test.c[1] == 1) return elfhdr[EI_DATA] != ELFDATA2MSB;
288 if (endian_test.c[0] == 1) return elfhdr[EI_DATA] != ELFDATA2LSB;
293 static struct module *grab_module(const char *dirname, const char *filename)
297 new = NOFAIL(malloc(sizeof(*new)
298 + strlen(dirname?:"") + 1 + strlen(filename) + 1));
300 sprintf(new->pathname, "%s/%s", dirname, filename);
302 strcpy(new->pathname, filename);
304 INIT_LIST_HEAD(&new->dep_list);
306 new->data = grab_file(new->pathname, &new->len);
308 warn("Can't read module %s: %s\n",
309 new->pathname, strerror(errno));
313 /* "\177ELF" <byte> where byte = 001 for 32-bit, 002 for 64 */
314 if (memcmp(new->data, ELFMAG, SELFMAG) != 0) {
315 warn("Module %s is not an elf object\n", new->pathname);
319 switch (((char *)new->data)[EI_CLASS]) {
321 new->ops = &mod_ops32;
324 new->ops = &mod_ops64;
327 warn("Module %s has elf unknown identifier %i\n",
328 new->pathname, ((char *)new->data)[EI_CLASS]);
331 new->conv = needconv(new->data);
335 release_file(new->data, new->len);
341 struct module_traverse
343 struct module_traverse *prev;
347 static int in_loop(struct module *mod, const struct module_traverse *traverse)
349 const struct module_traverse *i;
351 for (i = traverse; i; i = i->prev) {
358 static char *basename(const char *name)
360 const char *base = strrchr(name, '/');
361 if (base) return (char *)base + 1;
365 /* Assume we are doing all the modules, so only report each loop once. */
366 static void report_loop(const struct module *mod,
367 const struct module_traverse *traverse)
369 const struct module_traverse *i;
371 /* Check that start is least alphabetically. eg. a depends
372 on b depends on a will get reported for a, not b. */
373 for (i = traverse->prev; i->prev; i = i->prev) {
374 if (strcmp(mod->pathname, i->mod->pathname) > 0)
378 /* Is start in the loop? If not, don't report now. eg. a
379 depends on b which depends on c which depends on b. Don't
380 report when generating depends for a. */
384 warn("Loop detected: %s ", mod->pathname);
385 for (i = traverse->prev; i->prev; i = i->prev)
386 fprintf(stderr, "needs %s ", basename(i->mod->pathname));
387 fprintf(stderr, "which needs %s again!\n", basename(mod->pathname));
390 /* This is damn slow, but loops actually happen, and we don't want to
391 just exit() and leave the user without any modules. */
392 static int has_dep_loop(struct module *module, struct module_traverse *prev)
395 struct module_traverse traverse = { .prev = prev, .mod = module };
397 if (in_loop(module, prev)) {
398 report_loop(module, &traverse);
402 for (i = 0; i < module->num_deps; i++)
403 if (has_dep_loop(module->deps[i], &traverse))
408 /* Uniquifies and orders a dependency list. */
409 static void order_dep_list(struct module *start, struct module *mod)
413 for (i = 0; i < mod->num_deps; i++) {
414 /* If it was previously depended on, move it to the
415 tail. ie. if a needs b and c, and c needs b, we
416 must order b after c. */
417 list_del(&mod->deps[i]->dep_list);
418 list_add_tail(&mod->deps[i]->dep_list, &start->dep_list);
419 order_dep_list(start, mod->deps[i]);
423 static void del_module(struct module **modules, struct module *delme)
427 /* Find pointer to it. */
428 for (i = modules; *i != delme; i = &(*i)->next);
433 static void output_deps(struct module *modules,
438 for (i = modules; i; i = i->next)
439 i->ops->calculate_deps(i, verbose);
441 /* Strip out loops. */
443 for (i = modules; i; i = i->next) {
444 if (has_dep_loop(i, NULL)) {
445 warn("Module %s ignored, due to loop\n",
446 i->pathname + skipchars);
447 del_module(&modules, i);
452 /* Now dump them out. */
453 for (i = modules; i; i = i->next) {
454 struct list_head *j, *tmp;
455 order_dep_list(i, i);
457 fprintf(out, "%s:", i->pathname + skipchars);
458 list_for_each_safe(j, tmp, &i->dep_list) {
460 = list_entry(j, struct module, dep_list);
461 fprintf(out, " %s", dep->pathname + skipchars);
468 static int smells_like_module(const char *name)
470 return ends_in(name,".ko") || ends_in(name, ".ko.gz");
473 typedef struct module *(*do_module_t)(const char *dirname,
474 const char *filename,
475 struct module *next);
477 static int is_update(const char *dirname)
481 p = strstr(dirname, "updates");
484 return (p[strlen("updates")] == '/' || p[strlen("updates")] == '\0');
487 /* Grab everything not under updates/ directories. */
488 static struct module *do_normal_module(const char *dirname,
489 const char *filename,
494 if (is_update(dirname))
496 new = grab_module(dirname, filename);
503 /* Grab everything under updates/ directories, override existing module. */
504 static struct module *do_update_module(const char *dirname,
505 const char *filename,
508 struct module *new, **i;
510 if (!is_update(dirname))
513 new = grab_module(dirname, filename);
517 /* Find module of same name, and replace it. */
518 for (i = &list; *i; i = &(*i)->next) {
519 if (streq(basename((*i)->pathname), filename)) {
520 new->next = (*i)->next;
526 /* Update of non-existent module. Just prepend. */
531 static struct module *grab_dir(const char *dirname,
536 struct dirent *dirent;
538 while ((dirent = readdir(dir)) != NULL) {
539 if (smells_like_module(dirent->d_name))
540 next = do_mod(dirname, dirent->d_name, next);
541 else if (!streq(dirent->d_name, ".")
542 && !streq(dirent->d_name, "..")) {
544 char dummy; /* readlink with 0 len always fails */
545 char subdir[strlen(dirname) + 1
546 + strlen(dirent->d_name) + 1];
547 sprintf(subdir, "%s/%s", dirname, dirent->d_name);
548 /* Don't follow links, eg. build/ */
549 if (readlink(subdir, &dummy, 1) < 0) {
550 sub = opendir(subdir);
552 next = grab_dir(subdir, sub, next,
563 /* RH-ism: updates/ dir overrides other modules. */
564 static struct module *grab_basedir(const char *dirname)
568 char updatedir[strlen(dirname) + sizeof("/updates")];
570 dir = opendir(dirname);
572 warn("Couldn't open directory %s: %s\n",
573 dirname, strerror(errno));
576 list = grab_dir(dirname, dir, NULL, do_normal_module);
579 sprintf(updatedir, "%s/updates", dirname);
580 dir = opendir(updatedir);
582 list = grab_dir(updatedir, dir, list, do_update_module);
588 static void parse_modules(struct module *list)
592 for (i = list; i; i = i->next) {
593 i->ops->load_symbols(i);
594 i->ops->fetch_tables(i);
598 /* Convert filename to the module name. Works if filename == modname, too. */
599 static void filename2modname(char *modname, const char *filename)
601 const char *afterslash;
604 afterslash = strrchr(filename, '/');
606 afterslash = filename;
610 /* Convert to underscores, stop at first . */
611 for (i = 0; afterslash[i] && afterslash[i] != '.'; i++) {
612 if (afterslash[i] == '-')
615 modname[i] = afterslash[i];
620 /* Simply dump hash table. */
621 static void output_symbols(struct module *unused, FILE *out)
625 fprintf(out, "# Aliases for symbols, used by symbol_request().\n");
626 for (i = 0; i < SYMBOL_HASH_SIZE; i++) {
629 for (s = symbolhash[i]; s; s = s->next) {
631 char modname[strlen(s->owner->pathname)+1];
632 filename2modname(modname, s->owner->pathname);
633 fprintf(out, "alias symbol:%s %s\n",
640 static const char *next_string(const char *string, unsigned long *secsize)
642 /* Skip non-zero chars */
645 if ((*secsize)-- <= 1)
649 /* Skip any zero padding. */
652 if ((*secsize)-- <= 1)
658 static void output_aliases(struct module *modules, FILE *out)
664 fprintf(out, "# Aliases extracted from modules themselves.\n");
665 for (i = modules; i; i = i->next) {
666 char modname[strlen(i->pathname)+1];
668 filename2modname(modname, i->pathname);
670 /* Grab from old-style .modalias section. */
671 for (p = i->ops->get_aliases(i, &size);
673 p = next_string(p, &size))
674 fprintf(out, "alias %s %s\n", p, modname);
676 /* Grab form new-style .modinfo section. */
677 for (p = i->ops->get_modinfo(i, &size);
679 p = next_string(p, &size)) {
680 if (strncmp(p, "alias=", strlen("alias=")) == 0)
681 fprintf(out, "alias %s %s\n",
682 p + strlen("alias="), modname);
689 void (*func)(struct module *, FILE *);
692 static struct depfile depfiles[] = {
693 { "modules.dep", output_deps }, /* This is what we check for '-A'. */
694 { "modules.pcimap", output_pci_table },
695 { "modules.usbmap", output_usb_table },
696 { "modules.ccwmap", output_ccw_table },
697 { "modules.ieee1394map", output_ieee1394_table },
698 { "modules.isapnpmap", output_isapnp_table },
699 { "modules.inputmap", output_input_table },
700 { "modules.ofmap", output_of_table },
701 { "modules.seriomap", output_serio_table },
702 { "modules.alias", output_aliases },
703 { "modules.symbols", output_symbols },
706 /* If we can't figure it out, it's safe to say "true". */
707 static int any_modules_newer(const char *dirname, time_t mtime)
710 struct dirent *dirent;
712 dir = opendir(dirname);
716 while ((dirent = readdir(dir)) != NULL) {
718 char file[strlen(dirname) + 1 + strlen(dirent->d_name) + 1];
720 if (streq(dirent->d_name, ".") || streq(dirent->d_name, ".."))
723 sprintf(file, "%s/%s", dirname, dirent->d_name);
724 if (lstat(file, &st) != 0)
727 if (smells_like_module(dirent->d_name)) {
728 if (st.st_mtime > mtime)
730 } else if (S_ISDIR(st.st_mode)) {
731 if (any_modules_newer(file, mtime))
739 static int depfile_out_of_date(const char *dirname)
742 char depfile[strlen(dirname) + 1 + strlen(depfiles[0].name) + 1];
744 sprintf(depfile, "%s/%s", dirname, depfiles[0].name);
746 if (stat(depfile, &st) != 0)
749 return any_modules_newer(dirname, st.st_mtime);
752 int main(int argc, char *argv[])
754 int opt, all = 0, maybe_all = 0, doing_stdout = 0;
755 char *basedir = "", *dirname, *version, *badopt = NULL,
757 struct module *list = NULL;
760 /* Don't print out any errors just yet, we might want to exec
761 backwards compat version. */
763 while ((opt = getopt_long(argc, argv, "ab:ArehnqruvVF:C:", options, NULL))
771 skipchars = strlen(basedir);
792 print_usage(argv[0]);
799 printf("%s %s\n", PACKAGE, VERSION);
802 badopt = argv[optind-1];
806 /* We can't print unknowns without a System.map */
810 load_system_map(system_map);
812 /* They can specify the version naked on the command line */
813 if (optind < argc && is_version_number(argv[optind])) {
814 version = strdup(argv[optind]);
819 version = strdup(buf.release);
822 /* Run old version if required. */
823 if (old_module_version(version))
824 exec_old_depmod(argv);
827 fprintf(stderr, "%s: malformed/unrecognized option '%s'\n",
829 print_usage(argv[0]);
833 /* Depmod -a by default if no names. */
837 dirname = NOFAIL(malloc(strlen(basedir)
839 + strlen(version) + 1));
840 sprintf(dirname, "%s%s%s", basedir, MODULE_DIR, version);
843 if (!doing_stdout && !depfile_out_of_date(dirname))
849 /* Do command line args. */
850 for (opt = optind; opt < argc; opt++) {
851 struct module *new = grab_module(NULL, argv[opt]);
858 list = grab_basedir(dirname);
862 for (i = 0; i < sizeof(depfiles)/sizeof(depfiles[0]); i++) {
864 struct depfile *d = &depfiles[i];
865 char depname[strlen(dirname) + 1 + strlen(d->name) + 1];
866 char tmpname[strlen(dirname) + 1 + strlen(d->name) +
867 strlen(".temp") + 1];
869 sprintf(depname, "%s/%s", dirname, d->name);
870 sprintf(tmpname, "%s/%s.temp", dirname, d->name);
872 out = fopen(tmpname, "w");
874 fatal("Could not open %s for writing: %s\n",
875 tmpname, strerror(errno));
881 if (rename(tmpname, depname) < 0)
882 fatal("Could not rename %s into %s: %s\n",
883 tmpname, depname, strerror(errno));