xref: /freebsd/tools/regression/sockets/reconnect/reconnect.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
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 
27 /*
28  * The reconnect regression test is designed to catch kernel bug that may
29  * prevent changing association of already associated datagram unix domain
30  * socket when server side of connection has been closed.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <sys/uio.h>
36 #include <sys/un.h>
37 #include <err.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <signal.h>
43 #include <string.h>
44 #include <unistd.h>
45 
46 static char uds_name1[] = "reconnect.XXXXXXXX";
47 static char uds_name2[] = "reconnect.XXXXXXXX";
48 
49 #define	sstosa(ss)	((struct sockaddr *)(ss))
50 
51 static void
prepare_ifsun(struct sockaddr_un * ifsun,const char * path)52 prepare_ifsun(struct sockaddr_un *ifsun, const char *path)
53 {
54 
55     memset(ifsun, '\0', sizeof(*ifsun));
56 #if !defined(__linux__) && !defined(__solaris__)
57     ifsun->sun_len = strlen(path);
58 #endif
59     ifsun->sun_family = AF_LOCAL;
60     strcpy(ifsun->sun_path, path);
61 }
62 
63 static int
create_uds_server(const char * path)64 create_uds_server(const char *path)
65 {
66     struct sockaddr_un ifsun;
67     int sock;
68 
69     prepare_ifsun(&ifsun, path);
70 
71     unlink(ifsun.sun_path);
72 
73     sock = socket(PF_LOCAL, SOCK_DGRAM, 0);
74     if (sock == -1)
75         err(1, "can't create socket");
76     setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &sock, sizeof(sock));
77     if (bind(sock, sstosa(&ifsun), sizeof(ifsun)) < 0)
78         err(1, "can't bind to a socket");
79 
80     return sock;
81 }
82 
83 static void
connect_uds_server(int sock,const char * path)84 connect_uds_server(int sock, const char *path)
85 {
86     struct sockaddr_un ifsun;
87     int e;
88 
89     prepare_ifsun(&ifsun, path);
90 
91     e = connect(sock, sstosa(&ifsun), sizeof(ifsun));
92     if (e < 0)
93         err(1, "can't connect to a socket");
94 }
95 
96 static void
cleanup(void)97 cleanup(void)
98 {
99 
100     unlink(uds_name1);
101     unlink(uds_name2);
102 }
103 
104 int
main(void)105 main(void)
106 {
107     int s_sock1, s_sock2, c_sock;
108 
109     atexit(cleanup);
110 
111     if (mkstemp(uds_name1) == -1)
112 	err(1, "mkstemp");
113     unlink(uds_name1);
114     s_sock1 = create_uds_server(uds_name1);
115 
116     if (mkstemp(uds_name2) == -1)
117         err(1, "mkstemp");
118     unlink(uds_name2);
119     s_sock2 = create_uds_server(uds_name2);
120 
121     c_sock = socket(PF_LOCAL, SOCK_DGRAM, 0);
122     if (c_sock < 0)
123         err(1, "can't create socket");
124 
125     connect_uds_server(c_sock, uds_name1);
126     close(s_sock1);
127     connect_uds_server(c_sock, uds_name2);
128     close(s_sock2);
129 
130     exit (0);
131 }
132