OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include "test/win/child_launcher.h" |
| 16 |
| 17 #include "gtest/gtest.h" |
| 18 |
| 19 namespace crashpad { |
| 20 namespace test { |
| 21 |
| 22 ChildLauncher::ChildLauncher(const std::wstring& executable, |
| 23 const std::wstring& command_line) |
| 24 : executable_(executable), |
| 25 command_line_(command_line), |
| 26 process_handle_(), |
| 27 main_thread_handle_(), |
| 28 stdout_read_handle_() { |
| 29 } |
| 30 |
| 31 ChildLauncher::~ChildLauncher() { |
| 32 EXPECT_EQ(WAIT_OBJECT_0, |
| 33 WaitForSingleObject(process_handle_.get(), INFINITE)); |
| 34 } |
| 35 |
| 36 void ChildLauncher::Start() { |
| 37 ASSERT_FALSE(process_handle_.is_valid()); |
| 38 ASSERT_FALSE(main_thread_handle_.is_valid()); |
| 39 ASSERT_FALSE(stdout_read_handle_.is_valid()); |
| 40 |
| 41 // Create a pipe for the stdout of the child. |
| 42 SECURITY_ATTRIBUTES security_attributes = {0}; |
| 43 security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 44 security_attributes.bInheritHandle = true; |
| 45 HANDLE stdout_read; |
| 46 HANDLE stdout_write; |
| 47 ASSERT_TRUE(CreatePipe(&stdout_read, &stdout_write, &security_attributes, 0)); |
| 48 stdout_read_handle_.reset(stdout_read); |
| 49 ScopedFileHANDLE write_handle(stdout_write); |
| 50 ASSERT_TRUE( |
| 51 SetHandleInformation(stdout_read_handle_.get(), HANDLE_FLAG_INHERIT, 0)); |
| 52 |
| 53 STARTUPINFO startup_info = {0}; |
| 54 startup_info.cb = sizeof(startup_info); |
| 55 startup_info.hStdInput = GetStdHandle(STD_INPUT_HANDLE); |
| 56 startup_info.hStdOutput = write_handle.get(); |
| 57 startup_info.hStdError = GetStdHandle(STD_ERROR_HANDLE); |
| 58 startup_info.dwFlags = STARTF_USESTDHANDLES; |
| 59 PROCESS_INFORMATION process_information; |
| 60 std::wstring command_line; |
| 61 AppendCommandLineArgument(executable_, &command_line); |
| 62 command_line += L" "; |
| 63 command_line += command_line_; |
| 64 ASSERT_TRUE(CreateProcess(executable_.c_str(), |
| 65 &command_line[0], |
| 66 nullptr, |
| 67 nullptr, |
| 68 true, |
| 69 0, |
| 70 nullptr, |
| 71 nullptr, |
| 72 &startup_info, |
| 73 &process_information)); |
| 74 // Take ownership of the two process handles returned. |
| 75 main_thread_handle_.reset(process_information.hThread); |
| 76 process_handle_.reset(process_information.hProcess); |
| 77 } |
| 78 |
| 79 // Ref: http://blogs.msdn.com/b/twistylittlepassagesallalike/archive/2011/04/23/
everyone-quotes-arguments-the-wrong-way.aspx |
| 80 void AppendCommandLineArgument(const std::wstring& argument, |
| 81 std::wstring* command_line) { |
| 82 // Don't bother quoting if unnecessary. |
| 83 if (!argument.empty() && |
| 84 argument.find_first_of(L" \t\n\v\"") == std::wstring::npos) { |
| 85 command_line->append(argument); |
| 86 } else { |
| 87 command_line->push_back(L'"'); |
| 88 for (std::wstring::const_iterator i = argument.begin();; ++i) { |
| 89 size_t backslash_count = 0; |
| 90 while (i != argument.end() && *i == L'\\') { |
| 91 ++i; |
| 92 ++backslash_count; |
| 93 } |
| 94 if (i == argument.end()) { |
| 95 // Escape all backslashes, but let the terminating double quotation mark |
| 96 // we add below be interpreted as a metacharacter. |
| 97 command_line->append(backslash_count * 2, L'\\'); |
| 98 break; |
| 99 } else if (*i == L'"') { |
| 100 // Escape all backslashes and the following double quotation mark. |
| 101 command_line->append(backslash_count * 2 + 1, L'\\'); |
| 102 command_line->push_back(*i); |
| 103 } else { |
| 104 // Backslashes aren't special here. |
| 105 command_line->append(backslash_count, L'\\'); |
| 106 command_line->push_back(*i); |
| 107 } |
| 108 } |
| 109 command_line->push_back(L'"'); |
| 110 } |
| 111 } |
| 112 |
| 113 } // namespace test |
| 114 } // namespace crashpad |
OLD | NEW |