6bda22277f95be1f3da7e6636344b4b69088cc5b
[oweals/busybox.git] / swaponoff.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini swapon/swapoff implementation for busybox
4  *
5  *
6  * Copyright (C) 1999 by Lineo, inc.
7  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  *
23  */
24
25 #include "internal.h"
26 #include <stdio.h>
27 #include <sys/mount.h>
28 #include <sys/swap.h>
29 #include <mntent.h>
30 #include <dirent.h>
31 #include <fstab.h>
32 #include <errno.h>
33
34
35 static int whichApp;
36 static const char *appName;
37
38 static const char swapoff_usage[] =
39
40         "swapoff device\n"
41         "\nStop swapping virtual memory pages on the given device.\n";
42 static const char swapon_usage[] =
43
44         "swapon device\n"
45         "\nStart swapping virtual memory pages on the given device.\n";
46
47
48 #define SWAPON_APP   1
49 #define SWAPOFF_APP  2
50
51
52 static void swap_enable_disable(char *device)
53 {
54         int status;
55
56         if (whichApp == SWAPON_APP)
57                 status = swapon(device, 0);
58         else
59                 status = swapoff(device);
60
61         if (status != 0) {
62                 perror(appName);
63                 exit(FALSE);
64         }
65 }
66
67 static void do_em_all()
68 {
69         struct mntent *m;
70         FILE *f = setmntent("/etc/fstab", "r");
71
72         if (f == NULL) {
73                 perror("/etc/fstab");
74                 exit(FALSE);
75         }
76         while ((m = getmntent(f)) != NULL) {
77                 if (!strstr(m->mnt_type, MNTTYPE_SWAP)) {
78                         swap_enable_disable(m->mnt_fsname);
79                 }
80         }
81         endmntent(f);
82         exit(TRUE);
83 }
84
85
86 extern int swap_on_off_main(int argc, char **argv)
87 {
88         struct stat statBuf;
89
90         if (stat("/etc/fstab", &statBuf) < 0)
91                 fprintf(stderr,
92                                 "/etc/fstab file missing -- Please install one.\n\n");
93
94         if (strcmp(*argv, "swapon") == 0) {
95                 appName = *argv;
96                 whichApp = SWAPON_APP;
97
98         } else {
99                 appName = *argv;
100                 whichApp = SWAPOFF_APP;
101         }
102
103         if (argc < 2)
104                 goto usage_and_exit;
105         argc--;
106         argv++;
107
108         /* Parse any options */
109         while (**argv == '-') {
110                 while (*++(*argv))
111                         switch (**argv) {
112                         case 'a':
113                                 do_em_all();
114                                 break;
115                         default:
116                                 goto usage_and_exit;
117                         }
118         }
119         swap_enable_disable(*argv);
120         exit(TRUE);
121
122   usage_and_exit:
123         usage((whichApp == SWAPON_APP) ? swapon_usage : swapoff_usage);
124         exit(FALSE);
125 }