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> |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 27 | #include <sys/swap.h> |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 28 | |
Natanael Copa | 9aff299 | 2009-09-20 04:28:22 +0200 | [diff] [blame] | 29 | #if ENABLE_FEATURE_MOUNT_LABEL |
| 30 | # include "volume_id.h" |
| 31 | #else |
| 32 | # define resolve_mount_spec(fsname) ((void)0) |
| 33 | #endif |
| 34 | |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 35 | #if ENABLE_FEATURE_SWAPON_PRI |
| 36 | struct globals { |
| 37 | int flags; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 38 | } FIX_ALIASING; |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 39 | #define G (*(struct globals*)&bb_common_bufsiz1) |
| 40 | #define g_flags (G.flags) |
| 41 | #else |
| 42 | #define g_flags 0 |
| 43 | #endif |
| 44 | |
Rob Landley | 20cc6d5 | 2006-09-12 21:42:17 +0000 | [diff] [blame] | 45 | static int swap_enable_disable(char *device) |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 46 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 47 | int status; |
Eric Andersen | 97b141a | 2002-11-03 00:25:23 +0000 | [diff] [blame] | 48 | struct stat st; |
| 49 | |
Natanael Copa | 9aff299 | 2009-09-20 04:28:22 +0200 | [diff] [blame] | 50 | resolve_mount_spec(&device); |
Rob Landley | c5b1d4d | 2006-03-13 15:45:16 +0000 | [diff] [blame] | 51 | xstat(device, &st); |
Eric Andersen | 97b141a | 2002-11-03 00:25:23 +0000 | [diff] [blame] | 52 | |
Denis Vlasenko | 5659407 | 2007-03-14 22:55:39 +0000 | [diff] [blame] | 53 | #if ENABLE_DESKTOP |
Eric Andersen | 97b141a | 2002-11-03 00:25:23 +0000 | [diff] [blame] | 54 | /* test for holes */ |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 55 | if (S_ISREG(st.st_mode)) |
Denis Vlasenko | f8b21d0 | 2007-11-05 19:33:38 +0000 | [diff] [blame] | 56 | if (st.st_blocks * (off_t)512 < st.st_size) |
Denis Vlasenko | 5659407 | 2007-03-14 22:55:39 +0000 | [diff] [blame] | 57 | bb_error_msg("warning: swap file has holes"); |
Denis Vlasenko | b3f09f4 | 2007-03-12 18:16:24 +0000 | [diff] [blame] | 58 | #endif |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 59 | |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 60 | if (applet_name[5] == 'n') |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 61 | status = swapon(device, g_flags); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 62 | else |
| 63 | status = swapoff(device); |
| 64 | |
Eric Andersen | db1df5e | 2002-10-26 10:27:42 +0000 | [diff] [blame] | 65 | if (status != 0) { |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 66 | bb_simple_perror_msg(device); |
Mike Frysinger | 2d5e4f6 | 2005-09-16 04:41:20 +0000 | [diff] [blame] | 67 | return 1; |
Eric Andersen | db1df5e | 2002-10-26 10:27:42 +0000 | [diff] [blame] | 68 | } |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 69 | |
Mike Frysinger | 2d5e4f6 | 2005-09-16 04:41:20 +0000 | [diff] [blame] | 70 | return 0; |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Eric Andersen | db1df5e | 2002-10-26 10:27:42 +0000 | [diff] [blame] | 73 | static int do_em_all(void) |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 74 | { |
| 75 | struct mntent *m; |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 76 | FILE *f; |
| 77 | int err; |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 78 | |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 79 | f = setmntent("/etc/fstab", "r"); |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 80 | if (f == NULL) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 81 | bb_perror_msg_and_die("/etc/fstab"); |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 82 | |
| 83 | err = 0; |
Lauri Kasanen | d2844fc | 2010-04-29 22:20:57 +0200 | [diff] [blame] | 84 | while ((m = getmntent(f)) != NULL) { |
| 85 | if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0) { |
| 86 | /* swapon -a should ignore entries with noauto, |
| 87 | * but swapoff -a should process them */ |
| 88 | if (applet_name[5] != 'n' |
| 89 | || hasmntopt(m, MNTOPT_NOAUTO) == NULL |
| 90 | ) { |
| 91 | err += swap_enable_disable(m->mnt_fsname); |
| 92 | } |
| 93 | } |
| 94 | } |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 95 | |
Lauri Kasanen | d2844fc | 2010-04-29 22:20:57 +0200 | [diff] [blame] | 96 | if (ENABLE_FEATURE_CLEAN_UP) |
| 97 | endmntent(f); |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 98 | |
Eric Andersen | db1df5e | 2002-10-26 10:27:42 +0000 | [diff] [blame] | 99 | return err; |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 102 | int swap_on_off_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 103 | int swap_on_off_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 104 | { |
Mike Frysinger | 2d5e4f6 | 2005-09-16 04:41:20 +0000 | [diff] [blame] | 105 | int ret; |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 106 | |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 107 | #if !ENABLE_FEATURE_SWAPON_PRI |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 108 | ret = getopt32(argv, "a"); |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 109 | #else |
| 110 | opt_complementary = "p+"; |
| 111 | ret = getopt32(argv, (applet_name[5] == 'n') ? "ap:" : "a", &g_flags); |
| 112 | |
| 113 | if (ret & 2) { // -p |
| 114 | g_flags = SWAP_FLAG_PREFER | |
| 115 | ((g_flags & SWAP_FLAG_PRIO_MASK) << SWAP_FLAG_PRIO_SHIFT); |
| 116 | ret &= 1; |
| 117 | } |
| 118 | #endif |
| 119 | |
| 120 | if (ret /* & 1: not needed */) // -a |
Rob Landley | bc3d4a1 | 2005-09-13 01:30:19 +0000 | [diff] [blame] | 121 | return do_em_all(); |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 122 | |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 123 | argv += optind; |
| 124 | if (!*argv) |
| 125 | bb_show_usage(); |
| 126 | |
Denis Vlasenko | b3f09f4 | 2007-03-12 18:16:24 +0000 | [diff] [blame] | 127 | /* ret = 0; redundant */ |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 128 | do { |
Mike Frysinger | 2d5e4f6 | 2005-09-16 04:41:20 +0000 | [diff] [blame] | 129 | ret += swap_enable_disable(*argv); |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 130 | } while (*++argv); |
| 131 | |
Mike Frysinger | 2d5e4f6 | 2005-09-16 04:41:20 +0000 | [diff] [blame] | 132 | return ret; |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 133 | } |