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.
24 #include <base/logging.h>
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()) {
34 static void ChildProcessFn(std::vector<std::string>& args, int start_signal_fd, int exec_child_fd);
36 bool Workload::CreateNewProcess() {
37 CHECK_EQ(work_state_, NotYetCreateNewProcess);
39 int start_signal_pipe[2];
40 if (pipe2(start_signal_pipe, O_CLOEXEC) != 0) {
41 PLOG(ERROR) << "pipe2() failed";
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]);
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]);
61 } else if (pid == 0) {
63 close(start_signal_pipe[1]);
64 close(exec_child_pipe[0]);
65 ChildProcessFn(args_, start_signal_pipe[0], exec_child_pipe[1]);
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];
73 work_state_ = NotYetStartNewProcess;
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];
82 argv[args.size()] = nullptr;
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));
96 PLOG(FATAL) << "execvp() failed";
98 PLOG(FATAL) << "child process failed to receive start_signal";
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));
107 PLOG(ERROR) << "write start signal failed";
110 char exec_child_failed;
111 ssize_t nread = TEMP_FAILURE_RETRY(read(exec_child_fd_, &exec_child_failed, 1));
113 LOG(ERROR) << "exec child failed";
116 work_state_ = Started;
120 bool Workload::IsFinished() {
121 if (work_state_ == Started) {
122 WaitChildProcess(true);
124 return work_state_ == Finished;
127 void Workload::WaitFinish() {
128 CHECK(work_state_ == Started || work_state_ == Finished);
129 if (work_state_ == Started) {
130 WaitChildProcess(false);
134 void Workload::WaitChildProcess(bool no_hang) {
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);
144 } else if (result == -1) {
145 PLOG(FATAL) << "waitpid() failed";