swaponoff: fix compile-time warning
[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] [-e]" IF_FEATURE_SWAPON_DISCARD(" [-d[POL]]") IF_FEATURE_SWAPON_PRI(" [-p PRI]") " [DEVICE]"
12 //usage:#define swapon_full_usage "\n\n"
13 //usage:       "Start swapping on DEVICE\n"
14 //usage:     "\n        -a      Start swapping on all swap devices"
15 //usage:        IF_FEATURE_SWAPON_DISCARD(
16 //usage:     "\n        -d[POL] Discard blocks at swapon (POL=once),"
17 //usage:     "\n                as freed (POL=pages), or both (POL omitted)"
18 //usage:        )
19 //usage:     "\n        -e      Silently skip devices that do not exist"
20 //usage:        IF_FEATURE_SWAPON_PRI(
21 //usage:     "\n        -p PRI  Set swap device priority"
22 //usage:        )
23 //usage:
24 //usage:#define swapoff_trivial_usage
25 //usage:       "[-a] [DEVICE]"
26 //usage:#define swapoff_full_usage "\n\n"
27 //usage:       "Stop swapping on DEVICE\n"
28 //usage:     "\n        -a      Stop swapping on all swap devices"
29
30 #include "libbb.h"
31 #include <mntent.h>
32 #ifndef __BIONIC__
33 # include <sys/swap.h>
34 #endif
35
36 #if ENABLE_FEATURE_MOUNT_LABEL
37 # include "volume_id.h"
38 #else
39 # define resolve_mount_spec(fsname) ((void)0)
40 #endif
41
42 #ifndef MNTTYPE_SWAP
43 # define MNTTYPE_SWAP "swap"
44 #endif
45
46 #if ENABLE_FEATURE_SWAPON_DISCARD
47 #ifndef SWAP_FLAG_DISCARD
48 #define SWAP_FLAG_DISCARD 0x10000
49 #endif
50 #ifndef SWAP_FLAG_DISCARD_ONCE
51 #define SWAP_FLAG_DISCARD_ONCE 0x20000
52 #endif
53 #ifndef SWAP_FLAG_DISCARD_PAGES
54 #define SWAP_FLAG_DISCARD_PAGES 0x40000
55 #endif
56 #define SWAP_FLAG_DISCARD_MASK \
57         (SWAP_FLAG_DISCARD | SWAP_FLAG_DISCARD_ONCE | SWAP_FLAG_DISCARD_PAGES)
58 #endif
59
60
61 #if ENABLE_FEATURE_SWAPON_DISCARD || ENABLE_FEATURE_SWAPON_PRI
62 struct globals {
63         int flags;
64 } FIX_ALIASING;
65 #define G (*(struct globals*)&bb_common_bufsiz1)
66 #define g_flags (G.flags)
67 #define save_g_flags()    int save_g_flags = g_flags
68 #define restore_g_flags() g_flags = save_g_flags
69 #else
70 #define g_flags 0
71 #define save_g_flags()    ((void)0)
72 #define restore_g_flags() ((void)0)
73 #endif
74 #define INIT_G() do { } while (0)
75
76 #define do_swapoff   (applet_name[5] == 'f')
77
78 /* Command line options */
79 enum {
80         OPTBIT_a,                              /* -a all      */
81         OPTBIT_e,                              /* -e ifexists */
82         IF_FEATURE_SWAPON_DISCARD( OPTBIT_d ,) /* -d discard  */
83         IF_FEATURE_SWAPON_PRI    ( OPTBIT_p ,) /* -p priority */
84         OPT_a = 1 << OPTBIT_a,
85         OPT_e = 1 << OPTBIT_e,
86         OPT_d = IF_FEATURE_SWAPON_DISCARD((1 << OPTBIT_d)) + 0,
87         OPT_p = IF_FEATURE_SWAPON_PRI    ((1 << OPTBIT_p)) + 0,
88 };
89
90 #define OPT_ALL      (option_mask32 & OPT_a)
91 #define OPT_DISCARD  (option_mask32 & OPT_d)
92 #define OPT_IFEXISTS (option_mask32 & OPT_e)
93 #define OPT_PRIO     (option_mask32 & OPT_p)
94
95 static int swap_enable_disable(char *device)
96 {
97         int err = 0;
98         int quiet = 0;
99
100         resolve_mount_spec(&device);
101
102         if (do_swapoff) {
103                 err = swapoff(device);
104                 /* Don't complain on OPT_ALL if not a swap device or if it doesn't exist */
105                 quiet = (OPT_ALL && (errno == EINVAL || errno == ENOENT));
106         } else {
107                 /* swapon */
108                 struct stat st;
109                 err = stat(device, &st);
110                 if (!err) {
111                         if (ENABLE_DESKTOP && S_ISREG(st.st_mode)) {
112                                 if (st.st_blocks * (off_t)512 < st.st_size) {
113                                         bb_error_msg("%s: file has holes", device);
114                                         return 1;
115                                 }
116                         }
117                         err = swapon(device, g_flags);
118                         /* Don't complain on swapon -a if device is already in use */
119                         quiet = (OPT_ALL && errno == EBUSY);
120                 }
121                 /* Don't complain if file does not exist with -e option */
122                 if (err && OPT_IFEXISTS && errno == ENOENT)
123                         err = 0;
124         }
125
126         if (err && !quiet) {
127                 bb_simple_perror_msg(device);
128                 return 1;
129         }
130         return 0;
131 }
132
133 #if ENABLE_FEATURE_SWAPON_DISCARD
134 static void set_discard_flag(char *s)
135 {
136         /* Unset the flag first to allow fstab options to override */
137         /* options set on the command line */
138         g_flags = (g_flags & ~SWAP_FLAG_DISCARD_MASK) | SWAP_FLAG_DISCARD;
139
140         if (!s) /* No optional policy value on the commandline */
141                 return;
142         /* Skip prepended '=' */
143         if (*s == '=')
144                 s++;
145         /* For fstab parsing: remove other appended options */
146         *strchrnul(s, ',') = '\0';
147
148         if (strcmp(s, "once") == 0)
149                 g_flags |= SWAP_FLAG_DISCARD_ONCE;
150         if  (strcmp(s, "pages") == 0)
151                 g_flags |= SWAP_FLAG_DISCARD_PAGES;
152 }
153 #else
154 #define set_discard_flag(s) ((void)0)
155 #endif
156
157 #if ENABLE_FEATURE_SWAPON_PRI
158 static void set_priority_flag(char *s)
159 {
160         unsigned prio;
161
162         /* For fstab parsing: remove other appended options */
163         *strchrnul(s, ',') = '\0';
164         /* Max allowed 32767 (== SWAP_FLAG_PRIO_MASK) */
165         prio = bb_strtou(s, NULL, 10);
166         if (!errno) {
167                 /* Unset the flag first to allow fstab options to override */
168                 /* options set on the command line */
169                 g_flags = (g_flags & ~SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER |
170                                         MIN(prio, SWAP_FLAG_PRIO_MASK);
171         }
172 }
173 #else
174 #define set_priority_flag(s) ((void)0)
175 #endif
176
177 static int do_em_all_in_fstab(void)
178 {
179         struct mntent *m;
180         int err = 0;
181         FILE *f = xfopen_for_read("/etc/fstab");
182
183         while ((m = getmntent(f)) != NULL) {
184                 if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0) {
185                         /* swapon -a should ignore entries with noauto,
186                          * but swapoff -a should process them
187                          */
188                         if (do_swapoff || hasmntopt(m, MNTOPT_NOAUTO) == NULL) {
189                                 /* each swap space might have different flags */
190                                 /* save global flags for the next round */
191                                 save_g_flags();
192                                 if (ENABLE_FEATURE_SWAPON_DISCARD) {
193                                         char *p = hasmntopt(m, "discard");
194                                         if (p) {
195                                                 /* move to '=' or to end of string */
196                                                 p += 7;
197                                                 set_discard_flag(p);
198                                         }
199                                 }
200                                 if (ENABLE_FEATURE_SWAPON_PRI) {
201                                         char *p = hasmntopt(m, "pri");
202                                         if (p) {
203                                                 set_priority_flag(p + 4);
204                                         }
205                                 }
206                                 err |= swap_enable_disable(m->mnt_fsname);
207                                 restore_g_flags();
208                         }
209                 }
210         }
211
212         if (ENABLE_FEATURE_CLEAN_UP)
213                 endmntent(f);
214
215         return err;
216 }
217
218 static int do_all_in_proc_swaps(void)
219 {
220         char *line;
221         int err = 0;
222         FILE *f = fopen_for_read("/proc/swaps");
223         /* Don't complain if missing */
224         if (f) {
225                 while ((line = xmalloc_fgetline(f)) != NULL) {
226                         if (line[0] == '/') {
227                                 *strchrnul(line, ' ') = '\0';
228                                 err |= swap_enable_disable(line);
229                         }
230                         free(line);
231                 }
232                 if (ENABLE_FEATURE_CLEAN_UP)
233                         fclose(f);
234         }
235
236         return err;
237 }
238
239 #define OPTSTR_SWAPON "ae" \
240         IF_FEATURE_SWAPON_DISCARD("d::") \
241         IF_FEATURE_SWAPON_PRI("p:")
242
243 int swap_on_off_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
244 int swap_on_off_main(int argc UNUSED_PARAM, char **argv)
245 {
246         IF_FEATURE_SWAPON_PRI(char *prio;)
247         IF_FEATURE_SWAPON_DISCARD(char *discard = NULL;)
248         int ret = 0;
249
250         INIT_G();
251
252         getopt32(argv, do_swapoff ? "ae" : OPTSTR_SWAPON
253                         IF_FEATURE_SWAPON_DISCARD(, &discard)
254                         IF_FEATURE_SWAPON_PRI(, &prio)
255         );
256
257         argv += optind;
258
259         if (OPT_DISCARD) {
260                 set_discard_flag(discard);
261         }
262         if (OPT_PRIO) {
263                 set_priority_flag(prio);
264         }
265
266         if (OPT_ALL) {
267                 /* swapoff -a does also /proc/swaps */
268                 if (do_swapoff)
269                         ret = do_all_in_proc_swaps();
270                 ret |= do_em_all_in_fstab();
271         } else if (!*argv) {
272                 /* if not -a we need at least one arg */
273                 bb_show_usage();
274         }
275         /* Unset -a now to allow for more messages in swap_enable_disable */
276         option_mask32 = option_mask32 & ~OPT_a;
277         /* Now process devices on the commandline if any */
278         while (*argv) {
279                 ret |= swap_enable_disable(*argv++);
280         }
281         return ret;
282 }