]> git.ipfire.org Git - thirdparty/kmod.git/blob - tools/kmod-rmmod.c
Change licenses
[thirdparty/kmod.git] / tools / kmod-rmmod.c
1 /*
2 * kmod-rmmod - remove modules from linux kernel using libkmod.
3 *
4 * Copyright (C) 2011 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 <stdio.h>
21 #include <stdlib.h>
22 #include <getopt.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <syslog.h>
29 #include "libkmod.h"
30
31
32 static const char cmdopts_s[] = "fsvVwh";
33 static const struct option cmdopts[] = {
34 {"force", no_argument, 0, 'f'},
35 {"syslog", no_argument, 0, 's'},
36 {"verbose", no_argument, 0, 'v'},
37 {"version", no_argument, 0, 'V'},
38 {"wait", no_argument, 0, 'w'},
39 {"help", no_argument, 0, 'h'},
40 {NULL, 0, 0, 0}
41 };
42
43 static void help(const char *progname)
44 {
45 fprintf(stderr,
46 "Usage:\n"
47 "\t%s [options] modulename ...\n"
48 "Options:\n"
49 "\t-f, --force forces a module unload and may crash your\n"
50 "\t machine. This requires Forced Module Removal\n"
51 "\t option in your kernel. DANGEROUS\n"
52 "\t-s, --syslog print to syslog, not stderr\n"
53 "\t-v, --verbose enables more messages\n"
54 "\t-V, --version show version\n"
55 "\t-w, --wait begins module removal even if it is used and\n"
56 "\t will stop new users from accessing it.\n"
57 "\t-h, --help show this help\n",
58 progname);
59 }
60
61 static void log_syslog(void *data, int priority, const char *file, int line,
62 const char *fn, const char *format,
63 va_list args)
64 {
65 char *str, buf[32];
66 const char *prioname;
67
68 switch (priority) {
69 case LOG_CRIT:
70 prioname = "FATAL";
71 break;
72 case LOG_ERR:
73 prioname = "ERROR";
74 break;
75 case LOG_WARNING:
76 prioname = "WARNING";
77 break;
78 case LOG_NOTICE:
79 prioname = "NOTICE";
80 break;
81 case LOG_INFO:
82 prioname = "INFO";
83 break;
84 case LOG_DEBUG:
85 prioname = "DEBUG";
86 break;
87 default:
88 snprintf(buf, sizeof(buf), "LOG-%03d", priority);
89 prioname = buf;
90 }
91
92 if (vasprintf(&str, format, args) < 0)
93 return;
94 #ifdef ENABLE_DEBUG
95 syslog(LOG_NOTICE, "%s: %s:%d %s() %s", prioname, file, line, fn, str);
96 #else
97 syslog(LOG_NOTICE, "%s: %s", prioname, str);
98 #endif
99 free(str);
100 (void)data;
101 }
102
103 int main(int argc, char *argv[])
104 {
105 struct kmod_ctx *ctx;
106 const char *null_config = NULL;
107 int flags = KMOD_REMOVE_NOWAIT;
108 int use_syslog = 0;
109 int verbose = 0;
110 int i, err = 0;
111
112 for (;;) {
113 int c, idx = 0;
114 c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
115 if (c == -1)
116 break;
117 switch (c) {
118 case 'f':
119 flags |= KMOD_REMOVE_FORCE;
120 break;
121 case 's':
122 use_syslog = 1;
123 break;
124 case 'v':
125 verbose++;
126 break;
127 case 'w':
128 flags &= ~KMOD_REMOVE_NOWAIT;
129 break;
130 case 'h':
131 help(argv[0]);
132 return EXIT_SUCCESS;
133 case 'V':
134 puts(PACKAGE " version " VERSION);
135 return EXIT_SUCCESS;
136 case '?':
137 return EXIT_FAILURE;
138 default:
139 fprintf(stderr,
140 "Error: unexpected getopt_long() value '%c'.\n",
141 c);
142 return EXIT_FAILURE;
143 }
144 }
145
146 if (optind >= argc) {
147 fprintf(stderr, "Error: missing module name.\n");
148 return EXIT_FAILURE;
149 }
150
151 ctx = kmod_new(NULL, &null_config);
152 if (!ctx) {
153 fputs("Error: kmod_new() failed!\n", stderr);
154 return EXIT_FAILURE;
155 }
156
157 kmod_set_log_priority(ctx, kmod_get_log_priority(ctx) + verbose);
158 if (use_syslog) {
159 openlog("rmmod", LOG_CONS, LOG_DAEMON);
160 kmod_set_log_fn(ctx, log_syslog, NULL);
161 }
162
163 for (i = optind; i < argc; i++) {
164 struct kmod_module *mod;
165 const char *arg = argv[i];
166 struct stat st;
167 if (stat(arg, &st) == 0)
168 err = kmod_module_new_from_path(ctx, arg, &mod);
169 else
170 err = kmod_module_new_from_name(ctx, arg, &mod);
171
172 if (err < 0) {
173 fprintf(stderr, "Error: could not use module %s: %s\n",
174 arg, strerror(-err));
175 break;
176 }
177
178 err = kmod_module_remove_module(mod, flags);
179 if (err < 0) {
180 fprintf(stderr,
181 "Error: could not remove module %s: %s\n",
182 arg, strerror(-err));
183 }
184 kmod_module_unref(mod);
185 if (err < 0)
186 break;
187 }
188
189 kmod_unref(ctx);
190
191 if (use_syslog)
192 closelog();
193
194 return err >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
195 }