xref: /freebsd/tools/test/stress2/misc/callout_reset_on.sh (revision 02e9120893770924227138ba49df1edb3896112a)
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
234	setproctitle("writer - init");
235	signal(SIGUSR1, handler);
236	alarm(60);
237	on = 1;
238	for (i = 1; i < 5; i++) {
239		if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
240			err(1, "socket(), %s:%d", __FILE__, __LINE__);
241
242		if (setsockopt(tcpsock,
243		    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
244			err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
245
246		hostent = gethostbyname ("localhost");
247		bzero(&inetaddr, sizeof(inetaddr));
248		memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
249			sizeof (struct in_addr));
250
251		inetaddr.sin_family = AF_INET;
252		inetaddr.sin_port = htons(port);
253		inetaddr.sin_len = sizeof(inetaddr);
254
255		r = connect(tcpsock, (struct sockaddr *) &inetaddr,
256			sizeof(inetaddr));
257		if (r == 0)
258			break;
259		sleep(1);
260		close(tcpsock);
261	}
262	if (r < 0)
263		err(1, "connect(), %s:%d", __FILE__, __LINE__);
264
265	setproctitle("writer");
266	barrier(BARRIER_DELETE);
267	alarm(0);
268	while (fgets(line, sizeof(line), stdin) != NULL) {
269		alarm(10);
270		alarm_exit = 1;
271		if (write(tcpsock, line, strlen(line)) < 0)
272			err(1, "socket write(). %s:%d", __FILE__, __LINE__);
273		alarm_exit = 0;
274		ualarm(5000, 0);
275		if (recvfrom(tcpsock, ack, 4, 0, NULL, NULL) < 0) {
276			if (errno == EAGAIN)
277				continue;
278			err(1, "read(), %s:%d", __FILE__, __LINE__);
279		}
280	}
281	sleep(30);
282	return;
283}
284
285int
286main(int argc, char **argv)
287{
288
289	pid_t kpid;
290
291	if (argc != 2)
292		errx(1, "Usage: %s <port number>\n", argv[0]);
293	port = atoi(argv[1]);
294	bufsize = 128;
295
296	barrier(BARRIER_CREATE);
297	signal(SIGCHLD, SIG_IGN);
298	if ((pid = fork()) == 0)
299		reader();
300
301	if ((kpid = fork()) == 0)
302		killer();
303
304	writer();
305	sleep(1);
306	kill(pid, SIGINT);
307	kill(kpid, SIGINT);
308
309	return (0);
310}
311EOF
312mycc -o /tmp/crlogger -Wall -Wextra -O2 -g /tmp/crlogger.c
313rm -f /tmp/crlogger.c
314
315N=200
316cd /tmp
317start=`date '+%s'`
318for i in `jot 40`; do
319	for j in `jot $N`; do
320		/tmp/crwriter | /tmp/crlogger 1236$j 2>/dev/null &
321	done
322
323	for j in `jot $N`; do
324		wait
325	done
326	[ $((`date '+%s'` - start)) -gt 1200 ] && break
327done
328rm -f /tmp/crwriter /tmp/crlogger ./barrier.*
329exit 0
330