summaryrefslogtreecommitdiff
path: root/corebinutils/sync
diff options
context:
space:
mode:
Diffstat (limited to 'corebinutils/sync')
-rw-r--r--corebinutils/sync/.gitignore25
-rw-r--r--corebinutils/sync/GNUmakefile35
-rw-r--r--corebinutils/sync/LICENSE29
-rw-r--r--corebinutils/sync/LICENSES/BSD-3-Clause.txt11
-rw-r--r--corebinutils/sync/README.md24
-rw-r--r--corebinutils/sync/sync.8101
-rw-r--r--corebinutils/sync/sync.c85
-rw-r--r--corebinutils/sync/tests/test.sh188
8 files changed, 498 insertions, 0 deletions
diff --git a/corebinutils/sync/.gitignore b/corebinutils/sync/.gitignore
new file mode 100644
index 0000000000..a74d30b48c
--- /dev/null
+++ b/corebinutils/sync/.gitignore
@@ -0,0 +1,25 @@
+*.a
+*.core
+*.lo
+*.nossppico
+*.o
+*.orig
+*.pico
+*.pieo
+*.po
+*.rej
+*.so
+*.so.[0-9]*
+*.sw[nop]
+*~
+.*DS_Store
+.cache
+.clangd
+.ccls-cache
+.depend*
+compile_commands.json
+compile_commands.events.json
+tags
+build/
+out/
+.linux-obj/
diff --git a/corebinutils/sync/GNUmakefile b/corebinutils/sync/GNUmakefile
new file mode 100644
index 0000000000..7d7c229083
--- /dev/null
+++ b/corebinutils/sync/GNUmakefile
@@ -0,0 +1,35 @@
+.DEFAULT_GOAL := all
+
+CC ?= cc
+CPPFLAGS += -D_POSIX_C_SOURCE=200809L
+CFLAGS ?= -O2
+CFLAGS += -std=c17 -g -Wall -Wextra -Werror
+LDFLAGS ?=
+LDLIBS ?=
+
+OBJDIR := $(CURDIR)/build
+OUTDIR := $(CURDIR)/out
+TARGET := $(OUTDIR)/sync
+OBJS := $(OBJDIR)/sync.o
+
+.PHONY: all clean dirs status test
+
+all: $(TARGET)
+
+dirs:
+ @mkdir -p "$(OBJDIR)" "$(OUTDIR)"
+
+$(TARGET): $(OBJS) | dirs
+ $(CC) $(LDFLAGS) -o "$@" $(OBJS) $(LDLIBS)
+
+$(OBJDIR)/sync.o: $(CURDIR)/sync.c | dirs
+ $(CC) $(CPPFLAGS) $(CFLAGS) -c "$(CURDIR)/sync.c" -o "$@"
+
+test: $(TARGET)
+ CC="$(CC)" SYNC_BIN="$(TARGET)" sh "$(CURDIR)/tests/test.sh"
+
+status:
+ @printf '%s\n' "$(TARGET)"
+
+clean:
+ @rm -rf "$(OBJDIR)" "$(OUTDIR)"
diff --git a/corebinutils/sync/LICENSE b/corebinutils/sync/LICENSE
new file mode 100644
index 0000000000..a5f0bec4fc
--- /dev/null
+++ b/corebinutils/sync/LICENSE
@@ -0,0 +1,29 @@
+Copyright (c) 1987, 1993
+ The Regents of the University of California. All rights reserved.
+
+Copyright (c) 2026
+ Project Tick. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+3. Neither the name of the University nor the names of its contributors
+ may be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
diff --git a/corebinutils/sync/LICENSES/BSD-3-Clause.txt b/corebinutils/sync/LICENSES/BSD-3-Clause.txt
new file mode 100644
index 0000000000..ea890afbc7
--- /dev/null
+++ b/corebinutils/sync/LICENSES/BSD-3-Clause.txt
@@ -0,0 +1,11 @@
+Copyright (c) <year> <owner>.
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+
+2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+
+3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/corebinutils/sync/README.md b/corebinutils/sync/README.md
new file mode 100644
index 0000000000..0edac75259
--- /dev/null
+++ b/corebinutils/sync/README.md
@@ -0,0 +1,24 @@
+# sync
+
+Standalone musl-libc-friendly Linux port of FreeBSD `sync` for Project Tick BSD/Linux Distribution.
+
+## Build
+
+```sh
+gmake -f GNUmakefile
+gmake -f GNUmakefile CC=musl-gcc
+```
+
+## Test
+
+```sh
+gmake -f GNUmakefile test
+gmake -f GNUmakefile test CC=musl-gcc
+```
+
+## Notes
+
+- Port strategy is direct Linux-native syscall/API mapping, not preservation of FreeBSD build glue or a BSD compatibility shim.
+- FreeBSD `sync(8)` is specified as a no-argument whole-system flush and is mapped directly to Linux `sync(2)`.
+- GNU/coreutils-style operands and options such as `sync FILE...`, `-d`, and `-f` are intentionally unsupported. `sync.8` does not define them, and file-scoped flushing would require `fsync(2)` or `syncfs(2)` on explicit file descriptors, which is not equivalent to the documented utility semantics.
+- On Linux, as on FreeBSD, `sync(2)` does not provide per-file error reporting to this utility. Successful execution therefore means the process reached and invoked the kernel-wide flush entry point.
diff --git a/corebinutils/sync/sync.8 b/corebinutils/sync/sync.8
new file mode 100644
index 0000000000..ef2ed6f627
--- /dev/null
+++ b/corebinutils/sync/sync.8
@@ -0,0 +1,101 @@
+.\"-
+.\" Copyright (c) 1980, 1991, 1993
+.\" The Regents of the University of California. All rights reserved.
+.\"
+.\" Copyright (c) 2026 Project Tick. All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\" notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\" notice, this list of conditions and the following disclaimer in the
+.\" documentation and/or other materials provided with the distribution.
+.\" 3. Neither the name of the University nor the names of its contributors
+.\" may be used to endorse or promote products derived from this software
+.\" without specific prior written permission.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.Dd April 25, 2024
+.Dt SYNC 8
+.Os
+.Sh NAME
+.Nm sync
+.Nd force completion of pending disk writes (flush cache)
+.Sh SYNOPSIS
+.Nm
+.Sh DESCRIPTION
+The
+.Nm
+utility
+can be called to ensure that all disk writes have been completed before the
+processor is halted in a way not suitably done by
+.Xr reboot 8
+or
+.Xr halt 8 .
+Generally, it is preferable to use
+.Xr reboot 8
+or
+.Xr halt 8
+to shut down the system,
+as they may perform additional actions
+such as resynchronizing the hardware clock
+and flushing internal caches before performing a final
+.Nm .
+.Pp
+The
+.Nm
+utility utilizes the
+.Xr sync 2
+function call.
+.Sh SEE ALSO
+.Xr fsync 2 ,
+.Xr sync 2 ,
+.Xr syncer 4 ,
+.Xr halt 8 ,
+.Xr reboot 8
+.Sh HISTORY
+A
+.Nm
+utility appeared in
+.At v4 .
+.Pp
+On systems older than
+.Bx 4.0 ,
+commands like
+.Xr reboot 8
+and
+.Xr halt 8
+were unavailable.
+The shutdown procedure involved running
+.Nm ,
+waiting for the lights to stop,
+and turning off the machine.
+.Pp
+Issuing three separate
+.Nm
+commands (one line each) was a placebo that would generally suffice in
+.At v7
+machines that were otherwise quiesced systems.
+It replaced the one-per-line
+.Nm
+as a substitute for waiting.
+.Pp
+.Bx 4.0
+introduced
+.Xr reboot 2
+and
+.Xr sync 2
+which rendered this trick obsolete.
diff --git a/corebinutils/sync/sync.c b/corebinutils/sync/sync.c
new file mode 100644
index 0000000000..c1ff6f783b
--- /dev/null
+++ b/corebinutils/sync/sync.c
@@ -0,0 +1,85 @@
+/*-
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ * Copyright (c) 1987, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Copyright (c) 2026
+ * Project Tick. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#define _POSIX_C_SOURCE 200809L
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+/*
+ * Linux libcs commonly hide sync(2) behind non-POSIX feature macros.
+ * Declare the documented interface explicitly so the port stays buildable
+ * under strict C/POSIX settings without enabling GNU extensions.
+ */
+extern void sync(void);
+
+static const char *progname = "sync";
+
+static void usage_error(const char *fmt, ...)
+ __attribute__((format(printf, 1, 2), noreturn));
+
+int
+main(int argc, char *argv[])
+{
+ const char *slash;
+
+ if (argv[0] != NULL && argv[0][0] != '\0') {
+ slash = strrchr(argv[0], '/');
+ progname = (slash != NULL && slash[1] != '\0') ? slash + 1 : argv[0];
+ }
+
+ if (argc > 1)
+ usage_error("unexpected argument: %s", argv[1]);
+
+ sync();
+ return (0);
+}
+
+static void
+usage_error(const char *fmt, ...)
+{
+ va_list ap;
+
+ fprintf(stderr, "%s: ", progname);
+
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+
+ fprintf(stderr, "\nusage: %s\n", progname);
+ exit(1);
+}
diff --git a/corebinutils/sync/tests/test.sh b/corebinutils/sync/tests/test.sh
new file mode 100644
index 0000000000..b38f16fa34
--- /dev/null
+++ b/corebinutils/sync/tests/test.sh
@@ -0,0 +1,188 @@
+#!/bin/sh
+set -eu
+
+ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
+SYNC_BIN=${SYNC_BIN:-"$ROOT/out/sync"}
+CC=${CC:-cc}
+TMPDIR=${TMPDIR:-/tmp}
+WORKDIR=$(mktemp -d "$TMPDIR/sync-test.XXXXXX")
+HOOK_C="$WORKDIR/sync-hook.c"
+HOOK_SO="$WORKDIR/sync-hook.so"
+STDOUT_FILE="$WORKDIR/stdout"
+STDERR_FILE="$WORKDIR/stderr"
+SYNC_MARKER="$WORKDIR/sync-marker"
+trap 'rm -rf "$WORKDIR"' EXIT INT TERM
+
+export LC_ALL=C
+
+fail() {
+ printf '%s\n' "FAIL: $1" >&2
+ exit 1
+}
+
+assert_eq() {
+ name=$1
+ expected=$2
+ actual=$3
+ if [ "$expected" != "$actual" ]; then
+ printf '%s\n' "FAIL: $name" >&2
+ printf '%s\n' "--- expected ---" >&2
+ printf '%s' "$expected" >&2
+ printf '\n%s\n' "--- actual ---" >&2
+ printf '%s' "$actual" >&2
+ printf '\n' >&2
+ exit 1
+ fi
+}
+
+assert_empty() {
+ name=$1
+ text=$2
+ if [ -n "$text" ]; then
+ printf '%s\n' "FAIL: $name" >&2
+ printf '%s\n' "--- expected empty ---" >&2
+ printf '%s\n' "--- actual ---" >&2
+ printf '%s' "$text" >&2
+ printf '\n' >&2
+ exit 1
+ fi
+}
+
+assert_status() {
+ name=$1
+ expected=$2
+ actual=$3
+ if [ "$expected" -ne "$actual" ]; then
+ printf '%s\n' "FAIL: $name" >&2
+ printf '%s\n' "expected status: $expected" >&2
+ printf '%s\n' "actual status: $actual" >&2
+ exit 1
+ fi
+}
+
+run_capture() {
+ if "$@" >"$STDOUT_FILE" 2>"$STDERR_FILE"; then
+ LAST_STATUS=0
+ else
+ LAST_STATUS=$?
+ fi
+
+ LAST_STDOUT=$(cat "$STDOUT_FILE")
+ LAST_STDERR=$(cat "$STDERR_FILE")
+}
+
+build_sync_hook() {
+ cat >"$HOOK_C" <<'EOF'
+#define _GNU_SOURCE 1
+
+#include <dlfcn.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+typedef void (*sync_fn_t)(void);
+
+static void
+write_marker(const char *path)
+{
+ static const char marker[] = "sync-called\n";
+ size_t offset;
+ int fd;
+
+ if (path == NULL || path[0] == '\0')
+ return;
+
+ fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);
+ if (fd < 0)
+ return;
+
+ offset = 0;
+ while (offset < sizeof(marker) - 1) {
+ ssize_t written;
+
+ written = write(fd, marker + offset, sizeof(marker) - 1 - offset);
+ if (written < 0) {
+ if (errno == EINTR)
+ continue;
+ break;
+ }
+ offset += (size_t)written;
+ }
+
+ (void)close(fd);
+}
+
+void
+sync(void)
+{
+ static sync_fn_t real_sync;
+ static int resolved;
+
+ if (!resolved) {
+ real_sync = (sync_fn_t)dlsym(RTLD_NEXT, "sync");
+ resolved = 1;
+ }
+
+ write_marker(getenv("SYNC_HOOK_OUTPUT"));
+ if (real_sync != NULL)
+ real_sync();
+}
+EOF
+
+ "$CC" -shared -fPIC -O2 -std=c17 -Wall -Wextra -Werror \
+ "$HOOK_C" -o "$HOOK_SO" -ldl || fail "failed to build sync hook with $CC"
+}
+
+[ -x "$SYNC_BIN" ] || fail "missing binary: $SYNC_BIN"
+build_sync_hook
+
+run_capture "$SYNC_BIN"
+assert_status "sync status" 0 "$LAST_STATUS"
+assert_empty "sync stdout" "$LAST_STDOUT"
+assert_empty "sync stderr" "$LAST_STDERR"
+
+run_capture "$SYNC_BIN" unexpected
+assert_status "operand status" 1 "$LAST_STATUS"
+assert_empty "operand stdout" "$LAST_STDOUT"
+assert_eq "operand stderr" \
+ "sync: unexpected argument: unexpected
+usage: sync" \
+ "$LAST_STDERR"
+
+run_capture "$SYNC_BIN" --
+assert_status "double dash status" 1 "$LAST_STATUS"
+assert_empty "double dash stdout" "$LAST_STDOUT"
+assert_eq "double dash stderr" \
+ "sync: unexpected argument: --
+usage: sync" \
+ "$LAST_STDERR"
+
+run_capture "$SYNC_BIN" first second
+assert_status "too many args status" 1 "$LAST_STATUS"
+assert_empty "too many args stdout" "$LAST_STDOUT"
+assert_eq "too many args stderr" \
+ "sync: unexpected argument: first
+usage: sync" \
+ "$LAST_STDERR"
+
+rm -f "$SYNC_MARKER"
+run_capture env LD_PRELOAD="$HOOK_SO" SYNC_HOOK_OUTPUT="$SYNC_MARKER" "$SYNC_BIN"
+assert_status "hooked sync status" 0 "$LAST_STATUS"
+assert_empty "hooked sync stdout" "$LAST_STDOUT"
+assert_empty "hooked sync stderr" "$LAST_STDERR"
+[ -f "$SYNC_MARKER" ] || fail "sync() was not invoked"
+assert_eq "hook marker" "sync-called" "$(cat "$SYNC_MARKER")"
+
+rm -f "$SYNC_MARKER"
+run_capture env LD_PRELOAD="$HOOK_SO" SYNC_HOOK_OUTPUT="$SYNC_MARKER" \
+ "$SYNC_BIN" unexpected
+assert_status "hooked invalid status" 1 "$LAST_STATUS"
+assert_empty "hooked invalid stdout" "$LAST_STDOUT"
+assert_eq "hooked invalid stderr" \
+ "sync: unexpected argument: unexpected
+usage: sync" \
+ "$LAST_STDERR"
+[ ! -e "$SYNC_MARKER" ] || fail "sync() ran on invalid arguments"
+
+printf '%s\n' "PASS"