xref: /freebsd/tools/test/stress2/misc/datagram2.sh (revision 19261079)
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# UNIX datagram socket test.
31
32# "panic: mutex unp not owned at ../../../kern/uipc_usrreq.c:879" seen.
33# Fixed by r334756.
34
35. ../default.cfg
36
37cd /tmp
38cat > datagram2.c <<EOF
39#include <sys/param.h>
40#include <sys/socket.h>
41#include <sys/un.h>
42#include <sys/wait.h>
43
44#include <netinet/in.h>
45
46#include <err.h>
47#include <errno.h>
48#include <netdb.h>
49#include <signal.h>
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
53#include <unistd.h>
54
55char *filename = "/tmp/datagram2.socket";
56
57int
58main(void) {
59
60	struct message { struct cmsghdr msg_hdr; int fd; } m;
61	struct msghdr mh;
62	struct sockaddr_un addr;
63	ssize_t len;
64	int sockfd;
65
66	unlink(filename);
67	memset(&addr, 0, sizeof(addr));
68	addr.sun_family = AF_UNIX;
69	strncpy(addr.sun_path, filename, 104);
70
71	if ((sockfd = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
72		err(1, "socket");
73
74	if (bind(sockfd, (struct sockaddr *) &addr,
75	    sizeof(addr)) == -1)
76		err(1, "bind");
77
78	if (connect(sockfd, (struct sockaddr *) &addr,
79	    sizeof(addr)) == -1)
80		err(1, "connect");
81
82	bzero(&mh, sizeof(mh));
83	bzero(&m, sizeof(m));
84	mh.msg_control = &m;
85	mh.msg_controllen = sizeof(m);
86	m.msg_hdr.cmsg_len = sizeof(m);
87	m.msg_hdr.cmsg_level = SOL_SOCKET;
88	m.msg_hdr.cmsg_type = SCM_RIGHTS;
89	m.fd = sockfd;
90	len = sendmsg(sockfd, &mh, 0);
91	if (len < 0)
92		err(1, "sendmsg");
93
94	return (0);
95}
96EOF
97
98cc -o datagram2 -Wall -Wextra -O2 -g datagram2.c || exit 1
99rm -f datagram2.c datagram2.socket
100
101./datagram2
102
103rm datagram2 datagram2.socket
104exit 0
105