]> git.ipfire.org Git - thirdparty/kmod.git/blame - tools/modinfo.c
libkmod: fix possible double free with wrong modules.builtin.modinfo
[thirdparty/kmod.git] / tools / modinfo.c
CommitLineData
0cc3ccfd
GSB
1/*
2 * kmod-modinfo - query kernel module information using libkmod.
3 *
e6b0e49b 4 * Copyright (C) 2011-2013 ProFUSION embedded systems
0cc3ccfd
GSB
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
c2e4286b
LDM
20#include <errno.h>
21#include <getopt.h>
22#include <limits.h>
23#include <stdbool.h>
0cc3ccfd
GSB
24#include <stdio.h>
25#include <stdlib.h>
0cc3ccfd 26#include <string.h>
0cc3ccfd 27#include <sys/stat.h>
c2e4286b
LDM
28#include <sys/utsname.h>
29
f4e8c162
LDM
30#include <shared/util.h>
31
f357866d 32#include <libkmod/libkmod.h>
0cc3ccfd 33
4a2e20df
LDM
34#include "kmod.h"
35
0cc3ccfd
GSB
36static char separator = '\n';
37static const char *field = NULL;
38
39struct param {
40 struct param *next;
41 const char *name;
42 const char *param;
43 const char *type;
44 int namelen;
45 int paramlen;
46 int typelen;
47};
48
49static struct param *add_param(const char *name, int namelen, const char *param, int paramlen, const char *type, int typelen, struct param **list)
50{
51 struct param *it;
52
53 for (it = *list; it != NULL; it = it->next) {
54 if (it->namelen == namelen &&
55 memcmp(it->name, name, namelen) == 0)
56 break;
57 }
58
59 if (it == NULL) {
60 it = malloc(sizeof(struct param));
61 if (it == NULL)
62 return NULL;
63 it->next = *list;
64 *list = it;
65 it->name = name;
66 it->namelen = namelen;
67 it->param = NULL;
68 it->type = NULL;
69 it->paramlen = 0;
70 it->typelen = 0;
71 }
72
73 if (param != NULL) {
74 it->param = param;
75 it->paramlen = paramlen;
76 }
77
78 if (type != NULL) {
79 it->type = type;
80 it->typelen = typelen;
81 }
82
83 return it;
84}
85
b014c490
GSB
86static int process_parm(const char *key, const char *value, struct param **params)
87{
88 const char *name, *param, *type;
89 int namelen, paramlen, typelen;
90 struct param *it;
91 const char *colon = strchr(value, ':');
92 if (colon == NULL) {
9382dbf7 93 ERR("Found invalid \"%s=%s\": missing ':'\n",
b014c490
GSB
94 key, value);
95 return 0;
96 }
97
98 name = value;
99 namelen = colon - value;
5b0436a7 100 if (streq(key, "parm")) {
b014c490
GSB
101 param = colon + 1;
102 paramlen = strlen(param);
103 type = NULL;
104 typelen = 0;
105 } else {
106 param = NULL;
107 paramlen = 0;
108 type = colon + 1;
109 typelen = strlen(type);
110 }
111
112 it = add_param(name, namelen, param, paramlen, type, typelen, params);
113 if (it == NULL) {
9382dbf7 114 ERR("Out of memory!\n");
b014c490
GSB
115 return -ENOMEM;
116 }
117
118 return 0;
119}
120
121static int modinfo_params_do(const struct kmod_list *list)
122{
123 const struct kmod_list *l;
124 struct param *params = NULL;
125 int err = 0;
126
127 kmod_list_foreach(l, list) {
128 const char *key = kmod_module_info_get_key(l);
129 const char *value = kmod_module_info_get_value(l);
5b0436a7 130 if (!streq(key, "parm") && !streq(key, "parmtype"))
b014c490
GSB
131 continue;
132
133 err = process_parm(key, value, &params);
134 if (err < 0)
135 goto end;
136 }
137
138 while (params != NULL) {
139 struct param *p = params;
140 params = p->next;
141
142 if (p->param == NULL)
143 printf("%.*s: (%.*s)%c",
144 p->namelen, p->name, p->typelen, p->type,
145 separator);
146 else if (p->type != NULL)
147 printf("%.*s:%.*s (%.*s)%c",
148 p->namelen, p->name,
149 p->paramlen, p->param,
150 p->typelen, p->type,
151 separator);
152 else
153 printf("%.*s:%.*s%c",
154 p->namelen, p->name,
155 p->paramlen, p->param,
156 separator);
157
158 free(p);
159 }
160
161end:
162 while (params != NULL) {
163 void *tmp = params;
164 params = params->next;
165 free(tmp);
166 }
167
168 return err;
169}
170
0cc3ccfd
GSB
171static int modinfo_do(struct kmod_module *mod)
172{
173 struct kmod_list *l, *list = NULL;
174 struct param *params = NULL;
e7e2cb61
AG
175 int err, is_builtin;
176 const char *filename = kmod_module_get_path(mod);
177
178 is_builtin = (filename == NULL);
179
180 if (is_builtin) {
fa67110f
MI
181 if (field == NULL)
182 printf("%-16s%s%c", "name:",
183 kmod_module_get_name(mod), separator);
184 else if (field != NULL && streq(field, "name"))
185 printf("%s%c", kmod_module_get_name(mod), separator);
e7e2cb61
AG
186 filename = "(builtin)";
187 }
0cc3ccfd 188
5b0436a7 189 if (field != NULL && streq(field, "filename")) {
e7e2cb61 190 printf("%s%c", filename, separator);
0cc3ccfd
GSB
191 return 0;
192 } else if (field == NULL) {
193 printf("%-16s%s%c", "filename:",
e7e2cb61 194 filename, separator);
0cc3ccfd
GSB
195 }
196
197 err = kmod_module_get_info(mod, &list);
198 if (err < 0) {
e7e2cb61
AG
199 if (is_builtin && err == -ENOENT) {
200 /*
201 * This is an old kernel that does not have a file
202 * with information about built-in modules.
203 */
204 return 0;
205 }
9382dbf7 206 ERR("could not get modinfo from '%s': %s\n",
0cc3ccfd
GSB
207 kmod_module_get_name(mod), strerror(-err));
208 return err;
209 }
210
5b0436a7 211 if (field != NULL && streq(field, "parm")) {
b014c490
GSB
212 err = modinfo_params_do(list);
213 goto end;
214 }
215
0cc3ccfd
GSB
216 kmod_list_foreach(l, list) {
217 const char *key = kmod_module_info_get_key(l);
218 const char *value = kmod_module_info_get_value(l);
219 int keylen;
220
221 if (field != NULL) {
5b0436a7 222 if (!streq(field, key))
0cc3ccfd
GSB
223 continue;
224 /* filtered output contains no key, just value */
225 printf("%s%c", value, separator);
226 continue;
227 }
228
5b0436a7 229 if (streq(key, "parm") || streq(key, "parmtype")) {
b014c490
GSB
230 err = process_parm(key, value, &params);
231 if (err < 0)
0cc3ccfd 232 goto end;
0cc3ccfd
GSB
233 continue;
234 }
235
236 if (separator == '\0') {
237 printf("%s=%s%c", key, value, separator);
238 continue;
239 }
240
241 keylen = strlen(key);
242 printf("%s:%-*s%s%c", key, 15 - keylen, "", value, separator);
243 }
244
245 if (field != NULL)
246 goto end;
247
248 while (params != NULL) {
249 struct param *p = params;
250 params = p->next;
251
252 if (p->param == NULL)
253 printf("%-16s%.*s:%.*s%c", "parm:",
254 p->namelen, p->name, p->typelen, p->type,
255 separator);
256 else if (p->type != NULL)
515ec796 257 printf("%-16s%.*s:%.*s (%.*s)%c", "parm:",
0cc3ccfd
GSB
258 p->namelen, p->name,
259 p->paramlen, p->param,
260 p->typelen, p->type,
261 separator);
262 else
515ec796 263 printf("%-16s%.*s:%.*s%c",
0cc3ccfd
GSB
264 "parm:",
265 p->namelen, p->name,
266 p->paramlen, p->param,
267 separator);
268
269 free(p);
270 }
271
272end:
273 while (params != NULL) {
274 void *tmp = params;
275 params = params->next;
276 free(tmp);
277 }
278 kmod_module_info_free_list(list);
279
280 return err;
281}
282
283static int modinfo_path_do(struct kmod_ctx *ctx, const char *path)
284{
285 struct kmod_module *mod;
286 int err = kmod_module_new_from_path(ctx, path, &mod);
287 if (err < 0) {
9382dbf7 288 ERR("Module file %s not found.\n", path);
0cc3ccfd
GSB
289 return err;
290 }
291 err = modinfo_do(mod);
292 kmod_module_unref(mod);
293 return err;
294}
295
296static int modinfo_alias_do(struct kmod_ctx *ctx, const char *alias)
297{
e7e2cb61 298 struct kmod_list *l, *list = NULL;
0cc3ccfd
GSB
299 int err = kmod_module_new_from_lookup(ctx, alias, &list);
300 if (err < 0) {
9382dbf7 301 ERR("Module alias %s not found.\n", alias);
0cc3ccfd
GSB
302 return err;
303 }
5f85a133
DR
304
305 if (list == NULL) {
9382dbf7 306 ERR("Module %s not found.\n", alias);
5f85a133
DR
307 return -ENOENT;
308 }
309
e7e2cb61 310 kmod_list_foreach(l, list) {
0cc3ccfd
GSB
311 struct kmod_module *mod = kmod_module_get_module(l);
312 int r = modinfo_do(mod);
313 kmod_module_unref(mod);
314 if (r < 0)
315 err = r;
316 }
e7e2cb61 317 kmod_module_unref_list(list);
0cc3ccfd
GSB
318 return err;
319}
320
022e1f0e 321static const char cmdopts_s[] = "adlpn0F:k:b:Vh";
0cc3ccfd
GSB
322static const struct option cmdopts[] = {
323 {"author", no_argument, 0, 'a'},
324 {"description", no_argument, 0, 'd'},
325 {"license", no_argument, 0, 'l'},
326 {"parameters", no_argument, 0, 'p'},
327 {"filename", no_argument, 0, 'n'},
328 {"null", no_argument, 0, '0'},
329 {"field", required_argument, 0, 'F'},
330 {"set-version", required_argument, 0, 'k'},
331 {"basedir", required_argument, 0, 'b'},
332 {"version", no_argument, 0, 'V'},
333 {"help", no_argument, 0, 'h'},
334 {NULL, 0, 0, 0}
335};
336
4a2e20df 337static void help(void)
0cc3ccfd 338{
34e06bfb 339 printf("Usage:\n"
0cc3ccfd
GSB
340 "\t%s [options] filename [args]\n"
341 "Options:\n"
342 "\t-a, --author Print only 'author'\n"
343 "\t-d, --description Print only 'description'\n"
344 "\t-l, --license Print only 'license'\n"
345 "\t-p, --parameters Print only 'parm'\n"
346 "\t-n, --filename Print only 'filename'\n"
347 "\t-0, --null Use \\0 instead of \\n\n"
348 "\t-F, --field=FIELD Print only provided FIELD\n"
349 "\t-k, --set-version=VERSION Use VERSION instead of `uname -r`\n"
c5b37dba 350 "\t-b, --basedir=DIR Use DIR as filesystem root for /lib/modules\n"
0cc3ccfd
GSB
351 "\t-V, --version Show version\n"
352 "\t-h, --help Show this help\n",
7c04aeee 353 program_invocation_short_name);
0cc3ccfd
GSB
354}
355
a23f0c9c
DM
356static bool is_module_filename(const char *name)
357{
358 struct stat st;
a23f0c9c
DM
359
360 if (stat(name, &st) == 0 && S_ISREG(st.st_mode) &&
6f02b6fa 361 path_ends_with_kmod_ext(name, strlen(name)))
a23f0c9c 362 return true;
a23f0c9c
DM
363
364 return false;
365}
366
769becb5 367static int do_modinfo(int argc, char *argv[])
0cc3ccfd
GSB
368{
369 struct kmod_ctx *ctx;
370 char dirname_buf[PATH_MAX];
371 const char *dirname = NULL;
372 const char *kversion = NULL;
373 const char *root = NULL;
374 const char *null_config = NULL;
375 int i, err;
376
377 for (;;) {
378 int c, idx = 0;
379 c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
380 if (c == -1)
381 break;
382 switch (c) {
383 case 'a':
384 field = "author";
385 break;
386 case 'd':
387 field = "description";
388 break;
389 case 'l':
390 field = "license";
391 break;
392 case 'p':
393 field = "parm";
394 break;
395 case 'n':
396 field = "filename";
397 break;
398 case '0':
399 separator = '\0';
400 break;
401 case 'F':
402 field = optarg;
403 break;
404 case 'k':
405 kversion = optarg;
406 break;
407 case 'b':
408 root = optarg;
409 break;
410 case 'h':
4a2e20df 411 help();
0cc3ccfd
GSB
412 return EXIT_SUCCESS;
413 case 'V':
414 puts(PACKAGE " version " VERSION);
655de275 415 puts(KMOD_FEATURES);
0cc3ccfd
GSB
416 return EXIT_SUCCESS;
417 case '?':
418 return EXIT_FAILURE;
419 default:
9382dbf7 420 ERR("unexpected getopt_long() value '%c'.\n", c);
0cc3ccfd
GSB
421 return EXIT_FAILURE;
422 }
423 }
424
425 if (optind >= argc) {
9382dbf7 426 ERR("missing module or filename.\n");
0cc3ccfd
GSB
427 return EXIT_FAILURE;
428 }
429
430 if (root != NULL || kversion != NULL) {
431 struct utsname u;
432 if (root == NULL)
433 root = "";
434 if (kversion == NULL) {
435 if (uname(&u) < 0) {
9382dbf7 436 ERR("uname() failed: %m\n");
0cc3ccfd
GSB
437 return EXIT_FAILURE;
438 }
439 kversion = u.release;
440 }
c5b37dba 441 snprintf(dirname_buf, sizeof(dirname_buf), "%s/lib/modules/%s",
0cc3ccfd
GSB
442 root, kversion);
443 dirname = dirname_buf;
444 }
445
446 ctx = kmod_new(dirname, &null_config);
447 if (!ctx) {
9382dbf7 448 ERR("kmod_new() failed!\n");
0cc3ccfd
GSB
449 return EXIT_FAILURE;
450 }
0cc3ccfd
GSB
451
452 err = 0;
453 for (i = optind; i < argc; i++) {
454 const char *name = argv[i];
0cc3ccfd
GSB
455 int r;
456
a23f0c9c 457 if (is_module_filename(name))
0cc3ccfd
GSB
458 r = modinfo_path_do(ctx, name);
459 else
460 r = modinfo_alias_do(ctx, name);
461
462 if (r < 0)
463 err = r;
464 }
465
466 kmod_unref(ctx);
467 return err >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
468}
769becb5 469
769becb5
LDM
470const struct kmod_cmd kmod_cmd_compat_modinfo = {
471 .name = "modinfo",
472 .cmd = do_modinfo,
473 .help = "compat modinfo command",
474};