modinfo.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * modinfo - retrieve module info
  4. * Copyright (c) 2008 Pascal Bellard
  5. *
  6. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  7. */
  8. //applet:IF_MODINFO(APPLET(modinfo, BB_DIR_SBIN, BB_SUID_DROP))
  9. //kbuild:lib-$(CONFIG_MODINFO) += modinfo.o modutils.o
  10. //config:config MODINFO
  11. //config: bool "modinfo"
  12. //config: default y
  13. //config: select PLATFORM_LINUX
  14. //config: help
  15. //config: Show information about a Linux Kernel module
  16. #include <fnmatch.h>
  17. #include <sys/utsname.h> /* uname() */
  18. #include "libbb.h"
  19. #include "modutils.h"
  20. enum {
  21. OPT_TAGS = (1 << 12) - 1, /* shortcut count */
  22. OPT_F = (1 << 12), /* field name */
  23. OPT_0 = (1 << 13), /* \0 as separator */
  24. };
  25. struct modinfo_env {
  26. char *field;
  27. int tags;
  28. };
  29. static void display(const char *data, const char *pattern, int flag)
  30. {
  31. if (flag) {
  32. int n = printf("%s:", pattern);
  33. while (n++ < 16)
  34. bb_putchar(' ');
  35. }
  36. printf("%s%c", data, (option_mask32 & OPT_0) ? '\0' : '\n');
  37. }
  38. static void modinfo(const char *path, const char *version,
  39. const struct modinfo_env *env)
  40. {
  41. static const char *const shortcuts[] = {
  42. "filename",
  43. "license",
  44. "author",
  45. "description",
  46. "version",
  47. "alias",
  48. "srcversion",
  49. "depends",
  50. "uts_release",
  51. "vermagic",
  52. "parm",
  53. "firmware",
  54. };
  55. size_t len;
  56. int j;
  57. char *ptr, *the_module;
  58. const char *field = env->field;
  59. int tags = env->tags;
  60. if (tags & 1) { /* filename */
  61. display(path, shortcuts[0], 1 != tags);
  62. }
  63. len = MAXINT(ssize_t);
  64. the_module = xmalloc_open_zipped_read_close(path, &len);
  65. if (!the_module) {
  66. if (path[0] == '/')
  67. return;
  68. /* Newer depmod puts relative paths in modules.dep */
  69. path = xasprintf("%s/%s/%s", CONFIG_DEFAULT_MODULES_DIR, version, path);
  70. the_module = xmalloc_open_zipped_read_close(path, &len);
  71. free((char*)path);
  72. if (!the_module)
  73. return;
  74. }
  75. if (field)
  76. tags |= OPT_F;
  77. for (j = 1; (1<<j) & (OPT_TAGS + OPT_F); j++) {
  78. const char *pattern;
  79. if (!((1<<j) & tags))
  80. continue;
  81. pattern = field;
  82. if ((1<<j) & OPT_TAGS)
  83. pattern = shortcuts[j];
  84. ptr = the_module;
  85. while (1) {
  86. char *after_pattern;
  87. ptr = memchr(ptr, *pattern, len - (ptr - (char*)the_module));
  88. if (ptr == NULL) /* no occurance left, done */
  89. break;
  90. after_pattern = is_prefixed_with(ptr, pattern);
  91. if (after_pattern && *after_pattern == '=') {
  92. /* field prefixes are 0x80 or 0x00 */
  93. if ((ptr[-1] & 0x7F) == 0x00) {
  94. ptr = after_pattern + 1;
  95. display(ptr, pattern, (1<<j) != tags);
  96. ptr += strlen(ptr);
  97. }
  98. }
  99. ++ptr;
  100. }
  101. }
  102. free(the_module);
  103. }
  104. //usage:#define modinfo_trivial_usage
  105. //usage: "[-adlp0] [-F keyword] MODULE"
  106. //usage:#define modinfo_full_usage "\n\n"
  107. //usage: " -a Shortcut for '-F author'"
  108. //usage: "\n -d Shortcut for '-F description'"
  109. //usage: "\n -l Shortcut for '-F license'"
  110. //usage: "\n -p Shortcut for '-F parm'"
  111. //usage: "\n -F keyword Keyword to look for"
  112. //usage: "\n -0 Separate output with NULs"
  113. //usage:#define modinfo_example_usage
  114. //usage: "$ modinfo -F vermagic loop\n"
  115. int modinfo_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  116. int modinfo_main(int argc UNUSED_PARAM, char **argv)
  117. {
  118. struct modinfo_env env;
  119. char name[MODULE_NAME_LEN];
  120. struct utsname uts;
  121. parser_t *parser;
  122. char *colon, *tokens[2];
  123. unsigned opts;
  124. unsigned i;
  125. env.field = NULL;
  126. opt_complementary = "-1"; /* minimum one param */
  127. opts = getopt32(argv, "nladvAsDumpF:0", &env.field);
  128. env.tags = opts & OPT_TAGS ? opts & OPT_TAGS : OPT_TAGS;
  129. argv += optind;
  130. uname(&uts);
  131. parser = config_open2(
  132. xasprintf("%s/%s/%s", CONFIG_DEFAULT_MODULES_DIR, uts.release, CONFIG_DEFAULT_DEPMOD_FILE),
  133. xfopen_for_read
  134. );
  135. while (config_read(parser, tokens, 2, 1, "# \t", PARSE_NORMAL)) {
  136. colon = last_char_is(tokens[0], ':');
  137. if (colon == NULL)
  138. continue;
  139. *colon = '\0';
  140. filename2modname(bb_basename(tokens[0]), name);
  141. for (i = 0; argv[i]; i++) {
  142. if (fnmatch(argv[i], name, 0) == 0) {
  143. modinfo(tokens[0], uts.release, &env);
  144. argv[i] = (char *) "";
  145. }
  146. }
  147. }
  148. if (ENABLE_FEATURE_CLEAN_UP)
  149. config_close(parser);
  150. for (i = 0; argv[i]; i++) {
  151. if (argv[i][0]) {
  152. modinfo(argv[i], uts.release, &env);
  153. }
  154. }
  155. return 0;
  156. }