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 #include <errno.h> | 5 #include <errno.h> |
6 #include <fcntl.h> | 6 #include <fcntl.h> |
7 #include <sys/file.h> | 7 #include <sys/file.h> |
8 | 8 |
9 #include "chrome/browser/process_singleton.h" | 9 #include "chrome/browser/process_singleton.h" |
10 | 10 |
11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/files/scoped_file.h" |
12 #include "base/path_service.h" | 13 #include "base/path_service.h" |
13 #include "base/posix/eintr_wrapper.h" | 14 #include "base/posix/eintr_wrapper.h" |
14 #include "chrome/common/chrome_constants.h" | 15 #include "chrome/common/chrome_constants.h" |
15 #include "chrome/common/chrome_paths.h" | 16 #include "chrome/common/chrome_paths.h" |
16 #include "chrome/test/base/testing_profile.h" | 17 #include "chrome/test/base/testing_profile.h" |
17 #include "testing/platform_test.h" | 18 #include "testing/platform_test.h" |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 class ProcessSingletonMacTest : public PlatformTest { | 22 class ProcessSingletonMacTest : public PlatformTest { |
(...skipping 10 matching lines...) Expand all Loading... |
32 virtual void TearDown() { | 33 virtual void TearDown() { |
33 PlatformTest::TearDown(); | 34 PlatformTest::TearDown(); |
34 | 35 |
35 // Verify that the lock was released. | 36 // Verify that the lock was released. |
36 EXPECT_FALSE(IsLocked()); | 37 EXPECT_FALSE(IsLocked()); |
37 } | 38 } |
38 | 39 |
39 // Return |true| if the file exists and is locked. Forces a failure | 40 // Return |true| if the file exists and is locked. Forces a failure |
40 // in the containing test in case of error condition. | 41 // in the containing test in case of error condition. |
41 bool IsLocked() { | 42 bool IsLocked() { |
42 int fd = HANDLE_EINTR(open(lock_path_.value().c_str(), O_RDONLY)); | 43 base::ScopedFD fd(HANDLE_EINTR(open(lock_path_.value().c_str(), O_RDONLY))); |
43 if (fd == -1) { | 44 if (!fd) { |
44 EXPECT_EQ(ENOENT, errno) << "Unexpected error opening lockfile."; | 45 EXPECT_EQ(ENOENT, errno) << "Unexpected error opening lockfile."; |
45 return false; | 46 return false; |
46 } | 47 } |
47 | 48 |
48 file_util::ScopedFD auto_close(&fd); | 49 int rc = HANDLE_EINTR(flock(fd.get(), LOCK_EX|LOCK_NB)); |
49 | |
50 int rc = HANDLE_EINTR(flock(fd, LOCK_EX|LOCK_NB)); | |
51 | 50 |
52 // Got the lock, so it wasn't already locked. Close releases. | 51 // Got the lock, so it wasn't already locked. Close releases. |
53 if (rc != -1) | 52 if (rc != -1) |
54 return false; | 53 return false; |
55 | 54 |
56 // Someone else has the lock. | 55 // Someone else has the lock. |
57 if (errno == EWOULDBLOCK) | 56 if (errno == EWOULDBLOCK) |
58 return true; | 57 return true; |
59 | 58 |
60 EXPECT_EQ(EWOULDBLOCK, errno) << "Unexpected error acquiring lock."; | 59 EXPECT_EQ(EWOULDBLOCK, errno) << "Unexpected error acquiring lock."; |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 ps2.Cleanup(); | 152 ps2.Cleanup(); |
154 EXPECT_FALSE(IsLocked()); | 153 EXPECT_FALSE(IsLocked()); |
155 } | 154 } |
156 | 155 |
157 // TODO(shess): Test that the lock is released when the process dies. | 156 // TODO(shess): Test that the lock is released when the process dies. |
158 // DEATH_TEST? I don't know. If the code to communicate between | 157 // DEATH_TEST? I don't know. If the code to communicate between |
159 // browser processes is ever written, this all would need to be tested | 158 // browser processes is ever written, this all would need to be tested |
160 // more like the other platforms, in which case it would be easy. | 159 // more like the other platforms, in which case it would be easy. |
161 | 160 |
162 } // namespace | 161 } // namespace |
OLD | NEW |