Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini swapon/swapoff 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> |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 8 | */ |
Denys Vlasenko | f417ffd | 2016-11-14 17:30:50 +0100 | [diff] [blame] | 9 | //config:config SWAPON |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 10 | //config: bool "swapon (4.9 kb)" |
Denys Vlasenko | f417ffd | 2016-11-14 17:30:50 +0100 | [diff] [blame] | 11 | //config: default y |
| 12 | //config: select PLATFORM_LINUX |
| 13 | //config: help |
Denys Vlasenko | f417ffd | 2016-11-14 17:30:50 +0100 | [diff] [blame] | 14 | //config: Once you have created some swap space using 'mkswap', you also need |
| 15 | //config: to enable your swap space with the 'swapon' utility. The 'swapoff' |
| 16 | //config: utility is used, typically at system shutdown, to disable any swap |
| 17 | //config: space. If you are not using any swap space, you can leave this |
| 18 | //config: option disabled. |
| 19 | //config: |
| 20 | //config:config FEATURE_SWAPON_DISCARD |
| 21 | //config: bool "Support discard option -d" |
| 22 | //config: default y |
| 23 | //config: depends on SWAPON |
| 24 | //config: help |
| 25 | //config: Enable support for discarding swap area blocks at swapon and/or as |
| 26 | //config: the kernel frees them. This option enables both the -d option on |
| 27 | //config: 'swapon' and the 'discard' option for swap entries in /etc/fstab. |
| 28 | //config: |
| 29 | //config:config FEATURE_SWAPON_PRI |
| 30 | //config: bool "Support priority option -p" |
| 31 | //config: default y |
| 32 | //config: depends on SWAPON |
| 33 | //config: help |
| 34 | //config: Enable support for setting swap device priority in swapon. |
| 35 | //config: |
| 36 | //config:config SWAPOFF |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 37 | //config: bool "swapoff (4.3 kb)" |
Denys Vlasenko | f417ffd | 2016-11-14 17:30:50 +0100 | [diff] [blame] | 38 | //config: default y |
| 39 | //config: select PLATFORM_LINUX |
Denys Vlasenko | f417ffd | 2016-11-14 17:30:50 +0100 | [diff] [blame] | 40 | |
Denys Vlasenko | 205d48e | 2017-01-29 14:57:33 +0100 | [diff] [blame] | 41 | // APPLET_ODDNAME:name main location suid_type help |
| 42 | //applet:IF_SWAPON( APPLET_ODDNAME(swapon, swap_on_off, BB_DIR_SBIN, BB_SUID_DROP, swapon)) |
Denys Vlasenko | f417ffd | 2016-11-14 17:30:50 +0100 | [diff] [blame] | 43 | //applet:IF_SWAPOFF(APPLET_ODDNAME(swapoff, swap_on_off, BB_DIR_SBIN, BB_SUID_DROP, swapoff)) |
| 44 | |
| 45 | //kbuild:lib-$(CONFIG_SWAPON) += swaponoff.o |
| 46 | //kbuild:lib-$(CONFIG_SWAPOFF) += swaponoff.o |
| 47 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 48 | //usage:#define swapon_trivial_usage |
René Rhéaume | e769577 | 2015-01-05 20:35:00 +0100 | [diff] [blame] | 49 | //usage: "[-a] [-e]" IF_FEATURE_SWAPON_DISCARD(" [-d[POL]]") IF_FEATURE_SWAPON_PRI(" [-p PRI]") " [DEVICE]" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 50 | //usage:#define swapon_full_usage "\n\n" |
| 51 | //usage: "Start swapping on DEVICE\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 52 | //usage: "\n -a Start swapping on all swap devices" |
Matt Whitlock | 0a53b20 | 2014-03-22 19:21:01 -0400 | [diff] [blame] | 53 | //usage: IF_FEATURE_SWAPON_DISCARD( |
| 54 | //usage: "\n -d[POL] Discard blocks at swapon (POL=once)," |
| 55 | //usage: "\n as freed (POL=pages), or both (POL omitted)" |
| 56 | //usage: ) |
René Rhéaume | e769577 | 2015-01-05 20:35:00 +0100 | [diff] [blame] | 57 | //usage: "\n -e Silently skip devices that do not exist" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 58 | //usage: IF_FEATURE_SWAPON_PRI( |
| 59 | //usage: "\n -p PRI Set swap device priority" |
| 60 | //usage: ) |
| 61 | //usage: |
| 62 | //usage:#define swapoff_trivial_usage |
Mike Frysinger | 5f11ec3 | 2015-12-16 12:59:08 -0500 | [diff] [blame] | 63 | //usage: "[-a] [DEVICE]" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 64 | //usage:#define swapoff_full_usage "\n\n" |
| 65 | //usage: "Stop swapping on DEVICE\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 66 | //usage: "\n -a Stop swapping on all swap devices" |
| 67 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 68 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 69 | #include "common_bufsiz.h" |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 70 | #include <mntent.h> |
Denys Vlasenko | 14bd16a | 2011-07-08 08:49:40 +0200 | [diff] [blame] | 71 | #ifndef __BIONIC__ |
| 72 | # include <sys/swap.h> |
| 73 | #endif |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 74 | |
Natanael Copa | 9aff299 | 2009-09-20 04:28:22 +0200 | [diff] [blame] | 75 | #if ENABLE_FEATURE_MOUNT_LABEL |
| 76 | # include "volume_id.h" |
| 77 | #else |
| 78 | # define resolve_mount_spec(fsname) ((void)0) |
| 79 | #endif |
| 80 | |
Denys Vlasenko | 14bd16a | 2011-07-08 08:49:40 +0200 | [diff] [blame] | 81 | #ifndef MNTTYPE_SWAP |
| 82 | # define MNTTYPE_SWAP "swap" |
| 83 | #endif |
| 84 | |
Matt Whitlock | 0a53b20 | 2014-03-22 19:21:01 -0400 | [diff] [blame] | 85 | #if ENABLE_FEATURE_SWAPON_DISCARD |
| 86 | #ifndef SWAP_FLAG_DISCARD |
| 87 | #define SWAP_FLAG_DISCARD 0x10000 |
| 88 | #endif |
| 89 | #ifndef SWAP_FLAG_DISCARD_ONCE |
| 90 | #define SWAP_FLAG_DISCARD_ONCE 0x20000 |
| 91 | #endif |
| 92 | #ifndef SWAP_FLAG_DISCARD_PAGES |
| 93 | #define SWAP_FLAG_DISCARD_PAGES 0x40000 |
| 94 | #endif |
| 95 | #define SWAP_FLAG_DISCARD_MASK \ |
| 96 | (SWAP_FLAG_DISCARD | SWAP_FLAG_DISCARD_ONCE | SWAP_FLAG_DISCARD_PAGES) |
| 97 | #endif |
| 98 | |
| 99 | |
| 100 | #if ENABLE_FEATURE_SWAPON_DISCARD || ENABLE_FEATURE_SWAPON_PRI |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 101 | struct globals { |
| 102 | int flags; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 103 | } FIX_ALIASING; |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 104 | #define G (*(struct globals*)bb_common_bufsiz1) |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 105 | #define g_flags (G.flags) |
Tito Ragusa | a3f326c | 2014-03-31 16:39:26 +0200 | [diff] [blame] | 106 | #define save_g_flags() int save_g_flags = g_flags |
| 107 | #define restore_g_flags() g_flags = save_g_flags |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 108 | #else |
| 109 | #define g_flags 0 |
Tito Ragusa | a3f326c | 2014-03-31 16:39:26 +0200 | [diff] [blame] | 110 | #define save_g_flags() ((void)0) |
| 111 | #define restore_g_flags() ((void)0) |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 112 | #endif |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 113 | #define INIT_G() do { setup_common_bufsiz(); } while (0) |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 114 | |
Denys Vlasenko | f417ffd | 2016-11-14 17:30:50 +0100 | [diff] [blame] | 115 | #if ENABLE_SWAPOFF |
| 116 | # if ENABLE_SWAPON |
| 117 | # define do_swapoff (applet_name[5] == 'f') |
| 118 | # else |
| 119 | # define do_swapoff 1 |
| 120 | # endif |
| 121 | #else |
| 122 | # define do_swapoff 0 |
| 123 | #endif |
Tito Ragusa | a3f326c | 2014-03-31 16:39:26 +0200 | [diff] [blame] | 124 | |
| 125 | /* Command line options */ |
| 126 | enum { |
| 127 | OPTBIT_a, /* -a all */ |
René Rhéaume | e769577 | 2015-01-05 20:35:00 +0100 | [diff] [blame] | 128 | OPTBIT_e, /* -e ifexists */ |
Tito Ragusa | a3f326c | 2014-03-31 16:39:26 +0200 | [diff] [blame] | 129 | IF_FEATURE_SWAPON_DISCARD( OPTBIT_d ,) /* -d discard */ |
| 130 | IF_FEATURE_SWAPON_PRI ( OPTBIT_p ,) /* -p priority */ |
| 131 | OPT_a = 1 << OPTBIT_a, |
René Rhéaume | e769577 | 2015-01-05 20:35:00 +0100 | [diff] [blame] | 132 | OPT_e = 1 << OPTBIT_e, |
Tito Ragusa | a3f326c | 2014-03-31 16:39:26 +0200 | [diff] [blame] | 133 | OPT_d = IF_FEATURE_SWAPON_DISCARD((1 << OPTBIT_d)) + 0, |
| 134 | OPT_p = IF_FEATURE_SWAPON_PRI ((1 << OPTBIT_p)) + 0, |
| 135 | }; |
| 136 | |
| 137 | #define OPT_ALL (option_mask32 & OPT_a) |
| 138 | #define OPT_DISCARD (option_mask32 & OPT_d) |
René Rhéaume | e769577 | 2015-01-05 20:35:00 +0100 | [diff] [blame] | 139 | #define OPT_IFEXISTS (option_mask32 & OPT_e) |
Tito Ragusa | a3f326c | 2014-03-31 16:39:26 +0200 | [diff] [blame] | 140 | #define OPT_PRIO (option_mask32 & OPT_p) |
| 141 | |
Denys Vlasenko | 6c634f7 | 2015-12-18 19:02:31 +0100 | [diff] [blame] | 142 | static int swap_enable_disable(char *device) |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 143 | { |
Tito Ragusa | a3f326c | 2014-03-31 16:39:26 +0200 | [diff] [blame] | 144 | int err = 0; |
| 145 | int quiet = 0; |
Eric Andersen | 97b141a | 2002-11-03 00:25:23 +0000 | [diff] [blame] | 146 | |
Natanael Copa | 9aff299 | 2009-09-20 04:28:22 +0200 | [diff] [blame] | 147 | resolve_mount_spec(&device); |
Eric Andersen | 97b141a | 2002-11-03 00:25:23 +0000 | [diff] [blame] | 148 | |
Tito Ragusa | a3f326c | 2014-03-31 16:39:26 +0200 | [diff] [blame] | 149 | if (do_swapoff) { |
| 150 | err = swapoff(device); |
| 151 | /* Don't complain on OPT_ALL if not a swap device or if it doesn't exist */ |
| 152 | quiet = (OPT_ALL && (errno == EINVAL || errno == ENOENT)); |
| 153 | } else { |
| 154 | /* swapon */ |
Mike Frysinger | 5f11ec3 | 2015-12-16 12:59:08 -0500 | [diff] [blame] | 155 | struct stat st; |
Tito Ragusa | a3f326c | 2014-03-31 16:39:26 +0200 | [diff] [blame] | 156 | err = stat(device, &st); |
| 157 | if (!err) { |
| 158 | if (ENABLE_DESKTOP && S_ISREG(st.st_mode)) { |
| 159 | if (st.st_blocks * (off_t)512 < st.st_size) { |
| 160 | bb_error_msg("%s: file has holes", device); |
| 161 | return 1; |
| 162 | } |
| 163 | } |
| 164 | err = swapon(device, g_flags); |
| 165 | /* Don't complain on swapon -a if device is already in use */ |
Mike Frysinger | 5f11ec3 | 2015-12-16 12:59:08 -0500 | [diff] [blame] | 166 | quiet = (OPT_ALL && errno == EBUSY); |
Tito Ragusa | a3f326c | 2014-03-31 16:39:26 +0200 | [diff] [blame] | 167 | } |
Mike Frysinger | 5f11ec3 | 2015-12-16 12:59:08 -0500 | [diff] [blame] | 168 | /* Don't complain if file does not exist with -e option */ |
| 169 | if (err && OPT_IFEXISTS && errno == ENOENT) |
| 170 | err = 0; |
Eric Andersen | db1df5e | 2002-10-26 10:27:42 +0000 | [diff] [blame] | 171 | } |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 172 | |
Tito Ragusa | c9a6713 | 2014-04-01 09:51:27 +0200 | [diff] [blame] | 173 | if (err && !quiet) { |
| 174 | bb_simple_perror_msg(device); |
Tito Ragusa | a3f326c | 2014-03-31 16:39:26 +0200 | [diff] [blame] | 175 | return 1; |
| 176 | } |
Mike Frysinger | 2d5e4f6 | 2005-09-16 04:41:20 +0000 | [diff] [blame] | 177 | return 0; |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Tito Ragusa | a3f326c | 2014-03-31 16:39:26 +0200 | [diff] [blame] | 180 | #if ENABLE_FEATURE_SWAPON_DISCARD |
| 181 | static void set_discard_flag(char *s) |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 182 | { |
Tito Ragusa | a3f326c | 2014-03-31 16:39:26 +0200 | [diff] [blame] | 183 | /* Unset the flag first to allow fstab options to override */ |
| 184 | /* options set on the command line */ |
| 185 | g_flags = (g_flags & ~SWAP_FLAG_DISCARD_MASK) | SWAP_FLAG_DISCARD; |
| 186 | |
| 187 | if (!s) /* No optional policy value on the commandline */ |
| 188 | return; |
| 189 | /* Skip prepended '=' */ |
| 190 | if (*s == '=') |
| 191 | s++; |
| 192 | /* For fstab parsing: remove other appended options */ |
| 193 | *strchrnul(s, ',') = '\0'; |
| 194 | |
| 195 | if (strcmp(s, "once") == 0) |
| 196 | g_flags |= SWAP_FLAG_DISCARD_ONCE; |
| 197 | if (strcmp(s, "pages") == 0) |
| 198 | g_flags |= SWAP_FLAG_DISCARD_PAGES; |
| 199 | } |
| 200 | #else |
| 201 | #define set_discard_flag(s) ((void)0) |
Matt Whitlock | b9bbd4d | 2014-03-22 19:10:08 -0400 | [diff] [blame] | 202 | #endif |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 203 | |
Tito Ragusa | a3f326c | 2014-03-31 16:39:26 +0200 | [diff] [blame] | 204 | #if ENABLE_FEATURE_SWAPON_PRI |
| 205 | static void set_priority_flag(char *s) |
| 206 | { |
| 207 | unsigned prio; |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 208 | |
Tito Ragusa | a3f326c | 2014-03-31 16:39:26 +0200 | [diff] [blame] | 209 | /* For fstab parsing: remove other appended options */ |
| 210 | *strchrnul(s, ',') = '\0'; |
| 211 | /* Max allowed 32767 (== SWAP_FLAG_PRIO_MASK) */ |
| 212 | prio = bb_strtou(s, NULL, 10); |
| 213 | if (!errno) { |
| 214 | /* Unset the flag first to allow fstab options to override */ |
| 215 | /* options set on the command line */ |
| 216 | g_flags = (g_flags & ~SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER | |
| 217 | MIN(prio, SWAP_FLAG_PRIO_MASK); |
| 218 | } |
| 219 | } |
| 220 | #else |
| 221 | #define set_priority_flag(s) ((void)0) |
| 222 | #endif |
| 223 | |
| 224 | static int do_em_all_in_fstab(void) |
| 225 | { |
| 226 | struct mntent *m; |
| 227 | int err = 0; |
| 228 | FILE *f = xfopen_for_read("/etc/fstab"); |
| 229 | |
Lauri Kasanen | d2844fc | 2010-04-29 22:20:57 +0200 | [diff] [blame] | 230 | while ((m = getmntent(f)) != NULL) { |
| 231 | if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0) { |
| 232 | /* swapon -a should ignore entries with noauto, |
Tito Ragusa | a3f326c | 2014-03-31 16:39:26 +0200 | [diff] [blame] | 233 | * but swapoff -a should process them |
| 234 | */ |
| 235 | if (do_swapoff || hasmntopt(m, MNTOPT_NOAUTO) == NULL) { |
| 236 | /* each swap space might have different flags */ |
| 237 | /* save global flags for the next round */ |
| 238 | save_g_flags(); |
| 239 | if (ENABLE_FEATURE_SWAPON_DISCARD) { |
| 240 | char *p = hasmntopt(m, "discard"); |
| 241 | if (p) { |
| 242 | /* move to '=' or to end of string */ |
| 243 | p += 7; |
| 244 | set_discard_flag(p); |
Tito Ragusa | 8c7fcbd | 2013-08-08 10:21:27 +0200 | [diff] [blame] | 245 | } |
| 246 | } |
Tito Ragusa | a3f326c | 2014-03-31 16:39:26 +0200 | [diff] [blame] | 247 | if (ENABLE_FEATURE_SWAPON_PRI) { |
| 248 | char *p = hasmntopt(m, "pri"); |
| 249 | if (p) { |
| 250 | set_priority_flag(p + 4); |
| 251 | } |
| 252 | } |
| 253 | err |= swap_enable_disable(m->mnt_fsname); |
| 254 | restore_g_flags(); |
Lauri Kasanen | d2844fc | 2010-04-29 22:20:57 +0200 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | } |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 258 | |
Lauri Kasanen | d2844fc | 2010-04-29 22:20:57 +0200 | [diff] [blame] | 259 | if (ENABLE_FEATURE_CLEAN_UP) |
| 260 | endmntent(f); |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 261 | |
Eric Andersen | db1df5e | 2002-10-26 10:27:42 +0000 | [diff] [blame] | 262 | return err; |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Tito Ragusa | a3f326c | 2014-03-31 16:39:26 +0200 | [diff] [blame] | 265 | static int do_all_in_proc_swaps(void) |
| 266 | { |
| 267 | char *line; |
| 268 | int err = 0; |
| 269 | FILE *f = fopen_for_read("/proc/swaps"); |
| 270 | /* Don't complain if missing */ |
| 271 | if (f) { |
| 272 | while ((line = xmalloc_fgetline(f)) != NULL) { |
| 273 | if (line[0] == '/') { |
| 274 | *strchrnul(line, ' ') = '\0'; |
| 275 | err |= swap_enable_disable(line); |
| 276 | } |
| 277 | free(line); |
| 278 | } |
| 279 | if (ENABLE_FEATURE_CLEAN_UP) |
| 280 | fclose(f); |
| 281 | } |
| 282 | |
| 283 | return err; |
| 284 | } |
| 285 | |
René Rhéaume | e769577 | 2015-01-05 20:35:00 +0100 | [diff] [blame] | 286 | #define OPTSTR_SWAPON "ae" \ |
Tito Ragusa | a3f326c | 2014-03-31 16:39:26 +0200 | [diff] [blame] | 287 | IF_FEATURE_SWAPON_DISCARD("d::") \ |
| 288 | IF_FEATURE_SWAPON_PRI("p:") |
| 289 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 290 | int swap_on_off_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 291 | int swap_on_off_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 292 | { |
Tito Ragusa | a3f326c | 2014-03-31 16:39:26 +0200 | [diff] [blame] | 293 | IF_FEATURE_SWAPON_PRI(char *prio;) |
| 294 | IF_FEATURE_SWAPON_DISCARD(char *discard = NULL;) |
| 295 | int ret = 0; |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 296 | |
Denys Vlasenko | 1671424 | 2011-09-21 01:59:15 +0200 | [diff] [blame] | 297 | INIT_G(); |
| 298 | |
René Rhéaume | e769577 | 2015-01-05 20:35:00 +0100 | [diff] [blame] | 299 | getopt32(argv, do_swapoff ? "ae" : OPTSTR_SWAPON |
Tito Ragusa | a3f326c | 2014-03-31 16:39:26 +0200 | [diff] [blame] | 300 | IF_FEATURE_SWAPON_DISCARD(, &discard) |
| 301 | IF_FEATURE_SWAPON_PRI(, &prio) |
| 302 | ); |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 303 | |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 304 | argv += optind; |
Tito Ragusa | a3f326c | 2014-03-31 16:39:26 +0200 | [diff] [blame] | 305 | |
| 306 | if (OPT_DISCARD) { |
| 307 | set_discard_flag(discard); |
| 308 | } |
| 309 | if (OPT_PRIO) { |
| 310 | set_priority_flag(prio); |
| 311 | } |
| 312 | |
| 313 | if (OPT_ALL) { |
| 314 | /* swapoff -a does also /proc/swaps */ |
| 315 | if (do_swapoff) |
| 316 | ret = do_all_in_proc_swaps(); |
| 317 | ret |= do_em_all_in_fstab(); |
| 318 | } else if (!*argv) { |
| 319 | /* if not -a we need at least one arg */ |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 320 | bb_show_usage(); |
Tito Ragusa | a3f326c | 2014-03-31 16:39:26 +0200 | [diff] [blame] | 321 | } |
| 322 | /* Unset -a now to allow for more messages in swap_enable_disable */ |
| 323 | option_mask32 = option_mask32 & ~OPT_a; |
| 324 | /* Now process devices on the commandline if any */ |
| 325 | while (*argv) { |
| 326 | ret |= swap_enable_disable(*argv++); |
| 327 | } |
Mike Frysinger | 2d5e4f6 | 2005-09-16 04:41:20 +0000 | [diff] [blame] | 328 | return ret; |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 329 | } |