1 /* rmmod.c: remove a module from the kernel.
2 Copyright (C) 2001 Rusty Russell.
3 Copyright (C) 2002 Rusty Russell, IBM Corporation.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <asm/unistd.h>
30 #include "backwards_compat.c"
32 extern long delete_module(const char *, unsigned int);
34 #define error(log, fmt, ...) message(log, "ERROR: ", fmt , ## __VA_ARGS__)
35 #define warn(log, fmt, ...) message(log, "WARNING: ", fmt , ## __VA_ARGS__)
36 #define info(log, fmt, ...) \
37 do { if (verbose) message(log, "", fmt , ## __VA_ARGS__); } while(0)
38 #define fatal(log, fmt, ...) \
39 do { message(log, "FATAL: ", fmt , ## __VA_ARGS__); exit(1); } while(0)
41 static void message(int log
, const char *prefix
, const char *fmt
, ...)
42 __attribute__ ((format (printf
, 3, 4)));
44 static int getlen(const char *fmt
, va_list ap
)
48 /* glibcs before 2.1 return -1 on "can't fit". */
49 ret
= vsnprintf(NULL
, 0, fmt
, ap
);
55 static void message(int log
, const char *prefix
, const char *fmt
, ...)
61 va_start(arglist
, fmt
);
62 len
= strlen(prefix
) + getlen(fmt
, arglist
) + 1;
65 /* Must restart args on some archs */
66 va_start(arglist
, fmt
);
68 vsnprintf(buf
+ strlen(buf
), len
, fmt
, arglist
);
71 // modutils-2.4 would buffer these up
72 syslog(LOG_INFO
, "%s", buf
);
74 fprintf(stderr
, "%s", buf
);
77 static char *next_field(const char *line
)
81 rest
= line
+ strcspn(line
, " ");
82 rest
+= strspn(rest
, " ");
86 /* Report that the module is in use. */
87 static void mod_in_use(int log
, const char *modname
, char *usedby
)
89 /* New /proc/modules uses a single "-" to mean "nothing". Old
90 one just puts nothing there. */
91 if (usedby
[0] == '-' || strcmp(usedby
, "") == 0) {
92 error(log
, "Module %s is in use\n", modname
);
96 /* New /proc/modules *always* prints commas (even if only one) */
97 if (strchr(usedby
, ','))
98 /* Blatt over trailing comma for neatness */
99 usedby
[strcspn(usedby
, " \n")-1] = '\0';
101 /* Older /proc/modules just put them as
102 space-separated names. Blatt over trailing \n */
103 usedby
[strlen(usedby
)-1] = '\0';
106 error(log
, "Module %s is in use by %s\n", modname
, usedby
);
109 /* If we can check usage without entering kernel, do so. */
110 static int check_usage(int log
, const char *modname
)
113 char line
[10240], name
[64];
114 unsigned long size
, refs
;
117 module_list
= fopen("/proc/modules", "r");
119 if (errno
== ENOENT
) /* /proc may not be mounted. */
121 fatal(log
, "can't open /proc/modules: %s\n", strerror(errno
));
123 while (fgets(line
, sizeof(line
)-1, module_list
) != NULL
) {
124 if (strchr(line
, '\n') == NULL
) {
125 fatal(log
, "V. v. long line broke rmmod.\n");
129 scanned
= sscanf(line
, "%s %lu %lu", name
, &size
, &refs
);
130 if (strcmp(name
, modname
) != 0)
134 fatal(log
, "Unknown format in /proc/modules: %s\n",
138 fatal(log
, "Kernel does not have unload support.\n");
140 /* Hand it fields 3 onwards. */
142 mod_in_use(log
, modname
,
143 next_field(next_field(next_field(line
))));
146 error(log
, "Module %s does not exist in /proc/modules\n", modname
);
150 return (refs
== 0) ? 0 : -EINVAL
;
153 static void filename2modname(char *modname
, const char *filename
)
155 const char *afterslash
;
158 afterslash
= strrchr(filename
, '/');
160 afterslash
= filename
;
164 /* Convert to underscores, stop at first . */
165 for (i
= 0; afterslash
[i
] && afterslash
[i
] != '.'; i
++) {
166 if (afterslash
[i
] == '-')
169 modname
[i
] = afterslash
[i
];
174 static int rmmod(int log
, int verbose
, const char *path
, int flags
)
177 char name
[strlen(path
) + 1];
179 filename2modname(name
, path
);
181 if ((flags
& O_NONBLOCK
) && !(flags
& O_TRUNC
)) {
182 if (check_usage(log
, name
) != 0)
186 info(log
, "rmmod %s, wait=%s%s\n", name
,
187 (flags
& O_NONBLOCK
) ? "no" : "yes",
188 (flags
& O_TRUNC
) ? " force" : "");
190 ret
= delete_module(name
, flags
);
192 error(log
, "Removing '%s': %s\n", name
, strerror(errno
));
196 static struct option options
[] = { { "all", 0, NULL
, 'a' },
197 { "force", 0, NULL
, 'f' },
198 { "help", 0, NULL
, 'h' },
199 { "syslog", 0, NULL
, 's' },
200 { "verbose", 0, NULL
, 'v' },
201 { "version", 0, NULL
, 'V' },
202 { "wait", 0, NULL
, 'w' },
203 { NULL
, 0, NULL
, 0 } };
205 static void print_usage(const char *progname
)
208 "Usage: %s [-fhswvV] modulename ...\n"
209 // " -a (or --all) to remove all modules (no names needed)\n"
210 " -f (or --force) forces a module unload, and may crash your\n"
211 " machine. This requires the Forced Module Removal option\n"
212 " when the kernel was compiled.\n"
213 " -h (or --help) prints this help text\n"
214 " -s (or --syslog) says use syslog, not stderr\n"
215 " -v (or --verbose) enables more messages\n"
216 " -V (or --version) prints the version code\n"
217 " -w (or --wait) begins a module removal even if it is used\n"
218 " and will stop new users from accessing the module (so it\n"
219 " should eventually fall to zero).\n"
224 int main(int argc
, char *argv
[])
226 /* O_EXCL so kernels can spot old rmmod versions */
227 unsigned int flags
= O_NONBLOCK
|O_EXCL
;
228 int i
, opt
, all
= 0, log
= 0, verbose
= 0;
231 try_old_version("rmmod", argv
);
233 while ((opt
= getopt_long(argc
, argv
,
234 "afh?swvV", options
, NULL
)) != EOF
) {
242 case 's': // --syslog
243 openlog("rmmod", LOG_CONS
, LOG_DAEMON
);
247 flags
&= ~O_NONBLOCK
;
249 case 'v': // --verbose
252 case 'V': // --version
253 puts(PACKAGE
" version " VERSION
);
257 print_usage(argv
[0]);
262 /* FIXME implement */
265 fprintf(stderr
, "no module names given\n");
266 print_usage(argv
[0]);
270 /* remove each specified module */
271 for (i
= optind
; i
< argc
; i
++) {
272 ret
= rmmod(log
, verbose
, argv
[i
], flags
);