1 /* vi: set sw=4 ts=4: */
3 * Mini swapon/swapoff implementation for busybox
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Licensed under GPLv2, see file LICENSE in this source tree.
10 //usage:#define swapon_trivial_usage
11 //usage: "[-a]" IF_FEATURE_SWAPON_PRI(" [-p PRI]") " [DEVICE]"
12 //usage:#define swapon_full_usage "\n\n"
13 //usage: "Start swapping on DEVICE\n"
15 //usage: "\n -a Start swapping on all swap devices"
16 //usage: IF_FEATURE_SWAPON_PRI(
17 //usage: "\n -p PRI Set swap device priority"
20 //usage:#define swapoff_trivial_usage
21 //usage: "[-a] [DEVICE]"
22 //usage:#define swapoff_full_usage "\n\n"
23 //usage: "Stop swapping on DEVICE\n"
25 //usage: "\n -a Stop swapping on all swap devices"
31 #if ENABLE_FEATURE_MOUNT_LABEL
32 # include "volume_id.h"
34 # define resolve_mount_spec(fsname) ((void)0)
37 #if ENABLE_FEATURE_SWAPON_PRI
41 #define G (*(struct globals*)&bb_common_bufsiz1)
42 #define g_flags (G.flags)
47 static int swap_enable_disable(char *device)
52 resolve_mount_spec(&device);
57 if (S_ISREG(st.st_mode))
58 if (st.st_blocks * (off_t)512 < st.st_size)
59 bb_error_msg("warning: swap file has holes");
62 if (applet_name[5] == 'n')
63 status = swapon(device, g_flags);
65 status = swapoff(device);
68 bb_simple_perror_msg(device);
75 static int do_em_all(void)
81 f = setmntent("/etc/fstab", "r");
83 bb_perror_msg_and_die("/etc/fstab");
86 while ((m = getmntent(f)) != NULL) {
87 if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0) {
88 /* swapon -a should ignore entries with noauto,
89 * but swapoff -a should process them */
90 if (applet_name[5] != 'n'
91 || hasmntopt(m, MNTOPT_NOAUTO) == NULL
93 err += swap_enable_disable(m->mnt_fsname);
98 if (ENABLE_FEATURE_CLEAN_UP)
104 int swap_on_off_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
105 int swap_on_off_main(int argc UNUSED_PARAM, char **argv)
109 #if !ENABLE_FEATURE_SWAPON_PRI
110 ret = getopt32(argv, "a");
112 opt_complementary = "p+";
113 ret = getopt32(argv, (applet_name[5] == 'n') ? "ap:" : "a", &g_flags);
116 g_flags = SWAP_FLAG_PREFER |
117 ((g_flags & SWAP_FLAG_PRIO_MASK) << SWAP_FLAG_PRIO_SHIFT);
122 if (ret /* & 1: not needed */) // -a
129 /* ret = 0; redundant */
131 ret += swap_enable_disable(*argv);