| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "base/test/launcher/test_launcher.h" | 5 #include "base/test/launcher/test_launcher.h" |
| 6 | 6 |
| 7 #if defined(OS_POSIX) | 7 #if defined(OS_POSIX) |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #endif | 9 #endif |
| 10 | 10 |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 } | 280 } |
| 281 #endif // defined(OS_WIN) | 281 #endif // defined(OS_WIN) |
| 282 | 282 |
| 283 #if defined(OS_LINUX) | 283 #if defined(OS_LINUX) |
| 284 // To prevent accidental privilege sharing to an untrusted child, processes | 284 // To prevent accidental privilege sharing to an untrusted child, processes |
| 285 // are started with PR_SET_NO_NEW_PRIVS. Do not set that here, since this | 285 // are started with PR_SET_NO_NEW_PRIVS. Do not set that here, since this |
| 286 // new child will be privileged and trusted. | 286 // new child will be privileged and trusted. |
| 287 new_options.allow_new_privs = true; | 287 new_options.allow_new_privs = true; |
| 288 #endif | 288 #endif |
| 289 | 289 |
| 290 ProcessHandle process_handle; | 290 Process process; |
| 291 | 291 |
| 292 { | 292 { |
| 293 // Note how we grab the lock before the process possibly gets created. | 293 // Note how we grab the lock before the process possibly gets created. |
| 294 // This ensures that when the lock is held, ALL the processes are registered | 294 // This ensures that when the lock is held, ALL the processes are registered |
| 295 // in the set. | 295 // in the set. |
| 296 AutoLock lock(g_live_processes_lock.Get()); | 296 AutoLock lock(g_live_processes_lock.Get()); |
| 297 | 297 |
| 298 if (!LaunchProcess(command_line, new_options, &process_handle)) | 298 process = LaunchProcess(command_line, new_options); |
| 299 if (!process.IsValid()) |
| 299 return -1; | 300 return -1; |
| 300 | 301 |
| 301 g_live_processes.Get().insert(std::make_pair(process_handle, command_line)); | 302 // TODO(rvargas) crbug.com/417532: Don't store process handles. |
| 303 g_live_processes.Get().insert(std::make_pair(process.Handle(), |
| 304 command_line)); |
| 302 } | 305 } |
| 303 | 306 |
| 304 int exit_code = 0; | 307 int exit_code = 0; |
| 305 if (!WaitForExitCodeWithTimeout(process_handle, &exit_code, timeout)) { | 308 if (!process.WaitForExitWithTimeout(timeout, &exit_code)) { |
| 306 *was_timeout = true; | 309 *was_timeout = true; |
| 307 exit_code = -1; // Set a non-zero exit code to signal a failure. | 310 exit_code = -1; // Set a non-zero exit code to signal a failure. |
| 308 | 311 |
| 309 // Ensure that the process terminates. | 312 // Ensure that the process terminates. |
| 310 KillProcess(process_handle, -1, true); | 313 KillProcess(process.Handle(), -1, true); |
| 311 } | 314 } |
| 312 | 315 |
| 313 { | 316 { |
| 314 // Note how we grab the log before issuing a possibly broad process kill. | 317 // Note how we grab the log before issuing a possibly broad process kill. |
| 315 // Other code parts that grab the log kill processes, so avoid trying | 318 // Other code parts that grab the log kill processes, so avoid trying |
| 316 // to do that twice and trigger all kinds of log messages. | 319 // to do that twice and trigger all kinds of log messages. |
| 317 AutoLock lock(g_live_processes_lock.Get()); | 320 AutoLock lock(g_live_processes_lock.Get()); |
| 318 | 321 |
| 319 #if defined(OS_POSIX) | 322 #if defined(OS_POSIX) |
| 320 if (exit_code != 0) { | 323 if (exit_code != 0) { |
| 321 // On POSIX, in case the test does not exit cleanly, either due to a crash | 324 // On POSIX, in case the test does not exit cleanly, either due to a crash |
| 322 // or due to it timing out, we need to clean up any child processes that | 325 // or due to it timing out, we need to clean up any child processes that |
| 323 // it might have created. On Windows, child processes are automatically | 326 // it might have created. On Windows, child processes are automatically |
| 324 // cleaned up using JobObjects. | 327 // cleaned up using JobObjects. |
| 325 KillProcessGroup(process_handle); | 328 KillProcessGroup(process.Handle()); |
| 326 } | 329 } |
| 327 #endif | 330 #endif |
| 328 | 331 |
| 329 g_live_processes.Get().erase(process_handle); | 332 g_live_processes.Get().erase(process.Handle()); |
| 330 } | 333 } |
| 331 | 334 |
| 332 CloseProcessHandle(process_handle); | |
| 333 | |
| 334 return exit_code; | 335 return exit_code; |
| 335 } | 336 } |
| 336 | 337 |
| 337 void RunCallback( | 338 void RunCallback( |
| 338 const TestLauncher::LaunchChildGTestProcessCallback& callback, | 339 const TestLauncher::LaunchChildGTestProcessCallback& callback, |
| 339 int exit_code, | 340 int exit_code, |
| 340 const TimeDelta& elapsed_time, | 341 const TimeDelta& elapsed_time, |
| 341 bool was_timeout, | 342 bool was_timeout, |
| 342 const std::string& output) { | 343 const std::string& output) { |
| 343 callback.Run(exit_code, elapsed_time, was_timeout, output); | 344 callback.Run(exit_code, elapsed_time, was_timeout, output); |
| (...skipping 736 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1080 } | 1081 } |
| 1081 | 1082 |
| 1082 std::string snippet(full_output.substr(run_pos)); | 1083 std::string snippet(full_output.substr(run_pos)); |
| 1083 if (end_pos != std::string::npos) | 1084 if (end_pos != std::string::npos) |
| 1084 snippet = full_output.substr(run_pos, end_pos - run_pos); | 1085 snippet = full_output.substr(run_pos, end_pos - run_pos); |
| 1085 | 1086 |
| 1086 return snippet; | 1087 return snippet; |
| 1087 } | 1088 } |
| 1088 | 1089 |
| 1089 } // namespace base | 1090 } // namespace base |
| OLD | NEW |