]>
git.ipfire.org Git - thirdparty/kmod.git/blob - tools/kmod-rmmod.c
2 * kmod-rmmod - remove modules from linux kernel using libkmod.
4 * Copyright (C) 2011 ProFUSION embedded systems
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.
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.
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/>.
25 #include <sys/types.h>
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'},
43 static void help(const char *progname
)
47 "\t%s [options] modulename ...\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",
61 static void log_syslog(void *data
, int priority
, const char *file
, int line
,
62 const char *fn
, const char *format
,
88 snprintf(buf
, sizeof(buf
), "LOG-%03d", priority
);
92 if (vasprintf(&str
, format
, args
) < 0)
95 syslog(LOG_NOTICE
, "%s: %s:%d %s() %s", prioname
, file
, line
, fn
, str
);
97 syslog(LOG_NOTICE
, "%s: %s", prioname
, str
);
103 int main(int argc
, char *argv
[])
105 struct kmod_ctx
*ctx
;
106 const char *null_config
= NULL
;
107 int flags
= KMOD_REMOVE_NOWAIT
;
114 c
= getopt_long(argc
, argv
, cmdopts_s
, cmdopts
, &idx
);
119 flags
|= KMOD_REMOVE_FORCE
;
128 flags
&= ~KMOD_REMOVE_NOWAIT
;
134 puts(PACKAGE
" version " VERSION
);
140 "Error: unexpected getopt_long() value '%c'.\n",
146 if (optind
>= argc
) {
147 fprintf(stderr
, "Error: missing module name.\n");
151 ctx
= kmod_new(NULL
, &null_config
);
153 fputs("Error: kmod_new() failed!\n", stderr
);
157 kmod_set_log_priority(ctx
, kmod_get_log_priority(ctx
) + verbose
);
159 openlog("rmmod", LOG_CONS
, LOG_DAEMON
);
160 kmod_set_log_fn(ctx
, log_syslog
, NULL
);
163 for (i
= optind
; i
< argc
; i
++) {
164 struct kmod_module
*mod
;
165 const char *arg
= argv
[i
];
167 if (stat(arg
, &st
) == 0)
168 err
= kmod_module_new_from_path(ctx
, arg
, &mod
);
170 err
= kmod_module_new_from_name(ctx
, arg
, &mod
);
173 fprintf(stderr
, "Error: could not use module %s: %s\n",
174 arg
, strerror(-err
));
178 err
= kmod_module_remove_module(mod
, flags
);
181 "Error: could not remove module %s: %s\n",
182 arg
, strerror(-err
));
184 kmod_module_unref(mod
);
194 return err
>= 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;