OLD | NEW |
1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "update_engine/subprocess.h" | 5 #include "update_engine/subprocess.h" |
6 #include <stdlib.h> | 6 #include <stdlib.h> |
7 #include <string.h> | 7 #include <string.h> |
8 #include <string> | 8 #include <string> |
9 #include <unistd.h> | 9 #include <unistd.h> |
10 #include <vector> | 10 #include <vector> |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 callback_records_[*tag] = callback_record; | 128 callback_records_[*tag] = callback_record; |
129 return *tag; | 129 return *tag; |
130 } | 130 } |
131 | 131 |
132 void Subprocess::CancelExec(uint32_t tag) { | 132 void Subprocess::CancelExec(uint32_t tag) { |
133 if (callback_records_[tag].callback) { | 133 if (callback_records_[tag].callback) { |
134 callback_records_[tag].callback = NULL; | 134 callback_records_[tag].callback = NULL; |
135 } | 135 } |
136 } | 136 } |
137 | 137 |
138 bool Subprocess::SynchronousExec(const std::vector<std::string>& cmd, | 138 bool Subprocess::SynchronousExecFlags(const std::vector<std::string>& cmd, |
139 int* return_code) { | 139 int* return_code, |
| 140 GSpawnFlags flags) { |
140 GError* err = NULL; | 141 GError* err = NULL; |
141 scoped_array<char*> argv(new char*[cmd.size() + 1]); | 142 scoped_array<char*> argv(new char*[cmd.size() + 1]); |
142 for (unsigned int i = 0; i < cmd.size(); i++) { | 143 for (unsigned int i = 0; i < cmd.size(); i++) { |
143 argv[i] = strdup(cmd[i].c_str()); | 144 argv[i] = strdup(cmd[i].c_str()); |
144 if (!argv[i]) { | 145 if (!argv[i]) { |
145 FreeArgvInError(argv.get()); // NULL in argv[i] terminates argv. | 146 FreeArgvInError(argv.get()); // NULL in argv[i] terminates argv. |
146 return false; | 147 return false; |
147 } | 148 } |
148 } | 149 } |
149 argv[cmd.size()] = NULL; | 150 argv[cmd.size()] = NULL; |
150 | 151 |
151 char** argp = ArgPointer(); | 152 char** argp = ArgPointer(); |
152 if (!argp) { | 153 if (!argp) { |
153 FreeArgvInError(argv.get()); // NULL in argv[i] terminates argv. | 154 FreeArgvInError(argv.get()); // NULL in argv[i] terminates argv. |
154 return false; | 155 return false; |
155 } | 156 } |
156 ScopedFreeArgPointer argp_free(argp); | 157 ScopedFreeArgPointer argp_free(argp); |
157 | 158 |
158 char* child_stdout; | 159 char* child_stdout; |
159 | 160 |
160 bool success = g_spawn_sync(NULL, // working directory | 161 bool success = g_spawn_sync( |
161 argv.get(), | 162 NULL, // working directory |
162 argp, | 163 argv.get(), |
163 G_SPAWN_STDERR_TO_DEV_NULL, // flags | 164 argp, |
164 GRedirectStderrToStdout, // child setup function | 165 static_cast<GSpawnFlags>(G_SPAWN_STDERR_TO_DEV_NULL | flags), // flags |
165 NULL, // data for child setup function | 166 GRedirectStderrToStdout, // child setup function |
166 &child_stdout, | 167 NULL, // data for child setup function |
167 NULL, | 168 &child_stdout, |
168 return_code, | 169 NULL, |
169 &err); | 170 return_code, |
| 171 &err); |
170 FreeArgv(argv.get()); | 172 FreeArgv(argv.get()); |
171 if (err) | 173 if (err) |
172 LOG(INFO) << "err is: " << err->code << ", " << err->message; | 174 LOG(INFO) << "err is: " << err->code << ", " << err->message; |
173 if (child_stdout && strlen(child_stdout)) | 175 if (child_stdout && strlen(child_stdout)) |
174 LOG(INFO) << "Subprocess output:\n" << child_stdout; | 176 LOG(INFO) << "Subprocess output:\n" << child_stdout; |
175 return success; | 177 return success; |
176 } | 178 } |
177 | 179 |
178 Subprocess* Subprocess::subprocess_singleton_ = NULL; | 180 Subprocess* Subprocess::subprocess_singleton_ = NULL; |
179 | 181 |
180 } // namespace chromeos_update_engine | 182 } // namespace chromeos_update_engine |
OLD | NEW |