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