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