Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 1 | /* |
| 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 Cui | 987d9ab | 2016-07-15 14:08:48 -0700 | [diff] [blame] | 21 | #include <sys/prctl.h> |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 22 | #include <sys/wait.h> |
| 23 | #include <unistd.h> |
| 24 | |
Elliott Hughes | 66dd09e | 2015-12-04 14:00:57 -0800 | [diff] [blame] | 25 | #include <android-base/logging.h> |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 26 | #include <android-base/strings.h> |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 27 | |
| 28 | std::unique_ptr<Workload> Workload::CreateWorkload(const std::vector<std::string>& args) { |
Yabin Cui | 5be914d | 2016-11-29 15:21:13 -0800 | [diff] [blame] | 29 | 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 | |
| 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)); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 38 | if (workload != nullptr && workload->CreateNewProcess()) { |
| 39 | return workload; |
| 40 | } |
| 41 | return nullptr; |
| 42 | } |
| 43 | |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 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) { |
Yabin Cui | f897452 | 2017-07-17 14:36:37 -0700 | [diff] [blame] | 48 | LOG(ERROR) << "Failed to run cmd " << arg_str << ", exitcode " << ret; |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 49 | return false; |
| 50 | } |
| 51 | return ret == 0; |
| 52 | } |
| 53 | |
Yabin Cui | 616b3a0 | 2017-07-14 15:59:56 -0700 | [diff] [blame] | 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), |
| 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 Cui | 7d4904d | 2015-06-09 13:38:42 -0700 | [diff] [blame] | 66 | Workload::~Workload() { |
Yabin Cui | 621a533 | 2015-06-15 16:17:20 -0700 | [diff] [blame] | 67 | if (work_pid_ != -1 && work_state_ != NotYetCreateNewProcess) { |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 68 | if (!Workload::WaitChildProcess(false, false, nullptr)) { |
Yabin Cui | 616b3a0 | 2017-07-14 15:59:56 -0700 | [diff] [blame] | 69 | kill_function_(work_pid_); |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 70 | Workload::WaitChildProcess(true, true, nullptr); |
Yabin Cui | 621a533 | 2015-06-15 16:17:20 -0700 | [diff] [blame] | 71 | } |
Yabin Cui | 7d4904d | 2015-06-09 13:38:42 -0700 | [diff] [blame] | 72 | } |
| 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 Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 81 | bool 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 Cui | 5be914d | 2016-11-29 15:21:13 -0800 | [diff] [blame] | 110 | ChildProcessFn(start_signal_pipe[0], exec_child_pipe[1]); |
Yabin Cui | c8fcde2 | 2015-09-24 12:37:27 -0700 | [diff] [blame] | 111 | _exit(0); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 112 | } |
| 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 Cui | 5be914d | 2016-11-29 15:21:13 -0800 | [diff] [blame] | 123 | void Workload::ChildProcessFn(int start_signal_fd, int exec_child_fd) { |
Yabin Cui | 987d9ab | 2016-07-15 14:08:48 -0700 | [diff] [blame] | 124 | // Die if parent exits. |
| 125 | prctl(PR_SET_PDEATHSIG, SIGHUP, 0, 0, 0); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 126 | |
| 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 Cui | 5be914d | 2016-11-29 15:21:13 -0800 | [diff] [blame] | 131 | 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 Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 150 | } else { |
Yabin Cui | 42aa127 | 2015-09-18 11:10:55 -0700 | [diff] [blame] | 151 | PLOG(ERROR) << "child process failed to receive start_signal, nread = " << nread; |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 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)); |
| 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 Cui | 42aa127 | 2015-09-18 11:10:55 -0700 | [diff] [blame] | 166 | 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 Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 171 | return false; |
| 172 | } |
| 173 | work_state_ = Started; |
| 174 | return true; |
| 175 | } |
| 176 | |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 177 | bool Workload::WaitChildProcess(int* exit_code) { |
| 178 | return WaitChildProcess(true, false, exit_code); |
| 179 | } |
| 180 | |
| 181 | bool Workload::WaitChildProcess(bool wait_forever, bool is_child_killed, int* exit_code) { |
| 182 | if (work_state_ == Finished) { |
| 183 | return true; |
| 184 | } |
Yabin Cui | 621a533 | 2015-06-15 16:17:20 -0700 | [diff] [blame] | 185 | bool finished = false; |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 186 | int status; |
Yabin Cui | 621a533 | 2015-06-15 16:17:20 -0700 | [diff] [blame] | 187 | pid_t result = TEMP_FAILURE_RETRY(waitpid(work_pid_, &status, (wait_forever ? 0 : WNOHANG))); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 188 | if (result == work_pid_) { |
Yabin Cui | 621a533 | 2015-06-15 16:17:20 -0700 | [diff] [blame] | 189 | finished = true; |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 190 | work_state_ = Finished; |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 191 | if (WIFSIGNALED(status)) { |
Yabin Cui | 6d1ee48 | 2016-07-07 15:00:10 -0700 | [diff] [blame] | 192 | if (!(is_child_killed && WTERMSIG(status) == SIGKILL)) { |
| 193 | LOG(WARNING) << "child process was terminated by signal " << strsignal(WTERMSIG(status)); |
| 194 | } |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 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); |
| 200 | } |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 201 | } |
| 202 | } else if (result == -1) { |
Yabin Cui | 621a533 | 2015-06-15 16:17:20 -0700 | [diff] [blame] | 203 | PLOG(ERROR) << "waitpid() failed"; |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 204 | } |
Yabin Cui | 621a533 | 2015-06-15 16:17:20 -0700 | [diff] [blame] | 205 | return finished; |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 206 | } |