reconnect.c (256281) | reconnect.c (281974) |
---|---|
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 unchanged lines hidden (view full) --- 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 * | 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 unchanged lines hidden (view full) --- 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: stable/10/tools/regression/sockets/reconnect/reconnect.c 140109 2005-01-12 09:57:18Z sobomax $ | 26 * $FreeBSD: stable/10/tools/regression/sockets/reconnect/reconnect.c 281974 2015-04-25 05:31:52Z ngie $ |
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 --- 5 unchanged lines hidden (view full) --- 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 | 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 --- 5 unchanged lines hidden (view full) --- 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 |
48static char *uds_name1 = NULL; 49static char *uds_name2 = NULL; | 48static char uds_name1[] = "reconnect.XXXXXXXX"; 49static char uds_name2[] = "reconnect.XXXXXXXX"; |
50 51#define sstosa(ss) ((struct sockaddr *)(ss)) 52 | 50 51#define sstosa(ss) ((struct sockaddr *)(ss)) 52 |
53void | 53static void |
54prepare_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 | 54prepare_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 |
65int | 65static int |
66create_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 | 66create_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 |
85void | 85static void |
86connect_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 | 86connect_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 |
98void | 98static void |
99cleanup(void) 100{ 101 | 99cleanup(void) 100{ 101 |
102 if (uds_name1 != NULL) 103 unlink(uds_name1); 104 if (uds_name2 != NULL) 105 unlink(uds_name2); | 102 unlink(uds_name1); 103 unlink(uds_name2); |
106} 107 108int 109main() 110{ 111 int s_sock1, s_sock2, c_sock; 112 113 atexit(cleanup); 114 | 104} 105 106int 107main() 108{ 109 int s_sock1, s_sock2, c_sock; 110 111 atexit(cleanup); 112 |
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"); | 113 if (mkstemp(uds_name1) == -1) 114 err(1, "mkstemp"); 115 unlink(uds_name1); |
121 s_sock1 = create_uds_server(uds_name1); 122 | 116 s_sock1 = create_uds_server(uds_name1); 117 |
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"); | 118 if (mkstemp(uds_name2) == -1) 119 err(1, "mkstemp"); 120 unlink(uds_name2); |
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} | 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 131 exit (0); 132} |