blob: 60c9ed1533b4d456f2d4b99ea9ebba6e49534ce7 [file] [log] [blame]
Yabin Cui323e9452015-04-20 18:07:17 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "workload.h"
18
19#include <errno.h>
20#include <fcntl.h>
Yabin Cui987d9ab2016-07-15 14:08:48 -070021#include <sys/prctl.h>
Yabin Cui323e9452015-04-20 18:07:17 -070022#include <sys/wait.h>
23#include <unistd.h>
24
Elliott Hughes66dd09e2015-12-04 14:00:57 -080025#include <android-base/logging.h>
Yabin Cuia80f8f72017-07-12 15:50:20 -070026#include <android-base/strings.h>
Yabin Cui323e9452015-04-20 18:07:17 -070027
28std::unique_ptr<Workload> Workload::CreateWorkload(const std::vector<std::string>& args) {
Yabin Cui5be914d2016-11-29 15:21:13 -080029 std::unique_ptr<Workload> workload(new Workload(args, std::function<void ()>()));
30 if (workload != nullptr && workload->CreateNewProcess()) {
31 return workload;
32 }
33 return nullptr;
34}
35
36std::unique_ptr<Workload> Workload::CreateWorkload(const std::function<void ()>& function) {
37 std::unique_ptr<Workload> workload(new Workload(std::vector<std::string>(), function));
Yabin Cui323e9452015-04-20 18:07:17 -070038 if (workload != nullptr && workload->CreateNewProcess()) {
39 return workload;
40 }
41 return nullptr;
42}
43
Yabin Cuia80f8f72017-07-12 15:50:20 -070044bool Workload::RunCmd(const std::vector<std::string>& args, bool report_error) {
45 std::string arg_str = android::base::Join(args, ' ');
46 int ret = system(arg_str.c_str());
47 if (ret != 0 && report_error) {
Yabin Cuif8974522017-07-17 14:36:37 -070048 LOG(ERROR) << "Failed to run cmd " << arg_str << ", exitcode " << ret;
Yabin Cuia80f8f72017-07-12 15:50:20 -070049 return false;
50 }
51 return ret == 0;
52}
53
Yabin Cui616b3a02017-07-14 15:59:56 -070054Workload::Workload(const std::vector<std::string>& args, const std::function<void ()>& function)
55 : work_state_(NotYetCreateNewProcess),
56 child_proc_args_(args),
57 child_proc_function_(function),
58 work_pid_(-1),
59 start_signal_fd_(-1),
60 exec_child_fd_(-1) {
61 kill_function_ = [](pid_t pid) {
62 kill(pid, SIGKILL);
63 };
64}
65
Yabin Cui7d4904d2015-06-09 13:38:42 -070066Workload::~Workload() {
Yabin Cui621a5332015-06-15 16:17:20 -070067 if (work_pid_ != -1 && work_state_ != NotYetCreateNewProcess) {
Yabin Cuia80f8f72017-07-12 15:50:20 -070068 if (!Workload::WaitChildProcess(false, false, nullptr)) {
Yabin Cui616b3a02017-07-14 15:59:56 -070069 kill_function_(work_pid_);
Yabin Cuia80f8f72017-07-12 15:50:20 -070070 Workload::WaitChildProcess(true, true, nullptr);
Yabin Cui621a5332015-06-15 16:17:20 -070071 }
Yabin Cui7d4904d2015-06-09 13:38:42 -070072 }
73 if (start_signal_fd_ != -1) {
74 close(start_signal_fd_);
75 }
76 if (exec_child_fd_ != -1) {
77 close(exec_child_fd_);
78 }
79}
80
Yabin Cui323e9452015-04-20 18:07:17 -070081bool Workload::CreateNewProcess() {
82 CHECK_EQ(work_state_, NotYetCreateNewProcess);
83
84 int start_signal_pipe[2];
85 if (pipe2(start_signal_pipe, O_CLOEXEC) != 0) {
86 PLOG(ERROR) << "pipe2() failed";
87 return false;
88 }
89
90 int exec_child_pipe[2];
91 if (pipe2(exec_child_pipe, O_CLOEXEC) != 0) {
92 PLOG(ERROR) << "pipe2() failed";
93 close(start_signal_pipe[0]);
94 close(start_signal_pipe[1]);
95 return false;
96 }
97
98 pid_t pid = fork();
99 if (pid == -1) {
100 PLOG(ERROR) << "fork() failed";
101 close(start_signal_pipe[0]);
102 close(start_signal_pipe[1]);
103 close(exec_child_pipe[0]);
104 close(exec_child_pipe[1]);
105 return false;
106 } else if (pid == 0) {
107 // In child process.
108 close(start_signal_pipe[1]);
109 close(exec_child_pipe[0]);
Yabin Cui5be914d2016-11-29 15:21:13 -0800110 ChildProcessFn(start_signal_pipe[0], exec_child_pipe[1]);
Yabin Cuic8fcde22015-09-24 12:37:27 -0700111 _exit(0);
Yabin Cui323e9452015-04-20 18:07:17 -0700112 }
113 // In parent process.
114 close(start_signal_pipe[0]);
115 close(exec_child_pipe[1]);
116 start_signal_fd_ = start_signal_pipe[1];
117 exec_child_fd_ = exec_child_pipe[0];
118 work_pid_ = pid;
119 work_state_ = NotYetStartNewProcess;
120 return true;
121}
122
Yabin Cui5be914d2016-11-29 15:21:13 -0800123void Workload::ChildProcessFn(int start_signal_fd, int exec_child_fd) {
Yabin Cui987d9ab2016-07-15 14:08:48 -0700124 // Die if parent exits.
125 prctl(PR_SET_PDEATHSIG, SIGHUP, 0, 0, 0);
Yabin Cui323e9452015-04-20 18:07:17 -0700126
127 char start_signal = 0;
128 ssize_t nread = TEMP_FAILURE_RETRY(read(start_signal_fd, &start_signal, 1));
129 if (nread == 1 && start_signal == 1) {
130 close(start_signal_fd);
Yabin Cui5be914d2016-11-29 15:21:13 -0800131 if (child_proc_function_) {
132 close(exec_child_fd);
133 child_proc_function_();
134 } else {
135 char* argv[child_proc_args_.size() + 1];
136 for (size_t i = 0; i < child_proc_args_.size(); ++i) {
137 argv[i] = &child_proc_args_[i][0];
138 }
139 argv[child_proc_args_.size()] = nullptr;
140 execvp(argv[0], argv);
141 // If execvp() succeed, we will not arrive here. But if it failed, we need to
142 // report the failure to the parent process by writing 1 to exec_child_fd.
143 int saved_errno = errno;
144 char exec_child_failed = 1;
145 TEMP_FAILURE_RETRY(write(exec_child_fd, &exec_child_failed, 1));
146 close(exec_child_fd);
147 errno = saved_errno;
148 PLOG(ERROR) << "child process failed to execvp(" << argv[0] << ")";
149 }
Yabin Cui323e9452015-04-20 18:07:17 -0700150 } else {
Yabin Cui42aa1272015-09-18 11:10:55 -0700151 PLOG(ERROR) << "child process failed to receive start_signal, nread = " << nread;
Yabin Cui323e9452015-04-20 18:07:17 -0700152 }
153}
154
155bool Workload::Start() {
156 CHECK_EQ(work_state_, NotYetStartNewProcess);
157 char start_signal = 1;
158 ssize_t nwrite = TEMP_FAILURE_RETRY(write(start_signal_fd_, &start_signal, 1));
159 if (nwrite != 1) {
160 PLOG(ERROR) << "write start signal failed";
161 return false;
162 }
163 char exec_child_failed;
164 ssize_t nread = TEMP_FAILURE_RETRY(read(exec_child_fd_, &exec_child_failed, 1));
165 if (nread != 0) {
Yabin Cui42aa1272015-09-18 11:10:55 -0700166 if (nread == -1) {
167 PLOG(ERROR) << "failed to receive error message from child process";
168 } else {
169 LOG(ERROR) << "received error message from child process";
170 }
Yabin Cui323e9452015-04-20 18:07:17 -0700171 return false;
172 }
173 work_state_ = Started;
174 return true;
175}
176
Yabin Cuia80f8f72017-07-12 15:50:20 -0700177bool Workload::WaitChildProcess(int* exit_code) {
178 return WaitChildProcess(true, false, exit_code);
179}
180
181bool Workload::WaitChildProcess(bool wait_forever, bool is_child_killed, int* exit_code) {
182 if (work_state_ == Finished) {
183 return true;
184 }
Yabin Cui621a5332015-06-15 16:17:20 -0700185 bool finished = false;
Yabin Cui323e9452015-04-20 18:07:17 -0700186 int status;
Yabin Cui621a5332015-06-15 16:17:20 -0700187 pid_t result = TEMP_FAILURE_RETRY(waitpid(work_pid_, &status, (wait_forever ? 0 : WNOHANG)));
Yabin Cui323e9452015-04-20 18:07:17 -0700188 if (result == work_pid_) {
Yabin Cui621a5332015-06-15 16:17:20 -0700189 finished = true;
Yabin Cuia80f8f72017-07-12 15:50:20 -0700190 work_state_ = Finished;
Yabin Cui323e9452015-04-20 18:07:17 -0700191 if (WIFSIGNALED(status)) {
Yabin Cui6d1ee482016-07-07 15:00:10 -0700192 if (!(is_child_killed && WTERMSIG(status) == SIGKILL)) {
193 LOG(WARNING) << "child process was terminated by signal " << strsignal(WTERMSIG(status));
194 }
Yabin Cuia80f8f72017-07-12 15:50:20 -0700195 } else if (WIFEXITED(status)) {
196 if (exit_code != nullptr) {
197 *exit_code = WEXITSTATUS(status);
198 } else if (WEXITSTATUS(status) != 0) {
199 LOG(WARNING) << "child process exited with exit code " << WEXITSTATUS(status);
200 }
Yabin Cui323e9452015-04-20 18:07:17 -0700201 }
202 } else if (result == -1) {
Yabin Cui621a5332015-06-15 16:17:20 -0700203 PLOG(ERROR) << "waitpid() failed";
Yabin Cui323e9452015-04-20 18:07:17 -0700204 }
Yabin Cui621a5332015-06-15 16:17:20 -0700205 return finished;
Yabin Cui323e9452015-04-20 18:07:17 -0700206}