The branch main has been updated by kevans:

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

commit c82203e65d08036d25ed117f2617ef4ad07d6f97
Author:     Kyle Evans <kev...@freebsd.org>
AuthorDate: 2025-06-24 23:04:11 +0000
Commit:     Kyle Evans <kev...@freebsd.org>
CommitDate: 2025-07-10 17:54:20 +0000

    lockf: tests: add tests for the -p and -T features
    
    Reviewed by:    des
    Differential Revision:  https://reviews.freebsd.org/D51026
---
 usr.bin/lockf/tests/lockf_test.sh | 95 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 95 insertions(+)

diff --git a/usr.bin/lockf/tests/lockf_test.sh 
b/usr.bin/lockf/tests/lockf_test.sh
index 0cdf7dae6f57..823b5673a176 100644
--- a/usr.bin/lockf/tests/lockf_test.sh
+++ b/usr.bin/lockf/tests/lockf_test.sh
@@ -31,6 +31,24 @@
 : ${EX_CANTCREAT:=73}
 : ${EX_TEMPFAIL:=75}
 
+waitlock()
+{
+       local cur lockfile tmo
+
+       lockfile="$1"
+
+       cur=0
+       tmo=20
+
+       while [ "$cur" -lt "$tmo" -a ! -f "$lockfile" ]; do
+               sleep 0.1
+               cur=$((cur + 1))
+       done
+
+       atf_check_not_equal "$cur" "$tmo"
+}
+
+
 atf_test_case badargs
 badargs_body()
 {
@@ -196,6 +214,52 @@ needfile_body()
        atf_check test "$tpass" -lt 10
 }
 
+atf_test_case termchild
+termchild_body()
+{
+       lockf -kp testlock sleep 30 &
+       lpid=$!
+
+       waitlock testlock
+
+       atf_check -o file:testlock pgrep -F testlock
+
+       start=$(date +"%s")
+       atf_check kill -TERM "$lpid"
+       wait "$lpid"
+       end=$(date +"%s")
+       elapsed=$((end - start))
+
+       if [ "$elapsed" -gt 5 ]; then
+               atf_fail "lockf seems to have dodged the SIGTERM ($elapsed 
passed)"
+       fi
+
+       # We didn't start lockf with -T this time, so the process should not
+       # have been terminated.
+       atf_check -o file:testlock pgrep -F testlock
+
+       lockf -kpT testlock sleep 30 &
+       lpid=$!
+
+       waitlock testlock
+
+       atf_check -o file:testlock pgrep -F testlock
+
+       start=$(date +"%s")
+       atf_check kill -TERM "$lpid"
+       wait "$lpid"
+       end=$(date +"%s")
+       elapsed=$((end - start))
+
+       if [ "$elapsed" -gt 5 ]; then
+               atf_fail "lockf -T seems to have dodged the SIGTERM ($elapsed 
passed)"
+       fi
+
+       # This time, it should have terminated (notably much earlier than our
+       # 30 second timeout).
+       atf_check -o empty -e not-empty -s not-exit:0 pgrep -F testlock
+}
+
 atf_test_case timeout
 timeout_body()
 {
@@ -219,6 +283,34 @@ timeout_body()
        wait "$lpid" || true
 }
 
+atf_test_case writepid
+writepid_body()
+{
+       lockf -p "testlock" sleep 10 &
+       lpid=$!
+
+       waitlock "testlock"
+
+       atf_check test -s testlock
+       atf_check -o file:testlock pgrep -F testlock
+       atf_check -o file:testlock pgrep -F testlock -fx "sleep 10"
+       atf_check pkill -TERM -F testlock
+
+       wait
+
+       atf_check test ! -f testlock
+}
+
+atf_test_case writepid_keep
+writepid_keep_body()
+{
+       # Check that we'll clobber any existing contents (a pid, usually)
+       # once we acquire the lock.
+       jot -b A -s "" 64 > testlock
+       atf_check lockf -kp testlock sleep 0
+       atf_check -o not-match:"A" cat testlock
+}
+
 atf_test_case wrlock
 wrlock_head()
 {
@@ -244,6 +336,9 @@ atf_init_test_cases()
        atf_add_test_case fdlock
        atf_add_test_case keep
        atf_add_test_case needfile
+       atf_add_test_case termchild
        atf_add_test_case timeout
+       atf_add_test_case writepid
+       atf_add_test_case writepid_keep
        atf_add_test_case wrlock
 }

Reply via email to