| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/test/chromedriver/net/port_server.h" | 5 #include "chrome/test/chromedriver/net/port_server.h" |
| 6 | 6 |
| 7 #include <stddef.h> |
| 8 #include <string.h> |
| 9 |
| 7 #include "base/bind.h" | 10 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 9 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/macros.h" |
| 10 #include "base/process/process_handle.h" | 14 #include "base/process/process_handle.h" |
| 11 #include "base/rand_util.h" | 15 #include "base/rand_util.h" |
| 12 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/sync_socket.h" | 17 #include "base/sync_socket.h" |
| 18 #include "build/build_config.h" |
| 14 #include "chrome/test/chromedriver/chrome/status.h" | 19 #include "chrome/test/chromedriver/chrome/status.h" |
| 15 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
| 16 #include "net/base/net_util.h" | 21 #include "net/base/net_util.h" |
| 17 #include "net/base/sys_addrinfo.h" | 22 #include "net/base/sys_addrinfo.h" |
| 18 #include "net/log/net_log.h" | 23 #include "net/log/net_log.h" |
| 19 #include "net/socket/tcp_server_socket.h" | 24 #include "net/socket/tcp_server_socket.h" |
| 20 | 25 |
| 21 #if defined(OS_LINUX) | 26 #if defined(OS_LINUX) |
| 22 #include <sys/socket.h> | 27 #include <sys/socket.h> |
| 23 #include <sys/un.h> | 28 #include <sys/un.h> |
| 24 #endif | 29 #endif |
| 25 | 30 |
| 26 PortReservation::PortReservation(const base::Closure& on_free_func, uint16 port) | 31 PortReservation::PortReservation(const base::Closure& on_free_func, |
| 32 uint16_t port) |
| 27 : on_free_func_(on_free_func), port_(port) {} | 33 : on_free_func_(on_free_func), port_(port) {} |
| 28 | 34 |
| 29 PortReservation::~PortReservation() { | 35 PortReservation::~PortReservation() { |
| 30 if (!on_free_func_.is_null()) | 36 if (!on_free_func_.is_null()) |
| 31 on_free_func_.Run(); | 37 on_free_func_.Run(); |
| 32 } | 38 } |
| 33 | 39 |
| 34 void PortReservation::Leak() { | 40 void PortReservation::Leak() { |
| 35 LOG(ERROR) << "Port leaked: " << port_; | 41 LOG(ERROR) << "Port leaked: " << port_; |
| 36 on_free_func_.Reset(); | 42 on_free_func_.Reset(); |
| 37 } | 43 } |
| 38 | 44 |
| 39 PortServer::PortServer(const std::string& path) : path_(path) { | 45 PortServer::PortServer(const std::string& path) : path_(path) { |
| 40 CHECK(path_.size() && path_[0] == 0) | 46 CHECK(path_.size() && path_[0] == 0) |
| 41 << "path must be for Linux abstract namespace"; | 47 << "path must be for Linux abstract namespace"; |
| 42 } | 48 } |
| 43 | 49 |
| 44 PortServer::~PortServer() {} | 50 PortServer::~PortServer() {} |
| 45 | 51 |
| 46 Status PortServer::ReservePort(uint16* port, | 52 Status PortServer::ReservePort(uint16_t* port, |
| 47 scoped_ptr<PortReservation>* reservation) { | 53 scoped_ptr<PortReservation>* reservation) { |
| 48 uint16 port_to_use = 0; | 54 uint16_t port_to_use = 0; |
| 49 { | 55 { |
| 50 base::AutoLock lock(free_lock_); | 56 base::AutoLock lock(free_lock_); |
| 51 if (free_.size()) { | 57 if (free_.size()) { |
| 52 port_to_use = free_.front(); | 58 port_to_use = free_.front(); |
| 53 free_.pop_front(); | 59 free_.pop_front(); |
| 54 } | 60 } |
| 55 } | 61 } |
| 56 if (!port_to_use) { | 62 if (!port_to_use) { |
| 57 Status status = RequestPort(&port_to_use); | 63 Status status = RequestPort(&port_to_use); |
| 58 if (status.IsError()) | 64 if (status.IsError()) |
| 59 return status; | 65 return status; |
| 60 } | 66 } |
| 61 *port = port_to_use; | 67 *port = port_to_use; |
| 62 reservation->reset(new PortReservation( | 68 reservation->reset(new PortReservation( |
| 63 base::Bind(&PortServer::ReleasePort, base::Unretained(this), port_to_use), | 69 base::Bind(&PortServer::ReleasePort, base::Unretained(this), port_to_use), |
| 64 port_to_use)); | 70 port_to_use)); |
| 65 return Status(kOk); | 71 return Status(kOk); |
| 66 } | 72 } |
| 67 | 73 |
| 68 Status PortServer::RequestPort(uint16* port) { | 74 Status PortServer::RequestPort(uint16_t* port) { |
| 69 // The client sends its PID + \n, and the server responds with a port + \n, | 75 // The client sends its PID + \n, and the server responds with a port + \n, |
| 70 // which is valid for the lifetime of the referred process. | 76 // which is valid for the lifetime of the referred process. |
| 71 #if defined(OS_LINUX) | 77 #if defined(OS_LINUX) |
| 72 int sock_fd = socket(AF_UNIX, SOCK_STREAM, 0); | 78 int sock_fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 73 if (sock_fd < 0) | 79 if (sock_fd < 0) |
| 74 return Status(kUnknownError, "unable to create socket"); | 80 return Status(kUnknownError, "unable to create socket"); |
| 75 base::SyncSocket sock(sock_fd); | 81 base::SyncSocket sock(sock_fd); |
| 76 struct timeval tv; | 82 struct timeval tv; |
| 77 tv.tv_sec = 10; | 83 tv.tv_sec = 10; |
| 78 tv.tv_usec = 0; | 84 tv.tv_usec = 0; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 if (response.empty()) | 123 if (response.empty()) |
| 118 return Status(kUnknownError, "failed to receive portserver response"); | 124 return Status(kUnknownError, "failed to receive portserver response"); |
| 119 VLOG(0) << "PORTSERVER RESPONSE " << response; | 125 VLOG(0) << "PORTSERVER RESPONSE " << response; |
| 120 | 126 |
| 121 int new_port = 0; | 127 int new_port = 0; |
| 122 if (*response.rbegin() != '\n' || | 128 if (*response.rbegin() != '\n' || |
| 123 !base::StringToInt(response.substr(0, response.length() - 1), | 129 !base::StringToInt(response.substr(0, response.length() - 1), |
| 124 &new_port) || | 130 &new_port) || |
| 125 new_port < 0 || new_port > 65535) | 131 new_port < 0 || new_port > 65535) |
| 126 return Status(kUnknownError, "failed to parse portserver response"); | 132 return Status(kUnknownError, "failed to parse portserver response"); |
| 127 *port = static_cast<uint16>(new_port); | 133 *port = static_cast<uint16_t>(new_port); |
| 128 return Status(kOk); | 134 return Status(kOk); |
| 129 #else | 135 #else |
| 130 return Status(kUnknownError, "not implemented for this platform"); | 136 return Status(kUnknownError, "not implemented for this platform"); |
| 131 #endif | 137 #endif |
| 132 } | 138 } |
| 133 | 139 |
| 134 void PortServer::ReleasePort(uint16 port) { | 140 void PortServer::ReleasePort(uint16_t port) { |
| 135 base::AutoLock lock(free_lock_); | 141 base::AutoLock lock(free_lock_); |
| 136 free_.push_back(port); | 142 free_.push_back(port); |
| 137 } | 143 } |
| 138 | 144 |
| 139 PortManager::PortManager(uint16 min_port, uint16 max_port) | 145 PortManager::PortManager(uint16_t min_port, uint16_t max_port) |
| 140 : min_port_(min_port), max_port_(max_port) { | 146 : min_port_(min_port), max_port_(max_port) { |
| 141 CHECK_GE(max_port_, min_port_); | 147 CHECK_GE(max_port_, min_port_); |
| 142 } | 148 } |
| 143 | 149 |
| 144 PortManager::~PortManager() {} | 150 PortManager::~PortManager() {} |
| 145 | 151 |
| 146 uint16 PortManager::FindAvailablePort() const { | 152 uint16_t PortManager::FindAvailablePort() const { |
| 147 uint16 start = static_cast<uint16>(base::RandInt(min_port_, max_port_)); | 153 uint16_t start = static_cast<uint16_t>(base::RandInt(min_port_, max_port_)); |
| 148 bool wrapped = false; | 154 bool wrapped = false; |
| 149 for (uint32 try_port = start; try_port != start || !wrapped; ++try_port) { | 155 for (uint32_t try_port = start; try_port != start || !wrapped; ++try_port) { |
| 150 if (try_port > max_port_) { | 156 if (try_port > max_port_) { |
| 151 wrapped = true; | 157 wrapped = true; |
| 152 if (min_port_ == max_port_) | 158 if (min_port_ == max_port_) |
| 153 break; | 159 break; |
| 154 try_port = min_port_; | 160 try_port = min_port_; |
| 155 } | 161 } |
| 156 uint16 try_port_uint16 = static_cast<uint16>(try_port); | 162 uint16_t try_port_uint16 = static_cast<uint16_t>(try_port); |
| 157 if (taken_.count(try_port_uint16)) | 163 if (taken_.count(try_port_uint16)) |
| 158 continue; | 164 continue; |
| 159 | 165 |
| 160 char parts[] = {127, 0, 0, 1}; | 166 char parts[] = {127, 0, 0, 1}; |
| 161 net::IPAddressNumber address(parts, parts + arraysize(parts)); | 167 net::IPAddressNumber address(parts, parts + arraysize(parts)); |
| 162 net::NetLog::Source source; | 168 net::NetLog::Source source; |
| 163 net::TCPServerSocket sock(NULL, source); | 169 net::TCPServerSocket sock(NULL, source); |
| 164 if (sock.Listen(net::IPEndPoint(address, try_port_uint16), 1) == net::OK) | 170 if (sock.Listen(net::IPEndPoint(address, try_port_uint16), 1) == net::OK) |
| 165 return try_port_uint16; | 171 return try_port_uint16; |
| 166 } | 172 } |
| 167 return 0; | 173 return 0; |
| 168 } | 174 } |
| 169 | 175 |
| 170 Status PortManager::ReservePort(uint16* port, | 176 Status PortManager::ReservePort(uint16_t* port, |
| 171 scoped_ptr<PortReservation>* reservation) { | 177 scoped_ptr<PortReservation>* reservation) { |
| 172 base::AutoLock lock(lock_); | 178 base::AutoLock lock(lock_); |
| 173 uint16 port_to_use = FindAvailablePort(); | 179 uint16_t port_to_use = FindAvailablePort(); |
| 174 if (!port_to_use) | 180 if (!port_to_use) |
| 175 return Status(kUnknownError, "unable to find open port"); | 181 return Status(kUnknownError, "unable to find open port"); |
| 176 | 182 |
| 177 taken_.insert(port_to_use); | 183 taken_.insert(port_to_use); |
| 178 *port = port_to_use; | 184 *port = port_to_use; |
| 179 reservation->reset(new PortReservation( | 185 reservation->reset(new PortReservation( |
| 180 base::Bind(&PortManager::ReleasePort, base::Unretained(this), | 186 base::Bind(&PortManager::ReleasePort, base::Unretained(this), |
| 181 port_to_use), | 187 port_to_use), |
| 182 port_to_use)); | 188 port_to_use)); |
| 183 return Status(kOk); | 189 return Status(kOk); |
| 184 } | 190 } |
| 185 | 191 |
| 186 Status PortManager::ReservePortFromPool( | 192 Status PortManager::ReservePortFromPool( |
| 187 uint16* port, | 193 uint16_t* port, |
| 188 scoped_ptr<PortReservation>* reservation) { | 194 scoped_ptr<PortReservation>* reservation) { |
| 189 base::AutoLock lock(lock_); | 195 base::AutoLock lock(lock_); |
| 190 uint16 port_to_use = 0; | 196 uint16_t port_to_use = 0; |
| 191 if (unused_forwarded_port_.size()) { | 197 if (unused_forwarded_port_.size()) { |
| 192 port_to_use = unused_forwarded_port_.front(); | 198 port_to_use = unused_forwarded_port_.front(); |
| 193 unused_forwarded_port_.pop_front(); | 199 unused_forwarded_port_.pop_front(); |
| 194 } else { | 200 } else { |
| 195 port_to_use = FindAvailablePort(); | 201 port_to_use = FindAvailablePort(); |
| 196 } | 202 } |
| 197 if (!port_to_use) | 203 if (!port_to_use) |
| 198 return Status(kUnknownError, "unable to find open port"); | 204 return Status(kUnknownError, "unable to find open port"); |
| 199 | 205 |
| 200 taken_.insert(port_to_use); | 206 taken_.insert(port_to_use); |
| 201 *port = port_to_use; | 207 *port = port_to_use; |
| 202 reservation->reset(new PortReservation( | 208 reservation->reset(new PortReservation( |
| 203 base::Bind(&PortManager::ReleasePortToPool, base::Unretained(this), | 209 base::Bind(&PortManager::ReleasePortToPool, base::Unretained(this), |
| 204 port_to_use), | 210 port_to_use), |
| 205 port_to_use)); | 211 port_to_use)); |
| 206 return Status(kOk); | 212 return Status(kOk); |
| 207 } | 213 } |
| 208 | 214 |
| 209 void PortManager::ReleasePort(uint16 port) { | 215 void PortManager::ReleasePort(uint16_t port) { |
| 210 base::AutoLock lock(lock_); | 216 base::AutoLock lock(lock_); |
| 211 taken_.erase(port); | 217 taken_.erase(port); |
| 212 } | 218 } |
| 213 | 219 |
| 214 void PortManager::ReleasePortToPool(uint16 port) { | 220 void PortManager::ReleasePortToPool(uint16_t port) { |
| 215 base::AutoLock lock(lock_); | 221 base::AutoLock lock(lock_); |
| 216 taken_.erase(port); | 222 taken_.erase(port); |
| 217 unused_forwarded_port_.push_back(port); | 223 unused_forwarded_port_.push_back(port); |
| 218 } | 224 } |
| OLD | NEW |