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 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->data
= grab_file(new->pathname
, &new->len
);
281 warn("Can't read module %s: %s\n",
282 new->pathname
, strerror(errno
));
286 switch (elf_ident(new->data
, new->len
, &new->conv
)) {
288 new->ops
= &mod_ops32
;
291 new->ops
= &mod_ops64
;
294 warn("Module %s is not an elf object\n", new->pathname
);
297 warn("Module %s has unknown endianness\n", new->pathname
);
300 warn("Module %s has unknown word size\n", new->pathname
);
306 release_file(new->data
, new->len
);
312 struct module_traverse
314 struct module_traverse
*prev
;
318 static int in_loop(struct module
*mod
, const struct module_traverse
*traverse
)
320 const struct module_traverse
*i
;
322 for (i
= traverse
; i
; i
= i
->prev
) {
329 /* Assume we are doing all the modules, so only report each loop once. */
330 static void report_loop(const struct module
*mod
,
331 const struct module_traverse
*traverse
)
333 const struct module_traverse
*i
;
335 /* Check that start is least alphabetically. eg. a depends
336 on b depends on a will get reported for a, not b. */
337 for (i
= traverse
->prev
; i
->prev
; i
= i
->prev
) {
338 if (strcmp(mod
->pathname
, i
->mod
->pathname
) > 0)
342 /* Is start in the loop? If not, don't report now. eg. a
343 depends on b which depends on c which depends on b. Don't
344 report when generating depends for a. */
348 warn("Loop detected: %s ", mod
->pathname
);
349 for (i
= traverse
->prev
; i
->prev
; i
= i
->prev
)
350 fprintf(stderr
, "needs %s ", i
->mod
->basename
);
351 fprintf(stderr
, "which needs %s again!\n", i
->mod
->basename
);
354 /* This is damn slow, but loops actually happen, and we don't want to
355 just exit() and leave the user without any modules. */
356 static int has_dep_loop(struct module
*module
, struct module_traverse
*prev
)
359 struct module_traverse traverse
= { .prev
= prev
, .mod
= module
};
361 if (in_loop(module
, prev
)) {
362 report_loop(module
, &traverse
);
366 for (i
= 0; i
< module
->num_deps
; i
++)
367 if (has_dep_loop(module
->deps
[i
], &traverse
))
372 /* Uniquifies and orders a dependency list. */
373 static void order_dep_list(struct module
*start
, struct module
*mod
)
377 for (i
= 0; i
< mod
->num_deps
; i
++) {
378 /* If it was previously depended on, move it to the
379 tail. ie. if a needs b and c, and c needs b, we
380 must order b after c. */
381 list_del(&mod
->deps
[i
]->dep_list
);
382 list_add_tail(&mod
->deps
[i
]->dep_list
, &start
->dep_list
);
383 order_dep_list(start
, mod
->deps
[i
]);
387 static struct module
*deleted
= NULL
;
389 static void del_module(struct module
**modules
, struct module
*delme
)
393 /* Find pointer to it. */
395 for (i
= modules
; *i
!= delme
; i
= &(*i
)->next
);
400 /* Save on a list to quiet valgrind.
401 Can't free - other modules may depend on them */
402 delme
->next
= deleted
;
406 /* convert to relative path if possible */
407 static const char *compress_path(const char *path
, const char *basedir
)
409 int len
= strlen(basedir
);
411 if (strncmp(path
, basedir
, len
) == 0)
416 static void output_deps(struct module
*modules
,
417 FILE *out
, char *dirname
)
421 for (i
= modules
; i
; i
= i
->next
) {
422 struct list_head
*j
, *tmp
;
423 order_dep_list(i
, i
);
425 fprintf(out
, "%s:", compress_path(i
->pathname
, dirname
));
426 list_for_each_safe(j
, tmp
, &i
->dep_list
) {
428 = list_entry(j
, struct module
, dep_list
);
430 compress_path(dep
->pathname
, dirname
));
437 /* warn whenever duplicate module aliases, deps, or symbols are found. */
440 static void output_deps_bin(struct module
*modules
,
441 FILE *out
, char *dirname
)
444 struct index_node
*index
;
448 index
= index_create();
450 for (i
= modules
; i
; i
= i
->next
) {
451 struct list_head
*j
, *tmp
;
452 char modname
[strlen(i
->pathname
)+1];
454 order_dep_list(i
, i
);
456 filename2modname(modname
, i
->pathname
);
457 nofail_asprintf(&line
, "%s:",
458 compress_path(i
->pathname
, dirname
));
460 list_for_each_safe(j
, tmp
, &i
->dep_list
) {
462 = list_entry(j
, struct module
, dep_list
);
463 nofail_asprintf(&line
, "%s %s",
465 compress_path(dep
->pathname
, dirname
));
470 if (index_insert(index
, modname
, line
, i
->order
) && warn_dups
)
471 warn("duplicate module deps:\n%s\n",line
);
475 index_write(index
, out
);
476 index_destroy(index
);
480 static int smells_like_module(const char *name
)
482 return ends_in(name
,".ko") || ends_in(name
, ".ko.gz");
485 typedef struct module
*(*do_module_t
)(const char *dirname
,
486 const char *filename
,
488 struct module_search
*search
,
489 struct module_overrides
*overrides
);
491 static int is_higher_priority(const char *newpath
, const char *oldpath
,
492 struct module_search
*search
,
493 struct module_overrides
*overrides
)
495 struct module_search
*tmp
;
496 struct module_overrides
*ovtmp
;
498 int prio_builtin
= -1;
502 /* The names already match, now we check for overrides and directory search
505 for (ovtmp
= overrides
; ovtmp
!= NULL
; ovtmp
= ovtmp
->next
) {
506 if (streq(ovtmp
->modfile
, newpath
))
508 if (streq(ovtmp
->modfile
, oldpath
))
511 for (i
= 0, tmp
= search
; tmp
!= NULL
; tmp
= tmp
->next
, i
++) {
512 if (streq(tmp
->search_path
, MODULE_BUILTIN_KEY
))
514 else if (strncmp(tmp
->search_path
, newpath
, tmp
->len
) == 0)
516 else if (strncmp(tmp
->search_path
, oldpath
, tmp
->len
) == 0)
520 prio_new
= prio_builtin
;
522 prio_old
= prio_builtin
;
524 return prio_new
> prio_old
;
528 static struct module
*do_module(const char *dirname
,
529 const char *filename
,
531 struct module_search
*search
,
532 struct module_overrides
*overrides
)
534 struct module
*new, **i
;
536 new = grab_module(dirname
, filename
);
540 /* Check if module is already in the list. */
541 for (i
= &list
; *i
; i
= &(*i
)->next
) {
543 if (streq((*i
)->basename
, filename
)) {
544 char newpath
[strlen(dirname
) + strlen("/")
545 + strlen(filename
) + 1];
547 sprintf(newpath
, "%s/%s", dirname
, filename
);
549 if (is_higher_priority(newpath
, (*i
)->pathname
,search
,
556 del_module(NULL
, new);
562 /* Not in the list already. Just prepend. */
567 static struct module
*grab_dir(const char *dirname
,
571 struct module_search
*search
,
572 struct module_overrides
*overrides
)
574 struct dirent
*dirent
;
576 while ((dirent
= readdir(dir
)) != NULL
) {
577 if (smells_like_module(dirent
->d_name
))
578 next
= do_mod(dirname
, dirent
->d_name
, next
,
580 else if (!streq(dirent
->d_name
, ".")
581 && !streq(dirent
->d_name
, "..")
582 && !streq(dirent
->d_name
, "source")
583 && !streq(dirent
->d_name
, "build")) {
586 char subdir
[strlen(dirname
) + 1
587 + strlen(dirent
->d_name
) + 1];
588 sprintf(subdir
, "%s/%s", dirname
, dirent
->d_name
);
589 sub
= opendir(subdir
);
591 next
= grab_dir(subdir
, sub
, next
, do_mod
,
600 static struct module
*grab_basedir(const char *dirname
,
601 struct module_search
*search
,
602 struct module_overrides
*overrides
)
607 dir
= opendir(dirname
);
609 warn("Couldn't open directory %s: %s\n",
610 dirname
, strerror(errno
));
613 list
= grab_dir(dirname
, dir
, NULL
, do_module
, search
, overrides
);
619 static struct module
*sort_modules(const char *dirname
, struct module
*list
)
621 struct module
*tlist
= NULL
, **tpos
= &tlist
;
623 int dir_len
= strlen(dirname
) + 1;
624 char file_name
[dir_len
+ strlen("modules.order") + 1];
626 unsigned int linenum
= 0;
628 sprintf(file_name
, "%s/%s", dirname
, "modules.order");
630 modorder
= fopen(file_name
, "r");
632 /* Older kernels don't generate modules.order. Just
633 return if the file doesn't exist. */
636 fatal("Could not open '%s': %s\n", file_name
, strerror(errno
));
639 sprintf(line
, "%s/", dirname
);
641 /* move modules listed in modorder file to tlist in order */
642 while (fgets(line
, sizeof(line
), modorder
)) {
643 struct module
**pos
, *mod
;
644 int len
= strlen(line
);
647 if (line
[len
- 1] == '\n')
648 line
[len
- 1] = '\0';
650 for (pos
= &list
; (mod
= *pos
); pos
= &(*pos
)->next
) {
651 if (streq(line
, mod
->pathname
+ dir_len
)) {
652 mod
->order
= linenum
;
662 /* append the rest */
670 static struct module
*parse_modules(struct module
*list
)
674 for (i
= list
; i
; i
= i
->next
) {
675 i
->ops
->load_symbols(i
);
676 i
->ops
->fetch_tables(i
);
679 for (i
= list
; i
; i
= i
->next
)
680 i
->ops
->calculate_deps(i
);
682 /* Strip out modules with dependency loops. */
684 for (i
= list
; i
; i
= i
->next
) {
685 if (has_dep_loop(i
, NULL
)) {
686 warn("Module %s ignored, due to loop\n",
687 i
->pathname
+ skipchars
);
688 del_module(&list
, i
);
696 /* Simply dump hash table. */
697 static void output_symbols(struct module
*unused
, FILE *out
, char *dirname
)
701 fprintf(out
, "# Aliases for symbols, used by symbol_request().\n");
702 for (i
= 0; i
< SYMBOL_HASH_SIZE
; i
++) {
705 for (s
= symbolhash
[i
]; s
; s
= s
->next
) {
707 char modname
[strlen(s
->owner
->pathname
)+1];
708 filename2modname(modname
, s
->owner
->pathname
);
709 fprintf(out
, "alias symbol:%s %s\n",
716 static void output_symbols_bin(struct module
*unused
, FILE *out
, char *dirname
)
718 struct index_node
*index
;
723 index
= index_create();
725 for (i
= 0; i
< SYMBOL_HASH_SIZE
; i
++) {
728 for (s
= symbolhash
[i
]; s
; s
= s
->next
) {
730 char modname
[strlen(s
->owner
->pathname
)+1];
731 filename2modname(modname
, s
->owner
->pathname
);
732 nofail_asprintf(&alias
, "symbol:%s", s
->name
);
733 duplicate
= index_insert(index
, alias
, modname
,
735 if (duplicate
&& warn_dups
)
736 warn("duplicate module syms:\n%s %s\n",
743 index_write(index
, out
);
744 index_destroy(index
);
747 static void output_aliases(struct module
*modules
, FILE *out
, char *dirname
)
753 fprintf(out
, "# Aliases extracted from modules themselves.\n");
754 for (i
= modules
; i
; i
= i
->next
) {
755 char modname
[strlen(i
->pathname
)+1];
757 filename2modname(modname
, i
->pathname
);
759 /* Grab from old-style .modalias section. */
760 for (p
= i
->ops
->get_aliases(i
, &size
);
762 p
= next_string(p
, &size
))
763 fprintf(out
, "alias %s %s\n", p
, modname
);
765 /* Grab form new-style .modinfo section. */
766 for (p
= i
->ops
->get_modinfo(i
, &size
);
768 p
= next_string(p
, &size
)) {
769 if (strstarts(p
, "alias="))
770 fprintf(out
, "alias %s %s\n",
771 p
+ strlen("alias="), modname
);
776 static void output_aliases_bin(struct module
*modules
, FILE *out
, char *dirname
)
782 struct index_node
*index
;
785 index
= index_create();
787 for (i
= modules
; i
; i
= i
->next
) {
788 char modname
[strlen(i
->pathname
)+1];
790 filename2modname(modname
, i
->pathname
);
792 /* Grab from old-style .modalias section. */
793 for (p
= i
->ops
->get_aliases(i
, &size
);
795 p
= next_string(p
, &size
)) {
796 alias
= NOFAIL(strdup(p
));
798 duplicate
= index_insert(index
, alias
, modname
, i
->order
);
799 if (duplicate
&& warn_dups
)
800 warn("duplicate module alias:\n%s %s\n",
805 /* Grab from new-style .modinfo section. */
806 for (p
= i
->ops
->get_modinfo(i
, &size
);
808 p
= next_string(p
, &size
)) {
809 if (strstarts(p
, "alias=")) {
810 alias
= NOFAIL(strdup(p
+ strlen("alias=")));
812 duplicate
= index_insert(index
, alias
, modname
, i
->order
);
813 if (duplicate
&& warn_dups
)
814 warn("duplicate module alias:\n%s %s\n",
821 index_write(index
, out
);
822 index_destroy(index
);
827 void (*func
)(struct module
*, FILE *, char *dirname
);
831 static struct depfile depfiles
[] = {
832 { "modules.dep", output_deps
, 0 }, /* This is what we check for '-A'. */
833 { "modules.dep.bin", output_deps_bin
, 0 },
834 { "modules.pcimap", output_pci_table
, 1 },
835 { "modules.usbmap", output_usb_table
, 1 },
836 { "modules.ccwmap", output_ccw_table
, 1 },
837 { "modules.ieee1394map", output_ieee1394_table
, 1 },
838 { "modules.isapnpmap", output_isapnp_table
, 1 },
839 { "modules.inputmap", output_input_table
, 1 },
840 { "modules.ofmap", output_of_table
, 1 },
841 { "modules.seriomap", output_serio_table
, 1 },
842 { "modules.alias", output_aliases
, 0 },
843 { "modules.alias.bin", output_aliases_bin
, 0 },
844 { "modules.symbols", output_symbols
, 0 },
845 { "modules.symbols.bin", output_symbols_bin
, 0 }
848 /* If we can't figure it out, it's safe to say "true". */
849 static int any_modules_newer(const char *dirname
, time_t mtime
)
852 struct dirent
*dirent
;
854 dir
= opendir(dirname
);
858 while ((dirent
= readdir(dir
)) != NULL
) {
860 char file
[strlen(dirname
) + 1 + strlen(dirent
->d_name
) + 1];
862 if (streq(dirent
->d_name
, ".") || streq(dirent
->d_name
, ".."))
865 sprintf(file
, "%s/%s", dirname
, dirent
->d_name
);
866 if (lstat(file
, &st
) != 0)
869 if (smells_like_module(dirent
->d_name
)) {
870 if (st
.st_mtime
> mtime
)
872 } else if (S_ISDIR(st
.st_mode
)) {
873 if (any_modules_newer(file
, mtime
))
885 static int depfile_out_of_date(const char *dirname
)
888 char depfile
[strlen(dirname
) + 1 + strlen(depfiles
[0].name
) + 1];
890 sprintf(depfile
, "%s/%s", dirname
, depfiles
[0].name
);
892 if (stat(depfile
, &st
) != 0)
895 return any_modules_newer(dirname
, st
.st_mtime
);
898 static char *strsep_skipspace(char **string
, char *delim
)
902 *string
+= strspn(*string
, delim
);
903 return strsep(string
, delim
);
906 static struct module_search
*add_search(const char *search_path
,
908 struct module_search
*search
)
911 struct module_search
*new;
913 new = NOFAIL(malloc(sizeof(*new)));
914 new->search_path
= NOFAIL(strdup(search_path
));
922 static struct module_overrides
*add_override(const char *modfile
,
923 struct module_overrides
*overrides
)
926 struct module_overrides
*new;
928 new = NOFAIL(malloc(sizeof(*new)));
929 new->modfile
= NOFAIL(strdup(modfile
));
930 new->next
= overrides
;
936 static int parse_config_scan(const char *filename
,
938 const char *kernelversion
,
939 struct module_search
**search
,
940 struct module_overrides
**overrides
);
942 static int parse_config_file(const char *filename
,
944 const char *kernelversion
,
945 struct module_search
**search
,
946 struct module_overrides
**overrides
)
949 unsigned int linenum
= 0;
952 cfile
= fopen(filename
, "r");
955 fatal("could not open '%s', reason: %s\n", filename
,
960 while ((line
= getline_wrapped(cfile
, &linenum
)) != NULL
) {
964 cmd
= strsep_skipspace(&ptr
, "\t ");
966 if (cmd
== NULL
|| cmd
[0] == '#' || cmd
[0] == '\0') {
971 if (streq(cmd
, "search")) {
974 while ((search_path
= strsep_skipspace(&ptr
, "\t "))) {
978 if (strcmp(search_path
,
979 MODULE_BUILTIN_KEY
) == 0) {
980 *search
= add_search(MODULE_BUILTIN_KEY
,
984 nofail_asprintf(&dirname
, "%s%s%s/%s", basedir
,
985 MODULE_DIR
, kernelversion
, search_path
);
986 len
= strlen(dirname
);
987 *search
= add_search(dirname
, len
, *search
);
990 } else if (streq(cmd
, "override")) {
991 char *pathname
= NULL
, *version
, *subdir
;
992 modname
= strsep_skipspace(&ptr
, "\t ");
993 version
= strsep_skipspace(&ptr
, "\t ");
994 subdir
= strsep_skipspace(&ptr
, "\t ");
996 if (strcmp(version
, kernelversion
) != 0 &&
997 strcmp(version
, "*") != 0)
1000 nofail_asprintf(&pathname
, "%s%s%s/%s/%s.ko", basedir
,
1001 MODULE_DIR
, kernelversion
, subdir
, modname
);
1003 *overrides
= add_override(pathname
, *overrides
);
1005 } else if (streq(cmd
, "include")) {
1008 newfilename
= strsep_skipspace(&ptr
, "\t ");
1010 grammar(cmd
, filename
, linenum
);
1012 warn("\"include %s\" is deprecated, "
1013 "please use /etc/depmod.d\n", newfilename
);
1014 if (strstarts(newfilename
, "/etc/depmod.d")) {
1015 warn("\"include /etc/depmod.d\" is "
1016 "the default, ignored\n");
1018 if (!parse_config_scan(newfilename
, basedir
,
1021 warn("Failed to open included"
1022 " config file %s: %s\n",
1023 newfilename
, strerror(errno
));
1026 } else if (streq(cmd
, "make_map_files")) {
1029 option
= strsep_skipspace(&ptr
, "\t ");
1031 grammar(cmd
, filename
, linenum
);
1033 if (streq(option
, "yes"))
1035 else if (streq(option
, "no"))
1038 grammar(cmd
, filename
, linenum
);
1041 grammar(cmd
, filename
, linenum
);
1049 static int parse_config_scan(const char *filename
,
1050 const char *basedir
,
1051 const char *kernelversion
,
1052 struct module_search
**search
,
1053 struct module_overrides
**overrides
)
1058 dir
= opendir(filename
);
1061 struct list_head node
;
1064 LIST_HEAD(files_list
);
1065 struct file_entry
*fe
, *fe_tmp
;
1068 /* sort files from directory into list */
1069 while ((i
= readdir(dir
)) != NULL
) {
1072 if (i
->d_name
[0] == '.')
1074 if (!config_filter(i
->d_name
))
1077 len
= strlen(i
->d_name
);
1078 if (len
< 6 || strcmp(&i
->d_name
[len
-5], ".conf") != 0)
1079 warn("All config files need .conf: %s/%s, "
1080 "it will be ignored in a future release.\n",
1081 filename
, i
->d_name
);
1082 fe
= malloc(sizeof(struct file_entry
) + len
+ 1);
1085 strcpy(fe
->name
, i
->d_name
);
1086 list_for_each_entry(fe_tmp
, &files_list
, node
)
1087 if (strcmp(fe_tmp
->name
, fe
->name
) >= 0)
1089 list_add_tail(&fe
->node
, &fe_tmp
->node
);
1093 /* parse list of files */
1094 list_for_each_entry_safe(fe
, fe_tmp
, &files_list
, node
) {
1097 nofail_asprintf(&cfgfile
, "%s/%s", filename
, fe
->name
);
1098 if (!parse_config_file(cfgfile
, basedir
, kernelversion
,
1100 warn("Failed to open config file "
1101 "%s: %s\n", fe
->name
, strerror(errno
));
1103 list_del(&fe
->node
);
1109 if (parse_config_file(filename
, basedir
, kernelversion
, search
,
1117 static void parse_toplevel_config(const char *filename
,
1118 const char *basedir
,
1119 const char *kernelversion
,
1120 struct module_search
**search
,
1121 struct module_overrides
**overrides
)
1124 if (!parse_config_scan(filename
, basedir
, kernelversion
, search
,
1126 fatal("Failed to open config file %s: %s\n",
1127 filename
, strerror(errno
));
1131 /* deprecated config file */
1132 if (parse_config_file("/etc/depmod.conf", basedir
, kernelversion
,
1133 search
, overrides
) > 0)
1134 warn("Deprecated config file /etc/depmod.conf, "
1135 "all config files belong into /etc/depmod.d/.\n");
1137 /* default config */
1138 parse_config_scan("/etc/depmod.d", basedir
, kernelversion
,
1142 /* Local to main, but not freed on exit. Keep valgrind quiet. */
1143 struct module
*list
= NULL
;
1144 struct module_search
*search
= NULL
;
1145 struct module_overrides
*overrides
= NULL
;
1147 int main(int argc
, char *argv
[])
1149 int opt
, all
= 0, maybe_all
= 0, doing_stdout
= 0;
1150 char *basedir
= "", *dirname
, *version
, *badopt
= NULL
,
1153 const char *config
= NULL
;
1155 if (native_endianness() == 0)
1158 /* Don't print out any errors just yet, we might want to exec
1159 backwards compat version. */
1161 while ((opt
= getopt_long(argc
, argv
, "ab:ArehnqruvVF:C:wm", options
, NULL
))
1169 skipchars
= strlen(basedir
);
1175 system_map
= optarg
;
1191 print_usage(argv
[0]);
1198 printf("%s %s\n", PACKAGE
, VERSION
);
1204 force_map_files
= 1;
1207 badopt
= argv
[optind
-1];
1211 /* We can't print unknowns without a System.map */
1215 load_system_map(system_map
);
1217 /* They can specify the version naked on the command line */
1218 if (optind
< argc
&& is_version_number(argv
[optind
])) {
1219 version
= NOFAIL(strdup(argv
[optind
]));
1224 version
= NOFAIL(strdup(buf
.release
));
1227 /* Check for old version. */
1228 if (old_module_version(version
)) {
1229 fprintf(stderr
, "Kernel version %s requires old depmod\n",
1235 fprintf(stderr
, "%s: malformed/unrecognized option '%s'\n",
1237 print_usage(argv
[0]);
1241 /* Depmod -a by default if no names. */
1245 nofail_asprintf(&dirname
, "%s%s%s", basedir
, MODULE_DIR
, version
);
1248 if (!doing_stdout
&& !depfile_out_of_date(dirname
))
1253 parse_toplevel_config(config
, basedir
, version
, &search
, &overrides
);
1255 /* For backward compatibility add "updates" to the head of the search
1256 * list here. But only if there was no "search" option specified.
1262 nofail_asprintf(&dirname
, "%s%s%s/updates", basedir
,
1263 MODULE_DIR
, version
);
1264 len
= strlen(dirname
);
1265 search
= add_search(dirname
, len
, search
);
1268 /* Do command line args. */
1269 for (opt
= optind
; opt
< argc
; opt
++) {
1272 if (argv
[opt
][0] != '/')
1273 fatal("modules must be specified using absolute paths.\n"
1274 "\"%s\" is a relative path\n", argv
[opt
]);
1276 new = grab_module(NULL
, argv
[opt
]);
1278 /* cmd-line specified modules must exist */
1279 fatal("grab_module() failed for module %s\n", argv
[opt
]);
1285 list
= grab_basedir(dirname
,search
,overrides
);
1287 list
= sort_modules(dirname
,list
);
1288 list
= parse_modules(list
);
1290 for (i
= 0; i
< sizeof(depfiles
)/sizeof(depfiles
[0]); i
++) {
1292 struct depfile
*d
= &depfiles
[i
];
1293 char depname
[strlen(dirname
) + 1 + strlen(d
->name
) + 1];
1294 char tmpname
[strlen(dirname
) + 1 + strlen(d
->name
) +
1295 strlen(".temp") + 1];
1297 if (d
->map_file
&& !make_map_files
&& !force_map_files
)
1300 sprintf(depname
, "%s/%s", dirname
, d
->name
);
1301 sprintf(tmpname
, "%s/%s.temp", dirname
, d
->name
);
1302 if (!doing_stdout
) {
1303 out
= fopen(tmpname
, "w");
1305 fatal("Could not open %s for writing: %s\n",
1306 tmpname
, strerror(errno
));
1309 if (ends_in(depname
, ".bin"))
1312 d
->func(list
, out
, dirname
);
1313 if (!doing_stdout
) {
1315 if (rename(tmpname
, depname
) < 0)
1316 fatal("Could not rename %s into %s: %s\n",
1317 tmpname
, depname
, strerror(errno
));