blob: c624e74e312f7b0e924891eb26247b1d061a8d18 [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
Mike Frysinger6943a942005-09-13 02:29:39 +000026 if (stat(device, &st) < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000027 bb_perror_msg_and_die("cannot stat %s", device);
Eric Andersen97b141a2002-11-03 00:25:23 +000028
29 /* test for holes */
Mike Frysinger6943a942005-09-13 02:29:39 +000030 if (S_ISREG(st.st_mode))
31 if (st.st_blocks * 512 < st.st_size)
Manuel Novoa III cad53642003-03-19 09:13:01 +000032 bb_error_msg_and_die("swap file has holes");
Eric Andersen87590061999-10-18 21:22:59 +000033
Rob Landleybc3d4a12005-09-13 01:30:19 +000034 if (bb_applet_name[5] == 'n')
Erik Andersene49d5ec2000-02-08 19:58:47 +000035 status = swapon(device, 0);
36 else
37 status = swapoff(device);
38
Eric Andersendb1df5e2002-10-26 10:27:42 +000039 if (status != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000040 bb_perror_msg("%s", device);
Mike Frysinger2d5e4f62005-09-16 04:41:20 +000041 return 1;
Eric Andersendb1df5e2002-10-26 10:27:42 +000042 }
Mike Frysinger6943a942005-09-13 02:29:39 +000043
Mike Frysinger2d5e4f62005-09-16 04:41:20 +000044 return 0;
Eric Andersen87590061999-10-18 21:22:59 +000045}
46
Eric Andersendb1df5e2002-10-26 10:27:42 +000047static int do_em_all(void)
Eric Andersen87590061999-10-18 21:22:59 +000048{
49 struct mntent *m;
Mike Frysinger6943a942005-09-13 02:29:39 +000050 FILE *f;
51 int err;
Eric Andersen87590061999-10-18 21:22:59 +000052
Mike Frysinger6943a942005-09-13 02:29:39 +000053 f = setmntent("/etc/fstab", "r");
Matt Kraaia9819b22000-12-22 01:48:07 +000054 if (f == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +000055 bb_perror_msg_and_die("/etc/fstab");
Mike Frysinger6943a942005-09-13 02:29:39 +000056
57 err = 0;
58 while ((m = getmntent(f)) != NULL)
59 if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0)
Mike Frysinger2d5e4f62005-09-16 04:41:20 +000060 err += swap_enable_disable(m->mnt_fsname);
Mike Frysinger6943a942005-09-13 02:29:39 +000061
Erik Andersene49d5ec2000-02-08 19:58:47 +000062 endmntent(f);
Mike Frysinger6943a942005-09-13 02:29:39 +000063
Eric Andersendb1df5e2002-10-26 10:27:42 +000064 return err;
Eric Andersen87590061999-10-18 21:22:59 +000065}
66
Mike Frysinger6943a942005-09-13 02:29:39 +000067#define DO_ALL 0x01
Eric Andersen87590061999-10-18 21:22:59 +000068
Erik Andersene49d5ec2000-02-08 19:58:47 +000069extern int swap_on_off_main(int argc, char **argv)
Eric Andersen87590061999-10-18 21:22:59 +000070{
Mike Frysinger2d5e4f62005-09-16 04:41:20 +000071 int ret;
Mike Frysinger6943a942005-09-13 02:29:39 +000072
Mike Frysinger2d5e4f62005-09-16 04:41:20 +000073 if (argc == 1)
Rob Landleybc3d4a12005-09-13 01:30:19 +000074 bb_show_usage();
Mike Frysinger6943a942005-09-13 02:29:39 +000075
Mike Frysinger2d5e4f62005-09-16 04:41:20 +000076 ret = bb_getopt_ulflags(argc, argv, "a");
77 if (ret & DO_ALL)
Rob Landleybc3d4a12005-09-13 01:30:19 +000078 return do_em_all();
Mike Frysinger6943a942005-09-13 02:29:39 +000079
Mike Frysinger2d5e4f62005-09-16 04:41:20 +000080 ret = 0;
81 while (*++argv)
82 ret += swap_enable_disable(*argv);
83 return ret;
Eric Andersen87590061999-10-18 21:22:59 +000084}