blob: 116adbeb14a0f53aa1821e446518c52cbce17be1 [file] [log] [blame]
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001/*
2 * kmod-depmod - calculate modules.dep using libkmod.
3 *
Lucas De Marchie6b0e492013-01-16 11:27:21 -02004 * Copyright (C) 2011-2013 ProFUSION embedded systems
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02005 *
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 */
Lucas De Marchic2e42862014-10-03 01:41:42 -030019
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>
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -020033
Lucas De Marchi74d1df62014-10-03 00:22:36 -030034#include <shared/array.h>
Lucas De Marchi0db718e2014-10-03 00:29:18 -030035#include <shared/hash.h>
Lucas De Marchi576dd432014-10-02 22:03:19 -030036#include <shared/macro.h>
Lucas De Marchi96573a02014-10-03 00:01:35 -030037#include <shared/util.h>
Lucas De Marchicb51a642016-08-10 15:33:18 -030038#include <shared/scratchbuf.h>
Lucas De Marchi576dd432014-10-02 22:03:19 -030039
Yauheni Kaliutac7ce9f02017-02-22 11:41:59 +020040#include <libkmod/libkmod-internal.h>
41
42#undef ERR
43#undef DBG
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -020044
Lucas De Marchi4a2e20d2012-11-06 16:54:17 -020045#include "kmod.h"
46
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -020047#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",
Dave Reisnerc5b37db2012-09-27 11:00:42 -040054 "/lib/depmod.d",
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -020055 NULL
56};
57
58static const char cmdopts_s[] = "aAb:C:E:F:euqrvnP:wmVh";
59static const struct option cmdopts[] = {
Lucas De Marchi015946d2012-06-15 01:10:14 -030060 { "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' },
Andrey Mazoc02a8e62012-12-12 16:36:58 +040073 { "symbol-prefix", required_argument, 0, 'P' },
Lucas De Marchi015946d2012-06-15 01:10:14 -030074 { "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 { }
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -020079};
80
Lucas De Marchi4a2e20d2012-11-06 16:54:17 -020081static void help(void)
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -020082{
Lucas De Marchi34e06bf2012-11-06 17:32:41 -020083 printf("Usage:\n"
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -020084 "\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"
Gustavo Sverzut Barbieri59886522012-01-03 15:06:08 -020096 "\t-C, --config=PATH Read configuration from PATH\n"
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -020097 "\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",
Lucas De Marchi7c04aee2012-11-06 19:20:09 -0200108 program_invocation_short_name);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200109}
110
Lucas De Marchi1958af82013-04-21 16:16:18 -0300111_printf_format_(1, 2)
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200112static 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}
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200124#define SHOW(...) _show(__VA_ARGS__)
125
126
Gustavo Sverzut Barbieri0de40462011-12-23 23:11:41 -0200127/* binary index write *************************************************/
128#include <arpa/inet.h>
Gustavo Sverzut Barbieri0de40462011-12-23 23:11:41 -0200129/* 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
Lucas De Marchi778395e2014-10-28 01:52:49 -0200149/* see documentation in libkmod/libkmod-index.c */
Gustavo Sverzut Barbieri0de40462011-12-23 23:11:41 -0200150
Gustavo Sverzut Barbieri0de40462011-12-23 23:11:41 -0200151#define INDEX_MAGIC 0xB007F457
Gustavo Sverzut Barbieri0de40462011-12-23 23:11:41 -0200152#define INDEX_VERSION_MAJOR 0x0002
153#define INDEX_VERSION_MINOR 0x0001
154#define INDEX_VERSION ((INDEX_VERSION_MAJOR<<16)|INDEX_VERSION_MINOR)
Lucas De Marchi778395e2014-10-28 01:52:49 -0200155#define INDEX_CHILDMAX 128
Gustavo Sverzut Barbieri0de40462011-12-23 23:11:41 -0200156
157struct index_value {
158 struct index_value *next;
159 unsigned int priority;
160 char value[0];
161};
162
163/* In-memory index (depmod only) */
Gustavo Sverzut Barbieri0de40462011-12-23 23:11:41 -0200164struct 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
Gustavo Sverzut Barbieri0de40462011-12-23 23:11:41 -0200172
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);
Gustavo Sverzut Barbieri0de40462011-12-23 23:11:41 -0200374 offset |= INDEX_NODE_CHILDS;
375 }
376
Lucas De Marchia1bec0d2015-02-26 03:38:52 -0300377 free(child_offs);
378
Gustavo Sverzut Barbieri0de40462011-12-23 23:11:41 -0200379 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);
Lucas De Marchi22df4562015-02-28 14:45:07 -0300414 assert(initial_offset >= 0);
Gustavo Sverzut Barbieri0de40462011-12-23 23:11:41 -0200415 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);
Lucas De Marchi22df4562015-02-28 14:45:07 -0300423 assert(final_offset >= 0);
424 (void)fseek(out, initial_offset, SEEK_SET);
Gustavo Sverzut Barbieri0de40462011-12-23 23:11:41 -0200425 fwrite(&u, sizeof(uint32_t), 1, out);
Lucas De Marchi22df4562015-02-28 14:45:07 -0300426 (void)fseek(out, final_offset, SEEK_SET);
Gustavo Sverzut Barbieri0de40462011-12-23 23:11:41 -0200427}
428
429/* END: code from module-init-tools/index.c just modified to compile here.
430 */
431
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200432/* 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
458static int cfg_search_add(struct cfg *cfg, const char *path, uint8_t builtin)
459{
460 struct cfg_search *s;
461 size_t len;
462
463 if (builtin)
464 len = 0;
465 else
466 len = strlen(path) + 1;
467
468 s = malloc(sizeof(struct cfg_search) + len);
469 if (s == NULL) {
470 ERR("search add: out of memory\n");
471 return -ENOMEM;
472 }
473 s->builtin = builtin;
474 if (builtin)
475 s->len = 0;
476 else {
Gustavo Sverzut Barbieri026c7b42012-01-03 16:03:06 -0200477 s->len = len - 1;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200478 memcpy(s->path, path, len);
479 }
480
481 DBG("search add: %s, builtin=%hhu\n", path, builtin);
482
483 s->next = cfg->searches;
484 cfg->searches = s;
485 return 0;
486}
487
488static void cfg_search_free(struct cfg_search *s)
489{
490 free(s);
491}
492
493static int cfg_override_add(struct cfg *cfg, const char *modname, const char *subdir)
494{
495 struct cfg_override *o;
496 size_t modnamelen = strlen(modname);
497 size_t subdirlen = strlen(subdir);
498 size_t i;
499
Gustavo Sverzut Barbieri026c7b42012-01-03 16:03:06 -0200500 o = malloc(sizeof(struct cfg_override) + subdirlen + 1
501 + modnamelen + 1);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200502 if (o == NULL) {
503 ERR("override add: out of memory\n");
504 return -ENOMEM;
505 }
Gustavo Sverzut Barbieri026c7b42012-01-03 16:03:06 -0200506 memcpy(o->path, subdir, subdirlen);
507 i = subdirlen;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200508 o->path[i] = '/';
509 i++;
510
511 memcpy(o->path + i, modname, modnamelen);
512 i += modnamelen;
513 o->path[i] = '\0'; /* no extension, so we can match .ko/.ko.gz */
514
515 o->len = i;
516
517 DBG("override add: %s\n", o->path);
518
519 o->next = cfg->overrides;
520 cfg->overrides = o;
521 return 0;
522}
523
524static void cfg_override_free(struct cfg_override *o)
525{
526 free(o);
527}
528
529static int cfg_kernel_matches(const struct cfg *cfg, const char *pattern)
530{
531 regex_t re;
532 int status;
533
534 /* old style */
535 if (streq(pattern, "*"))
536 return 1;
537
538 if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB) != 0)
539 return 0;
540
541 status = regexec(&re, cfg->kversion, 0, NULL, 0);
542 regfree(&re);
543
544 return status == 0;
545}
546
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200547static int cfg_file_parse(struct cfg *cfg, const char *filename)
548{
549 char *line;
550 FILE *fp;
551 unsigned int linenum = 0;
552 int err;
553
554 fp = fopen(filename, "r");
555 if (fp == NULL) {
556 err = -errno;
Gustavo Sverzut Barbierib0bcadd2012-01-03 15:04:34 -0200557 ERR("file parse %s: %m\n", filename);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200558 return err;
559 }
560
Lucas De Marchiaafd3832014-10-03 03:25:06 -0300561 while ((line = freadline_wrapped(fp, &linenum)) != NULL) {
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200562 char *cmd, *saveptr;
563
564 if (line[0] == '\0' || line[0] == '#')
565 goto done_next;
566
567 cmd = strtok_r(line, "\t ", &saveptr);
568 if (cmd == NULL)
569 goto done_next;
570
571 if (streq(cmd, "search")) {
572 const char *sp;
573 while ((sp = strtok_r(NULL, "\t ", &saveptr)) != NULL) {
574 uint8_t builtin = streq(sp, CFG_BUILTIN_KEY);
575 cfg_search_add(cfg, sp, builtin);
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);
Gustavo Sverzut Barbieri8ea02fe2012-01-03 15:11:58 -0200660 dirlen -= namelen + 1;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200661 }
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{
Lucas De Marchi7e0385c2013-08-27 23:29:47 -0300713 struct dirent *dent;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200714 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
Michal Marek519d27d2014-03-04 16:51:25 +0100724 if (!S_ISDIR(st.st_mode)) {
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200725 cfg_files_insert_sorted(p_files, p_n_files, path, NULL);
726 return 0;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200727 }
728
729 d = opendir(path);
730 if (d == NULL) {
731 ERR("files list %s: %m\n", path);
732 return -EINVAL;
733 }
734
Lucas De Marchi7e0385c2013-08-27 23:29:47 -0300735 for (dent = readdir(d); dent != NULL; dent = readdir(d)) {
736 if (cfg_files_filter_out(d, path, dent->d_name))
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200737 continue;
738
Lucas De Marchi7e0385c2013-08-27 23:29:47 -0300739 cfg_files_insert_sorted(p_files, p_n_files, path, dent->d_name);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200740 }
741
742 closedir(d);
Khem Raj035cbdc2012-02-02 23:09:59 -0800743 DBG("parsed configuration files from %s\n", path);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200744 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)
769 cfg_search_add(cfg, "updates", 0);
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 ***********************************************/
Yauheni Kaliutac7ce9f02017-02-22 11:41:59 +0200791struct vertex;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200792struct mod {
793 struct kmod_module *kmod;
Lucas De Marchie4a73522012-10-30 03:46:12 -0200794 char *path;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200795 const char *relpath; /* path relative to '$ROOT/lib/modules/$VER/' */
Lucas De Marchib51ac402012-10-30 02:32:48 -0200796 char *uncrelpath; /* same as relpath but ending in .ko */
Lucas De Marchi7062eca2012-10-30 03:16:10 -0200797 struct kmod_list *info_list;
Lucas De Marchiec587f22012-10-30 03:16:10 -0200798 struct kmod_list *dep_sym_list;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200799 struct array deps; /* struct symbol */
800 size_t baselen; /* points to start of basename/filename */
Lucas De Marchia873f232014-05-30 09:01:24 -0300801 size_t modnamesz;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200802 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 */
Lucas De Marchic89d2192014-05-30 09:38:26 -0300806 bool visited; /* helper field to report cycles */
Yauheni Kaliutac7ce9f02017-02-22 11:41:59 +0200807 struct vertex *vertex; /* helper field to report cycles */
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200808 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;
Lucas De Marchi88c247f2012-10-03 16:28:24 -0300821 struct hash *modules_by_uncrelpath;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200822 struct hash *modules_by_name;
823 struct hash *symbols;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200824};
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);
Lucas De Marchi02c64df2012-11-16 12:05:42 -0200830 kmod_module_unref(mod->kmod);
Lucas De Marchi7062eca2012-10-30 03:16:10 -0200831 kmod_module_info_free_list(mod->info_list);
Lucas De Marchiec587f22012-10-30 03:16:10 -0200832 kmod_module_dependency_symbols_free_list(mod->dep_sym_list);
Lucas De Marchi88c247f2012-10-03 16:28:24 -0300833 free(mod->uncrelpath);
Lucas De Marchie4a73522012-10-30 03:46:12 -0200834 free(mod->path);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200835 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
Lucas De Marchif6b838e2012-01-14 02:03:21 -0200866static int depmod_init(struct depmod *depmod, struct cfg *cfg,
867 struct kmod_ctx *ctx)
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200868{
869 int err = 0;
870
871 depmod->cfg = cfg;
872 depmod->ctx = ctx;
873
874 array_init(&depmod->modules, 128);
875
Lucas De Marchi88c247f2012-10-03 16:28:24 -0300876 depmod->modules_by_uncrelpath = hash_new(512, NULL);
877 if (depmod->modules_by_uncrelpath == NULL) {
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200878 err = -errno;
Lucas De Marchi88c247f2012-10-03 16:28:24 -0300879 goto modules_by_uncrelpath_failed;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200880 }
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:
Lucas De Marchi88c247f2012-10-03 16:28:24 -0300899 hash_free(depmod->modules_by_uncrelpath);
900modules_by_uncrelpath_failed:
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200901 return err;
902}
903
904static void depmod_shutdown(struct depmod *depmod)
905{
906 size_t i;
907
908 hash_free(depmod->symbols);
909
Lucas De Marchi88c247f2012-10-03 16:28:24 -0300910 hash_free(depmod->modules_by_uncrelpath);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200911
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;
Lucas De Marchi88c247f2012-10-03 16:28:24 -0300924 const char *modname, *lastslash;
Lucas De Marchia873f232014-05-30 09:01:24 -0300925 size_t modnamesz;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200926 struct mod *mod;
927 int err;
928
929 modname = kmod_module_get_name(kmod);
Lucas De Marchia873f232014-05-30 09:01:24 -0300930 modnamesz = strlen(modname) + 1;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200931
Lucas De Marchia873f232014-05-30 09:01:24 -0300932 mod = calloc(1, sizeof(struct mod) + modnamesz);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200933 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;
Lucas De Marchia873f232014-05-30 09:01:24 -0300938 memcpy(mod->modname, modname, modnamesz);
939 mod->modnamesz = modnamesz;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200940
941 array_init(&mod->deps, 4);
942
Lucas De Marchie4a73522012-10-30 03:46:12 -0200943 mod->path = strdup(kmod_module_get_path(kmod));
Lucas De Marchi88c247f2012-10-03 16:28:24 -0300944 lastslash = strrchr(mod->path, '/');
945 mod->baselen = lastslash - mod->path;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200946 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
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200952 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));
Lucas De Marchi88c247f2012-10-03 16:28:24 -0300955 goto fail;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200956 }
957
958 if (mod->relpath != NULL) {
Lucas De Marchia873f232014-05-30 09:01:24 -0300959 size_t uncrelpathlen = lastslash - mod->relpath + modnamesz
Lucas De Marchib95506f2014-10-08 14:33:42 -0300960 + strlen(KMOD_EXTENSION_UNCOMPRESSED);
Lucas De Marchi88c247f2012-10-03 16:28:24 -0300961 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);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200965 if (err < 0) {
966 ERR("hash_add_unique %s: %s\n",
Lucas De Marchi06294622012-11-16 11:35:30 -0200967 mod->uncrelpath, strerror(-err));
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200968 hash_del(depmod->modules_by_name, mod->modname);
Lucas De Marchi88c247f2012-10-03 16:28:24 -0300969 goto fail;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200970 }
971 }
972
973 DBG("add %p kmod=%p, path=%s\n", mod, kmod, mod->path);
974
975 return 0;
Lucas De Marchi88c247f2012-10-03 16:28:24 -0300976
977fail:
978 free(mod->uncrelpath);
979 free(mod);
980 return err;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200981}
982
Gustavo Sverzut Barbieri3db5bf92012-01-03 16:14:57 -0200983static int depmod_module_del(struct depmod *depmod, struct mod *mod)
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200984{
Gustavo Sverzut Barbieri3db5bf92012-01-03 16:14:57 -0200985 DBG("del %p kmod=%p, path=%s\n", mod, mod->kmod, mod->path);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200986
Lucas De Marchi06294622012-11-16 11:35:30 -0200987 if (mod->uncrelpath != NULL)
988 hash_del(depmod->modules_by_uncrelpath, mod->uncrelpath);
Gustavo Sverzut Barbieri3db5bf92012-01-03 16:14:57 -0200989
990 hash_del(depmod->modules_by_name, mod->modname);
991
Gustavo Sverzut Barbieri3db5bf92012-01-03 16:14:57 -0200992 mod_free(mod);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200993 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;
Lucas De Marchia873f232014-05-30 09:01:24 -03001004
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 */
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001008 size_t newlen = baselen + modnamelen;
Lucas De Marchia873f232014-05-30 09:01:24 -03001009 size_t oldlen = mod->baselen + mod->modnamesz;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001010 const char *oldpath = mod->path;
1011 int i, bprio = -1, oldprio = -1, newprio = -1;
1012
Gustavo Sverzut Barbieri026c7b42012-01-03 16:03:06 -02001013 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
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001021 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;
Anssi Hannula49b33c12014-03-19 01:26:00 +02001036 else if (newlen > se->len && newpath[se->len] == '/' &&
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001037 memcmp(se->path, newpath, se->len) == 0)
1038 newprio = i;
Anssi Hannula49b33c12014-03-19 01:26:00 +02001039 else if (oldlen > se->len && oldpath[se->len] == '/' &&
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001040 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",
Anssi Hannula27881f62014-03-19 01:26:01 +02001050 bprio, oldprio, newprio);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001051
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;
Gustavo Sverzut Barbieri3db5bf92012-01-03 16:14:57 -02001059 const char *relpath;
Lucas De Marchi6daceb22012-01-08 01:02:29 -02001060 char modname[PATH_MAX];
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001061 size_t modnamelen;
Lucas De Marchi18a492e2012-04-26 11:39:54 -03001062 int err;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001063
Lucas De Marchi650f89c2012-11-28 14:26:23 -02001064 if (!path_ends_with_kmod_ext(path + baselen, namelen))
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001065 return 0;
1066
Gustavo Sverzut Barbieri3db5bf92012-01-03 16:14:57 -02001067 if (path_to_modname(path, modname, &modnamelen) == NULL) {
Dave Reisner63698372012-01-04 10:41:50 -05001068 ERR("could not get modname from path %s\n", path);
Gustavo Sverzut Barbieri3db5bf92012-01-03 16:14:57 -02001069 return -EINVAL;
1070 }
1071
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001072 relpath = path + depmod->cfg->dirnamelen + 1;
Gustavo Sverzut Barbieri3db5bf92012-01-03 16:14:57 -02001073 DBG("try %s (%s)\n", relpath, modname);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001074
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001075 mod = hash_find(depmod->modules_by_name, modname);
Gustavo Sverzut Barbieri3db5bf92012-01-03 16:14:57 -02001076 if (mod == NULL)
1077 goto add;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001078
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001079 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);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001083 return 0;
1084 }
1085
Gustavo Sverzut Barbieri3db5bf92012-01-03 16:14:57 -02001086 DBG("Replace lower priority %s with new module %s\n",
1087 mod->relpath, relpath);
1088 err = depmod_module_del(depmod, mod);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001089 if (err < 0) {
Dave Reisner63698372012-01-04 10:41:50 -05001090 ERR("could not del module %s: %s\n", mod->path, strerror(-err));
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001091 return err;
1092 }
1093
Gustavo Sverzut Barbieri3db5bf92012-01-03 16:14:57 -02001094add:
1095 err = kmod_module_new_from_path(depmod->ctx, path, &kmod);
1096 if (err < 0) {
Dave Reisner63698372012-01-04 10:41:50 -05001097 ERR("could not create module %s: %s\n", path, strerror(-err));
Gustavo Sverzut Barbieri3db5bf92012-01-03 16:14:57 -02001098 return err;
1099 }
1100
1101 err = depmod_module_add(depmod, kmod);
1102 if (err < 0) {
Dave Reisner63698372012-01-04 10:41:50 -05001103 ERR("could not add module %s: %s\n",
Gustavo Sverzut Barbieri3db5bf92012-01-03 16:14:57 -02001104 path, strerror(-err));
1105 kmod_module_unref(kmod);
1106 return err;
1107 }
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001108 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';
Lucas De Marchi1958af82013-04-21 16:16:18 -03001129 ERR("path is too long %s%s\n", path, name);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001130 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
1194static int depmod_modules_search(struct depmod *depmod)
1195{
1196 char path[PATH_MAX];
1197 DIR *d = opendir(depmod->cfg->dirname);
1198 size_t baselen;
1199 int err;
1200 if (d == NULL) {
1201 err = -errno;
Dave Reisner63698372012-01-04 10:41:50 -05001202 ERR("could not open directory %s: %m\n", depmod->cfg->dirname);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001203 return err;
1204 }
1205
1206 baselen = depmod->cfg->dirnamelen;
1207 memcpy(path, depmod->cfg->dirname, baselen);
1208 path[baselen] = '/';
1209 baselen++;
1210 path[baselen] = '\0';
1211
1212 err = depmod_modules_search_dir(depmod, d, baselen, path);
1213 closedir(d);
Lucas De Marchi20c6e182012-04-26 11:40:26 -03001214 return err;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001215}
1216
1217static int mod_cmp(const void *pa, const void *pb) {
1218 const struct mod *a = *(const struct mod **)pa;
1219 const struct mod *b = *(const struct mod **)pb;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001220 return a->sort_idx - b->sort_idx;
1221}
1222
Jan Alexander Steffens (heftig)00bd3192012-01-11 23:17:38 +01001223static int depmod_modules_build_array(struct depmod *depmod)
1224{
1225 struct hash_iter module_iter;
Lucas De Marchi33557e82012-01-14 02:04:56 -02001226 const void *v;
Jan Alexander Steffens (heftig)00bd3192012-01-11 23:17:38 +01001227 int err;
1228
1229 hash_iter_init(depmod->modules_by_name, &module_iter);
Lucas De Marchi33557e82012-01-14 02:04:56 -02001230 while (hash_iter_next(&module_iter, NULL, &v)) {
1231 struct mod *mod = (struct mod *) v;
1232 mod->idx = depmod->modules.count;
Jan Alexander Steffens (heftig)00bd3192012-01-11 23:17:38 +01001233 err = array_append(&depmod->modules, mod);
1234 if (err < 0)
1235 return err;
1236 }
1237
1238 return 0;
1239}
1240
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001241static void depmod_modules_sort(struct depmod *depmod)
1242{
1243 char order_file[PATH_MAX], line[PATH_MAX];
1244 FILE *fp;
1245 unsigned idx = 0, total = 0;
1246
1247 snprintf(order_file, sizeof(order_file), "%s/modules.order",
1248 depmod->cfg->dirname);
1249 fp = fopen(order_file, "r");
1250 if (fp == NULL) {
Lucas De Marchi56406fd2012-01-04 20:53:10 -02001251 WRN("could not open %s: %m\n", order_file);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001252 return;
1253 }
1254
1255 while (fgets(line, sizeof(line), fp) != NULL) {
1256 size_t len = strlen(line);
1257 idx++;
1258 if (len == 0)
1259 continue;
1260 if (line[len - 1] != '\n') {
1261 ERR("%s:%u corrupted line misses '\\n'\n",
1262 order_file, idx);
1263 goto corrupted;
1264 }
1265 }
1266 total = idx + 1;
1267 idx = 0;
1268 fseek(fp, 0, SEEK_SET);
1269 while (fgets(line, sizeof(line), fp) != NULL) {
1270 size_t len = strlen(line);
1271 struct mod *mod;
1272
1273 idx++;
1274 if (len == 0)
1275 continue;
1276 line[len - 1] = '\0';
1277
Lucas De Marchi88c247f2012-10-03 16:28:24 -03001278 mod = hash_find(depmod->modules_by_uncrelpath, line);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001279 if (mod == NULL)
1280 continue;
1281 mod->sort_idx = idx - total;
1282 }
1283
1284 array_sort(&depmod->modules, mod_cmp);
1285 for (idx = 0; idx < depmod->modules.count; idx++) {
1286 struct mod *m = depmod->modules.array[idx];
1287 m->idx = idx;
1288 }
1289
1290corrupted:
1291 fclose(fp);
1292}
1293
Lucas De Marchif6b838e2012-01-14 02:03:21 -02001294static int depmod_symbol_add(struct depmod *depmod, const char *name,
Andrey Mazo572a2712012-12-12 16:39:18 +04001295 bool prefix_skipped, uint64_t crc,
1296 const struct mod *owner)
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001297{
1298 size_t namelen;
1299 int err;
1300 struct symbol *sym;
1301
Andrey Mazo572a2712012-12-12 16:39:18 +04001302 if (!prefix_skipped && (name[0] == depmod->cfg->sym_prefix))
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001303 name++;
1304
1305 namelen = strlen(name) + 1;
1306 sym = malloc(sizeof(struct symbol) + namelen);
1307 if (sym == NULL)
1308 return -ENOMEM;
1309
1310 sym->owner = (struct mod *)owner;
1311 sym->crc = crc;
1312 memcpy(sym->name, name, namelen);
1313
1314 err = hash_add(depmod->symbols, sym->name, sym);
1315 if (err < 0) {
1316 free(sym);
1317 return err;
1318 }
1319
1320 DBG("add %p sym=%s, owner=%p %s\n", sym, sym->name, owner,
1321 owner != NULL ? owner->path : "");
1322
1323 return 0;
1324}
1325
Lucas De Marchif6b838e2012-01-14 02:03:21 -02001326static struct symbol *depmod_symbol_find(const struct depmod *depmod,
1327 const char *name)
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001328{
1329 if (name[0] == '.') /* PPC64 needs this: .foo == foo */
1330 name++;
1331 if (name[0] == depmod->cfg->sym_prefix)
1332 name++;
1333 return hash_find(depmod->symbols, name);
1334}
1335
Lucas De Marchiec587f22012-10-30 03:16:10 -02001336static int depmod_load_modules(struct depmod *depmod)
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001337{
Lucas De Marchi7062eca2012-10-30 03:16:10 -02001338 struct mod **itr, **itr_end;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001339
1340 DBG("load symbols (%zd modules)\n", depmod->modules.count);
1341
Lucas De Marchi7062eca2012-10-30 03:16:10 -02001342 itr = (struct mod **)depmod->modules.array;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001343 itr_end = itr + depmod->modules.count;
1344 for (; itr < itr_end; itr++) {
Lucas De Marchi7062eca2012-10-30 03:16:10 -02001345 struct mod *mod = *itr;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001346 struct kmod_list *l, *list = NULL;
1347 int err = kmod_module_get_symbols(mod->kmod, &list);
1348 if (err < 0) {
Dave Reisner819f79a2012-05-07 19:41:42 -04001349 if (err == -ENOENT)
1350 DBG("ignoring %s: no symbols\n", mod->path);
1351 else
1352 ERR("failed to load symbols from %s: %s\n",
1353 mod->path, strerror(-err));
Lucas De Marchi7062eca2012-10-30 03:16:10 -02001354 goto load_info;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001355 }
1356 kmod_list_foreach(l, list) {
1357 const char *name = kmod_module_symbol_get_symbol(l);
1358 uint64_t crc = kmod_module_symbol_get_crc(l);
Andrey Mazo572a2712012-12-12 16:39:18 +04001359 depmod_symbol_add(depmod, name, false, crc, mod);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001360 }
1361 kmod_module_symbols_free_list(list);
Lucas De Marchi7062eca2012-10-30 03:16:10 -02001362
1363load_info:
1364 kmod_module_get_info(mod->kmod, &mod->info_list);
Lucas De Marchiec587f22012-10-30 03:16:10 -02001365 kmod_module_get_dependency_symbols(mod->kmod,
1366 &mod->dep_sym_list);
Lucas De Marchie4a73522012-10-30 03:46:12 -02001367 kmod_module_unref(mod->kmod);
1368 mod->kmod = NULL;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001369 }
1370
Lucas De Marchi1958af82013-04-21 16:16:18 -03001371 DBG("loaded symbols (%zd modules, %u symbols)\n",
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001372 depmod->modules.count, hash_get_count(depmod->symbols));
1373
1374 return 0;
1375}
1376
1377static int depmod_load_module_dependencies(struct depmod *depmod, struct mod *mod)
1378{
1379 const struct cfg *cfg = depmod->cfg;
Lucas De Marchiec587f22012-10-30 03:16:10 -02001380 struct kmod_list *l;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001381
1382 DBG("do dependencies of %s\n", mod->path);
Lucas De Marchiec587f22012-10-30 03:16:10 -02001383 kmod_list_foreach(l, mod->dep_sym_list) {
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001384 const char *name = kmod_module_dependency_symbol_get_symbol(l);
1385 uint64_t crc = kmod_module_dependency_symbol_get_crc(l);
Gustavo Sverzut Barbieri0de40462011-12-23 23:11:41 -02001386 int bindtype = kmod_module_dependency_symbol_get_bind(l);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001387 struct symbol *sym = depmod_symbol_find(depmod, name);
Gustavo Sverzut Barbieri0de40462011-12-23 23:11:41 -02001388 uint8_t is_weak = bindtype == KMOD_SYMBOL_WEAK;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001389
1390 if (sym == NULL) {
1391 DBG("%s needs (%c) unknown symbol %s\n",
Gustavo Sverzut Barbieri0de40462011-12-23 23:11:41 -02001392 mod->path, bindtype, name);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001393 if (cfg->print_unknown && !is_weak)
1394 WRN("%s needs unknown symbol %s\n",
1395 mod->path, name);
1396 continue;
1397 }
1398
1399 if (cfg->check_symvers && sym->crc != crc && !is_weak) {
1400 DBG("symbol %s (%#"PRIx64") module %s (%#"PRIx64")\n",
1401 sym->name, sym->crc, mod->path, crc);
1402 if (cfg->print_unknown)
1403 WRN("%s disagrees about version of symbol %s\n",
1404 mod->path, name);
1405 }
1406
1407 mod_add_dependency(mod, sym);
1408 }
Lucas De Marchiec587f22012-10-30 03:16:10 -02001409
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001410 return 0;
1411}
1412
1413static int depmod_load_dependencies(struct depmod *depmod)
1414{
1415 struct mod **itr, **itr_end;
1416
Lucas De Marchi1958af82013-04-21 16:16:18 -03001417 DBG("load dependencies (%zd modules, %u symbols)\n",
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001418 depmod->modules.count, hash_get_count(depmod->symbols));
1419
1420 itr = (struct mod **)depmod->modules.array;
1421 itr_end = itr + depmod->modules.count;
1422 for (; itr < itr_end; itr++) {
1423 struct mod *mod = *itr;
Lucas De Marchiec587f22012-10-30 03:16:10 -02001424
1425 if (mod->dep_sym_list == NULL) {
1426 DBG("ignoring %s: no dependency symbols\n", mod->path);
1427 continue;
1428 }
1429
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001430 depmod_load_module_dependencies(depmod, mod);
1431 }
1432
Lucas De Marchi1958af82013-04-21 16:16:18 -03001433 DBG("loaded dependencies (%zd modules, %u symbols)\n",
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001434 depmod->modules.count, hash_get_count(depmod->symbols));
1435
1436 return 0;
1437}
1438
1439static int dep_cmp(const void *pa, const void *pb)
1440{
1441 const struct mod *a = *(const struct mod **)pa;
1442 const struct mod *b = *(const struct mod **)pb;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001443 return a->dep_sort_idx - b->dep_sort_idx;
1444}
1445
1446static void depmod_sort_dependencies(struct depmod *depmod)
1447{
1448 struct mod **itr, **itr_end;
1449 itr = (struct mod **)depmod->modules.array;
1450 itr_end = itr + depmod->modules.count;
1451 for (; itr < itr_end; itr++) {
1452 struct mod *m = *itr;
1453 if (m->deps.count > 1)
1454 array_sort(&m->deps, dep_cmp);
1455 }
1456}
1457
Yauheni Kaliutac7ce9f02017-02-22 11:41:59 +02001458struct vertex {
1459 struct vertex *parent;
1460 struct mod *mod;
1461};
1462
1463static struct vertex *vertex_new(struct mod *mod, struct vertex *parent)
1464{
1465 struct vertex *v;
1466
1467 v = malloc(sizeof(*v));
1468 if (v == NULL)
1469 return NULL;
1470
1471 v->parent = parent;
1472 v->mod = mod;
1473 return v;
1474}
1475
1476static void depmod_list_remove_data(struct kmod_list **list, void *data)
1477{
1478 struct kmod_list *l;
1479
1480 l = kmod_list_remove_data(*list, data);
1481 *list = l;
1482}
1483
1484static void depmod_report_one_cycle(struct depmod *depmod,
1485 struct vertex *vertex,
1486 struct kmod_list **roots,
1487 struct hash *loop_set)
Lucas De Marchic89d2192014-05-30 09:38:26 -03001488{
1489 const char sep[] = " -> ";
Yauheni Kaliutac7ce9f02017-02-22 11:41:59 +02001490 size_t sz;
1491 char *buf;
1492 struct array reverse;
1493 int i;
1494 int n;
1495 struct vertex *v;
Lucas De Marchic89d2192014-05-30 09:38:26 -03001496
Yauheni Kaliutac7ce9f02017-02-22 11:41:59 +02001497 array_init(&reverse, 3);
Lucas De Marchic89d2192014-05-30 09:38:26 -03001498
Yauheni Kaliutac7ce9f02017-02-22 11:41:59 +02001499 sz = 0;
1500 for (v = vertex->parent, n = 0;
1501 v != NULL;
1502 v = v->parent, n++) {
Lucas De Marchic89d2192014-05-30 09:38:26 -03001503
Yauheni Kaliutac7ce9f02017-02-22 11:41:59 +02001504 sz += v->mod->modnamesz - 1;
1505 array_append(&reverse, v);
1506 hash_add(loop_set, v->mod->modname, NULL);
1507 }
1508 sz += vertex->mod->modnamesz - 1;
Lucas De Marchic89d2192014-05-30 09:38:26 -03001509
Yauheni Kaliutac7ce9f02017-02-22 11:41:59 +02001510 buf = malloc(sz + n * strlen(sep) + 1);
Lucas De Marchic89d2192014-05-30 09:38:26 -03001511
Yauheni Kaliutac7ce9f02017-02-22 11:41:59 +02001512 sz = 0;
1513 for (i = reverse.count - 1; i >= 0; i--) {
1514 size_t len;
Lucas De Marchic89d2192014-05-30 09:38:26 -03001515
Yauheni Kaliutac7ce9f02017-02-22 11:41:59 +02001516 v = reverse.array[i];
Lucas De Marchic89d2192014-05-30 09:38:26 -03001517
Yauheni Kaliutac7ce9f02017-02-22 11:41:59 +02001518 len = v->mod->modnamesz - 1;
1519 memcpy(buf + sz, v->mod->modname, len);
1520 sz += len;
1521 strcpy(buf + sz, sep);
1522 sz += strlen(sep);
Lucas De Marchic89d2192014-05-30 09:38:26 -03001523
Yauheni Kaliutac7ce9f02017-02-22 11:41:59 +02001524 depmod_list_remove_data(roots, v->mod);
1525 }
1526 strcpy(buf + sz, vertex->mod->modname);
1527 ERR("Cycle detected: %s\n", buf);
Lucas De Marchic89d2192014-05-30 09:38:26 -03001528
Yauheni Kaliutac7ce9f02017-02-22 11:41:59 +02001529 free(buf);
1530 array_free_array(&reverse);
1531}
Lucas De Marchic89d2192014-05-30 09:38:26 -03001532
Yauheni Kaliutac7ce9f02017-02-22 11:41:59 +02001533static int depmod_report_cycles_from_root(struct depmod *depmod,
1534 struct mod *root_mod,
1535 struct kmod_list **roots,
1536 void **stack,
1537 size_t stack_size,
1538 struct hash *loop_set)
1539{
1540 struct kmod_list *free_list = NULL; /* struct vertex */
1541 struct kmod_list *l;
1542 struct vertex *root;
1543 struct vertex *vertex;
1544 struct vertex *v;
1545 struct mod *m;
1546 struct mod **itr, **itr_end;
1547 size_t is;
Mian Yousaf Kaukab6b77f182016-11-08 17:45:50 +01001548
Yauheni Kaliutac7ce9f02017-02-22 11:41:59 +02001549 root = vertex_new(root_mod, NULL);
1550 if (root == NULL) {
1551 ERR("No memory to report cycles\n");
1552 return -ENOMEM;
Lucas De Marchic89d2192014-05-30 09:38:26 -03001553 }
Mian Yousaf Kaukab6b77f182016-11-08 17:45:50 +01001554
Yauheni Kaliutac7ce9f02017-02-22 11:41:59 +02001555 l = kmod_list_append(free_list, root);
1556 if (l == NULL) {
1557 ERR("No memory to report cycles\n");
1558 return -ENOMEM;
1559 }
1560 free_list = l;
1561
1562 is = 0;
1563 stack[is++] = (void *)root;
1564
1565 while (is > 0) {
1566 vertex = stack[--is];
1567 m = vertex->mod;
1568 /*
1569 * because of the topological sort we can start only
1570 * from part of a loop or from a branch after a loop
1571 */
1572 if (m->visited && m == root->mod) {
1573 depmod_report_one_cycle(depmod, vertex,
1574 roots, loop_set);
1575 continue;
1576 }
1577
1578 m->visited = true;
1579 if (m->deps.count == 0) {
1580 /*
1581 * boundary condition: if there is more than one
1582 * single node branch (not a loop), it is
1583 * recognized as a loop by the code above:
1584 * m->visited because more then one,
1585 * m == root->mod since it is a single node.
1586 * So, prevent deeping into the branch second
1587 * time.
1588 */
1589 depmod_list_remove_data(roots, m);
1590
1591 continue;
1592 }
1593
1594 itr = (struct mod **) m->deps.array;
1595 itr_end = itr + m->deps.count;
1596 for (; itr < itr_end; itr++) {
1597 struct mod *dep = *itr;
1598 v = vertex_new(dep, vertex);
1599 if (v == NULL) {
1600 ERR("No memory to report cycles\n");
1601 return -ENOMEM;
1602 }
1603 assert(is < stack_size);
1604 stack[is++] = v;
1605
1606 l = kmod_list_append(free_list, v);
1607 if (l == NULL) {
1608 ERR("No memory to report cycles\n");
1609 return -ENOMEM;
1610 }
1611 free_list = l;
1612
1613 }
1614 }
1615 while (free_list) {
1616 v = free_list->data;
1617 l = kmod_list_remove(free_list);
1618 free_list = l;
1619 free(v);
1620 }
1621
1622 return 0;
1623}
1624
1625static void depmod_report_cycles(struct depmod *depmod, uint16_t n_mods,
1626 uint16_t *users)
1627{
1628 int num_cyclic = 0;
1629 struct kmod_list *roots = NULL; /* struct mod */
1630 struct kmod_list *l;
1631 size_t n_r; /* local n_roots */
1632 int i;
1633 int err;
Lucas De Marchi32007802017-02-23 21:47:37 -08001634 _cleanup_free_ void **stack = NULL;
Yauheni Kaliutac7ce9f02017-02-22 11:41:59 +02001635 struct mod *m;
1636 struct mod *root;
1637 struct hash *loop_set;
1638
1639 for (i = 0, n_r = 0; i < n_mods; i++) {
1640 if (users[i] <= 0)
1641 continue;
1642 m = depmod->modules.array[i];
1643 l = kmod_list_append(roots, m);
1644 if (l == NULL) {
1645 ERR("No memory to report cycles\n");
1646 return;
1647 }
1648 roots = l;
1649 n_r++;
1650 }
1651
1652 stack = malloc(n_r * sizeof(void *));
1653 if (stack == NULL) {
1654 ERR("No memory to report cycles\n");
1655 return;
1656 }
1657
1658 loop_set = hash_new(16, NULL);
1659 if (loop_set == NULL) {
1660 ERR("No memory to report cycles\n");
1661 return;
1662 }
1663
1664 while (roots != NULL) {
1665 root = roots->data;
1666 l = kmod_list_remove(roots);
1667 roots = l;
1668 err = depmod_report_cycles_from_root(depmod,
1669 root,
1670 &roots,
1671 stack, n_r, loop_set);
1672 if (err < 0)
1673 goto err;
1674 }
1675
1676 num_cyclic = hash_get_count(loop_set);
Mian Yousaf Kaukab6b77f182016-11-08 17:45:50 +01001677 ERR("Found %d modules in dependency cycles!\n", num_cyclic);
Lucas De Marchi32007802017-02-23 21:47:37 -08001678
Yauheni Kaliutac7ce9f02017-02-22 11:41:59 +02001679err:
1680 hash_free(loop_set);
Lucas De Marchic89d2192014-05-30 09:38:26 -03001681}
1682
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001683static int depmod_calculate_dependencies(struct depmod *depmod)
1684{
1685 const struct mod **itrm;
1686 uint16_t *users, *roots, *sorted;
1687 uint16_t i, n_roots = 0, n_sorted = 0, n_mods = depmod->modules.count;
Lucas De Marchic48b2692014-05-09 08:22:02 -03001688 int ret = 0;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001689
1690 users = malloc(sizeof(uint16_t) * n_mods * 3);
1691 if (users == NULL)
1692 return -ENOMEM;
1693 roots = users + n_mods;
1694 sorted = roots + n_mods;
1695
Lucas De Marchi1958af82013-04-21 16:16:18 -03001696 DBG("calculate dependencies and ordering (%hu modules)\n", n_mods);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001697
1698 assert(depmod->modules.count < UINT16_MAX);
1699
1700 /* populate modules users (how many modules uses it) */
1701 itrm = (const struct mod **)depmod->modules.array;
1702 for (i = 0; i < n_mods; i++, itrm++) {
1703 const struct mod *m = *itrm;
1704 users[i] = m->users;
1705 if (users[i] == 0) {
1706 roots[n_roots] = i;
1707 n_roots++;
1708 }
1709 }
1710
1711 /* topological sort (outputs modules without users first) */
1712 while (n_roots > 0) {
1713 const struct mod **itr_dst, **itr_dst_end;
1714 struct mod *src;
1715 uint16_t src_idx = roots[--n_roots];
1716
1717 src = depmod->modules.array[src_idx];
1718 src->dep_sort_idx = n_sorted;
1719 sorted[n_sorted] = src_idx;
1720 n_sorted++;
1721
1722 itr_dst = (const struct mod **)src->deps.array;
1723 itr_dst_end = itr_dst + src->deps.count;
1724 for (; itr_dst < itr_dst_end; itr_dst++) {
1725 const struct mod *dst = *itr_dst;
1726 uint16_t dst_idx = dst->idx;
1727 assert(users[dst_idx] > 0);
1728 users[dst_idx]--;
1729 if (users[dst_idx] == 0) {
1730 roots[n_roots] = dst_idx;
1731 n_roots++;
1732 }
1733 }
1734 }
1735
1736 if (n_sorted < n_mods) {
Yauheni Kaliutac7ce9f02017-02-22 11:41:59 +02001737 depmod_report_cycles(depmod, n_mods, users);
Lucas De Marchic48b2692014-05-09 08:22:02 -03001738 ret = -EINVAL;
Lucas De Marchic48b2692014-05-09 08:22:02 -03001739 goto exit;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001740 }
1741
1742 depmod_sort_dependencies(depmod);
1743
Lucas De Marchic48b2692014-05-09 08:22:02 -03001744 DBG("calculated dependencies and ordering (%hu modules)\n", n_mods);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001745
Lucas De Marchic48b2692014-05-09 08:22:02 -03001746exit:
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001747 free(users);
Lucas De Marchic48b2692014-05-09 08:22:02 -03001748 return ret;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001749}
1750
1751static int depmod_load(struct depmod *depmod)
1752{
1753 int err;
1754
Lucas De Marchiec587f22012-10-30 03:16:10 -02001755 err = depmod_load_modules(depmod);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001756 if (err < 0)
1757 return err;
1758
1759 err = depmod_load_dependencies(depmod);
1760 if (err < 0)
1761 return err;
1762
1763 err = depmod_calculate_dependencies(depmod);
1764 if (err < 0)
1765 return err;
1766
1767 return 0;
1768}
1769
Gustavo Sverzut Barbieri8e3505c2011-12-23 20:53:16 -02001770static size_t mod_count_all_dependencies(const struct mod *mod)
1771{
1772 size_t i, count = 0;
1773 for (i = 0; i < mod->deps.count; i++) {
1774 const struct mod *d = mod->deps.array[i];
1775 count += 1 + mod_count_all_dependencies(d);
1776 }
1777 return count;
1778}
1779
1780static int mod_fill_all_unique_dependencies(const struct mod *mod, const struct mod **deps, size_t n_deps, size_t *last)
1781{
1782 size_t i;
1783 int err = 0;
1784 for (i = 0; i < mod->deps.count; i++) {
1785 const struct mod *d = mod->deps.array[i];
1786 size_t j;
1787 uint8_t exists = 0;
1788
1789 for (j = 0; j < *last; j++) {
1790 if (deps[j] == d) {
1791 exists = 1;
1792 break;
1793 }
1794 }
1795
1796 if (exists)
1797 continue;
1798
1799 if (*last >= n_deps)
1800 return -ENOSPC;
1801 deps[*last] = d;
1802 (*last)++;
1803 err = mod_fill_all_unique_dependencies(d, deps, n_deps, last);
1804 if (err < 0)
1805 break;
1806 }
1807 return err;
1808}
1809
1810static const struct mod **mod_get_all_sorted_dependencies(const struct mod *mod, size_t *n_deps)
1811{
1812 const struct mod **deps;
1813 size_t last = 0;
1814
1815 *n_deps = mod_count_all_dependencies(mod);
1816 if (*n_deps == 0)
1817 return NULL;
1818
1819 deps = malloc(sizeof(struct mod *) * (*n_deps));
1820 if (deps == NULL)
1821 return NULL;
1822
1823 if (mod_fill_all_unique_dependencies(mod, deps, *n_deps, &last) < 0) {
1824 free(deps);
1825 return NULL;
1826 }
1827
1828 qsort(deps, last, sizeof(struct mod *), dep_cmp);
1829 *n_deps = last;
1830 return deps;
1831}
1832
1833static inline const char *mod_get_compressed_path(const struct mod *mod)
1834{
1835 if (mod->relpath != NULL)
1836 return mod->relpath;
1837 return mod->path;
1838}
1839
1840static int output_deps(struct depmod *depmod, FILE *out)
1841{
1842 size_t i;
1843
1844 for (i = 0; i < depmod->modules.count; i++) {
1845 const struct mod **deps, *mod = depmod->modules.array[i];
1846 const char *p = mod_get_compressed_path(mod);
1847 size_t j, n_deps;
1848
Gustavo Sverzut Barbieri8e3505c2011-12-23 20:53:16 -02001849 fprintf(out, "%s:", p);
1850
1851 if (mod->deps.count == 0)
1852 goto end;
1853
1854 deps = mod_get_all_sorted_dependencies(mod, &n_deps);
1855 if (deps == NULL) {
Dave Reisner63698372012-01-04 10:41:50 -05001856 ERR("could not get all sorted dependencies of %s\n", p);
Gustavo Sverzut Barbieri8e3505c2011-12-23 20:53:16 -02001857 goto end;
1858 }
1859
1860 for (j = 0; j < n_deps; j++) {
1861 const struct mod *d = deps[j];
Gustavo Sverzut Barbieri8e3505c2011-12-23 20:53:16 -02001862 fprintf(out, " %s", mod_get_compressed_path(d));
1863 }
1864 free(deps);
1865 end:
1866 putc('\n', out);
1867 }
1868
1869 return 0;
1870}
1871
Gustavo Sverzut Barbieri74367882011-12-23 23:21:48 -02001872static int output_deps_bin(struct depmod *depmod, FILE *out)
1873{
1874 struct index_node *idx;
1875 size_t i;
1876
1877 if (out == stdout)
1878 return 0;
1879
1880 idx = index_create();
1881 if (idx == NULL)
1882 return -ENOMEM;
1883
1884 for (i = 0; i < depmod->modules.count; i++) {
1885 const struct mod **deps, *mod = depmod->modules.array[i];
1886 const char *p = mod_get_compressed_path(mod);
1887 char *line;
1888 size_t j, n_deps, linepos, linelen, slen;
1889 int duplicate;
1890
Gustavo Sverzut Barbieri74367882011-12-23 23:21:48 -02001891 deps = mod_get_all_sorted_dependencies(mod, &n_deps);
1892 if (deps == NULL && n_deps > 0) {
Dave Reisner63698372012-01-04 10:41:50 -05001893 ERR("could not get all sorted dependencies of %s\n", p);
Gustavo Sverzut Barbieri74367882011-12-23 23:21:48 -02001894 continue;
1895 }
1896
1897 linelen = strlen(p) + 1;
1898 for (j = 0; j < n_deps; j++) {
1899 const struct mod *d = deps[j];
Gustavo Sverzut Barbieri74367882011-12-23 23:21:48 -02001900 linelen += 1 + strlen(mod_get_compressed_path(d));
1901 }
1902
1903 line = malloc(linelen + 1);
1904 if (line == NULL) {
1905 free(deps);
1906 ERR("modules.deps.bin: out of memory\n");
1907 continue;
1908 }
1909
1910 linepos = 0;
1911 slen = strlen(p);
1912 memcpy(line + linepos, p, slen);
1913 linepos += slen;
1914 line[linepos] = ':';
1915 linepos++;
1916
1917 for (j = 0; j < n_deps; j++) {
1918 const struct mod *d = deps[j];
1919 const char *dp;
Lucas De Marchic48b2692014-05-09 08:22:02 -03001920
Gustavo Sverzut Barbieri74367882011-12-23 23:21:48 -02001921 line[linepos] = ' ';
1922 linepos++;
1923
1924 dp = mod_get_compressed_path(d);
1925 slen = strlen(dp);
1926 memcpy(line + linepos, dp, slen);
1927 linepos += slen;
1928 }
1929 line[linepos] = '\0';
1930
1931 duplicate = index_insert(idx, mod->modname, line, mod->idx);
1932 if (duplicate && depmod->cfg->warn_dups)
1933 WRN("duplicate module deps:\n%s\n", line);
1934 free(line);
Lucas De Marchiea1b8c32011-12-27 14:18:32 -02001935 free(deps);
Gustavo Sverzut Barbieri74367882011-12-23 23:21:48 -02001936 }
1937
1938 index_write(idx, out);
1939 index_destroy(idx);
1940
1941 return 0;
1942}
1943
Gustavo Sverzut Barbieri0d131742011-12-23 01:43:01 -02001944static int output_aliases(struct depmod *depmod, FILE *out)
1945{
1946 size_t i;
1947
1948 fputs("# Aliases extracted from modules themselves.\n", out);
1949
1950 for (i = 0; i < depmod->modules.count; i++) {
1951 const struct mod *mod = depmod->modules.array[i];
Lucas De Marchi7062eca2012-10-30 03:16:10 -02001952 struct kmod_list *l;
1953
1954 kmod_list_foreach(l, mod->info_list) {
Gustavo Sverzut Barbieri0d131742011-12-23 01:43:01 -02001955 const char *key = kmod_module_info_get_key(l);
1956 const char *value = kmod_module_info_get_value(l);
1957
1958 if (!streq(key, "alias"))
1959 continue;
1960
Lucas De Marchi447eed82012-10-30 04:08:54 -02001961 fprintf(out, "alias %s %s\n", value, mod->modname);
Gustavo Sverzut Barbieri0d131742011-12-23 01:43:01 -02001962 }
Gustavo Sverzut Barbieri0d131742011-12-23 01:43:01 -02001963 }
1964
1965 return 0;
1966}
1967
Gustavo Sverzut Barbieriec77abb2011-12-23 23:21:09 -02001968static int output_aliases_bin(struct depmod *depmod, FILE *out)
1969{
Gustavo Sverzut Barbieriec77abb2011-12-23 23:21:09 -02001970 struct index_node *idx;
1971 size_t i;
1972
1973 if (out == stdout)
1974 return 0;
1975
1976 idx = index_create();
1977 if (idx == NULL)
1978 return -ENOMEM;
1979
1980 for (i = 0; i < depmod->modules.count; i++) {
1981 const struct mod *mod = depmod->modules.array[i];
Lucas De Marchi7062eca2012-10-30 03:16:10 -02001982 struct kmod_list *l;
1983
1984 kmod_list_foreach(l, mod->info_list) {
Gustavo Sverzut Barbieriec77abb2011-12-23 23:21:09 -02001985 const char *key = kmod_module_info_get_key(l);
1986 const char *value = kmod_module_info_get_value(l);
Lucas De Marchi3753ae12014-10-09 00:54:29 -03001987 char buf[PATH_MAX];
Lucas De Marchi447eed82012-10-30 04:08:54 -02001988 const char *alias;
Gustavo Sverzut Barbieriec77abb2011-12-23 23:21:09 -02001989 int duplicate;
1990
1991 if (!streq(key, "alias"))
1992 continue;
1993
Lucas De Marchi3753ae12014-10-09 00:54:29 -03001994 if (alias_normalize(value, buf, NULL) < 0) {
1995 WRN("Unmatched bracket in %s\n", value);
Gustavo Sverzut Barbieriec77abb2011-12-23 23:21:09 -02001996 continue;
Lucas De Marchi3753ae12014-10-09 00:54:29 -03001997 }
1998 alias = buf;
Gustavo Sverzut Barbieriec77abb2011-12-23 23:21:09 -02001999
Lucas De Marchi447eed82012-10-30 04:08:54 -02002000 duplicate = index_insert(idx, alias, mod->modname,
Gustavo Sverzut Barbieriec77abb2011-12-23 23:21:09 -02002001 mod->idx);
2002 if (duplicate && depmod->cfg->warn_dups)
2003 WRN("duplicate module alias:\n%s %s\n",
Lucas De Marchi447eed82012-10-30 04:08:54 -02002004 alias, mod->modname);
Gustavo Sverzut Barbieriec77abb2011-12-23 23:21:09 -02002005 }
Gustavo Sverzut Barbieriec77abb2011-12-23 23:21:09 -02002006 }
2007
2008 index_write(idx, out);
2009 index_destroy(idx);
2010
2011 return 0;
2012}
2013
Gustavo Sverzut Barbieri8bc830e2011-12-23 01:37:07 -02002014static int output_softdeps(struct depmod *depmod, FILE *out)
2015{
2016 size_t i;
2017
2018 fputs("# Soft dependencies extracted from modules themselves.\n", out);
Gustavo Sverzut Barbieri8bc830e2011-12-23 01:37:07 -02002019
2020 for (i = 0; i < depmod->modules.count; i++) {
2021 const struct mod *mod = depmod->modules.array[i];
Lucas De Marchi7062eca2012-10-30 03:16:10 -02002022 struct kmod_list *l;
2023
2024 kmod_list_foreach(l, mod->info_list) {
Gustavo Sverzut Barbieri8bc830e2011-12-23 01:37:07 -02002025 const char *key = kmod_module_info_get_key(l);
2026 const char *value = kmod_module_info_get_value(l);
2027
2028 if (!streq(key, "softdep"))
2029 continue;
2030
Lucas De Marchi447eed82012-10-30 04:08:54 -02002031 fprintf(out, "softdep %s %s\n", mod->modname, value);
Gustavo Sverzut Barbieri8bc830e2011-12-23 01:37:07 -02002032 }
Gustavo Sverzut Barbieri8bc830e2011-12-23 01:37:07 -02002033 }
2034
2035 return 0;
2036}
2037
Gustavo Sverzut Barbieri9a14d0e2011-12-23 01:27:28 -02002038static int output_symbols(struct depmod *depmod, FILE *out)
2039{
Lucas De Marchi5cd13062011-12-27 13:27:42 -02002040 struct hash_iter iter;
Rolf Eike Beer55021be2012-01-06 13:33:11 +01002041 const void *v;
Gustavo Sverzut Barbieri9a14d0e2011-12-23 01:27:28 -02002042
2043 fputs("# Aliases for symbols, used by symbol_request().\n", out);
2044
Lucas De Marchi5cd13062011-12-27 13:27:42 -02002045 hash_iter_init(depmod->symbols, &iter);
2046
Rolf Eike Beer55021be2012-01-06 13:33:11 +01002047 while (hash_iter_next(&iter, NULL, &v)) {
2048 const struct symbol *sym = v;
Lucas De Marchi5cd13062011-12-27 13:27:42 -02002049 if (sym->owner == NULL)
2050 continue;
2051
2052 fprintf(out, "alias symbol:%s %s\n",
2053 sym->name, sym->owner->modname);
Gustavo Sverzut Barbieri9a14d0e2011-12-23 01:27:28 -02002054 }
2055
2056 return 0;
2057}
2058
Gustavo Sverzut Barbieri75a97232011-12-23 23:20:44 -02002059static int output_symbols_bin(struct depmod *depmod, FILE *out)
2060{
2061 struct index_node *idx;
2062 char alias[1024];
Lucas De Marchicb51a642016-08-10 15:33:18 -03002063 _cleanup_(scratchbuf_release) struct scratchbuf salias =
2064 SCRATCHBUF_INITIALIZER(alias);
Lucas De Marchi5cd13062011-12-27 13:27:42 -02002065 size_t baselen = sizeof("symbol:") - 1;
2066 struct hash_iter iter;
Rolf Eike Beer55021be2012-01-06 13:33:11 +01002067 const void *v;
Lucas De Marchicb51a642016-08-10 15:33:18 -03002068 int ret = 0;
Gustavo Sverzut Barbieri75a97232011-12-23 23:20:44 -02002069
2070 if (out == stdout)
2071 return 0;
2072
2073 idx = index_create();
2074 if (idx == NULL)
2075 return -ENOMEM;
2076
2077 memcpy(alias, "symbol:", baselen);
Lucas De Marchicb51a642016-08-10 15:33:18 -03002078
Lucas De Marchi5cd13062011-12-27 13:27:42 -02002079 hash_iter_init(depmod->symbols, &iter);
Gustavo Sverzut Barbieri75a97232011-12-23 23:20:44 -02002080
Rolf Eike Beer55021be2012-01-06 13:33:11 +01002081 while (hash_iter_next(&iter, NULL, &v)) {
Lucas De Marchi5cd13062011-12-27 13:27:42 -02002082 int duplicate;
Rolf Eike Beer55021be2012-01-06 13:33:11 +01002083 const struct symbol *sym = v;
Lucas De Marchicb51a642016-08-10 15:33:18 -03002084 size_t len;
Gustavo Sverzut Barbieri75a97232011-12-23 23:20:44 -02002085
Lucas De Marchi5cd13062011-12-27 13:27:42 -02002086 if (sym->owner == NULL)
2087 continue;
Gustavo Sverzut Barbieri75a97232011-12-23 23:20:44 -02002088
Lucas De Marchicb51a642016-08-10 15:33:18 -03002089 len = strlen(sym->name);
2090
2091 if (scratchbuf_alloc(&salias, baselen + len + 1) < 0) {
2092 ret = -ENOMEM;
2093 goto err_scratchbuf;
2094 }
2095 memcpy(scratchbuf_str(&salias) + baselen, sym->name, len + 1);
Lucas De Marchi5cd13062011-12-27 13:27:42 -02002096 duplicate = index_insert(idx, alias, sym->owner->modname,
2097 sym->owner->idx);
2098
2099 if (duplicate && depmod->cfg->warn_dups)
2100 WRN("duplicate module syms:\n%s %s\n",
2101 alias, sym->owner->modname);
Gustavo Sverzut Barbieri75a97232011-12-23 23:20:44 -02002102 }
2103
2104 index_write(idx, out);
Lucas De Marchicb51a642016-08-10 15:33:18 -03002105
2106err_scratchbuf:
Gustavo Sverzut Barbieri75a97232011-12-23 23:20:44 -02002107 index_destroy(idx);
Lucas De Marchi5cd13062011-12-27 13:27:42 -02002108
Lucas De Marchicb51a642016-08-10 15:33:18 -03002109 if (ret < 0)
2110 ERR("output symbols: %s\n", strerror(-ret));
2111
2112 return ret;
Gustavo Sverzut Barbieri75a97232011-12-23 23:20:44 -02002113}
2114
Gustavo Sverzut Barbieri4b144e52011-12-23 23:19:22 -02002115static int output_builtin_bin(struct depmod *depmod, FILE *out)
2116{
2117 FILE *in;
2118 struct index_node *idx;
Lucas De Marchi6daceb22012-01-08 01:02:29 -02002119 char infile[PATH_MAX], line[PATH_MAX], modname[PATH_MAX];
Gustavo Sverzut Barbieri4b144e52011-12-23 23:19:22 -02002120
2121 if (out == stdout)
2122 return 0;
2123
2124 snprintf(infile, sizeof(infile), "%s/modules.builtin",
Lucas De Marchic5db1a32012-06-15 02:39:34 -03002125 depmod->cfg->dirname);
Gustavo Sverzut Barbieri4b144e52011-12-23 23:19:22 -02002126 in = fopen(infile, "r");
2127 if (in == NULL) {
Lucas De Marchi56406fd2012-01-04 20:53:10 -02002128 WRN("could not open %s: %m\n", infile);
Lucas De Marchic5db1a32012-06-15 02:39:34 -03002129 return 0;
Gustavo Sverzut Barbieri4b144e52011-12-23 23:19:22 -02002130 }
2131
2132 idx = index_create();
2133 if (idx == NULL) {
2134 fclose(in);
2135 return -ENOMEM;
2136 }
2137
2138 while (fgets(line, sizeof(line), in) != NULL) {
2139 if (!isalpha(line[0])) {
2140 ERR("Invalid modules.builtin line: %s\n", line);
2141 continue;
2142 }
2143
2144 path_to_modname(line, modname, NULL);
2145 index_insert(idx, modname, "", 0);
2146 }
2147
2148 index_write(idx, out);
2149 index_destroy(idx);
2150 fclose(in);
2151
2152 return 0;
2153}
2154
Gustavo Sverzut Barbieri25c41512011-12-23 00:55:33 -02002155static int output_devname(struct depmod *depmod, FILE *out)
2156{
2157 size_t i;
Josh Triplett4c30a112016-01-10 07:10:25 -08002158 bool empty = true;
Gustavo Sverzut Barbieri25c41512011-12-23 00:55:33 -02002159
2160 for (i = 0; i < depmod->modules.count; i++) {
2161 const struct mod *mod = depmod->modules.array[i];
Lucas De Marchi7062eca2012-10-30 03:16:10 -02002162 struct kmod_list *l;
Gustavo Sverzut Barbieri25c41512011-12-23 00:55:33 -02002163 const char *devname = NULL;
2164 char type = '\0';
2165 unsigned int major = 0, minor = 0;
Gustavo Sverzut Barbieri25c41512011-12-23 00:55:33 -02002166
Lucas De Marchi7062eca2012-10-30 03:16:10 -02002167 kmod_list_foreach(l, mod->info_list) {
Gustavo Sverzut Barbieri25c41512011-12-23 00:55:33 -02002168 const char *key = kmod_module_info_get_key(l);
2169 const char *value = kmod_module_info_get_value(l);
2170 unsigned int maj, min;
2171
2172 if (!streq(key, "alias"))
2173 continue;
2174
2175 if (strstartswith(value, "devname:"))
2176 devname = value + sizeof("devname:") - 1;
2177 else if (sscanf(value, "char-major-%u-%u",
2178 &maj, &min) == 2) {
2179 type = 'c';
2180 major = maj;
2181 minor = min;
2182 } else if (sscanf(value, "block-major-%u-%u",
2183 &maj, &min) == 2) {
2184 type = 'b';
2185 major = maj;
2186 minor = min;
2187 }
2188
Tom Gundersen6506ddf2013-09-09 20:01:01 +02002189 if (type != '\0' && devname != NULL)
2190 break;
2191 }
2192
2193 if (devname != NULL) {
Josh Triplett4c30a112016-01-10 07:10:25 -08002194 if (type != '\0') {
2195 if (empty) {
2196 fputs("# Device nodes to trigger on-demand module loading.\n",
2197 out);
2198 empty = false;
2199 }
Lucas De Marchi447eed82012-10-30 04:08:54 -02002200 fprintf(out, "%s %s %c%u:%u\n", mod->modname,
Gustavo Sverzut Barbieri25c41512011-12-23 00:55:33 -02002201 devname, type, major, minor);
Josh Triplett4c30a112016-01-10 07:10:25 -08002202 } else
Tom Gundersen6506ddf2013-09-09 20:01:01 +02002203 ERR("Module '%s' has devname (%s) but "
2204 "lacks major and minor information. "
2205 "Ignoring.\n", mod->modname, devname);
Gustavo Sverzut Barbieri25c41512011-12-23 00:55:33 -02002206 }
Gustavo Sverzut Barbieri25c41512011-12-23 00:55:33 -02002207 }
2208
2209 return 0;
2210}
2211
2212static int depmod_output(struct depmod *depmod, FILE *out)
2213{
2214 static const struct depfile {
2215 const char *name;
2216 int (*cb)(struct depmod *depmod, FILE *out);
2217 } *itr, depfiles[] = {
Lucas De Marchi015946d2012-06-15 01:10:14 -03002218 { "modules.dep", output_deps },
2219 { "modules.dep.bin", output_deps_bin },
2220 { "modules.alias", output_aliases },
2221 { "modules.alias.bin", output_aliases_bin },
2222 { "modules.softdep", output_softdeps },
2223 { "modules.symbols", output_symbols },
2224 { "modules.symbols.bin", output_symbols_bin },
2225 { "modules.builtin.bin", output_builtin_bin },
2226 { "modules.devname", output_devname },
2227 { }
Gustavo Sverzut Barbieri25c41512011-12-23 00:55:33 -02002228 };
2229 const char *dname = depmod->cfg->dirname;
2230 int dfd, err = 0;
2231
2232 if (out != NULL)
2233 dfd = -1;
2234 else {
2235 dfd = open(dname, O_RDONLY);
2236 if (dfd < 0) {
2237 err = -errno;
Dave Reisner63698372012-01-04 10:41:50 -05002238 CRIT("could not open directory %s: %m\n", dname);
Gustavo Sverzut Barbieri25c41512011-12-23 00:55:33 -02002239 return err;
2240 }
2241 }
2242
2243 for (itr = depfiles; itr->name != NULL; itr++) {
2244 FILE *fp = out;
2245 char tmp[NAME_MAX] = "";
Lucas De Marchi3f376cd2012-06-15 22:14:00 -03002246 int r, ferr;
Gustavo Sverzut Barbieri25c41512011-12-23 00:55:33 -02002247
2248 if (fp == NULL) {
2249 int flags = O_CREAT | O_TRUNC | O_WRONLY;
2250 int mode = 0644;
2251 int fd;
2252
2253 snprintf(tmp, sizeof(tmp), "%s.tmp", itr->name);
2254 fd = openat(dfd, tmp, flags, mode);
2255 if (fd < 0) {
2256 ERR("openat(%s, %s, %o, %o): %m\n",
2257 dname, tmp, flags, mode);
2258 continue;
2259 }
2260 fp = fdopen(fd, "wb");
2261 if (fp == NULL) {
2262 ERR("fdopen(%d=%s/%s): %m\n", fd, dname, tmp);
2263 close(fd);
2264 continue;
2265 }
2266 }
2267
2268 r = itr->cb(depmod, fp);
2269 if (fp == out)
2270 continue;
2271
Lucas De Marchi3f376cd2012-06-15 22:14:00 -03002272 ferr = ferror(fp) | fclose(fp);
2273
Gustavo Sverzut Barbieri25c41512011-12-23 00:55:33 -02002274 if (r < 0) {
2275 if (unlinkat(dfd, tmp, 0) != 0)
2276 ERR("unlinkat(%s, %s): %m\n", dname, tmp);
Lucas De Marchi80e49ad2012-06-15 01:08:17 -03002277
2278 ERR("Could not write index '%s': %s\n", itr->name,
2279 strerror(-r));
2280 err = -errno;
2281 break;
2282 }
2283
2284 unlinkat(dfd, itr->name, 0);
2285 if (renameat(dfd, tmp, dfd, itr->name) != 0) {
2286 err = -errno;
2287 CRIT("renameat(%s, %s, %s, %s): %m\n",
2288 dname, tmp, dname, itr->name);
2289 break;
Gustavo Sverzut Barbieri25c41512011-12-23 00:55:33 -02002290 }
Lucas De Marchia4fb97a2012-06-15 20:13:31 -03002291
Lucas De Marchi3f376cd2012-06-15 22:14:00 -03002292 if (ferr) {
Lucas De Marchia4fb97a2012-06-15 20:13:31 -03002293 err = -ENOSPC;
Lucas De Marchi3f376cd2012-06-15 22:14:00 -03002294 ERR("Could not create index '%s'. Output is truncated: %s\n",
2295 itr->name, strerror(-err));
Lucas De Marchia4fb97a2012-06-15 20:13:31 -03002296 break;
2297 }
Gustavo Sverzut Barbieri25c41512011-12-23 00:55:33 -02002298 }
2299
2300 if (dfd >= 0)
2301 close(dfd);
Lucas De Marchi80e49ad2012-06-15 01:08:17 -03002302
Gustavo Sverzut Barbieri25c41512011-12-23 00:55:33 -02002303 return err;
2304}
2305
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002306static void depmod_add_fake_syms(struct depmod *depmod)
2307{
2308 /* __this_module is magic inserted by kernel loader. */
Andrey Mazo572a2712012-12-12 16:39:18 +04002309 depmod_symbol_add(depmod, "__this_module", true, 0, NULL);
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002310 /* On S390, this is faked up too */
Andrey Mazo572a2712012-12-12 16:39:18 +04002311 depmod_symbol_add(depmod, "_GLOBAL_OFFSET_TABLE_", true, 0, NULL);
Anton Blanchardd46136b2016-06-10 15:24:30 +10002312 /* On PowerPC64 ABIv2, .TOC. is more or less _GLOBAL_OFFSET_TABLE_ */
2313 depmod_symbol_add(depmod, "TOC.", true, 0, NULL);
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002314}
2315
2316static int depmod_load_symvers(struct depmod *depmod, const char *filename)
2317{
2318 char line[10240];
2319 FILE *fp;
2320 unsigned int linenum = 0;
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002321
2322 fp = fopen(filename, "r");
Khem Raj035cbdc2012-02-02 23:09:59 -08002323 if (fp == NULL) {
2324 int err = -errno;
2325 DBG("load symvers: %s: %m\n", filename);
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002326 return err;
Khem Raj035cbdc2012-02-02 23:09:59 -08002327 }
2328 DBG("load symvers: %s\n", filename);
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002329
2330 /* eg. "0xb352177e\tfind_first_bit\tvmlinux\tEXPORT_SYMBOL" */
2331 while (fgets(line, sizeof(line), fp) != NULL) {
2332 const char *ver, *sym, *where;
2333 char *verend;
2334 uint64_t crc;
2335
2336 linenum++;
2337
2338 ver = strtok(line, " \t");
2339 sym = strtok(NULL, " \t");
2340 where = strtok(NULL, " \t");
2341 if (!ver || !sym || !where)
2342 continue;
2343
2344 if (!streq(where, "vmlinux"))
2345 continue;
2346
2347 crc = strtoull(ver, &verend, 16);
2348 if (verend[0] != '\0') {
2349 ERR("%s:%u Invalid symbol version %s: %m\n",
2350 filename, linenum, ver);
2351 continue;
2352 }
2353
Andrey Mazo572a2712012-12-12 16:39:18 +04002354 depmod_symbol_add(depmod, sym, false, crc, NULL);
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002355 }
2356 depmod_add_fake_syms(depmod);
2357
Khem Raj035cbdc2012-02-02 23:09:59 -08002358 DBG("loaded symvers: %s\n", filename);
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002359
2360 fclose(fp);
Khem Raj035cbdc2012-02-02 23:09:59 -08002361 return 0;
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002362}
2363
2364static int depmod_load_system_map(struct depmod *depmod, const char *filename)
2365{
2366 const char ksymstr[] = "__ksymtab_";
2367 const size_t ksymstr_len = sizeof(ksymstr) - 1;
2368 char line[10240];
2369 FILE *fp;
2370 unsigned int linenum = 0;
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002371
2372 fp = fopen(filename, "r");
Khem Raj035cbdc2012-02-02 23:09:59 -08002373 if (fp == NULL) {
2374 int err = -errno;
2375 DBG("load System.map: %s: %m\n", filename);
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002376 return err;
Khem Raj035cbdc2012-02-02 23:09:59 -08002377 }
2378 DBG("load System.map: %s\n", filename);
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002379
2380 /* eg. c0294200 R __ksymtab_devfs_alloc_devnum */
2381 while (fgets(line, sizeof(line), fp) != NULL) {
2382 char *p, *end;
2383
2384 linenum++;
2385
2386 p = strchr(line, ' ');
2387 if (p == NULL)
2388 goto invalid_syntax;
2389 p++;
2390 p = strchr(p, ' ');
2391 if (p == NULL)
2392 goto invalid_syntax;
2393 p++;
2394
Andrey Mazo572a2712012-12-12 16:39:18 +04002395 /* skip prefix */
2396 if (p[0] == depmod->cfg->sym_prefix)
2397 p++;
2398
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002399 /* Covers gpl-only and normal symbols. */
2400 if (strncmp(p, ksymstr, ksymstr_len) != 0)
2401 continue;
2402
2403 end = strchr(p, '\n');
2404 if (end != NULL)
2405 *end = '\0';
2406
Andrey Mazo572a2712012-12-12 16:39:18 +04002407 depmod_symbol_add(depmod, p + ksymstr_len, true, 0, NULL);
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002408 continue;
2409
2410 invalid_syntax:
2411 ERR("%s:%u: invalid line: %s\n", filename, linenum, line);
2412 }
2413 depmod_add_fake_syms(depmod);
2414
Khem Raj035cbdc2012-02-02 23:09:59 -08002415 DBG("loaded System.map: %s\n", filename);
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002416
2417 fclose(fp);
Khem Raj035cbdc2012-02-02 23:09:59 -08002418 return 0;
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002419}
Gustavo Sverzut Barbieri25c41512011-12-23 00:55:33 -02002420
Gustavo Sverzut Barbieri18cd9da2011-12-24 11:16:40 -02002421
2422static int depfile_up_to_date_dir(DIR *d, time_t mtime, size_t baselen, char *path)
2423{
2424 struct dirent *de;
2425 int err = 1, dfd = dirfd(d);
2426
2427 while ((de = readdir(d)) != NULL) {
2428 const char *name = de->d_name;
2429 size_t namelen;
2430 struct stat st;
2431
2432 if (name[0] == '.' && (name[1] == '\0' ||
2433 (name[1] == '.' && name[2] == '\0')))
2434 continue;
2435 if (streq(name, "build") || streq(name, "source"))
2436 continue;
2437 namelen = strlen(name);
2438 if (baselen + namelen + 2 >= PATH_MAX) {
2439 path[baselen] = '\0';
Lucas De Marchi1958af82013-04-21 16:16:18 -03002440 ERR("path is too long %s%s\n", path, name);
Gustavo Sverzut Barbieri18cd9da2011-12-24 11:16:40 -02002441 continue;
2442 }
2443
2444 if (fstatat(dfd, name, &st, 0) < 0) {
2445 ERR("fstatat(%d, %s): %m\n", dfd, name);
2446 continue;
2447 }
2448
2449 if (S_ISDIR(st.st_mode)) {
2450 int fd;
2451 DIR *subdir;
2452 memcpy(path + baselen, name, namelen + 1);
2453 if (baselen + namelen + 2 + NAME_MAX >= PATH_MAX) {
2454 ERR("directory path is too long %s\n", path);
2455 continue;
2456 }
2457 fd = openat(dfd, name, O_RDONLY);
2458 if (fd < 0) {
2459 ERR("openat(%d, %s, O_RDONLY): %m\n",
2460 dfd, name);
2461 continue;
2462 }
2463 subdir = fdopendir(fd);
2464 if (subdir == NULL) {
2465 ERR("fdopendir(%d): %m\n", fd);
2466 close(fd);
2467 continue;
2468 }
2469 path[baselen + namelen] = '/';
2470 path[baselen + namelen + 1] = '\0';
2471 err = depfile_up_to_date_dir(subdir, mtime,
2472 baselen + namelen + 1,
2473 path);
2474 closedir(subdir);
2475 } else if (S_ISREG(st.st_mode)) {
Lucas De Marchi650f89c2012-11-28 14:26:23 -02002476 if (!path_ends_with_kmod_ext(name, namelen))
Gustavo Sverzut Barbieri18cd9da2011-12-24 11:16:40 -02002477 continue;
Lucas De Marchi650f89c2012-11-28 14:26:23 -02002478
Gustavo Sverzut Barbieri18cd9da2011-12-24 11:16:40 -02002479 memcpy(path + baselen, name, namelen + 1);
2480 err = st.st_mtime <= mtime;
2481 if (err == 0) {
2482 DBG("%s %"PRIu64" is newer than %"PRIu64"\n",
2483 path, (uint64_t)st.st_mtime,
2484 (uint64_t)mtime);
2485 }
2486 } else {
2487 ERR("unsupported file type %s: %o\n",
2488 path, st.st_mode & S_IFMT);
2489 continue;
2490 }
2491
2492 if (err == 0)
2493 break; /* outdated! */
2494 else if (err < 0) {
2495 path[baselen + namelen] = '\0';
2496 ERR("failed %s: %s\n", path, strerror(-err));
2497 err = 1; /* ignore errors */
2498 }
2499 }
2500
2501 return err;
2502}
2503
2504/* uptodate: 1, outdated: 0, errors < 0 */
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002505static int depfile_up_to_date(const char *dirname)
2506{
Gustavo Sverzut Barbieri18cd9da2011-12-24 11:16:40 -02002507 char path[PATH_MAX];
2508 DIR *d = opendir(dirname);
2509 struct stat st;
2510 size_t baselen;
2511 int err;
2512 if (d == NULL) {
2513 err = -errno;
Dave Reisner63698372012-01-04 10:41:50 -05002514 ERR("could not open directory %s: %m\n", dirname);
Gustavo Sverzut Barbieri18cd9da2011-12-24 11:16:40 -02002515 return err;
2516 }
2517
2518 if (fstatat(dirfd(d), "modules.dep", &st, 0) != 0) {
2519 err = -errno;
Dave Reisner63698372012-01-04 10:41:50 -05002520 ERR("could not fstatat(%s, modules.dep): %m\n", dirname);
Gustavo Sverzut Barbieri18cd9da2011-12-24 11:16:40 -02002521 closedir(d);
2522 return err;
2523 }
2524
2525 baselen = strlen(dirname);
2526 memcpy(path, dirname, baselen);
2527 path[baselen] = '/';
2528 baselen++;
2529 path[baselen] = '\0';
2530
2531 err = depfile_up_to_date_dir(d, st.st_mtime, baselen, path);
2532 closedir(d);
2533 return err;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002534}
2535
2536static int is_version_number(const char *version)
2537{
2538 unsigned int d1, d2;
2539 return (sscanf(version, "%u.%u", &d1, &d2) == 2);
2540}
2541
Lucas De Marchif6cf14c2011-12-27 19:56:33 -02002542static int do_depmod(int argc, char *argv[])
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002543{
2544 FILE *out = NULL;
Lucas De Marchi31f1d0d2012-01-14 02:04:31 -02002545 int err = 0, all = 0, maybe_all = 0, n_config_paths = 0;
Lucas De Marchia07ea032015-02-25 12:06:44 -03002546 _cleanup_free_ char *root = NULL;
2547 _cleanup_free_ const char **config_paths = NULL;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002548 const char *system_map = NULL;
2549 const char *module_symvers = NULL;
2550 const char *null_kmod_config = NULL;
2551 struct utsname un;
2552 struct kmod_ctx *ctx = NULL;
2553 struct cfg cfg;
2554 struct depmod depmod;
2555
2556 memset(&cfg, 0, sizeof(cfg));
2557 memset(&depmod, 0, sizeof(depmod));
2558
2559 for (;;) {
2560 int c, idx = 0;
2561 c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
2562 if (c == -1)
2563 break;
2564 switch (c) {
2565 case 'a':
2566 all = 1;
2567 break;
2568 case 'A':
2569 maybe_all = 1;
2570 break;
2571 case 'b':
Lucas De Marchia07ea032015-02-25 12:06:44 -03002572 if (root)
2573 free(root);
2574 root = path_make_absolute_cwd(optarg);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002575 break;
2576 case 'C': {
2577 size_t bytes = sizeof(char *) * (n_config_paths + 2);
2578 void *tmp = realloc(config_paths, bytes);
2579 if (!tmp) {
2580 fputs("Error: out-of-memory\n", stderr);
2581 goto cmdline_failed;
2582 }
2583 config_paths = tmp;
2584 config_paths[n_config_paths] = optarg;
2585 n_config_paths++;
2586 config_paths[n_config_paths] = NULL;
2587 break;
2588 }
2589 case 'E':
2590 module_symvers = optarg;
2591 cfg.check_symvers = 1;
2592 break;
2593 case 'F':
2594 system_map = optarg;
2595 break;
2596 case 'e':
2597 cfg.print_unknown = 1;
2598 break;
2599 case 'v':
2600 verbose++;
2601 break;
2602 case 'n':
2603 out = stdout;
2604 break;
2605 case 'P':
2606 if (optarg[1] != '\0') {
2607 CRIT("-P only takes a single char\n");
2608 goto cmdline_failed;
2609 }
2610 cfg.sym_prefix = optarg[0];
2611 break;
2612 case 'w':
2613 cfg.warn_dups = 1;
2614 break;
2615 case 'u':
2616 case 'q':
2617 case 'r':
2618 case 'm':
Lucas De Marchi61c48db2012-11-05 02:01:06 -02002619 if (idx > 0)
2620 WRN("Ignored deprecated option --%s\n",
2621 cmdopts[idx].name);
2622 else
2623 WRN("Ignored deprecated option -%c\n", c);
2624
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002625 break;
2626 case 'h':
Lucas De Marchi4a2e20d2012-11-06 16:54:17 -02002627 help();
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002628 return EXIT_SUCCESS;
2629 case 'V':
2630 puts(PACKAGE " version " VERSION);
Lucas De Marchi655de272015-06-07 02:46:22 -03002631 puts(KMOD_FEATURES);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002632 return EXIT_SUCCESS;
2633 case '?':
2634 goto cmdline_failed;
2635 default:
Lucas De Marchi61c48db2012-11-05 02:01:06 -02002636 ERR("unexpected getopt_long() value '%c'.\n", c);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002637 goto cmdline_failed;
2638 }
2639 }
2640
Laura Abbottf3f62f52015-09-28 15:39:14 -07002641 if (optind < argc) {
2642 if (!is_version_number(argv[optind])) {
2643 ERR("Bad version passed %s\n", argv[optind]);
2644 goto cmdline_failed;
2645 }
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002646 cfg.kversion = argv[optind];
2647 optind++;
2648 } else {
2649 if (uname(&un) < 0) {
2650 CRIT("uname() failed: %s\n", strerror(errno));
2651 goto cmdline_failed;
2652 }
2653 cfg.kversion = un.release;
2654 }
2655
2656 cfg.dirnamelen = snprintf(cfg.dirname, PATH_MAX,
Dave Reisnerc5b37db2012-09-27 11:00:42 -04002657 "%s/lib/modules/%s",
Lucas De Marchie15a56a2012-01-14 02:46:58 -02002658 root == NULL ? "" : root, cfg.kversion);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002659
2660 if (optind == argc)
2661 all = 1;
2662
2663 if (maybe_all) {
2664 if (out == stdout)
2665 goto done;
Gustavo Sverzut Barbieri18cd9da2011-12-24 11:16:40 -02002666 /* ignore up-to-date errors (< 0) */
Laura Abbott50f43ce2015-09-28 15:39:13 -07002667 if (depfile_up_to_date(cfg.dirname) == 1)
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002668 goto done;
2669 all = 1;
2670 }
2671
2672 ctx = kmod_new(cfg.dirname, &null_kmod_config);
2673 if (ctx == NULL) {
2674 CRIT("kmod_new(\"%s\", {NULL}) failed: %m\n", cfg.dirname);
2675 goto cmdline_failed;
2676 }
Lucas De Marchi52a50fe2012-11-06 18:26:34 -02002677
2678 log_setup_kmod_log(ctx, verbose);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002679
2680 err = depmod_init(&depmod, &cfg, ctx);
2681 if (err < 0) {
2682 CRIT("depmod_init: %s\n", strerror(-err));
2683 goto depmod_init_failed;
2684 }
2685 ctx = NULL; /* owned by depmod */
2686
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002687 if (module_symvers != NULL) {
2688 err = depmod_load_symvers(&depmod, module_symvers);
2689 if (err < 0) {
Dave Reisner63698372012-01-04 10:41:50 -05002690 CRIT("could not load %s: %s\n", module_symvers,
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002691 strerror(-err));
2692 goto cmdline_failed;
2693 }
2694 } else if (system_map != NULL) {
2695 err = depmod_load_system_map(&depmod, system_map);
2696 if (err < 0) {
Khem Raj035cbdc2012-02-02 23:09:59 -08002697 CRIT("could not load %s: %s\n", system_map,
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002698 strerror(-err));
2699 goto cmdline_failed;
2700 }
2701 } else if (cfg.print_unknown) {
2702 WRN("-e needs -E or -F\n");
2703 cfg.print_unknown = 0;
2704 }
2705
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002706 if (all) {
2707 err = cfg_load(&cfg, config_paths);
2708 if (err < 0) {
Dave Reisner63698372012-01-04 10:41:50 -05002709 CRIT("could not load configuration files\n");
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002710 goto cmdline_modules_failed;
2711 }
2712 err = depmod_modules_search(&depmod);
2713 if (err < 0) {
Dave Reisner63698372012-01-04 10:41:50 -05002714 CRIT("could not search modules: %s\n", strerror(-err));
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002715 goto cmdline_modules_failed;
2716 }
2717 } else {
Lucas De Marchi31f1d0d2012-01-14 02:04:31 -02002718 int i;
2719
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002720 for (i = optind; i < argc; i++) {
2721 const char *path = argv[i];
2722 struct kmod_module *mod;
2723
2724 if (path[0] != '/') {
Gustavo Sverzut Barbierib0bcadd2012-01-03 15:04:34 -02002725 CRIT("%s: not absolute path.\n", path);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002726 goto cmdline_modules_failed;
2727 }
2728
2729 err = kmod_module_new_from_path(depmod.ctx, path, &mod);
2730 if (err < 0) {
Dave Reisner63698372012-01-04 10:41:50 -05002731 CRIT("could not create module %s: %s\n",
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002732 path, strerror(-err));
2733 goto cmdline_modules_failed;
2734 }
2735
2736 err = depmod_module_add(&depmod, mod);
2737 if (err < 0) {
Dave Reisner63698372012-01-04 10:41:50 -05002738 CRIT("could not add module %s: %s\n",
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002739 path, strerror(-err));
2740 kmod_module_unref(mod);
2741 goto cmdline_modules_failed;
2742 }
2743 }
2744 }
2745
Jan Alexander Steffens (heftig)00bd3192012-01-11 23:17:38 +01002746 err = depmod_modules_build_array(&depmod);
2747 if (err < 0) {
2748 CRIT("could not build module array: %s\n",
2749 strerror(-err));
2750 goto cmdline_modules_failed;
2751 }
2752
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002753 depmod_modules_sort(&depmod);
2754 err = depmod_load(&depmod);
2755 if (err < 0)
2756 goto cmdline_modules_failed;
2757
Gustavo Sverzut Barbieri25c41512011-12-23 00:55:33 -02002758 err = depmod_output(&depmod, out);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002759
2760done:
2761 depmod_shutdown(&depmod);
2762 cfg_free(&cfg);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002763 return err >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
2764
2765cmdline_modules_failed:
2766 depmod_shutdown(&depmod);
2767depmod_init_failed:
2768 if (ctx != NULL)
2769 kmod_unref(ctx);
2770cmdline_failed:
2771 cfg_free(&cfg);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002772 return EXIT_FAILURE;
2773}
Lucas De Marchif6cf14c2011-12-27 19:56:33 -02002774
Lucas De Marchif6cf14c2011-12-27 19:56:33 -02002775const struct kmod_cmd kmod_cmd_compat_depmod = {
2776 .name = "depmod",
2777 .cmd = do_depmod,
2778 .help = "compat depmod command",
2779};