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"
33 # include <sys/swap.h>
36 #if ENABLE_FEATURE_MOUNT_LABEL
37 # include "volume_id.h"
39 # define resolve_mount_spec(fsname) ((void)0)
43 # define MNTTYPE_SWAP "swap"
46 #if ENABLE_FEATURE_SWAPON_DISCARD
47 #ifndef SWAP_FLAG_DISCARD
48 #define SWAP_FLAG_DISCARD 0x10000
50 #ifndef SWAP_FLAG_DISCARD_ONCE
51 #define SWAP_FLAG_DISCARD_ONCE 0x20000
53 #ifndef SWAP_FLAG_DISCARD_PAGES
54 #define SWAP_FLAG_DISCARD_PAGES 0x40000
56 #define SWAP_FLAG_DISCARD_MASK \
57 (SWAP_FLAG_DISCARD | SWAP_FLAG_DISCARD_ONCE | SWAP_FLAG_DISCARD_PAGES)
61 #if ENABLE_FEATURE_SWAPON_DISCARD || ENABLE_FEATURE_SWAPON_PRI
65 #define G (*(struct globals*)&bb_common_bufsiz1)
66 #define g_flags (G.flags)
67 #define save_g_flags() int save_g_flags = g_flags
68 #define restore_g_flags() g_flags = save_g_flags
71 #define save_g_flags() ((void)0)
72 #define restore_g_flags() ((void)0)
74 #define INIT_G() do { } while (0)
76 #define do_swapoff (applet_name[5] == 'f')
78 /* Command line options */
80 OPTBIT_a, /* -a all */
81 OPTBIT_e, /* -e ifexists */
82 IF_FEATURE_SWAPON_DISCARD( OPTBIT_d ,) /* -d discard */
83 IF_FEATURE_SWAPON_PRI ( OPTBIT_p ,) /* -p priority */
84 OPT_a = 1 << OPTBIT_a,
85 OPT_e = 1 << OPTBIT_e,
86 OPT_d = IF_FEATURE_SWAPON_DISCARD((1 << OPTBIT_d)) + 0,
87 OPT_p = IF_FEATURE_SWAPON_PRI ((1 << OPTBIT_p)) + 0,
90 #define OPT_ALL (option_mask32 & OPT_a)
91 #define OPT_DISCARD (option_mask32 & OPT_d)
92 #define OPT_IFEXISTS (option_mask32 & OPT_e)
93 #define OPT_PRIO (option_mask32 & OPT_p)
95 static int swap_enable_disable(char *device)
100 resolve_mount_spec(&device);
103 err = swapoff(device);
104 /* Don't complain on OPT_ALL if not a swap device or if it doesn't exist */
105 quiet = (OPT_ALL && (errno == EINVAL || errno == ENOENT));
109 err = stat(device, &st);
111 if (ENABLE_DESKTOP && S_ISREG(st.st_mode)) {
112 if (st.st_blocks * (off_t)512 < st.st_size) {
113 bb_error_msg("%s: file has holes", device);
117 err = swapon(device, g_flags);
118 /* Don't complain on swapon -a if device is already in use */
119 quiet = (OPT_ALL && errno == EBUSY);
121 /* Don't complain if file does not exist with -e option */
122 if (err && OPT_IFEXISTS && errno == ENOENT)
127 bb_simple_perror_msg(device);
133 #if ENABLE_FEATURE_SWAPON_DISCARD
134 static void set_discard_flag(char *s)
136 /* Unset the flag first to allow fstab options to override */
137 /* options set on the command line */
138 g_flags = (g_flags & ~SWAP_FLAG_DISCARD_MASK) | SWAP_FLAG_DISCARD;
140 if (!s) /* No optional policy value on the commandline */
142 /* Skip prepended '=' */
145 /* For fstab parsing: remove other appended options */
146 *strchrnul(s, ',') = '\0';
148 if (strcmp(s, "once") == 0)
149 g_flags |= SWAP_FLAG_DISCARD_ONCE;
150 if (strcmp(s, "pages") == 0)
151 g_flags |= SWAP_FLAG_DISCARD_PAGES;
154 #define set_discard_flag(s) ((void)0)
157 #if ENABLE_FEATURE_SWAPON_PRI
158 static void set_priority_flag(char *s)
162 /* For fstab parsing: remove other appended options */
163 *strchrnul(s, ',') = '\0';
164 /* Max allowed 32767 (== SWAP_FLAG_PRIO_MASK) */
165 prio = bb_strtou(s, NULL, 10);
167 /* Unset the flag first to allow fstab options to override */
168 /* options set on the command line */
169 g_flags = (g_flags & ~SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER |
170 MIN(prio, SWAP_FLAG_PRIO_MASK);
174 #define set_priority_flag(s) ((void)0)
177 static int do_em_all_in_fstab(void)
181 FILE *f = xfopen_for_read("/etc/fstab");
183 while ((m = getmntent(f)) != NULL) {
184 if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0) {
185 /* swapon -a should ignore entries with noauto,
186 * but swapoff -a should process them
188 if (do_swapoff || hasmntopt(m, MNTOPT_NOAUTO) == NULL) {
189 /* each swap space might have different flags */
190 /* save global flags for the next round */
192 if (ENABLE_FEATURE_SWAPON_DISCARD) {
193 char *p = hasmntopt(m, "discard");
195 /* move to '=' or to end of string */
200 if (ENABLE_FEATURE_SWAPON_PRI) {
201 char *p = hasmntopt(m, "pri");
203 set_priority_flag(p + 4);
206 err |= swap_enable_disable(m->mnt_fsname);
212 if (ENABLE_FEATURE_CLEAN_UP)
218 static int do_all_in_proc_swaps(void)
222 FILE *f = fopen_for_read("/proc/swaps");
223 /* Don't complain if missing */
225 while ((line = xmalloc_fgetline(f)) != NULL) {
226 if (line[0] == '/') {
227 *strchrnul(line, ' ') = '\0';
228 err |= swap_enable_disable(line);
232 if (ENABLE_FEATURE_CLEAN_UP)
239 #define OPTSTR_SWAPON "ae" \
240 IF_FEATURE_SWAPON_DISCARD("d::") \
241 IF_FEATURE_SWAPON_PRI("p:")
243 int swap_on_off_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
244 int swap_on_off_main(int argc UNUSED_PARAM, char **argv)
246 IF_FEATURE_SWAPON_PRI(char *prio;)
247 IF_FEATURE_SWAPON_DISCARD(char *discard = NULL;)
252 getopt32(argv, do_swapoff ? "ae" : OPTSTR_SWAPON
253 IF_FEATURE_SWAPON_DISCARD(, &discard)
254 IF_FEATURE_SWAPON_PRI(, &prio)
260 set_discard_flag(discard);
263 set_priority_flag(prio);
267 /* swapoff -a does also /proc/swaps */
269 ret = do_all_in_proc_swaps();
270 ret |= do_em_all_in_fstab();
272 /* if not -a we need at least one arg */
275 /* Unset -a now to allow for more messages in swap_enable_disable */
276 option_mask32 = option_mask32 & ~OPT_a;
277 /* Now process devices on the commandline if any */
279 ret |= swap_enable_disable(*argv++);