2 * New simplified depmod without backwards compat stuff and not
5 * (C) 2010 Jon Masters <jcm@jonmasters.org>, and others.
6 * (C) 2002 Rusty Russell IBM Corporation
8 #define _GNU_SOURCE /* asprintf */
17 #include <sys/types.h>
21 #include <sys/utsname.h>
24 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
27 #include "zlibsupport.h"
33 #include "config_filter.h"
38 #define MODULE_DIR "/lib/modules/"
41 #ifndef MODULE_BUILTIN_KEY
42 #define MODULE_BUILTIN_KEY "built-in"
45 struct module_overrides
48 struct module_overrides
*next
;
50 /* overridden module */
57 struct module_search
*next
;
64 static unsigned int skipchars
;
65 static unsigned int make_map_files
= 1; /* default to on */
66 static unsigned int force_map_files
= 0; /* default to on */
68 #define SYMBOL_HASH_SIZE 1024
77 static struct symbol
*symbolhash
[SYMBOL_HASH_SIZE
];
79 /* This is based on the hash agorithm from gdbm, via tdb */
80 static inline unsigned int tdb_hash(const char *name
)
82 unsigned value
; /* Used to compute the hash value. */
83 unsigned i
; /* Used to cycle through random values. */
85 /* Set the initial value from the key size. */
86 for (value
= 0x238F13AF * strlen(name
), i
=0; name
[i
]; i
++)
87 value
= (value
+ (((unsigned char *)name
)[i
] << (i
*5 % 24)));
89 return (1103515243 * value
+ 12345);
92 void add_symbol(const char *name
, uint64_t ver
, struct module
*owner
)
95 struct symbol
*new = NOFAIL(malloc(sizeof *new + strlen(name
) + 1));
99 strcpy(new->name
, name
);
101 hash
= tdb_hash(name
) % SYMBOL_HASH_SIZE
;
102 new->next
= symbolhash
[hash
];
103 symbolhash
[hash
] = new;
106 static int print_unknown
, check_symvers
;
108 struct module
*find_symbol(const char *name
, uint64_t ver
,
109 const char *modname
, int weak
)
113 /* For our purposes, .foo matches foo. PPC64 needs this. */
117 for (s
= symbolhash
[tdb_hash(name
) % SYMBOL_HASH_SIZE
]; s
; s
=s
->next
) {
118 if (streq(s
->name
, name
))
122 if (ver
&& s
->ver
&& s
->ver
!= ver
&& print_unknown
&& !weak
)
123 warn("%s disagrees about version of symbol %s\n",
128 if (print_unknown
&& !weak
)
129 warn("%s needs unknown symbol %s\n", modname
, name
);
134 void add_dep(struct module
*mod
, struct module
*depends_on
)
138 for (i
= 0; i
< mod
->num_deps
; i
++)
139 if (mod
->deps
[i
] == depends_on
)
142 mod
->deps
= NOFAIL(realloc(mod
->deps
, sizeof(mod
->deps
[0])*(mod
->num_deps
+1)));
143 mod
->deps
[mod
->num_deps
++] = depends_on
;
146 static void add_fake_syms(void)
148 /* __this_module is magic inserted by kernel loader. */
149 add_symbol("__this_module", 0, NULL
);
150 /* On S390, this is faked up too */
151 add_symbol("_GLOBAL_OFFSET_TABLE_", 0, NULL
);
154 static void load_system_map(const char *filename
)
158 const char ksymstr
[] = "__ksymtab_";
159 const int ksymstr_len
= strlen(ksymstr
);
161 system_map
= fopen(filename
, "r");
163 fatal("Could not open '%s': %s\n", filename
, strerror(errno
));
165 /* eg. c0294200 R __ksymtab_devfs_alloc_devnum */
166 while (fgets(line
, sizeof(line
)-1, system_map
)) {
170 ptr
= strchr(line
, '\n');
174 ptr
= strchr(line
, ' ');
175 if (!ptr
|| !(ptr
= strchr(ptr
+ 1, ' ')))
178 /* Covers gpl-only and normal symbols. */
179 if (strstarts(ptr
+1, ksymstr
))
180 add_symbol(ptr
+1+ksymstr_len
, 0, NULL
);
187 static void load_module_symvers(const char *filename
)
189 FILE *module_symvers
;
192 module_symvers
= fopen(filename
, "r");
194 fatal("Could not open '%s': %s\n", filename
, strerror(errno
));
196 /* eg. "0xb352177e\tfind_first_bit\tvmlinux\tEXPORT_SYMBOL" */
197 while (fgets(line
, sizeof(line
)-1, module_symvers
)) {
198 const char *ver
, *sym
, *where
;
200 ver
= strtok(line
, " \t");
201 sym
= strtok(NULL
, " \t");
202 where
= strtok(NULL
, " \t");
203 if (!ver
|| !sym
|| !where
)
206 if (streq(where
, "vmlinux"))
207 add_symbol(sym
, strtoull(ver
, NULL
, 16), NULL
);
210 fclose(module_symvers
);
214 static struct option options
[] = { { "all", 0, NULL
, 'a' },
215 { "quick", 0, NULL
, 'A' },
216 { "basedir", 1, NULL
, 'b' },
217 { "config", 1, NULL
, 'C' },
218 { "symvers", 1, NULL
, 'E' },
219 { "filesyms", 1, NULL
, 'F' },
220 { "errsyms", 0, NULL
, 'e' },
221 { "unresolved-error", 0, NULL
, 'u' },
222 { "quiet", 0, NULL
, 'q' },
223 { "root", 0, NULL
, 'r' },
224 { "verbose", 0, NULL
, 'v' },
225 { "show", 0, NULL
, 'n' },
226 { "dry-run", 0, NULL
, 'n' },
227 { "help", 0, NULL
, 'h' },
228 { "version", 0, NULL
, 'V' },
229 { "warn", 0, NULL
, 'w' },
230 { "map", 0, NULL
, 'm' },
231 { NULL
, 0, NULL
, 0 } };
233 /* Version number or module name? Don't assume extension. */
234 static int is_version_number(const char *version
)
238 return (sscanf(version
, "%u.%u.%u", &dummy
, &dummy
, &dummy
) == 3);
241 static int old_module_version(const char *version
)
243 /* Expect three part version. */
244 unsigned int major
, sub
, minor
;
246 sscanf(version
, "%u.%u.%u", &major
, &sub
, &minor
);
248 if (major
> 2) return 0;
249 if (major
< 2) return 1;
252 if (sub
> 5) return 0;
253 if (sub
< 5) return 1;
256 if (minor
>= 48) return 0;
260 static void print_usage(const char *name
)
263 "%s " VERSION
" -- part of " PACKAGE
"\n"
264 "%s -[aA] [-n -e -v -q -V -r -u -w -m]\n"
265 " [-b basedirectory] [forced_version]\n"
266 "depmod [-n -e -v -q -r -u -w] [-F kernelsyms] module1.ko module2.ko ...\n"
267 "If no arguments (except options) are given, \"depmod -a\" is assumed\n"
269 "depmod will output a dependancy list suitable for the modprobe utility.\n"
273 "\t-a, --all Probe all modules\n"
274 "\t-A, --quick Only does the work if there's a new module\n"
275 "\t-e, --errsyms Report not supplied symbols\n"
276 "\t-m, --map Create the legacy map files\n"
277 "\t-n, --show Write the dependency file on stdout only\n"
278 "\t-V, --version Print the release version\n"
279 "\t-v, --verbose Enable verbose mode\n"
280 "\t-w, --warn Warn on duplicates\n"
281 "\t-h, --help Print this usage message\n"
283 "The following options are useful for people managing distributions:\n"
284 "\t-b basedirectory\n"
285 "\t --basedir basedirectory Use an image of a module tree.\n"
287 "\t --filesyms kernelsyms Use the file instead of the\n"
288 "\t current kernel symbols.\n"
289 "\t-E Module.symvers\n"
290 "\t --symvers Module.symvers Use Module.symvers file to check\n"
291 "\t symbol versions.\n",
295 static int ends_in(const char *name
, const char *ext
)
297 unsigned int namelen
, extlen
;
300 namelen
= strlen(name
);
301 extlen
= strlen(ext
);
303 if (namelen
< extlen
) return 0;
305 if (streq(name
+ namelen
- extlen
, ext
))
310 static struct module
*grab_module(const char *dirname
, const char *filename
)
314 new = NOFAIL(malloc(sizeof(*new)
315 + strlen(dirname
?:"") + 1 + strlen(filename
) + 1));
317 sprintf(new->pathname
, "%s/%s", dirname
, filename
);
319 strcpy(new->pathname
, filename
);
320 new->basename
= my_basename(new->pathname
);
322 INIT_LIST_HEAD(&new->dep_list
);
323 new->order
= INDEX_PRIORITY_MIN
;
325 new->file
= grab_elf_file(new->pathname
);
327 warn("Can't read module %s: %s\n",
328 new->pathname
, strerror(errno
));
335 struct module_traverse
337 struct module_traverse
*prev
;
341 static int in_loop(struct module
*mod
, const struct module_traverse
*traverse
)
343 const struct module_traverse
*i
;
345 for (i
= traverse
; i
; i
= i
->prev
) {
352 /* Assume we are doing all the modules, so only report each loop once. */
353 static void report_loop(const struct module
*mod
,
354 const struct module_traverse
*traverse
)
356 const struct module_traverse
*i
;
358 /* Check that start is least alphabetically. eg. a depends
359 on b depends on a will get reported for a, not b. */
360 for (i
= traverse
->prev
; i
->prev
; i
= i
->prev
) {
361 if (strcmp(mod
->pathname
, i
->mod
->pathname
) > 0)
365 /* Is start in the loop? If not, don't report now. eg. a
366 depends on b which depends on c which depends on b. Don't
367 report when generating depends for a. */
371 warn("Loop detected: %s ", mod
->pathname
);
372 for (i
= traverse
->prev
; i
->prev
; i
= i
->prev
)
373 fprintf(stderr
, "needs %s ", i
->mod
->basename
);
374 fprintf(stderr
, "which needs %s again!\n", i
->mod
->basename
);
377 /* This is damn slow, but loops actually happen, and we don't want to
378 just exit() and leave the user without any modules. */
379 static int has_dep_loop(struct module
*module
, struct module_traverse
*prev
)
382 struct module_traverse traverse
= { .prev
= prev
, .mod
= module
};
384 if (in_loop(module
, prev
)) {
385 report_loop(module
, &traverse
);
389 for (i
= 0; i
< module
->num_deps
; i
++)
390 if (has_dep_loop(module
->deps
[i
], &traverse
))
395 /* Uniquifies and orders a dependency list. */
396 static void order_dep_list(struct module
*start
, struct module
*mod
)
400 for (i
= 0; i
< mod
->num_deps
; i
++) {
401 /* If it was previously depended on, move it to the
402 tail. ie. if a needs b and c, and c needs b, we
403 must order b after c. */
404 list_del(&mod
->deps
[i
]->dep_list
);
405 list_add_tail(&mod
->deps
[i
]->dep_list
, &start
->dep_list
);
406 order_dep_list(start
, mod
->deps
[i
]);
410 static struct module
*deleted
= NULL
;
412 static void del_module(struct module
**modules
, struct module
*delme
)
416 /* Find pointer to it. */
418 for (i
= modules
; *i
!= delme
; i
= &(*i
)->next
);
423 /* Save on a list to quiet valgrind.
424 Can't free - other modules may depend on them */
425 delme
->next
= deleted
;
429 /* convert to relative path if possible */
430 static const char *compress_path(const char *path
, const char *basedir
)
432 int len
= strlen(basedir
);
434 if (strncmp(path
, basedir
, len
) == 0)
439 static int output_deps(struct module
*modules
,
440 FILE *out
, char *dirname
)
444 for (i
= modules
; i
; i
= i
->next
) {
445 struct list_head
*j
, *tmp
;
446 order_dep_list(i
, i
);
448 fprintf(out
, "%s:", compress_path(i
->pathname
, dirname
));
449 list_for_each_safe(j
, tmp
, &i
->dep_list
) {
451 = list_entry(j
, struct module
, dep_list
);
453 compress_path(dep
->pathname
, dirname
));
461 /* warn whenever duplicate module aliases, deps, or symbols are found. */
464 static int output_deps_bin(struct module
*modules
,
465 FILE *out
, char *dirname
)
468 struct index_node
*index
;
472 index
= index_create();
474 for (i
= modules
; i
; i
= i
->next
) {
475 struct list_head
*j
, *tmp
;
476 char modname
[strlen(i
->pathname
)+1];
478 order_dep_list(i
, i
);
480 filename2modname(modname
, i
->pathname
);
481 nofail_asprintf(&line
, "%s:",
482 compress_path(i
->pathname
, dirname
));
484 list_for_each_safe(j
, tmp
, &i
->dep_list
) {
486 = list_entry(j
, struct module
, dep_list
);
487 nofail_asprintf(&line
, "%s %s",
489 compress_path(dep
->pathname
, dirname
));
494 if (index_insert(index
, modname
, line
, i
->order
) && warn_dups
)
495 warn("duplicate module deps:\n%s\n",line
);
499 index_write(index
, out
);
500 index_destroy(index
);
506 static int smells_like_module(const char *name
)
508 return ends_in(name
,".ko") || ends_in(name
, ".ko.gz");
511 typedef struct module
*(*do_module_t
)(const char *dirname
,
512 const char *filename
,
514 struct module_search
*search
,
515 struct module_overrides
*overrides
);
517 static int is_higher_priority(const char *newpath
, const char *oldpath
,
518 struct module_search
*search
,
519 struct module_overrides
*overrides
)
521 struct module_search
*tmp
;
522 struct module_overrides
*ovtmp
;
524 int prio_builtin
= -1;
528 /* The names already match, now we check for overrides and directory search
531 for (ovtmp
= overrides
; ovtmp
!= NULL
; ovtmp
= ovtmp
->next
) {
532 if (streq(ovtmp
->modfile
, newpath
))
534 if (streq(ovtmp
->modfile
, oldpath
))
537 for (i
= 0, tmp
= search
; tmp
!= NULL
; tmp
= tmp
->next
, i
++) {
538 if (streq(tmp
->search_path
, MODULE_BUILTIN_KEY
))
540 else if (strncmp(tmp
->search_path
, newpath
, tmp
->len
) == 0)
542 else if (strncmp(tmp
->search_path
, oldpath
, tmp
->len
) == 0)
546 prio_new
= prio_builtin
;
548 prio_old
= prio_builtin
;
550 return prio_new
> prio_old
;
554 static struct module
*do_module(const char *dirname
,
555 const char *filename
,
557 struct module_search
*search
,
558 struct module_overrides
*overrides
)
560 struct module
*new, **i
;
562 new = grab_module(dirname
, filename
);
566 /* Check if module is already in the list. */
567 for (i
= &list
; *i
; i
= &(*i
)->next
) {
569 if (streq((*i
)->basename
, filename
)) {
570 char newpath
[strlen(dirname
) + strlen("/")
571 + strlen(filename
) + 1];
573 sprintf(newpath
, "%s/%s", dirname
, filename
);
575 if (is_higher_priority(newpath
, (*i
)->pathname
,search
,
582 del_module(NULL
, new);
588 /* Not in the list already. Just prepend. */
593 static struct module
*grab_dir(const char *dirname
,
597 struct module_search
*search
,
598 struct module_overrides
*overrides
)
600 struct dirent
*dirent
;
602 while ((dirent
= readdir(dir
)) != NULL
) {
603 if (smells_like_module(dirent
->d_name
))
604 next
= do_mod(dirname
, dirent
->d_name
, next
,
606 else if (!streq(dirent
->d_name
, ".")
607 && !streq(dirent
->d_name
, "..")
608 && !streq(dirent
->d_name
, "source")
609 && !streq(dirent
->d_name
, "build")) {
612 char subdir
[strlen(dirname
) + 1
613 + strlen(dirent
->d_name
) + 1];
614 sprintf(subdir
, "%s/%s", dirname
, dirent
->d_name
);
615 sub
= opendir(subdir
);
617 next
= grab_dir(subdir
, sub
, next
, do_mod
,
626 static struct module
*grab_basedir(const char *dirname
,
627 struct module_search
*search
,
628 struct module_overrides
*overrides
)
633 dir
= opendir(dirname
);
635 warn("Couldn't open directory %s: %s\n",
636 dirname
, strerror(errno
));
639 list
= grab_dir(dirname
, dir
, NULL
, do_module
, search
, overrides
);
645 static struct module
*sort_modules(const char *dirname
, struct module
*list
)
647 struct module
*tlist
= NULL
, **tpos
= &tlist
;
649 int dir_len
= strlen(dirname
) + 1;
650 char file_name
[dir_len
+ strlen("modules.order") + 1];
652 unsigned int linenum
= 0;
654 sprintf(file_name
, "%s/%s", dirname
, "modules.order");
656 modorder
= fopen(file_name
, "r");
658 /* Older kernels don't generate modules.order. Just
659 return if the file doesn't exist. */
662 fatal("Could not open '%s': %s\n", file_name
, strerror(errno
));
665 sprintf(line
, "%s/", dirname
);
667 /* move modules listed in modorder file to tlist in order */
668 while (fgets(line
, sizeof(line
), modorder
)) {
669 struct module
**pos
, *mod
;
670 int len
= strlen(line
);
673 if (line
[len
- 1] == '\n')
674 line
[len
- 1] = '\0';
676 for (pos
= &list
; (mod
= *pos
); pos
= &(*pos
)->next
) {
677 if (streq(line
, mod
->pathname
+ dir_len
)) {
678 mod
->order
= linenum
;
688 /* append the rest */
696 /* Calculate the dependencies for this module */
697 static void calculate_deps(struct module
*module
)
700 struct string_table
*symnames
;
701 struct string_table
*symtypes
;
702 uint64_t *symvers
= NULL
;
703 struct elf_file
*file
;
705 module
->num_deps
= 0;
709 symnames
= file
->ops
->load_dep_syms(file
, &symtypes
,
710 check_symvers
? &symvers
: NULL
);
711 if (!symnames
|| !symtypes
)
714 for (i
= 0; i
< symnames
->cnt
; i
++) {
717 struct module
*owner
;
720 name
= symnames
->str
[i
];
721 ver
= symvers
? symvers
[i
] : 0;
722 weak
= (*(symtypes
->str
[i
]) == 'W');
723 owner
= find_symbol(name
, ver
, module
->pathname
, weak
);
725 info("%s needs \"%s\": %s\n",
726 module
->pathname
, name
,
728 add_dep(module
, owner
);
737 static struct module
*parse_modules(struct module
*list
)
740 struct elf_file
*file
;
741 struct string_table
*syms
;
744 for (i
= list
; i
; i
= i
->next
) {
745 uint64_t *symvers
= NULL
;
747 syms
= file
->ops
->load_symbols(file
,
748 check_symvers
? &symvers
: NULL
);
750 for (j
= 0; j
< syms
->cnt
; j
++)
751 add_symbol(syms
->str
[j
],
752 symvers
? symvers
[j
] : 0, i
);
756 file
->ops
->fetch_tables(file
, &i
->tables
);
759 for (i
= list
; i
; i
= i
->next
)
762 /* Strip out modules with dependency loops. */
764 for (i
= list
; i
; i
= i
->next
) {
765 if (has_dep_loop(i
, NULL
)) {
766 warn("Module %s ignored, due to loop\n",
767 i
->pathname
+ skipchars
);
768 del_module(&list
, i
);
776 /* Simply dump hash table. */
777 static int output_symbols(struct module
*unused
, FILE *out
, char *dirname
)
781 fprintf(out
, "# Aliases for symbols, used by symbol_request().\n");
782 for (i
= 0; i
< SYMBOL_HASH_SIZE
; i
++) {
785 for (s
= symbolhash
[i
]; s
; s
= s
->next
) {
787 char modname
[strlen(s
->owner
->pathname
)+1];
788 filename2modname(modname
, s
->owner
->pathname
);
789 fprintf(out
, "alias symbol:%s %s\n",
797 static int output_symbols_bin(struct module
*unused
, FILE *out
, char *dirname
)
799 struct index_node
*index
;
804 index
= index_create();
806 for (i
= 0; i
< SYMBOL_HASH_SIZE
; i
++) {
809 for (s
= symbolhash
[i
]; s
; s
= s
->next
) {
811 char modname
[strlen(s
->owner
->pathname
)+1];
812 filename2modname(modname
, s
->owner
->pathname
);
813 nofail_asprintf(&alias
, "symbol:%s", s
->name
);
814 duplicate
= index_insert(index
, alias
, modname
,
816 if (duplicate
&& warn_dups
)
817 warn("duplicate module syms:\n%s %s\n",
824 index_write(index
, out
);
825 index_destroy(index
);
830 static int output_builtin_bin(struct module
*unused
, FILE *out
, char *dirname
)
832 struct index_node
*index
;
833 char *textfile
, *line
;
834 unsigned int linenum
;
837 nofail_asprintf(&textfile
, "%s/modules.builtin", dirname
);
838 if (!(f
= fopen(textfile
, "r"))) {
840 fatal("Could not open '%s': %s\n",
841 textfile
, strerror(errno
));
846 index
= index_create();
848 while ((line
= getline_wrapped(f
, &linenum
)) != NULL
) {
851 if (!*line
|| *line
== '#') {
855 filename2modname(module
, module
);
856 index_insert(index
, module
, "", 0);
860 index_write(index
, out
);
861 index_destroy(index
);
866 static int output_aliases(struct module
*modules
, FILE *out
, char *dirname
)
869 struct elf_file
*file
;
870 struct string_table
*tbl
;
873 fprintf(out
, "# Aliases extracted from modules themselves.\n");
874 for (i
= modules
; i
; i
= i
->next
) {
875 char modname
[strlen(i
->pathname
)+1];
878 filename2modname(modname
, i
->pathname
);
880 /* Grab from old-style .modalias section. */
881 tbl
= file
->ops
->load_strings(file
, ".modalias", NULL
);
882 for (j
= 0; tbl
&& j
< tbl
->cnt
; j
++)
883 fprintf(out
, "alias %s %s\n", tbl
->str
[j
], modname
);
886 /* Grab from new-style .modinfo section. */
887 tbl
= file
->ops
->load_strings(file
, ".modinfo", NULL
);
888 for (j
= 0; tbl
&& j
< tbl
->cnt
; j
++) {
889 const char *p
= tbl
->str
[j
];
890 if (strstarts(p
, "alias="))
891 fprintf(out
, "alias %s %s\n",
892 p
+ strlen("alias="), modname
);
899 static int output_aliases_bin(struct module
*modules
, FILE *out
, char *dirname
)
902 struct elf_file
*file
;
903 struct string_table
*tbl
;
906 struct index_node
*index
;
909 index
= index_create();
911 for (i
= modules
; i
; i
= i
->next
) {
912 char modname
[strlen(i
->pathname
)+1];
915 filename2modname(modname
, i
->pathname
);
917 /* Grab from old-style .modalias section. */
918 tbl
= file
->ops
->load_strings(file
, ".modalias", NULL
);
919 for (j
= 0; tbl
&& j
< tbl
->cnt
; j
++) {
920 alias
= NOFAIL(strdup(tbl
->str
[j
]));
922 duplicate
= index_insert(index
, alias
, modname
, i
->order
);
923 if (duplicate
&& warn_dups
)
924 warn("duplicate module alias:\n%s %s\n",
930 /* Grab from new-style .modinfo section. */
931 tbl
= file
->ops
->load_strings(file
, ".modinfo", NULL
);
932 for (j
= 0; tbl
&& j
< tbl
->cnt
; j
++) {
933 const char *p
= tbl
->str
[j
];
934 if (strstarts(p
, "alias=")) {
935 alias
= NOFAIL(strdup(p
+ strlen("alias=")));
937 duplicate
= index_insert(index
, alias
, modname
, i
->order
);
938 if (duplicate
&& warn_dups
)
939 warn("duplicate module alias:\n%s %s\n",
947 index_write(index
, out
);
948 index_destroy(index
);
953 static int output_softdeps(struct module
*modules
, FILE *out
, char *dirname
)
956 struct elf_file
*file
;
957 struct string_table
*tbl
;
960 fprintf(out
, "# Soft dependencies extracted from modules themselves.\n");
961 fprintf(out
, "# Copy, with a .conf extension, to /etc/modprobe.d to use "
962 "it with modprobe.\n");
963 for (i
= modules
; i
; i
= i
->next
) {
964 char modname
[strlen(i
->pathname
)+1];
967 filename2modname(modname
, i
->pathname
);
969 /* Grab from new-style .modinfo section. */
970 tbl
= file
->ops
->load_strings(file
, ".modinfo", NULL
);
971 for (j
= 0; tbl
&& j
< tbl
->cnt
; j
++) {
972 const char *p
= tbl
->str
[j
];
973 if (strstarts(p
, "softdep="))
974 fprintf(out
, "softdep %s %s\n",
975 modname
, p
+ strlen("softdep="));
982 static int output_devname(struct module
*modules
, FILE *out
, char *dirname
)
986 fprintf(out
, "# Device nodes to trigger on-demand module loading.\n");
987 for (m
= modules
; m
!= NULL
; m
= m
->next
) {
988 struct string_table
*tbl
;
991 const char *devname
= NULL
;
993 tbl
= m
->file
->ops
->load_strings(m
->file
, ".modinfo", NULL
);
994 for (i
= 0; tbl
&& i
< tbl
->cnt
; i
++) {
995 const char *p
= tbl
->str
[i
];
996 unsigned int maj
, min
;
998 if (sscanf(p
, "alias=char-major-%u-%u", &maj
, &min
) == 2)
1000 else if (sscanf(p
, "alias=block-major-%u-%u", &maj
, &min
) == 2)
1002 else if (strstarts(p
, "alias=devname:"))
1003 devname
= &p
[strlen("alias=devname:")];
1005 if (type
&& devname
) {
1006 char modname
[strlen(m
->pathname
)+1];
1008 filename2modname(modname
, m
->pathname
);
1009 fprintf(out
, "%s %s %c%u:%u\n",
1010 modname
, devname
, type
, maj
, min
);
1021 int (*func
)(struct module
*, FILE *, char *dirname
);
1025 static struct depfile depfiles
[] = {
1026 { "modules.dep", output_deps
, 0 }, /* This is what we check for '-A'. */
1027 { "modules.dep.bin", output_deps_bin
, 0 },
1028 { "modules.pcimap", output_pci_table
, 1 },
1029 { "modules.usbmap", output_usb_table
, 1 },
1030 { "modules.ccwmap", output_ccw_table
, 1 },
1031 { "modules.ieee1394map", output_ieee1394_table
, 1 },
1032 { "modules.isapnpmap", output_isapnp_table
, 1 },
1033 { "modules.inputmap", output_input_table
, 1 },
1034 { "modules.ofmap", output_of_table
, 1 },
1035 { "modules.seriomap", output_serio_table
, 1 },
1036 { "modules.alias", output_aliases
, 0 },
1037 { "modules.alias.bin", output_aliases_bin
, 0 },
1038 { "modules.softdep", output_softdeps
, 0 },
1039 { "modules.symbols", output_symbols
, 0 },
1040 { "modules.symbols.bin", output_symbols_bin
, 0 },
1041 { "modules.builtin.bin", output_builtin_bin
, 0 },
1042 { "modules.devname", output_devname
, 0 },
1045 /* If we can't figure it out, it's safe to say "true". */
1046 static int any_modules_newer(const char *dirname
, time_t mtime
)
1049 struct dirent
*dirent
;
1051 dir
= opendir(dirname
);
1055 while ((dirent
= readdir(dir
)) != NULL
) {
1057 char file
[strlen(dirname
) + 1 + strlen(dirent
->d_name
) + 1];
1059 if (streq(dirent
->d_name
, ".") || streq(dirent
->d_name
, ".."))
1062 sprintf(file
, "%s/%s", dirname
, dirent
->d_name
);
1063 if (lstat(file
, &st
) != 0)
1066 if (smells_like_module(dirent
->d_name
)) {
1067 if (st
.st_mtime
> mtime
)
1069 } else if (S_ISDIR(st
.st_mode
)) {
1070 if (any_modules_newer(file
, mtime
))
1082 static int depfile_out_of_date(const char *dirname
)
1085 char depfile
[strlen(dirname
) + 1 + strlen(depfiles
[0].name
) + 1];
1087 sprintf(depfile
, "%s/%s", dirname
, depfiles
[0].name
);
1089 if (stat(depfile
, &st
) != 0)
1092 return any_modules_newer(dirname
, st
.st_mtime
);
1095 static char *strsep_skipspace(char **string
, char *delim
)
1099 *string
+= strspn(*string
, delim
);
1100 return strsep(string
, delim
);
1103 static struct module_search
*add_search(const char *search_path
,
1105 struct module_search
*search
)
1108 struct module_search
*new;
1110 new = NOFAIL(malloc(sizeof(*new)));
1111 new->search_path
= NOFAIL(strdup(search_path
));
1119 static struct module_overrides
*add_override(const char *modfile
,
1120 struct module_overrides
*overrides
)
1123 struct module_overrides
*new;
1125 new = NOFAIL(malloc(sizeof(*new)));
1126 new->modfile
= NOFAIL(strdup(modfile
));
1127 new->next
= overrides
;
1133 static int parse_config_scan(const char *filename
,
1134 const char *basedir
,
1135 const char *kernelversion
,
1136 struct module_search
**search
,
1137 struct module_overrides
**overrides
);
1139 static int parse_config_file(const char *filename
,
1140 const char *basedir
,
1141 const char *kernelversion
,
1142 struct module_search
**search
,
1143 struct module_overrides
**overrides
)
1146 unsigned int linenum
= 0;
1149 cfile
= fopen(filename
, "r");
1151 if (errno
!= ENOENT
)
1152 fatal("could not open '%s', reason: %s\n", filename
,
1157 while ((line
= getline_wrapped(cfile
, &linenum
)) != NULL
) {
1159 char *cmd
, *modname
;
1161 cmd
= strsep_skipspace(&ptr
, "\t ");
1163 if (cmd
== NULL
|| cmd
[0] == '#' || cmd
[0] == '\0') {
1168 if (streq(cmd
, "search")) {
1171 while ((search_path
= strsep_skipspace(&ptr
, "\t "))) {
1175 if (strcmp(search_path
,
1176 MODULE_BUILTIN_KEY
) == 0) {
1177 *search
= add_search(MODULE_BUILTIN_KEY
,
1181 nofail_asprintf(&dirname
, "%s%s%s/%s", basedir
,
1182 MODULE_DIR
, kernelversion
, search_path
);
1183 len
= strlen(dirname
);
1184 *search
= add_search(dirname
, len
, *search
);
1187 } else if (streq(cmd
, "override")) {
1188 char *pathname
= NULL
, *version
, *subdir
;
1189 modname
= strsep_skipspace(&ptr
, "\t ");
1190 version
= strsep_skipspace(&ptr
, "\t ");
1191 subdir
= strsep_skipspace(&ptr
, "\t ");
1193 if (!regex_match(kernelversion
, (const char *)version
))
1196 nofail_asprintf(&pathname
, "%s%s%s/%s/%s.ko", basedir
,
1197 MODULE_DIR
, kernelversion
, subdir
, modname
);
1199 *overrides
= add_override(pathname
, *overrides
);
1201 } else if (streq(cmd
, "include")) {
1204 newfilename
= strsep_skipspace(&ptr
, "\t ");
1206 grammar(cmd
, filename
, linenum
);
1208 warn("\"include %s\" is deprecated, "
1209 "please use /etc/depmod.d\n", newfilename
);
1210 if (strstarts(newfilename
, "/etc/depmod.d")) {
1211 warn("\"include /etc/depmod.d\" is "
1212 "the default, ignored\n");
1214 if (!parse_config_scan(newfilename
, basedir
,
1217 warn("Failed to open included"
1218 " config file %s: %s\n",
1219 newfilename
, strerror(errno
));
1222 } else if (streq(cmd
, "make_map_files")) {
1225 option
= strsep_skipspace(&ptr
, "\t ");
1227 grammar(cmd
, filename
, linenum
);
1229 if (streq(option
, "yes"))
1231 else if (streq(option
, "no"))
1234 grammar(cmd
, filename
, linenum
);
1237 grammar(cmd
, filename
, linenum
);
1245 static int parse_config_scan(const char *filename
,
1246 const char *basedir
,
1247 const char *kernelversion
,
1248 struct module_search
**search
,
1249 struct module_overrides
**overrides
)
1254 dir
= opendir(filename
);
1257 struct list_head node
;
1260 LIST_HEAD(files_list
);
1261 struct file_entry
*fe
, *fe_tmp
;
1264 /* sort files from directory into list */
1265 while ((i
= readdir(dir
)) != NULL
) {
1268 if (i
->d_name
[0] == '.')
1270 if (!config_filter(i
->d_name
))
1273 len
= strlen(i
->d_name
);
1274 if (len
< 6 || strcmp(&i
->d_name
[len
-5], ".conf") != 0)
1275 warn("All config files need .conf: %s/%s, "
1276 "it will be ignored in a future release.\n",
1277 filename
, i
->d_name
);
1278 fe
= malloc(sizeof(struct file_entry
) + len
+ 1);
1281 strcpy(fe
->name
, i
->d_name
);
1282 list_for_each_entry(fe_tmp
, &files_list
, node
)
1283 if (strcmp(fe_tmp
->name
, fe
->name
) >= 0)
1285 list_add_tail(&fe
->node
, &fe_tmp
->node
);
1289 /* parse list of files */
1290 list_for_each_entry_safe(fe
, fe_tmp
, &files_list
, node
) {
1293 nofail_asprintf(&cfgfile
, "%s/%s", filename
, fe
->name
);
1294 if (!parse_config_file(cfgfile
, basedir
, kernelversion
,
1296 warn("Failed to open config file "
1297 "%s: %s\n", fe
->name
, strerror(errno
));
1299 list_del(&fe
->node
);
1305 if (parse_config_file(filename
, basedir
, kernelversion
, search
,
1313 static void parse_toplevel_config(const char *filename
,
1314 const char *basedir
,
1315 const char *kernelversion
,
1316 struct module_search
**search
,
1317 struct module_overrides
**overrides
)
1320 if (!parse_config_scan(filename
, basedir
, kernelversion
, search
,
1322 fatal("Failed to open config file %s: %s\n",
1323 filename
, strerror(errno
));
1327 /* deprecated config file */
1328 if (parse_config_file("/etc/depmod.conf", basedir
, kernelversion
,
1329 search
, overrides
) > 0)
1330 warn("Deprecated config file /etc/depmod.conf, "
1331 "all config files belong into /etc/depmod.d/.\n");
1333 /* default config */
1334 parse_config_scan("/etc/depmod.d", basedir
, kernelversion
,
1338 /* Local to main, but not freed on exit. Keep valgrind quiet. */
1339 struct module
*list
= NULL
;
1340 struct module_search
*search
= NULL
;
1341 struct module_overrides
*overrides
= NULL
;
1343 int main(int argc
, char *argv
[])
1345 int opt
, all
= 0, maybe_all
= 0, doing_stdout
= 0;
1346 char *basedir
= "", *dirname
, *version
;
1347 char *system_map
= NULL
, *module_symvers
= NULL
;
1349 const char *config
= NULL
;
1351 if (native_endianness() == 0)
1354 while ((opt
= getopt_long(argc
, argv
, "aAb:C:E:F:euqrvnhVwm", options
, NULL
))
1365 skipchars
= strlen(basedir
);
1371 module_symvers
= optarg
;
1375 system_map
= optarg
;
1391 print_usage(argv
[0]);
1395 printf("%s %s\n", PACKAGE
, VERSION
);
1401 force_map_files
= 1;
1404 print_usage(argv
[0]);
1410 load_module_symvers(module_symvers
);
1411 else if (system_map
)
1412 load_system_map(system_map
);
1413 else if (print_unknown
) {
1414 warn("-e needs -E or -F\n");
1418 /* They can specify the version naked on the command line */
1419 if (optind
< argc
&& is_version_number(argv
[optind
])) {
1420 version
= NOFAIL(strdup(argv
[optind
]));
1425 version
= NOFAIL(strdup(buf
.release
));
1428 /* Check for old version. */
1429 if (old_module_version(version
)) {
1430 fprintf(stderr
, "Kernel version %s requires old depmod\n",
1435 /* Depmod -a by default if no names. */
1439 nofail_asprintf(&dirname
, "%s%s%s", basedir
, MODULE_DIR
, version
);
1442 if (!doing_stdout
&& !depfile_out_of_date(dirname
))
1447 parse_toplevel_config(config
, basedir
, version
, &search
, &overrides
);
1449 /* For backward compatibility add "updates" to the head of the search
1450 * list here. But only if there was no "search" option specified.
1456 nofail_asprintf(&dirname
, "%s%s%s/updates", basedir
,
1457 MODULE_DIR
, version
);
1458 len
= strlen(dirname
);
1459 search
= add_search(dirname
, len
, search
);
1462 /* Do command line args. */
1463 for (opt
= optind
; opt
< argc
; opt
++) {
1466 if (argv
[opt
][0] != '/')
1467 fatal("modules must be specified using absolute paths.\n"
1468 "\"%s\" is a relative path\n", argv
[opt
]);
1470 new = grab_module(NULL
, argv
[opt
]);
1472 /* cmd-line specified modules must exist */
1473 fatal("grab_module() failed for module %s\n", argv
[opt
]);
1479 list
= grab_basedir(dirname
,search
,overrides
);
1481 list
= sort_modules(dirname
,list
);
1482 list
= parse_modules(list
);
1484 for (i
= 0; i
< sizeof(depfiles
)/sizeof(depfiles
[0]); i
++) {
1487 struct depfile
*d
= &depfiles
[i
];
1488 char depname
[strlen(dirname
) + 1 + strlen(d
->name
) + 1];
1489 char tmpname
[strlen(dirname
) + 1 + strlen(d
->name
) +
1490 strlen(".temp") + 1];
1492 if (d
->map_file
&& !make_map_files
&& !force_map_files
)
1495 sprintf(depname
, "%s/%s", dirname
, d
->name
);
1496 sprintf(tmpname
, "%s/%s.temp", dirname
, d
->name
);
1497 if (!doing_stdout
) {
1498 out
= fopen(tmpname
, "w");
1500 fatal("Could not open %s for writing: %s\n",
1501 tmpname
, strerror(errno
));
1504 if (ends_in(depname
, ".bin"))
1507 res
= d
->func(list
, out
, dirname
);
1512 if (rename(tmpname
, depname
) < 0)
1513 fatal("Could not rename %s into %s: %s\n",
1514 tmpname
, depname
, strerror(errno
));
1516 if (unlink(tmpname
) < 0)
1517 warn("Could not delete %s: %s\n",
1518 tmpname
, strerror(errno
));