Merge branch 'master' of git://github.com/sourcejedi/module-init-tools
[mit.git] / rmmod.c
blob7ef2e3147046fedb2623f8177f0b02146583e825
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 "util.h"
31 #include "logging.h"
32 #include "testing.h"
33 #include "backwards_compat.c"
35 extern long delete_module(const char *, unsigned int);
37 static char *next_field(const char *line)
39 const char *rest;
41 rest = line + strcspn(line, " ");
42 rest += strspn(rest, " ");
43 return (char *)rest;
46 /* Report that the module is in use. */
47 static void mod_in_use(const char *modname, char *usedby)
49 /* New /proc/modules uses a single "-" to mean "nothing". Old
50 one just puts nothing there. */
51 if (usedby[0] == '-' || streq(usedby, "")) {
52 error("Module %s is in use\n", modname);
53 return;
56 /* New /proc/modules *always* prints commas (even if only one) */
57 if (strchr(usedby, ','))
58 /* Blatt over trailing comma for neatness */
59 usedby[strcspn(usedby, " \n")-1] = '\0';
60 else {
61 /* Older /proc/modules just put them as
62 space-separated names. Blatt over trailing \n */
63 usedby[strlen(usedby)-1] = '\0';
66 error("Module %s is in use by %s\n", modname, usedby);
69 /* If we can check usage without entering kernel, do so. */
70 static int check_usage(const char *modname)
72 FILE *module_list;
73 char line[10240], name[64];
74 unsigned long size, refs;
75 int scanned;
77 module_list = fopen("/proc/modules", "r");
78 if (!module_list) {
79 if (errno == ENOENT) /* /proc may not be mounted. */
80 return 0;
81 fatal("can't open /proc/modules: %s\n", strerror(errno));
83 while (fgets(line, sizeof(line)-1, module_list) != NULL) {
84 if (strchr(line, '\n') == NULL) {
85 fatal("V. v. long line broke rmmod.\n");
86 exit(1);
89 scanned = sscanf(line, "%s %lu %lu", name, &size, &refs);
90 if (strcmp(name, modname) != 0)
91 continue;
93 if (scanned < 2)
94 fatal("Unknown format in /proc/modules: %s\n", line);
96 if (scanned == 2)
97 fatal("Kernel does not have unload support.\n");
99 /* Hand it fields 3 onwards. */
100 if (refs != 0)
101 mod_in_use(modname,
102 next_field(next_field(next_field(line))));
103 goto out;
105 error("Module %s does not exist in /proc/modules\n", modname);
106 refs = 1;
107 out:
108 fclose(module_list);
109 return (refs == 0) ? 0 : -EINVAL;
112 static int rmmod(const char *path, int flags)
114 long ret;
115 char name[strlen(path) + 1];
117 filename2modname(name, path);
119 if ((flags & O_NONBLOCK) && !(flags & O_TRUNC)) {
120 if (check_usage(name) != 0)
121 return 1;
124 info("rmmod %s, wait=%s%s\n", name,
125 (flags & O_NONBLOCK) ? "no" : "yes",
126 (flags & O_TRUNC) ? " force" : "");
128 ret = delete_module(name, flags);
129 if (ret != 0)
130 error("Removing '%s': %s\n", name, strerror(errno));
131 return ret;
134 static struct option options[] = { { "all", 0, NULL, 'a' },
135 { "force", 0, NULL, 'f' },
136 { "help", 0, NULL, 'h' },
137 { "syslog", 0, NULL, 's' },
138 { "verbose", 0, NULL, 'v' },
139 { "version", 0, NULL, 'V' },
140 { "wait", 0, NULL, 'w' },
141 { NULL, 0, NULL, 0 } };
143 static void print_usage(const char *progname)
145 fprintf(stderr,
146 "Usage: %s [-fhswvV] modulename ...\n"
147 // " -a (or --all) to remove all modules (no names needed)\n"
148 " -f (or --force) forces a module unload, and may crash your\n"
149 " machine. This requires the Forced Module Removal option\n"
150 " when the kernel was compiled.\n"
151 " -h (or --help) prints this help text\n"
152 " -s (or --syslog) says use syslog, not stderr\n"
153 " -v (or --verbose) enables more messages\n"
154 " -V (or --version) prints the version code\n"
155 " -w (or --wait) begins a module removal even if it is used\n"
156 " and will stop new users from accessing the module (so it\n"
157 " should eventually fall to zero).\n"
158 , progname);
159 exit(1);
162 int main(int argc, char *argv[])
164 /* O_EXCL so kernels can spot old rmmod versions */
165 unsigned int flags = O_NONBLOCK|O_EXCL;
166 int i, opt, all = 0;
167 int ret, err;
169 try_old_version("rmmod", argv);
171 while ((opt = getopt_long(argc, argv,
172 "afh?swvV", options, NULL)) != EOF) {
173 switch (opt) {
174 case 'a': // --all
175 all = 1;
176 break;
177 case 'f': // --force
178 flags |= O_TRUNC;
179 break;
180 case 's': // --syslog
181 openlog("rmmod", LOG_CONS, LOG_DAEMON);
182 logging = 1;
183 break;
184 case 'w': // --wait
185 flags &= ~O_NONBLOCK;
186 break;
187 case 'v': // --verbose
188 verbose++;
189 break;
190 case 'V': // --version
191 puts(PACKAGE " version " VERSION);
192 return 0;
194 default:
195 print_usage(argv[0]);
198 if (!argv[optind]) {
199 fprintf(stderr, "no module names given\n");
200 print_usage(argv[0]);
203 err = 0;
204 /* remove each specified module */
205 for (i = optind; i < argc; i++) {
206 ret = rmmod(argv[i], flags);
207 if (ret != 0)
208 err = 1;
211 if (logging)
212 closelog();
213 exit(err);