-
Notifications
You must be signed in to change notification settings - Fork 11
/
depmod.c
1922 lines (1682 loc) · 46.3 KB
/
depmod.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* depmod.c: generate module dependency meta-data (aliases, etc.)
*
* (C) 2002 Rusty Russell IBM Corporation
* (C) 2006-2011 Jon Masters <[email protected]>, and others.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#define _GNU_SOURCE /* asprintf */
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <elf.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/utsname.h>
#include <sys/mman.h>
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#include "util.h"
#include "zlibsupport.h"
#include "depmod.h"
#include "logging.h"
#include "index.h"
#include "elfops.h"
#include "tables.h"
#include "config_filter.h"
#include "testing.h"
#ifndef MODULE_DIR
#define MODULE_DIR "/lib/modules/"
#endif
#ifndef MODULE_BUILTIN_KEY
#define MODULE_BUILTIN_KEY "built-in"
#endif
/* used to replace one module with another based on conf override entries */
struct module_overrides
{
/* Next override */
struct module_overrides *next;
/* overridden module */
char *modfile;
};
/* used to specify the order in which /lib/modules subdirs are searched */
struct module_search
{
/* Next search */
struct module_search *next;
/* search path */
char *search_path;
size_t len;
};
static char sym_prefix; /* used for -P option */
static unsigned int skipchars; /* prefix target part of basedir to skip over */
static unsigned int make_map_files = 1; /* default to on */
static unsigned int force_map_files = 0; /* default to on */
#define SYMBOL_HASH_SIZE 1024
struct symbol
{
struct symbol *next;
struct module *owner;
uint64_t ver;
char name[0];
};
static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
/**
* tdb_hash - calculate hash entry for a symbol (algorithm from gdbm, via tdb)
*
* @name: symbol name
*
*/
static inline unsigned int tdb_hash(const char *name)
{
unsigned value; /* Used to compute the hash value. */
unsigned i; /* Used to cycle through random values. */
/* Set the initial value from the key size. */
for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
return (1103515243 * value + 12345);
}
/**
* skip_symprefix - remove extraneous prefix character on some architectures
*
* @symname: symbol name to process for prefix character removal
*
*/
static const char *skip_symprefix(const char *symname)
{
return symname + (symname[0] == sym_prefix ? 1 : 0);
}
/**
* add_symbol - add a symbol to the symbol hash list
*
* @name: symbol name
* @ver: symbol version (checksum)
* @owner: module owning this symbol
*
*/
static void add_symbol(const char *name, uint64_t ver, struct module *owner)
{
unsigned int hash;
struct symbol *new = NOFAIL(malloc(sizeof *new + strlen(name) + 1));
new->owner = owner;
new->ver = ver;
strcpy(new->name, name);
hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
new->next = symbolhash[hash];
symbolhash[hash] = new;
}
static int print_unknown, check_symvers;
/**
* find_symbol - lookup module owning a symbol in the symbol hash list
*
* @name: symbol name
* @ver: symbol version
* @modname: pathname of module requesting lookup (being processed)
* @weak: whether the symbol is weakly defined or not
*
* This function is used during dependency calculation to match the
* dependencies a module says it requires with symbols we have seen.
* calculate_deps calls us after it has load_dep_syms on a module.
*
*/
static struct module *find_symbol(const char *name, uint64_t ver,
const char *modname, int weak)
{
struct symbol *s;
/* For our purposes, .foo matches foo. PPC64 needs this. */
if (name[0] == '.')
name++;
name = skip_symprefix(name);
for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) {
if (streq(s->name, name))
break;
}
if (s) {
if (ver && s->ver && s->ver != ver && print_unknown && !weak)
warn("%s disagrees about version of symbol %s\n",
modname, name);
return s->owner;
}
if (print_unknown && !weak)
warn("%s needs unknown symbol %s\n", modname, name);
return NULL;
}
/**
* add_dep - add a module dependency
*
* @mod: module name
* @depends_on: new module dependency
*
*/
static void add_dep(struct module *mod, struct module *depends_on)
{
unsigned int i;
for (i = 0; i < mod->num_deps; i++)
if (mod->deps[i] == depends_on)
return;
mod->deps = NOFAIL(realloc(mod->deps, sizeof(mod->deps[0])*(mod->num_deps+1)));
mod->deps[mod->num_deps++] = depends_on;
}
/**
* add_fake_syms - symbols not explicitly otherwise provided
*
* The kernel provides a few dependencies within the module loader.
*
*/
static void add_fake_syms(void)
{
/* __this_module is magic inserted by kernel loader. */
add_symbol("__this_module", 0, NULL);
/* On S390, this is faked up too */
add_symbol("_GLOBAL_OFFSET_TABLE_", 0, NULL);
}
/**
* load_system_map - load list of symbols from the System.map
*
* @filename: path to the System.map-like file
*
*/
static void load_system_map(const char *filename)
{
FILE *system_map;
char line[10240];
const char ksymstr[] = "__ksymtab_";
const int ksymstr_len = strlen(ksymstr);
system_map = fopen(filename, "r");
if (!system_map)
fatal("Could not open '%s': %s\n", filename, strerror(errno));
/* eg. c0294200 R __ksymtab_devfs_alloc_devnum */
while (fgets(line, sizeof(line)-1, system_map)) {
char *ptr;
const char *cptr;
/* Snip \n */
ptr = strchr(line, '\n');
if (ptr)
*ptr = '\0';
ptr = strchr(line, ' ');
if (!ptr || !(ptr = strchr(ptr + 1, ' ')))
continue;
/* Skip the space before symbol name */
cptr = skip_symprefix(ptr + 1);
/* Covers gpl-only and normal symbols. */
if (strstarts(cptr, ksymstr))
add_symbol(cptr + ksymstr_len, 0, NULL);
}
fclose(system_map);
add_fake_syms();
}
/**
* load_module_symvers - load list of symbol versions from the module.symvers
*
* @filename: path to the module.symvers-like file
*
*/
static void load_module_symvers(const char *filename)
{
FILE *module_symvers;
char line[10240];
module_symvers = fopen(filename, "r");
if (!module_symvers)
fatal("Could not open '%s': %s\n", filename, strerror(errno));
/* eg. "0xb352177e\tfind_first_bit\tvmlinux\tEXPORT_SYMBOL" */
while (fgets(line, sizeof(line)-1, module_symvers)) {
const char *ver, *sym, *where;
ver = strtok(line, " \t");
sym = strtok(NULL, " \t");
where = strtok(NULL, " \t");
if (!ver || !sym || !where)
continue;
if (streq(where, "vmlinux"))
add_symbol(skip_symprefix(sym), strtoull(ver, NULL, 16), NULL);
}
fclose(module_symvers);
add_fake_syms();
}
static const struct option options[] = { { "all", 0, NULL, 'a' },
{ "quick", 0, NULL, 'A' },
{ "basedir", 1, NULL, 'b' },
{ "config", 1, NULL, 'C' },
{ "symvers", 1, NULL, 'E' },
{ "filesyms", 1, NULL, 'F' },
{ "errsyms", 0, NULL, 'e' },
{ "unresolved-error", 0, NULL, 'u' },
{ "quiet", 0, NULL, 'q' },
{ "root", 0, NULL, 'r' },
{ "verbose", 0, NULL, 'v' },
{ "show", 0, NULL, 'n' },
{ "dry-run", 0, NULL, 'n' },
{ "symbol-prefix", 0, NULL, 'P' },
{ "help", 0, NULL, 'h' },
{ "version", 0, NULL, 'V' },
{ "warn", 0, NULL, 'w' },
{ "map", 0, NULL, 'm' },
{ NULL, 0, NULL, 0 } };
/**
* is_version_number - is the option a kernel version or module name
*
* @version: possible version number
*
*/
static int is_version_number(const char *version)
{
unsigned int dummy;
return (sscanf(version, "%u.%u", &dummy, &dummy) == 2);
}
/**
* old_module_version - is the kernel version too old for these tools
*
* @version: version number
*
*/
static int old_module_version(const char *version)
{
/* Expect three part version (but won't fail it only two part). */
unsigned int major, sub, minor;
sscanf(version, "%u.%u.%u", &major, &sub, &minor);
if (major > 2) return 0;
if (major < 2) return 1;
/* 2.x */
if (sub > 5) return 0;
if (sub < 5) return 1;
/* 2.5.x */
if (minor >= 48) return 0;
return 1;
}
/**
* print_usage - output a list of all possible options
*
* @name: not currently used
*
*/
static void print_usage(const char *name)
{
fprintf(stderr,
"%s " VERSION " -- part of " PACKAGE "\n"
"%s -[aA] [-n -e -v -q -V -r -u -w -m]\n"
" [-b basedirectory] [forced_version]\n"
"depmod [-n -e -v -q -r -u -w] [-F kernelsyms] module1.ko module2.ko ...\n"
"If no arguments (except options) are given, \"depmod -a\" is assumed\n"
"\n"
"depmod will output a dependancy list suitable for the modprobe utility.\n"
"\n"
"\n"
"Options:\n"
"\t-a, --all Probe all modules\n"
"\t-A, --quick Only does the work if there's a new module\n"
"\t-e, --errsyms Report not supplied symbols\n"
"\t-m, --map Create the legacy map files\n"
"\t-n, --show Write the dependency file on stdout only\n"
"\t-P, --symbol-prefix Architecture symbol prefix\n"
"\t-V, --version Print the release version\n"
"\t-v, --verbose Enable verbose mode\n"
"\t-w, --warn Warn on duplicates\n"
"\t-h, --help Print this usage message\n"
"\n"
"The following options are useful for people managing distributions:\n"
"\t-b basedirectory\n"
"\t --basedir basedirectory Use an image of a module tree.\n"
"\t-F kernelsyms\n"
"\t --filesyms kernelsyms Use the file instead of the\n"
"\t current kernel symbols.\n"
"\t-E Module.symvers\n"
"\t --symvers Module.symvers Use Module.symvers file to check\n"
"\t symbol versions.\n",
"depmod", "depmod");
}
/**
* ends_in - check file extension
*
* @name: filename
* @ext: extension to check
*
*/
static int ends_in(const char *name, const char *ext)
{
unsigned int namelen, extlen;
/* Grab lengths */
namelen = strlen(name);
extlen = strlen(ext);
if (namelen < extlen) return 0;
if (streq(name + namelen - extlen, ext))
return 1;
return 0;
}
/**
* grab_module - open module ELF file and load symbol data
*
* @dirname: path prefix
* @filename: filename within path
*
*/
static struct module *grab_module(const char *dirname, const char *filename)
{
struct module *new;
new = NOFAIL(malloc(sizeof(*new)
+ strlen(dirname?:"") + 1 + strlen(filename) + 1));
if (dirname)
sprintf(new->pathname, "%s/%s", dirname, filename);
else
strcpy(new->pathname, filename);
new->basename = my_basename(new->pathname);
INIT_LIST_HEAD(&new->dep_list);
new->order = INDEX_PRIORITY_MIN;
new->file = grab_elf_file(new->pathname);
if (!new->file) {
warn("Can't read module %s: %s\n",
new->pathname, strerror(errno));
free(new);
return NULL;
}
return new;
}
/* We use this on-stack structure to track recursive calls to has_dep_loop */
struct module_traverse
{
struct module_traverse *prev;
struct module *mod;
};
/**
* in_loop - determine if a module dependency loop exists
*
* @mod: current module
* @traverse: on-stack structure created as module deps were processed
*
*/
static int in_loop(struct module *mod, const struct module_traverse *traverse)
{
const struct module_traverse *i;
for (i = traverse; i; i = i->prev) {
if (i->mod == mod)
return 1;
}
return 0;
}
/**
* report_loop - report (once) that a dependency loop exists for a module
*
* @mod: module with dep loop
* @traverse: on-stack structure created as module deps were processed
*
*/
static void report_loop(const struct module *mod,
const struct module_traverse *traverse)
{
const struct module_traverse *i;
/* Check that start is least alphabetically. eg. a depends
on b depends on a will get reported for a, not b. */
for (i = traverse->prev; i->prev; i = i->prev) {
if (strcmp(mod->pathname, i->mod->pathname) > 0)
return;
}
/* Is start in the loop? If not, don't report now. eg. a
depends on b which depends on c which depends on b. Don't
report when generating depends for a. */
if (mod != i->mod)
return;
warn("Loop detected: %s ", mod->pathname);
for (i = traverse->prev; i->prev; i = i->prev)
fprintf(stderr, "needs %s ", i->mod->basename);
fprintf(stderr, "which needs %s again!\n", i->mod->basename);
}
/**
* has_dep_loop - iterate over all module deps and check for loops
*
* @module: module to process
* @prev: previously processed dependency
*
* This function is called recursively, following every dep and creating
* a module_traverse on the stack describing each dependency encountered.
* Determining a loop is as simple (and slow) as finding repetitions.
*
* This is slow, but we can't leave the user without any modules, so we
* need to detect loops and just fail those modules that cause loops.
*
*/
static int has_dep_loop(struct module *module, struct module_traverse *prev)
{
unsigned int i;
struct module_traverse traverse = { .prev = prev, .mod = module };
if (in_loop(module, prev)) {
report_loop(module, &traverse);
return 1;
}
for (i = 0; i < module->num_deps; i++)
if (has_dep_loop(module->deps[i], &traverse))
return 1;
return 0;
}
/**
* order_dep_list - expand all module deps recursively and in order
*
* @start: module being processed
* @mod: recursive dep
*
* We expand all of the dependencies of the dependencies of a module
* and ensure that the lowest dependency is loaded first, etc.
*
*/
static void order_dep_list(struct module *start, struct module *mod)
{
unsigned int i;
for (i = 0; i < mod->num_deps; i++) {
/* If it was previously depended on, move it to the
tail. ie. if a needs b and c, and c needs b, we
must order b after c. */
list_del(&mod->deps[i]->dep_list);
list_add_tail(&mod->deps[i]->dep_list, &start->dep_list);
order_dep_list(start, mod->deps[i]);
}
}
static struct module *deleted = NULL;
/**
* del_module - remove from list of modules
*
* @modules: list of modules
* @delme: module to remove
*
*/
static void del_module(struct module **modules, struct module *delme)
{
struct module **i;
/* Find pointer to it. */
if (modules) {
for (i = modules; *i != delme; i = &(*i)->next);
*i = delme->next;
}
/* Save on a list to quiet valgrind.
Can't free - other modules may depend on them */
delme->next = deleted;
deleted = delme;
}
/**
* compress_path - strip out common path prefix for modules
*
* @path: path to module
* @basedir: top level modules directory
*
* Modules are typically located in /lib/modules. There is no need to
* store the same common path prefix for all modules - make paths
* relative to directory that contains the dep information files.
*
*/
static const char *compress_path(const char *path, const char *basedir)
{
int len = strlen(basedir);
if (strncmp(path, basedir, len) == 0)
path += len + 1;
return path;
}
/**
* output_deps - create ascii text file representation of module deps
*
* @modules: list of modules
* @out: output file
* @dirname: output directory
*
*/
static int output_deps(struct module *modules,
FILE *out, char *dirname)
{
struct module *i;
for (i = modules; i; i = i->next) {
struct list_head *j, *tmp;
order_dep_list(i, i);
fprintf(out, "%s:", compress_path(i->pathname, dirname));
list_for_each_safe(j, tmp, &i->dep_list) {
struct module *dep
= list_entry(j, struct module, dep_list);
fprintf(out, " %s",
compress_path(dep->pathname, dirname));
list_del_init(j);
}
fprintf(out, "\n");
}
return 1;
}
/* warn whenever duplicate module aliases, deps, or symbols are found. */
static int warn_dups = 0;
/**
* output_deps_bin - create binary trie representation of module deps
*
* @modules: list of modules
* @out: output binary file
* @dirname: output directory
*
* This optimized dependency file contains an ordered structure that is
* more easily processed by modprobe in a time sensitive manner.
*
*/
static int output_deps_bin(struct module *modules,
FILE *out, char *dirname)
{
struct module *i;
struct index_node *index;
char *line;
char *p;
index = index_create();
for (i = modules; i; i = i->next) {
struct list_head *j, *tmp;
char modname[strlen(i->pathname)+1];
order_dep_list(i, i);
filename2modname(modname, i->pathname);
nofail_asprintf(&line, "%s:",
compress_path(i->pathname, dirname));
p = line;
list_for_each_safe(j, tmp, &i->dep_list) {
struct module *dep
= list_entry(j, struct module, dep_list);
nofail_asprintf(&line, "%s %s",
p,
compress_path(dep->pathname, dirname));
free(p);
p = line;
list_del_init(j);
}
if (index_insert(index, modname, line, i->order) && warn_dups)
warn("duplicate module deps:\n%s\n",line);
free(line);
}
index_write(index, out);
index_destroy(index);
return 1;
}
/**
* smells_like_module - detect common module extensions
*
* @name: filename
*
*/
static int smells_like_module(const char *name)
{
return ends_in(name,".ko") || ends_in(name, ".ko.gz");
}
typedef struct module *(*do_module_t)(const char *dirname,
const char *filename,
struct module *next,
struct module_search *search,
struct module_overrides *overrides);
/**
* is_higher_priority - find modules replacing other modules
*
* @newpath: new module path
* @oldpath: old module path
* @search: path search order
* @overrides: module override directives
*
* Compares one module (path) to another module (path) and determines
* whether the new module should replace the existing module of the
* same name. Overriding is handled very coarsely for the moment.
*
*/
static int is_higher_priority(const char *newpath, const char *oldpath,
struct module_search *search,
struct module_overrides *overrides)
{
struct module_search *tmp;
struct module_overrides *ovtmp;
int i = 0;
int prio_builtin = -1;
int prio_new = -1;
int prio_old = -1;
/* The names already match, now we check for overrides and directory search
* order
*/
for (ovtmp = overrides; ovtmp != NULL; ovtmp = ovtmp->next) {
if (streq(ovtmp->modfile, newpath))
return 1;
if (streq(ovtmp->modfile, oldpath))
return 0;
}
for (i = 0, tmp = search; tmp != NULL; tmp = tmp->next, i++) {
if (streq(tmp->search_path, MODULE_BUILTIN_KEY))
prio_builtin = i;
else if (strncmp(tmp->search_path, newpath, tmp->len) == 0)
prio_new = i;
else if (strncmp(tmp->search_path, oldpath, tmp->len) == 0)
prio_old = i;
}
if (prio_new < 0)
prio_new = prio_builtin;
if (prio_old < 0)
prio_old = prio_builtin;
return prio_new > prio_old;
}
/**
* do_module - process a module file
*
* @dirname: directory containing module
* @filename: module disk file
* @list: list of modules
* @search: path search order
* @overrides: module override directives
*
*/
static struct module *do_module(const char *dirname,
const char *filename,
struct module *list,
struct module_search *search,
struct module_overrides *overrides)
{
struct module *new, **i;
new = grab_module(dirname, filename);
if (!new)
return list;
/* Check if module is already in the list. */
for (i = &list; *i; i = &(*i)->next) {
if (streq((*i)->basename, filename)) {
char newpath[strlen(dirname) + strlen("/")
+ strlen(filename) + 1];
sprintf(newpath, "%s/%s", dirname, filename);
/* if module matches an existing entry (name) but */
/* has a higher priority, replace existing entry. */
if (is_higher_priority(newpath, (*i)->pathname,search,
overrides)) {
del_module(i, *i);
new->next = *i;
*i = new;
} else
del_module(NULL, new);
return list;
}
}
/* Not in the list already. Just prepend. */
new->next = list;
return new;
}
/**
* grab_dir - process a directory of modules
*
* @dirname: directory name
* @dir: open directory reference
* @do_mod: do_module function to use
* @search: path search order
* @overrides: module overrides directives
*
*/
static struct module *grab_dir(const char *dirname,
DIR *dir,
struct module *next,
do_module_t do_mod,
struct module_search *search,
struct module_overrides *overrides)
{
struct dirent *dirent;
while ((dirent = readdir(dir)) != NULL) {
if (smells_like_module(dirent->d_name))
next = do_mod(dirname, dirent->d_name, next,
search, overrides);
else if (!streq(dirent->d_name, ".")
&& !streq(dirent->d_name, "..")
&& !streq(dirent->d_name, "source")
&& !streq(dirent->d_name, "build")) {
DIR *sub;
char subdir[strlen(dirname) + 1
+ strlen(dirent->d_name) + 1];
sprintf(subdir, "%s/%s", dirname, dirent->d_name);
sub = opendir(subdir);
if (sub) {
next = grab_dir(subdir, sub, next, do_mod,
search, overrides);
closedir(sub);
}
}
}
return next;
}
/**
* grab_basedir - top-level module processing
*
* @dirname: top-level directory name
* @search: path search order
* @overrides: module overrides directives
*
*/
static struct module *grab_basedir(const char *dirname,
struct module_search *search,
struct module_overrides *overrides)
{
DIR *dir;
struct module *list;
dir = opendir(dirname);
if (!dir) {
warn("Couldn't open directory %s: %s\n",
dirname, strerror(errno));
return NULL;
}
list = grab_dir(dirname, dir, NULL, do_module, search, overrides);
closedir(dir);
return list;
}
/**
* sort_modules - order modules in list on modules.order if available
*
* @dirname: directory name
* @list: module list
*
* Using the modules.order file (if available), give every module an index
* based on its position in the file, and order list based on the index. If
* no ordering data is available, fallback to existing unordered list.
*
*/
static struct module *sort_modules(const char *dirname, struct module *list)
{
struct module *tlist = NULL, **tpos = &tlist;
FILE *modorder;
int dir_len = strlen(dirname) + 1;
char file_name[dir_len + strlen("modules.order") + 1];
char line[10240];
unsigned int linenum = 0;
sprintf(file_name, "%s/%s", dirname, "modules.order");
modorder = fopen(file_name, "r");
if (!modorder) {
/* Older kernels don't generate modules.order. Just
return if the file doesn't exist. */
if (errno == ENOENT)
return list;
fatal("Could not open '%s': %s\n", file_name, strerror(errno));
}
sprintf(line, "%s/", dirname);
/* move modules listed in modorder file to tlist in order */
while (fgets(line, sizeof(line), modorder)) {
struct module **pos, *mod;
int len = strlen(line);
linenum++;
if (line[len - 1] == '\n')
line[len - 1] = '\0';
for (pos = &list; (mod = *pos); pos = &(*pos)->next) {
if (streq(line, mod->pathname + dir_len)) {
mod->order = linenum;
*pos = mod->next;
mod->next = NULL;
*tpos = mod;
tpos = &mod->next;
break;
}
}
}
/* append the rest */
*tpos = list;
fclose(modorder);
return tlist;
}
/**
* calculate_deps - calculate deps for module
*
* @module: module to process
*
*/
static void calculate_deps(struct module *module)
{
unsigned int i;
struct string_table *symnames;
struct string_table *symtypes;
uint64_t *symvers = NULL;
struct elf_file *file;
module->num_deps = 0;
module->deps = NULL;
file = module->file;
symnames = file->ops->load_dep_syms(file, &symtypes,
check_symvers ? &symvers : NULL);
if (!symnames || !symtypes)
return;
for (i = 0; i < symnames->cnt; i++) {
const char *name;
uint64_t ver;
struct module *owner;
int weak;
name = symnames->str[i];
ver = symvers ? symvers[i] : 0;
weak = (*(symtypes->str[i]) == 'W');
owner = find_symbol(name, ver, module->pathname, weak);
if (owner) {
info("%s needs \"%s\": %s\n",
module->pathname, name,
owner->pathname);
add_dep(module, owner);
}
}
free(symnames);
free(symtypes);
free(symvers);
}
/**
* parse_modules - process the modules list
*
* @module: module list
*
* Process each module in the (sorted by sort_modules) list for symbols,
* dependencies, and other meta-data that will be output later.
*
*/
static struct module *parse_modules(struct module *list)
{
struct module *i;
struct elf_file *file;