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 | * |
Rob Landley | e9a7a62 | 2006-09-22 02:52:41 +0000 | [diff] [blame] | 7 | * Licensed under the GPL version 2, see the file LICENSE in this tarball. |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 11 | #include <mntent.h> |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 12 | #include <sys/swap.h> |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 13 | |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 14 | #if ENABLE_FEATURE_SWAPON_PRI |
| 15 | struct globals { |
| 16 | int flags; |
| 17 | }; |
| 18 | #define G (*(struct globals*)&bb_common_bufsiz1) |
| 19 | #define g_flags (G.flags) |
| 20 | #else |
| 21 | #define g_flags 0 |
| 22 | #endif |
| 23 | |
Rob Landley | 20cc6d5 | 2006-09-12 21:42:17 +0000 | [diff] [blame] | 24 | static int swap_enable_disable(char *device) |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 25 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 26 | int status; |
Eric Andersen | 97b141a | 2002-11-03 00:25:23 +0000 | [diff] [blame] | 27 | struct stat st; |
| 28 | |
Rob Landley | c5b1d4d | 2006-03-13 15:45:16 +0000 | [diff] [blame] | 29 | xstat(device, &st); |
Eric Andersen | 97b141a | 2002-11-03 00:25:23 +0000 | [diff] [blame] | 30 | |
Denis Vlasenko | 5659407 | 2007-03-14 22:55:39 +0000 | [diff] [blame] | 31 | #if ENABLE_DESKTOP |
Eric Andersen | 97b141a | 2002-11-03 00:25:23 +0000 | [diff] [blame] | 32 | /* test for holes */ |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 33 | if (S_ISREG(st.st_mode)) |
Denis Vlasenko | f8b21d0 | 2007-11-05 19:33:38 +0000 | [diff] [blame] | 34 | if (st.st_blocks * (off_t)512 < st.st_size) |
Denis Vlasenko | 5659407 | 2007-03-14 22:55:39 +0000 | [diff] [blame] | 35 | bb_error_msg("warning: swap file has holes"); |
Denis Vlasenko | b3f09f4 | 2007-03-12 18:16:24 +0000 | [diff] [blame] | 36 | #endif |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 37 | |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 38 | if (applet_name[5] == 'n') |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 39 | status = swapon(device, g_flags); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 40 | else |
| 41 | status = swapoff(device); |
| 42 | |
Eric Andersen | db1df5e | 2002-10-26 10:27:42 +0000 | [diff] [blame] | 43 | if (status != 0) { |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 44 | bb_simple_perror_msg(device); |
Mike Frysinger | 2d5e4f6 | 2005-09-16 04:41:20 +0000 | [diff] [blame] | 45 | return 1; |
Eric Andersen | db1df5e | 2002-10-26 10:27:42 +0000 | [diff] [blame] | 46 | } |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 47 | |
Mike Frysinger | 2d5e4f6 | 2005-09-16 04:41:20 +0000 | [diff] [blame] | 48 | return 0; |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Eric Andersen | db1df5e | 2002-10-26 10:27:42 +0000 | [diff] [blame] | 51 | static int do_em_all(void) |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 52 | { |
| 53 | struct mntent *m; |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 54 | FILE *f; |
| 55 | int err; |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 56 | |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 57 | f = setmntent("/etc/fstab", "r"); |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 58 | if (f == NULL) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 59 | bb_perror_msg_and_die("/etc/fstab"); |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 60 | |
| 61 | err = 0; |
| 62 | while ((m = getmntent(f)) != NULL) |
| 63 | if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0) |
Mike Frysinger | 2d5e4f6 | 2005-09-16 04:41:20 +0000 | [diff] [blame] | 64 | err += swap_enable_disable(m->mnt_fsname); |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 65 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 66 | endmntent(f); |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 67 | |
Eric Andersen | db1df5e | 2002-10-26 10:27:42 +0000 | [diff] [blame] | 68 | return err; |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 71 | int swap_on_off_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 72 | int swap_on_off_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 73 | { |
Mike Frysinger | 2d5e4f6 | 2005-09-16 04:41:20 +0000 | [diff] [blame] | 74 | int ret; |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 75 | |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 76 | #if !ENABLE_FEATURE_SWAPON_PRI |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 77 | ret = getopt32(argv, "a"); |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 78 | #else |
| 79 | opt_complementary = "p+"; |
| 80 | ret = getopt32(argv, (applet_name[5] == 'n') ? "ap:" : "a", &g_flags); |
| 81 | |
| 82 | if (ret & 2) { // -p |
| 83 | g_flags = SWAP_FLAG_PREFER | |
| 84 | ((g_flags & SWAP_FLAG_PRIO_MASK) << SWAP_FLAG_PRIO_SHIFT); |
| 85 | ret &= 1; |
| 86 | } |
| 87 | #endif |
| 88 | |
| 89 | if (ret /* & 1: not needed */) // -a |
Rob Landley | bc3d4a1 | 2005-09-13 01:30:19 +0000 | [diff] [blame] | 90 | return do_em_all(); |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 91 | |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 92 | argv += optind; |
| 93 | if (!*argv) |
| 94 | bb_show_usage(); |
| 95 | |
Denis Vlasenko | b3f09f4 | 2007-03-12 18:16:24 +0000 | [diff] [blame] | 96 | /* ret = 0; redundant */ |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 97 | do { |
Mike Frysinger | 2d5e4f6 | 2005-09-16 04:41:20 +0000 | [diff] [blame] | 98 | ret += swap_enable_disable(*argv); |
Denis Vlasenko | ee56e01 | 2008-05-18 23:05:34 +0000 | [diff] [blame] | 99 | } while (*++argv); |
| 100 | |
Mike Frysinger | 2d5e4f6 | 2005-09-16 04:41:20 +0000 | [diff] [blame] | 101 | return ret; |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 102 | } |