blob: 22e864d57300fee073a888edd36acb182c0617e8 [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
Bernhard Reutner-Fischerdeda6a52006-06-03 19:35:15 +000010#include "busybox.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
Rob Landleydfba7412006-03-06 20:47:33 +000041int rmmod_main(int argc, char **argv)
Erik Andersen3d7e3411999-12-16 23:04:20 +000042{
Mark Whitleyf90c28d2001-03-09 21:49:12 +000043 int n, ret = EXIT_SUCCESS;
Eric Andersene8521f12004-07-13 00:09:34 +000044 unsigned int flags = O_NONBLOCK|O_EXCL;
Erik Andersen3d7e3411999-12-16 23:04:20 +000045
Mark Whitleyf90c28d2001-03-09 21:49:12 +000046 /* Parse command line. */
Denis Vlasenko67b23e62006-10-03 21:00:06 +000047 n = getopt32(argc, argv, "wfa");
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000048 if((n & 1)) // --wait
49 flags &= ~O_NONBLOCK;
50 if((n & 2)) // --force
51 flags |= O_TRUNC;
52 if((n & 4)) {
53 /* Unload _all_ unused modules via NULL delete_module() call */
54 /* until the number of modules does not change */
55 size_t nmod = 0; /* number of modules */
56 size_t pnmod = -1; /* previous number of modules */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000057
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000058 while (nmod != pnmod) {
Rob Landley82327f42005-12-11 19:46:50 +000059 if (syscall(__NR_delete_module, NULL, flags) != 0) {
Denis Vlasenko079f8af2006-11-27 16:49:31 +000060 if (errno == EFAULT)
61 return ret;
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000062 bb_perror_msg_and_die("rmmod");
63 }
64 pnmod = nmod;
Rob Landleyabfe1072006-08-28 19:40:08 +000065 // the 1 here is QM_MODULES.
66 if (ENABLE_FEATURE_QUERY_MODULE_INTERFACE && query_module(NULL,
67 1, bb_common_bufsiz1, sizeof(bb_common_bufsiz1),
68 &nmod))
69 {
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000070 bb_perror_msg_and_die("QM_MODULES");
71 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000072 }
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000073 return EXIT_SUCCESS;
Erik Andersen3d7e3411999-12-16 23:04:20 +000074 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000075
Mark Whitleyf90c28d2001-03-09 21:49:12 +000076 if (optind == argc)
Eric Andersen3b1a7442003-12-24 20:30:45 +000077 bb_show_usage();
Mark Whitleyf90c28d2001-03-09 21:49:12 +000078
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000079 for (n = optind; n < argc; n++) {
Rob Landleyabfe1072006-08-28 19:40:08 +000080 if (ENABLE_FEATURE_2_6_MODULES) {
81 const char *afterslash;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000082
Rob Landleyabfe1072006-08-28 19:40:08 +000083 afterslash = strrchr(argv[n], '/');
84 if (!afterslash) afterslash = argv[n];
85 else afterslash++;
86 filename2modname(bb_common_bufsiz1, afterslash);
87 }
88
89 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 +000090 bb_perror_msg("%s", argv[n]);
91 ret = EXIT_FAILURE;
Erik Andersene49d5ec2000-02-08 19:58:47 +000092 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000093 }
Mark Whitleyf90c28d2001-03-09 21:49:12 +000094
Denis Vlasenko079f8af2006-11-27 16:49:31 +000095 return ret;
Erik Andersen3d7e3411999-12-16 23:04:20 +000096}