modinfo.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 << 8) - 1,
  22. OPT_F = (1 << 8), /* field name */
  23. OPT_0 = (1 << 9), /* \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. "description",
  44. "author",
  45. "license",
  46. "vermagic",
  47. "parm",
  48. "firmware",
  49. "depends",
  50. };
  51. size_t len;
  52. int j, length;
  53. char *ptr, *the_module;
  54. const char *field = env->field;
  55. int tags = env->tags;
  56. if (tags & 1) { /* filename */
  57. display(path, shortcuts[0], 1 != tags);
  58. }
  59. len = MAXINT(ssize_t);
  60. the_module = xmalloc_open_zipped_read_close(path, &len);
  61. if (!the_module) {
  62. if (path[0] == '/')
  63. return;
  64. /* Newer depmod puts relative paths in modules.dep */
  65. path = xasprintf("%s/%s/%s", CONFIG_DEFAULT_MODULES_DIR, version, path);
  66. the_module = xmalloc_open_zipped_read_close(path, &len);
  67. free((char*)path);
  68. if (!the_module)
  69. return;
  70. }
  71. if (field)
  72. tags |= OPT_F;
  73. for (j = 1; (1<<j) & (OPT_TAGS + OPT_F); j++) {
  74. const char *pattern;
  75. if (!((1<<j) & tags))
  76. continue;
  77. pattern = field;
  78. if ((1<<j) & OPT_TAGS)
  79. pattern = shortcuts[j];
  80. length = strlen(pattern);
  81. ptr = the_module;
  82. while (1) {
  83. ptr = memchr(ptr, *pattern, len - (ptr - (char*)the_module));
  84. if (ptr == NULL) /* no occurance left, done */
  85. break;
  86. if (strncmp(ptr, pattern, length) == 0 && ptr[length] == '=') {
  87. ptr += length + 1;
  88. ptr += display(ptr, pattern, (1<<j) != tags);
  89. }
  90. ++ptr;
  91. }
  92. }
  93. free(the_module);
  94. }
  95. //usage:#define modinfo_trivial_usage
  96. //usage: "[-adlp0] [-F keyword] MODULE"
  97. //usage:#define modinfo_full_usage "\n\n"
  98. //usage: " -a Shortcut for '-F author'"
  99. //usage: "\n -d Shortcut for '-F description'"
  100. //usage: "\n -l Shortcut for '-F license'"
  101. //usage: "\n -p Shortcut for '-F parm'"
  102. //usage: "\n -F keyword Keyword to look for"
  103. //usage: "\n -0 Separate output with NULs"
  104. //usage:#define modinfo_example_usage
  105. //usage: "$ modinfo -F vermagic loop\n"
  106. int modinfo_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  107. int modinfo_main(int argc UNUSED_PARAM, char **argv)
  108. {
  109. struct modinfo_env env;
  110. char name[MODULE_NAME_LEN];
  111. struct utsname uts;
  112. parser_t *parser;
  113. char *colon, *tokens[2];
  114. unsigned opts;
  115. unsigned i;
  116. env.field = NULL;
  117. opt_complementary = "-1"; /* minimum one param */
  118. opts = getopt32(argv, "fdalvpF:0", &env.field);
  119. env.tags = opts & OPT_TAGS ? opts & OPT_TAGS : OPT_TAGS;
  120. argv += optind;
  121. uname(&uts);
  122. parser = config_open2(
  123. xasprintf("%s/%s/%s", CONFIG_DEFAULT_MODULES_DIR, uts.release, CONFIG_DEFAULT_DEPMOD_FILE),
  124. xfopen_for_read
  125. );
  126. while (config_read(parser, tokens, 2, 1, "# \t", PARSE_NORMAL)) {
  127. colon = last_char_is(tokens[0], ':');
  128. if (colon == NULL)
  129. continue;
  130. *colon = '\0';
  131. filename2modname(tokens[0], name);
  132. for (i = 0; argv[i]; i++) {
  133. if (fnmatch(argv[i], name, 0) == 0) {
  134. modinfo(tokens[0], uts.release, &env);
  135. argv[i] = (char *) "";
  136. }
  137. }
  138. }
  139. if (ENABLE_FEATURE_CLEAN_UP)
  140. config_close(parser);
  141. for (i = 0; argv[i]; i++) {
  142. if (argv[i][0]) {
  143. modinfo(argv[i], uts.release, &env);
  144. }
  145. }
  146. return 0;
  147. }