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:       help
  14//config:       lsmod is used to display a list of loaded modules.
  15//config:
  16//config:config FEATURE_LSMOD_PRETTY_2_6_OUTPUT
  17//config:       bool "Pretty output"
  18//config:       default y
  19//config:       depends on LSMOD && !MODPROBE_SMALL
  20//config:       help
  21//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.
  24
  25//applet:IF_LSMOD(IF_NOT_MODPROBE_SMALL(APPLET_NOEXEC(lsmod, lsmod, BB_DIR_SBIN, BB_SUID_DROP, lsmod)))
  26
  27//kbuild:ifneq ($(CONFIG_MODPROBE_SMALL),y)
  28//kbuild:lib-$(CONFIG_LSMOD) += lsmod.o modutils.o
  29//kbuild:endif
  30
  31//usage:#if !ENABLE_MODPROBE_SMALL
  32//usage:#define lsmod_trivial_usage
  33//usage:       ""
  34//usage:#define lsmod_full_usage "\n\n"
  35//usage:       "List loaded kernel modules"
  36//usage:#endif
  37
  38#include "libbb.h"
  39#include "unicode.h"
  40
  41#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};
  47
  48static void check_tainted(void)
  49{
  50        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);
  56        }
  57
  58        if (tainted) {
  59                printf("    Tainted: %c%c%c\n",
  60                                tainted & TAINT_PROPRIETORY_MODULE      ? 'P' : 'G',
  61                                tainted & TAINT_FORCED_MODULE           ? 'F' : ' ',
  62                                tainted & TAINT_UNSAFE_SMP              ? 'S' : ' ');
  63        } else {
  64                puts("    Not tainted");
  65        }
  66}
  67#else
  68static ALWAYS_INLINE void check_tainted(void)
  69{
  70        putchar('\n');
  71}
  72#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");
  80        init_unicode();
  81
  82        printf("%-24sSize  Used by", "Module");
  83        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 *) "";
  94# if ENABLE_UNICODE_SUPPORT
  95                        {
  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                        }
 102# else
 103                        printf("%-19s %8s %2s %s\n", token[0], token[1], token[2], token[3]);
 104# endif
 105                }
 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!
 111                        if (token[3][0])
 112                                token[3][strlen(token[3]) - 1] = '\0';
 113# if ENABLE_UNICODE_SUPPORT
 114                        {
 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                        }
 121# else
 122                        printf("%-19s %8s %2s %s\n", token[0], token[1], token[2], token[3]);
 123# endif
 124                }
 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
 132        return EXIT_SUCCESS;
 133}
 134