Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: tools/android/forwarder2/host_forwarder_main.cc

Issue 2736053003: [Android] Fix port leak in the forwarder. (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/android/forwarder2/command.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 301 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 // - Remove from "adb forward" command. 312 // - Remove from "adb forward" command.
313 LOG(INFO) << "Device " << device_serial << " has no more ports."; 313 LOG(INFO) << "Device " << device_serial << " has no more ports.";
314 device_serial_to_adb_port_map_.erase(device_serial); 314 device_serial_to_adb_port_map_.erase(device_serial);
315 const std::string serial_part = device_serial.empty() ? 315 const std::string serial_part = device_serial.empty() ?
316 std::string() : std::string("-s ") + device_serial; 316 std::string() : std::string("-s ") + device_serial;
317 const std::string command = base::StringPrintf( 317 const std::string command = base::StringPrintf(
318 "%s %s forward --remove tcp:%d", 318 "%s %s forward --remove tcp:%d",
319 adb_path.c_str(), 319 adb_path.c_str(),
320 serial_part.c_str(), 320 serial_part.c_str(),
321 port); 321 port);
322 const base::CommandLine command_line(base::SplitString( 322 const std::vector<std::string> split_command = base::SplitString(
Ted C 2017/03/08 04:55:05 maybe a comment here about the need for it to be o
jbudorick 2017/03/08 05:37:40 Done.
323 command, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY)); 323 command, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
324 int ret;
325 std::string output; 324 std::string output;
326 base::GetAppOutputWithExitCode(command_line, &output, &ret); 325 if (!base::GetAppOutputAndError(split_command, &output)) {
327 LOG(INFO) << command << " ret: " << ret << " output: " << output; 326 LOG(ERROR) << command << " failed. output: " << output;
327 } else {
328 LOG(INFO) << command;
329 }
328 // Wait for the socket to be fully unmapped. 330 // Wait for the socket to be fully unmapped.
329 const std::string port_mapped_cmd = base::StringPrintf( 331 const std::string port_mapped_cmd = base::StringPrintf(
330 "lsof -nPi:%d", 332 "lsof -nPi:%d",
331 port); 333 port);
332 const base::CommandLine port_mapped_cmd_line( 334 const std::vector<std::string> port_mapped_split_cmd = base::SplitString(
333 base::SplitString(port_mapped_cmd, " ", base::TRIM_WHITESPACE, 335 port_mapped_cmd, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
334 base::SPLIT_WANT_NONEMPTY));
335 const int poll_interval_us = 500 * 1000; 336 const int poll_interval_us = 500 * 1000;
336 int retries = 3; 337 int retries = 3;
337 while (retries) { 338 while (retries) {
338 int port_unmapped; 339 // lsof failure means the port was successfully unmapped.
339 base::GetAppOutputWithExitCode(port_mapped_cmd_line, &output, 340 bool port_unmapped =
340 &port_unmapped); 341 !base::GetAppOutputAndError(port_mapped_split_cmd, &output);
341 LOG(INFO) << "Device " << device_serial << " port " << port << " unmap " 342 LOG(INFO) << "Device " << device_serial << " port " << port
342 << port_unmapped; 343 << (port_unmapped ? "" : " not") << " unmapped";
343 if (port_unmapped) 344 if (port_unmapped)
344 break; 345 break;
345 --retries; 346 --retries;
346 usleep(poll_interval_us); 347 usleep(poll_interval_us);
347 } 348 }
348 } 349 }
349 350
350 int GetAdbPortForDevice(const std::string adb_path, 351 int GetAdbPortForDevice(const std::string adb_path,
351 const std::string& device_serial) { 352 const std::string& device_serial) {
352 base::hash_map<std::string, int>::const_iterator it = 353 base::hash_map<std::string, int>::const_iterator it =
353 device_serial_to_adb_port_map_.find(device_serial); 354 device_serial_to_adb_port_map_.find(device_serial);
354 if (it != device_serial_to_adb_port_map_.end()) 355 if (it != device_serial_to_adb_port_map_.end())
355 return it->second; 356 return it->second;
356 Socket bind_socket; 357 Socket bind_socket;
357 CHECK(bind_socket.BindTcp("127.0.0.1", 0)); 358 CHECK(bind_socket.BindTcp("127.0.0.1", 0));
358 const int port = bind_socket.GetPort(); 359 const int port = bind_socket.GetPort();
359 bind_socket.Close(); 360 bind_socket.Close();
360 const std::string serial_part = device_serial.empty() ? 361 const std::string serial_part = device_serial.empty() ?
361 std::string() : std::string("-s ") + device_serial; 362 std::string() : std::string("-s ") + device_serial;
362 const std::string command = base::StringPrintf( 363 const std::string command = base::StringPrintf(
363 "%s %s forward tcp:%d localabstract:chrome_device_forwarder", 364 "%s %s forward tcp:%d localabstract:chrome_device_forwarder",
364 adb_path.c_str(), 365 adb_path.c_str(),
365 serial_part.c_str(), 366 serial_part.c_str(),
366 port); 367 port);
367 const base::CommandLine command_line(base::SplitString( 368 const std::vector<std::string> split_command = base::SplitString(
368 command, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY)); 369 command, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
369 int ret;
370 std::string output; 370 std::string output;
371 base::GetAppOutputWithExitCode(command_line, &output, &ret); 371 bool success = base::GetAppOutputAndError(split_command, &output);
372 LOG(INFO) << command << " ret: " << ret << " output: " << output; 372 if (!success) {
373 if (ret < 0 || !WIFEXITED(ret) || WEXITSTATUS(ret) != 0) 373 LOG(ERROR) << command << " failed. output: " << output;
374 return -1; 374 return -1;
375 }
376 LOG(INFO) << command;
375 device_serial_to_adb_port_map_[device_serial] = port; 377 device_serial_to_adb_port_map_[device_serial] = port;
376 return port; 378 return port;
377 } 379 }
378 380
379 bool SendMessage(const std::string& msg, Socket* client_socket) { 381 bool SendMessage(const std::string& msg, Socket* client_socket) {
380 bool result = client_socket->WriteString(msg); 382 bool result = client_socket->WriteString(msg);
381 DCHECK(result); 383 DCHECK(result);
382 if (!result) 384 if (!result)
383 has_failed_ = true; 385 has_failed_ = true;
384 return result; 386 return result;
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
560 562
561 return client_delegate.has_failed() || daemon_delegate.has_failed(); 563 return client_delegate.has_failed() || daemon_delegate.has_failed();
562 } 564 }
563 565
564 } // namespace 566 } // namespace
565 } // namespace forwarder2 567 } // namespace forwarder2
566 568
567 int main(int argc, char** argv) { 569 int main(int argc, char** argv) {
568 return forwarder2::RunHostForwarder(argc, argv); 570 return forwarder2::RunHostForwarder(argc, argv);
569 } 571 }
OLDNEW
« no previous file with comments | « tools/android/forwarder2/command.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698