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 "platform/globals.h" | 5 #include "platform/globals.h" |
6 #if defined(TARGET_OS_WINDOWS) | 6 #if defined(TARGET_OS_WINDOWS) |
7 | 7 |
8 #include <process.h> // NOLINT | 8 #include <process.h> // NOLINT |
9 | 9 |
10 #include "bin/builtin.h" | 10 #include "bin/builtin.h" |
11 #include "bin/process.h" | 11 #include "bin/process.h" |
12 #include "bin/eventhandler.h" | 12 #include "bin/eventhandler.h" |
13 #include "bin/log.h" | 13 #include "bin/log.h" |
14 #include "bin/socket.h" | |
15 #include "bin/thread.h" | 14 #include "bin/thread.h" |
16 #include "bin/utils.h" | 15 #include "bin/utils.h" |
17 #include "bin/utils_win.h" | 16 #include "bin/utils_win.h" |
18 | 17 |
19 | 18 |
20 namespace dart { | 19 namespace dart { |
21 namespace bin { | 20 namespace bin { |
22 | 21 |
23 static const int kReadHandle = 0; | 22 static const int kReadHandle = 0; |
24 static const int kWriteHandle = 1; | 23 static const int kWriteHandle = 1; |
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 GetProcAddress(kernel32_module, "UpdateProcThreadAttribute")); | 342 GetProcAddress(kernel32_module, "UpdateProcThreadAttribute")); |
344 delete_proc_thread_attr_list = reinterpret_cast<DeleteProcThreadAttrListFn>( | 343 delete_proc_thread_attr_list = reinterpret_cast<DeleteProcThreadAttrListFn>( |
345 reinterpret_cast<DeleteProcThreadAttrListFn>( | 344 reinterpret_cast<DeleteProcThreadAttrListFn>( |
346 GetProcAddress(kernel32_module, "DeleteProcThreadAttributeList"))); | 345 GetProcAddress(kernel32_module, "DeleteProcThreadAttributeList"))); |
347 load_attempted = true; | 346 load_attempted = true; |
348 return delete_proc_thread_attr_list != NULL; | 347 return delete_proc_thread_attr_list != NULL; |
349 } | 348 } |
350 return delete_proc_thread_attr_list != NULL; | 349 return delete_proc_thread_attr_list != NULL; |
351 } | 350 } |
352 | 351 |
353 int GenerateNames(int count, wchar_t* pipe_names[], int max_length) { | 352 |
| 353 int Process::Start(const char* path, |
| 354 char* arguments[], |
| 355 intptr_t arguments_length, |
| 356 const char* working_directory, |
| 357 char* environment[], |
| 358 intptr_t environment_length, |
| 359 intptr_t* in, |
| 360 intptr_t* out, |
| 361 intptr_t* err, |
| 362 intptr_t* id, |
| 363 intptr_t* exit_handler, |
| 364 char** os_error_message) { |
| 365 HANDLE stdin_handles[2] = { INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE }; |
| 366 HANDLE stdout_handles[2] = { INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE }; |
| 367 HANDLE stderr_handles[2] = { INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE }; |
| 368 HANDLE exit_handles[2] = { INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE }; |
| 369 |
| 370 // Generate unique pipe names for the four named pipes needed. |
| 371 static const int kMaxPipeNameSize = 80; |
| 372 wchar_t pipe_names[4][kMaxPipeNameSize]; |
354 UUID uuid; | 373 UUID uuid; |
355 RPC_STATUS status = UuidCreateSequential(&uuid); | 374 RPC_STATUS status = UuidCreateSequential(&uuid); |
356 if (status != RPC_S_OK && status != RPC_S_UUID_LOCAL_ONLY) { | 375 if (status != RPC_S_OK && status != RPC_S_UUID_LOCAL_ONLY) { |
357 SetOsErrorMessage(os_error_message); | 376 SetOsErrorMessage(os_error_message); |
358 Log::PrintErr("UuidCreateSequential failed %d\n", status); | 377 Log::PrintErr("UuidCreateSequential failed %d\n", status); |
359 return status; | 378 return status; |
360 } | 379 } |
361 RPC_WSTR uuid_string; | 380 RPC_WSTR uuid_string; |
362 status = UuidToStringW(&uuid, &uuid_string); | 381 status = UuidToStringW(&uuid, &uuid_string); |
363 if (status != RPC_S_OK) { | 382 if (status != RPC_S_OK) { |
364 SetOsErrorMessage(os_error_message); | 383 SetOsErrorMessage(os_error_message); |
365 Log::PrintErr("UuidToString failed %d\n", status); | 384 Log::PrintErr("UuidToString failed %d\n", status); |
366 return status; | 385 return status; |
367 } | 386 } |
368 for (int i = 0; i < count; i++) { | 387 for (int i = 0; i < 4; i++) { |
369 static const wchar_t* prefix = L"\\\\.\\Pipe\\dart"; | 388 static const wchar_t* prefix = L"\\\\.\\Pipe\\dart"; |
370 _snwprintf(pipe_names[i], | 389 _snwprintf(pipe_names[i], |
371 max_length, | 390 kMaxPipeNameSize, |
372 L"%s_%s_%d", prefix, uuid_string, i + 1); | 391 L"%s_%s_%d", prefix, uuid_string, i + 1); |
373 } | 392 } |
374 status = RpcStringFreeW(&uuid_string); | 393 status = RpcStringFreeW(&uuid_string); |
375 if (status != RPC_S_OK) { | 394 if (status != RPC_S_OK) { |
376 SetOsErrorMessage(os_error_message); | 395 SetOsErrorMessage(os_error_message); |
377 Log::PrintErr("RpcStringFree failed %d\n", status); | 396 Log::PrintErr("RpcStringFree failed %d\n", status); |
378 return status; | 397 return status; |
379 } | 398 } |
380 return 0; | |
381 } | |
382 | |
383 | |
384 int Process::Start(const char* path, | |
385 char* arguments[], | |
386 intptr_t arguments_length, | |
387 const char* working_directory, | |
388 char* environment[], | |
389 intptr_t environment_length, | |
390 intptr_t* in, | |
391 intptr_t* out, | |
392 intptr_t* err, | |
393 intptr_t* id, | |
394 intptr_t* exit_handler, | |
395 char** os_error_message) { | |
396 HANDLE stdin_handles[2] = { INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE }; | |
397 HANDLE stdout_handles[2] = { INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE }; | |
398 HANDLE stderr_handles[2] = { INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE }; | |
399 HANDLE exit_handles[2] = { INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE }; | |
400 | |
401 // Generate unique pipe names for the four named pipes needed. | |
402 static const int kMaxPipeNameSize = 80; | |
403 wchar_t pipe_names[4][kMaxPipeNameSize]; | |
404 int status = GenerateNames(4, pipe_names, kMaxPipeNameSize); | |
405 if (status != 0) return status; | |
406 | 399 |
407 if (!CreateProcessPipe(stdin_handles, pipe_names[0], kInheritRead)) { | 400 if (!CreateProcessPipe(stdin_handles, pipe_names[0], kInheritRead)) { |
408 int error_code = SetOsErrorMessage(os_error_message); | 401 int error_code = SetOsErrorMessage(os_error_message); |
409 CloseProcessPipes( | 402 CloseProcessPipes( |
410 stdin_handles, stdout_handles, stderr_handles, exit_handles); | 403 stdin_handles, stdout_handles, stderr_handles, exit_handles); |
411 return error_code; | 404 return error_code; |
412 } | 405 } |
413 if (!CreateProcessPipe(stdout_handles, pipe_names[1], kInheritWrite)) { | 406 if (!CreateProcessPipe(stdout_handles, pipe_names[1], kInheritWrite)) { |
414 int error_code = SetOsErrorMessage(os_error_message); | 407 int error_code = SetOsErrorMessage(os_error_message); |
415 CloseProcessPipes( | 408 CloseProcessPipes( |
(...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
849 | 842 |
850 void Process::TerminateExitCodeHandler() { | 843 void Process::TerminateExitCodeHandler() { |
851 // Nothing needs to be done on Windows. | 844 // Nothing needs to be done on Windows. |
852 } | 845 } |
853 | 846 |
854 | 847 |
855 intptr_t Process::CurrentProcessId() { | 848 intptr_t Process::CurrentProcessId() { |
856 return static_cast<intptr_t>(GetCurrentProcessId()); | 849 return static_cast<intptr_t>(GetCurrentProcessId()); |
857 } | 850 } |
858 | 851 |
859 | |
860 static SignalInfo* signal_handlers = NULL; | |
861 static Mutex* signal_mutex = new Mutex(); | |
862 | |
863 | |
864 SignalInfo::~SignalInfo() { | |
865 reinterpret_cast<FileHandle*>(fd_)->Close(); | |
866 } | |
867 | |
868 | |
869 BOOL WINAPI SignalHandler(DWORD signal) { | |
870 MutexLocker lock(signal_mutex); | |
871 const SignalInfo* handler = signal_handlers; | |
872 bool handled = false; | |
873 while (handler != NULL) { | |
874 if (handler->signal() == signal) { | |
875 int value = 0; | |
876 Socket::Write(handler->fd(), &value, 1); | |
877 handled = true; | |
878 } | |
879 handler = handler->next(); | |
880 } | |
881 return handled; | |
882 } | |
883 | |
884 | |
885 intptr_t GetWinSignal(intptr_t signal) { | |
886 switch (signal) { | |
887 case 1: // SIGHUP | |
888 return CTRL_CLOSE_EVENT; | |
889 case 2: // SIGINT | |
890 return CTRL_C_EVENT; | |
891 default: | |
892 return -1; | |
893 } | |
894 } | |
895 | |
896 | |
897 intptr_t Process::SetSignalHandler(intptr_t signal) { | |
898 signal = GetWinSignal(signal); | |
899 if (signal == -1) return -1; | |
900 | |
901 // Generate a unique pipe name for the named pipe. | |
902 static const int kMaxPipeNameSize = 80; | |
903 wchar_t pipe_name[kMaxPipeNameSize]; | |
904 GenerateNames(1, &pipe_name, kMaxPipeNameSize); | |
905 if (status != 0) return status; | |
906 | |
907 HANDLE fds[2]; | |
908 if (!CreateProcessPipe(fds, pipe_name, kInheritNone)) { | |
909 int error_code = GetLastError(); | |
910 CloseProcessPipe(fds); | |
911 SetLastError(error_code); | |
912 return -1; | |
913 } | |
914 MutexLocker lock(signal_mutex); | |
915 FileHandle* write_handle = new FileHandle(fds[kWriteHandle]); | |
916 write_handle->EnsureInitialized(EventHandler::delegate()); | |
917 intptr_t write_fd = reinterpret_cast<intptr_t>(write_handle); | |
918 if (signal_handlers == NULL) { | |
919 if (SetConsoleCtrlHandler(SignalHandler, true) == 0) { | |
920 int error_code = GetLastError(); | |
921 delete write_handle; | |
922 CloseProcessPipe(fds); | |
923 SetLastError(error_code); | |
924 return -1; | |
925 } | |
926 signal_handlers = new SignalInfo(write_fd, signal); | |
927 } else { | |
928 new SignalInfo(write_fd, signal, signal_handlers); | |
929 } | |
930 return reinterpret_cast<intptr_t>(new FileHandle(fds[kReadHandle])); | |
931 } | |
932 | |
933 | |
934 void Process::ClearSignalHandler(intptr_t signal) { | |
935 signal = GetWinSignal(signal); | |
936 if (signal == -1) return; | |
937 MutexLocker lock(signal_mutex); | |
938 SignalInfo* handler = signal_handlers; | |
939 while (handler != NULL) { | |
940 if (handler->port() == Dart_GetMainPortId() && | |
941 handler->signal() == signal) { | |
942 handler->Unlink(); | |
943 break; | |
944 } | |
945 handler = handler->next(); | |
946 } | |
947 if (handler != NULL) { | |
948 if (signal_handlers == handler) { | |
949 signal_handlers = handler->next(); | |
950 } | |
951 if (signal_handlers == NULL) { | |
952 USE(SetConsoleCtrlHandler(SignalHandler, false)); | |
953 } | |
954 } | |
955 delete handler; | |
956 } | |
957 | |
958 } // namespace bin | 852 } // namespace bin |
959 } // namespace dart | 853 } // namespace dart |
960 | 854 |
961 #endif // defined(TARGET_OS_WINDOWS) | 855 #endif // defined(TARGET_OS_WINDOWS) |
OLD | NEW |