blob: 9669a617ce29dd072610c9df113fa89759b261d1 [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 Marchi576dd432014-10-02 22:03:19 -030038
Lucas De Marchic2e42862014-10-03 01:41:42 -030039#include <libkmod.h>
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -020040
Lucas De Marchi4a2e20d2012-11-06 16:54:17 -020041#include "kmod.h"
42
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -020043#define DEFAULT_VERBOSE LOG_WARNING
44static int verbose = DEFAULT_VERBOSE;
45
46static const char CFG_BUILTIN_KEY[] = "built-in";
47static const char *default_cfg_paths[] = {
48 "/run/depmod.d",
49 SYSCONFDIR "/depmod.d",
Dave Reisnerc5b37db2012-09-27 11:00:42 -040050 "/lib/depmod.d",
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -020051 NULL
52};
53
54static const char cmdopts_s[] = "aAb:C:E:F:euqrvnP:wmVh";
55static const struct option cmdopts[] = {
Lucas De Marchi015946d2012-06-15 01:10:14 -030056 { "all", no_argument, 0, 'a' },
57 { "quick", no_argument, 0, 'A' },
58 { "basedir", required_argument, 0, 'b' },
59 { "config", required_argument, 0, 'C' },
60 { "symvers", required_argument, 0, 'E' },
61 { "filesyms", required_argument, 0, 'F' },
62 { "errsyms", no_argument, 0, 'e' },
63 { "unresolved-error", no_argument, 0, 'u' }, /* deprecated */
64 { "quiet", no_argument, 0, 'q' }, /* deprecated */
65 { "root", no_argument, 0, 'r' }, /* deprecated */
66 { "verbose", no_argument, 0, 'v' },
67 { "show", no_argument, 0, 'n' },
68 { "dry-run", no_argument, 0, 'n' },
Andrey Mazoc02a8e62012-12-12 16:36:58 +040069 { "symbol-prefix", required_argument, 0, 'P' },
Lucas De Marchi015946d2012-06-15 01:10:14 -030070 { "warn", no_argument, 0, 'w' },
71 { "map", no_argument, 0, 'm' }, /* deprecated */
72 { "version", no_argument, 0, 'V' },
73 { "help", no_argument, 0, 'h' },
74 { }
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -020075};
76
Lucas De Marchi4a2e20d2012-11-06 16:54:17 -020077static void help(void)
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -020078{
Lucas De Marchi34e06bf2012-11-06 17:32:41 -020079 printf("Usage:\n"
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -020080 "\t%s -[aA] [options] [forced_version]\n"
81 "\n"
82 "If no arguments (except options) are given, \"depmod -a\" is assumed\n"
83 "\n"
84 "depmod will output a dependency list suitable for the modprobe utility.\n"
85 "\n"
86 "Options:\n"
87 "\t-a, --all Probe all modules\n"
88 "\t-A, --quick Only does the work if there's a new module\n"
89 "\t-e, --errsyms Report not supplied symbols\n"
90 "\t-n, --show Write the dependency file on stdout only\n"
91 "\t-P, --symbol-prefix Architecture symbol prefix\n"
Gustavo Sverzut Barbieri59886522012-01-03 15:06:08 -020092 "\t-C, --config=PATH Read configuration from PATH\n"
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -020093 "\t-v, --verbose Enable verbose mode\n"
94 "\t-w, --warn Warn on duplicates\n"
95 "\t-V, --version show version\n"
96 "\t-h, --help show this help\n"
97 "\n"
98 "The following options are useful for people managing distributions:\n"
99 "\t-b, --basedir=DIR Use an image of a module tree.\n"
100 "\t-F, --filesyms=FILE Use the file instead of the\n"
101 "\t current kernel symbols.\n"
102 "\t-E, --symvers=FILE Use Module.symvers file to check\n"
103 "\t symbol versions.\n",
Lucas De Marchi7c04aee2012-11-06 19:20:09 -0200104 program_invocation_short_name);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200105}
106
Lucas De Marchi1958af82013-04-21 16:16:18 -0300107_printf_format_(1, 2)
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200108static inline void _show(const char *fmt, ...)
109{
110 va_list args;
111
112 if (verbose <= DEFAULT_VERBOSE)
113 return;
114
115 va_start(args, fmt);
116 vfprintf(stdout, fmt, args);
117 fflush(stdout);
118 va_end(args);
119}
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200120#define SHOW(...) _show(__VA_ARGS__)
121
122
Gustavo Sverzut Barbieri0de40462011-12-23 23:11:41 -0200123/* binary index write *************************************************/
124#include <arpa/inet.h>
Gustavo Sverzut Barbieri0de40462011-12-23 23:11:41 -0200125/* BEGIN: code from module-init-tools/index.c just modified to compile here.
126 *
127 * Original copyright:
128 * index.c: module index file shared functions for modprobe and depmod
129 * Copyright (C) 2008 Alan Jenkins <alan-jenkins@tuffmail.co.uk>.
130 *
131 * These programs are free software; you can redistribute it and/or modify
132 * it under the terms of the GNU General Public License as published by
133 * the Free Software Foundation; either version 2 of the License, or
134 * (at your option) any later version.
135 *
136 * This program is distributed in the hope that it will be useful,
137 * but WITHOUT ANY WARRANTY; without even the implied warranty of
138 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
139 * GNU General Public License for more details.
140 *
141 * You should have received a copy of the GNU General Public License
142 * along with these programs. If not, see <http://www.gnu.org/licenses/>.
143 */
144
Lucas De Marchi778395e2014-10-28 01:52:49 -0200145/* see documentation in libkmod/libkmod-index.c */
Gustavo Sverzut Barbieri0de40462011-12-23 23:11:41 -0200146
Gustavo Sverzut Barbieri0de40462011-12-23 23:11:41 -0200147#define INDEX_MAGIC 0xB007F457
Gustavo Sverzut Barbieri0de40462011-12-23 23:11:41 -0200148#define INDEX_VERSION_MAJOR 0x0002
149#define INDEX_VERSION_MINOR 0x0001
150#define INDEX_VERSION ((INDEX_VERSION_MAJOR<<16)|INDEX_VERSION_MINOR)
Lucas De Marchi778395e2014-10-28 01:52:49 -0200151#define INDEX_CHILDMAX 128
Gustavo Sverzut Barbieri0de40462011-12-23 23:11:41 -0200152
153struct index_value {
154 struct index_value *next;
155 unsigned int priority;
156 char value[0];
157};
158
159/* In-memory index (depmod only) */
Gustavo Sverzut Barbieri0de40462011-12-23 23:11:41 -0200160struct index_node {
161 char *prefix; /* path compression */
162 struct index_value *values;
163 unsigned char first; /* range of child nodes */
164 unsigned char last;
165 struct index_node *children[INDEX_CHILDMAX]; /* indexed by character */
166};
167
Gustavo Sverzut Barbieri0de40462011-12-23 23:11:41 -0200168
169/* Format of node offsets within index file */
170enum node_offset {
171 INDEX_NODE_FLAGS = 0xF0000000, /* Flags in high nibble */
172 INDEX_NODE_PREFIX = 0x80000000,
173 INDEX_NODE_VALUES = 0x40000000,
174 INDEX_NODE_CHILDS = 0x20000000,
175
176 INDEX_NODE_MASK = 0x0FFFFFFF, /* Offset value */
177};
178
179static struct index_node *index_create(void)
180{
181 struct index_node *node;
182
183 node = NOFAIL(calloc(sizeof(struct index_node), 1));
184 node->prefix = NOFAIL(strdup(""));
185 node->first = INDEX_CHILDMAX;
186
187 return node;
188}
189
190static void index_values_free(struct index_value *values)
191{
192 while (values) {
193 struct index_value *value = values;
194
195 values = value->next;
196 free(value);
197 }
198}
199
200static void index_destroy(struct index_node *node)
201{
202 int c;
203
204 for (c = node->first; c <= node->last; c++) {
205 struct index_node *child = node->children[c];
206
207 if (child)
208 index_destroy(child);
209 }
210 index_values_free(node->values);
211 free(node->prefix);
212 free(node);
213}
214
215static void index__checkstring(const char *str)
216{
217 int i;
218
219 for (i = 0; str[i]; i++) {
220 int ch = str[i];
221
222 if (ch >= INDEX_CHILDMAX)
223 CRIT("Module index: bad character '%c'=0x%x - only 7-bit ASCII is supported:"
224 "\n%s\n", (char) ch, (int) ch, str);
225 }
226}
227
228static int index_add_value(struct index_value **values,
229 const char *value, unsigned int priority)
230{
231 struct index_value *v;
232 int duplicate = 0;
233 int len;
234
235 /* report the presence of duplicate values */
236 for (v = *values; v; v = v->next) {
237 if (streq(v->value, value))
238 duplicate = 1;
239 }
240
241 /* find position to insert value */
242 while (*values && (*values)->priority < priority)
243 values = &(*values)->next;
244
245 len = strlen(value);
246 v = NOFAIL(calloc(sizeof(struct index_value) + len + 1, 1));
247 v->next = *values;
248 v->priority = priority;
249 memcpy(v->value, value, len + 1);
250 *values = v;
251
252 return duplicate;
253}
254
255static int index_insert(struct index_node *node, const char *key,
256 const char *value, unsigned int priority)
257{
258 int i = 0; /* index within str */
259 int ch;
260
261 index__checkstring(key);
262 index__checkstring(value);
263
264 while(1) {
265 int j; /* index within node->prefix */
266
267 /* Ensure node->prefix is a prefix of &str[i].
268 If it is not already, then we must split node. */
269 for (j = 0; node->prefix[j]; j++) {
270 ch = node->prefix[j];
271
272 if (ch != key[i+j]) {
273 char *prefix = node->prefix;
274 struct index_node *n;
275
276 /* New child is copy of node with prefix[j+1..N] */
277 n = NOFAIL(calloc(sizeof(struct index_node), 1));
278 memcpy(n, node, sizeof(struct index_node));
279 n->prefix = NOFAIL(strdup(&prefix[j+1]));
280
281 /* Parent has prefix[0..j], child at prefix[j] */
282 memset(node, 0, sizeof(struct index_node));
283 prefix[j] = '\0';
284 node->prefix = prefix;
285 node->first = ch;
286 node->last = ch;
287 node->children[ch] = n;
288
289 break;
290 }
291 }
292 /* j is now length of node->prefix */
293 i += j;
294
295 ch = key[i];
296 if(ch == '\0')
297 return index_add_value(&node->values, value, priority);
298
299 if (!node->children[ch]) {
300 struct index_node *child;
301
302 if (ch < node->first)
303 node->first = ch;
304 if (ch > node->last)
305 node->last = ch;
306 node->children[ch] = NOFAIL(calloc(sizeof(struct index_node), 1));
307
308 child = node->children[ch];
309 child->prefix = NOFAIL(strdup(&key[i+1]));
310 child->first = INDEX_CHILDMAX;
311 index_add_value(&child->values, value, priority);
312
313 return 0;
314 }
315
316 /* Descend into child node and continue */
317 node = node->children[ch];
318 i++;
319 }
320}
321
322static int index__haschildren(const struct index_node *node)
323{
324 return node->first < INDEX_CHILDMAX;
325}
326
327/* Recursive post-order traversal
328
329 Pre-order would make for better read-side buffering / readahead / caching.
330 (post-order means you go backwards in the file as you descend the tree).
331 However, index reading is already fast enough.
332 Pre-order is simpler for writing, and depmod is already slow.
333 */
334static uint32_t index_write__node(const struct index_node *node, FILE *out)
335{
336 uint32_t *child_offs = NULL;
337 int child_count = 0;
338 long offset;
339
340 if (!node)
341 return 0;
342
343 /* Write children and save their offsets */
344 if (index__haschildren(node)) {
345 const struct index_node *child;
346 int i;
347
348 child_count = node->last - node->first + 1;
349 child_offs = NOFAIL(malloc(child_count * sizeof(uint32_t)));
350
351 for (i = 0; i < child_count; i++) {
352 child = node->children[node->first + i];
353 child_offs[i] = htonl(index_write__node(child, out));
354 }
355 }
356
357 /* Now write this node */
358 offset = ftell(out);
359
360 if (node->prefix[0]) {
361 fputs(node->prefix, out);
362 fputc('\0', out);
363 offset |= INDEX_NODE_PREFIX;
364 }
365
366 if (child_count) {
367 fputc(node->first, out);
368 fputc(node->last, out);
369 fwrite(child_offs, sizeof(uint32_t), child_count, out);
370 free(child_offs);
371 offset |= INDEX_NODE_CHILDS;
372 }
373
374 if (node->values) {
375 const struct index_value *v;
376 unsigned int value_count;
377 uint32_t u;
378
379 value_count = 0;
380 for (v = node->values; v != NULL; v = v->next)
381 value_count++;
382 u = htonl(value_count);
383 fwrite(&u, sizeof(u), 1, out);
384
385 for (v = node->values; v != NULL; v = v->next) {
386 u = htonl(v->priority);
387 fwrite(&u, sizeof(u), 1, out);
388 fputs(v->value, out);
389 fputc('\0', out);
390 }
391 offset |= INDEX_NODE_VALUES;
392 }
393
394 return offset;
395}
396
397static void index_write(const struct index_node *node, FILE *out)
398{
399 long initial_offset, final_offset;
400 uint32_t u;
401
402 u = htonl(INDEX_MAGIC);
403 fwrite(&u, sizeof(u), 1, out);
404 u = htonl(INDEX_VERSION);
405 fwrite(&u, sizeof(u), 1, out);
406
407 /* Second word is reserved for the offset of the root node */
408 initial_offset = ftell(out);
409 u = 0;
410 fwrite(&u, sizeof(uint32_t), 1, out);
411
412 /* Dump trie */
413 u = htonl(index_write__node(node, out));
414
415 /* Update first word */
416 final_offset = ftell(out);
417 fseek(out, initial_offset, SEEK_SET);
418 fwrite(&u, sizeof(uint32_t), 1, out);
419 fseek(out, final_offset, SEEK_SET);
420}
421
422/* END: code from module-init-tools/index.c just modified to compile here.
423 */
424
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200425/* configuration parsing **********************************************/
426struct cfg_override {
427 struct cfg_override *next;
428 size_t len;
429 char path[];
430};
431
432struct cfg_search {
433 struct cfg_search *next;
434 uint8_t builtin;
435 size_t len;
436 char path[];
437};
438
439struct cfg {
440 const char *kversion;
441 char dirname[PATH_MAX];
442 size_t dirnamelen;
443 char sym_prefix;
444 uint8_t check_symvers;
445 uint8_t print_unknown;
446 uint8_t warn_dups;
447 struct cfg_override *overrides;
448 struct cfg_search *searches;
449};
450
451static int cfg_search_add(struct cfg *cfg, const char *path, uint8_t builtin)
452{
453 struct cfg_search *s;
454 size_t len;
455
456 if (builtin)
457 len = 0;
458 else
459 len = strlen(path) + 1;
460
461 s = malloc(sizeof(struct cfg_search) + len);
462 if (s == NULL) {
463 ERR("search add: out of memory\n");
464 return -ENOMEM;
465 }
466 s->builtin = builtin;
467 if (builtin)
468 s->len = 0;
469 else {
Gustavo Sverzut Barbieri026c7b42012-01-03 16:03:06 -0200470 s->len = len - 1;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200471 memcpy(s->path, path, len);
472 }
473
474 DBG("search add: %s, builtin=%hhu\n", path, builtin);
475
476 s->next = cfg->searches;
477 cfg->searches = s;
478 return 0;
479}
480
481static void cfg_search_free(struct cfg_search *s)
482{
483 free(s);
484}
485
486static int cfg_override_add(struct cfg *cfg, const char *modname, const char *subdir)
487{
488 struct cfg_override *o;
489 size_t modnamelen = strlen(modname);
490 size_t subdirlen = strlen(subdir);
491 size_t i;
492
Gustavo Sverzut Barbieri026c7b42012-01-03 16:03:06 -0200493 o = malloc(sizeof(struct cfg_override) + subdirlen + 1
494 + modnamelen + 1);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200495 if (o == NULL) {
496 ERR("override add: out of memory\n");
497 return -ENOMEM;
498 }
Gustavo Sverzut Barbieri026c7b42012-01-03 16:03:06 -0200499 memcpy(o->path, subdir, subdirlen);
500 i = subdirlen;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200501 o->path[i] = '/';
502 i++;
503
504 memcpy(o->path + i, modname, modnamelen);
505 i += modnamelen;
506 o->path[i] = '\0'; /* no extension, so we can match .ko/.ko.gz */
507
508 o->len = i;
509
510 DBG("override add: %s\n", o->path);
511
512 o->next = cfg->overrides;
513 cfg->overrides = o;
514 return 0;
515}
516
517static void cfg_override_free(struct cfg_override *o)
518{
519 free(o);
520}
521
522static int cfg_kernel_matches(const struct cfg *cfg, const char *pattern)
523{
524 regex_t re;
525 int status;
526
527 /* old style */
528 if (streq(pattern, "*"))
529 return 1;
530
531 if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB) != 0)
532 return 0;
533
534 status = regexec(&re, cfg->kversion, 0, NULL, 0);
535 regfree(&re);
536
537 return status == 0;
538}
539
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200540static int cfg_file_parse(struct cfg *cfg, const char *filename)
541{
542 char *line;
543 FILE *fp;
544 unsigned int linenum = 0;
545 int err;
546
547 fp = fopen(filename, "r");
548 if (fp == NULL) {
549 err = -errno;
Gustavo Sverzut Barbierib0bcadd2012-01-03 15:04:34 -0200550 ERR("file parse %s: %m\n", filename);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200551 return err;
552 }
553
Lucas De Marchiaafd3832014-10-03 03:25:06 -0300554 while ((line = freadline_wrapped(fp, &linenum)) != NULL) {
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200555 char *cmd, *saveptr;
556
557 if (line[0] == '\0' || line[0] == '#')
558 goto done_next;
559
560 cmd = strtok_r(line, "\t ", &saveptr);
561 if (cmd == NULL)
562 goto done_next;
563
564 if (streq(cmd, "search")) {
565 const char *sp;
566 while ((sp = strtok_r(NULL, "\t ", &saveptr)) != NULL) {
567 uint8_t builtin = streq(sp, CFG_BUILTIN_KEY);
568 cfg_search_add(cfg, sp, builtin);
569 }
570 } else if (streq(cmd, "override")) {
571 const char *modname = strtok_r(NULL, "\t ", &saveptr);
572 const char *version = strtok_r(NULL, "\t ", &saveptr);
573 const char *subdir = strtok_r(NULL, "\t ", &saveptr);
574
575 if (modname == NULL || version == NULL ||
576 subdir == NULL)
577 goto syntax_error;
578
579 if (!cfg_kernel_matches(cfg, version)) {
580 INF("%s:%u: override kernel did not match %s\n",
581 filename, linenum, version);
582 goto done_next;
583 }
584
585 cfg_override_add(cfg, modname, subdir);
586 } else if (streq(cmd, "include")
587 || streq(cmd, "make_map_files")) {
588 INF("%s:%u: command %s not implemented yet\n",
589 filename, linenum, cmd);
590 } else {
591syntax_error:
592 ERR("%s:%u: ignoring bad line starting with '%s'\n",
593 filename, linenum, cmd);
594 }
595
596done_next:
597 free(line);
598 }
599
600 fclose(fp);
601
602 return 0;
603}
604
605static int cfg_files_filter_out(DIR *d, const char *dir, const char *name)
606{
607 size_t len = strlen(name);
608 struct stat st;
609
610 if (name[0] == '.')
611 return 1;
612
613 if (len < 6 || !streq(name + len - 5, ".conf")) {
614 INF("All cfg files need .conf: %s/%s\n", dir, name);
615 return 1;
616 }
617
618 fstatat(dirfd(d), name, &st, 0);
619 if (S_ISDIR(st.st_mode)) {
620 ERR("Directories inside directories are not supported: %s/%s\n",
621 dir, name);
622 return 1;
623 }
624
625 return 0;
626}
627
628struct cfg_file {
629 size_t dirlen;
630 size_t namelen;
631 const char *name;
632 char path[];
633};
634
635static void cfg_file_free(struct cfg_file *f)
636{
637 free(f);
638}
639
640static int cfg_files_insert_sorted(struct cfg_file ***p_files, size_t *p_n_files,
641 const char *dir, const char *name)
642{
643 struct cfg_file **files, *f;
644 size_t i, n_files, namelen, dirlen;
645 void *tmp;
646
647 dirlen = strlen(dir);
648 if (name != NULL)
649 namelen = strlen(name);
650 else {
651 name = basename(dir);
652 namelen = strlen(name);
Gustavo Sverzut Barbieri8ea02fe2012-01-03 15:11:58 -0200653 dirlen -= namelen + 1;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200654 }
655
656 n_files = *p_n_files;
657 files = *p_files;
658 for (i = 0; i < n_files; i++) {
659 int cmp = strcmp(name, files[i]->name);
660 if (cmp == 0) {
661 DBG("Ignoring duplicate config file: %.*s/%s\n",
662 (int)dirlen, dir, name);
663 return -EEXIST;
664 } else if (cmp < 0)
665 break;
666 }
667
668 f = malloc(sizeof(struct cfg_file) + dirlen + namelen + 2);
669 if (f == NULL) {
670 ERR("files insert sorted: out of memory\n");
671 return -ENOMEM;
672 }
673
674 tmp = realloc(files, sizeof(struct cfg_file *) * (n_files + 1));
675 if (tmp == NULL) {
676 ERR("files insert sorted: out of memory\n");
677 free(f);
678 return -ENOMEM;
679 }
680 *p_files = files = tmp;
681
682 if (i < n_files) {
683 memmove(files + i + 1, files + i,
684 sizeof(struct cfg_file *) * (n_files - i));
685 }
686 files[i] = f;
687
688 f->dirlen = dirlen;
689 f->namelen = namelen;
690 f->name = f->path + dirlen + 1;
691 memcpy(f->path, dir, dirlen);
692 f->path[dirlen] = '/';
693 memcpy(f->path + dirlen + 1, name, namelen);
694 f->path[dirlen + 1 + namelen] = '\0';
695
696 *p_n_files = n_files + 1;
697 return 0;
698}
699
700/*
701 * Insert configuration files ignoring duplicates
702 */
703static int cfg_files_list(struct cfg_file ***p_files, size_t *p_n_files,
704 const char *path)
705{
Lucas De Marchi7e0385c2013-08-27 23:29:47 -0300706 struct dirent *dent;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200707 DIR *d;
708 int err = 0;
709 struct stat st;
710
711 if (stat(path, &st) != 0) {
712 err = -errno;
713 DBG("could not stat '%s': %m\n", path);
714 return err;
715 }
716
Michal Marek519d27d2014-03-04 16:51:25 +0100717 if (!S_ISDIR(st.st_mode)) {
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200718 cfg_files_insert_sorted(p_files, p_n_files, path, NULL);
719 return 0;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200720 }
721
722 d = opendir(path);
723 if (d == NULL) {
724 ERR("files list %s: %m\n", path);
725 return -EINVAL;
726 }
727
Lucas De Marchi7e0385c2013-08-27 23:29:47 -0300728 for (dent = readdir(d); dent != NULL; dent = readdir(d)) {
729 if (cfg_files_filter_out(d, path, dent->d_name))
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200730 continue;
731
Lucas De Marchi7e0385c2013-08-27 23:29:47 -0300732 cfg_files_insert_sorted(p_files, p_n_files, path, dent->d_name);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200733 }
734
735 closedir(d);
Khem Raj035cbdc2012-02-02 23:09:59 -0800736 DBG("parsed configuration files from %s\n", path);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200737 return err;
738}
739
740static int cfg_load(struct cfg *cfg, const char * const *cfg_paths)
741{
742 size_t i, n_files = 0;
743 struct cfg_file **files = NULL;
744
745 if (cfg_paths == NULL)
746 cfg_paths = default_cfg_paths;
747
748 for (i = 0; cfg_paths[i] != NULL; i++)
749 cfg_files_list(&files, &n_files, cfg_paths[i]);
750
751 for (i = 0; i < n_files; i++) {
752 struct cfg_file *f = files[i];
753 cfg_file_parse(cfg, f->path);
754 cfg_file_free(f);
755 }
756 free(files);
757
758 /* For backward compatibility add "updates" to the head of the search
759 * list here. But only if there was no "search" option specified.
760 */
761 if (cfg->searches == NULL)
762 cfg_search_add(cfg, "updates", 0);
763
764 return 0;
765}
766
767static void cfg_free(struct cfg *cfg)
768{
769 while (cfg->overrides) {
770 struct cfg_override *tmp = cfg->overrides;
771 cfg->overrides = cfg->overrides->next;
772 cfg_override_free(tmp);
773 }
774
775 while (cfg->searches) {
776 struct cfg_search *tmp = cfg->searches;
777 cfg->searches = cfg->searches->next;
778 cfg_search_free(tmp);
779 }
780}
781
782
783/* depmod calculations ***********************************************/
784struct mod {
785 struct kmod_module *kmod;
Lucas De Marchie4a73522012-10-30 03:46:12 -0200786 char *path;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200787 const char *relpath; /* path relative to '$ROOT/lib/modules/$VER/' */
Lucas De Marchib51ac402012-10-30 02:32:48 -0200788 char *uncrelpath; /* same as relpath but ending in .ko */
Lucas De Marchi7062eca2012-10-30 03:16:10 -0200789 struct kmod_list *info_list;
Lucas De Marchiec587f22012-10-30 03:16:10 -0200790 struct kmod_list *dep_sym_list;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200791 struct array deps; /* struct symbol */
792 size_t baselen; /* points to start of basename/filename */
Lucas De Marchia873f232014-05-30 09:01:24 -0300793 size_t modnamesz;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200794 int sort_idx; /* sort index using modules.order */
795 int dep_sort_idx; /* topological sort index */
796 uint16_t idx; /* index in depmod->modules.array */
797 uint16_t users; /* how many modules depend on this one */
Lucas De Marchic89d2192014-05-30 09:38:26 -0300798 bool visited; /* helper field to report cycles */
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200799 char modname[];
800};
801
802struct symbol {
803 struct mod *owner;
804 uint64_t crc;
805 char name[];
806};
807
808struct depmod {
809 const struct cfg *cfg;
810 struct kmod_ctx *ctx;
811 struct array modules;
Lucas De Marchi88c247f2012-10-03 16:28:24 -0300812 struct hash *modules_by_uncrelpath;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200813 struct hash *modules_by_name;
814 struct hash *symbols;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200815};
816
817static void mod_free(struct mod *mod)
818{
819 DBG("free %p kmod=%p, path=%s\n", mod, mod->kmod, mod->path);
820 array_free_array(&mod->deps);
Lucas De Marchi02c64df2012-11-16 12:05:42 -0200821 kmod_module_unref(mod->kmod);
Lucas De Marchi7062eca2012-10-30 03:16:10 -0200822 kmod_module_info_free_list(mod->info_list);
Lucas De Marchiec587f22012-10-30 03:16:10 -0200823 kmod_module_dependency_symbols_free_list(mod->dep_sym_list);
Lucas De Marchi88c247f2012-10-03 16:28:24 -0300824 free(mod->uncrelpath);
Lucas De Marchie4a73522012-10-30 03:46:12 -0200825 free(mod->path);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200826 free(mod);
827}
828
829static int mod_add_dependency(struct mod *mod, struct symbol *sym)
830{
831 int err;
832
833 DBG("%s depends on %s %s\n", mod->path, sym->name,
834 sym->owner != NULL ? sym->owner->path : "(unknown)");
835
836 if (sym->owner == NULL)
837 return 0;
838
839 err = array_append_unique(&mod->deps, sym->owner);
840 if (err == -EEXIST)
841 return 0;
842 if (err < 0)
843 return err;
844
845 sym->owner->users++;
846 SHOW("%s needs \"%s\": %s\n", mod->path, sym->name, sym->owner->path);
847 return 0;
848}
849
850static void symbol_free(struct symbol *sym)
851{
852 DBG("free %p sym=%s, owner=%p %s\n", sym, sym->name, sym->owner,
853 sym->owner != NULL ? sym->owner->path : "");
854 free(sym);
855}
856
Lucas De Marchif6b838e2012-01-14 02:03:21 -0200857static int depmod_init(struct depmod *depmod, struct cfg *cfg,
858 struct kmod_ctx *ctx)
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200859{
860 int err = 0;
861
862 depmod->cfg = cfg;
863 depmod->ctx = ctx;
864
865 array_init(&depmod->modules, 128);
866
Lucas De Marchi88c247f2012-10-03 16:28:24 -0300867 depmod->modules_by_uncrelpath = hash_new(512, NULL);
868 if (depmod->modules_by_uncrelpath == NULL) {
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200869 err = -errno;
Lucas De Marchi88c247f2012-10-03 16:28:24 -0300870 goto modules_by_uncrelpath_failed;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200871 }
872
873 depmod->modules_by_name = hash_new(512, NULL);
874 if (depmod->modules_by_name == NULL) {
875 err = -errno;
876 goto modules_by_name_failed;
877 }
878
879 depmod->symbols = hash_new(2048, (void (*)(void *))symbol_free);
880 if (depmod->symbols == NULL) {
881 err = -errno;
882 goto symbols_failed;
883 }
884
885 return 0;
886
887symbols_failed:
888 hash_free(depmod->modules_by_name);
889modules_by_name_failed:
Lucas De Marchi88c247f2012-10-03 16:28:24 -0300890 hash_free(depmod->modules_by_uncrelpath);
891modules_by_uncrelpath_failed:
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200892 return err;
893}
894
895static void depmod_shutdown(struct depmod *depmod)
896{
897 size_t i;
898
899 hash_free(depmod->symbols);
900
Lucas De Marchi88c247f2012-10-03 16:28:24 -0300901 hash_free(depmod->modules_by_uncrelpath);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200902
903 hash_free(depmod->modules_by_name);
904
905 for (i = 0; i < depmod->modules.count; i++)
906 mod_free(depmod->modules.array[i]);
907 array_free_array(&depmod->modules);
908
909 kmod_unref(depmod->ctx);
910}
911
912static int depmod_module_add(struct depmod *depmod, struct kmod_module *kmod)
913{
914 const struct cfg *cfg = depmod->cfg;
Lucas De Marchi88c247f2012-10-03 16:28:24 -0300915 const char *modname, *lastslash;
Lucas De Marchia873f232014-05-30 09:01:24 -0300916 size_t modnamesz;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200917 struct mod *mod;
918 int err;
919
920 modname = kmod_module_get_name(kmod);
Lucas De Marchia873f232014-05-30 09:01:24 -0300921 modnamesz = strlen(modname) + 1;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200922
Lucas De Marchia873f232014-05-30 09:01:24 -0300923 mod = calloc(1, sizeof(struct mod) + modnamesz);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200924 if (mod == NULL)
925 return -ENOMEM;
926 mod->kmod = kmod;
927 mod->sort_idx = depmod->modules.count + 1;
928 mod->dep_sort_idx = INT32_MAX;
Lucas De Marchia873f232014-05-30 09:01:24 -0300929 memcpy(mod->modname, modname, modnamesz);
930 mod->modnamesz = modnamesz;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200931
932 array_init(&mod->deps, 4);
933
Lucas De Marchie4a73522012-10-30 03:46:12 -0200934 mod->path = strdup(kmod_module_get_path(kmod));
Lucas De Marchi88c247f2012-10-03 16:28:24 -0300935 lastslash = strrchr(mod->path, '/');
936 mod->baselen = lastslash - mod->path;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200937 if (strncmp(mod->path, cfg->dirname, cfg->dirnamelen) == 0 &&
938 mod->path[cfg->dirnamelen] == '/')
939 mod->relpath = mod->path + cfg->dirnamelen + 1;
940 else
941 mod->relpath = NULL;
942
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200943 err = hash_add_unique(depmod->modules_by_name, mod->modname, mod);
944 if (err < 0) {
945 ERR("hash_add_unique %s: %s\n", mod->modname, strerror(-err));
Lucas De Marchi88c247f2012-10-03 16:28:24 -0300946 goto fail;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200947 }
948
949 if (mod->relpath != NULL) {
Lucas De Marchia873f232014-05-30 09:01:24 -0300950 size_t uncrelpathlen = lastslash - mod->relpath + modnamesz
Lucas De Marchib95506f2014-10-08 14:33:42 -0300951 + strlen(KMOD_EXTENSION_UNCOMPRESSED);
Lucas De Marchi88c247f2012-10-03 16:28:24 -0300952 mod->uncrelpath = memdup(mod->relpath, uncrelpathlen + 1);
953 mod->uncrelpath[uncrelpathlen] = '\0';
954 err = hash_add_unique(depmod->modules_by_uncrelpath,
955 mod->uncrelpath, mod);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200956 if (err < 0) {
957 ERR("hash_add_unique %s: %s\n",
Lucas De Marchi06294622012-11-16 11:35:30 -0200958 mod->uncrelpath, strerror(-err));
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200959 hash_del(depmod->modules_by_name, mod->modname);
Lucas De Marchi88c247f2012-10-03 16:28:24 -0300960 goto fail;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200961 }
962 }
963
964 DBG("add %p kmod=%p, path=%s\n", mod, kmod, mod->path);
965
966 return 0;
Lucas De Marchi88c247f2012-10-03 16:28:24 -0300967
968fail:
969 free(mod->uncrelpath);
970 free(mod);
971 return err;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200972}
973
Gustavo Sverzut Barbieri3db5bf92012-01-03 16:14:57 -0200974static int depmod_module_del(struct depmod *depmod, struct mod *mod)
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200975{
Gustavo Sverzut Barbieri3db5bf92012-01-03 16:14:57 -0200976 DBG("del %p kmod=%p, path=%s\n", mod, mod->kmod, mod->path);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200977
Lucas De Marchi06294622012-11-16 11:35:30 -0200978 if (mod->uncrelpath != NULL)
979 hash_del(depmod->modules_by_uncrelpath, mod->uncrelpath);
Gustavo Sverzut Barbieri3db5bf92012-01-03 16:14:57 -0200980
981 hash_del(depmod->modules_by_name, mod->modname);
982
Gustavo Sverzut Barbieri3db5bf92012-01-03 16:14:57 -0200983 mod_free(mod);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200984 return 0;
985}
986
987/* returns if existing module @mod is higher priority than newpath.
988 * note this is the inverse of module-init-tools is_higher_priority()
989 */
990static 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)
991{
992 const struct cfg *cfg = depmod->cfg;
993 const struct cfg_override *ov;
994 const struct cfg_search *se;
Lucas De Marchia873f232014-05-30 09:01:24 -0300995
996 /* baselen includes the last '/' and mod->baselen doesn't. So it's
997 * actually correct to use modnamelen in the first and modnamesz in
998 * the latter */
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -0200999 size_t newlen = baselen + modnamelen;
Lucas De Marchia873f232014-05-30 09:01:24 -03001000 size_t oldlen = mod->baselen + mod->modnamesz;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001001 const char *oldpath = mod->path;
1002 int i, bprio = -1, oldprio = -1, newprio = -1;
1003
Gustavo Sverzut Barbieri026c7b42012-01-03 16:03:06 -02001004 assert(strncmp(newpath, cfg->dirname, cfg->dirnamelen) == 0);
1005 assert(strncmp(oldpath, cfg->dirname, cfg->dirnamelen) == 0);
1006
1007 newpath += cfg->dirnamelen + 1;
1008 newlen -= cfg->dirnamelen + 1;
1009 oldpath += cfg->dirnamelen + 1;
1010 oldlen -= cfg->dirnamelen + 1;
1011
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001012 DBG("comparing priorities of %s and %s\n",
1013 oldpath, newpath);
1014
1015 for (ov = cfg->overrides; ov != NULL; ov = ov->next) {
1016 DBG("override %s\n", ov->path);
1017 if (newlen == ov->len && memcmp(ov->path, newpath, newlen) == 0)
1018 return 0;
1019 if (oldlen == ov->len && memcmp(ov->path, oldpath, oldlen) == 0)
1020 return 1;
1021 }
1022
1023 for (i = 0, se = cfg->searches; se != NULL; se = se->next, i++) {
1024 DBG("search %s\n", se->builtin ? "built-in" : se->path);
1025 if (se->builtin)
1026 bprio = i;
Anssi Hannula49b33c12014-03-19 01:26:00 +02001027 else if (newlen > se->len && newpath[se->len] == '/' &&
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001028 memcmp(se->path, newpath, se->len) == 0)
1029 newprio = i;
Anssi Hannula49b33c12014-03-19 01:26:00 +02001030 else if (oldlen > se->len && oldpath[se->len] == '/' &&
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001031 memcmp(se->path, oldpath, se->len) == 0)
1032 oldprio = i;
1033 }
1034
1035 if (newprio < 0)
1036 newprio = bprio;
1037 if (oldprio < 0)
1038 oldprio = bprio;
1039
1040 DBG("priorities: built-in: %d, old: %d, new: %d\n",
Anssi Hannula27881f62014-03-19 01:26:01 +02001041 bprio, oldprio, newprio);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001042
1043 return newprio <= oldprio;
1044}
1045
1046static int depmod_modules_search_file(struct depmod *depmod, size_t baselen, size_t namelen, const char *path)
1047{
1048 struct kmod_module *kmod;
1049 struct mod *mod;
Gustavo Sverzut Barbieri3db5bf92012-01-03 16:14:57 -02001050 const char *relpath;
Lucas De Marchi6daceb22012-01-08 01:02:29 -02001051 char modname[PATH_MAX];
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001052 size_t modnamelen;
Lucas De Marchi18a492e2012-04-26 11:39:54 -03001053 int err;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001054
Lucas De Marchi650f89c2012-11-28 14:26:23 -02001055 if (!path_ends_with_kmod_ext(path + baselen, namelen))
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001056 return 0;
1057
Gustavo Sverzut Barbieri3db5bf92012-01-03 16:14:57 -02001058 if (path_to_modname(path, modname, &modnamelen) == NULL) {
Dave Reisner63698372012-01-04 10:41:50 -05001059 ERR("could not get modname from path %s\n", path);
Gustavo Sverzut Barbieri3db5bf92012-01-03 16:14:57 -02001060 return -EINVAL;
1061 }
1062
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001063 relpath = path + depmod->cfg->dirnamelen + 1;
Gustavo Sverzut Barbieri3db5bf92012-01-03 16:14:57 -02001064 DBG("try %s (%s)\n", relpath, modname);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001065
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001066 mod = hash_find(depmod->modules_by_name, modname);
Gustavo Sverzut Barbieri3db5bf92012-01-03 16:14:57 -02001067 if (mod == NULL)
1068 goto add;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001069
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001070 if (depmod_module_is_higher_priority(depmod, mod, baselen,
1071 namelen, modnamelen, path)) {
1072 DBG("Ignored lower priority: %s, higher: %s\n",
1073 path, mod->path);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001074 return 0;
1075 }
1076
Gustavo Sverzut Barbieri3db5bf92012-01-03 16:14:57 -02001077 DBG("Replace lower priority %s with new module %s\n",
1078 mod->relpath, relpath);
1079 err = depmod_module_del(depmod, mod);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001080 if (err < 0) {
Dave Reisner63698372012-01-04 10:41:50 -05001081 ERR("could not del module %s: %s\n", mod->path, strerror(-err));
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001082 return err;
1083 }
1084
Gustavo Sverzut Barbieri3db5bf92012-01-03 16:14:57 -02001085add:
1086 err = kmod_module_new_from_path(depmod->ctx, path, &kmod);
1087 if (err < 0) {
Dave Reisner63698372012-01-04 10:41:50 -05001088 ERR("could not create module %s: %s\n", path, strerror(-err));
Gustavo Sverzut Barbieri3db5bf92012-01-03 16:14:57 -02001089 return err;
1090 }
1091
1092 err = depmod_module_add(depmod, kmod);
1093 if (err < 0) {
Dave Reisner63698372012-01-04 10:41:50 -05001094 ERR("could not add module %s: %s\n",
Gustavo Sverzut Barbieri3db5bf92012-01-03 16:14:57 -02001095 path, strerror(-err));
1096 kmod_module_unref(kmod);
1097 return err;
1098 }
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001099 return 0;
1100}
1101
1102static int depmod_modules_search_dir(struct depmod *depmod, DIR *d, size_t baselen, char *path)
1103{
1104 struct dirent *de;
1105 int err = 0, dfd = dirfd(d);
1106
1107 while ((de = readdir(d)) != NULL) {
1108 const char *name = de->d_name;
1109 size_t namelen;
1110 uint8_t is_dir;
1111
1112 if (name[0] == '.' && (name[1] == '\0' ||
1113 (name[1] == '.' && name[2] == '\0')))
1114 continue;
1115 if (streq(name, "build") || streq(name, "source"))
1116 continue;
1117 namelen = strlen(name);
1118 if (baselen + namelen + 2 >= PATH_MAX) {
1119 path[baselen] = '\0';
Lucas De Marchi1958af82013-04-21 16:16:18 -03001120 ERR("path is too long %s%s\n", path, name);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001121 continue;
1122 }
1123 memcpy(path + baselen, name, namelen + 1);
1124
1125 if (de->d_type == DT_REG)
1126 is_dir = 0;
1127 else if (de->d_type == DT_DIR)
1128 is_dir = 1;
1129 else {
1130 struct stat st;
1131 if (fstatat(dfd, name, &st, 0) < 0) {
1132 ERR("fstatat(%d, %s): %m\n", dfd, name);
1133 continue;
1134 } else if (S_ISREG(st.st_mode))
1135 is_dir = 0;
1136 else if (S_ISDIR(st.st_mode))
1137 is_dir = 1;
1138 else {
1139 ERR("unsupported file type %s: %o\n",
1140 path, st.st_mode & S_IFMT);
1141 continue;
1142 }
1143 }
1144
1145 if (is_dir) {
1146 int fd;
1147 DIR *subdir;
1148 if (baselen + namelen + 2 + NAME_MAX >= PATH_MAX) {
1149 ERR("directory path is too long %s\n", path);
1150 continue;
1151 }
1152 fd = openat(dfd, name, O_RDONLY);
1153 if (fd < 0) {
1154 ERR("openat(%d, %s, O_RDONLY): %m\n",
1155 dfd, name);
1156 continue;
1157 }
1158 subdir = fdopendir(fd);
1159 if (subdir == NULL) {
1160 ERR("fdopendir(%d): %m\n", fd);
1161 close(fd);
1162 continue;
1163 }
1164 path[baselen + namelen] = '/';
1165 path[baselen + namelen + 1] = '\0';
1166 err = depmod_modules_search_dir(depmod, subdir,
1167 baselen + namelen + 1,
1168 path);
1169 closedir(subdir);
1170 } else {
1171 err = depmod_modules_search_file(depmod, baselen,
1172 namelen, path);
1173 }
1174
1175 if (err < 0) {
1176 path[baselen + namelen] = '\0';
1177 ERR("failed %s: %s\n", path, strerror(-err));
1178 err = 0; /* ignore errors */
1179 }
1180 }
1181
1182 return err;
1183}
1184
1185static int depmod_modules_search(struct depmod *depmod)
1186{
1187 char path[PATH_MAX];
1188 DIR *d = opendir(depmod->cfg->dirname);
1189 size_t baselen;
1190 int err;
1191 if (d == NULL) {
1192 err = -errno;
Dave Reisner63698372012-01-04 10:41:50 -05001193 ERR("could not open directory %s: %m\n", depmod->cfg->dirname);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001194 return err;
1195 }
1196
1197 baselen = depmod->cfg->dirnamelen;
1198 memcpy(path, depmod->cfg->dirname, baselen);
1199 path[baselen] = '/';
1200 baselen++;
1201 path[baselen] = '\0';
1202
1203 err = depmod_modules_search_dir(depmod, d, baselen, path);
1204 closedir(d);
Lucas De Marchi20c6e182012-04-26 11:40:26 -03001205 return err;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001206}
1207
1208static int mod_cmp(const void *pa, const void *pb) {
1209 const struct mod *a = *(const struct mod **)pa;
1210 const struct mod *b = *(const struct mod **)pb;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001211 return a->sort_idx - b->sort_idx;
1212}
1213
Jan Alexander Steffens (heftig)00bd3192012-01-11 23:17:38 +01001214static int depmod_modules_build_array(struct depmod *depmod)
1215{
1216 struct hash_iter module_iter;
Lucas De Marchi33557e82012-01-14 02:04:56 -02001217 const void *v;
Jan Alexander Steffens (heftig)00bd3192012-01-11 23:17:38 +01001218 int err;
1219
1220 hash_iter_init(depmod->modules_by_name, &module_iter);
Lucas De Marchi33557e82012-01-14 02:04:56 -02001221 while (hash_iter_next(&module_iter, NULL, &v)) {
1222 struct mod *mod = (struct mod *) v;
1223 mod->idx = depmod->modules.count;
Jan Alexander Steffens (heftig)00bd3192012-01-11 23:17:38 +01001224 err = array_append(&depmod->modules, mod);
1225 if (err < 0)
1226 return err;
1227 }
1228
1229 return 0;
1230}
1231
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001232static void depmod_modules_sort(struct depmod *depmod)
1233{
1234 char order_file[PATH_MAX], line[PATH_MAX];
1235 FILE *fp;
1236 unsigned idx = 0, total = 0;
1237
1238 snprintf(order_file, sizeof(order_file), "%s/modules.order",
1239 depmod->cfg->dirname);
1240 fp = fopen(order_file, "r");
1241 if (fp == NULL) {
Lucas De Marchi56406fd2012-01-04 20:53:10 -02001242 WRN("could not open %s: %m\n", order_file);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001243 return;
1244 }
1245
1246 while (fgets(line, sizeof(line), fp) != NULL) {
1247 size_t len = strlen(line);
1248 idx++;
1249 if (len == 0)
1250 continue;
1251 if (line[len - 1] != '\n') {
1252 ERR("%s:%u corrupted line misses '\\n'\n",
1253 order_file, idx);
1254 goto corrupted;
1255 }
1256 }
1257 total = idx + 1;
1258 idx = 0;
1259 fseek(fp, 0, SEEK_SET);
1260 while (fgets(line, sizeof(line), fp) != NULL) {
1261 size_t len = strlen(line);
1262 struct mod *mod;
1263
1264 idx++;
1265 if (len == 0)
1266 continue;
1267 line[len - 1] = '\0';
1268
Lucas De Marchi88c247f2012-10-03 16:28:24 -03001269 mod = hash_find(depmod->modules_by_uncrelpath, line);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001270 if (mod == NULL)
1271 continue;
1272 mod->sort_idx = idx - total;
1273 }
1274
1275 array_sort(&depmod->modules, mod_cmp);
1276 for (idx = 0; idx < depmod->modules.count; idx++) {
1277 struct mod *m = depmod->modules.array[idx];
1278 m->idx = idx;
1279 }
1280
1281corrupted:
1282 fclose(fp);
1283}
1284
Lucas De Marchif6b838e2012-01-14 02:03:21 -02001285static int depmod_symbol_add(struct depmod *depmod, const char *name,
Andrey Mazo572a2712012-12-12 16:39:18 +04001286 bool prefix_skipped, uint64_t crc,
1287 const struct mod *owner)
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001288{
1289 size_t namelen;
1290 int err;
1291 struct symbol *sym;
1292
Andrey Mazo572a2712012-12-12 16:39:18 +04001293 if (!prefix_skipped && (name[0] == depmod->cfg->sym_prefix))
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001294 name++;
1295
1296 namelen = strlen(name) + 1;
1297 sym = malloc(sizeof(struct symbol) + namelen);
1298 if (sym == NULL)
1299 return -ENOMEM;
1300
1301 sym->owner = (struct mod *)owner;
1302 sym->crc = crc;
1303 memcpy(sym->name, name, namelen);
1304
1305 err = hash_add(depmod->symbols, sym->name, sym);
1306 if (err < 0) {
1307 free(sym);
1308 return err;
1309 }
1310
1311 DBG("add %p sym=%s, owner=%p %s\n", sym, sym->name, owner,
1312 owner != NULL ? owner->path : "");
1313
1314 return 0;
1315}
1316
Lucas De Marchif6b838e2012-01-14 02:03:21 -02001317static struct symbol *depmod_symbol_find(const struct depmod *depmod,
1318 const char *name)
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001319{
1320 if (name[0] == '.') /* PPC64 needs this: .foo == foo */
1321 name++;
1322 if (name[0] == depmod->cfg->sym_prefix)
1323 name++;
1324 return hash_find(depmod->symbols, name);
1325}
1326
Lucas De Marchiec587f22012-10-30 03:16:10 -02001327static int depmod_load_modules(struct depmod *depmod)
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001328{
Lucas De Marchi7062eca2012-10-30 03:16:10 -02001329 struct mod **itr, **itr_end;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001330
1331 DBG("load symbols (%zd modules)\n", depmod->modules.count);
1332
Lucas De Marchi7062eca2012-10-30 03:16:10 -02001333 itr = (struct mod **)depmod->modules.array;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001334 itr_end = itr + depmod->modules.count;
1335 for (; itr < itr_end; itr++) {
Lucas De Marchi7062eca2012-10-30 03:16:10 -02001336 struct mod *mod = *itr;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001337 struct kmod_list *l, *list = NULL;
1338 int err = kmod_module_get_symbols(mod->kmod, &list);
1339 if (err < 0) {
Dave Reisner819f79a2012-05-07 19:41:42 -04001340 if (err == -ENOENT)
1341 DBG("ignoring %s: no symbols\n", mod->path);
1342 else
1343 ERR("failed to load symbols from %s: %s\n",
1344 mod->path, strerror(-err));
Lucas De Marchi7062eca2012-10-30 03:16:10 -02001345 goto load_info;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001346 }
1347 kmod_list_foreach(l, list) {
1348 const char *name = kmod_module_symbol_get_symbol(l);
1349 uint64_t crc = kmod_module_symbol_get_crc(l);
Andrey Mazo572a2712012-12-12 16:39:18 +04001350 depmod_symbol_add(depmod, name, false, crc, mod);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001351 }
1352 kmod_module_symbols_free_list(list);
Lucas De Marchi7062eca2012-10-30 03:16:10 -02001353
1354load_info:
1355 kmod_module_get_info(mod->kmod, &mod->info_list);
Lucas De Marchiec587f22012-10-30 03:16:10 -02001356 kmod_module_get_dependency_symbols(mod->kmod,
1357 &mod->dep_sym_list);
Lucas De Marchie4a73522012-10-30 03:46:12 -02001358 kmod_module_unref(mod->kmod);
1359 mod->kmod = NULL;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001360 }
1361
Lucas De Marchi1958af82013-04-21 16:16:18 -03001362 DBG("loaded symbols (%zd modules, %u symbols)\n",
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001363 depmod->modules.count, hash_get_count(depmod->symbols));
1364
1365 return 0;
1366}
1367
1368static int depmod_load_module_dependencies(struct depmod *depmod, struct mod *mod)
1369{
1370 const struct cfg *cfg = depmod->cfg;
Lucas De Marchiec587f22012-10-30 03:16:10 -02001371 struct kmod_list *l;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001372
1373 DBG("do dependencies of %s\n", mod->path);
Lucas De Marchiec587f22012-10-30 03:16:10 -02001374 kmod_list_foreach(l, mod->dep_sym_list) {
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001375 const char *name = kmod_module_dependency_symbol_get_symbol(l);
1376 uint64_t crc = kmod_module_dependency_symbol_get_crc(l);
Gustavo Sverzut Barbieri0de40462011-12-23 23:11:41 -02001377 int bindtype = kmod_module_dependency_symbol_get_bind(l);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001378 struct symbol *sym = depmod_symbol_find(depmod, name);
Gustavo Sverzut Barbieri0de40462011-12-23 23:11:41 -02001379 uint8_t is_weak = bindtype == KMOD_SYMBOL_WEAK;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001380
1381 if (sym == NULL) {
1382 DBG("%s needs (%c) unknown symbol %s\n",
Gustavo Sverzut Barbieri0de40462011-12-23 23:11:41 -02001383 mod->path, bindtype, name);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001384 if (cfg->print_unknown && !is_weak)
1385 WRN("%s needs unknown symbol %s\n",
1386 mod->path, name);
1387 continue;
1388 }
1389
1390 if (cfg->check_symvers && sym->crc != crc && !is_weak) {
1391 DBG("symbol %s (%#"PRIx64") module %s (%#"PRIx64")\n",
1392 sym->name, sym->crc, mod->path, crc);
1393 if (cfg->print_unknown)
1394 WRN("%s disagrees about version of symbol %s\n",
1395 mod->path, name);
1396 }
1397
1398 mod_add_dependency(mod, sym);
1399 }
Lucas De Marchiec587f22012-10-30 03:16:10 -02001400
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001401 return 0;
1402}
1403
1404static int depmod_load_dependencies(struct depmod *depmod)
1405{
1406 struct mod **itr, **itr_end;
1407
Lucas De Marchi1958af82013-04-21 16:16:18 -03001408 DBG("load dependencies (%zd modules, %u symbols)\n",
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001409 depmod->modules.count, hash_get_count(depmod->symbols));
1410
1411 itr = (struct mod **)depmod->modules.array;
1412 itr_end = itr + depmod->modules.count;
1413 for (; itr < itr_end; itr++) {
1414 struct mod *mod = *itr;
Lucas De Marchiec587f22012-10-30 03:16:10 -02001415
1416 if (mod->dep_sym_list == NULL) {
1417 DBG("ignoring %s: no dependency symbols\n", mod->path);
1418 continue;
1419 }
1420
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001421 depmod_load_module_dependencies(depmod, mod);
1422 }
1423
Lucas De Marchi1958af82013-04-21 16:16:18 -03001424 DBG("loaded dependencies (%zd modules, %u symbols)\n",
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001425 depmod->modules.count, hash_get_count(depmod->symbols));
1426
1427 return 0;
1428}
1429
1430static int dep_cmp(const void *pa, const void *pb)
1431{
1432 const struct mod *a = *(const struct mod **)pa;
1433 const struct mod *b = *(const struct mod **)pb;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001434 return a->dep_sort_idx - b->dep_sort_idx;
1435}
1436
1437static void depmod_sort_dependencies(struct depmod *depmod)
1438{
1439 struct mod **itr, **itr_end;
1440 itr = (struct mod **)depmod->modules.array;
1441 itr_end = itr + depmod->modules.count;
1442 for (; itr < itr_end; itr++) {
1443 struct mod *m = *itr;
1444 if (m->deps.count > 1)
1445 array_sort(&m->deps, dep_cmp);
1446 }
1447}
1448
Lucas De Marchic89d2192014-05-30 09:38:26 -03001449static void depmod_report_cycles(struct depmod *depmod, uint16_t n_mods,
1450 uint16_t n_roots, uint16_t *users,
1451 uint16_t *stack, uint16_t *edges)
1452{
1453 const char sep[] = " -> ";
1454 int ir = 0;
1455 ERR("Found %u modules in dependency cycles!\n", n_roots);
1456
1457 while (n_roots > 0) {
1458 int is, ie;
1459
1460 for (; ir < n_mods; ir++) {
1461 if (users[ir] > 0) {
1462 break;
1463 }
1464 }
1465
1466 if (ir >= n_mods)
1467 break;
1468
1469 /* New DFS with ir as root, no edges */
1470 stack[0] = ir;
1471 ie = 0;
1472
1473 /* at least one root less */
1474 n_roots--;
1475
1476 /* skip this root on next iteration */
1477 ir++;
1478
1479 for (is = 1; is > 0;) {
1480 uint16_t idx = stack[--is];
1481 struct mod *m = depmod->modules.array[idx];
1482 const struct mod **itr, **itr_end;
1483
1484 DBG("Cycle report: Trying %s visited=%d users=%d\n",
1485 m->modname, m->visited, users[idx]);
1486
1487 if (m->visited) {
1488 int i, n = 0, sz = 0;
1489 char *buf;
1490
1491 for (i = ie - 1; i >= 0; i--) {
1492 struct mod *loop = depmod->modules.array[edges[i]];
1493 sz += loop->modnamesz - 1;
1494 n++;
1495 if (loop == m) {
1496 sz += loop->modnamesz - 1;
1497 break;
1498 }
1499 }
1500
1501 buf = malloc(sz + n * strlen(sep) + 1);
1502 sz = 0;
1503 for (i = ie - n; i < ie; i++) {
1504 struct mod *loop =
1505 depmod->modules.array[edges[i]];
1506 memcpy(buf + sz, loop->modname,
1507 loop->modnamesz - 1);
1508 sz += loop->modnamesz - 1;
1509 memcpy(buf + sz, sep, strlen(sep));
1510 sz += strlen(sep);
1511 }
1512 memcpy(buf + sz, m->modname, m->modnamesz);
1513
1514 ERR("Cycle detected: %s\n", buf);
1515
1516 free(buf);
1517 continue;
1518 }
1519
1520 m->visited = true;
1521
1522 if (m->deps.count == 0) {
1523 continue;
1524 }
1525
1526 edges[ie++] = idx;
1527
1528 itr = (const struct mod **) m->deps.array;
1529 itr_end = itr + m->deps.count;
1530 for (; itr < itr_end; itr++) {
1531 const struct mod *dep = *itr;
1532 stack[is++] = dep->idx;
1533 users[dep->idx]--;
1534 }
1535 }
1536 }
1537}
1538
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001539static int depmod_calculate_dependencies(struct depmod *depmod)
1540{
1541 const struct mod **itrm;
1542 uint16_t *users, *roots, *sorted;
1543 uint16_t i, n_roots = 0, n_sorted = 0, n_mods = depmod->modules.count;
Lucas De Marchic48b2692014-05-09 08:22:02 -03001544 int ret = 0;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001545
1546 users = malloc(sizeof(uint16_t) * n_mods * 3);
1547 if (users == NULL)
1548 return -ENOMEM;
1549 roots = users + n_mods;
1550 sorted = roots + n_mods;
1551
Lucas De Marchi1958af82013-04-21 16:16:18 -03001552 DBG("calculate dependencies and ordering (%hu modules)\n", n_mods);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001553
1554 assert(depmod->modules.count < UINT16_MAX);
1555
1556 /* populate modules users (how many modules uses it) */
1557 itrm = (const struct mod **)depmod->modules.array;
1558 for (i = 0; i < n_mods; i++, itrm++) {
1559 const struct mod *m = *itrm;
1560 users[i] = m->users;
1561 if (users[i] == 0) {
1562 roots[n_roots] = i;
1563 n_roots++;
1564 }
1565 }
1566
1567 /* topological sort (outputs modules without users first) */
1568 while (n_roots > 0) {
1569 const struct mod **itr_dst, **itr_dst_end;
1570 struct mod *src;
1571 uint16_t src_idx = roots[--n_roots];
1572
1573 src = depmod->modules.array[src_idx];
1574 src->dep_sort_idx = n_sorted;
1575 sorted[n_sorted] = src_idx;
1576 n_sorted++;
1577
1578 itr_dst = (const struct mod **)src->deps.array;
1579 itr_dst_end = itr_dst + src->deps.count;
1580 for (; itr_dst < itr_dst_end; itr_dst++) {
1581 const struct mod *dst = *itr_dst;
1582 uint16_t dst_idx = dst->idx;
1583 assert(users[dst_idx] > 0);
1584 users[dst_idx]--;
1585 if (users[dst_idx] == 0) {
1586 roots[n_roots] = dst_idx;
1587 n_roots++;
1588 }
1589 }
1590 }
1591
1592 if (n_sorted < n_mods) {
Lucas De Marchic89d2192014-05-30 09:38:26 -03001593 depmod_report_cycles(depmod, n_mods, n_mods - n_sorted,
1594 users, roots, sorted);
Lucas De Marchic48b2692014-05-09 08:22:02 -03001595 ret = -EINVAL;
Lucas De Marchic48b2692014-05-09 08:22:02 -03001596 goto exit;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001597 }
1598
1599 depmod_sort_dependencies(depmod);
1600
Lucas De Marchic48b2692014-05-09 08:22:02 -03001601 DBG("calculated dependencies and ordering (%hu modules)\n", n_mods);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001602
Lucas De Marchic48b2692014-05-09 08:22:02 -03001603exit:
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001604 free(users);
Lucas De Marchic48b2692014-05-09 08:22:02 -03001605 return ret;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001606}
1607
1608static int depmod_load(struct depmod *depmod)
1609{
1610 int err;
1611
Lucas De Marchiec587f22012-10-30 03:16:10 -02001612 err = depmod_load_modules(depmod);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02001613 if (err < 0)
1614 return err;
1615
1616 err = depmod_load_dependencies(depmod);
1617 if (err < 0)
1618 return err;
1619
1620 err = depmod_calculate_dependencies(depmod);
1621 if (err < 0)
1622 return err;
1623
1624 return 0;
1625}
1626
Gustavo Sverzut Barbieri8e3505c2011-12-23 20:53:16 -02001627static size_t mod_count_all_dependencies(const struct mod *mod)
1628{
1629 size_t i, count = 0;
1630 for (i = 0; i < mod->deps.count; i++) {
1631 const struct mod *d = mod->deps.array[i];
1632 count += 1 + mod_count_all_dependencies(d);
1633 }
1634 return count;
1635}
1636
1637static int mod_fill_all_unique_dependencies(const struct mod *mod, const struct mod **deps, size_t n_deps, size_t *last)
1638{
1639 size_t i;
1640 int err = 0;
1641 for (i = 0; i < mod->deps.count; i++) {
1642 const struct mod *d = mod->deps.array[i];
1643 size_t j;
1644 uint8_t exists = 0;
1645
1646 for (j = 0; j < *last; j++) {
1647 if (deps[j] == d) {
1648 exists = 1;
1649 break;
1650 }
1651 }
1652
1653 if (exists)
1654 continue;
1655
1656 if (*last >= n_deps)
1657 return -ENOSPC;
1658 deps[*last] = d;
1659 (*last)++;
1660 err = mod_fill_all_unique_dependencies(d, deps, n_deps, last);
1661 if (err < 0)
1662 break;
1663 }
1664 return err;
1665}
1666
1667static const struct mod **mod_get_all_sorted_dependencies(const struct mod *mod, size_t *n_deps)
1668{
1669 const struct mod **deps;
1670 size_t last = 0;
1671
1672 *n_deps = mod_count_all_dependencies(mod);
1673 if (*n_deps == 0)
1674 return NULL;
1675
1676 deps = malloc(sizeof(struct mod *) * (*n_deps));
1677 if (deps == NULL)
1678 return NULL;
1679
1680 if (mod_fill_all_unique_dependencies(mod, deps, *n_deps, &last) < 0) {
1681 free(deps);
1682 return NULL;
1683 }
1684
1685 qsort(deps, last, sizeof(struct mod *), dep_cmp);
1686 *n_deps = last;
1687 return deps;
1688}
1689
1690static inline const char *mod_get_compressed_path(const struct mod *mod)
1691{
1692 if (mod->relpath != NULL)
1693 return mod->relpath;
1694 return mod->path;
1695}
1696
1697static int output_deps(struct depmod *depmod, FILE *out)
1698{
1699 size_t i;
1700
1701 for (i = 0; i < depmod->modules.count; i++) {
1702 const struct mod **deps, *mod = depmod->modules.array[i];
1703 const char *p = mod_get_compressed_path(mod);
1704 size_t j, n_deps;
1705
Gustavo Sverzut Barbieri8e3505c2011-12-23 20:53:16 -02001706 fprintf(out, "%s:", p);
1707
1708 if (mod->deps.count == 0)
1709 goto end;
1710
1711 deps = mod_get_all_sorted_dependencies(mod, &n_deps);
1712 if (deps == NULL) {
Dave Reisner63698372012-01-04 10:41:50 -05001713 ERR("could not get all sorted dependencies of %s\n", p);
Gustavo Sverzut Barbieri8e3505c2011-12-23 20:53:16 -02001714 goto end;
1715 }
1716
1717 for (j = 0; j < n_deps; j++) {
1718 const struct mod *d = deps[j];
Gustavo Sverzut Barbieri8e3505c2011-12-23 20:53:16 -02001719 fprintf(out, " %s", mod_get_compressed_path(d));
1720 }
1721 free(deps);
1722 end:
1723 putc('\n', out);
1724 }
1725
1726 return 0;
1727}
1728
Gustavo Sverzut Barbieri74367882011-12-23 23:21:48 -02001729static int output_deps_bin(struct depmod *depmod, FILE *out)
1730{
1731 struct index_node *idx;
1732 size_t i;
1733
1734 if (out == stdout)
1735 return 0;
1736
1737 idx = index_create();
1738 if (idx == NULL)
1739 return -ENOMEM;
1740
1741 for (i = 0; i < depmod->modules.count; i++) {
1742 const struct mod **deps, *mod = depmod->modules.array[i];
1743 const char *p = mod_get_compressed_path(mod);
1744 char *line;
1745 size_t j, n_deps, linepos, linelen, slen;
1746 int duplicate;
1747
Gustavo Sverzut Barbieri74367882011-12-23 23:21:48 -02001748 deps = mod_get_all_sorted_dependencies(mod, &n_deps);
1749 if (deps == NULL && n_deps > 0) {
Dave Reisner63698372012-01-04 10:41:50 -05001750 ERR("could not get all sorted dependencies of %s\n", p);
Gustavo Sverzut Barbieri74367882011-12-23 23:21:48 -02001751 continue;
1752 }
1753
1754 linelen = strlen(p) + 1;
1755 for (j = 0; j < n_deps; j++) {
1756 const struct mod *d = deps[j];
Gustavo Sverzut Barbieri74367882011-12-23 23:21:48 -02001757 linelen += 1 + strlen(mod_get_compressed_path(d));
1758 }
1759
1760 line = malloc(linelen + 1);
1761 if (line == NULL) {
1762 free(deps);
1763 ERR("modules.deps.bin: out of memory\n");
1764 continue;
1765 }
1766
1767 linepos = 0;
1768 slen = strlen(p);
1769 memcpy(line + linepos, p, slen);
1770 linepos += slen;
1771 line[linepos] = ':';
1772 linepos++;
1773
1774 for (j = 0; j < n_deps; j++) {
1775 const struct mod *d = deps[j];
1776 const char *dp;
Lucas De Marchic48b2692014-05-09 08:22:02 -03001777
Gustavo Sverzut Barbieri74367882011-12-23 23:21:48 -02001778 line[linepos] = ' ';
1779 linepos++;
1780
1781 dp = mod_get_compressed_path(d);
1782 slen = strlen(dp);
1783 memcpy(line + linepos, dp, slen);
1784 linepos += slen;
1785 }
1786 line[linepos] = '\0';
1787
1788 duplicate = index_insert(idx, mod->modname, line, mod->idx);
1789 if (duplicate && depmod->cfg->warn_dups)
1790 WRN("duplicate module deps:\n%s\n", line);
1791 free(line);
Lucas De Marchiea1b8c32011-12-27 14:18:32 -02001792 free(deps);
Gustavo Sverzut Barbieri74367882011-12-23 23:21:48 -02001793 }
1794
1795 index_write(idx, out);
1796 index_destroy(idx);
1797
1798 return 0;
1799}
1800
Gustavo Sverzut Barbieri0d131742011-12-23 01:43:01 -02001801static int output_aliases(struct depmod *depmod, FILE *out)
1802{
1803 size_t i;
1804
1805 fputs("# Aliases extracted from modules themselves.\n", out);
1806
1807 for (i = 0; i < depmod->modules.count; i++) {
1808 const struct mod *mod = depmod->modules.array[i];
Lucas De Marchi7062eca2012-10-30 03:16:10 -02001809 struct kmod_list *l;
1810
1811 kmod_list_foreach(l, mod->info_list) {
Gustavo Sverzut Barbieri0d131742011-12-23 01:43:01 -02001812 const char *key = kmod_module_info_get_key(l);
1813 const char *value = kmod_module_info_get_value(l);
1814
1815 if (!streq(key, "alias"))
1816 continue;
1817
Lucas De Marchi447eed82012-10-30 04:08:54 -02001818 fprintf(out, "alias %s %s\n", value, mod->modname);
Gustavo Sverzut Barbieri0d131742011-12-23 01:43:01 -02001819 }
Gustavo Sverzut Barbieri0d131742011-12-23 01:43:01 -02001820 }
1821
1822 return 0;
1823}
1824
Gustavo Sverzut Barbieriec77abb2011-12-23 23:21:09 -02001825static int output_aliases_bin(struct depmod *depmod, FILE *out)
1826{
Gustavo Sverzut Barbieriec77abb2011-12-23 23:21:09 -02001827 struct index_node *idx;
1828 size_t i;
1829
1830 if (out == stdout)
1831 return 0;
1832
1833 idx = index_create();
1834 if (idx == NULL)
1835 return -ENOMEM;
1836
1837 for (i = 0; i < depmod->modules.count; i++) {
1838 const struct mod *mod = depmod->modules.array[i];
Lucas De Marchi7062eca2012-10-30 03:16:10 -02001839 struct kmod_list *l;
1840
1841 kmod_list_foreach(l, mod->info_list) {
Gustavo Sverzut Barbieriec77abb2011-12-23 23:21:09 -02001842 const char *key = kmod_module_info_get_key(l);
1843 const char *value = kmod_module_info_get_value(l);
Lucas De Marchi3753ae12014-10-09 00:54:29 -03001844 char buf[PATH_MAX];
Lucas De Marchi447eed82012-10-30 04:08:54 -02001845 const char *alias;
Gustavo Sverzut Barbieriec77abb2011-12-23 23:21:09 -02001846 int duplicate;
1847
1848 if (!streq(key, "alias"))
1849 continue;
1850
Lucas De Marchi3753ae12014-10-09 00:54:29 -03001851 if (alias_normalize(value, buf, NULL) < 0) {
1852 WRN("Unmatched bracket in %s\n", value);
Gustavo Sverzut Barbieriec77abb2011-12-23 23:21:09 -02001853 continue;
Lucas De Marchi3753ae12014-10-09 00:54:29 -03001854 }
1855 alias = buf;
Gustavo Sverzut Barbieriec77abb2011-12-23 23:21:09 -02001856
Lucas De Marchi447eed82012-10-30 04:08:54 -02001857 duplicate = index_insert(idx, alias, mod->modname,
Gustavo Sverzut Barbieriec77abb2011-12-23 23:21:09 -02001858 mod->idx);
1859 if (duplicate && depmod->cfg->warn_dups)
1860 WRN("duplicate module alias:\n%s %s\n",
Lucas De Marchi447eed82012-10-30 04:08:54 -02001861 alias, mod->modname);
Gustavo Sverzut Barbieriec77abb2011-12-23 23:21:09 -02001862 }
Gustavo Sverzut Barbieriec77abb2011-12-23 23:21:09 -02001863 }
1864
1865 index_write(idx, out);
1866 index_destroy(idx);
1867
1868 return 0;
1869}
1870
Gustavo Sverzut Barbieri8bc830e2011-12-23 01:37:07 -02001871static int output_softdeps(struct depmod *depmod, FILE *out)
1872{
1873 size_t i;
1874
1875 fputs("# Soft dependencies extracted from modules themselves.\n", out);
Gustavo Sverzut Barbieri8bc830e2011-12-23 01:37:07 -02001876
1877 for (i = 0; i < depmod->modules.count; i++) {
1878 const struct mod *mod = depmod->modules.array[i];
Lucas De Marchi7062eca2012-10-30 03:16:10 -02001879 struct kmod_list *l;
1880
1881 kmod_list_foreach(l, mod->info_list) {
Gustavo Sverzut Barbieri8bc830e2011-12-23 01:37:07 -02001882 const char *key = kmod_module_info_get_key(l);
1883 const char *value = kmod_module_info_get_value(l);
1884
1885 if (!streq(key, "softdep"))
1886 continue;
1887
Lucas De Marchi447eed82012-10-30 04:08:54 -02001888 fprintf(out, "softdep %s %s\n", mod->modname, value);
Gustavo Sverzut Barbieri8bc830e2011-12-23 01:37:07 -02001889 }
Gustavo Sverzut Barbieri8bc830e2011-12-23 01:37:07 -02001890 }
1891
1892 return 0;
1893}
1894
Gustavo Sverzut Barbieri9a14d0e2011-12-23 01:27:28 -02001895static int output_symbols(struct depmod *depmod, FILE *out)
1896{
Lucas De Marchi5cd13062011-12-27 13:27:42 -02001897 struct hash_iter iter;
Rolf Eike Beer55021be2012-01-06 13:33:11 +01001898 const void *v;
Gustavo Sverzut Barbieri9a14d0e2011-12-23 01:27:28 -02001899
1900 fputs("# Aliases for symbols, used by symbol_request().\n", out);
1901
Lucas De Marchi5cd13062011-12-27 13:27:42 -02001902 hash_iter_init(depmod->symbols, &iter);
1903
Rolf Eike Beer55021be2012-01-06 13:33:11 +01001904 while (hash_iter_next(&iter, NULL, &v)) {
1905 const struct symbol *sym = v;
Lucas De Marchi5cd13062011-12-27 13:27:42 -02001906 if (sym->owner == NULL)
1907 continue;
1908
1909 fprintf(out, "alias symbol:%s %s\n",
1910 sym->name, sym->owner->modname);
Gustavo Sverzut Barbieri9a14d0e2011-12-23 01:27:28 -02001911 }
1912
1913 return 0;
1914}
1915
Gustavo Sverzut Barbieri75a97232011-12-23 23:20:44 -02001916static int output_symbols_bin(struct depmod *depmod, FILE *out)
1917{
1918 struct index_node *idx;
1919 char alias[1024];
Lucas De Marchi5cd13062011-12-27 13:27:42 -02001920 size_t baselen = sizeof("symbol:") - 1;
1921 struct hash_iter iter;
Rolf Eike Beer55021be2012-01-06 13:33:11 +01001922 const void *v;
Gustavo Sverzut Barbieri75a97232011-12-23 23:20:44 -02001923
1924 if (out == stdout)
1925 return 0;
1926
1927 idx = index_create();
1928 if (idx == NULL)
1929 return -ENOMEM;
1930
1931 memcpy(alias, "symbol:", baselen);
Lucas De Marchi5cd13062011-12-27 13:27:42 -02001932 hash_iter_init(depmod->symbols, &iter);
Gustavo Sverzut Barbieri75a97232011-12-23 23:20:44 -02001933
Rolf Eike Beer55021be2012-01-06 13:33:11 +01001934 while (hash_iter_next(&iter, NULL, &v)) {
Lucas De Marchi5cd13062011-12-27 13:27:42 -02001935 int duplicate;
Rolf Eike Beer55021be2012-01-06 13:33:11 +01001936 const struct symbol *sym = v;
Gustavo Sverzut Barbieri75a97232011-12-23 23:20:44 -02001937
Lucas De Marchi5cd13062011-12-27 13:27:42 -02001938 if (sym->owner == NULL)
1939 continue;
Gustavo Sverzut Barbieri75a97232011-12-23 23:20:44 -02001940
Lucas De Marchi5cd13062011-12-27 13:27:42 -02001941 strcpy(alias + baselen, sym->name);
1942 duplicate = index_insert(idx, alias, sym->owner->modname,
1943 sym->owner->idx);
1944
1945 if (duplicate && depmod->cfg->warn_dups)
1946 WRN("duplicate module syms:\n%s %s\n",
1947 alias, sym->owner->modname);
Gustavo Sverzut Barbieri75a97232011-12-23 23:20:44 -02001948 }
1949
1950 index_write(idx, out);
1951 index_destroy(idx);
Lucas De Marchi5cd13062011-12-27 13:27:42 -02001952
Gustavo Sverzut Barbieri75a97232011-12-23 23:20:44 -02001953 return 0;
1954}
1955
Gustavo Sverzut Barbieri4b144e52011-12-23 23:19:22 -02001956static int output_builtin_bin(struct depmod *depmod, FILE *out)
1957{
1958 FILE *in;
1959 struct index_node *idx;
Lucas De Marchi6daceb22012-01-08 01:02:29 -02001960 char infile[PATH_MAX], line[PATH_MAX], modname[PATH_MAX];
Gustavo Sverzut Barbieri4b144e52011-12-23 23:19:22 -02001961
1962 if (out == stdout)
1963 return 0;
1964
1965 snprintf(infile, sizeof(infile), "%s/modules.builtin",
Lucas De Marchic5db1a32012-06-15 02:39:34 -03001966 depmod->cfg->dirname);
Gustavo Sverzut Barbieri4b144e52011-12-23 23:19:22 -02001967 in = fopen(infile, "r");
1968 if (in == NULL) {
Lucas De Marchi56406fd2012-01-04 20:53:10 -02001969 WRN("could not open %s: %m\n", infile);
Lucas De Marchic5db1a32012-06-15 02:39:34 -03001970 return 0;
Gustavo Sverzut Barbieri4b144e52011-12-23 23:19:22 -02001971 }
1972
1973 idx = index_create();
1974 if (idx == NULL) {
1975 fclose(in);
1976 return -ENOMEM;
1977 }
1978
1979 while (fgets(line, sizeof(line), in) != NULL) {
1980 if (!isalpha(line[0])) {
1981 ERR("Invalid modules.builtin line: %s\n", line);
1982 continue;
1983 }
1984
1985 path_to_modname(line, modname, NULL);
1986 index_insert(idx, modname, "", 0);
1987 }
1988
1989 index_write(idx, out);
1990 index_destroy(idx);
1991 fclose(in);
1992
1993 return 0;
1994}
1995
Gustavo Sverzut Barbieri25c41512011-12-23 00:55:33 -02001996static int output_devname(struct depmod *depmod, FILE *out)
1997{
1998 size_t i;
1999
2000 fputs("# Device nodes to trigger on-demand module loading.\n", out);
2001
2002 for (i = 0; i < depmod->modules.count; i++) {
2003 const struct mod *mod = depmod->modules.array[i];
Lucas De Marchi7062eca2012-10-30 03:16:10 -02002004 struct kmod_list *l;
Gustavo Sverzut Barbieri25c41512011-12-23 00:55:33 -02002005 const char *devname = NULL;
2006 char type = '\0';
2007 unsigned int major = 0, minor = 0;
Gustavo Sverzut Barbieri25c41512011-12-23 00:55:33 -02002008
Lucas De Marchi7062eca2012-10-30 03:16:10 -02002009 kmod_list_foreach(l, mod->info_list) {
Gustavo Sverzut Barbieri25c41512011-12-23 00:55:33 -02002010 const char *key = kmod_module_info_get_key(l);
2011 const char *value = kmod_module_info_get_value(l);
2012 unsigned int maj, min;
2013
2014 if (!streq(key, "alias"))
2015 continue;
2016
2017 if (strstartswith(value, "devname:"))
2018 devname = value + sizeof("devname:") - 1;
2019 else if (sscanf(value, "char-major-%u-%u",
2020 &maj, &min) == 2) {
2021 type = 'c';
2022 major = maj;
2023 minor = min;
2024 } else if (sscanf(value, "block-major-%u-%u",
2025 &maj, &min) == 2) {
2026 type = 'b';
2027 major = maj;
2028 minor = min;
2029 }
2030
Tom Gundersen6506ddf2013-09-09 20:01:01 +02002031 if (type != '\0' && devname != NULL)
2032 break;
2033 }
2034
2035 if (devname != NULL) {
2036 if (type != '\0')
Lucas De Marchi447eed82012-10-30 04:08:54 -02002037 fprintf(out, "%s %s %c%u:%u\n", mod->modname,
Gustavo Sverzut Barbieri25c41512011-12-23 00:55:33 -02002038 devname, type, major, minor);
Tom Gundersen6506ddf2013-09-09 20:01:01 +02002039 else
2040 ERR("Module '%s' has devname (%s) but "
2041 "lacks major and minor information. "
2042 "Ignoring.\n", mod->modname, devname);
Gustavo Sverzut Barbieri25c41512011-12-23 00:55:33 -02002043 }
Gustavo Sverzut Barbieri25c41512011-12-23 00:55:33 -02002044 }
2045
2046 return 0;
2047}
2048
2049static int depmod_output(struct depmod *depmod, FILE *out)
2050{
2051 static const struct depfile {
2052 const char *name;
2053 int (*cb)(struct depmod *depmod, FILE *out);
2054 } *itr, depfiles[] = {
Lucas De Marchi015946d2012-06-15 01:10:14 -03002055 { "modules.dep", output_deps },
2056 { "modules.dep.bin", output_deps_bin },
2057 { "modules.alias", output_aliases },
2058 { "modules.alias.bin", output_aliases_bin },
2059 { "modules.softdep", output_softdeps },
2060 { "modules.symbols", output_symbols },
2061 { "modules.symbols.bin", output_symbols_bin },
2062 { "modules.builtin.bin", output_builtin_bin },
2063 { "modules.devname", output_devname },
2064 { }
Gustavo Sverzut Barbieri25c41512011-12-23 00:55:33 -02002065 };
2066 const char *dname = depmod->cfg->dirname;
2067 int dfd, err = 0;
2068
2069 if (out != NULL)
2070 dfd = -1;
2071 else {
2072 dfd = open(dname, O_RDONLY);
2073 if (dfd < 0) {
2074 err = -errno;
Dave Reisner63698372012-01-04 10:41:50 -05002075 CRIT("could not open directory %s: %m\n", dname);
Gustavo Sverzut Barbieri25c41512011-12-23 00:55:33 -02002076 return err;
2077 }
2078 }
2079
2080 for (itr = depfiles; itr->name != NULL; itr++) {
2081 FILE *fp = out;
2082 char tmp[NAME_MAX] = "";
Lucas De Marchi3f376cd2012-06-15 22:14:00 -03002083 int r, ferr;
Gustavo Sverzut Barbieri25c41512011-12-23 00:55:33 -02002084
2085 if (fp == NULL) {
2086 int flags = O_CREAT | O_TRUNC | O_WRONLY;
2087 int mode = 0644;
2088 int fd;
2089
2090 snprintf(tmp, sizeof(tmp), "%s.tmp", itr->name);
2091 fd = openat(dfd, tmp, flags, mode);
2092 if (fd < 0) {
2093 ERR("openat(%s, %s, %o, %o): %m\n",
2094 dname, tmp, flags, mode);
2095 continue;
2096 }
2097 fp = fdopen(fd, "wb");
2098 if (fp == NULL) {
2099 ERR("fdopen(%d=%s/%s): %m\n", fd, dname, tmp);
2100 close(fd);
2101 continue;
2102 }
2103 }
2104
2105 r = itr->cb(depmod, fp);
2106 if (fp == out)
2107 continue;
2108
Lucas De Marchi3f376cd2012-06-15 22:14:00 -03002109 ferr = ferror(fp) | fclose(fp);
2110
Gustavo Sverzut Barbieri25c41512011-12-23 00:55:33 -02002111 if (r < 0) {
2112 if (unlinkat(dfd, tmp, 0) != 0)
2113 ERR("unlinkat(%s, %s): %m\n", dname, tmp);
Lucas De Marchi80e49ad2012-06-15 01:08:17 -03002114
2115 ERR("Could not write index '%s': %s\n", itr->name,
2116 strerror(-r));
2117 err = -errno;
2118 break;
2119 }
2120
2121 unlinkat(dfd, itr->name, 0);
2122 if (renameat(dfd, tmp, dfd, itr->name) != 0) {
2123 err = -errno;
2124 CRIT("renameat(%s, %s, %s, %s): %m\n",
2125 dname, tmp, dname, itr->name);
2126 break;
Gustavo Sverzut Barbieri25c41512011-12-23 00:55:33 -02002127 }
Lucas De Marchia4fb97a2012-06-15 20:13:31 -03002128
Lucas De Marchi3f376cd2012-06-15 22:14:00 -03002129 if (ferr) {
Lucas De Marchia4fb97a2012-06-15 20:13:31 -03002130 err = -ENOSPC;
Lucas De Marchi3f376cd2012-06-15 22:14:00 -03002131 ERR("Could not create index '%s'. Output is truncated: %s\n",
2132 itr->name, strerror(-err));
Lucas De Marchia4fb97a2012-06-15 20:13:31 -03002133 break;
2134 }
Gustavo Sverzut Barbieri25c41512011-12-23 00:55:33 -02002135 }
2136
2137 if (dfd >= 0)
2138 close(dfd);
Lucas De Marchi80e49ad2012-06-15 01:08:17 -03002139
Gustavo Sverzut Barbieri25c41512011-12-23 00:55:33 -02002140 return err;
2141}
2142
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002143static void depmod_add_fake_syms(struct depmod *depmod)
2144{
2145 /* __this_module is magic inserted by kernel loader. */
Andrey Mazo572a2712012-12-12 16:39:18 +04002146 depmod_symbol_add(depmod, "__this_module", true, 0, NULL);
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002147 /* On S390, this is faked up too */
Andrey Mazo572a2712012-12-12 16:39:18 +04002148 depmod_symbol_add(depmod, "_GLOBAL_OFFSET_TABLE_", true, 0, NULL);
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002149}
2150
2151static int depmod_load_symvers(struct depmod *depmod, const char *filename)
2152{
2153 char line[10240];
2154 FILE *fp;
2155 unsigned int linenum = 0;
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002156
2157 fp = fopen(filename, "r");
Khem Raj035cbdc2012-02-02 23:09:59 -08002158 if (fp == NULL) {
2159 int err = -errno;
2160 DBG("load symvers: %s: %m\n", filename);
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002161 return err;
Khem Raj035cbdc2012-02-02 23:09:59 -08002162 }
2163 DBG("load symvers: %s\n", filename);
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002164
2165 /* eg. "0xb352177e\tfind_first_bit\tvmlinux\tEXPORT_SYMBOL" */
2166 while (fgets(line, sizeof(line), fp) != NULL) {
2167 const char *ver, *sym, *where;
2168 char *verend;
2169 uint64_t crc;
2170
2171 linenum++;
2172
2173 ver = strtok(line, " \t");
2174 sym = strtok(NULL, " \t");
2175 where = strtok(NULL, " \t");
2176 if (!ver || !sym || !where)
2177 continue;
2178
2179 if (!streq(where, "vmlinux"))
2180 continue;
2181
2182 crc = strtoull(ver, &verend, 16);
2183 if (verend[0] != '\0') {
2184 ERR("%s:%u Invalid symbol version %s: %m\n",
2185 filename, linenum, ver);
2186 continue;
2187 }
2188
Andrey Mazo572a2712012-12-12 16:39:18 +04002189 depmod_symbol_add(depmod, sym, false, crc, NULL);
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002190 }
2191 depmod_add_fake_syms(depmod);
2192
Khem Raj035cbdc2012-02-02 23:09:59 -08002193 DBG("loaded symvers: %s\n", filename);
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002194
2195 fclose(fp);
Khem Raj035cbdc2012-02-02 23:09:59 -08002196 return 0;
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002197}
2198
2199static int depmod_load_system_map(struct depmod *depmod, const char *filename)
2200{
2201 const char ksymstr[] = "__ksymtab_";
2202 const size_t ksymstr_len = sizeof(ksymstr) - 1;
2203 char line[10240];
2204 FILE *fp;
2205 unsigned int linenum = 0;
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002206
2207 fp = fopen(filename, "r");
Khem Raj035cbdc2012-02-02 23:09:59 -08002208 if (fp == NULL) {
2209 int err = -errno;
2210 DBG("load System.map: %s: %m\n", filename);
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002211 return err;
Khem Raj035cbdc2012-02-02 23:09:59 -08002212 }
2213 DBG("load System.map: %s\n", filename);
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002214
2215 /* eg. c0294200 R __ksymtab_devfs_alloc_devnum */
2216 while (fgets(line, sizeof(line), fp) != NULL) {
2217 char *p, *end;
2218
2219 linenum++;
2220
2221 p = strchr(line, ' ');
2222 if (p == NULL)
2223 goto invalid_syntax;
2224 p++;
2225 p = strchr(p, ' ');
2226 if (p == NULL)
2227 goto invalid_syntax;
2228 p++;
2229
Andrey Mazo572a2712012-12-12 16:39:18 +04002230 /* skip prefix */
2231 if (p[0] == depmod->cfg->sym_prefix)
2232 p++;
2233
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002234 /* Covers gpl-only and normal symbols. */
2235 if (strncmp(p, ksymstr, ksymstr_len) != 0)
2236 continue;
2237
2238 end = strchr(p, '\n');
2239 if (end != NULL)
2240 *end = '\0';
2241
Andrey Mazo572a2712012-12-12 16:39:18 +04002242 depmod_symbol_add(depmod, p + ksymstr_len, true, 0, NULL);
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002243 continue;
2244
2245 invalid_syntax:
2246 ERR("%s:%u: invalid line: %s\n", filename, linenum, line);
2247 }
2248 depmod_add_fake_syms(depmod);
2249
Khem Raj035cbdc2012-02-02 23:09:59 -08002250 DBG("loaded System.map: %s\n", filename);
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002251
2252 fclose(fp);
Khem Raj035cbdc2012-02-02 23:09:59 -08002253 return 0;
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002254}
Gustavo Sverzut Barbieri25c41512011-12-23 00:55:33 -02002255
Gustavo Sverzut Barbieri18cd9da2011-12-24 11:16:40 -02002256
2257static int depfile_up_to_date_dir(DIR *d, time_t mtime, size_t baselen, char *path)
2258{
2259 struct dirent *de;
2260 int err = 1, dfd = dirfd(d);
2261
2262 while ((de = readdir(d)) != NULL) {
2263 const char *name = de->d_name;
2264 size_t namelen;
2265 struct stat st;
2266
2267 if (name[0] == '.' && (name[1] == '\0' ||
2268 (name[1] == '.' && name[2] == '\0')))
2269 continue;
2270 if (streq(name, "build") || streq(name, "source"))
2271 continue;
2272 namelen = strlen(name);
2273 if (baselen + namelen + 2 >= PATH_MAX) {
2274 path[baselen] = '\0';
Lucas De Marchi1958af82013-04-21 16:16:18 -03002275 ERR("path is too long %s%s\n", path, name);
Gustavo Sverzut Barbieri18cd9da2011-12-24 11:16:40 -02002276 continue;
2277 }
2278
2279 if (fstatat(dfd, name, &st, 0) < 0) {
2280 ERR("fstatat(%d, %s): %m\n", dfd, name);
2281 continue;
2282 }
2283
2284 if (S_ISDIR(st.st_mode)) {
2285 int fd;
2286 DIR *subdir;
2287 memcpy(path + baselen, name, namelen + 1);
2288 if (baselen + namelen + 2 + NAME_MAX >= PATH_MAX) {
2289 ERR("directory path is too long %s\n", path);
2290 continue;
2291 }
2292 fd = openat(dfd, name, O_RDONLY);
2293 if (fd < 0) {
2294 ERR("openat(%d, %s, O_RDONLY): %m\n",
2295 dfd, name);
2296 continue;
2297 }
2298 subdir = fdopendir(fd);
2299 if (subdir == NULL) {
2300 ERR("fdopendir(%d): %m\n", fd);
2301 close(fd);
2302 continue;
2303 }
2304 path[baselen + namelen] = '/';
2305 path[baselen + namelen + 1] = '\0';
2306 err = depfile_up_to_date_dir(subdir, mtime,
2307 baselen + namelen + 1,
2308 path);
2309 closedir(subdir);
2310 } else if (S_ISREG(st.st_mode)) {
Lucas De Marchi650f89c2012-11-28 14:26:23 -02002311 if (!path_ends_with_kmod_ext(name, namelen))
Gustavo Sverzut Barbieri18cd9da2011-12-24 11:16:40 -02002312 continue;
Lucas De Marchi650f89c2012-11-28 14:26:23 -02002313
Gustavo Sverzut Barbieri18cd9da2011-12-24 11:16:40 -02002314 memcpy(path + baselen, name, namelen + 1);
2315 err = st.st_mtime <= mtime;
2316 if (err == 0) {
2317 DBG("%s %"PRIu64" is newer than %"PRIu64"\n",
2318 path, (uint64_t)st.st_mtime,
2319 (uint64_t)mtime);
2320 }
2321 } else {
2322 ERR("unsupported file type %s: %o\n",
2323 path, st.st_mode & S_IFMT);
2324 continue;
2325 }
2326
2327 if (err == 0)
2328 break; /* outdated! */
2329 else if (err < 0) {
2330 path[baselen + namelen] = '\0';
2331 ERR("failed %s: %s\n", path, strerror(-err));
2332 err = 1; /* ignore errors */
2333 }
2334 }
2335
2336 return err;
2337}
2338
2339/* uptodate: 1, outdated: 0, errors < 0 */
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002340static int depfile_up_to_date(const char *dirname)
2341{
Gustavo Sverzut Barbieri18cd9da2011-12-24 11:16:40 -02002342 char path[PATH_MAX];
2343 DIR *d = opendir(dirname);
2344 struct stat st;
2345 size_t baselen;
2346 int err;
2347 if (d == NULL) {
2348 err = -errno;
Dave Reisner63698372012-01-04 10:41:50 -05002349 ERR("could not open directory %s: %m\n", dirname);
Gustavo Sverzut Barbieri18cd9da2011-12-24 11:16:40 -02002350 return err;
2351 }
2352
2353 if (fstatat(dirfd(d), "modules.dep", &st, 0) != 0) {
2354 err = -errno;
Dave Reisner63698372012-01-04 10:41:50 -05002355 ERR("could not fstatat(%s, modules.dep): %m\n", dirname);
Gustavo Sverzut Barbieri18cd9da2011-12-24 11:16:40 -02002356 closedir(d);
2357 return err;
2358 }
2359
2360 baselen = strlen(dirname);
2361 memcpy(path, dirname, baselen);
2362 path[baselen] = '/';
2363 baselen++;
2364 path[baselen] = '\0';
2365
2366 err = depfile_up_to_date_dir(d, st.st_mtime, baselen, path);
2367 closedir(d);
2368 return err;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002369}
2370
2371static int is_version_number(const char *version)
2372{
2373 unsigned int d1, d2;
2374 return (sscanf(version, "%u.%u", &d1, &d2) == 2);
2375}
2376
Lucas De Marchif6cf14c2011-12-27 19:56:33 -02002377static int do_depmod(int argc, char *argv[])
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002378{
2379 FILE *out = NULL;
Lucas De Marchi31f1d0d2012-01-14 02:04:31 -02002380 int err = 0, all = 0, maybe_all = 0, n_config_paths = 0;
Lucas De Marchie15a56a2012-01-14 02:46:58 -02002381 char *root = NULL;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002382 const char **config_paths = NULL;
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002383 const char *system_map = NULL;
2384 const char *module_symvers = NULL;
2385 const char *null_kmod_config = NULL;
2386 struct utsname un;
2387 struct kmod_ctx *ctx = NULL;
2388 struct cfg cfg;
2389 struct depmod depmod;
2390
2391 memset(&cfg, 0, sizeof(cfg));
2392 memset(&depmod, 0, sizeof(depmod));
2393
2394 for (;;) {
2395 int c, idx = 0;
2396 c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
2397 if (c == -1)
2398 break;
2399 switch (c) {
2400 case 'a':
2401 all = 1;
2402 break;
2403 case 'A':
2404 maybe_all = 1;
2405 break;
2406 case 'b':
Lucas De Marchie15a56a2012-01-14 02:46:58 -02002407 root = path_make_absolute_cwd(optarg);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002408 break;
2409 case 'C': {
2410 size_t bytes = sizeof(char *) * (n_config_paths + 2);
2411 void *tmp = realloc(config_paths, bytes);
2412 if (!tmp) {
2413 fputs("Error: out-of-memory\n", stderr);
2414 goto cmdline_failed;
2415 }
2416 config_paths = tmp;
2417 config_paths[n_config_paths] = optarg;
2418 n_config_paths++;
2419 config_paths[n_config_paths] = NULL;
2420 break;
2421 }
2422 case 'E':
2423 module_symvers = optarg;
2424 cfg.check_symvers = 1;
2425 break;
2426 case 'F':
2427 system_map = optarg;
2428 break;
2429 case 'e':
2430 cfg.print_unknown = 1;
2431 break;
2432 case 'v':
2433 verbose++;
2434 break;
2435 case 'n':
2436 out = stdout;
2437 break;
2438 case 'P':
2439 if (optarg[1] != '\0') {
2440 CRIT("-P only takes a single char\n");
2441 goto cmdline_failed;
2442 }
2443 cfg.sym_prefix = optarg[0];
2444 break;
2445 case 'w':
2446 cfg.warn_dups = 1;
2447 break;
2448 case 'u':
2449 case 'q':
2450 case 'r':
2451 case 'm':
Lucas De Marchi61c48db2012-11-05 02:01:06 -02002452 if (idx > 0)
2453 WRN("Ignored deprecated option --%s\n",
2454 cmdopts[idx].name);
2455 else
2456 WRN("Ignored deprecated option -%c\n", c);
2457
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002458 break;
2459 case 'h':
Lucas De Marchi4a2e20d2012-11-06 16:54:17 -02002460 help();
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002461 free(config_paths);
2462 return EXIT_SUCCESS;
2463 case 'V':
2464 puts(PACKAGE " version " VERSION);
2465 free(config_paths);
2466 return EXIT_SUCCESS;
2467 case '?':
2468 goto cmdline_failed;
2469 default:
Lucas De Marchi61c48db2012-11-05 02:01:06 -02002470 ERR("unexpected getopt_long() value '%c'.\n", c);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002471 goto cmdline_failed;
2472 }
2473 }
2474
2475 if (optind < argc && is_version_number(argv[optind])) {
2476 cfg.kversion = argv[optind];
2477 optind++;
2478 } else {
2479 if (uname(&un) < 0) {
2480 CRIT("uname() failed: %s\n", strerror(errno));
2481 goto cmdline_failed;
2482 }
2483 cfg.kversion = un.release;
2484 }
2485
2486 cfg.dirnamelen = snprintf(cfg.dirname, PATH_MAX,
Dave Reisnerc5b37db2012-09-27 11:00:42 -04002487 "%s/lib/modules/%s",
Lucas De Marchie15a56a2012-01-14 02:46:58 -02002488 root == NULL ? "" : root, cfg.kversion);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002489
2490 if (optind == argc)
2491 all = 1;
2492
2493 if (maybe_all) {
2494 if (out == stdout)
2495 goto done;
Gustavo Sverzut Barbieri18cd9da2011-12-24 11:16:40 -02002496 /* ignore up-to-date errors (< 0) */
2497 if (depfile_up_to_date(cfg.dirname) == 1) {
2498 DBG("%s/modules.dep is up to date!\n", cfg.dirname);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002499 goto done;
Gustavo Sverzut Barbieri18cd9da2011-12-24 11:16:40 -02002500 }
2501 DBG("%s/modules.dep is outdated, do -a\n", cfg.dirname);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002502 all = 1;
2503 }
2504
2505 ctx = kmod_new(cfg.dirname, &null_kmod_config);
2506 if (ctx == NULL) {
2507 CRIT("kmod_new(\"%s\", {NULL}) failed: %m\n", cfg.dirname);
2508 goto cmdline_failed;
2509 }
Lucas De Marchi52a50fe2012-11-06 18:26:34 -02002510
2511 log_setup_kmod_log(ctx, verbose);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002512
2513 err = depmod_init(&depmod, &cfg, ctx);
2514 if (err < 0) {
2515 CRIT("depmod_init: %s\n", strerror(-err));
2516 goto depmod_init_failed;
2517 }
2518 ctx = NULL; /* owned by depmod */
2519
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002520 if (module_symvers != NULL) {
2521 err = depmod_load_symvers(&depmod, module_symvers);
2522 if (err < 0) {
Dave Reisner63698372012-01-04 10:41:50 -05002523 CRIT("could not load %s: %s\n", module_symvers,
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002524 strerror(-err));
2525 goto cmdline_failed;
2526 }
2527 } else if (system_map != NULL) {
2528 err = depmod_load_system_map(&depmod, system_map);
2529 if (err < 0) {
Khem Raj035cbdc2012-02-02 23:09:59 -08002530 CRIT("could not load %s: %s\n", system_map,
Gustavo Sverzut Barbieri4a0e46d2011-12-24 10:31:55 -02002531 strerror(-err));
2532 goto cmdline_failed;
2533 }
2534 } else if (cfg.print_unknown) {
2535 WRN("-e needs -E or -F\n");
2536 cfg.print_unknown = 0;
2537 }
2538
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002539 if (all) {
2540 err = cfg_load(&cfg, config_paths);
2541 if (err < 0) {
Dave Reisner63698372012-01-04 10:41:50 -05002542 CRIT("could not load configuration files\n");
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002543 goto cmdline_modules_failed;
2544 }
2545 err = depmod_modules_search(&depmod);
2546 if (err < 0) {
Dave Reisner63698372012-01-04 10:41:50 -05002547 CRIT("could not search modules: %s\n", strerror(-err));
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002548 goto cmdline_modules_failed;
2549 }
2550 } else {
Lucas De Marchi31f1d0d2012-01-14 02:04:31 -02002551 int i;
2552
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002553 for (i = optind; i < argc; i++) {
2554 const char *path = argv[i];
2555 struct kmod_module *mod;
2556
2557 if (path[0] != '/') {
Gustavo Sverzut Barbierib0bcadd2012-01-03 15:04:34 -02002558 CRIT("%s: not absolute path.\n", path);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002559 goto cmdline_modules_failed;
2560 }
2561
2562 err = kmod_module_new_from_path(depmod.ctx, path, &mod);
2563 if (err < 0) {
Dave Reisner63698372012-01-04 10:41:50 -05002564 CRIT("could not create module %s: %s\n",
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002565 path, strerror(-err));
2566 goto cmdline_modules_failed;
2567 }
2568
2569 err = depmod_module_add(&depmod, mod);
2570 if (err < 0) {
Dave Reisner63698372012-01-04 10:41:50 -05002571 CRIT("could not add module %s: %s\n",
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002572 path, strerror(-err));
2573 kmod_module_unref(mod);
2574 goto cmdline_modules_failed;
2575 }
2576 }
2577 }
2578
Jan Alexander Steffens (heftig)00bd3192012-01-11 23:17:38 +01002579 err = depmod_modules_build_array(&depmod);
2580 if (err < 0) {
2581 CRIT("could not build module array: %s\n",
2582 strerror(-err));
2583 goto cmdline_modules_failed;
2584 }
2585
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002586 depmod_modules_sort(&depmod);
2587 err = depmod_load(&depmod);
2588 if (err < 0)
2589 goto cmdline_modules_failed;
2590
Gustavo Sverzut Barbieri25c41512011-12-23 00:55:33 -02002591 err = depmod_output(&depmod, out);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002592
2593done:
2594 depmod_shutdown(&depmod);
2595 cfg_free(&cfg);
2596 free(config_paths);
2597 return err >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
2598
2599cmdline_modules_failed:
2600 depmod_shutdown(&depmod);
2601depmod_init_failed:
2602 if (ctx != NULL)
2603 kmod_unref(ctx);
2604cmdline_failed:
2605 cfg_free(&cfg);
2606 free(config_paths);
Lucas De Marchie15a56a2012-01-14 02:46:58 -02002607 free(root);
Gustavo Sverzut Barbieri64b8b582011-12-22 20:23:11 -02002608 return EXIT_FAILURE;
2609}
Lucas De Marchif6cf14c2011-12-27 19:56:33 -02002610
Lucas De Marchif6cf14c2011-12-27 19:56:33 -02002611const struct kmod_cmd kmod_cmd_compat_depmod = {
2612 .name = "depmod",
2613 .cmd = do_depmod,
2614 .help = "compat depmod command",
2615};