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

Side by Side Diff: tools/gn/exec_process.cc

Issue 646013003: GN: Capture and print stderr from exec_script (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Maybe fix windows compile Created 6 years, 2 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
OLDNEW
(Empty)
1 // Copyright 2014 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 "base/command_line.h"
6 #include "base/files/file_util.h"
7 #include "base/logging.h"
8 #include "base/process/kill.h"
9 #include "base/process/launch.h"
10
11 #if defined(OS_WIN)
12 #include <windows.h>
13
14 #include "base/win/scoped_handle.h"
15 #include "base/win/scoped_process_information.h"
16 #endif
17
18 #if defined(OS_POSIX)
19 #include <fcntl.h>
20 #include <unistd.h>
21
22 #include "base/posix/eintr_wrapper.h"
23 #include "base/posix/file_descriptor_shuffle.h"
24 #endif
25
26 namespace internal {
27
28 #if defined(OS_WIN)
29 bool ExecProcess(const CommandLine& cmdline,
30 const base::FilePath& startup_dir,
31 std::string* std_out,
32 std::string* std_err,
33 int* exit_code) {
34 SECURITY_ATTRIBUTES sa_attr;
35 // Set the bInheritHandle flag so pipe handles are inherited.
36 sa_attr.nLength = sizeof(SECURITY_ATTRIBUTES);
37 sa_attr.bInheritHandle = TRUE;
38 sa_attr.lpSecurityDescriptor = NULL;
39
40 // Create the pipe for the child process's STDOUT.
41 HANDLE out_read = NULL;
42 HANDLE out_write = NULL;
43 if (!CreatePipe(&out_read, &out_write, &sa_attr, 0)) {
44 NOTREACHED() << "Failed to create pipe";
45 return false;
46 }
47 base::win::ScopedHandle scoped_out_read(out_read);
48 base::win::ScopedHandle scoped_out_write(out_write);
49
50 // Create the pipe for the child process's STDERR.
51 HANDLE err_read = NULL;
52 HANDLE err_write = NULL;
53 if (!CreatePipe(&err_read, &err_write, &sa_attr, 0)) {
54 NOTREACHED() << "Failed to create pipe";
55 return false;
56 }
57 base::win::ScopedHandle scoped_err_read(err_read);
58 base::win::ScopedHandle scoped_err_write(err_write);
59
60 // Ensure the read handle to the pipe for STDOUT/STDERR is not inherited.
61 if (!SetHandleInformation(out_read, HANDLE_FLAG_INHERIT, 0)) {
62 NOTREACHED() << "Failed to disabled pipe inheritance";
63 return false;
64 }
65 if (!SetHandleInformation(err_read, HANDLE_FLAG_INHERIT, 0)) {
66 NOTREACHED() << "Failed to disabled pipe inheritance";
67 return false;
68 }
69
70 base::FilePath::StringType cmdline_str(cmdline.GetCommandLineString());
71
72 STARTUPINFO start_info = {};
73
74 start_info.cb = sizeof(STARTUPINFO);
75 start_info.hStdOutput = out_write;
76 // Keep the normal stdin.
77 start_info.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
78 // FIXME(brettw) set stderr here when we actually read it below.
79 //start_info.hStdError = err_write;
80 start_info.hStdError = GetStdHandle(STD_ERROR_HANDLE);
81 start_info.dwFlags |= STARTF_USESTDHANDLES;
82
83 // Create the child process.
84 PROCESS_INFORMATION temp_process_info = {};
85 if (!CreateProcess(NULL,
86 &cmdline_str[0],
87 NULL, NULL,
88 TRUE, // Handles are inherited.
89 0, NULL,
90 startup_dir.value().c_str(),
91 &start_info, &temp_process_info)) {
92 return false;
93 }
94 base::win::ScopedProcessInformation proc_info(temp_process_info);
95
96 // Close our writing end of pipes now. Otherwise later read would not be able
97 // to detect end of child's output.
98 scoped_out_write.Close();
99 scoped_err_write.Close();
100
101 // Read output from the child process's pipe for STDOUT
102 const int kBufferSize = 1024;
103 char buffer[kBufferSize];
104
105 // FIXME(brettw) read from stderr here! This is complicated because we want
106 // to read both of them at the same time, probably need overlapped I/O.
107 // Also uncomment start_info code above.
108 for (;;) {
109 DWORD bytes_read = 0;
110 BOOL success = ReadFile(out_read, buffer, kBufferSize, &bytes_read, NULL);
111 if (!success || bytes_read == 0)
112 break;
113 std_out->append(buffer, bytes_read);
114 }
115
116 // Let's wait for the process to finish.
117 WaitForSingleObject(proc_info.process_handle(), INFINITE);
118
119 DWORD dw_exit_code;
120 GetExitCodeProcess(proc_info.process_handle(), &dw_exit_code);
121 *exit_code = static_cast<int>(dw_exit_code);
122
123 return true;
124 }
125 #else
126 // Reads from the provided file descriptor and appends to output. Returns false
127 // if the fd is closed or there is an unexpected error (not
128 // EINTR/EAGAIN/EWOULDBLOCK).
129 bool ReadFromPipe(int fd, std::string* output) {
130 char buffer[256];
131 int bytes_read = HANDLE_EINTR(read(fd, buffer, sizeof(buffer)));
132 if (bytes_read == -1) {
133 return errno == EAGAIN || errno == EWOULDBLOCK;
134 } else if (bytes_read <= 0) {
135 return false;
136 }
137 output->append(buffer, bytes_read);
138 return true;
139 }
140
141
142 bool ExecProcess(const CommandLine& cmdline,
143 const base::FilePath& startup_dir,
144 std::string* std_out,
145 std::string* std_err,
146 int* exit_code) {
147 *exit_code = EXIT_FAILURE;
148
149 std::vector<std::string> argv = cmdline.argv();
150
151 int out_fd[2], err_fd[2];
152 pid_t pid;
153 base::InjectiveMultimap fd_shuffle1, fd_shuffle2;
154 scoped_ptr<char*[]> argv_cstr(new char*[argv.size() + 1]);
155
156 fd_shuffle1.reserve(3);
157 fd_shuffle2.reserve(3);
158
159 if (pipe(out_fd) < 0)
160 return false;
161 base::ScopedFD out_read(out_fd[0]), out_write(out_fd[1]);
162
163 if (pipe(err_fd) < 0)
164 return false;
165 base::ScopedFD err_read(err_fd[0]), err_write(err_fd[1]);
166
167 if (out_read.get() > FD_SETSIZE || err_read.get() > FD_SETSIZE) {
168 return false;
169 }
170
171 switch (pid = fork()) {
172 case -1: // error
173 return false;
174 case 0: // child
175 {
176 // DANGER: no calls to malloc are allowed from now on:
177 // http://crbug.com/36678
178
179 // Obscure fork() rule: in the child, if you don't end up doing exec*(),
180 // you call _exit() instead of exit(). This is because _exit() does not
181 // call any previously-registered (in the parent) exit handlers, which
182 // might do things like block waiting for threads that don't even exist
183 // in the child.
184 int dev_null = open("/dev/null", O_WRONLY);
185 if (dev_null < 0)
186 _exit(127);
187
188 fd_shuffle1.push_back(
189 base::InjectionArc(out_write.get(), STDOUT_FILENO, true));
190 fd_shuffle1.push_back(
191 base::InjectionArc(err_write.get(), STDERR_FILENO, true));
192 //fd_shuffle1.push_back(
193 //base::InjectionArc(STDERR_FILENO, STDERR_FILENO, true));
194 fd_shuffle1.push_back(
195 base::InjectionArc(dev_null, STDIN_FILENO, true));
196 // Adding another element here? Remeber to increase the argument to
197 // reserve(), above.
198
199 for (size_t i = 0; i < fd_shuffle1.size(); ++i)
200 fd_shuffle2.push_back(fd_shuffle1[i]);
201
202 if (!ShuffleFileDescriptors(&fd_shuffle1))
203 _exit(127);
204
205 base::SetCurrentDirectory(startup_dir);
206
207 // TODO(brettw) the base version GetAppOutput does a
208 // CloseSuperfluousFds call here. Do we need this?
209
210 for (size_t i = 0; i < argv.size(); i++)
211 argv_cstr[i] = const_cast<char*>(argv[i].c_str());
212 argv_cstr[argv.size()] = NULL;
213 execvp(argv_cstr[0], argv_cstr.get());
214 _exit(127);
215 }
216 default: // parent
217 {
218 // Close our writing end of pipe now. Otherwise later read would not
219 // be able to detect end of child's output (in theory we could still
220 // write to the pipe).
221 out_write.reset();
222 err_write.reset();
223
224 bool out_open = true, err_open = true;
225 while (out_open || err_open) {
226 fd_set read_fds;
227 FD_ZERO(&read_fds);
228 FD_SET(out_read.get(), &read_fds);
229 FD_SET(err_read.get(), &read_fds);
230 int res =
231 HANDLE_EINTR(select(std::max(out_read.get(), err_read.get()) + 1,
232 &read_fds,
233 NULL,
234 NULL,
235 NULL));
236 if (res <= 0)
237 break;
238 if (FD_ISSET(out_read.get(), &read_fds)) {
239 out_open = ReadFromPipe(out_read.get(), std_out);
240 }
241 if (FD_ISSET(err_read.get(), &read_fds)) {
242 err_open = ReadFromPipe(err_read.get(), std_err);
243 }
244 }
245
246 return base::WaitForExitCode(pid, exit_code);
247 }
248 }
249
250 return false;
251 }
252 #endif
253
254 } // namespace internal
255
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698