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>
30 #include <shared/util.h>
32 #include <libkmod/libkmod.h>
36 static char separator
= '\n';
37 static const char *field
= NULL
;
49 static struct param
*add_param(const char *name
, int namelen
, const char *param
, int paramlen
, const char *type
, int typelen
, struct param
**list
)
53 for (it
= *list
; it
!= NULL
; it
= it
->next
) {
54 if (it
->namelen
== namelen
&&
55 memcmp(it
->name
, name
, namelen
) == 0)
60 it
= malloc(sizeof(struct param
));
66 it
->namelen
= namelen
;
75 it
->paramlen
= paramlen
;
80 it
->typelen
= typelen
;
86 static int process_parm(const char *key
, const char *value
, struct param
**params
)
88 const char *name
, *param
, *type
;
89 int namelen
, paramlen
, typelen
;
91 const char *colon
= strchr(value
, ':');
93 ERR("Found invalid \"%s=%s\": missing ':'\n",
99 namelen
= colon
- value
;
100 if (strcmp(key
, "parm") == 0) {
102 paramlen
= strlen(param
);
109 typelen
= strlen(type
);
112 it
= add_param(name
, namelen
, param
, paramlen
, type
, typelen
, params
);
114 ERR("Out of memory!\n");
121 static int modinfo_params_do(const struct kmod_list
*list
)
123 const struct kmod_list
*l
;
124 struct param
*params
= NULL
;
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
);
130 if (strcmp(key
, "parm") != 0 &&
131 strcmp(key
, "parmtype") != 0)
134 err
= process_parm(key
, value
, ¶ms
);
139 while (params
!= NULL
) {
140 struct param
*p
= params
;
143 if (p
->param
== NULL
)
144 printf("%.*s: (%.*s)%c",
145 p
->namelen
, p
->name
, p
->typelen
, p
->type
,
147 else if (p
->type
!= NULL
)
148 printf("%.*s:%.*s (%.*s)%c",
150 p
->paramlen
, p
->param
,
154 printf("%.*s:%.*s%c",
156 p
->paramlen
, p
->param
,
163 while (params
!= NULL
) {
165 params
= params
->next
;
172 static int modinfo_do(struct kmod_module
*mod
)
174 struct kmod_list
*l
, *list
= NULL
;
175 struct param
*params
= NULL
;
178 if (field
!= NULL
&& strcmp(field
, "filename") == 0) {
179 printf("%s%c", kmod_module_get_path(mod
), separator
);
181 } else if (field
== NULL
) {
182 printf("%-16s%s%c", "filename:",
183 kmod_module_get_path(mod
), separator
);
186 err
= kmod_module_get_info(mod
, &list
);
188 ERR("could not get modinfo from '%s': %s\n",
189 kmod_module_get_name(mod
), strerror(-err
));
193 if (field
!= NULL
&& strcmp(field
, "parm") == 0) {
194 err
= modinfo_params_do(list
);
198 kmod_list_foreach(l
, list
) {
199 const char *key
= kmod_module_info_get_key(l
);
200 const char *value
= kmod_module_info_get_value(l
);
204 if (strcmp(field
, key
) != 0)
206 /* filtered output contains no key, just value */
207 printf("%s%c", value
, separator
);
211 if (strcmp(key
, "parm") == 0 || strcmp(key
, "parmtype") == 0) {
212 err
= process_parm(key
, value
, ¶ms
);
218 if (separator
== '\0') {
219 printf("%s=%s%c", key
, value
, separator
);
223 keylen
= strlen(key
);
224 printf("%s:%-*s%s%c", key
, 15 - keylen
, "", value
, separator
);
230 while (params
!= NULL
) {
231 struct param
*p
= params
;
234 if (p
->param
== NULL
)
235 printf("%-16s%.*s:%.*s%c", "parm:",
236 p
->namelen
, p
->name
, p
->typelen
, p
->type
,
238 else if (p
->type
!= NULL
)
239 printf("%-16s%.*s:%.*s (%.*s)%c", "parm:",
241 p
->paramlen
, p
->param
,
245 printf("%-16s%.*s:%.*s%c",
248 p
->paramlen
, p
->param
,
255 while (params
!= NULL
) {
257 params
= params
->next
;
260 kmod_module_info_free_list(list
);
265 static int modinfo_path_do(struct kmod_ctx
*ctx
, const char *path
)
267 struct kmod_module
*mod
;
268 int err
= kmod_module_new_from_path(ctx
, path
, &mod
);
270 ERR("Module file %s not found.\n", path
);
273 err
= modinfo_do(mod
);
274 kmod_module_unref(mod
);
278 static int modinfo_alias_do(struct kmod_ctx
*ctx
, const char *alias
)
280 struct kmod_list
*l
, *filtered
, *list
= NULL
;
281 int err
= kmod_module_new_from_lookup(ctx
, alias
, &list
);
283 ERR("Module alias %s not found.\n", alias
);
288 ERR("Module %s not found.\n", alias
);
292 err
= kmod_module_apply_filter(ctx
, KMOD_FILTER_BUILTIN
, list
, &filtered
);
293 kmod_module_unref_list(list
);
295 ERR("Failed to filter list: %m\n");
299 if (filtered
== NULL
) {
300 ERR("Module %s not found.\n", alias
);
304 kmod_list_foreach(l
, filtered
) {
305 struct kmod_module
*mod
= kmod_module_get_module(l
);
306 int r
= modinfo_do(mod
);
307 kmod_module_unref(mod
);
311 kmod_module_unref_list(filtered
);
315 static const char cmdopts_s
[] = "adlpn0F:k:b:Vh";
316 static const struct option cmdopts
[] = {
317 {"author", no_argument
, 0, 'a'},
318 {"description", no_argument
, 0, 'd'},
319 {"license", no_argument
, 0, 'l'},
320 {"parameters", no_argument
, 0, 'p'},
321 {"filename", no_argument
, 0, 'n'},
322 {"null", no_argument
, 0, '0'},
323 {"field", required_argument
, 0, 'F'},
324 {"set-version", required_argument
, 0, 'k'},
325 {"basedir", required_argument
, 0, 'b'},
326 {"version", no_argument
, 0, 'V'},
327 {"help", no_argument
, 0, 'h'},
331 static void help(void)
334 "\t%s [options] filename [args]\n"
336 "\t-a, --author Print only 'author'\n"
337 "\t-d, --description Print only 'description'\n"
338 "\t-l, --license Print only 'license'\n"
339 "\t-p, --parameters Print only 'parm'\n"
340 "\t-n, --filename Print only 'filename'\n"
341 "\t-0, --null Use \\0 instead of \\n\n"
342 "\t-F, --field=FIELD Print only provided FIELD\n"
343 "\t-k, --set-version=VERSION Use VERSION instead of `uname -r`\n"
344 "\t-b, --basedir=DIR Use DIR as filesystem root for /lib/modules\n"
345 "\t-V, --version Show version\n"
346 "\t-h, --help Show this help\n",
347 program_invocation_short_name
);
350 static bool is_module_filename(const char *name
)
354 if (stat(name
, &st
) == 0 && S_ISREG(st
.st_mode
) &&
355 path_ends_with_kmod_ext(name
, strlen(name
)))
361 static int do_modinfo(int argc
, char *argv
[])
363 struct kmod_ctx
*ctx
;
364 char dirname_buf
[PATH_MAX
];
365 const char *dirname
= NULL
;
366 const char *kversion
= NULL
;
367 const char *root
= NULL
;
368 const char *null_config
= NULL
;
373 c
= getopt_long(argc
, argv
, cmdopts_s
, cmdopts
, &idx
);
381 field
= "description";
408 puts(PACKAGE
" version " VERSION
);
413 ERR("unexpected getopt_long() value '%c'.\n", c
);
418 if (optind
>= argc
) {
419 ERR("missing module or filename.\n");
423 if (root
!= NULL
|| kversion
!= NULL
) {
427 if (kversion
== NULL
) {
429 ERR("uname() failed: %m\n");
432 kversion
= u
.release
;
434 snprintf(dirname_buf
, sizeof(dirname_buf
), "%s/lib/modules/%s",
436 dirname
= dirname_buf
;
439 ctx
= kmod_new(dirname
, &null_config
);
441 ERR("kmod_new() failed!\n");
446 for (i
= optind
; i
< argc
; i
++) {
447 const char *name
= argv
[i
];
450 if (is_module_filename(name
))
451 r
= modinfo_path_do(ctx
, name
);
453 r
= modinfo_alias_do(ctx
, name
);
460 return err
>= 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
463 const struct kmod_cmd kmod_cmd_compat_modinfo
= {
466 .help
= "compat modinfo command",