blob: 9da70756ee51ba54007381e0937c9c0691df1e5d [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 Andersencb81e642003-07-14 21:21:08 +00005 * Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org>
6 * Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.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#include <sys/swap.h>
Eric Andersene76c3b02001-04-05 03:14:39 +000032
Eric Andersencbe31da2001-02-20 06:14:08 +000033#include "busybox.h"
Eric Andersen5e29c6f2000-06-19 18:25:49 +000034
Eric Andersendb1df5e2002-10-26 10:27:42 +000035static int whichApp; /* default SWAPON_APP */
Eric Andersen87590061999-10-18 21:22:59 +000036
Eric Andersendb1df5e2002-10-26 10:27:42 +000037static const int SWAPON_APP = 0;
38static const int SWAPOFF_APP = 1;
Eric Andersen87590061999-10-18 21:22:59 +000039
40
Eric Andersendb1df5e2002-10-26 10:27:42 +000041static int swap_enable_disable(const char *device)
Eric Andersen87590061999-10-18 21:22:59 +000042{
Erik Andersene49d5ec2000-02-08 19:58:47 +000043 int status;
Eric Andersen97b141a2002-11-03 00:25:23 +000044 struct stat st;
45
46 if (stat(device, &st) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000047 bb_perror_msg_and_die("cannot stat %s", device);
Eric Andersen97b141a2002-11-03 00:25:23 +000048 }
49
50 /* test for holes */
51 if (S_ISREG(st.st_mode)) {
52 if (st.st_blocks * 512 < st.st_size) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000053 bb_error_msg_and_die("swap file has holes");
Eric Andersen97b141a2002-11-03 00:25:23 +000054 }
55 }
Eric Andersen87590061999-10-18 21:22:59 +000056
Erik Andersene49d5ec2000-02-08 19:58:47 +000057 if (whichApp == SWAPON_APP)
58 status = swapon(device, 0);
59 else
60 status = swapoff(device);
61
Eric Andersendb1df5e2002-10-26 10:27:42 +000062 if (status != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000063 bb_perror_msg("%s", device);
Eric Andersendb1df5e2002-10-26 10:27:42 +000064 return EXIT_FAILURE;
65 }
66 return EXIT_SUCCESS;
Eric Andersen87590061999-10-18 21:22:59 +000067}
68
Eric Andersendb1df5e2002-10-26 10:27:42 +000069static int do_em_all(void)
Eric Andersen87590061999-10-18 21:22:59 +000070{
71 struct mntent *m;
Erik Andersene49d5ec2000-02-08 19:58:47 +000072 FILE *f = setmntent("/etc/fstab", "r");
Eric Andersendb1df5e2002-10-26 10:27:42 +000073 int err = 0;
Eric Andersen87590061999-10-18 21:22:59 +000074
Matt Kraaia9819b22000-12-22 01:48:07 +000075 if (f == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +000076 bb_perror_msg_and_die("/etc/fstab");
Erik Andersene49d5ec2000-02-08 19:58:47 +000077 while ((m = getmntent(f)) != NULL) {
Eric Andersena42982e2000-06-07 17:28:53 +000078 if (strcmp(m->mnt_type, MNTTYPE_SWAP)==0) {
Eric Andersendb1df5e2002-10-26 10:27:42 +000079 if(swap_enable_disable(m->mnt_fsname) == EXIT_FAILURE)
80 err++;
Erik Andersene49d5ec2000-02-08 19:58:47 +000081 }
Eric Andersen87590061999-10-18 21:22:59 +000082 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000083 endmntent(f);
Eric Andersendb1df5e2002-10-26 10:27:42 +000084 return err;
Eric Andersen87590061999-10-18 21:22:59 +000085}
86
87
Erik Andersene49d5ec2000-02-08 19:58:47 +000088extern int swap_on_off_main(int argc, char **argv)
Eric Andersen87590061999-10-18 21:22:59 +000089{
Manuel Novoa III cad53642003-03-19 09:13:01 +000090 if (bb_applet_name[5] == 'f') { /* "swapoff" */
Erik Andersene49d5ec2000-02-08 19:58:47 +000091 whichApp = SWAPOFF_APP;
Eric Andersen87590061999-10-18 21:22:59 +000092 }
Eric Andersen87590061999-10-18 21:22:59 +000093
Erik Andersene132f4b2000-02-09 04:16:43 +000094 if (argc != 2) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000095 goto usage_and_exit;
Erik Andersene132f4b2000-02-09 04:16:43 +000096 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000097 argc--;
98 argv++;
99
100 /* Parse any options */
101 while (**argv == '-') {
102 while (*++(*argv))
103 switch (**argv) {
104 case 'a':
Erik Andersen3364d782000-03-28 00:58:14 +0000105 {
106 struct stat statBuf;
107
108 if (stat("/etc/fstab", &statBuf) < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000109 bb_error_msg_and_die("/etc/fstab file missing");
Erik Andersen3364d782000-03-28 00:58:14 +0000110 }
Eric Andersendb1df5e2002-10-26 10:27:42 +0000111 return do_em_all();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000112 break;
113 default:
114 goto usage_and_exit;
115 }
116 }
Eric Andersendb1df5e2002-10-26 10:27:42 +0000117 return swap_enable_disable(*argv);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000118
119 usage_and_exit:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000120 bb_show_usage();
Eric Andersen87590061999-10-18 21:22:59 +0000121}