swaponoff.c 8.8 KB

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