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" |
| 8 #include "base/files/file_util.h" |
7 #include "base/process/kill.h" | 9 #include "base/process/kill.h" |
8 #include "base/test/multiprocess_test.h" | 10 #include "base/test/multiprocess_test.h" |
9 #include "base/test/test_timeouts.h" | 11 #include "base/test/test_timeouts.h" |
| 12 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
10 #include "base/threading/platform_thread.h" | 13 #include "base/threading/platform_thread.h" |
| 14 #include "build/build_config.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
12 #include "testing/multiprocess_func_list.h" | 16 #include "testing/multiprocess_func_list.h" |
13 | 17 |
| 18 #if defined(OS_LINUX) |
| 19 #include <errno.h> |
| 20 #include <sched.h> |
| 21 #include <sys/syscall.h> |
| 22 #include <sys/types.h> |
| 23 #include <sys/wait.h> |
| 24 #include <unistd.h> |
| 25 #endif |
14 | 26 |
15 namespace { | 27 namespace { |
16 | 28 |
17 #if defined(OS_WIN) | 29 #if defined(OS_WIN) |
18 const int kExpectedStillRunningExitCode = 0x102; | 30 const int kExpectedStillRunningExitCode = 0x102; |
19 #else | 31 #else |
20 const int kExpectedStillRunningExitCode = 0; | 32 const int kExpectedStillRunningExitCode = 0; |
21 #endif | 33 #endif |
22 | 34 |
23 } // namespace | 35 } // namespace |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 EXPECT_TRUE(process.SetProcessBackgrounded(false)); | 206 EXPECT_TRUE(process.SetProcessBackgrounded(false)); |
195 EXPECT_FALSE(process.IsProcessBackgrounded()); | 207 EXPECT_FALSE(process.IsProcessBackgrounded()); |
196 #else | 208 #else |
197 process.SetProcessBackgrounded(true); | 209 process.SetProcessBackgrounded(true); |
198 process.SetProcessBackgrounded(false); | 210 process.SetProcessBackgrounded(false); |
199 #endif | 211 #endif |
200 int new_priority = process.GetPriority(); | 212 int new_priority = process.GetPriority(); |
201 EXPECT_EQ(old_priority, new_priority); | 213 EXPECT_EQ(old_priority, new_priority); |
202 } | 214 } |
203 | 215 |
| 216 #if defined(OS_LINUX) |
| 217 const int kSuccess = 0; |
| 218 |
| 219 MULTIPROCESS_TEST_MAIN(CheckPidProcess) { |
| 220 const pid_t kInitPid = 1; |
| 221 const pid_t pid = syscall(__NR_getpid); |
| 222 CHECK(pid == kInitPid); |
| 223 CHECK(getpid() == pid); |
| 224 return kSuccess; |
| 225 } |
| 226 |
| 227 TEST_F(ProcessTest, CloneFlags) { |
| 228 if (RunningOnValgrind() || !PathExists(FilePath("/proc/self/ns/user")) || |
| 229 !PathExists(FilePath("/proc/self/ns/pid"))) { |
| 230 // User or PID namespaces are not supported. |
| 231 return; |
| 232 } |
| 233 |
| 234 LaunchOptions options; |
| 235 options.clone_flags = CLONE_NEWUSER | CLONE_NEWPID; |
| 236 |
| 237 Process process(SpawnChildWithOptions("CheckPidProcess", options)); |
| 238 ASSERT_TRUE(process.IsValid()); |
| 239 |
| 240 int exit_code = 42; |
| 241 EXPECT_TRUE(process.WaitForExit(&exit_code)); |
| 242 EXPECT_EQ(kSuccess, exit_code); |
| 243 } |
| 244 |
| 245 TEST(ForkWithFlagsTest, UpdatesPidCache) { |
| 246 // The libc clone function, which allows ForkWithFlags to keep the pid cache |
| 247 // up to date, does not work on Valgrind. |
| 248 if (RunningOnValgrind()) { |
| 249 return; |
| 250 } |
| 251 |
| 252 // Warm up the libc pid cache, if there is one. |
| 253 ASSERT_EQ(syscall(__NR_getpid), getpid()); |
| 254 |
| 255 pid_t ctid = 0; |
| 256 const pid_t pid = ForkWithFlags(SIGCHLD | CLONE_CHILD_SETTID, nullptr, &ctid); |
| 257 if (pid == 0) { |
| 258 // In child. Check both the raw getpid syscall and the libc getpid wrapper |
| 259 // (which may rely on a pid cache). |
| 260 RAW_CHECK(syscall(__NR_getpid) == ctid); |
| 261 RAW_CHECK(getpid() == ctid); |
| 262 _exit(kSuccess); |
| 263 } |
| 264 |
| 265 ASSERT_NE(-1, pid); |
| 266 int status = 42; |
| 267 ASSERT_EQ(pid, HANDLE_EINTR(waitpid(pid, &status, 0))); |
| 268 ASSERT_TRUE(WIFEXITED(status)); |
| 269 EXPECT_EQ(kSuccess, WEXITSTATUS(status)); |
| 270 } |
| 271 |
| 272 #endif |
| 273 |
204 } // namespace base | 274 } // namespace base |
OLD | NEW |