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 "chrome/browser/process_singleton.h" | 5 #include "chrome/browser/process_singleton.h" |
6 | 6 |
7 #include <fcntl.h> | 7 #include <fcntl.h> |
8 #include <limits.h> | 8 #include <limits.h> |
9 #include <signal.h> | 9 #include <signal.h> |
10 #include <stddef.h> | 10 #include <stddef.h> |
11 #include <sys/types.h> | 11 #include <sys/types.h> |
12 #include <sys/un.h> | 12 #include <sys/un.h> |
13 #include <sys/wait.h> | 13 #include <sys/wait.h> |
14 #include <unistd.h> | 14 #include <unistd.h> |
15 | 15 |
16 #include <memory> | 16 #include <memory> |
17 #include <string> | 17 #include <string> |
18 #include <vector> | 18 #include <vector> |
19 | 19 |
20 #include "base/bind.h" | 20 #include "base/bind.h" |
21 #include "base/command_line.h" | 21 #include "base/command_line.h" |
22 #include "base/files/file_path.h" | 22 #include "base/files/file_path.h" |
23 #include "base/files/file_util.h" | 23 #include "base/files/file_util.h" |
24 #include "base/files/scoped_temp_dir.h" | 24 #include "base/files/scoped_temp_dir.h" |
25 #include "base/location.h" | 25 #include "base/location.h" |
26 #include "base/posix/eintr_wrapper.h" | 26 #include "base/posix/eintr_wrapper.h" |
27 #include "base/single_thread_task_runner.h" | 27 #include "base/single_thread_task_runner.h" |
28 #include "base/strings/stringprintf.h" | 28 #include "base/strings/stringprintf.h" |
29 #include "base/synchronization/waitable_event.h" | 29 #include "base/synchronization/waitable_event.h" |
| 30 #include "base/test/histogram_tester.h" |
30 #include "base/test/test_timeouts.h" | 31 #include "base/test/test_timeouts.h" |
31 #include "base/test/thread_test_helper.h" | 32 #include "base/test/thread_test_helper.h" |
32 #include "base/threading/thread.h" | 33 #include "base/threading/thread.h" |
33 #include "build/build_config.h" | 34 #include "build/build_config.h" |
34 #include "chrome/common/chrome_constants.h" | 35 #include "chrome/common/chrome_constants.h" |
35 #include "content/public/browser/browser_thread.h" | 36 #include "content/public/browser/browser_thread.h" |
36 #include "content/public/test/test_browser_thread_bundle.h" | 37 #include "content/public/test/test_browser_thread_bundle.h" |
37 #include "net/base/network_interfaces.h" | 38 #include "net/base/network_interfaces.h" |
38 #include "testing/gtest/include/gtest/gtest.h" | 39 #include "testing/gtest/include/gtest/gtest.h" |
39 | 40 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 wait_event_(base::WaitableEvent::ResetPolicy::MANUAL, | 75 wait_event_(base::WaitableEvent::ResetPolicy::MANUAL, |
75 base::WaitableEvent::InitialState::NOT_SIGNALED), | 76 base::WaitableEvent::InitialState::NOT_SIGNALED), |
76 signal_event_(base::WaitableEvent::ResetPolicy::MANUAL, | 77 signal_event_(base::WaitableEvent::ResetPolicy::MANUAL, |
77 base::WaitableEvent::InitialState::NOT_SIGNALED), | 78 base::WaitableEvent::InitialState::NOT_SIGNALED), |
78 process_singleton_on_thread_(NULL) {} | 79 process_singleton_on_thread_(NULL) {} |
79 | 80 |
80 void SetUp() override { | 81 void SetUp() override { |
81 testing::Test::SetUp(); | 82 testing::Test::SetUp(); |
82 | 83 |
83 ProcessSingleton::DisablePromptForTesting(); | 84 ProcessSingleton::DisablePromptForTesting(); |
| 85 ProcessSingleton::SkipIsChromeProcessCheckForTesting(false); |
84 // Put the lock in a temporary directory. Doesn't need to be a | 86 // Put the lock in a temporary directory. Doesn't need to be a |
85 // full profile to test this code. | 87 // full profile to test this code. |
86 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 88 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
87 // Use a long directory name to ensure that the socket isn't opened through | 89 // Use a long directory name to ensure that the socket isn't opened through |
88 // the symlink. | 90 // the symlink. |
89 user_data_path_ = temp_dir_.GetPath().Append( | 91 user_data_path_ = temp_dir_.GetPath().Append( |
90 std::string(sizeof(sockaddr_un::sun_path), 'a')); | 92 std::string(sizeof(sockaddr_un::sun_path), 'a')); |
91 ASSERT_TRUE(CreateDirectory(user_data_path_)); | 93 ASSERT_TRUE(CreateDirectory(user_data_path_)); |
92 | 94 |
93 lock_path_ = user_data_path_.Append(chrome::kSingletonLockFilename); | 95 lock_path_ = user_data_path_.Append(chrome::kSingletonLockFilename); |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 // TODO(james.su@gmail.com): port following tests to Windows. | 281 // TODO(james.su@gmail.com): port following tests to Windows. |
280 // Test success case of NotifyOtherProcess(). | 282 // Test success case of NotifyOtherProcess(). |
281 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessSuccess) { | 283 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessSuccess) { |
282 CreateProcessSingletonOnThread(); | 284 CreateProcessSingletonOnThread(); |
283 EXPECT_EQ(ProcessSingleton::PROCESS_NOTIFIED, NotifyOtherProcess(true)); | 285 EXPECT_EQ(ProcessSingleton::PROCESS_NOTIFIED, NotifyOtherProcess(true)); |
284 CheckNotified(); | 286 CheckNotified(); |
285 } | 287 } |
286 | 288 |
287 // Test failure case of NotifyOtherProcess(). | 289 // Test failure case of NotifyOtherProcess(). |
288 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessFailure) { | 290 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessFailure) { |
| 291 base::HistogramTester histogram_tester; |
289 CreateProcessSingletonOnThread(); | 292 CreateProcessSingletonOnThread(); |
290 | 293 |
291 BlockWorkerThread(); | 294 BlockWorkerThread(); |
292 EXPECT_EQ(ProcessSingleton::PROCESS_NONE, NotifyOtherProcess(true)); | 295 EXPECT_EQ(ProcessSingleton::PROCESS_NONE, NotifyOtherProcess(true)); |
293 ASSERT_EQ(1, kill_callbacks_); | 296 ASSERT_EQ(1, kill_callbacks_); |
294 UnblockWorkerThread(); | 297 UnblockWorkerThread(); |
| 298 histogram_tester.ExpectUniqueSample( |
| 299 "Chrome.ProcessSingleton.RemoteHungProcessTerminateReason", |
| 300 ProcessSingleton::SOCKET_READ_FAILED, 1u); |
295 } | 301 } |
296 | 302 |
297 // Test that we don't kill ourselves by accident if a lockfile with the same pid | 303 // Test that we don't kill ourselves by accident if a lockfile with the same pid |
298 // happens to exist. | 304 // happens to exist. |
299 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessNoSuicide) { | 305 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessNoSuicide) { |
| 306 base::HistogramTester histogram_tester; |
300 CreateProcessSingletonOnThread(); | 307 CreateProcessSingletonOnThread(); |
301 // Replace lockfile with one containing our own pid. | 308 // Replace lockfile with one containing our own pid. |
302 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); | 309 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); |
303 std::string symlink_content = base::StringPrintf( | 310 std::string symlink_content = base::StringPrintf( |
304 "%s%c%u", | 311 "%s%c%u", |
305 net::GetHostName().c_str(), | 312 net::GetHostName().c_str(), |
306 '-', | 313 '-', |
307 base::GetCurrentProcId()); | 314 base::GetCurrentProcId()); |
308 EXPECT_EQ(0, symlink(symlink_content.c_str(), lock_path_.value().c_str())); | 315 EXPECT_EQ(0, symlink(symlink_content.c_str(), lock_path_.value().c_str())); |
309 | 316 |
310 // Remove socket so that we will not be able to notify the existing browser. | 317 // Remove socket so that we will not be able to notify the existing browser. |
311 EXPECT_EQ(0, unlink(socket_path_.value().c_str())); | 318 EXPECT_EQ(0, unlink(socket_path_.value().c_str())); |
312 | 319 |
| 320 // Pretend we are browser process. |
| 321 ProcessSingleton::SkipIsChromeProcessCheckForTesting(true); |
| 322 |
313 EXPECT_EQ(ProcessSingleton::PROCESS_NONE, NotifyOtherProcess(false)); | 323 EXPECT_EQ(ProcessSingleton::PROCESS_NONE, NotifyOtherProcess(false)); |
314 // If we've gotten to this point without killing ourself, the test succeeded. | 324 // If we've gotten to this point without killing ourself, the test succeeded. |
| 325 histogram_tester.ExpectUniqueSample( |
| 326 "Chrome.ProcessSingleton.RemoteProcessInteractionResult", |
| 327 ProcessSingleton::SAME_BROWSER_INSTANCE, 1u); |
315 } | 328 } |
316 | 329 |
317 // Test that we can still notify a process on the same host even after the | 330 // Test that we can still notify a process on the same host even after the |
318 // hostname changed. | 331 // hostname changed. |
319 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessHostChanged) { | 332 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessHostChanged) { |
320 CreateProcessSingletonOnThread(); | 333 CreateProcessSingletonOnThread(); |
321 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); | 334 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); |
322 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str())); | 335 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str())); |
323 | 336 |
324 EXPECT_EQ(ProcessSingleton::PROCESS_NOTIFIED, NotifyOtherProcess(false)); | 337 EXPECT_EQ(ProcessSingleton::PROCESS_NOTIFIED, NotifyOtherProcess(false)); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 | 414 |
402 // Also change the hostname, so the remote does not retry. | 415 // Also change the hostname, so the remote does not retry. |
403 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); | 416 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); |
404 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str())); | 417 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str())); |
405 | 418 |
406 std::string url("about:blank"); | 419 std::string url("about:blank"); |
407 EXPECT_EQ(ProcessSingleton::PROFILE_IN_USE, NotifyOtherProcessOrCreate(url)); | 420 EXPECT_EQ(ProcessSingleton::PROFILE_IN_USE, NotifyOtherProcessOrCreate(url)); |
408 } | 421 } |
409 | 422 |
410 TEST_F(ProcessSingletonPosixTest, IgnoreSocketSymlinkWithTooLongTarget) { | 423 TEST_F(ProcessSingletonPosixTest, IgnoreSocketSymlinkWithTooLongTarget) { |
| 424 base::HistogramTester histogram_tester; |
411 CreateProcessSingletonOnThread(); | 425 CreateProcessSingletonOnThread(); |
412 // Change the symlink to one with a too-long target. | 426 // Change the symlink to one with a too-long target. |
413 char buf[PATH_MAX]; | 427 char buf[PATH_MAX]; |
414 ssize_t len = readlink(socket_path_.value().c_str(), buf, PATH_MAX); | 428 ssize_t len = readlink(socket_path_.value().c_str(), buf, PATH_MAX); |
415 ASSERT_GT(len, 0); | 429 ASSERT_GT(len, 0); |
416 base::FilePath socket_target_path = base::FilePath(std::string(buf, len)); | 430 base::FilePath socket_target_path = base::FilePath(std::string(buf, len)); |
417 base::FilePath long_socket_target_path = socket_target_path.DirName().Append( | 431 base::FilePath long_socket_target_path = socket_target_path.DirName().Append( |
418 std::string(sizeof(sockaddr_un::sun_path), 'b')); | 432 std::string(sizeof(sockaddr_un::sun_path), 'b')); |
419 ASSERT_EQ(0, unlink(socket_path_.value().c_str())); | 433 ASSERT_EQ(0, unlink(socket_path_.value().c_str())); |
420 ASSERT_EQ(0, symlink(long_socket_target_path.value().c_str(), | 434 ASSERT_EQ(0, symlink(long_socket_target_path.value().c_str(), |
421 socket_path_.value().c_str())); | 435 socket_path_.value().c_str())); |
422 | 436 |
423 // A new ProcessSingleton should ignore the invalid socket path target. | 437 // A new ProcessSingleton should ignore the invalid socket path target. |
424 std::string url("about:blank"); | 438 std::string url("about:blank"); |
425 EXPECT_EQ(ProcessSingleton::PROCESS_NONE, NotifyOtherProcessOrCreate(url)); | 439 EXPECT_EQ(ProcessSingleton::PROCESS_NONE, NotifyOtherProcessOrCreate(url)); |
| 440 |
| 441 // Lock file contains PID of unit_tests process. It is non browser process so |
| 442 // we treat lock file as orphaned. |
| 443 histogram_tester.ExpectUniqueSample( |
| 444 "Chrome.ProcessSingleton.RemoteProcessInteractionResult", |
| 445 ProcessSingleton::ORPHANED_LOCK_FILE, 1u); |
426 } | 446 } |
427 | 447 |
428 #if defined(OS_MACOSX) | 448 #if defined(OS_MACOSX) |
429 // Test that if there is an existing lock file, and we could not flock() | 449 // Test that if there is an existing lock file, and we could not flock() |
430 // it, then exit. | 450 // it, then exit. |
431 TEST_F(ProcessSingletonPosixTest, CreateRespectsOldMacLock) { | 451 TEST_F(ProcessSingletonPosixTest, CreateRespectsOldMacLock) { |
432 std::unique_ptr<TestableProcessSingleton> process_singleton( | 452 std::unique_ptr<TestableProcessSingleton> process_singleton( |
433 CreateProcessSingleton()); | 453 CreateProcessSingleton()); |
434 base::ScopedFD lock_fd(HANDLE_EINTR( | 454 base::ScopedFD lock_fd(HANDLE_EINTR( |
435 open(lock_path_.value().c_str(), O_RDWR | O_CREAT | O_EXLOCK, 0644))); | 455 open(lock_path_.value().c_str(), O_RDWR | O_CREAT | O_EXLOCK, 0644))); |
436 ASSERT_TRUE(lock_fd.is_valid()); | 456 ASSERT_TRUE(lock_fd.is_valid()); |
437 EXPECT_FALSE(process_singleton->Create()); | 457 EXPECT_FALSE(process_singleton->Create()); |
438 base::File::Info info; | 458 base::File::Info info; |
439 EXPECT_TRUE(base::GetFileInfo(lock_path_, &info)); | 459 EXPECT_TRUE(base::GetFileInfo(lock_path_, &info)); |
440 EXPECT_FALSE(info.is_directory); | 460 EXPECT_FALSE(info.is_directory); |
441 EXPECT_FALSE(info.is_symbolic_link); | 461 EXPECT_FALSE(info.is_symbolic_link); |
442 } | 462 } |
443 | 463 |
444 // Test that if there is an existing lock file, and it's not locked, we replace | 464 // Test that if there is an existing lock file, and it's not locked, we replace |
445 // it. | 465 // it. |
446 TEST_F(ProcessSingletonPosixTest, CreateReplacesOldMacLock) { | 466 TEST_F(ProcessSingletonPosixTest, CreateReplacesOldMacLock) { |
447 std::unique_ptr<TestableProcessSingleton> process_singleton( | 467 std::unique_ptr<TestableProcessSingleton> process_singleton( |
448 CreateProcessSingleton()); | 468 CreateProcessSingleton()); |
449 EXPECT_EQ(0, base::WriteFile(lock_path_, "", 0)); | 469 EXPECT_EQ(0, base::WriteFile(lock_path_, "", 0)); |
450 EXPECT_TRUE(process_singleton->Create()); | 470 EXPECT_TRUE(process_singleton->Create()); |
451 VerifyFiles(); | 471 VerifyFiles(); |
452 } | 472 } |
453 #endif // defined(OS_MACOSX) | 473 #endif // defined(OS_MACOSX) |
OLD | NEW |