| 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 |
|
| 19 |
#include <grpc/support/port_platform.h> |
| 20 |
|
| 21 |
#ifdef GPR_WINDOWS_SUBPROCESS |
| 22 |
|
| 23 |
#include <string.h> |
| 24 |
#include <tchar.h> |
| 25 |
#include <windows.h> |
| 26 |
|
| 27 |
#include "absl/strings/str_join.h" |
| 28 |
#include "absl/types/span.h" |
| 29 |
|
| 30 |
#include <grpc/support/alloc.h> |
| 31 |
#include <grpc/support/log.h> |
| 32 |
|
| 33 |
#include "src/core/lib/gpr/string.h" |
| 34 |
#include "src/core/lib/gprpp/tchar.h" |
| 35 |
#include "test/core/util/subprocess.h" |
| 36 |
|
| 37 |
struct gpr_subprocess { |
| 38 |
PROCESS_INFORMATION pi; |
| 39 |
int joined; |
| 40 |
int interrupted; |
| 41 |
}; |
| 42 |
|
| 43 |
const char* gpr_subprocess_binary_extension() { return ".exe"; } |
| 44 |
|
| 45 |
gpr_subprocess* gpr_subprocess_create(int argc, const char** argv) { |
| 46 |
gpr_subprocess* r; |
| 47 |
|
| 48 |
STARTUPINFO si; |
| 49 |
PROCESS_INFORMATION pi; |
| 50 |
|
| 51 |
grpc_core::TcharString args = grpc_core::CharToTchar( |
| 52 |
absl::StrJoin(absl::Span<const char*>(argv, argc), " ")); |
| 53 |
|
| 54 |
memset(&si, 0, sizeof(si)); |
| 55 |
si.cb = sizeof(si); |
| 56 |
memset(&pi, 0, sizeof(pi)); |
| 57 |
|
| 58 |
if (!CreateProcess(NULL, const_cast<LPTSTR>(args.c_str()), NULL, NULL, FALSE, |
| 59 |
CREATE_NEW_PROCESS_GROUP, NULL, NULL, &si, &pi)) { |
| 60 |
return NULL; |
| 61 |
} |
| 62 |
|
| 63 |
r = (gpr_subprocess*)gpr_malloc(sizeof(gpr_subprocess)); |
| 64 |
memset(r, 0, sizeof(*r)); |
| 65 |
r->pi = pi; |
| 66 |
return r; |
| 67 |
} |
| 68 |
|
| 69 |
void gpr_subprocess_destroy(gpr_subprocess* p) { |
| 70 |
if (p) { |
| 71 |
if (!p->joined) { |
| 72 |
gpr_subprocess_interrupt(p); |
| 73 |
gpr_subprocess_join(p); |
| 74 |
} |
| 75 |
if (p->pi.hProcess) { |
| 76 |
CloseHandle(p->pi.hProcess); |
| 77 |
} |
| 78 |
if (p->pi.hThread) { |
| 79 |
CloseHandle(p->pi.hThread); |
| 80 |
} |
| 81 |
gpr_free(p); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
int gpr_subprocess_join(gpr_subprocess* p) { |
| 86 |
DWORD dwExitCode; |
| 87 |
if (GetExitCodeProcess(p->pi.hProcess, &dwExitCode)) { |
| 88 |
if (dwExitCode == STILL_ACTIVE) { |
| 89 |
if (WaitForSingleObject(p->pi.hProcess, INFINITE) == WAIT_OBJECT_0) { |
| 90 |
p->joined = 1; |
| 91 |
goto getExitCode; |
| 92 |
} |
| 93 |
return -1; // failed to join |
| 94 |
} else { |
| 95 |
goto getExitCode; |
| 96 |
} |
| 97 |
} else { |
| 98 |
return -1; // failed to get exit code |
| 99 |
} |
| 100 |
|
| 101 |
getExitCode: |
| 102 |
if (p->interrupted) { |
| 103 |
return 0; |
| 104 |
} |
| 105 |
if (GetExitCodeProcess(p->pi.hProcess, &dwExitCode)) { |
| 106 |
return (int)dwExitCode; |
| 107 |
} else { |
| 108 |
return -1; // failed to get exit code |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
void gpr_subprocess_interrupt(gpr_subprocess* p) { |
| 113 |
DWORD dwExitCode; |
| 114 |
if (GetExitCodeProcess(p->pi.hProcess, &dwExitCode)) { |
| 115 |
if (dwExitCode == STILL_ACTIVE) { |
| 116 |
gpr_log(GPR_INFO, "sending ctrl-break"); |
| 117 |
GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, p->pi.dwProcessId); |
| 118 |
p->joined = 1; |
| 119 |
p->interrupted = 1; |
| 120 |
} |
| 121 |
} |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
int gpr_subprocess_get_process_id(gpr_subprocess* p) { |
| 126 |
return p->pi.dwProcessId; |
| 127 |
} |
| 128 |
|
| 129 |
#endif /* GPR_WINDOWS_SUBPROCESS */ |