Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 2 | // 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 Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 6 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
Kenneth Waters | a7fcafa | 2010-09-21 10:27:03 -0700 | [diff] [blame] | 9 | #include <unistd.h> |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 10 | |
| 11 | #include <string> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 12 | #include <vector> |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 13 | |
| 14 | #include <base/logging.h> |
| 15 | #include <base/memory/scoped_ptr.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 16 | #include <base/strings/string_util.h> |
| 17 | #include <base/strings/stringprintf.h> |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 18 | |
| 19 | #include "update_engine/utils.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 20 | |
Alex Deymo | bc91a27 | 2014-05-20 16:45:33 -0700 | [diff] [blame] | 21 | using std::shared_ptr; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 22 | using std::string; |
| 23 | using std::vector; |
| 24 | |
| 25 | namespace chromeos_update_engine { |
| 26 | |
| 27 | void Subprocess::GChildExitedCallback(GPid pid, gint status, gpointer data) { |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 28 | SubprocessRecord* record = reinterpret_cast<SubprocessRecord*>(data); |
| 29 | |
| 30 | // Make sure we read any remaining process output. Then close the pipe. |
| 31 | GStdoutWatchCallback(record->gioout, G_IO_IN, &record->stdout); |
| 32 | int fd = g_io_channel_unix_get_fd(record->gioout); |
| 33 | g_source_remove(record->gioout_tag); |
| 34 | g_io_channel_unref(record->gioout); |
| 35 | close(fd); |
| 36 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 37 | g_spawn_close_pid(pid); |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 38 | gint use_status = status; |
| 39 | if (WIFEXITED(status)) |
| 40 | use_status = WEXITSTATUS(status); |
| 41 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 42 | if (status) { |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 43 | LOG(INFO) << "Subprocess status: " << use_status; |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 44 | } |
| 45 | if (!record->stdout.empty()) { |
| 46 | LOG(INFO) << "Subprocess output:\n" << record->stdout; |
| 47 | } |
| 48 | if (record->callback) { |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 49 | record->callback(use_status, record->stdout, record->callback_data); |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 50 | } |
| 51 | Get().subprocess_records_.erase(record->tag); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Kenneth Waters | a7fcafa | 2010-09-21 10:27:03 -0700 | [diff] [blame] | 54 | void Subprocess::GRedirectStderrToStdout(gpointer user_data) { |
| 55 | dup2(1, 2); |
| 56 | } |
| 57 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 58 | gboolean Subprocess::GStdoutWatchCallback(GIOChannel* source, |
| 59 | GIOCondition condition, |
| 60 | gpointer data) { |
| 61 | string* stdout = reinterpret_cast<string*>(data); |
| 62 | char buf[1024]; |
| 63 | gsize bytes_read; |
| 64 | while (g_io_channel_read_chars(source, |
| 65 | buf, |
| 66 | arraysize(buf), |
| 67 | &bytes_read, |
| 68 | NULL) == G_IO_STATUS_NORMAL && |
| 69 | bytes_read > 0) { |
| 70 | stdout->append(buf, bytes_read); |
| 71 | } |
| 72 | return TRUE; // Keep the callback source. It's freed in GChilExitedCallback. |
| 73 | } |
| 74 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 75 | namespace { |
| 76 | void FreeArgv(char** argv) { |
| 77 | for (int i = 0; argv[i]; i++) { |
| 78 | free(argv[i]); |
| 79 | argv[i] = NULL; |
| 80 | } |
| 81 | } |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 82 | |
Chris Masone | c6c57a5 | 2010-09-23 13:06:14 -0700 | [diff] [blame] | 83 | void FreeArgvInError(char** argv) { |
| 84 | FreeArgv(argv); |
| 85 | LOG(ERROR) << "Ran out of memory copying args."; |
| 86 | } |
| 87 | |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 88 | // Note: Caller responsible for free()ing the returned value! |
Chris Masone | c6c57a5 | 2010-09-23 13:06:14 -0700 | [diff] [blame] | 89 | // Will return NULL on failure and free any allocated memory. |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 90 | char** ArgPointer() { |
| 91 | const char* keys[] = {"LD_LIBRARY_PATH", "PATH"}; |
| 92 | char** ret = new char*[arraysize(keys) + 1]; |
| 93 | int pointer = 0; |
| 94 | for (size_t i = 0; i < arraysize(keys); i++) { |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 95 | if (getenv(keys[i])) { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 96 | ret[pointer] = strdup(base::StringPrintf("%s=%s", keys[i], |
| 97 | getenv(keys[i])).c_str()); |
Chris Masone | c6c57a5 | 2010-09-23 13:06:14 -0700 | [diff] [blame] | 98 | if (!ret[pointer]) { |
| 99 | FreeArgv(ret); |
| 100 | delete [] ret; |
| 101 | return NULL; |
| 102 | } |
| 103 | ++pointer; |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 104 | } |
| 105 | } |
Chris Masone | c6c57a5 | 2010-09-23 13:06:14 -0700 | [diff] [blame] | 106 | ret[pointer] = NULL; |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 107 | return ret; |
| 108 | } |
| 109 | |
| 110 | class ScopedFreeArgPointer { |
| 111 | public: |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame^] | 112 | explicit ScopedFreeArgPointer(char** arr) : arr_(arr) {} |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 113 | ~ScopedFreeArgPointer() { |
| 114 | if (!arr_) |
| 115 | return; |
| 116 | for (int i = 0; arr_[i]; i++) |
| 117 | free(arr_[i]); |
| 118 | delete[] arr_; |
| 119 | } |
| 120 | private: |
| 121 | char** arr_; |
| 122 | DISALLOW_COPY_AND_ASSIGN(ScopedFreeArgPointer); |
| 123 | }; |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame^] | 124 | } // namespace |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 125 | |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 126 | uint32_t Subprocess::Exec(const vector<string>& cmd, |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 127 | ExecCallback callback, |
| 128 | void* p) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 129 | GPid child_pid; |
Ben Chan | 8303a86 | 2014-01-27 23:24:02 -0800 | [diff] [blame] | 130 | scoped_ptr<char*[]> argv(new char*[cmd.size() + 1]); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 131 | for (unsigned int i = 0; i < cmd.size(); i++) { |
| 132 | argv[i] = strdup(cmd[i].c_str()); |
Chris Masone | c6c57a5 | 2010-09-23 13:06:14 -0700 | [diff] [blame] | 133 | if (!argv[i]) { |
| 134 | FreeArgvInError(argv.get()); // NULL in argv[i] terminates argv. |
| 135 | return 0; |
| 136 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 137 | } |
| 138 | argv[cmd.size()] = NULL; |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 139 | |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 140 | char** argp = ArgPointer(); |
Chris Masone | c6c57a5 | 2010-09-23 13:06:14 -0700 | [diff] [blame] | 141 | if (!argp) { |
| 142 | FreeArgvInError(argv.get()); // NULL in argv[i] terminates argv. |
| 143 | return 0; |
| 144 | } |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 145 | ScopedFreeArgPointer argp_free(argp); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 146 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 147 | shared_ptr<SubprocessRecord> record(new SubprocessRecord); |
| 148 | record->callback = callback; |
| 149 | record->callback_data = p; |
| 150 | gint stdout_fd = -1; |
| 151 | bool success = g_spawn_async_with_pipes( |
| 152 | NULL, // working directory |
| 153 | argv.get(), |
| 154 | argp, |
| 155 | G_SPAWN_DO_NOT_REAP_CHILD, // flags |
| 156 | GRedirectStderrToStdout, // child setup function |
| 157 | NULL, // child setup data pointer |
| 158 | &child_pid, |
| 159 | NULL, |
| 160 | &stdout_fd, |
| 161 | NULL, |
Darin Petkov | 478435e | 2011-05-17 11:46:31 -0700 | [diff] [blame] | 162 | NULL); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 163 | FreeArgv(argv.get()); |
| 164 | if (!success) { |
| 165 | LOG(ERROR) << "g_spawn_async failed"; |
| 166 | return 0; |
| 167 | } |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 168 | record->tag = |
| 169 | g_child_watch_add(child_pid, GChildExitedCallback, record.get()); |
| 170 | subprocess_records_[record->tag] = record; |
| 171 | |
| 172 | // Capture the subprocess output. |
| 173 | record->gioout = g_io_channel_unix_new(stdout_fd); |
| 174 | g_io_channel_set_encoding(record->gioout, NULL, NULL); |
| 175 | LOG_IF(WARNING, |
| 176 | g_io_channel_set_flags(record->gioout, G_IO_FLAG_NONBLOCK, NULL) != |
| 177 | G_IO_STATUS_NORMAL) << "Unable to set non-blocking I/O mode."; |
| 178 | record->gioout_tag = g_io_add_watch( |
| 179 | record->gioout, |
| 180 | static_cast<GIOCondition>(G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP), |
| 181 | GStdoutWatchCallback, |
| 182 | &record->stdout); |
| 183 | return record->tag; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 186 | void Subprocess::CancelExec(uint32_t tag) { |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 187 | subprocess_records_[tag]->callback = NULL; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 190 | bool Subprocess::SynchronousExecFlags(const vector<string>& cmd, |
| 191 | GSpawnFlags flags, |
Andrew de los Reyes | 5a23283 | 2010-10-12 16:20:54 -0700 | [diff] [blame] | 192 | int* return_code, |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 193 | string* stdout) { |
| 194 | if (stdout) { |
| 195 | *stdout = ""; |
| 196 | } |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 197 | GError* err = NULL; |
Ben Chan | 8303a86 | 2014-01-27 23:24:02 -0800 | [diff] [blame] | 198 | scoped_ptr<char*[]> argv(new char*[cmd.size() + 1]); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 199 | for (unsigned int i = 0; i < cmd.size(); i++) { |
| 200 | argv[i] = strdup(cmd[i].c_str()); |
Chris Masone | c6c57a5 | 2010-09-23 13:06:14 -0700 | [diff] [blame] | 201 | if (!argv[i]) { |
| 202 | FreeArgvInError(argv.get()); // NULL in argv[i] terminates argv. |
| 203 | return false; |
| 204 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 205 | } |
| 206 | argv[cmd.size()] = NULL; |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 207 | |
| 208 | char** argp = ArgPointer(); |
Chris Masone | c6c57a5 | 2010-09-23 13:06:14 -0700 | [diff] [blame] | 209 | if (!argp) { |
| 210 | FreeArgvInError(argv.get()); // NULL in argv[i] terminates argv. |
| 211 | return false; |
| 212 | } |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 213 | ScopedFreeArgPointer argp_free(argp); |
| 214 | |
| 215 | char* child_stdout; |
Andrew de los Reyes | 5a23283 | 2010-10-12 16:20:54 -0700 | [diff] [blame] | 216 | bool success = g_spawn_sync( |
| 217 | NULL, // working directory |
| 218 | argv.get(), |
| 219 | argp, |
Andrew de los Reyes | 50f3649 | 2010-11-01 13:57:12 -0700 | [diff] [blame] | 220 | static_cast<GSpawnFlags>(G_SPAWN_STDERR_TO_DEV_NULL | |
| 221 | G_SPAWN_SEARCH_PATH | flags), // flags |
Andrew de los Reyes | 5a23283 | 2010-10-12 16:20:54 -0700 | [diff] [blame] | 222 | GRedirectStderrToStdout, // child setup function |
| 223 | NULL, // data for child setup function |
| 224 | &child_stdout, |
| 225 | NULL, |
| 226 | return_code, |
| 227 | &err); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 228 | FreeArgv(argv.get()); |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 229 | LOG_IF(INFO, err) << utils::GetAndFreeGError(&err); |
Darin Petkov | 478435e | 2011-05-17 11:46:31 -0700 | [diff] [blame] | 230 | if (child_stdout) { |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 231 | if (stdout) { |
| 232 | *stdout = child_stdout; |
| 233 | } else if (*child_stdout) { |
Darin Petkov | 478435e | 2011-05-17 11:46:31 -0700 | [diff] [blame] | 234 | LOG(INFO) << "Subprocess output:\n" << child_stdout; |
| 235 | } |
| 236 | g_free(child_stdout); |
| 237 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 238 | return success; |
| 239 | } |
| 240 | |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 241 | bool Subprocess::SynchronousExec(const std::vector<std::string>& cmd, |
| 242 | int* return_code, |
| 243 | std::string* stdout) { |
| 244 | return SynchronousExecFlags(cmd, |
| 245 | static_cast<GSpawnFlags>(0), |
| 246 | return_code, |
| 247 | stdout); |
| 248 | } |
| 249 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 250 | bool Subprocess::SubprocessInFlight() { |
| 251 | for (std::map<int, shared_ptr<SubprocessRecord> >::iterator it = |
| 252 | subprocess_records_.begin(); |
| 253 | it != subprocess_records_.end(); ++it) { |
| 254 | if (it->second->callback) |
| 255 | return true; |
| 256 | } |
| 257 | return false; |
| 258 | } |
| 259 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 260 | Subprocess* Subprocess::subprocess_singleton_ = NULL; |
| 261 | |
| 262 | } // namespace chromeos_update_engine |