annotate toys/other/modinfo.c @ 986:69adf14d70e1

System V style init, submitted by Kyungwan Han.
author Rob Landley <rob@landley.net>
date Sun, 04 Aug 2013 00:31:27 -0500
parents 436c60124674
children fc1bb49e58a9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
1 /* modinfo.c - Display module info
620
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 * Copyright 2012 Andre Renaud <andre@bluewatersys.com>
312e4c3e00b1 Add modinfo by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
4
936
859a93658467 modinfo: support -b basedir and -k kernel.release, fix two bugs
Isaac Dunham <idunham@lavabit.com>
parents: 934
diff changeset
5 USE_MODINFO(NEWTOY(modinfo, "<1b:k:F:0", TOYFLAG_BIN))
620
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 config MODINFO
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
8 bool "modinfo"
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
9 default y
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
10 help
936
859a93658467 modinfo: support -b basedir and -k kernel.release, fix two bugs
Isaac Dunham <idunham@lavabit.com>
parents: 934
diff changeset
11 usage: modinfo [-0] [-b basedir] [-k kernrelease] [-F field] [modulename...]
859a93658467 modinfo: support -b basedir and -k kernel.release, fix two bugs
Isaac Dunham <idunham@lavabit.com>
parents: 934
diff changeset
12 Display module fields for all specified modules, looking in
859a93658467 modinfo: support -b basedir and -k kernel.release, fix two bugs
Isaac Dunham <idunham@lavabit.com>
parents: 934
diff changeset
13 <basedir>/lib/modules/<kernrelease>/ (kernrelease defaults to uname -r).
620
312e4c3e00b1 Add modinfo by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
14 */
312e4c3e00b1 Add modinfo by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
15
674
7e846e281e38 New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
16 #define FOR_modinfo
620
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
674
7e846e281e38 New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
19 GLOBALS(
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
20 char *field;
936
859a93658467 modinfo: support -b basedir and -k kernel.release, fix two bugs
Isaac Dunham <idunham@lavabit.com>
parents: 934
diff changeset
21 char *knam;
859a93658467 modinfo: support -b basedir and -k kernel.release, fix two bugs
Isaac Dunham <idunham@lavabit.com>
parents: 934
diff changeset
22 char *base;
923
aff9fa1075eb Upgrade modinfo to support multiple modules, and add tests, from Isaac Dunham.
Rob Landley <rob@landley.net>
parents: 884
diff changeset
23
aff9fa1075eb Upgrade modinfo to support multiple modules, and add tests, from Isaac Dunham.
Rob Landley <rob@landley.net>
parents: 884
diff changeset
24 long mod;
620
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
877
37e668afd008 Isaac Dunham pointed out that the kernel treats - and _ as identical in module names, so modinfo should too. Made it use mmap() while I was there, and some cosmetic refactoring.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
27 static void output_field(char *field, char *value)
620
312e4c3e00b1 Add modinfo by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
28 {
934
d0ab54f62346 Modinfo cleanups.
Rob Landley <rob@landley.net>
parents: 933
diff changeset
29 if (!TT.field) xprintf("%s:%*c", field, 15 - strlen(field), ' ');
936
859a93658467 modinfo: support -b basedir and -k kernel.release, fix two bugs
Isaac Dunham <idunham@lavabit.com>
parents: 934
diff changeset
30 else if (strcmp(TT.field, field)) return;
934
d0ab54f62346 Modinfo cleanups.
Rob Landley <rob@landley.net>
parents: 933
diff changeset
31 xprintf("%s", value);
877
37e668afd008 Isaac Dunham pointed out that the kernel treats - and _ as identical in module names, so modinfo should too. Made it use mmap() while I was there, and some cosmetic refactoring.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
32 xputc((toys.optflags & FLAG_0) ? 0 : '\n');
620
312e4c3e00b1 Add modinfo by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
33 }
312e4c3e00b1 Add modinfo by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
34
934
d0ab54f62346 Modinfo cleanups.
Rob Landley <rob@landley.net>
parents: 933
diff changeset
35 static void modinfo_file(char *full_name)
620
312e4c3e00b1 Add modinfo by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
36 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
37 int fd, len, i;
934
d0ab54f62346 Modinfo cleanups.
Rob Landley <rob@landley.net>
parents: 933
diff changeset
38 char *buf = 0, *pos, *modinfo_tags[] = {
d0ab54f62346 Modinfo cleanups.
Rob Landley <rob@landley.net>
parents: 933
diff changeset
39 "alias", "license", "description", "author", "firmware",
945
436c60124674 add paramtype in to the list of tags
Isaac Dunham <idunham@lavabit.com>
parents: 936
diff changeset
40 "vermagic", "srcversion", "intree", "depends", "parm",
436c60124674 add paramtype in to the list of tags
Isaac Dunham <idunham@lavabit.com>
parents: 936
diff changeset
41 "parmtype",
934
d0ab54f62346 Modinfo cleanups.
Rob Landley <rob@landley.net>
parents: 933
diff changeset
42 };
620
312e4c3e00b1 Add modinfo by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
43
934
d0ab54f62346 Modinfo cleanups.
Rob Landley <rob@landley.net>
parents: 933
diff changeset
44 if (-1 != (fd = open(full_name, O_RDONLY))) {
d0ab54f62346 Modinfo cleanups.
Rob Landley <rob@landley.net>
parents: 933
diff changeset
45 len = fdlength(fd);
d0ab54f62346 Modinfo cleanups.
Rob Landley <rob@landley.net>
parents: 933
diff changeset
46 if (!(buf = mmap(0, len, PROT_READ, MAP_SHARED, fd, 0))) close(fd);
d0ab54f62346 Modinfo cleanups.
Rob Landley <rob@landley.net>
parents: 933
diff changeset
47 }
d0ab54f62346 Modinfo cleanups.
Rob Landley <rob@landley.net>
parents: 933
diff changeset
48
d0ab54f62346 Modinfo cleanups.
Rob Landley <rob@landley.net>
parents: 933
diff changeset
49 if (!buf) {
d0ab54f62346 Modinfo cleanups.
Rob Landley <rob@landley.net>
parents: 933
diff changeset
50 perror_msg("%s", full_name);
d0ab54f62346 Modinfo cleanups.
Rob Landley <rob@landley.net>
parents: 933
diff changeset
51 return;
d0ab54f62346 Modinfo cleanups.
Rob Landley <rob@landley.net>
parents: 933
diff changeset
52 }
d0ab54f62346 Modinfo cleanups.
Rob Landley <rob@landley.net>
parents: 933
diff changeset
53
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
54 output_field("filename", full_name);
620
312e4c3e00b1 Add modinfo by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
55
877
37e668afd008 Isaac Dunham pointed out that the kernel treats - and _ as identical in module names, so modinfo should too. Made it use mmap() while I was there, and some cosmetic refactoring.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
56 for (pos = buf; pos < buf+len; pos++) {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
57 if (*pos) continue;
620
312e4c3e00b1 Add modinfo by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
58
877
37e668afd008 Isaac Dunham pointed out that the kernel treats - and _ as identical in module names, so modinfo should too. Made it use mmap() while I was there, and some cosmetic refactoring.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
59 for (i = 0; i < sizeof(modinfo_tags) / sizeof(*modinfo_tags); i++) {
37e668afd008 Isaac Dunham pointed out that the kernel treats - and _ as identical in module names, so modinfo should too. Made it use mmap() while I was there, and some cosmetic refactoring.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
60 char *str = modinfo_tags[i];
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
61 int len = strlen(str);
877
37e668afd008 Isaac Dunham pointed out that the kernel treats - and _ as identical in module names, so modinfo should too. Made it use mmap() while I was there, and some cosmetic refactoring.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
62
37e668afd008 Isaac Dunham pointed out that the kernel treats - and _ as identical in module names, so modinfo should too. Made it use mmap() while I was there, and some cosmetic refactoring.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
63 if (!strncmp(pos+1, str, len) && pos[len+1] == '=')
37e668afd008 Isaac Dunham pointed out that the kernel treats - and _ as identical in module names, so modinfo should too. Made it use mmap() while I was there, and some cosmetic refactoring.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
64 output_field(str, pos+len+2);
620
312e4c3e00b1 Add modinfo by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
65 }
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
66 }
620
312e4c3e00b1 Add modinfo by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
67
877
37e668afd008 Isaac Dunham pointed out that the kernel treats - and _ as identical in module names, so modinfo should too. Made it use mmap() while I was there, and some cosmetic refactoring.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
68 munmap(buf, len);
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
69 close(fd);
620
312e4c3e00b1 Add modinfo by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
70 }
312e4c3e00b1 Add modinfo by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
71
312e4c3e00b1 Add modinfo by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
72 static int check_module(struct dirtree *new)
312e4c3e00b1 Add modinfo by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
73 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
74 if (S_ISREG(new->st.st_mode)) {
923
aff9fa1075eb Upgrade modinfo to support multiple modules, and add tests, from Isaac Dunham.
Rob Landley <rob@landley.net>
parents: 884
diff changeset
75 char *s;
877
37e668afd008 Isaac Dunham pointed out that the kernel treats - and _ as identical in module names, so modinfo should too. Made it use mmap() while I was there, and some cosmetic refactoring.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
76
923
aff9fa1075eb Upgrade modinfo to support multiple modules, and add tests, from Isaac Dunham.
Rob Landley <rob@landley.net>
parents: 884
diff changeset
77 for (s = toys.optargs[TT.mod]; *s; s++) {
877
37e668afd008 Isaac Dunham pointed out that the kernel treats - and _ as identical in module names, so modinfo should too. Made it use mmap() while I was there, and some cosmetic refactoring.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
78 int len = 0;
37e668afd008 Isaac Dunham pointed out that the kernel treats - and _ as identical in module names, so modinfo should too. Made it use mmap() while I was there, and some cosmetic refactoring.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
79
37e668afd008 Isaac Dunham pointed out that the kernel treats - and _ as identical in module names, so modinfo should too. Made it use mmap() while I was there, and some cosmetic refactoring.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
80 // The kernel treats - and _ the same, so we should too.
37e668afd008 Isaac Dunham pointed out that the kernel treats - and _ as identical in module names, so modinfo should too. Made it use mmap() while I was there, and some cosmetic refactoring.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
81 for (len = 0; s[len]; len++) {
37e668afd008 Isaac Dunham pointed out that the kernel treats - and _ as identical in module names, so modinfo should too. Made it use mmap() while I was there, and some cosmetic refactoring.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
82 if (s[len] == '-' && new->name[len] == '_') continue;
37e668afd008 Isaac Dunham pointed out that the kernel treats - and _ as identical in module names, so modinfo should too. Made it use mmap() while I was there, and some cosmetic refactoring.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
83 if (s[len] == '_' && new->name[len] == '-') continue;
37e668afd008 Isaac Dunham pointed out that the kernel treats - and _ as identical in module names, so modinfo should too. Made it use mmap() while I was there, and some cosmetic refactoring.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
84 if (s[len] != new->name[len]) break;
37e668afd008 Isaac Dunham pointed out that the kernel treats - and _ as identical in module names, so modinfo should too. Made it use mmap() while I was there, and some cosmetic refactoring.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
85 }
37e668afd008 Isaac Dunham pointed out that the kernel treats - and _ as identical in module names, so modinfo should too. Made it use mmap() while I was there, and some cosmetic refactoring.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
86 if (s[len] || strcmp(new->name+len, ".ko")) break;
37e668afd008 Isaac Dunham pointed out that the kernel treats - and _ as identical in module names, so modinfo should too. Made it use mmap() while I was there, and some cosmetic refactoring.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
87
934
d0ab54f62346 Modinfo cleanups.
Rob Landley <rob@landley.net>
parents: 933
diff changeset
88 modinfo_file(s = dirtree_path(new, NULL));
d0ab54f62346 Modinfo cleanups.
Rob Landley <rob@landley.net>
parents: 933
diff changeset
89 free(s);
d0ab54f62346 Modinfo cleanups.
Rob Landley <rob@landley.net>
parents: 933
diff changeset
90
d0ab54f62346 Modinfo cleanups.
Rob Landley <rob@landley.net>
parents: 933
diff changeset
91 return DIRTREE_ABORT;
620
312e4c3e00b1 Add modinfo by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
92 }
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
93 }
620
312e4c3e00b1 Add modinfo by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
94
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
95 return dirtree_notdotdot(new);
620
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 {
934
d0ab54f62346 Modinfo cleanups.
Rob Landley <rob@landley.net>
parents: 933
diff changeset
100 for(TT.mod = 0; TT.mod<toys.optc; TT.mod++) {
d0ab54f62346 Modinfo cleanups.
Rob Landley <rob@landley.net>
parents: 933
diff changeset
101 char *s = strstr(toys.optargs[TT.mod], ".ko");
877
37e668afd008 Isaac Dunham pointed out that the kernel treats - and _ as identical in module names, so modinfo should too. Made it use mmap() while I was there, and some cosmetic refactoring.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
102
934
d0ab54f62346 Modinfo cleanups.
Rob Landley <rob@landley.net>
parents: 933
diff changeset
103 if (s && !s[3]) modinfo_file(toys.optargs[TT.mod]);
d0ab54f62346 Modinfo cleanups.
Rob Landley <rob@landley.net>
parents: 933
diff changeset
104 else {
d0ab54f62346 Modinfo cleanups.
Rob Landley <rob@landley.net>
parents: 933
diff changeset
105 struct utsname uts;
d0ab54f62346 Modinfo cleanups.
Rob Landley <rob@landley.net>
parents: 933
diff changeset
106
d0ab54f62346 Modinfo cleanups.
Rob Landley <rob@landley.net>
parents: 933
diff changeset
107 if (uname(&uts) < 0) perror_exit("bad uname");
936
859a93658467 modinfo: support -b basedir and -k kernel.release, fix two bugs
Isaac Dunham <idunham@lavabit.com>
parents: 934
diff changeset
108 if (snprintf(toybuf, sizeof(toybuf), "%s/lib/modules/%s",
859a93658467 modinfo: support -b basedir and -k kernel.release, fix two bugs
Isaac Dunham <idunham@lavabit.com>
parents: 934
diff changeset
109 (toys.optflags & FLAG_b) ? TT.base : "",
859a93658467 modinfo: support -b basedir and -k kernel.release, fix two bugs
Isaac Dunham <idunham@lavabit.com>
parents: 934
diff changeset
110 (toys.optflags & FLAG_k) ? TT.knam : uts.release) >= sizeof(toybuf))
859a93658467 modinfo: support -b basedir and -k kernel.release, fix two bugs
Isaac Dunham <idunham@lavabit.com>
parents: 934
diff changeset
111 perror_exit("basedir/kernrelease too long");
933
a81921ce4930 Patch that assumes that the presence of the string ".ko" indicates
Isaac Dunham <idunham@lavabit.com>
parents: 923
diff changeset
112 dirtree_read(toybuf, check_module);
a81921ce4930 Patch that assumes that the presence of the string ".ko" indicates
Isaac Dunham <idunham@lavabit.com>
parents: 923
diff changeset
113 }
923
aff9fa1075eb Upgrade modinfo to support multiple modules, and add tests, from Isaac Dunham.
Rob Landley <rob@landley.net>
parents: 884
diff changeset
114 }
620
312e4c3e00b1 Add modinfo by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
115 }