blob: fa7e4cb610073263d940faa7fedacfaa830fd7ec [file] [log] [blame]
Mike Frysinger8155d082012-04-06 15:23:18 -04001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
adlr@google.com3defe6a2009-12-04 20:57:17 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "update_engine/subprocess.h"
Darin Petkova0b9e772011-10-06 05:05:56 -07006
Alex Deymo29b81532015-07-09 11:51:49 -07007#include <fcntl.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +00008#include <stdlib.h>
9#include <string.h>
Kenneth Watersa7fcafa2010-09-21 10:27:03 -070010#include <unistd.h>
Darin Petkova0b9e772011-10-06 05:05:56 -070011
Ben Chan02f7c1d2014-10-18 15:18:02 -070012#include <memory>
Darin Petkova0b9e772011-10-06 05:05:56 -070013#include <string>
adlr@google.com3defe6a2009-12-04 20:57:17 +000014#include <vector>
Darin Petkova0b9e772011-10-06 05:05:56 -070015
Alex Deymo29b81532015-07-09 11:51:49 -070016#include <base/bind.h>
Darin Petkova0b9e772011-10-06 05:05:56 -070017#include <base/logging.h>
Alex Deymo29b81532015-07-09 11:51:49 -070018#include <base/posix/eintr_wrapper.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070019#include <base/strings/string_util.h>
20#include <base/strings/stringprintf.h>
Alex Deymo461b2592015-07-24 20:10:52 -070021#include <chromeos/process.h>
22#include <chromeos/secure_blob.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000023
Alex Deymo29b81532015-07-09 11:51:49 -070024using chromeos::MessageLoop;
adlr@google.com3defe6a2009-12-04 20:57:17 +000025using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070026using std::unique_ptr;
adlr@google.com3defe6a2009-12-04 20:57:17 +000027using std::vector;
28
29namespace chromeos_update_engine {
30
Alex Deymo461b2592015-07-24 20:10:52 -070031namespace {
Darin Petkov6f03a3b2010-11-10 14:27:14 -080032
Alex Deymo461b2592015-07-24 20:10:52 -070033bool SetupChild(const std::map<string, string>& env, uint32_t flags) {
34 // Setup the environment variables.
35 clearenv();
36 for (const auto& key_value : env) {
37 setenv(key_value.first.c_str(), key_value.second.c_str(), 0);
38 }
Darin Petkov6f03a3b2010-11-10 14:27:14 -080039
Alex Deymo461b2592015-07-24 20:10:52 -070040 if ((flags & Subprocess::kRedirectStderrToStdout) != 0) {
41 if (HANDLE_EINTR(dup2(STDOUT_FILENO, STDERR_FILENO)) != STDERR_FILENO)
42 return false;
Alex Deymo29b81532015-07-09 11:51:49 -070043 }
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -070044
Alex Deymo461b2592015-07-24 20:10:52 -070045 int fd = HANDLE_EINTR(open("/dev/null", O_RDONLY));
46 if (fd < 0)
47 return false;
48 if (HANDLE_EINTR(dup2(fd, STDIN_FILENO)) != STDIN_FILENO)
49 return false;
50 IGNORE_EINTR(close(fd));
51
52 return true;
adlr@google.com3defe6a2009-12-04 20:57:17 +000053}
54
Alex Deymo461b2592015-07-24 20:10:52 -070055// Helper function to launch a process with the given Subprocess::Flags.
56// This function only sets up and starts the process according to the |flags|.
57// The caller is responsible for watching the termination of the subprocess.
58// Return whether the process was successfully launched and fills in the |proc|
59// Process.
60bool LaunchProcess(const vector<string>& cmd,
61 uint32_t flags,
62 chromeos::Process* proc) {
63 for (const string& arg : cmd)
64 proc->AddArg(arg);
65 proc->SetSearchPath((flags & Subprocess::kSearchPath) != 0);
66
67 // Create an environment for the child process with just the required PATHs.
68 std::map<string, string> env;
69 for (const char* key : {"LD_LIBRARY_PATH", "PATH"}) {
70 const char* value = getenv(key);
71 if (value)
72 env.emplace(key, value);
73 }
74
75 proc->RedirectUsingPipe(STDOUT_FILENO, false);
76 proc->SetPreExecCallback(base::Bind(&SetupChild, env, flags));
77
78 return proc->Start();
79}
80
81} // namespace
82
Alex Deymob7ca0962014-10-01 17:58:07 -070083void Subprocess::Init(
84 chromeos::AsynchronousSignalHandlerInterface* async_signal_handler) {
Alex Deymo461b2592015-07-24 20:10:52 -070085 if (subprocess_singleton_ == this)
86 return;
87 CHECK(subprocess_singleton_ == nullptr);
88 subprocess_singleton_ = this;
89
Alex Deymob7ca0962014-10-01 17:58:07 -070090 process_reaper_.Register(async_signal_handler);
Alex Deymo461b2592015-07-24 20:10:52 -070091}
92
93Subprocess::~Subprocess() {
94 if (subprocess_singleton_ == this)
95 subprocess_singleton_ = nullptr;
Kenneth Watersa7fcafa2010-09-21 10:27:03 -070096}
97
Alex Deymo29b81532015-07-09 11:51:49 -070098void Subprocess::OnStdoutReady(SubprocessRecord* record) {
Darin Petkov6f03a3b2010-11-10 14:27:14 -080099 char buf[1024];
Alex Deymo29b81532015-07-09 11:51:49 -0700100 ssize_t rc = 0;
101 do {
102 rc = HANDLE_EINTR(read(record->stdout_fd, buf, arraysize(buf)));
103 if (rc < 0) {
104 // EAGAIN and EWOULDBLOCK are normal return values when there's no more
105 // input as we are in non-blocking mode.
106 if (errno != EWOULDBLOCK && errno != EAGAIN) {
107 PLOG(ERROR) << "Error reading fd " << record->stdout_fd;
Alex Deymo461b2592015-07-24 20:10:52 -0700108 MessageLoop::current()->CancelTask(record->stdout_task_id);
109 record->stdout_task_id = MessageLoop::kTaskIdNull;
Alex Deymo29b81532015-07-09 11:51:49 -0700110 }
Alex Deymo957cf4d2015-07-14 23:18:33 -0700111 } else if (rc == 0) {
112 // A value of 0 means that the child closed its end of the pipe and there
113 // is nothing else to read from stdout.
Alex Deymo461b2592015-07-24 20:10:52 -0700114 MessageLoop::current()->CancelTask(record->stdout_task_id);
115 record->stdout_task_id = MessageLoop::kTaskIdNull;
Alex Deymo29b81532015-07-09 11:51:49 -0700116 } else {
117 record->stdout.append(buf, rc);
118 }
119 } while (rc > 0);
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800120}
121
Alex Deymo461b2592015-07-24 20:10:52 -0700122void Subprocess::ChildExitedCallback(const siginfo_t& info) {
123 auto pid_record = subprocess_records_.find(info.si_pid);
124 if (pid_record == subprocess_records_.end())
125 return;
126 SubprocessRecord* record = pid_record->second.get();
127
128 // Make sure we read any remaining process output and then close the pipe.
129 OnStdoutReady(record);
130
131 MessageLoop::current()->CancelTask(record->stdout_task_id);
132 record->stdout_task_id = MessageLoop::kTaskIdNull;
133
134 // Release and close all the pipes now.
135 record->proc.Release();
136 record->proc.Reset(0);
137
138 // Don't print any log if the subprocess exited with exit code 0.
139 if (info.si_code != CLD_EXITED) {
140 LOG(INFO) << "Subprocess terminated with si_code " << info.si_code;
141 } else if (info.si_status != 0) {
142 LOG(INFO) << "Subprocess exited with si_status: " << info.si_status;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000143 }
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700144
Alex Deymo461b2592015-07-24 20:10:52 -0700145 if (!record->stdout.empty()) {
146 LOG(INFO) << "Subprocess output:\n" << record->stdout;
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700147 }
Alex Deymo461b2592015-07-24 20:10:52 -0700148 if (!record->callback.is_null()) {
149 record->callback.Run(info.si_status, record->stdout);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700150 }
Alex Deymo461b2592015-07-24 20:10:52 -0700151 subprocess_records_.erase(pid_record);
Alex Deymo29b81532015-07-09 11:51:49 -0700152}
153
Alex Deymo461b2592015-07-24 20:10:52 -0700154pid_t Subprocess::Exec(const vector<string>& cmd,
155 const ExecCallback& callback) {
156 return ExecFlags(cmd, kRedirectStderrToStdout, callback);
157}
Andrew de los Reyes08c4e272010-04-15 14:02:17 -0700158
Alex Deymo461b2592015-07-24 20:10:52 -0700159pid_t Subprocess::ExecFlags(const vector<string>& cmd,
160 uint32_t flags,
161 const ExecCallback& callback) {
162 unique_ptr<SubprocessRecord> record(new SubprocessRecord(callback));
163
164 if (!LaunchProcess(cmd, flags, &record->proc)) {
165 LOG(ERROR) << "Failed to launch subprocess";
Chris Masonec6c57a52010-09-23 13:06:14 -0700166 return 0;
167 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000168
Alex Deymo461b2592015-07-24 20:10:52 -0700169 pid_t pid = record->proc.pid();
170 CHECK(process_reaper_.WatchForChild(FROM_HERE, pid, base::Bind(
171 &Subprocess::ChildExitedCallback,
172 base::Unretained(this))));
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800173
Alex Deymo461b2592015-07-24 20:10:52 -0700174 record->stdout_fd = record->proc.GetPipe(STDOUT_FILENO);
Alex Deymo29b81532015-07-09 11:51:49 -0700175 // Capture the subprocess output. Make our end of the pipe non-blocking.
Alex Deymo461b2592015-07-24 20:10:52 -0700176 int fd_flags = fcntl(record->stdout_fd, F_GETFL, 0) | O_NONBLOCK;
Alex Deymo29b81532015-07-09 11:51:49 -0700177 if (HANDLE_EINTR(fcntl(record->stdout_fd, F_SETFL, fd_flags)) < 0) {
178 LOG(ERROR) << "Unable to set non-blocking I/O mode on fd "
179 << record->stdout_fd << ".";
180 }
181
Alex Deymo461b2592015-07-24 20:10:52 -0700182 record->stdout_task_id = MessageLoop::current()->WatchFileDescriptor(
Alex Deymo29b81532015-07-09 11:51:49 -0700183 FROM_HERE,
184 record->stdout_fd,
185 MessageLoop::WatchMode::kWatchRead,
186 true,
187 base::Bind(&Subprocess::OnStdoutReady, record.get()));
188
Alex Deymo461b2592015-07-24 20:10:52 -0700189 subprocess_records_[pid].reset(record.release());
190 return pid;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000191}
192
Alex Deymo461b2592015-07-24 20:10:52 -0700193void Subprocess::KillExec(pid_t pid) {
194 auto pid_record = subprocess_records_.find(pid);
195 if (pid_record == subprocess_records_.end())
Alex Deymo29b81532015-07-09 11:51:49 -0700196 return;
Alex Deymo461b2592015-07-24 20:10:52 -0700197 pid_record->second->callback.Reset();
198 kill(pid, SIGTERM);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000199}
200
Alex Deymof329b932014-10-30 01:37:48 -0700201bool Subprocess::SynchronousExec(const vector<string>& cmd,
Darin Petkov85d02b72011-05-17 13:25:51 -0700202 int* return_code,
Alex Deymof329b932014-10-30 01:37:48 -0700203 string* stdout) {
Alex Deymo461b2592015-07-24 20:10:52 -0700204 // The default for SynchronousExec is to use kSearchPath since the code relies
205 // on that.
206 return SynchronousExecFlags(
207 cmd,
208 kRedirectStderrToStdout | kSearchPath,
209 return_code,
210 stdout);
211}
212
213bool Subprocess::SynchronousExecFlags(const vector<string>& cmd,
214 uint32_t flags,
215 int* return_code,
216 string* stdout) {
217 chromeos::ProcessImpl proc;
218 if (!LaunchProcess(cmd, flags, &proc)) {
219 LOG(ERROR) << "Failed to launch subprocess";
220 return false;
221 }
222
223 if (stdout) {
224 stdout->clear();
225 }
226
227 int fd = proc.GetPipe(STDOUT_FILENO);
228 vector<char> buffer(32 * 1024);
229 while (true) {
230 int rc = HANDLE_EINTR(read(fd, buffer.data(), buffer.size()));
231 if (rc < 0) {
232 PLOG(ERROR) << "Reading from child's output";
233 break;
234 } else if (rc == 0) {
235 break;
236 } else {
237 if (stdout)
238 stdout->append(buffer.data(), rc);
239 }
240 }
241 // At this point, the subprocess already closed the output, so we only need to
242 // wait for it to finish.
243 int proc_return_code = proc.Wait();
244 if (return_code)
245 *return_code = proc_return_code;
246 return proc_return_code != chromeos::Process::kErrorExitStatus;
Darin Petkov85d02b72011-05-17 13:25:51 -0700247}
248
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800249bool Subprocess::SubprocessInFlight() {
Alex Deymo461b2592015-07-24 20:10:52 -0700250 for (const auto& pid_record : subprocess_records_) {
251 if (!pid_record.second->callback.is_null())
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800252 return true;
253 }
254 return false;
255}
256
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700257Subprocess* Subprocess::subprocess_singleton_ = nullptr;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000258
259} // namespace chromeos_update_engine