| 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 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 312 Log::PrintErr("FormatMessage failed %d\n", GetLastError()); | 312 Log::PrintErr("FormatMessage failed %d\n", GetLastError()); |
| 313 } | 313 } |
| 314 _snwprintf(message, kMaxMessageLength, L"OS Error %d", error_code); | 314 _snwprintf(message, kMaxMessageLength, L"OS Error %d", error_code); |
| 315 } | 315 } |
| 316 message[kMaxMessageLength - 1] = '\0'; | 316 message[kMaxMessageLength - 1] = '\0'; |
| 317 *os_error_message = StringUtils::WideToUtf8(message); | 317 *os_error_message = StringUtils::WideToUtf8(message); |
| 318 return error_code; | 318 return error_code; |
| 319 } | 319 } |
| 320 | 320 |
| 321 | 321 |
| 322 typedef BOOL (WINAPI *InitProcThreadAttrListFn)( |
| 323 LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD, PSIZE_T); |
| 324 |
| 325 typedef BOOL (WINAPI *UpdateProcThreadAttrFn)( |
| 326 LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD_PTR, |
| 327 PVOID, SIZE_T, PVOID, PSIZE_T); |
| 328 |
| 329 typedef VOID (WINAPI *DeleteProcThreadAttrListFn)( |
| 330 LPPROC_THREAD_ATTRIBUTE_LIST); |
| 331 |
| 332 |
| 333 static InitProcThreadAttrListFn init_proc_thread_attr_list = NULL; |
| 334 static UpdateProcThreadAttrFn update_proc_thread_attr = NULL; |
| 335 static DeleteProcThreadAttrListFn delete_proc_thread_attr_list = NULL; |
| 336 |
| 337 |
| 338 static bool EnsureInitialized() { |
| 339 static bool load_attempted = false; |
| 340 static dart::Mutex mutex; |
| 341 HMODULE kernel32_module = GetModuleHandle(L"kernel32.dll"); |
| 342 if (!load_attempted) { |
| 343 MutexLocker locker(&mutex); |
| 344 if (load_attempted) return delete_proc_thread_attr_list != NULL; |
| 345 init_proc_thread_attr_list = reinterpret_cast<InitProcThreadAttrListFn>( |
| 346 GetProcAddress(kernel32_module, "InitializeProcThreadAttributeList")); |
| 347 update_proc_thread_attr = |
| 348 reinterpret_cast<UpdateProcThreadAttrFn>( |
| 349 GetProcAddress(kernel32_module, "UpdateProcThreadAttribute")); |
| 350 delete_proc_thread_attr_list = reinterpret_cast<DeleteProcThreadAttrListFn>( |
| 351 reinterpret_cast<DeleteProcThreadAttrListFn>( |
| 352 GetProcAddress(kernel32_module, "DeleteProcThreadAttributeList"))); |
| 353 load_attempted = true; |
| 354 return delete_proc_thread_attr_list != NULL; |
| 355 } |
| 356 return delete_proc_thread_attr_list != NULL; |
| 357 } |
| 358 |
| 359 |
| 322 int Process::Start(const char* path, | 360 int Process::Start(const char* path, |
| 323 char* arguments[], | 361 char* arguments[], |
| 324 intptr_t arguments_length, | 362 intptr_t arguments_length, |
| 325 const char* working_directory, | 363 const char* working_directory, |
| 326 char* environment[], | 364 char* environment[], |
| 327 intptr_t environment_length, | 365 intptr_t environment_length, |
| 328 intptr_t* in, | 366 intptr_t* in, |
| 329 intptr_t* out, | 367 intptr_t* out, |
| 330 intptr_t* err, | 368 intptr_t* err, |
| 331 intptr_t* id, | 369 intptr_t* id, |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 393 | 431 |
| 394 // Setup info structures. | 432 // Setup info structures. |
| 395 STARTUPINFOEXW startup_info; | 433 STARTUPINFOEXW startup_info; |
| 396 ZeroMemory(&startup_info, sizeof(startup_info)); | 434 ZeroMemory(&startup_info, sizeof(startup_info)); |
| 397 startup_info.StartupInfo.cb = sizeof(startup_info); | 435 startup_info.StartupInfo.cb = sizeof(startup_info); |
| 398 startup_info.StartupInfo.hStdInput = stdin_handles[kReadHandle]; | 436 startup_info.StartupInfo.hStdInput = stdin_handles[kReadHandle]; |
| 399 startup_info.StartupInfo.hStdOutput = stdout_handles[kWriteHandle]; | 437 startup_info.StartupInfo.hStdOutput = stdout_handles[kWriteHandle]; |
| 400 startup_info.StartupInfo.hStdError = stderr_handles[kWriteHandle]; | 438 startup_info.StartupInfo.hStdError = stderr_handles[kWriteHandle]; |
| 401 startup_info.StartupInfo.dwFlags = STARTF_USESTDHANDLES; | 439 startup_info.StartupInfo.dwFlags = STARTF_USESTDHANDLES; |
| 402 | 440 |
| 403 // Setup the handles to inherit. We only want to inherit the three handles | 441 LPPROC_THREAD_ATTRIBUTE_LIST attribute_list = NULL; |
| 404 // for stdin, stdout and stderr. | 442 |
| 405 SIZE_T size = 0; | 443 bool supports_proc_thread_attr_lists = EnsureInitialized(); |
| 406 // The call to determine the size of an attribute list always fails with | 444 if (supports_proc_thread_attr_lists) { |
| 407 // ERROR_INSUFFICIENT_BUFFER and that error should be ignored. | 445 // Setup the handles to inherit. We only want to inherit the three handles |
| 408 if (!InitializeProcThreadAttributeList(NULL, 1, 0, &size) && | 446 // for stdin, stdout and stderr. |
| 409 GetLastError() != ERROR_INSUFFICIENT_BUFFER) { | 447 SIZE_T size = 0; |
| 410 int error_code = SetOsErrorMessage(os_error_message); | 448 // The call to determine the size of an attribute list always fails with |
| 411 CloseProcessPipes( | 449 // ERROR_INSUFFICIENT_BUFFER and that error should be ignored. |
| 412 stdin_handles, stdout_handles, stderr_handles, exit_handles); | 450 if (!init_proc_thread_attr_list(NULL, 1, 0, &size) && |
| 413 return error_code; | 451 GetLastError() != ERROR_INSUFFICIENT_BUFFER) { |
| 414 } | 452 int error_code = SetOsErrorMessage(os_error_message); |
| 415 LPPROC_THREAD_ATTRIBUTE_LIST attribute_list = | 453 CloseProcessPipes( |
| 416 reinterpret_cast<LPPROC_THREAD_ATTRIBUTE_LIST>(malloc(size)); | 454 stdin_handles, stdout_handles, stderr_handles, exit_handles); |
| 417 ZeroMemory(attribute_list, size); | 455 return error_code; |
| 418 if (!InitializeProcThreadAttributeList(attribute_list, 1, 0, &size)) { | 456 } |
| 419 int error_code = SetOsErrorMessage(os_error_message); | 457 attribute_list = |
| 420 CloseProcessPipes( | 458 reinterpret_cast<LPPROC_THREAD_ATTRIBUTE_LIST>(malloc(size)); |
| 421 stdin_handles, stdout_handles, stderr_handles, exit_handles); | 459 ZeroMemory(attribute_list, size); |
| 422 free(attribute_list); | 460 if (!init_proc_thread_attr_list(attribute_list, 1, 0, &size)) { |
| 423 return error_code; | 461 int error_code = SetOsErrorMessage(os_error_message); |
| 424 } | 462 CloseProcessPipes( |
| 425 static const int kNumInheritedHandles = 3; | 463 stdin_handles, stdout_handles, stderr_handles, exit_handles); |
| 426 HANDLE inherited_handles[kNumInheritedHandles] = | 464 free(attribute_list); |
| 427 { stdin_handles[kReadHandle], | 465 return error_code; |
| 428 stdout_handles[kWriteHandle], | 466 } |
| 429 stderr_handles[kWriteHandle] }; | 467 static const int kNumInheritedHandles = 3; |
| 430 if (!UpdateProcThreadAttribute(attribute_list, | 468 HANDLE inherited_handles[kNumInheritedHandles] = |
| 469 { stdin_handles[kReadHandle], |
| 470 stdout_handles[kWriteHandle], |
| 471 stderr_handles[kWriteHandle] }; |
| 472 if (!update_proc_thread_attr(attribute_list, |
| 431 0, | 473 0, |
| 432 PROC_THREAD_ATTRIBUTE_HANDLE_LIST, | 474 PROC_THREAD_ATTRIBUTE_HANDLE_LIST, |
| 433 inherited_handles, | 475 inherited_handles, |
| 434 kNumInheritedHandles * sizeof(HANDLE), | 476 kNumInheritedHandles * sizeof(HANDLE), |
| 435 NULL, | 477 NULL, |
| 436 NULL)) { | 478 NULL)) { |
| 437 DeleteProcThreadAttributeList(attribute_list); | 479 delete_proc_thread_attr_list(attribute_list); |
| 438 int error_code = SetOsErrorMessage(os_error_message); | 480 int error_code = SetOsErrorMessage(os_error_message); |
| 439 CloseProcessPipes( | 481 CloseProcessPipes( |
| 440 stdin_handles, stdout_handles, stderr_handles, exit_handles); | 482 stdin_handles, stdout_handles, stderr_handles, exit_handles); |
| 441 free(attribute_list); | 483 free(attribute_list); |
| 442 return error_code; | 484 return error_code; |
| 485 } |
| 486 startup_info.lpAttributeList = attribute_list; |
| 443 } | 487 } |
| 444 startup_info.lpAttributeList = attribute_list; | |
| 445 | 488 |
| 446 PROCESS_INFORMATION process_info; | 489 PROCESS_INFORMATION process_info; |
| 447 ZeroMemory(&process_info, sizeof(process_info)); | 490 ZeroMemory(&process_info, sizeof(process_info)); |
| 448 | 491 |
| 449 // Transform input strings to system format. | 492 // Transform input strings to system format. |
| 450 const wchar_t* system_path = StringUtils::Utf8ToWide(path); | 493 const wchar_t* system_path = StringUtils::Utf8ToWide(path); |
| 451 wchar_t** system_arguments = new wchar_t*[arguments_length]; | 494 wchar_t** system_arguments = new wchar_t*[arguments_length]; |
| 452 for (int i = 0; i < arguments_length; i++) { | 495 for (int i = 0; i < arguments_length; i++) { |
| 453 system_arguments[i] = StringUtils::Utf8ToWide(arguments[i]); | 496 system_arguments[i] = StringUtils::Utf8ToWide(arguments[i]); |
| 454 } | 497 } |
| 455 | 498 |
| 456 // Compute command-line length. | 499 // Compute command-line length. |
| 457 int command_line_length = wcslen(system_path); | 500 int command_line_length = wcslen(system_path); |
| 458 for (int i = 0; i < arguments_length; i++) { | 501 for (int i = 0; i < arguments_length; i++) { |
| 459 command_line_length += wcslen(system_arguments[i]); | 502 command_line_length += wcslen(system_arguments[i]); |
| 460 } | 503 } |
| 461 // Account for null termination and one space per argument. | 504 // Account for null termination and one space per argument. |
| 462 command_line_length += arguments_length + 1; | 505 command_line_length += arguments_length + 1; |
| 463 static const int kMaxCommandLineLength = 32768; | 506 static const int kMaxCommandLineLength = 32768; |
| 464 if (command_line_length > kMaxCommandLineLength) { | 507 if (command_line_length > kMaxCommandLineLength) { |
| 465 int error_code = SetOsErrorMessage(os_error_message); | 508 int error_code = SetOsErrorMessage(os_error_message); |
| 466 CloseProcessPipes( | 509 CloseProcessPipes( |
| 467 stdin_handles, stdout_handles, stderr_handles, exit_handles); | 510 stdin_handles, stdout_handles, stderr_handles, exit_handles); |
| 468 free(const_cast<wchar_t*>(system_path)); | 511 free(const_cast<wchar_t*>(system_path)); |
| 469 for (int i = 0; i < arguments_length; i++) free(system_arguments[i]); | 512 for (int i = 0; i < arguments_length; i++) free(system_arguments[i]); |
| 470 delete[] system_arguments; | 513 delete[] system_arguments; |
| 471 DeleteProcThreadAttributeList(attribute_list); | 514 if (supports_proc_thread_attr_lists) { |
| 472 free(attribute_list); | 515 delete_proc_thread_attr_list(attribute_list); |
| 516 free(attribute_list); |
| 517 } |
| 473 return error_code; | 518 return error_code; |
| 474 } | 519 } |
| 475 | 520 |
| 476 // Put together command-line string. | 521 // Put together command-line string. |
| 477 wchar_t* command_line = new wchar_t[command_line_length]; | 522 wchar_t* command_line = new wchar_t[command_line_length]; |
| 478 int len = 0; | 523 int len = 0; |
| 479 int remaining = command_line_length; | 524 int remaining = command_line_length; |
| 480 int written = _snwprintf(command_line + len, remaining, L"%s", system_path); | 525 int written = _snwprintf(command_line + len, remaining, L"%s", system_path); |
| 481 len += written; | 526 len += written; |
| 482 remaining -= written; | 527 remaining -= written; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 547 reinterpret_cast<STARTUPINFOW*>(&startup_info), | 592 reinterpret_cast<STARTUPINFOW*>(&startup_info), |
| 548 &process_info); | 593 &process_info); |
| 549 | 594 |
| 550 // Deallocate command-line and environment block strings. | 595 // Deallocate command-line and environment block strings. |
| 551 delete[] command_line; | 596 delete[] command_line; |
| 552 delete[] environment_block; | 597 delete[] environment_block; |
| 553 if (system_working_directory != NULL) { | 598 if (system_working_directory != NULL) { |
| 554 free(const_cast<wchar_t*>(system_working_directory)); | 599 free(const_cast<wchar_t*>(system_working_directory)); |
| 555 } | 600 } |
| 556 | 601 |
| 557 DeleteProcThreadAttributeList(attribute_list); | 602 if (supports_proc_thread_attr_lists) { |
| 558 free(attribute_list); | 603 delete_proc_thread_attr_list(attribute_list); |
| 604 free(attribute_list); |
| 605 } |
| 559 | 606 |
| 560 if (result == 0) { | 607 if (result == 0) { |
| 561 int error_code = SetOsErrorMessage(os_error_message); | 608 int error_code = SetOsErrorMessage(os_error_message); |
| 562 CloseProcessPipes( | 609 CloseProcessPipes( |
| 563 stdin_handles, stdout_handles, stderr_handles, exit_handles); | 610 stdin_handles, stdout_handles, stderr_handles, exit_handles); |
| 564 return error_code; | 611 return error_code; |
| 565 } | 612 } |
| 566 | 613 |
| 567 ProcessInfoList::AddProcess(process_info.dwProcessId, | 614 ProcessInfoList::AddProcess(process_info.dwProcessId, |
| 568 process_info.hProcess, | 615 process_info.hProcess, |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 606 | 653 |
| 607 | 654 |
| 608 void Process::TerminateExitCodeHandler() { | 655 void Process::TerminateExitCodeHandler() { |
| 609 // Nothing needs to be done on Windows. | 656 // Nothing needs to be done on Windows. |
| 610 } | 657 } |
| 611 | 658 |
| 612 | 659 |
| 613 intptr_t Process::CurrentProcessId() { | 660 intptr_t Process::CurrentProcessId() { |
| 614 return static_cast<intptr_t>(GetCurrentProcessId()); | 661 return static_cast<intptr_t>(GetCurrentProcessId()); |
| 615 } | 662 } |
| OLD | NEW |