| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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 "base/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "base/files/file_util.h" | 6 #include "base/files/file_util.h" |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/process/kill.h" | |
| 9 #include "base/process/launch.h" | |
| 10 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 13 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 14 #include "tools/gn/err.h" | 12 #include "tools/gn/err.h" |
| 13 #include "tools/gn/exec_process.h" |
| 15 #include "tools/gn/filesystem_utils.h" | 14 #include "tools/gn/filesystem_utils.h" |
| 16 #include "tools/gn/functions.h" | 15 #include "tools/gn/functions.h" |
| 17 #include "tools/gn/input_conversion.h" | 16 #include "tools/gn/input_conversion.h" |
| 18 #include "tools/gn/input_file.h" | 17 #include "tools/gn/input_file.h" |
| 19 #include "tools/gn/parse_tree.h" | 18 #include "tools/gn/parse_tree.h" |
| 20 #include "tools/gn/scheduler.h" | 19 #include "tools/gn/scheduler.h" |
| 21 #include "tools/gn/trace.h" | 20 #include "tools/gn/trace.h" |
| 22 #include "tools/gn/value.h" | 21 #include "tools/gn/value.h" |
| 23 | 22 |
| 24 #if defined(OS_WIN) | |
| 25 #include <windows.h> | |
| 26 | |
| 27 #include "base/win/scoped_handle.h" | |
| 28 #include "base/win/scoped_process_information.h" | |
| 29 #endif | |
| 30 | |
| 31 #if defined(OS_POSIX) | |
| 32 #include <fcntl.h> | |
| 33 #include <unistd.h> | |
| 34 | |
| 35 #include "base/posix/file_descriptor_shuffle.h" | |
| 36 #endif | |
| 37 | |
| 38 namespace functions { | 23 namespace functions { |
| 39 | 24 |
| 40 namespace { | 25 namespace { |
| 41 | |
| 42 const char kNoExecSwitch[] = "no-exec"; | 26 const char kNoExecSwitch[] = "no-exec"; |
| 43 | |
| 44 #if defined(OS_WIN) | |
| 45 bool ExecProcess(const CommandLine& cmdline, | |
| 46 const base::FilePath& startup_dir, | |
| 47 std::string* std_out, | |
| 48 std::string* std_err, | |
| 49 int* exit_code) { | |
| 50 SECURITY_ATTRIBUTES sa_attr; | |
| 51 // Set the bInheritHandle flag so pipe handles are inherited. | |
| 52 sa_attr.nLength = sizeof(SECURITY_ATTRIBUTES); | |
| 53 sa_attr.bInheritHandle = TRUE; | |
| 54 sa_attr.lpSecurityDescriptor = NULL; | |
| 55 | |
| 56 // Create the pipe for the child process's STDOUT. | |
| 57 HANDLE out_read = NULL; | |
| 58 HANDLE out_write = NULL; | |
| 59 if (!CreatePipe(&out_read, &out_write, &sa_attr, 0)) { | |
| 60 NOTREACHED() << "Failed to create pipe"; | |
| 61 return false; | |
| 62 } | |
| 63 base::win::ScopedHandle scoped_out_read(out_read); | |
| 64 base::win::ScopedHandle scoped_out_write(out_write); | |
| 65 | |
| 66 // Create the pipe for the child process's STDERR. | |
| 67 HANDLE err_read = NULL; | |
| 68 HANDLE err_write = NULL; | |
| 69 if (!CreatePipe(&err_read, &err_write, &sa_attr, 0)) { | |
| 70 NOTREACHED() << "Failed to create pipe"; | |
| 71 return false; | |
| 72 } | |
| 73 base::win::ScopedHandle scoped_err_read(err_read); | |
| 74 base::win::ScopedHandle scoped_err_write(err_write); | |
| 75 | |
| 76 // Ensure the read handle to the pipe for STDOUT/STDERR is not inherited. | |
| 77 if (!SetHandleInformation(out_read, HANDLE_FLAG_INHERIT, 0)) { | |
| 78 NOTREACHED() << "Failed to disabled pipe inheritance"; | |
| 79 return false; | |
| 80 } | |
| 81 if (!SetHandleInformation(err_read, HANDLE_FLAG_INHERIT, 0)) { | |
| 82 NOTREACHED() << "Failed to disabled pipe inheritance"; | |
| 83 return false; | |
| 84 } | |
| 85 | |
| 86 base::FilePath::StringType cmdline_str(cmdline.GetCommandLineString()); | |
| 87 | |
| 88 STARTUPINFO start_info = {}; | |
| 89 | |
| 90 start_info.cb = sizeof(STARTUPINFO); | |
| 91 start_info.hStdOutput = out_write; | |
| 92 // Keep the normal stdin. | |
| 93 start_info.hStdInput = GetStdHandle(STD_INPUT_HANDLE); | |
| 94 // FIXME(brettw) set stderr here when we actually read it below. | |
| 95 //start_info.hStdError = err_write; | |
| 96 start_info.hStdError = GetStdHandle(STD_ERROR_HANDLE); | |
| 97 start_info.dwFlags |= STARTF_USESTDHANDLES; | |
| 98 | |
| 99 // Create the child process. | |
| 100 PROCESS_INFORMATION temp_process_info = {}; | |
| 101 if (!CreateProcess(NULL, | |
| 102 &cmdline_str[0], | |
| 103 NULL, NULL, | |
| 104 TRUE, // Handles are inherited. | |
| 105 0, NULL, | |
| 106 startup_dir.value().c_str(), | |
| 107 &start_info, &temp_process_info)) { | |
| 108 return false; | |
| 109 } | |
| 110 base::win::ScopedProcessInformation proc_info(temp_process_info); | |
| 111 | |
| 112 // Close our writing end of pipes now. Otherwise later read would not be able | |
| 113 // to detect end of child's output. | |
| 114 scoped_out_write.Close(); | |
| 115 scoped_err_write.Close(); | |
| 116 | |
| 117 // Read output from the child process's pipe for STDOUT | |
| 118 const int kBufferSize = 1024; | |
| 119 char buffer[kBufferSize]; | |
| 120 | |
| 121 // FIXME(brettw) read from stderr here! This is complicated because we want | |
| 122 // to read both of them at the same time, probably need overlapped I/O. | |
| 123 // Also uncomment start_info code above. | |
| 124 for (;;) { | |
| 125 DWORD bytes_read = 0; | |
| 126 BOOL success = ReadFile(out_read, buffer, kBufferSize, &bytes_read, NULL); | |
| 127 if (!success || bytes_read == 0) | |
| 128 break; | |
| 129 std_out->append(buffer, bytes_read); | |
| 130 } | |
| 131 | |
| 132 // Let's wait for the process to finish. | |
| 133 WaitForSingleObject(proc_info.process_handle(), INFINITE); | |
| 134 | |
| 135 DWORD dw_exit_code; | |
| 136 GetExitCodeProcess(proc_info.process_handle(), &dw_exit_code); | |
| 137 *exit_code = static_cast<int>(dw_exit_code); | |
| 138 | |
| 139 return true; | |
| 140 } | |
| 141 #else | |
| 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 pipe_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(pipe_fd) < 0) | |
| 160 return false; | |
| 161 | |
| 162 switch (pid = fork()) { | |
| 163 case -1: // error | |
| 164 close(pipe_fd[0]); | |
| 165 close(pipe_fd[1]); | |
| 166 return false; | |
| 167 case 0: // child | |
| 168 { | |
| 169 // DANGER: no calls to malloc are allowed from now on: | |
| 170 // http://crbug.com/36678 | |
| 171 | |
| 172 // Obscure fork() rule: in the child, if you don't end up doing exec*(), | |
| 173 // you call _exit() instead of exit(). This is because _exit() does not | |
| 174 // call any previously-registered (in the parent) exit handlers, which | |
| 175 // might do things like block waiting for threads that don't even exist | |
| 176 // in the child. | |
| 177 int dev_null = open("/dev/null", O_WRONLY); | |
| 178 if (dev_null < 0) | |
| 179 _exit(127); | |
| 180 | |
| 181 fd_shuffle1.push_back( | |
| 182 base::InjectionArc(pipe_fd[1], STDOUT_FILENO, true)); | |
| 183 fd_shuffle1.push_back( | |
| 184 base::InjectionArc(dev_null, STDERR_FILENO, true)); | |
| 185 fd_shuffle1.push_back( | |
| 186 base::InjectionArc(dev_null, STDIN_FILENO, true)); | |
| 187 // Adding another element here? Remeber to increase the argument to | |
| 188 // reserve(), above. | |
| 189 | |
| 190 for (size_t i = 0; i < fd_shuffle1.size(); ++i) | |
| 191 fd_shuffle2.push_back(fd_shuffle1[i]); | |
| 192 | |
| 193 if (!ShuffleFileDescriptors(&fd_shuffle1)) | |
| 194 _exit(127); | |
| 195 | |
| 196 base::SetCurrentDirectory(startup_dir); | |
| 197 | |
| 198 // TODO(brettw) the base version GetAppOutput does a | |
| 199 // CloseSuperfluousFds call here. Do we need this? | |
| 200 | |
| 201 for (size_t i = 0; i < argv.size(); i++) | |
| 202 argv_cstr[i] = const_cast<char*>(argv[i].c_str()); | |
| 203 argv_cstr[argv.size()] = NULL; | |
| 204 execvp(argv_cstr[0], argv_cstr.get()); | |
| 205 _exit(127); | |
| 206 } | |
| 207 default: // parent | |
| 208 { | |
| 209 // Close our writing end of pipe now. Otherwise later read would not | |
| 210 // be able to detect end of child's output (in theory we could still | |
| 211 // write to the pipe). | |
| 212 close(pipe_fd[1]); | |
| 213 | |
| 214 char buffer[256]; | |
| 215 ssize_t bytes_read = 0; | |
| 216 | |
| 217 while (true) { | |
| 218 bytes_read = HANDLE_EINTR(read(pipe_fd[0], buffer, sizeof(buffer))); | |
| 219 if (bytes_read <= 0) | |
| 220 break; | |
| 221 std_out->append(buffer, bytes_read); | |
| 222 } | |
| 223 close(pipe_fd[0]); | |
| 224 | |
| 225 return base::WaitForExitCode(pid, exit_code); | |
| 226 } | |
| 227 } | |
| 228 | |
| 229 return false; | |
| 230 } | |
| 231 #endif | |
| 232 | |
| 233 } // namespace | 27 } // namespace |
| 234 | 28 |
| 235 const char kExecScript[] = "exec_script"; | 29 const char kExecScript[] = "exec_script"; |
| 236 const char kExecScript_HelpShort[] = | 30 const char kExecScript_HelpShort[] = |
| 237 "exec_script: Synchronously run a script and return the output."; | 31 "exec_script: Synchronously run a script and return the output."; |
| 238 const char kExecScript_Help[] = | 32 const char kExecScript_Help[] = |
| 239 "exec_script: Synchronously run a script and return the output.\n" | 33 "exec_script: Synchronously run a script and return the output.\n" |
| 240 "\n" | 34 "\n" |
| 241 " exec_script(filename,\n" | 35 " exec_script(filename,\n" |
| 242 " arguments = [],\n" | 36 " arguments = [],\n" |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 371 // running any scripts with this as its startup directory, although it will | 165 // running any scripts with this as its startup directory, although it will |
| 372 // be relatively rare that the directory won't exist by the time we get here. | 166 // be relatively rare that the directory won't exist by the time we get here. |
| 373 // | 167 // |
| 374 // If this shows up on benchmarks, we can cache whether we've done this | 168 // If this shows up on benchmarks, we can cache whether we've done this |
| 375 // or not and skip creating the directory. | 169 // or not and skip creating the directory. |
| 376 base::CreateDirectory(startup_dir); | 170 base::CreateDirectory(startup_dir); |
| 377 | 171 |
| 378 // Execute the process. | 172 // Execute the process. |
| 379 // TODO(brettw) set the environment block. | 173 // TODO(brettw) set the environment block. |
| 380 std::string output; | 174 std::string output; |
| 381 std::string stderr_output; // TODO(brettw) not hooked up, see above. | 175 std::string stderr_output; |
| 382 int exit_code = 0; | 176 int exit_code = 0; |
| 383 if (!CommandLine::ForCurrentProcess()->HasSwitch(kNoExecSwitch)) { | 177 if (!CommandLine::ForCurrentProcess()->HasSwitch(kNoExecSwitch)) { |
| 384 if (!ExecProcess(cmdline, startup_dir, | 178 if (!internal::ExecProcess( |
| 385 &output, &stderr_output, &exit_code)) { | 179 cmdline, startup_dir, &output, &stderr_output, &exit_code)) { |
| 386 *err = Err(function->function(), "Could not execute python.", | 180 *err = Err(function->function(), "Could not execute python.", |
| 387 "I was trying to execute \"" + FilePathToUTF8(python_path) + "\"."); | 181 "I was trying to execute \"" + FilePathToUTF8(python_path) + "\"."); |
| 388 return Value(); | 182 return Value(); |
| 389 } | 183 } |
| 390 } | 184 } |
| 391 if (g_scheduler->verbose_logging()) { | 185 if (g_scheduler->verbose_logging()) { |
| 392 g_scheduler->Log("Pythoning", script_source.value() + " took " + | 186 g_scheduler->Log("Pythoning", script_source.value() + " took " + |
| 393 base::Int64ToString( | 187 base::Int64ToString( |
| 394 (base::TimeTicks::Now() - begin_exec).InMilliseconds()) + | 188 (base::TimeTicks::Now() - begin_exec).InMilliseconds()) + |
| 395 "ms"); | 189 "ms"); |
| 396 } | 190 } |
| 397 | 191 |
| 398 // TODO(brettw) maybe we need stderr also for reasonable stack dumps. | |
| 399 if (exit_code != 0) { | 192 if (exit_code != 0) { |
| 400 std::string msg = "Current dir: " + FilePathToUTF8(startup_dir) + | 193 std::string msg = "Current dir: " + FilePathToUTF8(startup_dir) + |
| 401 "\nCommand: " + FilePathToUTF8(cmdline.GetCommandLineString()) + | 194 "\nCommand: " + FilePathToUTF8(cmdline.GetCommandLineString()) + |
| 402 "\nReturned " + base::IntToString(exit_code); | 195 "\nReturned " + base::IntToString(exit_code); |
| 403 if (!output.empty()) | 196 if (!output.empty()) |
| 404 msg += " and printed out:\n\n" + output; | 197 msg += " and printed out:\n\n" + output; |
| 405 else | 198 else |
| 406 msg += "."; | 199 msg += "."; |
| 200 if (!stderr_output.empty()) |
| 201 msg += "\nstderr:\n\n" + stderr_output; |
| 202 |
| 407 *err = Err(function->function(), "Script returned non-zero exit code.", | 203 *err = Err(function->function(), "Script returned non-zero exit code.", |
| 408 msg); | 204 msg); |
| 409 return Value(); | 205 return Value(); |
| 410 } | 206 } |
| 411 | 207 |
| 412 // Default to None value for the input conversion if unspecified. | 208 // Default to None value for the input conversion if unspecified. |
| 413 return ConvertInputToValue(scope->settings(), output, function, | 209 return ConvertInputToValue(scope->settings(), output, function, |
| 414 args.size() >= 3 ? args[2] : Value(), err); | 210 args.size() >= 3 ? args[2] : Value(), err); |
| 415 } | 211 } |
| 416 | 212 |
| 417 } // namespace functions | 213 } // namespace functions |
| OLD | NEW |