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.
100 lines
2.3 KiB
100 lines
2.3 KiB
10 years ago
|
/*
|
||
|
*
|
||
8 years ago
|
* Copyright 2015 gRPC authors.
|
||
10 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
|
||
10 years ago
|
*
|
||
8 years ago
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||
10 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.
|
||
10 years ago
|
*
|
||
|
*/
|
||
|
|
||
|
#include <grpc/support/port_platform.h>
|
||
|
|
||
|
#ifdef GPR_POSIX_SUBPROCESS
|
||
|
|
||
|
#include <assert.h>
|
||
|
#include <errno.h>
|
||
|
#include <signal.h>
|
||
8 years ago
|
#include <stdbool.h>
|
||
9 years ago
|
#include <stdio.h>
|
||
10 years ago
|
#include <stdlib.h>
|
||
9 years ago
|
#include <string.h>
|
||
10 years ago
|
#include <sys/types.h>
|
||
|
#include <sys/wait.h>
|
||
9 years ago
|
#include <unistd.h>
|
||
10 years ago
|
|
||
|
#include <grpc/support/alloc.h>
|
||
|
#include <grpc/support/log.h>
|
||
|
|
||
7 years ago
|
#include "test/core/util/subprocess.h"
|
||
|
|
||
10 years ago
|
struct gpr_subprocess {
|
||
|
int pid;
|
||
8 years ago
|
bool joined;
|
||
10 years ago
|
};
|
||
|
|
||
7 years ago
|
const char* gpr_subprocess_binary_extension() { return ""; }
|
||
10 years ago
|
|
||
7 years ago
|
gpr_subprocess* gpr_subprocess_create(int argc, const char** argv) {
|
||
|
gpr_subprocess* r;
|
||
10 years ago
|
int pid;
|
||
7 years ago
|
char** exec_args;
|
||
10 years ago
|
|
||
|
pid = fork();
|
||
|
if (pid == -1) {
|
||
7 years ago
|
return nullptr;
|
||
10 years ago
|
} else if (pid == 0) {
|
||
7 years ago
|
exec_args = (char**)gpr_malloc(((size_t)argc + 1) * sizeof(char*));
|
||
|
memcpy(exec_args, argv, (size_t)argc * sizeof(char*));
|
||
7 years ago
|
exec_args[argc] = nullptr;
|
||
10 years ago
|
execv(exec_args[0], exec_args);
|
||
|
/* if we reach here, an error has occurred */
|
||
|
gpr_log(GPR_ERROR, "execv '%s' failed: %s", exec_args[0], strerror(errno));
|
||
|
_exit(1);
|
||
7 years ago
|
return nullptr;
|
||
10 years ago
|
} else {
|
||
7 years ago
|
r = (gpr_subprocess*)gpr_zalloc(sizeof(gpr_subprocess));
|
||
10 years ago
|
r->pid = pid;
|
||
|
return r;
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
void gpr_subprocess_destroy(gpr_subprocess* p) {
|
||
10 years ago
|
if (!p->joined) {
|
||
|
kill(p->pid, SIGKILL);
|
||
|
gpr_subprocess_join(p);
|
||
|
}
|
||
|
gpr_free(p);
|
||
|
}
|
||
|
|
||
7 years ago
|
int gpr_subprocess_join(gpr_subprocess* p) {
|
||
10 years ago
|
int status;
|
||
10 years ago
|
retry:
|
||
10 years ago
|
if (waitpid(p->pid, &status, 0) == -1) {
|
||
10 years ago
|
if (errno == EINTR) {
|
||
|
goto retry;
|
||
|
}
|
||
8 years ago
|
gpr_log(GPR_ERROR, "waitpid failed for pid %d: %s", p->pid,
|
||
|
strerror(errno));
|
||
10 years ago
|
return -1;
|
||
|
}
|
||
8 years ago
|
p->joined = true;
|
||
10 years ago
|
return status;
|
||
|
}
|
||
|
|
||
7 years ago
|
void gpr_subprocess_interrupt(gpr_subprocess* p) {
|
||
10 years ago
|
if (!p->joined) {
|
||
|
kill(p->pid, SIGINT);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endif /* GPR_POSIX_SUBPROCESS */
|