Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/modutils/lsmod.c
- Timestamp:
- Feb 25, 2011, 9:26:54 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.2.9/mindi-busybox/modutils/lsmod.c
r1765 r2725 4 4 * 5 5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> 6 * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com> 6 7 * 7 * Modified by Alcove, Julien Gaulmin <julien.gaulmin@alcove.fr> and 8 * Nicolas Ferre <nicolas.ferre@alcove.fr> to support pre 2.1 kernels 9 * (which lack the query_module() interface). 10 * 11 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. 8 * Licensed under GPLv2 or later, see file LICENSE in this source tree. 12 9 */ 13 10 11 //applet:IF_LSMOD(APPLET(lsmod, _BB_DIR_SBIN, _BB_SUID_DROP)) 12 13 //usage:#if !ENABLE_MODPROBE_SMALL 14 //usage:#define lsmod_trivial_usage 15 //usage: "" 16 //usage:#define lsmod_full_usage "\n\n" 17 //usage: "List the currently loaded kernel modules" 18 //usage:#endif 19 14 20 #include "libbb.h" 21 #include "unicode.h" 15 22 16 17 #if !ENABLE_FEATURE_CHECK_TAINTED_MODULE 18 static void check_tainted(void) { puts(""); } 19 #else 20 #define TAINT_FILENAME "/proc/sys/kernel/tainted" 21 #define TAINT_PROPRIETORY_MODULE (1<<0) 22 #define TAINT_FORCED_MODULE (1<<1) 23 #define TAINT_UNSAFE_SMP (1<<2) 23 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE 24 enum { 25 TAINT_PROPRIETORY_MODULE = (1 << 0), 26 TAINT_FORCED_MODULE = (1 << 1), 27 TAINT_UNSAFE_SMP = (1 << 2), 28 }; 24 29 25 30 static void check_tainted(void) 26 31 { 27 int tainted; 28 FILE *f; 32 int tainted = 0; 33 char *buf = xmalloc_open_read_close("/proc/sys/kernel/tainted", NULL); 34 if (buf) { 35 tainted = atoi(buf); 36 if (ENABLE_FEATURE_CLEAN_UP) 37 free(buf); 38 } 29 39 30 tainted = 0; 31 if ((f = fopen(TAINT_FILENAME, "r"))) { 32 fscanf(f, "%d", &tainted); 33 fclose(f); 34 } 35 if (f && tainted) { 40 if (tainted) { 36 41 printf(" Tainted: %c%c%c\n", 37 42 tainted & TAINT_PROPRIETORY_MODULE ? 'P' : 'G', 38 43 tainted & TAINT_FORCED_MODULE ? 'F' : ' ', 39 44 tainted & TAINT_UNSAFE_SMP ? 'S' : ' '); 40 } 41 else { 42 printf(" Not tainted\n"); 45 } else { 46 puts(" Not tainted"); 43 47 } 44 48 } 49 #else 50 static void check_tainted(void) { putchar('\n'); } 45 51 #endif 46 52 47 #if ENABLE_FEATURE_QUERY_MODULE_INTERFACE 53 int lsmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 54 int lsmod_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) 55 { 56 #if ENABLE_FEATURE_LSMOD_PRETTY_2_6_OUTPUT 57 char *token[4]; 58 parser_t *parser = config_open("/proc/modules"); 59 init_unicode(); 48 60 49 struct module_info 50 { 51 unsigned long addr; 52 unsigned long size; 53 unsigned long flags; 54 long usecount; 55 }; 56 57 58 int query_module(const char *name, int which, void *buf, size_t bufsize, size_t *ret); 59 60 enum { 61 /* Values for query_module's which. */ 62 QM_MODULES = 1, 63 QM_DEPS = 2, 64 QM_REFS = 3, 65 QM_SYMBOLS = 4, 66 QM_INFO = 5, 67 68 /* Bits of module.flags. */ 69 NEW_MOD_RUNNING = 1, 70 NEW_MOD_DELETED = 2, 71 NEW_MOD_AUTOCLEAN = 4, 72 NEW_MOD_VISITED = 8, 73 NEW_MOD_USED_ONCE = 16, 74 NEW_MOD_INITIALIZING = 64 75 }; 76 77 int lsmod_main(int argc, char **argv); 78 int lsmod_main(int argc, char **argv) 79 { 80 struct module_info info; 81 char *module_names, *mn, *deps, *dn; 82 size_t bufsize, depsize, nmod, count, i, j; 83 84 module_names = deps = NULL; 85 bufsize = depsize = 0; 86 while (query_module(NULL, QM_MODULES, module_names, bufsize, &nmod)) { 87 if (errno != ENOSPC) bb_perror_msg_and_die("QM_MODULES"); 88 module_names = xmalloc(bufsize = nmod); 89 } 90 91 deps = xmalloc(depsize = 256); 92 printf("Module\t\t\tSize Used by"); 61 printf("%-24sSize Used by", "Module"); 93 62 check_tainted(); 94 63 95 for (i = 0, mn = module_names; i < nmod; mn += strlen(mn) + 1, i++) { 96 if (query_module(mn, QM_INFO, &info, sizeof(info), &count)) { 97 if (errno == ENOENT) { 98 /* The module was removed out from underneath us. */ 99 continue; 64 if (ENABLE_FEATURE_2_4_MODULES 65 && get_linux_version_code() < KERNEL_VERSION(2,6,0) 66 ) { 67 while (config_read(parser, token, 4, 3, "# \t", PARSE_NORMAL)) { 68 if (token[3] != NULL && token[3][0] == '[') { 69 token[3]++; 70 token[3][strlen(token[3])-1] = '\0'; 71 } else 72 token[3] = (char *) ""; 73 # if ENABLE_UNICODE_SUPPORT 74 { 75 uni_stat_t uni_stat; 76 char *uni_name = unicode_conv_to_printable(&uni_stat, token[0]); 77 unsigned pad_len = (uni_stat.unicode_width > 19) ? 0 : 19 - uni_stat.unicode_width; 78 printf("%s%*s %8s %2s %s\n", uni_name, pad_len, "", token[1], token[2], token[3]); 79 free(uni_name); 100 80 } 101 /* else choke */ 102 bb_perror_msg_and_die("module %s: QM_INFO", mn); 81 # else 82 printf("%-19s %8s %2s %s\n", token[0], token[1], token[2], token[3]); 83 # endif 103 84 } 104 while (query_module(mn, QM_REFS, deps, depsize, &count)) { 105 if (errno == ENOENT) { 106 /* The module was removed out from underneath us. */ 107 continue; 108 } else if (errno != ENOSPC) 109 bb_perror_msg_and_die("module %s: QM_REFS", mn); 110 deps = xrealloc(deps, count); 85 } else { 86 while (config_read(parser, token, 4, 4, "# \t", PARSE_NORMAL & ~PARSE_GREEDY)) { 87 // N.B. token[3] is either '-' (module is not used by others) 88 // or comma-separated list ended by comma 89 // so trimming the trailing char is just what we need! 90 token[3][strlen(token[3])-1] = '\0'; 91 # if ENABLE_UNICODE_SUPPORT 92 { 93 uni_stat_t uni_stat; 94 char *uni_name = unicode_conv_to_printable(&uni_stat, token[0]); 95 unsigned pad_len = (uni_stat.unicode_width > 19) ? 0 : 19 - uni_stat.unicode_width; 96 printf("%s%*s %8s %2s %s\n", uni_name, pad_len, "", token[1], token[2], token[3]); 97 free(uni_name); 98 } 99 # else 100 printf("%-19s %8s %2s %s\n", token[0], token[1], token[2], token[3]); 101 # endif 111 102 } 112 printf("%-20s%8lu%4ld", mn, info.size, info.usecount);113 if (info.flags & NEW_MOD_DELETED)114 printf(" (deleted)");115 else if (info.flags & NEW_MOD_INITIALIZING)116 printf(" (initializing)");117 else if (!(info.flags & NEW_MOD_RUNNING))118 printf(" (uninitialized)");119 else {120 if (info.flags & NEW_MOD_AUTOCLEAN)121 printf(" (autoclean) ");122 if (!(info.flags & NEW_MOD_USED_ONCE))123 printf(" (unused)");124 }125 if (count) printf(" [");126 for (j = 0, dn = deps; j < count; dn += strlen(dn) + 1, j++) {127 printf("%s%s", dn, (j==count-1)? "":" ");128 }129 if (count) printf("]");130 131 puts("");132 103 } 133 134 #if ENABLE_FEATURE_CLEAN_UP 135 free(module_names); 104 if (ENABLE_FEATURE_CLEAN_UP) 105 config_close(parser); 106 #else 107 check_tainted(); 108 xprint_and_close_file(xfopen_for_read("/proc/modules")); 136 109 #endif 137 138 return 0;139 }140 141 #else /* CONFIG_FEATURE_QUERY_MODULE_INTERFACE */142 143 int lsmod_main(int argc, char **argv);144 int lsmod_main(int argc, char **argv)145 {146 FILE *file = xfopen("/proc/modules", "r");147 148 printf("Module Size Used by");149 check_tainted();150 #if defined(CONFIG_FEATURE_LSMOD_PRETTY_2_6_OUTPUT)151 {152 char *line;153 while ((line = xmalloc_fgets(file)) != NULL) {154 char *tok;155 156 tok = strtok(line, " \t");157 printf("%-19s", tok);158 tok = strtok(NULL, " \t\n");159 printf(" %8s", tok);160 tok = strtok(NULL, " \t\n");161 /* Null if no module unloading support. */162 if (tok) {163 printf(" %s", tok);164 tok = strtok(NULL, "\n");165 if (!tok)166 tok = (char*)"";167 /* New-style has commas, or -. If so,168 truncate (other fields might follow). */169 else if (strchr(tok, ',')) {170 tok = strtok(tok, "\t ");171 /* Strip trailing comma. */172 if (tok[strlen(tok)-1] == ',')173 tok[strlen(tok)-1] = '\0';174 } else if (tok[0] == '-'175 && (tok[1] == '\0' || isspace(tok[1]))176 ) {177 tok = (char*)"";178 }179 printf(" %s", tok);180 }181 puts("");182 free(line);183 }184 fclose(file);185 }186 #else187 xprint_and_close_file(file);188 #endif /* CONFIG_FEATURE_2_6_MODULES */189 110 return EXIT_SUCCESS; 190 111 } 191 192 #endif /* CONFIG_FEATURE_QUERY_MODULE_INTERFACE */
Note:
See TracChangeset
for help on using the changeset viewer.