3
0

swaponoff.c 9.0 KB

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