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"
14 //usage: "\n -a Start swapping on all swap devices"
15 //usage: IF_FEATURE_SWAPON_PRI(
16 //usage: "\n -p PRI Set swap device priority"
19 //usage:#define swapoff_trivial_usage
20 //usage: "[-a] [DEVICE]"
21 //usage:#define swapoff_full_usage "\n\n"
22 //usage: "Stop swapping on DEVICE\n"
23 //usage: "\n -a Stop swapping on all swap devices"
29 # include <sys/swap.h>
32 #if ENABLE_FEATURE_MOUNT_LABEL
33 # include "volume_id.h"
35 # define resolve_mount_spec(fsname) ((void)0)
39 # define MNTTYPE_SWAP "swap"
42 #if ENABLE_FEATURE_SWAPON_PRI
46 #define G (*(struct globals*)&bb_common_bufsiz1)
47 #define g_flags (G.flags)
51 #define INIT_G() do { } while (0)
53 static int swap_enable_disable(char *device)
58 resolve_mount_spec(&device);
63 if (S_ISREG(st.st_mode))
64 if (st.st_blocks * (off_t)512 < st.st_size)
65 bb_error_msg("warning: swap file has holes");
68 if (applet_name[5] == 'n')
69 status = swapon(device, g_flags);
71 status = swapoff(device);
74 bb_simple_perror_msg(device);
81 static int do_em_all(void)
87 f = setmntent("/etc/fstab", "r");
89 bb_perror_msg_and_die("/etc/fstab");
92 while ((m = getmntent(f)) != NULL) {
93 if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0) {
94 /* swapon -a should ignore entries with noauto,
95 * but swapoff -a should process them */
96 if (applet_name[5] != 'n'
97 || hasmntopt(m, MNTOPT_NOAUTO) == NULL
99 err += swap_enable_disable(m->mnt_fsname);
104 if (ENABLE_FEATURE_CLEAN_UP)
110 int swap_on_off_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
111 int swap_on_off_main(int argc UNUSED_PARAM, char **argv)
117 #if !ENABLE_FEATURE_SWAPON_PRI
118 ret = getopt32(argv, "a");
120 if (applet_name[5] == 'n')
121 opt_complementary = "p+";
122 ret = getopt32(argv, (applet_name[5] == 'n') ? "ap:" : "a", &g_flags);
125 g_flags = SWAP_FLAG_PREFER |
126 ((g_flags & SWAP_FLAG_PRIO_MASK) << SWAP_FLAG_PRIO_SHIFT);
131 if (ret /* & 1: not needed */) // -a
138 /* ret = 0; redundant */
140 ret += swap_enable_disable(*argv);