xref: /freebsd/tools/test/stress2/misc/setsockopt2.sh (revision 9f23cbd6cae82fd77edfad7173432fa8dccd0a95)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6# Copyright (c) 2019 Dell EMC Isilon
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27# SUCH DAMAGE.
28#
29
30# "panic: Assertion in_epoch(net_epoch_preempt) failed at raw_ip6.c:742"
31# https://people.freebsd.org/~pho/stress/log/setsockopt2.txt
32
33. ../default.cfg
34[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
35
36dir=/tmp
37odir=`pwd`
38cd $dir
39sed '1,/^EOF/d' < $odir/$0 > $dir/setsockopt2.c
40mycc -o setsockopt2 -Wall -Wextra -O0 -g setsockopt2.c || exit 1
41rm -f setsockopt2.c
42cd $odir
43
44set -e
45mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
46[ -c /dev/md$mdstart ] &&  mdconfig -d -u $mdstart
47mdconfig -a -t swap -s 2g -u $mdstart
48newfs $newfs_flags md$mdstart > /dev/null
49mount /dev/md$mdstart $mntpoint
50set +e
51
52(cd ../testcases/swap; ./swap -t 5m -i 20) &
53cd $mntpoint
54$dir/setsockopt2
55s=$?
56[ -f setsockopt2.core -a $s -eq 0 ] &&
57    { ls -l setsockopt2.core; mv setsockopt2.core $dir; s=1; }
58cd $odir
59while pkill swap; do :; done
60wait
61
62for i in `jot 6`; do
63	mount | grep -q "on $mntpoint " || break
64	umount $mntpoint && break || sleep 10
65	[ $i -eq 6 ] &&
66	    { echo FATAL; fstat -mf $mntpoint; exit 1; }
67done
68mdconfig -d -u $mdstart
69rm -rf $dir/setsockopt2
70exit $s
71
72EOF
73#include <sys/param.h>
74#include <sys/mman.h>
75#include <sys/socket.h>
76#include <sys/stat.h>
77#include <sys/un.h>
78#include <sys/wait.h>
79
80#include <err.h>
81#include <errno.h>
82#include <fcntl.h>
83#include <stdio.h>
84#include <stdlib.h>
85#include <string.h>
86#include <time.h>
87#include <unistd.h>
88
89static volatile u_int *share;
90
91#define PARALLEL 128
92#define RUNTIME (2 * 60)
93#define SYNC 0
94
95static void
96test(void)
97{
98	struct sockaddr_un sun;
99	pid_t pid;
100	time_t start;
101	char file[80];
102	int domain, fd, i, one, prot, success, typ;
103
104	success = 0;
105	snprintf(file, sizeof(file), "setsockopt2.socket.%d", getpid());
106	memset(&sun, 0, sizeof(sun));
107	sun.sun_family = AF_LOCAL;
108	sun.sun_len = sizeof(sun);
109	snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", file);
110	unlink(file);
111
112	start = time(NULL);
113	while (time(NULL) - start < 30) {
114		share[SYNC] = 0;
115		fd = -1;
116		while (fd == -1) {
117			domain = arc4random() % 256;
118			typ = arc4random() % 10;
119			if (arc4random() % 100 < 20)
120				prot = arc4random() % 10;
121			else
122				prot = 0;
123			if ((fd = socket(domain, typ, prot)) == -1)
124				continue;
125			close(fd);
126		}
127		pid = fork();
128		if (pid < 0)
129			err(1, "fork");
130		if (pid == 0) {
131			// listen
132			fd = socket(domain, typ, prot);
133			if (fd < 0)
134				_exit(0);
135			one = 1;
136			if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one,
137			    sizeof(one)) < 0)
138				err(1, "setsockopt");
139			if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) < 0)
140				_exit(0);
141			if (listen(fd, 10) == 0) {
142				share[SYNC] = 1;
143				usleep(random() % 10000);
144			}
145			(void)close(fd);
146			(void)unlink(file);
147			_exit(0);
148		}
149		fd = socket(domain, typ, prot);
150		if (fd == -1)
151			goto bad;
152
153		setsockopt(fd, 0xffff, 0x80, 0x0, 0x0);
154		sun.sun_len = arc4random() % 128;
155		sun.sun_family = arc4random() % 10;
156		setsockopt(fd, 0x6, 0x401, &sun, 0x14);
157
158		for (i = 0; share[SYNC] == 0 && i < 10; i++)
159			usleep(100);
160		if (connect(fd, (struct sockaddr *)&sun, sizeof(sun)) != -1)
161			success++;
162		usleep(random() % 100);
163bad:
164		(void)close(fd);
165		if (waitpid(pid, NULL, 0) != pid)
166			err(1, "waitpid(%d)", pid);
167	}
168
169	_exit(0);
170}
171
172int
173main(void)
174{
175	pid_t pids[PARALLEL];
176	size_t len;
177	time_t start;
178	int e, i, status;
179
180	e = 0;
181	len = PAGE_SIZE;
182	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
183	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
184		err(1, "mmap");
185
186	start = time(NULL);
187	while ((time(NULL) - start) < RUNTIME) {
188		for (i = 0; i < PARALLEL; i++) {
189			if ((pids[i] = fork()) == 0)
190				test();
191			if (pids[i] == -1)
192				err(1, "fork()");
193		}
194		for (i = 0; i < PARALLEL; i++) {
195			if (waitpid(pids[i], &status, 0) == -1)
196				err(1, "waitpid(%d)", pids[i]);
197			if (status != 0) {
198				if (WIFSIGNALED(status))
199					fprintf(stderr,
200					    "pid %d exit signal %d\n",
201					    pids[i], WTERMSIG(status));
202			}
203			e += status == 0 ? 0 : 1;
204		}
205	}
206
207	return (e);
208}
209