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 | ) { |
| 98 | err += swap_enable_disable(m->mnt_fsname); |
| 99 | } |
| 100 | } |
| 101 | } |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 102 | |
Lauri Kasanen | d2844fc | 2010-04-29 22:20:57 +0200 | [diff] [blame] | 103 | if (ENABLE_FEATURE_CLEAN_UP) |
| 104 | endmntent(f); |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 105 | |
Eric Andersen | db1df5e | 2002-10-26 10:27:42 +0000 | [diff] [blame] | 106 | return err; |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 109 | int swap_on_off_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 110 | int swap_on_off_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 111 | { |
Mike Frysinger | 2d5e4f6 | 2005-09-16 04:41:20 +0000 | [diff] [blame] | 112 | int ret; |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 113 | |
Denys Vlasenko | 1671424 | 2011-09-21 01:59:15 +0200 | [diff] [blame] | 114 | INIT_G(); |
| 115 | |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 116 | #if !ENABLE_FEATURE_SWAPON_PRI |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 117 | ret = getopt32(argv, "a"); |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 118 | #else |
Denys Vlasenko | c21c310 | 2011-08-13 23:36:36 +0200 | [diff] [blame] | 119 | if (applet_name[5] == 'n') |
| 120 | opt_complementary = "p+"; |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 121 | ret = getopt32(argv, (applet_name[5] == 'n') ? "ap:" : "a", &g_flags); |
| 122 | |
| 123 | if (ret & 2) { // -p |
| 124 | g_flags = SWAP_FLAG_PREFER | |
| 125 | ((g_flags & SWAP_FLAG_PRIO_MASK) << SWAP_FLAG_PRIO_SHIFT); |
| 126 | ret &= 1; |
| 127 | } |
| 128 | #endif |
| 129 | |
| 130 | if (ret /* & 1: not needed */) // -a |
Rob Landley | bc3d4a1 | 2005-09-13 01:30:19 +0000 | [diff] [blame] | 131 | return do_em_all(); |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 132 | |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 133 | argv += optind; |
| 134 | if (!*argv) |
| 135 | bb_show_usage(); |
| 136 | |
Denis Vlasenko | b3f09f4 | 2007-03-12 18:16:24 +0000 | [diff] [blame] | 137 | /* ret = 0; redundant */ |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 138 | do { |
Mike Frysinger | 2d5e4f6 | 2005-09-16 04:41:20 +0000 | [diff] [blame] | 139 | ret += swap_enable_disable(*argv); |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 140 | } while (*++argv); |
| 141 | |
Mike Frysinger | 2d5e4f6 | 2005-09-16 04:41:20 +0000 | [diff] [blame] | 142 | return ret; |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 143 | } |