1/* vi: set sw=4 ts=4: */
2/*
3 * Mini rmmod implementation for busybox
4 *
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6 * Copyright (C) 2008 Timo Teras <timo.teras@iki.fi>
7 *
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
9 */
10
11#include "libbb.h"
12#include "modutils.h"
13
14int rmmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
15int rmmod_main(int argc UNUSED_PARAM, char **argv)
16{
17	int n;
18	unsigned flags = O_NONBLOCK | O_EXCL;
19
20	/* Parse command line. */
21	n = getopt32(argv, "wfas"); // -s ignored
22	argv += optind;
23	if (n & 1)	// --wait
24		flags &= ~O_NONBLOCK;
25	if (n & 2)	// --force
26		flags |= O_TRUNC;
27	if (n & 4) {
28		/* Unload _all_ unused modules via NULL delete_module() call */
29		if (bb_delete_module(NULL, flags) != 0 && errno != EFAULT)
30			bb_perror_msg_and_die("rmmod");
31		return EXIT_SUCCESS;
32	}
33
34	if (!*argv)
35		bb_show_usage();
36
37	n = ENABLE_FEATURE_2_4_MODULES && get_linux_version_code() < KERNEL_VERSION(2,6,0);
38	while (*argv) {
39		char modname[MODULE_NAME_LEN];
40		const char *bname;
41
42		bname = bb_basename(*argv++);
43		if (n)
44			safe_strncpy(modname, bname, MODULE_NAME_LEN);
45		else
46			filename2modname(bname, modname);
47		if (bb_delete_module(modname, flags))
48			bb_error_msg_and_die("can't unload '%s': %s",
49					     modname, moderror(errno));
50	}
51
52	return EXIT_SUCCESS;
53}
54