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