add INIT_G()'s. No code changes.
[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:     "\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"
23 //usage:     "\n        -a      Stop swapping on all swap devices"
24
25 #include "libbb.h"
26 #include <mntent.h>
27 #include <sys/swap.h>
28 #ifndef __BIONIC__
29 # include <sys/swap.h>
30 #endif
31
32 #if ENABLE_FEATURE_MOUNT_LABEL
33 # include "volume_id.h"
34 #else
35 # define resolve_mount_spec(fsname) ((void)0)
36 #endif
37
38 #ifndef MNTTYPE_SWAP
39 # define MNTTYPE_SWAP "swap"
40 #endif
41
42 #if ENABLE_FEATURE_SWAPON_PRI
43 struct globals {
44         int flags;
45 } FIX_ALIASING;
46 #define G (*(struct globals*)&bb_common_bufsiz1)
47 #define g_flags (G.flags)
48 #else
49 #define g_flags 0
50 #endif
51 #define INIT_G() do { } while (0)
52
53 static int swap_enable_disable(char *device)
54 {
55         int status;
56         struct stat st;
57
58         resolve_mount_spec(&device);
59         xstat(device, &st);
60
61 #if ENABLE_DESKTOP
62         /* test for holes */
63         if (S_ISREG(st.st_mode))
64                 if (st.st_blocks * (off_t)512 < st.st_size)
65                         bb_error_msg("warning: swap file has holes");
66 #endif
67
68         if (applet_name[5] == 'n')
69                 status = swapon(device, g_flags);
70         else
71                 status = swapoff(device);
72
73         if (status != 0) {
74                 bb_simple_perror_msg(device);
75                 return 1;
76         }
77
78         return 0;
79 }
80
81 static int do_em_all(void)
82 {
83         struct mntent *m;
84         FILE *f;
85         int err;
86
87         f = setmntent("/etc/fstab", "r");
88         if (f == NULL)
89                 bb_perror_msg_and_die("/etc/fstab");
90
91         err = 0;
92         while ((m = getmntent(f)) != NULL) {
93                 if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0) {
94                         /* swapon -a should ignore entries with noauto,
95                          * but swapoff -a should process them */
96                         if (applet_name[5] != 'n'
97                          || hasmntopt(m, MNTOPT_NOAUTO) == NULL
98                         ) {
99                                 err += swap_enable_disable(m->mnt_fsname);
100                         }
101                 }
102         }
103
104         if (ENABLE_FEATURE_CLEAN_UP)
105                 endmntent(f);
106
107         return err;
108 }
109
110 int swap_on_off_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
111 int swap_on_off_main(int argc UNUSED_PARAM, char **argv)
112 {
113         int ret;
114
115         INIT_G();
116
117 #if !ENABLE_FEATURE_SWAPON_PRI
118         ret = getopt32(argv, "a");
119 #else
120         if (applet_name[5] == 'n')
121                 opt_complementary = "p+";
122         ret = getopt32(argv, (applet_name[5] == 'n') ? "ap:" : "a", &g_flags);
123
124         if (ret & 2) { // -p
125                 g_flags = SWAP_FLAG_PREFER |
126                         ((g_flags & SWAP_FLAG_PRIO_MASK) << SWAP_FLAG_PRIO_SHIFT);
127                 ret &= 1;
128         }
129 #endif
130
131         if (ret /* & 1: not needed */) // -a
132                 return do_em_all();
133
134         argv += optind;
135         if (!*argv)
136                 bb_show_usage();
137
138         /* ret = 0; redundant */
139         do {
140                 ret += swap_enable_disable(*argv);
141         } while (*++argv);
142
143         return ret;
144 }