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"
34 #define MODULE_DIR "/lib/modules/"
37 #ifndef MODULE_BUILTIN_KEY
38 #define MODULE_BUILTIN_KEY "built-in"
41 struct module_overrides
44 struct module_overrides
*next
;
46 /* overridden module */
53 struct module_search
*next
;
60 static unsigned int skipchars
;
61 static unsigned int make_map_files
= 1; /* default to on */
62 static unsigned int force_map_files
= 0; /* default to on */
64 #define SYMBOL_HASH_SIZE 1024
72 static struct symbol
*symbolhash
[SYMBOL_HASH_SIZE
];
74 /* This is based on the hash agorithm from gdbm, via tdb */
75 static inline unsigned int tdb_hash(const char *name
)
77 unsigned value
; /* Used to compute the hash value. */
78 unsigned i
; /* Used to cycle through random values. */
80 /* Set the initial value from the key size. */
81 for (value
= 0x238F13AF * strlen(name
), i
=0; name
[i
]; i
++)
82 value
= (value
+ (((unsigned char *)name
)[i
] << (i
*5 % 24)));
84 return (1103515243 * value
+ 12345);
87 void add_symbol(const char *name
, struct module
*owner
)
90 struct symbol
*new = NOFAIL(malloc(sizeof *new + strlen(name
) + 1));
93 strcpy(new->name
, name
);
95 hash
= tdb_hash(name
) % SYMBOL_HASH_SIZE
;
96 new->next
= symbolhash
[hash
];
97 symbolhash
[hash
] = new;
100 static int print_unknown
;
102 struct module
*find_symbol(const char *name
, const char *modname
, int weak
)
106 /* For our purposes, .foo matches foo. PPC64 needs this. */
110 for (s
= symbolhash
[tdb_hash(name
) % SYMBOL_HASH_SIZE
]; s
; s
=s
->next
) {
111 if (streq(s
->name
, name
))
115 if (print_unknown
&& !weak
)
116 warn("%s needs unknown symbol %s\n", modname
, name
);
121 void add_dep(struct module
*mod
, struct module
*depends_on
)
125 for (i
= 0; i
< mod
->num_deps
; i
++)
126 if (mod
->deps
[i
] == depends_on
)
129 mod
->deps
= NOFAIL(realloc(mod
->deps
, sizeof(mod
->deps
[0])*(mod
->num_deps
+1)));
130 mod
->deps
[mod
->num_deps
++] = depends_on
;
133 static void load_system_map(const char *filename
)
137 const char ksymstr
[] = "__ksymtab_";
138 const int ksymstr_len
= strlen(ksymstr
);
140 system_map
= fopen(filename
, "r");
142 fatal("Could not open '%s': %s\n", filename
, strerror(errno
));
144 /* eg. c0294200 R __ksymtab_devfs_alloc_devnum */
145 while (fgets(line
, sizeof(line
)-1, system_map
)) {
149 ptr
= strchr(line
, '\n');
153 ptr
= strchr(line
, ' ');
154 if (!ptr
|| !(ptr
= strchr(ptr
+ 1, ' ')))
157 /* Covers gpl-only and normal symbols. */
158 if (strncmp(ptr
+1, ksymstr
, ksymstr_len
) == 0)
159 add_symbol(ptr
+1+ksymstr_len
, NULL
);
164 /* __this_module is magic inserted by kernel loader. */
165 add_symbol("__this_module", NULL
);
166 /* On S390, this is faked up too */
167 add_symbol("_GLOBAL_OFFSET_TABLE_", NULL
);
170 static struct option options
[] = { { "all", 0, NULL
, 'a' },
171 { "quick", 0, NULL
, 'A' },
172 { "basedir", 1, NULL
, 'b' },
173 { "errsyms", 0, NULL
, 'e' },
174 { "filesyms", 1, NULL
, 'F' },
175 { "help", 0, NULL
, 'h' },
176 { "show", 0, NULL
, 'n' },
177 { "dry-run", 0, NULL
, 'n' },
178 { "quiet", 0, NULL
, 'q' },
179 { "root", 0, NULL
, 'r' },
180 { "unresolved-error", 0, NULL
, 'u' },
181 { "verbose", 0, NULL
, 'v' },
182 { "version", 0, NULL
, 'V' },
183 { "config", 1, NULL
, 'C' },
184 { "warn", 1, NULL
, 'w' },
185 { "map", 0, NULL
, 'm' },
186 { NULL
, 0, NULL
, 0 } };
188 /* Version number or module name? Don't assume extension. */
189 static int is_version_number(const char *version
)
193 return (sscanf(version
, "%u.%u.%u", &dummy
, &dummy
, &dummy
) == 3);
196 static int old_module_version(const char *version
)
198 /* Expect three part version. */
199 unsigned int major
, sub
, minor
;
201 sscanf(version
, "%u.%u.%u", &major
, &sub
, &minor
);
203 if (major
> 2) return 0;
204 if (major
< 2) return 1;
207 if (sub
> 5) return 0;
208 if (sub
< 5) return 1;
211 if (minor
>= 48) return 0;
215 static void exec_old_depmod(char *argv
[])
218 char pathname
[strlen(argv
[0])+1];
219 char oldname
[strlen("depmod") + strlen(argv
[0]) + sizeof(".old")];
221 memset(pathname
, 0, strlen(argv
[0])+1);
222 sep
= strrchr(argv
[0], '/');
224 memcpy(pathname
, argv
[0], sep
- argv
[0]+1);
225 sprintf(oldname
, "%s%s.old", pathname
, "depmod");
227 /* Recursion detection: we need an env var since we can't
228 change argv[0] (as older modutils uses it to determine
230 if (getenv("MODULE_RECURSE"))
232 setenv("MODULE_RECURSE", "y", 0);
234 execvp(oldname
, argv
);
236 "Version requires old depmod, but couldn't run %s: %s\n",
237 oldname
, strerror(errno
));
241 static void grammar(const char *cmd
, const char *filename
, unsigned int line
)
243 warn("%s line %u: ignoring bad line starting with '%s'\n",
244 filename
, line
, cmd
);
248 static void print_usage(const char *name
)
251 "%s " VERSION
" -- part of " PACKAGE
"\n"
252 "%s -[aA] [-n -e -v -q -V -r -u -w -m]\n"
253 " [-b basedirectory] [forced_version]\n"
254 "depmod [-n -e -v -q -r -u -w] [-F kernelsyms] module1.ko module2.ko ...\n"
255 "If no arguments (except options) are given, \"depmod -a\" is assumed\n"
257 "depmod will output a dependancy list suitable for the modprobe utility.\n"
261 "\t-a, --all Probe all modules\n"
262 "\t-A, --quick Only does the work if there's a new module\n"
263 "\t-e, --errsyms Report not supplied symbols\n"
264 "\t-m, --map Create the legacy map files\n"
265 "\t-n, --show Write the dependency file on stdout only\n"
266 "\t-V, --version Print the release version\n"
267 "\t-v, --verbose Enable verbose mode\n"
268 "\t-w, --warn Warn on duplicates\n"
269 "\t-h, --help Print this usage message\n"
271 "The following options are useful for people managing distributions:\n"
272 "\t-b basedirectory\n"
273 "\t --basedir basedirectory Use an image of a module tree.\n"
275 "\t --filesyms kernelsyms Use the file instead of the\n"
276 "\t current kernel symbols.\n",
280 static int ends_in(const char *name
, const char *ext
)
282 unsigned int namelen
, extlen
;
285 namelen
= strlen(name
);
286 extlen
= strlen(ext
);
288 if (namelen
< extlen
) return 0;
290 if (streq(name
+ namelen
- extlen
, ext
))
295 /* "\177ELF" <byte> where byte = 001 for 32-bit, 002 for 64 */
296 int needconv(const char *elfhdr
)
298 union { short s
; char c
[2]; } endian_test
;
301 if (endian_test
.c
[1] == 1) return elfhdr
[EI_DATA
] != ELFDATA2MSB
;
302 if (endian_test
.c
[0] == 1) return elfhdr
[EI_DATA
] != ELFDATA2LSB
;
307 static char *my_basename(const char *name
)
309 const char *base
= strrchr(name
, '/');
310 if (base
) return (char *)base
+ 1;
314 static struct module
*grab_module(const char *dirname
, const char *filename
)
318 new = NOFAIL(malloc(sizeof(*new)
319 + strlen(dirname
?:"") + 1 + strlen(filename
) + 1));
321 sprintf(new->pathname
, "%s/%s", dirname
, filename
);
323 strcpy(new->pathname
, filename
);
324 new->basename
= my_basename(new->pathname
);
326 INIT_LIST_HEAD(&new->dep_list
);
328 new->data
= grab_file(new->pathname
, &new->len
);
330 warn("Can't read module %s: %s\n",
331 new->pathname
, strerror(errno
));
335 /* "\177ELF" <byte> where byte = 001 for 32-bit, 002 for 64 */
336 if (memcmp(new->data
, ELFMAG
, SELFMAG
) != 0) {
337 warn("Module %s is not an elf object\n", new->pathname
);
341 switch (((char *)new->data
)[EI_CLASS
]) {
343 new->ops
= &mod_ops32
;
346 new->ops
= &mod_ops64
;
349 warn("Module %s has elf unknown identifier %i\n",
350 new->pathname
, ((char *)new->data
)[EI_CLASS
]);
353 new->conv
= needconv(new->data
);
357 release_file(new->data
, new->len
);
363 struct module_traverse
365 struct module_traverse
*prev
;
369 static int in_loop(struct module
*mod
, const struct module_traverse
*traverse
)
371 const struct module_traverse
*i
;
373 for (i
= traverse
; i
; i
= i
->prev
) {
380 /* Assume we are doing all the modules, so only report each loop once. */
381 static void report_loop(const struct module
*mod
,
382 const struct module_traverse
*traverse
)
384 const struct module_traverse
*i
;
386 /* Check that start is least alphabetically. eg. a depends
387 on b depends on a will get reported for a, not b. */
388 for (i
= traverse
->prev
; i
->prev
; i
= i
->prev
) {
389 if (strcmp(mod
->pathname
, i
->mod
->pathname
) > 0)
393 /* Is start in the loop? If not, don't report now. eg. a
394 depends on b which depends on c which depends on b. Don't
395 report when generating depends for a. */
399 warn("Loop detected: %s ", mod
->pathname
);
400 for (i
= traverse
->prev
; i
->prev
; i
= i
->prev
)
401 fprintf(stderr
, "needs %s ", i
->mod
->basename
);
402 fprintf(stderr
, "which needs %s again!\n", i
->mod
->basename
);
405 /* This is damn slow, but loops actually happen, and we don't want to
406 just exit() and leave the user without any modules. */
407 static int has_dep_loop(struct module
*module
, struct module_traverse
*prev
)
410 struct module_traverse traverse
= { .prev
= prev
, .mod
= module
};
412 if (in_loop(module
, prev
)) {
413 report_loop(module
, &traverse
);
417 for (i
= 0; i
< module
->num_deps
; i
++)
418 if (has_dep_loop(module
->deps
[i
], &traverse
))
423 /* Uniquifies and orders a dependency list. */
424 static void order_dep_list(struct module
*start
, struct module
*mod
)
428 for (i
= 0; i
< mod
->num_deps
; i
++) {
429 /* If it was previously depended on, move it to the
430 tail. ie. if a needs b and c, and c needs b, we
431 must order b after c. */
432 list_del(&mod
->deps
[i
]->dep_list
);
433 list_add_tail(&mod
->deps
[i
]->dep_list
, &start
->dep_list
);
434 order_dep_list(start
, mod
->deps
[i
]);
438 static struct module
*deleted
= NULL
;
440 static void del_module(struct module
**modules
, struct module
*delme
)
444 /* Find pointer to it. */
446 for (i
= modules
; *i
!= delme
; i
= &(*i
)->next
);
451 /* Save on a list to quiet valgrind.
452 Can't free - other modules may depend on them */
453 delme
->next
= deleted
;
457 /* Convert filename to the module name. Works if filename == modname, too. */
458 static void filename2modname(char *modname
, const char *filename
)
460 const char *afterslash
;
463 afterslash
= strrchr(filename
, '/');
465 afterslash
= filename
;
469 /* Convert to underscores, stop at first . */
470 for (i
= 0; afterslash
[i
] && afterslash
[i
] != '.'; i
++) {
471 if (afterslash
[i
] == '-')
474 modname
[i
] = afterslash
[i
];
479 static void output_deps(struct module
*modules
,
480 FILE *out
, char *dirname
)
484 for (i
= modules
; i
; i
= i
->next
) {
485 struct list_head
*j
, *tmp
;
486 order_dep_list(i
, i
);
488 fprintf(out
, "%s:", i
->pathname
+ strlen(dirname
)+1);
489 list_for_each_safe(j
, tmp
, &i
->dep_list
) {
491 = list_entry(j
, struct module
, dep_list
);
492 fprintf(out
, " %s", dep
->pathname
+ strlen(dirname
)+1);
499 /* warn whenever duplicate module aliases, deps, or symbols are found. */
502 static void output_deps_bin(struct module
*modules
,
503 FILE *out
, char *dirname
)
506 struct index_node
*index
;
510 index
= index_create();
512 for (i
= modules
; i
; i
= i
->next
) {
513 struct list_head
*j
, *tmp
;
514 char modname
[strlen(i
->pathname
)+1];
516 order_dep_list(i
, i
);
518 filename2modname(modname
, i
->pathname
+ strlen(dirname
)+1);
519 nofail_asprintf(&line
, "%s %s:", modname
, i
->pathname
+ strlen(dirname
)+1);
521 list_for_each_safe(j
, tmp
, &i
->dep_list
) {
523 = list_entry(j
, struct module
, dep_list
);
524 nofail_asprintf(&line
, "%s %s", p
, dep
->pathname
+ strlen(dirname
)+1);
529 if (index_insert(index
, line
) && warn_dups
)
530 warn("duplicate module deps:\n%s\n",line
);
534 index_write(index
, out
);
535 index_destroy(index
);
539 static int smells_like_module(const char *name
)
541 return ends_in(name
,".ko") || ends_in(name
, ".ko.gz");
544 typedef struct module
*(*do_module_t
)(const char *dirname
,
545 const char *filename
,
547 struct module_search
*search
,
548 struct module_overrides
*overrides
);
550 static int is_higher_priority(const char *newpath
, const char *oldpath
,
551 struct module_search
*search
,
552 struct module_overrides
*overrides
)
555 struct module_search
*tmp
;
556 struct module_overrides
*ovtmp
;
558 int prio_builtin
= -1;
562 /* The names already match, now we check for wildcard overrides and other
563 * high priority overrides we added to the config.
565 for (ovtmp
=overrides
;ovtmp
!=NULL
;ovtmp
=ovtmp
->next
) {
567 p
= strstr(ovtmp
->modfile
,newpath
);
571 q
= strstr(ovtmp
->modfile
,"*");
573 p
= strstr(newpath
, q
+1);
577 p
= strstr(oldpath
, q
+1);
582 p
= strstr(ovtmp
->modfile
,oldpath
);
587 for (i
=0,tmp
=search
;tmp
!=NULL
;tmp
=tmp
->next
,i
++) {
589 s
= NOFAIL(malloc(strlen(tmp
->search_path
)+2));
591 strncpy(s
+1,tmp
->search_path
, strlen(tmp
->search_path
)+1);
593 if (0 == strncmp(tmp
->search_path
,MODULE_BUILTIN_KEY
,
594 strlen(MODULE_BUILTIN_KEY
)))
597 p
= strstr(newpath
,s
);
598 if ((p
) && ((p
[strlen(s
)] == '/')
599 || (p
[strlen(s
)] == '\0')))
602 p
= strstr(oldpath
,s
);
603 if ((p
) && ((p
[strlen(s
)] == '/')
604 || (p
[strlen(s
)] == '\0')))
612 prio_new
= prio_builtin
;
615 prio_old
= prio_builtin
;
617 return prio_new
> prio_old
;
622 static struct module
*do_module(const char *dirname
,
623 const char *filename
,
625 struct module_search
*search
,
626 struct module_overrides
*overrides
)
628 struct module
*new, **i
;
630 new = grab_module(dirname
, filename
);
634 /* Check if module is already in the list. */
635 for (i
= &list
; *i
; i
= &(*i
)->next
) {
637 if (streq((*i
)->basename
, filename
)) {
638 char newpath
[strlen(dirname
) + strlen("/")
639 + strlen(filename
) + 1];
641 sprintf(newpath
, "%s/%s", dirname
, filename
);
643 if (is_higher_priority(newpath
, (*i
)->pathname
,search
,
650 del_module(NULL
, new);
656 /* Not in the list already. Just prepend. */
661 static struct module
*grab_dir(const char *dirname
,
665 struct module_search
*search
,
666 struct module_overrides
*overrides
)
668 struct dirent
*dirent
;
670 while ((dirent
= readdir(dir
)) != NULL
) {
671 if (smells_like_module(dirent
->d_name
))
672 next
= do_mod(dirname
, dirent
->d_name
, next
,
674 else if (!streq(dirent
->d_name
, ".")
675 && !streq(dirent
->d_name
, "..")
676 && !streq(dirent
->d_name
, "source")
677 && !streq(dirent
->d_name
, "build")) {
680 char subdir
[strlen(dirname
) + 1
681 + strlen(dirent
->d_name
) + 1];
682 sprintf(subdir
, "%s/%s", dirname
, dirent
->d_name
);
683 sub
= opendir(subdir
);
685 next
= grab_dir(subdir
, sub
, next
, do_mod
,
694 static struct module
*grab_basedir(const char *dirname
,
695 struct module_search
*search
,
696 struct module_overrides
*overrides
)
701 dir
= opendir(dirname
);
703 warn("Couldn't open directory %s: %s\n",
704 dirname
, strerror(errno
));
707 list
= grab_dir(dirname
, dir
, NULL
, do_module
, search
, overrides
);
713 static struct module
*parse_modules(struct module
*list
)
717 for (i
= list
; i
; i
= i
->next
) {
718 i
->ops
->load_symbols(i
);
719 i
->ops
->fetch_tables(i
);
722 for (i
= list
; i
; i
= i
->next
)
723 i
->ops
->calculate_deps(i
, verbose
);
725 /* Strip out modules with dependency loops. */
727 for (i
= list
; i
; i
= i
->next
) {
728 if (has_dep_loop(i
, NULL
)) {
729 warn("Module %s ignored, due to loop\n",
730 i
->pathname
+ skipchars
);
731 del_module(&list
, i
);
739 /* Simply dump hash table. */
740 static void output_symbols(struct module
*unused
, FILE *out
, char *dirname
)
744 fprintf(out
, "# Aliases for symbols, used by symbol_request().\n");
745 for (i
= 0; i
< SYMBOL_HASH_SIZE
; i
++) {
748 for (s
= symbolhash
[i
]; s
; s
= s
->next
) {
750 char modname
[strlen(s
->owner
->pathname
)+1];
751 filename2modname(modname
, s
->owner
->pathname
);
752 fprintf(out
, "alias symbol:%s %s\n",
759 static void output_symbols_bin(struct module
*unused
, FILE *out
, char *dirname
)
761 struct index_node
*index
;
765 index
= index_create();
767 for (i
= 0; i
< SYMBOL_HASH_SIZE
; i
++) {
770 for (s
= symbolhash
[i
]; s
; s
= s
->next
) {
772 char modname
[strlen(s
->owner
->pathname
)+1];
773 filename2modname(modname
, s
->owner
->pathname
);
774 nofail_asprintf(&line
, "symbol:%s %s",
776 if (index_insert(index
, line
) && warn_dups
)
777 warn("duplicate module syms:\n%s\n",
784 index_write(index
, out
);
785 index_destroy(index
);
788 static const char *next_string(const char *string
, unsigned long *secsize
)
790 /* Skip non-zero chars */
793 if ((*secsize
)-- <= 1)
797 /* Skip any zero padding. */
800 if ((*secsize
)-- <= 1)
806 /* Careful! Don't munge - in [ ] as per Debian Bug#350915 */
807 static char *underscores(char *string
)
814 for (i
= 0; string
[i
]; i
++) {
821 i
+= strcspn(&string
[i
], "]");
823 warn("Unmatched bracket in %s\n", string
);
829 static void output_aliases(struct module
*modules
, FILE *out
, char *dirname
)
835 fprintf(out
, "# Aliases extracted from modules themselves.\n");
836 for (i
= modules
; i
; i
= i
->next
) {
837 char modname
[strlen(i
->pathname
)+1];
839 filename2modname(modname
, i
->pathname
);
841 /* Grab from old-style .modalias section. */
842 for (p
= i
->ops
->get_aliases(i
, &size
);
844 p
= next_string(p
, &size
))
845 fprintf(out
, "alias %s %s\n", p
, modname
);
847 /* Grab form new-style .modinfo section. */
848 for (p
= i
->ops
->get_modinfo(i
, &size
);
850 p
= next_string(p
, &size
)) {
851 if (strncmp(p
, "alias=", strlen("alias=")) == 0)
852 fprintf(out
, "alias %s %s\n",
853 p
+ strlen("alias="), modname
);
858 static void output_aliases_bin(struct module
*modules
, FILE *out
, char *dirname
)
864 struct index_node
*index
;
866 index
= index_create();
868 for (i
= modules
; i
; i
= i
->next
) {
869 char modname
[strlen(i
->pathname
)+1];
871 filename2modname(modname
, i
->pathname
);
873 /* Grab from old-style .modalias section. */
874 for (p
= i
->ops
->get_aliases(i
, &size
);
876 p
= next_string(p
, &size
)) {
877 nofail_asprintf(&line
, "%s %s", p
, modname
);
879 if (index_insert(index
, line
) && warn_dups
)
880 warn("duplicate module alias:\n%s\n", line
);
884 /* Grab from new-style .modinfo section. */
885 for (p
= i
->ops
->get_modinfo(i
, &size
);
887 p
= next_string(p
, &size
)) {
888 if (strncmp(p
, "alias=", strlen("alias=")) == 0) {
889 nofail_asprintf(&line
, "%s %s",
890 p
+ strlen("alias="), modname
);
892 if (index_insert(index
, line
) && warn_dups
)
893 warn("duplicate module alias:\n%s\n",
900 index_write(index
, out
);
901 index_destroy(index
);
906 void (*func
)(struct module
*, FILE *, char *dirname
);
910 static struct depfile depfiles
[] = {
911 { "modules.dep", output_deps
, 0 }, /* This is what we check for '-A'. */
912 { "modules.dep.bin", output_deps_bin
, 0 },
913 { "modules.pcimap", output_pci_table
, 1 },
914 { "modules.usbmap", output_usb_table
, 1 },
915 { "modules.ccwmap", output_ccw_table
, 1 },
916 { "modules.ieee1394map", output_ieee1394_table
, 1 },
917 { "modules.isapnpmap", output_isapnp_table
, 1 },
918 { "modules.inputmap", output_input_table
, 1 },
919 { "modules.ofmap", output_of_table
, 1 },
920 { "modules.seriomap", output_serio_table
, 1 },
921 { "modules.alias", output_aliases
, 0 },
922 { "modules.alias.bin", output_aliases_bin
, 0 },
923 { "modules.symbols", output_symbols
, 0 },
924 { "modules.symbols.bin", output_symbols_bin
, 0 }
927 /* If we can't figure it out, it's safe to say "true". */
928 static int any_modules_newer(const char *dirname
, time_t mtime
)
931 struct dirent
*dirent
;
933 dir
= opendir(dirname
);
937 while ((dirent
= readdir(dir
)) != NULL
) {
939 char file
[strlen(dirname
) + 1 + strlen(dirent
->d_name
) + 1];
941 if (streq(dirent
->d_name
, ".") || streq(dirent
->d_name
, ".."))
944 sprintf(file
, "%s/%s", dirname
, dirent
->d_name
);
945 if (lstat(file
, &st
) != 0)
948 if (smells_like_module(dirent
->d_name
)) {
949 if (st
.st_mtime
> mtime
)
951 } else if (S_ISDIR(st
.st_mode
)) {
952 if (any_modules_newer(file
, mtime
))
964 static int depfile_out_of_date(const char *dirname
)
967 char depfile
[strlen(dirname
) + 1 + strlen(depfiles
[0].name
) + 1];
969 sprintf(depfile
, "%s/%s", dirname
, depfiles
[0].name
);
971 if (stat(depfile
, &st
) != 0)
974 return any_modules_newer(dirname
, st
.st_mtime
);
977 static char *getline_wrapped(FILE *file
, unsigned int *linenum
)
981 char *buf
= NOFAIL(malloc(size
));
983 int ch
= getc_unlocked(file
);
991 /* else fall through */
997 buf
= NOFAIL(realloc(buf
, size
+ 1));
1002 ch
= getc_unlocked(file
);
1009 /* else fall through */
1016 buf
= NOFAIL(realloc(buf
, size
));
1023 static char *strsep_skipspace(char **string
, char *delim
)
1027 *string
+= strspn(*string
, delim
);
1028 return strsep(string
, delim
);
1031 static struct module_search
*add_search(const char *search_path
,
1032 struct module_search
*search
)
1035 struct module_search
*new;
1037 new = NOFAIL(malloc(sizeof(*new)));
1038 new->search_path
= NOFAIL(strdup(search_path
));
1045 static struct module_overrides
*add_override(const char *modfile
,
1046 struct module_overrides
*overrides
)
1049 struct module_overrides
*new;
1051 new = NOFAIL(malloc(sizeof(*new)));
1052 new->modfile
= NOFAIL(strdup(modfile
));
1053 new->next
= overrides
;
1060 static int read_config(const char *filename
,
1061 const char *basedir
,
1062 struct module_search
**search
,
1063 struct module_overrides
**overrides
);
1065 static int read_config_file(const char *filename
,
1066 const char *basedir
,
1067 struct module_search
**search
,
1068 struct module_overrides
**overrides
)
1071 unsigned int linenum
= 0;
1074 cfile
= fopen(filename
, "r");
1076 if (errno
!= ENOENT
)
1077 fatal("could not open '%s', reason: %s\n", filename
,
1082 while ((line
= getline_wrapped(cfile
, &linenum
)) != NULL
) {
1084 char *cmd
, *modname
;
1086 cmd
= strsep_skipspace(&ptr
, "\t ");
1088 if (cmd
== NULL
|| cmd
[0] == '#' || cmd
[0] == '\0') {
1093 if (strcmp(cmd
, "search") == 0) {
1096 while ((search_path
= strsep_skipspace(&ptr
, "\t ")))
1097 *search
= add_search(search_path
, *search
);
1098 } else if (strcmp(cmd
, "override") == 0) {
1099 char *pathname
= NULL
, *version
, *subdir
;
1100 modname
= strsep_skipspace(&ptr
, "\t ");
1101 version
= strsep_skipspace(&ptr
, "\t ");
1102 subdir
= strsep_skipspace(&ptr
, "\t ");
1104 pathname
= NOFAIL(malloc(strlen(basedir
)
1105 + strlen(MODULE_DIR
)
1111 sprintf(pathname
, "%s%s%s/%s/%s.ko", basedir
,
1112 MODULE_DIR
, version
, subdir
, modname
);
1114 *overrides
= add_override(pathname
, *overrides
);
1115 } else if (strcmp(cmd
, "include") == 0) {
1118 newfilename
= strsep_skipspace(&ptr
, "\t ");
1120 grammar(cmd
, filename
, linenum
);
1122 if (!read_config(newfilename
, basedir
,
1124 warn("Failed to open included"
1125 " config file %s: %s\n",
1126 newfilename
, strerror(errno
));
1129 } else if (strcmp(cmd
, "make_map_files") == 0) {
1132 option
= strsep_skipspace(&ptr
, "\t ");
1134 grammar(cmd
, filename
, linenum
);
1136 if (0 == strncmp(option
, "yes", 3))
1138 else if (0 == strncmp(option
, "no", 2))
1141 grammar(cmd
, filename
, linenum
);
1144 grammar(cmd
, filename
, linenum
);
1152 /* Simple format, ignore lines starting with #, one command per line.
1153 Returns true or false. */
1154 static int read_config(const char *filename
,
1155 const char *basedir
,
1156 struct module_search
**search
,
1157 struct module_overrides
**overrides
)
1162 dir
= opendir(filename
);
1165 while ((i
= readdir(dir
)) != NULL
) {
1166 if (!streq(i
->d_name
,".") && !streq(i
->d_name
,"..")) {
1167 char sub
[strlen(filename
) + 1
1168 + strlen(i
->d_name
) + 1];
1170 sprintf(sub
, "%s/%s", filename
, i
->d_name
);
1171 if (!read_config(sub
, basedir
, search
,
1173 warn("Failed to open"
1174 " config file %s: %s\n",
1175 sub
, strerror(errno
));
1181 if (read_config_file(filename
, basedir
, search
, overrides
))
1188 static const char *default_configs
[] =
1194 static void read_toplevel_config(const char *filename
,
1195 const char *basedir
,
1196 struct module_search
**search
,
1197 struct module_overrides
**overrides
)
1202 if (!read_config(filename
, basedir
, search
, overrides
))
1203 fatal("Failed to open config file %s: %s\n",
1204 filename
, strerror(errno
));
1209 for (i
= 0; i
< ARRAY_SIZE(default_configs
); i
++) {
1210 read_config(default_configs
[i
], basedir
, search
, overrides
);
1214 /* Local to main, but not freed on exit. Keep valgrind quiet. */
1215 struct module
*list
= NULL
;
1216 struct module_search
*search
= NULL
;
1217 struct module_overrides
*overrides
= NULL
;
1219 int main(int argc
, char *argv
[])
1221 int opt
, all
= 0, maybe_all
= 0, doing_stdout
= 0;
1222 char *basedir
= "", *dirname
, *version
, *badopt
= NULL
,
1225 const char *config
= NULL
;
1227 /* Don't print out any errors just yet, we might want to exec
1228 backwards compat version. */
1230 while ((opt
= getopt_long(argc
, argv
, "ab:ArehnqruvVF:C:wm", options
, NULL
))
1238 skipchars
= strlen(basedir
);
1244 system_map
= optarg
;
1260 print_usage(argv
[0]);
1267 printf("%s %s\n", PACKAGE
, VERSION
);
1273 force_map_files
= 1;
1276 badopt
= argv
[optind
-1];
1280 /* We can't print unknowns without a System.map */
1284 load_system_map(system_map
);
1286 /* They can specify the version naked on the command line */
1287 if (optind
< argc
&& is_version_number(argv
[optind
])) {
1288 version
= NOFAIL(strdup(argv
[optind
]));
1293 version
= NOFAIL(strdup(buf
.release
));
1296 /* Run old version if required. */
1297 if (old_module_version(version
))
1298 exec_old_depmod(argv
);
1301 fprintf(stderr
, "%s: malformed/unrecognized option '%s'\n",
1303 print_usage(argv
[0]);
1307 /* Depmod -a by default if no names. */
1311 dirname
= NOFAIL(malloc(strlen(basedir
)
1312 + strlen(MODULE_DIR
)
1313 + strlen(version
) + 1));
1314 sprintf(dirname
, "%s%s%s", basedir
, MODULE_DIR
, version
);
1317 if (!doing_stdout
&& !depfile_out_of_date(dirname
))
1322 read_toplevel_config(config
, basedir
, &search
, &overrides
);
1324 /* For backward compatibility add "updates" to the head of the search
1325 * list here. But only if there was no "search" option specified.
1329 search
= add_search("updates",search
);
1332 /* Do command line args. */
1333 for (opt
= optind
; opt
< argc
; opt
++) {
1334 struct module
*new = grab_module(NULL
, argv
[opt
]);
1341 list
= grab_basedir(dirname
,search
,overrides
);
1343 list
= parse_modules(list
);
1345 for (i
= 0; i
< sizeof(depfiles
)/sizeof(depfiles
[0]); i
++) {
1347 struct depfile
*d
= &depfiles
[i
];
1348 char depname
[strlen(dirname
) + 1 + strlen(d
->name
) + 1];
1349 char tmpname
[strlen(dirname
) + 1 + strlen(d
->name
) +
1350 strlen(".temp") + 1];
1352 if (d
->map_file
&& !make_map_files
&& !force_map_files
)
1355 sprintf(depname
, "%s/%s", dirname
, d
->name
);
1356 sprintf(tmpname
, "%s/%s.temp", dirname
, d
->name
);
1357 if (!doing_stdout
) {
1358 out
= fopen(tmpname
, "w");
1360 fatal("Could not open %s for writing: %s\n",
1361 tmpname
, strerror(errno
));
1364 if (ends_in(depname
, ".bin"))
1367 d
->func(list
, out
, dirname
);
1368 if (!doing_stdout
) {
1370 if (rename(tmpname
, depname
) < 0)
1371 fatal("Could not rename %s into %s: %s\n",
1372 tmpname
, depname
, strerror(errno
));