Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Erik Andersen | 6f23cec | 1999-12-15 22:14:12 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini lsmod implementation for busybox |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 6 | * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com> |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 7 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Erik Andersen | 6f23cec | 1999-12-15 22:14:12 +0000 | [diff] [blame] | 9 | */ |
Denys Vlasenko | e32b64c | 2016-11-23 07:54:52 +0100 | [diff] [blame] | 10 | //config:config LSMOD |
| 11 | //config: bool "lsmod" |
Denys Vlasenko | a1cd0d9 | 2016-12-23 15:12:27 +0100 | [diff] [blame] | 12 | //config: default y |
Denys Vlasenko | e32b64c | 2016-11-23 07:54:52 +0100 | [diff] [blame] | 13 | //config: select PLATFORM_LINUX |
| 14 | //config: help |
| 15 | //config: lsmod is used to display a list of loaded modules. |
| 16 | //config: |
| 17 | //config:config FEATURE_LSMOD_PRETTY_2_6_OUTPUT |
| 18 | //config: bool "Pretty output" |
Denys Vlasenko | a1cd0d9 | 2016-12-23 15:12:27 +0100 | [diff] [blame] | 19 | //config: default y |
| 20 | //config: depends on LSMOD && !MODPROBE_SMALL |
Denys Vlasenko | e32b64c | 2016-11-23 07:54:52 +0100 | [diff] [blame] | 21 | //config: help |
| 22 | //config: This option makes output format of lsmod adjusted to |
| 23 | //config: the format of module-init-tools for Linux kernel 2.6. |
| 24 | //config: Increases size somewhat. |
Denys Vlasenko | c15613c | 2010-10-15 11:29:02 +0200 | [diff] [blame] | 25 | |
Denys Vlasenko | a1cd0d9 | 2016-12-23 15:12:27 +0100 | [diff] [blame] | 26 | //applet:IF_LSMOD(IF_NOT_MODPROBE_SMALL(APPLET(lsmod, BB_DIR_SBIN, BB_SUID_DROP))) |
Denys Vlasenko | c15613c | 2010-10-15 11:29:02 +0200 | [diff] [blame] | 27 | |
Denys Vlasenko | a1cd0d9 | 2016-12-23 15:12:27 +0100 | [diff] [blame] | 28 | //kbuild:ifneq ($(CONFIG_MODPROBE_SMALL),y) |
Denys Vlasenko | e32b64c | 2016-11-23 07:54:52 +0100 | [diff] [blame] | 29 | //kbuild:lib-$(CONFIG_LSMOD) += lsmod.o modutils.o |
Denys Vlasenko | a1cd0d9 | 2016-12-23 15:12:27 +0100 | [diff] [blame] | 30 | //kbuild:endif |
Denys Vlasenko | e32b64c | 2016-11-23 07:54:52 +0100 | [diff] [blame] | 31 | |
Denys Vlasenko | 1a5e11c | 2010-10-16 01:56:41 +0200 | [diff] [blame] | 32 | //usage:#if !ENABLE_MODPROBE_SMALL |
| 33 | //usage:#define lsmod_trivial_usage |
| 34 | //usage: "" |
| 35 | //usage:#define lsmod_full_usage "\n\n" |
Denys Vlasenko | ca659f8 | 2017-01-30 19:21:12 +0100 | [diff] [blame] | 36 | //usage: "List loaded kernel modules" |
Denys Vlasenko | 1a5e11c | 2010-10-16 01:56:41 +0200 | [diff] [blame] | 37 | //usage:#endif |
| 38 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 39 | #include "libbb.h" |
Tomas Heinrich | d31a879 | 2009-10-19 23:58:31 +0200 | [diff] [blame] | 40 | #include "unicode.h" |
Eric Andersen | be0dc0d | 2000-08-21 19:25:16 +0000 | [diff] [blame] | 41 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 42 | #if ENABLE_FEATURE_CHECK_TAINTED_MODULE |
| 43 | enum { |
| 44 | TAINT_PROPRIETORY_MODULE = (1 << 0), |
| 45 | TAINT_FORCED_MODULE = (1 << 1), |
| 46 | TAINT_UNSAFE_SMP = (1 << 2), |
| 47 | }; |
Eric Andersen | 166fa46 | 2002-09-16 05:30:24 +0000 | [diff] [blame] | 48 | |
Eric Andersen | 71ae64b | 2002-10-10 04:20:21 +0000 | [diff] [blame] | 49 | static void check_tainted(void) |
Eric Andersen | 166fa46 | 2002-09-16 05:30:24 +0000 | [diff] [blame] | 50 | { |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 51 | int tainted = 0; |
| 52 | char *buf = xmalloc_open_read_close("/proc/sys/kernel/tainted", NULL); |
| 53 | if (buf) { |
| 54 | tainted = atoi(buf); |
| 55 | if (ENABLE_FEATURE_CLEAN_UP) |
| 56 | free(buf); |
Eric Andersen | 166fa46 | 2002-09-16 05:30:24 +0000 | [diff] [blame] | 57 | } |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 58 | |
Denis Vlasenko | b68979a | 2007-11-02 23:31:10 +0000 | [diff] [blame] | 59 | if (tainted) { |
Eric Andersen | 5286494 | 2002-10-08 09:38:07 +0000 | [diff] [blame] | 60 | printf(" Tainted: %c%c%c\n", |
Eric Andersen | 166fa46 | 2002-09-16 05:30:24 +0000 | [diff] [blame] | 61 | tainted & TAINT_PROPRIETORY_MODULE ? 'P' : 'G', |
| 62 | tainted & TAINT_FORCED_MODULE ? 'F' : ' ', |
| 63 | tainted & TAINT_UNSAFE_SMP ? 'S' : ' '); |
Denis Vlasenko | b68979a | 2007-11-02 23:31:10 +0000 | [diff] [blame] | 64 | } else { |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 65 | puts(" Not tainted"); |
Eric Andersen | 166fa46 | 2002-09-16 05:30:24 +0000 | [diff] [blame] | 66 | } |
| 67 | } |
Rob Landley | 627814b | 2005-05-03 22:34:03 +0000 | [diff] [blame] | 68 | #else |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 69 | static void check_tainted(void) { putchar('\n'); } |
| 70 | #endif |
| 71 | |
| 72 | int lsmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 73 | int lsmod_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
| 74 | { |
| 75 | #if ENABLE_FEATURE_LSMOD_PRETTY_2_6_OUTPUT |
| 76 | char *token[4]; |
| 77 | parser_t *parser = config_open("/proc/modules"); |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 78 | init_unicode(); |
Tomas Heinrich | d31a879 | 2009-10-19 23:58:31 +0200 | [diff] [blame] | 79 | |
Denis Vlasenko | 1a9e9bd | 2008-11-01 12:54:56 +0000 | [diff] [blame] | 80 | printf("%-24sSize Used by", "Module"); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 81 | check_tainted(); |
| 82 | |
| 83 | if (ENABLE_FEATURE_2_4_MODULES |
| 84 | && get_linux_version_code() < KERNEL_VERSION(2,6,0) |
| 85 | ) { |
| 86 | while (config_read(parser, token, 4, 3, "# \t", PARSE_NORMAL)) { |
| 87 | if (token[3] != NULL && token[3][0] == '[') { |
| 88 | token[3]++; |
| 89 | token[3][strlen(token[3])-1] = '\0'; |
| 90 | } else |
| 91 | token[3] = (char *) ""; |
Denys Vlasenko | 19158a8 | 2010-03-26 14:06:56 +0100 | [diff] [blame] | 92 | # if ENABLE_UNICODE_SUPPORT |
Denys Vlasenko | e17764c | 2010-01-30 23:16:21 +0100 | [diff] [blame] | 93 | { |
| 94 | uni_stat_t uni_stat; |
| 95 | char *uni_name = unicode_conv_to_printable(&uni_stat, token[0]); |
| 96 | unsigned pad_len = (uni_stat.unicode_width > 19) ? 0 : 19 - uni_stat.unicode_width; |
| 97 | printf("%s%*s %8s %2s %s\n", uni_name, pad_len, "", token[1], token[2], token[3]); |
| 98 | free(uni_name); |
| 99 | } |
Denys Vlasenko | f8a73bf | 2009-11-04 18:01:17 +0100 | [diff] [blame] | 100 | # else |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 101 | printf("%-19s %8s %2s %s\n", token[0], token[1], token[2], token[3]); |
Denys Vlasenko | f8a73bf | 2009-11-04 18:01:17 +0100 | [diff] [blame] | 102 | # endif |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 103 | } |
| 104 | } else { |
| 105 | while (config_read(parser, token, 4, 4, "# \t", PARSE_NORMAL & ~PARSE_GREEDY)) { |
| 106 | // N.B. token[3] is either '-' (module is not used by others) |
| 107 | // or comma-separated list ended by comma |
| 108 | // so trimming the trailing char is just what we need! |
Denys Vlasenko | 5a163b2 | 2011-01-11 13:07:22 +0100 | [diff] [blame] | 109 | if (token[3][0]) |
| 110 | token[3][strlen(token[3]) - 1] = '\0'; |
Denys Vlasenko | 19158a8 | 2010-03-26 14:06:56 +0100 | [diff] [blame] | 111 | # if ENABLE_UNICODE_SUPPORT |
Denys Vlasenko | e17764c | 2010-01-30 23:16:21 +0100 | [diff] [blame] | 112 | { |
| 113 | uni_stat_t uni_stat; |
| 114 | char *uni_name = unicode_conv_to_printable(&uni_stat, token[0]); |
| 115 | unsigned pad_len = (uni_stat.unicode_width > 19) ? 0 : 19 - uni_stat.unicode_width; |
| 116 | printf("%s%*s %8s %2s %s\n", uni_name, pad_len, "", token[1], token[2], token[3]); |
| 117 | free(uni_name); |
| 118 | } |
Denys Vlasenko | f8a73bf | 2009-11-04 18:01:17 +0100 | [diff] [blame] | 119 | # else |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 120 | printf("%-19s %8s %2s %s\n", token[0], token[1], token[2], token[3]); |
Denys Vlasenko | f8a73bf | 2009-11-04 18:01:17 +0100 | [diff] [blame] | 121 | # endif |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | if (ENABLE_FEATURE_CLEAN_UP) |
| 125 | config_close(parser); |
| 126 | #else |
| 127 | check_tainted(); |
| 128 | xprint_and_close_file(xfopen_for_read("/proc/modules")); |
| 129 | #endif |
Rob Landley | 86b4d64 | 2006-08-03 17:58:17 +0000 | [diff] [blame] | 130 | return EXIT_SUCCESS; |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 131 | } |