git: e7437ae907c8 - main - rc: Start testing the rc(8) framework (beginning with *_oomprotect)

From: Mateusz Piotrowski <0mp_at_FreeBSD.org>
Date: Tue, 26 Jul 2022 13:41:28 UTC
The branch main has been updated by 0mp (doc, ports committer):

URL: https://cgit.FreeBSD.org/src/commit/?id=e7437ae907c89bf85a99c5cbb7ddd194a1ff1354

commit e7437ae907c89bf85a99c5cbb7ddd194a1ff1354
Author:     Mateusz Piotrowski <0mp@FreeBSD.org>
AuthorDate: 2022-07-07 18:24:27 +0000
Commit:     Mateusz Piotrowski <0mp@FreeBSD.org>
CommitDate: 2022-07-26 13:39:39 +0000

    rc: Start testing the rc(8) framework (beginning with *_oomprotect)
    
    This change adds 2 tests to make sure that the *_oomprotect variable
    sets the protection against OOM killer properly within rc(8) scripts.
    
    This is also adding the first tests for the rc(8) framework. More tests
    will be added as we go.
    
    PR:             256148
    Approved by:    des
    MFC after:      2 weeks
    Differential Revision:  https://reviews.freebsd.org/D35745
---
 etc/mtree/BSD.tests.dist         |   2 +
 libexec/rc/Makefile              |   3 ++
 libexec/rc/tests/Makefile        |   3 ++
 libexec/rc/tests/rc_subr_test.sh | 103 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 111 insertions(+)

diff --git a/etc/mtree/BSD.tests.dist b/etc/mtree/BSD.tests.dist
index e2dbc478ca64..19b0ccc25936 100644
--- a/etc/mtree/BSD.tests.dist
+++ b/etc/mtree/BSD.tests.dist
@@ -426,6 +426,8 @@
             atf-sh
             ..
         ..
+        rc
+        ..
         rtld-elf
         ..
         tftpd
diff --git a/libexec/rc/Makefile b/libexec/rc/Makefile
index 974eb8661182..5c35ab677223 100644
--- a/libexec/rc/Makefile
+++ b/libexec/rc/Makefile
@@ -24,4 +24,7 @@ CONFETCDEFAULTSPACKAGE=	rc
 
 SUBDIR+=	rc.d
 
+HAS_TESTS=
+SUBDIR.${MK_TESTS}+=	tests
+
 .include <bsd.prog.mk>
diff --git a/libexec/rc/tests/Makefile b/libexec/rc/tests/Makefile
new file mode 100644
index 000000000000..c44c6db90b77
--- /dev/null
+++ b/libexec/rc/tests/Makefile
@@ -0,0 +1,3 @@
+ATF_TESTS_SH+=  rc_subr_test
+
+.include <bsd.test.mk>
diff --git a/libexec/rc/tests/rc_subr_test.sh b/libexec/rc/tests/rc_subr_test.sh
new file mode 100644
index 000000000000..0ca790e39a14
--- /dev/null
+++ b/libexec/rc/tests/rc_subr_test.sh
@@ -0,0 +1,103 @@
+#
+# Copyright 2022 Mateusz Piotrowski <0mp@FreeBSD.org>
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# 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.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
+#
+
+atf_test_case oomprotect_all
+oomprotect_all_head()
+{
+	atf_set "descr" "Verify that \${name}_oomprotect=all protects " \
+		"the command and all its current and future children"
+	atf_set "require.user" "root" # For protect(1).
+}
+
+oomprotect_all_body()
+{
+	__name="$(atf_get ident)"
+	__pidfile="$(mktemp -t "${__name}.pid")"
+	__childpidfile="$(mktemp -t "${__name}.childpid")"
+	__script=$(mktemp -t "${__name}.script")
+
+	cat >> "$__script" <<-'LITERAL'
+	. /etc/rc.subr
+	name="$1"
+	pidfile="$2"
+	_childpidfile="$3"
+	_rc_arg="$4"
+	setvar "${name}_oomprotect" all
+	command="/usr/sbin/daemon"
+	command_args="-P $pidfile -p $_childpidfile -- /bin/sleep 5"
+	run_rc_command "$_rc_arg"
+	LITERAL
+
+	atf_check -s exit:0 -o inline:"Starting ${__name}.\n" -e empty \
+		/bin/sh "$__script" "$__name" "$__pidfile" "$__childpidfile" onestart
+	atf_check -s exit:0 -o match:'^..1..... .......1$' -e empty \
+		ps -p "$(cat "$__pidfile")" -ax -o flags,flags2
+	atf_check -s exit:0 -o match:'^..1..... .......1$' -e empty \
+		ps -p "$(cat "$__childpidfile")" -ax -o flags,flags2
+	atf_check -s exit:0 -o ignore -e empty \
+		/bin/sh "$__script" "$__name" "$__pidfile" "$__childpidfile" onestop
+}
+
+atf_test_case oomprotect_yes
+oomprotect_yes_head()
+{
+	atf_set "descr" "Verify that \${name}_oomprotect=yes protects " \
+		"the command but not its children"
+	atf_set "require.user" "root" # For protect(1).
+}
+
+oomprotect_yes_body()
+{
+	__name="$(atf_get ident)"
+	__pidfile="$(mktemp -t "${__name}.pid")"
+	__script=$(mktemp -t "${__name}.script")
+
+	cat >> "$__script" <<-'LITERAL'
+	. /etc/rc.subr
+	name="$1"
+	pidfile="$2"
+	_rc_arg="$3"
+	setvar "${name}_oomprotect" yes
+	procname="/bin/sleep"
+	command="/usr/sbin/daemon"
+	command_args="-p $pidfile -- $procname 5"
+	run_rc_command "$_rc_arg"
+	LITERAL
+
+	atf_check -s exit:0 -o inline:"Starting ${__name}.\n" -e empty \
+		/bin/sh "$__script" "$__name" "$__pidfile" onestart
+	atf_check -s exit:0 -o match:'^..1..... .......0$' -e empty \
+		ps -p "$(cat "$__pidfile")" -ax -o flags,flags2
+	atf_check -s exit:0 -o ignore -e empty \
+		/bin/sh "$__script" "$__name" "$__pidfile" onestop
+}
+
+atf_init_test_cases()
+{
+	atf_add_test_case oomprotect_all
+	atf_add_test_case oomprotect_yes
+}