blob: 7c7031bcee79424fbac0edf0be42d9f8cc21ef9e [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 Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersen87590061999-10-18 21:22:59 +00006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
Eric Andersen87590061999-10-18 21:22:59 +000023#include <stdio.h>
Eric Andersen87590061999-10-18 21:22:59 +000024#include <mntent.h>
25#include <dirent.h>
Eric Andersen87590061999-10-18 21:22:59 +000026#include <errno.h>
Eric Anderseneba8ed72001-03-09 14:36:42 +000027#include <string.h>
Eric Andersened3ef502001-01-27 08:24:39 +000028#include <stdlib.h>
29#include <sys/mount.h>
Eric Andersene76c3b02001-04-05 03:14:39 +000030#include <sys/swap.h>
Eric Andersene76c3b02001-04-05 03:14:39 +000031
Eric Andersencbe31da2001-02-20 06:14:08 +000032#include "busybox.h"
Eric Andersen5e29c6f2000-06-19 18:25:49 +000033
Eric Andersendb1df5e2002-10-26 10:27:42 +000034static int whichApp; /* default SWAPON_APP */
Eric Andersen87590061999-10-18 21:22:59 +000035
Eric Andersendb1df5e2002-10-26 10:27:42 +000036static const int SWAPON_APP = 0;
37static const int SWAPOFF_APP = 1;
Eric Andersen87590061999-10-18 21:22:59 +000038
39
Eric Andersendb1df5e2002-10-26 10:27:42 +000040static int swap_enable_disable(const char *device)
Eric Andersen87590061999-10-18 21:22:59 +000041{
Erik Andersene49d5ec2000-02-08 19:58:47 +000042 int status;
Eric Andersen97b141a2002-11-03 00:25:23 +000043 struct stat st;
44
45 if (stat(device, &st) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000046 bb_perror_msg_and_die("cannot stat %s", device);
Eric Andersen97b141a2002-11-03 00:25:23 +000047 }
48
49 /* test for holes */
50 if (S_ISREG(st.st_mode)) {
51 if (st.st_blocks * 512 < st.st_size) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000052 bb_error_msg_and_die("swap file has holes");
Eric Andersen97b141a2002-11-03 00:25:23 +000053 }
54 }
Eric Andersen87590061999-10-18 21:22:59 +000055
Erik Andersene49d5ec2000-02-08 19:58:47 +000056 if (whichApp == SWAPON_APP)
57 status = swapon(device, 0);
58 else
59 status = swapoff(device);
60
Eric Andersendb1df5e2002-10-26 10:27:42 +000061 if (status != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000062 bb_perror_msg("%s", device);
Eric Andersendb1df5e2002-10-26 10:27:42 +000063 return EXIT_FAILURE;
64 }
65 return EXIT_SUCCESS;
Eric Andersen87590061999-10-18 21:22:59 +000066}
67
Eric Andersendb1df5e2002-10-26 10:27:42 +000068static int do_em_all(void)
Eric Andersen87590061999-10-18 21:22:59 +000069{
70 struct mntent *m;
Erik Andersene49d5ec2000-02-08 19:58:47 +000071 FILE *f = setmntent("/etc/fstab", "r");
Eric Andersendb1df5e2002-10-26 10:27:42 +000072 int err = 0;
Eric Andersen87590061999-10-18 21:22:59 +000073
Matt Kraaia9819b22000-12-22 01:48:07 +000074 if (f == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +000075 bb_perror_msg_and_die("/etc/fstab");
Erik Andersene49d5ec2000-02-08 19:58:47 +000076 while ((m = getmntent(f)) != NULL) {
Eric Andersena42982e2000-06-07 17:28:53 +000077 if (strcmp(m->mnt_type, MNTTYPE_SWAP)==0) {
Eric Andersendb1df5e2002-10-26 10:27:42 +000078 if(swap_enable_disable(m->mnt_fsname) == EXIT_FAILURE)
79 err++;
Erik Andersene49d5ec2000-02-08 19:58:47 +000080 }
Eric Andersen87590061999-10-18 21:22:59 +000081 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000082 endmntent(f);
Eric Andersendb1df5e2002-10-26 10:27:42 +000083 return err;
Eric Andersen87590061999-10-18 21:22:59 +000084}
85
86
Erik Andersene49d5ec2000-02-08 19:58:47 +000087extern int swap_on_off_main(int argc, char **argv)
Eric Andersen87590061999-10-18 21:22:59 +000088{
Manuel Novoa III cad53642003-03-19 09:13:01 +000089 if (bb_applet_name[5] == 'f') { /* "swapoff" */
Erik Andersene49d5ec2000-02-08 19:58:47 +000090 whichApp = SWAPOFF_APP;
Eric Andersen87590061999-10-18 21:22:59 +000091 }
Eric Andersen87590061999-10-18 21:22:59 +000092
Erik Andersene132f4b2000-02-09 04:16:43 +000093 if (argc != 2) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000094 goto usage_and_exit;
Erik Andersene132f4b2000-02-09 04:16:43 +000095 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000096 argc--;
97 argv++;
98
99 /* Parse any options */
100 while (**argv == '-') {
101 while (*++(*argv))
102 switch (**argv) {
103 case 'a':
Erik Andersen3364d782000-03-28 00:58:14 +0000104 {
105 struct stat statBuf;
106
107 if (stat("/etc/fstab", &statBuf) < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000108 bb_error_msg_and_die("/etc/fstab file missing");
Erik Andersen3364d782000-03-28 00:58:14 +0000109 }
Eric Andersendb1df5e2002-10-26 10:27:42 +0000110 return do_em_all();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000111 break;
112 default:
113 goto usage_and_exit;
114 }
115 }
Eric Andersendb1df5e2002-10-26 10:27:42 +0000116 return swap_enable_disable(*argv);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000117
118 usage_and_exit:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000119 bb_show_usage();
Eric Andersen87590061999-10-18 21:22:59 +0000120}