fsfreeze.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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.5 kb)"
  9. //config: default y
  10. //config: select PLATFORM_LINUX
  11. //config: select LONG_OPTS
  12. //config: help
  13. //config: Halt new accesses and flush writes on a mounted filesystem.
  14. //applet:IF_FSFREEZE(APPLET_NOEXEC(fsfreeze, fsfreeze, BB_DIR_USR_SBIN, BB_SUID_DROP, fsfreeze))
  15. //kbuild:lib-$(CONFIG_FSFREEZE) += fsfreeze.o
  16. //usage:#define fsfreeze_trivial_usage
  17. //usage: "--[un]freeze MOUNTPOINT"
  18. //usage:#define fsfreeze_full_usage "\n\n"
  19. //usage: "Flush and halt writes to MOUNTPOINT"
  20. #include "libbb.h"
  21. #include <linux/fs.h>
  22. #ifndef FIFREEZE
  23. # define FIFREEZE _IOWR('X', 119, int)
  24. # define FITHAW _IOWR('X', 120, int)
  25. #endif
  26. int fsfreeze_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  27. int fsfreeze_main(int argc UNUSED_PARAM, char **argv)
  28. {
  29. unsigned opts;
  30. int fd;
  31. /* exactly one non-option arg: the mountpoint */
  32. /* one of opts is required */
  33. /* opts are mutually exclusive */
  34. opts = getopt32long(argv, "^"
  35. "" /* no opts */
  36. "\0" "=1:""\xff:\xfe:""\xff--\xfe:\xfe--\xff",
  37. "freeze\0" No_argument "\xff"
  38. "unfreeze\0" No_argument "\xfe"
  39. );
  40. fd = xopen(argv[optind], O_RDONLY);
  41. /* Works with NULL arg on linux-4.8.0 */
  42. xioctl(fd, (opts & 1) ? FIFREEZE : FITHAW, NULL);
  43. return EXIT_SUCCESS;
  44. }