1 /* modinfo.c - Display module info
2 *
3 * Copyright 2012 Andre Renaud <andre@bluewatersys.com>
4 *
5 * TODO: cleanup
6
7 USE_MODINFO(NEWTOY(modinfo, "<1b:k:F:0", TOYFLAG_BIN))
8
9 config MODINFO
10 bool "modinfo"
11 default y
12 help
13 usage: modinfo [-0] [-b basedir] [-k kernrelease] [-F field] [modulename...]
14
15 Display module fields for all specified modules, looking in
16 <basedir>/lib/modules/<kernrelease>/ (kernrelease defaults to uname -r).
17 */
18
19 #define FOR_modinfo
20 #include "toys.h"
21
GLOBALS(char * field;char * knam;char * base;long mod;)22 GLOBALS(
23 char *field;
24 char *knam;
25 char *base;
26
27 long mod;
28 )
29
30 static void output_field(char *field, char *value)
31 {
32 if (!TT.field) xprintf("%s:%*c", field, 15-(int)strlen(field), ' ');
33 else if (strcmp(TT.field, field)) return;
34 xprintf("%s", value);
35 xputc((toys.optflags & FLAG_0) ? 0 : '\n');
36 }
37
modinfo_file(char * full_name)38 static void modinfo_file(char *full_name)
39 {
40 int fd, len, i;
41 char *buf = 0, *pos, *modinfo_tags[] = {
42 "alias", "license", "description", "author", "firmware",
43 "vermagic", "srcversion", "intree", "depends", "parm",
44 "parmtype",
45 };
46
47 if (-1 != (fd = open(full_name, O_RDONLY))) {
48 len = fdlength(fd);
49 buf = xmmap(0, len, PROT_READ, MAP_SHARED, fd, 0);
50 close(fd);
51 }
52
53 if (!buf) {
54 perror_msg_raw(full_name);
55 return;
56 }
57
58 output_field("filename", full_name);
59
60 for (pos = buf; pos < buf+len; pos++) {
61 if (*pos) continue;
62
63 for (i = 0; i < sizeof(modinfo_tags) / sizeof(*modinfo_tags); i++) {
64 char *str = modinfo_tags[i];
65 int len = strlen(str);
66
67 if (!strncmp(pos+1, str, len) && pos[len+1] == '=')
68 output_field(str, pos+len+2);
69 }
70 }
71
72 munmap(buf, len);
73 }
74
check_module(struct dirtree * new)75 static int check_module(struct dirtree *new)
76 {
77 if (!dirtree_notdotdot(new)) return 0;
78
79 if (S_ISREG(new->st.st_mode)) {
80 char *s;
81
82 for (s = toys.optargs[TT.mod]; *s; s++) {
83 int len = 0;
84
85 // The kernel treats - and _ the same, so we should too.
86 for (len = 0; s[len]; len++) {
87 if (s[len] == '-' && new->name[len] == '_') continue;
88 if (s[len] == '_' && new->name[len] == '-') continue;
89 if (s[len] != new->name[len]) break;
90 }
91 if (s[len] || strcmp(new->name+len, ".ko")) break;
92
93 modinfo_file(s = dirtree_path(new, 0));
94 free(s);
95
96 return DIRTREE_ABORT;
97 }
98 }
99
100 return DIRTREE_RECURSE;
101 }
102
modinfo_main(void)103 void modinfo_main(void)
104 {
105 for(TT.mod = 0; TT.mod<toys.optc; TT.mod++) {
106 char *s = strstr(toys.optargs[TT.mod], ".ko");
107
108 if (s && !s[3]) modinfo_file(toys.optargs[TT.mod]);
109 else {
110 struct utsname uts;
111
112 if (uname(&uts) < 0) perror_exit("bad uname");
113 if (snprintf(toybuf, sizeof(toybuf), "%s/lib/modules/%s",
114 (toys.optflags & FLAG_b) ? TT.base : "",
115 (toys.optflags & FLAG_k) ? TT.knam : uts.release) >= sizeof(toybuf))
116 perror_exit("basedir/kernrelease too long");
117 dirtree_read(toybuf, check_module);
118 }
119 }
120 }
121