3
0

swaponoff.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <stdio.h>
  23. #include <mntent.h>
  24. #include <dirent.h>
  25. #include <errno.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include <sys/mount.h>
  29. #include <sys/swap.h>
  30. #include "busybox.h"
  31. static int whichApp; /* default SWAPON_APP */
  32. static const int SWAPON_APP = 0;
  33. static const int SWAPOFF_APP = 1;
  34. static int swap_enable_disable(const char *device)
  35. {
  36. int status;
  37. struct stat st;
  38. if (stat(device, &st) < 0) {
  39. bb_perror_msg_and_die("cannot stat %s", device);
  40. }
  41. /* test for holes */
  42. if (S_ISREG(st.st_mode)) {
  43. if (st.st_blocks * 512 < st.st_size) {
  44. bb_error_msg_and_die("swap file has holes");
  45. }
  46. }
  47. if (whichApp == SWAPON_APP)
  48. status = swapon(device, 0);
  49. else
  50. status = swapoff(device);
  51. if (status != 0) {
  52. bb_perror_msg("%s", device);
  53. return EXIT_FAILURE;
  54. }
  55. return EXIT_SUCCESS;
  56. }
  57. static int do_em_all(void)
  58. {
  59. struct mntent *m;
  60. FILE *f = setmntent("/etc/fstab", "r");
  61. int err = 0;
  62. if (f == NULL)
  63. bb_perror_msg_and_die("/etc/fstab");
  64. while ((m = getmntent(f)) != NULL) {
  65. if (strcmp(m->mnt_type, MNTTYPE_SWAP)==0) {
  66. if(swap_enable_disable(m->mnt_fsname) == EXIT_FAILURE)
  67. err++;
  68. }
  69. }
  70. endmntent(f);
  71. return err;
  72. }
  73. extern int swap_on_off_main(int argc, char **argv)
  74. {
  75. if (bb_applet_name[5] == 'f') { /* "swapoff" */
  76. whichApp = SWAPOFF_APP;
  77. }
  78. if (argc != 2) {
  79. goto usage_and_exit;
  80. }
  81. argc--;
  82. argv++;
  83. /* Parse any options */
  84. while (**argv == '-') {
  85. while (*++(*argv))
  86. switch (**argv) {
  87. case 'a':
  88. {
  89. struct stat statBuf;
  90. if (stat("/etc/fstab", &statBuf) < 0)
  91. bb_error_msg_and_die("/etc/fstab file missing");
  92. }
  93. return do_em_all();
  94. break;
  95. default:
  96. goto usage_and_exit;
  97. }
  98. }
  99. return swap_enable_disable(*argv);
  100. usage_and_exit:
  101. bb_show_usage();
  102. }