• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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 
37 #include "test/core/util/subprocess.h"
38 
39 struct gpr_subprocess {
40   int pid;
41   bool joined;
42 };
43 
gpr_subprocess_binary_extension()44 const char* gpr_subprocess_binary_extension() { return ""; }
45 
gpr_subprocess_create(int argc,const char ** argv)46 gpr_subprocess* gpr_subprocess_create(int argc, const char** argv) {
47   gpr_subprocess* r;
48   int pid;
49   char** exec_args;
50 
51   pid = fork();
52   if (pid == -1) {
53     return nullptr;
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));
62     _exit(1);
63     return nullptr;
64   } else {
65     r = static_cast<gpr_subprocess*>(gpr_zalloc(sizeof(gpr_subprocess)));
66     r->pid = pid;
67     return r;
68   }
69 }
70 
gpr_subprocess_destroy(gpr_subprocess * p)71 void gpr_subprocess_destroy(gpr_subprocess* p) {
72   if (!p->joined) {
73     kill(p->pid, SIGKILL);
74     gpr_subprocess_join(p);
75   }
76   gpr_free(p);
77 }
78 
gpr_subprocess_join(gpr_subprocess * p)79 int gpr_subprocess_join(gpr_subprocess* p) {
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 
gpr_subprocess_interrupt(gpr_subprocess * p)94 void gpr_subprocess_interrupt(gpr_subprocess* p) {
95   if (!p->joined) {
96     kill(p->pid, SIGINT);
97   }
98 }
99 
100 #endif /* GPR_POSIX_SUBPROCESS */
101