blob: 3c02bdd42d1baf1efefc1fc926e95cd177213acf [file] [log] [blame]
Eric Andersen87590061999-10-18 21:22:59 +00001/*
2 * Mini swapon/swapoff implementation for busybox
3 *
Eric Andersenc4996011999-10-20 22:08:37 +00004 *
5 * Copyright (C) 1999 by Lineo, inc.
6 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
Eric Andersen87590061999-10-18 21:22:59 +00007 *
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
34static int whichApp;
35static const char* appName;
36
37static const char swapoff_usage[] =
Eric Andersenb0e9a701999-10-18 22:28:26 +000038"swapoff device\n"
Eric Andersen87590061999-10-18 21:22:59 +000039"\nStop swapping virtual memory pages on the given device.\n";
40static const char swapon_usage[] =
Eric Andersenb0e9a701999-10-18 22:28:26 +000041"swapon device\n"
Eric Andersen87590061999-10-18 21:22:59 +000042"\nStart swapping virtual memory pages on the given device.\n";
43
44
45#define SWAPON_APP 1
46#define SWAPOFF_APP 2
47
48
49static void
50swap_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
64static void
65do_em_all()
66{
67 struct mntent *m;
Eric Andersen87590061999-10-18 21:22:59 +000068 FILE *f = setmntent ("/etc/fstab", "r");
69
70 if (f == NULL) {
71 perror("/etc/fstab");
72 exit( FALSE);
73 }
74 while ((m = getmntent (f)) != NULL) {
Erik Andersenfac10d72000-02-07 05:29:42 +000075 if (!strstr (m->mnt_type, MNTTYPE_SWAP)) {
76 swap_enable_disable( m->mnt_fsname);
Eric Andersen87590061999-10-18 21:22:59 +000077 }
78 }
79 endmntent (f);
80 exit( TRUE);
81}
82
83
84extern int
85swap_on_off_main(int argc, char * * argv)
86{
87 struct stat statBuf;
88 if (stat("/etc/fstab", &statBuf) < 0)
89 fprintf(stderr, "/etc/fstab file missing -- Please install one.\n\n");
90
91 if (strcmp(*argv, "swapon")==0) {
92 appName = *argv;
93 whichApp = SWAPON_APP;
94
95 } else {
96 appName = *argv;
97 whichApp = SWAPOFF_APP;
98 }
99
100 if (argc < 2)
101 goto usage_and_exit;
102 argc--;
103 argv++;
104
105 /* Parse any options */
106 while (**argv == '-') {
107 while (*++(*argv)) switch (**argv) {
108 case 'a':
109 do_em_all();
110 break;
111 default:
112 goto usage_and_exit;
113 }
114 }
115 swap_enable_disable(*argv);
Eric Andersenb0e9a701999-10-18 22:28:26 +0000116 exit( TRUE);
Eric Andersen87590061999-10-18 21:22:59 +0000117
118usage_and_exit:
Eric Andersenb0e9a701999-10-18 22:28:26 +0000119 usage( (whichApp==SWAPON_APP)? swapon_usage : swapoff_usage);
120 exit( FALSE);
Eric Andersen87590061999-10-18 21:22:59 +0000121}
122