blob: b7cf69833304d060f0e5566cfa7627f9a737050c [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"
14//usage: "\nOptions:"
15//usage: "\n -a Start swapping on all swap devices"
16//usage: IF_FEATURE_SWAPON_PRI(
17//usage: "\n -p PRI Set swap device priority"
18//usage: )
19//usage:
20//usage:#define swapoff_trivial_usage
21//usage: "[-a] [DEVICE]"
22//usage:#define swapoff_full_usage "\n\n"
23//usage: "Stop swapping on DEVICE\n"
24//usage: "\nOptions:"
25//usage: "\n -a Stop swapping on all swap devices"
26
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000027#include "libbb.h"
Eric Andersen87590061999-10-18 21:22:59 +000028#include <mntent.h>
Eric Andersene76c3b02001-04-05 03:14:39 +000029#include <sys/swap.h>
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
Denis Vlasenkoee56e012008-05-18 23:05:34 +000037#if ENABLE_FEATURE_SWAPON_PRI
38struct globals {
39 int flags;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010040} FIX_ALIASING;
Denis Vlasenkoee56e012008-05-18 23:05:34 +000041#define G (*(struct globals*)&bb_common_bufsiz1)
42#define g_flags (G.flags)
43#else
44#define g_flags 0
45#endif
46
Rob Landley20cc6d52006-09-12 21:42:17 +000047static int swap_enable_disable(char *device)
Eric Andersen87590061999-10-18 21:22:59 +000048{
Erik Andersene49d5ec2000-02-08 19:58:47 +000049 int status;
Eric Andersen97b141a2002-11-03 00:25:23 +000050 struct stat st;
51
Natanael Copa9aff2992009-09-20 04:28:22 +020052 resolve_mount_spec(&device);
Rob Landleyc5b1d4d2006-03-13 15:45:16 +000053 xstat(device, &st);
Eric Andersen97b141a2002-11-03 00:25:23 +000054
Denis Vlasenko56594072007-03-14 22:55:39 +000055#if ENABLE_DESKTOP
Eric Andersen97b141a2002-11-03 00:25:23 +000056 /* test for holes */
Mike Frysinger6943a942005-09-13 02:29:39 +000057 if (S_ISREG(st.st_mode))
Denis Vlasenkof8b21d02007-11-05 19:33:38 +000058 if (st.st_blocks * (off_t)512 < st.st_size)
Denis Vlasenko56594072007-03-14 22:55:39 +000059 bb_error_msg("warning: swap file has holes");
Denis Vlasenkob3f09f42007-03-12 18:16:24 +000060#endif
Eric Andersen87590061999-10-18 21:22:59 +000061
Denis Vlasenko8f8f2682006-10-03 21:00:43 +000062 if (applet_name[5] == 'n')
Denis Vlasenkoee56e012008-05-18 23:05:34 +000063 status = swapon(device, g_flags);
Erik Andersene49d5ec2000-02-08 19:58:47 +000064 else
65 status = swapoff(device);
66
Eric Andersendb1df5e2002-10-26 10:27:42 +000067 if (status != 0) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +000068 bb_simple_perror_msg(device);
Mike Frysinger2d5e4f62005-09-16 04:41:20 +000069 return 1;
Eric Andersendb1df5e2002-10-26 10:27:42 +000070 }
Mike Frysinger6943a942005-09-13 02:29:39 +000071
Mike Frysinger2d5e4f62005-09-16 04:41:20 +000072 return 0;
Eric Andersen87590061999-10-18 21:22:59 +000073}
74
Eric Andersendb1df5e2002-10-26 10:27:42 +000075static int do_em_all(void)
Eric Andersen87590061999-10-18 21:22:59 +000076{
77 struct mntent *m;
Mike Frysinger6943a942005-09-13 02:29:39 +000078 FILE *f;
79 int err;
Eric Andersen87590061999-10-18 21:22:59 +000080
Mike Frysinger6943a942005-09-13 02:29:39 +000081 f = setmntent("/etc/fstab", "r");
Matt Kraaia9819b22000-12-22 01:48:07 +000082 if (f == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +000083 bb_perror_msg_and_die("/etc/fstab");
Mike Frysinger6943a942005-09-13 02:29:39 +000084
85 err = 0;
Lauri Kasanend2844fc2010-04-29 22:20:57 +020086 while ((m = getmntent(f)) != NULL) {
87 if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0) {
88 /* swapon -a should ignore entries with noauto,
89 * but swapoff -a should process them */
90 if (applet_name[5] != 'n'
91 || hasmntopt(m, MNTOPT_NOAUTO) == NULL
92 ) {
93 err += swap_enable_disable(m->mnt_fsname);
94 }
95 }
96 }
Mike Frysinger6943a942005-09-13 02:29:39 +000097
Lauri Kasanend2844fc2010-04-29 22:20:57 +020098 if (ENABLE_FEATURE_CLEAN_UP)
99 endmntent(f);
Mike Frysinger6943a942005-09-13 02:29:39 +0000100
Eric Andersendb1df5e2002-10-26 10:27:42 +0000101 return err;
Eric Andersen87590061999-10-18 21:22:59 +0000102}
103
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000104int swap_on_off_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000105int swap_on_off_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen87590061999-10-18 21:22:59 +0000106{
Mike Frysinger2d5e4f62005-09-16 04:41:20 +0000107 int ret;
Mike Frysinger6943a942005-09-13 02:29:39 +0000108
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000109#if !ENABLE_FEATURE_SWAPON_PRI
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000110 ret = getopt32(argv, "a");
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000111#else
112 opt_complementary = "p+";
113 ret = getopt32(argv, (applet_name[5] == 'n') ? "ap:" : "a", &g_flags);
114
115 if (ret & 2) { // -p
116 g_flags = SWAP_FLAG_PREFER |
117 ((g_flags & SWAP_FLAG_PRIO_MASK) << SWAP_FLAG_PRIO_SHIFT);
118 ret &= 1;
119 }
120#endif
121
122 if (ret /* & 1: not needed */) // -a
Rob Landleybc3d4a12005-09-13 01:30:19 +0000123 return do_em_all();
Mike Frysinger6943a942005-09-13 02:29:39 +0000124
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000125 argv += optind;
126 if (!*argv)
127 bb_show_usage();
128
Denis Vlasenkob3f09f42007-03-12 18:16:24 +0000129 /* ret = 0; redundant */
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000130 do {
Mike Frysinger2d5e4f62005-09-16 04:41:20 +0000131 ret += swap_enable_disable(*argv);
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000132 } while (*++argv);
133
Mike Frysinger2d5e4f62005-09-16 04:41:20 +0000134 return ret;
Eric Andersen87590061999-10-18 21:22:59 +0000135}