]> git.ipfire.org Git - thirdparty/kmod.git/blame - tools/depmod.c
depmod: create depmod dir independent search function
[thirdparty/kmod.git] / tools / depmod.c
CommitLineData
64b8b586
GSB
1/*
2 * kmod-depmod - calculate modules.dep using libkmod.
3 *
e6b0e49b 4 * Copyright (C) 2011-2013 ProFUSION embedded systems
64b8b586
GSB
5 *
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.
10 *
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.
15 *
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/>.
18 */
c2e4286b
LDM
19
20#include <assert.h>
21#include <ctype.h>
22#include <dirent.h>
23#include <errno.h>
24#include <getopt.h>
25#include <limits.h>
26#include <regex.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <unistd.h>
31#include <sys/stat.h>
32#include <sys/utsname.h>
64b8b586 33
74d1df66 34#include <shared/array.h>
0db718ed 35#include <shared/hash.h>
576dd439 36#include <shared/macro.h>
96573a02 37#include <shared/util.h>
cb51a641 38#include <shared/scratchbuf.h>
576dd439 39
c7ce9f0c
YK
40#include <libkmod/libkmod-internal.h>
41
42#undef ERR
43#undef DBG
64b8b586 44
4a2e20df
LDM
45#include "kmod.h"
46
64b8b586
GSB
47#define DEFAULT_VERBOSE LOG_WARNING
48static int verbose = DEFAULT_VERBOSE;
49
50static const char CFG_BUILTIN_KEY[] = "built-in";
51static const char *default_cfg_paths[] = {
52 "/run/depmod.d",
53 SYSCONFDIR "/depmod.d",
c5b37dba 54 "/lib/depmod.d",
64b8b586
GSB
55 NULL
56};
57
58static const char cmdopts_s[] = "aAb:C:E:F:euqrvnP:wmVh";
59static const struct option cmdopts[] = {
015946da
LDM
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' },
c02a8e6f 73 { "symbol-prefix", required_argument, 0, 'P' },
015946da
LDM
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' },
78 { }
64b8b586
GSB
79};
80
4a2e20df 81static void help(void)
64b8b586 82{
34e06bfb 83 printf("Usage:\n"
64b8b586
GSB
84 "\t%s -[aA] [options] [forced_version]\n"
85 "\n"
86 "If no arguments (except options) are given, \"depmod -a\" is assumed\n"
87 "\n"
88 "depmod will output a dependency list suitable for the modprobe utility.\n"
89 "\n"
90 "Options:\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"
59886525 96 "\t-C, --config=PATH Read configuration from PATH\n"
64b8b586
GSB
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"
101 "\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",
7c04aeee 108 program_invocation_short_name);
64b8b586
GSB
109}
110
1958af88 111_printf_format_(1, 2)
64b8b586
GSB
112static inline void _show(const char *fmt, ...)
113{
114 va_list args;
115
116 if (verbose <= DEFAULT_VERBOSE)
117 return;
118
119 va_start(args, fmt);
120 vfprintf(stdout, fmt, args);
121 fflush(stdout);
122 va_end(args);
123}
64b8b586
GSB
124#define SHOW(...) _show(__VA_ARGS__)
125
126
0de40463
GSB
127/* binary index write *************************************************/
128#include <arpa/inet.h>
0de40463
GSB
129/* BEGIN: code from module-init-tools/index.c just modified to compile here.
130 *
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>.
134 *
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.
139 *
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.
144 *
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/>.
147 */
148
778395e4 149/* see documentation in libkmod/libkmod-index.c */
0de40463 150
0de40463 151#define INDEX_MAGIC 0xB007F457
0de40463
GSB
152#define INDEX_VERSION_MAJOR 0x0002
153#define INDEX_VERSION_MINOR 0x0001
154#define INDEX_VERSION ((INDEX_VERSION_MAJOR<<16)|INDEX_VERSION_MINOR)
778395e4 155#define INDEX_CHILDMAX 128
0de40463
GSB
156
157struct index_value {
158 struct index_value *next;
159 unsigned int priority;
160 char value[0];
161};
162
163/* In-memory index (depmod only) */
0de40463
GSB
164struct index_node {
165 char *prefix; /* path compression */
166 struct index_value *values;
167 unsigned char first; /* range of child nodes */
168 unsigned char last;
169 struct index_node *children[INDEX_CHILDMAX]; /* indexed by character */
170};
171
0de40463
GSB
172
173/* Format of node offsets within index file */
174enum node_offset {
175 INDEX_NODE_FLAGS = 0xF0000000, /* Flags in high nibble */
176 INDEX_NODE_PREFIX = 0x80000000,
177 INDEX_NODE_VALUES = 0x40000000,
178 INDEX_NODE_CHILDS = 0x20000000,
179
180 INDEX_NODE_MASK = 0x0FFFFFFF, /* Offset value */
181};
182
183static struct index_node *index_create(void)
184{
185 struct index_node *node;
186
187 node = NOFAIL(calloc(sizeof(struct index_node), 1));
188 node->prefix = NOFAIL(strdup(""));
189 node->first = INDEX_CHILDMAX;
190
191 return node;
192}
193
194static void index_values_free(struct index_value *values)
195{
196 while (values) {
197 struct index_value *value = values;
198
199 values = value->next;
200 free(value);
201 }
202}
203
204static void index_destroy(struct index_node *node)
205{
206 int c;
207
208 for (c = node->first; c <= node->last; c++) {
209 struct index_node *child = node->children[c];
210
211 if (child)
212 index_destroy(child);
213 }
214 index_values_free(node->values);
215 free(node->prefix);
216 free(node);
217}
218
219static void index__checkstring(const char *str)
220{
221 int i;
222
223 for (i = 0; str[i]; i++) {
224 int ch = str[i];
225
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);
229 }
230}
231
232static int index_add_value(struct index_value **values,
233 const char *value, unsigned int priority)
234{
235 struct index_value *v;
236 int duplicate = 0;
237 int len;
238
239 /* report the presence of duplicate values */
240 for (v = *values; v; v = v->next) {
241 if (streq(v->value, value))
242 duplicate = 1;
243 }
244
245 /* find position to insert value */
246 while (*values && (*values)->priority < priority)
247 values = &(*values)->next;
248
249 len = strlen(value);
250 v = NOFAIL(calloc(sizeof(struct index_value) + len + 1, 1));
251 v->next = *values;
252 v->priority = priority;
253 memcpy(v->value, value, len + 1);
254 *values = v;
255
256 return duplicate;
257}
258
259static int index_insert(struct index_node *node, const char *key,
260 const char *value, unsigned int priority)
261{
262 int i = 0; /* index within str */
263 int ch;
264
265 index__checkstring(key);
266 index__checkstring(value);
267
268 while(1) {
269 int j; /* index within node->prefix */
270
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];
275
276 if (ch != key[i+j]) {
277 char *prefix = node->prefix;
278 struct index_node *n;
279
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]));
284
285 /* Parent has prefix[0..j], child at prefix[j] */
286 memset(node, 0, sizeof(struct index_node));
287 prefix[j] = '\0';
288 node->prefix = prefix;
289 node->first = ch;
290 node->last = ch;
291 node->children[ch] = n;
292
293 break;
294 }
295 }
296 /* j is now length of node->prefix */
297 i += j;
298
299 ch = key[i];
300 if(ch == '\0')
301 return index_add_value(&node->values, value, priority);
302
303 if (!node->children[ch]) {
304 struct index_node *child;
305
306 if (ch < node->first)
307 node->first = ch;
308 if (ch > node->last)
309 node->last = ch;
310 node->children[ch] = NOFAIL(calloc(sizeof(struct index_node), 1));
311
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);
316
317 return 0;
318 }
319
320 /* Descend into child node and continue */
321 node = node->children[ch];
322 i++;
323 }
324}
325
326static int index__haschildren(const struct index_node *node)
327{
328 return node->first < INDEX_CHILDMAX;
329}
330
331/* Recursive post-order traversal
332
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.
337 */
338static uint32_t index_write__node(const struct index_node *node, FILE *out)
339{
340 uint32_t *child_offs = NULL;
341 int child_count = 0;
342 long offset;
343
344 if (!node)
345 return 0;
346
347 /* Write children and save their offsets */
348 if (index__haschildren(node)) {
349 const struct index_node *child;
350 int i;
351
352 child_count = node->last - node->first + 1;
353 child_offs = NOFAIL(malloc(child_count * sizeof(uint32_t)));
354
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));
358 }
359 }
360
361 /* Now write this node */
362 offset = ftell(out);
363
364 if (node->prefix[0]) {
365 fputs(node->prefix, out);
366 fputc('\0', out);
367 offset |= INDEX_NODE_PREFIX;
368 }
369
370 if (child_count) {
371 fputc(node->first, out);
372 fputc(node->last, out);
373 fwrite(child_offs, sizeof(uint32_t), child_count, out);
0de40463
GSB
374 offset |= INDEX_NODE_CHILDS;
375 }
376
a1bec0df
LDM
377 free(child_offs);
378
0de40463
GSB
379 if (node->values) {
380 const struct index_value *v;
381 unsigned int value_count;
382 uint32_t u;
383
384 value_count = 0;
385 for (v = node->values; v != NULL; v = v->next)
386 value_count++;
387 u = htonl(value_count);
388 fwrite(&u, sizeof(u), 1, out);
389
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);
394 fputc('\0', out);
395 }
396 offset |= INDEX_NODE_VALUES;
397 }
398
399 return offset;
400}
401
402static void index_write(const struct index_node *node, FILE *out)
403{
404 long initial_offset, final_offset;
405 uint32_t u;
406
407 u = htonl(INDEX_MAGIC);
408 fwrite(&u, sizeof(u), 1, out);
409 u = htonl(INDEX_VERSION);
410 fwrite(&u, sizeof(u), 1, out);
411
412 /* Second word is reserved for the offset of the root node */
413 initial_offset = ftell(out);
22df4567 414 assert(initial_offset >= 0);
0de40463
GSB
415 u = 0;
416 fwrite(&u, sizeof(uint32_t), 1, out);
417
418 /* Dump trie */
419 u = htonl(index_write__node(node, out));
420
421 /* Update first word */
422 final_offset = ftell(out);
22df4567
LDM
423 assert(final_offset >= 0);
424 (void)fseek(out, initial_offset, SEEK_SET);
0de40463 425 fwrite(&u, sizeof(uint32_t), 1, out);
22df4567 426 (void)fseek(out, final_offset, SEEK_SET);
0de40463
GSB
427}
428
429/* END: code from module-init-tools/index.c just modified to compile here.
430 */
431
64b8b586
GSB
432/* configuration parsing **********************************************/
433struct cfg_override {
434 struct cfg_override *next;
435 size_t len;
436 char path[];
437};
438
439struct cfg_search {
440 struct cfg_search *next;
441 uint8_t builtin;
442 size_t len;
443 char path[];
444};
445
446struct cfg {
447 const char *kversion;
448 char dirname[PATH_MAX];
449 size_t dirnamelen;
450 char sym_prefix;
451 uint8_t check_symvers;
452 uint8_t print_unknown;
453 uint8_t warn_dups;
454 struct cfg_override *overrides;
455 struct cfg_search *searches;
456};
457
0d6b3f9b 458static int cfg_search_add(struct cfg *cfg, const char *path)
64b8b586
GSB
459{
460 struct cfg_search *s;
461 size_t len;
0d6b3f9b 462 uint8_t builtin = streq(path, CFG_BUILTIN_KEY);
64b8b586
GSB
463
464 if (builtin)
465 len = 0;
466 else
467 len = strlen(path) + 1;
468
469 s = malloc(sizeof(struct cfg_search) + len);
470 if (s == NULL) {
471 ERR("search add: out of memory\n");
472 return -ENOMEM;
473 }
474 s->builtin = builtin;
475 if (builtin)
476 s->len = 0;
477 else {
026c7b44 478 s->len = len - 1;
64b8b586
GSB
479 memcpy(s->path, path, len);
480 }
481
482 DBG("search add: %s, builtin=%hhu\n", path, builtin);
483
484 s->next = cfg->searches;
485 cfg->searches = s;
486 return 0;
487}
488
489static void cfg_search_free(struct cfg_search *s)
490{
491 free(s);
492}
493
494static int cfg_override_add(struct cfg *cfg, const char *modname, const char *subdir)
495{
496 struct cfg_override *o;
497 size_t modnamelen = strlen(modname);
498 size_t subdirlen = strlen(subdir);
499 size_t i;
500
026c7b44
GSB
501 o = malloc(sizeof(struct cfg_override) + subdirlen + 1
502 + modnamelen + 1);
64b8b586
GSB
503 if (o == NULL) {
504 ERR("override add: out of memory\n");
505 return -ENOMEM;
506 }
026c7b44
GSB
507 memcpy(o->path, subdir, subdirlen);
508 i = subdirlen;
64b8b586
GSB
509 o->path[i] = '/';
510 i++;
511
512 memcpy(o->path + i, modname, modnamelen);
513 i += modnamelen;
514 o->path[i] = '\0'; /* no extension, so we can match .ko/.ko.gz */
515
516 o->len = i;
517
518 DBG("override add: %s\n", o->path);
519
520 o->next = cfg->overrides;
521 cfg->overrides = o;
522 return 0;
523}
524
525static void cfg_override_free(struct cfg_override *o)
526{
527 free(o);
528}
529
530static int cfg_kernel_matches(const struct cfg *cfg, const char *pattern)
531{
532 regex_t re;
533 int status;
534
535 /* old style */
536 if (streq(pattern, "*"))
537 return 1;
538
539 if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB) != 0)
540 return 0;
541
542 status = regexec(&re, cfg->kversion, 0, NULL, 0);
543 regfree(&re);
544
545 return status == 0;
546}
547
64b8b586
GSB
548static int cfg_file_parse(struct cfg *cfg, const char *filename)
549{
550 char *line;
551 FILE *fp;
552 unsigned int linenum = 0;
553 int err;
554
555 fp = fopen(filename, "r");
556 if (fp == NULL) {
557 err = -errno;
b0bcadd0 558 ERR("file parse %s: %m\n", filename);
64b8b586
GSB
559 return err;
560 }
561
aafd3835 562 while ((line = freadline_wrapped(fp, &linenum)) != NULL) {
64b8b586
GSB
563 char *cmd, *saveptr;
564
565 if (line[0] == '\0' || line[0] == '#')
566 goto done_next;
567
568 cmd = strtok_r(line, "\t ", &saveptr);
569 if (cmd == NULL)
570 goto done_next;
571
572 if (streq(cmd, "search")) {
573 const char *sp;
574 while ((sp = strtok_r(NULL, "\t ", &saveptr)) != NULL) {
0d6b3f9b 575 cfg_search_add(cfg, sp);
64b8b586
GSB
576 }
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);
581
582 if (modname == NULL || version == NULL ||
583 subdir == NULL)
584 goto syntax_error;
585
586 if (!cfg_kernel_matches(cfg, version)) {
587 INF("%s:%u: override kernel did not match %s\n",
588 filename, linenum, version);
589 goto done_next;
590 }
591
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);
597 } else {
598syntax_error:
599 ERR("%s:%u: ignoring bad line starting with '%s'\n",
600 filename, linenum, cmd);
601 }
602
603done_next:
604 free(line);
605 }
606
607 fclose(fp);
608
609 return 0;
610}
611
612static int cfg_files_filter_out(DIR *d, const char *dir, const char *name)
613{
614 size_t len = strlen(name);
615 struct stat st;
616
617 if (name[0] == '.')
618 return 1;
619
620 if (len < 6 || !streq(name + len - 5, ".conf")) {
621 INF("All cfg files need .conf: %s/%s\n", dir, name);
622 return 1;
623 }
624
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",
628 dir, name);
629 return 1;
630 }
631
632 return 0;
633}
634
635struct cfg_file {
636 size_t dirlen;
637 size_t namelen;
638 const char *name;
639 char path[];
640};
641
642static void cfg_file_free(struct cfg_file *f)
643{
644 free(f);
645}
646
647static int cfg_files_insert_sorted(struct cfg_file ***p_files, size_t *p_n_files,
648 const char *dir, const char *name)
649{
650 struct cfg_file **files, *f;
651 size_t i, n_files, namelen, dirlen;
652 void *tmp;
653
654 dirlen = strlen(dir);
655 if (name != NULL)
656 namelen = strlen(name);
657 else {
658 name = basename(dir);
659 namelen = strlen(name);
8ea02fe0 660 dirlen -= namelen + 1;
64b8b586
GSB
661 }
662
663 n_files = *p_n_files;
664 files = *p_files;
665 for (i = 0; i < n_files; i++) {
666 int cmp = strcmp(name, files[i]->name);
667 if (cmp == 0) {
668 DBG("Ignoring duplicate config file: %.*s/%s\n",
669 (int)dirlen, dir, name);
670 return -EEXIST;
671 } else if (cmp < 0)
672 break;
673 }
674
675 f = malloc(sizeof(struct cfg_file) + dirlen + namelen + 2);
676 if (f == NULL) {
677 ERR("files insert sorted: out of memory\n");
678 return -ENOMEM;
679 }
680
681 tmp = realloc(files, sizeof(struct cfg_file *) * (n_files + 1));
682 if (tmp == NULL) {
683 ERR("files insert sorted: out of memory\n");
684 free(f);
685 return -ENOMEM;
686 }
687 *p_files = files = tmp;
688
689 if (i < n_files) {
690 memmove(files + i + 1, files + i,
691 sizeof(struct cfg_file *) * (n_files - i));
692 }
693 files[i] = f;
694
695 f->dirlen = dirlen;
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';
702
703 *p_n_files = n_files + 1;
704 return 0;
705}
706
707/*
708 * Insert configuration files ignoring duplicates
709 */
710static int cfg_files_list(struct cfg_file ***p_files, size_t *p_n_files,
711 const char *path)
712{
7e0385c4 713 struct dirent *dent;
64b8b586
GSB
714 DIR *d;
715 int err = 0;
716 struct stat st;
717
718 if (stat(path, &st) != 0) {
719 err = -errno;
720 DBG("could not stat '%s': %m\n", path);
721 return err;
722 }
723
519d27de 724 if (!S_ISDIR(st.st_mode)) {
64b8b586
GSB
725 cfg_files_insert_sorted(p_files, p_n_files, path, NULL);
726 return 0;
64b8b586
GSB
727 }
728
729 d = opendir(path);
730 if (d == NULL) {
731 ERR("files list %s: %m\n", path);
732 return -EINVAL;
733 }
734
7e0385c4
LDM
735 for (dent = readdir(d); dent != NULL; dent = readdir(d)) {
736 if (cfg_files_filter_out(d, path, dent->d_name))
64b8b586
GSB
737 continue;
738
7e0385c4 739 cfg_files_insert_sorted(p_files, p_n_files, path, dent->d_name);
64b8b586
GSB
740 }
741
742 closedir(d);
035cbdc7 743 DBG("parsed configuration files from %s\n", path);
64b8b586
GSB
744 return err;
745}
746
747static int cfg_load(struct cfg *cfg, const char * const *cfg_paths)
748{
749 size_t i, n_files = 0;
750 struct cfg_file **files = NULL;
751
752 if (cfg_paths == NULL)
753 cfg_paths = default_cfg_paths;
754
755 for (i = 0; cfg_paths[i] != NULL; i++)
756 cfg_files_list(&files, &n_files, cfg_paths[i]);
757
758 for (i = 0; i < n_files; i++) {
759 struct cfg_file *f = files[i];
760 cfg_file_parse(cfg, f->path);
761 cfg_file_free(f);
762 }
763 free(files);
764
765 /* For backward compatibility add "updates" to the head of the search
766 * list here. But only if there was no "search" option specified.
767 */
768 if (cfg->searches == NULL)
0d6b3f9b 769 cfg_search_add(cfg, "updates");
64b8b586
GSB
770
771 return 0;
772}
773
774static void cfg_free(struct cfg *cfg)
775{
776 while (cfg->overrides) {
777 struct cfg_override *tmp = cfg->overrides;
778 cfg->overrides = cfg->overrides->next;
779 cfg_override_free(tmp);
780 }
781
782 while (cfg->searches) {
783 struct cfg_search *tmp = cfg->searches;
784 cfg->searches = cfg->searches->next;
785 cfg_search_free(tmp);
786 }
787}
788
789
790/* depmod calculations ***********************************************/
c7ce9f0c 791struct vertex;
64b8b586
GSB
792struct mod {
793 struct kmod_module *kmod;
e4a7352a 794 char *path;
64b8b586 795 const char *relpath; /* path relative to '$ROOT/lib/modules/$VER/' */
b51ac407 796 char *uncrelpath; /* same as relpath but ending in .ko */
7062eca3 797 struct kmod_list *info_list;
ec587f29 798 struct kmod_list *dep_sym_list;
64b8b586
GSB
799 struct array deps; /* struct symbol */
800 size_t baselen; /* points to start of basename/filename */
a873f235 801 size_t modnamesz;
64b8b586
GSB
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 */
c89d2198 806 bool visited; /* helper field to report cycles */
c7ce9f0c 807 struct vertex *vertex; /* helper field to report cycles */
64b8b586
GSB
808 char modname[];
809};
810
811struct symbol {
812 struct mod *owner;
813 uint64_t crc;
814 char name[];
815};
816
817struct depmod {
818 const struct cfg *cfg;
819 struct kmod_ctx *ctx;
820 struct array modules;
88c247f7 821 struct hash *modules_by_uncrelpath;
64b8b586
GSB
822 struct hash *modules_by_name;
823 struct hash *symbols;
64b8b586
GSB
824};
825
826static void mod_free(struct mod *mod)
827{
828 DBG("free %p kmod=%p, path=%s\n", mod, mod->kmod, mod->path);
829 array_free_array(&mod->deps);
02c64df3 830 kmod_module_unref(mod->kmod);
7062eca3 831 kmod_module_info_free_list(mod->info_list);
ec587f29 832 kmod_module_dependency_symbols_free_list(mod->dep_sym_list);
88c247f7 833 free(mod->uncrelpath);
e4a7352a 834 free(mod->path);
64b8b586
GSB
835 free(mod);
836}
837
838static int mod_add_dependency(struct mod *mod, struct symbol *sym)
839{
840 int err;
841
842 DBG("%s depends on %s %s\n", mod->path, sym->name,
843 sym->owner != NULL ? sym->owner->path : "(unknown)");
844
845 if (sym->owner == NULL)
846 return 0;
847
848 err = array_append_unique(&mod->deps, sym->owner);
849 if (err == -EEXIST)
850 return 0;
851 if (err < 0)
852 return err;
853
854 sym->owner->users++;
855 SHOW("%s needs \"%s\": %s\n", mod->path, sym->name, sym->owner->path);
856 return 0;
857}
858
859static void symbol_free(struct symbol *sym)
860{
861 DBG("free %p sym=%s, owner=%p %s\n", sym, sym->name, sym->owner,
862 sym->owner != NULL ? sym->owner->path : "");
863 free(sym);
864}
865
f6b838e1
LDM
866static int depmod_init(struct depmod *depmod, struct cfg *cfg,
867 struct kmod_ctx *ctx)
64b8b586
GSB
868{
869 int err = 0;
870
871 depmod->cfg = cfg;
872 depmod->ctx = ctx;
873
874 array_init(&depmod->modules, 128);
875
88c247f7
LDM
876 depmod->modules_by_uncrelpath = hash_new(512, NULL);
877 if (depmod->modules_by_uncrelpath == NULL) {
64b8b586 878 err = -errno;
88c247f7 879 goto modules_by_uncrelpath_failed;
64b8b586
GSB
880 }
881
882 depmod->modules_by_name = hash_new(512, NULL);
883 if (depmod->modules_by_name == NULL) {
884 err = -errno;
885 goto modules_by_name_failed;
886 }
887
888 depmod->symbols = hash_new(2048, (void (*)(void *))symbol_free);
889 if (depmod->symbols == NULL) {
890 err = -errno;
891 goto symbols_failed;
892 }
893
894 return 0;
895
896symbols_failed:
897 hash_free(depmod->modules_by_name);
898modules_by_name_failed:
88c247f7
LDM
899 hash_free(depmod->modules_by_uncrelpath);
900modules_by_uncrelpath_failed:
64b8b586
GSB
901 return err;
902}
903
904static void depmod_shutdown(struct depmod *depmod)
905{
906 size_t i;
907
908 hash_free(depmod->symbols);
909
88c247f7 910 hash_free(depmod->modules_by_uncrelpath);
64b8b586
GSB
911
912 hash_free(depmod->modules_by_name);
913
914 for (i = 0; i < depmod->modules.count; i++)
915 mod_free(depmod->modules.array[i]);
916 array_free_array(&depmod->modules);
917
918 kmod_unref(depmod->ctx);
919}
920
921static int depmod_module_add(struct depmod *depmod, struct kmod_module *kmod)
922{
923 const struct cfg *cfg = depmod->cfg;
88c247f7 924 const char *modname, *lastslash;
a873f235 925 size_t modnamesz;
64b8b586
GSB
926 struct mod *mod;
927 int err;
928
929 modname = kmod_module_get_name(kmod);
a873f235 930 modnamesz = strlen(modname) + 1;
64b8b586 931
a873f235 932 mod = calloc(1, sizeof(struct mod) + modnamesz);
64b8b586
GSB
933 if (mod == NULL)
934 return -ENOMEM;
935 mod->kmod = kmod;
936 mod->sort_idx = depmod->modules.count + 1;
937 mod->dep_sort_idx = INT32_MAX;
a873f235
LDM
938 memcpy(mod->modname, modname, modnamesz);
939 mod->modnamesz = modnamesz;
64b8b586
GSB
940
941 array_init(&mod->deps, 4);
942
e4a7352a 943 mod->path = strdup(kmod_module_get_path(kmod));
88c247f7
LDM
944 lastslash = strrchr(mod->path, '/');
945 mod->baselen = lastslash - mod->path;
64b8b586
GSB
946 if (strncmp(mod->path, cfg->dirname, cfg->dirnamelen) == 0 &&
947 mod->path[cfg->dirnamelen] == '/')
948 mod->relpath = mod->path + cfg->dirnamelen + 1;
949 else
950 mod->relpath = NULL;
951
64b8b586
GSB
952 err = hash_add_unique(depmod->modules_by_name, mod->modname, mod);
953 if (err < 0) {
954 ERR("hash_add_unique %s: %s\n", mod->modname, strerror(-err));
88c247f7 955 goto fail;
64b8b586
GSB
956 }
957
958 if (mod->relpath != NULL) {
a873f235 959 size_t uncrelpathlen = lastslash - mod->relpath + modnamesz
b95506ff 960 + strlen(KMOD_EXTENSION_UNCOMPRESSED);
88c247f7
LDM
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);
64b8b586
GSB
965 if (err < 0) {
966 ERR("hash_add_unique %s: %s\n",
06294621 967 mod->uncrelpath, strerror(-err));
64b8b586 968 hash_del(depmod->modules_by_name, mod->modname);
88c247f7 969 goto fail;
64b8b586
GSB
970 }
971 }
972
973 DBG("add %p kmod=%p, path=%s\n", mod, kmod, mod->path);
974
975 return 0;
88c247f7
LDM
976
977fail:
978 free(mod->uncrelpath);
979 free(mod);
980 return err;
64b8b586
GSB
981}
982
3db5bf9d 983static int depmod_module_del(struct depmod *depmod, struct mod *mod)
64b8b586 984{
3db5bf9d 985 DBG("del %p kmod=%p, path=%s\n", mod, mod->kmod, mod->path);
64b8b586 986
06294621
LDM
987 if (mod->uncrelpath != NULL)
988 hash_del(depmod->modules_by_uncrelpath, mod->uncrelpath);
3db5bf9d
GSB
989
990 hash_del(depmod->modules_by_name, mod->modname);
991
3db5bf9d 992 mod_free(mod);
64b8b586
GSB
993 return 0;
994}
995
996/* returns if existing module @mod is higher priority than newpath.
997 * note this is the inverse of module-init-tools is_higher_priority()
998 */
999static 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)
1000{
1001 const struct cfg *cfg = depmod->cfg;
1002 const struct cfg_override *ov;
1003 const struct cfg_search *se;
a873f235
LDM
1004
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
1007 * the latter */
64b8b586 1008 size_t newlen = baselen + modnamelen;
a873f235 1009 size_t oldlen = mod->baselen + mod->modnamesz;
64b8b586
GSB
1010 const char *oldpath = mod->path;
1011 int i, bprio = -1, oldprio = -1, newprio = -1;
1012
026c7b44
GSB
1013 assert(strncmp(newpath, cfg->dirname, cfg->dirnamelen) == 0);
1014 assert(strncmp(oldpath, cfg->dirname, cfg->dirnamelen) == 0);
1015
1016 newpath += cfg->dirnamelen + 1;
1017 newlen -= cfg->dirnamelen + 1;
1018 oldpath += cfg->dirnamelen + 1;
1019 oldlen -= cfg->dirnamelen + 1;
1020
64b8b586
GSB
1021 DBG("comparing priorities of %s and %s\n",
1022 oldpath, newpath);
1023
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)
1027 return 0;
1028 if (oldlen == ov->len && memcmp(ov->path, oldpath, oldlen) == 0)
1029 return 1;
1030 }
1031
1032 for (i = 0, se = cfg->searches; se != NULL; se = se->next, i++) {
1033 DBG("search %s\n", se->builtin ? "built-in" : se->path);
1034 if (se->builtin)
1035 bprio = i;
49b33c1f 1036 else if (newlen > se->len && newpath[se->len] == '/' &&
64b8b586
GSB
1037 memcmp(se->path, newpath, se->len) == 0)
1038 newprio = i;
49b33c1f 1039 else if (oldlen > se->len && oldpath[se->len] == '/' &&
64b8b586
GSB
1040 memcmp(se->path, oldpath, se->len) == 0)
1041 oldprio = i;
1042 }
1043
1044 if (newprio < 0)
1045 newprio = bprio;
1046 if (oldprio < 0)
1047 oldprio = bprio;
1048
1049 DBG("priorities: built-in: %d, old: %d, new: %d\n",
27881f6f 1050 bprio, oldprio, newprio);
64b8b586
GSB
1051
1052 return newprio <= oldprio;
1053}
1054
1055static int depmod_modules_search_file(struct depmod *depmod, size_t baselen, size_t namelen, const char *path)
1056{
1057 struct kmod_module *kmod;
1058 struct mod *mod;
3db5bf9d 1059 const char *relpath;
6daceb2f 1060 char modname[PATH_MAX];
64b8b586 1061 size_t modnamelen;
18a492e6 1062 int err;
64b8b586 1063
650f89cd 1064 if (!path_ends_with_kmod_ext(path + baselen, namelen))
64b8b586
GSB
1065 return 0;
1066
3db5bf9d 1067 if (path_to_modname(path, modname, &modnamelen) == NULL) {
63698377 1068 ERR("could not get modname from path %s\n", path);
3db5bf9d 1069 return -EINVAL;
64b8b586
GSB
1070 }
1071
3db5bf9d
GSB
1072 relpath = path + depmod->cfg->dirnamelen + 1;
1073 DBG("try %s (%s)\n", relpath, modname);
1074
64b8b586 1075 mod = hash_find(depmod->modules_by_name, modname);
3db5bf9d
GSB
1076 if (mod == NULL)
1077 goto add;
64b8b586 1078
64b8b586
GSB
1079 if (depmod_module_is_higher_priority(depmod, mod, baselen,
1080 namelen, modnamelen, path)) {
1081 DBG("Ignored lower priority: %s, higher: %s\n",
1082 path, mod->path);
64b8b586
GSB
1083 return 0;
1084 }
1085
3db5bf9d
GSB
1086 DBG("Replace lower priority %s with new module %s\n",
1087 mod->relpath, relpath);
1088 err = depmod_module_del(depmod, mod);
64b8b586 1089 if (err < 0) {
63698377 1090 ERR("could not del module %s: %s\n", mod->path, strerror(-err));
64b8b586
GSB
1091 return err;
1092 }
1093
3db5bf9d
GSB
1094add:
1095 err = kmod_module_new_from_path(depmod->ctx, path, &kmod);
1096 if (err < 0) {
63698377 1097 ERR("could not create module %s: %s\n", path, strerror(-err));
3db5bf9d
GSB
1098 return err;
1099 }
1100
1101 err = depmod_module_add(depmod, kmod);
1102 if (err < 0) {
63698377 1103 ERR("could not add module %s: %s\n",
3db5bf9d
GSB
1104 path, strerror(-err));
1105 kmod_module_unref(kmod);
1106 return err;
1107 }
64b8b586
GSB
1108 return 0;
1109}
1110
1111static int depmod_modules_search_dir(struct depmod *depmod, DIR *d, size_t baselen, char *path)
1112{
1113 struct dirent *de;
1114 int err = 0, dfd = dirfd(d);
1115
1116 while ((de = readdir(d)) != NULL) {
1117 const char *name = de->d_name;
1118 size_t namelen;
1119 uint8_t is_dir;
1120
1121 if (name[0] == '.' && (name[1] == '\0' ||
1122 (name[1] == '.' && name[2] == '\0')))
1123 continue;
1124 if (streq(name, "build") || streq(name, "source"))
1125 continue;
1126 namelen = strlen(name);
1127 if (baselen + namelen + 2 >= PATH_MAX) {
1128 path[baselen] = '\0';
1958af88 1129 ERR("path is too long %s%s\n", path, name);
64b8b586
GSB
1130 continue;
1131 }
1132 memcpy(path + baselen, name, namelen + 1);
1133
1134 if (de->d_type == DT_REG)
1135 is_dir = 0;
1136 else if (de->d_type == DT_DIR)
1137 is_dir = 1;
1138 else {
1139 struct stat st;
1140 if (fstatat(dfd, name, &st, 0) < 0) {
1141 ERR("fstatat(%d, %s): %m\n", dfd, name);
1142 continue;
1143 } else if (S_ISREG(st.st_mode))
1144 is_dir = 0;
1145 else if (S_ISDIR(st.st_mode))
1146 is_dir = 1;
1147 else {
1148 ERR("unsupported file type %s: %o\n",
1149 path, st.st_mode & S_IFMT);
1150 continue;
1151 }
1152 }
1153
1154 if (is_dir) {
1155 int fd;
1156 DIR *subdir;
1157 if (baselen + namelen + 2 + NAME_MAX >= PATH_MAX) {
1158 ERR("directory path is too long %s\n", path);
1159 continue;
1160 }
1161 fd = openat(dfd, name, O_RDONLY);
1162 if (fd < 0) {
1163 ERR("openat(%d, %s, O_RDONLY): %m\n",
1164 dfd, name);
1165 continue;
1166 }
1167 subdir = fdopendir(fd);
1168 if (subdir == NULL) {
1169 ERR("fdopendir(%d): %m\n", fd);
1170 close(fd);
1171 continue;
1172 }
1173 path[baselen + namelen] = '/';
1174 path[baselen + namelen + 1] = '\0';
1175 err = depmod_modules_search_dir(depmod, subdir,
1176 baselen + namelen + 1,
1177 path);
1178 closedir(subdir);
1179 } else {
1180 err = depmod_modules_search_file(depmod, baselen,
1181 namelen, path);
1182 }
1183
1184 if (err < 0) {
1185 path[baselen + namelen] = '\0';
1186 ERR("failed %s: %s\n", path, strerror(-err));
1187 err = 0; /* ignore errors */
1188 }
1189 }
1190
1191 return err;
1192}
1193
369ca656
YK
1194static int depmod_modules_search_path(struct depmod *depmod,
1195 const char *path)
64b8b586 1196{
369ca656
YK
1197 char path_buf[PATH_MAX];
1198 DIR *d;
64b8b586
GSB
1199 size_t baselen;
1200 int err;
369ca656
YK
1201
1202 d = opendir(path);
64b8b586
GSB
1203 if (d == NULL) {
1204 err = -errno;
369ca656 1205 ERR("could not open directory %s: %m\n", path);
64b8b586
GSB
1206 return err;
1207 }
1208
369ca656
YK
1209 baselen = strlen(path);
1210 memcpy(path_buf, path, baselen);
1211 path_buf[baselen] = '/';
64b8b586 1212 baselen++;
369ca656 1213 path_buf[baselen] = '\0';
64b8b586 1214
369ca656 1215 err = depmod_modules_search_dir(depmod, d, baselen, path_buf);
64b8b586 1216 closedir(d);
20c6e18c 1217 return err;
64b8b586
GSB
1218}
1219
369ca656
YK
1220static int depmod_modules_search(struct depmod *depmod)
1221{
1222 int err;
1223
1224 err = depmod_modules_search_path(depmod, depmod->cfg->dirname);
1225 if (err < 0)
1226 return err;
1227 return 0;
1228}
1229
64b8b586
GSB
1230static 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;
64b8b586
GSB
1233 return a->sort_idx - b->sort_idx;
1234}
1235
00bd3191
JAS
1236static int depmod_modules_build_array(struct depmod *depmod)
1237{
1238 struct hash_iter module_iter;
33557e8d 1239 const void *v;
00bd3191
JAS
1240 int err;
1241
1242 hash_iter_init(depmod->modules_by_name, &module_iter);
33557e8d
LDM
1243 while (hash_iter_next(&module_iter, NULL, &v)) {
1244 struct mod *mod = (struct mod *) v;
1245 mod->idx = depmod->modules.count;
00bd3191
JAS
1246 err = array_append(&depmod->modules, mod);
1247 if (err < 0)
1248 return err;
1249 }
1250
1251 return 0;
1252}
1253
64b8b586
GSB
1254static void depmod_modules_sort(struct depmod *depmod)
1255{
1256 char order_file[PATH_MAX], line[PATH_MAX];
1257 FILE *fp;
1258 unsigned idx = 0, total = 0;
1259
1260 snprintf(order_file, sizeof(order_file), "%s/modules.order",
1261 depmod->cfg->dirname);
1262 fp = fopen(order_file, "r");
1263 if (fp == NULL) {
56406fdb 1264 WRN("could not open %s: %m\n", order_file);
64b8b586
GSB
1265 return;
1266 }
1267
1268 while (fgets(line, sizeof(line), fp) != NULL) {
1269 size_t len = strlen(line);
1270 idx++;
1271 if (len == 0)
1272 continue;
1273 if (line[len - 1] != '\n') {
1274 ERR("%s:%u corrupted line misses '\\n'\n",
1275 order_file, idx);
1276 goto corrupted;
1277 }
1278 }
1279 total = idx + 1;
1280 idx = 0;
1281 fseek(fp, 0, SEEK_SET);
1282 while (fgets(line, sizeof(line), fp) != NULL) {
1283 size_t len = strlen(line);
1284 struct mod *mod;
1285
1286 idx++;
1287 if (len == 0)
1288 continue;
1289 line[len - 1] = '\0';
1290
88c247f7 1291 mod = hash_find(depmod->modules_by_uncrelpath, line);
64b8b586
GSB
1292 if (mod == NULL)
1293 continue;
1294 mod->sort_idx = idx - total;
1295 }
1296
1297 array_sort(&depmod->modules, mod_cmp);
1298 for (idx = 0; idx < depmod->modules.count; idx++) {
1299 struct mod *m = depmod->modules.array[idx];
1300 m->idx = idx;
1301 }
1302
1303corrupted:
1304 fclose(fp);
1305}
1306
f6b838e1 1307static int depmod_symbol_add(struct depmod *depmod, const char *name,
572a2711
AM
1308 bool prefix_skipped, uint64_t crc,
1309 const struct mod *owner)
64b8b586
GSB
1310{
1311 size_t namelen;
1312 int err;
1313 struct symbol *sym;
1314
572a2711 1315 if (!prefix_skipped && (name[0] == depmod->cfg->sym_prefix))
64b8b586
GSB
1316 name++;
1317
1318 namelen = strlen(name) + 1;
1319 sym = malloc(sizeof(struct symbol) + namelen);
1320 if (sym == NULL)
1321 return -ENOMEM;
1322
1323 sym->owner = (struct mod *)owner;
1324 sym->crc = crc;
1325 memcpy(sym->name, name, namelen);
1326
1327 err = hash_add(depmod->symbols, sym->name, sym);
1328 if (err < 0) {
1329 free(sym);
1330 return err;
1331 }
1332
1333 DBG("add %p sym=%s, owner=%p %s\n", sym, sym->name, owner,
1334 owner != NULL ? owner->path : "");
1335
1336 return 0;
1337}
1338
f6b838e1
LDM
1339static struct symbol *depmod_symbol_find(const struct depmod *depmod,
1340 const char *name)
64b8b586
GSB
1341{
1342 if (name[0] == '.') /* PPC64 needs this: .foo == foo */
1343 name++;
1344 if (name[0] == depmod->cfg->sym_prefix)
1345 name++;
1346 return hash_find(depmod->symbols, name);
1347}
1348
ec587f29 1349static int depmod_load_modules(struct depmod *depmod)
64b8b586 1350{
7062eca3 1351 struct mod **itr, **itr_end;
64b8b586
GSB
1352
1353 DBG("load symbols (%zd modules)\n", depmod->modules.count);
1354
7062eca3 1355 itr = (struct mod **)depmod->modules.array;
64b8b586
GSB
1356 itr_end = itr + depmod->modules.count;
1357 for (; itr < itr_end; itr++) {
7062eca3 1358 struct mod *mod = *itr;
64b8b586
GSB
1359 struct kmod_list *l, *list = NULL;
1360 int err = kmod_module_get_symbols(mod->kmod, &list);
1361 if (err < 0) {
819f79a2
DR
1362 if (err == -ENOENT)
1363 DBG("ignoring %s: no symbols\n", mod->path);
1364 else
1365 ERR("failed to load symbols from %s: %s\n",
1366 mod->path, strerror(-err));
7062eca3 1367 goto load_info;
64b8b586
GSB
1368 }
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);
572a2711 1372 depmod_symbol_add(depmod, name, false, crc, mod);
64b8b586
GSB
1373 }
1374 kmod_module_symbols_free_list(list);
7062eca3
LDM
1375
1376load_info:
1377 kmod_module_get_info(mod->kmod, &mod->info_list);
ec587f29
LDM
1378 kmod_module_get_dependency_symbols(mod->kmod,
1379 &mod->dep_sym_list);
e4a7352a
LDM
1380 kmod_module_unref(mod->kmod);
1381 mod->kmod = NULL;
64b8b586
GSB
1382 }
1383
1958af88 1384 DBG("loaded symbols (%zd modules, %u symbols)\n",
64b8b586
GSB
1385 depmod->modules.count, hash_get_count(depmod->symbols));
1386
1387 return 0;
1388}
1389
1390static int depmod_load_module_dependencies(struct depmod *depmod, struct mod *mod)
1391{
1392 const struct cfg *cfg = depmod->cfg;
ec587f29 1393 struct kmod_list *l;
64b8b586
GSB
1394
1395 DBG("do dependencies of %s\n", mod->path);
ec587f29 1396 kmod_list_foreach(l, mod->dep_sym_list) {
64b8b586
GSB
1397 const char *name = kmod_module_dependency_symbol_get_symbol(l);
1398 uint64_t crc = kmod_module_dependency_symbol_get_crc(l);
0de40463 1399 int bindtype = kmod_module_dependency_symbol_get_bind(l);
64b8b586 1400 struct symbol *sym = depmod_symbol_find(depmod, name);
0de40463 1401 uint8_t is_weak = bindtype == KMOD_SYMBOL_WEAK;
64b8b586
GSB
1402
1403 if (sym == NULL) {
1404 DBG("%s needs (%c) unknown symbol %s\n",
0de40463 1405 mod->path, bindtype, name);
64b8b586
GSB
1406 if (cfg->print_unknown && !is_weak)
1407 WRN("%s needs unknown symbol %s\n",
1408 mod->path, name);
1409 continue;
1410 }
1411
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",
1417 mod->path, name);
1418 }
1419
1420 mod_add_dependency(mod, sym);
1421 }
ec587f29 1422
64b8b586
GSB
1423 return 0;
1424}
1425
1426static int depmod_load_dependencies(struct depmod *depmod)
1427{
1428 struct mod **itr, **itr_end;
1429
1958af88 1430 DBG("load dependencies (%zd modules, %u symbols)\n",
64b8b586
GSB
1431 depmod->modules.count, hash_get_count(depmod->symbols));
1432
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;
ec587f29
LDM
1437
1438 if (mod->dep_sym_list == NULL) {
1439 DBG("ignoring %s: no dependency symbols\n", mod->path);
1440 continue;
1441 }
1442
64b8b586
GSB
1443 depmod_load_module_dependencies(depmod, mod);
1444 }
1445
1958af88 1446 DBG("loaded dependencies (%zd modules, %u symbols)\n",
64b8b586
GSB
1447 depmod->modules.count, hash_get_count(depmod->symbols));
1448
1449 return 0;
1450}
1451
1452static int dep_cmp(const void *pa, const void *pb)
1453{
1454 const struct mod *a = *(const struct mod **)pa;
1455 const struct mod *b = *(const struct mod **)pb;
64b8b586
GSB
1456 return a->dep_sort_idx - b->dep_sort_idx;
1457}
1458
1459static void depmod_sort_dependencies(struct depmod *depmod)
1460{
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);
1468 }
1469}
1470
c7ce9f0c
YK
1471struct vertex {
1472 struct vertex *parent;
1473 struct mod *mod;
1474};
1475
1476static struct vertex *vertex_new(struct mod *mod, struct vertex *parent)
1477{
1478 struct vertex *v;
1479
1480 v = malloc(sizeof(*v));
1481 if (v == NULL)
1482 return NULL;
1483
1484 v->parent = parent;
1485 v->mod = mod;
1486 return v;
1487}
1488
1489static void depmod_list_remove_data(struct kmod_list **list, void *data)
1490{
1491 struct kmod_list *l;
1492
1493 l = kmod_list_remove_data(*list, data);
1494 *list = l;
1495}
1496
cd019a39
YK
1497static int depmod_report_one_cycle(struct depmod *depmod,
1498 struct vertex *vertex,
1499 struct kmod_list **roots,
1500 struct hash *loop_set)
c89d2198
LDM
1501{
1502 const char sep[] = " -> ";
c7ce9f0c
YK
1503 size_t sz;
1504 char *buf;
1505 struct array reverse;
1506 int i;
1507 int n;
1508 struct vertex *v;
cd019a39 1509 int rc;
c89d2198 1510
c7ce9f0c 1511 array_init(&reverse, 3);
c89d2198 1512
c7ce9f0c
YK
1513 sz = 0;
1514 for (v = vertex->parent, n = 0;
1515 v != NULL;
1516 v = v->parent, n++) {
1517
1518 sz += v->mod->modnamesz - 1;
1519 array_append(&reverse, v);
cd019a39
YK
1520 rc = hash_add(loop_set, v->mod->modname, NULL);
1521 if (rc != 0)
1522 return rc;
1523 /* the hash will be freed where created */
c7ce9f0c
YK
1524 }
1525 sz += vertex->mod->modnamesz - 1;
1526
1527 buf = malloc(sz + n * strlen(sep) + 1);
1528
1529 sz = 0;
1530 for (i = reverse.count - 1; i >= 0; i--) {
1531 size_t len;
1532
1533 v = reverse.array[i];
1534
1535 len = v->mod->modnamesz - 1;
1536 memcpy(buf + sz, v->mod->modname, len);
1537 sz += len;
1538 strcpy(buf + sz, sep);
1539 sz += strlen(sep);
1540
1541 depmod_list_remove_data(roots, v->mod);
1542 }
1543 strcpy(buf + sz, vertex->mod->modname);
1544 ERR("Cycle detected: %s\n", buf);
1545
1546 free(buf);
1547 array_free_array(&reverse);
cd019a39
YK
1548
1549 return 0;
c7ce9f0c
YK
1550}
1551
1552static int depmod_report_cycles_from_root(struct depmod *depmod,
1553 struct mod *root_mod,
1554 struct kmod_list **roots,
1555 void **stack,
1556 size_t stack_size,
1557 struct hash *loop_set)
1558{
1559 struct kmod_list *free_list = NULL; /* struct vertex */
1560 struct kmod_list *l;
1561 struct vertex *root;
1562 struct vertex *vertex;
1563 struct vertex *v;
1564 struct mod *m;
1565 struct mod **itr, **itr_end;
1566 size_t is;
cd019a39 1567 int ret = -ENOMEM;
c7ce9f0c
YK
1568
1569 root = vertex_new(root_mod, NULL);
1570 if (root == NULL) {
1571 ERR("No memory to report cycles\n");
cd019a39 1572 goto out;
c7ce9f0c
YK
1573 }
1574
1575 l = kmod_list_append(free_list, root);
1576 if (l == NULL) {
1577 ERR("No memory to report cycles\n");
cd019a39 1578 goto out;
c7ce9f0c
YK
1579 }
1580 free_list = l;
1581
1582 is = 0;
1583 stack[is++] = (void *)root;
1584
1585 while (is > 0) {
1586 vertex = stack[--is];
1587 m = vertex->mod;
1588 /*
1589 * because of the topological sort we can start only
1590 * from part of a loop or from a branch after a loop
1591 */
1592 if (m->visited && m == root->mod) {
cd019a39
YK
1593 int rc;
1594 rc = depmod_report_one_cycle(depmod, vertex,
1595 roots, loop_set);
1596 if (rc != 0) {
1597 ret = rc;
1598 goto out;
1599 }
c7ce9f0c 1600 continue;
c89d2198
LDM
1601 }
1602
c7ce9f0c
YK
1603 m->visited = true;
1604 if (m->deps.count == 0) {
1605 /*
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
1612 * time.
1613 */
1614 depmod_list_remove_data(roots, m);
c89d2198 1615
c7ce9f0c
YK
1616 continue;
1617 }
c89d2198 1618
c7ce9f0c
YK
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);
1624 if (v == NULL) {
1625 ERR("No memory to report cycles\n");
cd019a39 1626 goto out;
c7ce9f0c
YK
1627 }
1628 assert(is < stack_size);
1629 stack[is++] = v;
c89d2198 1630
c7ce9f0c
YK
1631 l = kmod_list_append(free_list, v);
1632 if (l == NULL) {
1633 ERR("No memory to report cycles\n");
cd019a39 1634 goto out;
c89d2198 1635 }
c7ce9f0c 1636 free_list = l;
c89d2198 1637
c7ce9f0c
YK
1638 }
1639 }
cd019a39
YK
1640 ret = 0;
1641
1642out:
c7ce9f0c
YK
1643 while (free_list) {
1644 v = free_list->data;
1645 l = kmod_list_remove(free_list);
1646 free_list = l;
1647 free(v);
1648 }
c89d2198 1649
cd019a39 1650 return ret;
c7ce9f0c
YK
1651}
1652
1653static void depmod_report_cycles(struct depmod *depmod, uint16_t n_mods,
1654 uint16_t *users)
1655{
1656 int num_cyclic = 0;
1657 struct kmod_list *roots = NULL; /* struct mod */
1658 struct kmod_list *l;
1659 size_t n_r; /* local n_roots */
1660 int i;
1661 int err;
32007808 1662 _cleanup_free_ void **stack = NULL;
c7ce9f0c
YK
1663 struct mod *m;
1664 struct mod *root;
1665 struct hash *loop_set;
1666
1667 for (i = 0, n_r = 0; i < n_mods; i++) {
1668 if (users[i] <= 0)
1669 continue;
1670 m = depmod->modules.array[i];
1671 l = kmod_list_append(roots, m);
1672 if (l == NULL) {
1673 ERR("No memory to report cycles\n");
cd019a39 1674 goto out_list;
c89d2198 1675 }
c7ce9f0c
YK
1676 roots = l;
1677 n_r++;
1678 }
1679
1680 stack = malloc(n_r * sizeof(void *));
1681 if (stack == NULL) {
1682 ERR("No memory to report cycles\n");
cd019a39 1683 goto out_list;
c7ce9f0c
YK
1684 }
1685
1686 loop_set = hash_new(16, NULL);
1687 if (loop_set == NULL) {
1688 ERR("No memory to report cycles\n");
cd019a39 1689 goto out_list;
c7ce9f0c
YK
1690 }
1691
1692 while (roots != NULL) {
1693 root = roots->data;
1694 l = kmod_list_remove(roots);
1695 roots = l;
1696 err = depmod_report_cycles_from_root(depmod,
1697 root,
1698 &roots,
1699 stack, n_r, loop_set);
1700 if (err < 0)
cd019a39 1701 goto out_hash;
c89d2198 1702 }
6b77f188 1703
c7ce9f0c 1704 num_cyclic = hash_get_count(loop_set);
6b77f188 1705 ERR("Found %d modules in dependency cycles!\n", num_cyclic);
32007808 1706
cd019a39 1707out_hash:
c7ce9f0c 1708 hash_free(loop_set);
cd019a39
YK
1709out_list:
1710 while (roots != NULL) {
1711 /* no need to free data, come from outside */
1712 roots = kmod_list_remove(roots);
1713 }
c89d2198
LDM
1714}
1715
64b8b586
GSB
1716static int depmod_calculate_dependencies(struct depmod *depmod)
1717{
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;
c48b269d 1721 int ret = 0;
64b8b586
GSB
1722
1723 users = malloc(sizeof(uint16_t) * n_mods * 3);
1724 if (users == NULL)
1725 return -ENOMEM;
1726 roots = users + n_mods;
1727 sorted = roots + n_mods;
1728
1958af88 1729 DBG("calculate dependencies and ordering (%hu modules)\n", n_mods);
64b8b586
GSB
1730
1731 assert(depmod->modules.count < UINT16_MAX);
1732
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) {
1739 roots[n_roots] = i;
1740 n_roots++;
1741 }
1742 }
1743
1744 /* topological sort (outputs modules without users first) */
1745 while (n_roots > 0) {
1746 const struct mod **itr_dst, **itr_dst_end;
1747 struct mod *src;
1748 uint16_t src_idx = roots[--n_roots];
1749
1750 src = depmod->modules.array[src_idx];
1751 src->dep_sort_idx = n_sorted;
1752 sorted[n_sorted] = src_idx;
1753 n_sorted++;
1754
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);
1761 users[dst_idx]--;
1762 if (users[dst_idx] == 0) {
1763 roots[n_roots] = dst_idx;
1764 n_roots++;
1765 }
1766 }
1767 }
1768
1769 if (n_sorted < n_mods) {
c7ce9f0c 1770 depmod_report_cycles(depmod, n_mods, users);
c48b269d 1771 ret = -EINVAL;
c48b269d 1772 goto exit;
64b8b586
GSB
1773 }
1774
1775 depmod_sort_dependencies(depmod);
1776
c48b269d 1777 DBG("calculated dependencies and ordering (%hu modules)\n", n_mods);
64b8b586 1778
c48b269d 1779exit:
64b8b586 1780 free(users);
c48b269d 1781 return ret;
64b8b586
GSB
1782}
1783
1784static int depmod_load(struct depmod *depmod)
1785{
1786 int err;
1787
ec587f29 1788 err = depmod_load_modules(depmod);
64b8b586
GSB
1789 if (err < 0)
1790 return err;
1791
1792 err = depmod_load_dependencies(depmod);
1793 if (err < 0)
1794 return err;
1795
1796 err = depmod_calculate_dependencies(depmod);
1797 if (err < 0)
1798 return err;
1799
1800 return 0;
1801}
1802
8e3505c5
GSB
1803static size_t mod_count_all_dependencies(const struct mod *mod)
1804{
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);
1809 }
1810 return count;
1811}
1812
1813static int mod_fill_all_unique_dependencies(const struct mod *mod, const struct mod **deps, size_t n_deps, size_t *last)
1814{
1815 size_t i;
1816 int err = 0;
1817 for (i = 0; i < mod->deps.count; i++) {
1818 const struct mod *d = mod->deps.array[i];
1819 size_t j;
1820 uint8_t exists = 0;
1821
1822 for (j = 0; j < *last; j++) {
1823 if (deps[j] == d) {
1824 exists = 1;
1825 break;
1826 }
1827 }
1828
1829 if (exists)
1830 continue;
1831
1832 if (*last >= n_deps)
1833 return -ENOSPC;
1834 deps[*last] = d;
1835 (*last)++;
1836 err = mod_fill_all_unique_dependencies(d, deps, n_deps, last);
1837 if (err < 0)
1838 break;
1839 }
1840 return err;
1841}
1842
1843static const struct mod **mod_get_all_sorted_dependencies(const struct mod *mod, size_t *n_deps)
1844{
1845 const struct mod **deps;
1846 size_t last = 0;
1847
1848 *n_deps = mod_count_all_dependencies(mod);
1849 if (*n_deps == 0)
1850 return NULL;
1851
1852 deps = malloc(sizeof(struct mod *) * (*n_deps));
1853 if (deps == NULL)
1854 return NULL;
1855
1856 if (mod_fill_all_unique_dependencies(mod, deps, *n_deps, &last) < 0) {
1857 free(deps);
1858 return NULL;
1859 }
1860
1861 qsort(deps, last, sizeof(struct mod *), dep_cmp);
1862 *n_deps = last;
1863 return deps;
1864}
1865
1866static inline const char *mod_get_compressed_path(const struct mod *mod)
1867{
1868 if (mod->relpath != NULL)
1869 return mod->relpath;
1870 return mod->path;
1871}
1872
1873static int output_deps(struct depmod *depmod, FILE *out)
1874{
1875 size_t i;
1876
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);
1880 size_t j, n_deps;
1881
8e3505c5
GSB
1882 fprintf(out, "%s:", p);
1883
1884 if (mod->deps.count == 0)
1885 goto end;
1886
1887 deps = mod_get_all_sorted_dependencies(mod, &n_deps);
1888 if (deps == NULL) {
63698377 1889 ERR("could not get all sorted dependencies of %s\n", p);
8e3505c5
GSB
1890 goto end;
1891 }
1892
1893 for (j = 0; j < n_deps; j++) {
1894 const struct mod *d = deps[j];
8e3505c5
GSB
1895 fprintf(out, " %s", mod_get_compressed_path(d));
1896 }
1897 free(deps);
1898 end:
1899 putc('\n', out);
1900 }
1901
1902 return 0;
1903}
1904
7436788c
GSB
1905static int output_deps_bin(struct depmod *depmod, FILE *out)
1906{
1907 struct index_node *idx;
1908 size_t i;
1909
1910 if (out == stdout)
1911 return 0;
1912
1913 idx = index_create();
1914 if (idx == NULL)
1915 return -ENOMEM;
1916
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);
1920 char *line;
1921 size_t j, n_deps, linepos, linelen, slen;
1922 int duplicate;
1923
7436788c
GSB
1924 deps = mod_get_all_sorted_dependencies(mod, &n_deps);
1925 if (deps == NULL && n_deps > 0) {
63698377 1926 ERR("could not get all sorted dependencies of %s\n", p);
7436788c
GSB
1927 continue;
1928 }
1929
1930 linelen = strlen(p) + 1;
1931 for (j = 0; j < n_deps; j++) {
1932 const struct mod *d = deps[j];
7436788c
GSB
1933 linelen += 1 + strlen(mod_get_compressed_path(d));
1934 }
1935
1936 line = malloc(linelen + 1);
1937 if (line == NULL) {
1938 free(deps);
1939 ERR("modules.deps.bin: out of memory\n");
1940 continue;
1941 }
1942
1943 linepos = 0;
1944 slen = strlen(p);
1945 memcpy(line + linepos, p, slen);
1946 linepos += slen;
1947 line[linepos] = ':';
1948 linepos++;
1949
1950 for (j = 0; j < n_deps; j++) {
1951 const struct mod *d = deps[j];
1952 const char *dp;
c48b269d 1953
7436788c
GSB
1954 line[linepos] = ' ';
1955 linepos++;
1956
1957 dp = mod_get_compressed_path(d);
1958 slen = strlen(dp);
1959 memcpy(line + linepos, dp, slen);
1960 linepos += slen;
1961 }
1962 line[linepos] = '\0';
1963
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);
1967 free(line);
ea1b8c37 1968 free(deps);
7436788c
GSB
1969 }
1970
1971 index_write(idx, out);
1972 index_destroy(idx);
1973
1974 return 0;
1975}
1976
0d131745
GSB
1977static int output_aliases(struct depmod *depmod, FILE *out)
1978{
1979 size_t i;
1980
1981 fputs("# Aliases extracted from modules themselves.\n", out);
1982
1983 for (i = 0; i < depmod->modules.count; i++) {
1984 const struct mod *mod = depmod->modules.array[i];
7062eca3
LDM
1985 struct kmod_list *l;
1986
1987 kmod_list_foreach(l, mod->info_list) {
0d131745
GSB
1988 const char *key = kmod_module_info_get_key(l);
1989 const char *value = kmod_module_info_get_value(l);
1990
1991 if (!streq(key, "alias"))
1992 continue;
1993
447eed8c 1994 fprintf(out, "alias %s %s\n", value, mod->modname);
0d131745 1995 }
0d131745
GSB
1996 }
1997
1998 return 0;
1999}
2000
ec77abb9
GSB
2001static int output_aliases_bin(struct depmod *depmod, FILE *out)
2002{
ec77abb9
GSB
2003 struct index_node *idx;
2004 size_t i;
2005
2006 if (out == stdout)
2007 return 0;
2008
2009 idx = index_create();
2010 if (idx == NULL)
2011 return -ENOMEM;
2012
2013 for (i = 0; i < depmod->modules.count; i++) {
2014 const struct mod *mod = depmod->modules.array[i];
7062eca3
LDM
2015 struct kmod_list *l;
2016
2017 kmod_list_foreach(l, mod->info_list) {
ec77abb9
GSB
2018 const char *key = kmod_module_info_get_key(l);
2019 const char *value = kmod_module_info_get_value(l);
3753ae16 2020 char buf[PATH_MAX];
447eed8c 2021 const char *alias;
ec77abb9
GSB
2022 int duplicate;
2023
2024 if (!streq(key, "alias"))
2025 continue;
2026
3753ae16
LDM
2027 if (alias_normalize(value, buf, NULL) < 0) {
2028 WRN("Unmatched bracket in %s\n", value);
ec77abb9 2029 continue;
3753ae16
LDM
2030 }
2031 alias = buf;
ec77abb9 2032
447eed8c 2033 duplicate = index_insert(idx, alias, mod->modname,
ec77abb9
GSB
2034 mod->idx);
2035 if (duplicate && depmod->cfg->warn_dups)
2036 WRN("duplicate module alias:\n%s %s\n",
447eed8c 2037 alias, mod->modname);
ec77abb9 2038 }
ec77abb9
GSB
2039 }
2040
2041 index_write(idx, out);
2042 index_destroy(idx);
2043
2044 return 0;
2045}
2046
8bc830ef
GSB
2047static int output_softdeps(struct depmod *depmod, FILE *out)
2048{
2049 size_t i;
2050
2051 fputs("# Soft dependencies extracted from modules themselves.\n", out);
8bc830ef
GSB
2052
2053 for (i = 0; i < depmod->modules.count; i++) {
2054 const struct mod *mod = depmod->modules.array[i];
7062eca3
LDM
2055 struct kmod_list *l;
2056
2057 kmod_list_foreach(l, mod->info_list) {
8bc830ef
GSB
2058 const char *key = kmod_module_info_get_key(l);
2059 const char *value = kmod_module_info_get_value(l);
2060
2061 if (!streq(key, "softdep"))
2062 continue;
2063
447eed8c 2064 fprintf(out, "softdep %s %s\n", mod->modname, value);
8bc830ef 2065 }
8bc830ef
GSB
2066 }
2067
2068 return 0;
2069}
2070
9a14d0e9
GSB
2071static int output_symbols(struct depmod *depmod, FILE *out)
2072{
5cd13064 2073 struct hash_iter iter;
55021bed 2074 const void *v;
9a14d0e9
GSB
2075
2076 fputs("# Aliases for symbols, used by symbol_request().\n", out);
2077
5cd13064
LDM
2078 hash_iter_init(depmod->symbols, &iter);
2079
55021bed
REB
2080 while (hash_iter_next(&iter, NULL, &v)) {
2081 const struct symbol *sym = v;
5cd13064
LDM
2082 if (sym->owner == NULL)
2083 continue;
2084
2085 fprintf(out, "alias symbol:%s %s\n",
2086 sym->name, sym->owner->modname);
9a14d0e9
GSB
2087 }
2088
2089 return 0;
2090}
2091
75a9723b
GSB
2092static int output_symbols_bin(struct depmod *depmod, FILE *out)
2093{
2094 struct index_node *idx;
2095 char alias[1024];
cb51a641
LDM
2096 _cleanup_(scratchbuf_release) struct scratchbuf salias =
2097 SCRATCHBUF_INITIALIZER(alias);
5cd13064
LDM
2098 size_t baselen = sizeof("symbol:") - 1;
2099 struct hash_iter iter;
55021bed 2100 const void *v;
cb51a641 2101 int ret = 0;
75a9723b
GSB
2102
2103 if (out == stdout)
2104 return 0;
2105
2106 idx = index_create();
2107 if (idx == NULL)
2108 return -ENOMEM;
2109
2110 memcpy(alias, "symbol:", baselen);
cb51a641 2111
5cd13064 2112 hash_iter_init(depmod->symbols, &iter);
75a9723b 2113
55021bed 2114 while (hash_iter_next(&iter, NULL, &v)) {
5cd13064 2115 int duplicate;
55021bed 2116 const struct symbol *sym = v;
cb51a641 2117 size_t len;
75a9723b 2118
5cd13064
LDM
2119 if (sym->owner == NULL)
2120 continue;
75a9723b 2121
cb51a641
LDM
2122 len = strlen(sym->name);
2123
2124 if (scratchbuf_alloc(&salias, baselen + len + 1) < 0) {
2125 ret = -ENOMEM;
2126 goto err_scratchbuf;
2127 }
2128 memcpy(scratchbuf_str(&salias) + baselen, sym->name, len + 1);
5cd13064
LDM
2129 duplicate = index_insert(idx, alias, sym->owner->modname,
2130 sym->owner->idx);
2131
2132 if (duplicate && depmod->cfg->warn_dups)
2133 WRN("duplicate module syms:\n%s %s\n",
2134 alias, sym->owner->modname);
75a9723b
GSB
2135 }
2136
2137 index_write(idx, out);
cb51a641
LDM
2138
2139err_scratchbuf:
75a9723b 2140 index_destroy(idx);
5cd13064 2141
cb51a641
LDM
2142 if (ret < 0)
2143 ERR("output symbols: %s\n", strerror(-ret));
2144
2145 return ret;
75a9723b
GSB
2146}
2147
4b144e5f
GSB
2148static int output_builtin_bin(struct depmod *depmod, FILE *out)
2149{
2150 FILE *in;
2151 struct index_node *idx;
6daceb2f 2152 char infile[PATH_MAX], line[PATH_MAX], modname[PATH_MAX];
4b144e5f
GSB
2153
2154 if (out == stdout)
2155 return 0;
2156
2157 snprintf(infile, sizeof(infile), "%s/modules.builtin",
c5db1a3f 2158 depmod->cfg->dirname);
4b144e5f
GSB
2159 in = fopen(infile, "r");
2160 if (in == NULL) {
56406fdb 2161 WRN("could not open %s: %m\n", infile);
c5db1a3f 2162 return 0;
4b144e5f
GSB
2163 }
2164
2165 idx = index_create();
2166 if (idx == NULL) {
2167 fclose(in);
2168 return -ENOMEM;
2169 }
2170
2171 while (fgets(line, sizeof(line), in) != NULL) {
2172 if (!isalpha(line[0])) {
2173 ERR("Invalid modules.builtin line: %s\n", line);
2174 continue;
2175 }
2176
2177 path_to_modname(line, modname, NULL);
2178 index_insert(idx, modname, "", 0);
2179 }
2180
2181 index_write(idx, out);
2182 index_destroy(idx);
2183 fclose(in);
2184
2185 return 0;
2186}
2187
25c41512
GSB
2188static int output_devname(struct depmod *depmod, FILE *out)
2189{
2190 size_t i;
4c30a11d 2191 bool empty = true;
25c41512
GSB
2192
2193 for (i = 0; i < depmod->modules.count; i++) {
2194 const struct mod *mod = depmod->modules.array[i];
7062eca3 2195 struct kmod_list *l;
25c41512
GSB
2196 const char *devname = NULL;
2197 char type = '\0';
2198 unsigned int major = 0, minor = 0;
25c41512 2199
7062eca3 2200 kmod_list_foreach(l, mod->info_list) {
25c41512
GSB
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;
2204
2205 if (!streq(key, "alias"))
2206 continue;
2207
2208 if (strstartswith(value, "devname:"))
2209 devname = value + sizeof("devname:") - 1;
2210 else if (sscanf(value, "char-major-%u-%u",
2211 &maj, &min) == 2) {
2212 type = 'c';
2213 major = maj;
2214 minor = min;
2215 } else if (sscanf(value, "block-major-%u-%u",
2216 &maj, &min) == 2) {
2217 type = 'b';
2218 major = maj;
2219 minor = min;
2220 }
2221
6506ddf5
TG
2222 if (type != '\0' && devname != NULL)
2223 break;
2224 }
2225
2226 if (devname != NULL) {
4c30a11d
JT
2227 if (type != '\0') {
2228 if (empty) {
2229 fputs("# Device nodes to trigger on-demand module loading.\n",
2230 out);
2231 empty = false;
2232 }
447eed8c 2233 fprintf(out, "%s %s %c%u:%u\n", mod->modname,
25c41512 2234 devname, type, major, minor);
4c30a11d 2235 } else
6506ddf5
TG
2236 ERR("Module '%s' has devname (%s) but "
2237 "lacks major and minor information. "
2238 "Ignoring.\n", mod->modname, devname);
25c41512 2239 }
25c41512
GSB
2240 }
2241
2242 return 0;
2243}
2244
2245static int depmod_output(struct depmod *depmod, FILE *out)
2246{
2247 static const struct depfile {
2248 const char *name;
2249 int (*cb)(struct depmod *depmod, FILE *out);
2250 } *itr, depfiles[] = {
015946da
LDM
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 },
2260 { }
25c41512
GSB
2261 };
2262 const char *dname = depmod->cfg->dirname;
2263 int dfd, err = 0;
2264
2265 if (out != NULL)
2266 dfd = -1;
2267 else {
2268 dfd = open(dname, O_RDONLY);
2269 if (dfd < 0) {
2270 err = -errno;
63698377 2271 CRIT("could not open directory %s: %m\n", dname);
25c41512
GSB
2272 return err;
2273 }
2274 }
2275
2276 for (itr = depfiles; itr->name != NULL; itr++) {
2277 FILE *fp = out;
2278 char tmp[NAME_MAX] = "";
3f376cd8 2279 int r, ferr;
25c41512
GSB
2280
2281 if (fp == NULL) {
2282 int flags = O_CREAT | O_TRUNC | O_WRONLY;
2283 int mode = 0644;
2284 int fd;
2285
2286 snprintf(tmp, sizeof(tmp), "%s.tmp", itr->name);
2287 fd = openat(dfd, tmp, flags, mode);
2288 if (fd < 0) {
2289 ERR("openat(%s, %s, %o, %o): %m\n",
2290 dname, tmp, flags, mode);
2291 continue;
2292 }
2293 fp = fdopen(fd, "wb");
2294 if (fp == NULL) {
2295 ERR("fdopen(%d=%s/%s): %m\n", fd, dname, tmp);
2296 close(fd);
2297 continue;
2298 }
2299 }
2300
2301 r = itr->cb(depmod, fp);
2302 if (fp == out)
2303 continue;
2304
3f376cd8
LDM
2305 ferr = ferror(fp) | fclose(fp);
2306
25c41512
GSB
2307 if (r < 0) {
2308 if (unlinkat(dfd, tmp, 0) != 0)
2309 ERR("unlinkat(%s, %s): %m\n", dname, tmp);
80e49ad9
LDM
2310
2311 ERR("Could not write index '%s': %s\n", itr->name,
2312 strerror(-r));
2313 err = -errno;
2314 break;
2315 }
2316
2317 unlinkat(dfd, itr->name, 0);
2318 if (renameat(dfd, tmp, dfd, itr->name) != 0) {
2319 err = -errno;
2320 CRIT("renameat(%s, %s, %s, %s): %m\n",
2321 dname, tmp, dname, itr->name);
2322 break;
25c41512 2323 }
a4fb97a7 2324
3f376cd8 2325 if (ferr) {
a4fb97a7 2326 err = -ENOSPC;
3f376cd8
LDM
2327 ERR("Could not create index '%s'. Output is truncated: %s\n",
2328 itr->name, strerror(-err));
a4fb97a7
LDM
2329 break;
2330 }
25c41512
GSB
2331 }
2332
2333 if (dfd >= 0)
2334 close(dfd);
80e49ad9 2335
25c41512
GSB
2336 return err;
2337}
2338
4a0e46da
GSB
2339static void depmod_add_fake_syms(struct depmod *depmod)
2340{
2341 /* __this_module is magic inserted by kernel loader. */
572a2711 2342 depmod_symbol_add(depmod, "__this_module", true, 0, NULL);
4a0e46da 2343 /* On S390, this is faked up too */
572a2711 2344 depmod_symbol_add(depmod, "_GLOBAL_OFFSET_TABLE_", true, 0, NULL);
d46136bb
AB
2345 /* On PowerPC64 ABIv2, .TOC. is more or less _GLOBAL_OFFSET_TABLE_ */
2346 depmod_symbol_add(depmod, "TOC.", true, 0, NULL);
4a0e46da
GSB
2347}
2348
2349static int depmod_load_symvers(struct depmod *depmod, const char *filename)
2350{
2351 char line[10240];
2352 FILE *fp;
2353 unsigned int linenum = 0;
4a0e46da
GSB
2354
2355 fp = fopen(filename, "r");
035cbdc7
KR
2356 if (fp == NULL) {
2357 int err = -errno;
2358 DBG("load symvers: %s: %m\n", filename);
4a0e46da 2359 return err;
035cbdc7
KR
2360 }
2361 DBG("load symvers: %s\n", filename);
4a0e46da
GSB
2362
2363 /* eg. "0xb352177e\tfind_first_bit\tvmlinux\tEXPORT_SYMBOL" */
2364 while (fgets(line, sizeof(line), fp) != NULL) {
2365 const char *ver, *sym, *where;
2366 char *verend;
2367 uint64_t crc;
2368
2369 linenum++;
2370
2371 ver = strtok(line, " \t");
2372 sym = strtok(NULL, " \t");
2373 where = strtok(NULL, " \t");
2374 if (!ver || !sym || !where)
2375 continue;
2376
2377 if (!streq(where, "vmlinux"))
2378 continue;
2379
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);
2384 continue;
2385 }
2386
572a2711 2387 depmod_symbol_add(depmod, sym, false, crc, NULL);
4a0e46da
GSB
2388 }
2389 depmod_add_fake_syms(depmod);
2390
035cbdc7 2391 DBG("loaded symvers: %s\n", filename);
4a0e46da
GSB
2392
2393 fclose(fp);
035cbdc7 2394 return 0;
4a0e46da
GSB
2395}
2396
2397static int depmod_load_system_map(struct depmod *depmod, const char *filename)
2398{
2399 const char ksymstr[] = "__ksymtab_";
2400 const size_t ksymstr_len = sizeof(ksymstr) - 1;
2401 char line[10240];
2402 FILE *fp;
2403 unsigned int linenum = 0;
4a0e46da
GSB
2404
2405 fp = fopen(filename, "r");
035cbdc7
KR
2406 if (fp == NULL) {
2407 int err = -errno;
2408 DBG("load System.map: %s: %m\n", filename);
4a0e46da 2409 return err;
035cbdc7
KR
2410 }
2411 DBG("load System.map: %s\n", filename);
4a0e46da
GSB
2412
2413 /* eg. c0294200 R __ksymtab_devfs_alloc_devnum */
2414 while (fgets(line, sizeof(line), fp) != NULL) {
2415 char *p, *end;
2416
2417 linenum++;
2418
2419 p = strchr(line, ' ');
2420 if (p == NULL)
2421 goto invalid_syntax;
2422 p++;
2423 p = strchr(p, ' ');
2424 if (p == NULL)
2425 goto invalid_syntax;
2426 p++;
2427
572a2711
AM
2428 /* skip prefix */
2429 if (p[0] == depmod->cfg->sym_prefix)
2430 p++;
2431
4a0e46da
GSB
2432 /* Covers gpl-only and normal symbols. */
2433 if (strncmp(p, ksymstr, ksymstr_len) != 0)
2434 continue;
2435
2436 end = strchr(p, '\n');
2437 if (end != NULL)
2438 *end = '\0';
2439
572a2711 2440 depmod_symbol_add(depmod, p + ksymstr_len, true, 0, NULL);
4a0e46da
GSB
2441 continue;
2442
2443 invalid_syntax:
2444 ERR("%s:%u: invalid line: %s\n", filename, linenum, line);
2445 }
2446 depmod_add_fake_syms(depmod);
2447
035cbdc7 2448 DBG("loaded System.map: %s\n", filename);
4a0e46da
GSB
2449
2450 fclose(fp);
035cbdc7 2451 return 0;
4a0e46da 2452}
25c41512 2453
18cd9da3
GSB
2454
2455static int depfile_up_to_date_dir(DIR *d, time_t mtime, size_t baselen, char *path)
2456{
2457 struct dirent *de;
2458 int err = 1, dfd = dirfd(d);
2459
2460 while ((de = readdir(d)) != NULL) {
2461 const char *name = de->d_name;
2462 size_t namelen;
2463 struct stat st;
2464
2465 if (name[0] == '.' && (name[1] == '\0' ||
2466 (name[1] == '.' && name[2] == '\0')))
2467 continue;
2468 if (streq(name, "build") || streq(name, "source"))
2469 continue;
2470 namelen = strlen(name);
2471 if (baselen + namelen + 2 >= PATH_MAX) {
2472 path[baselen] = '\0';
1958af88 2473 ERR("path is too long %s%s\n", path, name);
18cd9da3
GSB
2474 continue;
2475 }
2476
2477 if (fstatat(dfd, name, &st, 0) < 0) {
2478 ERR("fstatat(%d, %s): %m\n", dfd, name);
2479 continue;
2480 }
2481
2482 if (S_ISDIR(st.st_mode)) {
2483 int fd;
2484 DIR *subdir;
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);
2488 continue;
2489 }
2490 fd = openat(dfd, name, O_RDONLY);
2491 if (fd < 0) {
2492 ERR("openat(%d, %s, O_RDONLY): %m\n",
2493 dfd, name);
2494 continue;
2495 }
2496 subdir = fdopendir(fd);
2497 if (subdir == NULL) {
2498 ERR("fdopendir(%d): %m\n", fd);
2499 close(fd);
2500 continue;
2501 }
2502 path[baselen + namelen] = '/';
2503 path[baselen + namelen + 1] = '\0';
2504 err = depfile_up_to_date_dir(subdir, mtime,
2505 baselen + namelen + 1,
2506 path);
2507 closedir(subdir);
2508 } else if (S_ISREG(st.st_mode)) {
650f89cd 2509 if (!path_ends_with_kmod_ext(name, namelen))
18cd9da3 2510 continue;
650f89cd 2511
18cd9da3
GSB
2512 memcpy(path + baselen, name, namelen + 1);
2513 err = st.st_mtime <= mtime;
2514 if (err == 0) {
2515 DBG("%s %"PRIu64" is newer than %"PRIu64"\n",
2516 path, (uint64_t)st.st_mtime,
2517 (uint64_t)mtime);
2518 }
2519 } else {
2520 ERR("unsupported file type %s: %o\n",
2521 path, st.st_mode & S_IFMT);
2522 continue;
2523 }
2524
2525 if (err == 0)
2526 break; /* outdated! */
2527 else if (err < 0) {
2528 path[baselen + namelen] = '\0';
2529 ERR("failed %s: %s\n", path, strerror(-err));
2530 err = 1; /* ignore errors */
2531 }
2532 }
2533
2534 return err;
2535}
2536
2537/* uptodate: 1, outdated: 0, errors < 0 */
64b8b586
GSB
2538static int depfile_up_to_date(const char *dirname)
2539{
18cd9da3
GSB
2540 char path[PATH_MAX];
2541 DIR *d = opendir(dirname);
2542 struct stat st;
2543 size_t baselen;
2544 int err;
2545 if (d == NULL) {
2546 err = -errno;
63698377 2547 ERR("could not open directory %s: %m\n", dirname);
18cd9da3
GSB
2548 return err;
2549 }
2550
2551 if (fstatat(dirfd(d), "modules.dep", &st, 0) != 0) {
2552 err = -errno;
63698377 2553 ERR("could not fstatat(%s, modules.dep): %m\n", dirname);
18cd9da3
GSB
2554 closedir(d);
2555 return err;
2556 }
2557
2558 baselen = strlen(dirname);
2559 memcpy(path, dirname, baselen);
2560 path[baselen] = '/';
2561 baselen++;
2562 path[baselen] = '\0';
2563
2564 err = depfile_up_to_date_dir(d, st.st_mtime, baselen, path);
2565 closedir(d);
2566 return err;
64b8b586
GSB
2567}
2568
2569static int is_version_number(const char *version)
2570{
2571 unsigned int d1, d2;
2572 return (sscanf(version, "%u.%u", &d1, &d2) == 2);
2573}
2574
f6cf14ce 2575static int do_depmod(int argc, char *argv[])
64b8b586
GSB
2576{
2577 FILE *out = NULL;
31f1d0d3 2578 int err = 0, all = 0, maybe_all = 0, n_config_paths = 0;
a07ea032
LDM
2579 _cleanup_free_ char *root = NULL;
2580 _cleanup_free_ const char **config_paths = NULL;
64b8b586
GSB
2581 const char *system_map = NULL;
2582 const char *module_symvers = NULL;
2583 const char *null_kmod_config = NULL;
2584 struct utsname un;
2585 struct kmod_ctx *ctx = NULL;
2586 struct cfg cfg;
2587 struct depmod depmod;
2588
2589 memset(&cfg, 0, sizeof(cfg));
2590 memset(&depmod, 0, sizeof(depmod));
2591
2592 for (;;) {
2593 int c, idx = 0;
2594 c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
2595 if (c == -1)
2596 break;
2597 switch (c) {
2598 case 'a':
2599 all = 1;
2600 break;
2601 case 'A':
2602 maybe_all = 1;
2603 break;
2604 case 'b':
a07ea032
LDM
2605 if (root)
2606 free(root);
2607 root = path_make_absolute_cwd(optarg);
64b8b586
GSB
2608 break;
2609 case 'C': {
2610 size_t bytes = sizeof(char *) * (n_config_paths + 2);
2611 void *tmp = realloc(config_paths, bytes);
2612 if (!tmp) {
2613 fputs("Error: out-of-memory\n", stderr);
2614 goto cmdline_failed;
2615 }
2616 config_paths = tmp;
2617 config_paths[n_config_paths] = optarg;
2618 n_config_paths++;
2619 config_paths[n_config_paths] = NULL;
2620 break;
2621 }
2622 case 'E':
2623 module_symvers = optarg;
2624 cfg.check_symvers = 1;
2625 break;
2626 case 'F':
2627 system_map = optarg;
2628 break;
2629 case 'e':
2630 cfg.print_unknown = 1;
2631 break;
2632 case 'v':
2633 verbose++;
2634 break;
2635 case 'n':
2636 out = stdout;
2637 break;
2638 case 'P':
2639 if (optarg[1] != '\0') {
2640 CRIT("-P only takes a single char\n");
2641 goto cmdline_failed;
2642 }
2643 cfg.sym_prefix = optarg[0];
2644 break;
2645 case 'w':
2646 cfg.warn_dups = 1;
2647 break;
2648 case 'u':
2649 case 'q':
2650 case 'r':
2651 case 'm':
61c48db3
LDM
2652 if (idx > 0)
2653 WRN("Ignored deprecated option --%s\n",
2654 cmdopts[idx].name);
2655 else
2656 WRN("Ignored deprecated option -%c\n", c);
2657
64b8b586
GSB
2658 break;
2659 case 'h':
4a2e20df 2660 help();
64b8b586
GSB
2661 return EXIT_SUCCESS;
2662 case 'V':
2663 puts(PACKAGE " version " VERSION);
655de275 2664 puts(KMOD_FEATURES);
64b8b586
GSB
2665 return EXIT_SUCCESS;
2666 case '?':
2667 goto cmdline_failed;
2668 default:
61c48db3 2669 ERR("unexpected getopt_long() value '%c'.\n", c);
64b8b586
GSB
2670 goto cmdline_failed;
2671 }
2672 }
2673
f3f62f5e
LA
2674 if (optind < argc) {
2675 if (!is_version_number(argv[optind])) {
2676 ERR("Bad version passed %s\n", argv[optind]);
2677 goto cmdline_failed;
2678 }
64b8b586
GSB
2679 cfg.kversion = argv[optind];
2680 optind++;
2681 } else {
2682 if (uname(&un) < 0) {
2683 CRIT("uname() failed: %s\n", strerror(errno));
2684 goto cmdline_failed;
2685 }
2686 cfg.kversion = un.release;
2687 }
2688
2689 cfg.dirnamelen = snprintf(cfg.dirname, PATH_MAX,
c5b37dba 2690 "%s/lib/modules/%s",
e15a56af 2691 root == NULL ? "" : root, cfg.kversion);
64b8b586
GSB
2692
2693 if (optind == argc)
2694 all = 1;
2695
2696 if (maybe_all) {
2697 if (out == stdout)
2698 goto done;
18cd9da3 2699 /* ignore up-to-date errors (< 0) */
50f43ce2 2700 if (depfile_up_to_date(cfg.dirname) == 1)
64b8b586
GSB
2701 goto done;
2702 all = 1;
2703 }
2704
2705 ctx = kmod_new(cfg.dirname, &null_kmod_config);
2706 if (ctx == NULL) {
2707 CRIT("kmod_new(\"%s\", {NULL}) failed: %m\n", cfg.dirname);
2708 goto cmdline_failed;
2709 }
52a50fe2
LDM
2710
2711 log_setup_kmod_log(ctx, verbose);
64b8b586
GSB
2712
2713 err = depmod_init(&depmod, &cfg, ctx);
2714 if (err < 0) {
2715 CRIT("depmod_init: %s\n", strerror(-err));
2716 goto depmod_init_failed;
2717 }
2718 ctx = NULL; /* owned by depmod */
2719
4a0e46da
GSB
2720 if (module_symvers != NULL) {
2721 err = depmod_load_symvers(&depmod, module_symvers);
2722 if (err < 0) {
63698377 2723 CRIT("could not load %s: %s\n", module_symvers,
4a0e46da
GSB
2724 strerror(-err));
2725 goto cmdline_failed;
2726 }
2727 } else if (system_map != NULL) {
2728 err = depmod_load_system_map(&depmod, system_map);
2729 if (err < 0) {
035cbdc7 2730 CRIT("could not load %s: %s\n", system_map,
4a0e46da
GSB
2731 strerror(-err));
2732 goto cmdline_failed;
2733 }
2734 } else if (cfg.print_unknown) {
2735 WRN("-e needs -E or -F\n");
2736 cfg.print_unknown = 0;
2737 }
2738
64b8b586
GSB
2739 if (all) {
2740 err = cfg_load(&cfg, config_paths);
2741 if (err < 0) {
63698377 2742 CRIT("could not load configuration files\n");
64b8b586
GSB
2743 goto cmdline_modules_failed;
2744 }
2745 err = depmod_modules_search(&depmod);
2746 if (err < 0) {
63698377 2747 CRIT("could not search modules: %s\n", strerror(-err));
64b8b586
GSB
2748 goto cmdline_modules_failed;
2749 }
2750 } else {
31f1d0d3
LDM
2751 int i;
2752
64b8b586
GSB
2753 for (i = optind; i < argc; i++) {
2754 const char *path = argv[i];
2755 struct kmod_module *mod;
2756
2757 if (path[0] != '/') {
b0bcadd0 2758 CRIT("%s: not absolute path.\n", path);
64b8b586
GSB
2759 goto cmdline_modules_failed;
2760 }
2761
2762 err = kmod_module_new_from_path(depmod.ctx, path, &mod);
2763 if (err < 0) {
63698377 2764 CRIT("could not create module %s: %s\n",
64b8b586
GSB
2765 path, strerror(-err));
2766 goto cmdline_modules_failed;
2767 }
2768
2769 err = depmod_module_add(&depmod, mod);
2770 if (err < 0) {
63698377 2771 CRIT("could not add module %s: %s\n",
64b8b586
GSB
2772 path, strerror(-err));
2773 kmod_module_unref(mod);
2774 goto cmdline_modules_failed;
2775 }
2776 }
2777 }
2778
00bd3191
JAS
2779 err = depmod_modules_build_array(&depmod);
2780 if (err < 0) {
2781 CRIT("could not build module array: %s\n",
2782 strerror(-err));
2783 goto cmdline_modules_failed;
2784 }
2785
64b8b586
GSB
2786 depmod_modules_sort(&depmod);
2787 err = depmod_load(&depmod);
2788 if (err < 0)
2789 goto cmdline_modules_failed;
2790
25c41512 2791 err = depmod_output(&depmod, out);
64b8b586
GSB
2792
2793done:
2794 depmod_shutdown(&depmod);
2795 cfg_free(&cfg);
64b8b586
GSB
2796 return err >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
2797
2798cmdline_modules_failed:
2799 depmod_shutdown(&depmod);
2800depmod_init_failed:
2801 if (ctx != NULL)
2802 kmod_unref(ctx);
2803cmdline_failed:
2804 cfg_free(&cfg);
64b8b586
GSB
2805 return EXIT_FAILURE;
2806}
f6cf14ce 2807
f6cf14ce
LDM
2808const struct kmod_cmd kmod_cmd_compat_depmod = {
2809 .name = "depmod",
2810 .cmd = do_depmod,
2811 .help = "compat depmod command",
2812};