2000-02-08 19:58:47 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
1999-10-20 22:08:37 +00:00
|
|
|
/*
|
|
|
|
* Mini sync implementation for busybox
|
|
|
|
*
|
|
|
|
* Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
|
2015-07-21 19:50:48 +02:00
|
|
|
* Copyright (C) 2015 by Ari Sundholm <ari@tuxera.com>
|
1999-10-20 22:08:37 +00:00
|
|
|
*
|
2010-08-16 20:14:46 +02:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
1999-10-20 22:08:37 +00:00
|
|
|
*/
|
2015-07-21 19:50:48 +02:00
|
|
|
//config:config SYNC
|
2017-07-18 22:01:24 +02:00
|
|
|
//config: bool "sync (769 bytes)"
|
2015-07-21 19:50:48 +02:00
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 09:50:55 +02:00
|
|
|
//config: sync is used to flush filesystem buffers.
|
2015-07-21 19:50:48 +02:00
|
|
|
//config:config FEATURE_SYNC_FANCY
|
2016-07-19 17:48:55 +02:00
|
|
|
//config: bool "Enable -d and -f flags (requires syncfs(2) in libc)"
|
2015-07-21 19:50:48 +02:00
|
|
|
//config: default y
|
|
|
|
//config: depends on SYNC
|
|
|
|
//config: help
|
2017-07-21 09:50:55 +02:00
|
|
|
//config: sync -d FILE... executes fdatasync() on each FILE.
|
|
|
|
//config: sync -f FILE... executes syncfs() on each FILE.
|
2015-07-21 19:50:48 +02:00
|
|
|
|
|
|
|
//applet:IF_SYNC(APPLET_NOFORK(sync, sync, BB_DIR_BIN, BB_SUID_DROP, sync))
|
2003-03-19 09:13:01 +00:00
|
|
|
|
2016-11-23 14:46:56 +01:00
|
|
|
//kbuild:lib-$(CONFIG_SYNC) += sync.o
|
|
|
|
|
|
|
|
/* BB_AUDIT SUSv3 N/A -- Matches GNU behavior. */
|
|
|
|
|
2011-03-31 14:43:25 +02:00
|
|
|
//usage:#define sync_trivial_usage
|
2015-07-21 19:50:48 +02:00
|
|
|
//usage: ""IF_FEATURE_SYNC_FANCY("[-df] [FILE]...")
|
2011-03-31 14:43:25 +02:00
|
|
|
//usage:#define sync_full_usage "\n\n"
|
2015-07-21 19:50:48 +02:00
|
|
|
//usage: IF_NOT_FEATURE_SYNC_FANCY(
|
2011-03-31 14:43:25 +02:00
|
|
|
//usage: "Write all buffered blocks to disk"
|
2015-07-21 19:50:48 +02:00
|
|
|
//usage: )
|
|
|
|
//usage: IF_FEATURE_SYNC_FANCY(
|
|
|
|
//usage: "Write all buffered blocks (in FILEs) to disk"
|
|
|
|
//usage: "\n -d Avoid syncing metadata"
|
|
|
|
//usage: "\n -f Sync filesystems underlying FILEs"
|
|
|
|
//usage: )
|
2011-03-31 14:43:25 +02:00
|
|
|
|
2007-05-26 19:00:18 +00:00
|
|
|
#include "libbb.h"
|
1999-10-05 16:24:54 +00:00
|
|
|
|
2007-04-10 15:43:37 +00:00
|
|
|
/* This is a NOFORK applet. Be very careful! */
|
|
|
|
|
2007-10-11 10:05:36 +00:00
|
|
|
int sync_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2010-01-13 22:43:13 +01:00
|
|
|
int sync_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
|
2000-07-14 23:28:47 +00:00
|
|
|
{
|
2015-07-21 19:50:48 +02:00
|
|
|
#if !ENABLE_FEATURE_SYNC_FANCY
|
2008-03-17 09:00:54 +00:00
|
|
|
/* coreutils-6.9 compat */
|
2010-01-04 13:16:08 +01:00
|
|
|
bb_warn_ignoring_args(argv[1]);
|
2001-02-16 10:21:35 +00:00
|
|
|
sync();
|
2006-11-27 16:49:31 +00:00
|
|
|
return EXIT_SUCCESS;
|
2015-07-21 19:50:48 +02:00
|
|
|
#else
|
|
|
|
unsigned opts;
|
|
|
|
int ret = EXIT_SUCCESS;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
OPT_DATASYNC = (1 << 0),
|
|
|
|
OPT_SYNCFS = (1 << 1),
|
|
|
|
};
|
|
|
|
|
2017-08-08 21:55:02 +02:00
|
|
|
opts = getopt32(argv, "^" "df" "\0" "d--f:f--d");
|
2015-07-21 19:50:48 +02:00
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
/* Handle the no-argument case. */
|
|
|
|
if (!argv[0])
|
|
|
|
sync();
|
|
|
|
|
|
|
|
while (*argv) {
|
|
|
|
int fd = open_or_warn(*argv, O_RDONLY);
|
|
|
|
|
|
|
|
if (fd < 0) {
|
|
|
|
ret = EXIT_FAILURE;
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
if (opts & OPT_DATASYNC) {
|
|
|
|
if (fdatasync(fd))
|
|
|
|
goto err;
|
|
|
|
goto do_close;
|
|
|
|
}
|
|
|
|
if (opts & OPT_SYNCFS) {
|
|
|
|
/*
|
|
|
|
* syncfs is documented to only fail with EBADF,
|
|
|
|
* which can't happen here. So, no error checks.
|
|
|
|
*/
|
|
|
|
syncfs(fd);
|
|
|
|
goto do_close;
|
|
|
|
}
|
|
|
|
if (fsync(fd)) {
|
|
|
|
err:
|
|
|
|
bb_simple_perror_msg(*argv);
|
|
|
|
ret = EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
do_close:
|
|
|
|
close(fd);
|
|
|
|
next:
|
|
|
|
++argv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
#endif
|
1999-10-05 16:24:54 +00:00
|
|
|
}
|