Mercurial > hg > toybox
annotate toys/other/modinfo.c @ 933:a81921ce4930
Patch that assumes that the presence of the string ".ko" indicates
use of a path to a module (*.ko.xz and similar included, but not supported).
author | Isaac Dunham <idunham@lavabit.com> |
---|---|
date | Sun, 23 Jun 2013 14:02:16 -0500 |
parents | aff9fa1075eb |
children | d0ab54f62346 |
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 | 2 * |
3 * Copyright 2012 Andre Renaud <andre@bluewatersys.com> | |
4 | |
5 USE_MODINFO(NEWTOY(modinfo, "<1F:0", TOYFLAG_BIN)) | |
6 | |
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 |
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
|
11 usage: modinfo [-0] [-F field] [modulename...] |
620 | 12 */ |
13 | |
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
|
14 #define FOR_modinfo |
620 | 15 #include "toys.h" |
16 | |
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
|
17 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
|
18 char *field; |
923
aff9fa1075eb
Upgrade modinfo to support multiple modules, and add tests, from Isaac Dunham.
Rob Landley <rob@landley.net>
parents:
884
diff
changeset
|
19 |
aff9fa1075eb
Upgrade modinfo to support multiple modules, and add tests, from Isaac Dunham.
Rob Landley <rob@landley.net>
parents:
884
diff
changeset
|
20 long mod; |
620 | 21 ) |
22 | |
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
|
23 static char *modinfo_tags[] = { |
884 | 24 "alias", "license", "description", "author", "firmware", |
25 "vermagic", "srcversion", "intree", "parm", "depends", | |
620 | 26 }; |
27 | |
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
|
28 static void output_field(char *field, char *value) |
620 | 29 { |
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
|
30 int len; |
620 | 31 |
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 if (TT.field && strcmp(TT.field, field)) return; |
620 | 33 |
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
|
34 len = strlen(field); |
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
|
35 |
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
|
36 if (TT.field) xprintf("%s", value); |
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 else xprintf("%s:%*s%s", field, 15 - len, "", 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
|
38 xputc((toys.optflags & FLAG_0) ? 0 : '\n'); |
620 | 39 } |
40 | |
933
a81921ce4930
Patch that assumes that the presence of the string ".ko" indicates
Isaac Dunham <idunham@lavabit.com>
parents:
923
diff
changeset
|
41 static int modinfo_file(struct dirtree *dir) |
620 | 42 { |
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
|
43 int fd, len, i; |
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
|
44 char *buf, *pos, *full_name; |
620 | 45 |
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
|
46 full_name = dirtree_path(dir, NULL); |
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
|
47 output_field("filename", full_name); |
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
|
48 fd = xopen(full_name, O_RDONLY); |
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
|
49 free(full_name); |
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
|
50 |
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
|
51 len = fdlength(fd); |
933
a81921ce4930
Patch that assumes that the presence of the string ".ko" indicates
Isaac Dunham <idunham@lavabit.com>
parents:
923
diff
changeset
|
52 if (!(buf = mmap(0, len, PROT_READ, MAP_SHARED, fd, 0))) { |
a81921ce4930
Patch that assumes that the presence of the string ".ko" indicates
Isaac Dunham <idunham@lavabit.com>
parents:
923
diff
changeset
|
53 perror_msg("mmap %s", full_name); |
a81921ce4930
Patch that assumes that the presence of the string ".ko" indicates
Isaac Dunham <idunham@lavabit.com>
parents:
923
diff
changeset
|
54 return 1; |
a81921ce4930
Patch that assumes that the presence of the string ".ko" indicates
Isaac Dunham <idunham@lavabit.com>
parents:
923
diff
changeset
|
55 } |
620 | 56 |
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
|
57 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
|
58 if (*pos) continue; |
620 | 59 |
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
|
60 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
|
61 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
|
62 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
|
63 |
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 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
|
65 output_field(str, pos+len+2); |
620 | 66 } |
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
|
67 } |
620 | 68 |
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
|
69 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
|
70 close(fd); |
933
a81921ce4930
Patch that assumes that the presence of the string ".ko" indicates
Isaac Dunham <idunham@lavabit.com>
parents:
923
diff
changeset
|
71 return 0; |
620 | 72 } |
73 | |
74 static int check_module(struct dirtree *new) | |
75 { | |
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
|
76 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
|
77 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
|
78 |
923
aff9fa1075eb
Upgrade modinfo to support multiple modules, and add tests, from Isaac Dunham.
Rob Landley <rob@landley.net>
parents:
884
diff
changeset
|
79 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
|
80 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
|
81 |
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 // 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
|
83 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
|
84 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
|
85 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
|
86 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
|
87 } |
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
|
88 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
|
89 |
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
|
90 modinfo_file(new); |
620 | 91 } |
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
|
92 } |
620 | 93 |
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
|
94 return dirtree_notdotdot(new); |
620 | 95 } |
96 | |
97 void modinfo_main(void) | |
98 { | |
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
|
99 struct utsname uts; |
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
|
100 |
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
|
101 if (uname(&uts) < 0) perror_exit("bad uname"); |
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
|
102 sprintf(toybuf, "/lib/modules/%s", uts.release); |
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
|
103 |
923
aff9fa1075eb
Upgrade modinfo to support multiple modules, and add tests, from Isaac Dunham.
Rob Landley <rob@landley.net>
parents:
884
diff
changeset
|
104 for(TT.mod = 0; TT.mod<toys.optc; TT.mod++) { |
933
a81921ce4930
Patch that assumes that the presence of the string ".ko" indicates
Isaac Dunham <idunham@lavabit.com>
parents:
923
diff
changeset
|
105 if (strstr(toys.optargs[TT.mod], ".ko")) { |
a81921ce4930
Patch that assumes that the presence of the string ".ko" indicates
Isaac Dunham <idunham@lavabit.com>
parents:
923
diff
changeset
|
106 dirtree_read(toys.optargs[TT.mod], modinfo_file); |
a81921ce4930
Patch that assumes that the presence of the string ".ko" indicates
Isaac Dunham <idunham@lavabit.com>
parents:
923
diff
changeset
|
107 } else { |
a81921ce4930
Patch that assumes that the presence of the string ".ko" indicates
Isaac Dunham <idunham@lavabit.com>
parents:
923
diff
changeset
|
108 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
|
109 } |
923
aff9fa1075eb
Upgrade modinfo to support multiple modules, and add tests, from Isaac Dunham.
Rob Landley <rob@landley.net>
parents:
884
diff
changeset
|
110 } |
620 | 111 } |