Move more logging to common files
[mit.git] / rmmod.c
blob38fa6af3efc5bf83c191fd90ed0132613223e750
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 "logging.h"
31 #include "testing.h"
32 #include "backwards_compat.c"
34 extern long delete_module(const char *, unsigned int);
36 static char *next_field(const char *line)
38 const char *rest;
40 rest = line + strcspn(line, " ");
41 rest += strspn(rest, " ");
42 return (char *)rest;
45 /* Report that the module is in use. */
46 static void mod_in_use(const char *modname, char *usedby)
48 /* New /proc/modules uses a single "-" to mean "nothing". Old
49 one just puts nothing there. */
50 if (usedby[0] == '-' || strcmp(usedby, "") == 0) {
51 error("Module %s is in use\n", modname);
52 return;
55 /* New /proc/modules *always* prints commas (even if only one) */
56 if (strchr(usedby, ','))
57 /* Blatt over trailing comma for neatness */
58 usedby[strcspn(usedby, " \n")-1] = '\0';
59 else {
60 /* Older /proc/modules just put them as
61 space-separated names. Blatt over trailing \n */
62 usedby[strlen(usedby)-1] = '\0';
65 error("Module %s is in use by %s\n", modname, usedby);
68 /* If we can check usage without entering kernel, do so. */
69 static int check_usage(const char *modname)
71 FILE *module_list;
72 char line[10240], name[64];
73 unsigned long size, refs;
74 int scanned;
76 module_list = fopen("/proc/modules", "r");
77 if (!module_list) {
78 if (errno == ENOENT) /* /proc may not be mounted. */
79 return 0;
80 fatal("can't open /proc/modules: %s\n", strerror(errno));
82 while (fgets(line, sizeof(line)-1, module_list) != NULL) {
83 if (strchr(line, '\n') == NULL) {
84 fatal("V. v. long line broke rmmod.\n");
85 exit(1);
88 scanned = sscanf(line, "%s %lu %lu", name, &size, &refs);
89 if (strcmp(name, modname) != 0)
90 continue;
92 if (scanned < 2)
93 fatal("Unknown format in /proc/modules: %s\n", line);
95 if (scanned == 2)
96 fatal("Kernel does not have unload support.\n");
98 /* Hand it fields 3 onwards. */
99 if (refs != 0)
100 mod_in_use(modname,
101 next_field(next_field(next_field(line))));
102 goto out;
104 error("Module %s does not exist in /proc/modules\n", modname);
105 refs = 1;
106 out:
107 fclose(module_list);
108 return (refs == 0) ? 0 : -EINVAL;
111 static void filename2modname(char *modname, const char *filename)
113 const char *afterslash;
114 unsigned int i;
116 afterslash = strrchr(filename, '/');
117 if (!afterslash)
118 afterslash = filename;
119 else
120 afterslash++;
122 /* Convert to underscores, stop at first . */
123 for (i = 0; afterslash[i] && afterslash[i] != '.'; i++) {
124 if (afterslash[i] == '-')
125 modname[i] = '_';
126 else
127 modname[i] = afterslash[i];
129 modname[i] = '\0';
132 static int rmmod(const char *path, int flags)
134 long ret;
135 char name[strlen(path) + 1];
137 filename2modname(name, path);
139 if ((flags & O_NONBLOCK) && !(flags & O_TRUNC)) {
140 if (check_usage(name) != 0)
141 return 1;
144 info("rmmod %s, wait=%s%s\n", name,
145 (flags & O_NONBLOCK) ? "no" : "yes",
146 (flags & O_TRUNC) ? " force" : "");
148 ret = delete_module(name, flags);
149 if (ret != 0)
150 error("Removing '%s': %s\n", name, strerror(errno));
151 return ret;
154 static struct option options[] = { { "all", 0, NULL, 'a' },
155 { "force", 0, NULL, 'f' },
156 { "help", 0, NULL, 'h' },
157 { "syslog", 0, NULL, 's' },
158 { "verbose", 0, NULL, 'v' },
159 { "version", 0, NULL, 'V' },
160 { "wait", 0, NULL, 'w' },
161 { NULL, 0, NULL, 0 } };
163 static void print_usage(const char *progname)
165 fprintf(stderr,
166 "Usage: %s [-fhswvV] modulename ...\n"
167 // " -a (or --all) to remove all modules (no names needed)\n"
168 " -f (or --force) forces a module unload, and may crash your\n"
169 " machine. This requires the Forced Module Removal option\n"
170 " when the kernel was compiled.\n"
171 " -h (or --help) prints this help text\n"
172 " -s (or --syslog) says use syslog, not stderr\n"
173 " -v (or --verbose) enables more messages\n"
174 " -V (or --version) prints the version code\n"
175 " -w (or --wait) begins a module removal even if it is used\n"
176 " and will stop new users from accessing the module (so it\n"
177 " should eventually fall to zero).\n"
178 , progname);
179 exit(1);
182 int main(int argc, char *argv[])
184 /* O_EXCL so kernels can spot old rmmod versions */
185 unsigned int flags = O_NONBLOCK|O_EXCL;
186 int i, opt, all = 0;
187 int ret, err;
189 try_old_version("rmmod", argv);
191 while ((opt = getopt_long(argc, argv,
192 "afh?swvV", options, NULL)) != EOF) {
193 switch (opt) {
194 case 'a': // --all
195 all = 1;
196 break;
197 case 'f': // --force
198 flags |= O_TRUNC;
199 break;
200 case 's': // --syslog
201 openlog("rmmod", LOG_CONS, LOG_DAEMON);
202 logging = 1;
203 break;
204 case 'w': // --wait
205 flags &= ~O_NONBLOCK;
206 break;
207 case 'v': // --verbose
208 verbose++;
209 break;
210 case 'V': // --version
211 puts(PACKAGE " version " VERSION);
212 return 0;
214 default:
215 print_usage(argv[0]);
218 if (!argv[optind]) {
219 fprintf(stderr, "no module names given\n");
220 print_usage(argv[0]);
223 err = 0;
224 /* remove each specified module */
225 for (i = optind; i < argc; i++) {
226 ret = rmmod(argv[i], flags);
227 if (ret != 0)
228 err = 1;
231 if (logging)
232 closelog();
233 exit(err);