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