fsfreeze.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Copyright (C) 2017 Denys Vlasenko <vda.linux@googlemail.com>
  4. *
  5. * Licensed under GPLv2, see file LICENSE in this source tree.
  6. */
  7. //config:config FSFREEZE
  8. //config: bool "fsfreeze (3.7 kb)"
  9. //config: default y
  10. //config: select LONG_OPTS
  11. //config: help
  12. //config: Halt new accesses and flush writes on a mounted filesystem.
  13. //applet:IF_FSFREEZE(APPLET_NOEXEC(fsfreeze, fsfreeze, BB_DIR_USR_SBIN, BB_SUID_DROP, fsfreeze))
  14. //kbuild:lib-$(CONFIG_FSFREEZE) += fsfreeze.o
  15. //usage:#define fsfreeze_trivial_usage
  16. //usage: "--[un]freeze MOUNTPOINT"
  17. //usage:#define fsfreeze_full_usage "\n\n"
  18. //usage: "Flush and halt writes to MOUNTPOINT"
  19. #include "libbb.h"
  20. #include <linux/fs.h>
  21. #ifndef FIFREEZE
  22. # define FIFREEZE _IOWR('X', 119, int)
  23. # define FITHAW _IOWR('X', 120, int)
  24. #endif
  25. int fsfreeze_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  26. int fsfreeze_main(int argc UNUSED_PARAM, char **argv)
  27. {
  28. unsigned opts;
  29. int fd;
  30. /* exactly one non-option arg: the mountpoint */
  31. /* one of opts is required */
  32. /* opts are mutually exclusive */
  33. opts = getopt32long(argv, "^"
  34. "" /* no opts */
  35. "\0" "=1:""\xff:\xfe:""\xff--\xfe:\xfe--\xff",
  36. "freeze\0" No_argument "\xff"
  37. "unfreeze\0" No_argument "\xfe"
  38. );
  39. fd = xopen(argv[optind], O_RDONLY);
  40. /* Works with NULL arg on linux-4.8.0 */
  41. xioctl(fd, (opts & 1) ? FIFREEZE : FITHAW, NULL);
  42. return EXIT_SUCCESS;
  43. }