xref: /freebsd/tools/test/stress2/misc/selfd.sh (revision 02e9120893770924227138ba49df1edb3896112a)
1#!/bin/sh
2
3#
4# Copyright (c) 2015 EMC Corp.
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# selfd regression test attempt for r285310. Not reproduced.
30# Watchdog fired seen: https://people.freebsd.org/~pho/stress/log/selfd.txt
31
32. ../default.cfg
33
34dir=/tmp
35odir=`pwd`
36cd $dir
37sed '1,/^EOF/d' < $odir/$0 > $dir/selfd.c
38mycc -o selfd -Wall -Wextra -O0 -g selfd.c -lpthread || exit 1
39rm -f selfd.c
40cd $odir
41
42rm -rf /tmp/stressX.control
43daemon sh -c "(cd ../testcases/swap; ./swap -t 10m -i 20 -l 100)" > \
44    /dev/null 2>&1
45sleep 2
46
47/tmp/selfd
48s=$?
49
50while pgrep -q swap; do
51	pkill -9 swap
52done
53
54rm -rf /tmp/selfd
55exit $s
56
57EOF
58#include <sys/param.h>
59#include <sys/select.h>
60#include <sys/stat.h>
61#include <sys/wait.h>
62
63#include <err.h>
64#include <errno.h>
65#include <fcntl.h>
66#include <pthread.h>
67#include <sched.h>
68#include <signal.h>
69#include <stdio.h>
70#include <stdlib.h>
71#include <time.h>
72#include <unistd.h>
73
74static pthread_barrier_t barr;
75
76#define PARALLEL 16
77#define PIPES 32
78#define RUNTIME (5 * 60)
79
80static void
81handler(int s __unused)
82{
83}
84
85static void
86test(void)
87{
88	fd_set rset, tmpl;
89	struct sigaction sa;
90	struct timeval timeout;
91	time_t start;
92	int fds[PIPES][2], i, n, r;
93
94	r = pthread_barrier_wait(&barr);
95	if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
96	    errc(1, r, "pthread_barrier_wait");
97
98	FD_ZERO(&tmpl);
99	for (i = 0; i < PIPES; i++) {
100		if (pipe(fds[i]) == -1)
101			err(1, "pipe()");
102		FD_SET(fds[i][0], &tmpl);
103	}
104	sa.sa_handler = handler;
105	sigemptyset(&sa.sa_mask);
106	sa.sa_flags = 0;
107	if (sigaction(SIGALRM, &sa, NULL) == -1)
108		err(1, "sigaction");
109	start = time(NULL);
110	while ((time(NULL) - start) < 10) {
111		rset = tmpl;
112		timeout.tv_sec  = 0;
113		timeout.tv_usec = arc4random() % 10000 + 100;
114		n = arc4random() % PIPES;
115		ualarm(arc4random() % 10000 + 100, 0);
116		write(fds[n][1], "a", 1);
117		if ((n = select(PIPES, &rset, NULL, NULL, &timeout)) < 0)
118			if (errno != EINTR)
119				err(1, "select()");
120		ualarm(0, 0);
121	}
122
123	_exit(0);
124
125}
126
127int
128main(void)
129{
130	pthread_barrierattr_t attr;
131	time_t start;
132	int i, r;
133
134	if ((r = pthread_barrierattr_init(&attr)) != 0)
135		errc(1, r, "pthread_barrierattr_init");
136	if ((r = pthread_barrierattr_setpshared(&attr,
137	    PTHREAD_PROCESS_SHARED)) != 0)
138		errc(1, r, "pthread_barrierattr_setpshared");
139	if ((r = pthread_barrier_init(&barr, &attr, PARALLEL)) != 0)
140		errc(1, r, "pthread_barrier_init");
141
142	start = time(NULL);
143	while ((time(NULL) - start) < RUNTIME) {
144		for (i = 0; i < PARALLEL; i++)
145			if (fork() == 0)
146				test();
147		for (i = 0; i < PARALLEL; i++)
148			wait(NULL);
149	}
150
151	return (0);
152}
153