blob: 4c792d3ae00f0137f1f367b127434a7f636e4cb5 [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
Eric Andersenc0693ed2004-07-20 10:05:13 +000013#ifdef CONFIG_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 Vlasenko06af2162007-02-03 17:28:39 +000041int rmmod_main(int argc, char **argv);
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
Mark Whitleyf90c28d2001-03-09 21:49:12 +000047 /* Parse command line. */
Denis Vlasenko67b23e62006-10-03 21:00:06 +000048 n = getopt32(argc, argv, "wfa");
Denis Vlasenko51742f42007-04-12 00:32:05 +000049 if (n & 1) // --wait
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000050 flags &= ~O_NONBLOCK;
Denis Vlasenko51742f42007-04-12 00:32:05 +000051 if (n & 2) // --force
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000052 flags |= O_TRUNC;
Denis Vlasenko51742f42007-04-12 00:32:05 +000053 if (n & 4) {
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000054 /* Unload _all_ unused modules via NULL delete_module() call */
55 /* until the number of modules does not change */
56 size_t nmod = 0; /* number of modules */
57 size_t pnmod = -1; /* previous number of modules */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000058
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000059 while (nmod != pnmod) {
Rob Landley82327f42005-12-11 19:46:50 +000060 if (syscall(__NR_delete_module, NULL, flags) != 0) {
Denis Vlasenko079f8af2006-11-27 16:49:31 +000061 if (errno == EFAULT)
62 return ret;
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000063 bb_perror_msg_and_die("rmmod");
64 }
65 pnmod = nmod;
Rob Landleyabfe1072006-08-28 19:40:08 +000066 // the 1 here is QM_MODULES.
67 if (ENABLE_FEATURE_QUERY_MODULE_INTERFACE && query_module(NULL,
68 1, bb_common_bufsiz1, sizeof(bb_common_bufsiz1),
69 &nmod))
70 {
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000071 bb_perror_msg_and_die("QM_MODULES");
72 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000073 }
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000074 return EXIT_SUCCESS;
Erik Andersen3d7e3411999-12-16 23:04:20 +000075 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000076
Mark Whitleyf90c28d2001-03-09 21:49:12 +000077 if (optind == argc)
Eric Andersen3b1a7442003-12-24 20:30:45 +000078 bb_show_usage();
Mark Whitleyf90c28d2001-03-09 21:49:12 +000079
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000080 for (n = optind; n < argc; n++) {
Rob Landleyabfe1072006-08-28 19:40:08 +000081 if (ENABLE_FEATURE_2_6_MODULES) {
82 const char *afterslash;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000083
Rob Landleyabfe1072006-08-28 19:40:08 +000084 afterslash = strrchr(argv[n], '/');
85 if (!afterslash) afterslash = argv[n];
86 else afterslash++;
87 filename2modname(bb_common_bufsiz1, afterslash);
88 }
89
90 if (syscall(__NR_delete_module, ENABLE_FEATURE_2_6_MODULES ? bb_common_bufsiz1 : argv[n], flags)) {
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000091 bb_perror_msg("%s", argv[n]);
92 ret = EXIT_FAILURE;
Erik Andersene49d5ec2000-02-08 19:58:47 +000093 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000094 }
Mark Whitleyf90c28d2001-03-09 21:49:12 +000095
Denis Vlasenko079f8af2006-11-27 16:49:31 +000096 return ret;
Erik Andersen3d7e3411999-12-16 23:04:20 +000097}