blob: 6a346411cc1f52cb595aa2f53b68ad29942ee502 [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 }
75 } else {
76 record->stdout.append(buf, rc);
77 }
78 } while (rc > 0);
Darin Petkov6f03a3b2010-11-10 14:27:14 -080079}
80
adlr@google.com3defe6a2009-12-04 20:57:17 +000081namespace {
82void FreeArgv(char** argv) {
83 for (int i = 0; argv[i]; i++) {
84 free(argv[i]);
Alex Vakulenko88b591f2014-08-28 16:48:57 -070085 argv[i] = nullptr;
adlr@google.com3defe6a2009-12-04 20:57:17 +000086 }
87}
Andrew de los Reyes3270f742010-07-15 22:28:14 -070088
Chris Masonec6c57a52010-09-23 13:06:14 -070089void FreeArgvInError(char** argv) {
90 FreeArgv(argv);
91 LOG(ERROR) << "Ran out of memory copying args.";
92}
93
Andrew de los Reyes3270f742010-07-15 22:28:14 -070094// Note: Caller responsible for free()ing the returned value!
Alex Vakulenko88b591f2014-08-28 16:48:57 -070095// Will return null on failure and free any allocated memory.
Andrew de los Reyes3270f742010-07-15 22:28:14 -070096char** ArgPointer() {
97 const char* keys[] = {"LD_LIBRARY_PATH", "PATH"};
98 char** ret = new char*[arraysize(keys) + 1];
99 int pointer = 0;
100 for (size_t i = 0; i < arraysize(keys); i++) {
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700101 if (getenv(keys[i])) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700102 ret[pointer] = strdup(base::StringPrintf("%s=%s", keys[i],
103 getenv(keys[i])).c_str());
Chris Masonec6c57a52010-09-23 13:06:14 -0700104 if (!ret[pointer]) {
105 FreeArgv(ret);
106 delete [] ret;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700107 return nullptr;
Chris Masonec6c57a52010-09-23 13:06:14 -0700108 }
109 ++pointer;
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700110 }
111 }
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700112 ret[pointer] = nullptr;
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700113 return ret;
114}
115
116class ScopedFreeArgPointer {
117 public:
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700118 explicit ScopedFreeArgPointer(char** arr) : arr_(arr) {}
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700119 ~ScopedFreeArgPointer() {
120 if (!arr_)
121 return;
122 for (int i = 0; arr_[i]; i++)
123 free(arr_[i]);
124 delete[] arr_;
125 }
126 private:
127 char** arr_;
128 DISALLOW_COPY_AND_ASSIGN(ScopedFreeArgPointer);
129};
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700130} // namespace
adlr@google.com3defe6a2009-12-04 20:57:17 +0000131
Darin Petkov85d02b72011-05-17 13:25:51 -0700132uint32_t Subprocess::Exec(const vector<string>& cmd,
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700133 ExecCallback callback,
134 void* p) {
Alex Deymo29b81532015-07-09 11:51:49 -0700135 return ExecFlags(cmd, static_cast<GSpawnFlags>(0), true, callback, p);
136}
137
138uint32_t Subprocess::ExecFlags(const vector<string>& cmd,
139 GSpawnFlags flags,
140 bool redirect_stderr_to_stdout,
141 ExecCallback callback,
142 void* p) {
143 unique_ptr<gchar*, utils::GLibStrvFreeDeleter> argv(
144 utils::StringVectorToGStrv(cmd));
Andrew de los Reyes08c4e272010-04-15 14:02:17 -0700145
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700146 char** argp = ArgPointer();
Chris Masonec6c57a52010-09-23 13:06:14 -0700147 if (!argp) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700148 FreeArgvInError(argv.get()); // null in argv[i] terminates argv.
Chris Masonec6c57a52010-09-23 13:06:14 -0700149 return 0;
150 }
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700151 ScopedFreeArgPointer argp_free(argp);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000152
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800153 shared_ptr<SubprocessRecord> record(new SubprocessRecord);
154 record->callback = callback;
155 record->callback_data = p;
156 gint stdout_fd = -1;
Alex Deymo3e0b53e2014-08-12 23:12:25 -0700157 GError* error = nullptr;
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800158 bool success = g_spawn_async_with_pipes(
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700159 nullptr, // working directory
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800160 argv.get(),
161 argp,
Alex Deymo29b81532015-07-09 11:51:49 -0700162 static_cast<GSpawnFlags>(flags | G_SPAWN_DO_NOT_REAP_CHILD), // flags
163 // child setup function:
164 redirect_stderr_to_stdout ? GRedirectStderrToStdout : nullptr,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700165 nullptr, // child setup data pointer
Alex Deymo29b81532015-07-09 11:51:49 -0700166 &record->pid,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700167 nullptr,
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800168 &stdout_fd,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700169 nullptr,
Alex Deymo3e0b53e2014-08-12 23:12:25 -0700170 &error);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000171 if (!success) {
Alex Deymo3e0b53e2014-08-12 23:12:25 -0700172 LOG(ERROR) << "g_spawn_async failed: " << utils::GetAndFreeGError(&error);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000173 return 0;
174 }
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800175 record->tag =
Alex Deymo29b81532015-07-09 11:51:49 -0700176 g_child_watch_add(record->pid, GChildExitedCallback, record.get());
177 record->stdout_fd = stdout_fd;
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800178 subprocess_records_[record->tag] = record;
179
Alex Deymo29b81532015-07-09 11:51:49 -0700180 // Capture the subprocess output. Make our end of the pipe non-blocking.
181 int fd_flags = fcntl(stdout_fd, F_GETFL, 0) | O_NONBLOCK;
182 if (HANDLE_EINTR(fcntl(record->stdout_fd, F_SETFL, fd_flags)) < 0) {
183 LOG(ERROR) << "Unable to set non-blocking I/O mode on fd "
184 << record->stdout_fd << ".";
185 }
186
187 record->task_id = MessageLoop::current()->WatchFileDescriptor(
188 FROM_HERE,
189 record->stdout_fd,
190 MessageLoop::WatchMode::kWatchRead,
191 true,
192 base::Bind(&Subprocess::OnStdoutReady, record.get()));
193
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800194 return record->tag;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000195}
196
Alex Deymo29b81532015-07-09 11:51:49 -0700197void Subprocess::KillExec(uint32_t tag) {
198 const auto& record = subprocess_records_.find(tag);
199 if (record == subprocess_records_.end())
200 return;
201 record->second->callback = nullptr;
202 kill(record->second->pid, SIGTERM);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000203}
204
Darin Petkov85d02b72011-05-17 13:25:51 -0700205bool Subprocess::SynchronousExecFlags(const vector<string>& cmd,
206 GSpawnFlags flags,
Andrew de los Reyes5a232832010-10-12 16:20:54 -0700207 int* return_code,
Darin Petkov85d02b72011-05-17 13:25:51 -0700208 string* stdout) {
209 if (stdout) {
210 *stdout = "";
211 }
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700212 GError* err = nullptr;
Ben Chan02f7c1d2014-10-18 15:18:02 -0700213 unique_ptr<char*[]> argv(new char*[cmd.size() + 1]);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000214 for (unsigned int i = 0; i < cmd.size(); i++) {
215 argv[i] = strdup(cmd[i].c_str());
Chris Masonec6c57a52010-09-23 13:06:14 -0700216 if (!argv[i]) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700217 FreeArgvInError(argv.get()); // null in argv[i] terminates argv.
Chris Masonec6c57a52010-09-23 13:06:14 -0700218 return false;
219 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000220 }
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700221 argv[cmd.size()] = nullptr;
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700222
223 char** argp = ArgPointer();
Chris Masonec6c57a52010-09-23 13:06:14 -0700224 if (!argp) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700225 FreeArgvInError(argv.get()); // null in argv[i] terminates argv.
Chris Masonec6c57a52010-09-23 13:06:14 -0700226 return false;
227 }
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700228 ScopedFreeArgPointer argp_free(argp);
229
230 char* child_stdout;
Andrew de los Reyes5a232832010-10-12 16:20:54 -0700231 bool success = g_spawn_sync(
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700232 nullptr, // working directory
Andrew de los Reyes5a232832010-10-12 16:20:54 -0700233 argv.get(),
234 argp,
Andrew de los Reyes50f36492010-11-01 13:57:12 -0700235 static_cast<GSpawnFlags>(G_SPAWN_STDERR_TO_DEV_NULL |
236 G_SPAWN_SEARCH_PATH | flags), // flags
Andrew de los Reyes5a232832010-10-12 16:20:54 -0700237 GRedirectStderrToStdout, // child setup function
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700238 nullptr, // data for child setup function
Andrew de los Reyes5a232832010-10-12 16:20:54 -0700239 &child_stdout,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700240 nullptr,
Andrew de los Reyes5a232832010-10-12 16:20:54 -0700241 return_code,
242 &err);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000243 FreeArgv(argv.get());
Darin Petkova0b9e772011-10-06 05:05:56 -0700244 LOG_IF(INFO, err) << utils::GetAndFreeGError(&err);
Darin Petkov478435e2011-05-17 11:46:31 -0700245 if (child_stdout) {
Darin Petkov85d02b72011-05-17 13:25:51 -0700246 if (stdout) {
247 *stdout = child_stdout;
248 } else if (*child_stdout) {
Darin Petkov478435e2011-05-17 11:46:31 -0700249 LOG(INFO) << "Subprocess output:\n" << child_stdout;
250 }
251 g_free(child_stdout);
252 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000253 return success;
254}
255
Alex Deymof329b932014-10-30 01:37:48 -0700256bool Subprocess::SynchronousExec(const vector<string>& cmd,
Darin Petkov85d02b72011-05-17 13:25:51 -0700257 int* return_code,
Alex Deymof329b932014-10-30 01:37:48 -0700258 string* stdout) {
Darin Petkov85d02b72011-05-17 13:25:51 -0700259 return SynchronousExecFlags(cmd,
260 static_cast<GSpawnFlags>(0),
261 return_code,
262 stdout);
263}
264
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800265bool Subprocess::SubprocessInFlight() {
Alex Deymo29b81532015-07-09 11:51:49 -0700266 for (const auto& tag_record_pair : subprocess_records_) {
267 if (tag_record_pair.second->callback)
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800268 return true;
269 }
270 return false;
271}
272
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700273Subprocess* Subprocess::subprocess_singleton_ = nullptr;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000274
275} // namespace chromeos_update_engine