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