xref: /freebsd/tools/test/stress2/misc/sigstop2.sh (revision 8eb2bee6c0f4957c6c1cea826e59cda4d18a2a64)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5#
6# Copyright (c) 2018 Dell EMC Isilon
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# No problems seen.
31
32. ../default.cfg
33
34dir=/tmp
35odir=`pwd`
36cd $dir
37sed '1,/^EOF/d' < $odir/$0 > $dir/sigstop2.c
38mycc -o sigstop2 -Wall -Wextra -O0 -g sigstop2.c || exit 1
39rm -f sigstop2.c
40
41$dir/sigstop2
42s=$?
43[ -f sigstop2.core -a $s -eq 0 ] &&
44    { ls -l sigstop2.core; mv sigstop2.core $dir; s=1; }
45
46rm -rf $dir/sigstop2
47exit $s
48
49EOF
50#include <sys/param.h>
51#include <sys/mman.h>
52#include <sys/stat.h>
53#include <sys/wait.h>
54
55#include <machine/atomic.h>
56
57#include <err.h>
58#include <errno.h>
59#include <fcntl.h>
60#include <signal.h>
61#include <stdio.h>
62#include <stdlib.h>
63#include <time.h>
64#include <unistd.h>
65
66static volatile u_int *share;
67
68#define N 100
69#define PARALLEL 4
70#define RUNTIME (1 * 60)
71#define SYNC 0
72
73static void
74test(void)
75{
76	pid_t pids[N];
77	int i;
78
79	atomic_add_int(&share[SYNC], 1);
80	while (share[SYNC] != PARALLEL)
81		;
82
83	for (i = 0; i < N; i++) {
84		if ((pids[i] = fork()) == 0) {
85			for (;;)
86				pause();
87			_exit(0);
88		}
89	}
90
91	for (i = 0; i < N; i++)
92		if (kill(pids[i], SIGSTOP) == -1)
93			err(1, "kill(%d)", pids[i]);
94	for (i = 0; i < N; i++)
95		if (kill(pids[i], SIGINT) == -1)
96			err(1, "kill(%d)", pids[i]);
97	for (i = 0; i < N; i++)
98		if (kill(pids[i], SIGKILL) == -1)
99			err(1, "kill(%d)", pids[i]);
100	for (i = 0; i < N; i++)
101		if (waitpid(pids[i], NULL, 0) != pids[i])
102			err(1, "waitpid(%d)", pids[i]);
103
104	_exit(0);
105}
106
107int
108main(void)
109{
110	pid_t pids[PARALLEL];
111	size_t len;
112	time_t start;
113	int e, i, status;
114
115	e = 0;
116	len = PAGE_SIZE;
117	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
118	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
119		err(1, "mmap");
120
121	start = time(NULL);
122	while ((time(NULL) - start) < RUNTIME && e == 0) {
123		share[SYNC] = 0;
124		for (i = 0; i < PARALLEL; i++) {
125			if ((pids[i] = fork()) == 0)
126				test();
127			if (pids[i] == -1)
128				err(1, "fork()");
129		}
130		for (i = 0; i < PARALLEL; i++) {
131			if (waitpid(pids[i], &status, 0) == -1)
132				err(1, "waitpid(%d)", pids[i]);
133			if (status != 0) {
134				if (WIFSIGNALED(status))
135					fprintf(stderr,
136					    "pid %d exit signal %d\n",
137					    pids[i], WTERMSIG(status));
138			}
139			e += status == 0 ? 0 : 1;
140		}
141	}
142
143	return (e);
144}
145