blob: b8f4e94474df17a4c487e1c05be89a7b3719a5d9 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersen87590061999-10-18 21:22:59 +00002/*
3 * Mini swapon/swapoff implementation for busybox
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersen87590061999-10-18 21:22:59 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Eric Andersen87590061999-10-18 21:22:59 +00008 */
Denys Vlasenkof417ffd2016-11-14 17:30:50 +01009//config:config SWAPON
10//config: bool "swapon"
11//config: default y
12//config: select PLATFORM_LINUX
13//config: help
14//config: This option enables the 'swapon' utility.
15//config: Once you have created some swap space using 'mkswap', you also need
16//config: to enable your swap space with the 'swapon' utility. The 'swapoff'
17//config: utility is used, typically at system shutdown, to disable any swap
18//config: space. If you are not using any swap space, you can leave this
19//config: option disabled.
20//config:
21//config:config FEATURE_SWAPON_DISCARD
22//config: bool "Support discard option -d"
23//config: default y
24//config: depends on SWAPON
25//config: help
26//config: Enable support for discarding swap area blocks at swapon and/or as
27//config: the kernel frees them. This option enables both the -d option on
28//config: 'swapon' and the 'discard' option for swap entries in /etc/fstab.
29//config:
30//config:config FEATURE_SWAPON_PRI
31//config: bool "Support priority option -p"
32//config: default y
33//config: depends on SWAPON
34//config: help
35//config: Enable support for setting swap device priority in swapon.
36//config:
37//config:config SWAPOFF
38//config: bool "swapoff"
39//config: default y
40//config: select PLATFORM_LINUX
41//config: help
42//config: This option enables the 'swapoff' utility.
43
44//applet:IF_SWAPON(APPLET_ODDNAME(swapon, swap_on_off, BB_DIR_SBIN, BB_SUID_DROP, swapon))
45//applet:IF_SWAPOFF(APPLET_ODDNAME(swapoff, swap_on_off, BB_DIR_SBIN, BB_SUID_DROP, swapoff))
46
47//kbuild:lib-$(CONFIG_SWAPON) += swaponoff.o
48//kbuild:lib-$(CONFIG_SWAPOFF) += swaponoff.o
49
Pere Orga5bc8c002011-04-11 03:29:49 +020050//usage:#define swapon_trivial_usage
René Rhéaumee7695772015-01-05 20:35:00 +010051//usage: "[-a] [-e]" IF_FEATURE_SWAPON_DISCARD(" [-d[POL]]") IF_FEATURE_SWAPON_PRI(" [-p PRI]") " [DEVICE]"
Pere Orga5bc8c002011-04-11 03:29:49 +020052//usage:#define swapon_full_usage "\n\n"
53//usage: "Start swapping on DEVICE\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020054//usage: "\n -a Start swapping on all swap devices"
Matt Whitlock0a53b202014-03-22 19:21:01 -040055//usage: IF_FEATURE_SWAPON_DISCARD(
56//usage: "\n -d[POL] Discard blocks at swapon (POL=once),"
57//usage: "\n as freed (POL=pages), or both (POL omitted)"
58//usage: )
René Rhéaumee7695772015-01-05 20:35:00 +010059//usage: "\n -e Silently skip devices that do not exist"
Pere Orga5bc8c002011-04-11 03:29:49 +020060//usage: IF_FEATURE_SWAPON_PRI(
61//usage: "\n -p PRI Set swap device priority"
62//usage: )
63//usage:
64//usage:#define swapoff_trivial_usage
Mike Frysinger5f11ec32015-12-16 12:59:08 -050065//usage: "[-a] [DEVICE]"
Pere Orga5bc8c002011-04-11 03:29:49 +020066//usage:#define swapoff_full_usage "\n\n"
67//usage: "Stop swapping on DEVICE\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020068//usage: "\n -a Stop swapping on all swap devices"
69
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000070#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020071#include "common_bufsiz.h"
Eric Andersen87590061999-10-18 21:22:59 +000072#include <mntent.h>
Denys Vlasenko14bd16a2011-07-08 08:49:40 +020073#ifndef __BIONIC__
74# include <sys/swap.h>
75#endif
Eric Andersene76c3b02001-04-05 03:14:39 +000076
Natanael Copa9aff2992009-09-20 04:28:22 +020077#if ENABLE_FEATURE_MOUNT_LABEL
78# include "volume_id.h"
79#else
80# define resolve_mount_spec(fsname) ((void)0)
81#endif
82
Denys Vlasenko14bd16a2011-07-08 08:49:40 +020083#ifndef MNTTYPE_SWAP
84# define MNTTYPE_SWAP "swap"
85#endif
86
Matt Whitlock0a53b202014-03-22 19:21:01 -040087#if ENABLE_FEATURE_SWAPON_DISCARD
88#ifndef SWAP_FLAG_DISCARD
89#define SWAP_FLAG_DISCARD 0x10000
90#endif
91#ifndef SWAP_FLAG_DISCARD_ONCE
92#define SWAP_FLAG_DISCARD_ONCE 0x20000
93#endif
94#ifndef SWAP_FLAG_DISCARD_PAGES
95#define SWAP_FLAG_DISCARD_PAGES 0x40000
96#endif
97#define SWAP_FLAG_DISCARD_MASK \
98 (SWAP_FLAG_DISCARD | SWAP_FLAG_DISCARD_ONCE | SWAP_FLAG_DISCARD_PAGES)
99#endif
100
101
102#if ENABLE_FEATURE_SWAPON_DISCARD || ENABLE_FEATURE_SWAPON_PRI
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000103struct globals {
104 int flags;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100105} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200106#define G (*(struct globals*)bb_common_bufsiz1)
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000107#define g_flags (G.flags)
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200108#define save_g_flags() int save_g_flags = g_flags
109#define restore_g_flags() g_flags = save_g_flags
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000110#else
111#define g_flags 0
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200112#define save_g_flags() ((void)0)
113#define restore_g_flags() ((void)0)
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000114#endif
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200115#define INIT_G() do { setup_common_bufsiz(); } while (0)
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000116
Denys Vlasenkof417ffd2016-11-14 17:30:50 +0100117#if ENABLE_SWAPOFF
118# if ENABLE_SWAPON
119# define do_swapoff (applet_name[5] == 'f')
120# else
121# define do_swapoff 1
122# endif
123#else
124# define do_swapoff 0
125#endif
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200126
127/* Command line options */
128enum {
129 OPTBIT_a, /* -a all */
René Rhéaumee7695772015-01-05 20:35:00 +0100130 OPTBIT_e, /* -e ifexists */
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200131 IF_FEATURE_SWAPON_DISCARD( OPTBIT_d ,) /* -d discard */
132 IF_FEATURE_SWAPON_PRI ( OPTBIT_p ,) /* -p priority */
133 OPT_a = 1 << OPTBIT_a,
René Rhéaumee7695772015-01-05 20:35:00 +0100134 OPT_e = 1 << OPTBIT_e,
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200135 OPT_d = IF_FEATURE_SWAPON_DISCARD((1 << OPTBIT_d)) + 0,
136 OPT_p = IF_FEATURE_SWAPON_PRI ((1 << OPTBIT_p)) + 0,
137};
138
139#define OPT_ALL (option_mask32 & OPT_a)
140#define OPT_DISCARD (option_mask32 & OPT_d)
René Rhéaumee7695772015-01-05 20:35:00 +0100141#define OPT_IFEXISTS (option_mask32 & OPT_e)
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200142#define OPT_PRIO (option_mask32 & OPT_p)
143
Denys Vlasenko6c634f72015-12-18 19:02:31 +0100144static int swap_enable_disable(char *device)
Eric Andersen87590061999-10-18 21:22:59 +0000145{
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200146 int err = 0;
147 int quiet = 0;
Eric Andersen97b141a2002-11-03 00:25:23 +0000148
Natanael Copa9aff2992009-09-20 04:28:22 +0200149 resolve_mount_spec(&device);
Eric Andersen97b141a2002-11-03 00:25:23 +0000150
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200151 if (do_swapoff) {
152 err = swapoff(device);
153 /* Don't complain on OPT_ALL if not a swap device or if it doesn't exist */
154 quiet = (OPT_ALL && (errno == EINVAL || errno == ENOENT));
155 } else {
156 /* swapon */
Mike Frysinger5f11ec32015-12-16 12:59:08 -0500157 struct stat st;
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200158 err = stat(device, &st);
159 if (!err) {
160 if (ENABLE_DESKTOP && S_ISREG(st.st_mode)) {
161 if (st.st_blocks * (off_t)512 < st.st_size) {
162 bb_error_msg("%s: file has holes", device);
163 return 1;
164 }
165 }
166 err = swapon(device, g_flags);
167 /* Don't complain on swapon -a if device is already in use */
Mike Frysinger5f11ec32015-12-16 12:59:08 -0500168 quiet = (OPT_ALL && errno == EBUSY);
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200169 }
Mike Frysinger5f11ec32015-12-16 12:59:08 -0500170 /* Don't complain if file does not exist with -e option */
171 if (err && OPT_IFEXISTS && errno == ENOENT)
172 err = 0;
Eric Andersendb1df5e2002-10-26 10:27:42 +0000173 }
Mike Frysinger6943a942005-09-13 02:29:39 +0000174
Tito Ragusac9a67132014-04-01 09:51:27 +0200175 if (err && !quiet) {
176 bb_simple_perror_msg(device);
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200177 return 1;
178 }
Mike Frysinger2d5e4f62005-09-16 04:41:20 +0000179 return 0;
Eric Andersen87590061999-10-18 21:22:59 +0000180}
181
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200182#if ENABLE_FEATURE_SWAPON_DISCARD
183static void set_discard_flag(char *s)
Eric Andersen87590061999-10-18 21:22:59 +0000184{
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200185 /* Unset the flag first to allow fstab options to override */
186 /* options set on the command line */
187 g_flags = (g_flags & ~SWAP_FLAG_DISCARD_MASK) | SWAP_FLAG_DISCARD;
188
189 if (!s) /* No optional policy value on the commandline */
190 return;
191 /* Skip prepended '=' */
192 if (*s == '=')
193 s++;
194 /* For fstab parsing: remove other appended options */
195 *strchrnul(s, ',') = '\0';
196
197 if (strcmp(s, "once") == 0)
198 g_flags |= SWAP_FLAG_DISCARD_ONCE;
199 if (strcmp(s, "pages") == 0)
200 g_flags |= SWAP_FLAG_DISCARD_PAGES;
201}
202#else
203#define set_discard_flag(s) ((void)0)
Matt Whitlockb9bbd4d2014-03-22 19:10:08 -0400204#endif
Eric Andersen87590061999-10-18 21:22:59 +0000205
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200206#if ENABLE_FEATURE_SWAPON_PRI
207static void set_priority_flag(char *s)
208{
209 unsigned prio;
Mike Frysinger6943a942005-09-13 02:29:39 +0000210
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200211 /* For fstab parsing: remove other appended options */
212 *strchrnul(s, ',') = '\0';
213 /* Max allowed 32767 (== SWAP_FLAG_PRIO_MASK) */
214 prio = bb_strtou(s, NULL, 10);
215 if (!errno) {
216 /* Unset the flag first to allow fstab options to override */
217 /* options set on the command line */
218 g_flags = (g_flags & ~SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER |
219 MIN(prio, SWAP_FLAG_PRIO_MASK);
220 }
221}
222#else
223#define set_priority_flag(s) ((void)0)
224#endif
225
226static int do_em_all_in_fstab(void)
227{
228 struct mntent *m;
229 int err = 0;
230 FILE *f = xfopen_for_read("/etc/fstab");
231
Lauri Kasanend2844fc2010-04-29 22:20:57 +0200232 while ((m = getmntent(f)) != NULL) {
233 if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0) {
234 /* swapon -a should ignore entries with noauto,
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200235 * but swapoff -a should process them
236 */
237 if (do_swapoff || hasmntopt(m, MNTOPT_NOAUTO) == NULL) {
238 /* each swap space might have different flags */
239 /* save global flags for the next round */
240 save_g_flags();
241 if (ENABLE_FEATURE_SWAPON_DISCARD) {
242 char *p = hasmntopt(m, "discard");
243 if (p) {
244 /* move to '=' or to end of string */
245 p += 7;
246 set_discard_flag(p);
Tito Ragusa8c7fcbd2013-08-08 10:21:27 +0200247 }
248 }
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200249 if (ENABLE_FEATURE_SWAPON_PRI) {
250 char *p = hasmntopt(m, "pri");
251 if (p) {
252 set_priority_flag(p + 4);
253 }
254 }
255 err |= swap_enable_disable(m->mnt_fsname);
256 restore_g_flags();
Lauri Kasanend2844fc2010-04-29 22:20:57 +0200257 }
258 }
259 }
Mike Frysinger6943a942005-09-13 02:29:39 +0000260
Lauri Kasanend2844fc2010-04-29 22:20:57 +0200261 if (ENABLE_FEATURE_CLEAN_UP)
262 endmntent(f);
Mike Frysinger6943a942005-09-13 02:29:39 +0000263
Eric Andersendb1df5e2002-10-26 10:27:42 +0000264 return err;
Eric Andersen87590061999-10-18 21:22:59 +0000265}
266
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200267static int do_all_in_proc_swaps(void)
268{
269 char *line;
270 int err = 0;
271 FILE *f = fopen_for_read("/proc/swaps");
272 /* Don't complain if missing */
273 if (f) {
274 while ((line = xmalloc_fgetline(f)) != NULL) {
275 if (line[0] == '/') {
276 *strchrnul(line, ' ') = '\0';
277 err |= swap_enable_disable(line);
278 }
279 free(line);
280 }
281 if (ENABLE_FEATURE_CLEAN_UP)
282 fclose(f);
283 }
284
285 return err;
286}
287
René Rhéaumee7695772015-01-05 20:35:00 +0100288#define OPTSTR_SWAPON "ae" \
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200289 IF_FEATURE_SWAPON_DISCARD("d::") \
290 IF_FEATURE_SWAPON_PRI("p:")
291
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000292int swap_on_off_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000293int swap_on_off_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen87590061999-10-18 21:22:59 +0000294{
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200295 IF_FEATURE_SWAPON_PRI(char *prio;)
296 IF_FEATURE_SWAPON_DISCARD(char *discard = NULL;)
297 int ret = 0;
Mike Frysinger6943a942005-09-13 02:29:39 +0000298
Denys Vlasenko16714242011-09-21 01:59:15 +0200299 INIT_G();
300
René Rhéaumee7695772015-01-05 20:35:00 +0100301 getopt32(argv, do_swapoff ? "ae" : OPTSTR_SWAPON
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200302 IF_FEATURE_SWAPON_DISCARD(, &discard)
303 IF_FEATURE_SWAPON_PRI(, &prio)
304 );
Mike Frysinger6943a942005-09-13 02:29:39 +0000305
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000306 argv += optind;
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200307
308 if (OPT_DISCARD) {
309 set_discard_flag(discard);
310 }
311 if (OPT_PRIO) {
312 set_priority_flag(prio);
313 }
314
315 if (OPT_ALL) {
316 /* swapoff -a does also /proc/swaps */
317 if (do_swapoff)
318 ret = do_all_in_proc_swaps();
319 ret |= do_em_all_in_fstab();
320 } else if (!*argv) {
321 /* if not -a we need at least one arg */
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000322 bb_show_usage();
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200323 }
324 /* Unset -a now to allow for more messages in swap_enable_disable */
325 option_mask32 = option_mask32 & ~OPT_a;
326 /* Now process devices on the commandline if any */
327 while (*argv) {
328 ret |= swap_enable_disable(*argv++);
329 }
Mike Frysinger2d5e4f62005-09-16 04:41:20 +0000330 return ret;
Eric Andersen87590061999-10-18 21:22:59 +0000331}