blob: 473325d0c99fe2dcc440cbdd851d44cbb2ff4c80 [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 *
Mike Frysinger6943a942005-09-13 02:29:39 +00007 * Licensed under the GPL v2, see the file LICENSE in this tarball.
Eric Andersen87590061999-10-18 21:22:59 +00008 */
9
Eric Andersen87590061999-10-18 21:22:59 +000010#include <stdio.h>
Eric Andersen87590061999-10-18 21:22:59 +000011#include <mntent.h>
12#include <dirent.h>
Eric Andersen87590061999-10-18 21:22:59 +000013#include <errno.h>
Eric Anderseneba8ed72001-03-09 14:36:42 +000014#include <string.h>
Eric Andersened3ef502001-01-27 08:24:39 +000015#include <stdlib.h>
16#include <sys/mount.h>
Eric Andersene76c3b02001-04-05 03:14:39 +000017#include <sys/swap.h>
Eric Andersene76c3b02001-04-05 03:14:39 +000018
Eric Andersencbe31da2001-02-20 06:14:08 +000019#include "busybox.h"
Eric Andersen5e29c6f2000-06-19 18:25:49 +000020
Eric Andersendb1df5e2002-10-26 10:27:42 +000021static int swap_enable_disable(const char *device)
Eric Andersen87590061999-10-18 21:22:59 +000022{
Erik Andersene49d5ec2000-02-08 19:58:47 +000023 int status;
Eric Andersen97b141a2002-11-03 00:25:23 +000024 struct stat st;
25
Rob Landleyc5b1d4d2006-03-13 15:45:16 +000026 xstat(device, &st);
Eric Andersen97b141a2002-11-03 00:25:23 +000027
28 /* test for holes */
Mike Frysinger6943a942005-09-13 02:29:39 +000029 if (S_ISREG(st.st_mode))
30 if (st.st_blocks * 512 < st.st_size)
Manuel Novoa III cad53642003-03-19 09:13:01 +000031 bb_error_msg_and_die("swap file has holes");
Eric Andersen87590061999-10-18 21:22:59 +000032
Rob Landleybc3d4a12005-09-13 01:30:19 +000033 if (bb_applet_name[5] == 'n')
Erik Andersene49d5ec2000-02-08 19:58:47 +000034 status = swapon(device, 0);
35 else
36 status = swapoff(device);
37
Eric Andersendb1df5e2002-10-26 10:27:42 +000038 if (status != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000039 bb_perror_msg("%s", device);
Mike Frysinger2d5e4f62005-09-16 04:41:20 +000040 return 1;
Eric Andersendb1df5e2002-10-26 10:27:42 +000041 }
Mike Frysinger6943a942005-09-13 02:29:39 +000042
Mike Frysinger2d5e4f62005-09-16 04:41:20 +000043 return 0;
Eric Andersen87590061999-10-18 21:22:59 +000044}
45
Eric Andersendb1df5e2002-10-26 10:27:42 +000046static int do_em_all(void)
Eric Andersen87590061999-10-18 21:22:59 +000047{
48 struct mntent *m;
Mike Frysinger6943a942005-09-13 02:29:39 +000049 FILE *f;
50 int err;
Eric Andersen87590061999-10-18 21:22:59 +000051
Mike Frysinger6943a942005-09-13 02:29:39 +000052 f = setmntent("/etc/fstab", "r");
Matt Kraaia9819b22000-12-22 01:48:07 +000053 if (f == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +000054 bb_perror_msg_and_die("/etc/fstab");
Mike Frysinger6943a942005-09-13 02:29:39 +000055
56 err = 0;
57 while ((m = getmntent(f)) != NULL)
58 if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0)
Mike Frysinger2d5e4f62005-09-16 04:41:20 +000059 err += swap_enable_disable(m->mnt_fsname);
Mike Frysinger6943a942005-09-13 02:29:39 +000060
Erik Andersene49d5ec2000-02-08 19:58:47 +000061 endmntent(f);
Mike Frysinger6943a942005-09-13 02:29:39 +000062
Eric Andersendb1df5e2002-10-26 10:27:42 +000063 return err;
Eric Andersen87590061999-10-18 21:22:59 +000064}
65
Mike Frysinger6943a942005-09-13 02:29:39 +000066#define DO_ALL 0x01
Eric Andersen87590061999-10-18 21:22:59 +000067
Rob Landleydfba7412006-03-06 20:47:33 +000068int swap_on_off_main(int argc, char **argv)
Eric Andersen87590061999-10-18 21:22:59 +000069{
Mike Frysinger2d5e4f62005-09-16 04:41:20 +000070 int ret;
Mike Frysinger6943a942005-09-13 02:29:39 +000071
Mike Frysinger2d5e4f62005-09-16 04:41:20 +000072 if (argc == 1)
Rob Landleybc3d4a12005-09-13 01:30:19 +000073 bb_show_usage();
Mike Frysinger6943a942005-09-13 02:29:39 +000074
Mike Frysinger2d5e4f62005-09-16 04:41:20 +000075 ret = bb_getopt_ulflags(argc, argv, "a");
76 if (ret & DO_ALL)
Rob Landleybc3d4a12005-09-13 01:30:19 +000077 return do_em_all();
Mike Frysinger6943a942005-09-13 02:29:39 +000078
Mike Frysinger2d5e4f62005-09-16 04:41:20 +000079 ret = 0;
80 while (*++argv)
81 ret += swap_enable_disable(*argv);
82 return ret;
Eric Andersen87590061999-10-18 21:22:59 +000083}