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