lsmod.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 tarball for details.
  9. */
  10. #include "libbb.h"
  11. #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
  12. enum {
  13. TAINT_PROPRIETORY_MODULE = (1 << 0),
  14. TAINT_FORCED_MODULE = (1 << 1),
  15. TAINT_UNSAFE_SMP = (1 << 2),
  16. };
  17. static void check_tainted(void)
  18. {
  19. int tainted = 0;
  20. char *buf = xmalloc_open_read_close("/proc/sys/kernel/tainted", NULL);
  21. if (buf) {
  22. tainted = atoi(buf);
  23. if (ENABLE_FEATURE_CLEAN_UP)
  24. free(buf);
  25. }
  26. if (tainted) {
  27. printf(" Tainted: %c%c%c\n",
  28. tainted & TAINT_PROPRIETORY_MODULE ? 'P' : 'G',
  29. tainted & TAINT_FORCED_MODULE ? 'F' : ' ',
  30. tainted & TAINT_UNSAFE_SMP ? 'S' : ' ');
  31. } else {
  32. puts(" Not tainted");
  33. }
  34. }
  35. #else
  36. static void check_tainted(void) { putchar('\n'); }
  37. #endif
  38. int lsmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  39. int lsmod_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
  40. {
  41. #if ENABLE_FEATURE_LSMOD_PRETTY_2_6_OUTPUT
  42. char *token[4];
  43. parser_t *parser = config_open("/proc/modules");
  44. printf("%-24sSize Used by", "Module");
  45. check_tainted();
  46. if (ENABLE_FEATURE_2_4_MODULES
  47. && get_linux_version_code() < KERNEL_VERSION(2,6,0)
  48. ) {
  49. while (config_read(parser, token, 4, 3, "# \t", PARSE_NORMAL)) {
  50. if (token[3] != NULL && token[3][0] == '[') {
  51. token[3]++;
  52. token[3][strlen(token[3])-1] = '\0';
  53. } else
  54. token[3] = (char *) "";
  55. printf("%-19s %8s %2s %s\n", token[0], token[1], token[2], token[3]);
  56. }
  57. } else {
  58. while (config_read(parser, token, 4, 4, "# \t", PARSE_NORMAL & ~PARSE_GREEDY)) {
  59. // N.B. token[3] is either '-' (module is not used by others)
  60. // or comma-separated list ended by comma
  61. // so trimming the trailing char is just what we need!
  62. token[3][strlen(token[3])-1] = '\0';
  63. printf("%-19s %8s %2s %s\n", token[0], token[1], token[2], token[3]);
  64. }
  65. }
  66. if (ENABLE_FEATURE_CLEAN_UP)
  67. config_close(parser);
  68. #else
  69. check_tainted();
  70. xprint_and_close_file(xfopen_for_read("/proc/modules"));
  71. #endif
  72. return EXIT_SUCCESS;
  73. }