add help text
[oweals/busybox.git] / util-linux / swaponoff.c
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
10 //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
27 #include "libbb.h"
28 #include <mntent.h>
29 #include <sys/swap.h>
30
31 #if ENABLE_FEATURE_MOUNT_LABEL
32 # include "volume_id.h"
33 #else
34 # define resolve_mount_spec(fsname) ((void)0)
35 #endif
36
37 #if ENABLE_FEATURE_SWAPON_PRI
38 struct globals {
39         int flags;
40 } FIX_ALIASING;
41 #define G (*(struct globals*)&bb_common_bufsiz1)
42 #define g_flags (G.flags)
43 #else
44 #define g_flags 0
45 #endif
46
47 static int swap_enable_disable(char *device)
48 {
49         int status;
50         struct stat st;
51
52         resolve_mount_spec(&device);
53         xstat(device, &st);
54
55 #if ENABLE_DESKTOP
56         /* test for holes */
57         if (S_ISREG(st.st_mode))
58                 if (st.st_blocks * (off_t)512 < st.st_size)
59                         bb_error_msg("warning: swap file has holes");
60 #endif
61
62         if (applet_name[5] == 'n')
63                 status = swapon(device, g_flags);
64         else
65                 status = swapoff(device);
66
67         if (status != 0) {
68                 bb_simple_perror_msg(device);
69                 return 1;
70         }
71
72         return 0;
73 }
74
75 static int do_em_all(void)
76 {
77         struct mntent *m;
78         FILE *f;
79         int err;
80
81         f = setmntent("/etc/fstab", "r");
82         if (f == NULL)
83                 bb_perror_msg_and_die("/etc/fstab");
84
85         err = 0;
86         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         }
97
98         if (ENABLE_FEATURE_CLEAN_UP)
99                 endmntent(f);
100
101         return err;
102 }
103
104 int swap_on_off_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
105 int swap_on_off_main(int argc UNUSED_PARAM, char **argv)
106 {
107         int ret;
108
109 #if !ENABLE_FEATURE_SWAPON_PRI
110         ret = getopt32(argv, "a");
111 #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
123                 return do_em_all();
124
125         argv += optind;
126         if (!*argv)
127                 bb_show_usage();
128
129         /* ret = 0; redundant */
130         do {
131                 ret += swap_enable_disable(*argv);
132         } while (*++argv);
133
134         return ret;
135 }