OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/process/process.h" | 5 #include "base/process/process.h" |
6 | 6 |
7 #include "base/posix/eintr_wrapper.h" | |
7 #include "base/process/kill.h" | 8 #include "base/process/kill.h" |
8 #include "base/test/multiprocess_test.h" | 9 #include "base/test/multiprocess_test.h" |
9 #include "base/test/test_timeouts.h" | 10 #include "base/test/test_timeouts.h" |
10 #include "base/threading/platform_thread.h" | 11 #include "base/threading/platform_thread.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
12 #include "testing/multiprocess_func_list.h" | 13 #include "testing/multiprocess_func_list.h" |
13 | 14 |
14 | 15 |
15 namespace { | 16 namespace { |
16 | 17 |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
194 EXPECT_TRUE(process.SetProcessBackgrounded(false)); | 195 EXPECT_TRUE(process.SetProcessBackgrounded(false)); |
195 EXPECT_FALSE(process.IsProcessBackgrounded()); | 196 EXPECT_FALSE(process.IsProcessBackgrounded()); |
196 #else | 197 #else |
197 process.SetProcessBackgrounded(true); | 198 process.SetProcessBackgrounded(true); |
198 process.SetProcessBackgrounded(false); | 199 process.SetProcessBackgrounded(false); |
199 #endif | 200 #endif |
200 int new_priority = process.GetPriority(); | 201 int new_priority = process.GetPriority(); |
201 EXPECT_EQ(old_priority, new_priority); | 202 EXPECT_EQ(old_priority, new_priority); |
202 } | 203 } |
203 | 204 |
205 #if defined(OS_POSIX) | |
206 const char kPipeValue = '\xcc'; | |
207 | |
208 class ReadFromPipeDelegate : public LaunchOptions::PreExecDelegate { | |
209 public: | |
210 explicit ReadFromPipeDelegate(int fd) : fd_(fd) {} | |
211 ~ReadFromPipeDelegate() override {} | |
212 void RunAsyncSafe() override { | |
213 char c; | |
214 RAW_CHECK(HANDLE_EINTR(read(fd_, &c, 1)) == 1); | |
215 RAW_CHECK(IGNORE_EINTR(close(fd_)) == 0); | |
216 RAW_CHECK(c == kPipeValue); | |
217 } | |
218 | |
219 private: | |
220 int fd_; | |
221 }; | |
jln (very slow on Chromium)
2015/01/07 00:40:37
Style: DISALLOW_COPY_AND_ASSIGN
rickyz (no longer on Chrome)
2015/01/07 01:39:07
Done.
| |
222 | |
223 TEST_F(ProcessTest, PreExecHook) { | |
224 int pipe_fds[2]; | |
225 ASSERT_EQ(0, pipe(pipe_fds)); | |
226 | |
227 const int read_fd = pipe_fds[0]; | |
jln (very slow on Chromium)
2015/01/07 00:40:38
How about making these ScopedFDs instead? That way
rickyz (no longer on Chrome)
2015/01/07 01:39:07
Sure - I kept the raw fd in the delegate because S
| |
228 const int write_fd = pipe_fds[1]; | |
229 base::FileHandleMappingVector fds_to_remap; | |
230 fds_to_remap.push_back(std::make_pair(read_fd, read_fd)); | |
231 | |
232 ReadFromPipeDelegate read_from_pipe_delegate(read_fd); | |
233 LaunchOptions options; | |
234 options.fds_to_remap = &fds_to_remap; | |
235 options.pre_exec_delegate = &read_from_pipe_delegate; | |
236 Process process(SpawnChildWithOptions("SimpleChildProcess", options)); | |
237 ASSERT_TRUE(process.IsValid()); | |
238 | |
239 ASSERT_EQ(0, IGNORE_EINTR(close(read_fd))); | |
240 ASSERT_EQ(1, HANDLE_EINTR(write(write_fd, &kPipeValue, 1))); | |
241 ASSERT_EQ(0, IGNORE_EINTR(close(write_fd))); | |
242 | |
243 int exit_code = 42; | |
244 EXPECT_TRUE(process.WaitForExit(&exit_code)); | |
245 EXPECT_EQ(0, exit_code); | |
246 } | |
247 #endif | |
jln (very slow on Chromium)
2015/01/07 00:40:38
Style: // defined(OS_POSIX)
rickyz (no longer on Chrome)
2015/01/07 01:39:07
Done.
| |
248 | |
204 } // namespace base | 249 } // namespace base |
OLD | NEW |