mirror of https://github.com/grpc/grpc.git
The C based gRPC (C++, Python, Ruby, Objective-C, PHP, C#)
https://grpc.io/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
127 lines
3.0 KiB
127 lines
3.0 KiB
9 years ago
|
/*
|
||
|
*
|
||
8 years ago
|
* Copyright 2016 gRPC authors.
|
||
9 years ago
|
*
|
||
8 years ago
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
* you may not use this file except in compliance with the License.
|
||
|
* You may obtain a copy of the License at
|
||
9 years ago
|
*
|
||
8 years ago
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||
9 years ago
|
*
|
||
8 years ago
|
* Unless required by applicable law or agreed to in writing, software
|
||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
* See the License for the specific language governing permissions and
|
||
|
* limitations under the License.
|
||
9 years ago
|
*
|
||
|
*/
|
||
|
|
||
|
#include <grpc/support/port_platform.h>
|
||
|
|
||
|
#ifdef GPR_WINDOWS_SUBPROCESS
|
||
|
|
||
|
#include <string.h>
|
||
|
#include <tchar.h>
|
||
9 years ago
|
#include <windows.h>
|
||
9 years ago
|
|
||
|
#include <grpc/support/alloc.h>
|
||
|
#include <grpc/support/log.h>
|
||
7 years ago
|
#include "src/core/lib/gpr/string.h"
|
||
|
#include "src/core/lib/gpr/string_windows.h"
|
||
7 years ago
|
#include "test/core/util/subprocess.h"
|
||
9 years ago
|
|
||
|
struct gpr_subprocess {
|
||
|
PROCESS_INFORMATION pi;
|
||
|
int joined;
|
||
|
int interrupted;
|
||
|
};
|
||
|
|
||
7 years ago
|
const char* gpr_subprocess_binary_extension() { return ".exe"; }
|
||
9 years ago
|
|
||
7 years ago
|
gpr_subprocess* gpr_subprocess_create(int argc, const char** argv) {
|
||
|
gpr_subprocess* r;
|
||
9 years ago
|
|
||
|
STARTUPINFO si;
|
||
|
PROCESS_INFORMATION pi;
|
||
|
|
||
7 years ago
|
char* args = gpr_strjoin_sep(argv, (size_t)argc, " ", NULL);
|
||
|
TCHAR* args_tchar;
|
||
9 years ago
|
|
||
|
args_tchar = gpr_char_to_tchar(args);
|
||
|
gpr_free(args);
|
||
|
|
||
|
memset(&si, 0, sizeof(si));
|
||
|
si.cb = sizeof(si);
|
||
|
memset(&pi, 0, sizeof(pi));
|
||
|
|
||
|
if (!CreateProcess(NULL, args_tchar, NULL, NULL, FALSE,
|
||
|
CREATE_NEW_PROCESS_GROUP, NULL, NULL, &si, &pi)) {
|
||
|
gpr_free(args_tchar);
|
||
|
return NULL;
|
||
|
}
|
||
|
gpr_free(args_tchar);
|
||
|
|
||
7 years ago
|
r = (gpr_subprocess*)gpr_malloc(sizeof(gpr_subprocess));
|
||
9 years ago
|
memset(r, 0, sizeof(*r));
|
||
|
r->pi = pi;
|
||
|
return r;
|
||
|
}
|
||
|
|
||
7 years ago
|
void gpr_subprocess_destroy(gpr_subprocess* p) {
|
||
9 years ago
|
if (p) {
|
||
|
if (!p->joined) {
|
||
|
gpr_subprocess_interrupt(p);
|
||
|
gpr_subprocess_join(p);
|
||
|
}
|
||
|
if (p->pi.hProcess) {
|
||
|
CloseHandle(p->pi.hProcess);
|
||
|
}
|
||
|
if (p->pi.hThread) {
|
||
|
CloseHandle(p->pi.hThread);
|
||
|
}
|
||
|
gpr_free(p);
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
int gpr_subprocess_join(gpr_subprocess* p) {
|
||
9 years ago
|
DWORD dwExitCode;
|
||
|
if (GetExitCodeProcess(p->pi.hProcess, &dwExitCode)) {
|
||
|
if (dwExitCode == STILL_ACTIVE) {
|
||
|
if (WaitForSingleObject(p->pi.hProcess, INFINITE) == WAIT_OBJECT_0) {
|
||
|
p->joined = 1;
|
||
|
goto getExitCode;
|
||
|
}
|
||
|
return -1; // failed to join
|
||
|
} else {
|
||
|
goto getExitCode;
|
||
|
}
|
||
|
} else {
|
||
|
return -1; // failed to get exit code
|
||
|
}
|
||
|
|
||
|
getExitCode:
|
||
|
if (p->interrupted) {
|
||
|
return 0;
|
||
|
}
|
||
|
if (GetExitCodeProcess(p->pi.hProcess, &dwExitCode)) {
|
||
9 years ago
|
return (int)dwExitCode;
|
||
9 years ago
|
} else {
|
||
|
return -1; // failed to get exit code
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
void gpr_subprocess_interrupt(gpr_subprocess* p) {
|
||
9 years ago
|
DWORD dwExitCode;
|
||
|
if (GetExitCodeProcess(p->pi.hProcess, &dwExitCode)) {
|
||
|
if (dwExitCode == STILL_ACTIVE) {
|
||
|
gpr_log(GPR_INFO, "sending ctrl-break");
|
||
|
GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, p->pi.dwProcessId);
|
||
|
p->joined = 1;
|
||
|
p->interrupted = 1;
|
||
|
}
|
||
|
}
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
#endif /* GPR_WINDOWS_SUBPROCESS */
|