xref: /freebsd/tools/test/stress2/misc/callout_reset_on.sh (revision 43a5ec4eb41567cc92586503212743d89686d78f)
1#!/bin/sh
2
3#
4# Copyright (c) 2012 Peter Holm <pho@FreeBSD.org>
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# Scenario based on pr. kern/166340
30# Process under FreeBSD 9.0 hangs in uninterruptable sleep with apparently
31# no syscall (empty wchan).
32
33# http://people.freebsd.org/~pho/stress/log/callout_reset_on.txt
34# Fixed in r243901.
35
36# panic: Bad link elm 0xfffff80012ba8ec8 prev->next != elm
37# https://people.freebsd.org/~pho/stress/log/rrs005.txt
38# Fixed in r278623.
39
40# "ritwait DE    0- 0:00.01 crlogger: writer" seen.
41# https://people.freebsd.org/~pho/stress/log/kostik917.txt
42# Fixed in r302981
43
44. ../default.cfg
45
46rm -f /tmp/crwriter /tmp/crlogger || exit 1
47
48cat > /tmp/crwriter.c <<EOF
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52#include <unistd.h>
53
54char *txt[] = {
55	"0 This is a line of text: abcdefghijklmnopqrstuvwxyz",
56	"1 Another line of text: ABCDEFGHIJKLMNOPQRSTUVWXYZ",
57	"2 A different line of text",
58	"3 A very, very different text",
59	"4 A much longer line with a lot of characters in the line",
60	"5 Now this is a quite long line of text, with both upper and lower case letters, and one digit!"
61};
62
63int
64main(void)
65{
66	int i, j, n;
67	char help[256];
68
69	for (i = 0; i < 100000; i++) {
70		j = arc4random() % 6;
71		n = arc4random() % strlen(txt[j]);
72		strncpy(help, txt[j], n);
73		help[n] = 0;
74		printf("%s\n", txt[j]);
75		if ((arc4random() % 1000) == 1)
76			usleep(100000);
77	}
78
79	return (0);
80}
81EOF
82mycc -o /tmp/crwriter -Wall -Wextra -O2 -g /tmp/crwriter.c
83rm -f /tmp/crwriter.c
84
85cat > /tmp/crlogger.c <<EOF
86#include <sys/param.h>
87#include <sys/socket.h>
88#include <netinet/in.h>
89#include <netdb.h>
90#include <stdio.h>
91#include <stdlib.h>
92#include <unistd.h>
93#include <signal.h>
94#include <errno.h>
95#include <string.h>
96#include <err.h>
97#include <sys/types.h>
98#include <err.h>
99#include <fcntl.h>
100#include <stdio.h>
101#include <sys/wait.h>
102#include <unistd.h>
103
104#define BARRIER_CREATE 1
105#define BARRIER_WAIT 2
106#define BARRIER_DELETE 3
107
108void
109barrier(int mode)
110{
111	int fd;
112	char path[128];
113
114	if (mode == BARRIER_CREATE) {
115		snprintf(path, sizeof(path), "barrier.%d", getpid());
116		if ((fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0)
117			err(1, "%s", path);
118	} else if (mode == BARRIER_WAIT) {
119		snprintf(path, sizeof(path), "barrier.%d", getppid());
120		for(;;) {
121			if (access(path, R_OK) == -1)
122				break;
123			usleep(10000);
124		}
125	} else if (mode == BARRIER_DELETE) {
126		snprintf(path, sizeof(path), "barrier.%d", getpid());
127		if (unlink(path) == -1)
128			err(1, "unlink(%s)", path);
129	} else
130		errx(1, "Bad barrier mode: %d", mode);
131}
132
133pid_t pid;
134int bufsize;
135int port;
136int alarm_exit;
137
138void
139killer(void)
140{
141	setproctitle("killer");
142	alarm(120);
143	barrier(BARRIER_WAIT);
144	for (;;) {
145		if (pid == 0)
146			break;
147		if (kill(pid, SIGUSR1) == -1)
148			break;
149		usleep(1000);
150	}
151	_exit(0);
152}
153
154void
155handler(int s __unused)
156{
157}
158
159void
160ahandler(int s __unused)
161{
162	if (alarm_exit)
163		_exit(0);
164}
165
166/* Read form socket, discard */
167static void
168reader(void) {
169	int tcpsock, msgsock;
170	int on;
171	socklen_t len;
172	struct sockaddr_in inetaddr, inetpeer;
173	int n, *buf;
174
175	setproctitle("reader - init");
176	on = 1;
177	if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
178		err(1, "socket(), %s:%d", __FILE__, __LINE__);
179
180	if (setsockopt(tcpsock,
181	    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
182		err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
183
184	inetaddr.sin_family = AF_INET;
185	inetaddr.sin_addr.s_addr = INADDR_ANY;
186	inetaddr.sin_port = htons(port);
187	inetaddr.sin_len = sizeof(inetaddr);
188
189	signal(SIGUSR1, handler);
190	alarm(60);
191	if (bind(tcpsock,
192	    (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0)
193		err(1, "bind(), %s:%d", __FILE__, __LINE__);
194
195	if (listen(tcpsock, 5) < 0)
196		err(1, "listen(), %s:%d", __FILE__, __LINE__);
197
198	len = sizeof(inetpeer);
199	if ((msgsock = accept(tcpsock,
200	    (struct sockaddr *)&inetpeer, &len)) < 0)
201		err(1, "accept(), %s:%d", __FILE__, __LINE__);
202
203	if ((buf = malloc(bufsize)) == NULL)
204			err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);
205	setproctitle("reader");
206	alarm(0);
207	signal(SIGALRM, ahandler);
208	for (;;) {
209		ualarm(5000, 0);
210		if ((n = recvfrom(msgsock, buf, 4, 0, NULL, NULL)) < 0) {
211			if (errno == EAGAIN)
212				continue;
213			err(1, "read(), %s:%d", __FILE__, __LINE__);
214		}
215		if (n == 0)
216			break;
217		if (write(msgsock, "OK", 3) != 3)
218			err(1, "write ack. %s:%d", __FILE__, __LINE__);
219
220	}
221	close(msgsock);
222	_exit(0);
223}
224
225/* read from stdin, write to socket */
226static void
227writer(void) {
228	int tcpsock, on;
229	struct sockaddr_in inetaddr;
230	struct hostent *hostent;
231	int i, r;
232	char line[1024], ack[80];;
233	pid_t ppid;
234
235	setproctitle("writer - init");
236	ppid = getppid();
237	signal(SIGUSR1, handler);
238	alarm(60);
239	on = 1;
240	for (i = 1; i < 5; i++) {
241		if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
242			err(1, "socket(), %s:%d", __FILE__, __LINE__);
243
244		if (setsockopt(tcpsock,
245		    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
246			err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
247
248		hostent = gethostbyname ("localhost");
249		bzero(&inetaddr, sizeof(inetaddr));
250		memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
251			sizeof (struct in_addr));
252
253		inetaddr.sin_family = AF_INET;
254		inetaddr.sin_port = htons(port);
255		inetaddr.sin_len = sizeof(inetaddr);
256
257		r = connect(tcpsock, (struct sockaddr *) &inetaddr,
258			sizeof(inetaddr));
259		if (r == 0)
260			break;
261		sleep(1);
262		close(tcpsock);
263	}
264	if (r < 0)
265		err(1, "connect(), %s:%d", __FILE__, __LINE__);
266
267	setproctitle("writer");
268	barrier(BARRIER_DELETE);
269	alarm(0);
270	while (fgets(line, sizeof(line), stdin) != NULL) {
271		alarm(10);
272		alarm_exit = 1;
273		if (write(tcpsock, line, strlen(line)) < 0)
274			err(1, "socket write(). %s:%d", __FILE__, __LINE__);
275		alarm_exit = 0;
276		ualarm(5000, 0);
277		if (recvfrom(tcpsock, ack, 4, 0, NULL, NULL) < 0) {
278			if (errno == EAGAIN)
279				continue;
280			err(1, "read(), %s:%d", __FILE__, __LINE__);
281		}
282	}
283	sleep(30);
284	return;
285}
286
287int
288main(int argc, char **argv)
289{
290
291	pid_t kpid;
292
293	if (argc != 2)
294		errx(1, "Usage: %s <port number>\n", argv[0]);
295	port = atoi(argv[1]);
296	bufsize = 128;
297
298	barrier(BARRIER_CREATE);
299	signal(SIGCHLD, SIG_IGN);
300	if ((pid = fork()) == 0)
301		reader();
302
303	if ((kpid = fork()) == 0)
304		killer();
305
306	writer();
307	sleep(1);
308	kill(pid, SIGINT);
309	kill(kpid, SIGINT);
310
311	return (0);
312}
313EOF
314mycc -o /tmp/crlogger -Wall -Wextra -O2 -g /tmp/crlogger.c
315rm -f /tmp/crlogger.c
316
317N=200
318cd /tmp
319start=`date '+%s'`
320for i in `jot 40`; do
321	for j in `jot $N`; do
322		/tmp/crwriter | /tmp/crlogger 1236$j 2>/dev/null &
323	done
324
325	for j in `jot $N`; do
326		wait
327	done
328	[ $((`date '+%s'` - start)) -gt 1200 ] && break
329done
330rm -f /tmp/crwriter /tmp/crlogger ./barrier.*
331exit 0
332