subprocess_windows.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. *
  3. * Copyright 2016 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include <grpc/support/port_platform.h>
  19. #ifdef GPR_WINDOWS_SUBPROCESS
  20. #include <string.h>
  21. #include <tchar.h>
  22. #include <windows.h>
  23. #include <grpc/support/alloc.h>
  24. #include <grpc/support/log.h>
  25. #include "src/core/lib/gpr/string.h"
  26. #include "src/core/lib/gpr/string_windows.h"
  27. #include "test/core/util/subprocess.h"
  28. struct gpr_subprocess {
  29. PROCESS_INFORMATION pi;
  30. int joined;
  31. int interrupted;
  32. };
  33. const char* gpr_subprocess_binary_extension() { return ".exe"; }
  34. gpr_subprocess* gpr_subprocess_create(int argc, const char** argv) {
  35. gpr_subprocess* r;
  36. STARTUPINFO si;
  37. PROCESS_INFORMATION pi;
  38. char* args = gpr_strjoin_sep(argv, (size_t)argc, " ", NULL);
  39. TCHAR* args_tchar;
  40. args_tchar = gpr_char_to_tchar(args);
  41. gpr_free(args);
  42. memset(&si, 0, sizeof(si));
  43. si.cb = sizeof(si);
  44. memset(&pi, 0, sizeof(pi));
  45. if (!CreateProcess(NULL, args_tchar, NULL, NULL, FALSE,
  46. CREATE_NEW_PROCESS_GROUP, NULL, NULL, &si, &pi)) {
  47. gpr_free(args_tchar);
  48. return NULL;
  49. }
  50. gpr_free(args_tchar);
  51. r = (gpr_subprocess*)gpr_malloc(sizeof(gpr_subprocess));
  52. memset(r, 0, sizeof(*r));
  53. r->pi = pi;
  54. return r;
  55. }
  56. void gpr_subprocess_destroy(gpr_subprocess* p) {
  57. if (p) {
  58. if (!p->joined) {
  59. gpr_subprocess_interrupt(p);
  60. gpr_subprocess_join(p);
  61. }
  62. if (p->pi.hProcess) {
  63. CloseHandle(p->pi.hProcess);
  64. }
  65. if (p->pi.hThread) {
  66. CloseHandle(p->pi.hThread);
  67. }
  68. gpr_free(p);
  69. }
  70. }
  71. int gpr_subprocess_join(gpr_subprocess* p) {
  72. DWORD dwExitCode;
  73. if (GetExitCodeProcess(p->pi.hProcess, &dwExitCode)) {
  74. if (dwExitCode == STILL_ACTIVE) {
  75. if (WaitForSingleObject(p->pi.hProcess, INFINITE) == WAIT_OBJECT_0) {
  76. p->joined = 1;
  77. goto getExitCode;
  78. }
  79. return -1; // failed to join
  80. } else {
  81. goto getExitCode;
  82. }
  83. } else {
  84. return -1; // failed to get exit code
  85. }
  86. getExitCode:
  87. if (p->interrupted) {
  88. return 0;
  89. }
  90. if (GetExitCodeProcess(p->pi.hProcess, &dwExitCode)) {
  91. return (int)dwExitCode;
  92. } else {
  93. return -1; // failed to get exit code
  94. }
  95. }
  96. void gpr_subprocess_interrupt(gpr_subprocess* p) {
  97. DWORD dwExitCode;
  98. if (GetExitCodeProcess(p->pi.hProcess, &dwExitCode)) {
  99. if (dwExitCode == STILL_ACTIVE) {
  100. gpr_log(GPR_INFO, "sending ctrl-break");
  101. GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, p->pi.dwProcessId);
  102. p->joined = 1;
  103. p->interrupted = 1;
  104. }
  105. }
  106. return;
  107. }
  108. #endif /* GPR_WINDOWS_SUBPROCESS */