Home | History | Annotate | Download | only in posix
      1 /* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 #include "tensorflow/core/platform/net.h"
     17 
     18 #include <cerrno>
     19 #include <cstdlib>
     20 #include <unordered_set>
     21 
     22 #include <netinet/in.h>
     23 #include <sys/socket.h>
     24 #include <sys/types.h>
     25 #include <unistd.h>
     26 
     27 #include "tensorflow/core/lib/strings/strcat.h"
     28 #include "tensorflow/core/platform/logging.h"
     29 
     30 namespace tensorflow {
     31 namespace internal {
     32 
     33 namespace {
     34 bool IsPortAvailable(int* port, bool is_tcp) {
     35   const int protocol = is_tcp ? IPPROTO_TCP : 0;
     36   const int fd = socket(AF_INET, is_tcp ? SOCK_STREAM : SOCK_DGRAM, protocol);
     37 
     38   struct sockaddr_in addr;
     39   socklen_t addr_len = sizeof(addr);
     40   int actual_port;
     41 
     42   CHECK_GE(*port, 0);
     43   CHECK_LE(*port, 65535);
     44   if (fd < 0) {
     45     LOG(ERROR) << "socket() failed: " << strerror(errno);
     46     return false;
     47   }
     48 
     49   // SO_REUSEADDR lets us start up a server immediately after it exists.
     50   int one = 1;
     51   if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) {
     52     LOG(ERROR) << "setsockopt() failed: " << strerror(errno);
     53     close(fd);
     54     return false;
     55   }
     56 
     57   // Try binding to port.
     58   addr.sin_family = AF_INET;
     59   addr.sin_addr.s_addr = INADDR_ANY;
     60   addr.sin_port = htons(static_cast<uint16_t>(*port));
     61   if (bind(fd, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)) < 0) {
     62     LOG(WARNING) << "bind(port=" << *port << ") failed: " << strerror(errno);
     63     close(fd);
     64     return false;
     65   }
     66 
     67   // Get the bound port number.
     68   if (getsockname(fd, reinterpret_cast<struct sockaddr*>(&addr), &addr_len) <
     69       0) {
     70     LOG(WARNING) << "getsockname() failed: " << strerror(errno);
     71     close(fd);
     72     return false;
     73   }
     74   CHECK_LE(addr_len, sizeof(addr));
     75   actual_port = ntohs(addr.sin_port);
     76   CHECK_GT(actual_port, 0);
     77   if (*port == 0) {
     78     *port = actual_port;
     79   } else {
     80     CHECK_EQ(*port, actual_port);
     81   }
     82   close(fd);
     83   return true;
     84 }
     85 
     86 const int kNumRandomPortsToPick = 100;
     87 const int kMaximumTrials = 1000;
     88 
     89 }  // namespace
     90 
     91 int PickUnusedPortOrDie() {
     92   static std::unordered_set<int> chosen_ports;
     93 
     94   // Type of port to first pick in the next iteration.
     95   bool is_tcp = true;
     96   int trial = 0;
     97   while (true) {
     98     int port;
     99     trial++;
    100     CHECK_LE(trial, kMaximumTrials)
    101         << "Failed to pick an unused port for testing.";
    102     if (trial == 1) {
    103       port = getpid() % (65536 - 30000) + 30000;
    104     } else if (trial <= kNumRandomPortsToPick) {
    105       port = rand() % (65536 - 30000) + 30000;
    106     } else {
    107       port = 0;
    108     }
    109 
    110     if (chosen_ports.find(port) != chosen_ports.end()) {
    111       continue;
    112     }
    113     if (!IsPortAvailable(&port, is_tcp)) {
    114       continue;
    115     }
    116 
    117     CHECK_GT(port, 0);
    118     if (!IsPortAvailable(&port, !is_tcp)) {
    119       is_tcp = !is_tcp;
    120       continue;
    121     }
    122 
    123     chosen_ports.insert(port);
    124     return port;
    125   }
    126 
    127   return 0;
    128 }
    129 
    130 }  // namespace internal
    131 }  // namespace tensorflow
    132