SSID: Respect ASCII character Label.
[tomato.git] / release / src / router / busybox / util-linux / swaponoff.c
blob1562424fa4136d48c215cb7a22e8d5cd9c9a6b2c
1 /* vi: set sw=4 ts=4: */
2 /*
3 * Mini swapon/swapoff implementation for busybox
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Licensed under GPLv2, see file LICENSE in this source tree.
8 */
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"
25 #include "libbb.h"
26 #include <mntent.h>
27 #ifndef __BIONIC__
28 # include <sys/swap.h>
29 #endif
31 #if ENABLE_FEATURE_MOUNT_LABEL
32 # include "volume_id.h"
33 #else
34 # define resolve_mount_spec(fsname) ((void)0)
35 #endif
37 #ifndef MNTTYPE_SWAP
38 # define MNTTYPE_SWAP "swap"
39 #endif
41 #if ENABLE_FEATURE_SWAPON_PRI
42 struct globals {
43 int flags;
44 } FIX_ALIASING;
45 #define G (*(struct globals*)&bb_common_bufsiz1)
46 #define g_flags (G.flags)
47 #else
48 #define g_flags 0
49 #endif
50 #define INIT_G() do { } while (0)
52 static int swap_enable_disable(char *device)
54 int status;
55 struct stat st;
57 resolve_mount_spec(&device);
58 if (stat(device, &st)) {
59 bb_perror_msg("warning: can't stat '%s'", device);
60 return 1;
63 #if ENABLE_DESKTOP
64 /* test for holes */
65 if (S_ISREG(st.st_mode))
66 if (st.st_blocks * (off_t)512 < st.st_size)
67 bb_error_msg("warning: swap file has holes");
68 #endif
70 if (applet_name[5] == 'n')
71 status = swapon(device, g_flags);
72 else
73 status = swapoff(device);
75 if (status != 0) {
76 bb_simple_perror_msg(device);
77 return 1;
80 return 0;
83 static int do_em_all(void)
85 struct mntent *m;
86 FILE *f;
87 int err;
89 f = setmntent("/etc/fstab", "r");
90 if (f == NULL)
91 bb_perror_msg_and_die("/etc/fstab");
93 err = 0;
94 while ((m = getmntent(f)) != NULL) {
95 if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0) {
96 /* swapon -a should ignore entries with noauto,
97 * but swapoff -a should process them */
98 if (applet_name[5] != 'n'
99 || hasmntopt(m, MNTOPT_NOAUTO) == NULL
101 err += swap_enable_disable(m->mnt_fsname);
106 if (ENABLE_FEATURE_CLEAN_UP)
107 endmntent(f);
109 return err;
112 int swap_on_off_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
113 int swap_on_off_main(int argc UNUSED_PARAM, char **argv)
115 int ret;
117 INIT_G();
119 #if !ENABLE_FEATURE_SWAPON_PRI
120 ret = getopt32(argv, "a");
121 #else
122 if (applet_name[5] == 'n')
123 opt_complementary = "p+";
124 ret = getopt32(argv, (applet_name[5] == 'n') ? "ap:" : "a", &g_flags);
126 if (ret & 2) { // -p
127 g_flags = SWAP_FLAG_PREFER |
128 ((g_flags & SWAP_FLAG_PRIO_MASK) << SWAP_FLAG_PRIO_SHIFT);
129 ret &= 1;
131 #endif
133 if (ret /* & 1: not needed */) // -a
134 return do_em_all();
136 argv += optind;
137 if (!*argv)
138 bb_show_usage();
140 /* ret = 0; redundant */
141 do {
142 ret += swap_enable_disable(*argv);
143 } while (*++argv);
145 return ret;