]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - tools/regression/sockets/reconnect/reconnect.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / tools / regression / sockets / reconnect / reconnect.c
1 /*-
2  * Copyright (c) 2005 Maxim Sobolev
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 /*
30  * The reconnect regression test is designed to catch kernel bug that may
31  * prevent changing association of already associated datagram unix domain
32  * socket when server side of connection has been closed.
33  */
34
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/uio.h>
38 #include <sys/un.h>
39 #include <err.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <signal.h>
45 #include <string.h>
46 #include <unistd.h>
47
48 static char *uds_name1 = NULL;
49 static char *uds_name2 = NULL;
50
51 #define sstosa(ss)      ((struct sockaddr *)(ss))
52
53 void
54 prepare_ifsun(struct sockaddr_un *ifsun, const char *path)
55 {
56
57     memset(ifsun, '\0', sizeof(*ifsun));
58 #if !defined(__linux__) && !defined(__solaris__)
59     ifsun->sun_len = strlen(path);
60 #endif
61     ifsun->sun_family = AF_LOCAL;
62     strcpy(ifsun->sun_path, path);
63 }
64
65 int
66 create_uds_server(const char *path)
67 {
68     struct sockaddr_un ifsun;
69     int sock;
70
71     prepare_ifsun(&ifsun, path);
72
73     unlink(ifsun.sun_path);
74
75     sock = socket(PF_LOCAL, SOCK_DGRAM, 0);
76     if (sock == -1)
77         err(1, "can't create socket");
78     setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &sock, sizeof(sock));
79     if (bind(sock, sstosa(&ifsun), sizeof(ifsun)) < 0)
80         err(1, "can't bind to a socket");
81
82     return sock;
83 }
84
85 void
86 connect_uds_server(int sock, const char *path)
87 {
88     struct sockaddr_un ifsun;
89     int e;
90
91     prepare_ifsun(&ifsun, path);
92
93     e = connect(sock, sstosa(&ifsun), sizeof(ifsun));
94     if (e < 0)
95         err(1, "can't connect to a socket");
96 }
97
98 void
99 cleanup(void)
100 {
101
102     if (uds_name1 != NULL)
103         unlink(uds_name1);
104     if (uds_name2 != NULL)
105         unlink(uds_name2);
106 }
107
108 int
109 main()
110 {
111     int s_sock1, s_sock2, c_sock;
112
113     atexit(cleanup);
114
115     uds_name1 = strdup("/tmp/reconnect.XXXXXX");
116     if (uds_name1 == NULL)
117         err(1, "can't allocate memory");
118     uds_name1 = mktemp(uds_name1);
119     if (uds_name1 == NULL)
120         err(1, "mktemp(3) failed");
121     s_sock1 = create_uds_server(uds_name1);
122
123     uds_name2 = strdup("/tmp/reconnect.XXXXXX");
124     if (uds_name2 == NULL)
125         err(1, "can't allocate memory");
126     uds_name2 = mktemp(uds_name2);
127     if (uds_name2 == NULL)
128         err(1, "mktemp(3) failed");
129     s_sock2 = create_uds_server(uds_name2);
130
131     c_sock = socket(PF_LOCAL, SOCK_DGRAM, 0);
132     if (c_sock < 0)
133         err(1, "can't create socket");
134
135     connect_uds_server(c_sock, uds_name1);
136     close(s_sock1);
137     connect_uds_server(c_sock, uds_name2);
138
139     exit (0);
140 }