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 | |
Alex Deymo | 44666f9 | 2014-07-22 20:29:24 -0700 | [diff] [blame] | 19 | #include "update_engine/glib_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, |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame^] | 68 | nullptr) == G_IO_STATUS_NORMAL && |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 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]); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame^] | 79 | argv[i] = nullptr; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 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! |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -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; |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame^] | 101 | return nullptr; |
Chris Masone | c6c57a5 | 2010-09-23 13:06:14 -0700 | [diff] [blame] | 102 | } |
| 103 | ++pointer; |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 104 | } |
| 105 | } |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame^] | 106 | ret[pointer] = nullptr; |
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]) { |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame^] | 134 | FreeArgvInError(argv.get()); // null in argv[i] terminates argv. |
Chris Masone | c6c57a5 | 2010-09-23 13:06:14 -0700 | [diff] [blame] | 135 | return 0; |
| 136 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 137 | } |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame^] | 138 | argv[cmd.size()] = nullptr; |
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) { |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame^] | 142 | FreeArgvInError(argv.get()); // null in argv[i] terminates argv. |
Chris Masone | c6c57a5 | 2010-09-23 13:06:14 -0700 | [diff] [blame] | 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; |
Alex Deymo | 3e0b53e | 2014-08-12 23:12:25 -0700 | [diff] [blame] | 151 | GError* error = nullptr; |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 152 | bool success = g_spawn_async_with_pipes( |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame^] | 153 | nullptr, // working directory |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 154 | argv.get(), |
| 155 | argp, |
| 156 | G_SPAWN_DO_NOT_REAP_CHILD, // flags |
| 157 | GRedirectStderrToStdout, // child setup function |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame^] | 158 | nullptr, // child setup data pointer |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 159 | &child_pid, |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame^] | 160 | nullptr, |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 161 | &stdout_fd, |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame^] | 162 | nullptr, |
Alex Deymo | 3e0b53e | 2014-08-12 23:12:25 -0700 | [diff] [blame] | 163 | &error); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 164 | FreeArgv(argv.get()); |
| 165 | if (!success) { |
Alex Deymo | 3e0b53e | 2014-08-12 23:12:25 -0700 | [diff] [blame] | 166 | LOG(ERROR) << "g_spawn_async failed: " << utils::GetAndFreeGError(&error); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 167 | return 0; |
| 168 | } |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 169 | record->tag = |
| 170 | g_child_watch_add(child_pid, GChildExitedCallback, record.get()); |
| 171 | subprocess_records_[record->tag] = record; |
| 172 | |
| 173 | // Capture the subprocess output. |
| 174 | record->gioout = g_io_channel_unix_new(stdout_fd); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame^] | 175 | g_io_channel_set_encoding(record->gioout, nullptr, nullptr); |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 176 | LOG_IF(WARNING, |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame^] | 177 | g_io_channel_set_flags(record->gioout, G_IO_FLAG_NONBLOCK, nullptr) != |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 178 | G_IO_STATUS_NORMAL) << "Unable to set non-blocking I/O mode."; |
| 179 | record->gioout_tag = g_io_add_watch( |
| 180 | record->gioout, |
| 181 | static_cast<GIOCondition>(G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP), |
| 182 | GStdoutWatchCallback, |
| 183 | &record->stdout); |
| 184 | return record->tag; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 187 | void Subprocess::CancelExec(uint32_t tag) { |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame^] | 188 | subprocess_records_[tag]->callback = nullptr; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 191 | bool Subprocess::SynchronousExecFlags(const vector<string>& cmd, |
| 192 | GSpawnFlags flags, |
Andrew de los Reyes | 5a23283 | 2010-10-12 16:20:54 -0700 | [diff] [blame] | 193 | int* return_code, |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 194 | string* stdout) { |
| 195 | if (stdout) { |
| 196 | *stdout = ""; |
| 197 | } |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame^] | 198 | GError* err = nullptr; |
Ben Chan | 8303a86 | 2014-01-27 23:24:02 -0800 | [diff] [blame] | 199 | scoped_ptr<char*[]> argv(new char*[cmd.size() + 1]); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 200 | for (unsigned int i = 0; i < cmd.size(); i++) { |
| 201 | argv[i] = strdup(cmd[i].c_str()); |
Chris Masone | c6c57a5 | 2010-09-23 13:06:14 -0700 | [diff] [blame] | 202 | if (!argv[i]) { |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame^] | 203 | FreeArgvInError(argv.get()); // null in argv[i] terminates argv. |
Chris Masone | c6c57a5 | 2010-09-23 13:06:14 -0700 | [diff] [blame] | 204 | return false; |
| 205 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 206 | } |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame^] | 207 | argv[cmd.size()] = nullptr; |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 208 | |
| 209 | char** argp = ArgPointer(); |
Chris Masone | c6c57a5 | 2010-09-23 13:06:14 -0700 | [diff] [blame] | 210 | if (!argp) { |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame^] | 211 | FreeArgvInError(argv.get()); // null in argv[i] terminates argv. |
Chris Masone | c6c57a5 | 2010-09-23 13:06:14 -0700 | [diff] [blame] | 212 | return false; |
| 213 | } |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 214 | ScopedFreeArgPointer argp_free(argp); |
| 215 | |
| 216 | char* child_stdout; |
Andrew de los Reyes | 5a23283 | 2010-10-12 16:20:54 -0700 | [diff] [blame] | 217 | bool success = g_spawn_sync( |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame^] | 218 | nullptr, // working directory |
Andrew de los Reyes | 5a23283 | 2010-10-12 16:20:54 -0700 | [diff] [blame] | 219 | argv.get(), |
| 220 | argp, |
Andrew de los Reyes | 50f3649 | 2010-11-01 13:57:12 -0700 | [diff] [blame] | 221 | static_cast<GSpawnFlags>(G_SPAWN_STDERR_TO_DEV_NULL | |
| 222 | G_SPAWN_SEARCH_PATH | flags), // flags |
Andrew de los Reyes | 5a23283 | 2010-10-12 16:20:54 -0700 | [diff] [blame] | 223 | GRedirectStderrToStdout, // child setup function |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame^] | 224 | nullptr, // data for child setup function |
Andrew de los Reyes | 5a23283 | 2010-10-12 16:20:54 -0700 | [diff] [blame] | 225 | &child_stdout, |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame^] | 226 | nullptr, |
Andrew de los Reyes | 5a23283 | 2010-10-12 16:20:54 -0700 | [diff] [blame] | 227 | return_code, |
| 228 | &err); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 229 | FreeArgv(argv.get()); |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 230 | LOG_IF(INFO, err) << utils::GetAndFreeGError(&err); |
Darin Petkov | 478435e | 2011-05-17 11:46:31 -0700 | [diff] [blame] | 231 | if (child_stdout) { |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 232 | if (stdout) { |
| 233 | *stdout = child_stdout; |
| 234 | } else if (*child_stdout) { |
Darin Petkov | 478435e | 2011-05-17 11:46:31 -0700 | [diff] [blame] | 235 | LOG(INFO) << "Subprocess output:\n" << child_stdout; |
| 236 | } |
| 237 | g_free(child_stdout); |
| 238 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 239 | return success; |
| 240 | } |
| 241 | |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 242 | bool Subprocess::SynchronousExec(const std::vector<std::string>& cmd, |
| 243 | int* return_code, |
| 244 | std::string* stdout) { |
| 245 | return SynchronousExecFlags(cmd, |
| 246 | static_cast<GSpawnFlags>(0), |
| 247 | return_code, |
| 248 | stdout); |
| 249 | } |
| 250 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 251 | bool Subprocess::SubprocessInFlight() { |
| 252 | for (std::map<int, shared_ptr<SubprocessRecord> >::iterator it = |
| 253 | subprocess_records_.begin(); |
| 254 | it != subprocess_records_.end(); ++it) { |
| 255 | if (it->second->callback) |
| 256 | return true; |
| 257 | } |
| 258 | return false; |
| 259 | } |
| 260 | |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame^] | 261 | Subprocess* Subprocess::subprocess_singleton_ = nullptr; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 262 | |
| 263 | } // namespace chromeos_update_engine |