swaponoff.c 2.6 KB

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