blob: db66eb97464a6e3c9a7d795cb1b5ca4e0e7c28bf [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>
Darin Petkova0b9e772011-10-06 05:05:56 -070021
Alex Deymo44666f92014-07-22 20:29:24 -070022#include "update_engine/glib_utils.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000023
Alex Deymo29b81532015-07-09 11:51:49 -070024using chromeos::MessageLoop;
Alex Deymobc91a272014-05-20 16:45:33 -070025using std::shared_ptr;
adlr@google.com3defe6a2009-12-04 20:57:17 +000026using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070027using std::unique_ptr;
adlr@google.com3defe6a2009-12-04 20:57:17 +000028using std::vector;
29
30namespace chromeos_update_engine {
31
32void Subprocess::GChildExitedCallback(GPid pid, gint status, gpointer data) {
Darin Petkov6f03a3b2010-11-10 14:27:14 -080033 SubprocessRecord* record = reinterpret_cast<SubprocessRecord*>(data);
34
Alex Deymo29b81532015-07-09 11:51:49 -070035 // Make sure we read any remaining process output and then close the pipe.
36 OnStdoutReady(record);
Darin Petkov6f03a3b2010-11-10 14:27:14 -080037
Alex Deymo29b81532015-07-09 11:51:49 -070038 MessageLoop::current()->CancelTask(record->task_id);
39 record->task_id = MessageLoop::kTaskIdNull;
40 if (IGNORE_EINTR(close(record->stdout_fd)) != 0) {
41 PLOG(ERROR) << "Error closing fd " << record->stdout_fd;
42 }
adlr@google.com3defe6a2009-12-04 20:57:17 +000043 g_spawn_close_pid(pid);
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -070044 gint use_status = status;
45 if (WIFEXITED(status))
46 use_status = WEXITSTATUS(status);
47
Darin Petkov6f03a3b2010-11-10 14:27:14 -080048 if (status) {
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -070049 LOG(INFO) << "Subprocess status: " << use_status;
Darin Petkov6f03a3b2010-11-10 14:27:14 -080050 }
51 if (!record->stdout.empty()) {
52 LOG(INFO) << "Subprocess output:\n" << record->stdout;
53 }
54 if (record->callback) {
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -070055 record->callback(use_status, record->stdout, record->callback_data);
Darin Petkov6f03a3b2010-11-10 14:27:14 -080056 }
57 Get().subprocess_records_.erase(record->tag);
adlr@google.com3defe6a2009-12-04 20:57:17 +000058}
59
Kenneth Watersa7fcafa2010-09-21 10:27:03 -070060void Subprocess::GRedirectStderrToStdout(gpointer user_data) {
61 dup2(1, 2);
62}
63
Alex Deymo29b81532015-07-09 11:51:49 -070064void Subprocess::OnStdoutReady(SubprocessRecord* record) {
Darin Petkov6f03a3b2010-11-10 14:27:14 -080065 char buf[1024];
Alex Deymo29b81532015-07-09 11:51:49 -070066 ssize_t rc = 0;
67 do {
68 rc = HANDLE_EINTR(read(record->stdout_fd, buf, arraysize(buf)));
69 if (rc < 0) {
70 // EAGAIN and EWOULDBLOCK are normal return values when there's no more
71 // input as we are in non-blocking mode.
72 if (errno != EWOULDBLOCK && errno != EAGAIN) {
73 PLOG(ERROR) << "Error reading fd " << record->stdout_fd;
74 }
Alex Deymo957cf4d2015-07-14 23:18:33 -070075 } else if (rc == 0) {
76 // A value of 0 means that the child closed its end of the pipe and there
77 // is nothing else to read from stdout.
78 MessageLoop::current()->CancelTask(record->task_id);
79 record->task_id = MessageLoop::kTaskIdNull;
Alex Deymo29b81532015-07-09 11:51:49 -070080 } else {
81 record->stdout.append(buf, rc);
82 }
83 } while (rc > 0);
Darin Petkov6f03a3b2010-11-10 14:27:14 -080084}
85
adlr@google.com3defe6a2009-12-04 20:57:17 +000086namespace {
87void FreeArgv(char** argv) {
88 for (int i = 0; argv[i]; i++) {
89 free(argv[i]);
Alex Vakulenko88b591f2014-08-28 16:48:57 -070090 argv[i] = nullptr;
adlr@google.com3defe6a2009-12-04 20:57:17 +000091 }
92}
Andrew de los Reyes3270f742010-07-15 22:28:14 -070093
Chris Masonec6c57a52010-09-23 13:06:14 -070094void FreeArgvInError(char** argv) {
95 FreeArgv(argv);
96 LOG(ERROR) << "Ran out of memory copying args.";
97}
98
Andrew de los Reyes3270f742010-07-15 22:28:14 -070099// Note: Caller responsible for free()ing the returned value!
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700100// Will return null on failure and free any allocated memory.
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700101char** ArgPointer() {
102 const char* keys[] = {"LD_LIBRARY_PATH", "PATH"};
103 char** ret = new char*[arraysize(keys) + 1];
104 int pointer = 0;
105 for (size_t i = 0; i < arraysize(keys); i++) {
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700106 if (getenv(keys[i])) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700107 ret[pointer] = strdup(base::StringPrintf("%s=%s", keys[i],
108 getenv(keys[i])).c_str());
Chris Masonec6c57a52010-09-23 13:06:14 -0700109 if (!ret[pointer]) {
110 FreeArgv(ret);
111 delete [] ret;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700112 return nullptr;
Chris Masonec6c57a52010-09-23 13:06:14 -0700113 }
114 ++pointer;
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700115 }
116 }
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700117 ret[pointer] = nullptr;
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700118 return ret;
119}
120
121class ScopedFreeArgPointer {
122 public:
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700123 explicit ScopedFreeArgPointer(char** arr) : arr_(arr) {}
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700124 ~ScopedFreeArgPointer() {
125 if (!arr_)
126 return;
127 for (int i = 0; arr_[i]; i++)
128 free(arr_[i]);
129 delete[] arr_;
130 }
131 private:
132 char** arr_;
133 DISALLOW_COPY_AND_ASSIGN(ScopedFreeArgPointer);
134};
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700135} // namespace
adlr@google.com3defe6a2009-12-04 20:57:17 +0000136
Darin Petkov85d02b72011-05-17 13:25:51 -0700137uint32_t Subprocess::Exec(const vector<string>& cmd,
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700138 ExecCallback callback,
139 void* p) {
Alex Deymo29b81532015-07-09 11:51:49 -0700140 return ExecFlags(cmd, static_cast<GSpawnFlags>(0), true, callback, p);
141}
142
143uint32_t Subprocess::ExecFlags(const vector<string>& cmd,
144 GSpawnFlags flags,
145 bool redirect_stderr_to_stdout,
146 ExecCallback callback,
147 void* p) {
148 unique_ptr<gchar*, utils::GLibStrvFreeDeleter> argv(
149 utils::StringVectorToGStrv(cmd));
Andrew de los Reyes08c4e272010-04-15 14:02:17 -0700150
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700151 char** argp = ArgPointer();
Chris Masonec6c57a52010-09-23 13:06:14 -0700152 if (!argp) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700153 FreeArgvInError(argv.get()); // null in argv[i] terminates argv.
Chris Masonec6c57a52010-09-23 13:06:14 -0700154 return 0;
155 }
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700156 ScopedFreeArgPointer argp_free(argp);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000157
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800158 shared_ptr<SubprocessRecord> record(new SubprocessRecord);
159 record->callback = callback;
160 record->callback_data = p;
161 gint stdout_fd = -1;
Alex Deymo3e0b53e2014-08-12 23:12:25 -0700162 GError* error = nullptr;
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800163 bool success = g_spawn_async_with_pipes(
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700164 nullptr, // working directory
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800165 argv.get(),
166 argp,
Alex Deymo29b81532015-07-09 11:51:49 -0700167 static_cast<GSpawnFlags>(flags | G_SPAWN_DO_NOT_REAP_CHILD), // flags
168 // child setup function:
169 redirect_stderr_to_stdout ? GRedirectStderrToStdout : nullptr,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700170 nullptr, // child setup data pointer
Alex Deymo29b81532015-07-09 11:51:49 -0700171 &record->pid,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700172 nullptr,
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800173 &stdout_fd,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700174 nullptr,
Alex Deymo3e0b53e2014-08-12 23:12:25 -0700175 &error);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000176 if (!success) {
Alex Deymo3e0b53e2014-08-12 23:12:25 -0700177 LOG(ERROR) << "g_spawn_async failed: " << utils::GetAndFreeGError(&error);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000178 return 0;
179 }
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800180 record->tag =
Alex Deymo29b81532015-07-09 11:51:49 -0700181 g_child_watch_add(record->pid, GChildExitedCallback, record.get());
182 record->stdout_fd = stdout_fd;
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800183 subprocess_records_[record->tag] = record;
184
Alex Deymo29b81532015-07-09 11:51:49 -0700185 // Capture the subprocess output. Make our end of the pipe non-blocking.
186 int fd_flags = fcntl(stdout_fd, F_GETFL, 0) | O_NONBLOCK;
187 if (HANDLE_EINTR(fcntl(record->stdout_fd, F_SETFL, fd_flags)) < 0) {
188 LOG(ERROR) << "Unable to set non-blocking I/O mode on fd "
189 << record->stdout_fd << ".";
190 }
191
192 record->task_id = MessageLoop::current()->WatchFileDescriptor(
193 FROM_HERE,
194 record->stdout_fd,
195 MessageLoop::WatchMode::kWatchRead,
196 true,
197 base::Bind(&Subprocess::OnStdoutReady, record.get()));
198
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800199 return record->tag;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000200}
201
Alex Deymo29b81532015-07-09 11:51:49 -0700202void Subprocess::KillExec(uint32_t tag) {
203 const auto& record = subprocess_records_.find(tag);
204 if (record == subprocess_records_.end())
205 return;
206 record->second->callback = nullptr;
207 kill(record->second->pid, SIGTERM);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000208}
209
Darin Petkov85d02b72011-05-17 13:25:51 -0700210bool Subprocess::SynchronousExecFlags(const vector<string>& cmd,
211 GSpawnFlags flags,
Andrew de los Reyes5a232832010-10-12 16:20:54 -0700212 int* return_code,
Darin Petkov85d02b72011-05-17 13:25:51 -0700213 string* stdout) {
214 if (stdout) {
215 *stdout = "";
216 }
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700217 GError* err = nullptr;
Ben Chan02f7c1d2014-10-18 15:18:02 -0700218 unique_ptr<char*[]> argv(new char*[cmd.size() + 1]);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000219 for (unsigned int i = 0; i < cmd.size(); i++) {
220 argv[i] = strdup(cmd[i].c_str());
Chris Masonec6c57a52010-09-23 13:06:14 -0700221 if (!argv[i]) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700222 FreeArgvInError(argv.get()); // null in argv[i] terminates argv.
Chris Masonec6c57a52010-09-23 13:06:14 -0700223 return false;
224 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000225 }
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700226 argv[cmd.size()] = nullptr;
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700227
228 char** argp = ArgPointer();
Chris Masonec6c57a52010-09-23 13:06:14 -0700229 if (!argp) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700230 FreeArgvInError(argv.get()); // null in argv[i] terminates argv.
Chris Masonec6c57a52010-09-23 13:06:14 -0700231 return false;
232 }
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700233 ScopedFreeArgPointer argp_free(argp);
234
235 char* child_stdout;
Andrew de los Reyes5a232832010-10-12 16:20:54 -0700236 bool success = g_spawn_sync(
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700237 nullptr, // working directory
Andrew de los Reyes5a232832010-10-12 16:20:54 -0700238 argv.get(),
239 argp,
Andrew de los Reyes50f36492010-11-01 13:57:12 -0700240 static_cast<GSpawnFlags>(G_SPAWN_STDERR_TO_DEV_NULL |
241 G_SPAWN_SEARCH_PATH | flags), // flags
Andrew de los Reyes5a232832010-10-12 16:20:54 -0700242 GRedirectStderrToStdout, // child setup function
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700243 nullptr, // data for child setup function
Andrew de los Reyes5a232832010-10-12 16:20:54 -0700244 &child_stdout,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700245 nullptr,
Andrew de los Reyes5a232832010-10-12 16:20:54 -0700246 return_code,
247 &err);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000248 FreeArgv(argv.get());
Darin Petkova0b9e772011-10-06 05:05:56 -0700249 LOG_IF(INFO, err) << utils::GetAndFreeGError(&err);
Darin Petkov478435e2011-05-17 11:46:31 -0700250 if (child_stdout) {
Darin Petkov85d02b72011-05-17 13:25:51 -0700251 if (stdout) {
252 *stdout = child_stdout;
253 } else if (*child_stdout) {
Darin Petkov478435e2011-05-17 11:46:31 -0700254 LOG(INFO) << "Subprocess output:\n" << child_stdout;
255 }
256 g_free(child_stdout);
257 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000258 return success;
259}
260
Alex Deymof329b932014-10-30 01:37:48 -0700261bool Subprocess::SynchronousExec(const vector<string>& cmd,
Darin Petkov85d02b72011-05-17 13:25:51 -0700262 int* return_code,
Alex Deymof329b932014-10-30 01:37:48 -0700263 string* stdout) {
Darin Petkov85d02b72011-05-17 13:25:51 -0700264 return SynchronousExecFlags(cmd,
265 static_cast<GSpawnFlags>(0),
266 return_code,
267 stdout);
268}
269
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800270bool Subprocess::SubprocessInFlight() {
Alex Deymo29b81532015-07-09 11:51:49 -0700271 for (const auto& tag_record_pair : subprocess_records_) {
272 if (tag_record_pair.second->callback)
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800273 return true;
274 }
275 return false;
276}
277
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700278Subprocess* Subprocess::subprocess_singleton_ = nullptr;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000279
280} // namespace chromeos_update_engine