3
0

modinfo.c 4.5 KB

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