• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * kmod-rmmod - remove modules from linux kernel using libkmod.
3  *
4  * Copyright (C) 2011-2013  ProFUSION embedded systems
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <errno.h>
21 #include <getopt.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 
29 #include <shared/macro.h>
30 
31 #include <libkmod/libkmod.h>
32 
33 #include "kmod.h"
34 
35 #define DEFAULT_VERBOSE LOG_ERR
36 static int verbose = DEFAULT_VERBOSE;
37 static int use_syslog;
38 
39 static const char cmdopts_s[] = "fsvVwh";
40 static const struct option cmdopts[] = {
41 	{"force", no_argument, 0, 'f'},
42 	{"syslog", no_argument, 0, 's'},
43 	{"verbose", no_argument, 0, 'v'},
44 	{"version", no_argument, 0, 'V'},
45 	{"help", no_argument, 0, 'h'},
46 	{NULL, 0, 0, 0}
47 };
48 
help(void)49 static void help(void)
50 {
51 	printf("Usage:\n"
52 		"\t%s [options] modulename ...\n"
53 		"Options:\n"
54 		"\t-f, --force       forces a module unload and may crash your\n"
55 		"\t                  machine. This requires Forced Module Removal\n"
56 		"\t                  option in your kernel. DANGEROUS\n"
57 		"\t-s, --syslog      print to syslog, not stderr\n"
58 		"\t-v, --verbose     enables more messages\n"
59 		"\t-V, --version     show version\n"
60 		"\t-h, --help        show this help\n",
61 		program_invocation_short_name);
62 }
63 
check_module_inuse(struct kmod_module * mod)64 static int check_module_inuse(struct kmod_module *mod) {
65 	struct kmod_list *holders;
66 	int state;
67 
68 	state = kmod_module_get_initstate(mod);
69 
70 	if (state == KMOD_MODULE_BUILTIN) {
71 		ERR("Module %s is builtin.\n", kmod_module_get_name(mod));
72 		return -ENOENT;
73 	} else if (state < 0) {
74 		ERR("Module %s is not currently loaded\n",
75 				kmod_module_get_name(mod));
76 		return -ENOENT;
77 	}
78 
79 	holders = kmod_module_get_holders(mod);
80 	if (holders != NULL) {
81 		struct kmod_list *itr;
82 
83 		ERR("Module %s is in use by:", kmod_module_get_name(mod));
84 
85 		kmod_list_foreach(itr, holders) {
86 			struct kmod_module *hm = kmod_module_get_module(itr);
87 			fprintf(stderr, " %s", kmod_module_get_name(hm));
88 			kmod_module_unref(hm);
89 		}
90 		fputc('\n', stderr);
91 
92 		kmod_module_unref_list(holders);
93 		return -EBUSY;
94 	}
95 
96 	if (kmod_module_get_refcnt(mod) != 0) {
97 		ERR("Module %s is in use\n", kmod_module_get_name(mod));
98 		return -EBUSY;
99 	}
100 
101 	return 0;
102 }
103 
do_rmmod(int argc,char * argv[])104 static int do_rmmod(int argc, char *argv[])
105 {
106 	struct kmod_ctx *ctx;
107 	const char *null_config = NULL;
108 	int flags = 0;
109 	int i, err, r = 0;
110 
111 	for (;;) {
112 		int c, idx = 0;
113 		c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
114 		if (c == -1)
115 			break;
116 		switch (c) {
117 		case 'f':
118 			flags |= KMOD_REMOVE_FORCE;
119 			break;
120 		case 's':
121 			use_syslog = 1;
122 			break;
123 		case 'v':
124 			verbose++;
125 			break;
126 		case 'h':
127 			help();
128 			return EXIT_SUCCESS;
129 		case 'V':
130 			puts(PACKAGE " version " VERSION);
131 			puts(KMOD_FEATURES);
132 			return EXIT_SUCCESS;
133 		case '?':
134 			return EXIT_FAILURE;
135 		default:
136 			ERR("unexpected getopt_long() value '%c'.\n", c);
137 			return EXIT_FAILURE;
138 		}
139 	}
140 
141 	log_open(use_syslog);
142 
143 	if (optind >= argc) {
144 		ERR("missing module name.\n");
145 		r = EXIT_FAILURE;
146 		goto done;
147 	}
148 
149 	ctx = kmod_new(NULL, &null_config);
150 	if (!ctx) {
151 		ERR("kmod_new() failed!\n");
152 		r = EXIT_FAILURE;
153 		goto done;
154 	}
155 
156 	log_setup_kmod_log(ctx, verbose);
157 
158 	for (i = optind; i < argc; i++) {
159 		struct kmod_module *mod;
160 		const char *arg = argv[i];
161 		struct stat st;
162 		if (stat(arg, &st) == 0)
163 			err = kmod_module_new_from_path(ctx, arg, &mod);
164 		else
165 			err = kmod_module_new_from_name(ctx, arg, &mod);
166 
167 		if (err < 0) {
168 			ERR("could not use module %s: %s\n", arg,
169 			    strerror(-err));
170 			break;
171 		}
172 
173 		if (!(flags & KMOD_REMOVE_FORCE) && check_module_inuse(mod) < 0) {
174 			r++;
175 			goto next;
176 		}
177 
178 		err = kmod_module_remove_module(mod, flags);
179 		if (err < 0) {
180 			ERR("could not remove module %s: %s\n", arg,
181 			    strerror(-err));
182 			r++;
183 		}
184 next:
185 		kmod_module_unref(mod);
186 	}
187 
188 	kmod_unref(ctx);
189 
190 done:
191 	log_close();
192 
193 	return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
194 }
195 
196 const struct kmod_cmd kmod_cmd_compat_rmmod = {
197 	.name = "rmmod",
198 	.cmd = do_rmmod,
199 	.help = "compat rmmod command",
200 };
201