blob: b323cb10af59c8794f19026936f87536ad1f1764 [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 Andersendb1df5e2002-10-26 10:27:42 +000042static int whichApp; /* default SWAPON_APP */
Eric Andersen87590061999-10-18 21:22:59 +000043
Eric Andersendb1df5e2002-10-26 10:27:42 +000044static const int SWAPON_APP = 0;
45static const int SWAPOFF_APP = 1;
Eric Andersen87590061999-10-18 21:22:59 +000046
47
Eric Andersendb1df5e2002-10-26 10:27:42 +000048static int swap_enable_disable(const char *device)
Eric Andersen87590061999-10-18 21:22:59 +000049{
Erik Andersene49d5ec2000-02-08 19:58:47 +000050 int status;
Eric Andersen97b141a2002-11-03 00:25:23 +000051 struct stat st;
52
53 if (stat(device, &st) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000054 bb_perror_msg_and_die("cannot stat %s", device);
Eric Andersen97b141a2002-11-03 00:25:23 +000055 }
56
57 /* test for holes */
58 if (S_ISREG(st.st_mode)) {
59 if (st.st_blocks * 512 < st.st_size) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000060 bb_error_msg_and_die("swap file has holes");
Eric Andersen97b141a2002-11-03 00:25:23 +000061 }
62 }
Eric Andersen87590061999-10-18 21:22:59 +000063
Erik Andersene49d5ec2000-02-08 19:58:47 +000064 if (whichApp == SWAPON_APP)
65 status = swapon(device, 0);
66 else
67 status = swapoff(device);
68
Eric Andersendb1df5e2002-10-26 10:27:42 +000069 if (status != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000070 bb_perror_msg("%s", device);
Eric Andersendb1df5e2002-10-26 10:27:42 +000071 return EXIT_FAILURE;
72 }
73 return EXIT_SUCCESS;
Eric Andersen87590061999-10-18 21:22:59 +000074}
75
Eric Andersendb1df5e2002-10-26 10:27:42 +000076static int do_em_all(void)
Eric Andersen87590061999-10-18 21:22:59 +000077{
78 struct mntent *m;
Erik Andersene49d5ec2000-02-08 19:58:47 +000079 FILE *f = setmntent("/etc/fstab", "r");
Eric Andersendb1df5e2002-10-26 10:27:42 +000080 int err = 0;
Eric Andersen87590061999-10-18 21:22:59 +000081
Matt Kraaia9819b22000-12-22 01:48:07 +000082 if (f == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +000083 bb_perror_msg_and_die("/etc/fstab");
Erik Andersene49d5ec2000-02-08 19:58:47 +000084 while ((m = getmntent(f)) != NULL) {
Eric Andersena42982e2000-06-07 17:28:53 +000085 if (strcmp(m->mnt_type, MNTTYPE_SWAP)==0) {
Eric Andersendb1df5e2002-10-26 10:27:42 +000086 if(swap_enable_disable(m->mnt_fsname) == EXIT_FAILURE)
87 err++;
Erik Andersene49d5ec2000-02-08 19:58:47 +000088 }
Eric Andersen87590061999-10-18 21:22:59 +000089 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000090 endmntent(f);
Eric Andersendb1df5e2002-10-26 10:27:42 +000091 return err;
Eric Andersen87590061999-10-18 21:22:59 +000092}
93
94
Erik Andersene49d5ec2000-02-08 19:58:47 +000095extern int swap_on_off_main(int argc, char **argv)
Eric Andersen87590061999-10-18 21:22:59 +000096{
Manuel Novoa III cad53642003-03-19 09:13:01 +000097 if (bb_applet_name[5] == 'f') { /* "swapoff" */
Erik Andersene49d5ec2000-02-08 19:58:47 +000098 whichApp = SWAPOFF_APP;
Eric Andersen87590061999-10-18 21:22:59 +000099 }
Eric Andersen87590061999-10-18 21:22:59 +0000100
Erik Andersene132f4b2000-02-09 04:16:43 +0000101 if (argc != 2) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000102 goto usage_and_exit;
Erik Andersene132f4b2000-02-09 04:16:43 +0000103 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000104 argc--;
105 argv++;
106
107 /* Parse any options */
108 while (**argv == '-') {
109 while (*++(*argv))
110 switch (**argv) {
111 case 'a':
Erik Andersen3364d782000-03-28 00:58:14 +0000112 {
113 struct stat statBuf;
114
115 if (stat("/etc/fstab", &statBuf) < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000116 bb_error_msg_and_die("/etc/fstab file missing");
Erik Andersen3364d782000-03-28 00:58:14 +0000117 }
Eric Andersendb1df5e2002-10-26 10:27:42 +0000118 return do_em_all();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000119 break;
120 default:
121 goto usage_and_exit;
122 }
123 }
Eric Andersendb1df5e2002-10-26 10:27:42 +0000124 return swap_enable_disable(*argv);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000125
126 usage_and_exit:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000127 bb_show_usage();
Eric Andersen87590061999-10-18 21:22:59 +0000128}