summaryrefslogtreecommitdiff
path: root/modutils/lsmod.c (plain)
blob: 24589420adf5f878b432b750ca6256bf38ee94b2
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//config:config LSMOD
11//config: bool "lsmod"
12//config: default y
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"
19//config: default y
20//config: depends on LSMOD && !MODPROBE_SMALL
21//config: select PLATFORM_LINUX
22//config: help
23//config: This option makes output format of lsmod adjusted to
24//config: the format of module-init-tools for Linux kernel 2.6.
25//config: Increases size somewhat.
26
27//applet:IF_LSMOD(IF_NOT_MODPROBE_SMALL(APPLET(lsmod, BB_DIR_SBIN, BB_SUID_DROP)))
28
29//kbuild:ifneq ($(CONFIG_MODPROBE_SMALL),y)
30//kbuild:lib-$(CONFIG_LSMOD) += lsmod.o modutils.o
31//kbuild:endif
32
33//usage:#if !ENABLE_MODPROBE_SMALL
34//usage:#define lsmod_trivial_usage
35//usage: ""
36//usage:#define lsmod_full_usage "\n\n"
37//usage: "List the currently loaded kernel modules"
38//usage:#endif
39
40#include "libbb.h"
41#include "unicode.h"
42
43#if ENABLE_FEATURE_CHECK_TAINTED_MODULE
44enum {
45 TAINT_PROPRIETORY_MODULE = (1 << 0),
46 TAINT_FORCED_MODULE = (1 << 1),
47 TAINT_UNSAFE_SMP = (1 << 2),
48};
49
50static void check_tainted(void)
51{
52 int tainted = 0;
53 char *buf = xmalloc_open_read_close("/proc/sys/kernel/tainted", NULL);
54 if (buf) {
55 tainted = atoi(buf);
56 if (ENABLE_FEATURE_CLEAN_UP)
57 free(buf);
58 }
59
60 if (tainted) {
61 printf(" Tainted: %c%c%c\n",
62 tainted & TAINT_PROPRIETORY_MODULE ? 'P' : 'G',
63 tainted & TAINT_FORCED_MODULE ? 'F' : ' ',
64 tainted & TAINT_UNSAFE_SMP ? 'S' : ' ');
65 } else {
66 puts(" Not tainted");
67 }
68}
69#else
70static void check_tainted(void) { putchar('\n'); }
71#endif
72
73int lsmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
74int lsmod_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
75{
76#if ENABLE_FEATURE_LSMOD_PRETTY_2_6_OUTPUT
77 char *token[4];
78 parser_t *parser = config_open("/proc/modules");
79 init_unicode();
80
81 printf("%-24sSize Used by", "Module");
82 check_tainted();
83
84 if (ENABLE_FEATURE_2_4_MODULES
85 && get_linux_version_code() < KERNEL_VERSION(2,6,0)
86 ) {
87 while (config_read(parser, token, 4, 3, "# \t", PARSE_NORMAL)) {
88 if (token[3] != NULL && token[3][0] == '[') {
89 token[3]++;
90 token[3][strlen(token[3])-1] = '\0';
91 } else
92 token[3] = (char *) "";
93# if ENABLE_UNICODE_SUPPORT
94 {
95 uni_stat_t uni_stat;
96 char *uni_name = unicode_conv_to_printable(&uni_stat, token[0]);
97 unsigned pad_len = (uni_stat.unicode_width > 19) ? 0 : 19 - uni_stat.unicode_width;
98 printf("%s%*s %8s %2s %s\n", uni_name, pad_len, "", token[1], token[2], token[3]);
99 free(uni_name);
100 }
101# else
102 printf("%-19s %8s %2s %s\n", token[0], token[1], token[2], token[3]);
103# endif
104 }
105 } else {
106 while (config_read(parser, token, 4, 4, "# \t", PARSE_NORMAL & ~PARSE_GREEDY)) {
107 // N.B. token[3] is either '-' (module is not used by others)
108 // or comma-separated list ended by comma
109 // so trimming the trailing char is just what we need!
110 if (token[3][0])
111 token[3][strlen(token[3]) - 1] = '\0';
112# if ENABLE_UNICODE_SUPPORT
113 {
114 uni_stat_t uni_stat;
115 char *uni_name = unicode_conv_to_printable(&uni_stat, token[0]);
116 unsigned pad_len = (uni_stat.unicode_width > 19) ? 0 : 19 - uni_stat.unicode_width;
117 printf("%s%*s %8s %2s %s\n", uni_name, pad_len, "", token[1], token[2], token[3]);
118 free(uni_name);
119 }
120# else
121 printf("%-19s %8s %2s %s\n", token[0], token[1], token[2], token[3]);
122# endif
123 }
124 }
125 if (ENABLE_FEATURE_CLEAN_UP)
126 config_close(parser);
127#else
128 check_tainted();
129 xprint_and_close_file(xfopen_for_read("/proc/modules"));
130#endif
131 return EXIT_SUCCESS;
132}
133