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 //config: bool "swapon (15 kb)"
12 //config: select PLATFORM_LINUX
14 //config: Once you have created some swap space using 'mkswap', you also need
15 //config: to enable your swap space with the 'swapon' utility. The 'swapoff'
16 //config: utility is used, typically at system shutdown, to disable any swap
17 //config: space. If you are not using any swap space, you can leave this
18 //config: option disabled.
20 //config:config FEATURE_SWAPON_DISCARD
21 //config: bool "Support discard option -d"
23 //config: depends on SWAPON
25 //config: Enable support for discarding swap area blocks at swapon and/or as
26 //config: the kernel frees them. This option enables both the -d option on
27 //config: 'swapon' and the 'discard' option for swap entries in /etc/fstab.
29 //config:config FEATURE_SWAPON_PRI
30 //config: bool "Support priority option -p"
32 //config: depends on SWAPON
34 //config: Enable support for setting swap device priority in swapon.
36 //config:config SWAPOFF
37 //config: bool "swapoff (14 kb)"
39 //config: select PLATFORM_LINUX
41 //config:config FEATURE_SWAPONOFF_LABEL
42 //config: bool "Support specifying devices by label or UUID"
44 //config: depends on SWAPON || SWAPOFF
45 //config: select VOLUMEID
47 //config: This allows for specifying a device by label or uuid, rather than by
48 //config: name. This feature utilizes the same functionality as blkid/findfs.
50 // APPLET_ODDNAME:name main location suid_type help
51 //applet:IF_SWAPON( APPLET_ODDNAME(swapon, swap_on_off, BB_DIR_SBIN, BB_SUID_DROP, swapon))
52 //applet:IF_SWAPOFF(APPLET_ODDNAME(swapoff, swap_on_off, BB_DIR_SBIN, BB_SUID_DROP, swapoff))
54 //kbuild:lib-$(CONFIG_SWAPON) += swaponoff.o
55 //kbuild:lib-$(CONFIG_SWAPOFF) += swaponoff.o
57 //usage:#define swapon_trivial_usage
58 //usage: "[-a] [-e]" IF_FEATURE_SWAPON_DISCARD(" [-d[POL]]") IF_FEATURE_SWAPON_PRI(" [-p PRI]") " [DEVICE]"
59 //usage:#define swapon_full_usage "\n\n"
60 //usage: "Start swapping on DEVICE\n"
61 //usage: "\n -a Start swapping on all swap devices"
62 //usage: IF_FEATURE_SWAPON_DISCARD(
63 //usage: "\n -d[POL] Discard blocks at swapon (POL=once),"
64 //usage: "\n as freed (POL=pages), or both (POL omitted)"
66 //usage: "\n -e Silently skip devices that do not exist"
67 //usage: IF_FEATURE_SWAPON_PRI(
68 //usage: "\n -p PRI Set swap device priority"
71 //usage:#define swapoff_trivial_usage
72 //usage: "[-a] [DEVICE]"
73 //usage:#define swapoff_full_usage "\n\n"
74 //usage: "Stop swapping on DEVICE\n"
75 //usage: "\n -a Stop swapping on all swap devices"
78 #include "common_bufsiz.h"
81 # include <sys/swap.h>
84 #if ENABLE_FEATURE_SWAPONOFF_LABEL
85 # include "volume_id.h"
87 # define resolve_mount_spec(fsname) ((void)0)
91 # define MNTTYPE_SWAP "swap"
94 #if ENABLE_FEATURE_SWAPON_DISCARD
95 #ifndef SWAP_FLAG_DISCARD
96 #define SWAP_FLAG_DISCARD 0x10000
98 #ifndef SWAP_FLAG_DISCARD_ONCE
99 #define SWAP_FLAG_DISCARD_ONCE 0x20000
101 #ifndef SWAP_FLAG_DISCARD_PAGES
102 #define SWAP_FLAG_DISCARD_PAGES 0x40000
104 #define SWAP_FLAG_DISCARD_MASK \
105 (SWAP_FLAG_DISCARD | SWAP_FLAG_DISCARD_ONCE | SWAP_FLAG_DISCARD_PAGES)
109 #if ENABLE_FEATURE_SWAPON_DISCARD || ENABLE_FEATURE_SWAPON_PRI
113 #define G (*(struct globals*)bb_common_bufsiz1)
114 #define g_flags (G.flags)
115 #define save_g_flags() int save_g_flags = g_flags
116 #define restore_g_flags() g_flags = save_g_flags
119 #define save_g_flags() ((void)0)
120 #define restore_g_flags() ((void)0)
122 #define INIT_G() do { setup_common_bufsiz(); } while (0)
126 # define do_swapoff (applet_name[5] == 'f')
128 # define do_swapoff 1
131 # define do_swapoff 0
134 /* Command line options */
136 OPTBIT_a, /* -a all */
137 OPTBIT_e, /* -e ifexists */
138 IF_FEATURE_SWAPON_DISCARD( OPTBIT_d ,) /* -d discard */
139 IF_FEATURE_SWAPON_PRI ( OPTBIT_p ,) /* -p priority */
140 OPT_a = 1 << OPTBIT_a,
141 OPT_e = 1 << OPTBIT_e,
142 OPT_d = IF_FEATURE_SWAPON_DISCARD((1 << OPTBIT_d)) + 0,
143 OPT_p = IF_FEATURE_SWAPON_PRI ((1 << OPTBIT_p)) + 0,
146 #define OPT_ALL (option_mask32 & OPT_a)
147 #define OPT_DISCARD (option_mask32 & OPT_d)
148 #define OPT_IFEXISTS (option_mask32 & OPT_e)
149 #define OPT_PRIO (option_mask32 & OPT_p)
151 static int swap_enable_disable(char *device)
156 resolve_mount_spec(&device);
159 err = swapoff(device);
160 /* Don't complain on OPT_ALL if not a swap device or if it doesn't exist */
161 quiet = (OPT_ALL && (errno == EINVAL || errno == ENOENT));
165 err = stat(device, &st);
167 if (ENABLE_DESKTOP && S_ISREG(st.st_mode)) {
168 if (st.st_blocks * (off_t)512 < st.st_size) {
169 bb_error_msg("%s: file has holes", device);
173 err = swapon(device, g_flags);
174 /* Don't complain on swapon -a if device is already in use */
175 quiet = (OPT_ALL && errno == EBUSY);
177 /* Don't complain if file does not exist with -e option */
178 if (err && OPT_IFEXISTS && errno == ENOENT)
183 bb_simple_perror_msg(device);
189 #if ENABLE_FEATURE_SWAPON_DISCARD
190 static void set_discard_flag(char *s)
192 /* Unset the flag first to allow fstab options to override */
193 /* options set on the command line */
194 g_flags = (g_flags & ~SWAP_FLAG_DISCARD_MASK) | SWAP_FLAG_DISCARD;
196 if (!s) /* No optional policy value on the commandline */
198 /* Skip prepended '=' */
201 /* For fstab parsing: remove other appended options */
202 *strchrnul(s, ',') = '\0';
204 if (strcmp(s, "once") == 0)
205 g_flags |= SWAP_FLAG_DISCARD_ONCE;
206 if (strcmp(s, "pages") == 0)
207 g_flags |= SWAP_FLAG_DISCARD_PAGES;
210 #define set_discard_flag(s) ((void)0)
213 #if ENABLE_FEATURE_SWAPON_PRI
214 static void set_priority_flag(char *s)
218 /* For fstab parsing: remove other appended options */
219 *strchrnul(s, ',') = '\0';
220 /* Max allowed 32767 (== SWAP_FLAG_PRIO_MASK) */
221 prio = bb_strtou(s, NULL, 10);
223 /* Unset the flag first to allow fstab options to override */
224 /* options set on the command line */
225 g_flags = (g_flags & ~SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER |
226 MIN(prio, SWAP_FLAG_PRIO_MASK);
230 #define set_priority_flag(s) ((void)0)
233 static int do_em_all_in_fstab(void)
237 FILE *f = xfopen_for_read("/etc/fstab");
239 while ((m = getmntent(f)) != NULL) {
240 if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0) {
241 /* swapon -a should ignore entries with noauto,
242 * but swapoff -a should process them
244 if (do_swapoff || hasmntopt(m, MNTOPT_NOAUTO) == NULL) {
245 /* each swap space might have different flags */
246 /* save global flags for the next round */
248 if (ENABLE_FEATURE_SWAPON_DISCARD) {
249 char *p = hasmntopt(m, "discard");
251 /* move to '=' or to end of string */
256 if (ENABLE_FEATURE_SWAPON_PRI) {
257 char *p = hasmntopt(m, "pri");
259 set_priority_flag(p + 4);
262 err |= swap_enable_disable(m->mnt_fsname);
268 if (ENABLE_FEATURE_CLEAN_UP)
274 static int do_all_in_proc_swaps(void)
278 FILE *f = fopen_for_read("/proc/swaps");
279 /* Don't complain if missing */
281 while ((line = xmalloc_fgetline(f)) != NULL) {
282 if (line[0] == '/') {
283 *strchrnul(line, ' ') = '\0';
284 err |= swap_enable_disable(line);
288 if (ENABLE_FEATURE_CLEAN_UP)
295 #define OPTSTR_SWAPON "ae" \
296 IF_FEATURE_SWAPON_DISCARD("d::") \
297 IF_FEATURE_SWAPON_PRI("p:")
299 int swap_on_off_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
300 int swap_on_off_main(int argc UNUSED_PARAM, char **argv)
302 IF_FEATURE_SWAPON_PRI(char *prio;)
303 IF_FEATURE_SWAPON_DISCARD(char *discard = NULL;)
308 getopt32(argv, do_swapoff ? "ae" : OPTSTR_SWAPON
309 IF_FEATURE_SWAPON_DISCARD(, &discard)
310 IF_FEATURE_SWAPON_PRI(, &prio)
316 set_discard_flag(discard);
319 set_priority_flag(prio);
323 /* swapoff -a does also /proc/swaps */
325 ret = do_all_in_proc_swaps();
326 ret |= do_em_all_in_fstab();
328 /* if not -a we need at least one arg */
331 /* Unset -a now to allow for more messages in swap_enable_disable */
332 option_mask32 = option_mask32 & ~OPT_a;
333 /* Now process devices on the commandline if any */
335 ret |= swap_enable_disable(*argv++);