blob: 61cfbd1473ae6bfcd7e488c96d3240667c9f1f2f [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"
Eric Andersene7047882003-12-11 01:42:13 +000011#include <sys/syscall.h>
Erik Andersen3d7e3411999-12-16 23:04:20 +000012
Denis Vlasenkoe3241842007-08-13 10:36:25 +000013#if ENABLE_FEATURE_2_6_MODULES
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000014static inline void filename2modname(char *modname, const char *afterslash)
Eric Andersenc0693ed2004-07-20 10:05:13 +000015{
Eric Andersenc0693ed2004-07-20 10:05:13 +000016 unsigned int i;
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000017 int kr_chk = 1;
Rob Landleyabfe1072006-08-28 19:40:08 +000018
19 if (ENABLE_FEATURE_2_4_MODULES
20 && get_linux_version_code() <= KERNEL_VERSION(2,6,0))
21 kr_chk = 0;
Eric Andersenc0693ed2004-07-20 10:05:13 +000022
23 /* Convert to underscores, stop at first . */
24 for (i = 0; afterslash[i] && afterslash[i] != '.'; i++) {
Rob Landley52219872005-11-27 19:01:53 +000025 if (kr_chk && (afterslash[i] == '-'))
Eric Andersenc0693ed2004-07-20 10:05:13 +000026 modname[i] = '_';
27 else
28 modname[i] = afterslash[i];
29 }
30 modname[i] = '\0';
31}
Rob Landleyabfe1072006-08-28 19:40:08 +000032#else
33void filename2modname(char *modname, const char *afterslash);
Eric Andersenc0693ed2004-07-20 10:05:13 +000034#endif
35
Rob Landleyabfe1072006-08-28 19:40:08 +000036// There really should be a header file for this...
37
38int query_module(const char *name, int which, void *buf,
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000039 size_t bufsize, size_t *ret);
Rob Landleyabfe1072006-08-28 19:40:08 +000040
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000041int rmmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +000042int rmmod_main(int argc, char **argv)
Erik Andersen3d7e3411999-12-16 23:04:20 +000043{
Mark Whitleyf90c28d2001-03-09 21:49:12 +000044 int n, ret = EXIT_SUCCESS;
Eric Andersene8521f12004-07-13 00:09:34 +000045 unsigned int flags = O_NONBLOCK|O_EXCL;
Erik Andersen3d7e3411999-12-16 23:04:20 +000046
Denis Vlasenko74324c82007-06-04 10:16:52 +000047#define misc_buf bb_common_bufsiz1
48
Mark Whitleyf90c28d2001-03-09 21:49:12 +000049 /* Parse command line. */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000050 n = getopt32(argv, "wfa");
Denis Vlasenko51742f42007-04-12 00:32:05 +000051 if (n & 1) // --wait
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000052 flags &= ~O_NONBLOCK;
Denis Vlasenko51742f42007-04-12 00:32:05 +000053 if (n & 2) // --force
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000054 flags |= O_TRUNC;
Denis Vlasenko51742f42007-04-12 00:32:05 +000055 if (n & 4) {
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000056 /* Unload _all_ unused modules via NULL delete_module() call */
57 /* until the number of modules does not change */
58 size_t nmod = 0; /* number of modules */
59 size_t pnmod = -1; /* previous number of modules */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000060
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000061 while (nmod != pnmod) {
Rob Landley82327f42005-12-11 19:46:50 +000062 if (syscall(__NR_delete_module, NULL, flags) != 0) {
Denis Vlasenko079f8af2006-11-27 16:49:31 +000063 if (errno == EFAULT)
64 return ret;
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000065 bb_perror_msg_and_die("rmmod");
66 }
67 pnmod = nmod;
Rob Landleyabfe1072006-08-28 19:40:08 +000068 // the 1 here is QM_MODULES.
69 if (ENABLE_FEATURE_QUERY_MODULE_INTERFACE && query_module(NULL,
Denis Vlasenko74324c82007-06-04 10:16:52 +000070 1, misc_buf, sizeof(misc_buf),
Rob Landleyabfe1072006-08-28 19:40:08 +000071 &nmod))
72 {
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000073 bb_perror_msg_and_die("QM_MODULES");
74 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000075 }
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000076 return EXIT_SUCCESS;
Erik Andersen3d7e3411999-12-16 23:04:20 +000077 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000078
Mark Whitleyf90c28d2001-03-09 21:49:12 +000079 if (optind == argc)
Eric Andersen3b1a7442003-12-24 20:30:45 +000080 bb_show_usage();
Mark Whitleyf90c28d2001-03-09 21:49:12 +000081
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000082 for (n = optind; n < argc; n++) {
Rob Landleyabfe1072006-08-28 19:40:08 +000083 if (ENABLE_FEATURE_2_6_MODULES) {
Denis Vlasenkodc757aa2007-06-30 08:04:05 +000084 filename2modname(misc_buf, bb_basename(argv[n]));
Rob Landleyabfe1072006-08-28 19:40:08 +000085 }
86
Denis Vlasenko74324c82007-06-04 10:16:52 +000087 if (syscall(__NR_delete_module, ENABLE_FEATURE_2_6_MODULES ? misc_buf : argv[n], flags)) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +000088 bb_simple_perror_msg(argv[n]);
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000089 ret = EXIT_FAILURE;
Erik Andersene49d5ec2000-02-08 19:58:47 +000090 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000091 }
Mark Whitleyf90c28d2001-03-09 21:49:12 +000092
Denis Vlasenko079f8af2006-11-27 16:49:31 +000093 return ret;
Erik Andersen3d7e3411999-12-16 23:04:20 +000094}