lsmod.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini lsmod implementation for busybox
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
  7. *
  8. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  9. */
  10. //applet:IF_LSMOD(APPLET(lsmod, _BB_DIR_SBIN, _BB_SUID_DROP))
  11. //usage:#if !ENABLE_MODPROBE_SMALL
  12. //usage:#define lsmod_trivial_usage
  13. //usage: ""
  14. //usage:#define lsmod_full_usage "\n\n"
  15. //usage: "List the currently loaded kernel modules"
  16. //usage:#endif
  17. #include "libbb.h"
  18. #include "unicode.h"
  19. #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
  20. enum {
  21. TAINT_PROPRIETORY_MODULE = (1 << 0),
  22. TAINT_FORCED_MODULE = (1 << 1),
  23. TAINT_UNSAFE_SMP = (1 << 2),
  24. };
  25. static void check_tainted(void)
  26. {
  27. int tainted = 0;
  28. char *buf = xmalloc_open_read_close("/proc/sys/kernel/tainted", NULL);
  29. if (buf) {
  30. tainted = atoi(buf);
  31. if (ENABLE_FEATURE_CLEAN_UP)
  32. free(buf);
  33. }
  34. if (tainted) {
  35. printf(" Tainted: %c%c%c\n",
  36. tainted & TAINT_PROPRIETORY_MODULE ? 'P' : 'G',
  37. tainted & TAINT_FORCED_MODULE ? 'F' : ' ',
  38. tainted & TAINT_UNSAFE_SMP ? 'S' : ' ');
  39. } else {
  40. puts(" Not tainted");
  41. }
  42. }
  43. #else
  44. static void check_tainted(void) { putchar('\n'); }
  45. #endif
  46. int lsmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  47. int lsmod_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
  48. {
  49. #if ENABLE_FEATURE_LSMOD_PRETTY_2_6_OUTPUT
  50. char *token[4];
  51. parser_t *parser = config_open("/proc/modules");
  52. init_unicode();
  53. printf("%-24sSize Used by", "Module");
  54. check_tainted();
  55. if (ENABLE_FEATURE_2_4_MODULES
  56. && get_linux_version_code() < KERNEL_VERSION(2,6,0)
  57. ) {
  58. while (config_read(parser, token, 4, 3, "# \t", PARSE_NORMAL)) {
  59. if (token[3] != NULL && token[3][0] == '[') {
  60. token[3]++;
  61. token[3][strlen(token[3])-1] = '\0';
  62. } else
  63. token[3] = (char *) "";
  64. # if ENABLE_UNICODE_SUPPORT
  65. {
  66. uni_stat_t uni_stat;
  67. char *uni_name = unicode_conv_to_printable(&uni_stat, token[0]);
  68. unsigned pad_len = (uni_stat.unicode_width > 19) ? 0 : 19 - uni_stat.unicode_width;
  69. printf("%s%*s %8s %2s %s\n", uni_name, pad_len, "", token[1], token[2], token[3]);
  70. free(uni_name);
  71. }
  72. # else
  73. printf("%-19s %8s %2s %s\n", token[0], token[1], token[2], token[3]);
  74. # endif
  75. }
  76. } else {
  77. while (config_read(parser, token, 4, 4, "# \t", PARSE_NORMAL & ~PARSE_GREEDY)) {
  78. // N.B. token[3] is either '-' (module is not used by others)
  79. // or comma-separated list ended by comma
  80. // so trimming the trailing char is just what we need!
  81. token[3][strlen(token[3])-1] = '\0';
  82. # if ENABLE_UNICODE_SUPPORT
  83. {
  84. uni_stat_t uni_stat;
  85. char *uni_name = unicode_conv_to_printable(&uni_stat, token[0]);
  86. unsigned pad_len = (uni_stat.unicode_width > 19) ? 0 : 19 - uni_stat.unicode_width;
  87. printf("%s%*s %8s %2s %s\n", uni_name, pad_len, "", token[1], token[2], token[3]);
  88. free(uni_name);
  89. }
  90. # else
  91. printf("%-19s %8s %2s %s\n", token[0], token[1], token[2], token[3]);
  92. # endif
  93. }
  94. }
  95. if (ENABLE_FEATURE_CLEAN_UP)
  96. config_close(parser);
  97. #else
  98. check_tainted();
  99. xprint_and_close_file(xfopen_for_read("/proc/modules"));
  100. #endif
  101. return EXIT_SUCCESS;
  102. }