blob: c29dd30712737da213433e64c14372e8862808e3 [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 */
9
Pere Orga5bc8c002011-04-11 03:29:49 +020010//usage:#define swapon_trivial_usage
René Rhéaumee7695772015-01-05 20:35:00 +010011//usage: "[-a] [-e]" IF_FEATURE_SWAPON_DISCARD(" [-d[POL]]") IF_FEATURE_SWAPON_PRI(" [-p PRI]") " [DEVICE]"
Pere Orga5bc8c002011-04-11 03:29:49 +020012//usage:#define swapon_full_usage "\n\n"
13//usage: "Start swapping on DEVICE\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020014//usage: "\n -a Start swapping on all swap devices"
Matt Whitlock0a53b202014-03-22 19:21:01 -040015//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)"
18//usage: )
René Rhéaumee7695772015-01-05 20:35:00 +010019//usage: "\n -e Silently skip devices that do not exist"
Pere Orga5bc8c002011-04-11 03:29:49 +020020//usage: IF_FEATURE_SWAPON_PRI(
21//usage: "\n -p PRI Set swap device priority"
22//usage: )
23//usage:
24//usage:#define swapoff_trivial_usage
Mike Frysinger5f11ec32015-12-16 12:59:08 -050025//usage: "[-a] [DEVICE]"
Pere Orga5bc8c002011-04-11 03:29:49 +020026//usage:#define swapoff_full_usage "\n\n"
27//usage: "Stop swapping on DEVICE\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020028//usage: "\n -a Stop swapping on all swap devices"
29
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000030#include "libbb.h"
Eric Andersen87590061999-10-18 21:22:59 +000031#include <mntent.h>
Denys Vlasenko14bd16a2011-07-08 08:49:40 +020032#ifndef __BIONIC__
33# include <sys/swap.h>
34#endif
Eric Andersene76c3b02001-04-05 03:14:39 +000035
Natanael Copa9aff2992009-09-20 04:28:22 +020036#if ENABLE_FEATURE_MOUNT_LABEL
37# include "volume_id.h"
38#else
39# define resolve_mount_spec(fsname) ((void)0)
40#endif
41
Denys Vlasenko14bd16a2011-07-08 08:49:40 +020042#ifndef MNTTYPE_SWAP
43# define MNTTYPE_SWAP "swap"
44#endif
45
Matt Whitlock0a53b202014-03-22 19:21:01 -040046#if ENABLE_FEATURE_SWAPON_DISCARD
47#ifndef SWAP_FLAG_DISCARD
48#define SWAP_FLAG_DISCARD 0x10000
49#endif
50#ifndef SWAP_FLAG_DISCARD_ONCE
51#define SWAP_FLAG_DISCARD_ONCE 0x20000
52#endif
53#ifndef SWAP_FLAG_DISCARD_PAGES
54#define SWAP_FLAG_DISCARD_PAGES 0x40000
55#endif
56#define SWAP_FLAG_DISCARD_MASK \
57 (SWAP_FLAG_DISCARD | SWAP_FLAG_DISCARD_ONCE | SWAP_FLAG_DISCARD_PAGES)
58#endif
59
60
61#if ENABLE_FEATURE_SWAPON_DISCARD || ENABLE_FEATURE_SWAPON_PRI
Denis Vlasenkoee56e012008-05-18 23:05:34 +000062struct globals {
63 int flags;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010064} FIX_ALIASING;
Denis Vlasenkoee56e012008-05-18 23:05:34 +000065#define G (*(struct globals*)&bb_common_bufsiz1)
66#define g_flags (G.flags)
Tito Ragusaa3f326c2014-03-31 16:39:26 +020067#define save_g_flags() int save_g_flags = g_flags
68#define restore_g_flags() g_flags = save_g_flags
Denis Vlasenkoee56e012008-05-18 23:05:34 +000069#else
70#define g_flags 0
Tito Ragusaa3f326c2014-03-31 16:39:26 +020071#define save_g_flags() ((void)0)
72#define restore_g_flags() ((void)0)
Denis Vlasenkoee56e012008-05-18 23:05:34 +000073#endif
Denys Vlasenko16714242011-09-21 01:59:15 +020074#define INIT_G() do { } while (0)
Denis Vlasenkoee56e012008-05-18 23:05:34 +000075
Tito Ragusaa3f326c2014-03-31 16:39:26 +020076#define do_swapoff (applet_name[5] == 'f')
77
78/* Command line options */
79enum {
80 OPTBIT_a, /* -a all */
René Rhéaumee7695772015-01-05 20:35:00 +010081 OPTBIT_e, /* -e ifexists */
Tito Ragusaa3f326c2014-03-31 16:39:26 +020082 IF_FEATURE_SWAPON_DISCARD( OPTBIT_d ,) /* -d discard */
83 IF_FEATURE_SWAPON_PRI ( OPTBIT_p ,) /* -p priority */
84 OPT_a = 1 << OPTBIT_a,
René Rhéaumee7695772015-01-05 20:35:00 +010085 OPT_e = 1 << OPTBIT_e,
Tito Ragusaa3f326c2014-03-31 16:39:26 +020086 OPT_d = IF_FEATURE_SWAPON_DISCARD((1 << OPTBIT_d)) + 0,
87 OPT_p = IF_FEATURE_SWAPON_PRI ((1 << OPTBIT_p)) + 0,
88};
89
90#define OPT_ALL (option_mask32 & OPT_a)
91#define OPT_DISCARD (option_mask32 & OPT_d)
René Rhéaumee7695772015-01-05 20:35:00 +010092#define OPT_IFEXISTS (option_mask32 & OPT_e)
Tito Ragusaa3f326c2014-03-31 16:39:26 +020093#define OPT_PRIO (option_mask32 & OPT_p)
94
Denys Vlasenko6c634f72015-12-18 19:02:31 +010095static int swap_enable_disable(char *device)
Eric Andersen87590061999-10-18 21:22:59 +000096{
Tito Ragusaa3f326c2014-03-31 16:39:26 +020097 int err = 0;
98 int quiet = 0;
Eric Andersen97b141a2002-11-03 00:25:23 +000099
Natanael Copa9aff2992009-09-20 04:28:22 +0200100 resolve_mount_spec(&device);
Eric Andersen97b141a2002-11-03 00:25:23 +0000101
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200102 if (do_swapoff) {
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));
106 } else {
107 /* swapon */
Mike Frysinger5f11ec32015-12-16 12:59:08 -0500108 struct stat st;
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200109 err = stat(device, &st);
110 if (!err) {
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);
114 return 1;
115 }
116 }
117 err = swapon(device, g_flags);
118 /* Don't complain on swapon -a if device is already in use */
Mike Frysinger5f11ec32015-12-16 12:59:08 -0500119 quiet = (OPT_ALL && errno == EBUSY);
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200120 }
Mike Frysinger5f11ec32015-12-16 12:59:08 -0500121 /* Don't complain if file does not exist with -e option */
122 if (err && OPT_IFEXISTS && errno == ENOENT)
123 err = 0;
Eric Andersendb1df5e2002-10-26 10:27:42 +0000124 }
Mike Frysinger6943a942005-09-13 02:29:39 +0000125
Tito Ragusac9a67132014-04-01 09:51:27 +0200126 if (err && !quiet) {
127 bb_simple_perror_msg(device);
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200128 return 1;
129 }
Mike Frysinger2d5e4f62005-09-16 04:41:20 +0000130 return 0;
Eric Andersen87590061999-10-18 21:22:59 +0000131}
132
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200133#if ENABLE_FEATURE_SWAPON_DISCARD
134static void set_discard_flag(char *s)
Eric Andersen87590061999-10-18 21:22:59 +0000135{
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200136 /* 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;
139
140 if (!s) /* No optional policy value on the commandline */
141 return;
142 /* Skip prepended '=' */
143 if (*s == '=')
144 s++;
145 /* For fstab parsing: remove other appended options */
146 *strchrnul(s, ',') = '\0';
147
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;
152}
153#else
154#define set_discard_flag(s) ((void)0)
Matt Whitlockb9bbd4d2014-03-22 19:10:08 -0400155#endif
Eric Andersen87590061999-10-18 21:22:59 +0000156
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200157#if ENABLE_FEATURE_SWAPON_PRI
158static void set_priority_flag(char *s)
159{
160 unsigned prio;
Mike Frysinger6943a942005-09-13 02:29:39 +0000161
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200162 /* 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);
166 if (!errno) {
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);
171 }
172}
173#else
174#define set_priority_flag(s) ((void)0)
175#endif
176
177static int do_em_all_in_fstab(void)
178{
179 struct mntent *m;
180 int err = 0;
181 FILE *f = xfopen_for_read("/etc/fstab");
182
Lauri Kasanend2844fc2010-04-29 22:20:57 +0200183 while ((m = getmntent(f)) != NULL) {
184 if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0) {
185 /* swapon -a should ignore entries with noauto,
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200186 * but swapoff -a should process them
187 */
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 */
191 save_g_flags();
192 if (ENABLE_FEATURE_SWAPON_DISCARD) {
193 char *p = hasmntopt(m, "discard");
194 if (p) {
195 /* move to '=' or to end of string */
196 p += 7;
197 set_discard_flag(p);
Tito Ragusa8c7fcbd2013-08-08 10:21:27 +0200198 }
199 }
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200200 if (ENABLE_FEATURE_SWAPON_PRI) {
201 char *p = hasmntopt(m, "pri");
202 if (p) {
203 set_priority_flag(p + 4);
204 }
205 }
206 err |= swap_enable_disable(m->mnt_fsname);
207 restore_g_flags();
Lauri Kasanend2844fc2010-04-29 22:20:57 +0200208 }
209 }
210 }
Mike Frysinger6943a942005-09-13 02:29:39 +0000211
Lauri Kasanend2844fc2010-04-29 22:20:57 +0200212 if (ENABLE_FEATURE_CLEAN_UP)
213 endmntent(f);
Mike Frysinger6943a942005-09-13 02:29:39 +0000214
Eric Andersendb1df5e2002-10-26 10:27:42 +0000215 return err;
Eric Andersen87590061999-10-18 21:22:59 +0000216}
217
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200218static int do_all_in_proc_swaps(void)
219{
220 char *line;
221 int err = 0;
222 FILE *f = fopen_for_read("/proc/swaps");
223 /* Don't complain if missing */
224 if (f) {
225 while ((line = xmalloc_fgetline(f)) != NULL) {
226 if (line[0] == '/') {
227 *strchrnul(line, ' ') = '\0';
228 err |= swap_enable_disable(line);
229 }
230 free(line);
231 }
232 if (ENABLE_FEATURE_CLEAN_UP)
233 fclose(f);
234 }
235
236 return err;
237}
238
René Rhéaumee7695772015-01-05 20:35:00 +0100239#define OPTSTR_SWAPON "ae" \
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200240 IF_FEATURE_SWAPON_DISCARD("d::") \
241 IF_FEATURE_SWAPON_PRI("p:")
242
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000243int swap_on_off_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000244int swap_on_off_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen87590061999-10-18 21:22:59 +0000245{
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200246 IF_FEATURE_SWAPON_PRI(char *prio;)
247 IF_FEATURE_SWAPON_DISCARD(char *discard = NULL;)
248 int ret = 0;
Mike Frysinger6943a942005-09-13 02:29:39 +0000249
Denys Vlasenko16714242011-09-21 01:59:15 +0200250 INIT_G();
251
René Rhéaumee7695772015-01-05 20:35:00 +0100252 getopt32(argv, do_swapoff ? "ae" : OPTSTR_SWAPON
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200253 IF_FEATURE_SWAPON_DISCARD(, &discard)
254 IF_FEATURE_SWAPON_PRI(, &prio)
255 );
Mike Frysinger6943a942005-09-13 02:29:39 +0000256
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000257 argv += optind;
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200258
259 if (OPT_DISCARD) {
260 set_discard_flag(discard);
261 }
262 if (OPT_PRIO) {
263 set_priority_flag(prio);
264 }
265
266 if (OPT_ALL) {
267 /* swapoff -a does also /proc/swaps */
268 if (do_swapoff)
269 ret = do_all_in_proc_swaps();
270 ret |= do_em_all_in_fstab();
271 } else if (!*argv) {
272 /* if not -a we need at least one arg */
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000273 bb_show_usage();
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200274 }
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 */
278 while (*argv) {
279 ret |= swap_enable_disable(*argv++);
280 }
Mike Frysinger2d5e4f62005-09-16 04:41:20 +0000281 return ret;
Eric Andersen87590061999-10-18 21:22:59 +0000282}