Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include <process.h> | 5 #include <process.h> |
| 6 | 6 |
| 7 #include "bin/builtin.h" | 7 #include "bin/builtin.h" |
| 8 #include "bin/process.h" | 8 #include "bin/process.h" |
| 9 #include "bin/eventhandler.h" | 9 #include "bin/eventhandler.h" |
| 10 #include "bin/log.h" | 10 #include "bin/log.h" |
| (...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 382 return error_code; | 382 return error_code; |
| 383 } | 383 } |
| 384 if (!CreateProcessPipe(exit_handles, pipe_names[3], kInheritNone)) { | 384 if (!CreateProcessPipe(exit_handles, pipe_names[3], kInheritNone)) { |
| 385 int error_code = SetOsErrorMessage(os_error_message, os_error_message_len); | 385 int error_code = SetOsErrorMessage(os_error_message, os_error_message_len); |
| 386 CloseProcessPipes( | 386 CloseProcessPipes( |
| 387 stdin_handles, stdout_handles, stderr_handles, exit_handles); | 387 stdin_handles, stdout_handles, stderr_handles, exit_handles); |
| 388 return error_code; | 388 return error_code; |
| 389 } | 389 } |
| 390 | 390 |
| 391 // Setup info structures. | 391 // Setup info structures. |
| 392 STARTUPINFO startup_info; | 392 STARTUPINFOEX startup_info; |
| 393 ZeroMemory(&startup_info, sizeof(startup_info)); | 393 ZeroMemory(&startup_info, sizeof(startup_info)); |
| 394 startup_info.cb = sizeof(startup_info); | 394 startup_info.StartupInfo.cb = sizeof(startup_info); |
| 395 startup_info.hStdInput = stdin_handles[kReadHandle]; | 395 startup_info.StartupInfo.hStdInput = stdin_handles[kReadHandle]; |
| 396 startup_info.hStdOutput = stdout_handles[kWriteHandle]; | 396 startup_info.StartupInfo.hStdOutput = stdout_handles[kWriteHandle]; |
| 397 startup_info.hStdError = stderr_handles[kWriteHandle]; | 397 startup_info.StartupInfo.hStdError = stderr_handles[kWriteHandle]; |
| 398 startup_info.dwFlags = STARTF_USESTDHANDLES; | 398 startup_info.StartupInfo.dwFlags = STARTF_USESTDHANDLES; |
| 399 | |
| 400 // Setup the handles to inherit. We only want to inherit the three handles | |
| 401 // for stdin, stdout and stderr. | |
| 402 SIZE_T size = 0; | |
| 403 if (!InitializeProcThreadAttributeList(NULL, 1, 0, &size) && | |
| 404 GetLastError() != ERROR_INSUFFICIENT_BUFFER) { | |
|
ahe
2012/12/10 10:30:51
Why are you ignoring ERROR_INSUFFICIENT_BUFFER?
Mads Ager (google)
2012/12/10 10:45:29
I will add a comment about that. When you pass in
| |
| 405 int error_code = SetOsErrorMessage(os_error_message, os_error_message_len); | |
| 406 CloseProcessPipes( | |
| 407 stdin_handles, stdout_handles, stderr_handles, exit_handles); | |
| 408 return error_code; | |
| 409 } | |
| 410 LPPROC_THREAD_ATTRIBUTE_LIST attribute_list = | |
| 411 reinterpret_cast<LPPROC_THREAD_ATTRIBUTE_LIST>(malloc(size)); | |
| 412 ZeroMemory(attribute_list, size); | |
| 413 if (!InitializeProcThreadAttributeList(attribute_list, 1, 0, &size)) { | |
| 414 free(attribute_list); | |
|
ahe
2012/12/10 10:30:51
Shouldn't you call free after getting the error me
Mads Ager (google)
2012/12/10 10:45:29
Yes, thanks!
| |
| 415 int error_code = SetOsErrorMessage(os_error_message, os_error_message_len); | |
| 416 CloseProcessPipes( | |
| 417 stdin_handles, stdout_handles, stderr_handles, exit_handles); | |
| 418 return error_code; | |
| 419 } | |
| 420 int num_inherited_handles = 3; | |
| 421 HANDLE* inherited_handles = new HANDLE[num_inherited_handles]; | |
|
kustermann
2012/12/08 11:53:42
Is there a reason why you allocate this array on t
Mads Ager (google)
2012/12/10 10:45:29
Thanks for catching that Martin. No, there is no r
| |
| 422 inherited_handles[0] = stdin_handles[kReadHandle]; | |
| 423 inherited_handles[1] = stdout_handles[kWriteHandle]; | |
| 424 inherited_handles[2] = stderr_handles[kWriteHandle]; | |
| 425 if (!UpdateProcThreadAttribute(attribute_list, | |
| 426 0, | |
| 427 PROC_THREAD_ATTRIBUTE_HANDLE_LIST, | |
| 428 inherited_handles, | |
| 429 num_inherited_handles * sizeof(HANDLE), | |
| 430 NULL, | |
| 431 NULL)) { | |
| 432 delete[] inherited_handles; | |
| 433 DeleteProcThreadAttributeList(attribute_list); | |
| 434 free(attribute_list); | |
| 435 int error_code = SetOsErrorMessage(os_error_message, os_error_message_len); | |
| 436 CloseProcessPipes( | |
| 437 stdin_handles, stdout_handles, stderr_handles, exit_handles); | |
| 438 return error_code; | |
| 439 } | |
| 440 startup_info.lpAttributeList = attribute_list; | |
| 399 | 441 |
| 400 PROCESS_INFORMATION process_info; | 442 PROCESS_INFORMATION process_info; |
| 401 ZeroMemory(&process_info, sizeof(process_info)); | 443 ZeroMemory(&process_info, sizeof(process_info)); |
| 402 | 444 |
| 403 // Transform input strings to system format. | 445 // Transform input strings to system format. |
| 404 path = StringUtils::Utf8ToSystemString(path); | 446 path = StringUtils::Utf8ToSystemString(path); |
| 405 for (int i = 0; i < arguments_length; i++) { | 447 for (int i = 0; i < arguments_length; i++) { |
| 406 arguments[i] = StringUtils::Utf8ToSystemString(arguments[i]); | 448 arguments[i] = StringUtils::Utf8ToSystemString(arguments[i]); |
| 407 } | 449 } |
| 408 | 450 |
| 409 // Compute command-line length. | 451 // Compute command-line length. |
| 410 int command_line_length = strlen(path); | 452 int command_line_length = strlen(path); |
| 411 for (int i = 0; i < arguments_length; i++) { | 453 for (int i = 0; i < arguments_length; i++) { |
| 412 command_line_length += strlen(arguments[i]); | 454 command_line_length += strlen(arguments[i]); |
| 413 } | 455 } |
| 414 // Account for null termination and one space per argument. | 456 // Account for null termination and one space per argument. |
| 415 command_line_length += arguments_length + 1; | 457 command_line_length += arguments_length + 1; |
| 416 static const int kMaxCommandLineLength = 32768; | 458 static const int kMaxCommandLineLength = 32768; |
| 417 if (command_line_length > kMaxCommandLineLength) { | 459 if (command_line_length > kMaxCommandLineLength) { |
| 460 delete[] inherited_handles; | |
| 461 DeleteProcThreadAttributeList(attribute_list); | |
| 462 free(attribute_list); | |
| 418 int error_code = SetOsErrorMessage(os_error_message, os_error_message_len); | 463 int error_code = SetOsErrorMessage(os_error_message, os_error_message_len); |
|
ahe
2012/12/10 10:30:51
Shouldn't you call SetOsErrorMessage first?
Mads Ager (google)
2012/12/10 10:45:29
Yes, thanks!
| |
| 419 CloseProcessPipes( | 464 CloseProcessPipes( |
| 420 stdin_handles, stdout_handles, stderr_handles, exit_handles); | 465 stdin_handles, stdout_handles, stderr_handles, exit_handles); |
| 421 free(const_cast<char*>(path)); | 466 free(const_cast<char*>(path)); |
| 422 for (int i = 0; i < arguments_length; i++) free(arguments[i]); | 467 for (int i = 0; i < arguments_length; i++) free(arguments[i]); |
| 423 return error_code; | 468 return error_code; |
| 424 } | 469 } |
| 425 | 470 |
| 426 // Put together command-line string. | 471 // Put together command-line string. |
| 427 char* command_line = new char[command_line_length]; | 472 char* command_line = new char[command_line_length]; |
| 428 int len = 0; | 473 int len = 0; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 475 if (working_directory != NULL) { | 520 if (working_directory != NULL) { |
| 476 working_directory = StringUtils::Utf8ToSystemString(working_directory); | 521 working_directory = StringUtils::Utf8ToSystemString(working_directory); |
| 477 } | 522 } |
| 478 | 523 |
| 479 // Create process. | 524 // Create process. |
| 480 BOOL result = CreateProcess(NULL, // ApplicationName | 525 BOOL result = CreateProcess(NULL, // ApplicationName |
| 481 command_line, | 526 command_line, |
| 482 NULL, // ProcessAttributes | 527 NULL, // ProcessAttributes |
| 483 NULL, // ThreadAttributes | 528 NULL, // ThreadAttributes |
| 484 TRUE, // InheritHandles | 529 TRUE, // InheritHandles |
| 485 0, // CreationFlags | 530 EXTENDED_STARTUPINFO_PRESENT, |
| 486 environment_block, | 531 environment_block, |
| 487 working_directory, | 532 working_directory, |
| 488 &startup_info, | 533 reinterpret_cast<STARTUPINFO*>(&startup_info), |
| 489 &process_info); | 534 &process_info); |
| 490 | 535 |
| 491 // Deallocate command-line and environment block strings. | 536 // Deallocate command-line and environment block strings. |
| 492 delete[] command_line; | 537 delete[] command_line; |
| 493 delete[] environment_block; | 538 delete[] environment_block; |
| 494 if (working_directory != NULL) { | 539 if (working_directory != NULL) { |
| 495 free(const_cast<char*>(working_directory)); | 540 free(const_cast<char*>(working_directory)); |
| 496 } | 541 } |
| 497 | 542 |
| 543 delete[] inherited_handles; | |
| 544 DeleteProcThreadAttributeList(attribute_list); | |
| 545 free(attribute_list); | |
| 546 | |
| 498 if (result == 0) { | 547 if (result == 0) { |
| 499 int error_code = SetOsErrorMessage(os_error_message, os_error_message_len); | 548 int error_code = SetOsErrorMessage(os_error_message, os_error_message_len); |
| 500 CloseProcessPipes( | 549 CloseProcessPipes( |
| 501 stdin_handles, stdout_handles, stderr_handles, exit_handles); | 550 stdin_handles, stdout_handles, stderr_handles, exit_handles); |
| 502 return error_code; | 551 return error_code; |
| 503 } | 552 } |
| 504 | 553 |
| 505 ProcessInfoList::AddProcess(process_info.dwProcessId, | 554 ProcessInfoList::AddProcess(process_info.dwProcessId, |
| 506 process_info.hProcess, | 555 process_info.hProcess, |
| 507 exit_handles[kWriteHandle]); | 556 exit_handles[kWriteHandle]); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 544 | 593 |
| 545 | 594 |
| 546 void Process::TerminateExitCodeHandler() { | 595 void Process::TerminateExitCodeHandler() { |
| 547 // Nothing needs to be done on Windows. | 596 // Nothing needs to be done on Windows. |
| 548 } | 597 } |
| 549 | 598 |
| 550 | 599 |
| 551 intptr_t Process::CurrentProcessId() { | 600 intptr_t Process::CurrentProcessId() { |
| 552 return static_cast<intptr_t>(GetCurrentProcessId()); | 601 return static_cast<intptr_t>(GetCurrentProcessId()); |
| 553 } | 602 } |
| OLD | NEW |