620
|
1 /* vi: set sw=4 ts=4:
|
|
2 *
|
|
3 * modinfo.c - Display module info
|
|
4 *
|
|
5 * Copyright 2012 Andre Renaud <andre@bluewatersys.com>
|
|
6 *
|
|
7
|
|
8 USE_MODINFO(NEWTOY(modinfo, "<1F:0", TOYFLAG_BIN))
|
|
9
|
|
10 config MODINFO
|
|
11 bool "modinfo"
|
|
12 default y
|
|
13 help
|
|
14 usage: modinfo [-0] [-F field] [modulename...]
|
|
15 */
|
|
16
|
|
17 #include "toys.h"
|
|
18
|
|
19 #define FLAG_0 (1 << 0)
|
|
20
|
|
21 DEFINE_GLOBALS(
|
|
22 char *field;
|
|
23 )
|
|
24 #define TT this.modinfo
|
|
25
|
|
26 static const char *modinfo_tags[] = {
|
|
27 "alias", "license", "description", "author", "vermagic",
|
|
28 "srcversion", "intree", "parm", "depends",
|
|
29 };
|
|
30
|
|
31 static void output_field(const char *field, const char *value)
|
|
32 {
|
|
33 int len;
|
|
34
|
|
35 if (TT.field && strcmp(TT.field, field) != 0)
|
|
36 return;
|
|
37
|
|
38 len = strlen(field);
|
|
39
|
|
40 if (TT.field)
|
|
41 xprintf("%s", value);
|
|
42 else
|
|
43 xprintf("%s:%*s%s",
|
|
44 field, 15 - len, "", value);
|
|
45 if (toys.optflags & FLAG_0)
|
|
46 xwrite(fileno(stdout), "\0", 1);
|
|
47 else
|
|
48 xputs("");
|
|
49 }
|
|
50
|
|
51
|
|
52 static void modinfo_file(struct dirtree *dir)
|
|
53 {
|
|
54 int fd, len, i;
|
|
55 char *buf, *pos;
|
|
56 char *full_name;
|
|
57
|
|
58 full_name = dirtree_path(dir, NULL);
|
|
59
|
|
60 output_field("filename", full_name);
|
|
61 fd = xopen(full_name, O_RDONLY);
|
|
62 len = fdlength(fd);
|
|
63 buf = xmalloc(len);
|
|
64 xreadall(fd, buf, len);
|
|
65
|
|
66 for (pos = buf; pos < buf + len + 10; pos++) {
|
|
67 if (*pos)
|
|
68 continue;
|
|
69
|
|
70 for (i = 0; i < sizeof(modinfo_tags) / sizeof(modinfo_tags[0]); i++) {
|
|
71 const char *str = modinfo_tags[i];
|
|
72 int len = strlen(str);
|
|
73 if (strncmp(pos + 1, str, len) == 0 && pos[len + 1] == '=')
|
|
74 output_field(str, &pos[len + 2]);
|
|
75 }
|
|
76 }
|
|
77
|
|
78 free(full_name);
|
|
79 free(buf);
|
|
80 close(fd);
|
|
81 }
|
|
82
|
|
83 static int check_module(struct dirtree *new)
|
|
84 {
|
|
85 if (S_ISREG(new->st.st_mode)) {
|
|
86 char **s;
|
|
87 for (s = toys.optargs; *s; s++) {
|
|
88 int len = strlen(*s);
|
|
89 if (strncmp(*s, new->name, len) == 0 &&
|
|
90 strcmp(&new->name[len], ".ko") == 0)
|
|
91 modinfo_file(new);
|
|
92 }
|
|
93 }
|
|
94
|
|
95 return dirtree_notdotdot(new);
|
|
96 }
|
|
97
|
|
98 void modinfo_main(void)
|
|
99 {
|
|
100 struct utsname uts;
|
|
101 if (uname(&uts) < 0)
|
|
102 perror_exit("unable to determine uname");
|
|
103 sprintf(toybuf, "/lib/modules/%s", uts.release);
|
|
104 dirtree_read(toybuf, check_module);
|
|
105 }
|