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