1 /* New simplified depmod without backwards compat stuff and not
4 (C) 2002 Rusty Russell IBM Corporation
14 #include <sys/types.h>
18 #include <sys/utsname.h>
21 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
23 #include "zlibsupport.h"
25 #include "moduleops.h"
31 #define MODULE_DIR "/lib/modules/"
34 #ifndef MODULE_BUILTIN_KEY
35 #define MODULE_BUILTIN_KEY "built-in"
38 struct module_overrides
41 struct module_overrides
*next
;
43 /* overridden module */
50 struct module_search
*next
;
57 static unsigned int skipchars
;
59 void fatal(const char *fmt
, ...)
63 fprintf(stderr
, "FATAL: ");
65 va_start(arglist
, fmt
);
66 vfprintf(stderr
, fmt
, arglist
);
72 void warn(const char *fmt
, ...)
76 fprintf(stderr
, "WARNING: ");
78 va_start(arglist
, fmt
);
79 vfprintf(stderr
, fmt
, arglist
);
83 void *do_nofail(void *ptr
, const char *file
, int line
, const char *expr
)
86 fatal("Memory allocation failure %s line %d: %s.\n",
92 #define SYMBOL_HASH_SIZE 1024
100 static struct symbol
*symbolhash
[SYMBOL_HASH_SIZE
];
102 /* This is based on the hash agorithm from gdbm, via tdb */
103 static inline unsigned int tdb_hash(const char *name
)
105 unsigned value
; /* Used to compute the hash value. */
106 unsigned i
; /* Used to cycle through random values. */
108 /* Set the initial value from the key size. */
109 for (value
= 0x238F13AF * strlen(name
), i
=0; name
[i
]; i
++)
110 value
= (value
+ (((unsigned char *)name
)[i
] << (i
*5 % 24)));
112 return (1103515243 * value
+ 12345);
115 void add_symbol(const char *name
, struct module
*owner
)
118 struct symbol
*new = NOFAIL(malloc(sizeof *new + strlen(name
) + 1));
121 strcpy(new->name
, name
);
123 hash
= tdb_hash(name
) % SYMBOL_HASH_SIZE
;
124 new->next
= symbolhash
[hash
];
125 symbolhash
[hash
] = new;
128 static int print_unknown
;
130 struct module
*find_symbol(const char *name
, const char *modname
, int weak
)
134 /* For our purposes, .foo matches foo. PPC64 needs this. */
138 for (s
= symbolhash
[tdb_hash(name
) % SYMBOL_HASH_SIZE
]; s
; s
=s
->next
) {
139 if (streq(s
->name
, name
))
143 if (print_unknown
&& !weak
)
144 warn("%s needs unknown symbol %s\n", modname
, name
);
149 void add_dep(struct module
*mod
, struct module
*depends_on
)
153 for (i
= 0; i
< mod
->num_deps
; i
++)
154 if (mod
->deps
[i
] == depends_on
)
157 mod
->deps
= NOFAIL(realloc(mod
->deps
, sizeof(mod
->deps
[0])*(mod
->num_deps
+1)));
158 mod
->deps
[mod
->num_deps
++] = depends_on
;
161 static void load_system_map(const char *filename
)
165 const char ksymstr
[] = "__ksymtab_";
166 const int ksymstr_len
= strlen(ksymstr
);
168 system_map
= fopen(filename
, "r");
170 fatal("Could not open '%s': %s\n", filename
, strerror(errno
));
172 /* eg. c0294200 R __ksymtab_devfs_alloc_devnum */
173 while (fgets(line
, sizeof(line
)-1, system_map
)) {
177 ptr
= strchr(line
, '\n');
181 ptr
= strchr(line
, ' ');
182 if (!ptr
|| !(ptr
= strchr(ptr
+ 1, ' ')))
185 /* Covers gpl-only and normal symbols. */
186 if (strncmp(ptr
+1, ksymstr
, ksymstr_len
) == 0)
187 add_symbol(ptr
+1+ksymstr_len
, NULL
);
192 /* __this_module is magic inserted by kernel loader. */
193 add_symbol("__this_module", NULL
);
194 /* On S390, this is faked up too */
195 add_symbol("_GLOBAL_OFFSET_TABLE_", NULL
);
198 static struct option options
[] = { { "all", 0, NULL
, 'a' },
199 { "quick", 0, NULL
, 'A' },
200 { "basedir", 1, NULL
, 'b' },
201 { "errsyms", 0, NULL
, 'e' },
202 { "filesyms", 1, NULL
, 'F' },
203 { "help", 0, NULL
, 'h' },
204 { "show", 0, NULL
, 'n' },
205 { "dry-run", 0, NULL
, 'n' },
206 { "quiet", 0, NULL
, 'q' },
207 { "root", 0, NULL
, 'r' },
208 { "unresolved-error", 0, NULL
, 'u' },
209 { "verbose", 0, NULL
, 'v' },
210 { "version", 0, NULL
, 'V' },
211 { "config", 1, NULL
, 'C' },
212 { NULL
, 0, NULL
, 0 } };
214 /* Version number or module name? Don't assume extension. */
215 static int is_version_number(const char *version
)
219 return (sscanf(version
, "%u.%u.%u", &dummy
, &dummy
, &dummy
) == 3);
222 static int old_module_version(const char *version
)
224 /* Expect three part version. */
225 unsigned int major
, sub
, minor
;
227 sscanf(version
, "%u.%u.%u", &major
, &sub
, &minor
);
229 if (major
> 2) return 0;
230 if (major
< 2) return 1;
233 if (sub
> 5) return 0;
234 if (sub
< 5) return 1;
237 if (minor
>= 48) return 0;
241 static void exec_old_depmod(char *argv
[])
244 char pathname
[strlen(argv
[0])+1];
245 char oldname
[strlen("depmod") + strlen(argv
[0]) + sizeof(".old")];
247 memset(pathname
, 0, strlen(argv
[0])+1);
248 sep
= strrchr(argv
[0], '/');
250 memcpy(pathname
, argv
[0], sep
- argv
[0]+1);
251 sprintf(oldname
, "%s%s.old", pathname
, "depmod");
253 /* Recursion detection: we need an env var since we can't
254 change argv[0] (as older modutils uses it to determine
256 if (getenv("MODULE_RECURSE"))
258 setenv("MODULE_RECURSE", "y", 0);
260 execvp(oldname
, argv
);
262 "Version requires old depmod, but couldn't run %s: %s\n",
263 oldname
, strerror(errno
));
267 static void grammar(const char *cmd
, const char *filename
, unsigned int line
)
269 warn("%s line %u: ignoring bad line starting with '%s'\n",
270 filename
, line
, cmd
);
274 static void print_usage(const char *name
)
277 "%s " VERSION
" -- part of " PACKAGE
"\n"
278 "%s -[aA] [-n -e -v -q -V -r -u]\n"
279 " [-b basedirectory] [forced_version]\n"
280 "depmod [-n -e -v -q -r -u] [-F kernelsyms] module1.ko module2.ko ...\n"
281 "If no arguments (except options) are given, \"depmod -a\" is assumed\n"
283 "depmod will output a dependancy list suitable for the modprobe utility.\n"
287 "\t-a, --all Probe all modules\n"
288 "\t-A, --quick Only does the work if there's a new module\n"
289 "\t-n, --show Write the dependency file on stdout only\n"
290 "\t-e, --errsyms Report not supplied symbols\n"
291 "\t-V, --version Print the release version\n"
292 "\t-v, --verbose Enable verbose mode\n"
293 "\t-h, --help Print this usage message\n"
295 "The following options are useful for people managing distributions:\n"
296 "\t-b basedirectory\n"
297 "\t --basedir basedirectory Use an image of a module tree.\n"
299 "\t --filesyms kernelsyms Use the file instead of the\n"
300 "\t current kernel symbols.\n",
304 static int ends_in(const char *name
, const char *ext
)
306 unsigned int namelen
, extlen
;
309 namelen
= strlen(name
);
310 extlen
= strlen(ext
);
312 if (namelen
< extlen
) return 0;
314 if (streq(name
+ namelen
- extlen
, ext
))
319 /* "\177ELF" <byte> where byte = 001 for 32-bit, 002 for 64 */
320 int needconv(const char *elfhdr
)
322 union { short s
; char c
[2]; } endian_test
;
325 if (endian_test
.c
[1] == 1) return elfhdr
[EI_DATA
] != ELFDATA2MSB
;
326 if (endian_test
.c
[0] == 1) return elfhdr
[EI_DATA
] != ELFDATA2LSB
;
331 static struct module
*grab_module(const char *dirname
, const char *filename
)
335 new = NOFAIL(malloc(sizeof(*new)
336 + strlen(dirname
?:"") + 1 + strlen(filename
) + 1));
338 sprintf(new->pathname
, "%s/%s", dirname
, filename
);
340 strcpy(new->pathname
, filename
);
342 INIT_LIST_HEAD(&new->dep_list
);
344 new->data
= grab_file(new->pathname
, &new->len
);
346 warn("Can't read module %s: %s\n",
347 new->pathname
, strerror(errno
));
351 /* "\177ELF" <byte> where byte = 001 for 32-bit, 002 for 64 */
352 if (memcmp(new->data
, ELFMAG
, SELFMAG
) != 0) {
353 warn("Module %s is not an elf object\n", new->pathname
);
357 switch (((char *)new->data
)[EI_CLASS
]) {
359 new->ops
= &mod_ops32
;
362 new->ops
= &mod_ops64
;
365 warn("Module %s has elf unknown identifier %i\n",
366 new->pathname
, ((char *)new->data
)[EI_CLASS
]);
369 new->conv
= needconv(new->data
);
373 release_file(new->data
, new->len
);
379 struct module_traverse
381 struct module_traverse
*prev
;
385 static int in_loop(struct module
*mod
, const struct module_traverse
*traverse
)
387 const struct module_traverse
*i
;
389 for (i
= traverse
; i
; i
= i
->prev
) {
396 static char *basename(const char *name
)
398 const char *base
= strrchr(name
, '/');
399 if (base
) return (char *)base
+ 1;
403 /* Assume we are doing all the modules, so only report each loop once. */
404 static void report_loop(const struct module
*mod
,
405 const struct module_traverse
*traverse
)
407 const struct module_traverse
*i
;
409 /* Check that start is least alphabetically. eg. a depends
410 on b depends on a will get reported for a, not b. */
411 for (i
= traverse
->prev
; i
->prev
; i
= i
->prev
) {
412 if (strcmp(mod
->pathname
, i
->mod
->pathname
) > 0)
416 /* Is start in the loop? If not, don't report now. eg. a
417 depends on b which depends on c which depends on b. Don't
418 report when generating depends for a. */
422 warn("Loop detected: %s ", mod
->pathname
);
423 for (i
= traverse
->prev
; i
->prev
; i
= i
->prev
)
424 fprintf(stderr
, "needs %s ", basename(i
->mod
->pathname
));
425 fprintf(stderr
, "which needs %s again!\n", basename(mod
->pathname
));
428 /* This is damn slow, but loops actually happen, and we don't want to
429 just exit() and leave the user without any modules. */
430 static int has_dep_loop(struct module
*module
, struct module_traverse
*prev
)
433 struct module_traverse traverse
= { .prev
= prev
, .mod
= module
};
435 if (in_loop(module
, prev
)) {
436 report_loop(module
, &traverse
);
440 for (i
= 0; i
< module
->num_deps
; i
++)
441 if (has_dep_loop(module
->deps
[i
], &traverse
))
446 /* Uniquifies and orders a dependency list. */
447 static void order_dep_list(struct module
*start
, struct module
*mod
)
451 for (i
= 0; i
< mod
->num_deps
; i
++) {
452 /* If it was previously depended on, move it to the
453 tail. ie. if a needs b and c, and c needs b, we
454 must order b after c. */
455 list_del(&mod
->deps
[i
]->dep_list
);
456 list_add_tail(&mod
->deps
[i
]->dep_list
, &start
->dep_list
);
457 order_dep_list(start
, mod
->deps
[i
]);
461 static struct module
*deleted
= NULL
;
463 static void del_module(struct module
**modules
, struct module
*delme
)
467 /* Find pointer to it. */
469 for (i
= modules
; *i
!= delme
; i
= &(*i
)->next
);
474 /* Save on a list to quiet valgrind.
475 Can't free - other modules may depend on them */
476 delme
->next
= deleted
;
480 static void output_deps(struct module
*modules
,
485 for (i
= modules
; i
; i
= i
->next
)
486 i
->ops
->calculate_deps(i
, verbose
);
488 /* Strip out loops. */
490 for (i
= modules
; i
; i
= i
->next
) {
491 if (has_dep_loop(i
, NULL
)) {
492 warn("Module %s ignored, due to loop\n",
493 i
->pathname
+ skipchars
);
494 del_module(&modules
, i
);
499 /* Now dump them out. */
500 for (i
= modules
; i
; i
= i
->next
) {
501 struct list_head
*j
, *tmp
;
502 order_dep_list(i
, i
);
504 fprintf(out
, "%s:", i
->pathname
+ skipchars
);
505 list_for_each_safe(j
, tmp
, &i
->dep_list
) {
507 = list_entry(j
, struct module
, dep_list
);
508 fprintf(out
, " %s", dep
->pathname
+ skipchars
);
515 static int smells_like_module(const char *name
)
517 return ends_in(name
,".ko") || ends_in(name
, ".ko.gz");
520 typedef struct module
*(*do_module_t
)(const char *dirname
,
521 const char *filename
,
523 struct module_search
*search
,
524 struct module_overrides
*overrides
);
526 static int is_higher_priority(const char *newpath
, const char *oldpath
,
527 struct module_search
*search
,
528 struct module_overrides
*overrides
)
531 struct module_search
*tmp
;
532 struct module_overrides
*ovtmp
;
534 int prio_builtin
= -1;
538 /* The names already match, now we check for wildcard overrides and other
539 * high priority overrides we added to the config.
541 for (ovtmp
=overrides
;ovtmp
!=NULL
;ovtmp
=ovtmp
->next
) {
543 p
= strstr(ovtmp
->modfile
,newpath
);
547 q
= strstr(ovtmp
->modfile
,"*");
549 p
= strstr(newpath
, q
+1);
553 p
= strstr(oldpath
, q
+1);
558 p
= strstr(ovtmp
->modfile
,oldpath
);
563 for (i
=0,tmp
=search
;tmp
!=NULL
;tmp
=tmp
->next
,i
++) {
565 s
= NOFAIL(malloc(strlen(tmp
->search_path
)+2));
567 strncpy(s
+1,tmp
->search_path
, strlen(tmp
->search_path
)+1);
569 if (0 == strncmp(tmp
->search_path
,MODULE_BUILTIN_KEY
,
570 strlen(MODULE_BUILTIN_KEY
)))
573 p
= strstr(newpath
,s
);
574 if ((p
) && ((p
[strlen(s
)] == '/')
575 || (p
[strlen(s
)] == '\0')))
578 p
= strstr(oldpath
,s
);
579 if ((p
) && ((p
[strlen(s
)] == '/')
580 || (p
[strlen(s
)] == '\0')))
588 prio_new
= prio_builtin
;
591 prio_old
= prio_builtin
;
593 return prio_new
> prio_old
;
598 static struct module
*do_module(const char *dirname
,
599 const char *filename
,
601 struct module_search
*search
,
602 struct module_overrides
*overrides
)
604 struct module
*new, **i
;
606 new = grab_module(dirname
, filename
);
610 /* Check if module is already in the list. */
611 for (i
= &list
; *i
; i
= &(*i
)->next
) {
613 if (streq(basename((*i
)->pathname
), filename
)) {
614 char newpath
[strlen(dirname
) + strlen("/")
615 + strlen(filename
) + 1];
617 sprintf(newpath
, "%s/%s", dirname
, filename
);
619 if (is_higher_priority(newpath
, (*i
)->pathname
,search
,
626 del_module(NULL
, new);
632 /* Not in the list already. Just prepend. */
637 static struct module
*grab_dir(const char *dirname
,
641 struct module_search
*search
,
642 struct module_overrides
*overrides
)
644 struct dirent
*dirent
;
646 while ((dirent
= readdir(dir
)) != NULL
) {
647 if (smells_like_module(dirent
->d_name
))
648 next
= do_mod(dirname
, dirent
->d_name
, next
,
650 else if (!streq(dirent
->d_name
, ".")
651 && !streq(dirent
->d_name
, "..")
652 && !streq(dirent
->d_name
, "source")
653 && !streq(dirent
->d_name
, "build")) {
656 char subdir
[strlen(dirname
) + 1
657 + strlen(dirent
->d_name
) + 1];
658 sprintf(subdir
, "%s/%s", dirname
, dirent
->d_name
);
659 sub
= opendir(subdir
);
661 next
= grab_dir(subdir
, sub
, next
, do_mod
,
670 static struct module
*grab_basedir(const char *dirname
,
671 struct module_search
*search
,
672 struct module_overrides
*overrides
)
677 dir
= opendir(dirname
);
679 warn("Couldn't open directory %s: %s\n",
680 dirname
, strerror(errno
));
683 list
= grab_dir(dirname
, dir
, NULL
, do_module
, search
, overrides
);
689 static void parse_modules(struct module
*list
)
693 for (i
= list
; i
; i
= i
->next
) {
694 i
->ops
->load_symbols(i
);
695 i
->ops
->fetch_tables(i
);
699 /* Convert filename to the module name. Works if filename == modname, too. */
700 static void filename2modname(char *modname
, const char *filename
)
702 const char *afterslash
;
705 afterslash
= strrchr(filename
, '/');
707 afterslash
= filename
;
711 /* Convert to underscores, stop at first . */
712 for (i
= 0; afterslash
[i
] && afterslash
[i
] != '.'; i
++) {
713 if (afterslash
[i
] == '-')
716 modname
[i
] = afterslash
[i
];
721 /* Simply dump hash table. */
722 static void output_symbols(struct module
*unused
, FILE *out
)
726 fprintf(out
, "# Aliases for symbols, used by symbol_request().\n");
727 for (i
= 0; i
< SYMBOL_HASH_SIZE
; i
++) {
730 for (s
= symbolhash
[i
]; s
; s
= s
->next
) {
732 char modname
[strlen(s
->owner
->pathname
)+1];
733 filename2modname(modname
, s
->owner
->pathname
);
734 fprintf(out
, "alias symbol:%s %s\n",
741 static const char *next_string(const char *string
, unsigned long *secsize
)
743 /* Skip non-zero chars */
746 if ((*secsize
)-- <= 1)
750 /* Skip any zero padding. */
753 if ((*secsize
)-- <= 1)
759 static void output_aliases(struct module
*modules
, FILE *out
)
765 fprintf(out
, "# Aliases extracted from modules themselves.\n");
766 for (i
= modules
; i
; i
= i
->next
) {
767 char modname
[strlen(i
->pathname
)+1];
769 filename2modname(modname
, i
->pathname
);
771 /* Grab from old-style .modalias section. */
772 for (p
= i
->ops
->get_aliases(i
, &size
);
774 p
= next_string(p
, &size
))
775 fprintf(out
, "alias %s %s\n", p
, modname
);
777 /* Grab form new-style .modinfo section. */
778 for (p
= i
->ops
->get_modinfo(i
, &size
);
780 p
= next_string(p
, &size
)) {
781 if (strncmp(p
, "alias=", strlen("alias=")) == 0)
782 fprintf(out
, "alias %s %s\n",
783 p
+ strlen("alias="), modname
);
790 void (*func
)(struct module
*, FILE *);
793 static struct depfile depfiles
[] = {
794 { "modules.dep", output_deps
}, /* This is what we check for '-A'. */
795 { "modules.pcimap", output_pci_table
},
796 { "modules.usbmap", output_usb_table
},
797 { "modules.ccwmap", output_ccw_table
},
798 { "modules.ieee1394map", output_ieee1394_table
},
799 { "modules.isapnpmap", output_isapnp_table
},
800 { "modules.inputmap", output_input_table
},
801 { "modules.ofmap", output_of_table
},
802 { "modules.seriomap", output_serio_table
},
803 { "modules.alias", output_aliases
},
804 { "modules.symbols", output_symbols
},
807 /* If we can't figure it out, it's safe to say "true". */
808 static int any_modules_newer(const char *dirname
, time_t mtime
)
811 struct dirent
*dirent
;
813 dir
= opendir(dirname
);
817 while ((dirent
= readdir(dir
)) != NULL
) {
819 char file
[strlen(dirname
) + 1 + strlen(dirent
->d_name
) + 1];
821 if (streq(dirent
->d_name
, ".") || streq(dirent
->d_name
, ".."))
824 sprintf(file
, "%s/%s", dirname
, dirent
->d_name
);
825 if (lstat(file
, &st
) != 0)
828 if (smells_like_module(dirent
->d_name
)) {
829 if (st
.st_mtime
> mtime
)
831 } else if (S_ISDIR(st
.st_mode
)) {
832 if (any_modules_newer(file
, mtime
))
844 static int depfile_out_of_date(const char *dirname
)
847 char depfile
[strlen(dirname
) + 1 + strlen(depfiles
[0].name
) + 1];
849 sprintf(depfile
, "%s/%s", dirname
, depfiles
[0].name
);
851 if (stat(depfile
, &st
) != 0)
854 return any_modules_newer(dirname
, st
.st_mtime
);
857 static int fgetc_wrapped(FILE *file
, unsigned int *linenum
)
860 int ch
= fgetc(file
);
871 static char *getline_wrapped(FILE *file
, unsigned int *linenum
)
875 char *buf
= NOFAIL(malloc(size
));
877 int ch
= fgetc_wrapped(file
, linenum
);
880 buf
= NOFAIL(realloc(buf
, size
));
882 if (ch
< 0 && i
== 0) {
886 if (ch
< 0 || ch
== '\n') {
890 return NOFAIL(realloc(buf
, i
+1));
897 static char *strsep_skipspace(char **string
, char *delim
)
901 *string
+= strspn(*string
, delim
);
902 return strsep(string
, delim
);
905 static struct module_search
*add_search(const char *search_path
,
906 struct module_search
*search
)
909 struct module_search
*new;
911 new = NOFAIL(malloc(sizeof(*new)));
912 new->search_path
= NOFAIL(strdup(search_path
));
919 static struct module_overrides
*add_override(const char *modfile
,
920 struct module_overrides
*overrides
)
923 struct module_overrides
*new;
925 new = NOFAIL(malloc(sizeof(*new)));
926 new->modfile
= NOFAIL(strdup(modfile
));
927 new->next
= overrides
;
934 static int read_config(const char *filename
,
936 struct module_search
**search
,
937 struct module_overrides
**overrides
);
939 static int read_config_file(const char *filename
,
941 struct module_search
**search
,
942 struct module_overrides
**overrides
)
945 unsigned int linenum
= 0;
948 cfile
= fopen(filename
, "r");
951 fatal("could not open '%s', reason: %s\n", filename
,
956 while ((line
= getline_wrapped(cfile
, &linenum
)) != NULL
) {
960 cmd
= strsep_skipspace(&ptr
, "\t ");
962 if (cmd
== NULL
|| cmd
[0] == '#' || cmd
[0] == '\0') {
967 if (strcmp(cmd
, "search") == 0) {
970 while ((search_path
= strsep_skipspace(&ptr
, "\t ")))
971 *search
= add_search(search_path
, *search
);
972 } else if (strcmp(cmd
, "override") == 0) {
973 char *pathname
= NULL
, *version
, *subdir
;
974 modname
= strsep_skipspace(&ptr
, "\t ");
975 version
= strsep_skipspace(&ptr
, "\t ");
976 subdir
= strsep_skipspace(&ptr
, "\t ");
978 pathname
= NOFAIL(malloc(strlen(basedir
)
985 sprintf(pathname
, "%s%s%s/%s/%s.ko", basedir
,
986 MODULE_DIR
, version
, subdir
, modname
);
988 *overrides
= add_override(pathname
, *overrides
);
989 } else if (strcmp(cmd
, "include") == 0) {
992 newfilename
= strsep_skipspace(&ptr
, "\t ");
994 grammar(cmd
, filename
, linenum
);
996 if (!read_config(newfilename
, basedir
,
998 warn("Failed to open included"
999 " config file %s: %s\n",
1000 newfilename
, strerror(errno
));
1003 grammar(cmd
, filename
, linenum
);
1011 /* Simple format, ignore lines starting with #, one command per line.
1012 Returns true or false. */
1013 static int read_config(const char *filename
,
1014 const char *basedir
,
1015 struct module_search
**search
,
1016 struct module_overrides
**overrides
)
1021 dir
= opendir(filename
);
1024 while ((i
= readdir(dir
)) != NULL
) {
1025 if (!streq(i
->d_name
,".") && !streq(i
->d_name
,"..")) {
1026 char sub
[strlen(filename
) + 1
1027 + strlen(i
->d_name
) + 1];
1029 sprintf(sub
, "%s/%s", filename
, i
->d_name
);
1030 if (!read_config(sub
, basedir
, search
,
1032 warn("Failed to open"
1033 " config file %s: %s\n",
1034 sub
, strerror(errno
));
1040 if (read_config_file(filename
, basedir
, search
, overrides
))
1047 static const char *default_configs
[] =
1053 static void read_toplevel_config(const char *filename
,
1054 const char *basedir
,
1055 struct module_search
**search
,
1056 struct module_overrides
**overrides
)
1061 if (!read_config(filename
, basedir
, search
, overrides
))
1062 fatal("Failed to open config file %s: %s\n",
1063 filename
, strerror(errno
));
1068 for (i
= 0; i
< ARRAY_SIZE(default_configs
); i
++) {
1069 if (read_config(default_configs
[i
], basedir
, search
, overrides
))
1074 /* Local to main, but not freed on exit. Keep valgrind quiet. */
1075 struct module
*list
= NULL
;
1076 struct module_search
*search
= NULL
;
1077 struct module_overrides
*overrides
= NULL
;
1079 int main(int argc
, char *argv
[])
1081 int opt
, all
= 0, maybe_all
= 0, doing_stdout
= 0;
1082 char *basedir
= "", *dirname
, *version
, *badopt
= NULL
,
1085 const char *config
= NULL
;
1087 /* Don't print out any errors just yet, we might want to exec
1088 backwards compat version. */
1090 while ((opt
= getopt_long(argc
, argv
, "ab:ArehnqruvVF:C:", options
, NULL
))
1098 skipchars
= strlen(basedir
);
1104 system_map
= optarg
;
1120 print_usage(argv
[0]);
1127 printf("%s %s\n", PACKAGE
, VERSION
);
1130 badopt
= argv
[optind
-1];
1134 /* We can't print unknowns without a System.map */
1138 load_system_map(system_map
);
1140 /* They can specify the version naked on the command line */
1141 if (optind
< argc
&& is_version_number(argv
[optind
])) {
1142 version
= NOFAIL(strdup(argv
[optind
]));
1147 version
= NOFAIL(strdup(buf
.release
));
1150 /* Run old version if required. */
1151 if (old_module_version(version
))
1152 exec_old_depmod(argv
);
1155 fprintf(stderr
, "%s: malformed/unrecognized option '%s'\n",
1157 print_usage(argv
[0]);
1161 /* Depmod -a by default if no names. */
1165 dirname
= NOFAIL(malloc(strlen(basedir
)
1166 + strlen(MODULE_DIR
)
1167 + strlen(version
) + 1));
1168 sprintf(dirname
, "%s%s%s", basedir
, MODULE_DIR
, version
);
1171 if (!doing_stdout
&& !depfile_out_of_date(dirname
))
1176 read_toplevel_config(config
, basedir
, &search
, &overrides
);
1178 /* For backward compatibility add "updates" to the head of the search
1179 * list here. But only if there was no "search" option specified.
1183 search
= add_search("updates",search
);
1186 /* Do command line args. */
1187 for (opt
= optind
; opt
< argc
; opt
++) {
1188 struct module
*new = grab_module(NULL
, argv
[opt
]);
1195 list
= grab_basedir(dirname
,search
,overrides
);
1197 parse_modules(list
);
1199 for (i
= 0; i
< sizeof(depfiles
)/sizeof(depfiles
[0]); i
++) {
1201 struct depfile
*d
= &depfiles
[i
];
1202 char depname
[strlen(dirname
) + 1 + strlen(d
->name
) + 1];
1203 char tmpname
[strlen(dirname
) + 1 + strlen(d
->name
) +
1204 strlen(".temp") + 1];
1206 sprintf(depname
, "%s/%s", dirname
, d
->name
);
1207 sprintf(tmpname
, "%s/%s.temp", dirname
, d
->name
);
1208 if (!doing_stdout
) {
1209 out
= fopen(tmpname
, "w");
1211 fatal("Could not open %s for writing: %s\n",
1212 tmpname
, strerror(errno
));
1216 if (!doing_stdout
) {
1218 if (rename(tmpname
, depname
) < 0)
1219 fatal("Could not rename %s into %s: %s\n",
1220 tmpname
, depname
, strerror(errno
));