Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: src/platform/update_engine/subprocess.cc

Issue 465067: Missed new files in last commit
Patch Set: Created 11 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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"
12
13 using std::string;
14 using std::vector;
15
16 namespace chromeos_update_engine {
17
18 void Subprocess::GChildExitedCallback(GPid pid, gint status, gpointer data) {
19 COMPILE_ASSERT(sizeof(guint) == sizeof(uint32),
20 guint_uint32_size_mismatch);
21 guint *tag = reinterpret_cast<guint*>(data);
22 const SubprocessCallbackRecord& record = Get().callback_records_[*tag];
23 if (record.callback)
24 record.callback(status, record.callback_data);
25 g_spawn_close_pid(pid);
26 Get().callback_records_.erase(*tag);
27 delete tag;
28 }
29
30 namespace {
31 void FreeArgv(char** argv) {
32 for (int i = 0; argv[i]; i++) {
33 free(argv[i]);
34 argv[i] = NULL;
35 }
36 }
37 } // namespace {}
38
39 uint32 Subprocess::Exec(const std::vector<std::string>& cmd,
40 ExecCallback callback,
41 void *p) {
42 GPid child_pid;
43 GError *err;
44 scoped_array<char *> argv(new char*[cmd.size() + 1]);
45 for (unsigned int i = 0; i < cmd.size(); i++) {
46 argv[i] = strdup(cmd[i].c_str());
47 }
48 argv[cmd.size()] = NULL;
49 char *argp[1];
50 argp[0] = NULL;
51
52 SubprocessCallbackRecord callback_record;
53 callback_record.callback = callback;
54 callback_record.callback_data = p;
55
56 bool success = g_spawn_async(NULL, // working directory
57 argv.get(),
58 argp,
59 G_SPAWN_DO_NOT_REAP_CHILD, // flags
60 NULL, // child setup function
61 NULL, // child setup data pointer
62 &child_pid,
63 &err);
64 FreeArgv(argv.get());
65 if (!success) {
66 LOG(ERROR) << "g_spawn_async failed";
67 return 0;
68 }
69 guint *tag = new guint;
70 *tag = g_child_watch_add(child_pid, GChildExitedCallback, tag);
71 callback_records_[*tag] = callback_record;
72 return *tag;
73 }
74
75 void Subprocess::CancelExec(uint32 tag) {
76 if (callback_records_[tag].callback) {
77 callback_records_[tag].callback = NULL;
78 }
79 }
80
81 bool Subprocess::SynchronousExec(const std::vector<std::string>& cmd,
82 int* return_code) {
83 GError *err;
84 scoped_array<char *> argv(new char*[cmd.size() + 1]);
85 for (unsigned int i = 0; i < cmd.size(); i++) {
86 argv[i] = strdup(cmd[i].c_str());
87 }
88 argv[cmd.size()] = NULL;
89 char *argp[1];
90 argp[0] = NULL;
91
92 bool success = g_spawn_sync(NULL, // working directory
93 argv.get(),
94 argp,
95 static_cast<GSpawnFlags>(NULL), // flags
96 NULL, // child setup function
97 NULL, // data for child setup function
98 NULL, // return location for stdout
99 NULL, // return location for stderr
100 return_code,
101 &err);
102 FreeArgv(argv.get());
103 return success;
104 }
105
106 Subprocess* Subprocess::subprocess_singleton_ = NULL;
107
108 } // namespace chromeos_update_engine
OLDNEW
« no previous file with comments | « src/platform/update_engine/subprocess.h ('k') | src/platform/update_engine/subprocess_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698