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 | |
Rob Landley | 22d26fc | 2006-06-15 15:49:36 +0000 | [diff] [blame] | 10 | #include "busybox.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 | |
Rob Landley | 20cc6d5 | 2006-09-12 21:42:17 +0000 | [diff] [blame] | 14 | static int swap_enable_disable(char *device) |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 15 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 16 | int status; |
Eric Andersen | 97b141a | 2002-11-03 00:25:23 +0000 | [diff] [blame] | 17 | struct stat st; |
| 18 | |
Rob Landley | c5b1d4d | 2006-03-13 15:45:16 +0000 | [diff] [blame] | 19 | xstat(device, &st); |
Eric Andersen | 97b141a | 2002-11-03 00:25:23 +0000 | [diff] [blame] | 20 | |
Denis Vlasenko | 5659407 | 2007-03-14 22:55:39 +0000 | [diff] [blame] | 21 | #if ENABLE_DESKTOP |
Eric Andersen | 97b141a | 2002-11-03 00:25:23 +0000 | [diff] [blame] | 22 | /* test for holes */ |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 23 | if (S_ISREG(st.st_mode)) |
| 24 | if (st.st_blocks * 512 < st.st_size) |
Denis Vlasenko | 5659407 | 2007-03-14 22:55:39 +0000 | [diff] [blame] | 25 | bb_error_msg("warning: swap file has holes"); |
Denis Vlasenko | b3f09f4 | 2007-03-12 18:16:24 +0000 | [diff] [blame] | 26 | #endif |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 27 | |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 28 | if (applet_name[5] == 'n') |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 29 | status = swapon(device, 0); |
| 30 | else |
| 31 | status = swapoff(device); |
| 32 | |
Eric Andersen | db1df5e | 2002-10-26 10:27:42 +0000 | [diff] [blame] | 33 | if (status != 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 34 | bb_perror_msg("%s", device); |
Mike Frysinger | 2d5e4f6 | 2005-09-16 04:41:20 +0000 | [diff] [blame] | 35 | return 1; |
Eric Andersen | db1df5e | 2002-10-26 10:27:42 +0000 | [diff] [blame] | 36 | } |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 37 | |
Mike Frysinger | 2d5e4f6 | 2005-09-16 04:41:20 +0000 | [diff] [blame] | 38 | return 0; |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Eric Andersen | db1df5e | 2002-10-26 10:27:42 +0000 | [diff] [blame] | 41 | static int do_em_all(void) |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 42 | { |
| 43 | struct mntent *m; |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 44 | FILE *f; |
| 45 | int err; |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 46 | |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 47 | f = setmntent("/etc/fstab", "r"); |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 48 | if (f == NULL) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 49 | bb_perror_msg_and_die("/etc/fstab"); |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 50 | |
| 51 | err = 0; |
| 52 | while ((m = getmntent(f)) != NULL) |
| 53 | if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0) |
Mike Frysinger | 2d5e4f6 | 2005-09-16 04:41:20 +0000 | [diff] [blame] | 54 | err += swap_enable_disable(m->mnt_fsname); |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 55 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 56 | endmntent(f); |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 57 | |
Eric Andersen | db1df5e | 2002-10-26 10:27:42 +0000 | [diff] [blame] | 58 | return err; |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 61 | int swap_on_off_main(int argc, char **argv); |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 62 | int swap_on_off_main(int argc, char **argv) |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 63 | { |
Mike Frysinger | 2d5e4f6 | 2005-09-16 04:41:20 +0000 | [diff] [blame] | 64 | int ret; |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 65 | |
Mike Frysinger | 2d5e4f6 | 2005-09-16 04:41:20 +0000 | [diff] [blame] | 66 | if (argc == 1) |
Rob Landley | bc3d4a1 | 2005-09-13 01:30:19 +0000 | [diff] [blame] | 67 | bb_show_usage(); |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 68 | |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 69 | ret = getopt32(argc, argv, "a"); |
Denis Vlasenko | b3f09f4 | 2007-03-12 18:16:24 +0000 | [diff] [blame] | 70 | if (ret) |
Rob Landley | bc3d4a1 | 2005-09-13 01:30:19 +0000 | [diff] [blame] | 71 | return do_em_all(); |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 72 | |
Denis Vlasenko | b3f09f4 | 2007-03-12 18:16:24 +0000 | [diff] [blame] | 73 | /* ret = 0; redundant */ |
Mike Frysinger | 2d5e4f6 | 2005-09-16 04:41:20 +0000 | [diff] [blame] | 74 | while (*++argv) |
| 75 | ret += swap_enable_disable(*argv); |
| 76 | return ret; |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 77 | } |