blob: 9bb70a1f5a797710d50a6e0cb86a54d9173e600c [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
Rob Landley22d26fc2006-06-15 15:49:36 +000010#include "busybox.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 Andersene76c3b02001-04-05 03:14:39 +000015#include <sys/swap.h>
Eric Andersene76c3b02001-04-05 03:14:39 +000016
Eric Andersen5e29c6f2000-06-19 18:25:49 +000017
Eric Andersendb1df5e2002-10-26 10:27:42 +000018static int swap_enable_disable(const char *device)
Eric Andersen87590061999-10-18 21:22:59 +000019{
Erik Andersene49d5ec2000-02-08 19:58:47 +000020 int status;
Eric Andersen97b141a2002-11-03 00:25:23 +000021 struct stat st;
22
Rob Landleyc5b1d4d2006-03-13 15:45:16 +000023 xstat(device, &st);
Eric Andersen97b141a2002-11-03 00:25:23 +000024
25 /* test for holes */
Mike Frysinger6943a942005-09-13 02:29:39 +000026 if (S_ISREG(st.st_mode))
27 if (st.st_blocks * 512 < st.st_size)
Manuel Novoa III cad53642003-03-19 09:13:01 +000028 bb_error_msg_and_die("swap file has holes");
Eric Andersen87590061999-10-18 21:22:59 +000029
Rob Landleybc3d4a12005-09-13 01:30:19 +000030 if (bb_applet_name[5] == 'n')
Erik Andersene49d5ec2000-02-08 19:58:47 +000031 status = swapon(device, 0);
32 else
33 status = swapoff(device);
34
Eric Andersendb1df5e2002-10-26 10:27:42 +000035 if (status != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000036 bb_perror_msg("%s", device);
Mike Frysinger2d5e4f62005-09-16 04:41:20 +000037 return 1;
Eric Andersendb1df5e2002-10-26 10:27:42 +000038 }
Mike Frysinger6943a942005-09-13 02:29:39 +000039
Mike Frysinger2d5e4f62005-09-16 04:41:20 +000040 return 0;
Eric Andersen87590061999-10-18 21:22:59 +000041}
42
Eric Andersendb1df5e2002-10-26 10:27:42 +000043static int do_em_all(void)
Eric Andersen87590061999-10-18 21:22:59 +000044{
45 struct mntent *m;
Mike Frysinger6943a942005-09-13 02:29:39 +000046 FILE *f;
47 int err;
Eric Andersen87590061999-10-18 21:22:59 +000048
Mike Frysinger6943a942005-09-13 02:29:39 +000049 f = setmntent("/etc/fstab", "r");
Matt Kraaia9819b22000-12-22 01:48:07 +000050 if (f == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +000051 bb_perror_msg_and_die("/etc/fstab");
Mike Frysinger6943a942005-09-13 02:29:39 +000052
53 err = 0;
54 while ((m = getmntent(f)) != NULL)
55 if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0)
Mike Frysinger2d5e4f62005-09-16 04:41:20 +000056 err += swap_enable_disable(m->mnt_fsname);
Mike Frysinger6943a942005-09-13 02:29:39 +000057
Erik Andersene49d5ec2000-02-08 19:58:47 +000058 endmntent(f);
Mike Frysinger6943a942005-09-13 02:29:39 +000059
Eric Andersendb1df5e2002-10-26 10:27:42 +000060 return err;
Eric Andersen87590061999-10-18 21:22:59 +000061}
62
Mike Frysinger6943a942005-09-13 02:29:39 +000063#define DO_ALL 0x01
Eric Andersen87590061999-10-18 21:22:59 +000064
Rob Landleydfba7412006-03-06 20:47:33 +000065int swap_on_off_main(int argc, char **argv)
Eric Andersen87590061999-10-18 21:22:59 +000066{
Mike Frysinger2d5e4f62005-09-16 04:41:20 +000067 int ret;
Mike Frysinger6943a942005-09-13 02:29:39 +000068
Mike Frysinger2d5e4f62005-09-16 04:41:20 +000069 if (argc == 1)
Rob Landleybc3d4a12005-09-13 01:30:19 +000070 bb_show_usage();
Mike Frysinger6943a942005-09-13 02:29:39 +000071
Mike Frysinger2d5e4f62005-09-16 04:41:20 +000072 ret = bb_getopt_ulflags(argc, argv, "a");
73 if (ret & DO_ALL)
Rob Landleybc3d4a12005-09-13 01:30:19 +000074 return do_em_all();
Mike Frysinger6943a942005-09-13 02:29:39 +000075
Mike Frysinger2d5e4f62005-09-16 04:41:20 +000076 ret = 0;
77 while (*++argv)
78 ret += swap_enable_disable(*argv);
79 return ret;
Eric Andersen87590061999-10-18 21:22:59 +000080}