annotate toys/modinfo.c @ 620:312e4c3e00b1

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