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