xref: /freebsd/tools/test/stress2/misc/pause.sh (revision 257e70f1d5ee61037c8c59b116538d3b6b1427a2)
1#!/bin/sh
2
3#
4# Copyright (c) 2017 Dell EMC Isilon
5# All rights reserved.
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
29# Hunt for lost wakeup problem.
30
31. ../default.cfg
32
33dir=/tmp
34odir=`pwd`
35cd $dir
36sed '1,/^EOF/d' < $odir/$0 > $dir/pause.c
37mycc -o pause -Wall -Wextra -O0 -g pause.c || exit 1
38rm -f pause.c
39
40pkill pause
41$dir/pause &
42pid=$!
43start=`date +%s`
44while pgrep -q pause; do
45	sleep .5
46	[ $((`date +%s` - $start)) -gt 1200 ] &&
47		{ echo "Timed out"; pgrep pause | xargs ps -lp; exit 1; }
48done
49wait $pid
50s=$?
51
52cd $odir
53rm -rf $dir/pause
54exit $s
55
56EOF
57#include <sys/param.h>
58#include <sys/mman.h>
59#include <sys/stat.h>
60#include <sys/wait.h>
61
62#include <machine/atomic.h>
63
64#include <err.h>
65#include <errno.h>
66#include <fcntl.h>
67#include <signal.h>
68#include <stdio.h>
69#include <stdlib.h>
70#include <time.h>
71#include <unistd.h>
72
73static volatile u_int *share;
74
75#define PARALLEL 400
76#define RUNTIME (5 * 60)
77#define SYNC 0
78
79void
80hand(int i __unused) {
81}
82
83static void
84test(int idx)
85{
86	pid_t pid;
87	time_t start;
88
89	atomic_add_int(&share[SYNC], 1);
90	while (share[SYNC] != PARALLEL)
91		usleep(1);
92	share[idx] = 0;
93
94	if ((pid = fork()) == 0) {
95		share[idx] = 1;
96		for (;;)
97			pause();
98		_exit(0);
99	}
100	while (share[idx] == 0)
101		usleep(10);
102	start = time(NULL);
103	while (time(NULL) - start < 60) {
104		usleep(arc4random() % 100);
105		if (kill(pid, SIGHUP) == -1)
106			err(1, "kill(%d)", pid);
107	}
108	kill(pid, SIGTERM);
109	if (waitpid(pid, NULL, 0) != pid)
110		err(1, "waitpid(%d)", pid);
111
112	_exit(0);
113}
114
115int
116main(void)
117{
118	pid_t pids[PARALLEL];
119	struct sigaction sa;
120	size_t len;
121	time_t start;
122	int e, i, status;
123
124	sa.sa_handler = hand;
125	sigemptyset(&sa.sa_mask);
126	sa.sa_flags = 0;
127	if (sigaction(SIGHUP, &sa, NULL) == -1)
128		err(1, "sigaction");
129
130	e = 0;
131	len = PAGE_SIZE;
132	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
133	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
134		err(1, "mmap");
135
136	start = time(NULL);
137	while ((time(NULL) - start) < RUNTIME && e == 0) {
138		share[SYNC] = 0;
139		for (i = 0; i < PARALLEL; i++) {
140			if ((pids[i] = fork()) == 0)
141				test(i + 1);
142			if (pids[i] == -1)
143				err(1, "fork()");
144		}
145		for (i = 0; i < PARALLEL; i++) {
146			if (waitpid(pids[i], &status, 0) == -1)
147				err(1, "waitpid(%d)", pids[i]);
148			if (status != 0) {
149				if (WIFSIGNALED(status))
150					fprintf(stderr,
151					    "pid %d exit signal %d\n",
152					    pids[i], WTERMSIG(status));
153			}
154			e += status == 0 ? 0 : 1;
155		}
156	}
157
158	return (e);
159}
160