2000-02-09 01:28:47 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
1999-10-19 02:52:59 +05:30
|
|
|
/*
|
|
|
|
* Mini swapon/swapoff implementation for busybox
|
|
|
|
*
|
2004-03-15 13:59:22 +05:30
|
|
|
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
|
1999-10-19 02:52:59 +05:30
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
1999-10-19 02:52:59 +05:30
|
|
|
*/
|
2016-11-14 22:00:50 +05:30
|
|
|
//config:config SWAPON
|
|
|
|
//config: bool "swapon"
|
|
|
|
//config: default y
|
|
|
|
//config: select PLATFORM_LINUX
|
|
|
|
//config: help
|
|
|
|
//config: This option enables the 'swapon' utility.
|
|
|
|
//config: Once you have created some swap space using 'mkswap', you also need
|
|
|
|
//config: to enable your swap space with the 'swapon' utility. The 'swapoff'
|
|
|
|
//config: utility is used, typically at system shutdown, to disable any swap
|
|
|
|
//config: space. If you are not using any swap space, you can leave this
|
|
|
|
//config: option disabled.
|
|
|
|
//config:
|
|
|
|
//config:config FEATURE_SWAPON_DISCARD
|
|
|
|
//config: bool "Support discard option -d"
|
|
|
|
//config: default y
|
|
|
|
//config: depends on SWAPON
|
|
|
|
//config: help
|
|
|
|
//config: Enable support for discarding swap area blocks at swapon and/or as
|
|
|
|
//config: the kernel frees them. This option enables both the -d option on
|
|
|
|
//config: 'swapon' and the 'discard' option for swap entries in /etc/fstab.
|
|
|
|
//config:
|
|
|
|
//config:config FEATURE_SWAPON_PRI
|
|
|
|
//config: bool "Support priority option -p"
|
|
|
|
//config: default y
|
|
|
|
//config: depends on SWAPON
|
|
|
|
//config: help
|
|
|
|
//config: Enable support for setting swap device priority in swapon.
|
|
|
|
//config:
|
|
|
|
//config:config SWAPOFF
|
|
|
|
//config: bool "swapoff"
|
|
|
|
//config: default y
|
|
|
|
//config: select PLATFORM_LINUX
|
|
|
|
//config: help
|
|
|
|
//config: This option enables the 'swapoff' utility.
|
|
|
|
|
2017-01-29 19:27:33 +05:30
|
|
|
// APPLET_ODDNAME:name main location suid_type help
|
|
|
|
//applet:IF_SWAPON( APPLET_ODDNAME(swapon, swap_on_off, BB_DIR_SBIN, BB_SUID_DROP, swapon))
|
2016-11-14 22:00:50 +05:30
|
|
|
//applet:IF_SWAPOFF(APPLET_ODDNAME(swapoff, swap_on_off, BB_DIR_SBIN, BB_SUID_DROP, swapoff))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_SWAPON) += swaponoff.o
|
|
|
|
//kbuild:lib-$(CONFIG_SWAPOFF) += swaponoff.o
|
|
|
|
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage:#define swapon_trivial_usage
|
2015-01-06 01:05:00 +05:30
|
|
|
//usage: "[-a] [-e]" IF_FEATURE_SWAPON_DISCARD(" [-d[POL]]") IF_FEATURE_SWAPON_PRI(" [-p PRI]") " [DEVICE]"
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage:#define swapon_full_usage "\n\n"
|
|
|
|
//usage: "Start swapping on DEVICE\n"
|
|
|
|
//usage: "\n -a Start swapping on all swap devices"
|
2014-03-23 04:51:01 +05:30
|
|
|
//usage: IF_FEATURE_SWAPON_DISCARD(
|
|
|
|
//usage: "\n -d[POL] Discard blocks at swapon (POL=once),"
|
|
|
|
//usage: "\n as freed (POL=pages), or both (POL omitted)"
|
|
|
|
//usage: )
|
2015-01-06 01:05:00 +05:30
|
|
|
//usage: "\n -e Silently skip devices that do not exist"
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage: IF_FEATURE_SWAPON_PRI(
|
|
|
|
//usage: "\n -p PRI Set swap device priority"
|
|
|
|
//usage: )
|
|
|
|
//usage:
|
|
|
|
//usage:#define swapoff_trivial_usage
|
2015-12-16 23:29:08 +05:30
|
|
|
//usage: "[-a] [DEVICE]"
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage:#define swapoff_full_usage "\n\n"
|
|
|
|
//usage: "Stop swapping on DEVICE\n"
|
|
|
|
//usage: "\n -a Stop swapping on all swap devices"
|
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2016-04-21 19:56:30 +05:30
|
|
|
#include "common_bufsiz.h"
|
1999-10-19 02:52:59 +05:30
|
|
|
#include <mntent.h>
|
2011-07-08 12:19:40 +05:30
|
|
|
#ifndef __BIONIC__
|
|
|
|
# include <sys/swap.h>
|
|
|
|
#endif
|
1999-10-19 02:52:59 +05:30
|
|
|
|
2009-09-20 07:58:22 +05:30
|
|
|
#if ENABLE_FEATURE_MOUNT_LABEL
|
|
|
|
# include "volume_id.h"
|
|
|
|
#else
|
|
|
|
# define resolve_mount_spec(fsname) ((void)0)
|
|
|
|
#endif
|
|
|
|
|
2011-07-08 12:19:40 +05:30
|
|
|
#ifndef MNTTYPE_SWAP
|
|
|
|
# define MNTTYPE_SWAP "swap"
|
|
|
|
#endif
|
|
|
|
|
2014-03-23 04:51:01 +05:30
|
|
|
#if ENABLE_FEATURE_SWAPON_DISCARD
|
|
|
|
#ifndef SWAP_FLAG_DISCARD
|
|
|
|
#define SWAP_FLAG_DISCARD 0x10000
|
|
|
|
#endif
|
|
|
|
#ifndef SWAP_FLAG_DISCARD_ONCE
|
|
|
|
#define SWAP_FLAG_DISCARD_ONCE 0x20000
|
|
|
|
#endif
|
|
|
|
#ifndef SWAP_FLAG_DISCARD_PAGES
|
|
|
|
#define SWAP_FLAG_DISCARD_PAGES 0x40000
|
|
|
|
#endif
|
|
|
|
#define SWAP_FLAG_DISCARD_MASK \
|
|
|
|
(SWAP_FLAG_DISCARD | SWAP_FLAG_DISCARD_ONCE | SWAP_FLAG_DISCARD_PAGES)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#if ENABLE_FEATURE_SWAPON_DISCARD || ENABLE_FEATURE_SWAPON_PRI
|
2008-05-19 04:35:34 +05:30
|
|
|
struct globals {
|
|
|
|
int flags;
|
2010-02-04 19:30:15 +05:30
|
|
|
} FIX_ALIASING;
|
2016-04-21 19:56:30 +05:30
|
|
|
#define G (*(struct globals*)bb_common_bufsiz1)
|
2008-05-19 04:35:34 +05:30
|
|
|
#define g_flags (G.flags)
|
2014-03-31 20:09:26 +05:30
|
|
|
#define save_g_flags() int save_g_flags = g_flags
|
|
|
|
#define restore_g_flags() g_flags = save_g_flags
|
2008-05-19 04:35:34 +05:30
|
|
|
#else
|
|
|
|
#define g_flags 0
|
2014-03-31 20:09:26 +05:30
|
|
|
#define save_g_flags() ((void)0)
|
|
|
|
#define restore_g_flags() ((void)0)
|
2008-05-19 04:35:34 +05:30
|
|
|
#endif
|
2016-04-21 21:48:48 +05:30
|
|
|
#define INIT_G() do { setup_common_bufsiz(); } while (0)
|
2008-05-19 04:35:34 +05:30
|
|
|
|
2016-11-14 22:00:50 +05:30
|
|
|
#if ENABLE_SWAPOFF
|
|
|
|
# if ENABLE_SWAPON
|
|
|
|
# define do_swapoff (applet_name[5] == 'f')
|
|
|
|
# else
|
|
|
|
# define do_swapoff 1
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
# define do_swapoff 0
|
|
|
|
#endif
|
2014-03-31 20:09:26 +05:30
|
|
|
|
|
|
|
/* Command line options */
|
|
|
|
enum {
|
|
|
|
OPTBIT_a, /* -a all */
|
2015-01-06 01:05:00 +05:30
|
|
|
OPTBIT_e, /* -e ifexists */
|
2014-03-31 20:09:26 +05:30
|
|
|
IF_FEATURE_SWAPON_DISCARD( OPTBIT_d ,) /* -d discard */
|
|
|
|
IF_FEATURE_SWAPON_PRI ( OPTBIT_p ,) /* -p priority */
|
|
|
|
OPT_a = 1 << OPTBIT_a,
|
2015-01-06 01:05:00 +05:30
|
|
|
OPT_e = 1 << OPTBIT_e,
|
2014-03-31 20:09:26 +05:30
|
|
|
OPT_d = IF_FEATURE_SWAPON_DISCARD((1 << OPTBIT_d)) + 0,
|
|
|
|
OPT_p = IF_FEATURE_SWAPON_PRI ((1 << OPTBIT_p)) + 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
#define OPT_ALL (option_mask32 & OPT_a)
|
|
|
|
#define OPT_DISCARD (option_mask32 & OPT_d)
|
2015-01-06 01:05:00 +05:30
|
|
|
#define OPT_IFEXISTS (option_mask32 & OPT_e)
|
2014-03-31 20:09:26 +05:30
|
|
|
#define OPT_PRIO (option_mask32 & OPT_p)
|
|
|
|
|
2015-12-18 23:32:31 +05:30
|
|
|
static int swap_enable_disable(char *device)
|
1999-10-19 02:52:59 +05:30
|
|
|
{
|
2014-03-31 20:09:26 +05:30
|
|
|
int err = 0;
|
|
|
|
int quiet = 0;
|
2002-11-03 05:55:23 +05:30
|
|
|
|
2009-09-20 07:58:22 +05:30
|
|
|
resolve_mount_spec(&device);
|
2000-02-09 01:28:47 +05:30
|
|
|
|
2014-03-31 20:09:26 +05:30
|
|
|
if (do_swapoff) {
|
|
|
|
err = swapoff(device);
|
|
|
|
/* Don't complain on OPT_ALL if not a swap device or if it doesn't exist */
|
|
|
|
quiet = (OPT_ALL && (errno == EINVAL || errno == ENOENT));
|
|
|
|
} else {
|
|
|
|
/* swapon */
|
2015-12-16 23:29:08 +05:30
|
|
|
struct stat st;
|
2014-03-31 20:09:26 +05:30
|
|
|
err = stat(device, &st);
|
|
|
|
if (!err) {
|
|
|
|
if (ENABLE_DESKTOP && S_ISREG(st.st_mode)) {
|
|
|
|
if (st.st_blocks * (off_t)512 < st.st_size) {
|
|
|
|
bb_error_msg("%s: file has holes", device);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = swapon(device, g_flags);
|
|
|
|
/* Don't complain on swapon -a if device is already in use */
|
2015-12-16 23:29:08 +05:30
|
|
|
quiet = (OPT_ALL && errno == EBUSY);
|
2014-03-31 20:09:26 +05:30
|
|
|
}
|
2015-12-16 23:29:08 +05:30
|
|
|
/* Don't complain if file does not exist with -e option */
|
|
|
|
if (err && OPT_IFEXISTS && errno == ENOENT)
|
|
|
|
err = 0;
|
2014-03-31 20:09:26 +05:30
|
|
|
}
|
2000-02-09 01:28:47 +05:30
|
|
|
|
2014-04-01 13:21:27 +05:30
|
|
|
if (err && !quiet) {
|
|
|
|
bb_simple_perror_msg(device);
|
2005-09-16 10:11:20 +05:30
|
|
|
return 1;
|
2002-10-26 15:57:42 +05:30
|
|
|
}
|
2005-09-16 10:11:20 +05:30
|
|
|
return 0;
|
1999-10-19 02:52:59 +05:30
|
|
|
}
|
|
|
|
|
2014-03-31 20:09:26 +05:30
|
|
|
#if ENABLE_FEATURE_SWAPON_DISCARD
|
|
|
|
static void set_discard_flag(char *s)
|
1999-10-19 02:52:59 +05:30
|
|
|
{
|
2014-03-31 20:09:26 +05:30
|
|
|
/* Unset the flag first to allow fstab options to override */
|
|
|
|
/* options set on the command line */
|
|
|
|
g_flags = (g_flags & ~SWAP_FLAG_DISCARD_MASK) | SWAP_FLAG_DISCARD;
|
|
|
|
|
|
|
|
if (!s) /* No optional policy value on the commandline */
|
|
|
|
return;
|
|
|
|
/* Skip prepended '=' */
|
|
|
|
if (*s == '=')
|
|
|
|
s++;
|
|
|
|
/* For fstab parsing: remove other appended options */
|
|
|
|
*strchrnul(s, ',') = '\0';
|
|
|
|
|
|
|
|
if (strcmp(s, "once") == 0)
|
|
|
|
g_flags |= SWAP_FLAG_DISCARD_ONCE;
|
|
|
|
if (strcmp(s, "pages") == 0)
|
|
|
|
g_flags |= SWAP_FLAG_DISCARD_PAGES;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define set_discard_flag(s) ((void)0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if ENABLE_FEATURE_SWAPON_PRI
|
|
|
|
static void set_priority_flag(char *s)
|
|
|
|
{
|
|
|
|
unsigned prio;
|
|
|
|
|
|
|
|
/* For fstab parsing: remove other appended options */
|
|
|
|
*strchrnul(s, ',') = '\0';
|
|
|
|
/* Max allowed 32767 (== SWAP_FLAG_PRIO_MASK) */
|
|
|
|
prio = bb_strtou(s, NULL, 10);
|
|
|
|
if (!errno) {
|
|
|
|
/* Unset the flag first to allow fstab options to override */
|
|
|
|
/* options set on the command line */
|
|
|
|
g_flags = (g_flags & ~SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER |
|
|
|
|
MIN(prio, SWAP_FLAG_PRIO_MASK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define set_priority_flag(s) ((void)0)
|
2014-03-23 04:40:08 +05:30
|
|
|
#endif
|
1999-10-19 02:52:59 +05:30
|
|
|
|
2014-03-31 20:09:26 +05:30
|
|
|
static int do_em_all_in_fstab(void)
|
|
|
|
{
|
|
|
|
struct mntent *m;
|
|
|
|
int err = 0;
|
|
|
|
FILE *f = xfopen_for_read("/etc/fstab");
|
2005-09-13 07:59:39 +05:30
|
|
|
|
2010-04-30 01:50:57 +05:30
|
|
|
while ((m = getmntent(f)) != NULL) {
|
|
|
|
if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0) {
|
|
|
|
/* swapon -a should ignore entries with noauto,
|
2014-03-31 20:09:26 +05:30
|
|
|
* but swapoff -a should process them
|
|
|
|
*/
|
|
|
|
if (do_swapoff || hasmntopt(m, MNTOPT_NOAUTO) == NULL) {
|
|
|
|
/* each swap space might have different flags */
|
|
|
|
/* save global flags for the next round */
|
|
|
|
save_g_flags();
|
|
|
|
if (ENABLE_FEATURE_SWAPON_DISCARD) {
|
|
|
|
char *p = hasmntopt(m, "discard");
|
|
|
|
if (p) {
|
|
|
|
/* move to '=' or to end of string */
|
|
|
|
p += 7;
|
|
|
|
set_discard_flag(p);
|
2014-03-23 04:51:01 +05:30
|
|
|
}
|
|
|
|
}
|
2014-03-31 20:09:26 +05:30
|
|
|
if (ENABLE_FEATURE_SWAPON_PRI) {
|
|
|
|
char *p = hasmntopt(m, "pri");
|
|
|
|
if (p) {
|
|
|
|
set_priority_flag(p + 4);
|
2013-08-08 13:51:27 +05:30
|
|
|
}
|
|
|
|
}
|
2014-03-31 20:09:26 +05:30
|
|
|
err |= swap_enable_disable(m->mnt_fsname);
|
|
|
|
restore_g_flags();
|
2010-04-30 01:50:57 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-09-13 07:59:39 +05:30
|
|
|
|
2010-04-30 01:50:57 +05:30
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
endmntent(f);
|
2005-09-13 07:59:39 +05:30
|
|
|
|
2002-10-26 15:57:42 +05:30
|
|
|
return err;
|
1999-10-19 02:52:59 +05:30
|
|
|
}
|
|
|
|
|
2014-03-31 20:09:26 +05:30
|
|
|
static int do_all_in_proc_swaps(void)
|
|
|
|
{
|
|
|
|
char *line;
|
|
|
|
int err = 0;
|
|
|
|
FILE *f = fopen_for_read("/proc/swaps");
|
|
|
|
/* Don't complain if missing */
|
|
|
|
if (f) {
|
|
|
|
while ((line = xmalloc_fgetline(f)) != NULL) {
|
|
|
|
if (line[0] == '/') {
|
|
|
|
*strchrnul(line, ' ') = '\0';
|
|
|
|
err |= swap_enable_disable(line);
|
|
|
|
}
|
|
|
|
free(line);
|
|
|
|
}
|
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2015-01-06 01:05:00 +05:30
|
|
|
#define OPTSTR_SWAPON "ae" \
|
2014-03-31 20:09:26 +05:30
|
|
|
IF_FEATURE_SWAPON_DISCARD("d::") \
|
|
|
|
IF_FEATURE_SWAPON_PRI("p:")
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int swap_on_off_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int swap_on_off_main(int argc UNUSED_PARAM, char **argv)
|
1999-10-19 02:52:59 +05:30
|
|
|
{
|
2014-03-31 20:09:26 +05:30
|
|
|
IF_FEATURE_SWAPON_PRI(char *prio;)
|
|
|
|
IF_FEATURE_SWAPON_DISCARD(char *discard = NULL;)
|
|
|
|
int ret = 0;
|
2005-09-13 07:59:39 +05:30
|
|
|
|
2011-09-21 05:29:15 +05:30
|
|
|
INIT_G();
|
|
|
|
|
2015-01-06 01:05:00 +05:30
|
|
|
getopt32(argv, do_swapoff ? "ae" : OPTSTR_SWAPON
|
2014-03-31 20:09:26 +05:30
|
|
|
IF_FEATURE_SWAPON_DISCARD(, &discard)
|
|
|
|
IF_FEATURE_SWAPON_PRI(, &prio)
|
|
|
|
);
|
2005-09-13 07:59:39 +05:30
|
|
|
|
2008-05-19 04:35:34 +05:30
|
|
|
argv += optind;
|
|
|
|
|
2014-03-31 20:09:26 +05:30
|
|
|
if (OPT_DISCARD) {
|
|
|
|
set_discard_flag(discard);
|
|
|
|
}
|
|
|
|
if (OPT_PRIO) {
|
|
|
|
set_priority_flag(prio);
|
|
|
|
}
|
2008-05-19 04:35:34 +05:30
|
|
|
|
2014-03-31 20:09:26 +05:30
|
|
|
if (OPT_ALL) {
|
|
|
|
/* swapoff -a does also /proc/swaps */
|
|
|
|
if (do_swapoff)
|
|
|
|
ret = do_all_in_proc_swaps();
|
|
|
|
ret |= do_em_all_in_fstab();
|
|
|
|
} else if (!*argv) {
|
|
|
|
/* if not -a we need at least one arg */
|
|
|
|
bb_show_usage();
|
|
|
|
}
|
|
|
|
/* Unset -a now to allow for more messages in swap_enable_disable */
|
|
|
|
option_mask32 = option_mask32 & ~OPT_a;
|
|
|
|
/* Now process devices on the commandline if any */
|
|
|
|
while (*argv) {
|
|
|
|
ret |= swap_enable_disable(*argv++);
|
|
|
|
}
|
2005-09-16 10:11:20 +05:30
|
|
|
return ret;
|
1999-10-19 02:52:59 +05:30
|
|
|
}
|