blob: dc8dd9df6ad0d0ec8ccc61ead0253548b9728a34 [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
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020010//config: bool "swapon (4.9 kb)"
Denys Vlasenkof417ffd2016-11-14 17:30:50 +010011//config: default y
12//config: select PLATFORM_LINUX
13//config: help
Denys Vlasenkof417ffd2016-11-14 17:30:50 +010014//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.
19//config:
20//config:config FEATURE_SWAPON_DISCARD
21//config: bool "Support discard option -d"
22//config: default y
23//config: depends on SWAPON
24//config: help
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.
28//config:
29//config:config FEATURE_SWAPON_PRI
30//config: bool "Support priority option -p"
31//config: default y
32//config: depends on SWAPON
33//config: help
34//config: Enable support for setting swap device priority in swapon.
35//config:
36//config:config SWAPOFF
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020037//config: bool "swapoff (4.3 kb)"
Denys Vlasenkof417ffd2016-11-14 17:30:50 +010038//config: default y
39//config: select PLATFORM_LINUX
Denys Vlasenkof417ffd2016-11-14 17:30:50 +010040
Denys Vlasenko205d48e2017-01-29 14:57:33 +010041// APPLET_ODDNAME:name main location suid_type help
42//applet:IF_SWAPON( APPLET_ODDNAME(swapon, swap_on_off, BB_DIR_SBIN, BB_SUID_DROP, swapon))
Denys Vlasenkof417ffd2016-11-14 17:30:50 +010043//applet:IF_SWAPOFF(APPLET_ODDNAME(swapoff, swap_on_off, BB_DIR_SBIN, BB_SUID_DROP, swapoff))
44
45//kbuild:lib-$(CONFIG_SWAPON) += swaponoff.o
46//kbuild:lib-$(CONFIG_SWAPOFF) += swaponoff.o
47
Pere Orga5bc8c002011-04-11 03:29:49 +020048//usage:#define swapon_trivial_usage
René Rhéaumee7695772015-01-05 20:35:00 +010049//usage: "[-a] [-e]" IF_FEATURE_SWAPON_DISCARD(" [-d[POL]]") IF_FEATURE_SWAPON_PRI(" [-p PRI]") " [DEVICE]"
Pere Orga5bc8c002011-04-11 03:29:49 +020050//usage:#define swapon_full_usage "\n\n"
51//usage: "Start swapping on DEVICE\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020052//usage: "\n -a Start swapping on all swap devices"
Matt Whitlock0a53b202014-03-22 19:21:01 -040053//usage: IF_FEATURE_SWAPON_DISCARD(
54//usage: "\n -d[POL] Discard blocks at swapon (POL=once),"
55//usage: "\n as freed (POL=pages), or both (POL omitted)"
56//usage: )
René Rhéaumee7695772015-01-05 20:35:00 +010057//usage: "\n -e Silently skip devices that do not exist"
Pere Orga5bc8c002011-04-11 03:29:49 +020058//usage: IF_FEATURE_SWAPON_PRI(
59//usage: "\n -p PRI Set swap device priority"
60//usage: )
61//usage:
62//usage:#define swapoff_trivial_usage
Mike Frysinger5f11ec32015-12-16 12:59:08 -050063//usage: "[-a] [DEVICE]"
Pere Orga5bc8c002011-04-11 03:29:49 +020064//usage:#define swapoff_full_usage "\n\n"
65//usage: "Stop swapping on DEVICE\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020066//usage: "\n -a Stop swapping on all swap devices"
67
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000068#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020069#include "common_bufsiz.h"
Eric Andersen87590061999-10-18 21:22:59 +000070#include <mntent.h>
Denys Vlasenko14bd16a2011-07-08 08:49:40 +020071#ifndef __BIONIC__
72# include <sys/swap.h>
73#endif
Eric Andersene76c3b02001-04-05 03:14:39 +000074
Natanael Copa9aff2992009-09-20 04:28:22 +020075#if ENABLE_FEATURE_MOUNT_LABEL
76# include "volume_id.h"
77#else
78# define resolve_mount_spec(fsname) ((void)0)
79#endif
80
Denys Vlasenko14bd16a2011-07-08 08:49:40 +020081#ifndef MNTTYPE_SWAP
82# define MNTTYPE_SWAP "swap"
83#endif
84
Matt Whitlock0a53b202014-03-22 19:21:01 -040085#if ENABLE_FEATURE_SWAPON_DISCARD
86#ifndef SWAP_FLAG_DISCARD
87#define SWAP_FLAG_DISCARD 0x10000
88#endif
89#ifndef SWAP_FLAG_DISCARD_ONCE
90#define SWAP_FLAG_DISCARD_ONCE 0x20000
91#endif
92#ifndef SWAP_FLAG_DISCARD_PAGES
93#define SWAP_FLAG_DISCARD_PAGES 0x40000
94#endif
95#define SWAP_FLAG_DISCARD_MASK \
96 (SWAP_FLAG_DISCARD | SWAP_FLAG_DISCARD_ONCE | SWAP_FLAG_DISCARD_PAGES)
97#endif
98
99
100#if ENABLE_FEATURE_SWAPON_DISCARD || ENABLE_FEATURE_SWAPON_PRI
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000101struct globals {
102 int flags;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100103} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200104#define G (*(struct globals*)bb_common_bufsiz1)
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000105#define g_flags (G.flags)
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200106#define save_g_flags() int save_g_flags = g_flags
107#define restore_g_flags() g_flags = save_g_flags
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000108#else
109#define g_flags 0
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200110#define save_g_flags() ((void)0)
111#define restore_g_flags() ((void)0)
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000112#endif
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200113#define INIT_G() do { setup_common_bufsiz(); } while (0)
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000114
Denys Vlasenkof417ffd2016-11-14 17:30:50 +0100115#if ENABLE_SWAPOFF
116# if ENABLE_SWAPON
117# define do_swapoff (applet_name[5] == 'f')
118# else
119# define do_swapoff 1
120# endif
121#else
122# define do_swapoff 0
123#endif
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200124
125/* Command line options */
126enum {
127 OPTBIT_a, /* -a all */
René Rhéaumee7695772015-01-05 20:35:00 +0100128 OPTBIT_e, /* -e ifexists */
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200129 IF_FEATURE_SWAPON_DISCARD( OPTBIT_d ,) /* -d discard */
130 IF_FEATURE_SWAPON_PRI ( OPTBIT_p ,) /* -p priority */
131 OPT_a = 1 << OPTBIT_a,
René Rhéaumee7695772015-01-05 20:35:00 +0100132 OPT_e = 1 << OPTBIT_e,
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200133 OPT_d = IF_FEATURE_SWAPON_DISCARD((1 << OPTBIT_d)) + 0,
134 OPT_p = IF_FEATURE_SWAPON_PRI ((1 << OPTBIT_p)) + 0,
135};
136
137#define OPT_ALL (option_mask32 & OPT_a)
138#define OPT_DISCARD (option_mask32 & OPT_d)
René Rhéaumee7695772015-01-05 20:35:00 +0100139#define OPT_IFEXISTS (option_mask32 & OPT_e)
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200140#define OPT_PRIO (option_mask32 & OPT_p)
141
Denys Vlasenko6c634f72015-12-18 19:02:31 +0100142static int swap_enable_disable(char *device)
Eric Andersen87590061999-10-18 21:22:59 +0000143{
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200144 int err = 0;
145 int quiet = 0;
Eric Andersen97b141a2002-11-03 00:25:23 +0000146
Natanael Copa9aff2992009-09-20 04:28:22 +0200147 resolve_mount_spec(&device);
Eric Andersen97b141a2002-11-03 00:25:23 +0000148
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200149 if (do_swapoff) {
150 err = swapoff(device);
151 /* Don't complain on OPT_ALL if not a swap device or if it doesn't exist */
152 quiet = (OPT_ALL && (errno == EINVAL || errno == ENOENT));
153 } else {
154 /* swapon */
Mike Frysinger5f11ec32015-12-16 12:59:08 -0500155 struct stat st;
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200156 err = stat(device, &st);
157 if (!err) {
158 if (ENABLE_DESKTOP && S_ISREG(st.st_mode)) {
159 if (st.st_blocks * (off_t)512 < st.st_size) {
160 bb_error_msg("%s: file has holes", device);
161 return 1;
162 }
163 }
164 err = swapon(device, g_flags);
165 /* Don't complain on swapon -a if device is already in use */
Mike Frysinger5f11ec32015-12-16 12:59:08 -0500166 quiet = (OPT_ALL && errno == EBUSY);
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200167 }
Mike Frysinger5f11ec32015-12-16 12:59:08 -0500168 /* Don't complain if file does not exist with -e option */
169 if (err && OPT_IFEXISTS && errno == ENOENT)
170 err = 0;
Eric Andersendb1df5e2002-10-26 10:27:42 +0000171 }
Mike Frysinger6943a942005-09-13 02:29:39 +0000172
Tito Ragusac9a67132014-04-01 09:51:27 +0200173 if (err && !quiet) {
174 bb_simple_perror_msg(device);
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200175 return 1;
176 }
Mike Frysinger2d5e4f62005-09-16 04:41:20 +0000177 return 0;
Eric Andersen87590061999-10-18 21:22:59 +0000178}
179
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200180#if ENABLE_FEATURE_SWAPON_DISCARD
181static void set_discard_flag(char *s)
Eric Andersen87590061999-10-18 21:22:59 +0000182{
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200183 /* Unset the flag first to allow fstab options to override */
184 /* options set on the command line */
185 g_flags = (g_flags & ~SWAP_FLAG_DISCARD_MASK) | SWAP_FLAG_DISCARD;
186
187 if (!s) /* No optional policy value on the commandline */
188 return;
189 /* Skip prepended '=' */
190 if (*s == '=')
191 s++;
192 /* For fstab parsing: remove other appended options */
193 *strchrnul(s, ',') = '\0';
194
195 if (strcmp(s, "once") == 0)
196 g_flags |= SWAP_FLAG_DISCARD_ONCE;
197 if (strcmp(s, "pages") == 0)
198 g_flags |= SWAP_FLAG_DISCARD_PAGES;
199}
200#else
201#define set_discard_flag(s) ((void)0)
Matt Whitlockb9bbd4d2014-03-22 19:10:08 -0400202#endif
Eric Andersen87590061999-10-18 21:22:59 +0000203
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200204#if ENABLE_FEATURE_SWAPON_PRI
205static void set_priority_flag(char *s)
206{
207 unsigned prio;
Mike Frysinger6943a942005-09-13 02:29:39 +0000208
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200209 /* For fstab parsing: remove other appended options */
210 *strchrnul(s, ',') = '\0';
211 /* Max allowed 32767 (== SWAP_FLAG_PRIO_MASK) */
212 prio = bb_strtou(s, NULL, 10);
213 if (!errno) {
214 /* Unset the flag first to allow fstab options to override */
215 /* options set on the command line */
216 g_flags = (g_flags & ~SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER |
217 MIN(prio, SWAP_FLAG_PRIO_MASK);
218 }
219}
220#else
221#define set_priority_flag(s) ((void)0)
222#endif
223
224static int do_em_all_in_fstab(void)
225{
226 struct mntent *m;
227 int err = 0;
228 FILE *f = xfopen_for_read("/etc/fstab");
229
Lauri Kasanend2844fc2010-04-29 22:20:57 +0200230 while ((m = getmntent(f)) != NULL) {
231 if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0) {
232 /* swapon -a should ignore entries with noauto,
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200233 * but swapoff -a should process them
234 */
235 if (do_swapoff || hasmntopt(m, MNTOPT_NOAUTO) == NULL) {
236 /* each swap space might have different flags */
237 /* save global flags for the next round */
238 save_g_flags();
239 if (ENABLE_FEATURE_SWAPON_DISCARD) {
240 char *p = hasmntopt(m, "discard");
241 if (p) {
242 /* move to '=' or to end of string */
243 p += 7;
244 set_discard_flag(p);
Tito Ragusa8c7fcbd2013-08-08 10:21:27 +0200245 }
246 }
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200247 if (ENABLE_FEATURE_SWAPON_PRI) {
248 char *p = hasmntopt(m, "pri");
249 if (p) {
250 set_priority_flag(p + 4);
251 }
252 }
253 err |= swap_enable_disable(m->mnt_fsname);
254 restore_g_flags();
Lauri Kasanend2844fc2010-04-29 22:20:57 +0200255 }
256 }
257 }
Mike Frysinger6943a942005-09-13 02:29:39 +0000258
Lauri Kasanend2844fc2010-04-29 22:20:57 +0200259 if (ENABLE_FEATURE_CLEAN_UP)
260 endmntent(f);
Mike Frysinger6943a942005-09-13 02:29:39 +0000261
Eric Andersendb1df5e2002-10-26 10:27:42 +0000262 return err;
Eric Andersen87590061999-10-18 21:22:59 +0000263}
264
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200265static int do_all_in_proc_swaps(void)
266{
267 char *line;
268 int err = 0;
269 FILE *f = fopen_for_read("/proc/swaps");
270 /* Don't complain if missing */
271 if (f) {
272 while ((line = xmalloc_fgetline(f)) != NULL) {
273 if (line[0] == '/') {
274 *strchrnul(line, ' ') = '\0';
275 err |= swap_enable_disable(line);
276 }
277 free(line);
278 }
279 if (ENABLE_FEATURE_CLEAN_UP)
280 fclose(f);
281 }
282
283 return err;
284}
285
René Rhéaumee7695772015-01-05 20:35:00 +0100286#define OPTSTR_SWAPON "ae" \
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200287 IF_FEATURE_SWAPON_DISCARD("d::") \
288 IF_FEATURE_SWAPON_PRI("p:")
289
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000290int swap_on_off_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000291int swap_on_off_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen87590061999-10-18 21:22:59 +0000292{
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200293 IF_FEATURE_SWAPON_PRI(char *prio;)
294 IF_FEATURE_SWAPON_DISCARD(char *discard = NULL;)
295 int ret = 0;
Mike Frysinger6943a942005-09-13 02:29:39 +0000296
Denys Vlasenko16714242011-09-21 01:59:15 +0200297 INIT_G();
298
René Rhéaumee7695772015-01-05 20:35:00 +0100299 getopt32(argv, do_swapoff ? "ae" : OPTSTR_SWAPON
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200300 IF_FEATURE_SWAPON_DISCARD(, &discard)
301 IF_FEATURE_SWAPON_PRI(, &prio)
302 );
Mike Frysinger6943a942005-09-13 02:29:39 +0000303
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000304 argv += optind;
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200305
306 if (OPT_DISCARD) {
307 set_discard_flag(discard);
308 }
309 if (OPT_PRIO) {
310 set_priority_flag(prio);
311 }
312
313 if (OPT_ALL) {
314 /* swapoff -a does also /proc/swaps */
315 if (do_swapoff)
316 ret = do_all_in_proc_swaps();
317 ret |= do_em_all_in_fstab();
318 } else if (!*argv) {
319 /* if not -a we need at least one arg */
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000320 bb_show_usage();
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200321 }
322 /* Unset -a now to allow for more messages in swap_enable_disable */
323 option_mask32 = option_mask32 & ~OPT_a;
324 /* Now process devices on the commandline if any */
325 while (*argv) {
326 ret |= swap_enable_disable(*argv++);
327 }
Mike Frysinger2d5e4f62005-09-16 04:41:20 +0000328 return ret;
Eric Andersen87590061999-10-18 21:22:59 +0000329}