From nobody Mon Jul 31 07:07:58 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4RDq8J4HzHz4psql; Mon, 31 Jul 2023 07:07:59 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4RDq7z2JNNz3KSL; Mon, 31 Jul 2023 07:07:59 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1690787279; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=TFq8zBeg8xL6P0BoYanux9zAajYUj8GYoREfZWZ2Pwo=; b=sFDB/PyRwi0U41FKVVu4+sI0zIsPoOXiS23nk0uy8fUzKxtTJFdrAXYXFx5U2sq7dnSgy9 j1beWbutZ8FigLzDWU4Ks4bDjyEPN2CKYuwZmmeXz9DLAn+em+UrlXiyhuHeeqqha8jMsL 7i3fNLS0AMse+C5dxTSOD6hkP8/zELp0jFJQZ3+dzy9nXMt7Tid+6rwrd4oz05GqnlWKG+ zTQKanu2wTOdEJVjlo+6MWHVFpUdfVLKIY1pI3CI1pb1MpxuXmEDoR4RoPSV9bvzK6mRqa qb35i+d3a1RxvG8hKUYp8g2dU3yUV1bJzkVBClZQIvfGyR2EtQWE5T595o5OPQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1690787279; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=TFq8zBeg8xL6P0BoYanux9zAajYUj8GYoREfZWZ2Pwo=; b=VivcwoDsOZRI8cRxiE32ReiKZUUVnbRoNeqfzMk6eh5vZXjIlKzTGIM/yaNRAX38OVbsFS IOcXHnemKHgM+AP216E/w9ihaaSsXbZTX46C/AdjQTsXAnG/v1hrLiUTvb/eRPq1JFDueH 5Fumk2HgBJwT3JvPbkZGcJYt80wFvceBFqJRAj/5xCUzfYBHNrU7NjbD2Mg03bruMBzDlC Fiv9t8HPBLEJi0p95uxiLnyjjVVYCxXyh3OOOnxIsF/06lFm3anFR/IhEPnvNixz4zlcEs 20DIXGLswSq6DtnFiU6Smw48fldzpBE/n7l/vglhlV7UgBetVU0oi3i+wCQ38g== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1690787279; a=rsa-sha256; cv=none; b=Y1s198S14flhx+yOUH/jWGyHojSSpd8jMxfB2cA7GHO6Iwe9WNwJgps2jtXx8oMPaCsj6P E1Cp8+clf5QdpYg2HeCBCL7IqA68MfiZ746+EecGzuy96GcluJxZig25mpLYN3+TX87fqU o0H5IbVq6GM2CkU7X4cPyIhtTpMOkYYeFHsC6tbBVbBPgWEtwLeC/BXahWzNehRmePEyZo VUVeI9Fb1rjT/JKwpMeEVeAIcc3xl+sU0KUjUYnkf7KzdqAqUQX9Z2XfPRiR+GI9tXG8Dc WNVifTk8jbQ9vd3USqHiJBmxWClsgbZh2sBq+LKlIsQ8v9jmkvKvH5BCxEBSOQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4RDq7y5Km4zmVN; Mon, 31 Jul 2023 07:07:58 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 36V77w4H014587; Mon, 31 Jul 2023 07:07:58 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 36V77wCd014586; Mon, 31 Jul 2023 07:07:58 GMT (envelope-from git) Date: Mon, 31 Jul 2023 07:07:58 GMT Message-Id: <202307310707.36V77wCd014586@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Peter Holm Subject: git: 896809e29675 - main - stress2: Added killpg(2) scenarios List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: pho X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 896809e296752c54884bde3b04cce213938dfa26 Auto-Submitted: auto-generated The branch main has been updated by pho: URL: https://cgit.FreeBSD.org/src/commit/?id=896809e296752c54884bde3b04cce213938dfa26 commit 896809e296752c54884bde3b04cce213938dfa26 Author: Peter Holm AuthorDate: 2023-07-31 07:07:06 +0000 Commit: Peter Holm CommitDate: 2023-07-31 07:07:06 +0000 stress2: Added killpg(2) scenarios --- tools/test/stress2/misc/killpg2.sh | 197 +++++++++++++++++++++++++++++++++++++ tools/test/stress2/misc/killpg3.sh | 192 ++++++++++++++++++++++++++++++++++++ tools/test/stress2/misc/killpg4.sh | 114 +++++++++++++++++++++ 3 files changed, 503 insertions(+) diff --git a/tools/test/stress2/misc/killpg2.sh b/tools/test/stress2/misc/killpg2.sh new file mode 100755 index 000000000000..5e986f059637 --- /dev/null +++ b/tools/test/stress2/misc/killpg2.sh @@ -0,0 +1,197 @@ +#!/bin/sh + +# +# SPDX-License-Identifier: BSD-2-Clause +# +# Copyright (c) 2023 Peter Holm +# +# 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. +# + +# killpg(2) version of reaper.sh. No problems seen. + +. ../default.cfg + +prog=$(basename "$0" .sh) +cat > /tmp/$prog.c < +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +static volatile u_int *share; + +#define CONT 0 +#define GID 1 +#define SYNC 2 +#define MAXP 10000 + +static void +hand(int i __unused) { + _exit(0); +} + +static void +looper(void) +{ + struct sigaction sa; + time_t start; + struct passwd *pw; + pid_t pids[MAXP]; + int i, parallel; + + setproctitle("looper"); + sa.sa_handler = SIG_IGN; + sigemptyset(&sa.sa_mask); + sa.sa_flags = 0; + if (sigaction(SIGUSR1, &sa, NULL) == -1) + err(1, "sigaction"); + + if ((pw = getpwnam("TUSER")) == NULL) + err(1, "no such user: TUSER"); + + if (setgroups(1, &pw->pw_gid) || + setegid(pw->pw_gid) || setgid(pw->pw_gid) || + seteuid(pw->pw_uid) || setuid(pw->pw_uid)) + err(1, "Can't drop privileges to \"TUSER\""); + endpwent(); + setpgrp(0, 0); + share[GID] = getpgrp(); + share[SYNC] = 1; + start = time(NULL); + while (time(NULL) - start < 120) { + parallel = arc4random() % MAXP + 1; + for (i = 0; i < parallel; i++) { + if ((pids[i] = fork()) == 0) { + sa.sa_handler = hand; + sigemptyset(&sa.sa_mask); + sa.sa_flags = 0; + if (sigaction(SIGUSR1, &sa, NULL) == -1) + err(1, "sigaction"); + setproctitle("child"); + for (;;) + pause(); + _exit(0); /* never reached */ + } + if (pids[i] == -1) + err(1, "fork()"); + } + for (i = 0; i < parallel; i++) { + if (waitpid(pids[i], NULL, 0) != pids[i]) + err(1, "waitpid(%d) in looper", pids[i]); + } + } + _exit(0); +} + +static void +killer(void) +{ + int e, gid; + + while (share[SYNC] == 0) + ; + gid = share[GID]; + while (share[CONT] == 1) { + usleep(arc4random() % 50000); + gid = share[GID]; + if (gid != 0) { + e = 0; + while (e == 0) { + if (killpg(gid, SIGUSR1) == -1) { + e = 1; + if (errno != ESRCH) + err(1, "pgkill(%d)", gid); + } + usleep(5000 + arc4random() % 5000); + } + } + } + _exit(0); +} + +int +main(void) +{ + size_t len; + time_t start; + int lpid, kpid, s1, s2; + + len = PAGE_SIZE; + if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED) + err(1, "mmap"); + + start = time(NULL); + while (time(NULL) - start < 120) { + share[CONT] = 1; + share[GID] = 0; + share[SYNC] = 0; + if ((lpid = fork()) == 0) + looper(); + usleep(arc4random() % 100000); + if ((kpid = fork()) == 0) + killer(); + + if (waitpid(lpid, &s1, 0) != lpid) + err(1, "waitpid looper"); + usleep(arc4random() % 1000); + share[CONT] = 0; + waitpid(kpid, &s2, 0); + } + + return (0); +} +EOF +sed -i '' "s#TUSER#$testuser#" /tmp/$prog.c +cc -o /tmp/$prog -Wall -Wextra -O0 /tmp/$prog.c || exit 1 +rm /tmp/$prog.c + +n=1 +start=`date +%s` +while true; do + /tmp/$prog + for i in `jot 50`; do + pgrep -q $prog || break + sleep .5 + done + if pgrep -q $prog; then + e=$((`date +%s` - start)) + echo "Failed in loop #$n after $e seconds." + pgrep "$prog|timeout" | xargs ps -jp + pkill $prog + rm -f /tmp/$prog + exit 1 + fi + [ $((`date +%s` - start)) -ge 600 ] && break + n=$((n + 1)) +done +rm /tmp/$prog +exit 0 diff --git a/tools/test/stress2/misc/killpg3.sh b/tools/test/stress2/misc/killpg3.sh new file mode 100755 index 000000000000..304b3e320f2f --- /dev/null +++ b/tools/test/stress2/misc/killpg3.sh @@ -0,0 +1,192 @@ +#!/bin/sh + +# +# SPDX-License-Identifier: BSD-2-Clause +# +# Copyright (c) 2023 Peter Holm +# +# 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. +# + +# killpg(2) version of reaper.sh. No problems seen. + +. ../default.cfg + +prog=$(basename "$0" .sh) +cat > /tmp/$prog.c < +#include +#include + + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static _Atomic(int) *share; + +#define GID 0 +#define PARALLEL 10 +#define RDY 1 +#define MAXP 7000 + +static void +hand(int i __unused) { + _exit(0); +} + +static void +innerloop(int parallel) +{ + pid_t pids[MAXP]; + struct sigaction sa; + int i; + + usleep(1000); + for (i = 0; i < parallel; i++) { + if ((pids[i] = fork()) == 0) { + sa.sa_handler = hand; + sigemptyset(&sa.sa_mask); + sa.sa_flags = 0; + if (sigaction(SIGUSR1, &sa, NULL) == -1) + err(1, "sigaction"); + atomic_fetch_add(&share[RDY], 1); + setproctitle("child"); + for (;;) + pause(); + _exit(0); /* never reached */ + } + if (pids[i] == -1) + err(1, "fork()"); + } + for (i = 0; i < parallel; i++) { + if (waitpid(pids[i], NULL, 0) != pids[i]) + err(1, "waitpid(%d) in looper", pids[i]); + } + _exit(0); +} + +static void +looper(void) +{ + struct sigaction sa; + struct passwd *pw; + pid_t pids[MAXP]; + int i, parallel; + + setproctitle("looper"); + sa.sa_handler = SIG_IGN; + sigemptyset(&sa.sa_mask); + sa.sa_flags = 0; + if (sigaction(SIGUSR1, &sa, NULL) == -1) + err(1, "sigaction"); + + if ((pw = getpwnam("TUSER")) == NULL) + err(1, "no such user: TUSER"); + + if (setgroups(1, &pw->pw_gid) || + setegid(pw->pw_gid) || setgid(pw->pw_gid) || + seteuid(pw->pw_uid) || setuid(pw->pw_uid)) + err(1, "Can't drop privileges to \"TUSER\""); + endpwent(); + setpgrp(0, 0); + share[GID] = getpgrp(); + parallel = arc4random() % MAXP + 1; + parallel = parallel / PARALLEL * PARALLEL; + for (i = 0; i < PARALLEL; i++) { + if ((pids[i] = fork()) == 0) + innerloop(parallel / PARALLEL); + } + while (atomic_load(&share[RDY]) != parallel) + usleep(10000); + if (killpg(share[GID], SIGUSR1) == -1) + err(1, "pgkill(%d)", share[GID]); + for (i = 0; i < 4; i++) { + if (waitpid(pids[i], NULL, 0) != pids[i]) + err(1, "waitpid(%d) in looper", pids[i]); + } + _exit(0); +} + +int +main(void) +{ + size_t len; + time_t start; + int lpid, s1; + + len = PAGE_SIZE; + if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED) + err(1, "mmap"); + + start = time(NULL); + while (time(NULL) - start < 120) { + share[GID] = 0; + share[RDY] = 0; + if ((lpid = fork()) == 0) + looper(); + if (waitpid(lpid, &s1, 0) != lpid) + err(1, "waitpid looper"); + } + + return (0); +} +EOF +sed -i '' "s#TUSER#$testuser#" /tmp/$prog.c +mycc -o /tmp/$prog -Wall -Wextra -O0 /tmp/$prog.c || exit 1 +rm /tmp/$prog.c + +export MAXSWAPPCT=70 +n=1 +start=`date +%s` +while true; do + ../testcases/swap/swap -t 2m -i 20 > /dev/null & + /tmp/$prog & pid=$! + st=`date +%s` + while kill -0 $pid > /dev/null 2>&1; do + e=$((`date +%s` - st)) + if [ $e -ge 120 ]; then + while pgrep -q swap; do pkill swap; done + fi + if [ $e -ge 600 ]; then + echo "Failed in loop #$n after $e seconds." + ps -jU$testuser | head -20 + kill $pid + pkill -U$testuser + wait + rm -f /tmp/$prog + exit 1 + fi + done + wait + [ $((`date +%s` - start)) -ge 300 ] && break + n=$((n + 1)) +done +rm /tmp/$prog +exit 0 diff --git a/tools/test/stress2/misc/killpg4.sh b/tools/test/stress2/misc/killpg4.sh new file mode 100755 index 000000000000..448a7f60dd4b --- /dev/null +++ b/tools/test/stress2/misc/killpg4.sh @@ -0,0 +1,114 @@ +#!/bin/sh + +# +# SPDX-License-Identifier: BSD-2-Clause +# +# Copyright (c) 2023 Peter Holm +# +# 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. +# + +# Another killpg(2) test scenario. No problems seen. + +. ../default.cfg +export prog=$(basename "$0" .sh) +set -u + +cat > /tmp/$prog.c < +#include +#include +#include +#include +#define PARALLEL 2 + +int +test(void) +{ + pid_t pid; + time_t start; + + start = time(NULL); + while (time(NULL) - start < 300) { + if ((pid = fork()) == -1) + err(1, "fork()"); + if (pid == 0) { + if (arc4random() % 100 < 20) + usleep(arc4random() % 5000); + _exit(0); /* Not reached */ + } + if (waitpid(pid, NULL, 0) != pid) + err(1, "waitpid()"); + } + _exit(0); +} + +int +main(void) +{ + pid_t pids[PARALLEL]; + int i; + + for (i = 0; i < PARALLEL; i++) { + test(); + } + for (i = 0; i < PARALLEL; i++) { + if (waitpid(pids[i], NULL, 0) != pids[i]) + err(1, "waotpid() main"); + } + +} +EOF +mycc -o /tmp/$prog -Wall -Wextra -O2 /tmp/$prog.c || exit 1 + +export MAXSWAPPCT=80 +../testcases/swap/swap -t 2m -i 5 > /dev/null 2>&1 & +sleep .5 +start=`date +%s` +while [ $((`date +%s` - start)) -lt 120 ]; do + for i in `jot 200 100`; do + ( + sl=$prog.$i + sleep=/tmp/$sl + cp /tmp/$prog $sleep + su $testuser -c "$sleep & $sleep & $sleep &" & pid=$! + for j in `jot 10`; do + pgrep -q "$sl" && break + sleep .5 + done + pgrep -q "$sl" || { echo "No start"; exit 1; } + pgid=`pgrep "$sl" | xargs ps -jp | sed 1d | \ + tail -1 | awk '{print $4}'` + [ -z "$pgid" ] && { echo "Zero pgid:$pgid"; ps aj | \ + sed -n "1p;/$sl/p"; exit 1; } + sleep 1.`jot -r 1 2 9` + kill -- -$pgid || { echo "kill -$pgid failed"; exit 1; } + wait $pid + rm -f $sleep + ) & + done + while [ `ps -U$testuser | wc -l` -gt 1 ] ; do sleep 2; done +done +while pkill swap; do :; done +wait +rm /tmp/$prog /tmp/$prog.c +exit 0