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