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 if (!(buf = mmap(0, len, PROT_READ, MAP_SHARED, fd, 0))) close(fd);
50 }
51
52 if (!buf) {
53 perror_msg_raw(full_name);
54 return;
55 }
56
57 output_field("filename", full_name);
58
59 for (pos = buf; pos < buf+len; pos++) {
60 if (*pos) continue;
61
62 for (i = 0; i < sizeof(modinfo_tags) / sizeof(*modinfo_tags); i++) {
63 char *str = modinfo_tags[i];
64 int len = strlen(str);
65
66 if (!strncmp(pos+1, str, len) && pos[len+1] == '=')
67 output_field(str, pos+len+2);
68 }
69 }
70
71 munmap(buf, len);
72 close(fd);
73 }
74
check_module(struct dirtree * new)75 static int check_module(struct dirtree *new)
76 {
77 if (S_ISREG(new->st.st_mode)) {
78 char *s;
79
80 for (s = toys.optargs[TT.mod]; *s; s++) {
81 int len = 0;
82
83 // The kernel treats - and _ the same, so we should too.
84 for (len = 0; s[len]; len++) {
85 if (s[len] == '-' && new->name[len] == '_') continue;
86 if (s[len] == '_' && new->name[len] == '-') continue;
87 if (s[len] != new->name[len]) break;
88 }
89 if (s[len] || strcmp(new->name+len, ".ko")) break;
90
91 modinfo_file(s = dirtree_path(new, NULL));
92 free(s);
93
94 return DIRTREE_ABORT;
95 }
96 }
97
98 return dirtree_notdotdot(new);
99 }
100
modinfo_main(void)101 void modinfo_main(void)
102 {
103 for(TT.mod = 0; TT.mod<toys.optc; TT.mod++) {
104 char *s = strstr(toys.optargs[TT.mod], ".ko");
105
106 if (s && !s[3]) modinfo_file(toys.optargs[TT.mod]);
107 else {
108 struct utsname uts;
109
110 if (uname(&uts) < 0) perror_exit("bad uname");
111 if (snprintf(toybuf, sizeof(toybuf), "%s/lib/modules/%s",
112 (toys.optflags & FLAG_b) ? TT.base : "",
113 (toys.optflags & FLAG_k) ? TT.knam : uts.release) >= sizeof(toybuf))
114 perror_exit("basedir/kernrelease too long");
115 dirtree_read(toybuf, check_module);
116 }
117 }
118 }
119