xref: /freebsd/tools/test/stress2/misc/killpg4.sh (revision fe75646a0234a261c0013bf1840fdac4acaf0cec)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6# Copyright (c) 2023 Peter Holm <pho@FreeBSD.org>
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27# SUCH DAMAGE.
28#
29
30# Another killpg(2) test scenario. No problems seen.
31
32. ../default.cfg
33export prog=$(basename "$0" .sh)
34set -u
35
36cat > /tmp/$prog.c <<EOF
37#include <sys/wait.h>
38#include <err.h>
39#include <time.h>
40#include <stdlib.h>
41#include <unistd.h>
42#define PARALLEL 2
43
44int
45test(void)
46{
47	pid_t pid;
48	time_t start;
49
50	start = time(NULL);
51	while (time(NULL) - start < 300) {
52		if ((pid = fork()) == -1)
53			err(1, "fork()");
54		if (pid == 0) {
55			if (arc4random() % 100 < 20)
56				usleep(arc4random() % 5000);
57			_exit(0); /* Not reached */
58		}
59		if (waitpid(pid, NULL, 0) != pid)
60			err(1, "waitpid()");
61	}
62	_exit(0);
63}
64
65int
66main(void)
67{
68	pid_t pids[PARALLEL];
69	int i;
70
71	for (i = 0; i < PARALLEL; i++) {
72			test();
73	}
74	for (i = 0; i < PARALLEL; i++) {
75		if (waitpid(pids[i], NULL, 0) != pids[i])
76			err(1, "waotpid() main");
77	}
78
79}
80EOF
81mycc -o /tmp/$prog -Wall -Wextra -O2 /tmp/$prog.c || exit 1
82
83export MAXSWAPPCT=80
84../testcases/swap/swap -t 2m -i 5 > /dev/null 2>&1 &
85sleep .5
86start=`date +%s`
87while [ $((`date +%s` - start)) -lt 120 ]; do
88	for i in `jot 200 100`; do
89		(
90		sl=$prog.$i
91		sleep=/tmp/$sl
92		cp /tmp/$prog $sleep
93		su $testuser -c "$sleep & $sleep & $sleep &" & pid=$!
94		for j in `jot 10`; do
95			pgrep -q "$sl" && break
96			sleep .5
97		done
98		pgrep -q "$sl" || { echo "No start"; exit  1; }
99		pgid=`pgrep "$sl" | xargs ps -jp | sed 1d | \
100		    tail -1 | awk '{print $4}'`
101		[ -z "$pgid" ] && { echo "Zero pgid:$pgid"; ps aj | \
102		    sed -n "1p;/$sl/p"; exit 1; }
103		sleep 1.`jot -r 1 2 9`
104		kill -- -$pgid || { echo "kill -$pgid failed"; exit 1; }
105		wait $pid
106		rm -f $sleep
107		) &
108	done
109	while [ `ps -U$testuser | wc -l` -gt 1 ] ; do sleep 2; done
110done
111while pkill swap; do :; done
112wait
113rm /tmp/$prog /tmp/$prog.c
114exit 0
115