use /sys/module/ rather than parsing legacy /proc/modules
[mit.git] / rmmod.c
blob80d55e5c363a4004290255c62de6b6781f12cb75
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
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <asm/unistd.h>
26 #include <stdarg.h>
27 #include <getopt.h>
28 #include <syslog.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)
46 int ret;
48 /* glibcs before 2.1 return -1 on "can't fit". */
49 ret = vsnprintf(NULL, 0, fmt, ap);
50 if (ret >= 0)
51 return ret;
52 return 4096;
55 static void message(int log, const char *prefix, const char *fmt, ...)
57 char *buf;
58 va_list arglist;
59 int len;
61 va_start(arglist, fmt);
62 len = strlen(prefix) + getlen(fmt, arglist) + 1;
63 buf = malloc(len);
65 /* Must restart args on some archs */
66 va_start(arglist, fmt);
67 strcpy(buf, prefix);
68 vsnprintf(buf + strlen(buf), len, fmt, arglist);
70 if (log)
71 // modutils-2.4 would buffer these up
72 syslog(LOG_INFO, "%s", buf);
73 else
74 fprintf(stderr, "%s", buf);
76 free(buf);
79 static char *next_field(const char *line)
81 const char *rest;
83 rest = line + strcspn(line, " ");
84 rest += strspn(rest, " ");
85 return (char *)rest;
88 /* Report that the module is in use. */
89 static void mod_in_use(int log, const char *modname, char *usedby)
91 /* New /proc/modules uses a single "-" to mean "nothing". Old
92 one just puts nothing there. */
93 if (usedby[0] == '-' || strcmp(usedby, "") == 0) {
94 error(log, "Module %s is in use\n", modname);
95 return;
98 /* New /proc/modules *always* prints commas (even if only one) */
99 if (strchr(usedby, ','))
100 /* Blatt over trailing comma for neatness */
101 usedby[strcspn(usedby, " \n")-1] = '\0';
102 else {
103 /* Older /proc/modules just put them as
104 space-separated names. Blatt over trailing \n */
105 usedby[strlen(usedby)-1] = '\0';
108 error(log, "Module %s is in use by %s\n", modname, usedby);
111 /* If we can check usage without entering kernel, do so. */
112 static int check_usage(int log, const char *modname)
114 FILE *module_list;
115 char line[10240], name[64];
116 unsigned long size, refs;
117 int scanned;
119 module_list = fopen("/proc/modules", "r");
120 if (!module_list) {
121 if (errno == ENOENT) /* /proc may not be mounted. */
122 return 0;
123 fatal(log, "can't open /proc/modules: %s\n", strerror(errno));
125 while (fgets(line, sizeof(line)-1, module_list) != NULL) {
126 if (strchr(line, '\n') == NULL) {
127 fatal(log, "V. v. long line broke rmmod.\n");
128 exit(1);
131 scanned = sscanf(line, "%s %lu %lu", name, &size, &refs);
132 if (strcmp(name, modname) != 0)
133 continue;
135 if (scanned < 2)
136 fatal(log, "Unknown format in /proc/modules: %s\n",
137 line);
139 if (scanned == 2)
140 fatal(log, "Kernel does not have unload support.\n");
142 /* Hand it fields 3 onwards. */
143 if (refs != 0)
144 mod_in_use(log, modname,
145 next_field(next_field(next_field(line))));
146 goto out;
148 error(log, "Module %s does not exist in /proc/modules\n", modname);
149 refs = 1;
150 out:
151 fclose(module_list);
152 return (refs == 0) ? 0 : -EINVAL;
155 static void filename2modname(char *modname, const char *filename)
157 const char *afterslash;
158 unsigned int i;
160 afterslash = strrchr(filename, '/');
161 if (!afterslash)
162 afterslash = filename;
163 else
164 afterslash++;
166 /* Convert to underscores, stop at first . */
167 for (i = 0; afterslash[i] && afterslash[i] != '.'; i++) {
168 if (afterslash[i] == '-')
169 modname[i] = '_';
170 else
171 modname[i] = afterslash[i];
173 modname[i] = '\0';
176 static int rmmod(int log, int verbose, const char *path, int flags)
178 long ret;
179 char name[strlen(path) + 1];
181 filename2modname(name, path);
183 if ((flags & O_NONBLOCK) && !(flags & O_TRUNC)) {
184 if (check_usage(log, name) != 0)
185 return 1;
188 info(log, "rmmod %s, wait=%s%s\n", name,
189 (flags & O_NONBLOCK) ? "no" : "yes",
190 (flags & O_TRUNC) ? " force" : "");
192 ret = delete_module(name, flags);
193 if (ret != 0)
194 error(log, "Removing '%s': %s\n", name, strerror(errno));
195 return ret;
198 static struct option options[] = { { "all", 0, NULL, 'a' },
199 { "force", 0, NULL, 'f' },
200 { "help", 0, NULL, 'h' },
201 { "syslog", 0, NULL, 's' },
202 { "verbose", 0, NULL, 'v' },
203 { "version", 0, NULL, 'V' },
204 { "wait", 0, NULL, 'w' },
205 { NULL, 0, NULL, 0 } };
207 static void print_usage(const char *progname)
209 fprintf(stderr,
210 "Usage: %s [-fhswvV] modulename ...\n"
211 // " -a (or --all) to remove all modules (no names needed)\n"
212 " -f (or --force) forces a module unload, and may crash your\n"
213 " machine. This requires the Forced Module Removal option\n"
214 " when the kernel was compiled.\n"
215 " -h (or --help) prints this help text\n"
216 " -s (or --syslog) says use syslog, not stderr\n"
217 " -v (or --verbose) enables more messages\n"
218 " -V (or --version) prints the version code\n"
219 " -w (or --wait) begins a module removal even if it is used\n"
220 " and will stop new users from accessing the module (so it\n"
221 " should eventually fall to zero).\n"
222 , progname);
223 exit(1);
226 int main(int argc, char *argv[])
228 /* O_EXCL so kernels can spot old rmmod versions */
229 unsigned int flags = O_NONBLOCK|O_EXCL;
230 int i, opt, all = 0, log = 0, verbose = 0;
231 int ret, err;
233 try_old_version("rmmod", argv);
235 while ((opt = getopt_long(argc, argv,
236 "afh?swvV", options, NULL)) != EOF) {
237 switch (opt) {
238 case 'a': // --all
239 all = 1;
240 break;
241 case 'f': // --force
242 flags |= O_TRUNC;
243 break;
244 case 's': // --syslog
245 openlog("rmmod", LOG_CONS, LOG_DAEMON);
246 log = 1;
247 break;
248 case 'w': // --wait
249 flags &= ~O_NONBLOCK;
250 break;
251 case 'v': // --verbose
252 verbose++;
253 break;
254 case 'V': // --version
255 puts(PACKAGE " version " VERSION);
256 return 0;
258 default:
259 print_usage(argv[0]);
262 if (!argv[optind]) {
263 if (all) {
264 /* FIXME implement */
265 exit(1);
267 fprintf(stderr, "no module names given\n");
268 print_usage(argv[0]);
271 err = 0;
272 /* remove each specified module */
273 for (i = optind; i < argc; i++) {
274 ret = rmmod(log, verbose, argv[i], flags);
275 if (ret != 0)
276 err = 1;
279 if (log)
280 closelog();
281 exit(err);