modinfo.c 3.9 KB

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