subprocess_windows.cc
Go to the documentation of this file.
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 
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 
33 
34 struct gpr_subprocess {
35  PROCESS_INFORMATION pi;
36  int joined;
37  int interrupted;
38 };
39 
40 const char* gpr_subprocess_binary_extension() { return ".exe"; }
41 
42 gpr_subprocess* gpr_subprocess_create(int argc, const char** argv) {
44 
45  STARTUPINFO si;
46  PROCESS_INFORMATION pi;
47 
48  char* args = gpr_strjoin_sep(argv, (size_t)argc, " ", NULL);
49  TCHAR* args_tchar;
50 
51  args_tchar = gpr_char_to_tchar(args);
52  gpr_free(args);
53 
54  memset(&si, 0, sizeof(si));
55  si.cb = sizeof(si);
56  memset(&pi, 0, sizeof(pi));
57 
58  if (!CreateProcess(NULL, args_tchar, NULL, NULL, FALSE,
59  CREATE_NEW_PROCESS_GROUP, NULL, NULL, &si, &pi)) {
60  gpr_free(args_tchar);
61  return NULL;
62  }
63  gpr_free(args_tchar);
64 
66  memset(r, 0, sizeof(*r));
67  r->pi = pi;
68  return r;
69 }
70 
72  if (p) {
73  if (!p->joined) {
76  }
77  if (p->pi.hProcess) {
78  CloseHandle(p->pi.hProcess);
79  }
80  if (p->pi.hThread) {
81  CloseHandle(p->pi.hThread);
82  }
83  gpr_free(p);
84  }
85 }
86 
88  DWORD dwExitCode;
89  if (GetExitCodeProcess(p->pi.hProcess, &dwExitCode)) {
90  if (dwExitCode == STILL_ACTIVE) {
91  if (WaitForSingleObject(p->pi.hProcess, INFINITE) == WAIT_OBJECT_0) {
92  p->joined = 1;
93  goto getExitCode;
94  }
95  return -1; // failed to join
96  } else {
97  goto getExitCode;
98  }
99  } else {
100  return -1; // failed to get exit code
101  }
102 
103 getExitCode:
104  if (p->interrupted) {
105  return 0;
106  }
107  if (GetExitCodeProcess(p->pi.hProcess, &dwExitCode)) {
108  return (int)dwExitCode;
109  } else {
110  return -1; // failed to get exit code
111  }
112 }
113 
115  DWORD dwExitCode;
116  if (GetExitCodeProcess(p->pi.hProcess, &dwExitCode)) {
117  if (dwExitCode == STILL_ACTIVE) {
118  gpr_log(GPR_INFO, "sending ctrl-break");
119  GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, p->pi.dwProcessId);
120  p->joined = 1;
121  p->interrupted = 1;
122  }
123  }
124  return;
125 }
126 
127 #endif /* GPR_WINDOWS_SUBPROCESS */
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
log.h
memset
return memset(p, 0, total)
string.h
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
gpr_malloc
GPRAPI void * gpr_malloc(size_t size)
Definition: alloc.cc:29
gpr_subprocess_binary_extension
const char * gpr_subprocess_binary_extension()
xds_manager.p
p
Definition: xds_manager.py:60
gpr_subprocess_interrupt
void gpr_subprocess_interrupt(gpr_subprocess *p)
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
gpr_strjoin_sep
char * gpr_strjoin_sep(const char **strs, size_t nstrs, const char *sep, size_t *final_length)
Definition: string.cc:239
string_windows.h
gpr_subprocess
struct gpr_subprocess gpr_subprocess
Definition: test/core/util/subprocess.h:24
subprocess.h
FALSE
const BOOL FALSE
Definition: undname.c:47
gpr_subprocess_create
gpr_subprocess * gpr_subprocess_create(int argc, const char **argv)
alloc.h
fix_build_deps.r
r
Definition: fix_build_deps.py:491
gpr_subprocess_join
int gpr_subprocess_join(gpr_subprocess *p)
gpr_subprocess_destroy
void gpr_subprocess_destroy(gpr_subprocess *p)
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:27