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 | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org> |
| 6 | * Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | */ |
| 23 | |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 24 | #include <stdio.h> |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 25 | #include <mntent.h> |
| 26 | #include <dirent.h> |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 27 | #include <errno.h> |
Eric Andersen | eba8ed7 | 2001-03-09 14:36:42 +0000 | [diff] [blame] | 28 | #include <string.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 29 | #include <stdlib.h> |
| 30 | #include <sys/mount.h> |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 31 | #include <sys/swap.h> |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 32 | |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 33 | #include "busybox.h" |
Eric Andersen | 5e29c6f | 2000-06-19 18:25:49 +0000 | [diff] [blame] | 34 | |
Eric Andersen | db1df5e | 2002-10-26 10:27:42 +0000 | [diff] [blame] | 35 | static int whichApp; /* default SWAPON_APP */ |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 36 | |
Eric Andersen | db1df5e | 2002-10-26 10:27:42 +0000 | [diff] [blame] | 37 | static const int SWAPON_APP = 0; |
| 38 | static const int SWAPOFF_APP = 1; |
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 swap_enable_disable(const char *device) |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 42 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 43 | int status; |
Eric Andersen | 97b141a | 2002-11-03 00:25:23 +0000 | [diff] [blame] | 44 | struct stat st; |
| 45 | |
| 46 | if (stat(device, &st) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 47 | bb_perror_msg_and_die("cannot stat %s", device); |
Eric Andersen | 97b141a | 2002-11-03 00:25:23 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /* test for holes */ |
| 51 | if (S_ISREG(st.st_mode)) { |
| 52 | if (st.st_blocks * 512 < st.st_size) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 53 | bb_error_msg_and_die("swap file has holes"); |
Eric Andersen | 97b141a | 2002-11-03 00:25:23 +0000 | [diff] [blame] | 54 | } |
| 55 | } |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 56 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 57 | if (whichApp == SWAPON_APP) |
| 58 | status = swapon(device, 0); |
| 59 | else |
| 60 | status = swapoff(device); |
| 61 | |
Eric Andersen | db1df5e | 2002-10-26 10:27:42 +0000 | [diff] [blame] | 62 | if (status != 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 63 | bb_perror_msg("%s", device); |
Eric Andersen | db1df5e | 2002-10-26 10:27:42 +0000 | [diff] [blame] | 64 | return EXIT_FAILURE; |
| 65 | } |
| 66 | return EXIT_SUCCESS; |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Eric Andersen | db1df5e | 2002-10-26 10:27:42 +0000 | [diff] [blame] | 69 | static int do_em_all(void) |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 70 | { |
| 71 | struct mntent *m; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 72 | FILE *f = setmntent("/etc/fstab", "r"); |
Eric Andersen | db1df5e | 2002-10-26 10:27:42 +0000 | [diff] [blame] | 73 | int err = 0; |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 74 | |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 75 | if (f == NULL) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 76 | bb_perror_msg_and_die("/etc/fstab"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 77 | while ((m = getmntent(f)) != NULL) { |
Eric Andersen | a42982e | 2000-06-07 17:28:53 +0000 | [diff] [blame] | 78 | if (strcmp(m->mnt_type, MNTTYPE_SWAP)==0) { |
Eric Andersen | db1df5e | 2002-10-26 10:27:42 +0000 | [diff] [blame] | 79 | if(swap_enable_disable(m->mnt_fsname) == EXIT_FAILURE) |
| 80 | err++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 81 | } |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 82 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 83 | endmntent(f); |
Eric Andersen | db1df5e | 2002-10-26 10:27:42 +0000 | [diff] [blame] | 84 | return err; |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 88 | extern int swap_on_off_main(int argc, char **argv) |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 89 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 90 | if (bb_applet_name[5] == 'f') { /* "swapoff" */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 91 | whichApp = SWAPOFF_APP; |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 92 | } |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 93 | |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 94 | if (argc != 2) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 95 | goto usage_and_exit; |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 96 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 97 | argc--; |
| 98 | argv++; |
| 99 | |
| 100 | /* Parse any options */ |
| 101 | while (**argv == '-') { |
| 102 | while (*++(*argv)) |
| 103 | switch (**argv) { |
| 104 | case 'a': |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 105 | { |
| 106 | struct stat statBuf; |
| 107 | |
| 108 | if (stat("/etc/fstab", &statBuf) < 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 109 | bb_error_msg_and_die("/etc/fstab file missing"); |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 110 | } |
Eric Andersen | db1df5e | 2002-10-26 10:27:42 +0000 | [diff] [blame] | 111 | return do_em_all(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 112 | break; |
| 113 | default: |
| 114 | goto usage_and_exit; |
| 115 | } |
| 116 | } |
Eric Andersen | db1df5e | 2002-10-26 10:27:42 +0000 | [diff] [blame] | 117 | return swap_enable_disable(*argv); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 118 | |
| 119 | usage_and_exit: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 120 | bb_show_usage(); |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 121 | } |