port.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. #include "libtest/yatlcon.h"
  37. #include <libtest/common.h>
  38. #include <cassert>
  39. #include <cstdlib>
  40. #include <cstring>
  41. #include <ctime>
  42. #include <fnmatch.h>
  43. #include <iostream>
  44. #include <sys/socket.h>
  45. #include <sys/stat.h>
  46. #include <sys/time.h>
  47. #include <sys/types.h>
  48. #include <sys/wait.h>
  49. #include <unistd.h>
  50. #include <utility>
  51. #include <vector>
  52. #include <signal.h>
  53. #include <libtest/signal.h>
  54. #ifndef SOCK_CLOEXEC
  55. # define SOCK_CLOEXEC 0
  56. #endif
  57. #ifndef SOCK_NONBLOCK
  58. # define SOCK_NONBLOCK 0
  59. #endif
  60. #ifndef FD_CLOEXEC
  61. # define FD_CLOEXEC 0
  62. #endif
  63. #ifndef __INTEL_COMPILER
  64. #pragma GCC diagnostic ignored "-Wold-style-cast"
  65. #endif
  66. using namespace libtest;
  67. struct socket_st {
  68. typedef std::vector< std::pair< int, in_port_t> > socket_port_t;
  69. socket_port_t _pair;
  70. in_port_t last_port;
  71. socket_st():
  72. last_port(0)
  73. { }
  74. void release(in_port_t _arg)
  75. {
  76. for (socket_port_t::iterator iter= _pair.begin();
  77. iter != _pair.end();
  78. ++iter)
  79. {
  80. if ((*iter).second == _arg)
  81. {
  82. shutdown((*iter).first, SHUT_RDWR);
  83. close((*iter).first);
  84. }
  85. }
  86. }
  87. ~socket_st()
  88. {
  89. for (socket_port_t::iterator iter= _pair.begin();
  90. iter != _pair.end();
  91. ++iter)
  92. {
  93. shutdown((*iter).first, SHUT_RDWR);
  94. close((*iter).first);
  95. }
  96. }
  97. };
  98. static socket_st all_socket_fd;
  99. static in_port_t global_port= 0;
  100. static void initialize_default_port()
  101. {
  102. global_port= get_free_port();
  103. }
  104. static pthread_once_t default_port_once= PTHREAD_ONCE_INIT;
  105. namespace libtest {
  106. in_port_t default_port()
  107. {
  108. {
  109. int ret;
  110. if ((ret= pthread_once(&default_port_once, initialize_default_port)) != 0)
  111. {
  112. FATAL(strerror(ret));
  113. }
  114. }
  115. return global_port;
  116. }
  117. void release_port(in_port_t arg)
  118. {
  119. all_socket_fd.release(arg);
  120. }
  121. in_port_t get_free_port()
  122. {
  123. const in_port_t default_port= in_port_t(-1);
  124. int retries= 1024;
  125. in_port_t ret_port;
  126. while (--retries)
  127. {
  128. ret_port= default_port;
  129. int sd;
  130. if ((sd= socket(AF_INET, SOCK_STREAM, 0)) != SOCKET_ERROR)
  131. {
  132. int optval= 1;
  133. if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) != SOCKET_ERROR)
  134. {
  135. struct sockaddr_in sin;
  136. sin.sin_port= 0;
  137. sin.sin_addr.s_addr= INADDR_ANY;
  138. sin.sin_family= AF_INET;
  139. int bind_ret;
  140. do
  141. {
  142. if ((bind_ret= bind(sd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in) )) != SOCKET_ERROR)
  143. {
  144. socklen_t addrlen= sizeof(sin);
  145. if (getsockname(sd, (struct sockaddr *)&sin, &addrlen) != -1)
  146. {
  147. ret_port= sin.sin_port;
  148. }
  149. }
  150. else
  151. {
  152. if (errno != EADDRINUSE)
  153. {
  154. Error << strerror(errno);
  155. }
  156. }
  157. if (errno == EADDRINUSE)
  158. {
  159. libtest::dream(2, 0);
  160. }
  161. } while (bind_ret == -1 and errno == EADDRINUSE);
  162. all_socket_fd._pair.push_back(std::make_pair(sd, ret_port));
  163. }
  164. else
  165. {
  166. Error << strerror(errno);
  167. }
  168. }
  169. else
  170. {
  171. Error << strerror(errno);
  172. }
  173. if (ret_port == default_port)
  174. {
  175. Error << "no ret_port set:" << strerror(errno);
  176. }
  177. else if (ret_port > 1024 and ret_port != all_socket_fd.last_port)
  178. {
  179. break;
  180. }
  181. }
  182. // We handle the case where if we max out retries, we still abort.
  183. if (retries == 0)
  184. {
  185. FATAL("No port could be found, exhausted retry");
  186. }
  187. if (ret_port == 0)
  188. {
  189. FATAL("No port could be found");
  190. }
  191. if (ret_port == default_port)
  192. {
  193. FATAL("No port could be found");
  194. }
  195. if (ret_port <= 1024)
  196. {
  197. FATAL("No port could be found, though some where available below or at 1024");
  198. }
  199. all_socket_fd.last_port= ret_port;
  200. release_port(ret_port);
  201. return ret_port;
  202. }
  203. } // namespace libtest