Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Mini swapon/swapoff implementation for busybox |
| 3 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 1999 by Lineo, inc. |
| 6 | * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.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 | |
| 24 | #include "internal.h" |
| 25 | #include <stdio.h> |
| 26 | #include <sys/mount.h> |
| 27 | #include <sys/swap.h> |
| 28 | #include <mntent.h> |
| 29 | #include <dirent.h> |
| 30 | #include <fstab.h> |
| 31 | #include <errno.h> |
| 32 | |
| 33 | |
| 34 | static int whichApp; |
| 35 | static const char* appName; |
| 36 | |
| 37 | static const char swapoff_usage[] = |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 38 | "swapoff device\n" |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 39 | "\nStop swapping virtual memory pages on the given device.\n"; |
| 40 | static const char swapon_usage[] = |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 41 | "swapon device\n" |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 42 | "\nStart swapping virtual memory pages on the given device.\n"; |
| 43 | |
| 44 | |
| 45 | #define SWAPON_APP 1 |
| 46 | #define SWAPOFF_APP 2 |
| 47 | |
| 48 | |
| 49 | static void |
| 50 | swap_enable_disable( char *device) |
| 51 | { |
| 52 | int status; |
| 53 | if ( whichApp == SWAPON_APP ) |
| 54 | status = swapon(device, 0); |
| 55 | else |
| 56 | status = swapoff(device); |
| 57 | |
| 58 | if ( status != 0 ) { |
| 59 | perror(appName); |
| 60 | exit( FALSE); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | static void |
| 65 | do_em_all() |
| 66 | { |
| 67 | struct mntent *m; |
| 68 | char swapName[NAME_MAX]; |
| 69 | FILE *f = setmntent ("/etc/fstab", "r"); |
| 70 | |
| 71 | if (f == NULL) { |
| 72 | perror("/etc/fstab"); |
| 73 | exit( FALSE); |
| 74 | } |
| 75 | while ((m = getmntent (f)) != NULL) { |
| 76 | if (!strstr (m->mnt_type, "swap")) { |
| 77 | swap_enable_disable( swapName); |
| 78 | } |
| 79 | } |
| 80 | endmntent (f); |
| 81 | exit( TRUE); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | extern int |
| 86 | swap_on_off_main(int argc, char * * argv) |
| 87 | { |
| 88 | struct stat statBuf; |
| 89 | if (stat("/etc/fstab", &statBuf) < 0) |
| 90 | fprintf(stderr, "/etc/fstab file missing -- Please install one.\n\n"); |
| 91 | |
| 92 | if (strcmp(*argv, "swapon")==0) { |
| 93 | appName = *argv; |
| 94 | whichApp = SWAPON_APP; |
| 95 | |
| 96 | } else { |
| 97 | appName = *argv; |
| 98 | whichApp = SWAPOFF_APP; |
| 99 | } |
| 100 | |
| 101 | if (argc < 2) |
| 102 | goto usage_and_exit; |
| 103 | argc--; |
| 104 | argv++; |
| 105 | |
| 106 | /* Parse any options */ |
| 107 | while (**argv == '-') { |
| 108 | while (*++(*argv)) switch (**argv) { |
| 109 | case 'a': |
| 110 | do_em_all(); |
| 111 | break; |
| 112 | default: |
| 113 | goto usage_and_exit; |
| 114 | } |
| 115 | } |
| 116 | swap_enable_disable(*argv); |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 117 | exit( TRUE); |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 118 | |
| 119 | usage_and_exit: |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 120 | usage( (whichApp==SWAPON_APP)? swapon_usage : swapoff_usage); |
| 121 | exit( FALSE); |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 122 | } |
| 123 | |