blob: cdc690a6936ac6ea32540439f5679d19d4058209 [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 *
Bernhard Reutner-Fischerdeda6a52006-06-03 19:35:15 +00008 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Erik Andersen3d7e3411999-12-16 23:04:20 +00009 */
10
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000011#include "libbb.h"
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000012#include "modutils.h"
Rob Landleyabfe1072006-08-28 19:40:08 +000013
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000014int rmmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000015int rmmod_main(int argc UNUSED_PARAM, char **argv)
Erik Andersen3d7e3411999-12-16 23:04:20 +000016{
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000017 int n;
Eric Andersene8521f12004-07-13 00:09:34 +000018 unsigned int flags = O_NONBLOCK|O_EXCL;
Erik Andersen3d7e3411999-12-16 23:04:20 +000019
Mark Whitleyf90c28d2001-03-09 21:49:12 +000020 /* Parse command line. */
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000021 n = getopt32(argv, "wfas"); // -s ignored
22 argv += optind;
23
Denis Vlasenko51742f42007-04-12 00:32:05 +000024 if (n & 1) // --wait
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000025 flags &= ~O_NONBLOCK;
Denis Vlasenko51742f42007-04-12 00:32:05 +000026 if (n & 2) // --force
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000027 flags |= O_TRUNC;
Denis Vlasenko51742f42007-04-12 00:32:05 +000028 if (n & 4) {
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000029 /* Unload _all_ unused modules via NULL delete_module() call */
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000030 if (bb_delete_module(NULL, flags) != 0 && errno != EFAULT)
31 bb_perror_msg_and_die("rmmod");
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000032 return EXIT_SUCCESS;
Erik Andersen3d7e3411999-12-16 23:04:20 +000033 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000034
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000035 if (!*argv)
Eric Andersen3b1a7442003-12-24 20:30:45 +000036 bb_show_usage();
Mark Whitleyf90c28d2001-03-09 21:49:12 +000037
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000038 while (*argv) {
39 char modname[MODULE_NAME_LEN];
40 filename2modname(bb_basename(*argv++), modname);
41 if (bb_delete_module(modname, flags))
42 bb_error_msg_and_die("cannot unload '%s': %s",
43 modname, moderror(errno));
Erik Andersene49d5ec2000-02-08 19:58:47 +000044 }
Mark Whitleyf90c28d2001-03-09 21:49:12 +000045
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000046 return EXIT_SUCCESS;
Erik Andersen3d7e3411999-12-16 23:04:20 +000047}