subprocess_posix.cc
Go to the documentation of this file.
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 
20 
21 #ifdef GPR_POSIX_SUBPROCESS
22 
23 #include <assert.h>
24 #include <errno.h>
25 #include <signal.h>
26 #include <stdbool.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #include <sys/wait.h>
32 #include <unistd.h>
33 
34 #include <grpc/support/alloc.h>
35 #include <grpc/support/log.h>
36 
39 
40 struct gpr_subprocess {
41  int pid;
42  bool joined;
43 };
44 
45 const char* gpr_subprocess_binary_extension() { return ""; }
46 
47 gpr_subprocess* gpr_subprocess_create(int argc, const char** argv) {
49  int pid;
50  char** exec_args;
51 
52  pid = fork();
53  if (pid == -1) {
54  return nullptr;
55  } else if (pid == 0) {
56  exec_args = static_cast<char**>(
57  gpr_malloc((static_cast<size_t>(argc) + 1) * sizeof(char*)));
58  memcpy(exec_args, argv, static_cast<size_t>(argc) * sizeof(char*));
59  exec_args[argc] = nullptr;
60  execv(exec_args[0], exec_args);
61  /* if we reach here, an error has occurred */
62  gpr_log(GPR_ERROR, "execv '%s' failed: %s", exec_args[0], strerror(errno));
63  _exit(1);
64  } else {
65  r = grpc_core::Zalloc<gpr_subprocess>();
66  r->pid = pid;
67  return r;
68  }
69 }
70 
72  if (!p->joined) {
73  kill(p->pid, SIGKILL);
75  }
76  gpr_free(p);
77 }
78 
80  int status;
81 retry:
82  if (waitpid(p->pid, &status, 0) == -1) {
83  if (errno == EINTR) {
84  goto retry;
85  }
86  gpr_log(GPR_ERROR, "waitpid failed for pid %d: %s", p->pid,
87  strerror(errno));
88  return -1;
89  }
90  p->joined = true;
91  return status;
92 }
93 
95  if (!p->joined) {
96  kill(p->pid, SIGINT);
97  }
98 }
99 
100 #endif /* GPR_POSIX_SUBPROCESS */
log.h
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()
status
absl::Status status
Definition: rls.cc:251
xds_manager.p
p
Definition: xds_manager.py:60
retry
void retry(grpc_end2end_test_config config)
Definition: retry.cc:319
memory.h
gpr_subprocess_interrupt
void gpr_subprocess_interrupt(gpr_subprocess *p)
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
SIGKILL
#define SIGKILL
Definition: win.h:87
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
gpr_subprocess
struct gpr_subprocess gpr_subprocess
Definition: test/core/util/subprocess.h:24
subprocess.h
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)
errno.h
port_platform.h


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