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