3
0

swaponoff.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 the GPL version 2, see the file LICENSE in this tarball.
  8. */
  9. #include "libbb.h"
  10. #include <mntent.h>
  11. #include <sys/swap.h>
  12. #if ENABLE_FEATURE_MOUNT_LABEL
  13. # include "volume_id.h"
  14. #else
  15. # define resolve_mount_spec(fsname) ((void)0)
  16. #endif
  17. #if ENABLE_FEATURE_SWAPON_PRI
  18. struct globals {
  19. int flags;
  20. };
  21. #define G (*(struct globals*)&bb_common_bufsiz1)
  22. #define g_flags (G.flags)
  23. #else
  24. #define g_flags 0
  25. #endif
  26. static int swap_enable_disable(char *device)
  27. {
  28. int status;
  29. struct stat st;
  30. resolve_mount_spec(&device);
  31. xstat(device, &st);
  32. #if ENABLE_DESKTOP
  33. /* test for holes */
  34. if (S_ISREG(st.st_mode))
  35. if (st.st_blocks * (off_t)512 < st.st_size)
  36. bb_error_msg("warning: swap file has holes");
  37. #endif
  38. if (applet_name[5] == 'n')
  39. status = swapon(device, g_flags);
  40. else
  41. status = swapoff(device);
  42. if (status != 0) {
  43. bb_simple_perror_msg(device);
  44. return 1;
  45. }
  46. return 0;
  47. }
  48. static int do_em_all(void)
  49. {
  50. struct mntent *m;
  51. FILE *f;
  52. int err;
  53. f = setmntent("/etc/fstab", "r");
  54. if (f == NULL)
  55. bb_perror_msg_and_die("/etc/fstab");
  56. err = 0;
  57. while ((m = getmntent(f)) != NULL)
  58. if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0)
  59. err += swap_enable_disable(m->mnt_fsname);
  60. endmntent(f);
  61. return err;
  62. }
  63. int swap_on_off_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  64. int swap_on_off_main(int argc UNUSED_PARAM, char **argv)
  65. {
  66. int ret;
  67. #if !ENABLE_FEATURE_SWAPON_PRI
  68. ret = getopt32(argv, "a");
  69. #else
  70. opt_complementary = "p+";
  71. ret = getopt32(argv, (applet_name[5] == 'n') ? "ap:" : "a", &g_flags);
  72. if (ret & 2) { // -p
  73. g_flags = SWAP_FLAG_PREFER |
  74. ((g_flags & SWAP_FLAG_PRIO_MASK) << SWAP_FLAG_PRIO_SHIFT);
  75. ret &= 1;
  76. }
  77. #endif
  78. if (ret /* & 1: not needed */) // -a
  79. return do_em_all();
  80. argv += optind;
  81. if (!*argv)
  82. bb_show_usage();
  83. /* ret = 0; redundant */
  84. do {
  85. ret += swap_enable_disable(*argv);
  86. } while (*++argv);
  87. return ret;
  88. }