swaponoff.c 7.1 KB

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