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