Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * modinfo - retrieve module info |
| 4 | * Copyright (c) 2008 Pascal Bellard |
| 5 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 6 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
Denys Vlasenko | b9f2d9f | 2011-01-18 13:58:01 +0100 | [diff] [blame] | 9 | //applet:IF_MODINFO(APPLET(modinfo, BB_DIR_SBIN, BB_SUID_DROP)) |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 10 | |
Denys Vlasenko | 7fdf5a8 | 2010-06-09 10:18:35 +0200 | [diff] [blame] | 11 | //kbuild:lib-$(CONFIG_MODINFO) += modinfo.o modutils.o |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 12 | |
| 13 | //config:config MODINFO |
| 14 | //config: bool "modinfo" |
| 15 | //config: default y |
Denys Vlasenko | b076193 | 2011-10-30 22:07:36 +0100 | [diff] [blame] | 16 | //config: select PLATFORM_LINUX |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 17 | //config: help |
| 18 | //config: Show information about a Linux Kernel module |
| 19 | |
| 20 | #include <fnmatch.h> |
| 21 | #include <sys/utsname.h> /* uname() */ |
| 22 | #include "libbb.h" |
| 23 | #include "modutils.h" |
| 24 | |
| 25 | |
| 26 | enum { |
Tanguy Pruvot | 772f17a | 2012-05-30 08:00:46 +0200 | [diff] [blame] | 27 | OPT_TAGS = (1 << 12) - 1, /* shortcut count */ |
| 28 | OPT_F = (1 << 12), /* field name */ |
| 29 | OPT_0 = (1 << 13), /* \0 as separator */ |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | struct modinfo_env { |
| 33 | char *field; |
| 34 | int tags; |
| 35 | }; |
| 36 | |
David Marchand | 7d16964 | 2014-07-03 12:24:55 +0200 | [diff] [blame] | 37 | static void display(const char *data, const char *pattern, int flag) |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 38 | { |
| 39 | if (flag) { |
| 40 | int n = printf("%s:", pattern); |
| 41 | while (n++ < 16) |
| 42 | bb_putchar(' '); |
| 43 | } |
David Marchand | 7d16964 | 2014-07-03 12:24:55 +0200 | [diff] [blame] | 44 | printf("%s%c", data, (option_mask32 & OPT_0) ? '\0' : '\n'); |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 45 | } |
| 46 | |
Lauri Kasanen | 1b14cdb | 2010-06-27 00:35:49 +0200 | [diff] [blame] | 47 | static void modinfo(const char *path, const char *version, |
Denys Vlasenko | df7f200 | 2011-02-15 01:45:14 +0100 | [diff] [blame] | 48 | const struct modinfo_env *env) |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 49 | { |
| 50 | static const char *const shortcuts[] = { |
| 51 | "filename", |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 52 | "license", |
Tanguy Pruvot | 772f17a | 2012-05-30 08:00:46 +0200 | [diff] [blame] | 53 | "author", |
| 54 | "description", |
| 55 | "version", |
| 56 | "alias", |
| 57 | "srcversion", |
| 58 | "depends", |
| 59 | "uts_release", |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 60 | "vermagic", |
| 61 | "parm", |
Lauri Kasanen | 12f44ea | 2011-02-14 19:50:30 +0200 | [diff] [blame] | 62 | "firmware", |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 63 | }; |
| 64 | size_t len; |
| 65 | int j, length; |
| 66 | char *ptr, *the_module; |
| 67 | const char *field = env->field; |
| 68 | int tags = env->tags; |
| 69 | |
| 70 | if (tags & 1) { /* filename */ |
| 71 | display(path, shortcuts[0], 1 != tags); |
| 72 | } |
Lauri Kasanen | 1b14cdb | 2010-06-27 00:35:49 +0200 | [diff] [blame] | 73 | |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 74 | len = MAXINT(ssize_t); |
| 75 | the_module = xmalloc_open_zipped_read_close(path, &len); |
Lauri Kasanen | 1b14cdb | 2010-06-27 00:35:49 +0200 | [diff] [blame] | 76 | if (!the_module) { |
| 77 | if (path[0] == '/') |
| 78 | return; |
| 79 | /* Newer depmod puts relative paths in modules.dep */ |
| 80 | path = xasprintf("%s/%s/%s", CONFIG_DEFAULT_MODULES_DIR, version, path); |
| 81 | the_module = xmalloc_open_zipped_read_close(path, &len); |
| 82 | free((char*)path); |
| 83 | if (!the_module) |
| 84 | return; |
| 85 | } |
| 86 | |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 87 | if (field) |
| 88 | tags |= OPT_F; |
| 89 | for (j = 1; (1<<j) & (OPT_TAGS + OPT_F); j++) { |
Denys Vlasenko | df7f200 | 2011-02-15 01:45:14 +0100 | [diff] [blame] | 90 | const char *pattern; |
| 91 | |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 92 | if (!((1<<j) & tags)) |
| 93 | continue; |
Denys Vlasenko | df7f200 | 2011-02-15 01:45:14 +0100 | [diff] [blame] | 94 | pattern = field; |
| 95 | if ((1<<j) & OPT_TAGS) |
| 96 | pattern = shortcuts[j]; |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 97 | length = strlen(pattern); |
| 98 | ptr = the_module; |
| 99 | while (1) { |
| 100 | ptr = memchr(ptr, *pattern, len - (ptr - (char*)the_module)); |
| 101 | if (ptr == NULL) /* no occurance left, done */ |
| 102 | break; |
| 103 | if (strncmp(ptr, pattern, length) == 0 && ptr[length] == '=') { |
Tanguy Pruvot | 772f17a | 2012-05-30 08:00:46 +0200 | [diff] [blame] | 104 | /* field prefixes are 0x80 or 0x00 */ |
| 105 | if ((ptr[-1] & 0x7F) == '\0') { |
| 106 | ptr += length + 1; |
David Marchand | 7d16964 | 2014-07-03 12:24:55 +0200 | [diff] [blame] | 107 | display(ptr, pattern, (1<<j) != tags); |
| 108 | ptr += strlen(ptr); |
Tanguy Pruvot | 772f17a | 2012-05-30 08:00:46 +0200 | [diff] [blame] | 109 | } |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 110 | } |
| 111 | ++ptr; |
| 112 | } |
| 113 | } |
| 114 | free(the_module); |
| 115 | } |
| 116 | |
| 117 | //usage:#define modinfo_trivial_usage |
| 118 | //usage: "[-adlp0] [-F keyword] MODULE" |
| 119 | //usage:#define modinfo_full_usage "\n\n" |
Denys Vlasenko | 6642676 | 2011-06-05 03:58:28 +0200 | [diff] [blame] | 120 | //usage: " -a Shortcut for '-F author'" |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 121 | //usage: "\n -d Shortcut for '-F description'" |
| 122 | //usage: "\n -l Shortcut for '-F license'" |
| 123 | //usage: "\n -p Shortcut for '-F parm'" |
| 124 | //usage: "\n -F keyword Keyword to look for" |
| 125 | //usage: "\n -0 Separate output with NULs" |
| 126 | //usage:#define modinfo_example_usage |
| 127 | //usage: "$ modinfo -F vermagic loop\n" |
| 128 | |
| 129 | int modinfo_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 130 | int modinfo_main(int argc UNUSED_PARAM, char **argv) |
| 131 | { |
| 132 | struct modinfo_env env; |
| 133 | char name[MODULE_NAME_LEN]; |
| 134 | struct utsname uts; |
Lauri Kasanen | 1b14cdb | 2010-06-27 00:35:49 +0200 | [diff] [blame] | 135 | parser_t *parser; |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 136 | char *colon, *tokens[2]; |
| 137 | unsigned opts; |
| 138 | unsigned i; |
| 139 | |
| 140 | env.field = NULL; |
| 141 | opt_complementary = "-1"; /* minimum one param */ |
Tanguy Pruvot | 772f17a | 2012-05-30 08:00:46 +0200 | [diff] [blame] | 142 | opts = getopt32(argv, "nladvAsDumpF:0", &env.field); |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 143 | env.tags = opts & OPT_TAGS ? opts & OPT_TAGS : OPT_TAGS; |
| 144 | argv += optind; |
| 145 | |
| 146 | uname(&uts); |
Lauri Kasanen | 1b14cdb | 2010-06-27 00:35:49 +0200 | [diff] [blame] | 147 | parser = config_open2( |
| 148 | xasprintf("%s/%s/%s", CONFIG_DEFAULT_MODULES_DIR, uts.release, CONFIG_DEFAULT_DEPMOD_FILE), |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 149 | xfopen_for_read |
| 150 | ); |
| 151 | |
Lauri Kasanen | 1b14cdb | 2010-06-27 00:35:49 +0200 | [diff] [blame] | 152 | while (config_read(parser, tokens, 2, 1, "# \t", PARSE_NORMAL)) { |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 153 | colon = last_char_is(tokens[0], ':'); |
| 154 | if (colon == NULL) |
| 155 | continue; |
| 156 | *colon = '\0'; |
| 157 | filename2modname(tokens[0], name); |
| 158 | for (i = 0; argv[i]; i++) { |
| 159 | if (fnmatch(argv[i], name, 0) == 0) { |
Lauri Kasanen | 1b14cdb | 2010-06-27 00:35:49 +0200 | [diff] [blame] | 160 | modinfo(tokens[0], uts.release, &env); |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 161 | argv[i] = (char *) ""; |
| 162 | } |
| 163 | } |
| 164 | } |
Lauri Kasanen | 1b14cdb | 2010-06-27 00:35:49 +0200 | [diff] [blame] | 165 | if (ENABLE_FEATURE_CLEAN_UP) |
| 166 | config_close(parser); |
| 167 | |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 168 | for (i = 0; argv[i]; i++) { |
| 169 | if (argv[i][0]) { |
Lauri Kasanen | 1b14cdb | 2010-06-27 00:35:49 +0200 | [diff] [blame] | 170 | modinfo(argv[i], uts.release, &env); |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 171 | } |
| 172 | } |
Lauri Kasanen | 1b14cdb | 2010-06-27 00:35:49 +0200 | [diff] [blame] | 173 | |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 174 | return 0; |
| 175 | } |