3
0

lsmod.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. if (token[3][0])
  82. token[3][strlen(token[3]) - 1] = '\0';
  83. # if ENABLE_UNICODE_SUPPORT
  84. {
  85. uni_stat_t uni_stat;
  86. char *uni_name = unicode_conv_to_printable(&uni_stat, token[0]);
  87. unsigned pad_len = (uni_stat.unicode_width > 19) ? 0 : 19 - uni_stat.unicode_width;
  88. printf("%s%*s %8s %2s %s\n", uni_name, pad_len, "", token[1], token[2], token[3]);
  89. free(uni_name);
  90. }
  91. # else
  92. printf("%-19s %8s %2s %s\n", token[0], token[1], token[2], token[3]);
  93. # endif
  94. }
  95. }
  96. if (ENABLE_FEATURE_CLEAN_UP)
  97. config_close(parser);
  98. #else
  99. check_tainted();
  100. xprint_and_close_file(xfopen_for_read("/proc/modules"));
  101. #endif
  102. return EXIT_SUCCESS;
  103. }