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)
596 prio_new
= prio_builtin
;
598 prio_old
= prio_builtin
;
600 return prio_new
> prio_old
;
604 static struct module
*do_module(const char *dirname
,
605 const char *filename
,
607 struct module_search
*search
,
608 struct module_overrides
*overrides
)
610 struct module
*new, **i
;
612 new = grab_module(dirname
, filename
);
616 /* Check if module is already in the list. */
617 for (i
= &list
; *i
; i
= &(*i
)->next
) {
619 if (streq((*i
)->basename
, filename
)) {
620 char newpath
[strlen(dirname
) + strlen("/")
621 + strlen(filename
) + 1];
623 sprintf(newpath
, "%s/%s", dirname
, filename
);
625 if (is_higher_priority(newpath
, (*i
)->pathname
,search
,
632 del_module(NULL
, new);
638 /* Not in the list already. Just prepend. */
643 static struct module
*grab_dir(const char *dirname
,
647 struct module_search
*search
,
648 struct module_overrides
*overrides
)
650 struct dirent
*dirent
;
652 while ((dirent
= readdir(dir
)) != NULL
) {
653 if (smells_like_module(dirent
->d_name
))
654 next
= do_mod(dirname
, dirent
->d_name
, next
,
656 else if (!streq(dirent
->d_name
, ".")
657 && !streq(dirent
->d_name
, "..")
658 && !streq(dirent
->d_name
, "source")
659 && !streq(dirent
->d_name
, "build")) {
662 char subdir
[strlen(dirname
) + 1
663 + strlen(dirent
->d_name
) + 1];
664 sprintf(subdir
, "%s/%s", dirname
, dirent
->d_name
);
665 sub
= opendir(subdir
);
667 next
= grab_dir(subdir
, sub
, next
, do_mod
,
676 static struct module
*grab_basedir(const char *dirname
,
677 struct module_search
*search
,
678 struct module_overrides
*overrides
)
683 dir
= opendir(dirname
);
685 warn("Couldn't open directory %s: %s\n",
686 dirname
, strerror(errno
));
689 list
= grab_dir(dirname
, dir
, NULL
, do_module
, search
, overrides
);
695 static struct module
*sort_modules(const char *dirname
, struct module
*list
)
697 struct module
*tlist
= NULL
, **tpos
= &tlist
;
699 int dir_len
= strlen(dirname
) + 1;
700 char file_name
[dir_len
+ strlen("modules.order") + 1];
702 unsigned int linenum
= 0;
704 sprintf(file_name
, "%s/%s", dirname
, "modules.order");
706 modorder
= fopen(file_name
, "r");
708 /* Older kernels don't generate modules.order. Just
709 return if the file doesn't exist. */
712 fatal("Could not open '%s': %s\n", file_name
, strerror(errno
));
715 sprintf(line
, "%s/", dirname
);
717 /* move modules listed in modorder file to tlist in order */
718 while (fgets(line
, sizeof(line
), modorder
)) {
719 struct module
**pos
, *mod
;
720 int len
= strlen(line
);
723 if (line
[len
- 1] == '\n')
724 line
[len
- 1] = '\0';
726 for (pos
= &list
; (mod
= *pos
); pos
= &(*pos
)->next
) {
727 if (strcmp(line
, mod
->pathname
+ dir_len
) == 0) {
728 mod
->order
= linenum
;
738 /* append the rest */
746 static struct module
*parse_modules(struct module
*list
)
750 for (i
= list
; i
; i
= i
->next
) {
751 i
->ops
->load_symbols(i
);
752 i
->ops
->fetch_tables(i
);
755 for (i
= list
; i
; i
= i
->next
)
756 i
->ops
->calculate_deps(i
, verbose
);
758 /* Strip out modules with dependency loops. */
760 for (i
= list
; i
; i
= i
->next
) {
761 if (has_dep_loop(i
, NULL
)) {
762 warn("Module %s ignored, due to loop\n",
763 i
->pathname
+ skipchars
);
764 del_module(&list
, i
);
772 /* Simply dump hash table. */
773 static void output_symbols(struct module
*unused
, FILE *out
, char *dirname
)
777 fprintf(out
, "# Aliases for symbols, used by symbol_request().\n");
778 for (i
= 0; i
< SYMBOL_HASH_SIZE
; i
++) {
781 for (s
= symbolhash
[i
]; s
; s
= s
->next
) {
783 char modname
[strlen(s
->owner
->pathname
)+1];
784 filename2modname(modname
, s
->owner
->pathname
);
785 fprintf(out
, "alias symbol:%s %s\n",
792 static void output_symbols_bin(struct module
*unused
, FILE *out
, char *dirname
)
794 struct index_node
*index
;
799 index
= index_create();
801 for (i
= 0; i
< SYMBOL_HASH_SIZE
; i
++) {
804 for (s
= symbolhash
[i
]; s
; s
= s
->next
) {
806 char modname
[strlen(s
->owner
->pathname
)+1];
807 filename2modname(modname
, s
->owner
->pathname
);
808 nofail_asprintf(&alias
, "symbol:%s", s
->name
);
809 duplicate
= index_insert(index
, alias
, modname
,
811 if (duplicate
&& warn_dups
)
812 warn("duplicate module syms:\n%s %s\n",
819 index_write(index
, out
);
820 index_destroy(index
);
823 static const char *next_string(const char *string
, unsigned long *secsize
)
825 /* Skip non-zero chars */
828 if ((*secsize
)-- <= 1)
832 /* Skip any zero padding. */
835 if ((*secsize
)-- <= 1)
841 /* Careful! Don't munge - in [ ] as per Debian Bug#350915 */
842 static char *underscores(char *string
)
849 for (i
= 0; string
[i
]; i
++) {
856 warn("Unmatched bracket in %s\n", string
);
860 i
+= strcspn(&string
[i
], "]");
862 warn("Unmatched bracket in %s\n", string
);
868 static void output_aliases(struct module
*modules
, FILE *out
, char *dirname
)
874 fprintf(out
, "# Aliases extracted from modules themselves.\n");
875 for (i
= modules
; i
; i
= i
->next
) {
876 char modname
[strlen(i
->pathname
)+1];
878 filename2modname(modname
, i
->pathname
);
880 /* Grab from old-style .modalias section. */
881 for (p
= i
->ops
->get_aliases(i
, &size
);
883 p
= next_string(p
, &size
))
884 fprintf(out
, "alias %s %s\n", p
, modname
);
886 /* Grab form new-style .modinfo section. */
887 for (p
= i
->ops
->get_modinfo(i
, &size
);
889 p
= next_string(p
, &size
)) {
890 if (strncmp(p
, "alias=", strlen("alias=")) == 0)
891 fprintf(out
, "alias %s %s\n",
892 p
+ strlen("alias="), modname
);
897 static void output_aliases_bin(struct module
*modules
, FILE *out
, char *dirname
)
903 struct index_node
*index
;
906 index
= index_create();
908 for (i
= modules
; i
; i
= i
->next
) {
909 char modname
[strlen(i
->pathname
)+1];
911 filename2modname(modname
, i
->pathname
);
913 /* Grab from old-style .modalias section. */
914 for (p
= i
->ops
->get_aliases(i
, &size
);
916 p
= next_string(p
, &size
)) {
917 alias
= NOFAIL(strdup(p
));
919 duplicate
= index_insert(index
, alias
, modname
, i
->order
);
920 if (duplicate
&& warn_dups
)
921 warn("duplicate module alias:\n%s %s\n",
926 /* Grab from new-style .modinfo section. */
927 for (p
= i
->ops
->get_modinfo(i
, &size
);
929 p
= next_string(p
, &size
)) {
930 if (strncmp(p
, "alias=", strlen("alias=")) == 0) {
931 alias
= NOFAIL(strdup(p
+ strlen("alias=")));
933 duplicate
= index_insert(index
, alias
, modname
, i
->order
);
934 if (duplicate
&& warn_dups
)
935 warn("duplicate module alias:\n%s %s\n",
942 index_write(index
, out
);
943 index_destroy(index
);
948 void (*func
)(struct module
*, FILE *, char *dirname
);
952 static struct depfile depfiles
[] = {
953 { "modules.dep", output_deps
, 0 }, /* This is what we check for '-A'. */
954 { "modules.dep.bin", output_deps_bin
, 0 },
955 { "modules.pcimap", output_pci_table
, 1 },
956 { "modules.usbmap", output_usb_table
, 1 },
957 { "modules.ccwmap", output_ccw_table
, 1 },
958 { "modules.ieee1394map", output_ieee1394_table
, 1 },
959 { "modules.isapnpmap", output_isapnp_table
, 1 },
960 { "modules.inputmap", output_input_table
, 1 },
961 { "modules.ofmap", output_of_table
, 1 },
962 { "modules.seriomap", output_serio_table
, 1 },
963 { "modules.alias", output_aliases
, 0 },
964 { "modules.alias.bin", output_aliases_bin
, 0 },
965 { "modules.symbols", output_symbols
, 0 },
966 { "modules.symbols.bin", output_symbols_bin
, 0 }
969 /* If we can't figure it out, it's safe to say "true". */
970 static int any_modules_newer(const char *dirname
, time_t mtime
)
973 struct dirent
*dirent
;
975 dir
= opendir(dirname
);
979 while ((dirent
= readdir(dir
)) != NULL
) {
981 char file
[strlen(dirname
) + 1 + strlen(dirent
->d_name
) + 1];
983 if (streq(dirent
->d_name
, ".") || streq(dirent
->d_name
, ".."))
986 sprintf(file
, "%s/%s", dirname
, dirent
->d_name
);
987 if (lstat(file
, &st
) != 0)
990 if (smells_like_module(dirent
->d_name
)) {
991 if (st
.st_mtime
> mtime
)
993 } else if (S_ISDIR(st
.st_mode
)) {
994 if (any_modules_newer(file
, mtime
))
1006 static int depfile_out_of_date(const char *dirname
)
1009 char depfile
[strlen(dirname
) + 1 + strlen(depfiles
[0].name
) + 1];
1011 sprintf(depfile
, "%s/%s", dirname
, depfiles
[0].name
);
1013 if (stat(depfile
, &st
) != 0)
1016 return any_modules_newer(dirname
, st
.st_mtime
);
1019 static char *getline_wrapped(FILE *file
, unsigned int *linenum
)
1023 char *buf
= NOFAIL(malloc(size
));
1025 int ch
= getc_unlocked(file
);
1033 /* else fall through */
1039 buf
= NOFAIL(realloc(buf
, size
+ 1));
1044 ch
= getc_unlocked(file
);
1051 /* else fall through */
1058 buf
= NOFAIL(realloc(buf
, size
));
1065 static char *strsep_skipspace(char **string
, char *delim
)
1069 *string
+= strspn(*string
, delim
);
1070 return strsep(string
, delim
);
1073 static struct module_search
*add_search(const char *search_path
,
1075 struct module_search
*search
)
1078 struct module_search
*new;
1080 new = NOFAIL(malloc(sizeof(*new)));
1081 new->search_path
= NOFAIL(strdup(search_path
));
1089 static struct module_overrides
*add_override(const char *modfile
,
1090 struct module_overrides
*overrides
)
1093 struct module_overrides
*new;
1095 new = NOFAIL(malloc(sizeof(*new)));
1096 new->modfile
= NOFAIL(strdup(modfile
));
1097 new->next
= overrides
;
1103 static int parse_config_scan(const char *filename
,
1104 const char *basedir
,
1105 const char *kernelversion
,
1106 struct module_search
**search
,
1107 struct module_overrides
**overrides
);
1109 static int parse_config_file(const char *filename
,
1110 const char *basedir
,
1111 const char *kernelversion
,
1112 struct module_search
**search
,
1113 struct module_overrides
**overrides
)
1116 unsigned int linenum
= 0;
1119 cfile
= fopen(filename
, "r");
1121 if (errno
!= ENOENT
)
1122 fatal("could not open '%s', reason: %s\n", filename
,
1127 while ((line
= getline_wrapped(cfile
, &linenum
)) != NULL
) {
1129 char *cmd
, *modname
;
1131 cmd
= strsep_skipspace(&ptr
, "\t ");
1133 if (cmd
== NULL
|| cmd
[0] == '#' || cmd
[0] == '\0') {
1138 if (strcmp(cmd
, "search") == 0) {
1141 while ((search_path
= strsep_skipspace(&ptr
, "\t "))) {
1145 if (strcmp(search_path
,
1146 MODULE_BUILTIN_KEY
) == 0) {
1147 *search
= add_search(MODULE_BUILTIN_KEY
,
1151 len
= strlen(basedir
)
1152 + strlen(MODULE_DIR
)
1153 + strlen(kernelversion
)
1155 + strlen(search_path
);
1156 dirname
= NOFAIL(malloc(len
+ 1));
1157 sprintf(dirname
, "%s%s%s/%s", basedir
,
1158 MODULE_DIR
, kernelversion
, search_path
);
1159 *search
= add_search(dirname
, len
, *search
);
1162 } else if (strcmp(cmd
, "override") == 0) {
1163 char *pathname
= NULL
, *version
, *subdir
;
1164 modname
= strsep_skipspace(&ptr
, "\t ");
1165 version
= strsep_skipspace(&ptr
, "\t ");
1166 subdir
= strsep_skipspace(&ptr
, "\t ");
1168 if (strcmp(version
, kernelversion
) != 0 &&
1169 strcmp(version
, "*") != 0)
1172 pathname
= NOFAIL(malloc(strlen(basedir
)
1173 + strlen(MODULE_DIR
)
1174 + strlen(kernelversion
)
1179 sprintf(pathname
, "%s%s%s/%s/%s.ko", basedir
,
1180 MODULE_DIR
, kernelversion
, subdir
, modname
);
1182 *overrides
= add_override(pathname
, *overrides
);
1184 } else if (strcmp(cmd
, "include") == 0) {
1187 newfilename
= strsep_skipspace(&ptr
, "\t ");
1189 grammar(cmd
, filename
, linenum
);
1191 warn("\"include %s\" is deprecated, "
1192 "please use /etc/depmod.d\n", newfilename
);
1193 if (strncmp(newfilename
, "/etc/depmod.d",
1194 strlen("/etc/depmod.d")) == 0) {
1195 warn("\"include /etc/depmod.d\" is "
1196 "the default, ignored\n");
1198 if (!parse_config_scan(newfilename
, basedir
,
1201 warn("Failed to open included"
1202 " config file %s: %s\n",
1203 newfilename
, strerror(errno
));
1206 } else if (strcmp(cmd
, "make_map_files") == 0) {
1209 option
= strsep_skipspace(&ptr
, "\t ");
1211 grammar(cmd
, filename
, linenum
);
1213 if (0 == strncmp(option
, "yes", 3))
1215 else if (0 == strncmp(option
, "no", 2))
1218 grammar(cmd
, filename
, linenum
);
1221 grammar(cmd
, filename
, linenum
);
1229 static int parse_config_scan(const char *filename
,
1230 const char *basedir
,
1231 const char *kernelversion
,
1232 struct module_search
**search
,
1233 struct module_overrides
**overrides
)
1238 dir
= opendir(filename
);
1241 struct list_head node
;
1244 LIST_HEAD(files_list
);
1245 struct file_entry
*fe
, *fe_tmp
;
1248 /* sort files from directory into list */
1249 while ((i
= readdir(dir
)) != NULL
) {
1252 if (i
->d_name
[0] == '.')
1254 if (!config_filter(i
->d_name
))
1257 len
= strlen(i
->d_name
);
1258 if (len
< 6 || strcmp(&i
->d_name
[len
-5], ".conf") != 0)
1259 warn("All config files need .conf: %s/%s, "
1260 "it will be ignored in a future release.\n",
1261 filename
, i
->d_name
);
1262 fe
= malloc(sizeof(struct file_entry
) + len
+ 1);
1265 strcpy(fe
->name
, i
->d_name
);
1266 list_for_each_entry(fe_tmp
, &files_list
, node
)
1267 if (strcmp(fe_tmp
->name
, fe
->name
) >= 0)
1269 list_add_tail(&fe
->node
, &fe_tmp
->node
);
1273 /* parse list of files */
1274 list_for_each_entry_safe(fe
, fe_tmp
, &files_list
, node
) {
1277 nofail_asprintf(&cfgfile
, "%s/%s", filename
, fe
->name
);
1278 if (!parse_config_file(cfgfile
, basedir
, kernelversion
,
1280 warn("Failed to open config file "
1281 "%s: %s\n", fe
->name
, strerror(errno
));
1283 list_del(&fe
->node
);
1289 if (parse_config_file(filename
, basedir
, kernelversion
, search
,
1297 static void parse_toplevel_config(const char *filename
,
1298 const char *basedir
,
1299 const char *kernelversion
,
1300 struct module_search
**search
,
1301 struct module_overrides
**overrides
)
1304 if (!parse_config_scan(filename
, basedir
, kernelversion
, search
,
1306 fatal("Failed to open config file %s: %s\n",
1307 filename
, strerror(errno
));
1311 /* deprecated config file */
1312 if (parse_config_file("/etc/depmod.conf", basedir
, kernelversion
,
1313 search
, overrides
) > 0)
1314 warn("Deprecated config file /etc/depmod.conf, "
1315 "all config files belong into /etc/depmod.d/.\n");
1317 /* default config */
1318 parse_config_scan("/etc/depmod.d", basedir
, kernelversion
,
1322 /* Local to main, but not freed on exit. Keep valgrind quiet. */
1323 struct module
*list
= NULL
;
1324 struct module_search
*search
= NULL
;
1325 struct module_overrides
*overrides
= NULL
;
1327 int main(int argc
, char *argv
[])
1329 int opt
, all
= 0, maybe_all
= 0, doing_stdout
= 0;
1330 char *basedir
= "", *dirname
, *version
, *badopt
= NULL
,
1333 const char *config
= NULL
;
1335 /* Don't print out any errors just yet, we might want to exec
1336 backwards compat version. */
1338 while ((opt
= getopt_long(argc
, argv
, "ab:ArehnqruvVF:C:wm", options
, NULL
))
1346 skipchars
= strlen(basedir
);
1352 system_map
= optarg
;
1368 print_usage(argv
[0]);
1375 printf("%s %s\n", PACKAGE
, VERSION
);
1381 force_map_files
= 1;
1384 badopt
= argv
[optind
-1];
1388 /* We can't print unknowns without a System.map */
1392 load_system_map(system_map
);
1394 /* They can specify the version naked on the command line */
1395 if (optind
< argc
&& is_version_number(argv
[optind
])) {
1396 version
= NOFAIL(strdup(argv
[optind
]));
1401 version
= NOFAIL(strdup(buf
.release
));
1404 /* Run old version if required. */
1405 if (old_module_version(version
))
1406 exec_old_depmod(argv
);
1409 fprintf(stderr
, "%s: malformed/unrecognized option '%s'\n",
1411 print_usage(argv
[0]);
1415 /* Depmod -a by default if no names. */
1419 dirname
= NOFAIL(malloc(strlen(basedir
)
1420 + strlen(MODULE_DIR
)
1421 + strlen(version
) + 1));
1422 sprintf(dirname
, "%s%s%s", basedir
, MODULE_DIR
, version
);
1425 if (!doing_stdout
&& !depfile_out_of_date(dirname
))
1430 parse_toplevel_config(config
, basedir
, version
, &search
, &overrides
);
1432 /* For backward compatibility add "updates" to the head of the search
1433 * list here. But only if there was no "search" option specified.
1439 len
= strlen(basedir
)
1440 + strlen(MODULE_DIR
)
1442 + strlen("/updates");
1443 dirname
= NOFAIL(malloc(len
+ 1));
1444 sprintf(dirname
, "%s%s%s/updates", basedir
,
1445 MODULE_DIR
, version
);
1446 search
= add_search(dirname
, len
, search
);
1449 /* Do command line args. */
1450 for (opt
= optind
; opt
< argc
; opt
++) {
1453 if (argv
[opt
][0] != '/')
1454 fatal("modules must be specified using absolute paths.\n"
1455 "\"%s\" is a relative path\n", argv
[opt
]);
1457 new = grab_module(NULL
, argv
[opt
]);
1459 /* cmd-line specified modules must exist */
1460 fatal("grab_module() failed for module %s\n", argv
[opt
]);
1466 list
= grab_basedir(dirname
,search
,overrides
);
1468 list
= sort_modules(dirname
,list
);
1469 list
= parse_modules(list
);
1471 for (i
= 0; i
< sizeof(depfiles
)/sizeof(depfiles
[0]); i
++) {
1473 struct depfile
*d
= &depfiles
[i
];
1474 char depname
[strlen(dirname
) + 1 + strlen(d
->name
) + 1];
1475 char tmpname
[strlen(dirname
) + 1 + strlen(d
->name
) +
1476 strlen(".temp") + 1];
1478 if (d
->map_file
&& !make_map_files
&& !force_map_files
)
1481 sprintf(depname
, "%s/%s", dirname
, d
->name
);
1482 sprintf(tmpname
, "%s/%s.temp", dirname
, d
->name
);
1483 if (!doing_stdout
) {
1484 out
= fopen(tmpname
, "w");
1486 fatal("Could not open %s for writing: %s\n",
1487 tmpname
, strerror(errno
));
1490 if (ends_in(depname
, ".bin"))
1493 d
->func(list
, out
, dirname
);
1494 if (!doing_stdout
) {
1496 if (rename(tmpname
, depname
) < 0)
1497 fatal("Could not rename %s into %s: %s\n",
1498 tmpname
, depname
, strerror(errno
));