1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
| #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <getopt.h> #include <sys/stat.h> #include <fcntl.h> #include <dirent.h>
// 配置结构体 struct module_config { int list_modules; // 列出模块 int show_details; // 显示详细信息 int verbose; // 详细输出 int show_refs; // 显示引用计数 int show_symbols; // 显示符号信息 char *module_name; // 指定模块名称 char *search_pattern; // 搜索模式 };
// 模块信息结构体 struct module_info { char name[256]; unsigned long size; int ref_count; char used_by[512]; char status[32]; unsigned long address; };
// 通过 /proc/modules 解析模块信息 int parse_proc_modules(struct module_info *modules, int max_modules) { FILE *fp = fopen("/proc/modules", "r"); if (!fp) { return -1; } int count = 0; char line[1024]; while (fgets(line, sizeof(line), fp) && count < max_modules) { struct module_info *mod = &modules[count]; char extra[512]; // 解析 /proc/modules 格式 int parsed = sscanf(line, "%255s %lu %d %511s %31s %31s", mod->name, &mod->size, &mod->ref_count, mod->used_by, mod->status, extra); if (parsed >= 4) { count++; } } fclose(fp); return count; }
// 显示模块列表 void show_module_list(struct module_info *modules, int count, const struct module_config *config) { printf("=== 内核模块列表 ===\n"); printf("%-25s %-12s %-8s %-20s %s\n", "模块名称", "大小", "引用数", "被谁使用", "状态"); printf("%-25s %-12s %-8s %-20s %s\n", "--------", "----", "----", "------", "----"); int displayed = 0; for (int i = 0; i < count && displayed < 50; i++) { struct module_info *mod = &modules[i]; // 如果指定了搜索模式,进行过滤 if (config->search_pattern) { if (strstr(mod->name, config->search_pattern) == NULL) { continue; } } // 如果指定了模块名称,精确匹配 if (config->module_name) { if (strcmp(mod->name, config->module_name) != 0) { continue; } } printf("%-25s %-12lu %-8d %-20s %s\n", mod->name, mod->size, mod->ref_count, mod->used_by, mod->status); displayed++; } if (displayed == 0) { printf("没有找到匹配的模块\n"); } else if (displayed < count) { printf("\n显示了 %d 个匹配模块 (总共 %d 个)\n", displayed, count); } }
// 显示模块详细信息 void show_module_details(struct module_info *modules, int count, const struct module_config *config) { printf("\n=== 模块详细信息 ===\n"); int found = 0; for (int i = 0; i < count; i++) { struct module_info *mod = &modules[i]; // 检查是否匹配 if (config->module_name) { if (strcmp(mod->name, config->module_name) != 0) { continue; } } else if (config->search_pattern) { if (strstr(mod->name, config->search_pattern) == NULL) { continue; } } printf("\n模块: %s\n", mod->name); printf(" 大小: %lu 字节 (%.2f KB)\n", mod->size, (double)mod->size / 1024); printf(" 引用计数: %d\n", mod->ref_count); printf(" 被谁使用: %s\n", mod->used_by); printf(" 状态: %s\n", mod->status); // 显示 sysfs 信息 char sysfs_path[256]; snprintf(sysfs_path, sizeof(sysfs_path), "/sys/module/%s", mod->name); if (access(sysfs_path, F_OK) == 0) { printf(" Sysfs 路径: %s\n", sysfs_path); // 检查参数 char param_path[256]; snprintf(param_path, sizeof(param_path), "%s/parameters", sysfs_path); if (access(param_path, F_OK) == 0) { printf(" ✓ 支持运行时参数配置\n"); } // 检查引用计数 char refcnt_path[256]; snprintf(refcnt_path, sizeof(refcnt_path), "%s/refcnt", sysfs_path); FILE *refcnt_fp = fopen(refcnt_path, "r"); if (refcnt_fp) { char refcnt[32]; if (fgets(refcnt, sizeof(refcnt), refcnt_fp)) { printf(" 当前引用计数: %s", refcnt); } fclose(refcnt_fp); } } found++; if (config->module_name) { break; // 只显示指定模块 } if (found >= 5) { printf("\n只显示前 5 个匹配模块的详细信息\n"); break; } } if (found == 0) { if (config->module_name) { printf("未找到模块: %s\n", config->module_name); } else if (config->search_pattern) { printf("未找到匹配模式 '%s' 的模块\n", config->search_pattern); } else { printf("没有模块信息\n"); } } }
// 显示系统模块统计 void show_module_statistics(struct module_info *modules, int count) { printf("\n=== 模块统计信息 ===\n"); printf("总模块数: %d\n", count); if (count > 0) { // 统计引用计数 int total_refs = 0; int max_refs = 0; int zero_refs = 0; unsigned long total_size = 0; for (int i = 0; i < count; i++) { total_refs += modules[i].ref_count; total_size += modules[i].size; if (modules[i].ref_count > max_refs) { max_refs = modules[i].ref_count; } if (modules[i].ref_count == 0) { zero_refs++; } } printf("总大小: %.2f MB\n", (double)total_size / (1024 * 1024)); printf("平均引用计数: %.2f\n", (double)total_refs / count); printf("最大引用计数: %d\n", max_refs); printf("零引用模块数: %d\n", zero_refs); // 显示最常见的模块 printf("\n最常用的模块:\n"); int shown = 0; for (int i = 0; i < count && shown < 5; i++) { if (modules[i].ref_count > 0) { printf(" %s (%d 引用)\n", modules[i].name, modules[i].ref_count); shown++; } } } }
// 显示帮助信息 void show_help(const char *program_name) { printf("用法: %s [选项]\n", program_name); printf("\n选项:\n"); printf(" -l, --list 列出所有模块\n"); printf(" -d, --details 显示模块详细信息\n"); printf(" -r, --refs 显示引用计数\n"); printf(" -n, --name=MODULE 指定模块名称\n"); printf(" -s, --search=PATTERN 搜索模块名称\n"); printf(" -v, --verbose 详细输出\n"); printf(" -S, --statistics 显示统计信息\n"); printf(" -h, --help 显示此帮助信息\n"); printf("\n示例:\n"); printf(" %s -l # 列出所有模块\n", program_name); printf(" %s -d -n usbcore # 显示 usbcore 模块详细信息\n", program_name); printf(" %s -l -s usb # 列出包含 usb 的模块\n", program_name); printf(" %s -S # 显示模块统计信息\n", program_name); printf(" %s -d -s network # 显示网络相关模块详情\n", program_name); }
int main(int argc, char *argv[]) { struct module_config config = { .list_modules = 0, .show_details = 0, .verbose = 0, .show_refs = 0, .show_symbols = 0, .module_name = NULL, .search_pattern = NULL }; printf("=== 内核模块查询工具 ===\n\n"); // 解析命令行参数 static struct option long_options[] = { {"list", no_argument, 0, 'l'}, {"details", no_argument, 0, 'd'}, {"refs", no_argument, 0, 'r'}, {"symbols", no_argument, 0, 'y'}, {"name", required_argument, 0, 'n'}, {"search", required_argument, 0, 's'}, {"verbose", no_argument, 0, 'v'}, {"statistics", no_argument, 0, 'S'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} }; int opt; while ((opt = getopt_long(argc, argv, "ldryn:s:vSh", long_options, NULL)) != -1) { switch (opt) { case 'l': config.list_modules = 1; break; case 'd': config.show_details = 1; break; case 'r': config.show_refs = 1; break; case 'y': config.show_symbols = 1; break; case 'n': config.module_name = optarg; break; case 's': config.search_pattern = optarg; break; case 'v': config.verbose = 1; break; case 'S': config.list_modules = 1; break; case 'h': show_help(argv[0]); return 0; default: fprintf(stderr, "使用 '%s --help' 查看帮助信息\n", argv[0]); return 1; } } // 如果没有指定操作,默认列出模块 if (!config.list_modules && !config.show_details && !config.show_refs && !config.show_symbols) { config.list_modules = 1; } // 显示系统信息 if (config.verbose) { printf("系统信息:\n"); printf(" 用户 ID: %d\n", getuid()); printf(" 进程 ID: %d\n", getpid()); printf(" 当前目录: %s\n", getcwd(NULL, 0)); printf("\n"); } // 读取模块信息 struct module_info modules[500]; int module_count = parse_proc_modules(modules, 500); if (module_count == -1) { printf("错误: 无法读取模块信息\n"); printf("可能的原因:\n"); printf("1. 系统不支持 /proc/modules\n"); printf("2. 权限不足\n"); printf("3. 内核不支持模块\n"); return 1; } if (config.verbose) { printf("读取到 %d 个模块\n\n", module_count); } // 执行相应操作 if (config.list_modules) { show_module_list(modules, module_count, &config); } if (config.show_details) { show_module_details(modules, module_count, &config); } if (optind == 1) { // 如果没有指定参数,显示统计信息 show_module_statistics(modules, module_count); } printf("\n=== 模块管理最佳实践 ===\n"); printf("模块安全建议:\n"); printf("1. 只加载可信的模块\n"); printf("2. 定期更新模块\n"); printf("3. 监控模块加载活动\n"); printf("4. 限制模块加载权限\n"); printf("5. 使用签名验证模块\n"); printf("\n"); printf("模块性能优化:\n"); printf("1. 卸载不用的模块\n"); printf("2. 优化模块参数\n"); printf("3. 监控模块内存使用\n"); printf("4. 使用模块黑名单\n"); printf("5. 合理配置模块依赖\n"); printf("\n"); printf("现代替代方案:\n"); printf("1. /proc/modules: 模块列表信息\n"); printf("2. /sys/module/: 模块 sysfs 接口\n"); printf("3. modinfo: 模块详细信息\n"); printf("4. lsmod: 列出模块\n"); printf("5. modprobe: 智能模块加载\n"); printf("6. rmmod: 卸载模块\n"); return 0; }
|