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