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 | * |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 7 | * Licensed under the GPL v2, see the file LICENSE in this tarball. |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 10 | #include <stdio.h> |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 11 | #include <mntent.h> |
| 12 | #include <dirent.h> |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 13 | #include <errno.h> |
Eric Andersen | eba8ed7 | 2001-03-09 14:36:42 +0000 | [diff] [blame] | 14 | #include <string.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 15 | #include <stdlib.h> |
| 16 | #include <sys/mount.h> |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 17 | #include <sys/swap.h> |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 18 | |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 19 | #include "busybox.h" |
Eric Andersen | 5e29c6f | 2000-06-19 18:25:49 +0000 | [diff] [blame] | 20 | |
Eric Andersen | db1df5e | 2002-10-26 10:27:42 +0000 | [diff] [blame] | 21 | static int swap_enable_disable(const char *device) |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 22 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 23 | int status; |
Eric Andersen | 97b141a | 2002-11-03 00:25:23 +0000 | [diff] [blame] | 24 | struct stat st; |
| 25 | |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 26 | if (stat(device, &st) < 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 27 | bb_perror_msg_and_die("cannot stat %s", device); |
Eric Andersen | 97b141a | 2002-11-03 00:25:23 +0000 | [diff] [blame] | 28 | |
| 29 | /* test for holes */ |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 30 | if (S_ISREG(st.st_mode)) |
| 31 | if (st.st_blocks * 512 < st.st_size) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 32 | bb_error_msg_and_die("swap file has holes"); |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 33 | |
Rob Landley | bc3d4a1 | 2005-09-13 01:30:19 +0000 | [diff] [blame] | 34 | if (bb_applet_name[5] == 'n') |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 35 | status = swapon(device, 0); |
| 36 | else |
| 37 | status = swapoff(device); |
| 38 | |
Eric Andersen | db1df5e | 2002-10-26 10:27:42 +0000 | [diff] [blame] | 39 | if (status != 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 40 | bb_perror_msg("%s", device); |
Mike Frysinger | 2d5e4f6 | 2005-09-16 04:41:20 +0000 | [diff] [blame] | 41 | return 1; |
Eric Andersen | db1df5e | 2002-10-26 10:27:42 +0000 | [diff] [blame] | 42 | } |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 43 | |
Mike Frysinger | 2d5e4f6 | 2005-09-16 04:41:20 +0000 | [diff] [blame] | 44 | return 0; |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Eric Andersen | db1df5e | 2002-10-26 10:27:42 +0000 | [diff] [blame] | 47 | static int do_em_all(void) |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 48 | { |
| 49 | struct mntent *m; |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 50 | FILE *f; |
| 51 | int err; |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 52 | |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 53 | f = setmntent("/etc/fstab", "r"); |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 54 | if (f == NULL) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 55 | bb_perror_msg_and_die("/etc/fstab"); |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 56 | |
| 57 | err = 0; |
| 58 | while ((m = getmntent(f)) != NULL) |
| 59 | if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0) |
Mike Frysinger | 2d5e4f6 | 2005-09-16 04:41:20 +0000 | [diff] [blame] | 60 | err += swap_enable_disable(m->mnt_fsname); |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 61 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 62 | endmntent(f); |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 63 | |
Eric Andersen | db1df5e | 2002-10-26 10:27:42 +0000 | [diff] [blame] | 64 | return err; |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 67 | #define DO_ALL 0x01 |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 68 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 69 | extern int swap_on_off_main(int argc, char **argv) |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 70 | { |
Mike Frysinger | 2d5e4f6 | 2005-09-16 04:41:20 +0000 | [diff] [blame] | 71 | int ret; |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 72 | |
Mike Frysinger | 2d5e4f6 | 2005-09-16 04:41:20 +0000 | [diff] [blame] | 73 | if (argc == 1) |
Rob Landley | bc3d4a1 | 2005-09-13 01:30:19 +0000 | [diff] [blame] | 74 | bb_show_usage(); |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 75 | |
Mike Frysinger | 2d5e4f6 | 2005-09-16 04:41:20 +0000 | [diff] [blame] | 76 | ret = bb_getopt_ulflags(argc, argv, "a"); |
| 77 | if (ret & DO_ALL) |
Rob Landley | bc3d4a1 | 2005-09-13 01:30:19 +0000 | [diff] [blame] | 78 | return do_em_all(); |
Mike Frysinger | 6943a94 | 2005-09-13 02:29:39 +0000 | [diff] [blame] | 79 | |
Mike Frysinger | 2d5e4f6 | 2005-09-16 04:41:20 +0000 | [diff] [blame] | 80 | ret = 0; |
| 81 | while (*++argv) |
| 82 | ret += swap_enable_disable(*argv); |
| 83 | return ret; |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 84 | } |