Imported Upstream version 1.41.0
[platform/upstream/grpc.git] / test / core / util / subprocess_posix.cc
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
44 const char* gpr_subprocess_binary_extension() { return ""; }
45
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   } else {
64     r = static_cast<gpr_subprocess*>(gpr_zalloc(sizeof(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             strerror(errno));
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 #endif /* GPR_POSIX_SUBPROCESS */