modinfo.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 int 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. return 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, length;
  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. length = strlen(pattern);
  85. ptr = the_module;
  86. while (1) {
  87. ptr = memchr(ptr, *pattern, len - (ptr - (char*)the_module));
  88. if (ptr == NULL) /* no occurance left, done */
  89. break;
  90. if (strncmp(ptr, pattern, length) == 0 && ptr[length] == '=') {
  91. /* field prefixes are 0x80 or 0x00 */
  92. if ((ptr[-1] & 0x7F) == '\0') {
  93. ptr += length + 1;
  94. ptr += display(ptr, pattern, (1<<j) != tags);
  95. }
  96. }
  97. ++ptr;
  98. }
  99. }
  100. free(the_module);
  101. }
  102. //usage:#define modinfo_trivial_usage
  103. //usage: "[-adlp0] [-F keyword] MODULE"
  104. //usage:#define modinfo_full_usage "\n\n"
  105. //usage: " -a Shortcut for '-F author'"
  106. //usage: "\n -d Shortcut for '-F description'"
  107. //usage: "\n -l Shortcut for '-F license'"
  108. //usage: "\n -p Shortcut for '-F parm'"
  109. //usage: "\n -F keyword Keyword to look for"
  110. //usage: "\n -0 Separate output with NULs"
  111. //usage:#define modinfo_example_usage
  112. //usage: "$ modinfo -F vermagic loop\n"
  113. int modinfo_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  114. int modinfo_main(int argc UNUSED_PARAM, char **argv)
  115. {
  116. struct modinfo_env env;
  117. char name[MODULE_NAME_LEN];
  118. struct utsname uts;
  119. parser_t *parser;
  120. char *colon, *tokens[2];
  121. unsigned opts;
  122. unsigned i;
  123. env.field = NULL;
  124. opt_complementary = "-1"; /* minimum one param */
  125. opts = getopt32(argv, "nladvAsDumpF:0", &env.field);
  126. env.tags = opts & OPT_TAGS ? opts & OPT_TAGS : OPT_TAGS;
  127. argv += optind;
  128. uname(&uts);
  129. parser = config_open2(
  130. xasprintf("%s/%s/%s", CONFIG_DEFAULT_MODULES_DIR, uts.release, CONFIG_DEFAULT_DEPMOD_FILE),
  131. xfopen_for_read
  132. );
  133. while (config_read(parser, tokens, 2, 1, "# \t", PARSE_NORMAL)) {
  134. colon = last_char_is(tokens[0], ':');
  135. if (colon == NULL)
  136. continue;
  137. *colon = '\0';
  138. filename2modname(tokens[0], name);
  139. for (i = 0; argv[i]; i++) {
  140. if (fnmatch(argv[i], name, 0) == 0) {
  141. modinfo(tokens[0], uts.release, &env);
  142. argv[i] = (char *) "";
  143. }
  144. }
  145. }
  146. if (ENABLE_FEATURE_CLEAN_UP)
  147. config_close(parser);
  148. for (i = 0; argv[i]; i++) {
  149. if (argv[i][0]) {
  150. modinfo(argv[i], uts.release, &env);
  151. }
  152. }
  153. return 0;
  154. }