xref: /freebsd/tools/test/stress2/misc/unix_socket_detach.sh (revision ba3c1f5972d7b90feb6e6da47905ff2757e0fe57)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6# Copyright (c) 2016 Mark Johnston <markj@FreeBSD.org>
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# Reproduce problem reported as PR 211531.
31# https://reviews.freebsd.org/D7398
32
33# "panic: __rw_wlock_hard: recursing but non-recursive rw unp_link_rwlock @
34#  ../../../kern/uipc_usrreq.c:655" seen:
35# https://people.freebsd.org/~pho/stress/log/unix_socket_detach.txt
36# Fixed in r303855.
37
38. ../default.cfg
39
40cd /tmp
41cat > unix_socket_detach.c <<EOF
42#include <sys/types.h>
43#include <sys/socket.h>
44#include <sys/un.h>
45
46#include <err.h>
47#include <fcntl.h>
48#include <signal.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52#include <unistd.h>
53
54static void
55handler(int i __unused) {
56	_exit(0);
57}
58
59int
60main(void)
61{
62	struct sockaddr_un sun;
63	char *file;
64	pid_t pid;
65	int sd, flags, one;
66
67	file = "unix_socket_detach.socket";
68	memset(&sun, 0, sizeof(sun));
69	sun.sun_family = AF_LOCAL;
70	sun.sun_len = sizeof(sun);
71	snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", file);
72
73	signal(SIGALRM, handler);
74	pid = fork();
75	if (pid < 0)
76		err(1, "fork");
77	if (pid == 0) {
78		alarm(300);
79		for (;;) {
80			sd = socket(PF_LOCAL, SOCK_STREAM, 0);
81			if (sd < 0)
82				err(1, "socket");
83			one = 1;
84			if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &one,
85			    sizeof(one)) < 0)
86				err(1, "setsockopt");
87			if (bind(sd, (struct sockaddr *)&sun, sizeof(sun)) < 0) {
88				close(sd);
89				continue;
90			}
91			if (listen(sd, 10) != 0)
92				err(1, "listen");
93			usleep(random() % 10);
94			(void)close(sd);
95			(void)unlink(file);
96		}
97	} else {
98		alarm(300);
99		for (;;) {
100			sd = socket(PF_LOCAL, SOCK_STREAM, 0);
101			if (sd < 0)
102				err(1, "socket");
103			if ((flags = fcntl(sd, F_GETFL, 0)) < 0)
104				err(1, "fcntl(F_GETFL)");
105			flags |= O_NONBLOCK;
106			if (fcntl(sd, F_SETFL, flags) < 0)
107				err(1, "fcntl(F_SETFL)");
108			(void)connect(sd, (struct sockaddr *)&sun, sizeof(sun));
109			usleep(random() % 10);
110			(void)close(sd);
111		}
112	}
113	return (0);
114}
115EOF
116
117mycc -o unix_socket_detach -Wall -Wextra -O2 -g unix_socket_detach.c || exit 1
118
119rm -f unix_socket_detach.socket
120/tmp/unix_socket_detach
121s=$?
122
123rm -f unix_socket_detach.c unix_socket_detach unix_socket_detach.socket
124exit $s
125