OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #define _CRT_SECURE_NO_WARNINGS | 5 #define _CRT_SECURE_NO_WARNINGS |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/debug/alias.h" | 10 #include "base/debug/alias.h" |
11 #include "base/debug/stack_trace.h" | 11 #include "base/debug/stack_trace.h" |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/files/file_util.h" |
| 14 #include "base/files/scoped_file.h" |
13 #include "base/logging.h" | 15 #include "base/logging.h" |
14 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
15 #include "base/path_service.h" | 17 #include "base/path_service.h" |
16 #include "base/posix/eintr_wrapper.h" | 18 #include "base/posix/eintr_wrapper.h" |
17 #include "base/process/kill.h" | 19 #include "base/process/kill.h" |
18 #include "base/process/launch.h" | 20 #include "base/process/launch.h" |
19 #include "base/process/memory.h" | 21 #include "base/process/memory.h" |
20 #include "base/process/process.h" | 22 #include "base/process/process.h" |
21 #include "base/process/process_metrics.h" | 23 #include "base/process/process_metrics.h" |
22 #include "base/strings/string_number_conversions.h" | 24 #include "base/strings/string_number_conversions.h" |
23 #include "base/strings/utf_string_conversions.h" | 25 #include "base/strings/utf_string_conversions.h" |
24 #include "base/synchronization/waitable_event.h" | 26 #include "base/synchronization/waitable_event.h" |
25 #include "base/test/multiprocess_test.h" | 27 #include "base/test/multiprocess_test.h" |
26 #include "base/test/test_timeouts.h" | 28 #include "base/test/test_timeouts.h" |
27 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 29 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
28 #include "base/threading/platform_thread.h" | 30 #include "base/threading/platform_thread.h" |
29 #include "base/threading/thread.h" | 31 #include "base/threading/thread.h" |
| 32 #include "build/build_config.h" |
30 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
31 #include "testing/multiprocess_func_list.h" | 34 #include "testing/multiprocess_func_list.h" |
32 | 35 |
33 #if defined(OS_LINUX) | 36 #if defined(OS_LINUX) |
34 #include <malloc.h> | 37 #include <malloc.h> |
35 #include <sched.h> | 38 #include <sched.h> |
| 39 #include <sys/syscall.h> |
36 #endif | 40 #endif |
37 #if defined(OS_POSIX) | 41 #if defined(OS_POSIX) |
38 #include <dlfcn.h> | 42 #include <dlfcn.h> |
39 #include <errno.h> | 43 #include <errno.h> |
40 #include <fcntl.h> | 44 #include <fcntl.h> |
| 45 #include <sched.h> |
41 #include <signal.h> | 46 #include <signal.h> |
42 #include <sys/resource.h> | 47 #include <sys/resource.h> |
43 #include <sys/socket.h> | 48 #include <sys/socket.h> |
| 49 #include <sys/types.h> |
44 #include <sys/wait.h> | 50 #include <sys/wait.h> |
| 51 #include <unistd.h> |
45 #endif | 52 #endif |
46 #if defined(OS_WIN) | 53 #if defined(OS_WIN) |
47 #include <windows.h> | 54 #include <windows.h> |
48 #include "base/win/windows_version.h" | 55 #include "base/win/windows_version.h" |
49 #endif | 56 #endif |
50 #if defined(OS_MACOSX) | 57 #if defined(OS_MACOSX) |
51 #include <mach/vm_param.h> | 58 #include <mach/vm_param.h> |
52 #include <malloc/malloc.h> | 59 #include <malloc/malloc.h> |
53 #include "base/mac/mac_util.h" | 60 #include "base/mac/mac_util.h" |
54 #endif | 61 #endif |
(...skipping 851 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
906 base::EnsureProcessTerminated(child_process.Duplicate()); | 913 base::EnsureProcessTerminated(child_process.Duplicate()); |
907 | 914 |
908 // Check that process was really killed. | 915 // Check that process was really killed. |
909 EXPECT_TRUE(IsProcessDead(child_process.Handle())); | 916 EXPECT_TRUE(IsProcessDead(child_process.Handle())); |
910 } | 917 } |
911 | 918 |
912 MULTIPROCESS_TEST_MAIN(process_util_test_die_immediately) { | 919 MULTIPROCESS_TEST_MAIN(process_util_test_die_immediately) { |
913 return 0; | 920 return 0; |
914 } | 921 } |
915 | 922 |
| 923 #if !defined(OS_ANDROID) |
| 924 const char kPipeValue = '\xcc'; |
| 925 |
| 926 class ReadFromPipeDelegate : public base::LaunchOptions::PreExecDelegate { |
| 927 public: |
| 928 explicit ReadFromPipeDelegate(int fd) : fd_(fd) {} |
| 929 ~ReadFromPipeDelegate() override {} |
| 930 void RunAsyncSafe() override { |
| 931 char c; |
| 932 RAW_CHECK(HANDLE_EINTR(read(fd_, &c, 1)) == 1); |
| 933 RAW_CHECK(IGNORE_EINTR(close(fd_)) == 0); |
| 934 RAW_CHECK(c == kPipeValue); |
| 935 } |
| 936 |
| 937 private: |
| 938 int fd_; |
| 939 DISALLOW_COPY_AND_ASSIGN(ReadFromPipeDelegate); |
| 940 }; |
| 941 |
| 942 TEST_F(ProcessUtilTest, PreExecHook) { |
| 943 int pipe_fds[2]; |
| 944 ASSERT_EQ(0, pipe(pipe_fds)); |
| 945 |
| 946 base::ScopedFD read_fd(pipe_fds[0]); |
| 947 base::ScopedFD write_fd(pipe_fds[1]); |
| 948 base::FileHandleMappingVector fds_to_remap; |
| 949 fds_to_remap.push_back(std::make_pair(read_fd.get(), read_fd.get())); |
| 950 |
| 951 ReadFromPipeDelegate read_from_pipe_delegate(read_fd.get()); |
| 952 base::LaunchOptions options; |
| 953 options.fds_to_remap = &fds_to_remap; |
| 954 options.pre_exec_delegate = &read_from_pipe_delegate; |
| 955 base::Process process(SpawnChildWithOptions("SimpleChildProcess", options)); |
| 956 ASSERT_TRUE(process.IsValid()); |
| 957 |
| 958 read_fd.reset(); |
| 959 ASSERT_EQ(1, HANDLE_EINTR(write(write_fd.get(), &kPipeValue, 1))); |
| 960 |
| 961 int exit_code = 42; |
| 962 EXPECT_TRUE(process.WaitForExit(&exit_code)); |
| 963 EXPECT_EQ(0, exit_code); |
| 964 } |
| 965 #endif // !defined(OS_ANDROID) |
| 966 |
916 #endif // defined(OS_POSIX) | 967 #endif // defined(OS_POSIX) |
| 968 |
| 969 #if defined(OS_LINUX) |
| 970 const int kSuccess = 0; |
| 971 |
| 972 MULTIPROCESS_TEST_MAIN(CheckPidProcess) { |
| 973 const pid_t kInitPid = 1; |
| 974 const pid_t pid = syscall(__NR_getpid); |
| 975 CHECK(pid == kInitPid); |
| 976 CHECK(getpid() == pid); |
| 977 return kSuccess; |
| 978 } |
| 979 |
| 980 TEST_F(ProcessUtilTest, CloneFlags) { |
| 981 if (RunningOnValgrind() || |
| 982 !base::PathExists(FilePath("/proc/self/ns/user")) || |
| 983 !base::PathExists(FilePath("/proc/self/ns/pid"))) { |
| 984 // User or PID namespaces are not supported. |
| 985 return; |
| 986 } |
| 987 |
| 988 base::LaunchOptions options; |
| 989 options.clone_flags = CLONE_NEWUSER | CLONE_NEWPID; |
| 990 |
| 991 base::Process process(SpawnChildWithOptions("CheckPidProcess", options)); |
| 992 ASSERT_TRUE(process.IsValid()); |
| 993 |
| 994 int exit_code = 42; |
| 995 EXPECT_TRUE(process.WaitForExit(&exit_code)); |
| 996 EXPECT_EQ(kSuccess, exit_code); |
| 997 } |
| 998 |
| 999 TEST(ForkWithFlagsTest, UpdatesPidCache) { |
| 1000 // The libc clone function, which allows ForkWithFlags to keep the pid cache |
| 1001 // up to date, does not work on Valgrind. |
| 1002 if (RunningOnValgrind()) { |
| 1003 return; |
| 1004 } |
| 1005 |
| 1006 // Warm up the libc pid cache, if there is one. |
| 1007 ASSERT_EQ(syscall(__NR_getpid), getpid()); |
| 1008 |
| 1009 pid_t ctid = 0; |
| 1010 const pid_t pid = |
| 1011 base::ForkWithFlags(SIGCHLD | CLONE_CHILD_SETTID, nullptr, &ctid); |
| 1012 if (pid == 0) { |
| 1013 // In child. Check both the raw getpid syscall and the libc getpid wrapper |
| 1014 // (which may rely on a pid cache). |
| 1015 RAW_CHECK(syscall(__NR_getpid) == ctid); |
| 1016 RAW_CHECK(getpid() == ctid); |
| 1017 _exit(kSuccess); |
| 1018 } |
| 1019 |
| 1020 ASSERT_NE(-1, pid); |
| 1021 int status = 42; |
| 1022 ASSERT_EQ(pid, HANDLE_EINTR(waitpid(pid, &status, 0))); |
| 1023 ASSERT_TRUE(WIFEXITED(status)); |
| 1024 EXPECT_EQ(kSuccess, WEXITSTATUS(status)); |
| 1025 } |
| 1026 #endif |
OLD | NEW |