1 /* New simplified depmod without backwards compat stuff and not
4 (C) 2002 Rusty Russell IBM Corporation
6 #define _GNU_SOURCE /* asprintf */
15 #include <sys/types.h>
19 #include <sys/utsname.h>
22 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
25 #include "zlibsupport.h"
31 #include "config_filter.h"
36 #define MODULE_DIR "/lib/modules/"
39 #ifndef MODULE_BUILTIN_KEY
40 #define MODULE_BUILTIN_KEY "built-in"
43 struct module_overrides
46 struct module_overrides
*next
;
48 /* overridden module */
55 struct module_search
*next
;
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
75 static struct symbol
*symbolhash
[SYMBOL_HASH_SIZE
];
77 /* This is based on the hash agorithm from gdbm, via tdb */
78 static inline unsigned int tdb_hash(const char *name
)
80 unsigned value
; /* Used to compute the hash value. */
81 unsigned i
; /* Used to cycle through random values. */
83 /* Set the initial value from the key size. */
84 for (value
= 0x238F13AF * strlen(name
), i
=0; name
[i
]; i
++)
85 value
= (value
+ (((unsigned char *)name
)[i
] << (i
*5 % 24)));
87 return (1103515243 * value
+ 12345);
90 void add_symbol(const char *name
, uint64_t ver
, struct module
*owner
)
93 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
, check_symvers
;
106 struct module
*find_symbol(const char *name
, uint64_t ver
,
107 const char *modname
, int weak
)
111 /* For our purposes, .foo matches foo. PPC64 needs this. */
115 for (s
= symbolhash
[tdb_hash(name
) % SYMBOL_HASH_SIZE
]; s
; s
=s
->next
) {
116 if (streq(s
->name
, name
))
120 if (ver
&& s
->ver
&& s
->ver
!= ver
&& print_unknown
&& !weak
)
121 warn("%s disagrees about version of symbol %s\n",
126 if (print_unknown
&& !weak
)
127 warn("%s needs unknown symbol %s\n", modname
, name
);
132 void add_dep(struct module
*mod
, struct module
*depends_on
)
136 for (i
= 0; i
< mod
->num_deps
; i
++)
137 if (mod
->deps
[i
] == depends_on
)
140 mod
->deps
= NOFAIL(realloc(mod
->deps
, sizeof(mod
->deps
[0])*(mod
->num_deps
+1)));
141 mod
->deps
[mod
->num_deps
++] = depends_on
;
144 static void add_fake_syms(void)
146 /* __this_module is magic inserted by kernel loader. */
147 add_symbol("__this_module", 0, NULL
);
148 /* On S390, this is faked up too */
149 add_symbol("_GLOBAL_OFFSET_TABLE_", 0, NULL
);
152 static void load_system_map(const char *filename
)
156 const char ksymstr
[] = "__ksymtab_";
157 const int ksymstr_len
= strlen(ksymstr
);
159 system_map
= fopen(filename
, "r");
161 fatal("Could not open '%s': %s\n", filename
, strerror(errno
));
163 /* eg. c0294200 R __ksymtab_devfs_alloc_devnum */
164 while (fgets(line
, sizeof(line
)-1, system_map
)) {
168 ptr
= strchr(line
, '\n');
172 ptr
= strchr(line
, ' ');
173 if (!ptr
|| !(ptr
= strchr(ptr
+ 1, ' ')))
176 /* Covers gpl-only and normal symbols. */
177 if (strstarts(ptr
+1, ksymstr
))
178 add_symbol(ptr
+1+ksymstr_len
, 0, NULL
);
185 static void load_module_symvers(const char *filename
)
187 FILE *module_symvers
;
190 module_symvers
= fopen(filename
, "r");
192 fatal("Could not open '%s': %s\n", filename
, strerror(errno
));
194 /* eg. "0xb352177e\tfind_first_bit\tvmlinux\tEXPORT_SYMBOL" */
195 while (fgets(line
, sizeof(line
)-1, module_symvers
)) {
196 const char *ver
, *sym
, *where
;
198 ver
= strtok(line
, " \t");
199 sym
= strtok(NULL
, " \t");
200 where
= strtok(NULL
, " \t");
201 if (!ver
|| !sym
|| !where
)
204 if (streq(where
, "vmlinux"))
205 add_symbol(sym
, strtoull(ver
, NULL
, 16), NULL
);
208 fclose(module_symvers
);
212 static struct option options
[] = { { "all", 0, NULL
, 'a' },
213 { "quick", 0, NULL
, 'A' },
214 { "basedir", 1, NULL
, 'b' },
215 { "config", 1, NULL
, 'C' },
216 { "symvers", 1, NULL
, 'E' },
217 { "filesyms", 1, NULL
, 'F' },
218 { "errsyms", 0, NULL
, 'e' },
219 { "unresolved-error", 0, NULL
, 'u' },
220 { "quiet", 0, NULL
, 'q' },
221 { "root", 0, NULL
, 'r' },
222 { "verbose", 0, NULL
, 'v' },
223 { "show", 0, NULL
, 'n' },
224 { "dry-run", 0, NULL
, 'n' },
225 { "help", 0, NULL
, 'h' },
226 { "version", 0, NULL
, 'V' },
227 { "warn", 0, NULL
, 'w' },
228 { "map", 0, NULL
, 'm' },
229 { NULL
, 0, NULL
, 0 } };
231 /* Version number or module name? Don't assume extension. */
232 static int is_version_number(const char *version
)
236 return (sscanf(version
, "%u.%u.%u", &dummy
, &dummy
, &dummy
) == 3);
239 static int old_module_version(const char *version
)
241 /* Expect three part version. */
242 unsigned int major
, sub
, minor
;
244 sscanf(version
, "%u.%u.%u", &major
, &sub
, &minor
);
246 if (major
> 2) return 0;
247 if (major
< 2) return 1;
250 if (sub
> 5) return 0;
251 if (sub
< 5) return 1;
254 if (minor
>= 48) return 0;
258 static void print_usage(const char *name
)
261 "%s " VERSION
" -- part of " PACKAGE
"\n"
262 "%s -[aA] [-n -e -v -q -V -r -u -w -m]\n"
263 " [-b basedirectory] [forced_version]\n"
264 "depmod [-n -e -v -q -r -u -w] [-F kernelsyms] module1.ko module2.ko ...\n"
265 "If no arguments (except options) are given, \"depmod -a\" is assumed\n"
267 "depmod will output a dependancy list suitable for the modprobe utility.\n"
271 "\t-a, --all Probe all modules\n"
272 "\t-A, --quick Only does the work if there's a new module\n"
273 "\t-e, --errsyms Report not supplied symbols\n"
274 "\t-m, --map Create the legacy map files\n"
275 "\t-n, --show Write the dependency file on stdout only\n"
276 "\t-V, --version Print the release version\n"
277 "\t-v, --verbose Enable verbose mode\n"
278 "\t-w, --warn Warn on duplicates\n"
279 "\t-h, --help Print this usage message\n"
281 "The following options are useful for people managing distributions:\n"
282 "\t-b basedirectory\n"
283 "\t --basedir basedirectory Use an image of a module tree.\n"
285 "\t --filesyms kernelsyms Use the file instead of the\n"
286 "\t current kernel symbols.\n"
287 "\t-E Module.symvers\n"
288 "\t --symvers Module.symvers Use Module.symvers file to check\n"
289 "\t symbol versions.\n",
293 static int ends_in(const char *name
, const char *ext
)
295 unsigned int namelen
, extlen
;
298 namelen
= strlen(name
);
299 extlen
= strlen(ext
);
301 if (namelen
< extlen
) return 0;
303 if (streq(name
+ namelen
- extlen
, ext
))
308 static struct module
*grab_module(const char *dirname
, const char *filename
)
312 new = NOFAIL(malloc(sizeof(*new)
313 + strlen(dirname
?:"") + 1 + strlen(filename
) + 1));
315 sprintf(new->pathname
, "%s/%s", dirname
, filename
);
317 strcpy(new->pathname
, filename
);
318 new->basename
= my_basename(new->pathname
);
320 INIT_LIST_HEAD(&new->dep_list
);
321 new->order
= INDEX_PRIORITY_MIN
;
323 new->file
= grab_elf_file(new->pathname
);
325 warn("Can't read module %s: %s\n",
326 new->pathname
, strerror(errno
));
333 struct module_traverse
335 struct module_traverse
*prev
;
339 static int in_loop(struct module
*mod
, const struct module_traverse
*traverse
)
341 const struct module_traverse
*i
;
343 for (i
= traverse
; i
; i
= i
->prev
) {
350 /* Assume we are doing all the modules, so only report each loop once. */
351 static void report_loop(const struct module
*mod
,
352 const struct module_traverse
*traverse
)
354 const struct module_traverse
*i
;
356 /* Check that start is least alphabetically. eg. a depends
357 on b depends on a will get reported for a, not b. */
358 for (i
= traverse
->prev
; i
->prev
; i
= i
->prev
) {
359 if (strcmp(mod
->pathname
, i
->mod
->pathname
) > 0)
363 /* Is start in the loop? If not, don't report now. eg. a
364 depends on b which depends on c which depends on b. Don't
365 report when generating depends for a. */
369 warn("Loop detected: %s ", mod
->pathname
);
370 for (i
= traverse
->prev
; i
->prev
; i
= i
->prev
)
371 fprintf(stderr
, "needs %s ", i
->mod
->basename
);
372 fprintf(stderr
, "which needs %s again!\n", i
->mod
->basename
);
375 /* This is damn slow, but loops actually happen, and we don't want to
376 just exit() and leave the user without any modules. */
377 static int has_dep_loop(struct module
*module
, struct module_traverse
*prev
)
380 struct module_traverse traverse
= { .prev
= prev
, .mod
= module
};
382 if (in_loop(module
, prev
)) {
383 report_loop(module
, &traverse
);
387 for (i
= 0; i
< module
->num_deps
; i
++)
388 if (has_dep_loop(module
->deps
[i
], &traverse
))
393 /* Uniquifies and orders a dependency list. */
394 static void order_dep_list(struct module
*start
, struct module
*mod
)
398 for (i
= 0; i
< mod
->num_deps
; i
++) {
399 /* If it was previously depended on, move it to the
400 tail. ie. if a needs b and c, and c needs b, we
401 must order b after c. */
402 list_del(&mod
->deps
[i
]->dep_list
);
403 list_add_tail(&mod
->deps
[i
]->dep_list
, &start
->dep_list
);
404 order_dep_list(start
, mod
->deps
[i
]);
408 static struct module
*deleted
= NULL
;
410 static void del_module(struct module
**modules
, struct module
*delme
)
414 /* Find pointer to it. */
416 for (i
= modules
; *i
!= delme
; i
= &(*i
)->next
);
421 /* Save on a list to quiet valgrind.
422 Can't free - other modules may depend on them */
423 delme
->next
= deleted
;
427 /* convert to relative path if possible */
428 static const char *compress_path(const char *path
, const char *basedir
)
430 int len
= strlen(basedir
);
432 if (strncmp(path
, basedir
, len
) == 0)
437 static void output_deps(struct module
*modules
,
438 FILE *out
, char *dirname
)
442 for (i
= modules
; i
; i
= i
->next
) {
443 struct list_head
*j
, *tmp
;
444 order_dep_list(i
, i
);
446 fprintf(out
, "%s:", compress_path(i
->pathname
, dirname
));
447 list_for_each_safe(j
, tmp
, &i
->dep_list
) {
449 = list_entry(j
, struct module
, dep_list
);
451 compress_path(dep
->pathname
, dirname
));
458 /* warn whenever duplicate module aliases, deps, or symbols are found. */
461 static void output_deps_bin(struct module
*modules
,
462 FILE *out
, char *dirname
)
465 struct index_node
*index
;
469 index
= index_create();
471 for (i
= modules
; i
; i
= i
->next
) {
472 struct list_head
*j
, *tmp
;
473 char modname
[strlen(i
->pathname
)+1];
475 order_dep_list(i
, i
);
477 filename2modname(modname
, i
->pathname
);
478 nofail_asprintf(&line
, "%s:",
479 compress_path(i
->pathname
, dirname
));
481 list_for_each_safe(j
, tmp
, &i
->dep_list
) {
483 = list_entry(j
, struct module
, dep_list
);
484 nofail_asprintf(&line
, "%s %s",
486 compress_path(dep
->pathname
, dirname
));
491 if (index_insert(index
, modname
, line
, i
->order
) && warn_dups
)
492 warn("duplicate module deps:\n%s\n",line
);
496 index_write(index
, out
);
497 index_destroy(index
);
501 static int smells_like_module(const char *name
)
503 return ends_in(name
,".ko") || ends_in(name
, ".ko.gz");
506 typedef struct module
*(*do_module_t
)(const char *dirname
,
507 const char *filename
,
509 struct module_search
*search
,
510 struct module_overrides
*overrides
);
512 static int is_higher_priority(const char *newpath
, const char *oldpath
,
513 struct module_search
*search
,
514 struct module_overrides
*overrides
)
516 struct module_search
*tmp
;
517 struct module_overrides
*ovtmp
;
519 int prio_builtin
= -1;
523 /* The names already match, now we check for overrides and directory search
526 for (ovtmp
= overrides
; ovtmp
!= NULL
; ovtmp
= ovtmp
->next
) {
527 if (streq(ovtmp
->modfile
, newpath
))
529 if (streq(ovtmp
->modfile
, oldpath
))
532 for (i
= 0, tmp
= search
; tmp
!= NULL
; tmp
= tmp
->next
, i
++) {
533 if (streq(tmp
->search_path
, MODULE_BUILTIN_KEY
))
535 else if (strncmp(tmp
->search_path
, newpath
, tmp
->len
) == 0)
537 else if (strncmp(tmp
->search_path
, oldpath
, tmp
->len
) == 0)
541 prio_new
= prio_builtin
;
543 prio_old
= prio_builtin
;
545 return prio_new
> prio_old
;
549 static struct module
*do_module(const char *dirname
,
550 const char *filename
,
552 struct module_search
*search
,
553 struct module_overrides
*overrides
)
555 struct module
*new, **i
;
557 new = grab_module(dirname
, filename
);
561 /* Check if module is already in the list. */
562 for (i
= &list
; *i
; i
= &(*i
)->next
) {
564 if (streq((*i
)->basename
, filename
)) {
565 char newpath
[strlen(dirname
) + strlen("/")
566 + strlen(filename
) + 1];
568 sprintf(newpath
, "%s/%s", dirname
, filename
);
570 if (is_higher_priority(newpath
, (*i
)->pathname
,search
,
577 del_module(NULL
, new);
583 /* Not in the list already. Just prepend. */
588 static struct module
*grab_dir(const char *dirname
,
592 struct module_search
*search
,
593 struct module_overrides
*overrides
)
595 struct dirent
*dirent
;
597 while ((dirent
= readdir(dir
)) != NULL
) {
598 if (smells_like_module(dirent
->d_name
))
599 next
= do_mod(dirname
, dirent
->d_name
, next
,
601 else if (!streq(dirent
->d_name
, ".")
602 && !streq(dirent
->d_name
, "..")
603 && !streq(dirent
->d_name
, "source")
604 && !streq(dirent
->d_name
, "build")) {
607 char subdir
[strlen(dirname
) + 1
608 + strlen(dirent
->d_name
) + 1];
609 sprintf(subdir
, "%s/%s", dirname
, dirent
->d_name
);
610 sub
= opendir(subdir
);
612 next
= grab_dir(subdir
, sub
, next
, do_mod
,
621 static struct module
*grab_basedir(const char *dirname
,
622 struct module_search
*search
,
623 struct module_overrides
*overrides
)
628 dir
= opendir(dirname
);
630 warn("Couldn't open directory %s: %s\n",
631 dirname
, strerror(errno
));
634 list
= grab_dir(dirname
, dir
, NULL
, do_module
, search
, overrides
);
640 static struct module
*sort_modules(const char *dirname
, struct module
*list
)
642 struct module
*tlist
= NULL
, **tpos
= &tlist
;
644 int dir_len
= strlen(dirname
) + 1;
645 char file_name
[dir_len
+ strlen("modules.order") + 1];
647 unsigned int linenum
= 0;
649 sprintf(file_name
, "%s/%s", dirname
, "modules.order");
651 modorder
= fopen(file_name
, "r");
653 /* Older kernels don't generate modules.order. Just
654 return if the file doesn't exist. */
657 fatal("Could not open '%s': %s\n", file_name
, strerror(errno
));
660 sprintf(line
, "%s/", dirname
);
662 /* move modules listed in modorder file to tlist in order */
663 while (fgets(line
, sizeof(line
), modorder
)) {
664 struct module
**pos
, *mod
;
665 int len
= strlen(line
);
668 if (line
[len
- 1] == '\n')
669 line
[len
- 1] = '\0';
671 for (pos
= &list
; (mod
= *pos
); pos
= &(*pos
)->next
) {
672 if (streq(line
, mod
->pathname
+ dir_len
)) {
673 mod
->order
= linenum
;
683 /* append the rest */
691 /* Calculate the dependencies for this module */
692 static void calculate_deps(struct module
*module
)
695 struct string_table
*symnames
;
696 struct string_table
*symtypes
;
697 uint64_t *symvers
= NULL
;
698 struct elf_file
*file
;
700 module
->num_deps
= 0;
704 symnames
= file
->ops
->load_dep_syms(file
, &symtypes
,
705 check_symvers
? &symvers
: NULL
);
706 if (!symnames
|| !symtypes
)
709 for (i
= 0; i
< symnames
->cnt
; i
++) {
712 struct module
*owner
;
715 name
= symnames
->str
[i
];
716 ver
= symvers
? symvers
[i
] : 0;
717 weak
= (*(symtypes
->str
[i
]) == 'W');
718 owner
= find_symbol(name
, ver
, module
->pathname
, weak
);
720 info("%s needs \"%s\": %s\n",
721 module
->pathname
, name
,
723 add_dep(module
, owner
);
732 static struct module
*parse_modules(struct module
*list
)
735 struct elf_file
*file
;
736 struct string_table
*syms
;
739 for (i
= list
; i
; i
= i
->next
) {
740 uint64_t *symvers
= NULL
;
742 syms
= file
->ops
->load_symbols(file
,
743 check_symvers
? &symvers
: NULL
);
745 for (j
= 0; j
< syms
->cnt
; j
++)
746 add_symbol(syms
->str
[j
],
747 symvers
? symvers
[j
] : 0, i
);
751 file
->ops
->fetch_tables(file
, &i
->tables
);
754 for (i
= list
; i
; i
= i
->next
)
757 /* Strip out modules with dependency loops. */
759 for (i
= list
; i
; i
= i
->next
) {
760 if (has_dep_loop(i
, NULL
)) {
761 warn("Module %s ignored, due to loop\n",
762 i
->pathname
+ skipchars
);
763 del_module(&list
, i
);
771 /* Simply dump hash table. */
772 static void output_symbols(struct module
*unused
, FILE *out
, char *dirname
)
776 fprintf(out
, "# Aliases for symbols, used by symbol_request().\n");
777 for (i
= 0; i
< SYMBOL_HASH_SIZE
; i
++) {
780 for (s
= symbolhash
[i
]; s
; s
= s
->next
) {
782 char modname
[strlen(s
->owner
->pathname
)+1];
783 filename2modname(modname
, s
->owner
->pathname
);
784 fprintf(out
, "alias symbol:%s %s\n",
791 static void output_symbols_bin(struct module
*unused
, FILE *out
, char *dirname
)
793 struct index_node
*index
;
798 index
= index_create();
800 for (i
= 0; i
< SYMBOL_HASH_SIZE
; i
++) {
803 for (s
= symbolhash
[i
]; s
; s
= s
->next
) {
805 char modname
[strlen(s
->owner
->pathname
)+1];
806 filename2modname(modname
, s
->owner
->pathname
);
807 nofail_asprintf(&alias
, "symbol:%s", s
->name
);
808 duplicate
= index_insert(index
, alias
, modname
,
810 if (duplicate
&& warn_dups
)
811 warn("duplicate module syms:\n%s %s\n",
818 index_write(index
, out
);
819 index_destroy(index
);
822 static void output_aliases(struct module
*modules
, FILE *out
, char *dirname
)
825 struct elf_file
*file
;
826 struct string_table
*tbl
;
829 fprintf(out
, "# Aliases extracted from modules themselves.\n");
830 for (i
= modules
; i
; i
= i
->next
) {
831 char modname
[strlen(i
->pathname
)+1];
834 filename2modname(modname
, i
->pathname
);
836 /* Grab from old-style .modalias section. */
837 tbl
= file
->ops
->load_strings(file
, ".modalias", NULL
, fatal
);
838 for (j
= 0; tbl
&& j
< tbl
->cnt
; j
++)
839 fprintf(out
, "alias %s %s\n", tbl
->str
[j
], modname
);
842 /* Grab from new-style .modinfo section. */
843 tbl
= file
->ops
->load_strings(file
, ".modinfo", NULL
, fatal
);
844 for (j
= 0; tbl
&& j
< tbl
->cnt
; j
++) {
845 const char *p
= tbl
->str
[j
];
846 if (strstarts(p
, "alias="))
847 fprintf(out
, "alias %s %s\n",
848 p
+ strlen("alias="), modname
);
854 static void output_aliases_bin(struct module
*modules
, FILE *out
, char *dirname
)
857 struct elf_file
*file
;
858 struct string_table
*tbl
;
861 struct index_node
*index
;
864 index
= index_create();
866 for (i
= modules
; i
; i
= i
->next
) {
867 char modname
[strlen(i
->pathname
)+1];
870 filename2modname(modname
, i
->pathname
);
872 /* Grab from old-style .modalias section. */
873 tbl
= file
->ops
->load_strings(file
, ".modalias", NULL
, fatal
);
874 for (j
= 0; tbl
&& j
< tbl
->cnt
; j
++) {
875 alias
= NOFAIL(strdup(tbl
->str
[j
]));
877 duplicate
= index_insert(index
, alias
, modname
, i
->order
);
878 if (duplicate
&& warn_dups
)
879 warn("duplicate module alias:\n%s %s\n",
885 /* Grab from new-style .modinfo section. */
886 tbl
= file
->ops
->load_strings(file
, ".modinfo", NULL
, fatal
);
887 for (j
= 0; tbl
&& j
< tbl
->cnt
; j
++) {
888 const char *p
= tbl
->str
[j
];
889 if (strstarts(p
, "alias=")) {
890 alias
= NOFAIL(strdup(p
+ strlen("alias=")));
892 duplicate
= index_insert(index
, alias
, modname
, i
->order
);
893 if (duplicate
&& warn_dups
)
894 warn("duplicate module alias:\n%s %s\n",
902 index_write(index
, out
);
903 index_destroy(index
);
908 void (*func
)(struct module
*, FILE *, char *dirname
);
912 static struct depfile depfiles
[] = {
913 { "modules.dep", output_deps
, 0 }, /* This is what we check for '-A'. */
914 { "modules.dep.bin", output_deps_bin
, 0 },
915 { "modules.pcimap", output_pci_table
, 1 },
916 { "modules.usbmap", output_usb_table
, 1 },
917 { "modules.ccwmap", output_ccw_table
, 1 },
918 { "modules.ieee1394map", output_ieee1394_table
, 1 },
919 { "modules.isapnpmap", output_isapnp_table
, 1 },
920 { "modules.inputmap", output_input_table
, 1 },
921 { "modules.ofmap", output_of_table
, 1 },
922 { "modules.seriomap", output_serio_table
, 1 },
923 { "modules.alias", output_aliases
, 0 },
924 { "modules.alias.bin", output_aliases_bin
, 0 },
925 { "modules.symbols", output_symbols
, 0 },
926 { "modules.symbols.bin", output_symbols_bin
, 0 }
929 /* If we can't figure it out, it's safe to say "true". */
930 static int any_modules_newer(const char *dirname
, time_t mtime
)
933 struct dirent
*dirent
;
935 dir
= opendir(dirname
);
939 while ((dirent
= readdir(dir
)) != NULL
) {
941 char file
[strlen(dirname
) + 1 + strlen(dirent
->d_name
) + 1];
943 if (streq(dirent
->d_name
, ".") || streq(dirent
->d_name
, ".."))
946 sprintf(file
, "%s/%s", dirname
, dirent
->d_name
);
947 if (lstat(file
, &st
) != 0)
950 if (smells_like_module(dirent
->d_name
)) {
951 if (st
.st_mtime
> mtime
)
953 } else if (S_ISDIR(st
.st_mode
)) {
954 if (any_modules_newer(file
, mtime
))
966 static int depfile_out_of_date(const char *dirname
)
969 char depfile
[strlen(dirname
) + 1 + strlen(depfiles
[0].name
) + 1];
971 sprintf(depfile
, "%s/%s", dirname
, depfiles
[0].name
);
973 if (stat(depfile
, &st
) != 0)
976 return any_modules_newer(dirname
, st
.st_mtime
);
979 static char *strsep_skipspace(char **string
, char *delim
)
983 *string
+= strspn(*string
, delim
);
984 return strsep(string
, delim
);
987 static struct module_search
*add_search(const char *search_path
,
989 struct module_search
*search
)
992 struct module_search
*new;
994 new = NOFAIL(malloc(sizeof(*new)));
995 new->search_path
= NOFAIL(strdup(search_path
));
1003 static struct module_overrides
*add_override(const char *modfile
,
1004 struct module_overrides
*overrides
)
1007 struct module_overrides
*new;
1009 new = NOFAIL(malloc(sizeof(*new)));
1010 new->modfile
= NOFAIL(strdup(modfile
));
1011 new->next
= overrides
;
1017 static int parse_config_scan(const char *filename
,
1018 const char *basedir
,
1019 const char *kernelversion
,
1020 struct module_search
**search
,
1021 struct module_overrides
**overrides
);
1023 static int parse_config_file(const char *filename
,
1024 const char *basedir
,
1025 const char *kernelversion
,
1026 struct module_search
**search
,
1027 struct module_overrides
**overrides
)
1030 unsigned int linenum
= 0;
1033 cfile
= fopen(filename
, "r");
1035 if (errno
!= ENOENT
)
1036 fatal("could not open '%s', reason: %s\n", filename
,
1041 while ((line
= getline_wrapped(cfile
, &linenum
)) != NULL
) {
1043 char *cmd
, *modname
;
1045 cmd
= strsep_skipspace(&ptr
, "\t ");
1047 if (cmd
== NULL
|| cmd
[0] == '#' || cmd
[0] == '\0') {
1052 if (streq(cmd
, "search")) {
1055 while ((search_path
= strsep_skipspace(&ptr
, "\t "))) {
1059 if (strcmp(search_path
,
1060 MODULE_BUILTIN_KEY
) == 0) {
1061 *search
= add_search(MODULE_BUILTIN_KEY
,
1065 nofail_asprintf(&dirname
, "%s%s%s/%s", basedir
,
1066 MODULE_DIR
, kernelversion
, search_path
);
1067 len
= strlen(dirname
);
1068 *search
= add_search(dirname
, len
, *search
);
1071 } else if (streq(cmd
, "override")) {
1072 char *pathname
= NULL
, *version
, *subdir
;
1073 modname
= strsep_skipspace(&ptr
, "\t ");
1074 version
= strsep_skipspace(&ptr
, "\t ");
1075 subdir
= strsep_skipspace(&ptr
, "\t ");
1077 if (strcmp(version
, kernelversion
) != 0 &&
1078 strcmp(version
, "*") != 0)
1081 nofail_asprintf(&pathname
, "%s%s%s/%s/%s.ko", basedir
,
1082 MODULE_DIR
, kernelversion
, subdir
, modname
);
1084 *overrides
= add_override(pathname
, *overrides
);
1086 } else if (streq(cmd
, "include")) {
1089 newfilename
= strsep_skipspace(&ptr
, "\t ");
1091 grammar(cmd
, filename
, linenum
);
1093 warn("\"include %s\" is deprecated, "
1094 "please use /etc/depmod.d\n", newfilename
);
1095 if (strstarts(newfilename
, "/etc/depmod.d")) {
1096 warn("\"include /etc/depmod.d\" is "
1097 "the default, ignored\n");
1099 if (!parse_config_scan(newfilename
, basedir
,
1102 warn("Failed to open included"
1103 " config file %s: %s\n",
1104 newfilename
, strerror(errno
));
1107 } else if (streq(cmd
, "make_map_files")) {
1110 option
= strsep_skipspace(&ptr
, "\t ");
1112 grammar(cmd
, filename
, linenum
);
1114 if (streq(option
, "yes"))
1116 else if (streq(option
, "no"))
1119 grammar(cmd
, filename
, linenum
);
1122 grammar(cmd
, filename
, linenum
);
1130 static int parse_config_scan(const char *filename
,
1131 const char *basedir
,
1132 const char *kernelversion
,
1133 struct module_search
**search
,
1134 struct module_overrides
**overrides
)
1139 dir
= opendir(filename
);
1142 struct list_head node
;
1145 LIST_HEAD(files_list
);
1146 struct file_entry
*fe
, *fe_tmp
;
1149 /* sort files from directory into list */
1150 while ((i
= readdir(dir
)) != NULL
) {
1153 if (i
->d_name
[0] == '.')
1155 if (!config_filter(i
->d_name
))
1158 len
= strlen(i
->d_name
);
1159 if (len
< 6 || strcmp(&i
->d_name
[len
-5], ".conf") != 0)
1160 warn("All config files need .conf: %s/%s, "
1161 "it will be ignored in a future release.\n",
1162 filename
, i
->d_name
);
1163 fe
= malloc(sizeof(struct file_entry
) + len
+ 1);
1166 strcpy(fe
->name
, i
->d_name
);
1167 list_for_each_entry(fe_tmp
, &files_list
, node
)
1168 if (strcmp(fe_tmp
->name
, fe
->name
) >= 0)
1170 list_add_tail(&fe
->node
, &fe_tmp
->node
);
1174 /* parse list of files */
1175 list_for_each_entry_safe(fe
, fe_tmp
, &files_list
, node
) {
1178 nofail_asprintf(&cfgfile
, "%s/%s", filename
, fe
->name
);
1179 if (!parse_config_file(cfgfile
, basedir
, kernelversion
,
1181 warn("Failed to open config file "
1182 "%s: %s\n", fe
->name
, strerror(errno
));
1184 list_del(&fe
->node
);
1190 if (parse_config_file(filename
, basedir
, kernelversion
, search
,
1198 static void parse_toplevel_config(const char *filename
,
1199 const char *basedir
,
1200 const char *kernelversion
,
1201 struct module_search
**search
,
1202 struct module_overrides
**overrides
)
1205 if (!parse_config_scan(filename
, basedir
, kernelversion
, search
,
1207 fatal("Failed to open config file %s: %s\n",
1208 filename
, strerror(errno
));
1212 /* deprecated config file */
1213 if (parse_config_file("/etc/depmod.conf", basedir
, kernelversion
,
1214 search
, overrides
) > 0)
1215 warn("Deprecated config file /etc/depmod.conf, "
1216 "all config files belong into /etc/depmod.d/.\n");
1218 /* default config */
1219 parse_config_scan("/etc/depmod.d", basedir
, kernelversion
,
1223 /* Local to main, but not freed on exit. Keep valgrind quiet. */
1224 struct module
*list
= NULL
;
1225 struct module_search
*search
= NULL
;
1226 struct module_overrides
*overrides
= NULL
;
1228 int main(int argc
, char *argv
[])
1230 int opt
, all
= 0, maybe_all
= 0, doing_stdout
= 0;
1231 char *basedir
= "", *dirname
, *version
;
1232 char *system_map
= NULL
, *module_symvers
= NULL
;
1234 const char *config
= NULL
;
1236 if (native_endianness() == 0)
1239 while ((opt
= getopt_long(argc
, argv
, "aAb:C:E:F:euqrvnhVwm", options
, NULL
))
1250 skipchars
= strlen(basedir
);
1256 module_symvers
= optarg
;
1260 system_map
= optarg
;
1276 print_usage(argv
[0]);
1280 printf("%s %s\n", PACKAGE
, VERSION
);
1286 force_map_files
= 1;
1289 print_usage(argv
[0]);
1295 load_module_symvers(module_symvers
);
1296 else if (system_map
)
1297 load_system_map(system_map
);
1298 else if (print_unknown
) {
1299 warn("-e needs -E or -F");
1303 /* They can specify the version naked on the command line */
1304 if (optind
< argc
&& is_version_number(argv
[optind
])) {
1305 version
= NOFAIL(strdup(argv
[optind
]));
1310 version
= NOFAIL(strdup(buf
.release
));
1313 /* Check for old version. */
1314 if (old_module_version(version
)) {
1315 fprintf(stderr
, "Kernel version %s requires old depmod\n",
1320 /* Depmod -a by default if no names. */
1324 nofail_asprintf(&dirname
, "%s%s%s", basedir
, MODULE_DIR
, version
);
1327 if (!doing_stdout
&& !depfile_out_of_date(dirname
))
1332 parse_toplevel_config(config
, basedir
, version
, &search
, &overrides
);
1334 /* For backward compatibility add "updates" to the head of the search
1335 * list here. But only if there was no "search" option specified.
1341 nofail_asprintf(&dirname
, "%s%s%s/updates", basedir
,
1342 MODULE_DIR
, version
);
1343 len
= strlen(dirname
);
1344 search
= add_search(dirname
, len
, search
);
1347 /* Do command line args. */
1348 for (opt
= optind
; opt
< argc
; opt
++) {
1351 if (argv
[opt
][0] != '/')
1352 fatal("modules must be specified using absolute paths.\n"
1353 "\"%s\" is a relative path\n", argv
[opt
]);
1355 new = grab_module(NULL
, argv
[opt
]);
1357 /* cmd-line specified modules must exist */
1358 fatal("grab_module() failed for module %s\n", argv
[opt
]);
1364 list
= grab_basedir(dirname
,search
,overrides
);
1366 list
= sort_modules(dirname
,list
);
1367 list
= parse_modules(list
);
1369 for (i
= 0; i
< sizeof(depfiles
)/sizeof(depfiles
[0]); i
++) {
1371 struct depfile
*d
= &depfiles
[i
];
1372 char depname
[strlen(dirname
) + 1 + strlen(d
->name
) + 1];
1373 char tmpname
[strlen(dirname
) + 1 + strlen(d
->name
) +
1374 strlen(".temp") + 1];
1376 if (d
->map_file
&& !make_map_files
&& !force_map_files
)
1379 sprintf(depname
, "%s/%s", dirname
, d
->name
);
1380 sprintf(tmpname
, "%s/%s.temp", dirname
, d
->name
);
1381 if (!doing_stdout
) {
1382 out
= fopen(tmpname
, "w");
1384 fatal("Could not open %s for writing: %s\n",
1385 tmpname
, strerror(errno
));
1388 if (ends_in(depname
, ".bin"))
1391 d
->func(list
, out
, dirname
);
1392 if (!doing_stdout
) {
1394 if (rename(tmpname
, depname
) < 0)
1395 fatal("Could not rename %s into %s: %s\n",
1396 tmpname
, depname
, strerror(errno
));