2 * Copyright (c) 2005 Maxim Sobolev
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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.
35 #include <sys/types.h>
36 #include <sys/socket.h>
48 static char *uds_name1 = NULL;
49 static char *uds_name2 = NULL;
51 #define sstosa(ss) ((struct sockaddr *)(ss))
54 prepare_ifsun(struct sockaddr_un *ifsun, const char *path)
57 memset(ifsun, '\0', sizeof(*ifsun));
58 #if !defined(__linux__) && !defined(__solaris__)
59 ifsun->sun_len = strlen(path);
61 ifsun->sun_family = AF_LOCAL;
62 strcpy(ifsun->sun_path, path);
66 create_uds_server(const char *path)
68 struct sockaddr_un ifsun;
71 prepare_ifsun(&ifsun, path);
73 unlink(ifsun.sun_path);
75 sock = socket(PF_LOCAL, SOCK_DGRAM, 0);
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");
86 connect_uds_server(int sock, const char *path)
88 struct sockaddr_un ifsun;
91 prepare_ifsun(&ifsun, path);
93 e = connect(sock, sstosa(&ifsun), sizeof(ifsun));
95 err(1, "can't connect to a socket");
102 if (uds_name1 != NULL)
104 if (uds_name2 != NULL)
111 int s_sock1, s_sock2, c_sock;
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);
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);
131 c_sock = socket(PF_LOCAL, SOCK_DGRAM, 0);
133 err(1, "can't create socket");
135 connect_uds_server(c_sock, uds_name1);
137 connect_uds_server(c_sock, uds_name2);