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 | * |
Bernhard Reutner-Fischer | deda6a5 | 2006-06-03 19:35:15 +0000 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Erik Andersen | 6f23cec | 1999-12-15 22:14:12 +0000 | [diff] [blame] | 9 | */ |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
Tomas Heinrich | d31a879 | 2009-10-19 23:58:31 +0200 | [diff] [blame] | 11 | #include "unicode.h" |
Eric Andersen | be0dc0d | 2000-08-21 19:25:16 +0000 | [diff] [blame] | 12 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 13 | #if ENABLE_FEATURE_CHECK_TAINTED_MODULE |
| 14 | enum { |
| 15 | TAINT_PROPRIETORY_MODULE = (1 << 0), |
| 16 | TAINT_FORCED_MODULE = (1 << 1), |
| 17 | TAINT_UNSAFE_SMP = (1 << 2), |
| 18 | }; |
Eric Andersen | 166fa46 | 2002-09-16 05:30:24 +0000 | [diff] [blame] | 19 | |
Eric Andersen | 71ae64b | 2002-10-10 04:20:21 +0000 | [diff] [blame] | 20 | static void check_tainted(void) |
Eric Andersen | 166fa46 | 2002-09-16 05:30:24 +0000 | [diff] [blame] | 21 | { |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 22 | int tainted = 0; |
| 23 | char *buf = xmalloc_open_read_close("/proc/sys/kernel/tainted", NULL); |
| 24 | if (buf) { |
| 25 | tainted = atoi(buf); |
| 26 | if (ENABLE_FEATURE_CLEAN_UP) |
| 27 | free(buf); |
Eric Andersen | 166fa46 | 2002-09-16 05:30:24 +0000 | [diff] [blame] | 28 | } |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 29 | |
Denis Vlasenko | b68979a | 2007-11-02 23:31:10 +0000 | [diff] [blame] | 30 | if (tainted) { |
Eric Andersen | 5286494 | 2002-10-08 09:38:07 +0000 | [diff] [blame] | 31 | printf(" Tainted: %c%c%c\n", |
Eric Andersen | 166fa46 | 2002-09-16 05:30:24 +0000 | [diff] [blame] | 32 | tainted & TAINT_PROPRIETORY_MODULE ? 'P' : 'G', |
| 33 | tainted & TAINT_FORCED_MODULE ? 'F' : ' ', |
| 34 | tainted & TAINT_UNSAFE_SMP ? 'S' : ' '); |
Denis Vlasenko | b68979a | 2007-11-02 23:31:10 +0000 | [diff] [blame] | 35 | } else { |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 36 | puts(" Not tainted"); |
Eric Andersen | 166fa46 | 2002-09-16 05:30:24 +0000 | [diff] [blame] | 37 | } |
| 38 | } |
Rob Landley | 627814b | 2005-05-03 22:34:03 +0000 | [diff] [blame] | 39 | #else |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 40 | static void check_tainted(void) { putchar('\n'); } |
| 41 | #endif |
| 42 | |
| 43 | int lsmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 44 | int lsmod_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
| 45 | { |
| 46 | #if ENABLE_FEATURE_LSMOD_PRETTY_2_6_OUTPUT |
| 47 | char *token[4]; |
| 48 | parser_t *parser = config_open("/proc/modules"); |
Denys Vlasenko | f8a73bf | 2009-11-04 18:01:17 +0100 | [diff] [blame] | 49 | # if ENABLE_FEATURE_ASSUME_UNICODE |
Tomas Heinrich | d31a879 | 2009-10-19 23:58:31 +0200 | [diff] [blame] | 50 | size_t name_len; |
Denys Vlasenko | f8a73bf | 2009-11-04 18:01:17 +0100 | [diff] [blame] | 51 | # endif |
Tomas Heinrich | d31a879 | 2009-10-19 23:58:31 +0200 | [diff] [blame] | 52 | check_unicode_in_env(); |
| 53 | |
Denis Vlasenko | 1a9e9bd | 2008-11-01 12:54:56 +0000 | [diff] [blame] | 54 | printf("%-24sSize Used by", "Module"); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 55 | check_tainted(); |
| 56 | |
| 57 | if (ENABLE_FEATURE_2_4_MODULES |
| 58 | && get_linux_version_code() < KERNEL_VERSION(2,6,0) |
| 59 | ) { |
| 60 | while (config_read(parser, token, 4, 3, "# \t", PARSE_NORMAL)) { |
| 61 | if (token[3] != NULL && token[3][0] == '[') { |
| 62 | token[3]++; |
| 63 | token[3][strlen(token[3])-1] = '\0'; |
| 64 | } else |
| 65 | token[3] = (char *) ""; |
Denys Vlasenko | f8a73bf | 2009-11-04 18:01:17 +0100 | [diff] [blame] | 66 | # if ENABLE_FEATURE_ASSUME_UNICODE |
Tomas Heinrich | d31a879 | 2009-10-19 23:58:31 +0200 | [diff] [blame] | 67 | name_len = bb_mbstrlen(token[0]); |
| 68 | name_len = (name_len > 19) ? 0 : 19 - name_len; |
| 69 | printf("%s%*s %8s %2s %s\n", token[0], name_len, "", token[1], token[2], token[3]); |
Denys Vlasenko | f8a73bf | 2009-11-04 18:01:17 +0100 | [diff] [blame] | 70 | # else |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 71 | 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] | 72 | # endif |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 73 | } |
| 74 | } else { |
| 75 | while (config_read(parser, token, 4, 4, "# \t", PARSE_NORMAL & ~PARSE_GREEDY)) { |
| 76 | // N.B. token[3] is either '-' (module is not used by others) |
| 77 | // or comma-separated list ended by comma |
| 78 | // so trimming the trailing char is just what we need! |
| 79 | token[3][strlen(token[3])-1] = '\0'; |
Denys Vlasenko | f8a73bf | 2009-11-04 18:01:17 +0100 | [diff] [blame] | 80 | # if ENABLE_FEATURE_ASSUME_UNICODE |
Tomas Heinrich | d31a879 | 2009-10-19 23:58:31 +0200 | [diff] [blame] | 81 | name_len = bb_mbstrlen(token[0]); |
| 82 | name_len = (name_len > 19) ? 0 : 19 - name_len; |
| 83 | printf("%s%*s %8s %2s %s\n", token[0], name_len, "", token[1], token[2], token[3]); |
Denys Vlasenko | f8a73bf | 2009-11-04 18:01:17 +0100 | [diff] [blame] | 84 | # else |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 85 | 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] | 86 | # endif |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | if (ENABLE_FEATURE_CLEAN_UP) |
| 90 | config_close(parser); |
| 91 | #else |
| 92 | check_tainted(); |
| 93 | xprint_and_close_file(xfopen_for_read("/proc/modules")); |
| 94 | #endif |
Rob Landley | 86b4d64 | 2006-08-03 17:58:17 +0000 | [diff] [blame] | 95 | return EXIT_SUCCESS; |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 96 | } |