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 |
| 11 | //usage: "[-a]" IF_FEATURE_SWAPON_PRI(" [-p PRI]") " [DEVICE]" |
| 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" |
| 15 | //usage: IF_FEATURE_SWAPON_PRI( |
| 16 | //usage: "\n -p PRI Set swap device priority" |
| 17 | //usage: ) |
| 18 | //usage: |
| 19 | //usage:#define swapoff_trivial_usage |
| 20 | //usage: "[-a] [DEVICE]" |
| 21 | //usage:#define swapoff_full_usage "\n\n" |
| 22 | //usage: "Stop swapping on DEVICE\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 23 | //usage: "\n -a Stop swapping on all swap devices" |
| 24 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 25 | #include "libbb.h" |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 26 | #include <mntent.h> |
Denys Vlasenko | 14bd16a | 2011-07-08 08:49:40 +0200 | [diff] [blame] | 27 | #ifndef __BIONIC__ |
| 28 | # include <sys/swap.h> |
| 29 | #endif |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 30 | |
Natanael Copa | 9aff299 | 2009-09-20 04:28:22 +0200 | [diff] [blame] | 31 | #if ENABLE_FEATURE_MOUNT_LABEL |
| 32 | # include "volume_id.h" |
| 33 | #else |
| 34 | # define resolve_mount_spec(fsname) ((void)0) |
| 35 | #endif |
| 36 | |
Denys Vlasenko | 14bd16a | 2011-07-08 08:49:40 +0200 | [diff] [blame] | 37 | #ifndef MNTTYPE_SWAP |
| 38 | # define MNTTYPE_SWAP "swap" |
| 39 | #endif |
| 40 | |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 41 | #if ENABLE_FEATURE_SWAPON_PRI |
| 42 | struct globals { |
| 43 | int flags; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 44 | } FIX_ALIASING; |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 45 | #define G (*(struct globals*)&bb_common_bufsiz1) |
| 46 | #define g_flags (G.flags) |
| 47 | #else |
| 48 | #define g_flags 0 |
| 49 | #endif |
Denys Vlasenko | 1671424 | 2011-09-21 01:59:15 +0200 | [diff] [blame] | 50 | #define INIT_G() do { } while (0) |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 51 | |
Rob Landley | 20cc6d5 | 2006-09-12 21:42:17 +0000 | [diff] [blame] | 52 | static int swap_enable_disable(char *device) |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 53 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 54 | int status; |
Eric Andersen | 97b141a | 2002-11-03 00:25:23 +0000 | [diff] [blame] | 55 | struct stat st; |
| 56 | |
Natanael Copa | 9aff299 | 2009-09-20 04:28:22 +0200 | [diff] [blame] | 57 | resolve_mount_spec(&device); |
Rob Landley | c5b1d4d | 2006-03-13 15:45:16 +0000 | [diff] [blame] | 58 | xstat(device, &st); |
Eric Andersen | 97b141a | 2002-11-03 00:25:23 +0000 | [diff] [blame] | 59 | |
Denis Vlasenko | 5659407 | 2007-03-14 22:55:39 +0000 | [diff] [blame] | 60 | #if ENABLE_DESKTOP |
Eric Andersen | 97b141a | 2002-11-03 00:25:23 +0000 | [diff] [blame] | 61 | /* test for holes */ |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 62 | if (S_ISREG(st.st_mode)) |
Denis Vlasenko | f8b21d0 | 2007-11-05 19:33:38 +0000 | [diff] [blame] | 63 | if (st.st_blocks * (off_t)512 < st.st_size) |
Denis Vlasenko | 5659407 | 2007-03-14 22:55:39 +0000 | [diff] [blame] | 64 | bb_error_msg("warning: swap file has holes"); |
Denis Vlasenko | b3f09f4 | 2007-03-12 18:16:24 +0000 | [diff] [blame] | 65 | #endif |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 66 | |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 67 | if (applet_name[5] == 'n') |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 68 | status = swapon(device, g_flags); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 69 | else |
| 70 | status = swapoff(device); |
| 71 | |
Eric Andersen | db1df5e | 2002-10-26 10:27:42 +0000 | [diff] [blame] | 72 | if (status != 0) { |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 73 | bb_simple_perror_msg(device); |
Mike Frysinger | 2d5e4f6 | 2005-09-16 04:41:20 +0000 | [diff] [blame] | 74 | return 1; |
Eric Andersen | db1df5e | 2002-10-26 10:27:42 +0000 | [diff] [blame] | 75 | } |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 76 | |
Mike Frysinger | 2d5e4f6 | 2005-09-16 04:41:20 +0000 | [diff] [blame] | 77 | return 0; |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Eric Andersen | db1df5e | 2002-10-26 10:27:42 +0000 | [diff] [blame] | 80 | static int do_em_all(void) |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 81 | { |
| 82 | struct mntent *m; |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 83 | FILE *f; |
| 84 | int err; |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 85 | |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 86 | f = setmntent("/etc/fstab", "r"); |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 87 | if (f == NULL) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 88 | bb_perror_msg_and_die("/etc/fstab"); |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 89 | |
| 90 | err = 0; |
Lauri Kasanen | d2844fc | 2010-04-29 22:20:57 +0200 | [diff] [blame] | 91 | while ((m = getmntent(f)) != NULL) { |
| 92 | if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0) { |
| 93 | /* swapon -a should ignore entries with noauto, |
| 94 | * but swapoff -a should process them */ |
| 95 | if (applet_name[5] != 'n' |
| 96 | || hasmntopt(m, MNTOPT_NOAUTO) == NULL |
| 97 | ) { |
Tito Ragusa | 8c7fcbd | 2013-08-08 10:21:27 +0200 | [diff] [blame] | 98 | #if ENABLE_FEATURE_SWAPON_PRI |
Denys Vlasenko | 087843a | 2013-11-12 11:16:30 +0100 | [diff] [blame] | 99 | char *p; |
Tito Ragusa | 8c7fcbd | 2013-08-08 10:21:27 +0200 | [diff] [blame] | 100 | g_flags = 0; /* each swap space might have different flags */ |
Denys Vlasenko | 087843a | 2013-11-12 11:16:30 +0100 | [diff] [blame] | 101 | p = hasmntopt(m, "pri"); |
Tito Ragusa | 8c7fcbd | 2013-08-08 10:21:27 +0200 | [diff] [blame] | 102 | if (p) { |
| 103 | /* Max allowed 32767 (==SWAP_FLAG_PRIO_MASK) */ |
Bernhard Reutner-Fischer | b541d1f | 2013-09-27 13:39:41 +0200 | [diff] [blame] | 104 | unsigned int swap_prio = MIN(bb_strtou(p + 4 , NULL, 10), SWAP_FLAG_PRIO_MASK); |
Tito Ragusa | 8c7fcbd | 2013-08-08 10:21:27 +0200 | [diff] [blame] | 105 | /* We want to allow "NNNN,foo", thus errno == EINVAL is allowed too */ |
| 106 | if (errno != ERANGE) { |
| 107 | g_flags = SWAP_FLAG_PREFER | |
| 108 | (swap_prio << SWAP_FLAG_PRIO_SHIFT); |
| 109 | } |
| 110 | } |
| 111 | #endif |
Lauri Kasanen | d2844fc | 2010-04-29 22:20:57 +0200 | [diff] [blame] | 112 | err += swap_enable_disable(m->mnt_fsname); |
| 113 | } |
| 114 | } |
| 115 | } |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 116 | |
Lauri Kasanen | d2844fc | 2010-04-29 22:20:57 +0200 | [diff] [blame] | 117 | if (ENABLE_FEATURE_CLEAN_UP) |
| 118 | endmntent(f); |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 119 | |
Eric Andersen | db1df5e | 2002-10-26 10:27:42 +0000 | [diff] [blame] | 120 | return err; |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 123 | int swap_on_off_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 124 | int swap_on_off_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 125 | { |
Mike Frysinger | 2d5e4f6 | 2005-09-16 04:41:20 +0000 | [diff] [blame] | 126 | int ret; |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 127 | |
Denys Vlasenko | 1671424 | 2011-09-21 01:59:15 +0200 | [diff] [blame] | 128 | INIT_G(); |
| 129 | |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 130 | #if !ENABLE_FEATURE_SWAPON_PRI |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 131 | ret = getopt32(argv, "a"); |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 132 | #else |
Denys Vlasenko | c21c310 | 2011-08-13 23:36:36 +0200 | [diff] [blame] | 133 | if (applet_name[5] == 'n') |
| 134 | opt_complementary = "p+"; |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 135 | ret = getopt32(argv, (applet_name[5] == 'n') ? "ap:" : "a", &g_flags); |
| 136 | |
| 137 | if (ret & 2) { // -p |
| 138 | g_flags = SWAP_FLAG_PREFER | |
| 139 | ((g_flags & SWAP_FLAG_PRIO_MASK) << SWAP_FLAG_PRIO_SHIFT); |
| 140 | ret &= 1; |
| 141 | } |
| 142 | #endif |
| 143 | |
| 144 | if (ret /* & 1: not needed */) // -a |
Rob Landley | bc3d4a1 | 2005-09-13 01:30:19 +0000 | [diff] [blame] | 145 | return do_em_all(); |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 146 | |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 147 | argv += optind; |
| 148 | if (!*argv) |
| 149 | bb_show_usage(); |
| 150 | |
Denis Vlasenko | b3f09f4 | 2007-03-12 18:16:24 +0000 | [diff] [blame] | 151 | /* ret = 0; redundant */ |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 152 | do { |
Mike Frysinger | 2d5e4f6 | 2005-09-16 04:41:20 +0000 | [diff] [blame] | 153 | ret += swap_enable_disable(*argv); |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 154 | } while (*++argv); |
| 155 | |
Mike Frysinger | 2d5e4f6 | 2005-09-16 04:41:20 +0000 | [diff] [blame] | 156 | return ret; |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 157 | } |