2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <sys/prctl.h>
25 #include <android-base/logging.h>
26 #include <android-base/strings.h>
28 std::unique_ptr<Workload> Workload::CreateWorkload(const std::vector<std::string>& args) {
29 std::unique_ptr<Workload> workload(new Workload(args, std::function<void ()>()));
30 if (workload != nullptr && workload->CreateNewProcess()) {
36 std::unique_ptr<Workload> Workload::CreateWorkload(const std::function<void ()>& function) {
37 std::unique_ptr<Workload> workload(new Workload(std::vector<std::string>(), function));
38 if (workload != nullptr && workload->CreateNewProcess()) {
44 bool 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) {
48 LOG(ERROR) << "Failed to run cmd " << arg_str << ", exitcode " << ret;
54 Workload::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),
61 kill_function_ = [](pid_t pid) {
66 Workload::~Workload() {
67 if (work_pid_ != -1 && work_state_ != NotYetCreateNewProcess) {
68 if (!Workload::WaitChildProcess(false, false, nullptr)) {
69 kill_function_(work_pid_);
70 Workload::WaitChildProcess(true, true, nullptr);
73 if (start_signal_fd_ != -1) {
74 close(start_signal_fd_);
76 if (exec_child_fd_ != -1) {
77 close(exec_child_fd_);
81 bool Workload::CreateNewProcess() {
82 CHECK_EQ(work_state_, NotYetCreateNewProcess);
84 int start_signal_pipe[2];
85 if (pipe2(start_signal_pipe, O_CLOEXEC) != 0) {
86 PLOG(ERROR) << "pipe2() failed";
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]);
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]);
106 } else if (pid == 0) {
108 close(start_signal_pipe[1]);
109 close(exec_child_pipe[0]);
110 ChildProcessFn(start_signal_pipe[0], exec_child_pipe[1]);
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];
119 work_state_ = NotYetStartNewProcess;
123 void Workload::ChildProcessFn(int start_signal_fd, int exec_child_fd) {
124 // Die if parent exits.
125 prctl(PR_SET_PDEATHSIG, SIGHUP, 0, 0, 0);
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);
131 if (child_proc_function_) {
132 close(exec_child_fd);
133 child_proc_function_();
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];
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);
148 PLOG(ERROR) << "child process failed to execvp(" << argv[0] << ")";
151 PLOG(ERROR) << "child process failed to receive start_signal, nread = " << nread;
155 bool 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));
160 PLOG(ERROR) << "write start signal failed";
163 char exec_child_failed;
164 ssize_t nread = TEMP_FAILURE_RETRY(read(exec_child_fd_, &exec_child_failed, 1));
167 PLOG(ERROR) << "failed to receive error message from child process";
169 LOG(ERROR) << "received error message from child process";
173 work_state_ = Started;
177 bool Workload::WaitChildProcess(int* exit_code) {
178 return WaitChildProcess(true, false, exit_code);
181 bool Workload::WaitChildProcess(bool wait_forever, bool is_child_killed, int* exit_code) {
182 if (work_state_ == Finished) {
185 bool finished = false;
187 pid_t result = TEMP_FAILURE_RETRY(waitpid(work_pid_, &status, (wait_forever ? 0 : WNOHANG)));
188 if (result == work_pid_) {
190 work_state_ = Finished;
191 if (WIFSIGNALED(status)) {
192 if (!(is_child_killed && WTERMSIG(status) == SIGKILL)) {
193 LOG(WARNING) << "child process was terminated by signal " << strsignal(WTERMSIG(status));
195 } 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);
202 } else if (result == -1) {
203 PLOG(ERROR) << "waitpid() failed";