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