xref: /freebsd/tools/test/stress2/misc/pipe3.sh (revision d0b2dbfa)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6# Copyright (c) 2019 Peter Holm
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# pipe(2) tests.
31# "panic: vm_page_dequeue: queued unlocked page 0xfffffe0019a73518" seen.
32
33# Reported by syzkaller
34# Fixed by r354400
35
36. ../default.cfg
37
38dir=/tmp
39odir=`pwd`
40cd $dir
41sed '1,/^EOF/d' < $odir/$0 > $dir/pipe3.c
42mycc -o pipe3 -Wall -Wextra -O0 -g pipe3.c || exit 1
43rm -f pipe3.c
44cd $odir
45
46(cd ../testcases/swap; ./swap -t 5m -i 20 -l 100) &
47sleep 1
48for i in `jot 25`; do
49	/tmp/pipe3 &
50done
51while pkill -0 pipe3; do sleep 2; done
52while pkill -9 swap; do sleep 1; done
53wait
54rm -rf /tmp/pipe3
55exit 0
56
57EOF
58#include <sys/types.h>
59#include <sys/wait.h>
60
61#include <err.h>
62#include <signal.h>
63#include <stdbool.h>
64#include <stdio.h>
65#include <stdlib.h>
66#include <time.h>
67#include <unistd.h>
68
69#define PARALLEL 64
70#define RUNTIME (5 * 60)
71
72static void
73handler(int i __unused) {
74	_exit(0);
75}
76
77void
78test(void)
79{
80	int fds[2], r;
81	char c;
82
83	if (pipe(fds) == -1)
84		err(1, "pipe");
85
86	if (fork() == 0) {
87		signal(SIGALRM, handler);
88		ualarm(1 + arc4random() % 10000, 0);
89		close(fds[1]);
90		if ((r = write(fds[0], &c, sizeof(c))) != sizeof(c))
91			if (r == -1)
92				err(1, "pipe write");
93		_exit(0);
94	}
95	signal(SIGALRM, handler);
96	ualarm(1 + arc4random() % 10000, 0);
97	close(fds[0]);
98	if ((r = read(fds[1], &c, sizeof(c))) != sizeof(c))
99		if (r == -1)
100			err(1, "pipe read");
101	wait(NULL);
102	_exit(0);
103}
104
105int
106main(void)
107{
108	pid_t pids[PARALLEL], rpid;
109	time_t start;
110	int i, running, status;
111	bool done;
112
113	for (i = 0; i < PARALLEL; i++)
114		pids[i] = 0;
115	running = 0;
116	start = time(NULL);
117	for (;;) {
118		done = (time(NULL) - start) >= RUNTIME;
119		for (i = 0; i < PARALLEL; i++) {
120			if (pids[i] == 0 && !done) {
121				if ((pids[i] = fork()) == 0)
122					test();
123				if (pids[i] == -1)
124					err(1, "fork()");
125				running++;
126			}
127		}
128		for (i = 0; i < PARALLEL; i++) {
129			if (pids[i] != 0) {
130				if ((rpid = waitpid(pids[i], &status,
131				    WNOHANG)) == -1)
132					err(1, "waitpid(%d)", pids[i]);
133				if (rpid == 0)
134					continue;
135				if (rpid != pids[i])
136					err(1, "waitpid(%d)", pids[i]);
137				running --;
138				pids[i] = 0;
139				break;
140			}
141		}
142		if (running == 0 && done)
143			break;
144		usleep(100);
145	}
146
147	return (0);
148}
149