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

Side by Side Diff: apps/app_host/operation_launcher.cc

Issue 12674028: Report text output and exit code for command-line operations. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Forgotten review responses. Created 7 years, 8 months 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 | Annotate | Revision Log
« no previous file with comments | « apps/app_host/operation_launcher.h ('k') | apps/app_host/test_operation.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2013 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 // Creates an anonymous pipe for communicating an exit code from an operation
6 // implemented by one or more subprocesses. If the original subprocess exits
7 // without writing its result and without duplicating its output HANDLE into
8 // some other process a failure is assumed to have occurred. Otherwise the
9 // client closes its copy of the output HANDLE and the operation is considered
10 // to be in progress until either its result has been read or no more valid
11 // output HANDLEs remain.
12
13 #include "operation_launcher.h"
14
15 #include "base/command_line.h"
16 #include "base/compiler_specific.h"
17 #include "base/logging.h"
18 #include "base/process_util.h"
19 #include "base/strings/string_number_conversions.h"
20 #include "base/synchronization/lock.h"
21 #include "base/threading/platform_thread.h"
22 #include "base/win/scoped_handle.h"
23 #include "chrome/common/chrome_switches.h"
24
25 namespace app_host {
26
27 namespace {
28
29 // Implements a background thread to read the exit code of the child process.
30 class ExitCodeReader : public base::PlatformThread::Delegate {
31 public:
32 ExitCodeReader(base::win::ScopedHandle exit_code_read)
33 : success_(false), exit_code_(0) {
34 exit_code_read_ = exit_code_read.Pass();
35 }
36
37 virtual void ThreadMain() OVERRIDE {
38 DWORD bytes_read = 0;
39 BOOL read_success = ::ReadFile(
40 exit_code_read_, &exit_code_, sizeof(exit_code_), &bytes_read, NULL);
41 if (!read_success || bytes_read != sizeof(exit_code_)) {
42 DLOG(ERROR) << "Failed to read exit code.";
43 } else {
44 success_ = true;
45 }
46 }
47
48 // Only call these accessors after the ExitCodeReader thread has completed.
49 DWORD exit_code() { return exit_code_; }
50 bool success() { return success_; }
51
52 private:
53 bool success_;
54 DWORD exit_code_;
55
56 base::win::ScopedHandle exit_code_read_;
57 };
58
59 } // namespace
60
61 bool LaunchOperation(const CommandLine& command_line,
62 HANDLE output_write,
63 DWORD* exit_code) {
64 base::win::ScopedHandle exit_code_read;
65 base::win::ScopedHandle exit_code_write;
66
67 if (::CreatePipe(exit_code_read.Receive(), exit_code_write.Receive(),
68 NULL, 0) == 0) {
69 DPLOG(ERROR) << "CreatePipe failed.";
70 return false;
71 }
72
73 CommandLine full_command_line(command_line);
74 full_command_line.AppendSwitchASCII(
75 switches::kTaskOutputHandle,
76 base::UintToString(reinterpret_cast<int>(output_write)));
77 full_command_line.AppendSwitchASCII(
78 switches::kTaskResultHandle,
79 base::UintToString(reinterpret_cast<int>(exit_code_write.Get())));
80 full_command_line.AppendSwitchASCII(
81 switches::kTaskRemoteProcessId,
82 base::UintToString(::GetCurrentProcessId()));
83
84 base::win::ScopedHandle child_process;
85 base::win::ScopedHandle background_thread;
86
87 bool launch_result = base::LaunchProcess(full_command_line,
88 base::LaunchOptions(),
89 child_process.Receive());
90 if (!launch_result) {
91 DLOG(INFO) << "Failed to execute "
92 << full_command_line.GetCommandLineString();
93 return false;
94 }
95
96 DLOG(INFO) << "Executed " << full_command_line.GetCommandLineString();
97
98 ExitCodeReader exit_code_reader(exit_code_read.Pass());
99 base::PlatformThread::Create(
100 0, &exit_code_reader, background_thread.Receive());
101
102 HANDLE handles[] = {child_process.Get(), background_thread.Get()};
103 DWORD wait_result = WaitForMultipleObjects(2, handles, FALSE, INFINITE);
104 switch (wait_result) {
105 case WAIT_FAILED:
106 DLOG(ERROR) << "Wait for operation failed.";
107 break;
108 case WAIT_OBJECT_0:
109 // The child process exited. If the operation is still in progress
110 // (delegated to a tertiary process) that process is required to have
111 // duplicated the write handles by now; the closing of all outstanding
112 // handles to exit_code_write will be our signal that the operation has
113 // completed.
114 exit_code_write.Close();
115 break;
116 case WAIT_OBJECT_0 + 1:
117 // The reader thread has completed. We have read (or permanently failed to
118 // read) the operation exit code. The child process may or may not exit at
119 // this point, but we already have everything we need.
120 break;
121 default:
122 NOTREACHED();
123 return false;
124 }
125
126 // Whether or not the thread is still running, allow Join to do whatever
127 // cleanup is required.
128 base::PlatformThread::Join(background_thread.Take());
129
130 if (exit_code_reader.success())
131 *exit_code = exit_code_reader.exit_code();
132 return exit_code_reader.success();
133 }
134
135 } // namespace app_host
OLDNEW
« no previous file with comments | « apps/app_host/operation_launcher.h ('k') | apps/app_host/test_operation.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698