swaponoff.c 9.1 KB

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