-
Notifications
You must be signed in to change notification settings - Fork 11
/
modprobe.c
2337 lines (2065 loc) · 54.2 KB
/
modprobe.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
/* modprobe.c: add or remove a module from the kernel, intelligently.
*
* Copyright (C) 2001 Rusty Russell.
* Copyright (C) 2002, 2003 Rusty Russell, IBM Corporation.
* Copyright (C) 2006-2011 Jon Masters <jcm@jonmasters.org>, 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 <sys/utsname.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <dirent.h>
#include <limits.h>
#include <elf.h>
#include <getopt.h>
#include <fnmatch.h>
#include <asm/unistd.h>
#include <sys/wait.h>
#include <syslog.h>
#include "util.h"
#include "elfops.h"
#include "zlibsupport.h"
#include "logging.h"
#include "index.h"
#include "list.h"
#include "config_filter.h"
#include "testing.h"
/* NOTE: in the future, binary indexes will always be used */
static int use_binary_indexes = 1; /* default to enabled. */
/* Limit do_softdep/do_modprobe recursion.
* This is a simple way to handle dependency loops
* caused by poorly written softdep commands.
*/
static int recursion_depth = 0;
static const int MAX_RECURSION = 50; /* Arbitrary choice */
extern long init_module(void *, unsigned long, const char *);
extern long delete_module(const char *, unsigned int);
struct module {
struct list_head list;
char *modname;
char filename[0];
};
typedef enum
{
mit_remove = 1,
mit_dry_run = 2,
mit_first_time = 4,
mit_use_blacklist = 8,
mit_ignore_commands = 16,
mit_ignore_loaded = 32,
mit_quiet_inuse = 64,
mit_strip_vermagic = 128,
mit_strip_modversion = 256,
mit_resolve_alias = 512
} modprobe_flags_t;
#ifndef MODULE_DIR
#define MODULE_DIR "/lib/modules"
#endif
/**
* print_usage - output the prefered program usage
*
* @progname: binary name invoked
*
*/
static void print_usage(const char *progname)
{
fprintf(stderr,
"Usage: %s [-v] [-V] [-C config-file] [-d <dirname> ] [-n] [-i] [-q] [-b] [-o <modname>] [ --dump-modversions ] <modname> [parameters...]\n"
"%s -r [-n] [-i] [-v] <modulename> ...\n"
"%s -l -t <dirname> [ -a <modulename> ...]\n",
progname, progname, progname);
exit(1);
}
/**
* find_module - search module list for module name
*
* @filename: module file name
* @list: module list
*
*/
static struct module *find_module(const char *filename, struct list_head *list)
{
struct module *i;
list_for_each_entry(i, list, list) {
if (streq(i->filename, filename))
return i;
}
return NULL;
}
/**
* add_module - add a module to the global module list
*
* @filename: module file name
* @list: module list
*
*/
static void add_module(char *filename, int namelen, struct list_head *list)
{
struct module *mod;
/* If it's a duplicate: move it to the end, so it gets
inserted where it is *first* required. */
mod = find_module(filename, list);
if (mod)
list_del(&mod->list);
else {
/* No match. Create a new module. */
mod = NOFAIL(malloc(sizeof(struct module) + namelen + 1));
memcpy(mod->filename, filename, namelen);
mod->filename[namelen] = '\0';
mod->modname = NOFAIL(malloc(namelen + 1));
filename2modname(mod->modname, mod->filename);
}
list_add_tail(&mod->list, list);
}
/**
* free_module - de-allocate module structure
*
* @mod: module structure
*
*/
static void free_module(struct module *mod)
{
free(mod->modname);
free(mod);
}
/**
* modname_equal - compare module names (up to len), with '_' and '-' equal
*
* @a: first module name
* @b: second module name
* @len: length to compare
*
*/
static int modname_equal(const char *a, const char *b, unsigned int len)
{
unsigned int i;
if (strlen(b) != len)
return 0;
for (i = 0; i < len; i++) {
if ((a[i] == '_' || a[i] == '-')
&& (b[i] == '_' || b[i] == '-'))
continue;
if (a[i] != b[i])
return 0;
}
return 1;
}
/**
* add_modules_dep_line - parse a dep line from the module.dep[.bin] file
*
* @line: input file line
* @name: module name
* @list: list of modules
* @dirname: module directory
*
* Add dependency information if this line of the dep file matches mod name
*/
static int add_modules_dep_line(char *line,
const char *name,
struct list_head *list,
const char *dirname)
{
char *ptr;
int len;
char *modname, *fullpath;
/* Ignore lines without : or which start with a # */
ptr = strchr(line, ':');
if (ptr == NULL || line[strspn(line, "\t ")] == '#')
return 0;
/* Is this the module we are looking for? */
*ptr = '\0';
modname = my_basename(line);
len = strlen(modname);
if (strchr(modname, '.'))
len = strchr(modname, '.') - modname;
if (!modname_equal(modname, name, len))
return 0;
/* Create the list. */
if ('/' == line[0]) { /* old style deps - absolute path specified */
add_module(line, ptr - line, list);
} else {
nofail_asprintf(&fullpath, "%s/%s", dirname, line);
add_module(fullpath, strlen(dirname)+1+(ptr - line), list);
free(fullpath);
}
ptr++;
for(;;) {
char *dep_start;
ptr += strspn(ptr, " \t");
if (*ptr == '\0')
break;
dep_start = ptr;
ptr += strcspn(ptr, " \t");
/* We handle deps in two possible ways. Either they have */
/* an absolute path, or a relative path (to toplevel moddir). */
if ('/' == dep_start[0]) { /* old style deps */
add_module(dep_start, ptr - dep_start, list);
} else {
nofail_asprintf(&fullpath, "%s/%s", dirname, dep_start);
add_module(fullpath,
strlen(dirname)+1+(ptr - dep_start), list);
free(fullpath);
}
}
return 1;
}
/**
* read_depends_file - import the modules.dep.bin file
*
* @dirname: module directory
* @start_name: initial module name to search
* @list: list of modules
*
*/
static int read_depends_file(const char *dirname,
const char *start_name,
struct list_head *list)
{
char *modules_dep_name;
char *line;
struct index_file *modules_dep;
nofail_asprintf(&modules_dep_name, "%s/%s", dirname, "modules.dep.bin");
modules_dep = index_file_open(modules_dep_name);
if (!modules_dep) {
free(modules_dep_name);
return 0;
}
line = index_search(modules_dep, start_name);
if (line) {
/* Value is standard dependency line format */
if (!add_modules_dep_line(line, start_name, list, dirname))
fatal("Module index is inconsistent\n");
free(line);
}
index_file_close(modules_dep);
free(modules_dep_name);
return 1;
}
/**
* read_depends - import the modules.dep[.bin] file
*
* @dirname: module directory
* @start_name: initial module name to search
* @list: list of modules
*
*/
static void read_depends(const char *dirname,
const char *start_name,
struct list_head *list)
{
char *modules_dep_name;
char *line;
FILE *modules_dep;
int done = 0;
if (use_binary_indexes)
if (read_depends_file(dirname, start_name, list))
return;
nofail_asprintf(&modules_dep_name, "%s/%s", dirname, "modules.dep");
modules_dep = fopen(modules_dep_name, "r");
if (!modules_dep)
fatal("Could not load %s: %s\n",
modules_dep_name, strerror(errno));
/* Stop at first line, as we can have duplicates (eg. symlinks
from boot/ */
while (!done && (line = getline_wrapped(modules_dep, NULL)) != NULL) {
done = add_modules_dep_line(line, start_name, list, dirname);
free(line);
}
fclose(modules_dep);
free(modules_dep_name);
}
/**
* insert_moderror - convert standard insert error numbers into error messages
*
* @err: error number
*
*/
static const char *insert_moderror(int err)
{
switch (err) {
case ENOEXEC:
return "Invalid module format";
case ENOENT:
return "Unknown symbol in module, or unknown parameter (see dmesg)";
case ENOSYS:
return "Kernel does not have module support";
default:
return strerror(err);
}
}
/**
* remove_moderror - convert standard remove error numbers into error messages
*
* @err: error number
*
*/
static const char *remove_moderror(int err)
{
switch (err) {
case ENOENT:
return "No such module";
case ENOSYS:
return "Kernel does not have module unloading support";
default:
return strerror(err);
}
}
/**
* clear_magic - strip any versioning information from the module
*
* @module: mapped module file
*
*/
static void clear_magic(struct elf_file *module)
{
struct string_table *tbl;
int j;
/* Old-style: __vermagic section */
module->ops->strip_section(module, "__vermagic");
/* New-style: in .modinfo section */
tbl = module->ops->load_strings(module, ".modinfo", NULL);
for (j = 0; tbl && j < tbl->cnt; j++) {
const char *p = tbl->str[j];
if (strstarts(p, "vermagic=")) {
memset((char *)p, 0, strlen(p));
return;
}
}
}
/* keep track of module options from config file(s) */
struct module_options
{
struct module_options *next;
char *modulename;
char *options;
};
/* keep track of module install commands from config file(s) */
struct module_command
{
struct module_command *next;
char *modulename;
char *command;
};
/* keep track of module aliases added in the config file(s) */
struct module_alias
{
struct module_alias *next;
char *aliasname;
char *module;
};
/* keep track of modules blacklisted in the config file(s) */
struct module_blacklist
{
struct module_blacklist *next;
char *modulename;
};
/* keep track of module softdeps added in the config file(s) */
struct module_softdep
{
struct module_softdep *next;
char *buf;
/* The modname and string tables point to buf. */
char *modname;
struct string_table *pre;
struct string_table *post;
};
/* keep track of all config options */
struct modprobe_conf
{
struct module_options *options;
struct module_command *commands;
struct module_alias *aliases;
struct module_blacklist *blacklist;
struct module_softdep *softdeps;
};
/**
* add_options - module options added in the config file(s)
*
* @modname: module name
* @option: options string
* @options: list of options
*
*/
static struct module_options *
add_options(const char *modname,
const char *option,
struct module_options *options)
{
struct module_options *new;
char *tab;
new = NOFAIL(malloc(sizeof(*new)));
new->modulename = NOFAIL(strdup(modname));
new->options = NOFAIL(strdup(option));
/* We can handle tabs, kernel can't. */
for (tab = strchr(new->options, '\t'); tab; tab = strchr(tab, '\t'))
*tab = ' ';
new->next = options;
return new;
}
/**
* add_command - module install commands added in the config file(s)
*
* @modname: module name
* @command: command string
* @commands: list of commands
*
*/
static struct module_command *
add_command(const char *modname,
const char *command,
struct module_command *commands)
{
struct module_command *new;
new = NOFAIL(malloc(sizeof(*new)));
new->modulename = NOFAIL(strdup(modname));
new->command = NOFAIL(strdup(command));
new->next = commands;
return new;
}
/**
* add_alias - module aliases added in the config file(s)
*
* @aliasname: alias string
* @modname: module name
* @aliases: list of aliases
*
*/
static struct module_alias *
add_alias(const char *aliasname, const char *modname, struct module_alias *aliases)
{
struct module_alias *new;
new = NOFAIL(malloc(sizeof(*new)));
new->aliasname = NOFAIL(strdup(aliasname));
new->module = NOFAIL(strdup(modname));
new->next = aliases;
return new;
}
/**
* find_aliases - find aliases for a module
*
* @aliases: list of aliases
* @name: module name
*
*/
static struct module_alias *
find_aliases(const struct module_alias *aliases,
const char *name)
{
struct module_alias *result = NULL;
while (aliases) {
char *aliasname = aliases->aliasname;
char *modname = aliases->module;
if (fnmatch(aliasname, name, 0) == 0)
result = add_alias(aliasname, modname, result);
aliases = aliases->next;
}
return result;
}
/**
* free_aliases - de-allocate the aliases list
*
* @alias_list: list of aliases
*
*/
static void free_aliases(struct module_alias *alias_list)
{
while (alias_list) {
struct module_alias *alias;
alias = alias_list;
alias_list = alias_list->next;
free(alias->aliasname);
free(alias->module);
free(alias);
}
}
/**
* add_blacklist - blacklist modules in config file(s)
*
* @modname: module name
* @blacklist list of blacklisted module names
*
*/
static struct module_blacklist *
add_blacklist(const char *modname, struct module_blacklist *blacklist)
{
struct module_blacklist *new;
new = NOFAIL(malloc(sizeof(*new)));
new->modulename = NOFAIL(strdup(modname));
new->next = blacklist;
return new;
}
/**
* find_blacklist - lookup any potentially blacklisted module
*
* @modname: module name
* @blacklist: list of blacklisted module names
*
*/
static int
find_blacklist(const char *modname, const struct module_blacklist *blacklist)
{
while (blacklist) {
if (streq(blacklist->modulename, modname))
return 1;
blacklist = blacklist->next;
}
return 0;
}
/**
* apply_blacklist - remove blacklisted modules from alias list
*
* @aliases: module alias list
* @blacklist: list of blacklisted module names
*
*/
static void
apply_blacklist(struct module_alias **aliases,
const struct module_blacklist *blacklist)
{
struct module_alias *result = NULL;
struct module_alias *alias = *aliases;
while (alias) {
char *modname = alias->module;
if (!find_blacklist(modname, blacklist))
result = add_alias(alias->aliasname, modname, result);
alias = alias->next;
}
free_aliases(*aliases);
*aliases = result;
}
/**
* find_command - lookup any install commands for a module
*
* @modname: module name
* @commands: list of install commands
*
*/
static const char *find_command(const char *modname,
const struct module_command *commands)
{
while (commands) {
if (fnmatch(commands->modulename, modname, 0) == 0)
return commands->command;
commands = commands->next;
}
return NULL;
}
/**
* find_softdep - lookup any softdeps for a module
*
* @modname: module name
* @softdeps: list of module softdeps
*
*/
static const struct module_softdep *
find_softdep(const char *modname, const struct module_softdep *softdeps)
{
while (softdeps) {
if (fnmatch(softdeps->modname, modname, 0) == 0)
return softdeps;
softdeps = softdeps->next;
}
return NULL;
}
/**
* append_option - add further options to modules (tail)
*
* @options: existing option string
* @newoption: additional option string
*
* options supplied on the command line
*/
static char *append_option(char *options, const char *newoption)
{
options = NOFAIL(realloc(options, strlen(options) + 1
+ strlen(newoption) + 1));
if (strlen(options)) strcat(options, " ");
strcat(options, newoption);
return options;
}
/**
* prepend_option - add further options to modules (head)
*
* @options: existing option string
* @newoption: additional option string
*
* options supplied in the config file(s)
*/
static char *prepend_option(char *options, const char *newoption)
{
size_t l1, l2;
l1 = strlen(options);
l2 = strlen(newoption);
/* the resulting string will look like
* newoption + ' ' + options + '\0' */
if (l1) {
options = NOFAIL(realloc(options, l2 + 1 + l1 + 1));
memmove(options + l2 + 1, options, l1 + 1);
options[l2] = ' ';
memcpy(options, newoption, l2);
} else {
options = NOFAIL(realloc(options, l2 + 1));
memcpy(options, newoption, l2);
options[l2] = '\0';
}
return options;
}
/**
* add_extra_options - add any relevant options from the config file(s)
*
* @modname: module name
* @optstring: options
* @options: list of options
*
*/
static char *add_extra_options(const char *modname,
const char *optstring,
const struct module_options *options)
{
char *opts = NOFAIL(strdup(optstring));
while (options) {
if (streq(options->modulename, modname))
opts = prepend_option(opts, options->options);
options = options->next;
}
return opts;
}
/**
* module_in_procfs - check if module is known to be loaded already
*
* @modname: module name
* @usecount: update usecount if possible (-1 if unknown)
*
*/
static int module_in_procfs(const char *modname, unsigned int *usecount)
{
FILE *proc_modules;
char *line;
again:
/* Might not be mounted yet. Don't fail. */
proc_modules = fopen("/proc/modules", "r");
if (!proc_modules)
return -1;
while ((line = getline_wrapped(proc_modules, NULL)) != NULL) {
char *entry = strtok(line, " \n");
if (entry && streq(entry, modname)) {
/* If it exists, usecount is the third entry. */
if (!strtok(NULL, " \n"))
goto out;
if (!(entry = strtok(NULL, " \n"))) /* usecount */
goto out;
else
if (usecount)
*usecount = atoi(entry);
/* Followed by - then status. */
if (strtok(NULL, " \n")
&& (entry = strtok(NULL, " \n")) != NULL) {
/* No locking, we might hit cases
* where module is in flux. Spin. */
if (streq(entry, "Loading")
|| streq(entry, "Unloading")) {
usleep(100000);
free(line);
fclose(proc_modules);
goto again;
}
}
out:
free(line);
fclose(proc_modules);
return 1;
}
free(line);
}
fclose(proc_modules);
return 0;
}
/**
* read_attribute - read sysfs file attributes into a buffer
*
* @filename: name of sysfs file
* @buf: buffer
* @buflen: size of buffer
*
*/
static int read_attribute(const char *filename, char *buf, size_t buflen)
{
FILE *file;
char *s;
file = fopen(filename, "r");
if (file == NULL)
return (errno == ENOENT) ? 0 : -1;
s = fgets(buf, buflen, file);
fclose(file);
/* return -1 if any problems */
return (s == NULL) ? -1 : 1;
}
/**
* module_builtin - try to determine whether a module is built-in
*
* @dirname: module directory
* @modname: name of module
*
*/
static int module_builtin(const char *dirname, const char *modname)
{
struct index_file *index;
char *filename, *value;
nofail_asprintf(&filename, "%s/modules.builtin.bin", dirname);
index = index_file_open(filename);
free(filename);
/* return -1 if no builtin list available (modern depmod file) */
if (!index)
return -1;
value = index_search(index, modname);
free(value);
return value ? 1 : 0;
}
/**
* module_in_sysfs - try to determine if module has a sysfs entry
*
* @modname: module name
* @usecount: update use count if possible (-1 if unknown)
*
*/
static int module_in_sysfs(const char *modname, unsigned int *usecount)
{
int ret;
char *name;
struct stat finfo;
const int ATTR_LEN = 16;
char attr[ATTR_LEN];
/* Check sysfs is mounted */
if (stat("/sys/module", &finfo) < 0)
return -1;
/* Find module. */
nofail_asprintf(&name, "/sys/module/%s", modname);
ret = stat(name, &finfo);
free(name);
if (ret < 0)
return (errno == ENOENT) ? 0 : -1; /* Not found or unknown. */
nofail_asprintf(&name, "/sys/module/%s/initstate", modname);
ret = read_attribute(name, attr, ATTR_LEN);
if (ret == 0) {
free(name);
nofail_asprintf(&name, "/sys/module/%s", modname);
if (stat(name, &finfo) < 0) {
/* module was removed before we could read initstate */
ret = 0;
} else {
/* initstate not available (2.6.19 or earlier) */
ret = -1;
}
free(name);
return ret;
}
/* Wait for the existing module to either go live or disappear. */
while (ret == 1 && !streq(attr, "live\n")) {
usleep(100000);
ret = read_attribute(name, attr, ATTR_LEN);
}
free(name);
if (ret != 1)
return ret;
/* Get reference count, if it exists. */
if (usecount != NULL) {
nofail_asprintf(&name, "/sys/module/%s/refcnt", modname);
ret = read_attribute(name, attr, ATTR_LEN);
free(name);
if (ret == 1)
*usecount = atoi(attr);
}
return 1;
}
/**
* module_in_kernel - try to determine if the module is loaded (sysfs,procfs)
*
* @modname: name of module
* @usecount: update module use count (-1 if unknown)
*
*/
static int module_in_kernel(const char *modname, unsigned int *usecount)
{
int result;
result = module_in_sysfs(modname, usecount);
if (result != -1)
return result;
/* /sys/module/%s/initstate is only available since 2.6.20,
fallback to /proc/modules to get module state on earlier kernels. */
return module_in_procfs(modname, usecount);
}
/**
* dump_modversions - output list of module version checksums
*
* @filename: module file name
* @error: error function to use
*
*/
static void dump_modversions(const char *filename, errfn_t error)
{
struct elf_file *module;
module = grab_elf_file(filename);
if (!module) {
error("%s: %s\n", filename, strerror(errno));
return;
}
if (module->ops->dump_modvers(module) < 0)
error("Wrong section size in '%s'\n", filename);
release_elf_file(module);
}
/**
* type_matches - constrain wildcard module matching to subdirectory
*
* @path: path string
* @subpath: search path for subpath
*
* Legacy function used to support deprecated option
*/
static int type_matches(const char *path, const char *subpath)
{
char *subpath_with_slashes;
int ret;
nofail_asprintf(&subpath_with_slashes, "/%s/", subpath);
ret = (strstr(path, subpath_with_slashes) != NULL);
free(subpath_with_slashes);
return ret;
}
/**
* do_wildcard - match modules (possibly in directory names containing "type")
*
* @dirname: module directory
* @type: possible subdirectory limiting
* @wildcard: what to match
*
*/
static int do_wildcard(const char *dirname,
const char *type,
const char *wildcard)
{
char *modules_dep_name;
char *line, *wcard;
FILE *modules_dep;
/* Canonicalize wildcard */
wcard = strdup(wildcard);
underscores(wcard);
nofail_asprintf(&modules_dep_name, "%s/%s", dirname, "modules.dep");
modules_dep = fopen(modules_dep_name, "r");
if (!modules_dep)
fatal("Could not load %s: %s\n",
modules_dep_name, strerror(errno));
while ((line = getline_wrapped(modules_dep, NULL)) != NULL) {
char *ptr;
/* Ignore lines without : or which start with a # */
ptr = strchr(line, ':');
if (ptr == NULL || line[strspn(line, "\t ")] == '#')
goto next;
*ptr = '\0';
/* "type" must match complete directory component(s). */
if (!type || type_matches(line, type)) {
char modname[strlen(line)+1];
filename2modname(modname, line);
if (fnmatch(wcard, modname, 0) == 0)
printf("%s\n", line);
}
next:
free(line);
}
free(modules_dep_name);
free(wcard);