1 /* New simplified depmod without backwards compat stuff and not
4 (C) 2010 Jon Masters <jcm@jonmasters.org>, and others.
5 (C) 2002 Rusty Russell IBM Corporation
7 #define _GNU_SOURCE /* asprintf */
16 #include <sys/types.h>
20 #include <sys/utsname.h>
23 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
26 #include "zlibsupport.h"
32 #include "config_filter.h"
37 #define MODULE_DIR "/lib/modules/"
40 #ifndef MODULE_BUILTIN_KEY
41 #define MODULE_BUILTIN_KEY "built-in"
44 struct module_overrides
47 struct module_overrides
*next
;
49 /* overridden module */
56 struct module_search
*next
;
63 static unsigned int skipchars
;
64 static unsigned int make_map_files
= 1; /* default to on */
65 static unsigned int force_map_files
= 0; /* default to on */
67 #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
, uint64_t ver
, struct module
*owner
)
94 struct symbol
*new = NOFAIL(malloc(sizeof *new + strlen(name
) + 1));
98 strcpy(new->name
, name
);
100 hash
= tdb_hash(name
) % SYMBOL_HASH_SIZE
;
101 new->next
= symbolhash
[hash
];
102 symbolhash
[hash
] = new;
105 static int print_unknown
, check_symvers
;
107 struct module
*find_symbol(const char *name
, uint64_t ver
,
108 const char *modname
, int weak
)
112 /* For our purposes, .foo matches foo. PPC64 needs this. */
116 for (s
= symbolhash
[tdb_hash(name
) % SYMBOL_HASH_SIZE
]; s
; s
=s
->next
) {
117 if (streq(s
->name
, name
))
121 if (ver
&& s
->ver
&& s
->ver
!= ver
&& print_unknown
&& !weak
)
122 warn("%s disagrees about version of symbol %s\n",
127 if (print_unknown
&& !weak
)
128 warn("%s needs unknown symbol %s\n", modname
, name
);
133 void add_dep(struct module
*mod
, struct module
*depends_on
)
137 for (i
= 0; i
< mod
->num_deps
; i
++)
138 if (mod
->deps
[i
] == depends_on
)
141 mod
->deps
= NOFAIL(realloc(mod
->deps
, sizeof(mod
->deps
[0])*(mod
->num_deps
+1)));
142 mod
->deps
[mod
->num_deps
++] = depends_on
;
145 static void add_fake_syms(void)
147 /* __this_module is magic inserted by kernel loader. */
148 add_symbol("__this_module", 0, NULL
);
149 /* On S390, this is faked up too */
150 add_symbol("_GLOBAL_OFFSET_TABLE_", 0, NULL
);
153 static void load_system_map(const char *filename
)
157 const char ksymstr
[] = "__ksymtab_";
158 const int ksymstr_len
= strlen(ksymstr
);
160 system_map
= fopen(filename
, "r");
162 fatal("Could not open '%s': %s\n", filename
, strerror(errno
));
164 /* eg. c0294200 R __ksymtab_devfs_alloc_devnum */
165 while (fgets(line
, sizeof(line
)-1, system_map
)) {
169 ptr
= strchr(line
, '\n');
173 ptr
= strchr(line
, ' ');
174 if (!ptr
|| !(ptr
= strchr(ptr
+ 1, ' ')))
177 /* Covers gpl-only and normal symbols. */
178 if (strstarts(ptr
+1, ksymstr
))
179 add_symbol(ptr
+1+ksymstr_len
, 0, NULL
);
186 static void load_module_symvers(const char *filename
)
188 FILE *module_symvers
;
191 module_symvers
= fopen(filename
, "r");
193 fatal("Could not open '%s': %s\n", filename
, strerror(errno
));
195 /* eg. "0xb352177e\tfind_first_bit\tvmlinux\tEXPORT_SYMBOL" */
196 while (fgets(line
, sizeof(line
)-1, module_symvers
)) {
197 const char *ver
, *sym
, *where
;
199 ver
= strtok(line
, " \t");
200 sym
= strtok(NULL
, " \t");
201 where
= strtok(NULL
, " \t");
202 if (!ver
|| !sym
|| !where
)
205 if (streq(where
, "vmlinux"))
206 add_symbol(sym
, strtoull(ver
, NULL
, 16), NULL
);
209 fclose(module_symvers
);
213 static struct option options
[] = { { "all", 0, NULL
, 'a' },
214 { "quick", 0, NULL
, 'A' },
215 { "basedir", 1, NULL
, 'b' },
216 { "config", 1, NULL
, 'C' },
217 { "symvers", 1, NULL
, 'E' },
218 { "filesyms", 1, NULL
, 'F' },
219 { "errsyms", 0, NULL
, 'e' },
220 { "unresolved-error", 0, NULL
, 'u' },
221 { "quiet", 0, NULL
, 'q' },
222 { "root", 0, NULL
, 'r' },
223 { "verbose", 0, NULL
, 'v' },
224 { "show", 0, NULL
, 'n' },
225 { "dry-run", 0, NULL
, 'n' },
226 { "help", 0, NULL
, 'h' },
227 { "version", 0, NULL
, 'V' },
228 { "warn", 0, NULL
, 'w' },
229 { "map", 0, NULL
, 'm' },
230 { NULL
, 0, NULL
, 0 } };
232 /* Version number or module name? Don't assume extension. */
233 static int is_version_number(const char *version
)
237 return (sscanf(version
, "%u.%u.%u", &dummy
, &dummy
, &dummy
) == 3);
240 static int old_module_version(const char *version
)
242 /* Expect three part version. */
243 unsigned int major
, sub
, minor
;
245 sscanf(version
, "%u.%u.%u", &major
, &sub
, &minor
);
247 if (major
> 2) return 0;
248 if (major
< 2) return 1;
251 if (sub
> 5) return 0;
252 if (sub
< 5) return 1;
255 if (minor
>= 48) return 0;
259 static void print_usage(const char *name
)
262 "%s " VERSION
" -- part of " PACKAGE
"\n"
263 "%s -[aA] [-n -e -v -q -V -r -u -w -m]\n"
264 " [-b basedirectory] [forced_version]\n"
265 "depmod [-n -e -v -q -r -u -w] [-F kernelsyms] module1.ko module2.ko ...\n"
266 "If no arguments (except options) are given, \"depmod -a\" is assumed\n"
268 "depmod will output a dependancy list suitable for the modprobe utility.\n"
272 "\t-a, --all Probe all modules\n"
273 "\t-A, --quick Only does the work if there's a new module\n"
274 "\t-e, --errsyms Report not supplied symbols\n"
275 "\t-m, --map Create the legacy map files\n"
276 "\t-n, --show Write the dependency file on stdout only\n"
277 "\t-V, --version Print the release version\n"
278 "\t-v, --verbose Enable verbose mode\n"
279 "\t-w, --warn Warn on duplicates\n"
280 "\t-h, --help Print this usage message\n"
282 "The following options are useful for people managing distributions:\n"
283 "\t-b basedirectory\n"
284 "\t --basedir basedirectory Use an image of a module tree.\n"
286 "\t --filesyms kernelsyms Use the file instead of the\n"
287 "\t current kernel symbols.\n"
288 "\t-E Module.symvers\n"
289 "\t --symvers Module.symvers Use Module.symvers file to check\n"
290 "\t symbol versions.\n",
294 static int ends_in(const char *name
, const char *ext
)
296 unsigned int namelen
, extlen
;
299 namelen
= strlen(name
);
300 extlen
= strlen(ext
);
302 if (namelen
< extlen
) return 0;
304 if (streq(name
+ namelen
- extlen
, ext
))
309 static struct module
*grab_module(const char *dirname
, const char *filename
)
313 new = NOFAIL(malloc(sizeof(*new)
314 + strlen(dirname
?:"") + 1 + strlen(filename
) + 1));
316 sprintf(new->pathname
, "%s/%s", dirname
, filename
);
318 strcpy(new->pathname
, filename
);
319 new->basename
= my_basename(new->pathname
);
321 INIT_LIST_HEAD(&new->dep_list
);
322 new->order
= INDEX_PRIORITY_MIN
;
324 new->file
= grab_elf_file(new->pathname
);
326 warn("Can't read module %s: %s\n",
327 new->pathname
, strerror(errno
));
334 struct module_traverse
336 struct module_traverse
*prev
;
340 static int in_loop(struct module
*mod
, const struct module_traverse
*traverse
)
342 const struct module_traverse
*i
;
344 for (i
= traverse
; i
; i
= i
->prev
) {
351 /* Assume we are doing all the modules, so only report each loop once. */
352 static void report_loop(const struct module
*mod
,
353 const struct module_traverse
*traverse
)
355 const struct module_traverse
*i
;
357 /* Check that start is least alphabetically. eg. a depends
358 on b depends on a will get reported for a, not b. */
359 for (i
= traverse
->prev
; i
->prev
; i
= i
->prev
) {
360 if (strcmp(mod
->pathname
, i
->mod
->pathname
) > 0)
364 /* Is start in the loop? If not, don't report now. eg. a
365 depends on b which depends on c which depends on b. Don't
366 report when generating depends for a. */
370 warn("Loop detected: %s ", mod
->pathname
);
371 for (i
= traverse
->prev
; i
->prev
; i
= i
->prev
)
372 fprintf(stderr
, "needs %s ", i
->mod
->basename
);
373 fprintf(stderr
, "which needs %s again!\n", i
->mod
->basename
);
376 /* This is damn slow, but loops actually happen, and we don't want to
377 just exit() and leave the user without any modules. */
378 static int has_dep_loop(struct module
*module
, struct module_traverse
*prev
)
381 struct module_traverse traverse
= { .prev
= prev
, .mod
= module
};
383 if (in_loop(module
, prev
)) {
384 report_loop(module
, &traverse
);
388 for (i
= 0; i
< module
->num_deps
; i
++)
389 if (has_dep_loop(module
->deps
[i
], &traverse
))
394 /* Uniquifies and orders a dependency list. */
395 static void order_dep_list(struct module
*start
, struct module
*mod
)
399 for (i
= 0; i
< mod
->num_deps
; i
++) {
400 /* If it was previously depended on, move it to the
401 tail. ie. if a needs b and c, and c needs b, we
402 must order b after c. */
403 list_del(&mod
->deps
[i
]->dep_list
);
404 list_add_tail(&mod
->deps
[i
]->dep_list
, &start
->dep_list
);
405 order_dep_list(start
, mod
->deps
[i
]);
409 static struct module
*deleted
= NULL
;
411 static void del_module(struct module
**modules
, struct module
*delme
)
415 /* Find pointer to it. */
417 for (i
= modules
; *i
!= delme
; i
= &(*i
)->next
);
422 /* Save on a list to quiet valgrind.
423 Can't free - other modules may depend on them */
424 delme
->next
= deleted
;
428 /* convert to relative path if possible */
429 static const char *compress_path(const char *path
, const char *basedir
)
431 int len
= strlen(basedir
);
433 if (strncmp(path
, basedir
, len
) == 0)
438 static int output_deps(struct module
*modules
,
439 FILE *out
, char *dirname
)
443 for (i
= modules
; i
; i
= i
->next
) {
444 struct list_head
*j
, *tmp
;
445 order_dep_list(i
, i
);
447 fprintf(out
, "%s:", compress_path(i
->pathname
, dirname
));
448 list_for_each_safe(j
, tmp
, &i
->dep_list
) {
450 = list_entry(j
, struct module
, dep_list
);
452 compress_path(dep
->pathname
, dirname
));
460 /* warn whenever duplicate module aliases, deps, or symbols are found. */
463 static int output_deps_bin(struct module
*modules
,
464 FILE *out
, char *dirname
)
467 struct index_node
*index
;
471 index
= index_create();
473 for (i
= modules
; i
; i
= i
->next
) {
474 struct list_head
*j
, *tmp
;
475 char modname
[strlen(i
->pathname
)+1];
477 order_dep_list(i
, i
);
479 filename2modname(modname
, i
->pathname
);
480 nofail_asprintf(&line
, "%s:",
481 compress_path(i
->pathname
, dirname
));
483 list_for_each_safe(j
, tmp
, &i
->dep_list
) {
485 = list_entry(j
, struct module
, dep_list
);
486 nofail_asprintf(&line
, "%s %s",
488 compress_path(dep
->pathname
, dirname
));
493 if (index_insert(index
, modname
, line
, i
->order
) && warn_dups
)
494 warn("duplicate module deps:\n%s\n",line
);
498 index_write(index
, out
);
499 index_destroy(index
);
505 static int smells_like_module(const char *name
)
507 return ends_in(name
,".ko") || ends_in(name
, ".ko.gz");
510 typedef struct module
*(*do_module_t
)(const char *dirname
,
511 const char *filename
,
513 struct module_search
*search
,
514 struct module_overrides
*overrides
);
516 static int is_higher_priority(const char *newpath
, const char *oldpath
,
517 struct module_search
*search
,
518 struct module_overrides
*overrides
)
520 struct module_search
*tmp
;
521 struct module_overrides
*ovtmp
;
523 int prio_builtin
= -1;
527 /* The names already match, now we check for overrides and directory search
530 for (ovtmp
= overrides
; ovtmp
!= NULL
; ovtmp
= ovtmp
->next
) {
531 if (streq(ovtmp
->modfile
, newpath
))
533 if (streq(ovtmp
->modfile
, oldpath
))
536 for (i
= 0, tmp
= search
; tmp
!= NULL
; tmp
= tmp
->next
, i
++) {
537 if (streq(tmp
->search_path
, MODULE_BUILTIN_KEY
))
539 else if (strncmp(tmp
->search_path
, newpath
, tmp
->len
) == 0)
541 else if (strncmp(tmp
->search_path
, oldpath
, tmp
->len
) == 0)
545 prio_new
= prio_builtin
;
547 prio_old
= prio_builtin
;
549 return prio_new
> prio_old
;
553 static struct module
*do_module(const char *dirname
,
554 const char *filename
,
556 struct module_search
*search
,
557 struct module_overrides
*overrides
)
559 struct module
*new, **i
;
561 new = grab_module(dirname
, filename
);
565 /* Check if module is already in the list. */
566 for (i
= &list
; *i
; i
= &(*i
)->next
) {
568 if (streq((*i
)->basename
, filename
)) {
569 char newpath
[strlen(dirname
) + strlen("/")
570 + strlen(filename
) + 1];
572 sprintf(newpath
, "%s/%s", dirname
, filename
);
574 if (is_higher_priority(newpath
, (*i
)->pathname
,search
,
581 del_module(NULL
, new);
587 /* Not in the list already. Just prepend. */
592 static struct module
*grab_dir(const char *dirname
,
596 struct module_search
*search
,
597 struct module_overrides
*overrides
)
599 struct dirent
*dirent
;
601 while ((dirent
= readdir(dir
)) != NULL
) {
602 if (smells_like_module(dirent
->d_name
))
603 next
= do_mod(dirname
, dirent
->d_name
, next
,
605 else if (!streq(dirent
->d_name
, ".")
606 && !streq(dirent
->d_name
, "..")
607 && !streq(dirent
->d_name
, "source")
608 && !streq(dirent
->d_name
, "build")) {
611 char subdir
[strlen(dirname
) + 1
612 + strlen(dirent
->d_name
) + 1];
613 sprintf(subdir
, "%s/%s", dirname
, dirent
->d_name
);
614 sub
= opendir(subdir
);
616 next
= grab_dir(subdir
, sub
, next
, do_mod
,
625 static struct module
*grab_basedir(const char *dirname
,
626 struct module_search
*search
,
627 struct module_overrides
*overrides
)
632 dir
= opendir(dirname
);
634 warn("Couldn't open directory %s: %s\n",
635 dirname
, strerror(errno
));
638 list
= grab_dir(dirname
, dir
, NULL
, do_module
, search
, overrides
);
644 static struct module
*sort_modules(const char *dirname
, struct module
*list
)
646 struct module
*tlist
= NULL
, **tpos
= &tlist
;
648 int dir_len
= strlen(dirname
) + 1;
649 char file_name
[dir_len
+ strlen("modules.order") + 1];
651 unsigned int linenum
= 0;
653 sprintf(file_name
, "%s/%s", dirname
, "modules.order");
655 modorder
= fopen(file_name
, "r");
657 /* Older kernels don't generate modules.order. Just
658 return if the file doesn't exist. */
661 fatal("Could not open '%s': %s\n", file_name
, strerror(errno
));
664 sprintf(line
, "%s/", dirname
);
666 /* move modules listed in modorder file to tlist in order */
667 while (fgets(line
, sizeof(line
), modorder
)) {
668 struct module
**pos
, *mod
;
669 int len
= strlen(line
);
672 if (line
[len
- 1] == '\n')
673 line
[len
- 1] = '\0';
675 for (pos
= &list
; (mod
= *pos
); pos
= &(*pos
)->next
) {
676 if (streq(line
, mod
->pathname
+ dir_len
)) {
677 mod
->order
= linenum
;
687 /* append the rest */
695 /* Calculate the dependencies for this module */
696 static void calculate_deps(struct module
*module
)
699 struct string_table
*symnames
;
700 struct string_table
*symtypes
;
701 uint64_t *symvers
= NULL
;
702 struct elf_file
*file
;
704 module
->num_deps
= 0;
708 symnames
= file
->ops
->load_dep_syms(file
, &symtypes
,
709 check_symvers
? &symvers
: NULL
);
710 if (!symnames
|| !symtypes
)
713 for (i
= 0; i
< symnames
->cnt
; i
++) {
716 struct module
*owner
;
719 name
= symnames
->str
[i
];
720 ver
= symvers
? symvers
[i
] : 0;
721 weak
= (*(symtypes
->str
[i
]) == 'W');
722 owner
= find_symbol(name
, ver
, module
->pathname
, weak
);
724 info("%s needs \"%s\": %s\n",
725 module
->pathname
, name
,
727 add_dep(module
, owner
);
736 static struct module
*parse_modules(struct module
*list
)
739 struct elf_file
*file
;
740 struct string_table
*syms
;
743 for (i
= list
; i
; i
= i
->next
) {
744 uint64_t *symvers
= NULL
;
746 syms
= file
->ops
->load_symbols(file
,
747 check_symvers
? &symvers
: NULL
);
749 for (j
= 0; j
< syms
->cnt
; j
++)
750 add_symbol(syms
->str
[j
],
751 symvers
? symvers
[j
] : 0, i
);
755 file
->ops
->fetch_tables(file
, &i
->tables
);
758 for (i
= list
; i
; i
= i
->next
)
761 /* Strip out modules with dependency loops. */
763 for (i
= list
; i
; i
= i
->next
) {
764 if (has_dep_loop(i
, NULL
)) {
765 warn("Module %s ignored, due to loop\n",
766 i
->pathname
+ skipchars
);
767 del_module(&list
, i
);
775 /* Simply dump hash table. */
776 static int output_symbols(struct module
*unused
, FILE *out
, char *dirname
)
780 fprintf(out
, "# Aliases for symbols, used by symbol_request().\n");
781 for (i
= 0; i
< SYMBOL_HASH_SIZE
; i
++) {
784 for (s
= symbolhash
[i
]; s
; s
= s
->next
) {
786 char modname
[strlen(s
->owner
->pathname
)+1];
787 filename2modname(modname
, s
->owner
->pathname
);
788 fprintf(out
, "alias symbol:%s %s\n",
796 static int output_symbols_bin(struct module
*unused
, FILE *out
, char *dirname
)
798 struct index_node
*index
;
803 index
= index_create();
805 for (i
= 0; i
< SYMBOL_HASH_SIZE
; i
++) {
808 for (s
= symbolhash
[i
]; s
; s
= s
->next
) {
810 char modname
[strlen(s
->owner
->pathname
)+1];
811 filename2modname(modname
, s
->owner
->pathname
);
812 nofail_asprintf(&alias
, "symbol:%s", s
->name
);
813 duplicate
= index_insert(index
, alias
, modname
,
815 if (duplicate
&& warn_dups
)
816 warn("duplicate module syms:\n%s %s\n",
823 index_write(index
, out
);
824 index_destroy(index
);
829 static int output_builtin_bin(struct module
*unused
, FILE *out
, char *dirname
)
831 struct index_node
*index
;
832 char *textfile
, *line
;
833 unsigned int linenum
;
836 nofail_asprintf(&textfile
, "%s/modules.builtin", dirname
);
837 if (!(f
= fopen(textfile
, "r"))) {
839 fatal("Could not open '%s': %s\n",
840 textfile
, strerror(errno
));
845 index
= index_create();
847 while ((line
= getline_wrapped(f
, &linenum
)) != NULL
) {
850 if (!*line
|| *line
== '#') {
854 filename2modname(module
, module
);
855 index_insert(index
, module
, "", 0);
859 index_write(index
, out
);
860 index_destroy(index
);
865 static int output_aliases(struct module
*modules
, FILE *out
, char *dirname
)
868 struct elf_file
*file
;
869 struct string_table
*tbl
;
872 fprintf(out
, "# Aliases extracted from modules themselves.\n");
873 for (i
= modules
; i
; i
= i
->next
) {
874 char modname
[strlen(i
->pathname
)+1];
877 filename2modname(modname
, i
->pathname
);
879 /* Grab from old-style .modalias section. */
880 tbl
= file
->ops
->load_strings(file
, ".modalias", NULL
, fatal
);
881 for (j
= 0; tbl
&& j
< tbl
->cnt
; j
++)
882 fprintf(out
, "alias %s %s\n", tbl
->str
[j
], modname
);
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 fprintf(out
, "alias %s %s\n",
891 p
+ strlen("alias="), modname
);
898 static int output_aliases_bin(struct module
*modules
, FILE *out
, char *dirname
)
901 struct elf_file
*file
;
902 struct string_table
*tbl
;
905 struct index_node
*index
;
908 index
= index_create();
910 for (i
= modules
; i
; i
= i
->next
) {
911 char modname
[strlen(i
->pathname
)+1];
914 filename2modname(modname
, i
->pathname
);
916 /* Grab from old-style .modalias section. */
917 tbl
= file
->ops
->load_strings(file
, ".modalias", NULL
, fatal
);
918 for (j
= 0; tbl
&& j
< tbl
->cnt
; j
++) {
919 alias
= NOFAIL(strdup(tbl
->str
[j
]));
921 duplicate
= index_insert(index
, alias
, modname
, i
->order
);
922 if (duplicate
&& warn_dups
)
923 warn("duplicate module alias:\n%s %s\n",
929 /* Grab from new-style .modinfo section. */
930 tbl
= file
->ops
->load_strings(file
, ".modinfo", NULL
, fatal
);
931 for (j
= 0; tbl
&& j
< tbl
->cnt
; j
++) {
932 const char *p
= tbl
->str
[j
];
933 if (strstarts(p
, "alias=")) {
934 alias
= NOFAIL(strdup(p
+ strlen("alias=")));
936 duplicate
= index_insert(index
, alias
, modname
, i
->order
);
937 if (duplicate
&& warn_dups
)
938 warn("duplicate module alias:\n%s %s\n",
946 index_write(index
, out
);
947 index_destroy(index
);
954 int (*func
)(struct module
*, FILE *, char *dirname
);
958 static struct depfile depfiles
[] = {
959 { "modules.dep", output_deps
, 0 }, /* This is what we check for '-A'. */
960 { "modules.dep.bin", output_deps_bin
, 0 },
961 { "modules.pcimap", output_pci_table
, 1 },
962 { "modules.usbmap", output_usb_table
, 1 },
963 { "modules.ccwmap", output_ccw_table
, 1 },
964 { "modules.ieee1394map", output_ieee1394_table
, 1 },
965 { "modules.isapnpmap", output_isapnp_table
, 1 },
966 { "modules.inputmap", output_input_table
, 1 },
967 { "modules.ofmap", output_of_table
, 1 },
968 { "modules.seriomap", output_serio_table
, 1 },
969 { "modules.alias", output_aliases
, 0 },
970 { "modules.alias.bin", output_aliases_bin
, 0 },
971 { "modules.symbols", output_symbols
, 0 },
972 { "modules.symbols.bin", output_symbols_bin
, 0 },
973 { "modules.builtin.bin", output_builtin_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
)
980 struct dirent
*dirent
;
982 dir
= opendir(dirname
);
986 while ((dirent
= readdir(dir
)) != NULL
) {
988 char file
[strlen(dirname
) + 1 + strlen(dirent
->d_name
) + 1];
990 if (streq(dirent
->d_name
, ".") || streq(dirent
->d_name
, ".."))
993 sprintf(file
, "%s/%s", dirname
, dirent
->d_name
);
994 if (lstat(file
, &st
) != 0)
997 if (smells_like_module(dirent
->d_name
)) {
998 if (st
.st_mtime
> mtime
)
1000 } else if (S_ISDIR(st
.st_mode
)) {
1001 if (any_modules_newer(file
, mtime
))
1013 static int depfile_out_of_date(const char *dirname
)
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)
1023 return any_modules_newer(dirname
, st
.st_mtime
);
1026 static char *strsep_skipspace(char **string
, char *delim
)
1030 *string
+= strspn(*string
, delim
);
1031 return strsep(string
, delim
);
1034 static struct module_search
*add_search(const char *search_path
,
1036 struct module_search
*search
)
1039 struct module_search
*new;
1041 new = NOFAIL(malloc(sizeof(*new)));
1042 new->search_path
= NOFAIL(strdup(search_path
));
1050 static struct module_overrides
*add_override(const char *modfile
,
1051 struct module_overrides
*overrides
)
1054 struct module_overrides
*new;
1056 new = NOFAIL(malloc(sizeof(*new)));
1057 new->modfile
= NOFAIL(strdup(modfile
));
1058 new->next
= overrides
;
1064 static int parse_config_scan(const char *filename
,
1065 const char *basedir
,
1066 const char *kernelversion
,
1067 struct module_search
**search
,
1068 struct module_overrides
**overrides
);
1070 static int parse_config_file(const char *filename
,
1071 const char *basedir
,
1072 const char *kernelversion
,
1073 struct module_search
**search
,
1074 struct module_overrides
**overrides
)
1077 unsigned int linenum
= 0;
1080 cfile
= fopen(filename
, "r");
1082 if (errno
!= ENOENT
)
1083 fatal("could not open '%s', reason: %s\n", filename
,
1088 while ((line
= getline_wrapped(cfile
, &linenum
)) != NULL
) {
1090 char *cmd
, *modname
;
1092 cmd
= strsep_skipspace(&ptr
, "\t ");
1094 if (cmd
== NULL
|| cmd
[0] == '#' || cmd
[0] == '\0') {
1099 if (streq(cmd
, "search")) {
1102 while ((search_path
= strsep_skipspace(&ptr
, "\t "))) {
1106 if (strcmp(search_path
,
1107 MODULE_BUILTIN_KEY
) == 0) {
1108 *search
= add_search(MODULE_BUILTIN_KEY
,
1112 nofail_asprintf(&dirname
, "%s%s%s/%s", basedir
,
1113 MODULE_DIR
, kernelversion
, search_path
);
1114 len
= strlen(dirname
);
1115 *search
= add_search(dirname
, len
, *search
);
1118 } else if (streq(cmd
, "override")) {
1119 char *pathname
= NULL
, *version
, *subdir
;
1120 modname
= strsep_skipspace(&ptr
, "\t ");
1121 version
= strsep_skipspace(&ptr
, "\t ");
1122 subdir
= strsep_skipspace(&ptr
, "\t ");
1124 if (!regex_match(kernelversion
, (const char *)version
))
1127 nofail_asprintf(&pathname
, "%s%s%s/%s/%s.ko", basedir
,
1128 MODULE_DIR
, kernelversion
, subdir
, modname
);
1130 *overrides
= add_override(pathname
, *overrides
);
1132 } else if (streq(cmd
, "include")) {
1135 newfilename
= strsep_skipspace(&ptr
, "\t ");
1137 grammar(cmd
, filename
, linenum
);
1139 warn("\"include %s\" is deprecated, "
1140 "please use /etc/depmod.d\n", newfilename
);
1141 if (strstarts(newfilename
, "/etc/depmod.d")) {
1142 warn("\"include /etc/depmod.d\" is "
1143 "the default, ignored\n");
1145 if (!parse_config_scan(newfilename
, basedir
,
1148 warn("Failed to open included"
1149 " config file %s: %s\n",
1150 newfilename
, strerror(errno
));
1153 } else if (streq(cmd
, "make_map_files")) {
1156 option
= strsep_skipspace(&ptr
, "\t ");
1158 grammar(cmd
, filename
, linenum
);
1160 if (streq(option
, "yes"))
1162 else if (streq(option
, "no"))
1165 grammar(cmd
, filename
, linenum
);
1168 grammar(cmd
, filename
, linenum
);
1176 static int parse_config_scan(const char *filename
,
1177 const char *basedir
,
1178 const char *kernelversion
,
1179 struct module_search
**search
,
1180 struct module_overrides
**overrides
)
1185 dir
= opendir(filename
);
1188 struct list_head node
;
1191 LIST_HEAD(files_list
);
1192 struct file_entry
*fe
, *fe_tmp
;
1195 /* sort files from directory into list */
1196 while ((i
= readdir(dir
)) != NULL
) {
1199 if (i
->d_name
[0] == '.')
1201 if (!config_filter(i
->d_name
))
1204 len
= strlen(i
->d_name
);
1205 if (len
< 6 || strcmp(&i
->d_name
[len
-5], ".conf") != 0)
1206 warn("All config files need .conf: %s/%s, "
1207 "it will be ignored in a future release.\n",
1208 filename
, i
->d_name
);
1209 fe
= malloc(sizeof(struct file_entry
) + len
+ 1);
1212 strcpy(fe
->name
, i
->d_name
);
1213 list_for_each_entry(fe_tmp
, &files_list
, node
)
1214 if (strcmp(fe_tmp
->name
, fe
->name
) >= 0)
1216 list_add_tail(&fe
->node
, &fe_tmp
->node
);
1220 /* parse list of files */
1221 list_for_each_entry_safe(fe
, fe_tmp
, &files_list
, node
) {
1224 nofail_asprintf(&cfgfile
, "%s/%s", filename
, fe
->name
);
1225 if (!parse_config_file(cfgfile
, basedir
, kernelversion
,
1227 warn("Failed to open config file "
1228 "%s: %s\n", fe
->name
, strerror(errno
));
1230 list_del(&fe
->node
);
1236 if (parse_config_file(filename
, basedir
, kernelversion
, search
,
1244 static void parse_toplevel_config(const char *filename
,
1245 const char *basedir
,
1246 const char *kernelversion
,
1247 struct module_search
**search
,
1248 struct module_overrides
**overrides
)
1251 if (!parse_config_scan(filename
, basedir
, kernelversion
, search
,
1253 fatal("Failed to open config file %s: %s\n",
1254 filename
, strerror(errno
));
1258 /* deprecated config file */
1259 if (parse_config_file("/etc/depmod.conf", basedir
, kernelversion
,
1260 search
, overrides
) > 0)
1261 warn("Deprecated config file /etc/depmod.conf, "
1262 "all config files belong into /etc/depmod.d/.\n");
1264 /* default config */
1265 parse_config_scan("/etc/depmod.d", basedir
, kernelversion
,
1269 /* Local to main, but not freed on exit. Keep valgrind quiet. */
1270 struct module
*list
= NULL
;
1271 struct module_search
*search
= NULL
;
1272 struct module_overrides
*overrides
= NULL
;
1274 int main(int argc
, char *argv
[])
1276 int opt
, all
= 0, maybe_all
= 0, doing_stdout
= 0;
1277 char *basedir
= "", *dirname
, *version
;
1278 char *system_map
= NULL
, *module_symvers
= NULL
;
1280 const char *config
= NULL
;
1282 if (native_endianness() == 0)
1285 while ((opt
= getopt_long(argc
, argv
, "aAb:C:E:F:euqrvnhVwm", options
, NULL
))
1296 skipchars
= strlen(basedir
);
1302 module_symvers
= optarg
;
1306 system_map
= optarg
;
1322 print_usage(argv
[0]);
1326 printf("%s %s\n", PACKAGE
, VERSION
);
1332 force_map_files
= 1;
1335 print_usage(argv
[0]);
1341 load_module_symvers(module_symvers
);
1342 else if (system_map
)
1343 load_system_map(system_map
);
1344 else if (print_unknown
) {
1345 warn("-e needs -E or -F");
1349 /* They can specify the version naked on the command line */
1350 if (optind
< argc
&& is_version_number(argv
[optind
])) {
1351 version
= NOFAIL(strdup(argv
[optind
]));
1356 version
= NOFAIL(strdup(buf
.release
));
1359 /* Check for old version. */
1360 if (old_module_version(version
)) {
1361 fprintf(stderr
, "Kernel version %s requires old depmod\n",
1366 /* Depmod -a by default if no names. */
1370 nofail_asprintf(&dirname
, "%s%s%s", basedir
, MODULE_DIR
, version
);
1373 if (!doing_stdout
&& !depfile_out_of_date(dirname
))
1378 parse_toplevel_config(config
, basedir
, version
, &search
, &overrides
);
1380 /* For backward compatibility add "updates" to the head of the search
1381 * list here. But only if there was no "search" option specified.
1387 nofail_asprintf(&dirname
, "%s%s%s/updates", basedir
,
1388 MODULE_DIR
, version
);
1389 len
= strlen(dirname
);
1390 search
= add_search(dirname
, len
, search
);
1393 /* Do command line args. */
1394 for (opt
= optind
; opt
< argc
; opt
++) {
1397 if (argv
[opt
][0] != '/')
1398 fatal("modules must be specified using absolute paths.\n"
1399 "\"%s\" is a relative path\n", argv
[opt
]);
1401 new = grab_module(NULL
, argv
[opt
]);
1403 /* cmd-line specified modules must exist */
1404 fatal("grab_module() failed for module %s\n", argv
[opt
]);
1410 list
= grab_basedir(dirname
,search
,overrides
);
1412 list
= sort_modules(dirname
,list
);
1413 list
= parse_modules(list
);
1415 for (i
= 0; i
< sizeof(depfiles
)/sizeof(depfiles
[0]); i
++) {
1418 struct depfile
*d
= &depfiles
[i
];
1419 char depname
[strlen(dirname
) + 1 + strlen(d
->name
) + 1];
1420 char tmpname
[strlen(dirname
) + 1 + strlen(d
->name
) +
1421 strlen(".temp") + 1];
1423 if (d
->map_file
&& !make_map_files
&& !force_map_files
)
1426 sprintf(depname
, "%s/%s", dirname
, d
->name
);
1427 sprintf(tmpname
, "%s/%s.temp", dirname
, d
->name
);
1428 if (!doing_stdout
) {
1429 out
= fopen(tmpname
, "w");
1431 fatal("Could not open %s for writing: %s\n",
1432 tmpname
, strerror(errno
));
1435 if (ends_in(depname
, ".bin"))
1438 res
= d
->func(list
, out
, dirname
);
1443 if (rename(tmpname
, depname
) < 0)
1444 fatal("Could not rename %s into %s: %s\n",
1445 tmpname
, depname
, strerror(errno
));
1447 if (unlink(tmpname
) < 0)
1448 warn("Could not delete %s: %s\n",
1449 tmpname
, strerror(errno
));