xref: /aosp_15_r20/external/grpc-grpc/src/core/lib/gpr/subprocess_windows.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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/gpr/subprocess.h"
35 #include "src/core/lib/gprpp/crash.h"
36 #include "src/core/lib/gprpp/tchar.h"
37 
38 struct gpr_subprocess {
39   PROCESS_INFORMATION pi;
40   int joined;
41   int interrupted;
42 };
43 
gpr_subprocess_binary_extension()44 const char* gpr_subprocess_binary_extension() { return ".exe"; }
45 
gpr_subprocess_create(int argc,const char ** argv)46 gpr_subprocess* gpr_subprocess_create(int argc, const char** argv) {
47   gpr_subprocess* r;
48 
49   STARTUPINFO si;
50   PROCESS_INFORMATION pi;
51 
52   grpc_core::TcharString args = grpc_core::CharToTchar(
53       absl::StrJoin(absl::Span<const char*>(argv, argc), " "));
54 
55   memset(&si, 0, sizeof(si));
56   si.cb = sizeof(si);
57   memset(&pi, 0, sizeof(pi));
58 
59   if (!CreateProcess(NULL, const_cast<LPTSTR>(args.c_str()), NULL, NULL, FALSE,
60                      CREATE_NEW_PROCESS_GROUP, NULL, NULL, &si, &pi)) {
61     return NULL;
62   }
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 
gpr_subprocess_get_process_id(gpr_subprocess * p)126 int gpr_subprocess_get_process_id(gpr_subprocess* p) {
127   return p->pi.dwProcessId;
128 }
129 
130 #endif  // GPR_WINDOWS_SUBPROCESS
131