Path: blob/main/tools/test/stress2/misc/callout_reset_on2.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# Copy of callout_reset_on.sh. Waiting to see if this catches anything.2930. ../default.cfg3132rm -f /tmp/crwriter2 /tmp/crlogger2 || exit 13334cat > /tmp/crwriter2.c <<EOF35#include <stdio.h>36#include <stdlib.h>37#include <string.h>38#include <time.h>39#include <unistd.h>4041char *txt[] = {42"0 This is a line of text: abcdefghijklmnopqrstuvwxyz",43"1 Another line of text: ABCDEFGHIJKLMNOPQRSTUVWXYZ",44"2 A different line of text",45"3 A very, very different text",46"4 A much longer line with a lot of characters in the line",47"5 Now this is a quite long line of text, with both upper and lower case letters, and one digit!"48};4950#define RUNTIME (10 * 60)5152int53main(void)54{55time_t start;56int j, n;57char help[256];5859start = time(NULL);60while (time(NULL) - start < RUNTIME) {61j = arc4random() % 6;62n = arc4random() % strlen(txt[j]);63strncpy(help, txt[j], n);64help[n] = 0;65printf("%s\n", txt[j]);66if ((arc4random() % 1000) == 1)67usleep(100000);68}6970return (0);71}72EOF73mycc -o /tmp/crwriter2 -Wall -Wextra -O2 -g /tmp/crwriter2.c74rm -f /tmp/crwriter2.c7576cat > /tmp/crlogger2.c <<EOF77#include <sys/param.h>78#include <sys/mman.h>79#include <sys/socket.h>80#include <sys/wait.h>8182#include <machine/atomic.h>8384#include <err.h>85#include <err.h>86#include <errno.h>87#include <fcntl.h>88#include <netdb.h>89#include <netinet/in.h>90#include <signal.h>91#include <stdio.h>92#include <stdio.h>93#include <stdlib.h>94#include <string.h>95#include <unistd.h>96#include <unistd.h>9798volatile u_int *share;99100#define SYNC 0101102pid_t pid;103int bufsize;104int port;105int alarm_exit;106107void108killer(void)109{110setproctitle("killer");111while (share[SYNC] == 0)112;113alarm(120);114for (;;) {115if (pid == 0)116break;117if (kill(pid, SIGUSR1) == -1)118break;119usleep(arc4random() % 2000 + 10);120}121_exit(0);122}123124void125handler(int s __unused)126{127}128129void130ahandler(int s __unused)131{132if (alarm_exit)133_exit(0);134}135136/* Read form socket, discard */137static void138reader(void) {139int tcpsock, msgsock;140int on;141socklen_t len;142struct sockaddr_in inetaddr, inetpeer;143int n, *buf;144145setproctitle("reader - init");146on = 1;147if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)148err(1, "socket(), %s:%d", __FILE__, __LINE__);149150if (setsockopt(tcpsock,151SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)152err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);153154inetaddr.sin_family = AF_INET;155inetaddr.sin_addr.s_addr = INADDR_ANY;156inetaddr.sin_port = htons(port);157inetaddr.sin_len = sizeof(inetaddr);158159signal(SIGUSR1, handler);160alarm(60);161if (bind(tcpsock,162(struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0)163err(1, "bind(), %s:%d", __FILE__, __LINE__);164165if (listen(tcpsock, 5) < 0)166err(1, "listen(), %s:%d", __FILE__, __LINE__);167168len = sizeof(inetpeer);169if ((msgsock = accept(tcpsock,170(struct sockaddr *)&inetpeer, &len)) < 0)171err(1, "accept(), %s:%d", __FILE__, __LINE__);172173if ((buf = malloc(bufsize)) == NULL)174err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);175setproctitle("reader");176alarm(0);177signal(SIGALRM, ahandler);178for (;;) {179ualarm(arc4random() % 5000 + 100, 0);180if ((n = recvfrom(msgsock, buf, bufsize, 0, NULL, NULL)) < 0) {181if (errno == EAGAIN)182continue;183err(1, "read(), %s:%d", __FILE__, __LINE__);184}185if (n == 0)186break;187if (write(msgsock, "OK", 3) != 3)188err(1, "write ack. %s:%d", __FILE__, __LINE__);189190}191close(msgsock);192_exit(0);193}194195/* read from stdin, write to socket */196static void197writer(void) {198int tcpsock, on;199struct sockaddr_in inetaddr;200struct hostent *hostent;201int i, r;202char line[1024], ack[80];;203204setproctitle("writer - init");205share[SYNC] = 1;206signal(SIGUSR1, handler);207signal(SIGALRM, ahandler);208alarm(60);209on = 1;210for (i = 1; i < 5; i++) {211if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)212err(1, "socket(), %s:%d", __FILE__, __LINE__);213214if (setsockopt(tcpsock,215SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)216err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);217218hostent = gethostbyname ("localhost");219bzero(&inetaddr, sizeof(inetaddr));220memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,221sizeof (struct in_addr));222223inetaddr.sin_family = AF_INET;224inetaddr.sin_port = htons(port);225inetaddr.sin_len = sizeof(inetaddr);226227r = connect(tcpsock, (struct sockaddr *) &inetaddr,228sizeof(inetaddr));229if (r == 0)230break;231sleep(1);232close(tcpsock);233}234if (r < 0)235err(1, "connect(), %s:%d", __FILE__, __LINE__);236237setproctitle("writer");238alarm(0);239while (fgets(line, sizeof(line), stdin) != NULL) {240alarm(10);241alarm_exit = 1;242if (write(tcpsock, line, strlen(line)) < 0)243err(1, "socket write(). %s:%d", __FILE__, __LINE__);244alarm_exit = 0;245ualarm(arc4random() % 5000 + 1000, 0);246if (recvfrom(tcpsock, ack, 4, 0, NULL, NULL) < 0) {247if (errno == EAGAIN)248continue;249err(1, "read(), %s:%d", __FILE__, __LINE__);250}251}252sleep(30);253return;254}255256int257main(int argc, char **argv)258{259260pid_t kpid;261size_t len;262263if (argc != 2) {264fprintf(stderr, "Usage: %s <port number>\n", argv[0]);265_exit(1);266}267port = atoi(argv[1]);268bufsize = 128;269270len = PAGE_SIZE;271if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,272MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)273err(1, "mmap");274275signal(SIGCHLD, SIG_IGN);276if ((pid = fork()) == 0)277reader();278279if ((kpid = fork()) == 0)280killer();281282writer();283sleep(1);284kill(pid, SIGINT);285kill(kpid, SIGINT);286287return (0);288}289EOF290mycc -o /tmp/crlogger2 -Wall -Wextra -O2 -g /tmp/crlogger2.c291rm -f /tmp/crlogger2.c292293N=50294cd /tmp295for j in `jot $N`; do296/tmp/crwriter2 | /tmp/crlogger2 1236$j &297done298wait299rm -f /tmp/crwriter2 /tmp/crlogger2300exit 0301302303