| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <errno.h> | 5 #include <errno.h> |
| 6 #include <signal.h> | 6 #include <signal.h> |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <sys/types.h> | 9 #include <sys/types.h> |
| 10 #include <sys/wait.h> | 10 #include <sys/wait.h> |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #include "base/command_line.h" | 22 #include "base/command_line.h" |
| 23 #include "base/compiler_specific.h" | 23 #include "base/compiler_specific.h" |
| 24 #include "base/containers/hash_tables.h" | 24 #include "base/containers/hash_tables.h" |
| 25 #include "base/files/file_path.h" | 25 #include "base/files/file_path.h" |
| 26 #include "base/files/file_util.h" | 26 #include "base/files/file_util.h" |
| 27 #include "base/logging.h" | 27 #include "base/logging.h" |
| 28 #include "base/macros.h" | 28 #include "base/macros.h" |
| 29 #include "base/memory/linked_ptr.h" | 29 #include "base/memory/linked_ptr.h" |
| 30 #include "base/memory/weak_ptr.h" | 30 #include "base/memory/weak_ptr.h" |
| 31 #include "base/pickle.h" | 31 #include "base/pickle.h" |
| 32 #include "base/process/launch.h" |
| 32 #include "base/strings/string_number_conversions.h" | 33 #include "base/strings/string_number_conversions.h" |
| 33 #include "base/strings/string_piece.h" | 34 #include "base/strings/string_piece.h" |
| 34 #include "base/strings/string_split.h" | 35 #include "base/strings/string_split.h" |
| 35 #include "base/strings/string_util.h" | 36 #include "base/strings/string_util.h" |
| 36 #include "base/strings/stringprintf.h" | 37 #include "base/strings/stringprintf.h" |
| 37 #include "base/task_runner.h" | 38 #include "base/task_runner.h" |
| 38 #include "base/threading/thread.h" | 39 #include "base/threading/thread.h" |
| 39 #include "tools/android/forwarder2/common.h" | 40 #include "tools/android/forwarder2/common.h" |
| 40 #include "tools/android/forwarder2/daemon.h" | 41 #include "tools/android/forwarder2/daemon.h" |
| 41 #include "tools/android/forwarder2/host_controller.h" | 42 #include "tools/android/forwarder2/host_controller.h" |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 // - Remove from "adb forward" command. | 312 // - Remove from "adb forward" command. |
| 312 LOG(INFO) << "Device " << device_serial << " has no more ports."; | 313 LOG(INFO) << "Device " << device_serial << " has no more ports."; |
| 313 device_serial_to_adb_port_map_.erase(device_serial); | 314 device_serial_to_adb_port_map_.erase(device_serial); |
| 314 const std::string serial_part = device_serial.empty() ? | 315 const std::string serial_part = device_serial.empty() ? |
| 315 std::string() : std::string("-s ") + device_serial; | 316 std::string() : std::string("-s ") + device_serial; |
| 316 const std::string command = base::StringPrintf( | 317 const std::string command = base::StringPrintf( |
| 317 "%s %s forward --remove tcp:%d", | 318 "%s %s forward --remove tcp:%d", |
| 318 adb_path.c_str(), | 319 adb_path.c_str(), |
| 319 serial_part.c_str(), | 320 serial_part.c_str(), |
| 320 port); | 321 port); |
| 321 const int ret = system(command.c_str()); | 322 const base::CommandLine command_line(base::SplitString( |
| 322 LOG(INFO) << command << " ret: " << ret; | 323 command, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY)); |
| 324 int ret; |
| 325 std::string output; |
| 326 base::GetAppOutputWithExitCode(command_line, &output, &ret); |
| 327 LOG(INFO) << command << " ret: " << ret << " output: " << output; |
| 323 // Wait for the socket to be fully unmapped. | 328 // Wait for the socket to be fully unmapped. |
| 324 const std::string port_mapped_cmd = base::StringPrintf( | 329 const std::string port_mapped_cmd = base::StringPrintf( |
| 325 "lsof -nPi:%d", | 330 "lsof -nPi:%d", |
| 326 port); | 331 port); |
| 332 const base::CommandLine port_mapped_cmd_line( |
| 333 base::SplitString(port_mapped_cmd, " ", base::TRIM_WHITESPACE, |
| 334 base::SPLIT_WANT_NONEMPTY)); |
| 327 const int poll_interval_us = 500 * 1000; | 335 const int poll_interval_us = 500 * 1000; |
| 328 int retries = 3; | 336 int retries = 3; |
| 329 while (retries) { | 337 while (retries) { |
| 330 const int port_unmapped = system(port_mapped_cmd.c_str()); | 338 int port_unmapped; |
| 339 base::GetAppOutputWithExitCode(port_mapped_cmd_line, &output, |
| 340 &port_unmapped); |
| 331 LOG(INFO) << "Device " << device_serial << " port " << port << " unmap " | 341 LOG(INFO) << "Device " << device_serial << " port " << port << " unmap " |
| 332 << port_unmapped; | 342 << port_unmapped; |
| 333 if (port_unmapped) | 343 if (port_unmapped) |
| 334 break; | 344 break; |
| 335 --retries; | 345 --retries; |
| 336 usleep(poll_interval_us); | 346 usleep(poll_interval_us); |
| 337 } | 347 } |
| 338 } | 348 } |
| 339 | 349 |
| 340 int GetAdbPortForDevice(const std::string adb_path, | 350 int GetAdbPortForDevice(const std::string adb_path, |
| 341 const std::string& device_serial) { | 351 const std::string& device_serial) { |
| 342 base::hash_map<std::string, int>::const_iterator it = | 352 base::hash_map<std::string, int>::const_iterator it = |
| 343 device_serial_to_adb_port_map_.find(device_serial); | 353 device_serial_to_adb_port_map_.find(device_serial); |
| 344 if (it != device_serial_to_adb_port_map_.end()) | 354 if (it != device_serial_to_adb_port_map_.end()) |
| 345 return it->second; | 355 return it->second; |
| 346 Socket bind_socket; | 356 Socket bind_socket; |
| 347 CHECK(bind_socket.BindTcp("127.0.0.1", 0)); | 357 CHECK(bind_socket.BindTcp("127.0.0.1", 0)); |
| 348 const int port = bind_socket.GetPort(); | 358 const int port = bind_socket.GetPort(); |
| 349 bind_socket.Close(); | 359 bind_socket.Close(); |
| 350 const std::string serial_part = device_serial.empty() ? | 360 const std::string serial_part = device_serial.empty() ? |
| 351 std::string() : std::string("-s ") + device_serial; | 361 std::string() : std::string("-s ") + device_serial; |
| 352 const std::string command = base::StringPrintf( | 362 const std::string command = base::StringPrintf( |
| 353 "%s %s forward tcp:%d localabstract:chrome_device_forwarder", | 363 "%s %s forward tcp:%d localabstract:chrome_device_forwarder", |
| 354 adb_path.c_str(), | 364 adb_path.c_str(), |
| 355 serial_part.c_str(), | 365 serial_part.c_str(), |
| 356 port); | 366 port); |
| 357 LOG(INFO) << command; | 367 const base::CommandLine command_line(base::SplitString( |
| 358 const int ret = system(command.c_str()); | 368 command, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY)); |
| 369 int ret; |
| 370 std::string output; |
| 371 base::GetAppOutputWithExitCode(command_line, &output, &ret); |
| 372 LOG(INFO) << command << " ret: " << ret << " output: " << output; |
| 359 if (ret < 0 || !WIFEXITED(ret) || WEXITSTATUS(ret) != 0) | 373 if (ret < 0 || !WIFEXITED(ret) || WEXITSTATUS(ret) != 0) |
| 360 return -1; | 374 return -1; |
| 361 device_serial_to_adb_port_map_[device_serial] = port; | 375 device_serial_to_adb_port_map_[device_serial] = port; |
| 362 return port; | 376 return port; |
| 363 } | 377 } |
| 364 | 378 |
| 365 bool SendMessage(const std::string& msg, Socket* client_socket) { | 379 bool SendMessage(const std::string& msg, Socket* client_socket) { |
| 366 bool result = client_socket->WriteString(msg); | 380 bool result = client_socket->WriteString(msg); |
| 367 DCHECK(result); | 381 DCHECK(result); |
| 368 if (!result) | 382 if (!result) |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 546 | 560 |
| 547 return client_delegate.has_failed() || daemon_delegate.has_failed(); | 561 return client_delegate.has_failed() || daemon_delegate.has_failed(); |
| 548 } | 562 } |
| 549 | 563 |
| 550 } // namespace | 564 } // namespace |
| 551 } // namespace forwarder2 | 565 } // namespace forwarder2 |
| 552 | 566 |
| 553 int main(int argc, char** argv) { | 567 int main(int argc, char** argv) { |
| 554 return forwarder2::RunHostForwarder(argc, argv); | 568 return forwarder2::RunHostForwarder(argc, argv); |
| 555 } | 569 } |
| OLD | NEW |