adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 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" |
| 6 | #include <stdlib.h> |
| 7 | #include <string.h> |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | #include "chromeos/obsolete_logging.h" |
| 11 | #include "base/scoped_ptr.h" |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 12 | #include "base/string_util.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 13 | |
| 14 | using std::string; |
| 15 | using std::vector; |
| 16 | |
| 17 | namespace chromeos_update_engine { |
| 18 | |
| 19 | void Subprocess::GChildExitedCallback(GPid pid, gint status, gpointer data) { |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 20 | COMPILE_ASSERT(sizeof(guint) == sizeof(uint32_t), |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 21 | guint_uint32_size_mismatch); |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 22 | guint* tag = reinterpret_cast<guint*>(data); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 23 | const SubprocessCallbackRecord& record = Get().callback_records_[*tag]; |
| 24 | if (record.callback) |
| 25 | record.callback(status, record.callback_data); |
| 26 | g_spawn_close_pid(pid); |
| 27 | Get().callback_records_.erase(*tag); |
| 28 | delete tag; |
| 29 | } |
| 30 | |
| 31 | namespace { |
| 32 | void FreeArgv(char** argv) { |
| 33 | for (int i = 0; argv[i]; i++) { |
| 34 | free(argv[i]); |
| 35 | argv[i] = NULL; |
| 36 | } |
| 37 | } |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame^] | 38 | |
| 39 | // Note: Caller responsible for free()ing the returned value! |
| 40 | char** ArgPointer() { |
| 41 | const char* keys[] = {"LD_LIBRARY_PATH", "PATH"}; |
| 42 | char** ret = new char*[arraysize(keys) + 1]; |
| 43 | int pointer = 0; |
| 44 | for (size_t i = 0; i < arraysize(keys); i++) { |
| 45 | ret[i] = NULL; |
| 46 | if (getenv(keys[i])) { |
| 47 | ret[pointer] = strdup(StringPrintf("%s=%s", keys[i], |
| 48 | getenv(keys[i])).c_str()); |
| 49 | pointer++; |
| 50 | } |
| 51 | } |
| 52 | return ret; |
| 53 | } |
| 54 | |
| 55 | class ScopedFreeArgPointer { |
| 56 | public: |
| 57 | ScopedFreeArgPointer(char** arr) : arr_(arr) {} |
| 58 | ~ScopedFreeArgPointer() { |
| 59 | if (!arr_) |
| 60 | return; |
| 61 | for (int i = 0; arr_[i]; i++) |
| 62 | free(arr_[i]); |
| 63 | delete[] arr_; |
| 64 | } |
| 65 | private: |
| 66 | char** arr_; |
| 67 | DISALLOW_COPY_AND_ASSIGN(ScopedFreeArgPointer); |
| 68 | }; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 69 | } // namespace {} |
| 70 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 71 | uint32_t Subprocess::Exec(const std::vector<std::string>& cmd, |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame^] | 72 | ExecCallback callback, |
| 73 | void* p) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 74 | GPid child_pid; |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 75 | GError* err; |
| 76 | scoped_array<char*> argv(new char*[cmd.size() + 1]); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 77 | for (unsigned int i = 0; i < cmd.size(); i++) { |
| 78 | argv[i] = strdup(cmd[i].c_str()); |
| 79 | } |
| 80 | argv[cmd.size()] = NULL; |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 81 | |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame^] | 82 | char** argp = ArgPointer(); |
| 83 | ScopedFreeArgPointer argp_free(argp); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 84 | |
| 85 | SubprocessCallbackRecord callback_record; |
| 86 | callback_record.callback = callback; |
| 87 | callback_record.callback_data = p; |
| 88 | |
| 89 | bool success = g_spawn_async(NULL, // working directory |
| 90 | argv.get(), |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame^] | 91 | argp, |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 92 | G_SPAWN_DO_NOT_REAP_CHILD, // flags |
| 93 | NULL, // child setup function |
| 94 | NULL, // child setup data pointer |
| 95 | &child_pid, |
| 96 | &err); |
| 97 | FreeArgv(argv.get()); |
| 98 | if (!success) { |
| 99 | LOG(ERROR) << "g_spawn_async failed"; |
| 100 | return 0; |
| 101 | } |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 102 | guint* tag = new guint; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 103 | *tag = g_child_watch_add(child_pid, GChildExitedCallback, tag); |
| 104 | callback_records_[*tag] = callback_record; |
| 105 | return *tag; |
| 106 | } |
| 107 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 108 | void Subprocess::CancelExec(uint32_t tag) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 109 | if (callback_records_[tag].callback) { |
| 110 | callback_records_[tag].callback = NULL; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | bool Subprocess::SynchronousExec(const std::vector<std::string>& cmd, |
| 115 | int* return_code) { |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 116 | GError* err = NULL; |
| 117 | scoped_array<char*> argv(new char*[cmd.size() + 1]); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 118 | for (unsigned int i = 0; i < cmd.size(); i++) { |
| 119 | argv[i] = strdup(cmd[i].c_str()); |
| 120 | } |
| 121 | argv[cmd.size()] = NULL; |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame^] | 122 | |
| 123 | char** argp = ArgPointer(); |
| 124 | ScopedFreeArgPointer argp_free(argp); |
| 125 | |
| 126 | char* child_stdout; |
| 127 | char* child_stderr; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 128 | |
| 129 | bool success = g_spawn_sync(NULL, // working directory |
| 130 | argv.get(), |
| 131 | argp, |
| 132 | static_cast<GSpawnFlags>(NULL), // flags |
| 133 | NULL, // child setup function |
| 134 | NULL, // data for child setup function |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame^] | 135 | &child_stdout, |
| 136 | &child_stderr, |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 137 | return_code, |
| 138 | &err); |
| 139 | FreeArgv(argv.get()); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 140 | if (err) |
| 141 | LOG(INFO) << "err is: " << err->code << ", " << err->message; |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame^] | 142 | if (child_stdout) |
| 143 | LOG(INFO) << "Postinst stdout:" << child_stdout; |
| 144 | if (child_stderr) |
| 145 | LOG(INFO) << "Postinst stderr:" << child_stderr; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 146 | return success; |
| 147 | } |
| 148 | |
| 149 | Subprocess* Subprocess::subprocess_singleton_ = NULL; |
| 150 | |
| 151 | } // namespace chromeos_update_engine |