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 (streq(key, "parm")) {
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 (!streq(key, "parm") && !streq(key, "parmtype"))
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;
176 const char *filename = kmod_module_get_path(mod);
178 is_builtin = (filename == NULL);
181 printf("%-16s%s%c", "name:", kmod_module_get_name(mod), separator);
182 filename = "(builtin)";
185 if (field != NULL && streq(field, "filename")) {
186 printf("%s%c", filename, separator);
188 } else if (field == NULL) {
189 printf("%-16s%s%c", "filename:",
190 filename, separator);
193 err = kmod_module_get_info(mod, &list);
195 if (is_builtin && err == -ENOENT) {
197 * This is an old kernel that does not have a file
198 * with information about built-in modules.
202 ERR("could not get modinfo from '%s': %s\n",
203 kmod_module_get_name(mod), strerror(-err));
207 if (field != NULL && streq(field, "parm")) {
208 err = modinfo_params_do(list);
212 kmod_list_foreach(l, list) {
213 const char *key = kmod_module_info_get_key(l);
214 const char *value = kmod_module_info_get_value(l);
218 if (!streq(field, key))
220 /* filtered output contains no key, just value */
221 printf("%s%c", value, separator);
225 if (streq(key, "parm") || streq(key, "parmtype")) {
226 err = process_parm(key, value, ¶ms);
232 if (separator == '\0') {
233 printf("%s=%s%c", key, value, separator);
237 keylen = strlen(key);
238 printf("%s:%-*s%s%c", key, 15 - keylen, "", value, separator);
244 while (params != NULL) {
245 struct param *p = params;
248 if (p->param == NULL)
249 printf("%-16s%.*s:%.*s%c", "parm:",
250 p->namelen, p->name, p->typelen, p->type,
252 else if (p->type != NULL)
253 printf("%-16s%.*s:%.*s (%.*s)%c", "parm:",
255 p->paramlen, p->param,
259 printf("%-16s%.*s:%.*s%c",
262 p->paramlen, p->param,
269 while (params != NULL) {
271 params = params->next;
274 kmod_module_info_free_list(list);
279 static int modinfo_path_do(struct kmod_ctx *ctx, const char *path)
281 struct kmod_module *mod;
282 int err = kmod_module_new_from_path(ctx, path, &mod);
284 ERR("Module file %s not found.\n", path);
287 err = modinfo_do(mod);
288 kmod_module_unref(mod);
292 static int modinfo_alias_do(struct kmod_ctx *ctx, const char *alias)
294 struct kmod_list *l, *list = NULL;
295 int err = kmod_module_new_from_lookup(ctx, alias, &list);
297 ERR("Module alias %s not found.\n", alias);
302 ERR("Module %s not found.\n", alias);
306 kmod_list_foreach(l, list) {
307 struct kmod_module *mod = kmod_module_get_module(l);
308 int r = modinfo_do(mod);
309 kmod_module_unref(mod);
313 kmod_module_unref_list(list);
317 static const char cmdopts_s[] = "adlpn0F:k:b:Vh";
318 static const struct option cmdopts[] = {
319 {"author", no_argument, 0, 'a'},
320 {"description", no_argument, 0, 'd'},
321 {"license", no_argument, 0, 'l'},
322 {"parameters", no_argument, 0, 'p'},
323 {"filename", no_argument, 0, 'n'},
324 {"null", no_argument, 0, '0'},
325 {"field", required_argument, 0, 'F'},
326 {"set-version", required_argument, 0, 'k'},
327 {"basedir", required_argument, 0, 'b'},
328 {"version", no_argument, 0, 'V'},
329 {"help", no_argument, 0, 'h'},
333 static void help(void)
336 "\t%s [options] filename [args]\n"
338 "\t-a, --author Print only 'author'\n"
339 "\t-d, --description Print only 'description'\n"
340 "\t-l, --license Print only 'license'\n"
341 "\t-p, --parameters Print only 'parm'\n"
342 "\t-n, --filename Print only 'filename'\n"
343 "\t-0, --null Use \\0 instead of \\n\n"
344 "\t-F, --field=FIELD Print only provided FIELD\n"
345 "\t-k, --set-version=VERSION Use VERSION instead of `uname -r`\n"
346 "\t-b, --basedir=DIR Use DIR as filesystem root for /lib/modules\n"
347 "\t-V, --version Show version\n"
348 "\t-h, --help Show this help\n",
349 program_invocation_short_name);
352 static bool is_module_filename(const char *name)
356 if (stat(name, &st) == 0 && S_ISREG(st.st_mode) &&
357 path_ends_with_kmod_ext(name, strlen(name)))
363 static int do_modinfo(int argc, char *argv[])
365 struct kmod_ctx *ctx;
366 char dirname_buf[PATH_MAX];
367 const char *dirname = NULL;
368 const char *kversion = NULL;
369 const char *root = NULL;
370 const char *null_config = NULL;
375 c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
383 field = "description";
410 puts(PACKAGE " version " VERSION);
416 ERR("unexpected getopt_long() value '%c'.\n", c);
421 if (optind >= argc) {
422 ERR("missing module or filename.\n");
426 if (root != NULL || kversion != NULL) {
430 if (kversion == NULL) {
432 ERR("uname() failed: %m\n");
435 kversion = u.release;
437 snprintf(dirname_buf, sizeof(dirname_buf), "%s/lib/modules/%s",
439 dirname = dirname_buf;
442 ctx = kmod_new(dirname, &null_config);
444 ERR("kmod_new() failed!\n");
449 for (i = optind; i < argc; i++) {
450 const char *name = argv[i];
453 if (is_module_filename(name))
454 r = modinfo_path_do(ctx, name);
456 r = modinfo_alias_do(ctx, name);
463 return err >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
466 const struct kmod_cmd kmod_cmd_compat_modinfo = {
469 .help = "compat modinfo command",