18a272653SPeter Holm#!/bin/sh 28a272653SPeter Holm 38a272653SPeter Holm# 4*4d846d26SWarner Losh# SPDX-License-Identifier: BSD-2-Clause 58a272653SPeter Holm# 68a272653SPeter Holm# Copyright (c) 2018 Dell EMC Isilon 78a272653SPeter Holm# 88a272653SPeter Holm# Redistribution and use in source and binary forms, with or without 98a272653SPeter Holm# modification, are permitted provided that the following conditions 108a272653SPeter Holm# are met: 118a272653SPeter Holm# 1. Redistributions of source code must retain the above copyright 128a272653SPeter Holm# notice, this list of conditions and the following disclaimer. 138a272653SPeter Holm# 2. Redistributions in binary form must reproduce the above copyright 148a272653SPeter Holm# notice, this list of conditions and the following disclaimer in the 158a272653SPeter Holm# documentation and/or other materials provided with the distribution. 168a272653SPeter Holm# 178a272653SPeter Holm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 188a272653SPeter Holm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 198a272653SPeter Holm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 208a272653SPeter Holm# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 218a272653SPeter Holm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 228a272653SPeter Holm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 238a272653SPeter Holm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 248a272653SPeter Holm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 258a272653SPeter Holm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 268a272653SPeter Holm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 278a272653SPeter Holm# SUCH DAMAGE. 288a272653SPeter Holm# 298a272653SPeter Holm 308a272653SPeter Holm# "panic: mutex unp not owned at ../../../kern/uipc_usrreq.c:879" seen: 318a272653SPeter Holm# https://people.freebsd.org/~pho/stress/log/mmacy018.txt 328a272653SPeter Holm# Fixed by r334756. 338a272653SPeter Holm 348a272653SPeter Holm. ../default.cfg 358a272653SPeter Holm 368a272653SPeter Holmcd /tmp 378a272653SPeter Holmcat > unix_socket.c <<EOF 388a272653SPeter Holm#include <sys/param.h> 398a272653SPeter Holm#include <sys/socket.h> 408a272653SPeter Holm#include <sys/un.h> 418a272653SPeter Holm#include <sys/wait.h> 428a272653SPeter Holm 438a272653SPeter Holm#include <netinet/in.h> 448a272653SPeter Holm 458a272653SPeter Holm#include <err.h> 468a272653SPeter Holm#include <errno.h> 478a272653SPeter Holm#include <netdb.h> 488a272653SPeter Holm#include <signal.h> 498a272653SPeter Holm#include <stdio.h> 508a272653SPeter Holm#include <stdlib.h> 518a272653SPeter Holm#include <string.h> 528a272653SPeter Holm#include <unistd.h> 538a272653SPeter Holm 548a272653SPeter Holm#define SOCK_FILE "/tmp/unix_socket.socket" 558a272653SPeter Holm 568a272653SPeter Holmstatic int 578a272653SPeter Holmclient(void) { 588a272653SPeter Holm struct sockaddr_un addr; 598a272653SPeter Holm int fd, len; 608a272653SPeter Holm char buff[8192]; 618a272653SPeter Holm 628a272653SPeter Holm if ((fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) 638a272653SPeter Holm err(1, "socket"); 648a272653SPeter Holm 658a272653SPeter Holm memset(&addr, 0, sizeof(addr)); 668a272653SPeter Holm addr.sun_family = AF_UNIX; 678a272653SPeter Holm strcpy(addr.sun_path, SOCK_FILE); 688a272653SPeter Holm unlink(SOCK_FILE); 698a272653SPeter Holm if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) 708a272653SPeter Holm err(1, "bind"); 718a272653SPeter Holm 728a272653SPeter Holm memset(&addr, 0, sizeof(addr)); 738a272653SPeter Holm addr.sun_family = AF_UNIX; 748a272653SPeter Holm strcpy(addr.sun_path, SOCK_FILE); 758a272653SPeter Holm if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) 768a272653SPeter Holm err(1, "connect"); 778a272653SPeter Holm 788a272653SPeter Holm strcpy (buff, "1234567890"); 798a272653SPeter Holm if (send(fd, buff, strlen(buff)+1, 0) == -1) 808a272653SPeter Holm err(1, "send"); 818a272653SPeter Holm printf ("sent i1234567890\n"); 828a272653SPeter Holm 838a272653SPeter Holm if ((len = recv(fd, buff, 8192, 0)) < 0) 848a272653SPeter Holm err(1, "recv"); 858a272653SPeter Holm printf ("receive %d %s\n", len, buff); 868a272653SPeter Holm 878a272653SPeter Holm if (fd >= 0) { 888a272653SPeter Holm close(fd); 898a272653SPeter Holm } 908a272653SPeter Holm unlink (SOCK_FILE); 918a272653SPeter Holm 928a272653SPeter Holm return(0); 938a272653SPeter Holm} 948a272653SPeter Holm 958a272653SPeter Holmstatic int 968a272653SPeter Holmserver() { 978a272653SPeter Holm struct sockaddr_un addr; 988a272653SPeter Holm struct sockaddr_un from; 998a272653SPeter Holm socklen_t fromlen = sizeof(from); 1008a272653SPeter Holm int fd, len, ret; 1018a272653SPeter Holm char buff[8192]; 1028a272653SPeter Holm 1038a272653SPeter Holm unlink(SOCK_FILE); 1048a272653SPeter Holm if ((fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) 1058a272653SPeter Holm err(1, "socket"); 1068a272653SPeter Holm 1078a272653SPeter Holm memset(&addr, 0, sizeof(addr)); 1088a272653SPeter Holm addr.sun_family = AF_UNIX; 1098a272653SPeter Holm strcpy(addr.sun_path, SOCK_FILE); 1108a272653SPeter Holm unlink(SOCK_FILE); 1118a272653SPeter Holm if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) 1128a272653SPeter Holm err(1, "bind"); 1138a272653SPeter Holm 1148a272653SPeter Holm alarm(2); 1158a272653SPeter Holm while ((len = recvfrom(fd, buff, 8192, 0, (struct sockaddr *)&from, 1168a272653SPeter Holm &fromlen)) > 0) { 1178a272653SPeter Holm printf ("recvfrom: %s\n", buff); 1188a272653SPeter Holm strcpy (buff, "transmit good!"); 1198a272653SPeter Holm ret = sendto(fd, buff, strlen(buff)+1, 0, 1208a272653SPeter Holm (struct sockaddr *)&from, fromlen); 1218a272653SPeter Holm if (ret < 0) { 1228a272653SPeter Holm perror("sendto"); 1238a272653SPeter Holm break; 1248a272653SPeter Holm } 1258a272653SPeter Holm } 1268a272653SPeter Holm 1278a272653SPeter Holm if (fd != -1) 1288a272653SPeter Holm close(fd); 1298a272653SPeter Holm 1308a272653SPeter Holm return (0); 1318a272653SPeter Holm} 1328a272653SPeter Holm 1338a272653SPeter Holmint 1348a272653SPeter Holmmain(void) 1358a272653SPeter Holm{ 1368a272653SPeter Holm pid_t pid; 1378a272653SPeter Holm 1388a272653SPeter Holm if ((pid = fork()) == -1) 1398a272653SPeter Holm err(1, "fork"); 1408a272653SPeter Holm 1418a272653SPeter Holm if (pid == 0) { 1428a272653SPeter Holm server(); 1438a272653SPeter Holm _exit(0); 1448a272653SPeter Holm } 1458a272653SPeter Holm sleep(2); 1468a272653SPeter Holm client(); 1478a272653SPeter Holm 1488a272653SPeter Holm if (waitpid(pid, NULL, 0) != pid) 1498a272653SPeter Holm err(1, "waitpid"); 1508a272653SPeter Holm 1518a272653SPeter Holm return (0); 1528a272653SPeter Holm} 1538a272653SPeter HolmEOF 1548a272653SPeter Holm 1558a272653SPeter Holmcc -o unix_socket -Wall -Wextra -O2 -g unix_socket.c || exit 1568a272653SPeter Holmrm unix_socket.c 1578a272653SPeter Holm 1588a272653SPeter Holm./unix_socket > /dev/null 1598a272653SPeter Holm 1608a272653SPeter Holmrm unix_socket 1618a272653SPeter Holmexit 0 162