1 /* vi: set sw=4 ts=4: */
3 * Mini swapon/swapoff implementation for busybox
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Licensed under the GPL version 2, see the file LICENSE in this tarball.
14 #if ENABLE_FEATURE_MOUNT_LABEL
15 # include "volume_id.h"
17 # define resolve_mount_spec(fsname) ((void)0)
20 #if ENABLE_FEATURE_SWAPON_PRI
24 #define G (*(struct globals*)&bb_common_bufsiz1)
25 #define g_flags (G.flags)
30 static int swap_enable_disable(char *device)
35 resolve_mount_spec(&device);
40 if (S_ISREG(st.st_mode))
41 if (st.st_blocks * (off_t)512 < st.st_size)
42 bb_error_msg("warning: swap file has holes");
45 if (applet_name[5] == 'n')
46 status = swapon(device, g_flags);
48 status = swapoff(device);
51 bb_simple_perror_msg(device);
58 static int do_em_all(void)
64 f = setmntent("/etc/fstab", "r");
66 bb_perror_msg_and_die("/etc/fstab");
69 while ((m = getmntent(f)) != NULL) {
70 if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0) {
71 /* swapon -a should ignore entries with noauto,
72 * but swapoff -a should process them */
73 if (applet_name[5] != 'n'
74 || hasmntopt(m, MNTOPT_NOAUTO) == NULL
76 err += swap_enable_disable(m->mnt_fsname);
81 if (ENABLE_FEATURE_CLEAN_UP)
87 int swap_on_off_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
88 int swap_on_off_main(int argc UNUSED_PARAM, char **argv)
92 #if !ENABLE_FEATURE_SWAPON_PRI
93 ret = getopt32(argv, "a");
95 opt_complementary = "p+";
96 ret = getopt32(argv, (applet_name[5] == 'n') ? "ap:" : "a", &g_flags);
99 g_flags = SWAP_FLAG_PREFER |
100 ((g_flags & SWAP_FLAG_PRIO_MASK) << SWAP_FLAG_PRIO_SHIFT);
105 if (ret /* & 1: not needed */) // -a
112 /* ret = 0; redundant */
114 ret += swap_enable_disable(*argv);