2 * kmod-modinfo - query kernel module information using libkmod.
4 * Copyright (C) 2011-2012 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>
31 #define LOG(fmt, ...) fprintf(stderr, "ERROR: "fmt, ##__VA_ARGS__)
33 static char separator
= '\n';
34 static const char *field
= NULL
;
46 static struct param
*add_param(const char *name
, int namelen
, const char *param
, int paramlen
, const char *type
, int typelen
, struct param
**list
)
50 for (it
= *list
; it
!= NULL
; it
= it
->next
) {
51 if (it
->namelen
== namelen
&&
52 memcmp(it
->name
, name
, namelen
) == 0)
57 it
= malloc(sizeof(struct param
));
63 it
->namelen
= namelen
;
72 it
->paramlen
= paramlen
;
77 it
->typelen
= typelen
;
83 static int process_parm(const char *key
, const char *value
, struct param
**params
)
85 const char *name
, *param
, *type
;
86 int namelen
, paramlen
, typelen
;
88 const char *colon
= strchr(value
, ':');
90 LOG("Found invalid \"%s=%s\": missing ':'\n",
96 namelen
= colon
- value
;
97 if (strcmp(key
, "parm") == 0) {
99 paramlen
= strlen(param
);
106 typelen
= strlen(type
);
109 it
= add_param(name
, namelen
, param
, paramlen
, type
, typelen
, params
);
111 LOG("Out of memory!\n");
118 static int modinfo_params_do(const struct kmod_list
*list
)
120 const struct kmod_list
*l
;
121 struct param
*params
= NULL
;
124 kmod_list_foreach(l
, list
) {
125 const char *key
= kmod_module_info_get_key(l
);
126 const char *value
= kmod_module_info_get_value(l
);
127 if (strcmp(key
, "parm") != 0 &&
128 strcmp(key
, "parmtype") != 0)
131 err
= process_parm(key
, value
, ¶ms
);
136 while (params
!= NULL
) {
137 struct param
*p
= params
;
140 if (p
->param
== NULL
)
141 printf("%.*s: (%.*s)%c",
142 p
->namelen
, p
->name
, p
->typelen
, p
->type
,
144 else if (p
->type
!= NULL
)
145 printf("%.*s:%.*s (%.*s)%c",
147 p
->paramlen
, p
->param
,
151 printf("%.*s:%.*s%c",
153 p
->paramlen
, p
->param
,
160 while (params
!= NULL
) {
162 params
= params
->next
;
169 static int modinfo_do(struct kmod_module
*mod
)
171 struct kmod_list
*l
, *list
= NULL
;
172 struct param
*params
= NULL
;
175 if (field
!= NULL
&& strcmp(field
, "filename") == 0) {
176 printf("%s%c", kmod_module_get_path(mod
), separator
);
178 } else if (field
== NULL
) {
179 printf("%-16s%s%c", "filename:",
180 kmod_module_get_path(mod
), separator
);
183 err
= kmod_module_get_info(mod
, &list
);
185 LOG("could not get modinfo from '%s': %s\n",
186 kmod_module_get_name(mod
), strerror(-err
));
190 if (field
!= NULL
&& strcmp(field
, "parm") == 0) {
191 err
= modinfo_params_do(list
);
195 kmod_list_foreach(l
, list
) {
196 const char *key
= kmod_module_info_get_key(l
);
197 const char *value
= kmod_module_info_get_value(l
);
201 if (strcmp(field
, key
) != 0)
203 /* filtered output contains no key, just value */
204 printf("%s%c", value
, separator
);
208 if (strcmp(key
, "parm") == 0 || strcmp(key
, "parmtype") == 0) {
209 err
= process_parm(key
, value
, ¶ms
);
215 if (separator
== '\0') {
216 printf("%s=%s%c", key
, value
, separator
);
220 keylen
= strlen(key
);
221 printf("%s:%-*s%s%c", key
, 15 - keylen
, "", value
, separator
);
227 while (params
!= NULL
) {
228 struct param
*p
= params
;
231 if (p
->param
== NULL
)
232 printf("%-16s%.*s:%.*s%c", "parm:",
233 p
->namelen
, p
->name
, p
->typelen
, p
->type
,
235 else if (p
->type
!= NULL
)
236 printf("%-16s%.*s:%.*s (%.*s)%c", "parm:",
238 p
->paramlen
, p
->param
,
242 printf("%-16s%.*s:%.*s%c",
245 p
->paramlen
, p
->param
,
252 while (params
!= NULL
) {
254 params
= params
->next
;
257 kmod_module_info_free_list(list
);
262 static int modinfo_path_do(struct kmod_ctx
*ctx
, const char *path
)
264 struct kmod_module
*mod
;
265 int err
= kmod_module_new_from_path(ctx
, path
, &mod
);
267 LOG("Module file %s not found.\n", path
);
270 err
= modinfo_do(mod
);
271 kmod_module_unref(mod
);
275 static int modinfo_alias_do(struct kmod_ctx
*ctx
, const char *alias
)
277 struct kmod_list
*l
, *filtered
, *list
= NULL
;
278 int err
= kmod_module_new_from_lookup(ctx
, alias
, &list
);
280 LOG("Module alias %s not found.\n", alias
);
285 LOG("Module %s not found.\n", alias
);
289 err
= kmod_module_apply_filter(ctx
, KMOD_FILTER_BUILTIN
, list
, &filtered
);
290 kmod_module_unref_list(list
);
292 LOG("Failed to filter list: %m\n");
296 if (filtered
== NULL
) {
297 LOG("Module %s not found.\n", alias
);
301 kmod_list_foreach(l
, filtered
) {
302 struct kmod_module
*mod
= kmod_module_get_module(l
);
303 int r
= modinfo_do(mod
);
304 kmod_module_unref(mod
);
308 kmod_module_unref_list(filtered
);
312 static const char cmdopts_s
[] = "adlpn0F:k:b:Vh";
313 static const struct option cmdopts
[] = {
314 {"author", no_argument
, 0, 'a'},
315 {"description", no_argument
, 0, 'd'},
316 {"license", no_argument
, 0, 'l'},
317 {"parameters", no_argument
, 0, 'p'},
318 {"filename", no_argument
, 0, 'n'},
319 {"null", no_argument
, 0, '0'},
320 {"field", required_argument
, 0, 'F'},
321 {"set-version", required_argument
, 0, 'k'},
322 {"basedir", required_argument
, 0, 'b'},
323 {"version", no_argument
, 0, 'V'},
324 {"help", no_argument
, 0, 'h'},
328 static void help(const char *progname
)
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",
348 static bool is_module_filename(const char *name
)
353 if (stat(name
, &st
) == 0 && S_ISREG(st
.st_mode
) &&
354 (ptr
= strstr(name
, ".ko")) != NULL
) {
356 * We screened for .ko; make sure this is either at the end of
357 * the name or followed by another '.' (e.g. gz or xz modules)
359 if(ptr
[3] == '\0' || ptr
[3] == '.')
366 static int do_modinfo(int argc
, char *argv
[])
368 struct kmod_ctx
*ctx
;
369 char dirname_buf
[PATH_MAX
];
370 const char *dirname
= NULL
;
371 const char *kversion
= NULL
;
372 const char *root
= NULL
;
373 const char *null_config
= NULL
;
378 c
= getopt_long(argc
, argv
, cmdopts_s
, cmdopts
, &idx
);
386 field
= "description";
410 help(basename(argv
[0]));
413 puts(PACKAGE
" version " VERSION
);
419 "Error: unexpected getopt_long() value '%c'.\n",
425 if (optind
>= argc
) {
426 fprintf(stderr
, "Error: missing module or filename.\n");
430 if (root
!= NULL
|| kversion
!= NULL
) {
434 if (kversion
== NULL
) {
436 fprintf(stderr
, "Error: uname() failed: %s\n",
440 kversion
= u
.release
;
442 snprintf(dirname_buf
, sizeof(dirname_buf
), "%s/lib/modules/%s",
444 dirname
= dirname_buf
;
447 ctx
= kmod_new(dirname
, &null_config
);
449 fputs("Error: kmod_new() failed!\n", stderr
);
454 for (i
= optind
; i
< argc
; i
++) {
455 const char *name
= argv
[i
];
458 if (is_module_filename(name
))
459 r
= modinfo_path_do(ctx
, name
);
461 r
= modinfo_alias_do(ctx
, name
);
468 return err
>= 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
473 const struct kmod_cmd kmod_cmd_compat_modinfo
= {
476 .help
= "compat modinfo command",