sync.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini sync implementation for busybox
  4. *
  5. * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
  6. * Copyright (C) 2015 by Ari Sundholm <ari@tuxera.com>
  7. *
  8. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  9. */
  10. //config:config SYNC
  11. //config: bool "sync (4 kb)"
  12. //config: default y
  13. //config: help
  14. //config: sync is used to flush filesystem buffers.
  15. //config:config FEATURE_SYNC_FANCY
  16. //config: bool "Enable -d and -f flags (requires syncfs(2) in libc)"
  17. //config: default y
  18. //config: depends on SYNC
  19. //config: help
  20. //config: sync -d FILE... executes fdatasync() on each FILE.
  21. //config: sync -f FILE... executes syncfs() on each FILE.
  22. // APPLET_NOFORK:name main location suid_type help
  23. //applet:IF_SYNC(APPLET_NOFORK(sync, sync, BB_DIR_BIN, BB_SUID_DROP, sync))
  24. //kbuild:lib-$(CONFIG_SYNC) += sync.o
  25. /* BB_AUDIT SUSv3 N/A -- Matches GNU behavior. */
  26. //usage:#define sync_trivial_usage
  27. //usage: ""IF_FEATURE_SYNC_FANCY("[-df] [FILE]...")
  28. //usage:#define sync_full_usage "\n\n"
  29. //usage: IF_NOT_FEATURE_SYNC_FANCY(
  30. //usage: "Write all buffered blocks to disk"
  31. //usage: )
  32. //usage: IF_FEATURE_SYNC_FANCY(
  33. //usage: "Write all buffered blocks (in FILEs) to disk"
  34. //usage: "\n -d Avoid syncing metadata"
  35. //usage: "\n -f Sync filesystems underlying FILEs"
  36. //usage: )
  37. #include "libbb.h"
  38. /* This is a NOFORK applet. Be very careful! */
  39. #if ENABLE_FEATURE_SYNC_FANCY || ENABLE_FSYNC
  40. static int sync_common(int opts, char **argv)
  41. {
  42. int ret;
  43. enum {
  44. OPT_DATASYNC = (1 << 0),
  45. OPT_SYNCFS = (1 << 1),
  46. };
  47. ret = EXIT_SUCCESS;
  48. do {
  49. /* GNU "sync FILE" uses O_NONBLOCK open */
  50. int fd = open_or_warn(*argv, /*O_NOATIME |*/ O_NOCTTY | O_RDONLY | O_NONBLOCK);
  51. /* open(NOATIME) can only be used by owner or root, don't use NOATIME here */
  52. if (fd < 0) {
  53. ret = EXIT_FAILURE;
  54. goto next;
  55. }
  56. # if ENABLE_FEATURE_SYNC_FANCY
  57. if (opts & OPT_SYNCFS) {
  58. /*
  59. * syncfs is documented to only fail with EBADF,
  60. * which can't happen here. So, no error checks.
  61. */
  62. syncfs(fd);
  63. } else
  64. # endif
  65. if (((opts & OPT_DATASYNC) ? fdatasync(fd) : fsync(fd)) != 0) {
  66. bb_simple_perror_msg(*argv);
  67. ret = EXIT_FAILURE;
  68. }
  69. close(fd);
  70. next:
  71. argv++;
  72. } while (*argv);
  73. return ret;
  74. }
  75. #endif
  76. #if ENABLE_SYNC
  77. int sync_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  78. int sync_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
  79. {
  80. # if !ENABLE_FEATURE_SYNC_FANCY
  81. /* coreutils-6.9 compat */
  82. bb_warn_ignoring_args(argv[1]);
  83. sync();
  84. return EXIT_SUCCESS;
  85. # else
  86. unsigned opts = getopt32(argv, "^" "df" "\0" "d--f:f--d");
  87. argv += optind;
  88. if (!argv[0]) {
  89. sync();
  90. return EXIT_SUCCESS;
  91. }
  92. return sync_common(opts, argv);
  93. # endif
  94. }
  95. #endif
  96. /*
  97. * Mini fsync implementation for busybox
  98. *
  99. * Copyright (C) 2008 Nokia Corporation. All rights reserved.
  100. *
  101. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  102. */
  103. //config:config FSYNC
  104. //config: bool "fsync (3.8 kb)"
  105. //config: default y
  106. //config: help
  107. //config: fsync is used to flush file-related cached blocks to disk.
  108. // APPLET_NOFORK:name main location suid_type help
  109. //applet:IF_FSYNC(APPLET_NOFORK(fsync, fsync, BB_DIR_BIN, BB_SUID_DROP, fsync))
  110. //kbuild:lib-$(CONFIG_FSYNC) += sync.o
  111. //usage:#define fsync_trivial_usage
  112. //usage: "[-d] FILE..."
  113. //usage:#define fsync_full_usage "\n\n"
  114. //usage: "Write all buffered blocks in FILEs to disk\n"
  115. //usage: "\n -d Avoid syncing metadata"
  116. #if ENABLE_FSYNC
  117. int fsync_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  118. int fsync_main(int argc UNUSED_PARAM, char **argv)
  119. {
  120. int opts = getopt32(argv, "^" "d" "\0" "-1"/*min 1 arg*/);
  121. argv += optind;
  122. return sync_common(opts, argv);
  123. }
  124. #endif