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"
29 #include "moduleops.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 exec_old_depmod(char *argv
[])
220 char pathname
[strlen(argv
[0])+1];
221 char oldname
[strlen("depmod") + strlen(argv
[0]) + sizeof(".old")];
223 memset(pathname
, 0, strlen(argv
[0])+1);
224 sep
= strrchr(argv
[0], '/');
226 memcpy(pathname
, argv
[0], sep
- argv
[0]+1);
227 sprintf(oldname
, "%s%s.old", pathname
, "depmod");
229 /* Recursion detection: we need an env var since we can't
230 change argv[0] (as older modutils uses it to determine
232 if (getenv("MODULE_RECURSE"))
234 setenv("MODULE_RECURSE", "y", 0);
236 execvp(oldname
, argv
);
238 "Version requires old depmod, but couldn't run %s: %s\n",
239 oldname
, strerror(errno
));
244 static void print_usage(const char *name
)
247 "%s " VERSION
" -- part of " PACKAGE
"\n"
248 "%s -[aA] [-n -e -v -q -V -r -u -w -m]\n"
249 " [-b basedirectory] [forced_version]\n"
250 "depmod [-n -e -v -q -r -u -w] [-F kernelsyms] module1.ko module2.ko ...\n"
251 "If no arguments (except options) are given, \"depmod -a\" is assumed\n"
253 "depmod will output a dependancy list suitable for the modprobe utility.\n"
257 "\t-a, --all Probe all modules\n"
258 "\t-A, --quick Only does the work if there's a new module\n"
259 "\t-e, --errsyms Report not supplied symbols\n"
260 "\t-m, --map Create the legacy map files\n"
261 "\t-n, --show Write the dependency file on stdout only\n"
262 "\t-V, --version Print the release version\n"
263 "\t-v, --verbose Enable verbose mode\n"
264 "\t-w, --warn Warn on duplicates\n"
265 "\t-h, --help Print this usage message\n"
267 "The following options are useful for people managing distributions:\n"
268 "\t-b basedirectory\n"
269 "\t --basedir basedirectory Use an image of a module tree.\n"
271 "\t --filesyms kernelsyms Use the file instead of the\n"
272 "\t current kernel symbols.\n",
276 static int ends_in(const char *name
, const char *ext
)
278 unsigned int namelen
, extlen
;
281 namelen
= strlen(name
);
282 extlen
= strlen(ext
);
284 if (namelen
< extlen
) return 0;
286 if (streq(name
+ namelen
- extlen
, ext
))
291 /* "\177ELF" <byte> where byte = 001 for 32-bit, 002 for 64 */
292 int needconv(const char *elfhdr
)
294 union { short s
; char c
[2]; } endian_test
;
297 if (endian_test
.c
[1] == 1) return elfhdr
[EI_DATA
] != ELFDATA2MSB
;
298 if (endian_test
.c
[0] == 1) return elfhdr
[EI_DATA
] != ELFDATA2LSB
;
303 static char *my_basename(const char *name
)
305 const char *base
= strrchr(name
, '/');
306 if (base
) return (char *)base
+ 1;
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->data
= grab_file(new->pathname
, &new->len
);
327 warn("Can't read module %s: %s\n",
328 new->pathname
, strerror(errno
));
332 /* "\177ELF" <byte> where byte = 001 for 32-bit, 002 for 64 */
333 if (memcmp(new->data
, ELFMAG
, SELFMAG
) != 0) {
334 warn("Module %s is not an elf object\n", new->pathname
);
338 switch (((char *)new->data
)[EI_CLASS
]) {
340 new->ops
= &mod_ops32
;
343 new->ops
= &mod_ops64
;
346 warn("Module %s has elf unknown identifier %i\n",
347 new->pathname
, ((char *)new->data
)[EI_CLASS
]);
350 new->conv
= needconv(new->data
);
354 release_file(new->data
, new->len
);
360 struct module_traverse
362 struct module_traverse
*prev
;
366 static int in_loop(struct module
*mod
, const struct module_traverse
*traverse
)
368 const struct module_traverse
*i
;
370 for (i
= traverse
; i
; i
= i
->prev
) {
377 /* Assume we are doing all the modules, so only report each loop once. */
378 static void report_loop(const struct module
*mod
,
379 const struct module_traverse
*traverse
)
381 const struct module_traverse
*i
;
383 /* Check that start is least alphabetically. eg. a depends
384 on b depends on a will get reported for a, not b. */
385 for (i
= traverse
->prev
; i
->prev
; i
= i
->prev
) {
386 if (strcmp(mod
->pathname
, i
->mod
->pathname
) > 0)
390 /* Is start in the loop? If not, don't report now. eg. a
391 depends on b which depends on c which depends on b. Don't
392 report when generating depends for a. */
396 warn("Loop detected: %s ", mod
->pathname
);
397 for (i
= traverse
->prev
; i
->prev
; i
= i
->prev
)
398 fprintf(stderr
, "needs %s ", i
->mod
->basename
);
399 fprintf(stderr
, "which needs %s again!\n", i
->mod
->basename
);
402 /* This is damn slow, but loops actually happen, and we don't want to
403 just exit() and leave the user without any modules. */
404 static int has_dep_loop(struct module
*module
, struct module_traverse
*prev
)
407 struct module_traverse traverse
= { .prev
= prev
, .mod
= module
};
409 if (in_loop(module
, prev
)) {
410 report_loop(module
, &traverse
);
414 for (i
= 0; i
< module
->num_deps
; i
++)
415 if (has_dep_loop(module
->deps
[i
], &traverse
))
420 /* Uniquifies and orders a dependency list. */
421 static void order_dep_list(struct module
*start
, struct module
*mod
)
425 for (i
= 0; i
< mod
->num_deps
; i
++) {
426 /* If it was previously depended on, move it to the
427 tail. ie. if a needs b and c, and c needs b, we
428 must order b after c. */
429 list_del(&mod
->deps
[i
]->dep_list
);
430 list_add_tail(&mod
->deps
[i
]->dep_list
, &start
->dep_list
);
431 order_dep_list(start
, mod
->deps
[i
]);
435 static struct module
*deleted
= NULL
;
437 static void del_module(struct module
**modules
, struct module
*delme
)
441 /* Find pointer to it. */
443 for (i
= modules
; *i
!= delme
; i
= &(*i
)->next
);
448 /* Save on a list to quiet valgrind.
449 Can't free - other modules may depend on them */
450 delme
->next
= deleted
;
454 /* convert to relative path if possible */
455 static const char *compress_path(const char *path
, const char *basedir
)
457 int len
= strlen(basedir
);
459 if (strncmp(path
, basedir
, len
) == 0)
464 static void output_deps(struct module
*modules
,
465 FILE *out
, char *dirname
)
469 for (i
= modules
; i
; i
= i
->next
) {
470 struct list_head
*j
, *tmp
;
471 order_dep_list(i
, i
);
473 fprintf(out
, "%s:", compress_path(i
->pathname
, dirname
));
474 list_for_each_safe(j
, tmp
, &i
->dep_list
) {
476 = list_entry(j
, struct module
, dep_list
);
478 compress_path(dep
->pathname
, dirname
));
485 /* warn whenever duplicate module aliases, deps, or symbols are found. */
488 static void output_deps_bin(struct module
*modules
,
489 FILE *out
, char *dirname
)
492 struct index_node
*index
;
496 index
= index_create();
498 for (i
= modules
; i
; i
= i
->next
) {
499 struct list_head
*j
, *tmp
;
500 char modname
[strlen(i
->pathname
)+1];
502 order_dep_list(i
, i
);
504 filename2modname(modname
, i
->pathname
);
505 nofail_asprintf(&line
, "%s:",
506 compress_path(i
->pathname
, dirname
));
508 list_for_each_safe(j
, tmp
, &i
->dep_list
) {
510 = list_entry(j
, struct module
, dep_list
);
511 nofail_asprintf(&line
, "%s %s",
513 compress_path(dep
->pathname
, dirname
));
518 if (index_insert(index
, modname
, line
, i
->order
) && warn_dups
)
519 warn("duplicate module deps:\n%s\n",line
);
523 index_write(index
, out
);
524 index_destroy(index
);
528 static int smells_like_module(const char *name
)
530 return ends_in(name
,".ko") || ends_in(name
, ".ko.gz");
533 typedef struct module
*(*do_module_t
)(const char *dirname
,
534 const char *filename
,
536 struct module_search
*search
,
537 struct module_overrides
*overrides
);
539 static int is_higher_priority(const char *newpath
, const char *oldpath
,
540 struct module_search
*search
,
541 struct module_overrides
*overrides
)
543 struct module_search
*tmp
;
544 struct module_overrides
*ovtmp
;
546 int prio_builtin
= -1;
550 /* The names already match, now we check for overrides and directory search
553 for (ovtmp
= overrides
; ovtmp
!= NULL
; ovtmp
= ovtmp
->next
) {
554 if (streq(ovtmp
->modfile
, newpath
))
556 if (streq(ovtmp
->modfile
, oldpath
))
559 for (i
= 0, tmp
= search
; tmp
!= NULL
; tmp
= tmp
->next
, i
++) {
560 if (streq(tmp
->search_path
, MODULE_BUILTIN_KEY
))
562 else if (strncmp(tmp
->search_path
, newpath
, tmp
->len
) == 0)
564 else if (strncmp(tmp
->search_path
, oldpath
, tmp
->len
) == 0)
568 prio_new
= prio_builtin
;
570 prio_old
= prio_builtin
;
572 return prio_new
> prio_old
;
576 static struct module
*do_module(const char *dirname
,
577 const char *filename
,
579 struct module_search
*search
,
580 struct module_overrides
*overrides
)
582 struct module
*new, **i
;
584 new = grab_module(dirname
, filename
);
588 /* Check if module is already in the list. */
589 for (i
= &list
; *i
; i
= &(*i
)->next
) {
591 if (streq((*i
)->basename
, filename
)) {
592 char newpath
[strlen(dirname
) + strlen("/")
593 + strlen(filename
) + 1];
595 sprintf(newpath
, "%s/%s", dirname
, filename
);
597 if (is_higher_priority(newpath
, (*i
)->pathname
,search
,
604 del_module(NULL
, new);
610 /* Not in the list already. Just prepend. */
615 static struct module
*grab_dir(const char *dirname
,
619 struct module_search
*search
,
620 struct module_overrides
*overrides
)
622 struct dirent
*dirent
;
624 while ((dirent
= readdir(dir
)) != NULL
) {
625 if (smells_like_module(dirent
->d_name
))
626 next
= do_mod(dirname
, dirent
->d_name
, next
,
628 else if (!streq(dirent
->d_name
, ".")
629 && !streq(dirent
->d_name
, "..")
630 && !streq(dirent
->d_name
, "source")
631 && !streq(dirent
->d_name
, "build")) {
634 char subdir
[strlen(dirname
) + 1
635 + strlen(dirent
->d_name
) + 1];
636 sprintf(subdir
, "%s/%s", dirname
, dirent
->d_name
);
637 sub
= opendir(subdir
);
639 next
= grab_dir(subdir
, sub
, next
, do_mod
,
648 static struct module
*grab_basedir(const char *dirname
,
649 struct module_search
*search
,
650 struct module_overrides
*overrides
)
655 dir
= opendir(dirname
);
657 warn("Couldn't open directory %s: %s\n",
658 dirname
, strerror(errno
));
661 list
= grab_dir(dirname
, dir
, NULL
, do_module
, search
, overrides
);
667 static struct module
*sort_modules(const char *dirname
, struct module
*list
)
669 struct module
*tlist
= NULL
, **tpos
= &tlist
;
671 int dir_len
= strlen(dirname
) + 1;
672 char file_name
[dir_len
+ strlen("modules.order") + 1];
674 unsigned int linenum
= 0;
676 sprintf(file_name
, "%s/%s", dirname
, "modules.order");
678 modorder
= fopen(file_name
, "r");
680 /* Older kernels don't generate modules.order. Just
681 return if the file doesn't exist. */
684 fatal("Could not open '%s': %s\n", file_name
, strerror(errno
));
687 sprintf(line
, "%s/", dirname
);
689 /* move modules listed in modorder file to tlist in order */
690 while (fgets(line
, sizeof(line
), modorder
)) {
691 struct module
**pos
, *mod
;
692 int len
= strlen(line
);
695 if (line
[len
- 1] == '\n')
696 line
[len
- 1] = '\0';
698 for (pos
= &list
; (mod
= *pos
); pos
= &(*pos
)->next
) {
699 if (streq(line
, mod
->pathname
+ dir_len
)) {
700 mod
->order
= linenum
;
710 /* append the rest */
718 static struct module
*parse_modules(struct module
*list
)
722 for (i
= list
; i
; i
= i
->next
) {
723 i
->ops
->load_symbols(i
);
724 i
->ops
->fetch_tables(i
);
727 for (i
= list
; i
; i
= i
->next
)
728 i
->ops
->calculate_deps(i
);
730 /* Strip out modules with dependency loops. */
732 for (i
= list
; i
; i
= i
->next
) {
733 if (has_dep_loop(i
, NULL
)) {
734 warn("Module %s ignored, due to loop\n",
735 i
->pathname
+ skipchars
);
736 del_module(&list
, i
);
744 /* Simply dump hash table. */
745 static void output_symbols(struct module
*unused
, FILE *out
, char *dirname
)
749 fprintf(out
, "# Aliases for symbols, used by symbol_request().\n");
750 for (i
= 0; i
< SYMBOL_HASH_SIZE
; i
++) {
753 for (s
= symbolhash
[i
]; s
; s
= s
->next
) {
755 char modname
[strlen(s
->owner
->pathname
)+1];
756 filename2modname(modname
, s
->owner
->pathname
);
757 fprintf(out
, "alias symbol:%s %s\n",
764 static void output_symbols_bin(struct module
*unused
, FILE *out
, char *dirname
)
766 struct index_node
*index
;
771 index
= index_create();
773 for (i
= 0; i
< SYMBOL_HASH_SIZE
; i
++) {
776 for (s
= symbolhash
[i
]; s
; s
= s
->next
) {
778 char modname
[strlen(s
->owner
->pathname
)+1];
779 filename2modname(modname
, s
->owner
->pathname
);
780 nofail_asprintf(&alias
, "symbol:%s", s
->name
);
781 duplicate
= index_insert(index
, alias
, modname
,
783 if (duplicate
&& warn_dups
)
784 warn("duplicate module syms:\n%s %s\n",
791 index_write(index
, out
);
792 index_destroy(index
);
795 static void output_aliases(struct module
*modules
, FILE *out
, char *dirname
)
801 fprintf(out
, "# Aliases extracted from modules themselves.\n");
802 for (i
= modules
; i
; i
= i
->next
) {
803 char modname
[strlen(i
->pathname
)+1];
805 filename2modname(modname
, i
->pathname
);
807 /* Grab from old-style .modalias section. */
808 for (p
= i
->ops
->get_aliases(i
, &size
);
810 p
= next_string(p
, &size
))
811 fprintf(out
, "alias %s %s\n", p
, modname
);
813 /* Grab form new-style .modinfo section. */
814 for (p
= i
->ops
->get_modinfo(i
, &size
);
816 p
= next_string(p
, &size
)) {
817 if (strstarts(p
, "alias="))
818 fprintf(out
, "alias %s %s\n",
819 p
+ strlen("alias="), modname
);
824 static void output_aliases_bin(struct module
*modules
, FILE *out
, char *dirname
)
830 struct index_node
*index
;
833 index
= index_create();
835 for (i
= modules
; i
; i
= i
->next
) {
836 char modname
[strlen(i
->pathname
)+1];
838 filename2modname(modname
, i
->pathname
);
840 /* Grab from old-style .modalias section. */
841 for (p
= i
->ops
->get_aliases(i
, &size
);
843 p
= next_string(p
, &size
)) {
844 alias
= NOFAIL(strdup(p
));
846 duplicate
= index_insert(index
, alias
, modname
, i
->order
);
847 if (duplicate
&& warn_dups
)
848 warn("duplicate module alias:\n%s %s\n",
853 /* Grab from new-style .modinfo section. */
854 for (p
= i
->ops
->get_modinfo(i
, &size
);
856 p
= next_string(p
, &size
)) {
857 if (strstarts(p
, "alias=")) {
858 alias
= NOFAIL(strdup(p
+ strlen("alias=")));
860 duplicate
= index_insert(index
, alias
, modname
, i
->order
);
861 if (duplicate
&& warn_dups
)
862 warn("duplicate module alias:\n%s %s\n",
869 index_write(index
, out
);
870 index_destroy(index
);
875 void (*func
)(struct module
*, FILE *, char *dirname
);
879 static struct depfile depfiles
[] = {
880 { "modules.dep", output_deps
, 0 }, /* This is what we check for '-A'. */
881 { "modules.dep.bin", output_deps_bin
, 0 },
882 { "modules.pcimap", output_pci_table
, 1 },
883 { "modules.usbmap", output_usb_table
, 1 },
884 { "modules.ccwmap", output_ccw_table
, 1 },
885 { "modules.ieee1394map", output_ieee1394_table
, 1 },
886 { "modules.isapnpmap", output_isapnp_table
, 1 },
887 { "modules.inputmap", output_input_table
, 1 },
888 { "modules.ofmap", output_of_table
, 1 },
889 { "modules.seriomap", output_serio_table
, 1 },
890 { "modules.alias", output_aliases
, 0 },
891 { "modules.alias.bin", output_aliases_bin
, 0 },
892 { "modules.symbols", output_symbols
, 0 },
893 { "modules.symbols.bin", output_symbols_bin
, 0 }
896 /* If we can't figure it out, it's safe to say "true". */
897 static int any_modules_newer(const char *dirname
, time_t mtime
)
900 struct dirent
*dirent
;
902 dir
= opendir(dirname
);
906 while ((dirent
= readdir(dir
)) != NULL
) {
908 char file
[strlen(dirname
) + 1 + strlen(dirent
->d_name
) + 1];
910 if (streq(dirent
->d_name
, ".") || streq(dirent
->d_name
, ".."))
913 sprintf(file
, "%s/%s", dirname
, dirent
->d_name
);
914 if (lstat(file
, &st
) != 0)
917 if (smells_like_module(dirent
->d_name
)) {
918 if (st
.st_mtime
> mtime
)
920 } else if (S_ISDIR(st
.st_mode
)) {
921 if (any_modules_newer(file
, mtime
))
933 static int depfile_out_of_date(const char *dirname
)
936 char depfile
[strlen(dirname
) + 1 + strlen(depfiles
[0].name
) + 1];
938 sprintf(depfile
, "%s/%s", dirname
, depfiles
[0].name
);
940 if (stat(depfile
, &st
) != 0)
943 return any_modules_newer(dirname
, st
.st_mtime
);
946 static char *strsep_skipspace(char **string
, char *delim
)
950 *string
+= strspn(*string
, delim
);
951 return strsep(string
, delim
);
954 static struct module_search
*add_search(const char *search_path
,
956 struct module_search
*search
)
959 struct module_search
*new;
961 new = NOFAIL(malloc(sizeof(*new)));
962 new->search_path
= NOFAIL(strdup(search_path
));
970 static struct module_overrides
*add_override(const char *modfile
,
971 struct module_overrides
*overrides
)
974 struct module_overrides
*new;
976 new = NOFAIL(malloc(sizeof(*new)));
977 new->modfile
= NOFAIL(strdup(modfile
));
978 new->next
= overrides
;
984 static int parse_config_scan(const char *filename
,
986 const char *kernelversion
,
987 struct module_search
**search
,
988 struct module_overrides
**overrides
);
990 static int parse_config_file(const char *filename
,
992 const char *kernelversion
,
993 struct module_search
**search
,
994 struct module_overrides
**overrides
)
997 unsigned int linenum
= 0;
1000 cfile
= fopen(filename
, "r");
1002 if (errno
!= ENOENT
)
1003 fatal("could not open '%s', reason: %s\n", filename
,
1008 while ((line
= getline_wrapped(cfile
, &linenum
)) != NULL
) {
1010 char *cmd
, *modname
;
1012 cmd
= strsep_skipspace(&ptr
, "\t ");
1014 if (cmd
== NULL
|| cmd
[0] == '#' || cmd
[0] == '\0') {
1019 if (streq(cmd
, "search")) {
1022 while ((search_path
= strsep_skipspace(&ptr
, "\t "))) {
1026 if (strcmp(search_path
,
1027 MODULE_BUILTIN_KEY
) == 0) {
1028 *search
= add_search(MODULE_BUILTIN_KEY
,
1032 nofail_asprintf(&dirname
, "%s%s%s/%s", basedir
,
1033 MODULE_DIR
, kernelversion
, search_path
);
1034 len
= strlen(dirname
);
1035 *search
= add_search(dirname
, len
, *search
);
1038 } else if (streq(cmd
, "override")) {
1039 char *pathname
= NULL
, *version
, *subdir
;
1040 modname
= strsep_skipspace(&ptr
, "\t ");
1041 version
= strsep_skipspace(&ptr
, "\t ");
1042 subdir
= strsep_skipspace(&ptr
, "\t ");
1044 if (strcmp(version
, kernelversion
) != 0 &&
1045 strcmp(version
, "*") != 0)
1048 nofail_asprintf(&pathname
, "%s%s%s/%s/%s.ko", basedir
,
1049 MODULE_DIR
, kernelversion
, subdir
, modname
);
1051 *overrides
= add_override(pathname
, *overrides
);
1053 } else if (streq(cmd
, "include")) {
1056 newfilename
= strsep_skipspace(&ptr
, "\t ");
1058 grammar(cmd
, filename
, linenum
);
1060 warn("\"include %s\" is deprecated, "
1061 "please use /etc/depmod.d\n", newfilename
);
1062 if (strstarts(newfilename
, "/etc/depmod.d")) {
1063 warn("\"include /etc/depmod.d\" is "
1064 "the default, ignored\n");
1066 if (!parse_config_scan(newfilename
, basedir
,
1069 warn("Failed to open included"
1070 " config file %s: %s\n",
1071 newfilename
, strerror(errno
));
1074 } else if (streq(cmd
, "make_map_files")) {
1077 option
= strsep_skipspace(&ptr
, "\t ");
1079 grammar(cmd
, filename
, linenum
);
1081 if (streq(option
, "yes"))
1083 else if (streq(option
, "no"))
1086 grammar(cmd
, filename
, linenum
);
1089 grammar(cmd
, filename
, linenum
);
1097 static int parse_config_scan(const char *filename
,
1098 const char *basedir
,
1099 const char *kernelversion
,
1100 struct module_search
**search
,
1101 struct module_overrides
**overrides
)
1106 dir
= opendir(filename
);
1109 struct list_head node
;
1112 LIST_HEAD(files_list
);
1113 struct file_entry
*fe
, *fe_tmp
;
1116 /* sort files from directory into list */
1117 while ((i
= readdir(dir
)) != NULL
) {
1120 if (i
->d_name
[0] == '.')
1122 if (!config_filter(i
->d_name
))
1125 len
= strlen(i
->d_name
);
1126 if (len
< 6 || strcmp(&i
->d_name
[len
-5], ".conf") != 0)
1127 warn("All config files need .conf: %s/%s, "
1128 "it will be ignored in a future release.\n",
1129 filename
, i
->d_name
);
1130 fe
= malloc(sizeof(struct file_entry
) + len
+ 1);
1133 strcpy(fe
->name
, i
->d_name
);
1134 list_for_each_entry(fe_tmp
, &files_list
, node
)
1135 if (strcmp(fe_tmp
->name
, fe
->name
) >= 0)
1137 list_add_tail(&fe
->node
, &fe_tmp
->node
);
1141 /* parse list of files */
1142 list_for_each_entry_safe(fe
, fe_tmp
, &files_list
, node
) {
1145 nofail_asprintf(&cfgfile
, "%s/%s", filename
, fe
->name
);
1146 if (!parse_config_file(cfgfile
, basedir
, kernelversion
,
1148 warn("Failed to open config file "
1149 "%s: %s\n", fe
->name
, strerror(errno
));
1151 list_del(&fe
->node
);
1157 if (parse_config_file(filename
, basedir
, kernelversion
, search
,
1165 static void parse_toplevel_config(const char *filename
,
1166 const char *basedir
,
1167 const char *kernelversion
,
1168 struct module_search
**search
,
1169 struct module_overrides
**overrides
)
1172 if (!parse_config_scan(filename
, basedir
, kernelversion
, search
,
1174 fatal("Failed to open config file %s: %s\n",
1175 filename
, strerror(errno
));
1179 /* deprecated config file */
1180 if (parse_config_file("/etc/depmod.conf", basedir
, kernelversion
,
1181 search
, overrides
) > 0)
1182 warn("Deprecated config file /etc/depmod.conf, "
1183 "all config files belong into /etc/depmod.d/.\n");
1185 /* default config */
1186 parse_config_scan("/etc/depmod.d", basedir
, kernelversion
,
1190 /* Local to main, but not freed on exit. Keep valgrind quiet. */
1191 struct module
*list
= NULL
;
1192 struct module_search
*search
= NULL
;
1193 struct module_overrides
*overrides
= NULL
;
1195 int main(int argc
, char *argv
[])
1197 int opt
, all
= 0, maybe_all
= 0, doing_stdout
= 0;
1198 char *basedir
= "", *dirname
, *version
, *badopt
= NULL
,
1201 const char *config
= NULL
;
1203 /* Don't print out any errors just yet, we might want to exec
1204 backwards compat version. */
1206 while ((opt
= getopt_long(argc
, argv
, "ab:ArehnqruvVF:C:wm", options
, NULL
))
1214 skipchars
= strlen(basedir
);
1220 system_map
= optarg
;
1236 print_usage(argv
[0]);
1243 printf("%s %s\n", PACKAGE
, VERSION
);
1249 force_map_files
= 1;
1252 badopt
= argv
[optind
-1];
1256 /* We can't print unknowns without a System.map */
1260 load_system_map(system_map
);
1262 /* They can specify the version naked on the command line */
1263 if (optind
< argc
&& is_version_number(argv
[optind
])) {
1264 version
= NOFAIL(strdup(argv
[optind
]));
1269 version
= NOFAIL(strdup(buf
.release
));
1272 /* Run old version if required. */
1273 if (old_module_version(version
))
1274 exec_old_depmod(argv
);
1277 fprintf(stderr
, "%s: malformed/unrecognized option '%s'\n",
1279 print_usage(argv
[0]);
1283 /* Depmod -a by default if no names. */
1287 nofail_asprintf(&dirname
, "%s%s%s", basedir
, MODULE_DIR
, version
);
1290 if (!doing_stdout
&& !depfile_out_of_date(dirname
))
1295 parse_toplevel_config(config
, basedir
, version
, &search
, &overrides
);
1297 /* For backward compatibility add "updates" to the head of the search
1298 * list here. But only if there was no "search" option specified.
1304 nofail_asprintf(&dirname
, "%s%s%s/updates", basedir
,
1305 MODULE_DIR
, version
);
1306 len
= strlen(dirname
);
1307 search
= add_search(dirname
, len
, search
);
1310 /* Do command line args. */
1311 for (opt
= optind
; opt
< argc
; opt
++) {
1314 if (argv
[opt
][0] != '/')
1315 fatal("modules must be specified using absolute paths.\n"
1316 "\"%s\" is a relative path\n", argv
[opt
]);
1318 new = grab_module(NULL
, argv
[opt
]);
1320 /* cmd-line specified modules must exist */
1321 fatal("grab_module() failed for module %s\n", argv
[opt
]);
1327 list
= grab_basedir(dirname
,search
,overrides
);
1329 list
= sort_modules(dirname
,list
);
1330 list
= parse_modules(list
);
1332 for (i
= 0; i
< sizeof(depfiles
)/sizeof(depfiles
[0]); i
++) {
1334 struct depfile
*d
= &depfiles
[i
];
1335 char depname
[strlen(dirname
) + 1 + strlen(d
->name
) + 1];
1336 char tmpname
[strlen(dirname
) + 1 + strlen(d
->name
) +
1337 strlen(".temp") + 1];
1339 if (d
->map_file
&& !make_map_files
&& !force_map_files
)
1342 sprintf(depname
, "%s/%s", dirname
, d
->name
);
1343 sprintf(tmpname
, "%s/%s.temp", dirname
, d
->name
);
1344 if (!doing_stdout
) {
1345 out
= fopen(tmpname
, "w");
1347 fatal("Could not open %s for writing: %s\n",
1348 tmpname
, strerror(errno
));
1351 if (ends_in(depname
, ".bin"))
1354 d
->func(list
, out
, dirname
);
1355 if (!doing_stdout
) {
1357 if (rename(tmpname
, depname
) < 0)
1358 fatal("Could not rename %s into %s: %s\n",
1359 tmpname
, depname
, strerror(errno
));