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