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 static void output_deps(struct module
*modules
,
483 FILE *out
, char *dirname
)
487 for (i
= modules
; i
; i
= i
->next
) {
488 struct list_head
*j
, *tmp
;
489 order_dep_list(i
, i
);
491 fprintf(out
, "%s:", i
->pathname
+ strlen(dirname
)+1);
492 list_for_each_safe(j
, tmp
, &i
->dep_list
) {
494 = list_entry(j
, struct module
, dep_list
);
495 fprintf(out
, " %s", dep
->pathname
+ strlen(dirname
)+1);
502 /* warn whenever duplicate module aliases, deps, or symbols are found. */
505 static void output_deps_bin(struct module
*modules
,
506 FILE *out
, char *dirname
)
509 struct index_node
*index
;
513 index
= index_create();
515 for (i
= modules
; i
; i
= i
->next
) {
516 struct list_head
*j
, *tmp
;
517 char modname
[strlen(i
->pathname
)+1];
519 order_dep_list(i
, i
);
521 filename2modname(modname
, i
->pathname
+ strlen(dirname
)+1);
522 nofail_asprintf(&line
, "%s:", i
->pathname
+ strlen(dirname
)+1);
524 list_for_each_safe(j
, tmp
, &i
->dep_list
) {
526 = list_entry(j
, struct module
, dep_list
);
527 nofail_asprintf(&line
, "%s %s", p
, dep
->pathname
+ strlen(dirname
)+1);
532 if (index_insert(index
, modname
, line
, i
->order
) && warn_dups
)
533 warn("duplicate module deps:\n%s\n",line
);
537 index_write(index
, out
);
538 index_destroy(index
);
542 static int smells_like_module(const char *name
)
544 return ends_in(name
,".ko") || ends_in(name
, ".ko.gz");
547 typedef struct module
*(*do_module_t
)(const char *dirname
,
548 const char *filename
,
550 struct module_search
*search
,
551 struct module_overrides
*overrides
);
553 static int is_higher_priority(const char *newpath
, const char *oldpath
,
554 struct module_search
*search
,
555 struct module_overrides
*overrides
)
557 struct module_search
*tmp
;
558 struct module_overrides
*ovtmp
;
560 int prio_builtin
= -1;
564 /* The names already match, now we check for overrides and directory search
567 for (ovtmp
= overrides
; ovtmp
!= NULL
; ovtmp
= ovtmp
->next
) {
568 if (strcmp(ovtmp
->modfile
, newpath
) == 0)
570 if (strcmp(ovtmp
->modfile
, oldpath
) == 0)
573 for (i
= 0, tmp
= search
; tmp
!= NULL
; tmp
= tmp
->next
, i
++) {
574 if (strcmp(tmp
->search_path
, MODULE_BUILTIN_KEY
) == 0)
576 else if (strncmp(tmp
->search_path
, newpath
, tmp
->len
) == 0)
578 else if (strncmp(tmp
->search_path
, oldpath
, tmp
->len
) == 0)
581 if (prio_builtin
< 0)
584 prio_new
= prio_builtin
;
586 prio_old
= prio_builtin
;
588 return prio_new
> prio_old
;
592 static struct module
*do_module(const char *dirname
,
593 const char *filename
,
595 struct module_search
*search
,
596 struct module_overrides
*overrides
)
598 struct module
*new, **i
;
600 new = grab_module(dirname
, filename
);
604 /* Check if module is already in the list. */
605 for (i
= &list
; *i
; i
= &(*i
)->next
) {
607 if (streq((*i
)->basename
, filename
)) {
608 char newpath
[strlen(dirname
) + strlen("/")
609 + strlen(filename
) + 1];
611 sprintf(newpath
, "%s/%s", dirname
, filename
);
613 if (is_higher_priority(newpath
, (*i
)->pathname
,search
,
620 del_module(NULL
, new);
626 /* Not in the list already. Just prepend. */
631 static struct module
*grab_dir(const char *dirname
,
635 struct module_search
*search
,
636 struct module_overrides
*overrides
)
638 struct dirent
*dirent
;
640 while ((dirent
= readdir(dir
)) != NULL
) {
641 if (smells_like_module(dirent
->d_name
))
642 next
= do_mod(dirname
, dirent
->d_name
, next
,
644 else if (!streq(dirent
->d_name
, ".")
645 && !streq(dirent
->d_name
, "..")
646 && !streq(dirent
->d_name
, "source")
647 && !streq(dirent
->d_name
, "build")) {
650 char subdir
[strlen(dirname
) + 1
651 + strlen(dirent
->d_name
) + 1];
652 sprintf(subdir
, "%s/%s", dirname
, dirent
->d_name
);
653 sub
= opendir(subdir
);
655 next
= grab_dir(subdir
, sub
, next
, do_mod
,
664 static struct module
*grab_basedir(const char *dirname
,
665 struct module_search
*search
,
666 struct module_overrides
*overrides
)
671 dir
= opendir(dirname
);
673 warn("Couldn't open directory %s: %s\n",
674 dirname
, strerror(errno
));
677 list
= grab_dir(dirname
, dir
, NULL
, do_module
, search
, overrides
);
683 static struct module
*sort_modules(const char *dirname
, struct module
*list
)
685 struct module
*tlist
= NULL
, **tpos
= &tlist
;
687 int dir_len
= strlen(dirname
) + 1;
688 char file_name
[dir_len
+ strlen("modules.order") + 1];
690 unsigned int linenum
= 0;
692 sprintf(file_name
, "%s/%s", dirname
, "modules.order");
694 modorder
= fopen(file_name
, "r");
696 /* Older kernels don't generate modules.order. Just
697 return if the file doesn't exist. */
700 fatal("Could not open '%s': %s\n", file_name
, strerror(errno
));
703 sprintf(line
, "%s/", dirname
);
705 /* move modules listed in modorder file to tlist in order */
706 while (fgets(line
, sizeof(line
), modorder
)) {
707 struct module
**pos
, *mod
;
708 int len
= strlen(line
);
711 if (line
[len
- 1] == '\n')
712 line
[len
- 1] = '\0';
714 for (pos
= &list
; (mod
= *pos
); pos
= &(*pos
)->next
) {
715 if (strcmp(line
, mod
->pathname
+ dir_len
) == 0) {
716 mod
->order
= linenum
;
726 /* append the rest */
734 static struct module
*parse_modules(struct module
*list
)
738 for (i
= list
; i
; i
= i
->next
) {
739 i
->ops
->load_symbols(i
);
740 i
->ops
->fetch_tables(i
);
743 for (i
= list
; i
; i
= i
->next
)
744 i
->ops
->calculate_deps(i
, verbose
);
746 /* Strip out modules with dependency loops. */
748 for (i
= list
; i
; i
= i
->next
) {
749 if (has_dep_loop(i
, NULL
)) {
750 warn("Module %s ignored, due to loop\n",
751 i
->pathname
+ skipchars
);
752 del_module(&list
, i
);
760 /* Simply dump hash table. */
761 static void output_symbols(struct module
*unused
, FILE *out
, char *dirname
)
765 fprintf(out
, "# Aliases for symbols, used by symbol_request().\n");
766 for (i
= 0; i
< SYMBOL_HASH_SIZE
; i
++) {
769 for (s
= symbolhash
[i
]; s
; s
= s
->next
) {
771 char modname
[strlen(s
->owner
->pathname
)+1];
772 filename2modname(modname
, s
->owner
->pathname
);
773 fprintf(out
, "alias symbol:%s %s\n",
780 static void output_symbols_bin(struct module
*unused
, FILE *out
, char *dirname
)
782 struct index_node
*index
;
787 index
= index_create();
789 for (i
= 0; i
< SYMBOL_HASH_SIZE
; i
++) {
792 for (s
= symbolhash
[i
]; s
; s
= s
->next
) {
794 char modname
[strlen(s
->owner
->pathname
)+1];
795 filename2modname(modname
, s
->owner
->pathname
);
796 nofail_asprintf(&alias
, "symbol:%s", s
->name
);
797 duplicate
= index_insert(index
, alias
, modname
,
799 if (duplicate
&& warn_dups
)
800 warn("duplicate module syms:\n%s %s\n",
807 index_write(index
, out
);
808 index_destroy(index
);
811 static const char *next_string(const char *string
, unsigned long *secsize
)
813 /* Skip non-zero chars */
816 if ((*secsize
)-- <= 1)
820 /* Skip any zero padding. */
823 if ((*secsize
)-- <= 1)
829 /* Careful! Don't munge - in [ ] as per Debian Bug#350915 */
830 static char *underscores(char *string
)
837 for (i
= 0; string
[i
]; i
++) {
844 i
+= strcspn(&string
[i
], "]");
846 warn("Unmatched bracket in %s\n", string
);
852 static void output_aliases(struct module
*modules
, FILE *out
, char *dirname
)
858 fprintf(out
, "# Aliases extracted from modules themselves.\n");
859 for (i
= modules
; i
; i
= i
->next
) {
860 char modname
[strlen(i
->pathname
)+1];
862 filename2modname(modname
, i
->pathname
);
864 /* Grab from old-style .modalias section. */
865 for (p
= i
->ops
->get_aliases(i
, &size
);
867 p
= next_string(p
, &size
))
868 fprintf(out
, "alias %s %s\n", p
, modname
);
870 /* Grab form new-style .modinfo section. */
871 for (p
= i
->ops
->get_modinfo(i
, &size
);
873 p
= next_string(p
, &size
)) {
874 if (strncmp(p
, "alias=", strlen("alias=")) == 0)
875 fprintf(out
, "alias %s %s\n",
876 p
+ strlen("alias="), modname
);
881 static void output_aliases_bin(struct module
*modules
, FILE *out
, char *dirname
)
887 struct index_node
*index
;
890 index
= index_create();
892 for (i
= modules
; i
; i
= i
->next
) {
893 char modname
[strlen(i
->pathname
)+1];
895 filename2modname(modname
, i
->pathname
);
897 /* Grab from old-style .modalias section. */
898 for (p
= i
->ops
->get_aliases(i
, &size
);
900 p
= next_string(p
, &size
)) {
901 alias
= NOFAIL(strdup(p
));
903 duplicate
= index_insert(index
, alias
, modname
, i
->order
);
904 if (duplicate
&& warn_dups
)
905 warn("duplicate module alias:\n%s %s\n",
910 /* Grab from new-style .modinfo section. */
911 for (p
= i
->ops
->get_modinfo(i
, &size
);
913 p
= next_string(p
, &size
)) {
914 if (strncmp(p
, "alias=", strlen("alias=")) == 0) {
915 alias
= NOFAIL(strdup(p
+ strlen("alias=")));
917 duplicate
= index_insert(index
, alias
, modname
, i
->order
);
918 if (duplicate
&& warn_dups
)
919 warn("duplicate module alias:\n%s %s\n",
926 index_write(index
, out
);
927 index_destroy(index
);
932 void (*func
)(struct module
*, FILE *, char *dirname
);
936 static struct depfile depfiles
[] = {
937 { "modules.dep", output_deps
, 0 }, /* This is what we check for '-A'. */
938 { "modules.dep.bin", output_deps_bin
, 0 },
939 { "modules.pcimap", output_pci_table
, 1 },
940 { "modules.usbmap", output_usb_table
, 1 },
941 { "modules.ccwmap", output_ccw_table
, 1 },
942 { "modules.ieee1394map", output_ieee1394_table
, 1 },
943 { "modules.isapnpmap", output_isapnp_table
, 1 },
944 { "modules.inputmap", output_input_table
, 1 },
945 { "modules.ofmap", output_of_table
, 1 },
946 { "modules.seriomap", output_serio_table
, 1 },
947 { "modules.alias", output_aliases
, 0 },
948 { "modules.alias.bin", output_aliases_bin
, 0 },
949 { "modules.symbols", output_symbols
, 0 },
950 { "modules.symbols.bin", output_symbols_bin
, 0 }
953 /* If we can't figure it out, it's safe to say "true". */
954 static int any_modules_newer(const char *dirname
, time_t mtime
)
957 struct dirent
*dirent
;
959 dir
= opendir(dirname
);
963 while ((dirent
= readdir(dir
)) != NULL
) {
965 char file
[strlen(dirname
) + 1 + strlen(dirent
->d_name
) + 1];
967 if (streq(dirent
->d_name
, ".") || streq(dirent
->d_name
, ".."))
970 sprintf(file
, "%s/%s", dirname
, dirent
->d_name
);
971 if (lstat(file
, &st
) != 0)
974 if (smells_like_module(dirent
->d_name
)) {
975 if (st
.st_mtime
> mtime
)
977 } else if (S_ISDIR(st
.st_mode
)) {
978 if (any_modules_newer(file
, mtime
))
990 static int depfile_out_of_date(const char *dirname
)
993 char depfile
[strlen(dirname
) + 1 + strlen(depfiles
[0].name
) + 1];
995 sprintf(depfile
, "%s/%s", dirname
, depfiles
[0].name
);
997 if (stat(depfile
, &st
) != 0)
1000 return any_modules_newer(dirname
, st
.st_mtime
);
1003 static char *getline_wrapped(FILE *file
, unsigned int *linenum
)
1007 char *buf
= NOFAIL(malloc(size
));
1009 int ch
= getc_unlocked(file
);
1017 /* else fall through */
1023 buf
= NOFAIL(realloc(buf
, size
+ 1));
1028 ch
= getc_unlocked(file
);
1035 /* else fall through */
1042 buf
= NOFAIL(realloc(buf
, size
));
1049 static char *strsep_skipspace(char **string
, char *delim
)
1053 *string
+= strspn(*string
, delim
);
1054 return strsep(string
, delim
);
1057 static struct module_search
*add_search(const char *search_path
,
1059 struct module_search
*search
)
1062 struct module_search
*new;
1064 new = NOFAIL(malloc(sizeof(*new)));
1065 new->search_path
= NOFAIL(strdup(search_path
));
1073 static struct module_overrides
*add_override(const char *modfile
,
1074 struct module_overrides
*overrides
)
1077 struct module_overrides
*new;
1079 new = NOFAIL(malloc(sizeof(*new)));
1080 new->modfile
= NOFAIL(strdup(modfile
));
1081 new->next
= overrides
;
1088 static int read_config(const char *filename
,
1089 const char *basedir
,
1090 const char *kernelversion
,
1091 struct module_search
**search
,
1092 struct module_overrides
**overrides
);
1094 static int read_config_file(const char *filename
,
1095 const char *basedir
,
1096 const char *kernelversion
,
1097 struct module_search
**search
,
1098 struct module_overrides
**overrides
)
1101 unsigned int linenum
= 0;
1104 cfile
= fopen(filename
, "r");
1106 if (errno
!= ENOENT
)
1107 fatal("could not open '%s', reason: %s\n", filename
,
1112 while ((line
= getline_wrapped(cfile
, &linenum
)) != NULL
) {
1114 char *cmd
, *modname
;
1116 cmd
= strsep_skipspace(&ptr
, "\t ");
1118 if (cmd
== NULL
|| cmd
[0] == '#' || cmd
[0] == '\0') {
1123 if (strcmp(cmd
, "search") == 0) {
1126 while ((search_path
= strsep_skipspace(&ptr
, "\t "))) {
1130 if (strcmp(search_path
,
1131 MODULE_BUILTIN_KEY
) == 0) {
1132 *search
= add_search(MODULE_BUILTIN_KEY
,
1136 len
= strlen(basedir
)
1137 + strlen(MODULE_DIR
)
1138 + strlen(kernelversion
)
1140 + strlen(search_path
);
1141 dirname
= NOFAIL(malloc(len
+ 1));
1142 sprintf(dirname
, "%s%s%s/%s", basedir
,
1143 MODULE_DIR
, kernelversion
, search_path
);
1144 *search
= add_search(dirname
, len
, *search
);
1147 } else if (strcmp(cmd
, "override") == 0) {
1148 char *pathname
= NULL
, *version
, *subdir
;
1149 modname
= strsep_skipspace(&ptr
, "\t ");
1150 version
= strsep_skipspace(&ptr
, "\t ");
1151 subdir
= strsep_skipspace(&ptr
, "\t ");
1153 if (strcmp(version
, kernelversion
) != 0 &&
1154 strcmp(version
, "*") != 0)
1157 pathname
= NOFAIL(malloc(strlen(basedir
)
1158 + strlen(MODULE_DIR
)
1159 + strlen(kernelversion
)
1164 sprintf(pathname
, "%s%s%s/%s/%s.ko", basedir
,
1165 MODULE_DIR
, kernelversion
, subdir
, modname
);
1167 *overrides
= add_override(pathname
, *overrides
);
1169 } else if (strcmp(cmd
, "include") == 0) {
1172 newfilename
= strsep_skipspace(&ptr
, "\t ");
1174 grammar(cmd
, filename
, linenum
);
1176 if (!read_config(newfilename
, basedir
,
1179 warn("Failed to open included"
1180 " config file %s: %s\n",
1181 newfilename
, strerror(errno
));
1184 } else if (strcmp(cmd
, "make_map_files") == 0) {
1187 option
= strsep_skipspace(&ptr
, "\t ");
1189 grammar(cmd
, filename
, linenum
);
1191 if (0 == strncmp(option
, "yes", 3))
1193 else if (0 == strncmp(option
, "no", 2))
1196 grammar(cmd
, filename
, linenum
);
1199 grammar(cmd
, filename
, linenum
);
1207 /* Simple format, ignore lines starting with #, one command per line.
1208 Returns true or false. */
1209 static int read_config(const char *filename
,
1210 const char *basedir
,
1211 const char *kernelversion
,
1212 struct module_search
**search
,
1213 struct module_overrides
**overrides
)
1218 dir
= opendir(filename
);
1221 while ((i
= readdir(dir
)) != NULL
) {
1222 if (!streq(i
->d_name
,".") && !streq(i
->d_name
,"..")
1223 && config_filter(i
->d_name
)) {
1224 char sub
[strlen(filename
) + 1
1225 + strlen(i
->d_name
) + 1];
1227 sprintf(sub
, "%s/%s", filename
, i
->d_name
);
1228 if (!read_config(sub
, basedir
, kernelversion
,
1230 warn("Failed to open"
1231 " config file %s: %s\n",
1232 sub
, strerror(errno
));
1238 if (read_config_file(filename
, basedir
, kernelversion
, search
,
1246 static const char *default_configs
[] =
1252 static void read_toplevel_config(const char *filename
,
1253 const char *basedir
,
1254 const char *kernelversion
,
1255 struct module_search
**search
,
1256 struct module_overrides
**overrides
)
1261 if (!read_config(filename
, basedir
, kernelversion
, search
,
1263 fatal("Failed to open config file %s: %s\n",
1264 filename
, strerror(errno
));
1269 for (i
= 0; i
< ARRAY_SIZE(default_configs
); i
++) {
1270 read_config(default_configs
[i
], basedir
, kernelversion
,
1275 /* Local to main, but not freed on exit. Keep valgrind quiet. */
1276 struct module
*list
= NULL
;
1277 struct module_search
*search
= NULL
;
1278 struct module_overrides
*overrides
= NULL
;
1280 int main(int argc
, char *argv
[])
1282 int opt
, all
= 0, maybe_all
= 0, doing_stdout
= 0;
1283 char *basedir
= "", *dirname
, *version
, *badopt
= NULL
,
1286 const char *config
= NULL
;
1288 /* Don't print out any errors just yet, we might want to exec
1289 backwards compat version. */
1291 while ((opt
= getopt_long(argc
, argv
, "ab:ArehnqruvVF:C:wm", options
, NULL
))
1299 skipchars
= strlen(basedir
);
1305 system_map
= optarg
;
1321 print_usage(argv
[0]);
1328 printf("%s %s\n", PACKAGE
, VERSION
);
1334 force_map_files
= 1;
1337 badopt
= argv
[optind
-1];
1341 /* We can't print unknowns without a System.map */
1345 load_system_map(system_map
);
1347 /* They can specify the version naked on the command line */
1348 if (optind
< argc
&& is_version_number(argv
[optind
])) {
1349 version
= NOFAIL(strdup(argv
[optind
]));
1354 version
= NOFAIL(strdup(buf
.release
));
1357 /* Run old version if required. */
1358 if (old_module_version(version
))
1359 exec_old_depmod(argv
);
1362 fprintf(stderr
, "%s: malformed/unrecognized option '%s'\n",
1364 print_usage(argv
[0]);
1368 /* Depmod -a by default if no names. */
1372 dirname
= NOFAIL(malloc(strlen(basedir
)
1373 + strlen(MODULE_DIR
)
1374 + strlen(version
) + 1));
1375 sprintf(dirname
, "%s%s%s", basedir
, MODULE_DIR
, version
);
1378 if (!doing_stdout
&& !depfile_out_of_date(dirname
))
1383 read_toplevel_config(config
, basedir
, version
, &search
, &overrides
);
1385 /* For backward compatibility add "updates" to the head of the search
1386 * list here. But only if there was no "search" option specified.
1390 search
= add_search("updates", strlen("updates"), search
);
1393 /* Do command line args. */
1394 for (opt
= optind
; opt
< argc
; opt
++) {
1395 struct module
*new = grab_module(NULL
, argv
[opt
]);
1397 /* cmd-line specified modules must exist */
1398 fatal("grab_module() failed for module %s\n", argv
[opt
]);
1404 list
= grab_basedir(dirname
,search
,overrides
);
1406 list
= sort_modules(dirname
,list
);
1407 list
= parse_modules(list
);
1409 for (i
= 0; i
< sizeof(depfiles
)/sizeof(depfiles
[0]); i
++) {
1411 struct depfile
*d
= &depfiles
[i
];
1412 char depname
[strlen(dirname
) + 1 + strlen(d
->name
) + 1];
1413 char tmpname
[strlen(dirname
) + 1 + strlen(d
->name
) +
1414 strlen(".temp") + 1];
1416 if (d
->map_file
&& !make_map_files
&& !force_map_files
)
1419 sprintf(depname
, "%s/%s", dirname
, d
->name
);
1420 sprintf(tmpname
, "%s/%s.temp", dirname
, d
->name
);
1421 if (!doing_stdout
) {
1422 out
= fopen(tmpname
, "w");
1424 fatal("Could not open %s for writing: %s\n",
1425 tmpname
, strerror(errno
));
1428 if (ends_in(depname
, ".bin"))
1431 d
->func(list
, out
, dirname
);
1432 if (!doing_stdout
) {
1434 if (rename(tmpname
, depname
) < 0)
1435 fatal("Could not rename %s into %s: %s\n",
1436 tmpname
, depname
, strerror(errno
));