Path: blob/main/tools/test/stress2/misc/callout_reset_on.sh
39536 views
#!/bin/sh12#3# Copyright (c) 2012 Peter Holm <[email protected]>4# All rights reserved.5#6# Redistribution and use in source and binary forms, with or without7# modification, are permitted provided that the following conditions8# are met:9# 1. Redistributions of source code must retain the above copyright10# notice, this list of conditions and the following disclaimer.11# 2. Redistributions in binary form must reproduce the above copyright12# notice, this list of conditions and the following disclaimer in the13# documentation and/or other materials provided with the distribution.14#15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25# SUCH DAMAGE.26#2728# Scenario based on pr. kern/16634029# Process under FreeBSD 9.0 hangs in uninterruptable sleep with apparently30# no syscall (empty wchan).3132# http://people.freebsd.org/~pho/stress/log/callout_reset_on.txt33# Fixed in r243901.3435# panic: Bad link elm 0xfffff80012ba8ec8 prev->next != elm36# https://people.freebsd.org/~pho/stress/log/rrs005.txt37# Fixed in r278623.3839# "ritwait DE 0- 0:00.01 crlogger: writer" seen.40# https://people.freebsd.org/~pho/stress/log/kostik917.txt41# Fixed in r3029814243. ../default.cfg4445rm -f /tmp/crwriter /tmp/crlogger || exit 14647cat > /tmp/crwriter.c <<EOF48#include <stdio.h>49#include <stdlib.h>50#include <string.h>51#include <unistd.h>5253char *txt[] = {54"0 This is a line of text: abcdefghijklmnopqrstuvwxyz",55"1 Another line of text: ABCDEFGHIJKLMNOPQRSTUVWXYZ",56"2 A different line of text",57"3 A very, very different text",58"4 A much longer line with a lot of characters in the line",59"5 Now this is a quite long line of text, with both upper and lower case letters, and one digit!"60};6162int63main(void)64{65int i, j, n;66char help[256];6768for (i = 0; i < 100000; i++) {69j = arc4random() % 6;70n = arc4random() % strlen(txt[j]);71strncpy(help, txt[j], n);72help[n] = 0;73printf("%s\n", txt[j]);74if ((arc4random() % 1000) == 1)75usleep(100000);76}7778return (0);79}80EOF81mycc -o /tmp/crwriter -Wall -Wextra -O2 -g /tmp/crwriter.c82rm -f /tmp/crwriter.c8384cat > /tmp/crlogger.c <<EOF85#include <sys/param.h>86#include <sys/socket.h>87#include <netinet/in.h>88#include <netdb.h>89#include <stdio.h>90#include <stdlib.h>91#include <unistd.h>92#include <signal.h>93#include <errno.h>94#include <string.h>95#include <err.h>96#include <sys/types.h>97#include <err.h>98#include <fcntl.h>99#include <stdio.h>100#include <sys/wait.h>101#include <unistd.h>102103#define BARRIER_CREATE 1104#define BARRIER_WAIT 2105#define BARRIER_DELETE 3106107void108barrier(int mode)109{110int fd;111char path[128];112113if (mode == BARRIER_CREATE) {114snprintf(path, sizeof(path), "barrier.%d", getpid());115if ((fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0)116err(1, "%s", path);117} else if (mode == BARRIER_WAIT) {118snprintf(path, sizeof(path), "barrier.%d", getppid());119for(;;) {120if (access(path, R_OK) == -1)121break;122usleep(10000);123}124} else if (mode == BARRIER_DELETE) {125snprintf(path, sizeof(path), "barrier.%d", getpid());126if (unlink(path) == -1)127err(1, "unlink(%s)", path);128} else129errx(1, "Bad barrier mode: %d", mode);130}131132pid_t pid;133int bufsize;134int port;135int alarm_exit;136137void138killer(void)139{140setproctitle("killer");141alarm(120);142barrier(BARRIER_WAIT);143for (;;) {144if (pid == 0)145break;146if (kill(pid, SIGUSR1) == -1)147break;148usleep(1000);149}150_exit(0);151}152153void154handler(int s __unused)155{156}157158void159ahandler(int s __unused)160{161if (alarm_exit)162_exit(0);163}164165/* Read form socket, discard */166static void167reader(void) {168int tcpsock, msgsock;169int on;170socklen_t len;171struct sockaddr_in inetaddr, inetpeer;172int n, *buf;173174setproctitle("reader - init");175on = 1;176if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)177err(1, "socket(), %s:%d", __FILE__, __LINE__);178179if (setsockopt(tcpsock,180SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)181err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);182183inetaddr.sin_family = AF_INET;184inetaddr.sin_addr.s_addr = INADDR_ANY;185inetaddr.sin_port = htons(port);186inetaddr.sin_len = sizeof(inetaddr);187188signal(SIGUSR1, handler);189alarm(60);190if (bind(tcpsock,191(struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0)192err(1, "bind(), %s:%d", __FILE__, __LINE__);193194if (listen(tcpsock, 5) < 0)195err(1, "listen(), %s:%d", __FILE__, __LINE__);196197len = sizeof(inetpeer);198if ((msgsock = accept(tcpsock,199(struct sockaddr *)&inetpeer, &len)) < 0)200err(1, "accept(), %s:%d", __FILE__, __LINE__);201202if ((buf = malloc(bufsize)) == NULL)203err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);204setproctitle("reader");205alarm(0);206signal(SIGALRM, ahandler);207for (;;) {208ualarm(5000, 0);209if ((n = recvfrom(msgsock, buf, 4, 0, NULL, NULL)) < 0) {210if (errno == EAGAIN)211continue;212err(1, "read(), %s:%d", __FILE__, __LINE__);213}214if (n == 0)215break;216if (write(msgsock, "OK", 3) != 3)217err(1, "write ack. %s:%d", __FILE__, __LINE__);218219}220close(msgsock);221_exit(0);222}223224/* read from stdin, write to socket */225static void226writer(void) {227int tcpsock, on;228struct sockaddr_in inetaddr;229struct hostent *hostent;230int i, r;231char line[1024], ack[80];;232233setproctitle("writer - init");234signal(SIGUSR1, handler);235alarm(60);236on = 1;237for (i = 1; i < 5; i++) {238if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)239err(1, "socket(), %s:%d", __FILE__, __LINE__);240241if (setsockopt(tcpsock,242SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)243err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);244245hostent = gethostbyname ("localhost");246bzero(&inetaddr, sizeof(inetaddr));247memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,248sizeof (struct in_addr));249250inetaddr.sin_family = AF_INET;251inetaddr.sin_port = htons(port);252inetaddr.sin_len = sizeof(inetaddr);253254r = connect(tcpsock, (struct sockaddr *) &inetaddr,255sizeof(inetaddr));256if (r == 0)257break;258sleep(1);259close(tcpsock);260}261if (r < 0)262err(1, "connect(), %s:%d", __FILE__, __LINE__);263264setproctitle("writer");265barrier(BARRIER_DELETE);266alarm(0);267while (fgets(line, sizeof(line), stdin) != NULL) {268alarm(10);269alarm_exit = 1;270if (write(tcpsock, line, strlen(line)) < 0)271err(1, "socket write(). %s:%d", __FILE__, __LINE__);272alarm_exit = 0;273ualarm(5000, 0);274if (recvfrom(tcpsock, ack, 4, 0, NULL, NULL) < 0) {275if (errno == EAGAIN)276continue;277err(1, "read(), %s:%d", __FILE__, __LINE__);278}279}280sleep(30);281return;282}283284int285main(int argc, char **argv)286{287288pid_t kpid;289290if (argc != 2)291errx(1, "Usage: %s <port number>\n", argv[0]);292port = atoi(argv[1]);293bufsize = 128;294295barrier(BARRIER_CREATE);296signal(SIGCHLD, SIG_IGN);297if ((pid = fork()) == 0)298reader();299300if ((kpid = fork()) == 0)301killer();302303writer();304sleep(1);305kill(pid, SIGINT);306kill(kpid, SIGINT);307308return (0);309}310EOF311mycc -o /tmp/crlogger -Wall -Wextra -O2 -g /tmp/crlogger.c312rm -f /tmp/crlogger.c313314N=200315cd /tmp316start=`date '+%s'`317for i in `jot 40`; do318for j in `jot $N`; do319/tmp/crwriter | /tmp/crlogger 1236$j 2>/dev/null &320done321322for j in `jot $N`; do323wait324done325[ $((`date '+%s'` - start)) -gt 1200 ] && break326done327rm -f /tmp/crwriter /tmp/crlogger ./barrier.*328exit 0329330331