1 /* New simplified depmod without backwards compat stuff and not
4 (C) 2002 Rusty Russell IBM Corporation
6 #define _GNU_SOURCE /* asprintf */
15 #include <sys/types.h>
19 #include <sys/utsname.h>
22 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
25 #include "zlibsupport.h"
31 #include "config_filter.h"
36 #define MODULE_DIR "/lib/modules/"
39 #ifndef MODULE_BUILTIN_KEY
40 #define MODULE_BUILTIN_KEY "built-in"
43 struct module_overrides
46 struct module_overrides
*next
;
48 /* overridden module */
55 struct module_search
*next
;
62 static unsigned int skipchars
;
63 static unsigned int make_map_files
= 1; /* default to on */
64 static unsigned int force_map_files
= 0; /* default to on */
66 #define SYMBOL_HASH_SIZE 1024
74 static struct symbol
*symbolhash
[SYMBOL_HASH_SIZE
];
76 /* This is based on the hash agorithm from gdbm, via tdb */
77 static inline unsigned int tdb_hash(const char *name
)
79 unsigned value
; /* Used to compute the hash value. */
80 unsigned i
; /* Used to cycle through random values. */
82 /* Set the initial value from the key size. */
83 for (value
= 0x238F13AF * strlen(name
), i
=0; name
[i
]; i
++)
84 value
= (value
+ (((unsigned char *)name
)[i
] << (i
*5 % 24)));
86 return (1103515243 * value
+ 12345);
89 void add_symbol(const char *name
, struct module
*owner
)
92 struct symbol
*new = NOFAIL(malloc(sizeof *new + strlen(name
) + 1));
95 strcpy(new->name
, name
);
97 hash
= tdb_hash(name
) % SYMBOL_HASH_SIZE
;
98 new->next
= symbolhash
[hash
];
99 symbolhash
[hash
] = new;
102 static int print_unknown
;
104 struct module
*find_symbol(const char *name
, const char *modname
, int weak
)
108 /* For our purposes, .foo matches foo. PPC64 needs this. */
112 for (s
= symbolhash
[tdb_hash(name
) % SYMBOL_HASH_SIZE
]; s
; s
=s
->next
) {
113 if (streq(s
->name
, name
))
117 if (print_unknown
&& !weak
)
118 warn("%s needs unknown symbol %s\n", modname
, name
);
123 void add_dep(struct module
*mod
, struct module
*depends_on
)
127 for (i
= 0; i
< mod
->num_deps
; i
++)
128 if (mod
->deps
[i
] == depends_on
)
131 mod
->deps
= NOFAIL(realloc(mod
->deps
, sizeof(mod
->deps
[0])*(mod
->num_deps
+1)));
132 mod
->deps
[mod
->num_deps
++] = depends_on
;
135 static void load_system_map(const char *filename
)
139 const char ksymstr
[] = "__ksymtab_";
140 const int ksymstr_len
= strlen(ksymstr
);
142 system_map
= fopen(filename
, "r");
144 fatal("Could not open '%s': %s\n", filename
, strerror(errno
));
146 /* eg. c0294200 R __ksymtab_devfs_alloc_devnum */
147 while (fgets(line
, sizeof(line
)-1, system_map
)) {
151 ptr
= strchr(line
, '\n');
155 ptr
= strchr(line
, ' ');
156 if (!ptr
|| !(ptr
= strchr(ptr
+ 1, ' ')))
159 /* Covers gpl-only and normal symbols. */
160 if (strstarts(ptr
+1, ksymstr
))
161 add_symbol(ptr
+1+ksymstr_len
, NULL
);
166 /* __this_module is magic inserted by kernel loader. */
167 add_symbol("__this_module", NULL
);
168 /* On S390, this is faked up too */
169 add_symbol("_GLOBAL_OFFSET_TABLE_", NULL
);
172 static struct option options
[] = { { "all", 0, NULL
, 'a' },
173 { "quick", 0, NULL
, 'A' },
174 { "basedir", 1, NULL
, 'b' },
175 { "errsyms", 0, NULL
, 'e' },
176 { "filesyms", 1, NULL
, 'F' },
177 { "help", 0, NULL
, 'h' },
178 { "show", 0, NULL
, 'n' },
179 { "dry-run", 0, NULL
, 'n' },
180 { "quiet", 0, NULL
, 'q' },
181 { "root", 0, NULL
, 'r' },
182 { "unresolved-error", 0, NULL
, 'u' },
183 { "verbose", 0, NULL
, 'v' },
184 { "version", 0, NULL
, 'V' },
185 { "config", 1, NULL
, 'C' },
186 { "warn", 1, NULL
, 'w' },
187 { "map", 0, NULL
, 'm' },
188 { NULL
, 0, NULL
, 0 } };
190 /* Version number or module name? Don't assume extension. */
191 static int is_version_number(const char *version
)
195 return (sscanf(version
, "%u.%u.%u", &dummy
, &dummy
, &dummy
) == 3);
198 static int old_module_version(const char *version
)
200 /* Expect three part version. */
201 unsigned int major
, sub
, minor
;
203 sscanf(version
, "%u.%u.%u", &major
, &sub
, &minor
);
205 if (major
> 2) return 0;
206 if (major
< 2) return 1;
209 if (sub
> 5) return 0;
210 if (sub
< 5) return 1;
213 if (minor
>= 48) return 0;
217 static void print_usage(const char *name
)
220 "%s " VERSION
" -- part of " PACKAGE
"\n"
221 "%s -[aA] [-n -e -v -q -V -r -u -w -m]\n"
222 " [-b basedirectory] [forced_version]\n"
223 "depmod [-n -e -v -q -r -u -w] [-F kernelsyms] module1.ko module2.ko ...\n"
224 "If no arguments (except options) are given, \"depmod -a\" is assumed\n"
226 "depmod will output a dependancy list suitable for the modprobe utility.\n"
230 "\t-a, --all Probe all modules\n"
231 "\t-A, --quick Only does the work if there's a new module\n"
232 "\t-e, --errsyms Report not supplied symbols\n"
233 "\t-m, --map Create the legacy map files\n"
234 "\t-n, --show Write the dependency file on stdout only\n"
235 "\t-V, --version Print the release version\n"
236 "\t-v, --verbose Enable verbose mode\n"
237 "\t-w, --warn Warn on duplicates\n"
238 "\t-h, --help Print this usage message\n"
240 "The following options are useful for people managing distributions:\n"
241 "\t-b basedirectory\n"
242 "\t --basedir basedirectory Use an image of a module tree.\n"
244 "\t --filesyms kernelsyms Use the file instead of the\n"
245 "\t current kernel symbols.\n",
249 static int ends_in(const char *name
, const char *ext
)
251 unsigned int namelen
, extlen
;
254 namelen
= strlen(name
);
255 extlen
= strlen(ext
);
257 if (namelen
< extlen
) return 0;
259 if (streq(name
+ namelen
- extlen
, ext
))
264 static struct module
*grab_module(const char *dirname
, const char *filename
)
268 new = NOFAIL(malloc(sizeof(*new)
269 + strlen(dirname
?:"") + 1 + strlen(filename
) + 1));
271 sprintf(new->pathname
, "%s/%s", dirname
, filename
);
273 strcpy(new->pathname
, filename
);
274 new->basename
= my_basename(new->pathname
);
276 INIT_LIST_HEAD(&new->dep_list
);
277 new->order
= INDEX_PRIORITY_MIN
;
279 new->file
= grab_elf_file(new->pathname
);
281 warn("Can't read module %s: %s\n",
282 new->pathname
, strerror(errno
));
289 struct module_traverse
291 struct module_traverse
*prev
;
295 static int in_loop(struct module
*mod
, const struct module_traverse
*traverse
)
297 const struct module_traverse
*i
;
299 for (i
= traverse
; i
; i
= i
->prev
) {
306 /* Assume we are doing all the modules, so only report each loop once. */
307 static void report_loop(const struct module
*mod
,
308 const struct module_traverse
*traverse
)
310 const struct module_traverse
*i
;
312 /* Check that start is least alphabetically. eg. a depends
313 on b depends on a will get reported for a, not b. */
314 for (i
= traverse
->prev
; i
->prev
; i
= i
->prev
) {
315 if (strcmp(mod
->pathname
, i
->mod
->pathname
) > 0)
319 /* Is start in the loop? If not, don't report now. eg. a
320 depends on b which depends on c which depends on b. Don't
321 report when generating depends for a. */
325 warn("Loop detected: %s ", mod
->pathname
);
326 for (i
= traverse
->prev
; i
->prev
; i
= i
->prev
)
327 fprintf(stderr
, "needs %s ", i
->mod
->basename
);
328 fprintf(stderr
, "which needs %s again!\n", i
->mod
->basename
);
331 /* This is damn slow, but loops actually happen, and we don't want to
332 just exit() and leave the user without any modules. */
333 static int has_dep_loop(struct module
*module
, struct module_traverse
*prev
)
336 struct module_traverse traverse
= { .prev
= prev
, .mod
= module
};
338 if (in_loop(module
, prev
)) {
339 report_loop(module
, &traverse
);
343 for (i
= 0; i
< module
->num_deps
; i
++)
344 if (has_dep_loop(module
->deps
[i
], &traverse
))
349 /* Uniquifies and orders a dependency list. */
350 static void order_dep_list(struct module
*start
, struct module
*mod
)
354 for (i
= 0; i
< mod
->num_deps
; i
++) {
355 /* If it was previously depended on, move it to the
356 tail. ie. if a needs b and c, and c needs b, we
357 must order b after c. */
358 list_del(&mod
->deps
[i
]->dep_list
);
359 list_add_tail(&mod
->deps
[i
]->dep_list
, &start
->dep_list
);
360 order_dep_list(start
, mod
->deps
[i
]);
364 static struct module
*deleted
= NULL
;
366 static void del_module(struct module
**modules
, struct module
*delme
)
370 /* Find pointer to it. */
372 for (i
= modules
; *i
!= delme
; i
= &(*i
)->next
);
377 /* Save on a list to quiet valgrind.
378 Can't free - other modules may depend on them */
379 delme
->next
= deleted
;
383 /* convert to relative path if possible */
384 static const char *compress_path(const char *path
, const char *basedir
)
386 int len
= strlen(basedir
);
388 if (strncmp(path
, basedir
, len
) == 0)
393 static void output_deps(struct module
*modules
,
394 FILE *out
, char *dirname
)
398 for (i
= modules
; i
; i
= i
->next
) {
399 struct list_head
*j
, *tmp
;
400 order_dep_list(i
, i
);
402 fprintf(out
, "%s:", compress_path(i
->pathname
, dirname
));
403 list_for_each_safe(j
, tmp
, &i
->dep_list
) {
405 = list_entry(j
, struct module
, dep_list
);
407 compress_path(dep
->pathname
, dirname
));
414 /* warn whenever duplicate module aliases, deps, or symbols are found. */
417 static void output_deps_bin(struct module
*modules
,
418 FILE *out
, char *dirname
)
421 struct index_node
*index
;
425 index
= index_create();
427 for (i
= modules
; i
; i
= i
->next
) {
428 struct list_head
*j
, *tmp
;
429 char modname
[strlen(i
->pathname
)+1];
431 order_dep_list(i
, i
);
433 filename2modname(modname
, i
->pathname
);
434 nofail_asprintf(&line
, "%s:",
435 compress_path(i
->pathname
, dirname
));
437 list_for_each_safe(j
, tmp
, &i
->dep_list
) {
439 = list_entry(j
, struct module
, dep_list
);
440 nofail_asprintf(&line
, "%s %s",
442 compress_path(dep
->pathname
, dirname
));
447 if (index_insert(index
, modname
, line
, i
->order
) && warn_dups
)
448 warn("duplicate module deps:\n%s\n",line
);
452 index_write(index
, out
);
453 index_destroy(index
);
457 static int smells_like_module(const char *name
)
459 return ends_in(name
,".ko") || ends_in(name
, ".ko.gz");
462 typedef struct module
*(*do_module_t
)(const char *dirname
,
463 const char *filename
,
465 struct module_search
*search
,
466 struct module_overrides
*overrides
);
468 static int is_higher_priority(const char *newpath
, const char *oldpath
,
469 struct module_search
*search
,
470 struct module_overrides
*overrides
)
472 struct module_search
*tmp
;
473 struct module_overrides
*ovtmp
;
475 int prio_builtin
= -1;
479 /* The names already match, now we check for overrides and directory search
482 for (ovtmp
= overrides
; ovtmp
!= NULL
; ovtmp
= ovtmp
->next
) {
483 if (streq(ovtmp
->modfile
, newpath
))
485 if (streq(ovtmp
->modfile
, oldpath
))
488 for (i
= 0, tmp
= search
; tmp
!= NULL
; tmp
= tmp
->next
, i
++) {
489 if (streq(tmp
->search_path
, MODULE_BUILTIN_KEY
))
491 else if (strncmp(tmp
->search_path
, newpath
, tmp
->len
) == 0)
493 else if (strncmp(tmp
->search_path
, oldpath
, tmp
->len
) == 0)
497 prio_new
= prio_builtin
;
499 prio_old
= prio_builtin
;
501 return prio_new
> prio_old
;
505 static struct module
*do_module(const char *dirname
,
506 const char *filename
,
508 struct module_search
*search
,
509 struct module_overrides
*overrides
)
511 struct module
*new, **i
;
513 new = grab_module(dirname
, filename
);
517 /* Check if module is already in the list. */
518 for (i
= &list
; *i
; i
= &(*i
)->next
) {
520 if (streq((*i
)->basename
, filename
)) {
521 char newpath
[strlen(dirname
) + strlen("/")
522 + strlen(filename
) + 1];
524 sprintf(newpath
, "%s/%s", dirname
, filename
);
526 if (is_higher_priority(newpath
, (*i
)->pathname
,search
,
533 del_module(NULL
, new);
539 /* Not in the list already. Just prepend. */
544 static struct module
*grab_dir(const char *dirname
,
548 struct module_search
*search
,
549 struct module_overrides
*overrides
)
551 struct dirent
*dirent
;
553 while ((dirent
= readdir(dir
)) != NULL
) {
554 if (smells_like_module(dirent
->d_name
))
555 next
= do_mod(dirname
, dirent
->d_name
, next
,
557 else if (!streq(dirent
->d_name
, ".")
558 && !streq(dirent
->d_name
, "..")
559 && !streq(dirent
->d_name
, "source")
560 && !streq(dirent
->d_name
, "build")) {
563 char subdir
[strlen(dirname
) + 1
564 + strlen(dirent
->d_name
) + 1];
565 sprintf(subdir
, "%s/%s", dirname
, dirent
->d_name
);
566 sub
= opendir(subdir
);
568 next
= grab_dir(subdir
, sub
, next
, do_mod
,
577 static struct module
*grab_basedir(const char *dirname
,
578 struct module_search
*search
,
579 struct module_overrides
*overrides
)
584 dir
= opendir(dirname
);
586 warn("Couldn't open directory %s: %s\n",
587 dirname
, strerror(errno
));
590 list
= grab_dir(dirname
, dir
, NULL
, do_module
, search
, overrides
);
596 static struct module
*sort_modules(const char *dirname
, struct module
*list
)
598 struct module
*tlist
= NULL
, **tpos
= &tlist
;
600 int dir_len
= strlen(dirname
) + 1;
601 char file_name
[dir_len
+ strlen("modules.order") + 1];
603 unsigned int linenum
= 0;
605 sprintf(file_name
, "%s/%s", dirname
, "modules.order");
607 modorder
= fopen(file_name
, "r");
609 /* Older kernels don't generate modules.order. Just
610 return if the file doesn't exist. */
613 fatal("Could not open '%s': %s\n", file_name
, strerror(errno
));
616 sprintf(line
, "%s/", dirname
);
618 /* move modules listed in modorder file to tlist in order */
619 while (fgets(line
, sizeof(line
), modorder
)) {
620 struct module
**pos
, *mod
;
621 int len
= strlen(line
);
624 if (line
[len
- 1] == '\n')
625 line
[len
- 1] = '\0';
627 for (pos
= &list
; (mod
= *pos
); pos
= &(*pos
)->next
) {
628 if (streq(line
, mod
->pathname
+ dir_len
)) {
629 mod
->order
= linenum
;
639 /* append the rest */
647 /* Calculate the dependencies for this module */
648 static void calculate_deps(struct module
*module
)
651 struct string_table
*symnames
;
652 struct string_table
*symtypes
;
653 struct elf_file
*file
;
655 module
->num_deps
= 0;
659 symnames
= file
->ops
->load_dep_syms(module
->pathname
, file
, &symtypes
);
660 if (!symnames
|| !symtypes
)
663 for (i
= 0; i
< symnames
->cnt
; i
++) {
665 struct module
*owner
;
668 name
= symnames
->str
[i
];
669 weak
= (*(symtypes
->str
[i
]) == 'W');
670 owner
= find_symbol(name
, module
->pathname
, weak
);
672 info("%s needs \"%s\": %s\n",
673 module
->pathname
, name
,
675 add_dep(module
, owner
);
683 static struct module
*parse_modules(struct module
*list
)
686 struct elf_file
*file
;
687 struct string_table
*syms
;
690 for (i
= list
; i
; i
= i
->next
) {
692 syms
= file
->ops
->load_symbols(file
);
694 for (j
= 0; j
< syms
->cnt
; j
++)
695 add_symbol(syms
->str
[j
], i
);
698 file
->ops
->fetch_tables(file
, &i
->tables
);
701 for (i
= list
; i
; i
= i
->next
)
704 /* Strip out modules with dependency loops. */
706 for (i
= list
; i
; i
= i
->next
) {
707 if (has_dep_loop(i
, NULL
)) {
708 warn("Module %s ignored, due to loop\n",
709 i
->pathname
+ skipchars
);
710 del_module(&list
, i
);
718 /* Simply dump hash table. */
719 static void output_symbols(struct module
*unused
, FILE *out
, char *dirname
)
723 fprintf(out
, "# Aliases for symbols, used by symbol_request().\n");
724 for (i
= 0; i
< SYMBOL_HASH_SIZE
; i
++) {
727 for (s
= symbolhash
[i
]; s
; s
= s
->next
) {
729 char modname
[strlen(s
->owner
->pathname
)+1];
730 filename2modname(modname
, s
->owner
->pathname
);
731 fprintf(out
, "alias symbol:%s %s\n",
738 static void output_symbols_bin(struct module
*unused
, FILE *out
, char *dirname
)
740 struct index_node
*index
;
745 index
= index_create();
747 for (i
= 0; i
< SYMBOL_HASH_SIZE
; i
++) {
750 for (s
= symbolhash
[i
]; s
; s
= s
->next
) {
752 char modname
[strlen(s
->owner
->pathname
)+1];
753 filename2modname(modname
, s
->owner
->pathname
);
754 nofail_asprintf(&alias
, "symbol:%s", s
->name
);
755 duplicate
= index_insert(index
, alias
, modname
,
757 if (duplicate
&& warn_dups
)
758 warn("duplicate module syms:\n%s %s\n",
765 index_write(index
, out
);
766 index_destroy(index
);
769 static void output_aliases(struct module
*modules
, FILE *out
, char *dirname
)
772 struct elf_file
*file
;
773 struct string_table
*tbl
;
776 fprintf(out
, "# Aliases extracted from modules themselves.\n");
777 for (i
= modules
; i
; i
= i
->next
) {
778 char modname
[strlen(i
->pathname
)+1];
781 filename2modname(modname
, i
->pathname
);
783 /* Grab from old-style .modalias section. */
784 tbl
= file
->ops
->load_strings(file
, ".modalias", NULL
, fatal
);
785 for (j
= 0; tbl
&& j
< tbl
->cnt
; j
++)
786 fprintf(out
, "alias %s %s\n", tbl
->str
[j
], modname
);
789 /* Grab from new-style .modinfo section. */
790 tbl
= file
->ops
->load_strings(file
, ".modinfo", NULL
, fatal
);
791 for (j
= 0; tbl
&& j
< tbl
->cnt
; j
++) {
792 const char *p
= tbl
->str
[j
];
793 if (strstarts(p
, "alias="))
794 fprintf(out
, "alias %s %s\n",
795 p
+ strlen("alias="), modname
);
801 static void output_aliases_bin(struct module
*modules
, FILE *out
, char *dirname
)
804 struct elf_file
*file
;
805 struct string_table
*tbl
;
808 struct index_node
*index
;
811 index
= index_create();
813 for (i
= modules
; i
; i
= i
->next
) {
814 char modname
[strlen(i
->pathname
)+1];
817 filename2modname(modname
, i
->pathname
);
819 /* Grab from old-style .modalias section. */
820 tbl
= file
->ops
->load_strings(file
, ".modalias", NULL
, fatal
);
821 for (j
= 0; tbl
&& j
< tbl
->cnt
; j
++) {
822 alias
= NOFAIL(strdup(tbl
->str
[j
]));
824 duplicate
= index_insert(index
, alias
, modname
, i
->order
);
825 if (duplicate
&& warn_dups
)
826 warn("duplicate module alias:\n%s %s\n",
832 /* Grab from new-style .modinfo section. */
833 tbl
= file
->ops
->load_strings(file
, ".modinfo", NULL
, fatal
);
834 for (j
= 0; tbl
&& j
< tbl
->cnt
; j
++) {
835 const char *p
= tbl
->str
[j
];
836 if (strstarts(p
, "alias=")) {
837 alias
= NOFAIL(strdup(p
+ strlen("alias=")));
839 duplicate
= index_insert(index
, alias
, modname
, i
->order
);
840 if (duplicate
&& warn_dups
)
841 warn("duplicate module alias:\n%s %s\n",
849 index_write(index
, out
);
850 index_destroy(index
);
855 void (*func
)(struct module
*, FILE *, char *dirname
);
859 static struct depfile depfiles
[] = {
860 { "modules.dep", output_deps
, 0 }, /* This is what we check for '-A'. */
861 { "modules.dep.bin", output_deps_bin
, 0 },
862 { "modules.pcimap", output_pci_table
, 1 },
863 { "modules.usbmap", output_usb_table
, 1 },
864 { "modules.ccwmap", output_ccw_table
, 1 },
865 { "modules.ieee1394map", output_ieee1394_table
, 1 },
866 { "modules.isapnpmap", output_isapnp_table
, 1 },
867 { "modules.inputmap", output_input_table
, 1 },
868 { "modules.ofmap", output_of_table
, 1 },
869 { "modules.seriomap", output_serio_table
, 1 },
870 { "modules.alias", output_aliases
, 0 },
871 { "modules.alias.bin", output_aliases_bin
, 0 },
872 { "modules.symbols", output_symbols
, 0 },
873 { "modules.symbols.bin", output_symbols_bin
, 0 }
876 /* If we can't figure it out, it's safe to say "true". */
877 static int any_modules_newer(const char *dirname
, time_t mtime
)
880 struct dirent
*dirent
;
882 dir
= opendir(dirname
);
886 while ((dirent
= readdir(dir
)) != NULL
) {
888 char file
[strlen(dirname
) + 1 + strlen(dirent
->d_name
) + 1];
890 if (streq(dirent
->d_name
, ".") || streq(dirent
->d_name
, ".."))
893 sprintf(file
, "%s/%s", dirname
, dirent
->d_name
);
894 if (lstat(file
, &st
) != 0)
897 if (smells_like_module(dirent
->d_name
)) {
898 if (st
.st_mtime
> mtime
)
900 } else if (S_ISDIR(st
.st_mode
)) {
901 if (any_modules_newer(file
, mtime
))
913 static int depfile_out_of_date(const char *dirname
)
916 char depfile
[strlen(dirname
) + 1 + strlen(depfiles
[0].name
) + 1];
918 sprintf(depfile
, "%s/%s", dirname
, depfiles
[0].name
);
920 if (stat(depfile
, &st
) != 0)
923 return any_modules_newer(dirname
, st
.st_mtime
);
926 static char *strsep_skipspace(char **string
, char *delim
)
930 *string
+= strspn(*string
, delim
);
931 return strsep(string
, delim
);
934 static struct module_search
*add_search(const char *search_path
,
936 struct module_search
*search
)
939 struct module_search
*new;
941 new = NOFAIL(malloc(sizeof(*new)));
942 new->search_path
= NOFAIL(strdup(search_path
));
950 static struct module_overrides
*add_override(const char *modfile
,
951 struct module_overrides
*overrides
)
954 struct module_overrides
*new;
956 new = NOFAIL(malloc(sizeof(*new)));
957 new->modfile
= NOFAIL(strdup(modfile
));
958 new->next
= overrides
;
964 static int parse_config_scan(const char *filename
,
966 const char *kernelversion
,
967 struct module_search
**search
,
968 struct module_overrides
**overrides
);
970 static int parse_config_file(const char *filename
,
972 const char *kernelversion
,
973 struct module_search
**search
,
974 struct module_overrides
**overrides
)
977 unsigned int linenum
= 0;
980 cfile
= fopen(filename
, "r");
983 fatal("could not open '%s', reason: %s\n", filename
,
988 while ((line
= getline_wrapped(cfile
, &linenum
)) != NULL
) {
992 cmd
= strsep_skipspace(&ptr
, "\t ");
994 if (cmd
== NULL
|| cmd
[0] == '#' || cmd
[0] == '\0') {
999 if (streq(cmd
, "search")) {
1002 while ((search_path
= strsep_skipspace(&ptr
, "\t "))) {
1006 if (strcmp(search_path
,
1007 MODULE_BUILTIN_KEY
) == 0) {
1008 *search
= add_search(MODULE_BUILTIN_KEY
,
1012 nofail_asprintf(&dirname
, "%s%s%s/%s", basedir
,
1013 MODULE_DIR
, kernelversion
, search_path
);
1014 len
= strlen(dirname
);
1015 *search
= add_search(dirname
, len
, *search
);
1018 } else if (streq(cmd
, "override")) {
1019 char *pathname
= NULL
, *version
, *subdir
;
1020 modname
= strsep_skipspace(&ptr
, "\t ");
1021 version
= strsep_skipspace(&ptr
, "\t ");
1022 subdir
= strsep_skipspace(&ptr
, "\t ");
1024 if (strcmp(version
, kernelversion
) != 0 &&
1025 strcmp(version
, "*") != 0)
1028 nofail_asprintf(&pathname
, "%s%s%s/%s/%s.ko", basedir
,
1029 MODULE_DIR
, kernelversion
, subdir
, modname
);
1031 *overrides
= add_override(pathname
, *overrides
);
1033 } else if (streq(cmd
, "include")) {
1036 newfilename
= strsep_skipspace(&ptr
, "\t ");
1038 grammar(cmd
, filename
, linenum
);
1040 warn("\"include %s\" is deprecated, "
1041 "please use /etc/depmod.d\n", newfilename
);
1042 if (strstarts(newfilename
, "/etc/depmod.d")) {
1043 warn("\"include /etc/depmod.d\" is "
1044 "the default, ignored\n");
1046 if (!parse_config_scan(newfilename
, basedir
,
1049 warn("Failed to open included"
1050 " config file %s: %s\n",
1051 newfilename
, strerror(errno
));
1054 } else if (streq(cmd
, "make_map_files")) {
1057 option
= strsep_skipspace(&ptr
, "\t ");
1059 grammar(cmd
, filename
, linenum
);
1061 if (streq(option
, "yes"))
1063 else if (streq(option
, "no"))
1066 grammar(cmd
, filename
, linenum
);
1069 grammar(cmd
, filename
, linenum
);
1077 static int parse_config_scan(const char *filename
,
1078 const char *basedir
,
1079 const char *kernelversion
,
1080 struct module_search
**search
,
1081 struct module_overrides
**overrides
)
1086 dir
= opendir(filename
);
1089 struct list_head node
;
1092 LIST_HEAD(files_list
);
1093 struct file_entry
*fe
, *fe_tmp
;
1096 /* sort files from directory into list */
1097 while ((i
= readdir(dir
)) != NULL
) {
1100 if (i
->d_name
[0] == '.')
1102 if (!config_filter(i
->d_name
))
1105 len
= strlen(i
->d_name
);
1106 if (len
< 6 || strcmp(&i
->d_name
[len
-5], ".conf") != 0)
1107 warn("All config files need .conf: %s/%s, "
1108 "it will be ignored in a future release.\n",
1109 filename
, i
->d_name
);
1110 fe
= malloc(sizeof(struct file_entry
) + len
+ 1);
1113 strcpy(fe
->name
, i
->d_name
);
1114 list_for_each_entry(fe_tmp
, &files_list
, node
)
1115 if (strcmp(fe_tmp
->name
, fe
->name
) >= 0)
1117 list_add_tail(&fe
->node
, &fe_tmp
->node
);
1121 /* parse list of files */
1122 list_for_each_entry_safe(fe
, fe_tmp
, &files_list
, node
) {
1125 nofail_asprintf(&cfgfile
, "%s/%s", filename
, fe
->name
);
1126 if (!parse_config_file(cfgfile
, basedir
, kernelversion
,
1128 warn("Failed to open config file "
1129 "%s: %s\n", fe
->name
, strerror(errno
));
1131 list_del(&fe
->node
);
1137 if (parse_config_file(filename
, basedir
, kernelversion
, search
,
1145 static void parse_toplevel_config(const char *filename
,
1146 const char *basedir
,
1147 const char *kernelversion
,
1148 struct module_search
**search
,
1149 struct module_overrides
**overrides
)
1152 if (!parse_config_scan(filename
, basedir
, kernelversion
, search
,
1154 fatal("Failed to open config file %s: %s\n",
1155 filename
, strerror(errno
));
1159 /* deprecated config file */
1160 if (parse_config_file("/etc/depmod.conf", basedir
, kernelversion
,
1161 search
, overrides
) > 0)
1162 warn("Deprecated config file /etc/depmod.conf, "
1163 "all config files belong into /etc/depmod.d/.\n");
1165 /* default config */
1166 parse_config_scan("/etc/depmod.d", basedir
, kernelversion
,
1170 /* Local to main, but not freed on exit. Keep valgrind quiet. */
1171 struct module
*list
= NULL
;
1172 struct module_search
*search
= NULL
;
1173 struct module_overrides
*overrides
= NULL
;
1175 int main(int argc
, char *argv
[])
1177 int opt
, all
= 0, maybe_all
= 0, doing_stdout
= 0;
1178 char *basedir
= "", *dirname
, *version
, *badopt
= NULL
,
1181 const char *config
= NULL
;
1183 if (native_endianness() == 0)
1186 /* Don't print out any errors just yet, we might want to exec
1187 backwards compat version. */
1189 while ((opt
= getopt_long(argc
, argv
, "ab:ArehnqruvVF:C:wm", options
, NULL
))
1197 skipchars
= strlen(basedir
);
1203 system_map
= optarg
;
1219 print_usage(argv
[0]);
1226 printf("%s %s\n", PACKAGE
, VERSION
);
1232 force_map_files
= 1;
1235 badopt
= argv
[optind
-1];
1239 /* We can't print unknowns without a System.map */
1243 load_system_map(system_map
);
1245 /* They can specify the version naked on the command line */
1246 if (optind
< argc
&& is_version_number(argv
[optind
])) {
1247 version
= NOFAIL(strdup(argv
[optind
]));
1252 version
= NOFAIL(strdup(buf
.release
));
1255 /* Check for old version. */
1256 if (old_module_version(version
)) {
1257 fprintf(stderr
, "Kernel version %s requires old depmod\n",
1263 fprintf(stderr
, "%s: malformed/unrecognized option '%s'\n",
1265 print_usage(argv
[0]);
1269 /* Depmod -a by default if no names. */
1273 nofail_asprintf(&dirname
, "%s%s%s", basedir
, MODULE_DIR
, version
);
1276 if (!doing_stdout
&& !depfile_out_of_date(dirname
))
1281 parse_toplevel_config(config
, basedir
, version
, &search
, &overrides
);
1283 /* For backward compatibility add "updates" to the head of the search
1284 * list here. But only if there was no "search" option specified.
1290 nofail_asprintf(&dirname
, "%s%s%s/updates", basedir
,
1291 MODULE_DIR
, version
);
1292 len
= strlen(dirname
);
1293 search
= add_search(dirname
, len
, search
);
1296 /* Do command line args. */
1297 for (opt
= optind
; opt
< argc
; opt
++) {
1300 if (argv
[opt
][0] != '/')
1301 fatal("modules must be specified using absolute paths.\n"
1302 "\"%s\" is a relative path\n", argv
[opt
]);
1304 new = grab_module(NULL
, argv
[opt
]);
1306 /* cmd-line specified modules must exist */
1307 fatal("grab_module() failed for module %s\n", argv
[opt
]);
1313 list
= grab_basedir(dirname
,search
,overrides
);
1315 list
= sort_modules(dirname
,list
);
1316 list
= parse_modules(list
);
1318 for (i
= 0; i
< sizeof(depfiles
)/sizeof(depfiles
[0]); i
++) {
1320 struct depfile
*d
= &depfiles
[i
];
1321 char depname
[strlen(dirname
) + 1 + strlen(d
->name
) + 1];
1322 char tmpname
[strlen(dirname
) + 1 + strlen(d
->name
) +
1323 strlen(".temp") + 1];
1325 if (d
->map_file
&& !make_map_files
&& !force_map_files
)
1328 sprintf(depname
, "%s/%s", dirname
, d
->name
);
1329 sprintf(tmpname
, "%s/%s.temp", dirname
, d
->name
);
1330 if (!doing_stdout
) {
1331 out
= fopen(tmpname
, "w");
1333 fatal("Could not open %s for writing: %s\n",
1334 tmpname
, strerror(errno
));
1337 if (ends_in(depname
, ".bin"))
1340 d
->func(list
, out
, dirname
);
1341 if (!doing_stdout
) {
1343 if (rename(tmpname
, depname
) < 0)
1344 fatal("Could not rename %s into %s: %s\n",
1345 tmpname
, depname
, strerror(errno
));