xref: /freebsd/libexec/rc/tests/rc_subr_test.sh (revision 1ddff51060ad759e35dcc4716b0bdcdb40255862)
1#-
2# SPDX-License-Identifier: BSD-2-Clause
3#
4# Copyright 2022 Mateusz Piotrowski <0mp@FreeBSD.org>
5# Copyright (c) 2025 Klara, Inc.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28
29atf_test_case no_cycles
30no_cycles_head()
31{
32	atf_set "descr" "Verify that /etc/rc.d/* contains no cycles"
33}
34
35no_cycles_body()
36{
37	atf_check -e empty -o ignore rcorder /etc/rc.d/*
38}
39
40atf_test_case oomprotect_all
41oomprotect_all_head()
42{
43	atf_set "descr" "Verify that \${name}_oomprotect=all protects " \
44		"the command and all its current and future children"
45	atf_set "require.user" "root" # For protect(1).
46}
47
48oomprotect_all_body()
49{
50	if [ "$(sysctl -n security.jail.jailed)" != 0 ]; then
51		atf_skip "protect(1) cannot be used in a jail"
52	fi
53
54	__name="$(atf_get ident)"
55	__pidfile="$(mktemp -t "${__name}.pid")"
56	__childpidfile="$(mktemp -t "${__name}.childpid")"
57	__script=$(mktemp -t "${__name}.script")
58
59	cat >> "$__script" <<-'LITERAL'
60	. /etc/rc.subr
61	name="$1"
62	pidfile="$2"
63	_childpidfile="$3"
64	_rc_arg="$4"
65	setvar "${name}_oomprotect" all
66	command="/usr/sbin/daemon"
67	command_args="-P $pidfile -p $_childpidfile -- /bin/sleep 60"
68	run_rc_command "$_rc_arg"
69	LITERAL
70
71	atf_check -s exit:0 -o inline:"Starting ${__name}.\n" -e empty \
72		/bin/sh "$__script" "$__name" "$__pidfile" "$__childpidfile" onestart
73	atf_check -s exit:0 -o match:'^..1..... .......1$' -e empty \
74		ps -p "$(cat "$__pidfile")" -o flags,flags2
75	atf_check -s exit:0 -o match:'^..1..... .......1$' -e empty \
76		ps -p "$(cat "$__childpidfile")" -o flags,flags2
77	atf_check -s exit:0 -o ignore -e empty \
78		/bin/sh "$__script" "$__name" "$__pidfile" "$__childpidfile" onestop
79}
80
81atf_test_case oomprotect_yes
82oomprotect_yes_head()
83{
84	atf_set "descr" "Verify that \${name}_oomprotect=yes protects " \
85		"the command but not its children"
86	atf_set "require.user" "root" # For protect(1).
87}
88
89oomprotect_yes_body()
90{
91	if [ "$(sysctl -n security.jail.jailed)" != 0 ]; then
92		atf_skip "protect(1) cannot be used in a jail"
93	fi
94
95	__name="$(atf_get ident)"
96	__pidfile="$(mktemp -t "${__name}.pid")"
97	__script=$(mktemp -t "${__name}.script")
98
99	cat >> "$__script" <<-'LITERAL'
100	. /etc/rc.subr
101	name="$1"
102	pidfile="$2"
103	_rc_arg="$3"
104	setvar "${name}_oomprotect" yes
105	procname="/bin/sleep"
106	command="/usr/sbin/daemon"
107	command_args="-p $pidfile -- $procname 60"
108	run_rc_command "$_rc_arg"
109	LITERAL
110
111	atf_check -s exit:0 -o inline:"Starting ${__name}.\n" -e empty \
112		/bin/sh "$__script" "$__name" "$__pidfile" onestart
113	atf_check -s exit:0 -o match:'^..1..... .......0$' -e empty \
114		ps -p "$(cat "$__pidfile")" -ax -o flags,flags2
115	atf_check -s exit:0 -o ignore -e empty \
116		/bin/sh "$__script" "$__name" "$__pidfile" onestop
117}
118
119atf_test_case wait_for_pids_progress
120wait_for_pids_progress_head()
121{
122	atf_set "descr" "Verify that wait_for_pids prints progress updates"
123}
124wait_for_pids_progress_body()
125{
126	cat >>script <<'EOF'
127. /etc/rc.subr
128sleep 15 &
129a=$!
130sleep 10 &
131b=$!
132sleep 5 &
133c=$!
134wait_for_pids $a $b $c
135EOF
136	re="^Waiting for PIDS: [0-9]+ [0-9]+ [0-9]+"
137	re="${re}, [0-9]+ [0-9]+"
138	re="${re}, [0-9]+\.$"
139	atf_check -s exit:0 -o match:"${re}" /bin/sh script
140}
141
142atf_init_test_cases()
143{
144	atf_add_test_case no_cycles
145	atf_add_test_case oomprotect_all
146	atf_add_test_case oomprotect_yes
147	atf_add_test_case wait_for_pids_progress
148}
149