swaponoff.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. //usage:#define swapon_trivial_usage
  10. //usage: "[-a] [-e]" IF_FEATURE_SWAPON_DISCARD(" [-d[POL]]") IF_FEATURE_SWAPON_PRI(" [-p PRI]") " [DEVICE]"
  11. //usage:#define swapon_full_usage "\n\n"
  12. //usage: "Start swapping on DEVICE\n"
  13. //usage: "\n -a Start swapping on all swap devices"
  14. //usage: IF_FEATURE_SWAPON_DISCARD(
  15. //usage: "\n -d[POL] Discard blocks at swapon (POL=once),"
  16. //usage: "\n as freed (POL=pages), or both (POL omitted)"
  17. //usage: )
  18. //usage: "\n -e Silently skip devices that do not exist"
  19. //usage: IF_FEATURE_SWAPON_PRI(
  20. //usage: "\n -p PRI Set swap device priority"
  21. //usage: )
  22. //usage:
  23. //usage:#define swapoff_trivial_usage
  24. //usage: "[-a] [-e] [DEVICE]"
  25. //usage:#define swapoff_full_usage "\n\n"
  26. //usage: "Stop swapping on DEVICE\n"
  27. //usage: "\n -a Stop swapping on all swap devices"
  28. //usage: "\n -e Silently skip devices that do not exist"
  29. #include "libbb.h"
  30. #include <mntent.h>
  31. #ifndef __BIONIC__
  32. # include <sys/swap.h>
  33. #endif
  34. #if ENABLE_FEATURE_MOUNT_LABEL
  35. # include "volume_id.h"
  36. #else
  37. # define resolve_mount_spec(fsname) ((void)0)
  38. #endif
  39. #ifndef MNTTYPE_SWAP
  40. # define MNTTYPE_SWAP "swap"
  41. #endif
  42. #if ENABLE_FEATURE_SWAPON_DISCARD
  43. #ifndef SWAP_FLAG_DISCARD
  44. #define SWAP_FLAG_DISCARD 0x10000
  45. #endif
  46. #ifndef SWAP_FLAG_DISCARD_ONCE
  47. #define SWAP_FLAG_DISCARD_ONCE 0x20000
  48. #endif
  49. #ifndef SWAP_FLAG_DISCARD_PAGES
  50. #define SWAP_FLAG_DISCARD_PAGES 0x40000
  51. #endif
  52. #define SWAP_FLAG_DISCARD_MASK \
  53. (SWAP_FLAG_DISCARD | SWAP_FLAG_DISCARD_ONCE | SWAP_FLAG_DISCARD_PAGES)
  54. #endif
  55. #if ENABLE_FEATURE_SWAPON_DISCARD || ENABLE_FEATURE_SWAPON_PRI
  56. struct globals {
  57. int flags;
  58. } FIX_ALIASING;
  59. #define G (*(struct globals*)&bb_common_bufsiz1)
  60. #define g_flags (G.flags)
  61. #define save_g_flags() int save_g_flags = g_flags
  62. #define restore_g_flags() g_flags = save_g_flags
  63. #else
  64. #define g_flags 0
  65. #define save_g_flags() ((void)0)
  66. #define restore_g_flags() ((void)0)
  67. #endif
  68. #define INIT_G() do { } while (0)
  69. #define do_swapoff (applet_name[5] == 'f')
  70. /* Command line options */
  71. enum {
  72. OPTBIT_a, /* -a all */
  73. OPTBIT_e, /* -e ifexists */
  74. IF_FEATURE_SWAPON_DISCARD( OPTBIT_d ,) /* -d discard */
  75. IF_FEATURE_SWAPON_PRI ( OPTBIT_p ,) /* -p priority */
  76. OPT_a = 1 << OPTBIT_a,
  77. OPT_e = 1 << OPTBIT_e,
  78. OPT_d = IF_FEATURE_SWAPON_DISCARD((1 << OPTBIT_d)) + 0,
  79. OPT_p = IF_FEATURE_SWAPON_PRI ((1 << OPTBIT_p)) + 0,
  80. };
  81. #define OPT_ALL (option_mask32 & OPT_a)
  82. #define OPT_DISCARD (option_mask32 & OPT_d)
  83. #define OPT_IFEXISTS (option_mask32 & OPT_e)
  84. #define OPT_PRIO (option_mask32 & OPT_p)
  85. static int swap_enable_disable(char *device)
  86. {
  87. int err = 0;
  88. int quiet = 0;
  89. struct stat st;
  90. resolve_mount_spec(&device);
  91. if (!OPT_IFEXISTS)
  92. xstat(device, &st);
  93. if (do_swapoff) {
  94. err = swapoff(device);
  95. /* Don't complain on OPT_ALL if not a swap device or if it doesn't exist */
  96. quiet = (OPT_ALL && (errno == EINVAL || errno == ENOENT));
  97. } else {
  98. /* swapon */
  99. err = stat(device, &st);
  100. if (!err) {
  101. if (ENABLE_DESKTOP && S_ISREG(st.st_mode)) {
  102. if (st.st_blocks * (off_t)512 < st.st_size) {
  103. bb_error_msg("%s: file has holes", device);
  104. return 1;
  105. }
  106. }
  107. err = swapon(device, g_flags);
  108. /* Don't complain on swapon -a if device is already in use */
  109. /* Don't complain if file does not exist with -e option */
  110. quiet = (OPT_ALL && errno == EBUSY) || (OPT_IFEXISTS && errno == ENOENT);
  111. }
  112. }
  113. if (err && !quiet) {
  114. bb_simple_perror_msg(device);
  115. return 1;
  116. }
  117. return 0;
  118. }
  119. #if ENABLE_FEATURE_SWAPON_DISCARD
  120. static void set_discard_flag(char *s)
  121. {
  122. /* Unset the flag first to allow fstab options to override */
  123. /* options set on the command line */
  124. g_flags = (g_flags & ~SWAP_FLAG_DISCARD_MASK) | SWAP_FLAG_DISCARD;
  125. if (!s) /* No optional policy value on the commandline */
  126. return;
  127. /* Skip prepended '=' */
  128. if (*s == '=')
  129. s++;
  130. /* For fstab parsing: remove other appended options */
  131. *strchrnul(s, ',') = '\0';
  132. if (strcmp(s, "once") == 0)
  133. g_flags |= SWAP_FLAG_DISCARD_ONCE;
  134. if (strcmp(s, "pages") == 0)
  135. g_flags |= SWAP_FLAG_DISCARD_PAGES;
  136. }
  137. #else
  138. #define set_discard_flag(s) ((void)0)
  139. #endif
  140. #if ENABLE_FEATURE_SWAPON_PRI
  141. static void set_priority_flag(char *s)
  142. {
  143. unsigned prio;
  144. /* For fstab parsing: remove other appended options */
  145. *strchrnul(s, ',') = '\0';
  146. /* Max allowed 32767 (== SWAP_FLAG_PRIO_MASK) */
  147. prio = bb_strtou(s, NULL, 10);
  148. if (!errno) {
  149. /* Unset the flag first to allow fstab options to override */
  150. /* options set on the command line */
  151. g_flags = (g_flags & ~SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER |
  152. MIN(prio, SWAP_FLAG_PRIO_MASK);
  153. }
  154. }
  155. #else
  156. #define set_priority_flag(s) ((void)0)
  157. #endif
  158. static int do_em_all_in_fstab(void)
  159. {
  160. struct mntent *m;
  161. int err = 0;
  162. FILE *f = xfopen_for_read("/etc/fstab");
  163. while ((m = getmntent(f)) != NULL) {
  164. if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0) {
  165. /* swapon -a should ignore entries with noauto,
  166. * but swapoff -a should process them
  167. */
  168. if (do_swapoff || hasmntopt(m, MNTOPT_NOAUTO) == NULL) {
  169. /* each swap space might have different flags */
  170. /* save global flags for the next round */
  171. save_g_flags();
  172. if (ENABLE_FEATURE_SWAPON_DISCARD) {
  173. char *p = hasmntopt(m, "discard");
  174. if (p) {
  175. /* move to '=' or to end of string */
  176. p += 7;
  177. set_discard_flag(p);
  178. }
  179. }
  180. if (ENABLE_FEATURE_SWAPON_PRI) {
  181. char *p = hasmntopt(m, "pri");
  182. if (p) {
  183. set_priority_flag(p + 4);
  184. }
  185. }
  186. err |= swap_enable_disable(m->mnt_fsname);
  187. restore_g_flags();
  188. }
  189. }
  190. }
  191. if (ENABLE_FEATURE_CLEAN_UP)
  192. endmntent(f);
  193. return err;
  194. }
  195. static int do_all_in_proc_swaps(void)
  196. {
  197. char *line;
  198. int err = 0;
  199. FILE *f = fopen_for_read("/proc/swaps");
  200. /* Don't complain if missing */
  201. if (f) {
  202. while ((line = xmalloc_fgetline(f)) != NULL) {
  203. if (line[0] == '/') {
  204. *strchrnul(line, ' ') = '\0';
  205. err |= swap_enable_disable(line);
  206. }
  207. free(line);
  208. }
  209. if (ENABLE_FEATURE_CLEAN_UP)
  210. fclose(f);
  211. }
  212. return err;
  213. }
  214. #define OPTSTR_SWAPON "ae" \
  215. IF_FEATURE_SWAPON_DISCARD("d::") \
  216. IF_FEATURE_SWAPON_PRI("p:")
  217. int swap_on_off_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  218. int swap_on_off_main(int argc UNUSED_PARAM, char **argv)
  219. {
  220. IF_FEATURE_SWAPON_PRI(char *prio;)
  221. IF_FEATURE_SWAPON_DISCARD(char *discard = NULL;)
  222. int ret = 0;
  223. INIT_G();
  224. getopt32(argv, do_swapoff ? "ae" : OPTSTR_SWAPON
  225. IF_FEATURE_SWAPON_DISCARD(, &discard)
  226. IF_FEATURE_SWAPON_PRI(, &prio)
  227. );
  228. argv += optind;
  229. if (OPT_DISCARD) {
  230. set_discard_flag(discard);
  231. }
  232. if (OPT_PRIO) {
  233. set_priority_flag(prio);
  234. }
  235. if (OPT_ALL) {
  236. /* swapoff -a does also /proc/swaps */
  237. if (do_swapoff)
  238. ret = do_all_in_proc_swaps();
  239. ret |= do_em_all_in_fstab();
  240. } else if (!*argv) {
  241. /* if not -a we need at least one arg */
  242. bb_show_usage();
  243. }
  244. /* Unset -a now to allow for more messages in swap_enable_disable */
  245. option_mask32 = option_mask32 & ~OPT_a;
  246. /* Now process devices on the commandline if any */
  247. while (*argv) {
  248. ret |= swap_enable_disable(*argv++);
  249. }
  250. return ret;
  251. }