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/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/files/scoped_file.h" |
| 10 #include "base/posix/eintr_wrapper.h" |
9 #include "base/process/kill.h" | 11 #include "base/process/kill.h" |
10 #include "base/test/multiprocess_test.h" | 12 #include "base/test/multiprocess_test.h" |
11 #include "base/test/test_timeouts.h" | 13 #include "base/test/test_timeouts.h" |
12 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 14 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
13 #include "base/threading/platform_thread.h" | 15 #include "base/threading/platform_thread.h" |
14 #include "build/build_config.h" | 16 #include "build/build_config.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "testing/multiprocess_func_list.h" | 18 #include "testing/multiprocess_func_list.h" |
17 | 19 |
18 #if defined(OS_LINUX) | 20 #if defined(OS_LINUX) |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 RAW_CHECK(getpid() == ctid); | 263 RAW_CHECK(getpid() == ctid); |
262 _exit(kSuccess); | 264 _exit(kSuccess); |
263 } | 265 } |
264 | 266 |
265 ASSERT_NE(-1, pid); | 267 ASSERT_NE(-1, pid); |
266 int status = 42; | 268 int status = 42; |
267 ASSERT_EQ(pid, HANDLE_EINTR(waitpid(pid, &status, 0))); | 269 ASSERT_EQ(pid, HANDLE_EINTR(waitpid(pid, &status, 0))); |
268 ASSERT_TRUE(WIFEXITED(status)); | 270 ASSERT_TRUE(WIFEXITED(status)); |
269 EXPECT_EQ(kSuccess, WEXITSTATUS(status)); | 271 EXPECT_EQ(kSuccess, WEXITSTATUS(status)); |
270 } | 272 } |
271 | |
272 #endif | 273 #endif |
273 | 274 |
| 275 #if defined(OS_POSIX) |
| 276 const char kPipeValue = '\xcc'; |
| 277 |
| 278 class ReadFromPipeDelegate : public LaunchOptions::PreExecDelegate { |
| 279 public: |
| 280 explicit ReadFromPipeDelegate(int fd) : fd_(fd) {} |
| 281 ~ReadFromPipeDelegate() override {} |
| 282 void RunAsyncSafe() override { |
| 283 char c; |
| 284 RAW_CHECK(HANDLE_EINTR(read(fd_, &c, 1)) == 1); |
| 285 RAW_CHECK(IGNORE_EINTR(close(fd_)) == 0); |
| 286 RAW_CHECK(c == kPipeValue); |
| 287 } |
| 288 |
| 289 private: |
| 290 int fd_; |
| 291 DISALLOW_COPY_AND_ASSIGN(ReadFromPipeDelegate); |
| 292 }; |
| 293 |
| 294 TEST_F(ProcessTest, PreExecHook) { |
| 295 int pipe_fds[2]; |
| 296 ASSERT_EQ(0, pipe(pipe_fds)); |
| 297 |
| 298 ScopedFD read_fd(pipe_fds[0]); |
| 299 ScopedFD write_fd(pipe_fds[1]); |
| 300 base::FileHandleMappingVector fds_to_remap; |
| 301 fds_to_remap.push_back(std::make_pair(read_fd.get(), read_fd.get())); |
| 302 |
| 303 ReadFromPipeDelegate read_from_pipe_delegate(read_fd.get()); |
| 304 LaunchOptions options; |
| 305 options.fds_to_remap = &fds_to_remap; |
| 306 options.pre_exec_delegate = &read_from_pipe_delegate; |
| 307 Process process(SpawnChildWithOptions("SimpleChildProcess", options)); |
| 308 ASSERT_TRUE(process.IsValid()); |
| 309 |
| 310 read_fd.reset(); |
| 311 ASSERT_EQ(1, HANDLE_EINTR(write(write_fd.get(), &kPipeValue, 1))); |
| 312 |
| 313 int exit_code = 42; |
| 314 EXPECT_TRUE(process.WaitForExit(&exit_code)); |
| 315 EXPECT_EQ(0, exit_code); |
| 316 } |
| 317 #endif // defined(OS_POSIX) |
| 318 |
274 } // namespace base | 319 } // namespace base |
OLD | NEW |