1 /* vi: set sw=4 ts=4: */
2 /*
3 * Mini swapon/swapoff implementation for busybox
4 *
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6 *
7 * Licensed under GPLv2, see file LICENSE in this source tree.
8 */
9 //config:config SWAPON
10 //config: bool "swapon (15 kb)"
11 //config: default y
12 //config: help
13 //config: Once you have created some swap space using 'mkswap', you also need
14 //config: to enable your swap space with the 'swapon' utility. The 'swapoff'
15 //config: utility is used, typically at system shutdown, to disable any swap
16 //config: space. If you are not using any swap space, you can leave this
17 //config: option disabled.
18 //config:
19 //config:config FEATURE_SWAPON_DISCARD
20 //config: bool "Support discard option -d"
21 //config: default y
22 //config: depends on SWAPON
23 //config: help
24 //config: Enable support for discarding swap area blocks at swapon and/or as
25 //config: the kernel frees them. This option enables both the -d option on
26 //config: 'swapon' and the 'discard' option for swap entries in /etc/fstab.
27 //config:
28 //config:config FEATURE_SWAPON_PRI
29 //config: bool "Support priority option -p"
30 //config: default y
31 //config: depends on SWAPON
32 //config: help
33 //config: Enable support for setting swap device priority in swapon.
34 //config:
35 //config:config SWAPOFF
36 //config: bool "swapoff (14 kb)"
37 //config: default y
38 //config:
39 //config:config FEATURE_SWAPONOFF_LABEL
40 //config: bool "Support specifying devices by label or UUID"
41 //config: default y
42 //config: depends on SWAPON || SWAPOFF
43 //config: select VOLUMEID
44 //config: help
45 //config: This allows for specifying a device by label or uuid, rather than by
46 //config: name. This feature utilizes the same functionality as blkid/findfs.
47
48 // APPLET_ODDNAME:name main location suid_type help
49 //applet:IF_SWAPON( APPLET_ODDNAME(swapon, swap_on_off, BB_DIR_SBIN, BB_SUID_DROP, swapon))
50 //applet:IF_SWAPOFF(APPLET_ODDNAME(swapoff, swap_on_off, BB_DIR_SBIN, BB_SUID_DROP, swapoff))
51
52 //kbuild:lib-$(CONFIG_SWAPON) += swaponoff.o
53 //kbuild:lib-$(CONFIG_SWAPOFF) += swaponoff.o
54
55 //usage:#define swapon_trivial_usage
56 //usage: "[-a] [-e]" IF_FEATURE_SWAPON_DISCARD(" [-d[POL]]") IF_FEATURE_SWAPON_PRI(" [-p PRI]") " [DEVICE]"
57 //usage:#define swapon_full_usage "\n\n"
58 //usage: "Start swapping on DEVICE\n"
59 //usage: "\n -a Start swapping on all swap devices"
60 //usage: IF_FEATURE_SWAPON_DISCARD(
61 //usage: "\n -d[POL] Discard blocks at swapon (POL=once),"
62 //usage: "\n as freed (POL=pages), or both (POL omitted)"
63 //usage: )
64 //usage: "\n -e Silently skip devices that do not exist"
65 //usage: IF_FEATURE_SWAPON_PRI(
66 //usage: "\n -p PRI Set swap device priority"
67 //usage: )
68 //usage:
69 //usage:#define swapoff_trivial_usage
70 //usage: "[-a] [DEVICE]"
71 //usage:#define swapoff_full_usage "\n\n"
72 //usage: "Stop swapping on DEVICE\n"
73 //usage: "\n -a Stop swapping on all swap devices"
74
75 #include "libbb.h"
76 #include "common_bufsiz.h"
77 #include <mntent.h>
78 #ifndef __BIONIC__
79 # include <sys/swap.h>
80 #endif
81
82 #if ENABLE_FEATURE_SWAPONOFF_LABEL
83 # include "volume_id.h"
84 #else
85 # define resolve_mount_spec(fsname) ((void)0)
86 #endif
87
88 #ifndef MNTTYPE_SWAP
89 # define MNTTYPE_SWAP "swap"
90 #endif
91
92 #if ENABLE_FEATURE_SWAPON_DISCARD
93 #ifndef SWAP_FLAG_DISCARD
94 #define SWAP_FLAG_DISCARD 0x10000
95 #endif
96 #ifndef SWAP_FLAG_DISCARD_ONCE
97 #define SWAP_FLAG_DISCARD_ONCE 0x20000
98 #endif
99 #ifndef SWAP_FLAG_DISCARD_PAGES
100 #define SWAP_FLAG_DISCARD_PAGES 0x40000
101 #endif
102 #define SWAP_FLAG_DISCARD_MASK \
103 (SWAP_FLAG_DISCARD | SWAP_FLAG_DISCARD_ONCE | SWAP_FLAG_DISCARD_PAGES)
104 #endif
105
106
107 #if ENABLE_FEATURE_SWAPON_DISCARD || ENABLE_FEATURE_SWAPON_PRI
108 struct globals {
109 int flags;
110 } FIX_ALIASING;
111 #define G (*(struct globals*)bb_common_bufsiz1)
112 #define g_flags (G.flags)
113 #define save_g_flags() int save_g_flags = g_flags
114 #define restore_g_flags() g_flags = save_g_flags
115 #else
116 #define g_flags 0
117 #define save_g_flags() ((void)0)
118 #define restore_g_flags() ((void)0)
119 #endif
120 #define INIT_G() do { setup_common_bufsiz(); } while (0)
121
122 #if ENABLE_SWAPOFF
123 # if ENABLE_SWAPON
124 # define do_swapoff (applet_name[5] == 'f')
125 # else
126 # define do_swapoff 1
127 # endif
128 #else
129 # define do_swapoff 0
130 #endif
131
132 /* Command line options */
133 enum {
134 OPTBIT_a, /* -a all */
135 OPTBIT_e, /* -e ifexists */
136 IF_FEATURE_SWAPON_DISCARD( OPTBIT_d ,) /* -d discard */
137 IF_FEATURE_SWAPON_PRI ( OPTBIT_p ,) /* -p priority */
138 OPT_a = 1 << OPTBIT_a,
139 OPT_e = 1 << OPTBIT_e,
140 OPT_d = IF_FEATURE_SWAPON_DISCARD((1 << OPTBIT_d)) + 0,
141 OPT_p = IF_FEATURE_SWAPON_PRI ((1 << OPTBIT_p)) + 0,
142 };
143
144 #define OPT_ALL (option_mask32 & OPT_a)
145 #define OPT_DISCARD (option_mask32 & OPT_d)
146 #define OPT_IFEXISTS (option_mask32 & OPT_e)
147 #define OPT_PRIO (option_mask32 & OPT_p)
148
swap_enable_disable(char * device)149 static int swap_enable_disable(char *device)
150 {
151 int err = 0;
152 int quiet = 0;
153
154 resolve_mount_spec(&device);
155
156 if (do_swapoff) {
157 err = swapoff(device);
158 /* Don't complain on OPT_ALL if not a swap device or if it doesn't exist */
159 quiet = (OPT_ALL && (errno == EINVAL || errno == ENOENT));
160 } else {
161 /* swapon */
162 struct stat st;
163 err = stat(device, &st);
164 if (!err) {
165 if (ENABLE_DESKTOP && S_ISREG(st.st_mode)) {
166 if (st.st_blocks * (off_t)512 < st.st_size) {
167 bb_error_msg("%s: file has holes", device);
168 return 1;
169 }
170 }
171 err = swapon(device, g_flags);
172 /* Don't complain on swapon -a if device is already in use */
173 quiet = (OPT_ALL && errno == EBUSY);
174 }
175 /* Don't complain if file does not exist with -e option */
176 if (err && OPT_IFEXISTS && errno == ENOENT)
177 err = 0;
178 }
179
180 if (err && !quiet) {
181 bb_simple_perror_msg(device);
182 return 1;
183 }
184 return 0;
185 }
186
187 #if ENABLE_FEATURE_SWAPON_DISCARD
set_discard_flag(char * s)188 static void set_discard_flag(char *s)
189 {
190 /* Unset the flag first to allow fstab options to override */
191 /* options set on the command line */
192 g_flags = (g_flags & ~SWAP_FLAG_DISCARD_MASK) | SWAP_FLAG_DISCARD;
193
194 if (!s) /* No optional policy value on the commandline */
195 return;
196 /* Skip prepended '=' */
197 if (*s == '=')
198 s++;
199 /* For fstab parsing: remove other appended options */
200 *strchrnul(s, ',') = '\0';
201
202 if (strcmp(s, "once") == 0)
203 g_flags |= SWAP_FLAG_DISCARD_ONCE;
204 if (strcmp(s, "pages") == 0)
205 g_flags |= SWAP_FLAG_DISCARD_PAGES;
206 }
207 #else
208 #define set_discard_flag(s) ((void)0)
209 #endif
210
211 #if ENABLE_FEATURE_SWAPON_PRI
set_priority_flag(char * s)212 static void set_priority_flag(char *s)
213 {
214 unsigned prio;
215
216 /* For fstab parsing: remove other appended options */
217 *strchrnul(s, ',') = '\0';
218 /* Max allowed 32767 (== SWAP_FLAG_PRIO_MASK) */
219 prio = bb_strtou(s, NULL, 10);
220 if (!errno) {
221 /* Unset the flag first to allow fstab options to override */
222 /* options set on the command line */
223 g_flags = (g_flags & ~SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER |
224 MIN(prio, SWAP_FLAG_PRIO_MASK);
225 }
226 }
227 #else
228 #define set_priority_flag(s) ((void)0)
229 #endif
230
do_em_all_in_fstab(void)231 static int do_em_all_in_fstab(void)
232 {
233 struct mntent *m;
234 int err = 0;
235 FILE *f = xfopen_for_read("/etc/fstab");
236
237 while ((m = getmntent(f)) != NULL) {
238 if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0) {
239 /* swapon -a should ignore entries with noauto,
240 * but swapoff -a should process them
241 */
242 if (do_swapoff || hasmntopt(m, MNTOPT_NOAUTO) == NULL) {
243 /* each swap space might have different flags */
244 /* save global flags for the next round */
245 save_g_flags();
246 if (ENABLE_FEATURE_SWAPON_DISCARD) {
247 char *p = hasmntopt(m, "discard");
248 if (p) {
249 /* move to '=' or to end of string */
250 p += 7;
251 set_discard_flag(p);
252 }
253 }
254 if (ENABLE_FEATURE_SWAPON_PRI) {
255 char *p = hasmntopt(m, "pri");
256 if (p) {
257 set_priority_flag(p + 4);
258 }
259 }
260 err |= swap_enable_disable(m->mnt_fsname);
261 restore_g_flags();
262 }
263 }
264 }
265
266 if (ENABLE_FEATURE_CLEAN_UP)
267 endmntent(f);
268
269 return err;
270 }
271
do_all_in_proc_swaps(void)272 static int do_all_in_proc_swaps(void)
273 {
274 char *line;
275 int err = 0;
276 FILE *f = fopen_for_read("/proc/swaps");
277 /* Don't complain if missing */
278 if (f) {
279 while ((line = xmalloc_fgetline(f)) != NULL) {
280 if (line[0] == '/') {
281 *strchrnul(line, ' ') = '\0';
282 err |= swap_enable_disable(line);
283 }
284 free(line);
285 }
286 if (ENABLE_FEATURE_CLEAN_UP)
287 fclose(f);
288 }
289
290 return err;
291 }
292
293 #define OPTSTR_SWAPON "ae" \
294 IF_FEATURE_SWAPON_DISCARD("d::") \
295 IF_FEATURE_SWAPON_PRI("p:")
296
297 int swap_on_off_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
swap_on_off_main(int argc UNUSED_PARAM,char ** argv)298 int swap_on_off_main(int argc UNUSED_PARAM, char **argv)
299 {
300 IF_FEATURE_SWAPON_PRI(char *prio;)
301 IF_FEATURE_SWAPON_DISCARD(char *discard = NULL;)
302 int ret = 0;
303
304 INIT_G();
305
306 getopt32(argv, do_swapoff ? "ae" : OPTSTR_SWAPON
307 IF_FEATURE_SWAPON_DISCARD(, &discard)
308 IF_FEATURE_SWAPON_PRI(, &prio)
309 );
310
311 argv += optind;
312
313 if (OPT_DISCARD) {
314 set_discard_flag(discard);
315 }
316 if (OPT_PRIO) {
317 set_priority_flag(prio);
318 }
319
320 if (OPT_ALL) {
321 /* swapoff -a does also /proc/swaps */
322 if (do_swapoff)
323 ret = do_all_in_proc_swaps();
324 ret |= do_em_all_in_fstab();
325 } else if (!*argv) {
326 /* if not -a we need at least one arg */
327 bb_show_usage();
328 }
329 /* Unset -a now to allow for more messages in swap_enable_disable */
330 option_mask32 = option_mask32 & ~OPT_a;
331 /* Now process devices on the commandline if any */
332 while (*argv) {
333 ret |= swap_enable_disable(*argv++);
334 }
335 return ret;
336 }
337