Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini rmmod implementation for busybox |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 6 | * Copyright (C) 2008 Timo Teras <timo.teras@iki.fi> |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 7 | * |
Bernhard Reutner-Fischer | deda6a5 | 2006-06-03 19:35:15 +0000 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 11 | #include "libbb.h" |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 12 | #include "modutils.h" |
Rob Landley | abfe107 | 2006-08-28 19:40:08 +0000 | [diff] [blame] | 13 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 14 | int rmmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 15 | int rmmod_main(int argc UNUSED_PARAM, char **argv) |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 16 | { |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 17 | int n; |
Denis Vlasenko | 1f63229 | 2009-04-13 02:25:40 +0000 | [diff] [blame] | 18 | unsigned flags = O_NONBLOCK | O_EXCL; |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 19 | |
Mark Whitley | f90c28d | 2001-03-09 21:49:12 +0000 | [diff] [blame] | 20 | /* Parse command line. */ |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 21 | n = getopt32(argv, "wfas"); // -s ignored |
| 22 | argv += optind; |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 23 | if (n & 1) // --wait |
"Vladimir N. Oleynik" | 8c44f01 | 2005-11-28 15:54:22 +0000 | [diff] [blame] | 24 | flags &= ~O_NONBLOCK; |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 25 | if (n & 2) // --force |
"Vladimir N. Oleynik" | 8c44f01 | 2005-11-28 15:54:22 +0000 | [diff] [blame] | 26 | flags |= O_TRUNC; |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 27 | if (n & 4) { |
"Vladimir N. Oleynik" | 8c44f01 | 2005-11-28 15:54:22 +0000 | [diff] [blame] | 28 | /* Unload _all_ unused modules via NULL delete_module() call */ |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 29 | if (bb_delete_module(NULL, flags) != 0 && errno != EFAULT) |
| 30 | bb_perror_msg_and_die("rmmod"); |
"Vladimir N. Oleynik" | 8c44f01 | 2005-11-28 15:54:22 +0000 | [diff] [blame] | 31 | return EXIT_SUCCESS; |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 32 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 33 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 34 | if (!*argv) |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 35 | bb_show_usage(); |
Mark Whitley | f90c28d | 2001-03-09 21:49:12 +0000 | [diff] [blame] | 36 | |
Denis Vlasenko | 1f63229 | 2009-04-13 02:25:40 +0000 | [diff] [blame] | 37 | n = ENABLE_FEATURE_2_4_MODULES && get_linux_version_code() < KERNEL_VERSION(2,6,0); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 38 | while (*argv) { |
| 39 | char modname[MODULE_NAME_LEN]; |
Denis Vlasenko | 1f63229 | 2009-04-13 02:25:40 +0000 | [diff] [blame] | 40 | const char *bname; |
| 41 | |
| 42 | bname = bb_basename(*argv++); |
| 43 | if (n) |
| 44 | safe_strncpy(modname, bname, MODULE_NAME_LEN); |
| 45 | else |
| 46 | filename2modname(bname, modname); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 47 | if (bb_delete_module(modname, flags)) |
Denis Vlasenko | 1f63229 | 2009-04-13 02:25:40 +0000 | [diff] [blame] | 48 | bb_error_msg_and_die("can't unload '%s': %s", |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 49 | modname, moderror(errno)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 50 | } |
Mark Whitley | f90c28d | 2001-03-09 21:49:12 +0000 | [diff] [blame] | 51 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 52 | return EXIT_SUCCESS; |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 53 | } |