modprobe: add support for -l and -s. Remove some unsupported options
[platform/upstream/busybox.git] / modutils / modinfo.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * modinfo - retrieve module info
4  * Copyright (c) 2008 Pascal Bellard
5  *
6  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
7  */
8
9 //applet:IF_MODINFO(APPLET(modinfo, _BB_DIR_SBIN, _BB_SUID_DROP))
10
11 //kbuild:lib-$(CONFIG_MODINFO) += modinfo.o
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 {
26         OPT_TAGS = (1 << 6) - 1,
27         OPT_F = (1 << 6), /* field name */
28         OPT_0 = (1 << 7),  /* \0 as separator */
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
46 static void modinfo(const char *path, struct modinfo_env *env)
47 {
48         static const char *const shortcuts[] = {
49                 "filename",
50                 "description",
51                 "author",
52                 "license",
53                 "vermagic",
54                 "parm",
55         };
56         size_t len;
57         int j, length;
58         char *ptr, *the_module;
59         const char *field = env->field;
60         int tags = env->tags;
61
62         if (tags & 1) { /* filename */
63                 display(path, shortcuts[0], 1 != tags);
64         }
65         len = MAXINT(ssize_t);
66         the_module = xmalloc_open_zipped_read_close(path, &len);
67         if (!the_module)
68                 return;
69         if (field)
70                 tags |= OPT_F;
71         for (j = 1; (1<<j) & (OPT_TAGS + OPT_F); j++) {
72                 const char *pattern = field;
73                 if ((1<<j) & OPT_TAGS)
74                         pattern = shortcuts[j];
75                 if (!((1<<j) & tags))
76                         continue;
77                 length = strlen(pattern);
78                 ptr = the_module;
79                 while (1) {
80                         ptr = memchr(ptr, *pattern, len - (ptr - (char*)the_module));
81                         if (ptr == NULL) /* no occurance left, done */
82                                 break;
83                         if (strncmp(ptr, pattern, length) == 0 && ptr[length] == '=') {
84                                 ptr += length + 1;
85                                 ptr += display(ptr, pattern, (1<<j) != tags);
86                         }
87                         ++ptr;
88                 }
89         }
90         free(the_module);
91 }
92
93 //usage:#define modinfo_trivial_usage
94 //usage:       "[-adlp0] [-F keyword] MODULE"
95 //usage:#define modinfo_full_usage "\n\n"
96 //usage:       "Options:"
97 //usage:     "\n        -a              Shortcut for '-F author'"
98 //usage:     "\n        -d              Shortcut for '-F description'"
99 //usage:     "\n        -l              Shortcut for '-F license'"
100 //usage:     "\n        -p              Shortcut for '-F parm'"
101 //usage:     "\n        -F keyword      Keyword to look for"
102 //usage:     "\n        -0              Separate output with NULs"
103 //usage:#define modinfo_example_usage
104 //usage:       "$ modinfo -F vermagic loop\n"
105
106 int modinfo_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
107 int modinfo_main(int argc UNUSED_PARAM, char **argv)
108 {
109         struct modinfo_env env;
110         char name[MODULE_NAME_LEN];
111         struct utsname uts;
112         parser_t *p;
113         char *colon, *tokens[2];
114         unsigned opts;
115         unsigned i;
116
117         env.field = NULL;
118         opt_complementary = "-1"; /* minimum one param */
119         opts = getopt32(argv, "fdalvpF:0", &env.field);
120         env.tags = opts & OPT_TAGS ? opts & OPT_TAGS : OPT_TAGS;
121         argv += optind;
122
123         uname(&uts);
124         p = config_open2(
125                 concat_path_file(
126                         concat_path_file(CONFIG_DEFAULT_MODULES_DIR, uts.release),
127                         CONFIG_DEFAULT_DEPMOD_FILE),
128                 xfopen_for_read
129         );
130
131         while (config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)) {
132                 colon = last_char_is(tokens[0], ':');
133                 if (colon == NULL)
134                         continue;
135                 *colon = '\0';
136                 filename2modname(tokens[0], name);
137                 for (i = 0; argv[i]; i++) {
138                         if (fnmatch(argv[i], name, 0) == 0) {
139                                 modinfo(tokens[0], &env);
140                                 argv[i] = (char *) "";
141                         }
142                 }
143         }
144         for (i = 0; argv[i]; i++) {
145                 if (argv[i][0]) {
146                         modinfo(argv[i], &env);
147                 }
148         }
149         return 0;
150 }