blob: 48f6f4e0bf979438ca7b9bd5d3cfbc2feeb18eaf [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 *
Rob Landleye9a7a622006-09-22 02:52:41 +00007 * Licensed under the GPL version 2, see the file LICENSE in this tarball.
Eric Andersen87590061999-10-18 21:22:59 +00008 */
9
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000010#include "libbb.h"
Eric Andersen87590061999-10-18 21:22:59 +000011#include <mntent.h>
Eric Andersene76c3b02001-04-05 03:14:39 +000012#include <sys/swap.h>
Eric Andersene76c3b02001-04-05 03:14:39 +000013
Rob Landley20cc6d52006-09-12 21:42:17 +000014static int swap_enable_disable(char *device)
Eric Andersen87590061999-10-18 21:22:59 +000015{
Erik Andersene49d5ec2000-02-08 19:58:47 +000016 int status;
Eric Andersen97b141a2002-11-03 00:25:23 +000017 struct stat st;
18
Rob Landleyc5b1d4d2006-03-13 15:45:16 +000019 xstat(device, &st);
Eric Andersen97b141a2002-11-03 00:25:23 +000020
Denis Vlasenko56594072007-03-14 22:55:39 +000021#if ENABLE_DESKTOP
Eric Andersen97b141a2002-11-03 00:25:23 +000022 /* test for holes */
Mike Frysinger6943a942005-09-13 02:29:39 +000023 if (S_ISREG(st.st_mode))
Denis Vlasenkof8b21d02007-11-05 19:33:38 +000024 if (st.st_blocks * (off_t)512 < st.st_size)
Denis Vlasenko56594072007-03-14 22:55:39 +000025 bb_error_msg("warning: swap file has holes");
Denis Vlasenkob3f09f42007-03-12 18:16:24 +000026#endif
Eric Andersen87590061999-10-18 21:22:59 +000027
Denis Vlasenko8f8f2682006-10-03 21:00:43 +000028 if (applet_name[5] == 'n')
Erik Andersene49d5ec2000-02-08 19:58:47 +000029 status = swapon(device, 0);
30 else
31 status = swapoff(device);
32
Eric Andersendb1df5e2002-10-26 10:27:42 +000033 if (status != 0) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +000034 bb_simple_perror_msg(device);
Mike Frysinger2d5e4f62005-09-16 04:41:20 +000035 return 1;
Eric Andersendb1df5e2002-10-26 10:27:42 +000036 }
Mike Frysinger6943a942005-09-13 02:29:39 +000037
Mike Frysinger2d5e4f62005-09-16 04:41:20 +000038 return 0;
Eric Andersen87590061999-10-18 21:22:59 +000039}
40
Eric Andersendb1df5e2002-10-26 10:27:42 +000041static int do_em_all(void)
Eric Andersen87590061999-10-18 21:22:59 +000042{
43 struct mntent *m;
Mike Frysinger6943a942005-09-13 02:29:39 +000044 FILE *f;
45 int err;
Eric Andersen87590061999-10-18 21:22:59 +000046
Mike Frysinger6943a942005-09-13 02:29:39 +000047 f = setmntent("/etc/fstab", "r");
Matt Kraaia9819b22000-12-22 01:48:07 +000048 if (f == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +000049 bb_perror_msg_and_die("/etc/fstab");
Mike Frysinger6943a942005-09-13 02:29:39 +000050
51 err = 0;
52 while ((m = getmntent(f)) != NULL)
53 if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0)
Mike Frysinger2d5e4f62005-09-16 04:41:20 +000054 err += swap_enable_disable(m->mnt_fsname);
Mike Frysinger6943a942005-09-13 02:29:39 +000055
Erik Andersene49d5ec2000-02-08 19:58:47 +000056 endmntent(f);
Mike Frysinger6943a942005-09-13 02:29:39 +000057
Eric Andersendb1df5e2002-10-26 10:27:42 +000058 return err;
Eric Andersen87590061999-10-18 21:22:59 +000059}
60
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000061int swap_on_off_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +000062int swap_on_off_main(int argc, char **argv)
Eric Andersen87590061999-10-18 21:22:59 +000063{
Mike Frysinger2d5e4f62005-09-16 04:41:20 +000064 int ret;
Mike Frysinger6943a942005-09-13 02:29:39 +000065
Mike Frysinger2d5e4f62005-09-16 04:41:20 +000066 if (argc == 1)
Rob Landleybc3d4a12005-09-13 01:30:19 +000067 bb_show_usage();
Mike Frysinger6943a942005-09-13 02:29:39 +000068
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000069 ret = getopt32(argv, "a");
Denis Vlasenkob3f09f42007-03-12 18:16:24 +000070 if (ret)
Rob Landleybc3d4a12005-09-13 01:30:19 +000071 return do_em_all();
Mike Frysinger6943a942005-09-13 02:29:39 +000072
Denis Vlasenkob3f09f42007-03-12 18:16:24 +000073 /* ret = 0; redundant */
Mike Frysinger2d5e4f62005-09-16 04:41:20 +000074 while (*++argv)
75 ret += swap_enable_disable(*argv);
76 return ret;
Eric Andersen87590061999-10-18 21:22:59 +000077}