Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 1 | /* |
| 2 | * kmod-modinfo - query kernel module information using libkmod. |
| 3 | * |
Lucas De Marchi | e6b0e49 | 2013-01-16 11:27:21 -0200 | [diff] [blame] | 4 | * Copyright (C) 2011-2013 ProFUSION embedded systems |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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/>. |
| 18 | */ |
| 19 | |
Lucas De Marchi | c2e4286 | 2014-10-03 01:41:42 -0300 | [diff] [blame] | 20 | #include <errno.h> |
| 21 | #include <getopt.h> |
| 22 | #include <limits.h> |
| 23 | #include <stdbool.h> |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 26 | #include <string.h> |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 27 | #include <sys/stat.h> |
Lucas De Marchi | c2e4286 | 2014-10-03 01:41:42 -0300 | [diff] [blame] | 28 | #include <sys/utsname.h> |
| 29 | |
Lucas De Marchi | f4e8c16 | 2014-10-09 01:14:16 -0300 | [diff] [blame] | 30 | #include <shared/util.h> |
| 31 | |
Lucas De Marchi | f357866 | 2015-01-02 12:38:27 -0200 | [diff] [blame] | 32 | #include <libkmod/libkmod.h> |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 33 | |
Lucas De Marchi | 4a2e20d | 2012-11-06 16:54:17 -0200 | [diff] [blame] | 34 | #include "kmod.h" |
| 35 | |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 36 | static char separator = '\n'; |
| 37 | static const char *field = NULL; |
| 38 | |
| 39 | struct param { |
| 40 | struct param *next; |
| 41 | const char *name; |
| 42 | const char *param; |
| 43 | const char *type; |
| 44 | int namelen; |
| 45 | int paramlen; |
| 46 | int typelen; |
| 47 | }; |
| 48 | |
| 49 | static struct param *add_param(const char *name, int namelen, const char *param, int paramlen, const char *type, int typelen, struct param **list) |
| 50 | { |
| 51 | struct param *it; |
| 52 | |
| 53 | for (it = *list; it != NULL; it = it->next) { |
| 54 | if (it->namelen == namelen && |
| 55 | memcmp(it->name, name, namelen) == 0) |
| 56 | break; |
| 57 | } |
| 58 | |
| 59 | if (it == NULL) { |
| 60 | it = malloc(sizeof(struct param)); |
| 61 | if (it == NULL) |
| 62 | return NULL; |
| 63 | it->next = *list; |
| 64 | *list = it; |
| 65 | it->name = name; |
| 66 | it->namelen = namelen; |
| 67 | it->param = NULL; |
| 68 | it->type = NULL; |
| 69 | it->paramlen = 0; |
| 70 | it->typelen = 0; |
| 71 | } |
| 72 | |
| 73 | if (param != NULL) { |
| 74 | it->param = param; |
| 75 | it->paramlen = paramlen; |
| 76 | } |
| 77 | |
| 78 | if (type != NULL) { |
| 79 | it->type = type; |
| 80 | it->typelen = typelen; |
| 81 | } |
| 82 | |
| 83 | return it; |
| 84 | } |
| 85 | |
Gustavo Sverzut Barbieri | b014c49 | 2011-12-19 18:31:15 -0200 | [diff] [blame] | 86 | static int process_parm(const char *key, const char *value, struct param **params) |
| 87 | { |
| 88 | const char *name, *param, *type; |
| 89 | int namelen, paramlen, typelen; |
| 90 | struct param *it; |
| 91 | const char *colon = strchr(value, ':'); |
| 92 | if (colon == NULL) { |
Lucas De Marchi | 9382dbf | 2012-11-05 17:58:57 -0200 | [diff] [blame] | 93 | ERR("Found invalid \"%s=%s\": missing ':'\n", |
Gustavo Sverzut Barbieri | b014c49 | 2011-12-19 18:31:15 -0200 | [diff] [blame] | 94 | key, value); |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | name = value; |
| 99 | namelen = colon - value; |
Lucas De Marchi | 5b0436a | 2015-01-14 14:22:23 -0200 | [diff] [blame] | 100 | if (streq(key, "parm")) { |
Gustavo Sverzut Barbieri | b014c49 | 2011-12-19 18:31:15 -0200 | [diff] [blame] | 101 | param = colon + 1; |
| 102 | paramlen = strlen(param); |
| 103 | type = NULL; |
| 104 | typelen = 0; |
| 105 | } else { |
| 106 | param = NULL; |
| 107 | paramlen = 0; |
| 108 | type = colon + 1; |
| 109 | typelen = strlen(type); |
| 110 | } |
| 111 | |
| 112 | it = add_param(name, namelen, param, paramlen, type, typelen, params); |
| 113 | if (it == NULL) { |
Lucas De Marchi | 9382dbf | 2012-11-05 17:58:57 -0200 | [diff] [blame] | 114 | ERR("Out of memory!\n"); |
Gustavo Sverzut Barbieri | b014c49 | 2011-12-19 18:31:15 -0200 | [diff] [blame] | 115 | return -ENOMEM; |
| 116 | } |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static int modinfo_params_do(const struct kmod_list *list) |
| 122 | { |
| 123 | const struct kmod_list *l; |
| 124 | struct param *params = NULL; |
| 125 | int err = 0; |
| 126 | |
| 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); |
Lucas De Marchi | 5b0436a | 2015-01-14 14:22:23 -0200 | [diff] [blame] | 130 | if (!streq(key, "parm") && !streq(key, "parmtype")) |
Gustavo Sverzut Barbieri | b014c49 | 2011-12-19 18:31:15 -0200 | [diff] [blame] | 131 | continue; |
| 132 | |
| 133 | err = process_parm(key, value, ¶ms); |
| 134 | if (err < 0) |
| 135 | goto end; |
| 136 | } |
| 137 | |
| 138 | while (params != NULL) { |
| 139 | struct param *p = params; |
| 140 | params = p->next; |
| 141 | |
| 142 | if (p->param == NULL) |
| 143 | printf("%.*s: (%.*s)%c", |
| 144 | p->namelen, p->name, p->typelen, p->type, |
| 145 | separator); |
| 146 | else if (p->type != NULL) |
| 147 | printf("%.*s:%.*s (%.*s)%c", |
| 148 | p->namelen, p->name, |
| 149 | p->paramlen, p->param, |
| 150 | p->typelen, p->type, |
| 151 | separator); |
| 152 | else |
| 153 | printf("%.*s:%.*s%c", |
| 154 | p->namelen, p->name, |
| 155 | p->paramlen, p->param, |
| 156 | separator); |
| 157 | |
| 158 | free(p); |
| 159 | } |
| 160 | |
| 161 | end: |
| 162 | while (params != NULL) { |
| 163 | void *tmp = params; |
| 164 | params = params->next; |
| 165 | free(tmp); |
| 166 | } |
| 167 | |
| 168 | return err; |
| 169 | } |
| 170 | |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 171 | static int modinfo_do(struct kmod_module *mod) |
| 172 | { |
| 173 | struct kmod_list *l, *list = NULL; |
| 174 | struct param *params = NULL; |
| 175 | int err; |
| 176 | |
Lucas De Marchi | 5b0436a | 2015-01-14 14:22:23 -0200 | [diff] [blame] | 177 | if (field != NULL && streq(field, "filename")) { |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 178 | printf("%s%c", kmod_module_get_path(mod), separator); |
| 179 | return 0; |
| 180 | } else if (field == NULL) { |
| 181 | printf("%-16s%s%c", "filename:", |
| 182 | kmod_module_get_path(mod), separator); |
| 183 | } |
| 184 | |
| 185 | err = kmod_module_get_info(mod, &list); |
| 186 | if (err < 0) { |
Lucas De Marchi | 9382dbf | 2012-11-05 17:58:57 -0200 | [diff] [blame] | 187 | ERR("could not get modinfo from '%s': %s\n", |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 188 | kmod_module_get_name(mod), strerror(-err)); |
| 189 | return err; |
| 190 | } |
| 191 | |
Lucas De Marchi | 5b0436a | 2015-01-14 14:22:23 -0200 | [diff] [blame] | 192 | if (field != NULL && streq(field, "parm")) { |
Gustavo Sverzut Barbieri | b014c49 | 2011-12-19 18:31:15 -0200 | [diff] [blame] | 193 | err = modinfo_params_do(list); |
| 194 | goto end; |
| 195 | } |
| 196 | |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 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); |
| 200 | int keylen; |
| 201 | |
| 202 | if (field != NULL) { |
Lucas De Marchi | 5b0436a | 2015-01-14 14:22:23 -0200 | [diff] [blame] | 203 | if (!streq(field, key)) |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 204 | continue; |
| 205 | /* filtered output contains no key, just value */ |
| 206 | printf("%s%c", value, separator); |
| 207 | continue; |
| 208 | } |
| 209 | |
Lucas De Marchi | 5b0436a | 2015-01-14 14:22:23 -0200 | [diff] [blame] | 210 | if (streq(key, "parm") || streq(key, "parmtype")) { |
Gustavo Sverzut Barbieri | b014c49 | 2011-12-19 18:31:15 -0200 | [diff] [blame] | 211 | err = process_parm(key, value, ¶ms); |
| 212 | if (err < 0) |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 213 | goto end; |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 214 | continue; |
| 215 | } |
| 216 | |
| 217 | if (separator == '\0') { |
| 218 | printf("%s=%s%c", key, value, separator); |
| 219 | continue; |
| 220 | } |
| 221 | |
| 222 | keylen = strlen(key); |
| 223 | printf("%s:%-*s%s%c", key, 15 - keylen, "", value, separator); |
| 224 | } |
| 225 | |
| 226 | if (field != NULL) |
| 227 | goto end; |
| 228 | |
| 229 | while (params != NULL) { |
| 230 | struct param *p = params; |
| 231 | params = p->next; |
| 232 | |
| 233 | if (p->param == NULL) |
| 234 | printf("%-16s%.*s:%.*s%c", "parm:", |
| 235 | p->namelen, p->name, p->typelen, p->type, |
| 236 | separator); |
| 237 | else if (p->type != NULL) |
Gustavo Sverzut Barbieri | 515ec79 | 2011-12-19 17:41:09 -0200 | [diff] [blame] | 238 | printf("%-16s%.*s:%.*s (%.*s)%c", "parm:", |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 239 | p->namelen, p->name, |
| 240 | p->paramlen, p->param, |
| 241 | p->typelen, p->type, |
| 242 | separator); |
| 243 | else |
Gustavo Sverzut Barbieri | 515ec79 | 2011-12-19 17:41:09 -0200 | [diff] [blame] | 244 | printf("%-16s%.*s:%.*s%c", |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 245 | "parm:", |
| 246 | p->namelen, p->name, |
| 247 | p->paramlen, p->param, |
| 248 | separator); |
| 249 | |
| 250 | free(p); |
| 251 | } |
| 252 | |
| 253 | end: |
| 254 | while (params != NULL) { |
| 255 | void *tmp = params; |
| 256 | params = params->next; |
| 257 | free(tmp); |
| 258 | } |
| 259 | kmod_module_info_free_list(list); |
| 260 | |
| 261 | return err; |
| 262 | } |
| 263 | |
| 264 | static int modinfo_path_do(struct kmod_ctx *ctx, const char *path) |
| 265 | { |
| 266 | struct kmod_module *mod; |
| 267 | int err = kmod_module_new_from_path(ctx, path, &mod); |
| 268 | if (err < 0) { |
Lucas De Marchi | 9382dbf | 2012-11-05 17:58:57 -0200 | [diff] [blame] | 269 | ERR("Module file %s not found.\n", path); |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 270 | return err; |
| 271 | } |
| 272 | err = modinfo_do(mod); |
| 273 | kmod_module_unref(mod); |
| 274 | return err; |
| 275 | } |
| 276 | |
| 277 | static int modinfo_alias_do(struct kmod_ctx *ctx, const char *alias) |
| 278 | { |
Dave Reisner | 3e4c6af | 2012-02-24 10:13:16 -0500 | [diff] [blame] | 279 | struct kmod_list *l, *filtered, *list = NULL; |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 280 | int err = kmod_module_new_from_lookup(ctx, alias, &list); |
| 281 | if (err < 0) { |
Lucas De Marchi | 9382dbf | 2012-11-05 17:58:57 -0200 | [diff] [blame] | 282 | ERR("Module alias %s not found.\n", alias); |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 283 | return err; |
| 284 | } |
Dave Reisner | 5f85a13 | 2012-01-05 22:56:53 -0500 | [diff] [blame] | 285 | |
| 286 | if (list == NULL) { |
Lucas De Marchi | 9382dbf | 2012-11-05 17:58:57 -0200 | [diff] [blame] | 287 | ERR("Module %s not found.\n", alias); |
Dave Reisner | 5f85a13 | 2012-01-05 22:56:53 -0500 | [diff] [blame] | 288 | return -ENOENT; |
| 289 | } |
| 290 | |
Dave Reisner | 3e4c6af | 2012-02-24 10:13:16 -0500 | [diff] [blame] | 291 | err = kmod_module_apply_filter(ctx, KMOD_FILTER_BUILTIN, list, &filtered); |
| 292 | kmod_module_unref_list(list); |
| 293 | if (err < 0) { |
Lucas De Marchi | 9382dbf | 2012-11-05 17:58:57 -0200 | [diff] [blame] | 294 | ERR("Failed to filter list: %m\n"); |
Dave Reisner | 3e4c6af | 2012-02-24 10:13:16 -0500 | [diff] [blame] | 295 | return err; |
| 296 | } |
| 297 | |
| 298 | if (filtered == NULL) { |
Lucas De Marchi | 9382dbf | 2012-11-05 17:58:57 -0200 | [diff] [blame] | 299 | ERR("Module %s not found.\n", alias); |
Dave Reisner | 3e4c6af | 2012-02-24 10:13:16 -0500 | [diff] [blame] | 300 | return -ENOENT; |
| 301 | } |
| 302 | |
| 303 | kmod_list_foreach(l, filtered) { |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 304 | struct kmod_module *mod = kmod_module_get_module(l); |
| 305 | int r = modinfo_do(mod); |
| 306 | kmod_module_unref(mod); |
| 307 | if (r < 0) |
| 308 | err = r; |
| 309 | } |
Dave Reisner | 3e4c6af | 2012-02-24 10:13:16 -0500 | [diff] [blame] | 310 | kmod_module_unref_list(filtered); |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 311 | return err; |
| 312 | } |
| 313 | |
Gustavo Sverzut Barbieri | 022e1f0 | 2011-12-19 17:28:06 -0200 | [diff] [blame] | 314 | static const char cmdopts_s[] = "adlpn0F:k:b:Vh"; |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 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'}, |
| 327 | {NULL, 0, 0, 0} |
| 328 | }; |
| 329 | |
Lucas De Marchi | 4a2e20d | 2012-11-06 16:54:17 -0200 | [diff] [blame] | 330 | static void help(void) |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 331 | { |
Lucas De Marchi | 34e06bf | 2012-11-06 17:32:41 -0200 | [diff] [blame] | 332 | printf("Usage:\n" |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 333 | "\t%s [options] filename [args]\n" |
| 334 | "Options:\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" |
Dave Reisner | c5b37db | 2012-09-27 11:00:42 -0400 | [diff] [blame] | 343 | "\t-b, --basedir=DIR Use DIR as filesystem root for /lib/modules\n" |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 344 | "\t-V, --version Show version\n" |
| 345 | "\t-h, --help Show this help\n", |
Lucas De Marchi | 7c04aee | 2012-11-06 19:20:09 -0200 | [diff] [blame] | 346 | program_invocation_short_name); |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 347 | } |
| 348 | |
Dan McGee | a23f0c9 | 2012-02-03 20:25:00 -0600 | [diff] [blame] | 349 | static bool is_module_filename(const char *name) |
| 350 | { |
| 351 | struct stat st; |
Dan McGee | a23f0c9 | 2012-02-03 20:25:00 -0600 | [diff] [blame] | 352 | |
| 353 | if (stat(name, &st) == 0 && S_ISREG(st.st_mode) && |
Aleksey Makarov | 6f02b6f | 2012-11-28 10:44:00 +0700 | [diff] [blame] | 354 | path_ends_with_kmod_ext(name, strlen(name))) |
Dan McGee | a23f0c9 | 2012-02-03 20:25:00 -0600 | [diff] [blame] | 355 | return true; |
Dan McGee | a23f0c9 | 2012-02-03 20:25:00 -0600 | [diff] [blame] | 356 | |
| 357 | return false; |
| 358 | } |
| 359 | |
Lucas De Marchi | 769becb | 2011-12-22 03:50:54 -0200 | [diff] [blame] | 360 | static int do_modinfo(int argc, char *argv[]) |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 361 | { |
| 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; |
| 368 | int i, err; |
| 369 | |
| 370 | for (;;) { |
| 371 | int c, idx = 0; |
| 372 | c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx); |
| 373 | if (c == -1) |
| 374 | break; |
| 375 | switch (c) { |
| 376 | case 'a': |
| 377 | field = "author"; |
| 378 | break; |
| 379 | case 'd': |
| 380 | field = "description"; |
| 381 | break; |
| 382 | case 'l': |
| 383 | field = "license"; |
| 384 | break; |
| 385 | case 'p': |
| 386 | field = "parm"; |
| 387 | break; |
| 388 | case 'n': |
| 389 | field = "filename"; |
| 390 | break; |
| 391 | case '0': |
| 392 | separator = '\0'; |
| 393 | break; |
| 394 | case 'F': |
| 395 | field = optarg; |
| 396 | break; |
| 397 | case 'k': |
| 398 | kversion = optarg; |
| 399 | break; |
| 400 | case 'b': |
| 401 | root = optarg; |
| 402 | break; |
| 403 | case 'h': |
Lucas De Marchi | 4a2e20d | 2012-11-06 16:54:17 -0200 | [diff] [blame] | 404 | help(); |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 405 | return EXIT_SUCCESS; |
| 406 | case 'V': |
| 407 | puts(PACKAGE " version " VERSION); |
Lucas De Marchi | 655de27 | 2015-06-07 02:46:22 -0300 | [diff] [blame] | 408 | puts(KMOD_FEATURES); |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 409 | return EXIT_SUCCESS; |
| 410 | case '?': |
| 411 | return EXIT_FAILURE; |
| 412 | default: |
Lucas De Marchi | 9382dbf | 2012-11-05 17:58:57 -0200 | [diff] [blame] | 413 | ERR("unexpected getopt_long() value '%c'.\n", c); |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 414 | return EXIT_FAILURE; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | if (optind >= argc) { |
Lucas De Marchi | 9382dbf | 2012-11-05 17:58:57 -0200 | [diff] [blame] | 419 | ERR("missing module or filename.\n"); |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 420 | return EXIT_FAILURE; |
| 421 | } |
| 422 | |
| 423 | if (root != NULL || kversion != NULL) { |
| 424 | struct utsname u; |
| 425 | if (root == NULL) |
| 426 | root = ""; |
| 427 | if (kversion == NULL) { |
| 428 | if (uname(&u) < 0) { |
Lucas De Marchi | 9382dbf | 2012-11-05 17:58:57 -0200 | [diff] [blame] | 429 | ERR("uname() failed: %m\n"); |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 430 | return EXIT_FAILURE; |
| 431 | } |
| 432 | kversion = u.release; |
| 433 | } |
Dave Reisner | c5b37db | 2012-09-27 11:00:42 -0400 | [diff] [blame] | 434 | snprintf(dirname_buf, sizeof(dirname_buf), "%s/lib/modules/%s", |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 435 | root, kversion); |
| 436 | dirname = dirname_buf; |
| 437 | } |
| 438 | |
| 439 | ctx = kmod_new(dirname, &null_config); |
| 440 | if (!ctx) { |
Lucas De Marchi | 9382dbf | 2012-11-05 17:58:57 -0200 | [diff] [blame] | 441 | ERR("kmod_new() failed!\n"); |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 442 | return EXIT_FAILURE; |
| 443 | } |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 444 | |
| 445 | err = 0; |
| 446 | for (i = optind; i < argc; i++) { |
| 447 | const char *name = argv[i]; |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 448 | int r; |
| 449 | |
Dan McGee | a23f0c9 | 2012-02-03 20:25:00 -0600 | [diff] [blame] | 450 | if (is_module_filename(name)) |
Gustavo Sverzut Barbieri | 0cc3ccf | 2011-12-19 14:48:07 -0200 | [diff] [blame] | 451 | r = modinfo_path_do(ctx, name); |
| 452 | else |
| 453 | r = modinfo_alias_do(ctx, name); |
| 454 | |
| 455 | if (r < 0) |
| 456 | err = r; |
| 457 | } |
| 458 | |
| 459 | kmod_unref(ctx); |
| 460 | return err >= 0 ? EXIT_SUCCESS : EXIT_FAILURE; |
| 461 | } |
Lucas De Marchi | 769becb | 2011-12-22 03:50:54 -0200 | [diff] [blame] | 462 | |
Lucas De Marchi | 769becb | 2011-12-22 03:50:54 -0200 | [diff] [blame] | 463 | const struct kmod_cmd kmod_cmd_compat_modinfo = { |
| 464 | .name = "modinfo", |
| 465 | .cmd = do_modinfo, |
| 466 | .help = "compat modinfo command", |
| 467 | }; |