xref: /freebsd/tools/test/stress2/misc/setsockopt2.sh (revision 02e9120893770924227138ba49df1edb3896112a)
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 int debug; /* set to 1 for debug output */
90static volatile u_int *share;
91
92#define PARALLEL 128
93#define RUNTIME (2 * 60)
94#define SYNC 0
95
96static void
97test(void)
98{
99	struct sockaddr_un sun;
100	pid_t pid;
101	time_t start;
102	char file[80];
103	int domain, fd, i, one, prot, success, typ;
104
105	success = 0;
106	snprintf(file, sizeof(file), "setsockopt2.socket.%d", getpid());
107	memset(&sun, 0, sizeof(sun));
108	sun.sun_family = AF_LOCAL;
109	sun.sun_len = sizeof(sun);
110	snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", file);
111	unlink(file);
112
113	start = time(NULL);
114	while (time(NULL) - start < 30) {
115		share[SYNC] = 0;
116		fd = -1;
117		while (fd == -1) {
118			domain = arc4random() % 256;
119			typ = arc4random() % 10;
120			if (arc4random() % 100 < 20)
121				prot = arc4random() % 10;
122			else
123				prot = 0;
124			if ((fd = socket(domain, typ, prot)) == -1)
125				continue;
126			close(fd);
127		}
128		pid = fork();
129		if (pid < 0)
130			err(1, "fork");
131		if (pid == 0) {
132			// listen
133			fd = socket(domain, typ, prot);
134			if (fd < 0)
135				_exit(0);
136			one = 1;
137			if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one,
138			    sizeof(one)) < 0)
139				err(1, "setsockopt");
140			if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) < 0)
141				_exit(0);
142			if (listen(fd, 10) == 0) {
143				share[SYNC] = 1;
144				usleep(random() % 10000);
145			}
146			(void)close(fd);
147			(void)unlink(file);
148			_exit(0);
149		}
150		fd = socket(domain, typ, prot);
151		if (fd == -1)
152			goto bad;
153
154		setsockopt(fd, 0xffff, 0x80, 0x0, 0x0);
155		sun.sun_len = arc4random() % 128;
156		sun.sun_family = arc4random() % 10;
157		setsockopt(fd, 0x6, 0x401, &sun, 0x14);
158
159		for (i = 0; share[SYNC] == 0 && i < 10; i++)
160			usleep(100);
161		if (connect(fd, (struct sockaddr *)&sun, sizeof(sun)) != -1)
162			success++;
163		usleep(random() % 100);
164bad:
165		(void)close(fd);
166		if (waitpid(pid, NULL, 0) != pid)
167			err(1, "waitpid(%d)", pid);
168	}
169	if (debug != 0 && success == 0)
170		fprintf(stderr, "No calls to connect() succeded.\n");
171
172	_exit(0);
173}
174
175int
176main(void)
177{
178	pid_t pids[PARALLEL];
179	size_t len;
180	time_t start;
181	int e, i, status;
182
183	e = 0;
184	len = PAGE_SIZE;
185	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
186	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
187		err(1, "mmap");
188
189	start = time(NULL);
190	while ((time(NULL) - start) < RUNTIME) {
191		for (i = 0; i < PARALLEL; i++) {
192			if ((pids[i] = fork()) == 0)
193				test();
194			if (pids[i] == -1)
195				err(1, "fork()");
196		}
197		for (i = 0; i < PARALLEL; i++) {
198			if (waitpid(pids[i], &status, 0) == -1)
199				err(1, "waitpid(%d)", pids[i]);
200			if (status != 0) {
201				if (WIFSIGNALED(status))
202					fprintf(stderr,
203					    "pid %d exit signal %d\n",
204					    pids[i], WTERMSIG(status));
205			}
206			e += status == 0 ? 0 : 1;
207		}
208	}
209
210	return (e);
211}
212