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/win_multiprocess.h" | |
16 | |
17 #include <shellapi.h> | |
18 | |
19 #include "base/logging.h" | |
20 #include "base/strings/stringprintf.h" | |
21 #include "base/strings/utf_string_conversions.h" | |
22 #include "gtest/gtest.h" | |
23 #include "util/string/split_string.h" | |
24 #include "test/paths.h" | |
25 | |
26 namespace crashpad { | |
27 namespace test { | |
28 | |
29 namespace { | |
30 | |
31 const char kIsMultiprocessChild[] = "--is-multiprocess-child"; | |
32 | |
33 bool GetSwitch(const char* switch_name, std::string* value) { | |
34 int num_args; | |
35 wchar_t** args = CommandLineToArgvW(GetCommandLine(), &num_args); | |
36 if (!args) { | |
cpu_(ooo_6.6-7.5)
2015/05/22 16:15:23
needs a localfree(args)
alternatively we can do s
scottmg
2015/05/25 19:04:57
Since this is test code only, I will stick to free
| |
37 PLOG(ERROR) << "couldn't parse command line"; | |
38 return false; | |
39 } | |
40 | |
41 std::string switch_name_with_equals(switch_name); | |
42 switch_name_with_equals += "="; | |
43 for (size_t i = 1; i < num_args; ++i) { | |
44 const wchar_t* arg = args[i]; | |
45 std::string arg_as_utf8 = base::UTF16ToUTF8(arg); | |
46 if (arg_as_utf8.compare( | |
47 0, switch_name_with_equals.size(), switch_name_with_equals) == 0) { | |
48 *value = arg_as_utf8.substr(switch_name_with_equals.size()); | |
49 return true; | |
50 } | |
51 } | |
52 | |
53 return false; | |
54 } | |
55 | |
56 } // namespace | |
57 | |
58 WinMultiprocess::WinMultiprocess() { | |
59 } | |
60 | |
61 void WinMultiprocess::Run() { | |
62 std::string switch_value; | |
63 if (GetSwitch(kIsMultiprocessChild, &switch_value)) { | |
64 // If we're in the child, then set up the handles we inherited from the | |
65 // parent. These are inherited from the parent and so are open and have the | |
66 // same value as in the parent. The values are passed to the child on the | |
67 // command line. | |
68 unsigned int c2p_write, p2c_read; | |
69 sscanf(switch_value.c_str(), "%x|%x", &c2p_write, &p2c_read); | |
cpu_(ooo_6.6-7.5)
2015/05/22 16:15:23
the way brettw made me do these things is with Spl
scottmg
2015/05/25 19:04:56
Probably better, done. (I thought I originally I w
| |
70 pipe_c2p_write_.reset(reinterpret_cast<HANDLE>(c2p_write)); | |
71 pipe_p2c_read_.reset(reinterpret_cast<HANDLE>(p2c_read)); | |
72 | |
73 // Notify the parent that it's OK to proceed. We only need to wait to get to | |
74 // the process entry point, but this is the easiest place we can notify. | |
75 char c = ' '; | |
76 CheckedWriteFile(WritePipeHandle(), &c, sizeof(c)); | |
77 | |
78 // Invoke the child side of the test. | |
79 WinMultiprocessChild(); | |
80 exit(0); | |
81 } else { | |
82 // If we're in the parent, make pipes for child-to-parent and | |
83 // parent-to-child communication. Mark them as inheritable via the | |
84 // SECURITY_ATTRIBUTES, but use SetHandleInformation to ensure that the | |
85 // parent sides are not inherited. | |
86 SECURITY_ATTRIBUTES security_attributes = {0}; | |
87 security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES); | |
88 security_attributes.bInheritHandle = true; | |
89 | |
90 HANDLE c2p_read, c2p_write; | |
91 PCHECK(CreatePipe(&c2p_read, &c2p_write, &security_attributes, 0)); | |
92 PCHECK(SetHandleInformation(c2p_read, HANDLE_FLAG_INHERIT, 0)); | |
93 pipe_c2p_read_.reset(c2p_read); | |
94 pipe_c2p_write_.reset(c2p_write); | |
95 | |
96 HANDLE p2c_read, p2c_write; | |
97 PCHECK(CreatePipe(&p2c_read, &p2c_write, &security_attributes, 0)); | |
98 PCHECK(SetHandleInformation(p2c_write, HANDLE_FLAG_INHERIT, 0)); | |
99 pipe_p2c_read_.reset(p2c_read); | |
100 pipe_p2c_write_.reset(p2c_write); | |
101 | |
102 // Build a command line for the child process that tells it only to run the | |
103 // current test, and to pass down the values of the pipe handles. | |
104 std::wstring command_line; | |
cpu_(ooo_6.6-7.5)
2015/05/22 16:15:23
104 can be in 107
scottmg
2015/05/25 19:04:57
Done.
| |
105 const ::testing::TestInfo* const test_info = | |
106 ::testing::UnitTest::GetInstance()->current_test_info(); | |
107 command_line = | |
108 Paths::Executable().value() + L" " + | |
109 base::UTF8ToUTF16(base::StringPrintf("--gtest_filter=%s.%s %s=%x|%x", | |
110 test_info->test_case_name(), | |
111 test_info->name(), | |
112 kIsMultiprocessChild, | |
113 c2p_write, | |
114 p2c_read)); | |
115 STARTUPINFO startup_info = {0}; | |
116 startup_info.cb = sizeof(startup_info); | |
117 startup_info.hStdInput = GetStdHandle(STD_INPUT_HANDLE); | |
118 startup_info.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); | |
119 startup_info.hStdError = GetStdHandle(STD_ERROR_HANDLE); | |
120 startup_info.dwFlags = STARTF_USESTDHANDLES; | |
121 PROCESS_INFORMATION process_info; | |
122 PCHECK( | |
123 CreateProcess(Paths::Executable().value().c_str(), | |
124 &command_line[0], // This cannot be constant, per MSDN. | |
cpu_(ooo_6.6-7.5)
2015/05/22 16:15:23
it will in some cases write a 0 to separate things
scottmg
2015/05/25 19:04:56
Acknowledged.
| |
125 nullptr, | |
126 nullptr, | |
127 true, // Inherit handles. | |
128 0, | |
129 nullptr, | |
130 nullptr, | |
131 &startup_info, | |
132 &process_info)); | |
133 child_handle_.reset(process_info.hProcess); | |
134 CloseHandle(process_info.hThread); | |
135 | |
136 // Block until the child process has launched. CreateProcess() returns | |
137 // immediately, and test code expects process initialization to have | |
138 // completed so it can, for example, use the process handle. | |
139 char c; | |
140 CheckedReadFile(pipe_c2p_read_.get(), &c, sizeof(c)); | |
141 ASSERT_EQ(' ', c); | |
142 | |
143 // These have been passed to the child, close our side. | |
144 pipe_c2p_write_.reset(); | |
145 pipe_p2c_read_.reset(); | |
146 | |
147 WinMultiprocessParent(); | |
148 } | |
149 } | |
150 | |
151 WinMultiprocess::~WinMultiprocess() { | |
152 } | |
153 | |
154 FileHandle WinMultiprocess::ReadPipeHandle() const { | |
155 FileHandle handle = | |
156 child_handle_.get() ? pipe_c2p_read_.get() : pipe_p2c_read_.get(); | |
157 CHECK(handle != nullptr); | |
158 return handle; | |
159 } | |
160 | |
161 FileHandle WinMultiprocess::WritePipeHandle() const { | |
162 FileHandle handle = | |
163 child_handle_.get() ? pipe_p2c_write_.get() : pipe_c2p_write_.get(); | |
164 CHECK(handle != nullptr); | |
165 return handle; | |
166 } | |
167 | |
168 void WinMultiprocess::CloseReadPipe() { | |
169 if (child_handle_.get()) | |
170 pipe_c2p_read_.reset(); | |
171 else | |
172 pipe_p2c_read_.reset(); | |
173 } | |
174 | |
175 void WinMultiprocess::CloseWritePipe() { | |
176 if (child_handle_.get()) | |
177 pipe_p2c_write_.reset(); | |
178 else | |
179 pipe_c2p_write_.reset(); | |
180 } | |
181 | |
182 HANDLE WinMultiprocess::ChildProcess() const { | |
183 EXPECT_NE(nullptr, child_handle_.get()); | |
184 return child_handle_.get(); | |
185 } | |
186 | |
187 } // namespace test | |
188 } // namespace crashpad | |
OLD | NEW |