blob: 8e74b643896a6b573a244877bfcc3ea089bb7453 [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 */
8
Denys Vlasenkob9f2d9f2011-01-18 13:58:01 +01009//applet:IF_MODINFO(APPLET(modinfo, BB_DIR_SBIN, BB_SUID_DROP))
Pascal Bellard22bdf902010-06-06 04:55:13 +020010
Denys Vlasenko7fdf5a82010-06-09 10:18:35 +020011//kbuild:lib-$(CONFIG_MODINFO) += modinfo.o modutils.o
Pascal Bellard22bdf902010-06-06 04:55:13 +020012
13//config:config MODINFO
14//config: bool "modinfo"
15//config: default y
Denys Vlasenkob0761932011-10-30 22:07:36 +010016//config: select PLATFORM_LINUX
Pascal Bellard22bdf902010-06-06 04:55:13 +020017//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
26enum {
Tanguy Pruvot772f17a2012-05-30 08:00:46 +020027 OPT_TAGS = (1 << 12) - 1, /* shortcut count */
28 OPT_F = (1 << 12), /* field name */
29 OPT_0 = (1 << 13), /* \0 as separator */
Pascal Bellard22bdf902010-06-06 04:55:13 +020030};
31
32struct modinfo_env {
33 char *field;
34 int tags;
35};
36
David Marchand7d169642014-07-03 12:24:55 +020037static void display(const char *data, const char *pattern, int flag)
Pascal Bellard22bdf902010-06-06 04:55:13 +020038{
39 if (flag) {
40 int n = printf("%s:", pattern);
41 while (n++ < 16)
42 bb_putchar(' ');
43 }
David Marchand7d169642014-07-03 12:24:55 +020044 printf("%s%c", data, (option_mask32 & OPT_0) ? '\0' : '\n');
Pascal Bellard22bdf902010-06-06 04:55:13 +020045}
46
Lauri Kasanen1b14cdb2010-06-27 00:35:49 +020047static void modinfo(const char *path, const char *version,
Denys Vlasenkodf7f2002011-02-15 01:45:14 +010048 const struct modinfo_env *env)
Pascal Bellard22bdf902010-06-06 04:55:13 +020049{
50 static const char *const shortcuts[] = {
51 "filename",
Pascal Bellard22bdf902010-06-06 04:55:13 +020052 "license",
Tanguy Pruvot772f17a2012-05-30 08:00:46 +020053 "author",
54 "description",
55 "version",
56 "alias",
57 "srcversion",
58 "depends",
59 "uts_release",
Pascal Bellard22bdf902010-06-06 04:55:13 +020060 "vermagic",
61 "parm",
Lauri Kasanen12f44ea2011-02-14 19:50:30 +020062 "firmware",
Pascal Bellard22bdf902010-06-06 04:55:13 +020063 };
64 size_t len;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +010065 int j;
Pascal Bellard22bdf902010-06-06 04:55:13 +020066 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 Kasanen1b14cdb2010-06-27 00:35:49 +020073
Pascal Bellard22bdf902010-06-06 04:55:13 +020074 len = MAXINT(ssize_t);
75 the_module = xmalloc_open_zipped_read_close(path, &len);
Lauri Kasanen1b14cdb2010-06-27 00:35:49 +020076 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 Bellard22bdf902010-06-06 04:55:13 +020087 if (field)
88 tags |= OPT_F;
89 for (j = 1; (1<<j) & (OPT_TAGS + OPT_F); j++) {
Denys Vlasenkodf7f2002011-02-15 01:45:14 +010090 const char *pattern;
91
Pascal Bellard22bdf902010-06-06 04:55:13 +020092 if (!((1<<j) & tags))
93 continue;
Denys Vlasenkodf7f2002011-02-15 01:45:14 +010094 pattern = field;
95 if ((1<<j) & OPT_TAGS)
96 pattern = shortcuts[j];
Pascal Bellard22bdf902010-06-06 04:55:13 +020097 ptr = the_module;
98 while (1) {
Denys Vlasenko8dff01d2015-03-12 17:48:34 +010099 char *after_pattern;
100
Pascal Bellard22bdf902010-06-06 04:55:13 +0200101 ptr = memchr(ptr, *pattern, len - (ptr - (char*)the_module));
102 if (ptr == NULL) /* no occurance left, done */
103 break;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100104 after_pattern = is_prefixed_with(ptr, pattern);
105 if (after_pattern && *after_pattern == '=') {
Tanguy Pruvot772f17a2012-05-30 08:00:46 +0200106 /* field prefixes are 0x80 or 0x00 */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100107 if ((ptr[-1] & 0x7F) == 0x00) {
108 ptr = after_pattern + 1;
David Marchand7d169642014-07-03 12:24:55 +0200109 display(ptr, pattern, (1<<j) != tags);
110 ptr += strlen(ptr);
Tanguy Pruvot772f17a2012-05-30 08:00:46 +0200111 }
Pascal Bellard22bdf902010-06-06 04:55:13 +0200112 }
113 ++ptr;
114 }
115 }
116 free(the_module);
117}
118
119//usage:#define modinfo_trivial_usage
120//usage: "[-adlp0] [-F keyword] MODULE"
121//usage:#define modinfo_full_usage "\n\n"
Denys Vlasenko66426762011-06-05 03:58:28 +0200122//usage: " -a Shortcut for '-F author'"
Pascal Bellard22bdf902010-06-06 04:55:13 +0200123//usage: "\n -d Shortcut for '-F description'"
124//usage: "\n -l Shortcut for '-F license'"
125//usage: "\n -p Shortcut for '-F parm'"
126//usage: "\n -F keyword Keyword to look for"
127//usage: "\n -0 Separate output with NULs"
128//usage:#define modinfo_example_usage
129//usage: "$ modinfo -F vermagic loop\n"
130
131int modinfo_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
132int modinfo_main(int argc UNUSED_PARAM, char **argv)
133{
134 struct modinfo_env env;
135 char name[MODULE_NAME_LEN];
136 struct utsname uts;
Lauri Kasanen1b14cdb2010-06-27 00:35:49 +0200137 parser_t *parser;
Pascal Bellard22bdf902010-06-06 04:55:13 +0200138 char *colon, *tokens[2];
139 unsigned opts;
140 unsigned i;
141
142 env.field = NULL;
143 opt_complementary = "-1"; /* minimum one param */
Tanguy Pruvot772f17a2012-05-30 08:00:46 +0200144 opts = getopt32(argv, "nladvAsDumpF:0", &env.field);
Pascal Bellard22bdf902010-06-06 04:55:13 +0200145 env.tags = opts & OPT_TAGS ? opts & OPT_TAGS : OPT_TAGS;
146 argv += optind;
147
148 uname(&uts);
Lauri Kasanen1b14cdb2010-06-27 00:35:49 +0200149 parser = config_open2(
150 xasprintf("%s/%s/%s", CONFIG_DEFAULT_MODULES_DIR, uts.release, CONFIG_DEFAULT_DEPMOD_FILE),
Pascal Bellard22bdf902010-06-06 04:55:13 +0200151 xfopen_for_read
152 );
153
Lauri Kasanen1b14cdb2010-06-27 00:35:49 +0200154 while (config_read(parser, tokens, 2, 1, "# \t", PARSE_NORMAL)) {
Pascal Bellard22bdf902010-06-06 04:55:13 +0200155 colon = last_char_is(tokens[0], ':');
156 if (colon == NULL)
157 continue;
158 *colon = '\0';
Denys Vlasenkoa88db5c2015-02-21 17:08:35 +0100159 filename2modname(bb_basename(tokens[0]), name);
Pascal Bellard22bdf902010-06-06 04:55:13 +0200160 for (i = 0; argv[i]; i++) {
161 if (fnmatch(argv[i], name, 0) == 0) {
Lauri Kasanen1b14cdb2010-06-27 00:35:49 +0200162 modinfo(tokens[0], uts.release, &env);
Pascal Bellard22bdf902010-06-06 04:55:13 +0200163 argv[i] = (char *) "";
164 }
165 }
166 }
Lauri Kasanen1b14cdb2010-06-27 00:35:49 +0200167 if (ENABLE_FEATURE_CLEAN_UP)
168 config_close(parser);
169
Pascal Bellard22bdf902010-06-06 04:55:13 +0200170 for (i = 0; argv[i]; i++) {
171 if (argv[i][0]) {
Lauri Kasanen1b14cdb2010-06-27 00:35:49 +0200172 modinfo(argv[i], uts.release, &env);
Pascal Bellard22bdf902010-06-06 04:55:13 +0200173 }
174 }
Lauri Kasanen1b14cdb2010-06-27 00:35:49 +0200175
Pascal Bellard22bdf902010-06-06 04:55:13 +0200176 return 0;
177}