| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 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 |
#include <string> |
| 22 |
|
| 23 |
#ifdef GPR_POSIX_SUBPROCESS |
| 24 |
|
| 25 |
#include <errno.h> |
| 26 |
#include <signal.h> |
| 27 |
#include <string.h> |
| 28 |
#include <sys/wait.h> |
| 29 |
#include <unistd.h> |
| 30 |
|
| 31 |
#include <grpc/support/alloc.h> |
| 32 |
#include <grpc/support/log.h> |
| 33 |
|
| 34 |
#include "src/core/lib/gprpp/memory.h" |
| 35 |
#include "src/core/lib/gprpp/strerror.h" |
| 36 |
#include "test/core/util/subprocess.h" |
| 37 |
|
| 38 |
struct gpr_subprocess { |
| 39 |
int pid; |
| 40 |
bool joined; |
| 41 |
}; |
| 42 |
|
| 43 |
const char* gpr_subprocess_binary_extension() { return ""; } |
| 44 |
|
| 45 |
gpr_subprocess* gpr_subprocess_create(int argc, const char** argv) { |
| 46 |
gpr_subprocess* r; |
| 47 |
int pid; |
| 48 |
char** exec_args; |
| 49 |
|
| 50 |
pid = fork(); |
| 51 |
if (pid == -1) { |
| 52 |
return nullptr; |
| 53 |
} else if (pid == 0) { |
| 54 |
exec_args = static_cast<char**>( |
| 55 |
gpr_malloc((static_cast<size_t>(argc) + 1) * sizeof(char*))); |
| 56 |
memcpy(exec_args, argv, static_cast<size_t>(argc) * sizeof(char*)); |
| 57 |
exec_args[argc] = nullptr; |
| 58 |
execv(exec_args[0], exec_args); |
| 59 |
/* if we reach here, an error has occurred */ |
| 60 |
gpr_log(GPR_ERROR, "execv '%s' failed: %s", exec_args[0], |
| 61 |
grpc_core::StrError(errno).c_str()); |
| 62 |
_exit(1); |
| 63 |
} else { |
| 64 |
r = grpc_core::Zalloc<gpr_subprocess>(); |
| 65 |
r->pid = pid; |
| 66 |
return r; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
void gpr_subprocess_destroy(gpr_subprocess* p) { |
| 71 |
if (!p->joined) { |
| 72 |
kill(p->pid, SIGKILL); |
| 73 |
gpr_subprocess_join(p); |
| 74 |
} |
| 75 |
gpr_free(p); |
| 76 |
} |
| 77 |
|
| 78 |
int gpr_subprocess_join(gpr_subprocess* p) { |
| 79 |
int status; |
| 80 |
retry: |
| 81 |
if (waitpid(p->pid, &status, 0) == -1) { |
| 82 |
if (errno == EINTR) { |
| 83 |
goto retry; |
| 84 |
} |
| 85 |
gpr_log(GPR_ERROR, "waitpid failed for pid %d: %s", p->pid, |
| 86 |
grpc_core::StrError(errno).c_str()); |
| 87 |
return -1; |
| 88 |
} |
| 89 |
p->joined = true; |
| 90 |
return status; |
| 91 |
} |
| 92 |
|
| 93 |
void gpr_subprocess_interrupt(gpr_subprocess* p) { |
| 94 |
if (!p->joined) { |
| 95 |
kill(p->pid, SIGINT); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
int gpr_subprocess_get_process_id(gpr_subprocess* p) { return p->pid; } |
| 100 |
|
| 101 |
#endif /* GPR_POSIX_SUBPROCESS */ |