blob: 51390eb830a0090c98185eba6d26e91077ccb776 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Erik Andersen3d7e3411999-12-16 23:04:20 +00002/*
3 * Mini rmmod implementation for busybox
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Denis Vlasenkoba1315d2008-09-13 14:59:38 +00006 * Copyright (C) 2008 Timo Teras <timo.teras@iki.fi>
Erik Andersen3d7e3411999-12-16 23:04:20 +00007 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Erik Andersen3d7e3411999-12-16 23:04:20 +00009 */
10
Denys Vlasenkoc15613c2010-10-15 11:29:02 +020011//applet:IF_RMMOD(APPLET(rmmod, _BB_DIR_SBIN, _BB_SUID_DROP))
12
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000013#include "libbb.h"
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000014#include "modutils.h"
Rob Landleyabfe1072006-08-28 19:40:08 +000015
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000016int rmmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000017int rmmod_main(int argc UNUSED_PARAM, char **argv)
Erik Andersen3d7e3411999-12-16 23:04:20 +000018{
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000019 int n;
Denis Vlasenko1f632292009-04-13 02:25:40 +000020 unsigned flags = O_NONBLOCK | O_EXCL;
Erik Andersen3d7e3411999-12-16 23:04:20 +000021
Mark Whitleyf90c28d2001-03-09 21:49:12 +000022 /* Parse command line. */
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000023 n = getopt32(argv, "wfas"); // -s ignored
24 argv += optind;
Denis Vlasenko51742f42007-04-12 00:32:05 +000025 if (n & 1) // --wait
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000026 flags &= ~O_NONBLOCK;
Denis Vlasenko51742f42007-04-12 00:32:05 +000027 if (n & 2) // --force
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000028 flags |= O_TRUNC;
Denis Vlasenko51742f42007-04-12 00:32:05 +000029 if (n & 4) {
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000030 /* Unload _all_ unused modules via NULL delete_module() call */
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000031 if (bb_delete_module(NULL, flags) != 0 && errno != EFAULT)
32 bb_perror_msg_and_die("rmmod");
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000033 return EXIT_SUCCESS;
Erik Andersen3d7e3411999-12-16 23:04:20 +000034 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000035
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000036 if (!*argv)
Eric Andersen3b1a7442003-12-24 20:30:45 +000037 bb_show_usage();
Mark Whitleyf90c28d2001-03-09 21:49:12 +000038
Denis Vlasenko1f632292009-04-13 02:25:40 +000039 n = ENABLE_FEATURE_2_4_MODULES && get_linux_version_code() < KERNEL_VERSION(2,6,0);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000040 while (*argv) {
41 char modname[MODULE_NAME_LEN];
Denis Vlasenko1f632292009-04-13 02:25:40 +000042 const char *bname;
43
44 bname = bb_basename(*argv++);
45 if (n)
46 safe_strncpy(modname, bname, MODULE_NAME_LEN);
47 else
48 filename2modname(bname, modname);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000049 if (bb_delete_module(modname, flags))
Denis Vlasenko1f632292009-04-13 02:25:40 +000050 bb_error_msg_and_die("can't unload '%s': %s",
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000051 modname, moderror(errno));
Erik Andersene49d5ec2000-02-08 19:58:47 +000052 }
Mark Whitleyf90c28d2001-03-09 21:49:12 +000053
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000054 return EXIT_SUCCESS;
Erik Andersen3d7e3411999-12-16 23:04:20 +000055}