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> |
| 21 | #include <sys/wait.h> |
| 22 | #include <unistd.h> |
| 23 | |
| 24 | #include <base/logging.h> |
| 25 | |
| 26 | std::unique_ptr<Workload> Workload::CreateWorkload(const std::vector<std::string>& args) { |
| 27 | std::unique_ptr<Workload> workload(new Workload(args)); |
| 28 | if (workload != nullptr && workload->CreateNewProcess()) { |
| 29 | return workload; |
| 30 | } |
| 31 | return nullptr; |
| 32 | } |
| 33 | |
| 34 | static void ChildProcessFn(std::vector<std::string>& args, int start_signal_fd, int exec_child_fd); |
| 35 | |
| 36 | bool Workload::CreateNewProcess() { |
| 37 | CHECK_EQ(work_state_, NotYetCreateNewProcess); |
| 38 | |
| 39 | int start_signal_pipe[2]; |
| 40 | if (pipe2(start_signal_pipe, O_CLOEXEC) != 0) { |
| 41 | PLOG(ERROR) << "pipe2() failed"; |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | int exec_child_pipe[2]; |
| 46 | if (pipe2(exec_child_pipe, O_CLOEXEC) != 0) { |
| 47 | PLOG(ERROR) << "pipe2() failed"; |
| 48 | close(start_signal_pipe[0]); |
| 49 | close(start_signal_pipe[1]); |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | pid_t pid = fork(); |
| 54 | if (pid == -1) { |
| 55 | PLOG(ERROR) << "fork() failed"; |
| 56 | close(start_signal_pipe[0]); |
| 57 | close(start_signal_pipe[1]); |
| 58 | close(exec_child_pipe[0]); |
| 59 | close(exec_child_pipe[1]); |
| 60 | return false; |
| 61 | } else if (pid == 0) { |
| 62 | // In child process. |
| 63 | close(start_signal_pipe[1]); |
| 64 | close(exec_child_pipe[0]); |
| 65 | ChildProcessFn(args_, start_signal_pipe[0], exec_child_pipe[1]); |
| 66 | } |
| 67 | // In parent process. |
| 68 | close(start_signal_pipe[0]); |
| 69 | close(exec_child_pipe[1]); |
| 70 | start_signal_fd_ = start_signal_pipe[1]; |
| 71 | exec_child_fd_ = exec_child_pipe[0]; |
| 72 | work_pid_ = pid; |
| 73 | work_state_ = NotYetStartNewProcess; |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | static void ChildProcessFn(std::vector<std::string>& args, int start_signal_fd, int exec_child_fd) { |
| 78 | std::vector<char*> argv(args.size() + 1); |
| 79 | for (size_t i = 0; i < args.size(); ++i) { |
| 80 | argv[i] = &args[i][0]; |
| 81 | } |
| 82 | argv[args.size()] = nullptr; |
| 83 | |
| 84 | char start_signal = 0; |
| 85 | ssize_t nread = TEMP_FAILURE_RETRY(read(start_signal_fd, &start_signal, 1)); |
| 86 | if (nread == 1 && start_signal == 1) { |
| 87 | close(start_signal_fd); |
| 88 | execvp(argv[0], argv.data()); |
| 89 | // If execvp() succeed, we will not arrive here. But if it failed, we need to |
| 90 | // report the failure to the parent process by writing 1 to exec_child_fd. |
| 91 | int saved_errno = errno; |
| 92 | char exec_child_failed = 1; |
| 93 | TEMP_FAILURE_RETRY(write(exec_child_fd, &exec_child_failed, 1)); |
| 94 | close(exec_child_fd); |
| 95 | errno = saved_errno; |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 96 | PLOG(FATAL) << "execvp(" << argv[0] << ") failed"; |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 97 | } else { |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 98 | PLOG(FATAL) << "child process failed to receive start_signal, nread = " << nread; |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
| 102 | bool Workload::Start() { |
| 103 | CHECK_EQ(work_state_, NotYetStartNewProcess); |
| 104 | char start_signal = 1; |
| 105 | ssize_t nwrite = TEMP_FAILURE_RETRY(write(start_signal_fd_, &start_signal, 1)); |
| 106 | if (nwrite != 1) { |
| 107 | PLOG(ERROR) << "write start signal failed"; |
| 108 | return false; |
| 109 | } |
| 110 | char exec_child_failed; |
| 111 | ssize_t nread = TEMP_FAILURE_RETRY(read(exec_child_fd_, &exec_child_failed, 1)); |
| 112 | if (nread != 0) { |
| 113 | LOG(ERROR) << "exec child failed"; |
| 114 | return false; |
| 115 | } |
| 116 | work_state_ = Started; |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | bool Workload::IsFinished() { |
| 121 | if (work_state_ == Started) { |
| 122 | WaitChildProcess(true); |
| 123 | } |
| 124 | return work_state_ == Finished; |
| 125 | } |
| 126 | |
| 127 | void Workload::WaitFinish() { |
| 128 | CHECK(work_state_ == Started || work_state_ == Finished); |
| 129 | if (work_state_ == Started) { |
| 130 | WaitChildProcess(false); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | void Workload::WaitChildProcess(bool no_hang) { |
| 135 | int status; |
| 136 | pid_t result = TEMP_FAILURE_RETRY(waitpid(work_pid_, &status, (no_hang ? WNOHANG : 0))); |
| 137 | if (result == work_pid_) { |
| 138 | work_state_ = Finished; |
| 139 | if (WIFSIGNALED(status)) { |
| 140 | LOG(ERROR) << "work process was terminated by signal " << strsignal(WTERMSIG(status)); |
| 141 | } else if (WIFEXITED(status) && WEXITSTATUS(status) != 0) { |
| 142 | LOG(ERROR) << "work process exited with exit code " << WEXITSTATUS(status); |
| 143 | } |
| 144 | } else if (result == -1) { |
| 145 | PLOG(FATAL) << "waitpid() failed"; |
| 146 | } |
| 147 | } |