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[] = "reconnect.XXXXXXXX"; 49 static char uds_name2[] = "reconnect.XXXXXXXX"; 50 51 #define sstosa(ss) ((struct sockaddr *)(ss)) 52 53 static 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 static 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 static 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 static void 99 cleanup(void) 100 { 101 102 unlink(uds_name1); 103 unlink(uds_name2); 104 } 105 106 int 107 main(void) 108 { 109 int s_sock1, s_sock2, c_sock; 110 111 atexit(cleanup); 112 113 if (mkstemp(uds_name1) == -1) 114 err(1, "mkstemp"); 115 unlink(uds_name1); 116 s_sock1 = create_uds_server(uds_name1); 117 118 if (mkstemp(uds_name2) == -1) 119 err(1, "mkstemp"); 120 unlink(uds_name2); 121 s_sock2 = create_uds_server(uds_name2); 122 123 c_sock = socket(PF_LOCAL, SOCK_DGRAM, 0); 124 if (c_sock < 0) 125 err(1, "can't create socket"); 126 127 connect_uds_server(c_sock, uds_name1); 128 close(s_sock1); 129 connect_uds_server(c_sock, uds_name2); 130 close(s_sock2); 131 132 exit (0); 133 } 134