3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <grpc/support/port_platform.h>
21 #ifdef GPR_POSIX_SUBPROCESS
30 #include <sys/types.h>
34 #include <grpc/support/alloc.h>
35 #include <grpc/support/log.h>
37 #include "test/core/util/subprocess.h"
39 struct gpr_subprocess {
44 const char* gpr_subprocess_binary_extension() { return ""; }
46 gpr_subprocess* gpr_subprocess_create(int argc, const char** argv) {
54 } else if (pid == 0) {
55 exec_args = static_cast<char**>(
56 gpr_malloc((static_cast<size_t>(argc) + 1) * sizeof(char*)));
57 memcpy(exec_args, argv, static_cast<size_t>(argc) * sizeof(char*));
58 exec_args[argc] = nullptr;
59 execv(exec_args[0], exec_args);
60 /* if we reach here, an error has occurred */
61 gpr_log(GPR_ERROR, "execv '%s' failed: %s", exec_args[0], strerror(errno));
65 r = static_cast<gpr_subprocess*>(gpr_zalloc(sizeof(gpr_subprocess)));
71 void gpr_subprocess_destroy(gpr_subprocess* p) {
73 kill(p->pid, SIGKILL);
74 gpr_subprocess_join(p);
79 int gpr_subprocess_join(gpr_subprocess* p) {
82 if (waitpid(p->pid, &status, 0) == -1) {
86 gpr_log(GPR_ERROR, "waitpid failed for pid %d: %s", p->pid,
94 void gpr_subprocess_interrupt(gpr_subprocess* p) {
100 #endif /* GPR_POSIX_SUBPROCESS */