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]))
24 #include "zlibsupport.h"
28 #include "moduleops.h"
30 #include "config_filter.h"
35 #define MODULE_DIR "/lib/modules/"
38 #ifndef MODULE_BUILTIN_KEY
39 #define MODULE_BUILTIN_KEY "built-in"
42 struct module_overrides
45 struct module_overrides
*next
;
47 /* overridden module */
54 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 (strncmp(ptr
+1, ksymstr
, ksymstr_len
) == 0)
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
));
243 static void grammar(const char *cmd
, const char *filename
, unsigned int line
)
245 warn("%s line %u: ignoring bad line starting with '%s'\n",
246 filename
, line
, cmd
);
250 static void print_usage(const char *name
)
253 "%s " VERSION
" -- part of " PACKAGE
"\n"
254 "%s -[aA] [-n -e -v -q -V -r -u -w -m]\n"
255 " [-b basedirectory] [forced_version]\n"
256 "depmod [-n -e -v -q -r -u -w] [-F kernelsyms] module1.ko module2.ko ...\n"
257 "If no arguments (except options) are given, \"depmod -a\" is assumed\n"
259 "depmod will output a dependancy list suitable for the modprobe utility.\n"
263 "\t-a, --all Probe all modules\n"
264 "\t-A, --quick Only does the work if there's a new module\n"
265 "\t-e, --errsyms Report not supplied symbols\n"
266 "\t-m, --map Create the legacy map files\n"
267 "\t-n, --show Write the dependency file on stdout only\n"
268 "\t-V, --version Print the release version\n"
269 "\t-v, --verbose Enable verbose mode\n"
270 "\t-w, --warn Warn on duplicates\n"
271 "\t-h, --help Print this usage message\n"
273 "The following options are useful for people managing distributions:\n"
274 "\t-b basedirectory\n"
275 "\t --basedir basedirectory Use an image of a module tree.\n"
277 "\t --filesyms kernelsyms Use the file instead of the\n"
278 "\t current kernel symbols.\n",
282 static int ends_in(const char *name
, const char *ext
)
284 unsigned int namelen
, extlen
;
287 namelen
= strlen(name
);
288 extlen
= strlen(ext
);
290 if (namelen
< extlen
) return 0;
292 if (streq(name
+ namelen
- extlen
, ext
))
297 /* "\177ELF" <byte> where byte = 001 for 32-bit, 002 for 64 */
298 int needconv(const char *elfhdr
)
300 union { short s
; char c
[2]; } endian_test
;
303 if (endian_test
.c
[1] == 1) return elfhdr
[EI_DATA
] != ELFDATA2MSB
;
304 if (endian_test
.c
[0] == 1) return elfhdr
[EI_DATA
] != ELFDATA2LSB
;
309 static char *my_basename(const char *name
)
311 const char *base
= strrchr(name
, '/');
312 if (base
) return (char *)base
+ 1;
316 static struct module
*grab_module(const char *dirname
, const char *filename
)
320 new = NOFAIL(malloc(sizeof(*new)
321 + strlen(dirname
?:"") + 1 + strlen(filename
) + 1));
323 sprintf(new->pathname
, "%s/%s", dirname
, filename
);
325 strcpy(new->pathname
, filename
);
326 new->basename
= my_basename(new->pathname
);
328 INIT_LIST_HEAD(&new->dep_list
);
329 new->order
= INDEX_PRIORITY_MIN
;
331 new->data
= grab_file(new->pathname
, &new->len
);
333 warn("Can't read module %s: %s\n",
334 new->pathname
, strerror(errno
));
338 /* "\177ELF" <byte> where byte = 001 for 32-bit, 002 for 64 */
339 if (memcmp(new->data
, ELFMAG
, SELFMAG
) != 0) {
340 warn("Module %s is not an elf object\n", new->pathname
);
344 switch (((char *)new->data
)[EI_CLASS
]) {
346 new->ops
= &mod_ops32
;
349 new->ops
= &mod_ops64
;
352 warn("Module %s has elf unknown identifier %i\n",
353 new->pathname
, ((char *)new->data
)[EI_CLASS
]);
356 new->conv
= needconv(new->data
);
360 release_file(new->data
, new->len
);
366 struct module_traverse
368 struct module_traverse
*prev
;
372 static int in_loop(struct module
*mod
, const struct module_traverse
*traverse
)
374 const struct module_traverse
*i
;
376 for (i
= traverse
; i
; i
= i
->prev
) {
383 /* Assume we are doing all the modules, so only report each loop once. */
384 static void report_loop(const struct module
*mod
,
385 const struct module_traverse
*traverse
)
387 const struct module_traverse
*i
;
389 /* Check that start is least alphabetically. eg. a depends
390 on b depends on a will get reported for a, not b. */
391 for (i
= traverse
->prev
; i
->prev
; i
= i
->prev
) {
392 if (strcmp(mod
->pathname
, i
->mod
->pathname
) > 0)
396 /* Is start in the loop? If not, don't report now. eg. a
397 depends on b which depends on c which depends on b. Don't
398 report when generating depends for a. */
402 warn("Loop detected: %s ", mod
->pathname
);
403 for (i
= traverse
->prev
; i
->prev
; i
= i
->prev
)
404 fprintf(stderr
, "needs %s ", i
->mod
->basename
);
405 fprintf(stderr
, "which needs %s again!\n", i
->mod
->basename
);
408 /* This is damn slow, but loops actually happen, and we don't want to
409 just exit() and leave the user without any modules. */
410 static int has_dep_loop(struct module
*module
, struct module_traverse
*prev
)
413 struct module_traverse traverse
= { .prev
= prev
, .mod
= module
};
415 if (in_loop(module
, prev
)) {
416 report_loop(module
, &traverse
);
420 for (i
= 0; i
< module
->num_deps
; i
++)
421 if (has_dep_loop(module
->deps
[i
], &traverse
))
426 /* Uniquifies and orders a dependency list. */
427 static void order_dep_list(struct module
*start
, struct module
*mod
)
431 for (i
= 0; i
< mod
->num_deps
; i
++) {
432 /* If it was previously depended on, move it to the
433 tail. ie. if a needs b and c, and c needs b, we
434 must order b after c. */
435 list_del(&mod
->deps
[i
]->dep_list
);
436 list_add_tail(&mod
->deps
[i
]->dep_list
, &start
->dep_list
);
437 order_dep_list(start
, mod
->deps
[i
]);
441 static struct module
*deleted
= NULL
;
443 static void del_module(struct module
**modules
, struct module
*delme
)
447 /* Find pointer to it. */
449 for (i
= modules
; *i
!= delme
; i
= &(*i
)->next
);
454 /* Save on a list to quiet valgrind.
455 Can't free - other modules may depend on them */
456 delme
->next
= deleted
;
460 /* Convert filename to the module name. Works if filename == modname, too. */
461 static void filename2modname(char *modname
, const char *filename
)
463 const char *afterslash
;
466 afterslash
= strrchr(filename
, '/');
468 afterslash
= filename
;
472 /* Convert to underscores, stop at first . */
473 for (i
= 0; afterslash
[i
] && afterslash
[i
] != '.'; i
++) {
474 if (afterslash
[i
] == '-')
477 modname
[i
] = afterslash
[i
];
482 /* convert to relative path if possible */
483 static const char *compress_path(const char *path
, const char *basedir
)
485 int len
= strlen(basedir
);
487 if (strncmp(path
, basedir
, len
) == 0)
492 static void output_deps(struct module
*modules
,
493 FILE *out
, char *dirname
)
497 for (i
= modules
; i
; i
= i
->next
) {
498 struct list_head
*j
, *tmp
;
499 order_dep_list(i
, i
);
501 fprintf(out
, "%s:", compress_path(i
->pathname
, dirname
));
502 list_for_each_safe(j
, tmp
, &i
->dep_list
) {
504 = list_entry(j
, struct module
, dep_list
);
506 compress_path(dep
->pathname
, dirname
));
513 /* warn whenever duplicate module aliases, deps, or symbols are found. */
516 static void output_deps_bin(struct module
*modules
,
517 FILE *out
, char *dirname
)
520 struct index_node
*index
;
524 index
= index_create();
526 for (i
= modules
; i
; i
= i
->next
) {
527 struct list_head
*j
, *tmp
;
528 char modname
[strlen(i
->pathname
)+1];
530 order_dep_list(i
, i
);
532 filename2modname(modname
, i
->pathname
);
533 nofail_asprintf(&line
, "%s:",
534 compress_path(i
->pathname
, dirname
));
536 list_for_each_safe(j
, tmp
, &i
->dep_list
) {
538 = list_entry(j
, struct module
, dep_list
);
539 nofail_asprintf(&line
, "%s %s",
541 compress_path(dep
->pathname
, dirname
));
546 if (index_insert(index
, modname
, line
, i
->order
) && warn_dups
)
547 warn("duplicate module deps:\n%s\n",line
);
551 index_write(index
, out
);
552 index_destroy(index
);
556 static int smells_like_module(const char *name
)
558 return ends_in(name
,".ko") || ends_in(name
, ".ko.gz");
561 typedef struct module
*(*do_module_t
)(const char *dirname
,
562 const char *filename
,
564 struct module_search
*search
,
565 struct module_overrides
*overrides
);
567 static int is_higher_priority(const char *newpath
, const char *oldpath
,
568 struct module_search
*search
,
569 struct module_overrides
*overrides
)
571 struct module_search
*tmp
;
572 struct module_overrides
*ovtmp
;
574 int prio_builtin
= -1;
578 /* The names already match, now we check for overrides and directory search
581 for (ovtmp
= overrides
; ovtmp
!= NULL
; ovtmp
= ovtmp
->next
) {
582 if (strcmp(ovtmp
->modfile
, newpath
) == 0)
584 if (strcmp(ovtmp
->modfile
, oldpath
) == 0)
587 for (i
= 0, tmp
= search
; tmp
!= NULL
; tmp
= tmp
->next
, i
++) {
588 if (strcmp(tmp
->search_path
, MODULE_BUILTIN_KEY
) == 0)
590 else if (strncmp(tmp
->search_path
, newpath
, tmp
->len
) == 0)
592 else if (strncmp(tmp
->search_path
, oldpath
, tmp
->len
) == 0)
595 if (prio_builtin
< 0)
598 prio_new
= prio_builtin
;
600 prio_old
= prio_builtin
;
602 return prio_new
> prio_old
;
606 static struct module
*do_module(const char *dirname
,
607 const char *filename
,
609 struct module_search
*search
,
610 struct module_overrides
*overrides
)
612 struct module
*new, **i
;
614 new = grab_module(dirname
, filename
);
618 /* Check if module is already in the list. */
619 for (i
= &list
; *i
; i
= &(*i
)->next
) {
621 if (streq((*i
)->basename
, filename
)) {
622 char newpath
[strlen(dirname
) + strlen("/")
623 + strlen(filename
) + 1];
625 sprintf(newpath
, "%s/%s", dirname
, filename
);
627 if (is_higher_priority(newpath
, (*i
)->pathname
,search
,
634 del_module(NULL
, new);
640 /* Not in the list already. Just prepend. */
645 static struct module
*grab_dir(const char *dirname
,
649 struct module_search
*search
,
650 struct module_overrides
*overrides
)
652 struct dirent
*dirent
;
654 while ((dirent
= readdir(dir
)) != NULL
) {
655 if (smells_like_module(dirent
->d_name
))
656 next
= do_mod(dirname
, dirent
->d_name
, next
,
658 else if (!streq(dirent
->d_name
, ".")
659 && !streq(dirent
->d_name
, "..")
660 && !streq(dirent
->d_name
, "source")
661 && !streq(dirent
->d_name
, "build")) {
664 char subdir
[strlen(dirname
) + 1
665 + strlen(dirent
->d_name
) + 1];
666 sprintf(subdir
, "%s/%s", dirname
, dirent
->d_name
);
667 sub
= opendir(subdir
);
669 next
= grab_dir(subdir
, sub
, next
, do_mod
,
678 static struct module
*grab_basedir(const char *dirname
,
679 struct module_search
*search
,
680 struct module_overrides
*overrides
)
685 dir
= opendir(dirname
);
687 warn("Couldn't open directory %s: %s\n",
688 dirname
, strerror(errno
));
691 list
= grab_dir(dirname
, dir
, NULL
, do_module
, search
, overrides
);
697 static struct module
*sort_modules(const char *dirname
, struct module
*list
)
699 struct module
*tlist
= NULL
, **tpos
= &tlist
;
701 int dir_len
= strlen(dirname
) + 1;
702 char file_name
[dir_len
+ strlen("modules.order") + 1];
704 unsigned int linenum
= 0;
706 sprintf(file_name
, "%s/%s", dirname
, "modules.order");
708 modorder
= fopen(file_name
, "r");
710 /* Older kernels don't generate modules.order. Just
711 return if the file doesn't exist. */
714 fatal("Could not open '%s': %s\n", file_name
, strerror(errno
));
717 sprintf(line
, "%s/", dirname
);
719 /* move modules listed in modorder file to tlist in order */
720 while (fgets(line
, sizeof(line
), modorder
)) {
721 struct module
**pos
, *mod
;
722 int len
= strlen(line
);
725 if (line
[len
- 1] == '\n')
726 line
[len
- 1] = '\0';
728 for (pos
= &list
; (mod
= *pos
); pos
= &(*pos
)->next
) {
729 if (strcmp(line
, mod
->pathname
+ dir_len
) == 0) {
730 mod
->order
= linenum
;
740 /* append the rest */
748 static struct module
*parse_modules(struct module
*list
)
752 for (i
= list
; i
; i
= i
->next
) {
753 i
->ops
->load_symbols(i
);
754 i
->ops
->fetch_tables(i
);
757 for (i
= list
; i
; i
= i
->next
)
758 i
->ops
->calculate_deps(i
, verbose
);
760 /* Strip out modules with dependency loops. */
762 for (i
= list
; i
; i
= i
->next
) {
763 if (has_dep_loop(i
, NULL
)) {
764 warn("Module %s ignored, due to loop\n",
765 i
->pathname
+ skipchars
);
766 del_module(&list
, i
);
774 /* Simply dump hash table. */
775 static void output_symbols(struct module
*unused
, FILE *out
, char *dirname
)
779 fprintf(out
, "# Aliases for symbols, used by symbol_request().\n");
780 for (i
= 0; i
< SYMBOL_HASH_SIZE
; i
++) {
783 for (s
= symbolhash
[i
]; s
; s
= s
->next
) {
785 char modname
[strlen(s
->owner
->pathname
)+1];
786 filename2modname(modname
, s
->owner
->pathname
);
787 fprintf(out
, "alias symbol:%s %s\n",
794 static void output_symbols_bin(struct module
*unused
, FILE *out
, char *dirname
)
796 struct index_node
*index
;
801 index
= index_create();
803 for (i
= 0; i
< SYMBOL_HASH_SIZE
; i
++) {
806 for (s
= symbolhash
[i
]; s
; s
= s
->next
) {
808 char modname
[strlen(s
->owner
->pathname
)+1];
809 filename2modname(modname
, s
->owner
->pathname
);
810 nofail_asprintf(&alias
, "symbol:%s", s
->name
);
811 duplicate
= index_insert(index
, alias
, modname
,
813 if (duplicate
&& warn_dups
)
814 warn("duplicate module syms:\n%s %s\n",
821 index_write(index
, out
);
822 index_destroy(index
);
825 static const char *next_string(const char *string
, unsigned long *secsize
)
827 /* Skip non-zero chars */
830 if ((*secsize
)-- <= 1)
834 /* Skip any zero padding. */
837 if ((*secsize
)-- <= 1)
843 /* Careful! Don't munge - in [ ] as per Debian Bug#350915 */
844 static char *underscores(char *string
)
851 for (i
= 0; string
[i
]; i
++) {
858 warn("Unmatched bracket in %s\n", string
);
862 i
+= strcspn(&string
[i
], "]");
864 warn("Unmatched bracket in %s\n", string
);
870 static void output_aliases(struct module
*modules
, FILE *out
, char *dirname
)
876 fprintf(out
, "# Aliases extracted from modules themselves.\n");
877 for (i
= modules
; i
; i
= i
->next
) {
878 char modname
[strlen(i
->pathname
)+1];
880 filename2modname(modname
, i
->pathname
);
882 /* Grab from old-style .modalias section. */
883 for (p
= i
->ops
->get_aliases(i
, &size
);
885 p
= next_string(p
, &size
))
886 fprintf(out
, "alias %s %s\n", p
, modname
);
888 /* Grab form new-style .modinfo section. */
889 for (p
= i
->ops
->get_modinfo(i
, &size
);
891 p
= next_string(p
, &size
)) {
892 if (strncmp(p
, "alias=", strlen("alias=")) == 0)
893 fprintf(out
, "alias %s %s\n",
894 p
+ strlen("alias="), modname
);
899 static void output_aliases_bin(struct module
*modules
, FILE *out
, char *dirname
)
905 struct index_node
*index
;
908 index
= index_create();
910 for (i
= modules
; i
; i
= i
->next
) {
911 char modname
[strlen(i
->pathname
)+1];
913 filename2modname(modname
, i
->pathname
);
915 /* Grab from old-style .modalias section. */
916 for (p
= i
->ops
->get_aliases(i
, &size
);
918 p
= next_string(p
, &size
)) {
919 alias
= NOFAIL(strdup(p
));
921 duplicate
= index_insert(index
, alias
, modname
, i
->order
);
922 if (duplicate
&& warn_dups
)
923 warn("duplicate module alias:\n%s %s\n",
928 /* Grab from new-style .modinfo section. */
929 for (p
= i
->ops
->get_modinfo(i
, &size
);
931 p
= next_string(p
, &size
)) {
932 if (strncmp(p
, "alias=", strlen("alias=")) == 0) {
933 alias
= NOFAIL(strdup(p
+ strlen("alias=")));
935 duplicate
= index_insert(index
, alias
, modname
, i
->order
);
936 if (duplicate
&& warn_dups
)
937 warn("duplicate module alias:\n%s %s\n",
944 index_write(index
, out
);
945 index_destroy(index
);
950 void (*func
)(struct module
*, FILE *, char *dirname
);
954 static struct depfile depfiles
[] = {
955 { "modules.dep", output_deps
, 0 }, /* This is what we check for '-A'. */
956 { "modules.dep.bin", output_deps_bin
, 0 },
957 { "modules.pcimap", output_pci_table
, 1 },
958 { "modules.usbmap", output_usb_table
, 1 },
959 { "modules.ccwmap", output_ccw_table
, 1 },
960 { "modules.ieee1394map", output_ieee1394_table
, 1 },
961 { "modules.isapnpmap", output_isapnp_table
, 1 },
962 { "modules.inputmap", output_input_table
, 1 },
963 { "modules.ofmap", output_of_table
, 1 },
964 { "modules.seriomap", output_serio_table
, 1 },
965 { "modules.alias", output_aliases
, 0 },
966 { "modules.alias.bin", output_aliases_bin
, 0 },
967 { "modules.symbols", output_symbols
, 0 },
968 { "modules.symbols.bin", output_symbols_bin
, 0 }
971 /* If we can't figure it out, it's safe to say "true". */
972 static int any_modules_newer(const char *dirname
, time_t mtime
)
975 struct dirent
*dirent
;
977 dir
= opendir(dirname
);
981 while ((dirent
= readdir(dir
)) != NULL
) {
983 char file
[strlen(dirname
) + 1 + strlen(dirent
->d_name
) + 1];
985 if (streq(dirent
->d_name
, ".") || streq(dirent
->d_name
, ".."))
988 sprintf(file
, "%s/%s", dirname
, dirent
->d_name
);
989 if (lstat(file
, &st
) != 0)
992 if (smells_like_module(dirent
->d_name
)) {
993 if (st
.st_mtime
> mtime
)
995 } else if (S_ISDIR(st
.st_mode
)) {
996 if (any_modules_newer(file
, mtime
))
1008 static int depfile_out_of_date(const char *dirname
)
1011 char depfile
[strlen(dirname
) + 1 + strlen(depfiles
[0].name
) + 1];
1013 sprintf(depfile
, "%s/%s", dirname
, depfiles
[0].name
);
1015 if (stat(depfile
, &st
) != 0)
1018 return any_modules_newer(dirname
, st
.st_mtime
);
1021 static char *getline_wrapped(FILE *file
, unsigned int *linenum
)
1025 char *buf
= NOFAIL(malloc(size
));
1027 int ch
= getc_unlocked(file
);
1035 /* else fall through */
1041 buf
= NOFAIL(realloc(buf
, size
+ 1));
1046 ch
= getc_unlocked(file
);
1053 /* else fall through */
1060 buf
= NOFAIL(realloc(buf
, size
));
1067 static char *strsep_skipspace(char **string
, char *delim
)
1071 *string
+= strspn(*string
, delim
);
1072 return strsep(string
, delim
);
1075 static struct module_search
*add_search(const char *search_path
,
1077 struct module_search
*search
)
1080 struct module_search
*new;
1082 new = NOFAIL(malloc(sizeof(*new)));
1083 new->search_path
= NOFAIL(strdup(search_path
));
1091 static struct module_overrides
*add_override(const char *modfile
,
1092 struct module_overrides
*overrides
)
1095 struct module_overrides
*new;
1097 new = NOFAIL(malloc(sizeof(*new)));
1098 new->modfile
= NOFAIL(strdup(modfile
));
1099 new->next
= overrides
;
1105 static int parse_config_scan(const char *filename
,
1106 const char *basedir
,
1107 const char *kernelversion
,
1108 struct module_search
**search
,
1109 struct module_overrides
**overrides
);
1111 static int parse_config_file(const char *filename
,
1112 const char *basedir
,
1113 const char *kernelversion
,
1114 struct module_search
**search
,
1115 struct module_overrides
**overrides
)
1118 unsigned int linenum
= 0;
1121 cfile
= fopen(filename
, "r");
1123 if (errno
!= ENOENT
)
1124 fatal("could not open '%s', reason: %s\n", filename
,
1129 while ((line
= getline_wrapped(cfile
, &linenum
)) != NULL
) {
1131 char *cmd
, *modname
;
1133 cmd
= strsep_skipspace(&ptr
, "\t ");
1135 if (cmd
== NULL
|| cmd
[0] == '#' || cmd
[0] == '\0') {
1140 if (strcmp(cmd
, "search") == 0) {
1143 while ((search_path
= strsep_skipspace(&ptr
, "\t "))) {
1147 if (strcmp(search_path
,
1148 MODULE_BUILTIN_KEY
) == 0) {
1149 *search
= add_search(MODULE_BUILTIN_KEY
,
1153 len
= strlen(basedir
)
1154 + strlen(MODULE_DIR
)
1155 + strlen(kernelversion
)
1157 + strlen(search_path
);
1158 dirname
= NOFAIL(malloc(len
+ 1));
1159 sprintf(dirname
, "%s%s%s/%s", basedir
,
1160 MODULE_DIR
, kernelversion
, search_path
);
1161 *search
= add_search(dirname
, len
, *search
);
1164 } else if (strcmp(cmd
, "override") == 0) {
1165 char *pathname
= NULL
, *version
, *subdir
;
1166 modname
= strsep_skipspace(&ptr
, "\t ");
1167 version
= strsep_skipspace(&ptr
, "\t ");
1168 subdir
= strsep_skipspace(&ptr
, "\t ");
1170 if (strcmp(version
, kernelversion
) != 0 &&
1171 strcmp(version
, "*") != 0)
1174 pathname
= NOFAIL(malloc(strlen(basedir
)
1175 + strlen(MODULE_DIR
)
1176 + strlen(kernelversion
)
1181 sprintf(pathname
, "%s%s%s/%s/%s.ko", basedir
,
1182 MODULE_DIR
, kernelversion
, subdir
, modname
);
1184 *overrides
= add_override(pathname
, *overrides
);
1186 } else if (strcmp(cmd
, "include") == 0) {
1189 newfilename
= strsep_skipspace(&ptr
, "\t ");
1191 grammar(cmd
, filename
, linenum
);
1193 warn("\"include %s\" is deprecated, "
1194 "please use /etc/depmod.d\n", newfilename
);
1195 if (strncmp(newfilename
, "/etc/depmod.d",
1196 strlen("/etc/depmod.d")) == 0) {
1197 warn("\"include /etc/depmod.d\" is "
1198 "the default, ignored\n");
1200 if (!parse_config_scan(newfilename
, basedir
,
1203 warn("Failed to open included"
1204 " config file %s: %s\n",
1205 newfilename
, strerror(errno
));
1208 } else if (strcmp(cmd
, "make_map_files") == 0) {
1211 option
= strsep_skipspace(&ptr
, "\t ");
1213 grammar(cmd
, filename
, linenum
);
1215 if (0 == strncmp(option
, "yes", 3))
1217 else if (0 == strncmp(option
, "no", 2))
1220 grammar(cmd
, filename
, linenum
);
1223 grammar(cmd
, filename
, linenum
);
1231 static int parse_config_scan(const char *filename
,
1232 const char *basedir
,
1233 const char *kernelversion
,
1234 struct module_search
**search
,
1235 struct module_overrides
**overrides
)
1240 dir
= opendir(filename
);
1243 struct list_head node
;
1246 LIST_HEAD(files_list
);
1247 struct file_entry
*fe
, *fe_tmp
;
1250 /* sort files from directory into list */
1251 while ((i
= readdir(dir
)) != NULL
) {
1254 if (i
->d_name
[0] == '.')
1256 if (!config_filter(i
->d_name
))
1259 len
= strlen(i
->d_name
);
1260 if (len
< 6 || strcmp(&i
->d_name
[len
-5], ".conf") != 0)
1261 warn("All config files need .conf: %s/%s, "
1262 "it will be ignored in a future release.\n",
1263 filename
, i
->d_name
);
1264 fe
= malloc(sizeof(struct file_entry
) + len
+ 1);
1267 strcpy(fe
->name
, i
->d_name
);
1268 list_for_each_entry(fe_tmp
, &files_list
, node
)
1269 if (strcmp(fe_tmp
->name
, fe
->name
) >= 0)
1271 list_add_tail(&fe
->node
, &fe_tmp
->node
);
1275 /* parse list of files */
1276 list_for_each_entry_safe(fe
, fe_tmp
, &files_list
, node
) {
1279 nofail_asprintf(&cfgfile
, "%s/%s", filename
, fe
->name
);
1280 if (!parse_config_file(cfgfile
, basedir
, kernelversion
,
1282 warn("Failed to open config file "
1283 "%s: %s\n", fe
->name
, strerror(errno
));
1285 list_del(&fe
->node
);
1291 if (parse_config_file(filename
, basedir
, kernelversion
, search
,
1299 static void parse_toplevel_config(const char *filename
,
1300 const char *basedir
,
1301 const char *kernelversion
,
1302 struct module_search
**search
,
1303 struct module_overrides
**overrides
)
1306 if (!parse_config_scan(filename
, basedir
, kernelversion
, search
,
1308 fatal("Failed to open config file %s: %s\n",
1309 filename
, strerror(errno
));
1313 /* deprecated config file */
1314 if (parse_config_file("/etc/depmod.conf", basedir
, kernelversion
,
1315 search
, overrides
) > 0)
1316 warn("Deprecated config file /etc/depmod.conf, "
1317 "all config files belong into /etc/depmod.d/.\n");
1319 /* default config */
1320 parse_config_scan("/etc/depmod.d", basedir
, kernelversion
,
1324 /* Local to main, but not freed on exit. Keep valgrind quiet. */
1325 struct module
*list
= NULL
;
1326 struct module_search
*search
= NULL
;
1327 struct module_overrides
*overrides
= NULL
;
1329 int main(int argc
, char *argv
[])
1331 int opt
, all
= 0, maybe_all
= 0, doing_stdout
= 0;
1332 char *basedir
= "", *dirname
, *version
, *badopt
= NULL
,
1335 const char *config
= NULL
;
1337 /* Don't print out any errors just yet, we might want to exec
1338 backwards compat version. */
1340 while ((opt
= getopt_long(argc
, argv
, "ab:ArehnqruvVF:C:wm", options
, NULL
))
1348 skipchars
= strlen(basedir
);
1354 system_map
= optarg
;
1370 print_usage(argv
[0]);
1377 printf("%s %s\n", PACKAGE
, VERSION
);
1383 force_map_files
= 1;
1386 badopt
= argv
[optind
-1];
1390 /* We can't print unknowns without a System.map */
1394 load_system_map(system_map
);
1396 /* They can specify the version naked on the command line */
1397 if (optind
< argc
&& is_version_number(argv
[optind
])) {
1398 version
= NOFAIL(strdup(argv
[optind
]));
1403 version
= NOFAIL(strdup(buf
.release
));
1406 /* Run old version if required. */
1407 if (old_module_version(version
))
1408 exec_old_depmod(argv
);
1411 fprintf(stderr
, "%s: malformed/unrecognized option '%s'\n",
1413 print_usage(argv
[0]);
1417 /* Depmod -a by default if no names. */
1421 dirname
= NOFAIL(malloc(strlen(basedir
)
1422 + strlen(MODULE_DIR
)
1423 + strlen(version
) + 1));
1424 sprintf(dirname
, "%s%s%s", basedir
, MODULE_DIR
, version
);
1427 if (!doing_stdout
&& !depfile_out_of_date(dirname
))
1432 parse_toplevel_config(config
, basedir
, version
, &search
, &overrides
);
1434 /* For backward compatibility add "updates" to the head of the search
1435 * list here. But only if there was no "search" option specified.
1439 search
= add_search("updates", strlen("updates"), search
);
1442 /* Do command line args. */
1443 for (opt
= optind
; opt
< argc
; opt
++) {
1446 if (argv
[opt
][0] != '/')
1447 fatal("modules must be specified using absolute paths.\n"
1448 "\"%s\" is a relative path\n", argv
[opt
]);
1450 new = grab_module(NULL
, argv
[opt
]);
1452 /* cmd-line specified modules must exist */
1453 fatal("grab_module() failed for module %s\n", argv
[opt
]);
1459 list
= grab_basedir(dirname
,search
,overrides
);
1461 list
= sort_modules(dirname
,list
);
1462 list
= parse_modules(list
);
1464 for (i
= 0; i
< sizeof(depfiles
)/sizeof(depfiles
[0]); i
++) {
1466 struct depfile
*d
= &depfiles
[i
];
1467 char depname
[strlen(dirname
) + 1 + strlen(d
->name
) + 1];
1468 char tmpname
[strlen(dirname
) + 1 + strlen(d
->name
) +
1469 strlen(".temp") + 1];
1471 if (d
->map_file
&& !make_map_files
&& !force_map_files
)
1474 sprintf(depname
, "%s/%s", dirname
, d
->name
);
1475 sprintf(tmpname
, "%s/%s.temp", dirname
, d
->name
);
1476 if (!doing_stdout
) {
1477 out
= fopen(tmpname
, "w");
1479 fatal("Could not open %s for writing: %s\n",
1480 tmpname
, strerror(errno
));
1483 if (ends_in(depname
, ".bin"))
1486 d
->func(list
, out
, dirname
);
1487 if (!doing_stdout
) {
1489 if (rename(tmpname
, depname
) < 0)
1490 fatal("Could not rename %s into %s: %s\n",
1491 tmpname
, depname
, strerror(errno
));