blob: 8eaf9797cbedde35de72f3e179b5c1c6084608fc [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;
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
85extern int
86swap_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 Andersenb0e9a701999-10-18 22:28:26 +0000117 exit( TRUE);
Eric Andersen87590061999-10-18 21:22:59 +0000118
119usage_and_exit:
Eric Andersenb0e9a701999-10-18 22:28:26 +0000120 usage( (whichApp==SWAPON_APP)? swapon_usage : swapoff_usage);
121 exit( FALSE);
Eric Andersen87590061999-10-18 21:22:59 +0000122}
123