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] [-e]" IF_FEATURE_SWAPON_DISCARD(" [-d[POL]]") 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_DISCARD(
16 //usage: "\n -d[POL] Discard blocks at swapon (POL=once),"
17 //usage: "\n as freed (POL=pages), or both (POL omitted)"
19 //usage: "\n -e Silently skip devices that do not exist"
20 //usage: IF_FEATURE_SWAPON_PRI(
21 //usage: "\n -p PRI Set swap device priority"
24 //usage:#define swapoff_trivial_usage
25 //usage: "[-a] [DEVICE]"
26 //usage:#define swapoff_full_usage "\n\n"
27 //usage: "Stop swapping on DEVICE\n"
28 //usage: "\n -a Stop swapping on all swap devices"
31 #include "common_bufsiz.h"
34 # include <sys/swap.h>
37 #if ENABLE_FEATURE_MOUNT_LABEL
38 # include "volume_id.h"
40 # define resolve_mount_spec(fsname) ((void)0)
44 # define MNTTYPE_SWAP "swap"
47 #if ENABLE_FEATURE_SWAPON_DISCARD
48 #ifndef SWAP_FLAG_DISCARD
49 #define SWAP_FLAG_DISCARD 0x10000
51 #ifndef SWAP_FLAG_DISCARD_ONCE
52 #define SWAP_FLAG_DISCARD_ONCE 0x20000
54 #ifndef SWAP_FLAG_DISCARD_PAGES
55 #define SWAP_FLAG_DISCARD_PAGES 0x40000
57 #define SWAP_FLAG_DISCARD_MASK \
58 (SWAP_FLAG_DISCARD | SWAP_FLAG_DISCARD_ONCE | SWAP_FLAG_DISCARD_PAGES)
62 #if ENABLE_FEATURE_SWAPON_DISCARD || ENABLE_FEATURE_SWAPON_PRI
66 #define G (*(struct globals*)bb_common_bufsiz1)
67 #define g_flags (G.flags)
68 #define save_g_flags() int save_g_flags = g_flags
69 #define restore_g_flags() g_flags = save_g_flags
72 #define save_g_flags() ((void)0)
73 #define restore_g_flags() ((void)0)
75 #define INIT_G() do { setup_common_bufsiz(); } while (0)
77 #define do_swapoff (applet_name[5] == 'f')
79 /* Command line options */
81 OPTBIT_a, /* -a all */
82 OPTBIT_e, /* -e ifexists */
83 IF_FEATURE_SWAPON_DISCARD( OPTBIT_d ,) /* -d discard */
84 IF_FEATURE_SWAPON_PRI ( OPTBIT_p ,) /* -p priority */
85 OPT_a = 1 << OPTBIT_a,
86 OPT_e = 1 << OPTBIT_e,
87 OPT_d = IF_FEATURE_SWAPON_DISCARD((1 << OPTBIT_d)) + 0,
88 OPT_p = IF_FEATURE_SWAPON_PRI ((1 << OPTBIT_p)) + 0,
91 #define OPT_ALL (option_mask32 & OPT_a)
92 #define OPT_DISCARD (option_mask32 & OPT_d)
93 #define OPT_IFEXISTS (option_mask32 & OPT_e)
94 #define OPT_PRIO (option_mask32 & OPT_p)
96 static int swap_enable_disable(char *device)
101 resolve_mount_spec(&device);
104 err = swapoff(device);
105 /* Don't complain on OPT_ALL if not a swap device or if it doesn't exist */
106 quiet = (OPT_ALL && (errno == EINVAL || errno == ENOENT));
110 err = stat(device, &st);
112 if (ENABLE_DESKTOP && S_ISREG(st.st_mode)) {
113 if (st.st_blocks * (off_t)512 < st.st_size) {
114 bb_error_msg("%s: file has holes", device);
118 err = swapon(device, g_flags);
119 /* Don't complain on swapon -a if device is already in use */
120 quiet = (OPT_ALL && errno == EBUSY);
122 /* Don't complain if file does not exist with -e option */
123 if (err && OPT_IFEXISTS && errno == ENOENT)
128 bb_simple_perror_msg(device);
134 #if ENABLE_FEATURE_SWAPON_DISCARD
135 static void set_discard_flag(char *s)
137 /* Unset the flag first to allow fstab options to override */
138 /* options set on the command line */
139 g_flags = (g_flags & ~SWAP_FLAG_DISCARD_MASK) | SWAP_FLAG_DISCARD;
141 if (!s) /* No optional policy value on the commandline */
143 /* Skip prepended '=' */
146 /* For fstab parsing: remove other appended options */
147 *strchrnul(s, ',') = '\0';
149 if (strcmp(s, "once") == 0)
150 g_flags |= SWAP_FLAG_DISCARD_ONCE;
151 if (strcmp(s, "pages") == 0)
152 g_flags |= SWAP_FLAG_DISCARD_PAGES;
155 #define set_discard_flag(s) ((void)0)
158 #if ENABLE_FEATURE_SWAPON_PRI
159 static void set_priority_flag(char *s)
163 /* For fstab parsing: remove other appended options */
164 *strchrnul(s, ',') = '\0';
165 /* Max allowed 32767 (== SWAP_FLAG_PRIO_MASK) */
166 prio = bb_strtou(s, NULL, 10);
168 /* Unset the flag first to allow fstab options to override */
169 /* options set on the command line */
170 g_flags = (g_flags & ~SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER |
171 MIN(prio, SWAP_FLAG_PRIO_MASK);
175 #define set_priority_flag(s) ((void)0)
178 static int do_em_all_in_fstab(void)
182 FILE *f = xfopen_for_read("/etc/fstab");
184 while ((m = getmntent(f)) != NULL) {
185 if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0) {
186 /* swapon -a should ignore entries with noauto,
187 * but swapoff -a should process them
189 if (do_swapoff || hasmntopt(m, MNTOPT_NOAUTO) == NULL) {
190 /* each swap space might have different flags */
191 /* save global flags for the next round */
193 if (ENABLE_FEATURE_SWAPON_DISCARD) {
194 char *p = hasmntopt(m, "discard");
196 /* move to '=' or to end of string */
201 if (ENABLE_FEATURE_SWAPON_PRI) {
202 char *p = hasmntopt(m, "pri");
204 set_priority_flag(p + 4);
207 err |= swap_enable_disable(m->mnt_fsname);
213 if (ENABLE_FEATURE_CLEAN_UP)
219 static int do_all_in_proc_swaps(void)
223 FILE *f = fopen_for_read("/proc/swaps");
224 /* Don't complain if missing */
226 while ((line = xmalloc_fgetline(f)) != NULL) {
227 if (line[0] == '/') {
228 *strchrnul(line, ' ') = '\0';
229 err |= swap_enable_disable(line);
233 if (ENABLE_FEATURE_CLEAN_UP)
240 #define OPTSTR_SWAPON "ae" \
241 IF_FEATURE_SWAPON_DISCARD("d::") \
242 IF_FEATURE_SWAPON_PRI("p:")
244 int swap_on_off_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
245 int swap_on_off_main(int argc UNUSED_PARAM, char **argv)
247 IF_FEATURE_SWAPON_PRI(char *prio;)
248 IF_FEATURE_SWAPON_DISCARD(char *discard = NULL;)
253 getopt32(argv, do_swapoff ? "ae" : OPTSTR_SWAPON
254 IF_FEATURE_SWAPON_DISCARD(, &discard)
255 IF_FEATURE_SWAPON_PRI(, &prio)
261 set_discard_flag(discard);
264 set_priority_flag(prio);
268 /* swapoff -a does also /proc/swaps */
270 ret = do_all_in_proc_swaps();
271 ret |= do_em_all_in_fstab();
273 /* if not -a we need at least one arg */
276 /* Unset -a now to allow for more messages in swap_enable_disable */
277 option_mask32 = option_mask32 & ~OPT_a;
278 /* Now process devices on the commandline if any */
280 ret |= swap_enable_disable(*argv++);