blob: 97650a4b290fb6e58c50b95bf0a6ed7c5251ee59 [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"
Erik Andersen3d7e3411999-12-16 23:04:20 +000011#include <stdio.h>
12#include <errno.h>
13#include <unistd.h>
Eric Andersened3ef502001-01-27 08:24:39 +000014#include <stdlib.h>
Mark Whitleyf90c28d2001-03-09 21:49:12 +000015#include <getopt.h>
Eric Andersene7047882003-12-11 01:42:13 +000016#include <fcntl.h>
Eric Andersenc0693ed2004-07-20 10:05:13 +000017#include <string.h>
Rob Landley52219872005-11-27 19:01:53 +000018#include <sys/utsname.h>
Eric Andersene7047882003-12-11 01:42:13 +000019#include <sys/syscall.h>
Erik Andersen3d7e3411999-12-16 23:04:20 +000020
Eric Andersenc0693ed2004-07-20 10:05:13 +000021#ifdef CONFIG_FEATURE_2_6_MODULES
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000022static inline void filename2modname(char *modname, const char *afterslash)
Eric Andersenc0693ed2004-07-20 10:05:13 +000023{
Eric Andersenc0693ed2004-07-20 10:05:13 +000024 unsigned int i;
25
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000026#if ENABLE_FEATURE_2_4_MODULES
27 int kr_chk = 1;
Bernhard Reutner-Fischere2e56c72006-05-19 11:54:02 +000028 if (get_linux_version_code() <= KERNEL_VERSION(2,6,0))
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000029 kr_chk = 0;
30#else
31#define kr_chk 1
32#endif
Eric Andersenc0693ed2004-07-20 10:05:13 +000033
34 /* Convert to underscores, stop at first . */
35 for (i = 0; afterslash[i] && afterslash[i] != '.'; i++) {
Rob Landley52219872005-11-27 19:01:53 +000036 if (kr_chk && (afterslash[i] == '-'))
Eric Andersenc0693ed2004-07-20 10:05:13 +000037 modname[i] = '_';
38 else
39 modname[i] = afterslash[i];
40 }
41 modname[i] = '\0';
42}
43#endif
44
Rob Landleydfba7412006-03-06 20:47:33 +000045int rmmod_main(int argc, char **argv)
Erik Andersen3d7e3411999-12-16 23:04:20 +000046{
Mark Whitleyf90c28d2001-03-09 21:49:12 +000047 int n, ret = EXIT_SUCCESS;
Eric Andersene8521f12004-07-13 00:09:34 +000048 unsigned int flags = O_NONBLOCK|O_EXCL;
49#ifdef CONFIG_FEATURE_QUERY_MODULE_INTERFACE
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000050 /* bb_common_bufsiz1 hold the module names which we ignore
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000051 but must get */
52 size_t bufsize = sizeof(bb_common_bufsiz1);
Eric Andersene8521f12004-07-13 00:09:34 +000053#endif
Erik Andersen3d7e3411999-12-16 23:04:20 +000054
Mark Whitleyf90c28d2001-03-09 21:49:12 +000055 /* Parse command line. */
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000056 n = bb_getopt_ulflags(argc, argv, "wfa");
57 if((n & 1)) // --wait
58 flags &= ~O_NONBLOCK;
59 if((n & 2)) // --force
60 flags |= O_TRUNC;
61 if((n & 4)) {
62 /* 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) {
Rob Landley82327f42005-12-11 19:46:50 +000068 if (syscall(__NR_delete_module, NULL, flags) != 0) {
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000069 if (errno==EFAULT)
70 return(ret);
71 bb_perror_msg_and_die("rmmod");
72 }
73 pnmod = nmod;
Eric Andersene8521f12004-07-13 00:09:34 +000074#ifdef CONFIG_FEATURE_QUERY_MODULE_INTERFACE
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000075 /* 1 == QM_MODULES */
76 if (my_query_module(NULL, 1, &bb_common_bufsiz1, &bufsize, &nmod)) {
77 bb_perror_msg_and_die("QM_MODULES");
78 }
Eric Andersene8521f12004-07-13 00:09:34 +000079#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000080 }
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000081 return EXIT_SUCCESS;
Erik Andersen3d7e3411999-12-16 23:04:20 +000082 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000083
Mark Whitleyf90c28d2001-03-09 21:49:12 +000084 if (optind == argc)
Eric Andersen3b1a7442003-12-24 20:30:45 +000085 bb_show_usage();
Mark Whitleyf90c28d2001-03-09 21:49:12 +000086
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000087 for (n = optind; n < argc; n++) {
Eric Andersenc0693ed2004-07-20 10:05:13 +000088#ifdef CONFIG_FEATURE_2_6_MODULES
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000089 const char *afterslash;
90 char *module_name;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000091
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000092 afterslash = strrchr(argv[n], '/');
93 if (!afterslash)
94 afterslash = argv[n];
95 else
96 afterslash++;
97 module_name = alloca(strlen(afterslash) + 1);
98 filename2modname(module_name, afterslash);
Eric Andersenc0693ed2004-07-20 10:05:13 +000099#else
100#define module_name argv[n]
101#endif
Rob Landley82327f42005-12-11 19:46:50 +0000102 if (syscall(__NR_delete_module, module_name, flags) != 0) {
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +0000103 bb_perror_msg("%s", argv[n]);
104 ret = EXIT_FAILURE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000105 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000106 }
Mark Whitleyf90c28d2001-03-09 21:49:12 +0000107
Eric Andersena5716d32000-07-28 15:16:37 +0000108 return(ret);
Erik Andersen3d7e3411999-12-16 23:04:20 +0000109}