xref: /freebsd/tools/test/stress2/misc/unix_socket.sh (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5#
6# Copyright (c) 2018 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: mutex unp not owned at ../../../kern/uipc_usrreq.c:879" seen:
31# https://people.freebsd.org/~pho/stress/log/mmacy018.txt
32# Fixed by r334756.
33
34. ../default.cfg
35
36cd /tmp
37cat > unix_socket.c <<EOF
38#include <sys/param.h>
39#include <sys/socket.h>
40#include <sys/un.h>
41#include <sys/wait.h>
42
43#include <netinet/in.h>
44
45#include <err.h>
46#include <errno.h>
47#include <netdb.h>
48#include <signal.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52#include <unistd.h>
53
54#define SOCK_FILE "/tmp/unix_socket.socket"
55
56static int
57client(void) {
58	struct sockaddr_un addr;
59	int fd, len;
60	char buff[8192];
61
62	if ((fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0)
63		err(1, "socket");
64
65	memset(&addr, 0, sizeof(addr));
66	addr.sun_family = AF_UNIX;
67	strcpy(addr.sun_path, SOCK_FILE);
68	unlink(SOCK_FILE);
69	if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
70		err(1, "bind");
71
72	memset(&addr, 0, sizeof(addr));
73	addr.sun_family = AF_UNIX;
74	strcpy(addr.sun_path, SOCK_FILE);
75	if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
76		err(1, "connect");
77
78	strcpy (buff, "1234567890");
79	if (send(fd, buff, strlen(buff)+1, 0) == -1)
80		err(1, "send");
81	printf ("sent i1234567890\n");
82
83	if ((len = recv(fd, buff, 8192, 0)) < 0)
84		err(1, "recv");
85	printf ("receive %d %s\n", len, buff);
86
87	if (fd >= 0) {
88		close(fd);
89	}
90	unlink (SOCK_FILE);
91
92	return(0);
93}
94
95static int
96server() {
97	struct sockaddr_un addr;
98	struct sockaddr_un from;
99	socklen_t fromlen = sizeof(from);
100	int fd, len, ret;
101	char buff[8192];
102
103	unlink(SOCK_FILE);
104	if ((fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0)
105		err(1, "socket");
106
107	memset(&addr, 0, sizeof(addr));
108	addr.sun_family = AF_UNIX;
109	strcpy(addr.sun_path, SOCK_FILE);
110	unlink(SOCK_FILE);
111	if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
112		err(1, "bind");
113
114	alarm(2);
115	while ((len = recvfrom(fd, buff, 8192, 0, (struct sockaddr *)&from,
116	    &fromlen)) > 0) {
117		printf ("recvfrom: %s\n", buff);
118		strcpy (buff, "transmit good!");
119		ret = sendto(fd, buff, strlen(buff)+1, 0,
120		    (struct sockaddr *)&from, fromlen);
121		if (ret < 0) {
122			perror("sendto");
123			break;
124		}
125	}
126
127	if (fd != -1)
128		close(fd);
129
130	return (0);
131}
132
133int
134main(void)
135{
136	pid_t pid;
137
138	if ((pid = fork()) == -1)
139		err(1, "fork");
140
141	if (pid == 0) {
142		server();
143		_exit(0);
144	}
145	sleep(2);
146	client();
147
148	if (waitpid(pid, NULL, 0) != pid)
149		err(1, "waitpid");
150
151	return (0);
152}
153EOF
154
155cc -o unix_socket -Wall -Wextra -O2 -g unix_socket.c || exit
156rm unix_socket.c
157
158./unix_socket > /dev/null
159
160rm unix_socket
161exit 0
162