swaponoff.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. static int swap_enable_disable(char *device)
  13. {
  14. int status;
  15. struct stat st;
  16. xstat(device, &st);
  17. #if ENABLE_DESKTOP
  18. /* test for holes */
  19. if (S_ISREG(st.st_mode))
  20. if (st.st_blocks * 512 < st.st_size)
  21. bb_error_msg("warning: swap file has holes");
  22. #endif
  23. if (applet_name[5] == 'n')
  24. status = swapon(device, 0);
  25. else
  26. status = swapoff(device);
  27. if (status != 0) {
  28. bb_simple_perror_msg(device);
  29. return 1;
  30. }
  31. return 0;
  32. }
  33. static int do_em_all(void)
  34. {
  35. struct mntent *m;
  36. FILE *f;
  37. int err;
  38. f = setmntent("/etc/fstab", "r");
  39. if (f == NULL)
  40. bb_perror_msg_and_die("/etc/fstab");
  41. err = 0;
  42. while ((m = getmntent(f)) != NULL)
  43. if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0)
  44. err += swap_enable_disable(m->mnt_fsname);
  45. endmntent(f);
  46. return err;
  47. }
  48. int swap_on_off_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  49. int swap_on_off_main(int argc, char **argv)
  50. {
  51. int ret;
  52. if (argc == 1)
  53. bb_show_usage();
  54. ret = getopt32(argv, "a");
  55. if (ret)
  56. return do_em_all();
  57. /* ret = 0; redundant */
  58. while (*++argv)
  59. ret += swap_enable_disable(*argv);
  60. return ret;
  61. }