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
)
460 struct cfg_search
*s
;
462 uint8_t builtin
= streq(path
, CFG_BUILTIN_KEY
);
467 len
= strlen(path
) + 1;
469 s
= malloc(sizeof(struct cfg_search
) + len
);
471 ERR("search add: out of memory\n");
474 s
->builtin
= builtin
;
479 memcpy(s
->path
, path
, len
);
482 DBG("search add: %s, builtin=%hhu\n", path
, builtin
);
484 s
->next
= cfg
->searches
;
489 static void cfg_search_free(struct cfg_search
*s
)
494 static int cfg_override_add(struct cfg
*cfg
, const char *modname
, const char *subdir
)
496 struct cfg_override
*o
;
497 size_t modnamelen
= strlen(modname
);
498 size_t subdirlen
= strlen(subdir
);
501 o
= malloc(sizeof(struct cfg_override
) + subdirlen
+ 1
504 ERR("override add: out of memory\n");
507 memcpy(o
->path
, subdir
, subdirlen
);
512 memcpy(o
->path
+ i
, modname
, modnamelen
);
514 o
->path
[i
] = '\0'; /* no extension, so we can match .ko/.ko.gz */
518 DBG("override add: %s\n", o
->path
);
520 o
->next
= cfg
->overrides
;
525 static void cfg_override_free(struct cfg_override
*o
)
530 static int cfg_kernel_matches(const struct cfg
*cfg
, const char *pattern
)
536 if (streq(pattern
, "*"))
539 if (regcomp(&re
, pattern
, REG_EXTENDED
|REG_NOSUB
) != 0)
542 status
= regexec(&re
, cfg
->kversion
, 0, NULL
, 0);
548 static int cfg_file_parse(struct cfg
*cfg
, const char *filename
)
552 unsigned int linenum
= 0;
555 fp
= fopen(filename
, "r");
558 ERR("file parse %s: %m\n", filename
);
562 while ((line
= freadline_wrapped(fp
, &linenum
)) != NULL
) {
565 if (line
[0] == '\0' || line
[0] == '#')
568 cmd
= strtok_r(line
, "\t ", &saveptr
);
572 if (streq(cmd
, "search")) {
574 while ((sp
= strtok_r(NULL
, "\t ", &saveptr
)) != NULL
) {
575 cfg_search_add(cfg
, sp
);
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");
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_path(struct depmod
*depmod
,
1197 char path_buf
[PATH_MAX
];
1205 ERR("could not open directory %s: %m\n", path
);
1209 baselen
= strlen(path
);
1210 memcpy(path_buf
, path
, baselen
);
1211 path_buf
[baselen
] = '/';
1213 path_buf
[baselen
] = '\0';
1215 err
= depmod_modules_search_dir(depmod
, d
, baselen
, path_buf
);
1220 static int depmod_modules_search(struct depmod
*depmod
)
1224 err
= depmod_modules_search_path(depmod
, depmod
->cfg
->dirname
);
1230 static int mod_cmp(const void *pa
, const void *pb
) {
1231 const struct mod
*a
= *(const struct mod
**)pa
;
1232 const struct mod
*b
= *(const struct mod
**)pb
;
1233 return a
->sort_idx
- b
->sort_idx
;
1236 static int depmod_modules_build_array(struct depmod
*depmod
)
1238 struct hash_iter module_iter
;
1242 hash_iter_init(depmod
->modules_by_name
, &module_iter
);
1243 while (hash_iter_next(&module_iter
, NULL
, &v
)) {
1244 struct mod
*mod
= (struct mod
*) v
;
1245 mod
->idx
= depmod
->modules
.count
;
1246 err
= array_append(&depmod
->modules
, mod
);
1254 static void depmod_modules_sort(struct depmod
*depmod
)
1256 char order_file
[PATH_MAX
], line
[PATH_MAX
];
1258 unsigned idx
= 0, total
= 0;
1260 snprintf(order_file
, sizeof(order_file
), "%s/modules.order",
1261 depmod
->cfg
->dirname
);
1262 fp
= fopen(order_file
, "r");
1264 WRN("could not open %s: %m\n", order_file
);
1268 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
1269 size_t len
= strlen(line
);
1273 if (line
[len
- 1] != '\n') {
1274 ERR("%s:%u corrupted line misses '\\n'\n",
1281 fseek(fp
, 0, SEEK_SET
);
1282 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
1283 size_t len
= strlen(line
);
1289 line
[len
- 1] = '\0';
1291 mod
= hash_find(depmod
->modules_by_uncrelpath
, line
);
1294 mod
->sort_idx
= idx
- total
;
1297 array_sort(&depmod
->modules
, mod_cmp
);
1298 for (idx
= 0; idx
< depmod
->modules
.count
; idx
++) {
1299 struct mod
*m
= depmod
->modules
.array
[idx
];
1307 static int depmod_symbol_add(struct depmod
*depmod
, const char *name
,
1308 bool prefix_skipped
, uint64_t crc
,
1309 const struct mod
*owner
)
1315 if (!prefix_skipped
&& (name
[0] == depmod
->cfg
->sym_prefix
))
1318 namelen
= strlen(name
) + 1;
1319 sym
= malloc(sizeof(struct symbol
) + namelen
);
1323 sym
->owner
= (struct mod
*)owner
;
1325 memcpy(sym
->name
, name
, namelen
);
1327 err
= hash_add(depmod
->symbols
, sym
->name
, sym
);
1333 DBG("add %p sym=%s, owner=%p %s\n", sym
, sym
->name
, owner
,
1334 owner
!= NULL
? owner
->path
: "");
1339 static struct symbol
*depmod_symbol_find(const struct depmod
*depmod
,
1342 if (name
[0] == '.') /* PPC64 needs this: .foo == foo */
1344 if (name
[0] == depmod
->cfg
->sym_prefix
)
1346 return hash_find(depmod
->symbols
, name
);
1349 static int depmod_load_modules(struct depmod
*depmod
)
1351 struct mod
**itr
, **itr_end
;
1353 DBG("load symbols (%zd modules)\n", depmod
->modules
.count
);
1355 itr
= (struct mod
**)depmod
->modules
.array
;
1356 itr_end
= itr
+ depmod
->modules
.count
;
1357 for (; itr
< itr_end
; itr
++) {
1358 struct mod
*mod
= *itr
;
1359 struct kmod_list
*l
, *list
= NULL
;
1360 int err
= kmod_module_get_symbols(mod
->kmod
, &list
);
1363 DBG("ignoring %s: no symbols\n", mod
->path
);
1365 ERR("failed to load symbols from %s: %s\n",
1366 mod
->path
, strerror(-err
));
1369 kmod_list_foreach(l
, list
) {
1370 const char *name
= kmod_module_symbol_get_symbol(l
);
1371 uint64_t crc
= kmod_module_symbol_get_crc(l
);
1372 depmod_symbol_add(depmod
, name
, false, crc
, mod
);
1374 kmod_module_symbols_free_list(list
);
1377 kmod_module_get_info(mod
->kmod
, &mod
->info_list
);
1378 kmod_module_get_dependency_symbols(mod
->kmod
,
1379 &mod
->dep_sym_list
);
1380 kmod_module_unref(mod
->kmod
);
1384 DBG("loaded symbols (%zd modules, %u symbols)\n",
1385 depmod
->modules
.count
, hash_get_count(depmod
->symbols
));
1390 static int depmod_load_module_dependencies(struct depmod
*depmod
, struct mod
*mod
)
1392 const struct cfg
*cfg
= depmod
->cfg
;
1393 struct kmod_list
*l
;
1395 DBG("do dependencies of %s\n", mod
->path
);
1396 kmod_list_foreach(l
, mod
->dep_sym_list
) {
1397 const char *name
= kmod_module_dependency_symbol_get_symbol(l
);
1398 uint64_t crc
= kmod_module_dependency_symbol_get_crc(l
);
1399 int bindtype
= kmod_module_dependency_symbol_get_bind(l
);
1400 struct symbol
*sym
= depmod_symbol_find(depmod
, name
);
1401 uint8_t is_weak
= bindtype
== KMOD_SYMBOL_WEAK
;
1404 DBG("%s needs (%c) unknown symbol %s\n",
1405 mod
->path
, bindtype
, name
);
1406 if (cfg
->print_unknown
&& !is_weak
)
1407 WRN("%s needs unknown symbol %s\n",
1412 if (cfg
->check_symvers
&& sym
->crc
!= crc
&& !is_weak
) {
1413 DBG("symbol %s (%#"PRIx64
") module %s (%#"PRIx64
")\n",
1414 sym
->name
, sym
->crc
, mod
->path
, crc
);
1415 if (cfg
->print_unknown
)
1416 WRN("%s disagrees about version of symbol %s\n",
1420 mod_add_dependency(mod
, sym
);
1426 static int depmod_load_dependencies(struct depmod
*depmod
)
1428 struct mod
**itr
, **itr_end
;
1430 DBG("load dependencies (%zd modules, %u symbols)\n",
1431 depmod
->modules
.count
, hash_get_count(depmod
->symbols
));
1433 itr
= (struct mod
**)depmod
->modules
.array
;
1434 itr_end
= itr
+ depmod
->modules
.count
;
1435 for (; itr
< itr_end
; itr
++) {
1436 struct mod
*mod
= *itr
;
1438 if (mod
->dep_sym_list
== NULL
) {
1439 DBG("ignoring %s: no dependency symbols\n", mod
->path
);
1443 depmod_load_module_dependencies(depmod
, mod
);
1446 DBG("loaded dependencies (%zd modules, %u symbols)\n",
1447 depmod
->modules
.count
, hash_get_count(depmod
->symbols
));
1452 static int dep_cmp(const void *pa
, const void *pb
)
1454 const struct mod
*a
= *(const struct mod
**)pa
;
1455 const struct mod
*b
= *(const struct mod
**)pb
;
1456 return a
->dep_sort_idx
- b
->dep_sort_idx
;
1459 static void depmod_sort_dependencies(struct depmod
*depmod
)
1461 struct mod
**itr
, **itr_end
;
1462 itr
= (struct mod
**)depmod
->modules
.array
;
1463 itr_end
= itr
+ depmod
->modules
.count
;
1464 for (; itr
< itr_end
; itr
++) {
1465 struct mod
*m
= *itr
;
1466 if (m
->deps
.count
> 1)
1467 array_sort(&m
->deps
, dep_cmp
);
1472 struct vertex
*parent
;
1476 static struct vertex
*vertex_new(struct mod
*mod
, struct vertex
*parent
)
1480 v
= malloc(sizeof(*v
));
1489 static void depmod_list_remove_data(struct kmod_list
**list
, void *data
)
1491 struct kmod_list
*l
;
1493 l
= kmod_list_remove_data(*list
, data
);
1497 static int depmod_report_one_cycle(struct depmod
*depmod
,
1498 struct vertex
*vertex
,
1499 struct kmod_list
**roots
,
1500 struct hash
*loop_set
)
1502 const char sep
[] = " -> ";
1505 struct array reverse
;
1511 array_init(&reverse
, 3);
1514 for (v
= vertex
->parent
, n
= 0;
1516 v
= v
->parent
, n
++) {
1518 sz
+= v
->mod
->modnamesz
- 1;
1519 array_append(&reverse
, v
);
1520 rc
= hash_add(loop_set
, v
->mod
->modname
, NULL
);
1523 /* the hash will be freed where created */
1525 sz
+= vertex
->mod
->modnamesz
- 1;
1527 buf
= malloc(sz
+ n
* strlen(sep
) + 1);
1530 for (i
= reverse
.count
- 1; i
>= 0; i
--) {
1533 v
= reverse
.array
[i
];
1535 len
= v
->mod
->modnamesz
- 1;
1536 memcpy(buf
+ sz
, v
->mod
->modname
, len
);
1538 strcpy(buf
+ sz
, sep
);
1541 depmod_list_remove_data(roots
, v
->mod
);
1543 strcpy(buf
+ sz
, vertex
->mod
->modname
);
1544 ERR("Cycle detected: %s\n", buf
);
1547 array_free_array(&reverse
);
1552 static int depmod_report_cycles_from_root(struct depmod
*depmod
,
1553 struct mod
*root_mod
,
1554 struct kmod_list
**roots
,
1557 struct hash
*loop_set
)
1559 struct kmod_list
*free_list
= NULL
; /* struct vertex */
1560 struct kmod_list
*l
;
1561 struct vertex
*root
;
1562 struct vertex
*vertex
;
1565 struct mod
**itr
, **itr_end
;
1569 root
= vertex_new(root_mod
, NULL
);
1571 ERR("No memory to report cycles\n");
1575 l
= kmod_list_append(free_list
, root
);
1577 ERR("No memory to report cycles\n");
1583 stack
[is
++] = (void *)root
;
1586 vertex
= stack
[--is
];
1589 * because of the topological sort we can start only
1590 * from part of a loop or from a branch after a loop
1592 if (m
->visited
&& m
== root
->mod
) {
1594 rc
= depmod_report_one_cycle(depmod
, vertex
,
1604 if (m
->deps
.count
== 0) {
1606 * boundary condition: if there is more than one
1607 * single node branch (not a loop), it is
1608 * recognized as a loop by the code above:
1609 * m->visited because more then one,
1610 * m == root->mod since it is a single node.
1611 * So, prevent deeping into the branch second
1614 depmod_list_remove_data(roots
, m
);
1619 itr
= (struct mod
**) m
->deps
.array
;
1620 itr_end
= itr
+ m
->deps
.count
;
1621 for (; itr
< itr_end
; itr
++) {
1622 struct mod
*dep
= *itr
;
1623 v
= vertex_new(dep
, vertex
);
1625 ERR("No memory to report cycles\n");
1628 assert(is
< stack_size
);
1631 l
= kmod_list_append(free_list
, v
);
1633 ERR("No memory to report cycles\n");
1644 v
= free_list
->data
;
1645 l
= kmod_list_remove(free_list
);
1653 static void depmod_report_cycles(struct depmod
*depmod
, uint16_t n_mods
,
1657 struct kmod_list
*roots
= NULL
; /* struct mod */
1658 struct kmod_list
*l
;
1659 size_t n_r
; /* local n_roots */
1662 _cleanup_free_
void **stack
= NULL
;
1665 struct hash
*loop_set
;
1667 for (i
= 0, n_r
= 0; i
< n_mods
; i
++) {
1670 m
= depmod
->modules
.array
[i
];
1671 l
= kmod_list_append(roots
, m
);
1673 ERR("No memory to report cycles\n");
1680 stack
= malloc(n_r
* sizeof(void *));
1681 if (stack
== NULL
) {
1682 ERR("No memory to report cycles\n");
1686 loop_set
= hash_new(16, NULL
);
1687 if (loop_set
== NULL
) {
1688 ERR("No memory to report cycles\n");
1692 while (roots
!= NULL
) {
1694 l
= kmod_list_remove(roots
);
1696 err
= depmod_report_cycles_from_root(depmod
,
1699 stack
, n_r
, loop_set
);
1704 num_cyclic
= hash_get_count(loop_set
);
1705 ERR("Found %d modules in dependency cycles!\n", num_cyclic
);
1708 hash_free(loop_set
);
1710 while (roots
!= NULL
) {
1711 /* no need to free data, come from outside */
1712 roots
= kmod_list_remove(roots
);
1716 static int depmod_calculate_dependencies(struct depmod
*depmod
)
1718 const struct mod
**itrm
;
1719 uint16_t *users
, *roots
, *sorted
;
1720 uint16_t i
, n_roots
= 0, n_sorted
= 0, n_mods
= depmod
->modules
.count
;
1723 users
= malloc(sizeof(uint16_t) * n_mods
* 3);
1726 roots
= users
+ n_mods
;
1727 sorted
= roots
+ n_mods
;
1729 DBG("calculate dependencies and ordering (%hu modules)\n", n_mods
);
1731 assert(depmod
->modules
.count
< UINT16_MAX
);
1733 /* populate modules users (how many modules uses it) */
1734 itrm
= (const struct mod
**)depmod
->modules
.array
;
1735 for (i
= 0; i
< n_mods
; i
++, itrm
++) {
1736 const struct mod
*m
= *itrm
;
1737 users
[i
] = m
->users
;
1738 if (users
[i
] == 0) {
1744 /* topological sort (outputs modules without users first) */
1745 while (n_roots
> 0) {
1746 const struct mod
**itr_dst
, **itr_dst_end
;
1748 uint16_t src_idx
= roots
[--n_roots
];
1750 src
= depmod
->modules
.array
[src_idx
];
1751 src
->dep_sort_idx
= n_sorted
;
1752 sorted
[n_sorted
] = src_idx
;
1755 itr_dst
= (const struct mod
**)src
->deps
.array
;
1756 itr_dst_end
= itr_dst
+ src
->deps
.count
;
1757 for (; itr_dst
< itr_dst_end
; itr_dst
++) {
1758 const struct mod
*dst
= *itr_dst
;
1759 uint16_t dst_idx
= dst
->idx
;
1760 assert(users
[dst_idx
] > 0);
1762 if (users
[dst_idx
] == 0) {
1763 roots
[n_roots
] = dst_idx
;
1769 if (n_sorted
< n_mods
) {
1770 depmod_report_cycles(depmod
, n_mods
, users
);
1775 depmod_sort_dependencies(depmod
);
1777 DBG("calculated dependencies and ordering (%hu modules)\n", n_mods
);
1784 static int depmod_load(struct depmod
*depmod
)
1788 err
= depmod_load_modules(depmod
);
1792 err
= depmod_load_dependencies(depmod
);
1796 err
= depmod_calculate_dependencies(depmod
);
1803 static size_t mod_count_all_dependencies(const struct mod
*mod
)
1805 size_t i
, count
= 0;
1806 for (i
= 0; i
< mod
->deps
.count
; i
++) {
1807 const struct mod
*d
= mod
->deps
.array
[i
];
1808 count
+= 1 + mod_count_all_dependencies(d
);
1813 static int mod_fill_all_unique_dependencies(const struct mod
*mod
, const struct mod
**deps
, size_t n_deps
, size_t *last
)
1817 for (i
= 0; i
< mod
->deps
.count
; i
++) {
1818 const struct mod
*d
= mod
->deps
.array
[i
];
1822 for (j
= 0; j
< *last
; j
++) {
1832 if (*last
>= n_deps
)
1836 err
= mod_fill_all_unique_dependencies(d
, deps
, n_deps
, last
);
1843 static const struct mod
**mod_get_all_sorted_dependencies(const struct mod
*mod
, size_t *n_deps
)
1845 const struct mod
**deps
;
1848 *n_deps
= mod_count_all_dependencies(mod
);
1852 deps
= malloc(sizeof(struct mod
*) * (*n_deps
));
1856 if (mod_fill_all_unique_dependencies(mod
, deps
, *n_deps
, &last
) < 0) {
1861 qsort(deps
, last
, sizeof(struct mod
*), dep_cmp
);
1866 static inline const char *mod_get_compressed_path(const struct mod
*mod
)
1868 if (mod
->relpath
!= NULL
)
1869 return mod
->relpath
;
1873 static int output_deps(struct depmod
*depmod
, FILE *out
)
1877 for (i
= 0; i
< depmod
->modules
.count
; i
++) {
1878 const struct mod
**deps
, *mod
= depmod
->modules
.array
[i
];
1879 const char *p
= mod_get_compressed_path(mod
);
1882 fprintf(out
, "%s:", p
);
1884 if (mod
->deps
.count
== 0)
1887 deps
= mod_get_all_sorted_dependencies(mod
, &n_deps
);
1889 ERR("could not get all sorted dependencies of %s\n", p
);
1893 for (j
= 0; j
< n_deps
; j
++) {
1894 const struct mod
*d
= deps
[j
];
1895 fprintf(out
, " %s", mod_get_compressed_path(d
));
1905 static int output_deps_bin(struct depmod
*depmod
, FILE *out
)
1907 struct index_node
*idx
;
1913 idx
= index_create();
1917 for (i
= 0; i
< depmod
->modules
.count
; i
++) {
1918 const struct mod
**deps
, *mod
= depmod
->modules
.array
[i
];
1919 const char *p
= mod_get_compressed_path(mod
);
1921 size_t j
, n_deps
, linepos
, linelen
, slen
;
1924 deps
= mod_get_all_sorted_dependencies(mod
, &n_deps
);
1925 if (deps
== NULL
&& n_deps
> 0) {
1926 ERR("could not get all sorted dependencies of %s\n", p
);
1930 linelen
= strlen(p
) + 1;
1931 for (j
= 0; j
< n_deps
; j
++) {
1932 const struct mod
*d
= deps
[j
];
1933 linelen
+= 1 + strlen(mod_get_compressed_path(d
));
1936 line
= malloc(linelen
+ 1);
1939 ERR("modules.deps.bin: out of memory\n");
1945 memcpy(line
+ linepos
, p
, slen
);
1947 line
[linepos
] = ':';
1950 for (j
= 0; j
< n_deps
; j
++) {
1951 const struct mod
*d
= deps
[j
];
1954 line
[linepos
] = ' ';
1957 dp
= mod_get_compressed_path(d
);
1959 memcpy(line
+ linepos
, dp
, slen
);
1962 line
[linepos
] = '\0';
1964 duplicate
= index_insert(idx
, mod
->modname
, line
, mod
->idx
);
1965 if (duplicate
&& depmod
->cfg
->warn_dups
)
1966 WRN("duplicate module deps:\n%s\n", line
);
1971 index_write(idx
, out
);
1977 static int output_aliases(struct depmod
*depmod
, FILE *out
)
1981 fputs("# Aliases extracted from modules themselves.\n", out
);
1983 for (i
= 0; i
< depmod
->modules
.count
; i
++) {
1984 const struct mod
*mod
= depmod
->modules
.array
[i
];
1985 struct kmod_list
*l
;
1987 kmod_list_foreach(l
, mod
->info_list
) {
1988 const char *key
= kmod_module_info_get_key(l
);
1989 const char *value
= kmod_module_info_get_value(l
);
1991 if (!streq(key
, "alias"))
1994 fprintf(out
, "alias %s %s\n", value
, mod
->modname
);
2001 static int output_aliases_bin(struct depmod
*depmod
, FILE *out
)
2003 struct index_node
*idx
;
2009 idx
= index_create();
2013 for (i
= 0; i
< depmod
->modules
.count
; i
++) {
2014 const struct mod
*mod
= depmod
->modules
.array
[i
];
2015 struct kmod_list
*l
;
2017 kmod_list_foreach(l
, mod
->info_list
) {
2018 const char *key
= kmod_module_info_get_key(l
);
2019 const char *value
= kmod_module_info_get_value(l
);
2024 if (!streq(key
, "alias"))
2027 if (alias_normalize(value
, buf
, NULL
) < 0) {
2028 WRN("Unmatched bracket in %s\n", value
);
2033 duplicate
= index_insert(idx
, alias
, mod
->modname
,
2035 if (duplicate
&& depmod
->cfg
->warn_dups
)
2036 WRN("duplicate module alias:\n%s %s\n",
2037 alias
, mod
->modname
);
2041 index_write(idx
, out
);
2047 static int output_softdeps(struct depmod
*depmod
, FILE *out
)
2051 fputs("# Soft dependencies extracted from modules themselves.\n", out
);
2053 for (i
= 0; i
< depmod
->modules
.count
; i
++) {
2054 const struct mod
*mod
= depmod
->modules
.array
[i
];
2055 struct kmod_list
*l
;
2057 kmod_list_foreach(l
, mod
->info_list
) {
2058 const char *key
= kmod_module_info_get_key(l
);
2059 const char *value
= kmod_module_info_get_value(l
);
2061 if (!streq(key
, "softdep"))
2064 fprintf(out
, "softdep %s %s\n", mod
->modname
, value
);
2071 static int output_symbols(struct depmod
*depmod
, FILE *out
)
2073 struct hash_iter iter
;
2076 fputs("# Aliases for symbols, used by symbol_request().\n", out
);
2078 hash_iter_init(depmod
->symbols
, &iter
);
2080 while (hash_iter_next(&iter
, NULL
, &v
)) {
2081 const struct symbol
*sym
= v
;
2082 if (sym
->owner
== NULL
)
2085 fprintf(out
, "alias symbol:%s %s\n",
2086 sym
->name
, sym
->owner
->modname
);
2092 static int output_symbols_bin(struct depmod
*depmod
, FILE *out
)
2094 struct index_node
*idx
;
2096 _cleanup_(scratchbuf_release
) struct scratchbuf salias
=
2097 SCRATCHBUF_INITIALIZER(alias
);
2098 size_t baselen
= sizeof("symbol:") - 1;
2099 struct hash_iter iter
;
2106 idx
= index_create();
2110 memcpy(alias
, "symbol:", baselen
);
2112 hash_iter_init(depmod
->symbols
, &iter
);
2114 while (hash_iter_next(&iter
, NULL
, &v
)) {
2116 const struct symbol
*sym
= v
;
2119 if (sym
->owner
== NULL
)
2122 len
= strlen(sym
->name
);
2124 if (scratchbuf_alloc(&salias
, baselen
+ len
+ 1) < 0) {
2126 goto err_scratchbuf
;
2128 memcpy(scratchbuf_str(&salias
) + baselen
, sym
->name
, len
+ 1);
2129 duplicate
= index_insert(idx
, alias
, sym
->owner
->modname
,
2132 if (duplicate
&& depmod
->cfg
->warn_dups
)
2133 WRN("duplicate module syms:\n%s %s\n",
2134 alias
, sym
->owner
->modname
);
2137 index_write(idx
, out
);
2143 ERR("output symbols: %s\n", strerror(-ret
));
2148 static int output_builtin_bin(struct depmod
*depmod
, FILE *out
)
2151 struct index_node
*idx
;
2152 char infile
[PATH_MAX
], line
[PATH_MAX
], modname
[PATH_MAX
];
2157 snprintf(infile
, sizeof(infile
), "%s/modules.builtin",
2158 depmod
->cfg
->dirname
);
2159 in
= fopen(infile
, "r");
2161 WRN("could not open %s: %m\n", infile
);
2165 idx
= index_create();
2171 while (fgets(line
, sizeof(line
), in
) != NULL
) {
2172 if (!isalpha(line
[0])) {
2173 ERR("Invalid modules.builtin line: %s\n", line
);
2177 path_to_modname(line
, modname
, NULL
);
2178 index_insert(idx
, modname
, "", 0);
2181 index_write(idx
, out
);
2188 static int output_devname(struct depmod
*depmod
, FILE *out
)
2193 for (i
= 0; i
< depmod
->modules
.count
; i
++) {
2194 const struct mod
*mod
= depmod
->modules
.array
[i
];
2195 struct kmod_list
*l
;
2196 const char *devname
= NULL
;
2198 unsigned int major
= 0, minor
= 0;
2200 kmod_list_foreach(l
, mod
->info_list
) {
2201 const char *key
= kmod_module_info_get_key(l
);
2202 const char *value
= kmod_module_info_get_value(l
);
2203 unsigned int maj
, min
;
2205 if (!streq(key
, "alias"))
2208 if (strstartswith(value
, "devname:"))
2209 devname
= value
+ sizeof("devname:") - 1;
2210 else if (sscanf(value
, "char-major-%u-%u",
2215 } else if (sscanf(value
, "block-major-%u-%u",
2222 if (type
!= '\0' && devname
!= NULL
)
2226 if (devname
!= NULL
) {
2229 fputs("# Device nodes to trigger on-demand module loading.\n",
2233 fprintf(out
, "%s %s %c%u:%u\n", mod
->modname
,
2234 devname
, type
, major
, minor
);
2236 ERR("Module '%s' has devname (%s) but "
2237 "lacks major and minor information. "
2238 "Ignoring.\n", mod
->modname
, devname
);
2245 static int depmod_output(struct depmod
*depmod
, FILE *out
)
2247 static const struct depfile
{
2249 int (*cb
)(struct depmod
*depmod
, FILE *out
);
2250 } *itr
, depfiles
[] = {
2251 { "modules.dep", output_deps
},
2252 { "modules.dep.bin", output_deps_bin
},
2253 { "modules.alias", output_aliases
},
2254 { "modules.alias.bin", output_aliases_bin
},
2255 { "modules.softdep", output_softdeps
},
2256 { "modules.symbols", output_symbols
},
2257 { "modules.symbols.bin", output_symbols_bin
},
2258 { "modules.builtin.bin", output_builtin_bin
},
2259 { "modules.devname", output_devname
},
2262 const char *dname
= depmod
->cfg
->dirname
;
2268 dfd
= open(dname
, O_RDONLY
);
2271 CRIT("could not open directory %s: %m\n", dname
);
2276 for (itr
= depfiles
; itr
->name
!= NULL
; itr
++) {
2278 char tmp
[NAME_MAX
] = "";
2282 int flags
= O_CREAT
| O_TRUNC
| O_WRONLY
;
2286 snprintf(tmp
, sizeof(tmp
), "%s.tmp", itr
->name
);
2287 fd
= openat(dfd
, tmp
, flags
, mode
);
2289 ERR("openat(%s, %s, %o, %o): %m\n",
2290 dname
, tmp
, flags
, mode
);
2293 fp
= fdopen(fd
, "wb");
2295 ERR("fdopen(%d=%s/%s): %m\n", fd
, dname
, tmp
);
2301 r
= itr
->cb(depmod
, fp
);
2305 ferr
= ferror(fp
) | fclose(fp
);
2308 if (unlinkat(dfd
, tmp
, 0) != 0)
2309 ERR("unlinkat(%s, %s): %m\n", dname
, tmp
);
2311 ERR("Could not write index '%s': %s\n", itr
->name
,
2317 unlinkat(dfd
, itr
->name
, 0);
2318 if (renameat(dfd
, tmp
, dfd
, itr
->name
) != 0) {
2320 CRIT("renameat(%s, %s, %s, %s): %m\n",
2321 dname
, tmp
, dname
, itr
->name
);
2327 ERR("Could not create index '%s'. Output is truncated: %s\n",
2328 itr
->name
, strerror(-err
));
2339 static void depmod_add_fake_syms(struct depmod
*depmod
)
2341 /* __this_module is magic inserted by kernel loader. */
2342 depmod_symbol_add(depmod
, "__this_module", true, 0, NULL
);
2343 /* On S390, this is faked up too */
2344 depmod_symbol_add(depmod
, "_GLOBAL_OFFSET_TABLE_", true, 0, NULL
);
2345 /* On PowerPC64 ABIv2, .TOC. is more or less _GLOBAL_OFFSET_TABLE_ */
2346 depmod_symbol_add(depmod
, "TOC.", true, 0, NULL
);
2349 static int depmod_load_symvers(struct depmod
*depmod
, const char *filename
)
2353 unsigned int linenum
= 0;
2355 fp
= fopen(filename
, "r");
2358 DBG("load symvers: %s: %m\n", filename
);
2361 DBG("load symvers: %s\n", filename
);
2363 /* eg. "0xb352177e\tfind_first_bit\tvmlinux\tEXPORT_SYMBOL" */
2364 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
2365 const char *ver
, *sym
, *where
;
2371 ver
= strtok(line
, " \t");
2372 sym
= strtok(NULL
, " \t");
2373 where
= strtok(NULL
, " \t");
2374 if (!ver
|| !sym
|| !where
)
2377 if (!streq(where
, "vmlinux"))
2380 crc
= strtoull(ver
, &verend
, 16);
2381 if (verend
[0] != '\0') {
2382 ERR("%s:%u Invalid symbol version %s: %m\n",
2383 filename
, linenum
, ver
);
2387 depmod_symbol_add(depmod
, sym
, false, crc
, NULL
);
2389 depmod_add_fake_syms(depmod
);
2391 DBG("loaded symvers: %s\n", filename
);
2397 static int depmod_load_system_map(struct depmod
*depmod
, const char *filename
)
2399 const char ksymstr
[] = "__ksymtab_";
2400 const size_t ksymstr_len
= sizeof(ksymstr
) - 1;
2403 unsigned int linenum
= 0;
2405 fp
= fopen(filename
, "r");
2408 DBG("load System.map: %s: %m\n", filename
);
2411 DBG("load System.map: %s\n", filename
);
2413 /* eg. c0294200 R __ksymtab_devfs_alloc_devnum */
2414 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
2419 p
= strchr(line
, ' ');
2421 goto invalid_syntax
;
2425 goto invalid_syntax
;
2429 if (p
[0] == depmod
->cfg
->sym_prefix
)
2432 /* Covers gpl-only and normal symbols. */
2433 if (strncmp(p
, ksymstr
, ksymstr_len
) != 0)
2436 end
= strchr(p
, '\n');
2440 depmod_symbol_add(depmod
, p
+ ksymstr_len
, true, 0, NULL
);
2444 ERR("%s:%u: invalid line: %s\n", filename
, linenum
, line
);
2446 depmod_add_fake_syms(depmod
);
2448 DBG("loaded System.map: %s\n", filename
);
2455 static int depfile_up_to_date_dir(DIR *d
, time_t mtime
, size_t baselen
, char *path
)
2458 int err
= 1, dfd
= dirfd(d
);
2460 while ((de
= readdir(d
)) != NULL
) {
2461 const char *name
= de
->d_name
;
2465 if (name
[0] == '.' && (name
[1] == '\0' ||
2466 (name
[1] == '.' && name
[2] == '\0')))
2468 if (streq(name
, "build") || streq(name
, "source"))
2470 namelen
= strlen(name
);
2471 if (baselen
+ namelen
+ 2 >= PATH_MAX
) {
2472 path
[baselen
] = '\0';
2473 ERR("path is too long %s%s\n", path
, name
);
2477 if (fstatat(dfd
, name
, &st
, 0) < 0) {
2478 ERR("fstatat(%d, %s): %m\n", dfd
, name
);
2482 if (S_ISDIR(st
.st_mode
)) {
2485 memcpy(path
+ baselen
, name
, namelen
+ 1);
2486 if (baselen
+ namelen
+ 2 + NAME_MAX
>= PATH_MAX
) {
2487 ERR("directory path is too long %s\n", path
);
2490 fd
= openat(dfd
, name
, O_RDONLY
);
2492 ERR("openat(%d, %s, O_RDONLY): %m\n",
2496 subdir
= fdopendir(fd
);
2497 if (subdir
== NULL
) {
2498 ERR("fdopendir(%d): %m\n", fd
);
2502 path
[baselen
+ namelen
] = '/';
2503 path
[baselen
+ namelen
+ 1] = '\0';
2504 err
= depfile_up_to_date_dir(subdir
, mtime
,
2505 baselen
+ namelen
+ 1,
2508 } else if (S_ISREG(st
.st_mode
)) {
2509 if (!path_ends_with_kmod_ext(name
, namelen
))
2512 memcpy(path
+ baselen
, name
, namelen
+ 1);
2513 err
= st
.st_mtime
<= mtime
;
2515 DBG("%s %"PRIu64
" is newer than %"PRIu64
"\n",
2516 path
, (uint64_t)st
.st_mtime
,
2520 ERR("unsupported file type %s: %o\n",
2521 path
, st
.st_mode
& S_IFMT
);
2526 break; /* outdated! */
2528 path
[baselen
+ namelen
] = '\0';
2529 ERR("failed %s: %s\n", path
, strerror(-err
));
2530 err
= 1; /* ignore errors */
2537 /* uptodate: 1, outdated: 0, errors < 0 */
2538 static int depfile_up_to_date(const char *dirname
)
2540 char path
[PATH_MAX
];
2541 DIR *d
= opendir(dirname
);
2547 ERR("could not open directory %s: %m\n", dirname
);
2551 if (fstatat(dirfd(d
), "modules.dep", &st
, 0) != 0) {
2553 ERR("could not fstatat(%s, modules.dep): %m\n", dirname
);
2558 baselen
= strlen(dirname
);
2559 memcpy(path
, dirname
, baselen
);
2560 path
[baselen
] = '/';
2562 path
[baselen
] = '\0';
2564 err
= depfile_up_to_date_dir(d
, st
.st_mtime
, baselen
, path
);
2569 static int is_version_number(const char *version
)
2571 unsigned int d1
, d2
;
2572 return (sscanf(version
, "%u.%u", &d1
, &d2
) == 2);
2575 static int do_depmod(int argc
, char *argv
[])
2578 int err
= 0, all
= 0, maybe_all
= 0, n_config_paths
= 0;
2579 _cleanup_free_
char *root
= NULL
;
2580 _cleanup_free_
const char **config_paths
= NULL
;
2581 const char *system_map
= NULL
;
2582 const char *module_symvers
= NULL
;
2583 const char *null_kmod_config
= NULL
;
2585 struct kmod_ctx
*ctx
= NULL
;
2587 struct depmod depmod
;
2589 memset(&cfg
, 0, sizeof(cfg
));
2590 memset(&depmod
, 0, sizeof(depmod
));
2594 c
= getopt_long(argc
, argv
, cmdopts_s
, cmdopts
, &idx
);
2607 root
= path_make_absolute_cwd(optarg
);
2610 size_t bytes
= sizeof(char *) * (n_config_paths
+ 2);
2611 void *tmp
= realloc(config_paths
, bytes
);
2613 fputs("Error: out-of-memory\n", stderr
);
2614 goto cmdline_failed
;
2617 config_paths
[n_config_paths
] = optarg
;
2619 config_paths
[n_config_paths
] = NULL
;
2623 module_symvers
= optarg
;
2624 cfg
.check_symvers
= 1;
2627 system_map
= optarg
;
2630 cfg
.print_unknown
= 1;
2639 if (optarg
[1] != '\0') {
2640 CRIT("-P only takes a single char\n");
2641 goto cmdline_failed
;
2643 cfg
.sym_prefix
= optarg
[0];
2653 WRN("Ignored deprecated option --%s\n",
2656 WRN("Ignored deprecated option -%c\n", c
);
2661 return EXIT_SUCCESS
;
2663 puts(PACKAGE
" version " VERSION
);
2664 puts(KMOD_FEATURES
);
2665 return EXIT_SUCCESS
;
2667 goto cmdline_failed
;
2669 ERR("unexpected getopt_long() value '%c'.\n", c
);
2670 goto cmdline_failed
;
2674 if (optind
< argc
) {
2675 if (!is_version_number(argv
[optind
])) {
2676 ERR("Bad version passed %s\n", argv
[optind
]);
2677 goto cmdline_failed
;
2679 cfg
.kversion
= argv
[optind
];
2682 if (uname(&un
) < 0) {
2683 CRIT("uname() failed: %s\n", strerror(errno
));
2684 goto cmdline_failed
;
2686 cfg
.kversion
= un
.release
;
2689 cfg
.dirnamelen
= snprintf(cfg
.dirname
, PATH_MAX
,
2690 "%s/lib/modules/%s",
2691 root
== NULL
? "" : root
, cfg
.kversion
);
2699 /* ignore up-to-date errors (< 0) */
2700 if (depfile_up_to_date(cfg
.dirname
) == 1)
2705 ctx
= kmod_new(cfg
.dirname
, &null_kmod_config
);
2707 CRIT("kmod_new(\"%s\", {NULL}) failed: %m\n", cfg
.dirname
);
2708 goto cmdline_failed
;
2711 log_setup_kmod_log(ctx
, verbose
);
2713 err
= depmod_init(&depmod
, &cfg
, ctx
);
2715 CRIT("depmod_init: %s\n", strerror(-err
));
2716 goto depmod_init_failed
;
2718 ctx
= NULL
; /* owned by depmod */
2720 if (module_symvers
!= NULL
) {
2721 err
= depmod_load_symvers(&depmod
, module_symvers
);
2723 CRIT("could not load %s: %s\n", module_symvers
,
2725 goto cmdline_failed
;
2727 } else if (system_map
!= NULL
) {
2728 err
= depmod_load_system_map(&depmod
, system_map
);
2730 CRIT("could not load %s: %s\n", system_map
,
2732 goto cmdline_failed
;
2734 } else if (cfg
.print_unknown
) {
2735 WRN("-e needs -E or -F\n");
2736 cfg
.print_unknown
= 0;
2740 err
= cfg_load(&cfg
, config_paths
);
2742 CRIT("could not load configuration files\n");
2743 goto cmdline_modules_failed
;
2745 err
= depmod_modules_search(&depmod
);
2747 CRIT("could not search modules: %s\n", strerror(-err
));
2748 goto cmdline_modules_failed
;
2753 for (i
= optind
; i
< argc
; i
++) {
2754 const char *path
= argv
[i
];
2755 struct kmod_module
*mod
;
2757 if (path
[0] != '/') {
2758 CRIT("%s: not absolute path.\n", path
);
2759 goto cmdline_modules_failed
;
2762 err
= kmod_module_new_from_path(depmod
.ctx
, path
, &mod
);
2764 CRIT("could not create module %s: %s\n",
2765 path
, strerror(-err
));
2766 goto cmdline_modules_failed
;
2769 err
= depmod_module_add(&depmod
, mod
);
2771 CRIT("could not add module %s: %s\n",
2772 path
, strerror(-err
));
2773 kmod_module_unref(mod
);
2774 goto cmdline_modules_failed
;
2779 err
= depmod_modules_build_array(&depmod
);
2781 CRIT("could not build module array: %s\n",
2783 goto cmdline_modules_failed
;
2786 depmod_modules_sort(&depmod
);
2787 err
= depmod_load(&depmod
);
2789 goto cmdline_modules_failed
;
2791 err
= depmod_output(&depmod
, out
);
2794 depmod_shutdown(&depmod
);
2796 return err
>= 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
2798 cmdline_modules_failed
:
2799 depmod_shutdown(&depmod
);
2805 return EXIT_FAILURE
;
2808 const struct kmod_cmd kmod_cmd_compat_depmod
= {
2811 .help
= "compat depmod command",