aboutsummaryrefslogtreecommitdiffstats
path: root/rmmod.c
blob: a384fa625f3329ebeae147a13d722deef6121419 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* rmmod.c: remove a module from the kernel.
    Copyright (C) 2001  Rusty Russell.
    Copyright (C) 2002  Rusty Russell, IBM Corporation.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <asm/unistd.h>
#include <stdarg.h>
#include <getopt.h>
#include <syslog.h>

#include "util.h"
#include "logging.h"
#include "testing.h"

extern long delete_module(const char *, unsigned int);

static char *next_field(const char *line)
{
	const char *rest;

	rest = line + strcspn(line, " ");
	rest += strspn(rest, " ");
	return (char *)rest;
}

/* Report that the module is in use. */
static void mod_in_use(const char *modname, char *usedby)
{
	/* New /proc/modules uses a single "-" to mean "nothing".  Old
           one just puts nothing there. */
	if (usedby[0] == '-' || streq(usedby, "")) {
		error("Module %s is in use\n", modname);
		return;
	}

	/* New /proc/modules *always* prints commas (even if only one) */
	if (strchr(usedby, ','))
		/* Blatt over trailing comma for neatness */
		usedby[strcspn(usedby, " \n")-1] = '\0';
	else {
		/* Older /proc/modules just put them as
                   space-separated names.  Blatt over trailing \n */
		usedby[strlen(usedby)-1] = '\0';
	}

	error("Module %s is in use by %s\n", modname, usedby);
}

/* If we can check usage without entering kernel, do so. */
static int check_usage(const char *modname)
{
	FILE *module_list;
	char line[10240], name[64];
	unsigned long size, refs;
	int scanned;

	module_list = fopen("/proc/modules", "r");
	if (!module_list) {
		if (errno == ENOENT) /* /proc may not be mounted. */
			return 0;
		fatal("can't open /proc/modules: %s\n", strerror(errno));
	}
	while (fgets(line, sizeof(line)-1, module_list) != NULL) {
		if (strchr(line, '\n') == NULL) {
			fatal("V. v. long line broke rmmod.\n");
			exit(1);
		}

		scanned = sscanf(line, "%s %lu %lu", name, &size, &refs);
		if (strcmp(name, modname) != 0)
			continue;

		if (scanned < 2)
			fatal("Unknown format in /proc/modules: %s\n", line);

		if (scanned == 2)
			fatal("Kernel does not have unload support.\n");

		/* Hand it fields 3 onwards. */
		if (refs != 0)
			mod_in_use(modname,
				   next_field(next_field(next_field(line))));
		goto out;
	}
	error("Module %s does not exist in /proc/modules\n", modname);
	refs = 1;
 out:
	fclose(module_list);
	return (refs == 0) ? 0 : -EINVAL;
}

static int rmmod(const char *path, int flags)
{
	long ret;
	char name[strlen(path) + 1];

	filename2modname(name, path);

	if ((flags & O_NONBLOCK) && !(flags & O_TRUNC)) {
		if (check_usage(name) != 0)
			return 1;
	}

	info("rmmod %s, wait=%s%s\n", name,
	     (flags & O_NONBLOCK) ? "no" : "yes",
	     (flags & O_TRUNC) ? " force" : "");

	ret = delete_module(name, flags);
	if (ret != 0)
		error("Removing '%s': %s\n", name, strerror(errno));
	return ret;
}

static const struct option options[] = { { "all", 0, NULL, 'a' },
				   { "force", 0, NULL, 'f' },
				   { "help", 0, NULL, 'h' },
				   { "syslog", 0, NULL, 's' },
				   { "verbose", 0, NULL, 'v' },
				   { "version", 0, NULL, 'V' },
				   { "wait", 0, NULL, 'w' },
				   { NULL, 0, NULL, 0 } };

static void print_usage(const char *progname)
{
	fprintf(stderr,
		"Usage: %s [-fhswvV] modulename ...\n"
		// " -a (or --all) to remove all modules (no names needed)\n"
		" -f (or --force) forces a module unload, and may crash your\n"
		"    machine.  This requires the Forced Module Removal option\n"
		"    when the kernel was compiled.\n"
		" -h (or --help) prints this help text\n"
		" -s (or --syslog) says use syslog, not stderr\n"
		" -v (or --verbose) enables more messages\n"
		" -V (or --version) prints the version code\n"
		" -w (or --wait) begins a module removal even if it is used\n"
		"    and will stop new users from accessing the module (so it\n"
		"    should eventually fall to zero).\n"
		, progname);
	exit(1);
}

int main(int argc, char *argv[])
{
	/* O_EXCL so kernels can spot old rmmod versions */
	unsigned int flags = O_NONBLOCK|O_EXCL;
	int i, opt, all = 0;
	int ret, err;

	while ((opt = getopt_long(argc, argv,
			"afh?swvV", options, NULL)) != EOF) {
		switch (opt) {
		case 'a':	// --all
			all = 1;
			break;
		case 'f':	// --force
			flags |= O_TRUNC;
			break;
		case 's':	// --syslog
			openlog("rmmod", LOG_CONS, LOG_DAEMON);
			logging = 1;
			break;
		case 'w':	// --wait
			flags &= ~O_NONBLOCK;
			break;
		case 'v':	// --verbose
			verbose++;
			break;
		case 'V':	// --version
			puts(PACKAGE " version " VERSION);
			return 0;

		default:
			print_usage(argv[0]);
		}
	}
	if (!argv[optind]) {
		fprintf(stderr, "no module names given\n");
		print_usage(argv[0]);
	}

	err = 0;
	/* remove each specified module */
	for (i = optind; i < argc; i++) {
		ret = rmmod(argv[i], flags);
		if (ret != 0)
			err = 1;
	}

	if (logging)
		closelog();
	exit(err);
}