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 | */ |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 8 | //config:config MODINFO |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 9 | //config: bool "modinfo (24 kb)" |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 10 | //config: default y |
| 11 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 12 | //config: Show information about a Linux Kernel module |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 13 | |
Denys Vlasenko | 3346b4a | 2017-08-04 02:56:39 +0200 | [diff] [blame] | 14 | //applet:IF_MODINFO(APPLET_NOEXEC(modinfo, modinfo, BB_DIR_SBIN, BB_SUID_DROP, modinfo)) |
Denys Vlasenko | 4c8576f | 2015-10-28 18:07:46 +0100 | [diff] [blame] | 15 | |
| 16 | //kbuild:lib-$(CONFIG_MODINFO) += modinfo.o modutils.o |
| 17 | |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 18 | #include <fnmatch.h> |
| 19 | #include <sys/utsname.h> /* uname() */ |
| 20 | #include "libbb.h" |
| 21 | #include "modutils.h" |
| 22 | |
Denys Vlasenko | 965b795 | 2020-11-30 13:03:03 +0100 | [diff] [blame] | 23 | static const char *const shortcuts[] ALIGN_PTR = { |
Denys Vlasenko | 4c8576f | 2015-10-28 18:07:46 +0100 | [diff] [blame] | 24 | "filename", // -n |
| 25 | "author", // -a |
| 26 | "description", // -d |
| 27 | "license", // -l |
| 28 | "parm", // -p |
| 29 | "version", // the rest has no shortcut options |
| 30 | "alias", |
| 31 | "srcversion", |
| 32 | "depends", |
| 33 | "uts_release", |
| 34 | "intree", |
| 35 | "vermagic", |
| 36 | "firmware", |
| 37 | }; |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 38 | |
| 39 | enum { |
Denys Vlasenko | 4c8576f | 2015-10-28 18:07:46 +0100 | [diff] [blame] | 40 | OPT_0 = (1 << 0), /* \0 as separator */ |
| 41 | OPT_F = (1 << 1), /* field name */ |
| 42 | /* first bits are for -nadlp options, the rest are for |
| 43 | * fields not selectable with "shortcut" options |
| 44 | */ |
| 45 | OPT_n = (1 << 2), |
| 46 | OPT_TAGS = ((1 << ARRAY_SIZE(shortcuts)) - 1) << 2, |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 47 | }; |
| 48 | |
Denys Vlasenko | 4c8576f | 2015-10-28 18:07:46 +0100 | [diff] [blame] | 49 | static void display(const char *data, const char *pattern) |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 50 | { |
Denys Vlasenko | 4c8576f | 2015-10-28 18:07:46 +0100 | [diff] [blame] | 51 | int flag = option_mask32 >> 1; /* shift out -0 bit */ |
| 52 | if (flag & (flag-1)) { |
| 53 | /* more than one field to show: print "FIELD:" pfx */ |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 54 | int n = printf("%s:", pattern); |
| 55 | while (n++ < 16) |
| 56 | bb_putchar(' '); |
| 57 | } |
David Marchand | 7d16964 | 2014-07-03 12:24:55 +0200 | [diff] [blame] | 58 | printf("%s%c", data, (option_mask32 & OPT_0) ? '\0' : '\n'); |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 59 | } |
| 60 | |
Lauri Kasanen | 1b14cdb | 2010-06-27 00:35:49 +0200 | [diff] [blame] | 61 | static void modinfo(const char *path, const char *version, |
Denys Vlasenko | 4c8576f | 2015-10-28 18:07:46 +0100 | [diff] [blame] | 62 | const char *field) |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 63 | { |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 64 | size_t len; |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 65 | int j; |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 66 | char *ptr, *the_module; |
Denys Vlasenko | 4c8576f | 2015-10-28 18:07:46 +0100 | [diff] [blame] | 67 | char *allocated; |
| 68 | int tags = option_mask32; |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 69 | |
Denys Vlasenko | 4c8576f | 2015-10-28 18:07:46 +0100 | [diff] [blame] | 70 | allocated = NULL; |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 71 | len = MAXINT(ssize_t); |
| 72 | the_module = xmalloc_open_zipped_read_close(path, &len); |
Lauri Kasanen | 1b14cdb | 2010-06-27 00:35:49 +0200 | [diff] [blame] | 73 | if (!the_module) { |
| 74 | if (path[0] == '/') |
| 75 | return; |
| 76 | /* Newer depmod puts relative paths in modules.dep */ |
Denys Vlasenko | 4c8576f | 2015-10-28 18:07:46 +0100 | [diff] [blame] | 77 | path = allocated = xasprintf("%s/%s/%s", CONFIG_DEFAULT_MODULES_DIR, version, path); |
Lauri Kasanen | 1b14cdb | 2010-06-27 00:35:49 +0200 | [diff] [blame] | 78 | the_module = xmalloc_open_zipped_read_close(path, &len); |
Denys Vlasenko | 4c8576f | 2015-10-28 18:07:46 +0100 | [diff] [blame] | 79 | if (!the_module) { |
| 80 | bb_error_msg("module '%s' not found", path); |
| 81 | goto ret; |
| 82 | } |
Lauri Kasanen | 1b14cdb | 2010-06-27 00:35:49 +0200 | [diff] [blame] | 83 | } |
| 84 | |
Denys Vlasenko | 4c8576f | 2015-10-28 18:07:46 +0100 | [diff] [blame] | 85 | for (j = 1; (1<<j) & (OPT_TAGS|OPT_F); j++) { |
Denys Vlasenko | df7f200 | 2011-02-15 01:45:14 +0100 | [diff] [blame] | 86 | const char *pattern; |
| 87 | |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 88 | if (!((1<<j) & tags)) |
| 89 | continue; |
Denys Vlasenko | 4c8576f | 2015-10-28 18:07:46 +0100 | [diff] [blame] | 90 | |
Denys Vlasenko | df7f200 | 2011-02-15 01:45:14 +0100 | [diff] [blame] | 91 | pattern = field; |
| 92 | if ((1<<j) & OPT_TAGS) |
Denys Vlasenko | 4c8576f | 2015-10-28 18:07:46 +0100 | [diff] [blame] | 93 | pattern = shortcuts[j-2]; |
| 94 | |
| 95 | if (strcmp(pattern, shortcuts[0]) == 0) { |
| 96 | /* "-n" or "-F filename" */ |
| 97 | display(path, shortcuts[0]); |
| 98 | continue; |
| 99 | } |
| 100 | |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 101 | ptr = the_module; |
| 102 | while (1) { |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 103 | char *after_pattern; |
| 104 | |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 105 | ptr = memchr(ptr, *pattern, len - (ptr - (char*)the_module)); |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 106 | if (ptr == NULL) /* no occurrence left, done */ |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 107 | break; |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 108 | after_pattern = is_prefixed_with(ptr, pattern); |
| 109 | if (after_pattern && *after_pattern == '=') { |
Tanguy Pruvot | 772f17a | 2012-05-30 08:00:46 +0200 | [diff] [blame] | 110 | /* field prefixes are 0x80 or 0x00 */ |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 111 | if ((ptr[-1] & 0x7F) == 0x00) { |
| 112 | ptr = after_pattern + 1; |
Denys Vlasenko | 4c8576f | 2015-10-28 18:07:46 +0100 | [diff] [blame] | 113 | display(ptr, pattern); |
David Marchand | 7d16964 | 2014-07-03 12:24:55 +0200 | [diff] [blame] | 114 | ptr += strlen(ptr); |
Tanguy Pruvot | 772f17a | 2012-05-30 08:00:46 +0200 | [diff] [blame] | 115 | } |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 116 | } |
| 117 | ++ptr; |
| 118 | } |
| 119 | } |
| 120 | free(the_module); |
Denys Vlasenko | 4c8576f | 2015-10-28 18:07:46 +0100 | [diff] [blame] | 121 | ret: |
| 122 | free(allocated); |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | //usage:#define modinfo_trivial_usage |
Denys Vlasenko | 4c8576f | 2015-10-28 18:07:46 +0100 | [diff] [blame] | 126 | //usage: "[-adlpn0] [-F keyword] MODULE" |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 127 | //usage:#define modinfo_full_usage "\n\n" |
Denys Vlasenko | 6642676 | 2011-06-05 03:58:28 +0200 | [diff] [blame] | 128 | //usage: " -a Shortcut for '-F author'" |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 129 | //usage: "\n -d Shortcut for '-F description'" |
| 130 | //usage: "\n -l Shortcut for '-F license'" |
| 131 | //usage: "\n -p Shortcut for '-F parm'" |
Denys Vlasenko | 4c8576f | 2015-10-28 18:07:46 +0100 | [diff] [blame] | 132 | ////usage: "\n -n Shortcut for '-F filename'" |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 133 | //usage: "\n -F keyword Keyword to look for" |
Denys Vlasenko | 894466c | 2021-06-17 00:36:13 +0200 | [diff] [blame] | 134 | //usage: "\n -0 NUL terminated output" |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 135 | //usage:#define modinfo_example_usage |
| 136 | //usage: "$ modinfo -F vermagic loop\n" |
| 137 | |
| 138 | int modinfo_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 139 | int modinfo_main(int argc UNUSED_PARAM, char **argv) |
| 140 | { |
Denys Vlasenko | 4c8576f | 2015-10-28 18:07:46 +0100 | [diff] [blame] | 141 | const char *field; |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 142 | char name[MODULE_NAME_LEN]; |
| 143 | struct utsname uts; |
Lauri Kasanen | 1b14cdb | 2010-06-27 00:35:49 +0200 | [diff] [blame] | 144 | parser_t *parser; |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 145 | char *colon, *tokens[2]; |
| 146 | unsigned opts; |
| 147 | unsigned i; |
| 148 | |
Denys Vlasenko | 4c8576f | 2015-10-28 18:07:46 +0100 | [diff] [blame] | 149 | field = NULL; |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 150 | opts = getopt32(argv, "^" "0F:nadlp" "\0" "-1"/*minimum one arg*/, &field); |
Denys Vlasenko | 4c8576f | 2015-10-28 18:07:46 +0100 | [diff] [blame] | 151 | /* If no field selected, show all */ |
| 152 | if (!(opts & (OPT_TAGS|OPT_F))) |
| 153 | option_mask32 |= OPT_TAGS; |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 154 | argv += optind; |
| 155 | |
| 156 | uname(&uts); |
Lauri Kasanen | 1b14cdb | 2010-06-27 00:35:49 +0200 | [diff] [blame] | 157 | parser = config_open2( |
| 158 | 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] | 159 | xfopen_for_read |
| 160 | ); |
| 161 | |
Lauri Kasanen | 1b14cdb | 2010-06-27 00:35:49 +0200 | [diff] [blame] | 162 | while (config_read(parser, tokens, 2, 1, "# \t", PARSE_NORMAL)) { |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 163 | colon = last_char_is(tokens[0], ':'); |
| 164 | if (colon == NULL) |
| 165 | continue; |
| 166 | *colon = '\0'; |
Denys Vlasenko | a88db5c | 2015-02-21 17:08:35 +0100 | [diff] [blame] | 167 | filename2modname(bb_basename(tokens[0]), name); |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 168 | for (i = 0; argv[i]; i++) { |
| 169 | if (fnmatch(argv[i], name, 0) == 0) { |
Denys Vlasenko | 4c8576f | 2015-10-28 18:07:46 +0100 | [diff] [blame] | 170 | modinfo(tokens[0], uts.release, field); |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 171 | argv[i] = (char *) ""; |
| 172 | } |
| 173 | } |
| 174 | } |
Lauri Kasanen | 1b14cdb | 2010-06-27 00:35:49 +0200 | [diff] [blame] | 175 | if (ENABLE_FEATURE_CLEAN_UP) |
| 176 | config_close(parser); |
| 177 | |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 178 | for (i = 0; argv[i]; i++) { |
| 179 | if (argv[i][0]) { |
Denys Vlasenko | 4c8576f | 2015-10-28 18:07:46 +0100 | [diff] [blame] | 180 | modinfo(argv[i], uts.release, field); |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 181 | } |
| 182 | } |
Lauri Kasanen | 1b14cdb | 2010-06-27 00:35:49 +0200 | [diff] [blame] | 183 | |
Pascal Bellard | 22bdf90 | 2010-06-06 04:55:13 +0200 | [diff] [blame] | 184 | return 0; |
| 185 | } |