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/>.
28 #include <sys/utsname.h>
31 #include <libkmod-util.h>
35 static char separator
= '\n';
36 static const char *field
= NULL
;
48 static struct param
*add_param(const char *name
, int namelen
, const char *param
, int paramlen
, const char *type
, int typelen
, struct param
**list
)
52 for (it
= *list
; it
!= NULL
; it
= it
->next
) {
53 if (it
->namelen
== namelen
&&
54 memcmp(it
->name
, name
, namelen
) == 0)
59 it
= malloc(sizeof(struct param
));
65 it
->namelen
= namelen
;
74 it
->paramlen
= paramlen
;
79 it
->typelen
= typelen
;
85 static int process_parm(const char *key
, const char *value
, struct param
**params
)
87 const char *name
, *param
, *type
;
88 int namelen
, paramlen
, typelen
;
90 const char *colon
= strchr(value
, ':');
92 ERR("Found invalid \"%s=%s\": missing ':'\n",
98 namelen
= colon
- value
;
99 if (strcmp(key
, "parm") == 0) {
101 paramlen
= strlen(param
);
108 typelen
= strlen(type
);
111 it
= add_param(name
, namelen
, param
, paramlen
, type
, typelen
, params
);
113 ERR("Out of memory!\n");
120 static int modinfo_params_do(const struct kmod_list
*list
)
122 const struct kmod_list
*l
;
123 struct param
*params
= NULL
;
126 kmod_list_foreach(l
, list
) {
127 const char *key
= kmod_module_info_get_key(l
);
128 const char *value
= kmod_module_info_get_value(l
);
129 if (strcmp(key
, "parm") != 0 &&
130 strcmp(key
, "parmtype") != 0)
133 err
= process_parm(key
, value
, ¶ms
);
138 while (params
!= NULL
) {
139 struct param
*p
= params
;
142 if (p
->param
== NULL
)
143 printf("%.*s: (%.*s)%c",
144 p
->namelen
, p
->name
, p
->typelen
, p
->type
,
146 else if (p
->type
!= NULL
)
147 printf("%.*s:%.*s (%.*s)%c",
149 p
->paramlen
, p
->param
,
153 printf("%.*s:%.*s%c",
155 p
->paramlen
, p
->param
,
162 while (params
!= NULL
) {
164 params
= params
->next
;
171 static int modinfo_do(struct kmod_module
*mod
)
173 struct kmod_list
*l
, *list
= NULL
;
174 struct param
*params
= NULL
;
177 if (field
!= NULL
&& strcmp(field
, "filename") == 0) {
178 printf("%s%c", kmod_module_get_path(mod
), separator
);
180 } else if (field
== NULL
) {
181 printf("%-16s%s%c", "filename:",
182 kmod_module_get_path(mod
), separator
);
185 err
= kmod_module_get_info(mod
, &list
);
187 ERR("could not get modinfo from '%s': %s\n",
188 kmod_module_get_name(mod
), strerror(-err
));
192 if (field
!= NULL
&& strcmp(field
, "parm") == 0) {
193 err
= modinfo_params_do(list
);
197 kmod_list_foreach(l
, list
) {
198 const char *key
= kmod_module_info_get_key(l
);
199 const char *value
= kmod_module_info_get_value(l
);
203 if (strcmp(field
, key
) != 0)
205 /* filtered output contains no key, just value */
206 printf("%s%c", value
, separator
);
210 if (strcmp(key
, "parm") == 0 || strcmp(key
, "parmtype") == 0) {
211 err
= process_parm(key
, value
, ¶ms
);
217 if (separator
== '\0') {
218 printf("%s=%s%c", key
, value
, separator
);
222 keylen
= strlen(key
);
223 printf("%s:%-*s%s%c", key
, 15 - keylen
, "", value
, separator
);
229 while (params
!= NULL
) {
230 struct param
*p
= params
;
233 if (p
->param
== NULL
)
234 printf("%-16s%.*s:%.*s%c", "parm:",
235 p
->namelen
, p
->name
, p
->typelen
, p
->type
,
237 else if (p
->type
!= NULL
)
238 printf("%-16s%.*s:%.*s (%.*s)%c", "parm:",
240 p
->paramlen
, p
->param
,
244 printf("%-16s%.*s:%.*s%c",
247 p
->paramlen
, p
->param
,
254 while (params
!= NULL
) {
256 params
= params
->next
;
259 kmod_module_info_free_list(list
);
264 static int modinfo_path_do(struct kmod_ctx
*ctx
, const char *path
)
266 struct kmod_module
*mod
;
267 int err
= kmod_module_new_from_path(ctx
, path
, &mod
);
269 ERR("Module file %s not found.\n", path
);
272 err
= modinfo_do(mod
);
273 kmod_module_unref(mod
);
277 static int modinfo_alias_do(struct kmod_ctx
*ctx
, const char *alias
)
279 struct kmod_list
*l
, *filtered
, *list
= NULL
;
280 int err
= kmod_module_new_from_lookup(ctx
, alias
, &list
);
282 ERR("Module alias %s not found.\n", alias
);
287 ERR("Module %s not found.\n", alias
);
291 err
= kmod_module_apply_filter(ctx
, KMOD_FILTER_BUILTIN
, list
, &filtered
);
292 kmod_module_unref_list(list
);
294 ERR("Failed to filter list: %m\n");
298 if (filtered
== NULL
) {
299 ERR("Module %s not found.\n", alias
);
303 kmod_list_foreach(l
, filtered
) {
304 struct kmod_module
*mod
= kmod_module_get_module(l
);
305 int r
= modinfo_do(mod
);
306 kmod_module_unref(mod
);
310 kmod_module_unref_list(filtered
);
314 static const char cmdopts_s
[] = "adlpn0F:k:b:Vh";
315 static const struct option cmdopts
[] = {
316 {"author", no_argument
, 0, 'a'},
317 {"description", no_argument
, 0, 'd'},
318 {"license", no_argument
, 0, 'l'},
319 {"parameters", no_argument
, 0, 'p'},
320 {"filename", no_argument
, 0, 'n'},
321 {"null", no_argument
, 0, '0'},
322 {"field", required_argument
, 0, 'F'},
323 {"set-version", required_argument
, 0, 'k'},
324 {"basedir", required_argument
, 0, 'b'},
325 {"version", no_argument
, 0, 'V'},
326 {"help", no_argument
, 0, 'h'},
330 static void help(void)
333 "\t%s [options] filename [args]\n"
335 "\t-a, --author Print only 'author'\n"
336 "\t-d, --description Print only 'description'\n"
337 "\t-l, --license Print only 'license'\n"
338 "\t-p, --parameters Print only 'parm'\n"
339 "\t-n, --filename Print only 'filename'\n"
340 "\t-0, --null Use \\0 instead of \\n\n"
341 "\t-F, --field=FIELD Print only provided FIELD\n"
342 "\t-k, --set-version=VERSION Use VERSION instead of `uname -r`\n"
343 "\t-b, --basedir=DIR Use DIR as filesystem root for /lib/modules\n"
344 "\t-V, --version Show version\n"
345 "\t-h, --help Show this help\n",
346 program_invocation_short_name
);
349 static bool is_module_filename(const char *name
)
353 if (stat(name
, &st
) == 0 && S_ISREG(st
.st_mode
) &&
354 path_ends_with_kmod_ext(name
, strlen(name
)))
360 static int do_modinfo(int argc
, char *argv
[])
362 struct kmod_ctx
*ctx
;
363 char dirname_buf
[PATH_MAX
];
364 const char *dirname
= NULL
;
365 const char *kversion
= NULL
;
366 const char *root
= NULL
;
367 const char *null_config
= NULL
;
372 c
= getopt_long(argc
, argv
, cmdopts_s
, cmdopts
, &idx
);
380 field
= "description";
407 puts(PACKAGE
" version " VERSION
);
412 ERR("unexpected getopt_long() value '%c'.\n", c
);
417 if (optind
>= argc
) {
418 ERR("missing module or filename.\n");
422 if (root
!= NULL
|| kversion
!= NULL
) {
426 if (kversion
== NULL
) {
428 ERR("uname() failed: %m\n");
431 kversion
= u
.release
;
433 snprintf(dirname_buf
, sizeof(dirname_buf
), "%s/lib/modules/%s",
435 dirname
= dirname_buf
;
438 ctx
= kmod_new(dirname
, &null_config
);
440 ERR("kmod_new() failed!\n");
445 for (i
= optind
; i
< argc
; i
++) {
446 const char *name
= argv
[i
];
449 if (is_module_filename(name
))
450 r
= modinfo_path_do(ctx
, name
);
452 r
= modinfo_alias_do(ctx
, name
);
459 return err
>= 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
462 const struct kmod_cmd kmod_cmd_compat_modinfo
= {
465 .help
= "compat modinfo command",