blob: 3f91622a94260699f29888e8c71a2f10228f5b52 [file] [log] [blame]
Pascal Bellard22bdf902010-06-06 04:55:13 +02001/* vi: set sw=4 ts=4: */
2/*
3 * modinfo - retrieve module info
4 * Copyright (c) 2008 Pascal Bellard
5 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02006 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Pascal Bellard22bdf902010-06-06 04:55:13 +02007 */
Pascal Bellard22bdf902010-06-06 04:55:13 +02008//config:config MODINFO
Denys Vlasenko4eed2c62017-07-18 22:01:24 +02009//config: bool "modinfo (25 kb)"
Pascal Bellard22bdf902010-06-06 04:55:13 +020010//config: default y
Denys Vlasenkob0761932011-10-30 22:07:36 +010011//config: select PLATFORM_LINUX
Pascal Bellard22bdf902010-06-06 04:55:13 +020012//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: Show information about a Linux Kernel module
Pascal Bellard22bdf902010-06-06 04:55:13 +020014
Denys Vlasenko3346b4a2017-08-04 02:56:39 +020015//applet:IF_MODINFO(APPLET_NOEXEC(modinfo, modinfo, BB_DIR_SBIN, BB_SUID_DROP, modinfo))
Denys Vlasenko4c8576f2015-10-28 18:07:46 +010016
17//kbuild:lib-$(CONFIG_MODINFO) += modinfo.o modutils.o
18
Pascal Bellard22bdf902010-06-06 04:55:13 +020019#include <fnmatch.h>
20#include <sys/utsname.h> /* uname() */
21#include "libbb.h"
22#include "modutils.h"
23
Denys Vlasenko4c8576f2015-10-28 18:07:46 +010024static const char *const shortcuts[] = {
25 "filename", // -n
26 "author", // -a
27 "description", // -d
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",
38};
Pascal Bellard22bdf902010-06-06 04:55:13 +020039
40enum {
Denys Vlasenko4c8576f2015-10-28 18:07:46 +010041 OPT_0 = (1 << 0), /* \0 as separator */
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,
Pascal Bellard22bdf902010-06-06 04:55:13 +020048};
49
Denys Vlasenko4c8576f2015-10-28 18:07:46 +010050static void display(const char *data, const char *pattern)
Pascal Bellard22bdf902010-06-06 04:55:13 +020051{
Denys Vlasenko4c8576f2015-10-28 18:07:46 +010052 int flag = option_mask32 >> 1; /* shift out -0 bit */
53 if (flag & (flag-1)) {
54 /* more than one field to show: print "FIELD:" pfx */
Pascal Bellard22bdf902010-06-06 04:55:13 +020055 int n = printf("%s:", pattern);
56 while (n++ < 16)
57 bb_putchar(' ');
58 }
David Marchand7d169642014-07-03 12:24:55 +020059 printf("%s%c", data, (option_mask32 & OPT_0) ? '\0' : '\n');
Pascal Bellard22bdf902010-06-06 04:55:13 +020060}
61
Lauri Kasanen1b14cdb2010-06-27 00:35:49 +020062static void modinfo(const char *path, const char *version,
Denys Vlasenko4c8576f2015-10-28 18:07:46 +010063 const char *field)
Pascal Bellard22bdf902010-06-06 04:55:13 +020064{
Pascal Bellard22bdf902010-06-06 04:55:13 +020065 size_t len;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +010066 int j;
Pascal Bellard22bdf902010-06-06 04:55:13 +020067 char *ptr, *the_module;
Denys Vlasenko4c8576f2015-10-28 18:07:46 +010068 char *allocated;
69 int tags = option_mask32;
Pascal Bellard22bdf902010-06-06 04:55:13 +020070
Denys Vlasenko4c8576f2015-10-28 18:07:46 +010071 allocated = NULL;
Pascal Bellard22bdf902010-06-06 04:55:13 +020072 len = MAXINT(ssize_t);
73 the_module = xmalloc_open_zipped_read_close(path, &len);
Lauri Kasanen1b14cdb2010-06-27 00:35:49 +020074 if (!the_module) {
75 if (path[0] == '/')
76 return;
77 /* Newer depmod puts relative paths in modules.dep */
Denys Vlasenko4c8576f2015-10-28 18:07:46 +010078 path = allocated = xasprintf("%s/%s/%s", CONFIG_DEFAULT_MODULES_DIR, version, path);
Lauri Kasanen1b14cdb2010-06-27 00:35:49 +020079 the_module = xmalloc_open_zipped_read_close(path, &len);
Denys Vlasenko4c8576f2015-10-28 18:07:46 +010080 if (!the_module) {
81 bb_error_msg("module '%s' not found", path);
82 goto ret;
83 }
Lauri Kasanen1b14cdb2010-06-27 00:35:49 +020084 }
85
Denys Vlasenko4c8576f2015-10-28 18:07:46 +010086 for (j = 1; (1<<j) & (OPT_TAGS|OPT_F); j++) {
Denys Vlasenkodf7f2002011-02-15 01:45:14 +010087 const char *pattern;
88
Pascal Bellard22bdf902010-06-06 04:55:13 +020089 if (!((1<<j) & tags))
90 continue;
Denys Vlasenko4c8576f2015-10-28 18:07:46 +010091
Denys Vlasenkodf7f2002011-02-15 01:45:14 +010092 pattern = field;
93 if ((1<<j) & OPT_TAGS)
Denys Vlasenko4c8576f2015-10-28 18:07:46 +010094 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
Pascal Bellard22bdf902010-06-06 04:55:13 +0200102 ptr = the_module;
103 while (1) {
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100104 char *after_pattern;
105
Pascal Bellard22bdf902010-06-06 04:55:13 +0200106 ptr = memchr(ptr, *pattern, len - (ptr - (char*)the_module));
Denys Vlasenko10ad6222017-04-17 16:13:32 +0200107 if (ptr == NULL) /* no occurrence left, done */
Pascal Bellard22bdf902010-06-06 04:55:13 +0200108 break;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100109 after_pattern = is_prefixed_with(ptr, pattern);
110 if (after_pattern && *after_pattern == '=') {
Tanguy Pruvot772f17a2012-05-30 08:00:46 +0200111 /* field prefixes are 0x80 or 0x00 */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100112 if ((ptr[-1] & 0x7F) == 0x00) {
113 ptr = after_pattern + 1;
Denys Vlasenko4c8576f2015-10-28 18:07:46 +0100114 display(ptr, pattern);
David Marchand7d169642014-07-03 12:24:55 +0200115 ptr += strlen(ptr);
Tanguy Pruvot772f17a2012-05-30 08:00:46 +0200116 }
Pascal Bellard22bdf902010-06-06 04:55:13 +0200117 }
118 ++ptr;
119 }
120 }
121 free(the_module);
Denys Vlasenko4c8576f2015-10-28 18:07:46 +0100122 ret:
123 free(allocated);
Pascal Bellard22bdf902010-06-06 04:55:13 +0200124}
125
126//usage:#define modinfo_trivial_usage
Denys Vlasenko4c8576f2015-10-28 18:07:46 +0100127//usage: "[-adlpn0] [-F keyword] MODULE"
Pascal Bellard22bdf902010-06-06 04:55:13 +0200128//usage:#define modinfo_full_usage "\n\n"
Denys Vlasenko66426762011-06-05 03:58:28 +0200129//usage: " -a Shortcut for '-F author'"
Pascal Bellard22bdf902010-06-06 04:55:13 +0200130//usage: "\n -d Shortcut for '-F description'"
131//usage: "\n -l Shortcut for '-F license'"
132//usage: "\n -p Shortcut for '-F parm'"
Denys Vlasenko4c8576f2015-10-28 18:07:46 +0100133////usage: "\n -n Shortcut for '-F filename'"
Pascal Bellard22bdf902010-06-06 04:55:13 +0200134//usage: "\n -F keyword Keyword to look for"
135//usage: "\n -0 Separate output with NULs"
136//usage:#define modinfo_example_usage
137//usage: "$ modinfo -F vermagic loop\n"
138
139int modinfo_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
140int modinfo_main(int argc UNUSED_PARAM, char **argv)
141{
Denys Vlasenko4c8576f2015-10-28 18:07:46 +0100142 const char *field;
Pascal Bellard22bdf902010-06-06 04:55:13 +0200143 char name[MODULE_NAME_LEN];
144 struct utsname uts;
Lauri Kasanen1b14cdb2010-06-27 00:35:49 +0200145 parser_t *parser;
Pascal Bellard22bdf902010-06-06 04:55:13 +0200146 char *colon, *tokens[2];
147 unsigned opts;
148 unsigned i;
149
Denys Vlasenko4c8576f2015-10-28 18:07:46 +0100150 field = NULL;
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200151 opts = getopt32(argv, "^" "0F:nadlp" "\0" "-1"/*minimum one arg*/, &field);
Denys Vlasenko4c8576f2015-10-28 18:07:46 +0100152 /* If no field selected, show all */
153 if (!(opts & (OPT_TAGS|OPT_F)))
154 option_mask32 |= OPT_TAGS;
Pascal Bellard22bdf902010-06-06 04:55:13 +0200155 argv += optind;
156
157 uname(&uts);
Lauri Kasanen1b14cdb2010-06-27 00:35:49 +0200158 parser = config_open2(
159 xasprintf("%s/%s/%s", CONFIG_DEFAULT_MODULES_DIR, uts.release, CONFIG_DEFAULT_DEPMOD_FILE),
Pascal Bellard22bdf902010-06-06 04:55:13 +0200160 xfopen_for_read
161 );
162
Lauri Kasanen1b14cdb2010-06-27 00:35:49 +0200163 while (config_read(parser, tokens, 2, 1, "# \t", PARSE_NORMAL)) {
Pascal Bellard22bdf902010-06-06 04:55:13 +0200164 colon = last_char_is(tokens[0], ':');
165 if (colon == NULL)
166 continue;
167 *colon = '\0';
Denys Vlasenkoa88db5c2015-02-21 17:08:35 +0100168 filename2modname(bb_basename(tokens[0]), name);
Pascal Bellard22bdf902010-06-06 04:55:13 +0200169 for (i = 0; argv[i]; i++) {
170 if (fnmatch(argv[i], name, 0) == 0) {
Denys Vlasenko4c8576f2015-10-28 18:07:46 +0100171 modinfo(tokens[0], uts.release, field);
Pascal Bellard22bdf902010-06-06 04:55:13 +0200172 argv[i] = (char *) "";
173 }
174 }
175 }
Lauri Kasanen1b14cdb2010-06-27 00:35:49 +0200176 if (ENABLE_FEATURE_CLEAN_UP)
177 config_close(parser);
178
Pascal Bellard22bdf902010-06-06 04:55:13 +0200179 for (i = 0; argv[i]; i++) {
180 if (argv[i][0]) {
Denys Vlasenko4c8576f2015-10-28 18:07:46 +0100181 modinfo(argv[i], uts.release, field);
Pascal Bellard22bdf902010-06-06 04:55:13 +0200182 }
183 }
Lauri Kasanen1b14cdb2010-06-27 00:35:49 +0200184
Pascal Bellard22bdf902010-06-06 04:55:13 +0200185 return 0;
186}