cjhopman | 5579301 | 2014-10-16 19:19:40 | [diff] [blame] | 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 | bool ExecProcess(const CommandLine& cmdline, |
| 142 | const base::FilePath& startup_dir, |
| 143 | std::string* std_out, |
| 144 | std::string* std_err, |
| 145 | int* exit_code) { |
| 146 | *exit_code = EXIT_FAILURE; |
| 147 | |
| 148 | std::vector<std::string> argv = cmdline.argv(); |
| 149 | |
| 150 | int out_fd[2], err_fd[2]; |
| 151 | pid_t pid; |
| 152 | base::InjectiveMultimap fd_shuffle1, fd_shuffle2; |
| 153 | scoped_ptr<char*[]> argv_cstr(new char*[argv.size() + 1]); |
| 154 | |
| 155 | fd_shuffle1.reserve(3); |
| 156 | fd_shuffle2.reserve(3); |
| 157 | |
| 158 | if (pipe(out_fd) < 0) |
| 159 | return false; |
| 160 | base::ScopedFD out_read(out_fd[0]), out_write(out_fd[1]); |
| 161 | |
| 162 | if (pipe(err_fd) < 0) |
| 163 | return false; |
| 164 | base::ScopedFD err_read(err_fd[0]), err_write(err_fd[1]); |
| 165 | |
| 166 | if (out_read.get() >= FD_SETSIZE || err_read.get() >= FD_SETSIZE) |
| 167 | return false; |
| 168 | |
| 169 | switch (pid = fork()) { |
| 170 | case -1: // error |
| 171 | return false; |
| 172 | case 0: // child |
| 173 | { |
| 174 | // DANGER: no calls to malloc are allowed from now on: |
| 175 | // http://crbug.com/36678 |
| 176 | |
| 177 | // Obscure fork() rule: in the child, if you don't end up doing exec*(), |
| 178 | // you call _exit() instead of exit(). This is because _exit() does not |
| 179 | // call any previously-registered (in the parent) exit handlers, which |
| 180 | // might do things like block waiting for threads that don't even exist |
| 181 | // in the child. |
| 182 | int dev_null = open("/dev/null", O_WRONLY); |
| 183 | if (dev_null < 0) |
| 184 | _exit(127); |
| 185 | |
| 186 | fd_shuffle1.push_back( |
| 187 | base::InjectionArc(out_write.get(), STDOUT_FILENO, true)); |
| 188 | fd_shuffle1.push_back( |
| 189 | base::InjectionArc(err_write.get(), STDERR_FILENO, true)); |
| 190 | fd_shuffle1.push_back( |
| 191 | base::InjectionArc(dev_null, STDIN_FILENO, true)); |
| 192 | // Adding another element here? Remeber to increase the argument to |
| 193 | // reserve(), above. |
| 194 | |
| 195 | for (size_t i = 0; i < fd_shuffle1.size(); ++i) |
| 196 | fd_shuffle2.push_back(fd_shuffle1[i]); |
| 197 | |
| 198 | if (!ShuffleFileDescriptors(&fd_shuffle1)) |
| 199 | _exit(127); |
| 200 | |
| 201 | base::SetCurrentDirectory(startup_dir); |
| 202 | |
| 203 | // TODO(brettw) the base version GetAppOutput does a |
| 204 | // CloseSuperfluousFds call here. Do we need this? |
| 205 | |
| 206 | for (size_t i = 0; i < argv.size(); i++) |
| 207 | argv_cstr[i] = const_cast<char*>(argv[i].c_str()); |
| 208 | argv_cstr[argv.size()] = NULL; |
| 209 | execvp(argv_cstr[0], argv_cstr.get()); |
| 210 | _exit(127); |
| 211 | } |
| 212 | default: // parent |
| 213 | { |
| 214 | // Close our writing end of pipe now. Otherwise later read would not |
| 215 | // be able to detect end of child's output (in theory we could still |
| 216 | // write to the pipe). |
| 217 | out_write.reset(); |
| 218 | err_write.reset(); |
| 219 | |
| 220 | bool out_open = true, err_open = true; |
| 221 | while (out_open || err_open) { |
| 222 | fd_set read_fds; |
| 223 | FD_ZERO(&read_fds); |
| 224 | FD_SET(out_read.get(), &read_fds); |
| 225 | FD_SET(err_read.get(), &read_fds); |
| 226 | int res = |
| 227 | HANDLE_EINTR(select(std::max(out_read.get(), err_read.get()) + 1, |
| 228 | &read_fds, |
| 229 | NULL, |
| 230 | NULL, |
| 231 | NULL)); |
| 232 | if (res <= 0) |
| 233 | break; |
| 234 | if (FD_ISSET(out_read.get(), &read_fds)) |
| 235 | out_open = ReadFromPipe(out_read.get(), std_out); |
| 236 | if (FD_ISSET(err_read.get(), &read_fds)) |
| 237 | err_open = ReadFromPipe(err_read.get(), std_err); |
| 238 | } |
| 239 | |
| 240 | return base::WaitForExitCode(pid, exit_code); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | return false; |
| 245 | } |
| 246 | #endif |
| 247 | |
| 248 | } // namespace internal |
| 249 | |