modprobe: make return values consistent for quiet/non-quiet
[mit.git] / rmmod.c
blob6e1d9126d767146959e32f5761a43942520e94b8
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 "testing.h"
31 #include "backwards_compat.c"
33 extern long delete_module(const char *, unsigned int);
35 #define error(log, fmt, ...) message(log, "ERROR: ", fmt , ## __VA_ARGS__)
36 #define warn(log, fmt, ...) message(log, "WARNING: ", fmt , ## __VA_ARGS__)
37 #define info(log, fmt, ...) \
38 do { if (verbose) message(log, "", fmt , ## __VA_ARGS__); } while(0)
39 #define fatal(log, fmt, ...) \
40 do { message(log, "FATAL: ", fmt , ## __VA_ARGS__); exit(1); } while(0)
42 static void message(int log, const char *prefix, const char *fmt, ...)
43 __attribute__ ((format (printf, 3, 4)));
45 static int getlen(const char *fmt, va_list ap)
47 int ret;
49 /* glibcs before 2.1 return -1 on "can't fit". */
50 ret = vsnprintf(NULL, 0, fmt, ap);
51 if (ret >= 0)
52 return ret;
53 return 4096;
56 static void message(int log, const char *prefix, const char *fmt, ...)
58 char *buf;
59 va_list arglist;
60 int len;
62 va_start(arglist, fmt);
63 len = strlen(prefix) + getlen(fmt, arglist) + 1;
64 buf = malloc(len);
66 /* Must restart args on some archs */
67 va_start(arglist, fmt);
68 strcpy(buf, prefix);
69 vsnprintf(buf + strlen(buf), len, fmt, arglist);
71 if (log)
72 // modutils-2.4 would buffer these up
73 syslog(LOG_INFO, "%s", buf);
74 else
75 fprintf(stderr, "%s", buf);
77 free(buf);
80 static char *next_field(const char *line)
82 const char *rest;
84 rest = line + strcspn(line, " ");
85 rest += strspn(rest, " ");
86 return (char *)rest;
89 /* Report that the module is in use. */
90 static void mod_in_use(int log, const char *modname, char *usedby)
92 /* New /proc/modules uses a single "-" to mean "nothing". Old
93 one just puts nothing there. */
94 if (usedby[0] == '-' || strcmp(usedby, "") == 0) {
95 error(log, "Module %s is in use\n", modname);
96 return;
99 /* New /proc/modules *always* prints commas (even if only one) */
100 if (strchr(usedby, ','))
101 /* Blatt over trailing comma for neatness */
102 usedby[strcspn(usedby, " \n")-1] = '\0';
103 else {
104 /* Older /proc/modules just put them as
105 space-separated names. Blatt over trailing \n */
106 usedby[strlen(usedby)-1] = '\0';
109 error(log, "Module %s is in use by %s\n", modname, usedby);
112 /* If we can check usage without entering kernel, do so. */
113 static int check_usage(int log, const char *modname)
115 FILE *module_list;
116 char line[10240], name[64];
117 unsigned long size, refs;
118 int scanned;
120 module_list = fopen("/proc/modules", "r");
121 if (!module_list) {
122 if (errno == ENOENT) /* /proc may not be mounted. */
123 return 0;
124 fatal(log, "can't open /proc/modules: %s\n", strerror(errno));
126 while (fgets(line, sizeof(line)-1, module_list) != NULL) {
127 if (strchr(line, '\n') == NULL) {
128 fatal(log, "V. v. long line broke rmmod.\n");
129 exit(1);
132 scanned = sscanf(line, "%s %lu %lu", name, &size, &refs);
133 if (strcmp(name, modname) != 0)
134 continue;
136 if (scanned < 2)
137 fatal(log, "Unknown format in /proc/modules: %s\n",
138 line);
140 if (scanned == 2)
141 fatal(log, "Kernel does not have unload support.\n");
143 /* Hand it fields 3 onwards. */
144 if (refs != 0)
145 mod_in_use(log, modname,
146 next_field(next_field(next_field(line))));
147 goto out;
149 error(log, "Module %s does not exist in /proc/modules\n", modname);
150 refs = 1;
151 out:
152 fclose(module_list);
153 return (refs == 0) ? 0 : -EINVAL;
156 static void filename2modname(char *modname, const char *filename)
158 const char *afterslash;
159 unsigned int i;
161 afterslash = strrchr(filename, '/');
162 if (!afterslash)
163 afterslash = filename;
164 else
165 afterslash++;
167 /* Convert to underscores, stop at first . */
168 for (i = 0; afterslash[i] && afterslash[i] != '.'; i++) {
169 if (afterslash[i] == '-')
170 modname[i] = '_';
171 else
172 modname[i] = afterslash[i];
174 modname[i] = '\0';
177 static int rmmod(int log, int verbose, const char *path, int flags)
179 long ret;
180 char name[strlen(path) + 1];
182 filename2modname(name, path);
184 if ((flags & O_NONBLOCK) && !(flags & O_TRUNC)) {
185 if (check_usage(log, name) != 0)
186 return 1;
189 info(log, "rmmod %s, wait=%s%s\n", name,
190 (flags & O_NONBLOCK) ? "no" : "yes",
191 (flags & O_TRUNC) ? " force" : "");
193 ret = delete_module(name, flags);
194 if (ret != 0)
195 error(log, "Removing '%s': %s\n", name, strerror(errno));
196 return ret;
199 static struct option options[] = { { "all", 0, NULL, 'a' },
200 { "force", 0, NULL, 'f' },
201 { "help", 0, NULL, 'h' },
202 { "syslog", 0, NULL, 's' },
203 { "verbose", 0, NULL, 'v' },
204 { "version", 0, NULL, 'V' },
205 { "wait", 0, NULL, 'w' },
206 { NULL, 0, NULL, 0 } };
208 static void print_usage(const char *progname)
210 fprintf(stderr,
211 "Usage: %s [-fhswvV] modulename ...\n"
212 // " -a (or --all) to remove all modules (no names needed)\n"
213 " -f (or --force) forces a module unload, and may crash your\n"
214 " machine. This requires the Forced Module Removal option\n"
215 " when the kernel was compiled.\n"
216 " -h (or --help) prints this help text\n"
217 " -s (or --syslog) says use syslog, not stderr\n"
218 " -v (or --verbose) enables more messages\n"
219 " -V (or --version) prints the version code\n"
220 " -w (or --wait) begins a module removal even if it is used\n"
221 " and will stop new users from accessing the module (so it\n"
222 " should eventually fall to zero).\n"
223 , progname);
224 exit(1);
227 int main(int argc, char *argv[])
229 /* O_EXCL so kernels can spot old rmmod versions */
230 unsigned int flags = O_NONBLOCK|O_EXCL;
231 int i, opt, all = 0, log = 0, verbose = 0;
232 int ret, err;
234 try_old_version("rmmod", argv);
236 while ((opt = getopt_long(argc, argv,
237 "afh?swvV", options, NULL)) != EOF) {
238 switch (opt) {
239 case 'a': // --all
240 all = 1;
241 break;
242 case 'f': // --force
243 flags |= O_TRUNC;
244 break;
245 case 's': // --syslog
246 openlog("rmmod", LOG_CONS, LOG_DAEMON);
247 log = 1;
248 break;
249 case 'w': // --wait
250 flags &= ~O_NONBLOCK;
251 break;
252 case 'v': // --verbose
253 verbose++;
254 break;
255 case 'V': // --version
256 puts(PACKAGE " version " VERSION);
257 return 0;
259 default:
260 print_usage(argv[0]);
263 if (!argv[optind]) {
264 if (all) {
265 /* FIXME implement */
266 exit(1);
268 fprintf(stderr, "no module names given\n");
269 print_usage(argv[0]);
272 err = 0;
273 /* remove each specified module */
274 for (i = optind; i < argc; i++) {
275 ret = rmmod(log, verbose, argv[i], flags);
276 if (ret != 0)
277 err = 1;
280 if (log)
281 closelog();
282 exit(err);