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
;
61 static unsigned int skipchars
;
62 static unsigned int make_map_files
= 1; /* default to on */
63 static unsigned int force_map_files
= 0; /* default to on */
65 #define SYMBOL_HASH_SIZE 1024
73 static struct symbol
*symbolhash
[SYMBOL_HASH_SIZE
];
75 /* This is based on the hash agorithm from gdbm, via tdb */
76 static inline unsigned int tdb_hash(const char *name
)
78 unsigned value
; /* Used to compute the hash value. */
79 unsigned i
; /* Used to cycle through random values. */
81 /* Set the initial value from the key size. */
82 for (value
= 0x238F13AF * strlen(name
), i
=0; name
[i
]; i
++)
83 value
= (value
+ (((unsigned char *)name
)[i
] << (i
*5 % 24)));
85 return (1103515243 * value
+ 12345);
88 void add_symbol(const char *name
, struct module
*owner
)
91 struct symbol
*new = NOFAIL(malloc(sizeof *new + strlen(name
) + 1));
94 strcpy(new->name
, name
);
96 hash
= tdb_hash(name
) % SYMBOL_HASH_SIZE
;
97 new->next
= symbolhash
[hash
];
98 symbolhash
[hash
] = new;
101 static int print_unknown
;
103 struct module
*find_symbol(const char *name
, const char *modname
, int weak
)
107 /* For our purposes, .foo matches foo. PPC64 needs this. */
111 for (s
= symbolhash
[tdb_hash(name
) % SYMBOL_HASH_SIZE
]; s
; s
=s
->next
) {
112 if (streq(s
->name
, name
))
116 if (print_unknown
&& !weak
)
117 warn("%s needs unknown symbol %s\n", modname
, name
);
122 void add_dep(struct module
*mod
, struct module
*depends_on
)
126 for (i
= 0; i
< mod
->num_deps
; i
++)
127 if (mod
->deps
[i
] == depends_on
)
130 mod
->deps
= NOFAIL(realloc(mod
->deps
, sizeof(mod
->deps
[0])*(mod
->num_deps
+1)));
131 mod
->deps
[mod
->num_deps
++] = depends_on
;
134 static void load_system_map(const char *filename
)
138 const char ksymstr
[] = "__ksymtab_";
139 const int ksymstr_len
= strlen(ksymstr
);
141 system_map
= fopen(filename
, "r");
143 fatal("Could not open '%s': %s\n", filename
, strerror(errno
));
145 /* eg. c0294200 R __ksymtab_devfs_alloc_devnum */
146 while (fgets(line
, sizeof(line
)-1, system_map
)) {
150 ptr
= strchr(line
, '\n');
154 ptr
= strchr(line
, ' ');
155 if (!ptr
|| !(ptr
= strchr(ptr
+ 1, ' ')))
158 /* Covers gpl-only and normal symbols. */
159 if (strncmp(ptr
+1, ksymstr
, ksymstr_len
) == 0)
160 add_symbol(ptr
+1+ksymstr_len
, NULL
);
165 /* __this_module is magic inserted by kernel loader. */
166 add_symbol("__this_module", NULL
);
167 /* On S390, this is faked up too */
168 add_symbol("_GLOBAL_OFFSET_TABLE_", NULL
);
171 static struct option options
[] = { { "all", 0, NULL
, 'a' },
172 { "quick", 0, NULL
, 'A' },
173 { "basedir", 1, NULL
, 'b' },
174 { "errsyms", 0, NULL
, 'e' },
175 { "filesyms", 1, NULL
, 'F' },
176 { "help", 0, NULL
, 'h' },
177 { "show", 0, NULL
, 'n' },
178 { "dry-run", 0, NULL
, 'n' },
179 { "quiet", 0, NULL
, 'q' },
180 { "root", 0, NULL
, 'r' },
181 { "unresolved-error", 0, NULL
, 'u' },
182 { "verbose", 0, NULL
, 'v' },
183 { "version", 0, NULL
, 'V' },
184 { "config", 1, NULL
, 'C' },
185 { "warn", 1, NULL
, 'w' },
186 { "map", 0, NULL
, 'm' },
187 { NULL
, 0, NULL
, 0 } };
189 /* Version number or module name? Don't assume extension. */
190 static int is_version_number(const char *version
)
194 return (sscanf(version
, "%u.%u.%u", &dummy
, &dummy
, &dummy
) == 3);
197 static int old_module_version(const char *version
)
199 /* Expect three part version. */
200 unsigned int major
, sub
, minor
;
202 sscanf(version
, "%u.%u.%u", &major
, &sub
, &minor
);
204 if (major
> 2) return 0;
205 if (major
< 2) return 1;
208 if (sub
> 5) return 0;
209 if (sub
< 5) return 1;
212 if (minor
>= 48) return 0;
216 static void exec_old_depmod(char *argv
[])
219 char pathname
[strlen(argv
[0])+1];
220 char oldname
[strlen("depmod") + strlen(argv
[0]) + sizeof(".old")];
222 memset(pathname
, 0, strlen(argv
[0])+1);
223 sep
= strrchr(argv
[0], '/');
225 memcpy(pathname
, argv
[0], sep
- argv
[0]+1);
226 sprintf(oldname
, "%s%s.old", pathname
, "depmod");
228 /* Recursion detection: we need an env var since we can't
229 change argv[0] (as older modutils uses it to determine
231 if (getenv("MODULE_RECURSE"))
233 setenv("MODULE_RECURSE", "y", 0);
235 execvp(oldname
, argv
);
237 "Version requires old depmod, but couldn't run %s: %s\n",
238 oldname
, strerror(errno
));
242 static void grammar(const char *cmd
, const char *filename
, unsigned int line
)
244 warn("%s line %u: ignoring bad line starting with '%s'\n",
245 filename
, line
, cmd
);
249 static void print_usage(const char *name
)
252 "%s " VERSION
" -- part of " PACKAGE
"\n"
253 "%s -[aA] [-n -e -v -q -V -r -u -w -m]\n"
254 " [-b basedirectory] [forced_version]\n"
255 "depmod [-n -e -v -q -r -u -w] [-F kernelsyms] module1.ko module2.ko ...\n"
256 "If no arguments (except options) are given, \"depmod -a\" is assumed\n"
258 "depmod will output a dependancy list suitable for the modprobe utility.\n"
262 "\t-a, --all Probe all modules\n"
263 "\t-A, --quick Only does the work if there's a new module\n"
264 "\t-e, --errsyms Report not supplied symbols\n"
265 "\t-m, --map Create the legacy map files\n"
266 "\t-n, --show Write the dependency file on stdout only\n"
267 "\t-V, --version Print the release version\n"
268 "\t-v, --verbose Enable verbose mode\n"
269 "\t-w, --warn Warn on duplicates\n"
270 "\t-h, --help Print this usage message\n"
272 "The following options are useful for people managing distributions:\n"
273 "\t-b basedirectory\n"
274 "\t --basedir basedirectory Use an image of a module tree.\n"
276 "\t --filesyms kernelsyms Use the file instead of the\n"
277 "\t current kernel symbols.\n",
281 static int ends_in(const char *name
, const char *ext
)
283 unsigned int namelen
, extlen
;
286 namelen
= strlen(name
);
287 extlen
= strlen(ext
);
289 if (namelen
< extlen
) return 0;
291 if (streq(name
+ namelen
- extlen
, ext
))
296 /* "\177ELF" <byte> where byte = 001 for 32-bit, 002 for 64 */
297 int needconv(const char *elfhdr
)
299 union { short s
; char c
[2]; } endian_test
;
302 if (endian_test
.c
[1] == 1) return elfhdr
[EI_DATA
] != ELFDATA2MSB
;
303 if (endian_test
.c
[0] == 1) return elfhdr
[EI_DATA
] != ELFDATA2LSB
;
308 static char *my_basename(const char *name
)
310 const char *base
= strrchr(name
, '/');
311 if (base
) return (char *)base
+ 1;
315 static struct module
*grab_module(const char *dirname
, const char *filename
)
319 new = NOFAIL(malloc(sizeof(*new)
320 + strlen(dirname
?:"") + 1 + strlen(filename
) + 1));
322 sprintf(new->pathname
, "%s/%s", dirname
, filename
);
324 strcpy(new->pathname
, filename
);
325 new->basename
= my_basename(new->pathname
);
327 INIT_LIST_HEAD(&new->dep_list
);
329 new->data
= grab_file(new->pathname
, &new->len
);
331 warn("Can't read module %s: %s\n",
332 new->pathname
, strerror(errno
));
336 /* "\177ELF" <byte> where byte = 001 for 32-bit, 002 for 64 */
337 if (memcmp(new->data
, ELFMAG
, SELFMAG
) != 0) {
338 warn("Module %s is not an elf object\n", new->pathname
);
342 switch (((char *)new->data
)[EI_CLASS
]) {
344 new->ops
= &mod_ops32
;
347 new->ops
= &mod_ops64
;
350 warn("Module %s has elf unknown identifier %i\n",
351 new->pathname
, ((char *)new->data
)[EI_CLASS
]);
354 new->conv
= needconv(new->data
);
358 release_file(new->data
, new->len
);
364 struct module_traverse
366 struct module_traverse
*prev
;
370 static int in_loop(struct module
*mod
, const struct module_traverse
*traverse
)
372 const struct module_traverse
*i
;
374 for (i
= traverse
; i
; i
= i
->prev
) {
381 /* Assume we are doing all the modules, so only report each loop once. */
382 static void report_loop(const struct module
*mod
,
383 const struct module_traverse
*traverse
)
385 const struct module_traverse
*i
;
387 /* Check that start is least alphabetically. eg. a depends
388 on b depends on a will get reported for a, not b. */
389 for (i
= traverse
->prev
; i
->prev
; i
= i
->prev
) {
390 if (strcmp(mod
->pathname
, i
->mod
->pathname
) > 0)
394 /* Is start in the loop? If not, don't report now. eg. a
395 depends on b which depends on c which depends on b. Don't
396 report when generating depends for a. */
400 warn("Loop detected: %s ", mod
->pathname
);
401 for (i
= traverse
->prev
; i
->prev
; i
= i
->prev
)
402 fprintf(stderr
, "needs %s ", i
->mod
->basename
);
403 fprintf(stderr
, "which needs %s again!\n", i
->mod
->basename
);
406 /* This is damn slow, but loops actually happen, and we don't want to
407 just exit() and leave the user without any modules. */
408 static int has_dep_loop(struct module
*module
, struct module_traverse
*prev
)
411 struct module_traverse traverse
= { .prev
= prev
, .mod
= module
};
413 if (in_loop(module
, prev
)) {
414 report_loop(module
, &traverse
);
418 for (i
= 0; i
< module
->num_deps
; i
++)
419 if (has_dep_loop(module
->deps
[i
], &traverse
))
424 /* Uniquifies and orders a dependency list. */
425 static void order_dep_list(struct module
*start
, struct module
*mod
)
429 for (i
= 0; i
< mod
->num_deps
; i
++) {
430 /* If it was previously depended on, move it to the
431 tail. ie. if a needs b and c, and c needs b, we
432 must order b after c. */
433 list_del(&mod
->deps
[i
]->dep_list
);
434 list_add_tail(&mod
->deps
[i
]->dep_list
, &start
->dep_list
);
435 order_dep_list(start
, mod
->deps
[i
]);
439 static struct module
*deleted
= NULL
;
441 static void del_module(struct module
**modules
, struct module
*delme
)
445 /* Find pointer to it. */
447 for (i
= modules
; *i
!= delme
; i
= &(*i
)->next
);
452 /* Save on a list to quiet valgrind.
453 Can't free - other modules may depend on them */
454 delme
->next
= deleted
;
458 /* Convert filename to the module name. Works if filename == modname, too. */
459 static void filename2modname(char *modname
, const char *filename
)
461 const char *afterslash
;
464 afterslash
= strrchr(filename
, '/');
466 afterslash
= filename
;
470 /* Convert to underscores, stop at first . */
471 for (i
= 0; afterslash
[i
] && afterslash
[i
] != '.'; i
++) {
472 if (afterslash
[i
] == '-')
475 modname
[i
] = afterslash
[i
];
480 static void output_deps(struct module
*modules
,
481 FILE *out
, char *dirname
)
485 for (i
= modules
; i
; i
= i
->next
) {
486 struct list_head
*j
, *tmp
;
487 order_dep_list(i
, i
);
489 fprintf(out
, "%s:", i
->pathname
+ strlen(dirname
)+1);
490 list_for_each_safe(j
, tmp
, &i
->dep_list
) {
492 = list_entry(j
, struct module
, dep_list
);
493 fprintf(out
, " %s", dep
->pathname
+ strlen(dirname
)+1);
500 /* warn whenever duplicate module aliases, deps, or symbols are found. */
503 static void output_deps_bin(struct module
*modules
,
504 FILE *out
, char *dirname
)
507 struct index_node
*index
;
511 index
= index_create();
513 for (i
= modules
; i
; i
= i
->next
) {
514 struct list_head
*j
, *tmp
;
515 char modname
[strlen(i
->pathname
)+1];
517 order_dep_list(i
, i
);
519 filename2modname(modname
, i
->pathname
+ strlen(dirname
)+1);
520 nofail_asprintf(&line
, "%s %s:", modname
, i
->pathname
+ strlen(dirname
)+1);
522 list_for_each_safe(j
, tmp
, &i
->dep_list
) {
524 = list_entry(j
, struct module
, dep_list
);
525 nofail_asprintf(&line
, "%s %s", p
, dep
->pathname
+ strlen(dirname
)+1);
530 if (index_insert(index
, line
) && warn_dups
)
531 warn("duplicate module deps:\n%s\n",line
);
535 index_write(index
, out
);
536 index_destroy(index
);
540 static int smells_like_module(const char *name
)
542 return ends_in(name
,".ko") || ends_in(name
, ".ko.gz");
545 typedef struct module
*(*do_module_t
)(const char *dirname
,
546 const char *filename
,
548 struct module_search
*search
,
549 struct module_overrides
*overrides
);
551 static int is_higher_priority(const char *newpath
, const char *oldpath
,
552 struct module_search
*search
,
553 struct module_overrides
*overrides
)
556 struct module_search
*tmp
;
557 struct module_overrides
*ovtmp
;
559 int prio_builtin
= -1;
563 /* The names already match, now we check for wildcard overrides and other
564 * high priority overrides we added to the config.
566 for (ovtmp
=overrides
;ovtmp
!=NULL
;ovtmp
=ovtmp
->next
) {
568 p
= strstr(ovtmp
->modfile
,newpath
);
572 q
= strstr(ovtmp
->modfile
,"*");
574 p
= strstr(newpath
, q
+1);
578 p
= strstr(oldpath
, q
+1);
583 p
= strstr(ovtmp
->modfile
,oldpath
);
588 for (i
=0,tmp
=search
;tmp
!=NULL
;tmp
=tmp
->next
,i
++) {
590 s
= NOFAIL(malloc(strlen(tmp
->search_path
)+2));
592 strncpy(s
+1,tmp
->search_path
, strlen(tmp
->search_path
)+1);
594 if (0 == strncmp(tmp
->search_path
,MODULE_BUILTIN_KEY
,
595 strlen(MODULE_BUILTIN_KEY
)))
598 p
= strstr(newpath
,s
);
599 if ((p
) && ((p
[strlen(s
)] == '/')
600 || (p
[strlen(s
)] == '\0')))
603 p
= strstr(oldpath
,s
);
604 if ((p
) && ((p
[strlen(s
)] == '/')
605 || (p
[strlen(s
)] == '\0')))
613 prio_new
= prio_builtin
;
616 prio_old
= prio_builtin
;
618 return prio_new
> prio_old
;
623 static struct module
*do_module(const char *dirname
,
624 const char *filename
,
626 struct module_search
*search
,
627 struct module_overrides
*overrides
)
629 struct module
*new, **i
;
631 new = grab_module(dirname
, filename
);
635 /* Check if module is already in the list. */
636 for (i
= &list
; *i
; i
= &(*i
)->next
) {
638 if (streq((*i
)->basename
, filename
)) {
639 char newpath
[strlen(dirname
) + strlen("/")
640 + strlen(filename
) + 1];
642 sprintf(newpath
, "%s/%s", dirname
, filename
);
644 if (is_higher_priority(newpath
, (*i
)->pathname
,search
,
651 del_module(NULL
, new);
657 /* Not in the list already. Just prepend. */
662 static struct module
*grab_dir(const char *dirname
,
666 struct module_search
*search
,
667 struct module_overrides
*overrides
)
669 struct dirent
*dirent
;
671 while ((dirent
= readdir(dir
)) != NULL
) {
672 if (smells_like_module(dirent
->d_name
))
673 next
= do_mod(dirname
, dirent
->d_name
, next
,
675 else if (!streq(dirent
->d_name
, ".")
676 && !streq(dirent
->d_name
, "..")
677 && !streq(dirent
->d_name
, "source")
678 && !streq(dirent
->d_name
, "build")) {
681 char subdir
[strlen(dirname
) + 1
682 + strlen(dirent
->d_name
) + 1];
683 sprintf(subdir
, "%s/%s", dirname
, dirent
->d_name
);
684 sub
= opendir(subdir
);
686 next
= grab_dir(subdir
, sub
, next
, do_mod
,
695 static struct module
*grab_basedir(const char *dirname
,
696 struct module_search
*search
,
697 struct module_overrides
*overrides
)
702 dir
= opendir(dirname
);
704 warn("Couldn't open directory %s: %s\n",
705 dirname
, strerror(errno
));
708 list
= grab_dir(dirname
, dir
, NULL
, do_module
, search
, overrides
);
714 static struct module
*sort_modules(const char *dirname
, struct module
*list
)
716 struct module
*tlist
= NULL
, **tpos
= &tlist
;
718 int dir_len
= strlen(dirname
) + 1;
719 char file_name
[dir_len
+ strlen("modules.order") + 1];
722 sprintf(file_name
, "%s/%s", dirname
, "modules.order");
724 modorder
= fopen(file_name
, "r");
726 /* Older kernels don't generate modules.order. Just
727 return if the file doesn't exist. */
730 fatal("Could not open '%s': %s\n", file_name
, strerror(errno
));
733 sprintf(line
, "%s/", dirname
);
735 /* move modules listed in modorder file to tlist in order */
736 while (fgets(line
, sizeof(line
), modorder
)) {
737 struct module
**pos
, *mod
;
738 int len
= strlen(line
);
740 if (line
[len
- 1] == '\n')
741 line
[len
- 1] = '\0';
743 for (pos
= &list
; (mod
= *pos
); pos
= &(*pos
)->next
) {
744 if (strcmp(line
, mod
->pathname
+ dir_len
) == 0) {
754 /* append the rest */
762 static struct module
*parse_modules(struct module
*list
)
766 for (i
= list
; i
; i
= i
->next
) {
767 i
->ops
->load_symbols(i
);
768 i
->ops
->fetch_tables(i
);
771 for (i
= list
; i
; i
= i
->next
)
772 i
->ops
->calculate_deps(i
, verbose
);
774 /* Strip out modules with dependency loops. */
776 for (i
= list
; i
; i
= i
->next
) {
777 if (has_dep_loop(i
, NULL
)) {
778 warn("Module %s ignored, due to loop\n",
779 i
->pathname
+ skipchars
);
780 del_module(&list
, i
);
788 /* Simply dump hash table. */
789 static void output_symbols(struct module
*unused
, FILE *out
, char *dirname
)
793 fprintf(out
, "# Aliases for symbols, used by symbol_request().\n");
794 for (i
= 0; i
< SYMBOL_HASH_SIZE
; i
++) {
797 for (s
= symbolhash
[i
]; s
; s
= s
->next
) {
799 char modname
[strlen(s
->owner
->pathname
)+1];
800 filename2modname(modname
, s
->owner
->pathname
);
801 fprintf(out
, "alias symbol:%s %s\n",
808 static void output_symbols_bin(struct module
*unused
, FILE *out
, char *dirname
)
810 struct index_node
*index
;
814 index
= index_create();
816 for (i
= 0; i
< SYMBOL_HASH_SIZE
; i
++) {
819 for (s
= symbolhash
[i
]; s
; s
= s
->next
) {
821 char modname
[strlen(s
->owner
->pathname
)+1];
822 filename2modname(modname
, s
->owner
->pathname
);
823 nofail_asprintf(&line
, "symbol:%s %s",
825 if (index_insert(index
, line
) && warn_dups
)
826 warn("duplicate module syms:\n%s\n",
833 index_write(index
, out
);
834 index_destroy(index
);
837 static const char *next_string(const char *string
, unsigned long *secsize
)
839 /* Skip non-zero chars */
842 if ((*secsize
)-- <= 1)
846 /* Skip any zero padding. */
849 if ((*secsize
)-- <= 1)
855 /* Careful! Don't munge - in [ ] as per Debian Bug#350915 */
856 static char *underscores(char *string
)
863 for (i
= 0; string
[i
]; i
++) {
870 i
+= strcspn(&string
[i
], "]");
872 warn("Unmatched bracket in %s\n", string
);
878 static void output_aliases(struct module
*modules
, FILE *out
, char *dirname
)
884 fprintf(out
, "# Aliases extracted from modules themselves.\n");
885 for (i
= modules
; i
; i
= i
->next
) {
886 char modname
[strlen(i
->pathname
)+1];
888 filename2modname(modname
, i
->pathname
);
890 /* Grab from old-style .modalias section. */
891 for (p
= i
->ops
->get_aliases(i
, &size
);
893 p
= next_string(p
, &size
))
894 fprintf(out
, "alias %s %s\n", p
, modname
);
896 /* Grab form new-style .modinfo section. */
897 for (p
= i
->ops
->get_modinfo(i
, &size
);
899 p
= next_string(p
, &size
)) {
900 if (strncmp(p
, "alias=", strlen("alias=")) == 0)
901 fprintf(out
, "alias %s %s\n",
902 p
+ strlen("alias="), modname
);
907 static void output_aliases_bin(struct module
*modules
, FILE *out
, char *dirname
)
913 struct index_node
*index
;
915 index
= index_create();
917 for (i
= modules
; i
; i
= i
->next
) {
918 char modname
[strlen(i
->pathname
)+1];
920 filename2modname(modname
, i
->pathname
);
922 /* Grab from old-style .modalias section. */
923 for (p
= i
->ops
->get_aliases(i
, &size
);
925 p
= next_string(p
, &size
)) {
926 nofail_asprintf(&line
, "%s %s", p
, modname
);
928 if (index_insert(index
, line
) && warn_dups
)
929 warn("duplicate module alias:\n%s\n", line
);
933 /* Grab from new-style .modinfo section. */
934 for (p
= i
->ops
->get_modinfo(i
, &size
);
936 p
= next_string(p
, &size
)) {
937 if (strncmp(p
, "alias=", strlen("alias=")) == 0) {
938 nofail_asprintf(&line
, "%s %s",
939 p
+ strlen("alias="), modname
);
941 if (index_insert(index
, line
) && warn_dups
)
942 warn("duplicate module alias:\n%s\n",
949 index_write(index
, out
);
950 index_destroy(index
);
955 void (*func
)(struct module
*, FILE *, char *dirname
);
959 static struct depfile depfiles
[] = {
960 { "modules.dep", output_deps
, 0 }, /* This is what we check for '-A'. */
961 { "modules.dep.bin", output_deps_bin
, 0 },
962 { "modules.pcimap", output_pci_table
, 1 },
963 { "modules.usbmap", output_usb_table
, 1 },
964 { "modules.ccwmap", output_ccw_table
, 1 },
965 { "modules.ieee1394map", output_ieee1394_table
, 1 },
966 { "modules.isapnpmap", output_isapnp_table
, 1 },
967 { "modules.inputmap", output_input_table
, 1 },
968 { "modules.ofmap", output_of_table
, 1 },
969 { "modules.seriomap", output_serio_table
, 1 },
970 { "modules.alias", output_aliases
, 0 },
971 { "modules.alias.bin", output_aliases_bin
, 0 },
972 { "modules.symbols", output_symbols
, 0 },
973 { "modules.symbols.bin", output_symbols_bin
, 0 }
976 /* If we can't figure it out, it's safe to say "true". */
977 static int any_modules_newer(const char *dirname
, time_t mtime
)
980 struct dirent
*dirent
;
982 dir
= opendir(dirname
);
986 while ((dirent
= readdir(dir
)) != NULL
) {
988 char file
[strlen(dirname
) + 1 + strlen(dirent
->d_name
) + 1];
990 if (streq(dirent
->d_name
, ".") || streq(dirent
->d_name
, ".."))
993 sprintf(file
, "%s/%s", dirname
, dirent
->d_name
);
994 if (lstat(file
, &st
) != 0)
997 if (smells_like_module(dirent
->d_name
)) {
998 if (st
.st_mtime
> mtime
)
1000 } else if (S_ISDIR(st
.st_mode
)) {
1001 if (any_modules_newer(file
, mtime
))
1013 static int depfile_out_of_date(const char *dirname
)
1016 char depfile
[strlen(dirname
) + 1 + strlen(depfiles
[0].name
) + 1];
1018 sprintf(depfile
, "%s/%s", dirname
, depfiles
[0].name
);
1020 if (stat(depfile
, &st
) != 0)
1023 return any_modules_newer(dirname
, st
.st_mtime
);
1026 static char *getline_wrapped(FILE *file
, unsigned int *linenum
)
1030 char *buf
= NOFAIL(malloc(size
));
1032 int ch
= getc_unlocked(file
);
1040 /* else fall through */
1046 buf
= NOFAIL(realloc(buf
, size
+ 1));
1051 ch
= getc_unlocked(file
);
1058 /* else fall through */
1065 buf
= NOFAIL(realloc(buf
, size
));
1072 static char *strsep_skipspace(char **string
, char *delim
)
1076 *string
+= strspn(*string
, delim
);
1077 return strsep(string
, delim
);
1080 static struct module_search
*add_search(const char *search_path
,
1081 struct module_search
*search
)
1084 struct module_search
*new;
1086 new = NOFAIL(malloc(sizeof(*new)));
1087 new->search_path
= NOFAIL(strdup(search_path
));
1094 static struct module_overrides
*add_override(const char *modfile
,
1095 struct module_overrides
*overrides
)
1098 struct module_overrides
*new;
1100 new = NOFAIL(malloc(sizeof(*new)));
1101 new->modfile
= NOFAIL(strdup(modfile
));
1102 new->next
= overrides
;
1109 static int read_config(const char *filename
,
1110 const char *basedir
,
1111 struct module_search
**search
,
1112 struct module_overrides
**overrides
);
1114 static int read_config_file(const char *filename
,
1115 const char *basedir
,
1116 struct module_search
**search
,
1117 struct module_overrides
**overrides
)
1120 unsigned int linenum
= 0;
1123 cfile
= fopen(filename
, "r");
1125 if (errno
!= ENOENT
)
1126 fatal("could not open '%s', reason: %s\n", filename
,
1131 while ((line
= getline_wrapped(cfile
, &linenum
)) != NULL
) {
1133 char *cmd
, *modname
;
1135 cmd
= strsep_skipspace(&ptr
, "\t ");
1137 if (cmd
== NULL
|| cmd
[0] == '#' || cmd
[0] == '\0') {
1142 if (strcmp(cmd
, "search") == 0) {
1145 while ((search_path
= strsep_skipspace(&ptr
, "\t ")))
1146 *search
= add_search(search_path
, *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 pathname
= NOFAIL(malloc(strlen(basedir
)
1154 + strlen(MODULE_DIR
)
1160 sprintf(pathname
, "%s%s%s/%s/%s.ko", basedir
,
1161 MODULE_DIR
, version
, subdir
, modname
);
1163 *overrides
= add_override(pathname
, *overrides
);
1164 } else if (strcmp(cmd
, "include") == 0) {
1167 newfilename
= strsep_skipspace(&ptr
, "\t ");
1169 grammar(cmd
, filename
, linenum
);
1171 if (!read_config(newfilename
, basedir
,
1173 warn("Failed to open included"
1174 " config file %s: %s\n",
1175 newfilename
, strerror(errno
));
1178 } else if (strcmp(cmd
, "make_map_files") == 0) {
1181 option
= strsep_skipspace(&ptr
, "\t ");
1183 grammar(cmd
, filename
, linenum
);
1185 if (0 == strncmp(option
, "yes", 3))
1187 else if (0 == strncmp(option
, "no", 2))
1190 grammar(cmd
, filename
, linenum
);
1193 grammar(cmd
, filename
, linenum
);
1201 /* Simple format, ignore lines starting with #, one command per line.
1202 Returns true or false. */
1203 static int read_config(const char *filename
,
1204 const char *basedir
,
1205 struct module_search
**search
,
1206 struct module_overrides
**overrides
)
1211 dir
= opendir(filename
);
1214 while ((i
= readdir(dir
)) != NULL
) {
1215 if (!streq(i
->d_name
,".") && !streq(i
->d_name
,"..")
1216 && config_filter(i
->d_name
)) {
1217 char sub
[strlen(filename
) + 1
1218 + strlen(i
->d_name
) + 1];
1220 sprintf(sub
, "%s/%s", filename
, i
->d_name
);
1221 if (!read_config(sub
, basedir
, search
,
1223 warn("Failed to open"
1224 " config file %s: %s\n",
1225 sub
, strerror(errno
));
1231 if (read_config_file(filename
, basedir
, search
, overrides
))
1238 static const char *default_configs
[] =
1244 static void read_toplevel_config(const char *filename
,
1245 const char *basedir
,
1246 struct module_search
**search
,
1247 struct module_overrides
**overrides
)
1252 if (!read_config(filename
, basedir
, search
, overrides
))
1253 fatal("Failed to open config file %s: %s\n",
1254 filename
, strerror(errno
));
1259 for (i
= 0; i
< ARRAY_SIZE(default_configs
); i
++) {
1260 read_config(default_configs
[i
], basedir
, search
, overrides
);
1264 /* Local to main, but not freed on exit. Keep valgrind quiet. */
1265 struct module
*list
= NULL
;
1266 struct module_search
*search
= NULL
;
1267 struct module_overrides
*overrides
= NULL
;
1269 int main(int argc
, char *argv
[])
1271 int opt
, all
= 0, maybe_all
= 0, doing_stdout
= 0;
1272 char *basedir
= "", *dirname
, *version
, *badopt
= NULL
,
1275 const char *config
= NULL
;
1277 /* Don't print out any errors just yet, we might want to exec
1278 backwards compat version. */
1280 while ((opt
= getopt_long(argc
, argv
, "ab:ArehnqruvVF:C:wm", options
, NULL
))
1288 skipchars
= strlen(basedir
);
1294 system_map
= optarg
;
1310 print_usage(argv
[0]);
1317 printf("%s %s\n", PACKAGE
, VERSION
);
1323 force_map_files
= 1;
1326 badopt
= argv
[optind
-1];
1330 /* We can't print unknowns without a System.map */
1334 load_system_map(system_map
);
1336 /* They can specify the version naked on the command line */
1337 if (optind
< argc
&& is_version_number(argv
[optind
])) {
1338 version
= NOFAIL(strdup(argv
[optind
]));
1343 version
= NOFAIL(strdup(buf
.release
));
1346 /* Run old version if required. */
1347 if (old_module_version(version
))
1348 exec_old_depmod(argv
);
1351 fprintf(stderr
, "%s: malformed/unrecognized option '%s'\n",
1353 print_usage(argv
[0]);
1357 /* Depmod -a by default if no names. */
1361 dirname
= NOFAIL(malloc(strlen(basedir
)
1362 + strlen(MODULE_DIR
)
1363 + strlen(version
) + 1));
1364 sprintf(dirname
, "%s%s%s", basedir
, MODULE_DIR
, version
);
1367 if (!doing_stdout
&& !depfile_out_of_date(dirname
))
1372 read_toplevel_config(config
, basedir
, &search
, &overrides
);
1374 /* For backward compatibility add "updates" to the head of the search
1375 * list here. But only if there was no "search" option specified.
1379 search
= add_search("updates",search
);
1382 /* Do command line args. */
1383 for (opt
= optind
; opt
< argc
; opt
++) {
1384 struct module
*new = grab_module(NULL
, argv
[opt
]);
1386 /* cmd-line specified modules must exist */
1387 fatal("grab_module() failed for module %s\n", argv
[opt
]);
1393 list
= grab_basedir(dirname
,search
,overrides
);
1395 list
= sort_modules(dirname
,list
);
1396 list
= parse_modules(list
);
1398 for (i
= 0; i
< sizeof(depfiles
)/sizeof(depfiles
[0]); i
++) {
1400 struct depfile
*d
= &depfiles
[i
];
1401 char depname
[strlen(dirname
) + 1 + strlen(d
->name
) + 1];
1402 char tmpname
[strlen(dirname
) + 1 + strlen(d
->name
) +
1403 strlen(".temp") + 1];
1405 if (d
->map_file
&& !make_map_files
&& !force_map_files
)
1408 sprintf(depname
, "%s/%s", dirname
, d
->name
);
1409 sprintf(tmpname
, "%s/%s.temp", dirname
, d
->name
);
1410 if (!doing_stdout
) {
1411 out
= fopen(tmpname
, "w");
1413 fatal("Could not open %s for writing: %s\n",
1414 tmpname
, strerror(errno
));
1417 if (ends_in(depname
, ".bin"))
1420 d
->func(list
, out
, dirname
);
1421 if (!doing_stdout
) {
1423 if (rename(tmpname
, depname
) < 0)
1424 fatal("Could not rename %s into %s: %s\n",
1425 tmpname
, depname
, strerror(errno
));