Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Erik Andersen | 6f23cec | 1999-12-15 22:14:12 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini lsmod implementation for busybox |
| 4 | * |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999,2000 by Lineo, inc. |
Erik Andersen | 6f23cec | 1999-12-15 22:14:12 +0000 | [diff] [blame] | 6 | * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include "internal.h" |
Eric Andersen | be0dc0d | 2000-08-21 19:25:16 +0000 | [diff] [blame] | 25 | #include <stdlib.h> |
Erik Andersen | 6f23cec | 1999-12-15 22:14:12 +0000 | [diff] [blame] | 26 | #include <stdio.h> |
Eric Andersen | be0dc0d | 2000-08-21 19:25:16 +0000 | [diff] [blame] | 27 | #include <stddef.h> |
| 28 | #include <errno.h> |
| 29 | #include <unistd.h> |
| 30 | #include <dirent.h> |
| 31 | #include <ctype.h> |
| 32 | #include <assert.h> |
| 33 | #include <getopt.h> |
| 34 | #include <sys/utsname.h> |
| 35 | |
| 36 | |
| 37 | |
| 38 | struct module_info |
| 39 | { |
| 40 | unsigned long addr; |
| 41 | unsigned long size; |
| 42 | unsigned long flags; |
| 43 | long usecount; |
| 44 | }; |
| 45 | |
| 46 | |
| 47 | int query_module(const char *name, int which, void *buf, size_t bufsize, |
| 48 | size_t *ret); |
| 49 | |
| 50 | /* Values for query_module's which. */ |
| 51 | #define QM_MODULES 1 |
| 52 | #define QM_DEPS 2 |
| 53 | #define QM_REFS 3 |
| 54 | #define QM_SYMBOLS 4 |
| 55 | #define QM_INFO 5 |
| 56 | |
Eric Andersen | de34e43 | 2000-09-10 16:16:00 +0000 | [diff] [blame] | 57 | /* Bits of module.flags. */ |
| 58 | #define NEW_MOD_RUNNING 1 |
| 59 | #define NEW_MOD_DELETED 2 |
| 60 | #define NEW_MOD_AUTOCLEAN 4 |
| 61 | #define NEW_MOD_VISITED 8 |
| 62 | #define NEW_MOD_USED_ONCE 16 |
| 63 | #define NEW_MOD_INITIALIZING 64 |
Erik Andersen | 6f23cec | 1999-12-15 22:14:12 +0000 | [diff] [blame] | 64 | |
| 65 | |
Erik Andersen | 6f23cec | 1999-12-15 22:14:12 +0000 | [diff] [blame] | 66 | extern int lsmod_main(int argc, char **argv) |
| 67 | { |
Eric Andersen | be0dc0d | 2000-08-21 19:25:16 +0000 | [diff] [blame] | 68 | struct module_info info; |
| 69 | char *module_names, *mn, *deps, *dn; |
| 70 | size_t bufsize, nmod, count, i, j; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 71 | |
Eric Andersen | be0dc0d | 2000-08-21 19:25:16 +0000 | [diff] [blame] | 72 | module_names = xmalloc(bufsize = 256); |
| 73 | deps = xmalloc(bufsize); |
| 74 | if (query_module(NULL, QM_MODULES, module_names, bufsize, &nmod)) { |
| 75 | fatalError("QM_MODULES: %s\n", strerror(errno)); |
| 76 | } |
| 77 | |
| 78 | printf("Module Size Used by\n"); |
| 79 | for (i = 0, mn = module_names; i < nmod; mn += strlen(mn) + 1, i++) { |
| 80 | if (query_module(mn, QM_INFO, &info, sizeof(info), &count)) { |
| 81 | if (errno == ENOENT) { |
| 82 | /* The module was removed out from underneath us. */ |
| 83 | continue; |
| 84 | } |
| 85 | /* else choke */ |
| 86 | fatalError("module %s: QM_INFO: %s\n", mn, strerror(errno)); |
| 87 | } |
| 88 | while (query_module(mn, QM_REFS, deps, bufsize, &count)) { |
| 89 | if (errno == ENOENT) { |
| 90 | /* The module was removed out from underneath us. */ |
| 91 | continue; |
| 92 | } |
| 93 | if (errno != ENOSPC) { |
| 94 | fatalError("module %s: QM_REFS: %s", mn, strerror(errno)); |
| 95 | } |
| 96 | deps = xrealloc(deps, bufsize = count); |
| 97 | } |
| 98 | printf("%-20s%8lu%4ld ", mn, info.size, info.usecount); |
| 99 | if (count) printf("["); |
| 100 | for (j = 0, dn = deps; j < count; dn += strlen(dn) + 1, j++) { |
| 101 | printf("%s%s", dn, (j==count-1)? "":" "); |
| 102 | } |
Eric Andersen | ccb0a9b | 2000-09-12 16:20:49 +0000 | [diff] [blame] | 103 | if (count) printf("] "); |
Eric Andersen | de34e43 | 2000-09-10 16:16:00 +0000 | [diff] [blame] | 104 | |
| 105 | if (info.flags & NEW_MOD_DELETED) |
Eric Andersen | ccb0a9b | 2000-09-12 16:20:49 +0000 | [diff] [blame] | 106 | printf("(deleted)"); |
Eric Andersen | de34e43 | 2000-09-10 16:16:00 +0000 | [diff] [blame] | 107 | else if (info.flags & NEW_MOD_INITIALIZING) |
Eric Andersen | ccb0a9b | 2000-09-12 16:20:49 +0000 | [diff] [blame] | 108 | printf("(initializing)"); |
Eric Andersen | de34e43 | 2000-09-10 16:16:00 +0000 | [diff] [blame] | 109 | else if (!(info.flags & NEW_MOD_RUNNING)) |
Eric Andersen | ccb0a9b | 2000-09-12 16:20:49 +0000 | [diff] [blame] | 110 | printf("(uninitialized)"); |
Eric Andersen | de34e43 | 2000-09-10 16:16:00 +0000 | [diff] [blame] | 111 | else { |
| 112 | if (info.flags & NEW_MOD_AUTOCLEAN) |
Eric Andersen | ccb0a9b | 2000-09-12 16:20:49 +0000 | [diff] [blame] | 113 | printf("(autoclean)"); |
Eric Andersen | de34e43 | 2000-09-10 16:16:00 +0000 | [diff] [blame] | 114 | if (!(info.flags & NEW_MOD_USED_ONCE)) |
Eric Andersen | ccb0a9b | 2000-09-12 16:20:49 +0000 | [diff] [blame] | 115 | printf("(unused)"); |
Eric Andersen | de34e43 | 2000-09-10 16:16:00 +0000 | [diff] [blame] | 116 | } |
Eric Andersen | ccb0a9b | 2000-09-12 16:20:49 +0000 | [diff] [blame] | 117 | printf("\n"); |
Eric Andersen | be0dc0d | 2000-08-21 19:25:16 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | |
| 121 | return( 0); |
Erik Andersen | 6f23cec | 1999-12-15 22:14:12 +0000 | [diff] [blame] | 122 | } |