Fix bad if path
[m6w6/libmemcached] / libtest / port.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Data Differential YATL (i.e. libtest) library
4 *
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 #include "libtest/yatlcon.h"
38 #include <libtest/common.h>
39
40 #include <cassert>
41 #include <cstdlib>
42 #include <cstring>
43 #include <ctime>
44 #include <fnmatch.h>
45 #include <iostream>
46 #include <sys/socket.h>
47 #include <sys/stat.h>
48 #include <sys/time.h>
49 #include <sys/types.h>
50 #include <sys/wait.h>
51 #include <unistd.h>
52
53 #include <utility>
54 #include <vector>
55
56 #include <signal.h>
57
58 #include <libtest/signal.h>
59
60 #ifndef SOCK_CLOEXEC
61 # define SOCK_CLOEXEC 0
62 #endif
63
64 #ifndef SOCK_NONBLOCK
65 # define SOCK_NONBLOCK 0
66 #endif
67
68 #ifndef FD_CLOEXEC
69 # define FD_CLOEXEC 0
70 #endif
71
72 #ifndef __INTEL_COMPILER
73 #pragma GCC diagnostic ignored "-Wold-style-cast"
74 #endif
75
76 using namespace libtest;
77
78 struct socket_st {
79 typedef std::vector< std::pair< int, in_port_t> > socket_port_t;
80 socket_port_t _pair;
81 in_port_t last_port;
82
83 socket_st():
84 last_port(0)
85 { }
86
87 void release(in_port_t _arg)
88 {
89 for (socket_port_t::iterator iter= _pair.begin();
90 iter != _pair.end();
91 ++iter)
92 {
93 if ((*iter).second == _arg)
94 {
95 shutdown((*iter).first, SHUT_RDWR);
96 close((*iter).first);
97 }
98 }
99 }
100
101 ~socket_st()
102 {
103 for (socket_port_t::iterator iter= _pair.begin();
104 iter != _pair.end();
105 ++iter)
106 {
107 shutdown((*iter).first, SHUT_RDWR);
108 close((*iter).first);
109 }
110 }
111 };
112
113 static socket_st all_socket_fd;
114
115 static in_port_t global_port= 0;
116
117 namespace libtest {
118
119 in_port_t default_port()
120 {
121 if (global_port == 0)
122 {
123 global_port= get_free_port();
124 }
125
126 return global_port;
127 }
128
129 void release_port(in_port_t arg)
130 {
131 all_socket_fd.release(arg);
132 }
133
134 in_port_t get_free_port()
135 {
136 const in_port_t default_port= in_port_t(-1);
137
138 int retries= 1024;
139
140 in_port_t ret_port;
141 while (--retries)
142 {
143 ret_port= default_port;
144 int sd;
145 if ((sd= socket(AF_INET, SOCK_STREAM, 0)) != SOCKET_ERROR)
146 {
147 int optval= 1;
148 if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) != SOCKET_ERROR)
149 {
150 struct sockaddr_in sin;
151 sin.sin_port= 0;
152 sin.sin_addr.s_addr= 0;
153 sin.sin_addr.s_addr= INADDR_ANY;
154 sin.sin_family= AF_INET;
155
156 int bind_ret;
157 do
158 {
159 if ((bind_ret= bind(sd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in) )) != SOCKET_ERROR)
160 {
161 socklen_t addrlen= sizeof(sin);
162
163 if (getsockname(sd, (struct sockaddr *)&sin, &addrlen) != -1)
164 {
165 ret_port= sin.sin_port;
166 }
167 }
168 else
169 {
170 if (errno != EADDRINUSE)
171 {
172 Error << strerror(errno);
173 }
174 }
175
176 if (errno == EADDRINUSE)
177 {
178 libtest::dream(2, 0);
179 }
180 } while (bind_ret == -1 and errno == EADDRINUSE);
181
182 all_socket_fd._pair.push_back(std::make_pair(sd, ret_port));
183 }
184 else
185 {
186 Error << strerror(errno);
187 }
188 }
189 else
190 {
191 Error << strerror(errno);
192 }
193
194 if (ret_port == default_port)
195 {
196 Error << "no ret_port set:" << strerror(errno);
197 }
198 else if (ret_port > 1024 and ret_port != all_socket_fd.last_port)
199 {
200 break;
201 }
202 }
203
204 // We handle the case where if we max out retries, we still abort.
205 if (retries == 0)
206 {
207 FATAL("No port could be found, exhausted retry");
208 }
209
210 if (ret_port == 0)
211 {
212 FATAL("No port could be found");
213 }
214
215 if (ret_port == default_port)
216 {
217 FATAL("No port could be found");
218 }
219
220 if (ret_port <= 1024)
221 {
222 FATAL("No port could be found, though some where available below or at 1024");
223 }
224
225 all_socket_fd.last_port= ret_port;
226 release_port(ret_port);
227
228 return ret_port;
229 }
230
231 } // namespace libtest