blob: 2beb123625ccb751f4302b7897ca2c46a6f23264 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Erik Andersen6f23cec1999-12-15 22:14:12 +00002/*
3 * Mini lsmod implementation for busybox
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Denis Vlasenkoba1315d2008-09-13 14:59:38 +00006 * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
Eric Andersen21adca72000-12-06 18:18:26 +00007 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Erik Andersen6f23cec1999-12-15 22:14:12 +00009 */
Denys Vlasenkoe32b64c2016-11-23 07:54:52 +010010//config:config LSMOD
Denys Vlasenkob097a842018-12-28 03:20:17 +010011//config: bool "lsmod (1.9 kb)"
Denys Vlasenkoa1cd0d92016-12-23 15:12:27 +010012//config: default y
Denys Vlasenkoe32b64c2016-11-23 07:54:52 +010013//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020014//config: lsmod is used to display a list of loaded modules.
Denys Vlasenkoe32b64c2016-11-23 07:54:52 +010015//config:
16//config:config FEATURE_LSMOD_PRETTY_2_6_OUTPUT
17//config: bool "Pretty output"
Denys Vlasenkoa1cd0d92016-12-23 15:12:27 +010018//config: default y
19//config: depends on LSMOD && !MODPROBE_SMALL
Denys Vlasenkoe32b64c2016-11-23 07:54:52 +010020//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020021//config: This option makes output format of lsmod adjusted to
22//config: the format of module-init-tools for Linux kernel 2.6.
23//config: Increases size somewhat.
Denys Vlasenkoc15613c2010-10-15 11:29:02 +020024
Denys Vlasenko3346b4a2017-08-04 02:56:39 +020025//applet:IF_LSMOD(IF_NOT_MODPROBE_SMALL(APPLET_NOEXEC(lsmod, lsmod, BB_DIR_SBIN, BB_SUID_DROP, lsmod)))
Denys Vlasenkoc15613c2010-10-15 11:29:02 +020026
Denys Vlasenkoa1cd0d92016-12-23 15:12:27 +010027//kbuild:ifneq ($(CONFIG_MODPROBE_SMALL),y)
Denys Vlasenkoe32b64c2016-11-23 07:54:52 +010028//kbuild:lib-$(CONFIG_LSMOD) += lsmod.o modutils.o
Denys Vlasenkoa1cd0d92016-12-23 15:12:27 +010029//kbuild:endif
Denys Vlasenkoe32b64c2016-11-23 07:54:52 +010030
Denys Vlasenko1a5e11c2010-10-16 01:56:41 +020031//usage:#if !ENABLE_MODPROBE_SMALL
32//usage:#define lsmod_trivial_usage
33//usage: ""
34//usage:#define lsmod_full_usage "\n\n"
Denys Vlasenkoca659f82017-01-30 19:21:12 +010035//usage: "List loaded kernel modules"
Denys Vlasenko1a5e11c2010-10-16 01:56:41 +020036//usage:#endif
37
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000038#include "libbb.h"
Tomas Heinrichd31a8792009-10-19 23:58:31 +020039#include "unicode.h"
Eric Andersenbe0dc0d2000-08-21 19:25:16 +000040
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000041#if ENABLE_FEATURE_CHECK_TAINTED_MODULE
42enum {
43 TAINT_PROPRIETORY_MODULE = (1 << 0),
44 TAINT_FORCED_MODULE = (1 << 1),
45 TAINT_UNSAFE_SMP = (1 << 2),
46};
Eric Andersen166fa462002-09-16 05:30:24 +000047
Eric Andersen71ae64b2002-10-10 04:20:21 +000048static void check_tainted(void)
Eric Andersen166fa462002-09-16 05:30:24 +000049{
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000050 int tainted = 0;
51 char *buf = xmalloc_open_read_close("/proc/sys/kernel/tainted", NULL);
52 if (buf) {
53 tainted = atoi(buf);
54 if (ENABLE_FEATURE_CLEAN_UP)
55 free(buf);
Eric Andersen166fa462002-09-16 05:30:24 +000056 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000057
Denis Vlasenkob68979a2007-11-02 23:31:10 +000058 if (tainted) {
Eric Andersen52864942002-10-08 09:38:07 +000059 printf(" Tainted: %c%c%c\n",
Eric Andersen166fa462002-09-16 05:30:24 +000060 tainted & TAINT_PROPRIETORY_MODULE ? 'P' : 'G',
61 tainted & TAINT_FORCED_MODULE ? 'F' : ' ',
62 tainted & TAINT_UNSAFE_SMP ? 'S' : ' ');
Denis Vlasenkob68979a2007-11-02 23:31:10 +000063 } else {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000064 puts(" Not tainted");
Eric Andersen166fa462002-09-16 05:30:24 +000065 }
66}
Rob Landley627814b2005-05-03 22:34:03 +000067#else
Denys Vlasenko8c317f02019-05-14 17:26:47 +020068static ALWAYS_INLINE void check_tainted(void)
69{
70 putchar('\n');
71}
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000072#endif
73
74int lsmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
75int lsmod_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
76{
77#if ENABLE_FEATURE_LSMOD_PRETTY_2_6_OUTPUT
78 char *token[4];
79 parser_t *parser = config_open("/proc/modules");
Denys Vlasenko28055022010-01-04 20:49:58 +010080 init_unicode();
Tomas Heinrichd31a8792009-10-19 23:58:31 +020081
Denis Vlasenko1a9e9bd2008-11-01 12:54:56 +000082 printf("%-24sSize Used by", "Module");
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000083 check_tainted();
84
85 if (ENABLE_FEATURE_2_4_MODULES
86 && get_linux_version_code() < KERNEL_VERSION(2,6,0)
87 ) {
88 while (config_read(parser, token, 4, 3, "# \t", PARSE_NORMAL)) {
89 if (token[3] != NULL && token[3][0] == '[') {
90 token[3]++;
91 token[3][strlen(token[3])-1] = '\0';
92 } else
93 token[3] = (char *) "";
Denys Vlasenko19158a82010-03-26 14:06:56 +010094# if ENABLE_UNICODE_SUPPORT
Denys Vlasenkoe17764c2010-01-30 23:16:21 +010095 {
96 uni_stat_t uni_stat;
97 char *uni_name = unicode_conv_to_printable(&uni_stat, token[0]);
98 unsigned pad_len = (uni_stat.unicode_width > 19) ? 0 : 19 - uni_stat.unicode_width;
99 printf("%s%*s %8s %2s %s\n", uni_name, pad_len, "", token[1], token[2], token[3]);
100 free(uni_name);
101 }
Denys Vlasenkof8a73bf2009-11-04 18:01:17 +0100102# else
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000103 printf("%-19s %8s %2s %s\n", token[0], token[1], token[2], token[3]);
Denys Vlasenkof8a73bf2009-11-04 18:01:17 +0100104# endif
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000105 }
106 } else {
107 while (config_read(parser, token, 4, 4, "# \t", PARSE_NORMAL & ~PARSE_GREEDY)) {
108 // N.B. token[3] is either '-' (module is not used by others)
109 // or comma-separated list ended by comma
110 // so trimming the trailing char is just what we need!
Denys Vlasenko5a163b22011-01-11 13:07:22 +0100111 if (token[3][0])
112 token[3][strlen(token[3]) - 1] = '\0';
Denys Vlasenko19158a82010-03-26 14:06:56 +0100113# if ENABLE_UNICODE_SUPPORT
Denys Vlasenkoe17764c2010-01-30 23:16:21 +0100114 {
115 uni_stat_t uni_stat;
116 char *uni_name = unicode_conv_to_printable(&uni_stat, token[0]);
117 unsigned pad_len = (uni_stat.unicode_width > 19) ? 0 : 19 - uni_stat.unicode_width;
118 printf("%s%*s %8s %2s %s\n", uni_name, pad_len, "", token[1], token[2], token[3]);
119 free(uni_name);
120 }
Denys Vlasenkof8a73bf2009-11-04 18:01:17 +0100121# else
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000122 printf("%-19s %8s %2s %s\n", token[0], token[1], token[2], token[3]);
Denys Vlasenkof8a73bf2009-11-04 18:01:17 +0100123# endif
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000124 }
125 }
126 if (ENABLE_FEATURE_CLEAN_UP)
127 config_close(parser);
128#else
129 check_tainted();
130 xprint_and_close_file(xfopen_for_read("/proc/modules"));
131#endif
Rob Landley86b4d642006-08-03 17:58:17 +0000132 return EXIT_SUCCESS;
Eric Andersen21adca72000-12-06 18:18:26 +0000133}