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>
33 #include "backwards_compat.c"
35 extern long delete_module(const char *, unsigned int);
37 static char *next_field(const char *line
)
41 rest
= line
+ strcspn(line
, " ");
42 rest
+= strspn(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
);
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';
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
)
73 char line
[10240], name
[64];
74 unsigned long size
, refs
;
77 module_list
= fopen("/proc/modules", "r");
79 if (errno
== ENOENT
) /* /proc may not be mounted. */
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");
89 scanned
= sscanf(line
, "%s %lu %lu", name
, &size
, &refs
);
90 if (strcmp(name
, modname
) != 0)
94 fatal("Unknown format in /proc/modules: %s\n", line
);
97 fatal("Kernel does not have unload support.\n");
99 /* Hand it fields 3 onwards. */
102 next_field(next_field(next_field(line
))));
105 error("Module %s does not exist in /proc/modules\n", modname
);
109 return (refs
== 0) ? 0 : -EINVAL
;
112 static int rmmod(const char *path
, int flags
)
115 char name
[strlen(path
) + 1];
117 filename2modname(name
, path
);
119 if ((flags
& O_NONBLOCK
) && !(flags
& O_TRUNC
)) {
120 if (check_usage(name
) != 0)
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
);
130 error("Removing '%s': %s\n", name
, strerror(errno
));
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
)
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"
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
;
169 try_old_version("rmmod", argv
);
171 while ((opt
= getopt_long(argc
, argv
,
172 "afh?swvV", options
, NULL
)) != EOF
) {
180 case 's': // --syslog
181 openlog("rmmod", LOG_CONS
, LOG_DAEMON
);
185 flags
&= ~O_NONBLOCK
;
187 case 'v': // --verbose
190 case 'V': // --version
191 puts(PACKAGE
" version " VERSION
);
195 print_usage(argv
[0]);
199 fprintf(stderr
, "no module names given\n");
200 print_usage(argv
[0]);
204 /* remove each specified module */
205 for (i
= optind
; i
< argc
; i
++) {
206 ret
= rmmod(argv
[i
], flags
);