diff options
| author | Denys Vlasenko <vda.linux@googlemail.com> | 2015-10-28 18:07:46 +0100 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2015-10-28 18:07:46 +0100 |
| commit | 4c8576fea18552d93d43c367638bacc54c26f2b7 (patch) | |
| tree | 9eae249540b7a5c61d4e13337008abda4ebef5b8 /modutils | |
| parent | 4840325351911780a02f953f17b2eee919a36340 (diff) | |
| download | busybox-w32-4c8576fea18552d93d43c367638bacc54c26f2b7.tar.gz busybox-w32-4c8576fea18552d93d43c367638bacc54c26f2b7.tar.bz2 busybox-w32-4c8576fea18552d93d43c367638bacc54c26f2b7.zip | |
modinfo: fix "-F firmware", add "intree" field display
function old new delta
shortcuts - 52 +52
modinfo 317 330 +13
display 77 87 +10
packed_usage 30752 30761 +9
modinfo_main 351 345 -6
static.shortcuts 48 - -48
------------------------------------------------------------------------------
(add/remove: 1/1 grow/shrink: 3/1 up/down: 84/-54) Total: 30 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'modutils')
| -rw-r--r-- | modutils/modinfo.c | 114 |
1 files changed, 62 insertions, 52 deletions
diff --git a/modutils/modinfo.c b/modutils/modinfo.c index 8e74b6438..aa641ad54 100644 --- a/modutils/modinfo.c +++ b/modutils/modinfo.c | |||
| @@ -5,11 +5,6 @@ | |||
| 5 | * | 5 | * |
| 6 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. | 6 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
| 7 | */ | 7 | */ |
| 8 | |||
| 9 | //applet:IF_MODINFO(APPLET(modinfo, BB_DIR_SBIN, BB_SUID_DROP)) | ||
| 10 | |||
| 11 | //kbuild:lib-$(CONFIG_MODINFO) += modinfo.o modutils.o | ||
| 12 | |||
| 13 | //config:config MODINFO | 8 | //config:config MODINFO |
| 14 | //config: bool "modinfo" | 9 | //config: bool "modinfo" |
| 15 | //config: default y | 10 | //config: default y |
| @@ -17,26 +12,46 @@ | |||
| 17 | //config: help | 12 | //config: help |
| 18 | //config: Show information about a Linux Kernel module | 13 | //config: Show information about a Linux Kernel module |
| 19 | 14 | ||
| 15 | //applet:IF_MODINFO(APPLET(modinfo, BB_DIR_SBIN, BB_SUID_DROP)) | ||
| 16 | |||
| 17 | //kbuild:lib-$(CONFIG_MODINFO) += modinfo.o modutils.o | ||
| 18 | |||
| 20 | #include <fnmatch.h> | 19 | #include <fnmatch.h> |
| 21 | #include <sys/utsname.h> /* uname() */ | 20 | #include <sys/utsname.h> /* uname() */ |
| 22 | #include "libbb.h" | 21 | #include "libbb.h" |
| 23 | #include "modutils.h" | 22 | #include "modutils.h" |
| 24 | 23 | ||
| 25 | 24 | static const char *const shortcuts[] = { | |
| 26 | enum { | 25 | "filename", // -n |
| 27 | OPT_TAGS = (1 << 12) - 1, /* shortcut count */ | 26 | "author", // -a |
| 28 | OPT_F = (1 << 12), /* field name */ | 27 | "description", // -d |
| 29 | OPT_0 = (1 << 13), /* \0 as separator */ | 28 | "license", // -l |
| 29 | "parm", // -p | ||
| 30 | "version", // the rest has no shortcut options | ||
| 31 | "alias", | ||
| 32 | "srcversion", | ||
| 33 | "depends", | ||
| 34 | "uts_release", | ||
| 35 | "intree", | ||
| 36 | "vermagic", | ||
| 37 | "firmware", | ||
| 30 | }; | 38 | }; |
| 31 | 39 | ||
| 32 | struct modinfo_env { | 40 | enum { |
| 33 | char *field; | 41 | OPT_0 = (1 << 0), /* \0 as separator */ |
| 34 | int tags; | 42 | OPT_F = (1 << 1), /* field name */ |
| 43 | /* first bits are for -nadlp options, the rest are for | ||
| 44 | * fields not selectable with "shortcut" options | ||
| 45 | */ | ||
| 46 | OPT_n = (1 << 2), | ||
| 47 | OPT_TAGS = ((1 << ARRAY_SIZE(shortcuts)) - 1) << 2, | ||
| 35 | }; | 48 | }; |
| 36 | 49 | ||
| 37 | static void display(const char *data, const char *pattern, int flag) | 50 | static void display(const char *data, const char *pattern) |
| 38 | { | 51 | { |
| 39 | if (flag) { | 52 | int flag = option_mask32 >> 1; /* shift out -0 bit */ |
| 53 | if (flag & (flag-1)) { | ||
| 54 | /* more than one field to show: print "FIELD:" pfx */ | ||
| 40 | int n = printf("%s:", pattern); | 55 | int n = printf("%s:", pattern); |
| 41 | while (n++ < 16) | 56 | while (n++ < 16) |
| 42 | bb_putchar(' '); | 57 | bb_putchar(' '); |
| @@ -45,55 +60,45 @@ static void display(const char *data, const char *pattern, int flag) | |||
| 45 | } | 60 | } |
| 46 | 61 | ||
| 47 | static void modinfo(const char *path, const char *version, | 62 | static void modinfo(const char *path, const char *version, |
| 48 | const struct modinfo_env *env) | 63 | const char *field) |
| 49 | { | 64 | { |
| 50 | static const char *const shortcuts[] = { | ||
| 51 | "filename", | ||
| 52 | "license", | ||
| 53 | "author", | ||
| 54 | "description", | ||
| 55 | "version", | ||
| 56 | "alias", | ||
| 57 | "srcversion", | ||
| 58 | "depends", | ||
| 59 | "uts_release", | ||
| 60 | "vermagic", | ||
| 61 | "parm", | ||
| 62 | "firmware", | ||
| 63 | }; | ||
| 64 | size_t len; | 65 | size_t len; |
| 65 | int j; | 66 | int j; |
| 66 | char *ptr, *the_module; | 67 | char *ptr, *the_module; |
| 67 | const char *field = env->field; | 68 | char *allocated; |
| 68 | int tags = env->tags; | 69 | int tags = option_mask32; |
| 69 | |||
| 70 | if (tags & 1) { /* filename */ | ||
| 71 | display(path, shortcuts[0], 1 != tags); | ||
| 72 | } | ||
| 73 | 70 | ||
| 71 | allocated = NULL; | ||
| 74 | len = MAXINT(ssize_t); | 72 | len = MAXINT(ssize_t); |
| 75 | the_module = xmalloc_open_zipped_read_close(path, &len); | 73 | the_module = xmalloc_open_zipped_read_close(path, &len); |
| 76 | if (!the_module) { | 74 | if (!the_module) { |
| 77 | if (path[0] == '/') | 75 | if (path[0] == '/') |
| 78 | return; | 76 | return; |
| 79 | /* Newer depmod puts relative paths in modules.dep */ | 77 | /* Newer depmod puts relative paths in modules.dep */ |
| 80 | path = xasprintf("%s/%s/%s", CONFIG_DEFAULT_MODULES_DIR, version, path); | 78 | path = allocated = xasprintf("%s/%s/%s", CONFIG_DEFAULT_MODULES_DIR, version, path); |
| 81 | the_module = xmalloc_open_zipped_read_close(path, &len); | 79 | the_module = xmalloc_open_zipped_read_close(path, &len); |
| 82 | free((char*)path); | 80 | if (!the_module) { |
| 83 | if (!the_module) | 81 | bb_error_msg("module '%s' not found", path); |
| 84 | return; | 82 | goto ret; |
| 83 | } | ||
| 85 | } | 84 | } |
| 86 | 85 | ||
| 87 | if (field) | 86 | for (j = 1; (1<<j) & (OPT_TAGS|OPT_F); j++) { |
| 88 | tags |= OPT_F; | ||
| 89 | for (j = 1; (1<<j) & (OPT_TAGS + OPT_F); j++) { | ||
| 90 | const char *pattern; | 87 | const char *pattern; |
| 91 | 88 | ||
| 92 | if (!((1<<j) & tags)) | 89 | if (!((1<<j) & tags)) |
| 93 | continue; | 90 | continue; |
| 91 | |||
| 94 | pattern = field; | 92 | pattern = field; |
| 95 | if ((1<<j) & OPT_TAGS) | 93 | if ((1<<j) & OPT_TAGS) |
| 96 | pattern = shortcuts[j]; | 94 | pattern = shortcuts[j-2]; |
| 95 | |||
| 96 | if (strcmp(pattern, shortcuts[0]) == 0) { | ||
| 97 | /* "-n" or "-F filename" */ | ||
| 98 | display(path, shortcuts[0]); | ||
| 99 | continue; | ||
| 100 | } | ||
| 101 | |||
| 97 | ptr = the_module; | 102 | ptr = the_module; |
| 98 | while (1) { | 103 | while (1) { |
| 99 | char *after_pattern; | 104 | char *after_pattern; |
| @@ -106,7 +111,7 @@ static void modinfo(const char *path, const char *version, | |||
| 106 | /* field prefixes are 0x80 or 0x00 */ | 111 | /* field prefixes are 0x80 or 0x00 */ |
| 107 | if ((ptr[-1] & 0x7F) == 0x00) { | 112 | if ((ptr[-1] & 0x7F) == 0x00) { |
| 108 | ptr = after_pattern + 1; | 113 | ptr = after_pattern + 1; |
| 109 | display(ptr, pattern, (1<<j) != tags); | 114 | display(ptr, pattern); |
| 110 | ptr += strlen(ptr); | 115 | ptr += strlen(ptr); |
| 111 | } | 116 | } |
| 112 | } | 117 | } |
| @@ -114,15 +119,18 @@ static void modinfo(const char *path, const char *version, | |||
| 114 | } | 119 | } |
| 115 | } | 120 | } |
| 116 | free(the_module); | 121 | free(the_module); |
| 122 | ret: | ||
| 123 | free(allocated); | ||
| 117 | } | 124 | } |
| 118 | 125 | ||
| 119 | //usage:#define modinfo_trivial_usage | 126 | //usage:#define modinfo_trivial_usage |
| 120 | //usage: "[-adlp0] [-F keyword] MODULE" | 127 | //usage: "[-adlpn0] [-F keyword] MODULE" |
| 121 | //usage:#define modinfo_full_usage "\n\n" | 128 | //usage:#define modinfo_full_usage "\n\n" |
| 122 | //usage: " -a Shortcut for '-F author'" | 129 | //usage: " -a Shortcut for '-F author'" |
| 123 | //usage: "\n -d Shortcut for '-F description'" | 130 | //usage: "\n -d Shortcut for '-F description'" |
| 124 | //usage: "\n -l Shortcut for '-F license'" | 131 | //usage: "\n -l Shortcut for '-F license'" |
| 125 | //usage: "\n -p Shortcut for '-F parm'" | 132 | //usage: "\n -p Shortcut for '-F parm'" |
| 133 | ////usage: "\n -n Shortcut for '-F filename'" | ||
| 126 | //usage: "\n -F keyword Keyword to look for" | 134 | //usage: "\n -F keyword Keyword to look for" |
| 127 | //usage: "\n -0 Separate output with NULs" | 135 | //usage: "\n -0 Separate output with NULs" |
| 128 | //usage:#define modinfo_example_usage | 136 | //usage:#define modinfo_example_usage |
| @@ -131,7 +139,7 @@ static void modinfo(const char *path, const char *version, | |||
| 131 | int modinfo_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 139 | int modinfo_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 132 | int modinfo_main(int argc UNUSED_PARAM, char **argv) | 140 | int modinfo_main(int argc UNUSED_PARAM, char **argv) |
| 133 | { | 141 | { |
| 134 | struct modinfo_env env; | 142 | const char *field; |
| 135 | char name[MODULE_NAME_LEN]; | 143 | char name[MODULE_NAME_LEN]; |
| 136 | struct utsname uts; | 144 | struct utsname uts; |
| 137 | parser_t *parser; | 145 | parser_t *parser; |
| @@ -139,10 +147,12 @@ int modinfo_main(int argc UNUSED_PARAM, char **argv) | |||
| 139 | unsigned opts; | 147 | unsigned opts; |
| 140 | unsigned i; | 148 | unsigned i; |
| 141 | 149 | ||
| 142 | env.field = NULL; | 150 | field = NULL; |
| 143 | opt_complementary = "-1"; /* minimum one param */ | 151 | opt_complementary = "-1"; /* minimum one param */ |
| 144 | opts = getopt32(argv, "nladvAsDumpF:0", &env.field); | 152 | opts = getopt32(argv, "0F:nadlp", &field); |
| 145 | env.tags = opts & OPT_TAGS ? opts & OPT_TAGS : OPT_TAGS; | 153 | /* If no field selected, show all */ |
| 154 | if (!(opts & (OPT_TAGS|OPT_F))) | ||
| 155 | option_mask32 |= OPT_TAGS; | ||
| 146 | argv += optind; | 156 | argv += optind; |
| 147 | 157 | ||
| 148 | uname(&uts); | 158 | uname(&uts); |
| @@ -159,7 +169,7 @@ int modinfo_main(int argc UNUSED_PARAM, char **argv) | |||
| 159 | filename2modname(bb_basename(tokens[0]), name); | 169 | filename2modname(bb_basename(tokens[0]), name); |
| 160 | for (i = 0; argv[i]; i++) { | 170 | for (i = 0; argv[i]; i++) { |
| 161 | if (fnmatch(argv[i], name, 0) == 0) { | 171 | if (fnmatch(argv[i], name, 0) == 0) { |
| 162 | modinfo(tokens[0], uts.release, &env); | 172 | modinfo(tokens[0], uts.release, field); |
| 163 | argv[i] = (char *) ""; | 173 | argv[i] = (char *) ""; |
| 164 | } | 174 | } |
| 165 | } | 175 | } |
| @@ -169,7 +179,7 @@ int modinfo_main(int argc UNUSED_PARAM, char **argv) | |||
| 169 | 179 | ||
| 170 | for (i = 0; argv[i]; i++) { | 180 | for (i = 0; argv[i]; i++) { |
| 171 | if (argv[i][0]) { | 181 | if (argv[i][0]) { |
| 172 | modinfo(argv[i], uts.release, &env); | 182 | modinfo(argv[i], uts.release, field); |
| 173 | } | 183 | } |
| 174 | } | 184 | } |
| 175 | 185 | ||
