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