summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>1999-10-18 21:22:59 +0000
committerEric Andersen <andersen@codepoet.org>1999-10-18 21:22:59 +0000
commit8759006b55c617811a207cc4e99792996c8b97fb (patch)
treeae74dfbfd1ea2d85e54673de631471ab40fc277a
parent7f04b5e3208882eaeabb8c11d64c9b6e90d3e253 (diff)
downloadbusybox-w32-8759006b55c617811a207cc4e99792996c8b97fb.tar.gz
busybox-w32-8759006b55c617811a207cc4e99792996c8b97fb.tar.bz2
busybox-w32-8759006b55c617811a207cc4e99792996c8b97fb.zip
More fixes
-rw-r--r--applets/busybox.c10
-rw-r--r--busybox.c10
-rw-r--r--busybox.def.h3
-rw-r--r--internal.h1
-rw-r--r--swapoff.c52
-rw-r--r--swapon.c34
-rw-r--r--swaponoff.c121
-rw-r--r--util-linux/swaponoff.c121
8 files changed, 252 insertions, 100 deletions
diff --git a/applets/busybox.c b/applets/busybox.c
index 45365b370..6bc223a1d 100644
--- a/applets/busybox.c
+++ b/applets/busybox.c
@@ -132,11 +132,9 @@ static const struct Applet applets[] = {
132#ifdef BB_TAR //bin 132#ifdef BB_TAR //bin
133 {"tar", tar_main}, 133 {"tar", tar_main},
134#endif 134#endif
135#ifdef BB_SWAPOFF //sbin 135#ifdef BB_SWAPONOFF //sbin
136 {"swapoff", monadic_main}, 136 {"swapon", swap_on_off_main},
137#endif 137 {"swapoff", swap_on_off_main},
138#ifdef BB_SWAPON //sbin
139 {"swapon", monadic_main},
140#endif 138#endif
141#ifdef BB_SYNC //bin 139#ifdef BB_SYNC //bin
142 {"sync", sync_main}, 140 {"sync", sync_main},
@@ -148,7 +146,7 @@ static const struct Applet applets[] = {
148 {"true", true_main}, 146 {"true", true_main},
149#endif 147#endif
150#ifdef BB_UMOUNT //bin 148#ifdef BB_UMOUNT //bin
151 {"umount", umount_main}, 149 {"umount", umount_main},
152#endif 150#endif
153#ifdef BB_UPDATE //sbin 151#ifdef BB_UPDATE //sbin
154 {"update", update_main}, 152 {"update", update_main},
diff --git a/busybox.c b/busybox.c
index 45365b370..6bc223a1d 100644
--- a/busybox.c
+++ b/busybox.c
@@ -132,11 +132,9 @@ static const struct Applet applets[] = {
132#ifdef BB_TAR //bin 132#ifdef BB_TAR //bin
133 {"tar", tar_main}, 133 {"tar", tar_main},
134#endif 134#endif
135#ifdef BB_SWAPOFF //sbin 135#ifdef BB_SWAPONOFF //sbin
136 {"swapoff", monadic_main}, 136 {"swapon", swap_on_off_main},
137#endif 137 {"swapoff", swap_on_off_main},
138#ifdef BB_SWAPON //sbin
139 {"swapon", monadic_main},
140#endif 138#endif
141#ifdef BB_SYNC //bin 139#ifdef BB_SYNC //bin
142 {"sync", sync_main}, 140 {"sync", sync_main},
@@ -148,7 +146,7 @@ static const struct Applet applets[] = {
148 {"true", true_main}, 146 {"true", true_main},
149#endif 147#endif
150#ifdef BB_UMOUNT //bin 148#ifdef BB_UMOUNT //bin
151 {"umount", umount_main}, 149 {"umount", umount_main},
152#endif 150#endif
153#ifdef BB_UPDATE //sbin 151#ifdef BB_UPDATE //sbin
154 {"update", update_main}, 152 {"update", update_main},
diff --git a/busybox.def.h b/busybox.def.h
index 10a55f409..8fcbf8350 100644
--- a/busybox.def.h
+++ b/busybox.def.h
@@ -42,8 +42,7 @@
42#define BB_RM 42#define BB_RM
43#define BB_RMDIR 43#define BB_RMDIR
44#define BB_SLEEP 44#define BB_SLEEP
45////#define BB_SWAPOFF 45#define BB_SWAPONOFF
46//#define BB_SWAPON
47#define BB_SYNC 46#define BB_SYNC
48#define BB_TAR 47#define BB_TAR
49#define BB_TOUCH 48#define BB_TOUCH
diff --git a/internal.h b/internal.h
index a87653927..b676e5f86 100644
--- a/internal.h
+++ b/internal.h
@@ -96,6 +96,7 @@ extern int rm_main(int argc, char** argv);
96extern int scan_partitions_main(int argc, char** argv); 96extern int scan_partitions_main(int argc, char** argv);
97extern int sh_main(int argc, char** argv); 97extern int sh_main(int argc, char** argv);
98extern int sleep_main(int argc, char** argv); 98extern int sleep_main(int argc, char** argv);
99extern int swap_on_off_main(int argc, char** argv);
99extern int tar_main(int argc, char** argv); 100extern int tar_main(int argc, char** argv);
100extern int touch_main(int argc, char** argv); 101extern int touch_main(int argc, char** argv);
101extern int sync_main(int argc, char** argv); 102extern int sync_main(int argc, char** argv);
diff --git a/swapoff.c b/swapoff.c
deleted file mode 100644
index 29b9daedd..000000000
--- a/swapoff.c
+++ /dev/null
@@ -1,52 +0,0 @@
1#include <sys/swap.h>
2#include <string.h>
3#include <errno.h>
4#include <mntent.h>
5#include "internal.h"
6
7const char swapoff_usage[] = "swapoff block-device\n"
8"\n"
9"\tStop swapping virtual memory pages on the given device.\n";
10
11extern int
12swapoff_fn(const struct FileInfo * i)
13{
14 struct mntent entries[100];
15 int count = 0;
16 FILE * swapsTable = setmntent("/proc/swaps", "r");
17 struct mntent * m;
18
19 if (!(swapoff(i->source))) {
20 if ( swapsTable == 0 ) {
21 fprintf(stderr, "/etc/swaps: %s\n", strerror(errno));
22 return 1;
23 }
24 while ( (m = getmntent(swapsTable)) != 0 ) {
25 entries[count].mnt_fsname = strdup(m->mnt_fsname);
26 entries[count].mnt_dir = strdup(m->mnt_dir);
27 entries[count].mnt_type = strdup(m->mnt_type);
28 entries[count].mnt_opts = strdup(m->mnt_opts);
29 entries[count].mnt_freq = m->mnt_freq;
30 entries[count].mnt_passno = m->mnt_passno;
31 count++;
32 }
33 endmntent(swapsTable);
34 if ( (swapsTable = setmntent("/etc/swaps", "w")) ) {
35 int id;
36 for ( id = 0; id < count; id++ ) {
37 int result =
38 (strcmp(entries[id].mnt_fsname, i->source)==0
39 ||strcmp(entries[id].mnt_dir, i->source)==0);
40 if ( result )
41 continue;
42 else
43 addmntent(swapsTable, &entries[id]);
44 }
45 endmntent(swapsTable);
46 }
47 else if ( errno != EROFS )
48 fprintf(stderr, "/etc/swaps: %s\n", strerror(errno));
49 return (0);
50 }
51 return (-1);
52}
diff --git a/swapon.c b/swapon.c
deleted file mode 100644
index 78360a55f..000000000
--- a/swapon.c
+++ /dev/null
@@ -1,34 +0,0 @@
1#include <stdio.h>
2#include <mntent.h>
3#include <sys/swap.h>
4#include "internal.h"
5
6const char swapon_usage[] = "swapon block-device\n"
7"\n"
8"\tSwap virtual memory pages on the given device.\n";
9
10extern int
11swapon_fn(const struct FileInfo * i)
12{
13 FILE *swapsTable;
14 struct mntent m;
15
16 if (!(swapon(i->source, 0))) {
17 if ((swapsTable = setmntent("/etc/swaps", "a+"))) {
18 /* Needs the cast to avoid warning about conversion from
19 * const char* to just char*
20 */
21 m.mnt_fsname = (char*)i->source;
22 m.mnt_dir = "none";
23 m.mnt_type = "swap";
24 m.mnt_opts = "sw";
25 m.mnt_freq = 0;
26 m.mnt_passno = 0;
27 addmntent(swapsTable, &m);
28 endmntent(swapsTable);
29 }
30 return (0);
31 }
32 return (-1);
33}
34
diff --git a/swaponoff.c b/swaponoff.c
new file mode 100644
index 000000000..56f93b393
--- /dev/null
+++ b/swaponoff.c
@@ -0,0 +1,121 @@
1/*
2 * Mini swapon/swapoff implementation for busybox
3 *
4 * Copyright (C) 1998 by Erik Andersen <andersee@debian.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include "internal.h"
23#include <stdio.h>
24#include <sys/mount.h>
25#include <sys/swap.h>
26#include <mntent.h>
27#include <dirent.h>
28#include <fstab.h>
29#include <errno.h>
30
31
32static int whichApp;
33static const char* appName;
34
35static const char swapoff_usage[] =
36"Usage: swapoff device\n"
37"\nStop swapping virtual memory pages on the given device.\n";
38static const char swapon_usage[] =
39"Usage: swapon device\n"
40"\nStart swapping virtual memory pages on the given device.\n";
41
42
43#define SWAPON_APP 1
44#define SWAPOFF_APP 2
45
46
47static void
48swap_enable_disable( char *device)
49{
50 int status;
51 if ( whichApp == SWAPON_APP )
52 status = swapon(device, 0);
53 else
54 status = swapoff(device);
55
56 if ( status != 0 ) {
57 perror(appName);
58 exit( FALSE);
59 }
60}
61
62static void
63do_em_all()
64{
65 struct mntent *m;
66 char swapName[NAME_MAX];
67 FILE *f = setmntent ("/etc/fstab", "r");
68
69 if (f == NULL) {
70 perror("/etc/fstab");
71 exit( FALSE);
72 }
73 while ((m = getmntent (f)) != NULL) {
74 if (!strstr (m->mnt_type, "swap")) {
75 swap_enable_disable( swapName);
76 }
77 }
78 endmntent (f);
79 exit( TRUE);
80}
81
82
83extern int
84swap_on_off_main(int argc, char * * argv)
85{
86 struct stat statBuf;
87 if (stat("/etc/fstab", &statBuf) < 0)
88 fprintf(stderr, "/etc/fstab file missing -- Please install one.\n\n");
89
90 if (strcmp(*argv, "swapon")==0) {
91 appName = *argv;
92 whichApp = SWAPON_APP;
93
94 } else {
95 appName = *argv;
96 whichApp = SWAPOFF_APP;
97 }
98
99 if (argc < 2)
100 goto usage_and_exit;
101 argc--;
102 argv++;
103
104 /* Parse any options */
105 while (**argv == '-') {
106 while (*++(*argv)) switch (**argv) {
107 case 'a':
108 do_em_all();
109 break;
110 default:
111 goto usage_and_exit;
112 }
113 }
114 swap_enable_disable(*argv);
115 //exit( TRUE);
116
117usage_and_exit:
118 fprintf(stderr, "Usage: %s", (whichApp==SWAPON_APP)? swapon_usage : swapoff_usage);
119 exit(FALSE);
120}
121
diff --git a/util-linux/swaponoff.c b/util-linux/swaponoff.c
new file mode 100644
index 000000000..56f93b393
--- /dev/null
+++ b/util-linux/swaponoff.c
@@ -0,0 +1,121 @@
1/*
2 * Mini swapon/swapoff implementation for busybox
3 *
4 * Copyright (C) 1998 by Erik Andersen <andersee@debian.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include "internal.h"
23#include <stdio.h>
24#include <sys/mount.h>
25#include <sys/swap.h>
26#include <mntent.h>
27#include <dirent.h>
28#include <fstab.h>
29#include <errno.h>
30
31
32static int whichApp;
33static const char* appName;
34
35static const char swapoff_usage[] =
36"Usage: swapoff device\n"
37"\nStop swapping virtual memory pages on the given device.\n";
38static const char swapon_usage[] =
39"Usage: swapon device\n"
40"\nStart swapping virtual memory pages on the given device.\n";
41
42
43#define SWAPON_APP 1
44#define SWAPOFF_APP 2
45
46
47static void
48swap_enable_disable( char *device)
49{
50 int status;
51 if ( whichApp == SWAPON_APP )
52 status = swapon(device, 0);
53 else
54 status = swapoff(device);
55
56 if ( status != 0 ) {
57 perror(appName);
58 exit( FALSE);
59 }
60}
61
62static void
63do_em_all()
64{
65 struct mntent *m;
66 char swapName[NAME_MAX];
67 FILE *f = setmntent ("/etc/fstab", "r");
68
69 if (f == NULL) {
70 perror("/etc/fstab");
71 exit( FALSE);
72 }
73 while ((m = getmntent (f)) != NULL) {
74 if (!strstr (m->mnt_type, "swap")) {
75 swap_enable_disable( swapName);
76 }
77 }
78 endmntent (f);
79 exit( TRUE);
80}
81
82
83extern int
84swap_on_off_main(int argc, char * * argv)
85{
86 struct stat statBuf;
87 if (stat("/etc/fstab", &statBuf) < 0)
88 fprintf(stderr, "/etc/fstab file missing -- Please install one.\n\n");
89
90 if (strcmp(*argv, "swapon")==0) {
91 appName = *argv;
92 whichApp = SWAPON_APP;
93
94 } else {
95 appName = *argv;
96 whichApp = SWAPOFF_APP;
97 }
98
99 if (argc < 2)
100 goto usage_and_exit;
101 argc--;
102 argv++;
103
104 /* Parse any options */
105 while (**argv == '-') {
106 while (*++(*argv)) switch (**argv) {
107 case 'a':
108 do_em_all();
109 break;
110 default:
111 goto usage_and_exit;
112 }
113 }
114 swap_enable_disable(*argv);
115 //exit( TRUE);
116
117usage_and_exit:
118 fprintf(stderr, "Usage: %s", (whichApp==SWAPON_APP)? swapon_usage : swapoff_usage);
119 exit(FALSE);
120}
121