blob: 963139adb6f8dc618076e13c307d1f59c994c1d4 [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
11//usage: "[-a]" IF_FEATURE_SWAPON_PRI(" [-p PRI]") " [DEVICE]"
12//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"
15//usage: IF_FEATURE_SWAPON_PRI(
16//usage: "\n -p PRI Set swap device priority"
17//usage: )
18//usage:
19//usage:#define swapoff_trivial_usage
20//usage: "[-a] [DEVICE]"
21//usage:#define swapoff_full_usage "\n\n"
22//usage: "Stop swapping on DEVICE\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020023//usage: "\n -a Stop swapping on all swap devices"
24
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000025#include "libbb.h"
Eric Andersen87590061999-10-18 21:22:59 +000026#include <mntent.h>
Tanguy Pruvot8aeb3712011-06-30 08:59:26 +020027#ifndef __BIONIC__
Denys Vlasenko14bd16a2011-07-08 08:49:40 +020028# include <sys/swap.h>
Tanguy Pruvot8aeb3712011-06-30 08:59:26 +020029#endif
Eric Andersene76c3b02001-04-05 03:14:39 +000030
Natanael Copa9aff2992009-09-20 04:28:22 +020031#if ENABLE_FEATURE_MOUNT_LABEL
32# include "volume_id.h"
33#else
34# define resolve_mount_spec(fsname) ((void)0)
35#endif
36
Denys Vlasenko14bd16a2011-07-08 08:49:40 +020037#ifndef MNTTYPE_SWAP
38# define MNTTYPE_SWAP "swap"
39#endif
40
Denis Vlasenkoee56e012008-05-18 23:05:34 +000041#if ENABLE_FEATURE_SWAPON_PRI
42struct globals {
43 int flags;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010044} FIX_ALIASING;
Denis Vlasenkoee56e012008-05-18 23:05:34 +000045#define G (*(struct globals*)&bb_common_bufsiz1)
46#define g_flags (G.flags)
47#else
48#define g_flags 0
49#endif
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +020050#define INIT_G() do { } while (0)
Denis Vlasenkoee56e012008-05-18 23:05:34 +000051
Rob Landley20cc6d52006-09-12 21:42:17 +000052static int swap_enable_disable(char *device)
Eric Andersen87590061999-10-18 21:22:59 +000053{
Erik Andersene49d5ec2000-02-08 19:58:47 +000054 int status;
Eric Andersen97b141a2002-11-03 00:25:23 +000055 struct stat st;
56
Natanael Copa9aff2992009-09-20 04:28:22 +020057 resolve_mount_spec(&device);
Rob Landleyc5b1d4d2006-03-13 15:45:16 +000058 xstat(device, &st);
Eric Andersen97b141a2002-11-03 00:25:23 +000059
Denis Vlasenko56594072007-03-14 22:55:39 +000060#if ENABLE_DESKTOP
Eric Andersen97b141a2002-11-03 00:25:23 +000061 /* test for holes */
Mike Frysinger6943a942005-09-13 02:29:39 +000062 if (S_ISREG(st.st_mode))
maxwen27116ba2015-08-14 21:41:28 +020063 if (st.st_blocks * (off_t)512 < (uint64_t) st.st_size)
Denis Vlasenko56594072007-03-14 22:55:39 +000064 bb_error_msg("warning: swap file has holes");
Denis Vlasenkob3f09f42007-03-12 18:16:24 +000065#endif
Eric Andersen87590061999-10-18 21:22:59 +000066
Denis Vlasenko8f8f2682006-10-03 21:00:43 +000067 if (applet_name[5] == 'n')
Denis Vlasenkoee56e012008-05-18 23:05:34 +000068 status = swapon(device, g_flags);
Erik Andersene49d5ec2000-02-08 19:58:47 +000069 else
70 status = swapoff(device);
71
Eric Andersendb1df5e2002-10-26 10:27:42 +000072 if (status != 0) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +000073 bb_simple_perror_msg(device);
Mike Frysinger2d5e4f62005-09-16 04:41:20 +000074 return 1;
Eric Andersendb1df5e2002-10-26 10:27:42 +000075 }
Mike Frysinger6943a942005-09-13 02:29:39 +000076
Mike Frysinger2d5e4f62005-09-16 04:41:20 +000077 return 0;
Eric Andersen87590061999-10-18 21:22:59 +000078}
79
Eric Andersendb1df5e2002-10-26 10:27:42 +000080static int do_em_all(void)
Eric Andersen87590061999-10-18 21:22:59 +000081{
82 struct mntent *m;
Mike Frysinger6943a942005-09-13 02:29:39 +000083 FILE *f;
84 int err;
Eric Andersen87590061999-10-18 21:22:59 +000085
Mike Frysinger6943a942005-09-13 02:29:39 +000086 f = setmntent("/etc/fstab", "r");
Matt Kraaia9819b22000-12-22 01:48:07 +000087 if (f == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +000088 bb_perror_msg_and_die("/etc/fstab");
Mike Frysinger6943a942005-09-13 02:29:39 +000089
90 err = 0;
Lauri Kasanend2844fc2010-04-29 22:20:57 +020091 while ((m = getmntent(f)) != NULL) {
92 if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0) {
93 /* swapon -a should ignore entries with noauto,
94 * but swapoff -a should process them */
95 if (applet_name[5] != 'n'
96 || hasmntopt(m, MNTOPT_NOAUTO) == NULL
97 ) {
maxwen27116ba2015-08-14 21:41:28 +020098#if ENABLE_FEATURE_SWAPON_PRI
99 const char *p;
100 g_flags = 0; /* each swap space might have different flags */
101 p = hasmntopt(m, "pri");
102 if (p) {
103 /* Max allowed 32767 (==SWAP_FLAG_PRIO_MASK) */
104 unsigned int swap_prio = MIN(bb_strtou(p + 4 , NULL, 10), SWAP_FLAG_PRIO_MASK);
105 /* We want to allow "NNNN,foo", thus errno == EINVAL is allowed too */
106 if (errno != ERANGE) {
107 g_flags = SWAP_FLAG_PREFER |
108 (swap_prio << SWAP_FLAG_PRIO_SHIFT);
109 }
110 }
111#endif
Lauri Kasanend2844fc2010-04-29 22:20:57 +0200112 err += swap_enable_disable(m->mnt_fsname);
113 }
114 }
115 }
Mike Frysinger6943a942005-09-13 02:29:39 +0000116
Lauri Kasanend2844fc2010-04-29 22:20:57 +0200117 if (ENABLE_FEATURE_CLEAN_UP)
118 endmntent(f);
Mike Frysinger6943a942005-09-13 02:29:39 +0000119
Eric Andersendb1df5e2002-10-26 10:27:42 +0000120 return err;
Eric Andersen87590061999-10-18 21:22:59 +0000121}
122
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000123int swap_on_off_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000124int swap_on_off_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen87590061999-10-18 21:22:59 +0000125{
Mike Frysinger2d5e4f62005-09-16 04:41:20 +0000126 int ret;
Mike Frysinger6943a942005-09-13 02:29:39 +0000127
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200128 INIT_G();
129
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000130#if !ENABLE_FEATURE_SWAPON_PRI
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000131 ret = getopt32(argv, "a");
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000132#else
Denys Vlasenkocc272b02011-08-28 13:00:29 +0200133 if (applet_name[5] == 'n')
134 opt_complementary = "p+";
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000135 ret = getopt32(argv, (applet_name[5] == 'n') ? "ap:" : "a", &g_flags);
136
137 if (ret & 2) { // -p
138 g_flags = SWAP_FLAG_PREFER |
139 ((g_flags & SWAP_FLAG_PRIO_MASK) << SWAP_FLAG_PRIO_SHIFT);
140 ret &= 1;
141 }
142#endif
143
144 if (ret /* & 1: not needed */) // -a
Rob Landleybc3d4a12005-09-13 01:30:19 +0000145 return do_em_all();
Mike Frysinger6943a942005-09-13 02:29:39 +0000146
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000147 argv += optind;
148 if (!*argv)
149 bb_show_usage();
150
Denis Vlasenkob3f09f42007-03-12 18:16:24 +0000151 /* ret = 0; redundant */
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000152 do {
Mike Frysinger2d5e4f62005-09-16 04:41:20 +0000153 ret += swap_enable_disable(*argv);
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000154 } while (*++argv);
155
Mike Frysinger2d5e4f62005-09-16 04:41:20 +0000156 return ret;
Eric Andersen87590061999-10-18 21:22:59 +0000157}