2 * kmod-modinfo - query kernel module information using libkmod.
4 * Copyright (C) 2011-2013 ProFUSION embedded systems
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.
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.
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/>.
27 #include <sys/utsname.h>
30 #include "libkmod-util.h"
34 static char separator
= '\n';
35 static const char *field
= NULL
;
47 static struct param
*add_param(const char *name
, int namelen
, const char *param
, int paramlen
, const char *type
, int typelen
, struct param
**list
)
51 for (it
= *list
; it
!= NULL
; it
= it
->next
) {
52 if (it
->namelen
== namelen
&&
53 memcmp(it
->name
, name
, namelen
) == 0)
58 it
= malloc(sizeof(struct param
));
64 it
->namelen
= namelen
;
73 it
->paramlen
= paramlen
;
78 it
->typelen
= typelen
;
84 static int process_parm(const char *key
, const char *value
, struct param
**params
)
86 const char *name
, *param
, *type
;
87 int namelen
, paramlen
, typelen
;
89 const char *colon
= strchr(value
, ':');
91 ERR("Found invalid \"%s=%s\": missing ':'\n",
97 namelen
= colon
- value
;
98 if (strcmp(key
, "parm") == 0) {
100 paramlen
= strlen(param
);
107 typelen
= strlen(type
);
110 it
= add_param(name
, namelen
, param
, paramlen
, type
, typelen
, params
);
112 ERR("Out of memory!\n");
119 static int modinfo_params_do(const struct kmod_list
*list
)
121 const struct kmod_list
*l
;
122 struct param
*params
= NULL
;
125 kmod_list_foreach(l
, list
) {
126 const char *key
= kmod_module_info_get_key(l
);
127 const char *value
= kmod_module_info_get_value(l
);
128 if (strcmp(key
, "parm") != 0 &&
129 strcmp(key
, "parmtype") != 0)
132 err
= process_parm(key
, value
, ¶ms
);
137 while (params
!= NULL
) {
138 struct param
*p
= params
;
141 if (p
->param
== NULL
)
142 printf("%.*s: (%.*s)%c",
143 p
->namelen
, p
->name
, p
->typelen
, p
->type
,
145 else if (p
->type
!= NULL
)
146 printf("%.*s:%.*s (%.*s)%c",
148 p
->paramlen
, p
->param
,
152 printf("%.*s:%.*s%c",
154 p
->paramlen
, p
->param
,
161 while (params
!= NULL
) {
163 params
= params
->next
;
170 static int modinfo_do(struct kmod_module
*mod
)
172 struct kmod_list
*l
, *list
= NULL
;
173 struct param
*params
= NULL
;
176 if (field
!= NULL
&& strcmp(field
, "filename") == 0) {
177 printf("%s%c", kmod_module_get_path(mod
), separator
);
179 } else if (field
== NULL
) {
180 printf("%-16s%s%c", "filename:",
181 kmod_module_get_path(mod
), separator
);
184 err
= kmod_module_get_info(mod
, &list
);
186 ERR("could not get modinfo from '%s': %s\n",
187 kmod_module_get_name(mod
), strerror(-err
));
191 if (field
!= NULL
&& strcmp(field
, "parm") == 0) {
192 err
= modinfo_params_do(list
);
196 kmod_list_foreach(l
, list
) {
197 const char *key
= kmod_module_info_get_key(l
);
198 const char *value
= kmod_module_info_get_value(l
);
202 if (strcmp(field
, key
) != 0)
204 /* filtered output contains no key, just value */
205 printf("%s%c", value
, separator
);
209 if (strcmp(key
, "parm") == 0 || strcmp(key
, "parmtype") == 0) {
210 err
= process_parm(key
, value
, ¶ms
);
216 if (separator
== '\0') {
217 printf("%s=%s%c", key
, value
, separator
);
221 keylen
= strlen(key
);
222 printf("%s:%-*s%s%c", key
, 15 - keylen
, "", value
, separator
);
228 while (params
!= NULL
) {
229 struct param
*p
= params
;
232 if (p
->param
== NULL
)
233 printf("%-16s%.*s:%.*s%c", "parm:",
234 p
->namelen
, p
->name
, p
->typelen
, p
->type
,
236 else if (p
->type
!= NULL
)
237 printf("%-16s%.*s:%.*s (%.*s)%c", "parm:",
239 p
->paramlen
, p
->param
,
243 printf("%-16s%.*s:%.*s%c",
246 p
->paramlen
, p
->param
,
253 while (params
!= NULL
) {
255 params
= params
->next
;
258 kmod_module_info_free_list(list
);
263 static int modinfo_path_do(struct kmod_ctx
*ctx
, const char *path
)
265 struct kmod_module
*mod
;
266 int err
= kmod_module_new_from_path(ctx
, path
, &mod
);
268 ERR("Module file %s not found.\n", path
);
271 err
= modinfo_do(mod
);
272 kmod_module_unref(mod
);
276 static int modinfo_alias_do(struct kmod_ctx
*ctx
, const char *alias
)
278 struct kmod_list
*l
, *filtered
, *list
= NULL
;
279 int err
= kmod_module_new_from_lookup(ctx
, alias
, &list
);
281 ERR("Module alias %s not found.\n", alias
);
286 ERR("Module %s not found.\n", alias
);
290 err
= kmod_module_apply_filter(ctx
, KMOD_FILTER_BUILTIN
, list
, &filtered
);
291 kmod_module_unref_list(list
);
293 ERR("Failed to filter list: %m\n");
297 if (filtered
== NULL
) {
298 ERR("Module %s not found.\n", alias
);
302 kmod_list_foreach(l
, filtered
) {
303 struct kmod_module
*mod
= kmod_module_get_module(l
);
304 int r
= modinfo_do(mod
);
305 kmod_module_unref(mod
);
309 kmod_module_unref_list(filtered
);
313 static const char cmdopts_s
[] = "adlpn0F:k:b:Vh";
314 static const struct option cmdopts
[] = {
315 {"author", no_argument
, 0, 'a'},
316 {"description", no_argument
, 0, 'd'},
317 {"license", no_argument
, 0, 'l'},
318 {"parameters", no_argument
, 0, 'p'},
319 {"filename", no_argument
, 0, 'n'},
320 {"null", no_argument
, 0, '0'},
321 {"field", required_argument
, 0, 'F'},
322 {"set-version", required_argument
, 0, 'k'},
323 {"basedir", required_argument
, 0, 'b'},
324 {"version", no_argument
, 0, 'V'},
325 {"help", no_argument
, 0, 'h'},
329 static void help(void)
332 "\t%s [options] filename [args]\n"
334 "\t-a, --author Print only 'author'\n"
335 "\t-d, --description Print only 'description'\n"
336 "\t-l, --license Print only 'license'\n"
337 "\t-p, --parameters Print only 'parm'\n"
338 "\t-n, --filename Print only 'filename'\n"
339 "\t-0, --null Use \\0 instead of \\n\n"
340 "\t-F, --field=FIELD Print only provided FIELD\n"
341 "\t-k, --set-version=VERSION Use VERSION instead of `uname -r`\n"
342 "\t-b, --basedir=DIR Use DIR as filesystem root for /lib/modules\n"
343 "\t-V, --version Show version\n"
344 "\t-h, --help Show this help\n",
345 program_invocation_short_name
);
348 static bool is_module_filename(const char *name
)
352 if (stat(name
, &st
) == 0 && S_ISREG(st
.st_mode
) &&
353 path_ends_with_kmod_ext(name
, strlen(name
)))
359 static int do_modinfo(int argc
, char *argv
[])
361 struct kmod_ctx
*ctx
;
362 char dirname_buf
[PATH_MAX
];
363 const char *dirname
= NULL
;
364 const char *kversion
= NULL
;
365 const char *root
= NULL
;
366 const char *null_config
= NULL
;
371 c
= getopt_long(argc
, argv
, cmdopts_s
, cmdopts
, &idx
);
379 field
= "description";
406 puts(PACKAGE
" version " VERSION
);
411 ERR("unexpected getopt_long() value '%c'.\n", c
);
416 if (optind
>= argc
) {
417 ERR("missing module or filename.\n");
421 if (root
!= NULL
|| kversion
!= NULL
) {
425 if (kversion
== NULL
) {
427 ERR("uname() failed: %m\n");
430 kversion
= u
.release
;
432 snprintf(dirname_buf
, sizeof(dirname_buf
), "%s/lib/modules/%s",
434 dirname
= dirname_buf
;
437 ctx
= kmod_new(dirname
, &null_config
);
439 ERR("kmod_new() failed!\n");
444 for (i
= optind
; i
< argc
; i
++) {
445 const char *name
= argv
[i
];
448 if (is_module_filename(name
))
449 r
= modinfo_path_do(ctx
, name
);
451 r
= modinfo_alias_do(ctx
, name
);
458 return err
>= 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
461 const struct kmod_cmd kmod_cmd_compat_modinfo
= {
464 .help
= "compat modinfo command",