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