modinfo.c 4.1 KB

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