blob: 6cc736ab159fc3406242ee323040c781b83f44e3 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersen87590061999-10-18 21:22:59 +00002/*
3 * Mini swapon/swapoff implementation for busybox
4 *
Eric Andersenbdfd0d72001-10-24 05:00:29 +00005 * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
6 * Copyright (C) 1999,2000,2001 by Erik Andersen <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
Eric Andersen87590061999-10-18 21:22:59 +000024#include <stdio.h>
Eric Andersen87590061999-10-18 21:22:59 +000025#include <mntent.h>
26#include <dirent.h>
Eric Andersen87590061999-10-18 21:22:59 +000027#include <errno.h>
Eric Anderseneba8ed72001-03-09 14:36:42 +000028#include <string.h>
Eric Andersened3ef502001-01-27 08:24:39 +000029#include <stdlib.h>
30#include <sys/mount.h>
Eric Andersene76c3b02001-04-05 03:14:39 +000031
32#if __GNU_LIBRARY__ < 5
33/* libc5 doesn't have sys/swap.h, define these here. */
34extern int swapon (__const char *__path, int __flags);
35extern int swapoff (__const char *__path);
36#else
37#include <sys/swap.h>
38#endif
39
Eric Andersencbe31da2001-02-20 06:14:08 +000040#include "busybox.h"
Eric Andersen5e29c6f2000-06-19 18:25:49 +000041
Eric Andersen87590061999-10-18 21:22:59 +000042static int whichApp;
Eric Andersen87590061999-10-18 21:22:59 +000043
Mark Whitley59ab0252001-01-23 22:30:04 +000044static const int SWAPON_APP = 1;
45static const int SWAPOFF_APP = 2;
Eric Andersen87590061999-10-18 21:22:59 +000046
47
Erik Andersene49d5ec2000-02-08 19:58:47 +000048static void swap_enable_disable(char *device)
Eric Andersen87590061999-10-18 21:22:59 +000049{
Erik Andersene49d5ec2000-02-08 19:58:47 +000050 int status;
Eric Andersen87590061999-10-18 21:22:59 +000051
Erik Andersene49d5ec2000-02-08 19:58:47 +000052 if (whichApp == SWAPON_APP)
53 status = swapon(device, 0);
54 else
55 status = swapoff(device);
56
Matt Kraaia9819b22000-12-22 01:48:07 +000057 if (status != 0)
58 perror_msg_and_die(applet_name);
Eric Andersen87590061999-10-18 21:22:59 +000059}
60
Eric Andersen74400cc2001-10-18 04:11:39 +000061static void do_em_all(void)
Eric Andersen87590061999-10-18 21:22:59 +000062{
63 struct mntent *m;
Erik Andersene49d5ec2000-02-08 19:58:47 +000064 FILE *f = setmntent("/etc/fstab", "r");
Eric Andersen87590061999-10-18 21:22:59 +000065
Matt Kraaia9819b22000-12-22 01:48:07 +000066 if (f == NULL)
67 perror_msg_and_die("/etc/fstab");
Erik Andersene49d5ec2000-02-08 19:58:47 +000068 while ((m = getmntent(f)) != NULL) {
Eric Andersena42982e2000-06-07 17:28:53 +000069 if (strcmp(m->mnt_type, MNTTYPE_SWAP)==0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000070 swap_enable_disable(m->mnt_fsname);
71 }
Eric Andersen87590061999-10-18 21:22:59 +000072 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000073 endmntent(f);
Matt Kraai3e856ce2000-12-01 02:55:13 +000074 exit(EXIT_SUCCESS);
Eric Andersen87590061999-10-18 21:22:59 +000075}
76
77
Erik Andersene49d5ec2000-02-08 19:58:47 +000078extern int swap_on_off_main(int argc, char **argv)
Eric Andersen87590061999-10-18 21:22:59 +000079{
Matt Kraaie58771e2000-07-12 15:38:49 +000080 if (strcmp(applet_name, "swapon") == 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000081 whichApp = SWAPON_APP;
Erik Andersene49d5ec2000-02-08 19:58:47 +000082 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +000083 whichApp = SWAPOFF_APP;
Eric Andersen87590061999-10-18 21:22:59 +000084 }
Eric Andersen87590061999-10-18 21:22:59 +000085
Erik Andersene132f4b2000-02-09 04:16:43 +000086 if (argc != 2) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000087 goto usage_and_exit;
Erik Andersene132f4b2000-02-09 04:16:43 +000088 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000089 argc--;
90 argv++;
91
92 /* Parse any options */
93 while (**argv == '-') {
94 while (*++(*argv))
95 switch (**argv) {
96 case 'a':
Erik Andersen3364d782000-03-28 00:58:14 +000097 {
98 struct stat statBuf;
99
100 if (stat("/etc/fstab", &statBuf) < 0)
Matt Kraaidd19c692001-01-31 19:00:21 +0000101 error_msg_and_die("/etc/fstab file missing");
Erik Andersen3364d782000-03-28 00:58:14 +0000102 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000103 do_em_all();
104 break;
105 default:
106 goto usage_and_exit;
107 }
108 }
109 swap_enable_disable(*argv);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000110 return EXIT_SUCCESS;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000111
112 usage_and_exit:
Eric Andersen67991cf2001-02-14 21:23:06 +0000113 show_usage();
Eric Andersen87590061999-10-18 21:22:59 +0000114}