2 * kmod-depmod - calculate modules.dep using libkmod.
4 * Copyright (C) 2011-2013 ProFUSION embedded systems
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #include <sys/utsname.h>
34 #include <shared/array.h>
35 #include <shared/hash.h>
36 #include <shared/macro.h>
37 #include <shared/util.h>
38 #include <shared/scratchbuf.h>
40 #include <libkmod/libkmod-internal.h>
47 #define DEFAULT_VERBOSE LOG_WARNING
48 static int verbose
= DEFAULT_VERBOSE
;
50 static const char CFG_BUILTIN_KEY
[] = "built-in";
51 static const char *default_cfg_paths
[] = {
53 SYSCONFDIR
"/depmod.d",
58 static const char cmdopts_s
[] = "aAb:C:E:F:euqrvnP:wmVh";
59 static const struct option cmdopts
[] = {
60 { "all", no_argument
, 0, 'a' },
61 { "quick", no_argument
, 0, 'A' },
62 { "basedir", required_argument
, 0, 'b' },
63 { "config", required_argument
, 0, 'C' },
64 { "symvers", required_argument
, 0, 'E' },
65 { "filesyms", required_argument
, 0, 'F' },
66 { "errsyms", no_argument
, 0, 'e' },
67 { "unresolved-error", no_argument
, 0, 'u' }, /* deprecated */
68 { "quiet", no_argument
, 0, 'q' }, /* deprecated */
69 { "root", no_argument
, 0, 'r' }, /* deprecated */
70 { "verbose", no_argument
, 0, 'v' },
71 { "show", no_argument
, 0, 'n' },
72 { "dry-run", no_argument
, 0, 'n' },
73 { "symbol-prefix", required_argument
, 0, 'P' },
74 { "warn", no_argument
, 0, 'w' },
75 { "map", no_argument
, 0, 'm' }, /* deprecated */
76 { "version", no_argument
, 0, 'V' },
77 { "help", no_argument
, 0, 'h' },
81 static void help(void)
84 "\t%s -[aA] [options] [forced_version]\n"
86 "If no arguments (except options) are given, \"depmod -a\" is assumed\n"
88 "depmod will output a dependency list suitable for the modprobe utility.\n"
91 "\t-a, --all Probe all modules\n"
92 "\t-A, --quick Only does the work if there's a new module\n"
93 "\t-e, --errsyms Report not supplied symbols\n"
94 "\t-n, --show Write the dependency file on stdout only\n"
95 "\t-P, --symbol-prefix Architecture symbol prefix\n"
96 "\t-C, --config=PATH Read configuration from PATH\n"
97 "\t-v, --verbose Enable verbose mode\n"
98 "\t-w, --warn Warn on duplicates\n"
99 "\t-V, --version show version\n"
100 "\t-h, --help show this help\n"
102 "The following options are useful for people managing distributions:\n"
103 "\t-b, --basedir=DIR Use an image of a module tree.\n"
104 "\t-F, --filesyms=FILE Use the file instead of the\n"
105 "\t current kernel symbols.\n"
106 "\t-E, --symvers=FILE Use Module.symvers file to check\n"
107 "\t symbol versions.\n",
108 program_invocation_short_name
);
111 _printf_format_(1, 2)
112 static inline void _show(const char *fmt
, ...)
116 if (verbose
<= DEFAULT_VERBOSE
)
120 vfprintf(stdout
, fmt
, args
);
124 #define SHOW(...) _show(__VA_ARGS__)
127 /* binary index write *************************************************/
128 #include <arpa/inet.h>
129 /* BEGIN: code from module-init-tools/index.c just modified to compile here.
131 * Original copyright:
132 * index.c: module index file shared functions for modprobe and depmod
133 * Copyright (C) 2008 Alan Jenkins <alan-jenkins@tuffmail.co.uk>.
135 * These programs are free software; you can redistribute it and/or modify
136 * it under the terms of the GNU General Public License as published by
137 * the Free Software Foundation; either version 2 of the License, or
138 * (at your option) any later version.
140 * This program is distributed in the hope that it will be useful,
141 * but WITHOUT ANY WARRANTY; without even the implied warranty of
142 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
143 * GNU General Public License for more details.
145 * You should have received a copy of the GNU General Public License
146 * along with these programs. If not, see <http://www.gnu.org/licenses/>.
149 /* see documentation in libkmod/libkmod-index.c */
151 #define INDEX_MAGIC 0xB007F457
152 #define INDEX_VERSION_MAJOR 0x0002
153 #define INDEX_VERSION_MINOR 0x0001
154 #define INDEX_VERSION ((INDEX_VERSION_MAJOR<<16)|INDEX_VERSION_MINOR)
155 #define INDEX_CHILDMAX 128
158 struct index_value
*next
;
159 unsigned int priority
;
163 /* In-memory index (depmod only) */
165 char *prefix
; /* path compression */
166 struct index_value
*values
;
167 unsigned char first
; /* range of child nodes */
169 struct index_node
*children
[INDEX_CHILDMAX
]; /* indexed by character */
173 /* Format of node offsets within index file */
175 INDEX_NODE_FLAGS
= 0xF0000000, /* Flags in high nibble */
176 INDEX_NODE_PREFIX
= 0x80000000,
177 INDEX_NODE_VALUES
= 0x40000000,
178 INDEX_NODE_CHILDS
= 0x20000000,
180 INDEX_NODE_MASK
= 0x0FFFFFFF, /* Offset value */
183 static struct index_node
*index_create(void)
185 struct index_node
*node
;
187 node
= NOFAIL(calloc(sizeof(struct index_node
), 1));
188 node
->prefix
= NOFAIL(strdup(""));
189 node
->first
= INDEX_CHILDMAX
;
194 static void index_values_free(struct index_value
*values
)
197 struct index_value
*value
= values
;
199 values
= value
->next
;
204 static void index_destroy(struct index_node
*node
)
208 for (c
= node
->first
; c
<= node
->last
; c
++) {
209 struct index_node
*child
= node
->children
[c
];
212 index_destroy(child
);
214 index_values_free(node
->values
);
219 static void index__checkstring(const char *str
)
223 for (i
= 0; str
[i
]; i
++) {
226 if (ch
>= INDEX_CHILDMAX
)
227 CRIT("Module index: bad character '%c'=0x%x - only 7-bit ASCII is supported:"
228 "\n%s\n", (char) ch
, (int) ch
, str
);
232 static int index_add_value(struct index_value
**values
,
233 const char *value
, unsigned int priority
)
235 struct index_value
*v
;
239 /* report the presence of duplicate values */
240 for (v
= *values
; v
; v
= v
->next
) {
241 if (streq(v
->value
, value
))
245 /* find position to insert value */
246 while (*values
&& (*values
)->priority
< priority
)
247 values
= &(*values
)->next
;
250 v
= NOFAIL(calloc(sizeof(struct index_value
) + len
+ 1, 1));
252 v
->priority
= priority
;
253 memcpy(v
->value
, value
, len
+ 1);
259 static int index_insert(struct index_node
*node
, const char *key
,
260 const char *value
, unsigned int priority
)
262 int i
= 0; /* index within str */
265 index__checkstring(key
);
266 index__checkstring(value
);
269 int j
; /* index within node->prefix */
271 /* Ensure node->prefix is a prefix of &str[i].
272 If it is not already, then we must split node. */
273 for (j
= 0; node
->prefix
[j
]; j
++) {
274 ch
= node
->prefix
[j
];
276 if (ch
!= key
[i
+j
]) {
277 char *prefix
= node
->prefix
;
278 struct index_node
*n
;
280 /* New child is copy of node with prefix[j+1..N] */
281 n
= NOFAIL(calloc(sizeof(struct index_node
), 1));
282 memcpy(n
, node
, sizeof(struct index_node
));
283 n
->prefix
= NOFAIL(strdup(&prefix
[j
+1]));
285 /* Parent has prefix[0..j], child at prefix[j] */
286 memset(node
, 0, sizeof(struct index_node
));
288 node
->prefix
= prefix
;
291 node
->children
[ch
] = n
;
296 /* j is now length of node->prefix */
301 return index_add_value(&node
->values
, value
, priority
);
303 if (!node
->children
[ch
]) {
304 struct index_node
*child
;
306 if (ch
< node
->first
)
310 node
->children
[ch
] = NOFAIL(calloc(sizeof(struct index_node
), 1));
312 child
= node
->children
[ch
];
313 child
->prefix
= NOFAIL(strdup(&key
[i
+1]));
314 child
->first
= INDEX_CHILDMAX
;
315 index_add_value(&child
->values
, value
, priority
);
320 /* Descend into child node and continue */
321 node
= node
->children
[ch
];
326 static int index__haschildren(const struct index_node
*node
)
328 return node
->first
< INDEX_CHILDMAX
;
331 /* Recursive post-order traversal
333 Pre-order would make for better read-side buffering / readahead / caching.
334 (post-order means you go backwards in the file as you descend the tree).
335 However, index reading is already fast enough.
336 Pre-order is simpler for writing, and depmod is already slow.
338 static uint32_t index_write__node(const struct index_node
*node
, FILE *out
)
340 uint32_t *child_offs
= NULL
;
347 /* Write children and save their offsets */
348 if (index__haschildren(node
)) {
349 const struct index_node
*child
;
352 child_count
= node
->last
- node
->first
+ 1;
353 child_offs
= NOFAIL(malloc(child_count
* sizeof(uint32_t)));
355 for (i
= 0; i
< child_count
; i
++) {
356 child
= node
->children
[node
->first
+ i
];
357 child_offs
[i
] = htonl(index_write__node(child
, out
));
361 /* Now write this node */
364 if (node
->prefix
[0]) {
365 fputs(node
->prefix
, out
);
367 offset
|= INDEX_NODE_PREFIX
;
371 fputc(node
->first
, out
);
372 fputc(node
->last
, out
);
373 fwrite(child_offs
, sizeof(uint32_t), child_count
, out
);
374 offset
|= INDEX_NODE_CHILDS
;
380 const struct index_value
*v
;
381 unsigned int value_count
;
385 for (v
= node
->values
; v
!= NULL
; v
= v
->next
)
387 u
= htonl(value_count
);
388 fwrite(&u
, sizeof(u
), 1, out
);
390 for (v
= node
->values
; v
!= NULL
; v
= v
->next
) {
391 u
= htonl(v
->priority
);
392 fwrite(&u
, sizeof(u
), 1, out
);
393 fputs(v
->value
, out
);
396 offset
|= INDEX_NODE_VALUES
;
402 static void index_write(const struct index_node
*node
, FILE *out
)
404 long initial_offset
, final_offset
;
407 u
= htonl(INDEX_MAGIC
);
408 fwrite(&u
, sizeof(u
), 1, out
);
409 u
= htonl(INDEX_VERSION
);
410 fwrite(&u
, sizeof(u
), 1, out
);
412 /* Second word is reserved for the offset of the root node */
413 initial_offset
= ftell(out
);
414 assert(initial_offset
>= 0);
416 fwrite(&u
, sizeof(uint32_t), 1, out
);
419 u
= htonl(index_write__node(node
, out
));
421 /* Update first word */
422 final_offset
= ftell(out
);
423 assert(final_offset
>= 0);
424 (void)fseek(out
, initial_offset
, SEEK_SET
);
425 fwrite(&u
, sizeof(uint32_t), 1, out
);
426 (void)fseek(out
, final_offset
, SEEK_SET
);
429 /* END: code from module-init-tools/index.c just modified to compile here.
432 /* configuration parsing **********************************************/
433 struct cfg_override
{
434 struct cfg_override
*next
;
440 struct cfg_search
*next
;
447 const char *kversion
;
448 char dirname
[PATH_MAX
];
451 uint8_t check_symvers
;
452 uint8_t print_unknown
;
454 struct cfg_override
*overrides
;
455 struct cfg_search
*searches
;
458 static int cfg_search_add(struct cfg
*cfg
, const char *path
, uint8_t builtin
)
460 struct cfg_search
*s
;
466 len
= strlen(path
) + 1;
468 s
= malloc(sizeof(struct cfg_search
) + len
);
470 ERR("search add: out of memory\n");
473 s
->builtin
= builtin
;
478 memcpy(s
->path
, path
, len
);
481 DBG("search add: %s, builtin=%hhu\n", path
, builtin
);
483 s
->next
= cfg
->searches
;
488 static void cfg_search_free(struct cfg_search
*s
)
493 static int cfg_override_add(struct cfg
*cfg
, const char *modname
, const char *subdir
)
495 struct cfg_override
*o
;
496 size_t modnamelen
= strlen(modname
);
497 size_t subdirlen
= strlen(subdir
);
500 o
= malloc(sizeof(struct cfg_override
) + subdirlen
+ 1
503 ERR("override add: out of memory\n");
506 memcpy(o
->path
, subdir
, subdirlen
);
511 memcpy(o
->path
+ i
, modname
, modnamelen
);
513 o
->path
[i
] = '\0'; /* no extension, so we can match .ko/.ko.gz */
517 DBG("override add: %s\n", o
->path
);
519 o
->next
= cfg
->overrides
;
524 static void cfg_override_free(struct cfg_override
*o
)
529 static int cfg_kernel_matches(const struct cfg
*cfg
, const char *pattern
)
535 if (streq(pattern
, "*"))
538 if (regcomp(&re
, pattern
, REG_EXTENDED
|REG_NOSUB
) != 0)
541 status
= regexec(&re
, cfg
->kversion
, 0, NULL
, 0);
547 static int cfg_file_parse(struct cfg
*cfg
, const char *filename
)
551 unsigned int linenum
= 0;
554 fp
= fopen(filename
, "r");
557 ERR("file parse %s: %m\n", filename
);
561 while ((line
= freadline_wrapped(fp
, &linenum
)) != NULL
) {
564 if (line
[0] == '\0' || line
[0] == '#')
567 cmd
= strtok_r(line
, "\t ", &saveptr
);
571 if (streq(cmd
, "search")) {
573 while ((sp
= strtok_r(NULL
, "\t ", &saveptr
)) != NULL
) {
574 uint8_t builtin
= streq(sp
, CFG_BUILTIN_KEY
);
575 cfg_search_add(cfg
, sp
, builtin
);
577 } else if (streq(cmd
, "override")) {
578 const char *modname
= strtok_r(NULL
, "\t ", &saveptr
);
579 const char *version
= strtok_r(NULL
, "\t ", &saveptr
);
580 const char *subdir
= strtok_r(NULL
, "\t ", &saveptr
);
582 if (modname
== NULL
|| version
== NULL
||
586 if (!cfg_kernel_matches(cfg
, version
)) {
587 INF("%s:%u: override kernel did not match %s\n",
588 filename
, linenum
, version
);
592 cfg_override_add(cfg
, modname
, subdir
);
593 } else if (streq(cmd
, "include")
594 || streq(cmd
, "make_map_files")) {
595 INF("%s:%u: command %s not implemented yet\n",
596 filename
, linenum
, cmd
);
599 ERR("%s:%u: ignoring bad line starting with '%s'\n",
600 filename
, linenum
, cmd
);
612 static int cfg_files_filter_out(DIR *d
, const char *dir
, const char *name
)
614 size_t len
= strlen(name
);
620 if (len
< 6 || !streq(name
+ len
- 5, ".conf")) {
621 INF("All cfg files need .conf: %s/%s\n", dir
, name
);
625 fstatat(dirfd(d
), name
, &st
, 0);
626 if (S_ISDIR(st
.st_mode
)) {
627 ERR("Directories inside directories are not supported: %s/%s\n",
642 static void cfg_file_free(struct cfg_file
*f
)
647 static int cfg_files_insert_sorted(struct cfg_file
***p_files
, size_t *p_n_files
,
648 const char *dir
, const char *name
)
650 struct cfg_file
**files
, *f
;
651 size_t i
, n_files
, namelen
, dirlen
;
654 dirlen
= strlen(dir
);
656 namelen
= strlen(name
);
658 name
= basename(dir
);
659 namelen
= strlen(name
);
660 dirlen
-= namelen
+ 1;
663 n_files
= *p_n_files
;
665 for (i
= 0; i
< n_files
; i
++) {
666 int cmp
= strcmp(name
, files
[i
]->name
);
668 DBG("Ignoring duplicate config file: %.*s/%s\n",
669 (int)dirlen
, dir
, name
);
675 f
= malloc(sizeof(struct cfg_file
) + dirlen
+ namelen
+ 2);
677 ERR("files insert sorted: out of memory\n");
681 tmp
= realloc(files
, sizeof(struct cfg_file
*) * (n_files
+ 1));
683 ERR("files insert sorted: out of memory\n");
687 *p_files
= files
= tmp
;
690 memmove(files
+ i
+ 1, files
+ i
,
691 sizeof(struct cfg_file
*) * (n_files
- i
));
696 f
->namelen
= namelen
;
697 f
->name
= f
->path
+ dirlen
+ 1;
698 memcpy(f
->path
, dir
, dirlen
);
699 f
->path
[dirlen
] = '/';
700 memcpy(f
->path
+ dirlen
+ 1, name
, namelen
);
701 f
->path
[dirlen
+ 1 + namelen
] = '\0';
703 *p_n_files
= n_files
+ 1;
708 * Insert configuration files ignoring duplicates
710 static int cfg_files_list(struct cfg_file
***p_files
, size_t *p_n_files
,
718 if (stat(path
, &st
) != 0) {
720 DBG("could not stat '%s': %m\n", path
);
724 if (!S_ISDIR(st
.st_mode
)) {
725 cfg_files_insert_sorted(p_files
, p_n_files
, path
, NULL
);
731 ERR("files list %s: %m\n", path
);
735 for (dent
= readdir(d
); dent
!= NULL
; dent
= readdir(d
)) {
736 if (cfg_files_filter_out(d
, path
, dent
->d_name
))
739 cfg_files_insert_sorted(p_files
, p_n_files
, path
, dent
->d_name
);
743 DBG("parsed configuration files from %s\n", path
);
747 static int cfg_load(struct cfg
*cfg
, const char * const *cfg_paths
)
749 size_t i
, n_files
= 0;
750 struct cfg_file
**files
= NULL
;
752 if (cfg_paths
== NULL
)
753 cfg_paths
= default_cfg_paths
;
755 for (i
= 0; cfg_paths
[i
] != NULL
; i
++)
756 cfg_files_list(&files
, &n_files
, cfg_paths
[i
]);
758 for (i
= 0; i
< n_files
; i
++) {
759 struct cfg_file
*f
= files
[i
];
760 cfg_file_parse(cfg
, f
->path
);
765 /* For backward compatibility add "updates" to the head of the search
766 * list here. But only if there was no "search" option specified.
768 if (cfg
->searches
== NULL
)
769 cfg_search_add(cfg
, "updates", 0);
774 static void cfg_free(struct cfg
*cfg
)
776 while (cfg
->overrides
) {
777 struct cfg_override
*tmp
= cfg
->overrides
;
778 cfg
->overrides
= cfg
->overrides
->next
;
779 cfg_override_free(tmp
);
782 while (cfg
->searches
) {
783 struct cfg_search
*tmp
= cfg
->searches
;
784 cfg
->searches
= cfg
->searches
->next
;
785 cfg_search_free(tmp
);
790 /* depmod calculations ***********************************************/
793 struct kmod_module
*kmod
;
795 const char *relpath
; /* path relative to '$ROOT/lib/modules/$VER/' */
796 char *uncrelpath
; /* same as relpath but ending in .ko */
797 struct kmod_list
*info_list
;
798 struct kmod_list
*dep_sym_list
;
799 struct array deps
; /* struct symbol */
800 size_t baselen
; /* points to start of basename/filename */
802 int sort_idx
; /* sort index using modules.order */
803 int dep_sort_idx
; /* topological sort index */
804 uint16_t idx
; /* index in depmod->modules.array */
805 uint16_t users
; /* how many modules depend on this one */
806 bool visited
; /* helper field to report cycles */
807 struct vertex
*vertex
; /* helper field to report cycles */
818 const struct cfg
*cfg
;
819 struct kmod_ctx
*ctx
;
820 struct array modules
;
821 struct hash
*modules_by_uncrelpath
;
822 struct hash
*modules_by_name
;
823 struct hash
*symbols
;
826 static void mod_free(struct mod
*mod
)
828 DBG("free %p kmod=%p, path=%s\n", mod
, mod
->kmod
, mod
->path
);
829 array_free_array(&mod
->deps
);
830 kmod_module_unref(mod
->kmod
);
831 kmod_module_info_free_list(mod
->info_list
);
832 kmod_module_dependency_symbols_free_list(mod
->dep_sym_list
);
833 free(mod
->uncrelpath
);
838 static int mod_add_dependency(struct mod
*mod
, struct symbol
*sym
)
842 DBG("%s depends on %s %s\n", mod
->path
, sym
->name
,
843 sym
->owner
!= NULL
? sym
->owner
->path
: "(unknown)");
845 if (sym
->owner
== NULL
)
848 err
= array_append_unique(&mod
->deps
, sym
->owner
);
855 SHOW("%s needs \"%s\": %s\n", mod
->path
, sym
->name
, sym
->owner
->path
);
859 static void symbol_free(struct symbol
*sym
)
861 DBG("free %p sym=%s, owner=%p %s\n", sym
, sym
->name
, sym
->owner
,
862 sym
->owner
!= NULL
? sym
->owner
->path
: "");
866 static int depmod_init(struct depmod
*depmod
, struct cfg
*cfg
,
867 struct kmod_ctx
*ctx
)
874 array_init(&depmod
->modules
, 128);
876 depmod
->modules_by_uncrelpath
= hash_new(512, NULL
);
877 if (depmod
->modules_by_uncrelpath
== NULL
) {
879 goto modules_by_uncrelpath_failed
;
882 depmod
->modules_by_name
= hash_new(512, NULL
);
883 if (depmod
->modules_by_name
== NULL
) {
885 goto modules_by_name_failed
;
888 depmod
->symbols
= hash_new(2048, (void (*)(void *))symbol_free
);
889 if (depmod
->symbols
== NULL
) {
897 hash_free(depmod
->modules_by_name
);
898 modules_by_name_failed
:
899 hash_free(depmod
->modules_by_uncrelpath
);
900 modules_by_uncrelpath_failed
:
904 static void depmod_shutdown(struct depmod
*depmod
)
908 hash_free(depmod
->symbols
);
910 hash_free(depmod
->modules_by_uncrelpath
);
912 hash_free(depmod
->modules_by_name
);
914 for (i
= 0; i
< depmod
->modules
.count
; i
++)
915 mod_free(depmod
->modules
.array
[i
]);
916 array_free_array(&depmod
->modules
);
918 kmod_unref(depmod
->ctx
);
921 static int depmod_module_add(struct depmod
*depmod
, struct kmod_module
*kmod
)
923 const struct cfg
*cfg
= depmod
->cfg
;
924 const char *modname
, *lastslash
;
929 modname
= kmod_module_get_name(kmod
);
930 modnamesz
= strlen(modname
) + 1;
932 mod
= calloc(1, sizeof(struct mod
) + modnamesz
);
936 mod
->sort_idx
= depmod
->modules
.count
+ 1;
937 mod
->dep_sort_idx
= INT32_MAX
;
938 memcpy(mod
->modname
, modname
, modnamesz
);
939 mod
->modnamesz
= modnamesz
;
941 array_init(&mod
->deps
, 4);
943 mod
->path
= strdup(kmod_module_get_path(kmod
));
944 lastslash
= strrchr(mod
->path
, '/');
945 mod
->baselen
= lastslash
- mod
->path
;
946 if (strncmp(mod
->path
, cfg
->dirname
, cfg
->dirnamelen
) == 0 &&
947 mod
->path
[cfg
->dirnamelen
] == '/')
948 mod
->relpath
= mod
->path
+ cfg
->dirnamelen
+ 1;
952 err
= hash_add_unique(depmod
->modules_by_name
, mod
->modname
, mod
);
954 ERR("hash_add_unique %s: %s\n", mod
->modname
, strerror(-err
));
958 if (mod
->relpath
!= NULL
) {
959 size_t uncrelpathlen
= lastslash
- mod
->relpath
+ modnamesz
960 + strlen(KMOD_EXTENSION_UNCOMPRESSED
);
961 mod
->uncrelpath
= memdup(mod
->relpath
, uncrelpathlen
+ 1);
962 mod
->uncrelpath
[uncrelpathlen
] = '\0';
963 err
= hash_add_unique(depmod
->modules_by_uncrelpath
,
964 mod
->uncrelpath
, mod
);
966 ERR("hash_add_unique %s: %s\n",
967 mod
->uncrelpath
, strerror(-err
));
968 hash_del(depmod
->modules_by_name
, mod
->modname
);
973 DBG("add %p kmod=%p, path=%s\n", mod
, kmod
, mod
->path
);
978 free(mod
->uncrelpath
);
983 static int depmod_module_del(struct depmod
*depmod
, struct mod
*mod
)
985 DBG("del %p kmod=%p, path=%s\n", mod
, mod
->kmod
, mod
->path
);
987 if (mod
->uncrelpath
!= NULL
)
988 hash_del(depmod
->modules_by_uncrelpath
, mod
->uncrelpath
);
990 hash_del(depmod
->modules_by_name
, mod
->modname
);
996 /* returns if existing module @mod is higher priority than newpath.
997 * note this is the inverse of module-init-tools is_higher_priority()
999 static int depmod_module_is_higher_priority(const struct depmod
*depmod
, const struct mod
*mod
, size_t baselen
, size_t namelen
, size_t modnamelen
, const char *newpath
)
1001 const struct cfg
*cfg
= depmod
->cfg
;
1002 const struct cfg_override
*ov
;
1003 const struct cfg_search
*se
;
1005 /* baselen includes the last '/' and mod->baselen doesn't. So it's
1006 * actually correct to use modnamelen in the first and modnamesz in
1008 size_t newlen
= baselen
+ modnamelen
;
1009 size_t oldlen
= mod
->baselen
+ mod
->modnamesz
;
1010 const char *oldpath
= mod
->path
;
1011 int i
, bprio
= -1, oldprio
= -1, newprio
= -1;
1013 assert(strncmp(newpath
, cfg
->dirname
, cfg
->dirnamelen
) == 0);
1014 assert(strncmp(oldpath
, cfg
->dirname
, cfg
->dirnamelen
) == 0);
1016 newpath
+= cfg
->dirnamelen
+ 1;
1017 newlen
-= cfg
->dirnamelen
+ 1;
1018 oldpath
+= cfg
->dirnamelen
+ 1;
1019 oldlen
-= cfg
->dirnamelen
+ 1;
1021 DBG("comparing priorities of %s and %s\n",
1024 for (ov
= cfg
->overrides
; ov
!= NULL
; ov
= ov
->next
) {
1025 DBG("override %s\n", ov
->path
);
1026 if (newlen
== ov
->len
&& memcmp(ov
->path
, newpath
, newlen
) == 0)
1028 if (oldlen
== ov
->len
&& memcmp(ov
->path
, oldpath
, oldlen
) == 0)
1032 for (i
= 0, se
= cfg
->searches
; se
!= NULL
; se
= se
->next
, i
++) {
1033 DBG("search %s\n", se
->builtin
? "built-in" : se
->path
);
1036 else if (newlen
> se
->len
&& newpath
[se
->len
] == '/' &&
1037 memcmp(se
->path
, newpath
, se
->len
) == 0)
1039 else if (oldlen
> se
->len
&& oldpath
[se
->len
] == '/' &&
1040 memcmp(se
->path
, oldpath
, se
->len
) == 0)
1049 DBG("priorities: built-in: %d, old: %d, new: %d\n",
1050 bprio
, oldprio
, newprio
);
1052 return newprio
<= oldprio
;
1055 static int depmod_modules_search_file(struct depmod
*depmod
, size_t baselen
, size_t namelen
, const char *path
)
1057 struct kmod_module
*kmod
;
1059 const char *relpath
;
1060 char modname
[PATH_MAX
];
1064 if (!path_ends_with_kmod_ext(path
+ baselen
, namelen
))
1067 if (path_to_modname(path
, modname
, &modnamelen
) == NULL
) {
1068 ERR("could not get modname from path %s\n", path
);
1072 relpath
= path
+ depmod
->cfg
->dirnamelen
+ 1;
1073 DBG("try %s (%s)\n", relpath
, modname
);
1075 mod
= hash_find(depmod
->modules_by_name
, modname
);
1079 if (depmod_module_is_higher_priority(depmod
, mod
, baselen
,
1080 namelen
, modnamelen
, path
)) {
1081 DBG("Ignored lower priority: %s, higher: %s\n",
1086 DBG("Replace lower priority %s with new module %s\n",
1087 mod
->relpath
, relpath
);
1088 err
= depmod_module_del(depmod
, mod
);
1090 ERR("could not del module %s: %s\n", mod
->path
, strerror(-err
));
1095 err
= kmod_module_new_from_path(depmod
->ctx
, path
, &kmod
);
1097 ERR("could not create module %s: %s\n", path
, strerror(-err
));
1101 err
= depmod_module_add(depmod
, kmod
);
1103 ERR("could not add module %s: %s\n",
1104 path
, strerror(-err
));
1105 kmod_module_unref(kmod
);
1111 static int depmod_modules_search_dir(struct depmod
*depmod
, DIR *d
, size_t baselen
, char *path
)
1114 int err
= 0, dfd
= dirfd(d
);
1116 while ((de
= readdir(d
)) != NULL
) {
1117 const char *name
= de
->d_name
;
1121 if (name
[0] == '.' && (name
[1] == '\0' ||
1122 (name
[1] == '.' && name
[2] == '\0')))
1124 if (streq(name
, "build") || streq(name
, "source"))
1126 namelen
= strlen(name
);
1127 if (baselen
+ namelen
+ 2 >= PATH_MAX
) {
1128 path
[baselen
] = '\0';
1129 ERR("path is too long %s%s\n", path
, name
);
1132 memcpy(path
+ baselen
, name
, namelen
+ 1);
1134 if (de
->d_type
== DT_REG
)
1136 else if (de
->d_type
== DT_DIR
)
1140 if (fstatat(dfd
, name
, &st
, 0) < 0) {
1141 ERR("fstatat(%d, %s): %m\n", dfd
, name
);
1143 } else if (S_ISREG(st
.st_mode
))
1145 else if (S_ISDIR(st
.st_mode
))
1148 ERR("unsupported file type %s: %o\n",
1149 path
, st
.st_mode
& S_IFMT
);
1157 if (baselen
+ namelen
+ 2 + NAME_MAX
>= PATH_MAX
) {
1158 ERR("directory path is too long %s\n", path
);
1161 fd
= openat(dfd
, name
, O_RDONLY
);
1163 ERR("openat(%d, %s, O_RDONLY): %m\n",
1167 subdir
= fdopendir(fd
);
1168 if (subdir
== NULL
) {
1169 ERR("fdopendir(%d): %m\n", fd
);
1173 path
[baselen
+ namelen
] = '/';
1174 path
[baselen
+ namelen
+ 1] = '\0';
1175 err
= depmod_modules_search_dir(depmod
, subdir
,
1176 baselen
+ namelen
+ 1,
1180 err
= depmod_modules_search_file(depmod
, baselen
,
1185 path
[baselen
+ namelen
] = '\0';
1186 ERR("failed %s: %s\n", path
, strerror(-err
));
1187 err
= 0; /* ignore errors */
1194 static int depmod_modules_search(struct depmod
*depmod
)
1196 char path
[PATH_MAX
];
1197 DIR *d
= opendir(depmod
->cfg
->dirname
);
1202 ERR("could not open directory %s: %m\n", depmod
->cfg
->dirname
);
1206 baselen
= depmod
->cfg
->dirnamelen
;
1207 memcpy(path
, depmod
->cfg
->dirname
, baselen
);
1208 path
[baselen
] = '/';
1210 path
[baselen
] = '\0';
1212 err
= depmod_modules_search_dir(depmod
, d
, baselen
, path
);
1217 static int mod_cmp(const void *pa
, const void *pb
) {
1218 const struct mod
*a
= *(const struct mod
**)pa
;
1219 const struct mod
*b
= *(const struct mod
**)pb
;
1220 return a
->sort_idx
- b
->sort_idx
;
1223 static int depmod_modules_build_array(struct depmod
*depmod
)
1225 struct hash_iter module_iter
;
1229 hash_iter_init(depmod
->modules_by_name
, &module_iter
);
1230 while (hash_iter_next(&module_iter
, NULL
, &v
)) {
1231 struct mod
*mod
= (struct mod
*) v
;
1232 mod
->idx
= depmod
->modules
.count
;
1233 err
= array_append(&depmod
->modules
, mod
);
1241 static void depmod_modules_sort(struct depmod
*depmod
)
1243 char order_file
[PATH_MAX
], line
[PATH_MAX
];
1245 unsigned idx
= 0, total
= 0;
1247 snprintf(order_file
, sizeof(order_file
), "%s/modules.order",
1248 depmod
->cfg
->dirname
);
1249 fp
= fopen(order_file
, "r");
1251 WRN("could not open %s: %m\n", order_file
);
1255 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
1256 size_t len
= strlen(line
);
1260 if (line
[len
- 1] != '\n') {
1261 ERR("%s:%u corrupted line misses '\\n'\n",
1268 fseek(fp
, 0, SEEK_SET
);
1269 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
1270 size_t len
= strlen(line
);
1276 line
[len
- 1] = '\0';
1278 mod
= hash_find(depmod
->modules_by_uncrelpath
, line
);
1281 mod
->sort_idx
= idx
- total
;
1284 array_sort(&depmod
->modules
, mod_cmp
);
1285 for (idx
= 0; idx
< depmod
->modules
.count
; idx
++) {
1286 struct mod
*m
= depmod
->modules
.array
[idx
];
1294 static int depmod_symbol_add(struct depmod
*depmod
, const char *name
,
1295 bool prefix_skipped
, uint64_t crc
,
1296 const struct mod
*owner
)
1302 if (!prefix_skipped
&& (name
[0] == depmod
->cfg
->sym_prefix
))
1305 namelen
= strlen(name
) + 1;
1306 sym
= malloc(sizeof(struct symbol
) + namelen
);
1310 sym
->owner
= (struct mod
*)owner
;
1312 memcpy(sym
->name
, name
, namelen
);
1314 err
= hash_add(depmod
->symbols
, sym
->name
, sym
);
1320 DBG("add %p sym=%s, owner=%p %s\n", sym
, sym
->name
, owner
,
1321 owner
!= NULL
? owner
->path
: "");
1326 static struct symbol
*depmod_symbol_find(const struct depmod
*depmod
,
1329 if (name
[0] == '.') /* PPC64 needs this: .foo == foo */
1331 if (name
[0] == depmod
->cfg
->sym_prefix
)
1333 return hash_find(depmod
->symbols
, name
);
1336 static int depmod_load_modules(struct depmod
*depmod
)
1338 struct mod
**itr
, **itr_end
;
1340 DBG("load symbols (%zd modules)\n", depmod
->modules
.count
);
1342 itr
= (struct mod
**)depmod
->modules
.array
;
1343 itr_end
= itr
+ depmod
->modules
.count
;
1344 for (; itr
< itr_end
; itr
++) {
1345 struct mod
*mod
= *itr
;
1346 struct kmod_list
*l
, *list
= NULL
;
1347 int err
= kmod_module_get_symbols(mod
->kmod
, &list
);
1350 DBG("ignoring %s: no symbols\n", mod
->path
);
1352 ERR("failed to load symbols from %s: %s\n",
1353 mod
->path
, strerror(-err
));
1356 kmod_list_foreach(l
, list
) {
1357 const char *name
= kmod_module_symbol_get_symbol(l
);
1358 uint64_t crc
= kmod_module_symbol_get_crc(l
);
1359 depmod_symbol_add(depmod
, name
, false, crc
, mod
);
1361 kmod_module_symbols_free_list(list
);
1364 kmod_module_get_info(mod
->kmod
, &mod
->info_list
);
1365 kmod_module_get_dependency_symbols(mod
->kmod
,
1366 &mod
->dep_sym_list
);
1367 kmod_module_unref(mod
->kmod
);
1371 DBG("loaded symbols (%zd modules, %u symbols)\n",
1372 depmod
->modules
.count
, hash_get_count(depmod
->symbols
));
1377 static int depmod_load_module_dependencies(struct depmod
*depmod
, struct mod
*mod
)
1379 const struct cfg
*cfg
= depmod
->cfg
;
1380 struct kmod_list
*l
;
1382 DBG("do dependencies of %s\n", mod
->path
);
1383 kmod_list_foreach(l
, mod
->dep_sym_list
) {
1384 const char *name
= kmod_module_dependency_symbol_get_symbol(l
);
1385 uint64_t crc
= kmod_module_dependency_symbol_get_crc(l
);
1386 int bindtype
= kmod_module_dependency_symbol_get_bind(l
);
1387 struct symbol
*sym
= depmod_symbol_find(depmod
, name
);
1388 uint8_t is_weak
= bindtype
== KMOD_SYMBOL_WEAK
;
1391 DBG("%s needs (%c) unknown symbol %s\n",
1392 mod
->path
, bindtype
, name
);
1393 if (cfg
->print_unknown
&& !is_weak
)
1394 WRN("%s needs unknown symbol %s\n",
1399 if (cfg
->check_symvers
&& sym
->crc
!= crc
&& !is_weak
) {
1400 DBG("symbol %s (%#"PRIx64
") module %s (%#"PRIx64
")\n",
1401 sym
->name
, sym
->crc
, mod
->path
, crc
);
1402 if (cfg
->print_unknown
)
1403 WRN("%s disagrees about version of symbol %s\n",
1407 mod_add_dependency(mod
, sym
);
1413 static int depmod_load_dependencies(struct depmod
*depmod
)
1415 struct mod
**itr
, **itr_end
;
1417 DBG("load dependencies (%zd modules, %u symbols)\n",
1418 depmod
->modules
.count
, hash_get_count(depmod
->symbols
));
1420 itr
= (struct mod
**)depmod
->modules
.array
;
1421 itr_end
= itr
+ depmod
->modules
.count
;
1422 for (; itr
< itr_end
; itr
++) {
1423 struct mod
*mod
= *itr
;
1425 if (mod
->dep_sym_list
== NULL
) {
1426 DBG("ignoring %s: no dependency symbols\n", mod
->path
);
1430 depmod_load_module_dependencies(depmod
, mod
);
1433 DBG("loaded dependencies (%zd modules, %u symbols)\n",
1434 depmod
->modules
.count
, hash_get_count(depmod
->symbols
));
1439 static int dep_cmp(const void *pa
, const void *pb
)
1441 const struct mod
*a
= *(const struct mod
**)pa
;
1442 const struct mod
*b
= *(const struct mod
**)pb
;
1443 return a
->dep_sort_idx
- b
->dep_sort_idx
;
1446 static void depmod_sort_dependencies(struct depmod
*depmod
)
1448 struct mod
**itr
, **itr_end
;
1449 itr
= (struct mod
**)depmod
->modules
.array
;
1450 itr_end
= itr
+ depmod
->modules
.count
;
1451 for (; itr
< itr_end
; itr
++) {
1452 struct mod
*m
= *itr
;
1453 if (m
->deps
.count
> 1)
1454 array_sort(&m
->deps
, dep_cmp
);
1459 struct vertex
*parent
;
1463 static struct vertex
*vertex_new(struct mod
*mod
, struct vertex
*parent
)
1467 v
= malloc(sizeof(*v
));
1476 static void depmod_list_remove_data(struct kmod_list
**list
, void *data
)
1478 struct kmod_list
*l
;
1480 l
= kmod_list_remove_data(*list
, data
);
1484 static void depmod_report_one_cycle(struct depmod
*depmod
,
1485 struct vertex
*vertex
,
1486 struct kmod_list
**roots
,
1487 struct hash
*loop_set
)
1489 const char sep
[] = " -> ";
1492 struct array reverse
;
1497 array_init(&reverse
, 3);
1500 for (v
= vertex
->parent
, n
= 0;
1502 v
= v
->parent
, n
++) {
1504 sz
+= v
->mod
->modnamesz
- 1;
1505 array_append(&reverse
, v
);
1506 hash_add(loop_set
, v
->mod
->modname
, NULL
);
1508 sz
+= vertex
->mod
->modnamesz
- 1;
1510 buf
= malloc(sz
+ n
* strlen(sep
) + 1);
1513 for (i
= reverse
.count
- 1; i
>= 0; i
--) {
1516 v
= reverse
.array
[i
];
1518 len
= v
->mod
->modnamesz
- 1;
1519 memcpy(buf
+ sz
, v
->mod
->modname
, len
);
1521 strcpy(buf
+ sz
, sep
);
1524 depmod_list_remove_data(roots
, v
->mod
);
1526 strcpy(buf
+ sz
, vertex
->mod
->modname
);
1527 ERR("Cycle detected: %s\n", buf
);
1530 array_free_array(&reverse
);
1533 static int depmod_report_cycles_from_root(struct depmod
*depmod
,
1534 struct mod
*root_mod
,
1535 struct kmod_list
**roots
,
1538 struct hash
*loop_set
)
1540 struct kmod_list
*free_list
= NULL
; /* struct vertex */
1541 struct kmod_list
*l
;
1542 struct vertex
*root
;
1543 struct vertex
*vertex
;
1546 struct mod
**itr
, **itr_end
;
1549 root
= vertex_new(root_mod
, NULL
);
1551 ERR("No memory to report cycles\n");
1555 l
= kmod_list_append(free_list
, root
);
1557 ERR("No memory to report cycles\n");
1563 stack
[is
++] = (void *)root
;
1566 vertex
= stack
[--is
];
1569 * because of the topological sort we can start only
1570 * from part of a loop or from a branch after a loop
1572 if (m
->visited
&& m
== root
->mod
) {
1573 depmod_report_one_cycle(depmod
, vertex
,
1579 if (m
->deps
.count
== 0) {
1581 * boundary condition: if there is more than one
1582 * single node branch (not a loop), it is
1583 * recognized as a loop by the code above:
1584 * m->visited because more then one,
1585 * m == root->mod since it is a single node.
1586 * So, prevent deeping into the branch second
1589 depmod_list_remove_data(roots
, m
);
1594 itr
= (struct mod
**) m
->deps
.array
;
1595 itr_end
= itr
+ m
->deps
.count
;
1596 for (; itr
< itr_end
; itr
++) {
1597 struct mod
*dep
= *itr
;
1598 v
= vertex_new(dep
, vertex
);
1600 ERR("No memory to report cycles\n");
1603 assert(is
< stack_size
);
1606 l
= kmod_list_append(free_list
, v
);
1608 ERR("No memory to report cycles\n");
1616 v
= free_list
->data
;
1617 l
= kmod_list_remove(free_list
);
1625 static void depmod_report_cycles(struct depmod
*depmod
, uint16_t n_mods
,
1629 struct kmod_list
*roots
= NULL
; /* struct mod */
1630 struct kmod_list
*l
;
1631 size_t n_r
; /* local n_roots */
1634 _cleanup_free_
void **stack
= NULL
;
1637 struct hash
*loop_set
;
1639 for (i
= 0, n_r
= 0; i
< n_mods
; i
++) {
1642 m
= depmod
->modules
.array
[i
];
1643 l
= kmod_list_append(roots
, m
);
1645 ERR("No memory to report cycles\n");
1652 stack
= malloc(n_r
* sizeof(void *));
1653 if (stack
== NULL
) {
1654 ERR("No memory to report cycles\n");
1658 loop_set
= hash_new(16, NULL
);
1659 if (loop_set
== NULL
) {
1660 ERR("No memory to report cycles\n");
1664 while (roots
!= NULL
) {
1666 l
= kmod_list_remove(roots
);
1668 err
= depmod_report_cycles_from_root(depmod
,
1671 stack
, n_r
, loop_set
);
1676 num_cyclic
= hash_get_count(loop_set
);
1677 ERR("Found %d modules in dependency cycles!\n", num_cyclic
);
1680 hash_free(loop_set
);
1683 static int depmod_calculate_dependencies(struct depmod
*depmod
)
1685 const struct mod
**itrm
;
1686 uint16_t *users
, *roots
, *sorted
;
1687 uint16_t i
, n_roots
= 0, n_sorted
= 0, n_mods
= depmod
->modules
.count
;
1690 users
= malloc(sizeof(uint16_t) * n_mods
* 3);
1693 roots
= users
+ n_mods
;
1694 sorted
= roots
+ n_mods
;
1696 DBG("calculate dependencies and ordering (%hu modules)\n", n_mods
);
1698 assert(depmod
->modules
.count
< UINT16_MAX
);
1700 /* populate modules users (how many modules uses it) */
1701 itrm
= (const struct mod
**)depmod
->modules
.array
;
1702 for (i
= 0; i
< n_mods
; i
++, itrm
++) {
1703 const struct mod
*m
= *itrm
;
1704 users
[i
] = m
->users
;
1705 if (users
[i
] == 0) {
1711 /* topological sort (outputs modules without users first) */
1712 while (n_roots
> 0) {
1713 const struct mod
**itr_dst
, **itr_dst_end
;
1715 uint16_t src_idx
= roots
[--n_roots
];
1717 src
= depmod
->modules
.array
[src_idx
];
1718 src
->dep_sort_idx
= n_sorted
;
1719 sorted
[n_sorted
] = src_idx
;
1722 itr_dst
= (const struct mod
**)src
->deps
.array
;
1723 itr_dst_end
= itr_dst
+ src
->deps
.count
;
1724 for (; itr_dst
< itr_dst_end
; itr_dst
++) {
1725 const struct mod
*dst
= *itr_dst
;
1726 uint16_t dst_idx
= dst
->idx
;
1727 assert(users
[dst_idx
] > 0);
1729 if (users
[dst_idx
] == 0) {
1730 roots
[n_roots
] = dst_idx
;
1736 if (n_sorted
< n_mods
) {
1737 depmod_report_cycles(depmod
, n_mods
, users
);
1742 depmod_sort_dependencies(depmod
);
1744 DBG("calculated dependencies and ordering (%hu modules)\n", n_mods
);
1751 static int depmod_load(struct depmod
*depmod
)
1755 err
= depmod_load_modules(depmod
);
1759 err
= depmod_load_dependencies(depmod
);
1763 err
= depmod_calculate_dependencies(depmod
);
1770 static size_t mod_count_all_dependencies(const struct mod
*mod
)
1772 size_t i
, count
= 0;
1773 for (i
= 0; i
< mod
->deps
.count
; i
++) {
1774 const struct mod
*d
= mod
->deps
.array
[i
];
1775 count
+= 1 + mod_count_all_dependencies(d
);
1780 static int mod_fill_all_unique_dependencies(const struct mod
*mod
, const struct mod
**deps
, size_t n_deps
, size_t *last
)
1784 for (i
= 0; i
< mod
->deps
.count
; i
++) {
1785 const struct mod
*d
= mod
->deps
.array
[i
];
1789 for (j
= 0; j
< *last
; j
++) {
1799 if (*last
>= n_deps
)
1803 err
= mod_fill_all_unique_dependencies(d
, deps
, n_deps
, last
);
1810 static const struct mod
**mod_get_all_sorted_dependencies(const struct mod
*mod
, size_t *n_deps
)
1812 const struct mod
**deps
;
1815 *n_deps
= mod_count_all_dependencies(mod
);
1819 deps
= malloc(sizeof(struct mod
*) * (*n_deps
));
1823 if (mod_fill_all_unique_dependencies(mod
, deps
, *n_deps
, &last
) < 0) {
1828 qsort(deps
, last
, sizeof(struct mod
*), dep_cmp
);
1833 static inline const char *mod_get_compressed_path(const struct mod
*mod
)
1835 if (mod
->relpath
!= NULL
)
1836 return mod
->relpath
;
1840 static int output_deps(struct depmod
*depmod
, FILE *out
)
1844 for (i
= 0; i
< depmod
->modules
.count
; i
++) {
1845 const struct mod
**deps
, *mod
= depmod
->modules
.array
[i
];
1846 const char *p
= mod_get_compressed_path(mod
);
1849 fprintf(out
, "%s:", p
);
1851 if (mod
->deps
.count
== 0)
1854 deps
= mod_get_all_sorted_dependencies(mod
, &n_deps
);
1856 ERR("could not get all sorted dependencies of %s\n", p
);
1860 for (j
= 0; j
< n_deps
; j
++) {
1861 const struct mod
*d
= deps
[j
];
1862 fprintf(out
, " %s", mod_get_compressed_path(d
));
1872 static int output_deps_bin(struct depmod
*depmod
, FILE *out
)
1874 struct index_node
*idx
;
1880 idx
= index_create();
1884 for (i
= 0; i
< depmod
->modules
.count
; i
++) {
1885 const struct mod
**deps
, *mod
= depmod
->modules
.array
[i
];
1886 const char *p
= mod_get_compressed_path(mod
);
1888 size_t j
, n_deps
, linepos
, linelen
, slen
;
1891 deps
= mod_get_all_sorted_dependencies(mod
, &n_deps
);
1892 if (deps
== NULL
&& n_deps
> 0) {
1893 ERR("could not get all sorted dependencies of %s\n", p
);
1897 linelen
= strlen(p
) + 1;
1898 for (j
= 0; j
< n_deps
; j
++) {
1899 const struct mod
*d
= deps
[j
];
1900 linelen
+= 1 + strlen(mod_get_compressed_path(d
));
1903 line
= malloc(linelen
+ 1);
1906 ERR("modules.deps.bin: out of memory\n");
1912 memcpy(line
+ linepos
, p
, slen
);
1914 line
[linepos
] = ':';
1917 for (j
= 0; j
< n_deps
; j
++) {
1918 const struct mod
*d
= deps
[j
];
1921 line
[linepos
] = ' ';
1924 dp
= mod_get_compressed_path(d
);
1926 memcpy(line
+ linepos
, dp
, slen
);
1929 line
[linepos
] = '\0';
1931 duplicate
= index_insert(idx
, mod
->modname
, line
, mod
->idx
);
1932 if (duplicate
&& depmod
->cfg
->warn_dups
)
1933 WRN("duplicate module deps:\n%s\n", line
);
1938 index_write(idx
, out
);
1944 static int output_aliases(struct depmod
*depmod
, FILE *out
)
1948 fputs("# Aliases extracted from modules themselves.\n", out
);
1950 for (i
= 0; i
< depmod
->modules
.count
; i
++) {
1951 const struct mod
*mod
= depmod
->modules
.array
[i
];
1952 struct kmod_list
*l
;
1954 kmod_list_foreach(l
, mod
->info_list
) {
1955 const char *key
= kmod_module_info_get_key(l
);
1956 const char *value
= kmod_module_info_get_value(l
);
1958 if (!streq(key
, "alias"))
1961 fprintf(out
, "alias %s %s\n", value
, mod
->modname
);
1968 static int output_aliases_bin(struct depmod
*depmod
, FILE *out
)
1970 struct index_node
*idx
;
1976 idx
= index_create();
1980 for (i
= 0; i
< depmod
->modules
.count
; i
++) {
1981 const struct mod
*mod
= depmod
->modules
.array
[i
];
1982 struct kmod_list
*l
;
1984 kmod_list_foreach(l
, mod
->info_list
) {
1985 const char *key
= kmod_module_info_get_key(l
);
1986 const char *value
= kmod_module_info_get_value(l
);
1991 if (!streq(key
, "alias"))
1994 if (alias_normalize(value
, buf
, NULL
) < 0) {
1995 WRN("Unmatched bracket in %s\n", value
);
2000 duplicate
= index_insert(idx
, alias
, mod
->modname
,
2002 if (duplicate
&& depmod
->cfg
->warn_dups
)
2003 WRN("duplicate module alias:\n%s %s\n",
2004 alias
, mod
->modname
);
2008 index_write(idx
, out
);
2014 static int output_softdeps(struct depmod
*depmod
, FILE *out
)
2018 fputs("# Soft dependencies extracted from modules themselves.\n", out
);
2020 for (i
= 0; i
< depmod
->modules
.count
; i
++) {
2021 const struct mod
*mod
= depmod
->modules
.array
[i
];
2022 struct kmod_list
*l
;
2024 kmod_list_foreach(l
, mod
->info_list
) {
2025 const char *key
= kmod_module_info_get_key(l
);
2026 const char *value
= kmod_module_info_get_value(l
);
2028 if (!streq(key
, "softdep"))
2031 fprintf(out
, "softdep %s %s\n", mod
->modname
, value
);
2038 static int output_symbols(struct depmod
*depmod
, FILE *out
)
2040 struct hash_iter iter
;
2043 fputs("# Aliases for symbols, used by symbol_request().\n", out
);
2045 hash_iter_init(depmod
->symbols
, &iter
);
2047 while (hash_iter_next(&iter
, NULL
, &v
)) {
2048 const struct symbol
*sym
= v
;
2049 if (sym
->owner
== NULL
)
2052 fprintf(out
, "alias symbol:%s %s\n",
2053 sym
->name
, sym
->owner
->modname
);
2059 static int output_symbols_bin(struct depmod
*depmod
, FILE *out
)
2061 struct index_node
*idx
;
2063 _cleanup_(scratchbuf_release
) struct scratchbuf salias
=
2064 SCRATCHBUF_INITIALIZER(alias
);
2065 size_t baselen
= sizeof("symbol:") - 1;
2066 struct hash_iter iter
;
2073 idx
= index_create();
2077 memcpy(alias
, "symbol:", baselen
);
2079 hash_iter_init(depmod
->symbols
, &iter
);
2081 while (hash_iter_next(&iter
, NULL
, &v
)) {
2083 const struct symbol
*sym
= v
;
2086 if (sym
->owner
== NULL
)
2089 len
= strlen(sym
->name
);
2091 if (scratchbuf_alloc(&salias
, baselen
+ len
+ 1) < 0) {
2093 goto err_scratchbuf
;
2095 memcpy(scratchbuf_str(&salias
) + baselen
, sym
->name
, len
+ 1);
2096 duplicate
= index_insert(idx
, alias
, sym
->owner
->modname
,
2099 if (duplicate
&& depmod
->cfg
->warn_dups
)
2100 WRN("duplicate module syms:\n%s %s\n",
2101 alias
, sym
->owner
->modname
);
2104 index_write(idx
, out
);
2110 ERR("output symbols: %s\n", strerror(-ret
));
2115 static int output_builtin_bin(struct depmod
*depmod
, FILE *out
)
2118 struct index_node
*idx
;
2119 char infile
[PATH_MAX
], line
[PATH_MAX
], modname
[PATH_MAX
];
2124 snprintf(infile
, sizeof(infile
), "%s/modules.builtin",
2125 depmod
->cfg
->dirname
);
2126 in
= fopen(infile
, "r");
2128 WRN("could not open %s: %m\n", infile
);
2132 idx
= index_create();
2138 while (fgets(line
, sizeof(line
), in
) != NULL
) {
2139 if (!isalpha(line
[0])) {
2140 ERR("Invalid modules.builtin line: %s\n", line
);
2144 path_to_modname(line
, modname
, NULL
);
2145 index_insert(idx
, modname
, "", 0);
2148 index_write(idx
, out
);
2155 static int output_devname(struct depmod
*depmod
, FILE *out
)
2160 for (i
= 0; i
< depmod
->modules
.count
; i
++) {
2161 const struct mod
*mod
= depmod
->modules
.array
[i
];
2162 struct kmod_list
*l
;
2163 const char *devname
= NULL
;
2165 unsigned int major
= 0, minor
= 0;
2167 kmod_list_foreach(l
, mod
->info_list
) {
2168 const char *key
= kmod_module_info_get_key(l
);
2169 const char *value
= kmod_module_info_get_value(l
);
2170 unsigned int maj
, min
;
2172 if (!streq(key
, "alias"))
2175 if (strstartswith(value
, "devname:"))
2176 devname
= value
+ sizeof("devname:") - 1;
2177 else if (sscanf(value
, "char-major-%u-%u",
2182 } else if (sscanf(value
, "block-major-%u-%u",
2189 if (type
!= '\0' && devname
!= NULL
)
2193 if (devname
!= NULL
) {
2196 fputs("# Device nodes to trigger on-demand module loading.\n",
2200 fprintf(out
, "%s %s %c%u:%u\n", mod
->modname
,
2201 devname
, type
, major
, minor
);
2203 ERR("Module '%s' has devname (%s) but "
2204 "lacks major and minor information. "
2205 "Ignoring.\n", mod
->modname
, devname
);
2212 static int depmod_output(struct depmod
*depmod
, FILE *out
)
2214 static const struct depfile
{
2216 int (*cb
)(struct depmod
*depmod
, FILE *out
);
2217 } *itr
, depfiles
[] = {
2218 { "modules.dep", output_deps
},
2219 { "modules.dep.bin", output_deps_bin
},
2220 { "modules.alias", output_aliases
},
2221 { "modules.alias.bin", output_aliases_bin
},
2222 { "modules.softdep", output_softdeps
},
2223 { "modules.symbols", output_symbols
},
2224 { "modules.symbols.bin", output_symbols_bin
},
2225 { "modules.builtin.bin", output_builtin_bin
},
2226 { "modules.devname", output_devname
},
2229 const char *dname
= depmod
->cfg
->dirname
;
2235 dfd
= open(dname
, O_RDONLY
);
2238 CRIT("could not open directory %s: %m\n", dname
);
2243 for (itr
= depfiles
; itr
->name
!= NULL
; itr
++) {
2245 char tmp
[NAME_MAX
] = "";
2249 int flags
= O_CREAT
| O_TRUNC
| O_WRONLY
;
2253 snprintf(tmp
, sizeof(tmp
), "%s.tmp", itr
->name
);
2254 fd
= openat(dfd
, tmp
, flags
, mode
);
2256 ERR("openat(%s, %s, %o, %o): %m\n",
2257 dname
, tmp
, flags
, mode
);
2260 fp
= fdopen(fd
, "wb");
2262 ERR("fdopen(%d=%s/%s): %m\n", fd
, dname
, tmp
);
2268 r
= itr
->cb(depmod
, fp
);
2272 ferr
= ferror(fp
) | fclose(fp
);
2275 if (unlinkat(dfd
, tmp
, 0) != 0)
2276 ERR("unlinkat(%s, %s): %m\n", dname
, tmp
);
2278 ERR("Could not write index '%s': %s\n", itr
->name
,
2284 unlinkat(dfd
, itr
->name
, 0);
2285 if (renameat(dfd
, tmp
, dfd
, itr
->name
) != 0) {
2287 CRIT("renameat(%s, %s, %s, %s): %m\n",
2288 dname
, tmp
, dname
, itr
->name
);
2294 ERR("Could not create index '%s'. Output is truncated: %s\n",
2295 itr
->name
, strerror(-err
));
2306 static void depmod_add_fake_syms(struct depmod
*depmod
)
2308 /* __this_module is magic inserted by kernel loader. */
2309 depmod_symbol_add(depmod
, "__this_module", true, 0, NULL
);
2310 /* On S390, this is faked up too */
2311 depmod_symbol_add(depmod
, "_GLOBAL_OFFSET_TABLE_", true, 0, NULL
);
2312 /* On PowerPC64 ABIv2, .TOC. is more or less _GLOBAL_OFFSET_TABLE_ */
2313 depmod_symbol_add(depmod
, "TOC.", true, 0, NULL
);
2316 static int depmod_load_symvers(struct depmod
*depmod
, const char *filename
)
2320 unsigned int linenum
= 0;
2322 fp
= fopen(filename
, "r");
2325 DBG("load symvers: %s: %m\n", filename
);
2328 DBG("load symvers: %s\n", filename
);
2330 /* eg. "0xb352177e\tfind_first_bit\tvmlinux\tEXPORT_SYMBOL" */
2331 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
2332 const char *ver
, *sym
, *where
;
2338 ver
= strtok(line
, " \t");
2339 sym
= strtok(NULL
, " \t");
2340 where
= strtok(NULL
, " \t");
2341 if (!ver
|| !sym
|| !where
)
2344 if (!streq(where
, "vmlinux"))
2347 crc
= strtoull(ver
, &verend
, 16);
2348 if (verend
[0] != '\0') {
2349 ERR("%s:%u Invalid symbol version %s: %m\n",
2350 filename
, linenum
, ver
);
2354 depmod_symbol_add(depmod
, sym
, false, crc
, NULL
);
2356 depmod_add_fake_syms(depmod
);
2358 DBG("loaded symvers: %s\n", filename
);
2364 static int depmod_load_system_map(struct depmod
*depmod
, const char *filename
)
2366 const char ksymstr
[] = "__ksymtab_";
2367 const size_t ksymstr_len
= sizeof(ksymstr
) - 1;
2370 unsigned int linenum
= 0;
2372 fp
= fopen(filename
, "r");
2375 DBG("load System.map: %s: %m\n", filename
);
2378 DBG("load System.map: %s\n", filename
);
2380 /* eg. c0294200 R __ksymtab_devfs_alloc_devnum */
2381 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
2386 p
= strchr(line
, ' ');
2388 goto invalid_syntax
;
2392 goto invalid_syntax
;
2396 if (p
[0] == depmod
->cfg
->sym_prefix
)
2399 /* Covers gpl-only and normal symbols. */
2400 if (strncmp(p
, ksymstr
, ksymstr_len
) != 0)
2403 end
= strchr(p
, '\n');
2407 depmod_symbol_add(depmod
, p
+ ksymstr_len
, true, 0, NULL
);
2411 ERR("%s:%u: invalid line: %s\n", filename
, linenum
, line
);
2413 depmod_add_fake_syms(depmod
);
2415 DBG("loaded System.map: %s\n", filename
);
2422 static int depfile_up_to_date_dir(DIR *d
, time_t mtime
, size_t baselen
, char *path
)
2425 int err
= 1, dfd
= dirfd(d
);
2427 while ((de
= readdir(d
)) != NULL
) {
2428 const char *name
= de
->d_name
;
2432 if (name
[0] == '.' && (name
[1] == '\0' ||
2433 (name
[1] == '.' && name
[2] == '\0')))
2435 if (streq(name
, "build") || streq(name
, "source"))
2437 namelen
= strlen(name
);
2438 if (baselen
+ namelen
+ 2 >= PATH_MAX
) {
2439 path
[baselen
] = '\0';
2440 ERR("path is too long %s%s\n", path
, name
);
2444 if (fstatat(dfd
, name
, &st
, 0) < 0) {
2445 ERR("fstatat(%d, %s): %m\n", dfd
, name
);
2449 if (S_ISDIR(st
.st_mode
)) {
2452 memcpy(path
+ baselen
, name
, namelen
+ 1);
2453 if (baselen
+ namelen
+ 2 + NAME_MAX
>= PATH_MAX
) {
2454 ERR("directory path is too long %s\n", path
);
2457 fd
= openat(dfd
, name
, O_RDONLY
);
2459 ERR("openat(%d, %s, O_RDONLY): %m\n",
2463 subdir
= fdopendir(fd
);
2464 if (subdir
== NULL
) {
2465 ERR("fdopendir(%d): %m\n", fd
);
2469 path
[baselen
+ namelen
] = '/';
2470 path
[baselen
+ namelen
+ 1] = '\0';
2471 err
= depfile_up_to_date_dir(subdir
, mtime
,
2472 baselen
+ namelen
+ 1,
2475 } else if (S_ISREG(st
.st_mode
)) {
2476 if (!path_ends_with_kmod_ext(name
, namelen
))
2479 memcpy(path
+ baselen
, name
, namelen
+ 1);
2480 err
= st
.st_mtime
<= mtime
;
2482 DBG("%s %"PRIu64
" is newer than %"PRIu64
"\n",
2483 path
, (uint64_t)st
.st_mtime
,
2487 ERR("unsupported file type %s: %o\n",
2488 path
, st
.st_mode
& S_IFMT
);
2493 break; /* outdated! */
2495 path
[baselen
+ namelen
] = '\0';
2496 ERR("failed %s: %s\n", path
, strerror(-err
));
2497 err
= 1; /* ignore errors */
2504 /* uptodate: 1, outdated: 0, errors < 0 */
2505 static int depfile_up_to_date(const char *dirname
)
2507 char path
[PATH_MAX
];
2508 DIR *d
= opendir(dirname
);
2514 ERR("could not open directory %s: %m\n", dirname
);
2518 if (fstatat(dirfd(d
), "modules.dep", &st
, 0) != 0) {
2520 ERR("could not fstatat(%s, modules.dep): %m\n", dirname
);
2525 baselen
= strlen(dirname
);
2526 memcpy(path
, dirname
, baselen
);
2527 path
[baselen
] = '/';
2529 path
[baselen
] = '\0';
2531 err
= depfile_up_to_date_dir(d
, st
.st_mtime
, baselen
, path
);
2536 static int is_version_number(const char *version
)
2538 unsigned int d1
, d2
;
2539 return (sscanf(version
, "%u.%u", &d1
, &d2
) == 2);
2542 static int do_depmod(int argc
, char *argv
[])
2545 int err
= 0, all
= 0, maybe_all
= 0, n_config_paths
= 0;
2546 _cleanup_free_
char *root
= NULL
;
2547 _cleanup_free_
const char **config_paths
= NULL
;
2548 const char *system_map
= NULL
;
2549 const char *module_symvers
= NULL
;
2550 const char *null_kmod_config
= NULL
;
2552 struct kmod_ctx
*ctx
= NULL
;
2554 struct depmod depmod
;
2556 memset(&cfg
, 0, sizeof(cfg
));
2557 memset(&depmod
, 0, sizeof(depmod
));
2561 c
= getopt_long(argc
, argv
, cmdopts_s
, cmdopts
, &idx
);
2574 root
= path_make_absolute_cwd(optarg
);
2577 size_t bytes
= sizeof(char *) * (n_config_paths
+ 2);
2578 void *tmp
= realloc(config_paths
, bytes
);
2580 fputs("Error: out-of-memory\n", stderr
);
2581 goto cmdline_failed
;
2584 config_paths
[n_config_paths
] = optarg
;
2586 config_paths
[n_config_paths
] = NULL
;
2590 module_symvers
= optarg
;
2591 cfg
.check_symvers
= 1;
2594 system_map
= optarg
;
2597 cfg
.print_unknown
= 1;
2606 if (optarg
[1] != '\0') {
2607 CRIT("-P only takes a single char\n");
2608 goto cmdline_failed
;
2610 cfg
.sym_prefix
= optarg
[0];
2620 WRN("Ignored deprecated option --%s\n",
2623 WRN("Ignored deprecated option -%c\n", c
);
2628 return EXIT_SUCCESS
;
2630 puts(PACKAGE
" version " VERSION
);
2631 puts(KMOD_FEATURES
);
2632 return EXIT_SUCCESS
;
2634 goto cmdline_failed
;
2636 ERR("unexpected getopt_long() value '%c'.\n", c
);
2637 goto cmdline_failed
;
2641 if (optind
< argc
) {
2642 if (!is_version_number(argv
[optind
])) {
2643 ERR("Bad version passed %s\n", argv
[optind
]);
2644 goto cmdline_failed
;
2646 cfg
.kversion
= argv
[optind
];
2649 if (uname(&un
) < 0) {
2650 CRIT("uname() failed: %s\n", strerror(errno
));
2651 goto cmdline_failed
;
2653 cfg
.kversion
= un
.release
;
2656 cfg
.dirnamelen
= snprintf(cfg
.dirname
, PATH_MAX
,
2657 "%s/lib/modules/%s",
2658 root
== NULL
? "" : root
, cfg
.kversion
);
2666 /* ignore up-to-date errors (< 0) */
2667 if (depfile_up_to_date(cfg
.dirname
) == 1)
2672 ctx
= kmod_new(cfg
.dirname
, &null_kmod_config
);
2674 CRIT("kmod_new(\"%s\", {NULL}) failed: %m\n", cfg
.dirname
);
2675 goto cmdline_failed
;
2678 log_setup_kmod_log(ctx
, verbose
);
2680 err
= depmod_init(&depmod
, &cfg
, ctx
);
2682 CRIT("depmod_init: %s\n", strerror(-err
));
2683 goto depmod_init_failed
;
2685 ctx
= NULL
; /* owned by depmod */
2687 if (module_symvers
!= NULL
) {
2688 err
= depmod_load_symvers(&depmod
, module_symvers
);
2690 CRIT("could not load %s: %s\n", module_symvers
,
2692 goto cmdline_failed
;
2694 } else if (system_map
!= NULL
) {
2695 err
= depmod_load_system_map(&depmod
, system_map
);
2697 CRIT("could not load %s: %s\n", system_map
,
2699 goto cmdline_failed
;
2701 } else if (cfg
.print_unknown
) {
2702 WRN("-e needs -E or -F\n");
2703 cfg
.print_unknown
= 0;
2707 err
= cfg_load(&cfg
, config_paths
);
2709 CRIT("could not load configuration files\n");
2710 goto cmdline_modules_failed
;
2712 err
= depmod_modules_search(&depmod
);
2714 CRIT("could not search modules: %s\n", strerror(-err
));
2715 goto cmdline_modules_failed
;
2720 for (i
= optind
; i
< argc
; i
++) {
2721 const char *path
= argv
[i
];
2722 struct kmod_module
*mod
;
2724 if (path
[0] != '/') {
2725 CRIT("%s: not absolute path.\n", path
);
2726 goto cmdline_modules_failed
;
2729 err
= kmod_module_new_from_path(depmod
.ctx
, path
, &mod
);
2731 CRIT("could not create module %s: %s\n",
2732 path
, strerror(-err
));
2733 goto cmdline_modules_failed
;
2736 err
= depmod_module_add(&depmod
, mod
);
2738 CRIT("could not add module %s: %s\n",
2739 path
, strerror(-err
));
2740 kmod_module_unref(mod
);
2741 goto cmdline_modules_failed
;
2746 err
= depmod_modules_build_array(&depmod
);
2748 CRIT("could not build module array: %s\n",
2750 goto cmdline_modules_failed
;
2753 depmod_modules_sort(&depmod
);
2754 err
= depmod_load(&depmod
);
2756 goto cmdline_modules_failed
;
2758 err
= depmod_output(&depmod
, out
);
2761 depmod_shutdown(&depmod
);
2763 return err
>= 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
2765 cmdline_modules_failed
:
2766 depmod_shutdown(&depmod
);
2772 return EXIT_FAILURE
;
2775 const struct kmod_cmd kmod_cmd_compat_depmod
= {
2778 .help
= "compat depmod command",