swaponoff.c 6.8 KB

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