fixes, fully translated tomato, with english dictionary and Polish translation
[tomato.git] / release / src / router / busybox / util-linux / swaponoff.c
blob8226ca2717c1f58f9b56b4460d7dfd1e360a4c6b
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 #include "libbb.h"
11 #include <mntent.h>
12 #include <sys/swap.h>
14 #if ENABLE_FEATURE_MOUNT_LABEL
15 # include "volume_id.h"
16 #else
17 # define resolve_mount_spec(fsname) ((void)0)
18 #endif
20 #if ENABLE_FEATURE_SWAPON_PRI
21 struct globals {
22 int flags;
23 } FIX_ALIASING;
24 #define G (*(struct globals*)&bb_common_bufsiz1)
25 #define g_flags (G.flags)
26 #else
27 #define g_flags 0
28 #endif
30 static int swap_enable_disable(char *device)
32 int status;
33 struct stat st;
35 resolve_mount_spec(&device);
36 if (stat(device, &st)) {
37 bb_perror_msg("warning: can't stat '%s'", device);
38 return 1;
41 #if ENABLE_DESKTOP
42 /* test for holes */
43 if (S_ISREG(st.st_mode))
44 if (st.st_blocks * (off_t)512 < st.st_size)
45 bb_error_msg("warning: swap file has holes");
46 #endif
48 if (applet_name[5] == 'n')
49 status = swapon(device, g_flags);
50 else
51 status = swapoff(device);
53 if (status != 0) {
54 bb_simple_perror_msg(device);
55 return 1;
58 return 0;
61 static int do_em_all(void)
63 struct mntent *m;
64 FILE *f;
65 int err;
67 f = setmntent("/etc/fstab", "r");
68 if (f == NULL)
69 bb_perror_msg_and_die("/etc/fstab");
71 err = 0;
72 while ((m = getmntent(f)) != NULL) {
73 if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0) {
74 /* swapon -a should ignore entries with noauto,
75 * but swapoff -a should process them */
76 if (applet_name[5] != 'n'
77 || hasmntopt(m, MNTOPT_NOAUTO) == NULL
78 ) {
79 err += swap_enable_disable(m->mnt_fsname);
84 if (ENABLE_FEATURE_CLEAN_UP)
85 endmntent(f);
87 return err;
90 int swap_on_off_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
91 int swap_on_off_main(int argc UNUSED_PARAM, char **argv)
93 int ret;
95 #if !ENABLE_FEATURE_SWAPON_PRI
96 ret = getopt32(argv, "a");
97 #else
98 opt_complementary = "p+";
99 ret = getopt32(argv, (applet_name[5] == 'n') ? "ap:" : "a", &g_flags);
101 if (ret & 2) { // -p
102 g_flags = SWAP_FLAG_PREFER |
103 ((g_flags & SWAP_FLAG_PRIO_MASK) << SWAP_FLAG_PRIO_SHIFT);
104 ret &= 1;
106 #endif
108 if (ret /* & 1: not needed */) // -a
109 return do_em_all();
111 argv += optind;
112 if (!*argv)
113 bb_show_usage();
115 /* ret = 0; redundant */
116 do {
117 ret += swap_enable_disable(*argv);
118 } while (*++argv);
120 return ret;