diff options
| author | Mehmet Samet Duman <yongdohyun@projecttick.org> | 2026-04-02 18:25:04 +0300 |
|---|---|---|
| committer | Mehmet Samet Duman <yongdohyun@projecttick.org> | 2026-04-02 18:25:04 +0300 |
| commit | 46f7aa5f7802807c850fcfdd3ec1e2072f7e9d94 (patch) | |
| tree | 338f4a5d3987801657e26d2ccad330be253bf6ab | |
| parent | 80f64ffaf20ed6ee696a2a3981684abeb4923f22 (diff) | |
| parent | f28fddc2e8208bca202d0074d14d2bb914dcc59e (diff) | |
| download | Project-Tick-46f7aa5f7802807c850fcfdd3ec1e2072f7e9d94.tar.gz Project-Tick-46f7aa5f7802807c850fcfdd3ec1e2072f7e9d94.zip | |
Add 'corebinutils/domainname/' from commit 'f28fddc2e8208bca202d0074d14d2bb914dcc59e'
git-subtree-dir: corebinutils/domainname
git-subtree-mainline: 80f64ffaf20ed6ee696a2a3981684abeb4923f22
git-subtree-split: f28fddc2e8208bca202d0074d14d2bb914dcc59e
| -rw-r--r-- | corebinutils/domainname/.gitignore | 25 | ||||
| -rw-r--r-- | corebinutils/domainname/GNUmakefile | 35 | ||||
| -rw-r--r-- | corebinutils/domainname/LICENSE | 32 | ||||
| -rw-r--r-- | corebinutils/domainname/LICENSES/BSD-3-Clause.txt | 11 | ||||
| -rw-r--r-- | corebinutils/domainname/README.md | 25 | ||||
| -rw-r--r-- | corebinutils/domainname/domainname.1 | 66 | ||||
| -rw-r--r-- | corebinutils/domainname/domainname.c | 164 | ||||
| -rw-r--r-- | corebinutils/domainname/tests/test.sh | 163 |
8 files changed, 521 insertions, 0 deletions
diff --git a/corebinutils/domainname/.gitignore b/corebinutils/domainname/.gitignore new file mode 100644 index 0000000000..a74d30b48c --- /dev/null +++ b/corebinutils/domainname/.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/domainname/GNUmakefile b/corebinutils/domainname/GNUmakefile new file mode 100644 index 0000000000..38e7635d1c --- /dev/null +++ b/corebinutils/domainname/GNUmakefile @@ -0,0 +1,35 @@ +.DEFAULT_GOAL := all + +CC ?= cc +CPPFLAGS += -D_GNU_SOURCE +CFLAGS ?= -O2 +CFLAGS += -std=c17 -g -Wall -Wextra -Werror +LDFLAGS ?= +LDLIBS ?= + +OBJDIR := $(CURDIR)/build +OUTDIR := $(CURDIR)/out +TARGET := $(OUTDIR)/domainname +OBJS := $(OBJDIR)/domainname.o + +.PHONY: all clean dirs status test + +all: $(TARGET) + +dirs: + @mkdir -p "$(OBJDIR)" "$(OUTDIR)" + +$(TARGET): $(OBJS) | dirs + $(CC) $(LDFLAGS) -o "$@" $(OBJS) $(LDLIBS) + +$(OBJDIR)/domainname.o: $(CURDIR)/domainname.c | dirs + $(CC) $(CPPFLAGS) $(CFLAGS) -c "$(CURDIR)/domainname.c" -o "$@" + +test: $(TARGET) + CC="$(CC)" DOMAINNAME_BIN="$(TARGET)" sh "$(CURDIR)/tests/test.sh" + +status: + @printf '%s\n' "$(TARGET)" + +clean: + @rm -rf "$(OBJDIR)" "$(OUTDIR)" diff --git a/corebinutils/domainname/LICENSE b/corebinutils/domainname/LICENSE new file mode 100644 index 0000000000..58bfc4a26f --- /dev/null +++ b/corebinutils/domainname/LICENSE @@ -0,0 +1,32 @@ +Copyright (c) 1988, 1993 + The Regents of the University of California. All rights reserved. + +Copyright (c) 2026 + Project Tick. All rights reserved. + +This code is derived from software contributed to Berkeley by +Kevin Fall. + +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.
\ No newline at end of file diff --git a/corebinutils/domainname/LICENSES/BSD-3-Clause.txt b/corebinutils/domainname/LICENSES/BSD-3-Clause.txt new file mode 100644 index 0000000000..ea890afbc7 --- /dev/null +++ b/corebinutils/domainname/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/domainname/README.md b/corebinutils/domainname/README.md new file mode 100644 index 0000000000..b899a8b4a1 --- /dev/null +++ b/corebinutils/domainname/README.md @@ -0,0 +1,25 @@ +# domainname + +Standalone musl-libc-based Linux port of FreeBSD `domainname` 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 Linux-native syscall/API translation rather than preserving FreeBSD libc assumptions. +- Reading the NIS/YP domain uses `uname(2)` and the Linux UTS namespace `domainname` field. +- Setting the NIS/YP domain uses `setdomainname(2)`. +- Linux supports at most 64 bytes for the UTS domain name. Longer FreeBSD inputs are rejected with an explicit error instead of truncation. +- Mutation tests run only inside a private UTS namespace when `unshare(1)` and the required namespace permissions are available; otherwise they are skipped to keep CI/container runs stable. diff --git a/corebinutils/domainname/domainname.1 b/corebinutils/domainname/domainname.1 new file mode 100644 index 0000000000..ed860aee83 --- /dev/null +++ b/corebinutils/domainname/domainname.1 @@ -0,0 +1,66 @@ +.\"- +.\" Copyright (c) 1983, 1988, 1990, 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 22, 2013 +.Dt DOMAINNAME 1 +.Os +.Sh NAME +.Nm domainname +.Nd set or print name of current YP/NIS domain +.Sh SYNOPSIS +.Nm +.Op Ar ypdomain +.Sh DESCRIPTION +The +.Nm +utility prints the name of the current YP/NIS domain. +The super-user can +set the domain name by supplying an argument; this is usually done with the +.Va nisdomainname +variable in the +.Pa /etc/rc.conf +file, normally runs at boot +time. +.Sh NOTES +The YP/NIS (formerly ``Yellow Pages'' but renamed for legal reasons) +domain name does not necessarily have anything to do with the Domain +Name System domain name, although they are often set equal for administrative +convenience. +.Sh SEE ALSO +.Xr getdomainname 3 , +.Xr rc.conf 5 +.Sh HISTORY +The +.Nm +command appeared in +.Fx 1.1 , +based on a similar command in +.Tn SunOS . diff --git a/corebinutils/domainname/domainname.c b/corebinutils/domainname/domainname.c new file mode 100644 index 0000000000..2d136e1ef9 --- /dev/null +++ b/corebinutils/domainname/domainname.c @@ -0,0 +1,164 @@ +/*- + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (c) 1988, 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 _GNU_SOURCE 1 + +#include <sys/utsname.h> + +#include <errno.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +static const char *progname = "domainname"; + +static void die_errno(const char *what) __attribute__((noreturn)); +static void diex(const char *fmt, ...) __attribute__((format(printf, 1, 2), + noreturn)); +static size_t linux_domainname_max(void); +static void parse_args(int argc, char *argv[], const char **domainname); +static void print_domainname(void); +static void set_domainname(const char *domainname); +static void usage(void) __attribute__((noreturn)); + +int +main(int argc, char *argv[]) +{ + const char *domainname; + 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]; + } + + parse_args(argc, argv, &domainname); + if (domainname != NULL) + set_domainname(domainname); + else + print_domainname(); + + return (0); +} + +static void +parse_args(int argc, char *argv[], const char **domainname) +{ + int ch; + + opterr = 0; + while ((ch = getopt(argc, argv, "")) != -1) { + switch (ch) { + case '?': + default: + usage(); + } + } + + argc -= optind; + argv += optind; + + if (argc > 1) + usage(); + + *domainname = (argc == 1) ? argv[0] : NULL; +} + +static void +print_domainname(void) +{ + struct utsname uts; + + if (uname(&uts) != 0) + die_errno("uname"); + + if (fputs(uts.domainname, stdout) == EOF || fputc('\n', stdout) == EOF) + die_errno("stdout"); +} + +static void +set_domainname(const char *domainname) +{ + size_t len; + size_t max_len; + + len = strlen(domainname); + max_len = linux_domainname_max(); + if (len > max_len) { + diex("domain name too long for Linux UTS namespace: %zu bytes given, " + "limit is %zu", len, max_len); + } + + if (setdomainname(domainname, len) != 0) + die_errno("setdomainname"); +} + +static size_t +linux_domainname_max(void) +{ + struct utsname uts; + + return (sizeof(uts.domainname) - 1); +} + +static void +die_errno(const char *what) +{ + fprintf(stderr, "%s: %s: %s\n", progname, what, strerror(errno)); + exit(1); +} + +static void +diex(const char *fmt, ...) +{ + va_list ap; + + fprintf(stderr, "%s: ", progname); + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + + fputc('\n', stderr); + exit(1); +} + +static void +usage(void) +{ + fprintf(stderr, "usage: %s [ypdomain]\n", progname); + exit(1); +} diff --git a/corebinutils/domainname/tests/test.sh b/corebinutils/domainname/tests/test.sh new file mode 100644 index 0000000000..b3ce41cf69 --- /dev/null +++ b/corebinutils/domainname/tests/test.sh @@ -0,0 +1,163 @@ +#!/bin/sh +set -eu + +ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd) +DOMAINNAME_BIN=${DOMAINNAME_BIN:-"$ROOT/out/domainname"} +CC=${CC:-cc} +TMPDIR=${TMPDIR:-/tmp} +WORKDIR=$(mktemp -d "$TMPDIR/domainname-test.XXXXXX") +HELPER_C="$WORKDIR/get-domainname.c" +HELPER_BIN="$WORKDIR/get-domainname" +trap 'rm -rf "$WORKDIR"' EXIT INT TERM + +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_contains() { + name=$1 + text=$2 + pattern=$3 + case $text in + *"$pattern"*) ;; + *) fail "$name" ;; + esac +} + +build_helper() { + cat >"$HELPER_C" <<'EOF' +#define _GNU_SOURCE 1 +#include <sys/utsname.h> + +#include <stdio.h> +#include <stdlib.h> + +int +main(void) +{ + struct utsname uts; + + if (uname(&uts) != 0) + return (1); + if (fputs(uts.domainname, stdout) == EOF || fputc('\n', stdout) == EOF) + return (1); + return (0); +} +EOF + + "$CC" -O2 -std=c17 -Wall -Wextra -Werror "$HELPER_C" -o "$HELPER_BIN" \ + || fail "failed to build helper with $CC" +} + +repeat_a() { + count=$1 + result= + while [ "$count" -gt 0 ]; do + result="${result}a" + count=$((count - 1)) + done + printf '%s' "$result" +} + +run_namespace_mutation_test() { + unshare_script=' + bin=$1 + helper=$2 + + "$bin" "" + [ "$("$helper")" = "" ] + + "$bin" freebsd-linux-port + [ "$("$helper")" = "freebsd-linux-port" ] + + "$bin" -- -leading-dash + [ "$("$helper")" = "-leading-dash" ] + ' + + if ! command -v unshare >/dev/null 2>&1; then + printf '%s\n' "SKIP: mutation test (missing unshare)" >&2 + return 0 + fi + + set +e + output=$(unshare -Ur -u sh -eu -c "$unshare_script" sh "$DOMAINNAME_BIN" \ + "$HELPER_BIN" 2>&1) + status=$? + set -e + + if [ "$status" -eq 0 ]; then + return 0 + fi + + case $output in + *"cannot open /proc/self/uid_map: Permission denied"*|\ + *"unshare: invalid option -- 'r'"*|\ + *"unshare: unrecognized option '--map-root-user'"*) + set +e + output=$(unshare -u sh -eu -c "$unshare_script" sh "$DOMAINNAME_BIN" \ + "$HELPER_BIN" 2>&1) + status=$? + set -e + if [ "$status" -eq 0 ]; then + return 0 + fi + ;; + esac + + case $output in + *"unshare failed: Operation not permitted"*|\ + *"unshare failed: Invalid argument"*|\ + *"unshare failed: No space left on device"*|\ + *"unshare: cannot open /proc/self/ns/uts: Permission denied"*|\ + *"unshare: invalid option"*|\ + *"unshare: unshare failed: Operation not permitted"*|\ + *"unshare: unshare failed: Invalid argument"*|\ + *"unshare: unshare failed: No space left on device"*|\ + *"setdomainname: Operation not permitted"*) + printf '%s\n' "SKIP: mutation test ($output)" >&2 + return 0 + ;; + esac + + fail "namespace mutation test failed: $output" +} + +[ -x "$DOMAINNAME_BIN" ] || fail "missing binary: $DOMAINNAME_BIN" +build_helper + +expected_domain=$("$HELPER_BIN") +actual_domain=$("$DOMAINNAME_BIN") +assert_eq "current domainname" "$expected_domain" "$actual_domain" + +usage_bad_flag=$("$DOMAINNAME_BIN" -x 2>&1 || true) +assert_contains "bad flag should print usage" "$usage_bad_flag" \ + "usage: domainname [ypdomain]" + +usage_too_many=$("$DOMAINNAME_BIN" first second 2>&1 || true) +assert_contains "too many args should print usage" "$usage_too_many" \ + "usage: domainname [ypdomain]" + +long_name=$(repeat_a 65) +long_name_output=$("$DOMAINNAME_BIN" "$long_name" 2>&1 || true) +assert_contains "too-long domain should be rejected" "$long_name_output" \ + "domainname: domain name too long for Linux UTS namespace: 65 bytes given, limit is 64" + +run_namespace_mutation_test + +printf '%s\n' "PASS" |
