modinfo.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. //config:config MODINFO
  9. //config: bool "modinfo (24 kb)"
  10. //config: default y
  11. //config: help
  12. //config: Show information about a Linux Kernel module
  13. //applet:IF_MODINFO(APPLET_NOEXEC(modinfo, modinfo, BB_DIR_SBIN, BB_SUID_DROP, modinfo))
  14. //kbuild:lib-$(CONFIG_MODINFO) += modinfo.o modutils.o
  15. #include <fnmatch.h>
  16. #include <sys/utsname.h> /* uname() */
  17. #include "libbb.h"
  18. #include "modutils.h"
  19. static const char *const shortcuts[] ALIGN_PTR = {
  20. "filename", // -n
  21. "author", // -a
  22. "description", // -d
  23. "license", // -l
  24. "parm", // -p
  25. "version", // the rest has no shortcut options
  26. "alias",
  27. "srcversion",
  28. "depends",
  29. "uts_release",
  30. "intree",
  31. "vermagic",
  32. "firmware",
  33. };
  34. enum {
  35. OPT_0 = (1 << 0), /* \0 as separator */
  36. OPT_F = (1 << 1), /* field name */
  37. /* first bits are for -nadlp options, the rest are for
  38. * fields not selectable with "shortcut" options
  39. */
  40. OPT_n = (1 << 2),
  41. OPT_TAGS = ((1 << ARRAY_SIZE(shortcuts)) - 1) << 2,
  42. };
  43. static void display(const char *data, const char *pattern)
  44. {
  45. int flag = option_mask32 >> 1; /* shift out -0 bit */
  46. if (flag & (flag-1)) {
  47. /* more than one field to show: print "FIELD:" pfx */
  48. int n = printf("%s:", pattern);
  49. while (n++ < 16)
  50. bb_putchar(' ');
  51. }
  52. printf("%s%c", data, (option_mask32 & OPT_0) ? '\0' : '\n');
  53. }
  54. static void modinfo(const char *path, const char *version,
  55. const char *field)
  56. {
  57. size_t len;
  58. int j;
  59. char *ptr, *the_module;
  60. char *allocated;
  61. int tags = option_mask32;
  62. allocated = NULL;
  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 = allocated = xasprintf("%s/%s/%s", CONFIG_DEFAULT_MODULES_DIR, version, path);
  70. the_module = xmalloc_open_zipped_read_close(path, &len);
  71. if (!the_module) {
  72. bb_error_msg("module '%s' not found", path);
  73. goto ret;
  74. }
  75. }
  76. for (j = 1; (1<<j) & (OPT_TAGS|OPT_F); j++) {
  77. const char *pattern;
  78. if (!((1<<j) & tags))
  79. continue;
  80. pattern = field;
  81. if ((1<<j) & OPT_TAGS)
  82. pattern = shortcuts[j-2];
  83. if (strcmp(pattern, shortcuts[0]) == 0) {
  84. /* "-n" or "-F filename" */
  85. display(path, shortcuts[0]);
  86. continue;
  87. }
  88. ptr = the_module;
  89. while (1) {
  90. char *after_pattern;
  91. ptr = memchr(ptr, *pattern, len - (ptr - (char*)the_module));
  92. if (ptr == NULL) /* no occurrence left, done */
  93. break;
  94. after_pattern = is_prefixed_with(ptr, pattern);
  95. if (after_pattern && *after_pattern == '=') {
  96. /* field prefixes are 0x80 or 0x00 */
  97. if ((ptr[-1] & 0x7F) == 0x00) {
  98. ptr = after_pattern + 1;
  99. display(ptr, pattern);
  100. ptr += strlen(ptr);
  101. }
  102. }
  103. ++ptr;
  104. }
  105. }
  106. free(the_module);
  107. ret:
  108. free(allocated);
  109. }
  110. //usage:#define modinfo_trivial_usage
  111. //usage: "[-adlpn0] [-F keyword] MODULE"
  112. //usage:#define modinfo_full_usage "\n\n"
  113. //usage: " -a Shortcut for '-F author'"
  114. //usage: "\n -d Shortcut for '-F description'"
  115. //usage: "\n -l Shortcut for '-F license'"
  116. //usage: "\n -p Shortcut for '-F parm'"
  117. ////usage: "\n -n Shortcut for '-F filename'"
  118. //usage: "\n -F keyword Keyword to look for"
  119. //usage: "\n -0 Separate output with NULs"
  120. //usage:#define modinfo_example_usage
  121. //usage: "$ modinfo -F vermagic loop\n"
  122. int modinfo_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  123. int modinfo_main(int argc UNUSED_PARAM, char **argv)
  124. {
  125. const char *field;
  126. char name[MODULE_NAME_LEN];
  127. struct utsname uts;
  128. parser_t *parser;
  129. char *colon, *tokens[2];
  130. unsigned opts;
  131. unsigned i;
  132. field = NULL;
  133. opts = getopt32(argv, "^" "0F:nadlp" "\0" "-1"/*minimum one arg*/, &field);
  134. /* If no field selected, show all */
  135. if (!(opts & (OPT_TAGS|OPT_F)))
  136. option_mask32 |= OPT_TAGS;
  137. argv += optind;
  138. uname(&uts);
  139. parser = config_open2(
  140. xasprintf("%s/%s/%s", CONFIG_DEFAULT_MODULES_DIR, uts.release, CONFIG_DEFAULT_DEPMOD_FILE),
  141. xfopen_for_read
  142. );
  143. while (config_read(parser, tokens, 2, 1, "# \t", PARSE_NORMAL)) {
  144. colon = last_char_is(tokens[0], ':');
  145. if (colon == NULL)
  146. continue;
  147. *colon = '\0';
  148. filename2modname(bb_basename(tokens[0]), name);
  149. for (i = 0; argv[i]; i++) {
  150. if (fnmatch(argv[i], name, 0) == 0) {
  151. modinfo(tokens[0], uts.release, field);
  152. argv[i] = (char *) "";
  153. }
  154. }
  155. }
  156. if (ENABLE_FEATURE_CLEAN_UP)
  157. config_close(parser);
  158. for (i = 0; argv[i]; i++) {
  159. if (argv[i][0]) {
  160. modinfo(argv[i], uts.release, field);
  161. }
  162. }
  163. return 0;
  164. }