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