2 * depmod.c: generate module dependency meta-data (aliases, etc.)
4 * (C) 2002 Rusty Russell IBM Corporation
5 * (C) 2006-2011 Jon Masters <jcm@jonmasters.org>, and others.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
22 #define _GNU_SOURCE /* asprintf */
31 #include <sys/types.h>
35 #include <sys/utsname.h>
38 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
41 #include "zlibsupport.h"
47 #include "config_filter.h"
52 #define MODULE_DIR "/lib/modules/"
55 #ifndef MODULE_BUILTIN_KEY
56 #define MODULE_BUILTIN_KEY "built-in"
59 /* used to replace one module with another based on conf override entries */
60 struct module_overrides
63 struct module_overrides
*next
;
65 /* overridden module */
69 /* used to specify the order in which /lib/modules subdirs are searched */
73 struct module_search
*next
;
80 static char sym_prefix
; /* used for -P option */
81 static unsigned int skipchars
; /* prefix target part of basedir to skip over */
82 static unsigned int make_map_files
= 1; /* default to on */
83 static unsigned int force_map_files
= 0; /* default to on */
85 #define SYMBOL_HASH_SIZE 1024
94 static struct symbol
*symbolhash
[SYMBOL_HASH_SIZE
];
97 * tdb_hash - calculate hash entry for a symbol (algorithm from gdbm, via tdb)
102 static inline unsigned int tdb_hash(const char *name
)
104 unsigned value
; /* Used to compute the hash value. */
105 unsigned i
; /* Used to cycle through random values. */
107 /* Set the initial value from the key size. */
108 for (value
= 0x238F13AF * strlen(name
), i
=0; name
[i
]; i
++)
109 value
= (value
+ (((unsigned char *)name
)[i
] << (i
*5 % 24)));
111 return (1103515243 * value
+ 12345);
115 * skip_symprefix - remove extraneous prefix character on some architectures
117 * @symname: symbol name to process for prefix character removal
120 static const char *skip_symprefix(const char *symname
)
122 return symname
+ (symname
[0] == sym_prefix
? 1 : 0);
126 * add_symbol - add a symbol to the symbol hash list
129 * @ver: symbol version (checksum)
130 * @owner: module owning this symbol
133 static void add_symbol(const char *name
, uint64_t ver
, struct module
*owner
)
136 struct symbol
*new = NOFAIL(malloc(sizeof *new + strlen(name
) + 1));
140 strcpy(new->name
, name
);
142 hash
= tdb_hash(name
) % SYMBOL_HASH_SIZE
;
143 new->next
= symbolhash
[hash
];
144 symbolhash
[hash
] = new;
147 static int print_unknown
, check_symvers
;
150 * find_symbol - lookup module owning a symbol in the symbol hash list
153 * @ver: symbol version
154 * @modname: pathname of module requesting lookup (being processed)
155 * @weak: whether the symbol is weakly defined or not
157 * This function is used during dependency calculation to match the
158 * dependencies a module says it requires with symbols we have seen.
159 * calculate_deps calls us after it has load_dep_syms on a module.
162 static struct module
*find_symbol(const char *name
, uint64_t ver
,
163 const char *modname
, int weak
)
167 /* For our purposes, .foo matches foo. PPC64 needs this. */
170 name
= skip_symprefix(name
);
172 for (s
= symbolhash
[tdb_hash(name
) % SYMBOL_HASH_SIZE
]; s
; s
=s
->next
) {
173 if (streq(s
->name
, name
))
177 if (ver
&& s
->ver
&& s
->ver
!= ver
&& print_unknown
&& !weak
)
178 warn("%s disagrees about version of symbol %s\n",
183 if (print_unknown
&& !weak
)
184 warn("%s needs unknown symbol %s\n", modname
, name
);
190 * add_dep - add a module dependency
193 * @depends_on: new module dependency
196 static void add_dep(struct module
*mod
, struct module
*depends_on
)
200 for (i
= 0; i
< mod
->num_deps
; i
++)
201 if (mod
->deps
[i
] == depends_on
)
204 mod
->deps
= NOFAIL(realloc(mod
->deps
, sizeof(mod
->deps
[0])*(mod
->num_deps
+1)));
205 mod
->deps
[mod
->num_deps
++] = depends_on
;
209 * add_fake_syms - symbols not explicitly otherwise provided
211 * The kernel provides a few dependencies within the module loader.
214 static void add_fake_syms(void)
216 /* __this_module is magic inserted by kernel loader. */
217 add_symbol("__this_module", 0, NULL
);
218 /* On S390, this is faked up too */
219 add_symbol("_GLOBAL_OFFSET_TABLE_", 0, NULL
);
223 * load_system_map - load list of symbols from the System.map
225 * @filename: path to the System.map-like file
228 static void load_system_map(const char *filename
)
232 const char ksymstr
[] = "__ksymtab_";
233 const int ksymstr_len
= strlen(ksymstr
);
235 system_map
= fopen(filename
, "r");
237 fatal("Could not open '%s': %s\n", filename
, strerror(errno
));
239 /* eg. c0294200 R __ksymtab_devfs_alloc_devnum */
240 while (fgets(line
, sizeof(line
)-1, system_map
)) {
245 ptr
= strchr(line
, '\n');
249 ptr
= strchr(line
, ' ');
250 if (!ptr
|| !(ptr
= strchr(ptr
+ 1, ' ')))
253 /* Skip the space before symbol name */
254 cptr
= skip_symprefix(ptr
+ 1);
256 /* Covers gpl-only and normal symbols. */
257 if (strstarts(cptr
, ksymstr
))
258 add_symbol(cptr
+ ksymstr_len
, 0, NULL
);
266 * load_module_symvers - load list of symbol versions from the module.symvers
268 * @filename: path to the module.symvers-like file
271 static void load_module_symvers(const char *filename
)
273 FILE *module_symvers
;
276 module_symvers
= fopen(filename
, "r");
278 fatal("Could not open '%s': %s\n", filename
, strerror(errno
));
280 /* eg. "0xb352177e\tfind_first_bit\tvmlinux\tEXPORT_SYMBOL" */
281 while (fgets(line
, sizeof(line
)-1, module_symvers
)) {
282 const char *ver
, *sym
, *where
;
284 ver
= strtok(line
, " \t");
285 sym
= strtok(NULL
, " \t");
286 where
= strtok(NULL
, " \t");
287 if (!ver
|| !sym
|| !where
)
290 if (streq(where
, "vmlinux"))
291 add_symbol(skip_symprefix(sym
), strtoull(ver
, NULL
, 16), NULL
);
294 fclose(module_symvers
);
298 static const struct option options
[] = { { "all", 0, NULL
, 'a' },
299 { "quick", 0, NULL
, 'A' },
300 { "basedir", 1, NULL
, 'b' },
301 { "config", 1, NULL
, 'C' },
302 { "symvers", 1, NULL
, 'E' },
303 { "filesyms", 1, NULL
, 'F' },
304 { "errsyms", 0, NULL
, 'e' },
305 { "unresolved-error", 0, NULL
, 'u' },
306 { "quiet", 0, NULL
, 'q' },
307 { "root", 0, NULL
, 'r' },
308 { "verbose", 0, NULL
, 'v' },
309 { "show", 0, NULL
, 'n' },
310 { "dry-run", 0, NULL
, 'n' },
311 { "symbol-prefix", 0, NULL
, 'P' },
312 { "help", 0, NULL
, 'h' },
313 { "version", 0, NULL
, 'V' },
314 { "warn", 0, NULL
, 'w' },
315 { "map", 0, NULL
, 'm' },
316 { NULL
, 0, NULL
, 0 } };
319 * is_version_number - is the option a kernel version or module name
321 * @version: possible version number
324 static int is_version_number(const char *version
)
328 return (sscanf(version
, "%u.%u", &dummy
, &dummy
) == 2);
332 * old_module_version - is the kernel version too old for these tools
334 * @version: version number
337 static int old_module_version(const char *version
)
339 /* Expect three part version (but won't fail it only two part). */
340 unsigned int major
, sub
, minor
;
342 sscanf(version
, "%u.%u.%u", &major
, &sub
, &minor
);
344 if (major
> 2) return 0;
345 if (major
< 2) return 1;
348 if (sub
> 5) return 0;
349 if (sub
< 5) return 1;
352 if (minor
>= 48) return 0;
357 * print_usage - output a list of all possible options
359 * @name: not currently used
362 static void print_usage(const char *name
)
365 "%s " VERSION
" -- part of " PACKAGE
"\n"
366 "%s -[aA] [-n -e -v -q -V -r -u -w -m]\n"
367 " [-b basedirectory] [forced_version]\n"
368 "depmod [-n -e -v -q -r -u -w] [-F kernelsyms] module1.ko module2.ko ...\n"
369 "If no arguments (except options) are given, \"depmod -a\" is assumed\n"
371 "depmod will output a dependancy list suitable for the modprobe utility.\n"
375 "\t-a, --all Probe all modules\n"
376 "\t-A, --quick Only does the work if there's a new module\n"
377 "\t-e, --errsyms Report not supplied symbols\n"
378 "\t-m, --map Create the legacy map files\n"
379 "\t-n, --show Write the dependency file on stdout only\n"
380 "\t-P, --symbol-prefix Architecture symbol prefix\n"
381 "\t-V, --version Print the release version\n"
382 "\t-v, --verbose Enable verbose mode\n"
383 "\t-w, --warn Warn on duplicates\n"
384 "\t-h, --help Print this usage message\n"
386 "The following options are useful for people managing distributions:\n"
387 "\t-b basedirectory\n"
388 "\t --basedir basedirectory Use an image of a module tree.\n"
390 "\t --filesyms kernelsyms Use the file instead of the\n"
391 "\t current kernel symbols.\n"
392 "\t-E Module.symvers\n"
393 "\t --symvers Module.symvers Use Module.symvers file to check\n"
394 "\t symbol versions.\n",
399 * ends_in - check file extension
402 * @ext: extension to check
405 static int ends_in(const char *name
, const char *ext
)
407 unsigned int namelen
, extlen
;
410 namelen
= strlen(name
);
411 extlen
= strlen(ext
);
413 if (namelen
< extlen
) return 0;
415 if (streq(name
+ namelen
- extlen
, ext
))
421 * grab_module - open module ELF file and load symbol data
423 * @dirname: path prefix
424 * @filename: filename within path
427 static struct module
*grab_module(const char *dirname
, const char *filename
)
431 new = NOFAIL(malloc(sizeof(*new)
432 + strlen(dirname
?:"") + 1 + strlen(filename
) + 1));
434 sprintf(new->pathname
, "%s/%s", dirname
, filename
);
436 strcpy(new->pathname
, filename
);
437 new->basename
= my_basename(new->pathname
);
439 INIT_LIST_HEAD(&new->dep_list
);
440 new->order
= INDEX_PRIORITY_MIN
;
442 new->file
= grab_elf_file(new->pathname
);
444 warn("Can't read module %s: %s\n",
445 new->pathname
, strerror(errno
));
452 /* We use this on-stack structure to track recursive calls to has_dep_loop */
453 struct module_traverse
455 struct module_traverse
*prev
;
460 * in_loop - determine if a module dependency loop exists
462 * @mod: current module
463 * @traverse: on-stack structure created as module deps were processed
466 static int in_loop(struct module
*mod
, const struct module_traverse
*traverse
)
468 const struct module_traverse
*i
;
470 for (i
= traverse
; i
; i
= i
->prev
) {
478 * report_loop - report (once) that a dependency loop exists for a module
480 * @mod: module with dep loop
481 * @traverse: on-stack structure created as module deps were processed
484 static void report_loop(const struct module
*mod
,
485 const struct module_traverse
*traverse
)
487 const struct module_traverse
*i
;
489 /* Check that start is least alphabetically. eg. a depends
490 on b depends on a will get reported for a, not b. */
491 for (i
= traverse
->prev
; i
->prev
; i
= i
->prev
) {
492 if (strcmp(mod
->pathname
, i
->mod
->pathname
) > 0)
496 /* Is start in the loop? If not, don't report now. eg. a
497 depends on b which depends on c which depends on b. Don't
498 report when generating depends for a. */
502 warn("Loop detected: %s ", mod
->pathname
);
503 for (i
= traverse
->prev
; i
->prev
; i
= i
->prev
)
504 fprintf(stderr
, "needs %s ", i
->mod
->basename
);
505 fprintf(stderr
, "which needs %s again!\n", i
->mod
->basename
);
509 * has_dep_loop - iterate over all module deps and check for loops
511 * @module: module to process
512 * @prev: previously processed dependency
514 * This function is called recursively, following every dep and creating
515 * a module_traverse on the stack describing each dependency encountered.
516 * Determining a loop is as simple (and slow) as finding repetitions.
518 * This is slow, but we can't leave the user without any modules, so we
519 * need to detect loops and just fail those modules that cause loops.
522 static int has_dep_loop(struct module
*module
, struct module_traverse
*prev
)
525 struct module_traverse traverse
= { .prev
= prev
, .mod
= module
};
527 if (in_loop(module
, prev
)) {
528 report_loop(module
, &traverse
);
532 for (i
= 0; i
< module
->num_deps
; i
++)
533 if (has_dep_loop(module
->deps
[i
], &traverse
))
539 * order_dep_list - expand all module deps recursively and in order
541 * @start: module being processed
542 * @mod: recursive dep
544 * We expand all of the dependencies of the dependencies of a module
545 * and ensure that the lowest dependency is loaded first, etc.
548 static void order_dep_list(struct module
*start
, struct module
*mod
)
552 for (i
= 0; i
< mod
->num_deps
; i
++) {
553 /* If it was previously depended on, move it to the
554 tail. ie. if a needs b and c, and c needs b, we
555 must order b after c. */
556 list_del(&mod
->deps
[i
]->dep_list
);
557 list_add_tail(&mod
->deps
[i
]->dep_list
, &start
->dep_list
);
558 order_dep_list(start
, mod
->deps
[i
]);
562 static struct module
*deleted
= NULL
;
565 * del_module - remove from list of modules
567 * @modules: list of modules
568 * @delme: module to remove
571 static void del_module(struct module
**modules
, struct module
*delme
)
575 /* Find pointer to it. */
577 for (i
= modules
; *i
!= delme
; i
= &(*i
)->next
);
582 /* Save on a list to quiet valgrind.
583 Can't free - other modules may depend on them */
584 delme
->next
= deleted
;
589 * compress_path - strip out common path prefix for modules
591 * @path: path to module
592 * @basedir: top level modules directory
594 * Modules are typically located in /lib/modules. There is no need to
595 * store the same common path prefix for all modules - make paths
596 * relative to directory that contains the dep information files.
599 static const char *compress_path(const char *path
, const char *basedir
)
601 int len
= strlen(basedir
);
603 if (strncmp(path
, basedir
, len
) == 0)
609 * output_deps - create ascii text file representation of module deps
611 * @modules: list of modules
613 * @dirname: output directory
616 static int output_deps(struct module
*modules
,
617 FILE *out
, char *dirname
)
621 for (i
= modules
; i
; i
= i
->next
) {
622 struct list_head
*j
, *tmp
;
623 order_dep_list(i
, i
);
625 fprintf(out
, "%s:", compress_path(i
->pathname
, dirname
));
626 list_for_each_safe(j
, tmp
, &i
->dep_list
) {
628 = list_entry(j
, struct module
, dep_list
);
630 compress_path(dep
->pathname
, dirname
));
638 /* warn whenever duplicate module aliases, deps, or symbols are found. */
639 static int warn_dups
= 0;
642 * output_deps_bin - create binary trie representation of module deps
644 * @modules: list of modules
645 * @out: output binary file
646 * @dirname: output directory
648 * This optimized dependency file contains an ordered structure that is
649 * more easily processed by modprobe in a time sensitive manner.
652 static int output_deps_bin(struct module
*modules
,
653 FILE *out
, char *dirname
)
656 struct index_node
*index
;
660 index
= index_create();
662 for (i
= modules
; i
; i
= i
->next
) {
663 struct list_head
*j
, *tmp
;
664 char modname
[strlen(i
->pathname
)+1];
666 order_dep_list(i
, i
);
668 filename2modname(modname
, i
->pathname
);
669 nofail_asprintf(&line
, "%s:",
670 compress_path(i
->pathname
, dirname
));
672 list_for_each_safe(j
, tmp
, &i
->dep_list
) {
674 = list_entry(j
, struct module
, dep_list
);
675 nofail_asprintf(&line
, "%s %s",
677 compress_path(dep
->pathname
, dirname
));
682 if (index_insert(index
, modname
, line
, i
->order
) && warn_dups
)
683 warn("duplicate module deps:\n%s\n",line
);
687 index_write(index
, out
);
688 index_destroy(index
);
694 * smells_like_module - detect common module extensions
699 static int smells_like_module(const char *name
)
701 return ends_in(name
,".ko") || ends_in(name
, ".ko.gz");
704 typedef struct module
*(*do_module_t
)(const char *dirname
,
705 const char *filename
,
707 struct module_search
*search
,
708 struct module_overrides
*overrides
);
711 * is_higher_priority - find modules replacing other modules
713 * @newpath: new module path
714 * @oldpath: old module path
715 * @search: path search order
716 * @overrides: module override directives
718 * Compares one module (path) to another module (path) and determines
719 * whether the new module should replace the existing module of the
720 * same name. Overriding is handled very coarsely for the moment.
723 static int is_higher_priority(const char *newpath
, const char *oldpath
,
724 struct module_search
*search
,
725 struct module_overrides
*overrides
)
727 struct module_search
*tmp
;
728 struct module_overrides
*ovtmp
;
730 int prio_builtin
= -1;
734 /* The names already match, now we check for overrides and directory search
737 for (ovtmp
= overrides
; ovtmp
!= NULL
; ovtmp
= ovtmp
->next
) {
738 if (streq(ovtmp
->modfile
, newpath
))
740 if (streq(ovtmp
->modfile
, oldpath
))
743 for (i
= 0, tmp
= search
; tmp
!= NULL
; tmp
= tmp
->next
, i
++) {
744 if (streq(tmp
->search_path
, MODULE_BUILTIN_KEY
))
746 else if (strncmp(tmp
->search_path
, newpath
, tmp
->len
) == 0)
748 else if (strncmp(tmp
->search_path
, oldpath
, tmp
->len
) == 0)
752 prio_new
= prio_builtin
;
754 prio_old
= prio_builtin
;
756 return prio_new
> prio_old
;
760 * do_module - process a module file
762 * @dirname: directory containing module
763 * @filename: module disk file
764 * @list: list of modules
765 * @search: path search order
766 * @overrides: module override directives
769 static struct module
*do_module(const char *dirname
,
770 const char *filename
,
772 struct module_search
*search
,
773 struct module_overrides
*overrides
)
775 struct module
*new, **i
;
777 new = grab_module(dirname
, filename
);
781 /* Check if module is already in the list. */
782 for (i
= &list
; *i
; i
= &(*i
)->next
) {
784 if (streq((*i
)->basename
, filename
)) {
785 char newpath
[strlen(dirname
) + strlen("/")
786 + strlen(filename
) + 1];
788 sprintf(newpath
, "%s/%s", dirname
, filename
);
790 /* if module matches an existing entry (name) but */
791 /* has a higher priority, replace existing entry. */
792 if (is_higher_priority(newpath
, (*i
)->pathname
,search
,
799 del_module(NULL
, new);
805 /* Not in the list already. Just prepend. */
811 * grab_dir - process a directory of modules
813 * @dirname: directory name
814 * @dir: open directory reference
815 * @do_mod: do_module function to use
816 * @search: path search order
817 * @overrides: module overrides directives
820 static struct module
*grab_dir(const char *dirname
,
824 struct module_search
*search
,
825 struct module_overrides
*overrides
)
827 struct dirent
*dirent
;
829 while ((dirent
= readdir(dir
)) != NULL
) {
830 if (smells_like_module(dirent
->d_name
))
831 next
= do_mod(dirname
, dirent
->d_name
, next
,
833 else if (!streq(dirent
->d_name
, ".")
834 && !streq(dirent
->d_name
, "..")
835 && !streq(dirent
->d_name
, "source")
836 && !streq(dirent
->d_name
, "build")) {
839 char subdir
[strlen(dirname
) + 1
840 + strlen(dirent
->d_name
) + 1];
841 sprintf(subdir
, "%s/%s", dirname
, dirent
->d_name
);
842 sub
= opendir(subdir
);
844 next
= grab_dir(subdir
, sub
, next
, do_mod
,
854 * grab_basedir - top-level module processing
856 * @dirname: top-level directory name
857 * @search: path search order
858 * @overrides: module overrides directives
861 static struct module
*grab_basedir(const char *dirname
,
862 struct module_search
*search
,
863 struct module_overrides
*overrides
)
868 dir
= opendir(dirname
);
870 warn("Couldn't open directory %s: %s\n",
871 dirname
, strerror(errno
));
874 list
= grab_dir(dirname
, dir
, NULL
, do_module
, search
, overrides
);
881 * sort_modules - order modules in list on modules.order if available
883 * @dirname: directory name
886 * Using the modules.order file (if available), give every module an index
887 * based on its position in the file, and order list based on the index. If
888 * no ordering data is available, fallback to existing unordered list.
891 static struct module
*sort_modules(const char *dirname
, struct module
*list
)
893 struct module
*tlist
= NULL
, **tpos
= &tlist
;
895 int dir_len
= strlen(dirname
) + 1;
896 char file_name
[dir_len
+ strlen("modules.order") + 1];
898 unsigned int linenum
= 0;
900 sprintf(file_name
, "%s/%s", dirname
, "modules.order");
902 modorder
= fopen(file_name
, "r");
904 /* Older kernels don't generate modules.order. Just
905 return if the file doesn't exist. */
908 fatal("Could not open '%s': %s\n", file_name
, strerror(errno
));
911 sprintf(line
, "%s/", dirname
);
913 /* move modules listed in modorder file to tlist in order */
914 while (fgets(line
, sizeof(line
), modorder
)) {
915 struct module
**pos
, *mod
;
916 int len
= strlen(line
);
919 if (line
[len
- 1] == '\n')
920 line
[len
- 1] = '\0';
922 for (pos
= &list
; (mod
= *pos
); pos
= &(*pos
)->next
) {
923 if (streq(line
, mod
->pathname
+ dir_len
)) {
924 mod
->order
= linenum
;
934 /* append the rest */
943 * calculate_deps - calculate deps for module
945 * @module: module to process
948 static void calculate_deps(struct module
*module
)
951 struct string_table
*symnames
;
952 struct string_table
*symtypes
;
953 uint64_t *symvers
= NULL
;
954 struct elf_file
*file
;
956 module
->num_deps
= 0;
960 symnames
= file
->ops
->load_dep_syms(file
, &symtypes
,
961 check_symvers
? &symvers
: NULL
);
962 if (!symnames
|| !symtypes
)
965 for (i
= 0; i
< symnames
->cnt
; i
++) {
968 struct module
*owner
;
971 name
= symnames
->str
[i
];
972 ver
= symvers
? symvers
[i
] : 0;
973 weak
= (*(symtypes
->str
[i
]) == 'W');
974 owner
= find_symbol(name
, ver
, module
->pathname
, weak
);
976 info("%s needs \"%s\": %s\n",
977 module
->pathname
, name
,
979 add_dep(module
, owner
);
989 * parse_modules - process the modules list
991 * @module: module list
993 * Process each module in the (sorted by sort_modules) list for symbols,
994 * dependencies, and other meta-data that will be output later.
997 static struct module
*parse_modules(struct module
*list
)
1000 struct elf_file
*file
;
1001 struct string_table
*syms
;
1004 for (i
= list
; i
; i
= i
->next
) {
1005 uint64_t *symvers
= NULL
;
1007 syms
= file
->ops
->load_symbols(file
,
1008 check_symvers
? &symvers
: NULL
);
1010 for (j
= 0; j
< syms
->cnt
; j
++)
1011 add_symbol(skip_symprefix(syms
->str
[j
]),
1012 symvers
? symvers
[j
] : 0, i
);
1016 file
->ops
->fetch_tables(file
, &i
->tables
);
1019 for (i
= list
; i
; i
= i
->next
)
1022 /* Strip out modules with dependency loops. */
1024 for (i
= list
; i
; i
= i
->next
) {
1025 if (has_dep_loop(i
, NULL
)) {
1026 warn("Module %s ignored, due to loop\n",
1027 i
->pathname
+ skipchars
);
1028 del_module(&list
, i
);
1037 * output_symbols - output symbol alias information
1040 * @out: output file reference
1041 * @dirname: output directory
1043 * Output the symbol hash table, in the form of symbol alias entries, to
1044 * an ascii text file of the form modules.symbols (depending on file).
1047 static int output_symbols(struct module
*unused
, FILE *out
, char *dirname
)
1051 fprintf(out
, "# Aliases for symbols, used by symbol_request().\n");
1052 for (i
= 0; i
< SYMBOL_HASH_SIZE
; i
++) {
1055 for (s
= symbolhash
[i
]; s
; s
= s
->next
) {
1057 char modname
[strlen(s
->owner
->pathname
)+1];
1058 filename2modname(modname
, s
->owner
->pathname
);
1059 fprintf(out
, "alias symbol:%s %s\n",
1068 * output_symbols_bin - output symbol alias information in binary format
1071 * @out: output file reference
1072 * @dirname: output directory
1074 * Output the symbol hash table, in the form of symbol alias entries, to
1075 * a trie ordered output file e.g. of the form modules.symbols.bin.
1078 static int output_symbols_bin(struct module
*unused
, FILE *out
, char *dirname
)
1080 struct index_node
*index
;
1085 index
= index_create();
1087 for (i
= 0; i
< SYMBOL_HASH_SIZE
; i
++) {
1090 for (s
= symbolhash
[i
]; s
; s
= s
->next
) {
1092 char modname
[strlen(s
->owner
->pathname
)+1];
1093 filename2modname(modname
, s
->owner
->pathname
);
1094 nofail_asprintf(&alias
, "symbol:%s", s
->name
);
1095 duplicate
= index_insert(index
, alias
, modname
,
1097 if (duplicate
&& warn_dups
)
1098 warn("duplicate module syms:\n%s %s\n",
1105 index_write(index
, out
);
1106 index_destroy(index
);
1112 * output_builtin_bin - output list of built-in modules in binary format
1115 * @out: output file reference
1116 * @dirname: output directory
1119 static int output_builtin_bin(struct module
*unused
, FILE *out
, char *dirname
)
1121 struct index_node
*index
;
1122 char *textfile
, *line
;
1123 unsigned int linenum
;
1126 nofail_asprintf(&textfile
, "%s/modules.builtin", dirname
);
1127 if (!(f
= fopen(textfile
, "r"))) {
1128 if (errno
!= ENOENT
)
1129 fatal("Could not open '%s': %s\n",
1130 textfile
, strerror(errno
));
1135 index
= index_create();
1137 while ((line
= getline_wrapped(f
, &linenum
)) != NULL
) {
1138 char *module
= line
;
1140 if (!*line
|| *line
== '#') {
1144 filename2modname(module
, module
);
1145 index_insert(index
, module
, "", 0);
1149 index_write(index
, out
);
1150 index_destroy(index
);
1156 * output_aliases - output list of module aliases
1158 * @modules: list of modules
1159 * @out: output file reference
1160 * @dirname: output directory
1163 static int output_aliases(struct module
*modules
, FILE *out
, char *dirname
)
1166 struct elf_file
*file
;
1167 struct string_table
*tbl
;
1170 fprintf(out
, "# Aliases extracted from modules themselves.\n");
1171 for (i
= modules
; i
; i
= i
->next
) {
1172 char modname
[strlen(i
->pathname
)+1];
1175 filename2modname(modname
, i
->pathname
);
1177 /* Grab from old-style .modalias section. */
1178 tbl
= file
->ops
->load_strings(file
, ".modalias", NULL
);
1179 for (j
= 0; tbl
&& j
< tbl
->cnt
; j
++)
1180 fprintf(out
, "alias %s %s\n", tbl
->str
[j
], modname
);
1183 /* Grab from new-style .modinfo section. */
1184 tbl
= file
->ops
->load_strings(file
, ".modinfo", NULL
);
1185 for (j
= 0; tbl
&& j
< tbl
->cnt
; j
++) {
1186 const char *p
= tbl
->str
[j
];
1187 if (strstarts(p
, "alias="))
1188 fprintf(out
, "alias %s %s\n",
1189 p
+ strlen("alias="), modname
);
1197 * output_aliases_bin - output list of module aliases in binary format
1199 * @modules: list of modules
1200 * @out: outout file reference
1201 * @dirname: output directory
1204 static int output_aliases_bin(struct module
*modules
, FILE *out
, char *dirname
)
1207 struct elf_file
*file
;
1208 struct string_table
*tbl
;
1211 struct index_node
*index
;
1214 index
= index_create();
1216 for (i
= modules
; i
; i
= i
->next
) {
1217 char modname
[strlen(i
->pathname
)+1];
1220 filename2modname(modname
, i
->pathname
);
1222 /* Grab from old-style .modalias section. */
1223 tbl
= file
->ops
->load_strings(file
, ".modalias", NULL
);
1224 for (j
= 0; tbl
&& j
< tbl
->cnt
; j
++) {
1225 alias
= NOFAIL(strdup(tbl
->str
[j
]));
1227 duplicate
= index_insert(index
, alias
, modname
, i
->order
);
1228 if (duplicate
&& warn_dups
)
1229 warn("duplicate module alias:\n%s %s\n",
1235 /* Grab from new-style .modinfo section. */
1236 tbl
= file
->ops
->load_strings(file
, ".modinfo", NULL
);
1237 for (j
= 0; tbl
&& j
< tbl
->cnt
; j
++) {
1238 const char *p
= tbl
->str
[j
];
1239 if (strstarts(p
, "alias=")) {
1240 alias
= NOFAIL(strdup(p
+ strlen("alias=")));
1242 duplicate
= index_insert(index
, alias
, modname
, i
->order
);
1243 if (duplicate
&& warn_dups
)
1244 warn("duplicate module alias:\n%s %s\n",
1252 index_write(index
, out
);
1253 index_destroy(index
);
1259 * output_softdeps - output module softdeps (non-implicit dependencies)
1261 * @modules: list of modules
1262 * @out: output file reference
1263 * @dirname: output directory
1266 static int output_softdeps(struct module
*modules
, FILE *out
, char *dirname
)
1269 struct elf_file
*file
;
1270 struct string_table
*tbl
;
1273 fprintf(out
, "# Soft dependencies extracted from modules themselves.\n");
1274 fprintf(out
, "# Copy, with a .conf extension, to /etc/modprobe.d to use "
1275 "it with modprobe.\n");
1276 for (i
= modules
; i
; i
= i
->next
) {
1277 char modname
[strlen(i
->pathname
)+1];
1280 filename2modname(modname
, i
->pathname
);
1282 /* Grab from new-style .modinfo section. */
1283 tbl
= file
->ops
->load_strings(file
, ".modinfo", NULL
);
1284 for (j
= 0; tbl
&& j
< tbl
->cnt
; j
++) {
1285 const char *p
= tbl
->str
[j
];
1286 if (strstarts(p
, "softdep="))
1287 fprintf(out
, "softdep %s %s\n",
1288 modname
, p
+ strlen("softdep="));
1296 * output_devname - output device names required by modules
1298 * @modules: list of modules
1299 * @out: output file reference
1300 * @dirname: output directory
1303 static int output_devname(struct module
*modules
, FILE *out
, char *dirname
)
1307 fprintf(out
, "# Device nodes to trigger on-demand module loading.\n");
1308 for (m
= modules
; m
!= NULL
; m
= m
->next
) {
1309 struct string_table
*tbl
;
1312 const char *devname
= NULL
;
1314 tbl
= m
->file
->ops
->load_strings(m
->file
, ".modinfo", NULL
);
1315 for (i
= 0; tbl
&& i
< tbl
->cnt
; i
++) {
1316 const char *p
= tbl
->str
[i
];
1317 unsigned int maj
, min
;
1319 if (sscanf(p
, "alias=char-major-%u-%u", &maj
, &min
) == 2)
1321 else if (sscanf(p
, "alias=block-major-%u-%u", &maj
, &min
) == 2)
1323 else if (strstarts(p
, "alias=devname:"))
1324 devname
= &p
[strlen("alias=devname:")];
1326 if (type
&& devname
) {
1327 char modname
[strlen(m
->pathname
)+1];
1329 filename2modname(modname
, m
->pathname
);
1330 fprintf(out
, "%s %s %c%u:%u\n",
1331 modname
, devname
, type
, maj
, min
);
1342 int (*func
)(struct module
*, FILE *, char *dirname
);
1346 /* The possible output files - those with map_file unset typically not made */
1347 static const struct depfile depfiles
[] = {
1348 { "modules.dep", output_deps
, 0 }, /* This is what we check for '-A'. */
1349 { "modules.dep.bin", output_deps_bin
, 0 },
1350 { "modules.pcimap", output_pci_table
, 1 },
1351 { "modules.usbmap", output_usb_table
, 1 },
1352 { "modules.ccwmap", output_ccw_table
, 1 },
1353 { "modules.ieee1394map", output_ieee1394_table
, 1 },
1354 { "modules.isapnpmap", output_isapnp_table
, 1 },
1355 { "modules.inputmap", output_input_table
, 1 },
1356 { "modules.ofmap", output_of_table
, 1 },
1357 { "modules.seriomap", output_serio_table
, 1 },
1358 { "modules.alias", output_aliases
, 0 },
1359 { "modules.alias.bin", output_aliases_bin
, 0 },
1360 { "modules.softdep", output_softdeps
, 0 },
1361 { "modules.symbols", output_symbols
, 0 },
1362 { "modules.symbols.bin", output_symbols_bin
, 0 },
1363 { "modules.builtin.bin", output_builtin_bin
, 0 },
1364 { "modules.devname", output_devname
, 0 },
1368 * any_modules_newer - determine if modules are newer than ref time
1370 * @dirname: directory to process
1371 * @mtime: comparison time
1373 * The worst case is that we process modules we didn't need to. It is
1374 * therefore safer to go with "true" if we can't figure it out.
1377 static int any_modules_newer(const char *dirname
, time_t mtime
)
1380 struct dirent
*dirent
;
1382 dir
= opendir(dirname
);
1386 while ((dirent
= readdir(dir
)) != NULL
) {
1388 char file
[strlen(dirname
) + 1 + strlen(dirent
->d_name
) + 1];
1390 if (streq(dirent
->d_name
, ".") || streq(dirent
->d_name
, ".."))
1393 sprintf(file
, "%s/%s", dirname
, dirent
->d_name
);
1394 if (lstat(file
, &st
) != 0)
1397 if (smells_like_module(dirent
->d_name
)) {
1398 if (st
.st_mtime
> mtime
)
1400 } else if (S_ISDIR(st
.st_mode
)) {
1401 if (any_modules_newer(file
, mtime
))
1414 * depfile_out_of_date - check if module dep files are older than any modules
1416 * @dirname: directory to process
1418 * Use any_modules_newer to determine if the dep files are up to date.
1421 static int depfile_out_of_date(const char *dirname
)
1424 char depfile
[strlen(dirname
) + 1 + strlen(depfiles
[0].name
) + 1];
1426 sprintf(depfile
, "%s/%s", dirname
, depfiles
[0].name
);
1428 if (stat(depfile
, &st
) != 0)
1431 return any_modules_newer(dirname
, st
.st_mtime
);
1435 * strsep_skipspace - skip over delimitors in strings
1437 * @string: string to process
1438 * @delim: delimitor (e.g. ' ')
1441 static char *strsep_skipspace(char **string
, char *delim
)
1445 *string
+= strspn(*string
, delim
);
1446 return strsep(string
, delim
);
1450 * add_search - add a new module search path
1452 * @search_path: path to search
1453 * @len: length of path
1454 * @search: list of search paths
1457 static struct module_search
*add_search(const char *search_path
,
1459 struct module_search
*search
)
1462 struct module_search
*new;
1464 new = NOFAIL(malloc(sizeof(*new)));
1465 new->search_path
= NOFAIL(strdup(search_path
));
1474 * add_override - add a new module override entry
1476 * @modfile: name of module file
1477 * @overrides: list of override entries
1480 static struct module_overrides
*add_override(const char *modfile
,
1481 struct module_overrides
*overrides
)
1484 struct module_overrides
*new;
1486 new = NOFAIL(malloc(sizeof(*new)));
1487 new->modfile
= NOFAIL(strdup(modfile
));
1488 new->next
= overrides
;
1494 static int parse_config_scan(const char *filename
,
1495 const char *basedir
,
1496 const char *kernelversion
,
1497 struct module_search
**search
,
1498 struct module_overrides
**overrides
);
1501 * parse_config_file - process an individual configuration file
1503 * @filename: name of config file
1504 * @basedir: module base directory
1505 * @kernelversion: kernel version to process
1506 * @search: search path order
1507 * @overrides: module override entries
1510 static int parse_config_file(const char *filename
,
1511 const char *basedir
,
1512 const char *kernelversion
,
1513 struct module_search
**search
,
1514 struct module_overrides
**overrides
)
1517 unsigned int linenum
= 0;
1520 cfile
= fopen(filename
, "r");
1522 if (errno
!= ENOENT
)
1523 fatal("could not open '%s', reason: %s\n", filename
,
1528 while ((line
= getline_wrapped(cfile
, &linenum
)) != NULL
) {
1530 char *cmd
, *modname
;
1532 cmd
= strsep_skipspace(&ptr
, "\t ");
1534 if (cmd
== NULL
|| cmd
[0] == '#' || cmd
[0] == '\0') {
1539 if (streq(cmd
, "search")) {
1542 while ((search_path
= strsep_skipspace(&ptr
, "\t "))) {
1546 if (strcmp(search_path
,
1547 MODULE_BUILTIN_KEY
) == 0) {
1548 *search
= add_search(MODULE_BUILTIN_KEY
,
1552 nofail_asprintf(&dirname
, "%s%s%s/%s", basedir
,
1553 MODULE_DIR
, kernelversion
, search_path
);
1554 len
= strlen(dirname
);
1555 *search
= add_search(dirname
, len
, *search
);
1558 } else if (streq(cmd
, "override")) {
1559 char *pathname
= NULL
, *version
, *subdir
;
1560 modname
= strsep_skipspace(&ptr
, "\t ");
1561 version
= strsep_skipspace(&ptr
, "\t ");
1562 subdir
= strsep_skipspace(&ptr
, "\t ");
1564 if (!regex_match(kernelversion
, (const char *)version
))
1567 nofail_asprintf(&pathname
, "%s%s%s/%s/%s.ko", basedir
,
1568 MODULE_DIR
, kernelversion
, subdir
, modname
);
1570 *overrides
= add_override(pathname
, *overrides
);
1572 } else if (streq(cmd
, "include")) {
1575 newfilename
= strsep_skipspace(&ptr
, "\t ");
1577 grammar(cmd
, filename
, linenum
);
1579 warn("\"include %s\" is deprecated, "
1580 "please use /etc/depmod.d\n", newfilename
);
1581 if (strstarts(newfilename
, "/etc/depmod.d")) {
1582 warn("\"include /etc/depmod.d\" is "
1583 "the default, ignored\n");
1585 if (!parse_config_scan(newfilename
, basedir
,
1588 warn("Failed to open included"
1589 " config file %s: %s\n",
1590 newfilename
, strerror(errno
));
1593 } else if (streq(cmd
, "make_map_files")) {
1596 option
= strsep_skipspace(&ptr
, "\t ");
1598 grammar(cmd
, filename
, linenum
);
1600 if (streq(option
, "yes"))
1602 else if (streq(option
, "no"))
1605 grammar(cmd
, filename
, linenum
);
1608 grammar(cmd
, filename
, linenum
);
1617 * parse_config_scan - handle a directory of config files
1619 * @filename: name of directory
1620 * @basedir: module base directory
1621 * @kernelversion: kernel version to process
1622 * @search: search path order
1623 * @overrides: module override entries
1626 static int parse_config_scan(const char *filename
,
1627 const char *basedir
,
1628 const char *kernelversion
,
1629 struct module_search
**search
,
1630 struct module_overrides
**overrides
)
1635 dir
= opendir(filename
);
1638 struct list_head node
;
1641 LIST_HEAD(files_list
);
1642 struct file_entry
*fe
, *fe_tmp
;
1645 /* sort files from directory into list */
1646 while ((i
= readdir(dir
)) != NULL
) {
1649 if (i
->d_name
[0] == '.')
1651 if (!config_filter(i
->d_name
))
1654 len
= strlen(i
->d_name
);
1655 if (len
< 6 || strcmp(&i
->d_name
[len
-5], ".conf") != 0)
1656 warn("All config files need .conf: %s/%s, "
1657 "it will be ignored in a future release.\n",
1658 filename
, i
->d_name
);
1659 fe
= malloc(sizeof(struct file_entry
) + len
+ 1);
1662 strcpy(fe
->name
, i
->d_name
);
1663 list_for_each_entry(fe_tmp
, &files_list
, node
)
1664 if (strcmp(fe_tmp
->name
, fe
->name
) >= 0)
1666 list_add_tail(&fe
->node
, &fe_tmp
->node
);
1670 /* parse list of files */
1671 list_for_each_entry_safe(fe
, fe_tmp
, &files_list
, node
) {
1674 nofail_asprintf(&cfgfile
, "%s/%s", filename
, fe
->name
);
1675 if (!parse_config_file(cfgfile
, basedir
, kernelversion
,
1677 warn("Failed to open config file "
1678 "%s: %s\n", fe
->name
, strerror(errno
));
1680 list_del(&fe
->node
);
1686 if (parse_config_file(filename
, basedir
, kernelversion
, search
,
1695 * parse_toplevel_config - handle top-level depmod.conf, depmod.d
1697 * @filename: possibly overridden config
1698 * @basedir: module base directory
1699 * @kernelversion: kernel version to process
1700 * @search: search path order
1701 * @overrides: module override entries
1704 static void parse_toplevel_config(const char *filename
,
1705 const char *basedir
,
1706 const char *kernelversion
,
1707 struct module_search
**search
,
1708 struct module_overrides
**overrides
)
1711 if (!parse_config_scan(filename
, basedir
, kernelversion
, search
,
1713 fatal("Failed to open config file %s: %s\n",
1714 filename
, strerror(errno
));
1718 /* deprecated config file */
1719 if (parse_config_file("/etc/depmod.conf", basedir
, kernelversion
,
1720 search
, overrides
) > 0)
1721 warn("Deprecated config file /etc/depmod.conf, "
1722 "all config files belong into /etc/depmod.d/.\n");
1724 /* default config */
1725 parse_config_scan("/etc/depmod.d", basedir
, kernelversion
,
1729 /* Local to main, but not freed on exit. Keep valgrind quiet. */
1730 static struct module
*list
= NULL
;
1731 static struct module_search
*search
= NULL
;
1732 static struct module_overrides
*overrides
= NULL
;
1734 int main(int argc
, char *argv
[])
1736 int opt
, all
= 0, maybe_all
= 0, doing_stdout
= 0;
1737 char *basedir
= "", *dirname
, *version
;
1738 char *system_map
= NULL
, *module_symvers
= NULL
;
1740 const char *config
= NULL
;
1742 if (native_endianness() == 0)
1745 while ((opt
= getopt_long(argc
, argv
, "aAb:C:E:F:euqrvnP:hVwm", options
, NULL
))
1756 skipchars
= strlen(basedir
);
1762 module_symvers
= optarg
;
1766 system_map
= optarg
;
1782 if (optarg
[1] != '\0')
1783 fatal("-P only takes a single char\n");
1784 sym_prefix
= optarg
[0];
1787 print_usage(argv
[0]);
1791 printf("%s %s\n", PACKAGE
, VERSION
);
1797 force_map_files
= 1;
1800 print_usage(argv
[0]);
1806 load_module_symvers(module_symvers
);
1807 else if (system_map
)
1808 load_system_map(system_map
);
1809 else if (print_unknown
) {
1810 warn("-e needs -E or -F\n");
1814 /* They can specify the version naked on the command line */
1815 if (optind
< argc
&& is_version_number(argv
[optind
])) {
1816 version
= NOFAIL(strdup(argv
[optind
]));
1821 version
= NOFAIL(strdup(buf
.release
));
1824 /* Check for old version. */
1825 if (old_module_version(version
)) {
1826 fprintf(stderr
, "Kernel version %s requires old depmod\n",
1831 /* Depmod -a by default if no names. */
1835 nofail_asprintf(&dirname
, "%s%s%s", basedir
, MODULE_DIR
, version
);
1838 if (!doing_stdout
&& !depfile_out_of_date(dirname
))
1843 parse_toplevel_config(config
, basedir
, version
, &search
, &overrides
);
1845 /* For backward compatibility add "updates" to the head of the search
1846 * list here. But only if there was no "search" option specified.
1852 nofail_asprintf(&dirname
, "%s%s%s/updates", basedir
,
1853 MODULE_DIR
, version
);
1854 len
= strlen(dirname
);
1855 search
= add_search(dirname
, len
, search
);
1858 /* Do command line args. */
1859 for (opt
= optind
; opt
< argc
; opt
++) {
1862 if (argv
[opt
][0] != '/')
1863 fatal("modules must be specified using absolute paths.\n"
1864 "\"%s\" is a relative path\n", argv
[opt
]);
1866 new = grab_module(NULL
, argv
[opt
]);
1868 /* cmd-line specified modules must exist */
1869 fatal("grab_module() failed for module %s\n", argv
[opt
]);
1875 list
= grab_basedir(dirname
,search
,overrides
);
1877 list
= sort_modules(dirname
,list
);
1878 list
= parse_modules(list
);
1880 for (i
= 0; i
< ARRAY_SIZE(depfiles
); i
++) {
1883 const struct depfile
*d
= &depfiles
[i
];
1884 char depname
[strlen(dirname
) + 1 + strlen(d
->name
) + 1];
1885 char tmpname
[strlen(dirname
) + 1 + strlen(d
->name
) +
1886 strlen(".temp") + 1];
1888 if (d
->map_file
&& !make_map_files
&& !force_map_files
)
1891 sprintf(depname
, "%s/%s", dirname
, d
->name
);
1892 sprintf(tmpname
, "%s/%s.temp", dirname
, d
->name
);
1893 if (!doing_stdout
) {
1894 out
= fopen(tmpname
, "w");
1896 fatal("Could not open %s for writing: %s\n",
1897 tmpname
, strerror(errno
));
1900 if (ends_in(depname
, ".bin"))
1903 res
= d
->func(list
, out
, dirname
);
1908 if (rename(tmpname
, depname
) < 0)
1909 fatal("Could not rename %s into %s: %s\n",
1910 tmpname
, depname
, strerror(errno
));
1912 if (unlink(tmpname
) < 0)
1913 warn("Could not delete %s: %s\n",
1914 tmpname
, strerror(errno
));