blob: 5129b34958adb0da37bf13849da2a0d84f278f51 [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>
Erik Andersen3d7e3411999-12-16 23:04:20 +00006 *
Bernhard Reutner-Fischerdeda6a52006-06-03 19:35:15 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Erik Andersen3d7e3411999-12-16 23:04:20 +00008 */
9
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000010#include "libbb.h"
Bernhard Reutner-Fischer4fec3462008-05-27 10:13:54 +000011
12#ifdef __UCLIBC__
13extern int delete_module(const char *module, unsigned int flags);
14#else
15# include <sys/syscall.h>
16# define delete_module(mod, flags) syscall(__NR_delete_module, mod, flags)
17#endif
Erik Andersen3d7e3411999-12-16 23:04:20 +000018
Denis Vlasenkoe3241842007-08-13 10:36:25 +000019#if ENABLE_FEATURE_2_6_MODULES
Denis Vlasenkodadfb492008-07-29 10:16:05 +000020static void filename2modname(char *modname, const char *afterslash)
Eric Andersenc0693ed2004-07-20 10:05:13 +000021{
Eric Andersenc0693ed2004-07-20 10:05:13 +000022 unsigned int i;
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000023 int kr_chk = 1;
Rob Landleyabfe1072006-08-28 19:40:08 +000024
25 if (ENABLE_FEATURE_2_4_MODULES
26 && get_linux_version_code() <= KERNEL_VERSION(2,6,0))
27 kr_chk = 0;
Eric Andersenc0693ed2004-07-20 10:05:13 +000028
29 /* Convert to underscores, stop at first . */
30 for (i = 0; afterslash[i] && afterslash[i] != '.'; i++) {
Rob Landley52219872005-11-27 19:01:53 +000031 if (kr_chk && (afterslash[i] == '-'))
Eric Andersenc0693ed2004-07-20 10:05:13 +000032 modname[i] = '_';
33 else
34 modname[i] = afterslash[i];
35 }
36 modname[i] = '\0';
37}
Rob Landleyabfe1072006-08-28 19:40:08 +000038#else
39void filename2modname(char *modname, const char *afterslash);
Eric Andersenc0693ed2004-07-20 10:05:13 +000040#endif
41
Rob Landleyabfe1072006-08-28 19:40:08 +000042// There really should be a header file for this...
43
44int query_module(const char *name, int which, void *buf,
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000045 size_t bufsize, size_t *ret);
Rob Landleyabfe1072006-08-28 19:40:08 +000046
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000047int rmmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +000048int rmmod_main(int argc, char **argv)
Erik Andersen3d7e3411999-12-16 23:04:20 +000049{
Mark Whitleyf90c28d2001-03-09 21:49:12 +000050 int n, ret = EXIT_SUCCESS;
Eric Andersene8521f12004-07-13 00:09:34 +000051 unsigned int flags = O_NONBLOCK|O_EXCL;
Erik Andersen3d7e3411999-12-16 23:04:20 +000052
Denis Vlasenko74324c82007-06-04 10:16:52 +000053#define misc_buf bb_common_bufsiz1
54
Mark Whitleyf90c28d2001-03-09 21:49:12 +000055 /* Parse command line. */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000056 n = getopt32(argv, "wfa");
Denis Vlasenko51742f42007-04-12 00:32:05 +000057 if (n & 1) // --wait
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000058 flags &= ~O_NONBLOCK;
Denis Vlasenko51742f42007-04-12 00:32:05 +000059 if (n & 2) // --force
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000060 flags |= O_TRUNC;
Denis Vlasenko51742f42007-04-12 00:32:05 +000061 if (n & 4) {
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000062 /* Unload _all_ unused modules via NULL delete_module() call */
63 /* until the number of modules does not change */
64 size_t nmod = 0; /* number of modules */
65 size_t pnmod = -1; /* previous number of modules */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000066
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000067 while (nmod != pnmod) {
Bernhard Reutner-Fischer4fec3462008-05-27 10:13:54 +000068 if (delete_module(NULL, flags) != 0) {
Denis Vlasenko079f8af2006-11-27 16:49:31 +000069 if (errno == EFAULT)
70 return ret;
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000071 bb_perror_msg_and_die("rmmod");
72 }
73 pnmod = nmod;
Rob Landleyabfe1072006-08-28 19:40:08 +000074 // the 1 here is QM_MODULES.
75 if (ENABLE_FEATURE_QUERY_MODULE_INTERFACE && query_module(NULL,
Denis Vlasenko74324c82007-06-04 10:16:52 +000076 1, misc_buf, sizeof(misc_buf),
Rob Landleyabfe1072006-08-28 19:40:08 +000077 &nmod))
78 {
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000079 bb_perror_msg_and_die("QM_MODULES");
80 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000081 }
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000082 return EXIT_SUCCESS;
Erik Andersen3d7e3411999-12-16 23:04:20 +000083 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000084
Mark Whitleyf90c28d2001-03-09 21:49:12 +000085 if (optind == argc)
Eric Andersen3b1a7442003-12-24 20:30:45 +000086 bb_show_usage();
Mark Whitleyf90c28d2001-03-09 21:49:12 +000087
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000088 for (n = optind; n < argc; n++) {
Rob Landleyabfe1072006-08-28 19:40:08 +000089 if (ENABLE_FEATURE_2_6_MODULES) {
Denis Vlasenkodc757aa2007-06-30 08:04:05 +000090 filename2modname(misc_buf, bb_basename(argv[n]));
Rob Landleyabfe1072006-08-28 19:40:08 +000091 }
92
Bernhard Reutner-Fischer4fec3462008-05-27 10:13:54 +000093 if (delete_module(ENABLE_FEATURE_2_6_MODULES ? misc_buf : argv[n], flags)) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +000094 bb_simple_perror_msg(argv[n]);
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000095 ret = EXIT_FAILURE;
Erik Andersene49d5ec2000-02-08 19:58:47 +000096 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000097 }
Mark Whitleyf90c28d2001-03-09 21:49:12 +000098
Denis Vlasenko079f8af2006-11-27 16:49:31 +000099 return ret;
Erik Andersen3d7e3411999-12-16 23:04:20 +0000100}