OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/common/multi_process_lock.h" | 5 #include "chrome/common/multi_process_lock.h" |
6 | 6 |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 #include <sys/socket.h> | 8 #include <sys/socket.h> |
9 #include <sys/un.h> | 9 #include <sys/un.h> |
10 #include <unistd.h> | 10 #include <unistd.h> |
11 | 11 |
12 #include "base/eintr_wrapper.h" | 12 #include "base/eintr_wrapper.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 | 14 |
15 class MultiProcessLockLinux : public MultiProcessLock { | 15 class MultiProcessLockLinux : public MultiProcessLock { |
16 public: | 16 public: |
17 explicit MultiProcessLockLinux(const std::string& name) | 17 explicit MultiProcessLockLinux(const std::string& name) |
18 : name_(name), fd_(-1) { } | 18 : name_(name), fd_(-1) { } |
19 | 19 |
20 virtual ~MultiProcessLockLinux() { | 20 virtual ~MultiProcessLockLinux() { |
21 if (fd_ != -1) { | 21 if (fd_ != -1) { |
22 Unlock(); | 22 Unlock(); |
23 } | 23 } |
24 } | 24 } |
25 | 25 |
26 virtual bool TryLock() { | 26 virtual bool TryLock() { |
| 27 struct sockaddr_un address; |
| 28 |
| 29 // +1 for terminator, +1 for 0 in position 0 that makes it an |
| 30 // abstract named socket. |
| 31 const size_t max_len = sizeof(address.sun_path) - 2; |
| 32 |
27 if (fd_ != -1) { | 33 if (fd_ != -1) { |
28 DLOG(ERROR) << "MultiProcessLock is already locked - " << name_; | 34 DLOG(ERROR) << "MultiProcessLock is already locked - " << name_; |
29 return true; | 35 return true; |
30 } | 36 } |
31 | 37 |
32 if (name_.length() > MULTI_PROCESS_LOCK_NAME_MAX_LEN) { | 38 if (name_.length() > max_len) { |
33 LOG(ERROR) << "Socket name too long (" << name_.length() | 39 LOG(ERROR) << "Socket name too long (" << name_.length() |
34 << " > " << MULTI_PROCESS_LOCK_NAME_MAX_LEN << ") - " << name_; | 40 << " > " << max_len << ") - " << name_; |
35 return false; | 41 return false; |
36 } | 42 } |
37 | 43 |
38 struct sockaddr_un address; | |
39 | |
40 // +1 for terminator, +1 for 0 in position 0 that makes it an | |
41 // abstract named socket. | |
42 // If this assert fails it is because sockaddr_un.sun_path size has been | |
43 // redefined and MULTI_PROCESS_LOCK_NAME_MAX_LEN can change accordingly. | |
44 COMPILE_ASSERT(sizeof(address.sun_path) | |
45 == MULTI_PROCESS_LOCK_NAME_MAX_LEN + 2, sun_path_size_changed); | |
46 | |
47 memset(&address, 0, sizeof(address)); | 44 memset(&address, 0, sizeof(address)); |
48 int print_length = snprintf(&address.sun_path[1], | 45 int print_length = snprintf(&address.sun_path[1], |
49 MULTI_PROCESS_LOCK_NAME_MAX_LEN + 1, | 46 max_len + 1, |
50 "%s", name_.c_str()); | 47 "%s", name_.c_str()); |
51 | 48 |
52 if (print_length < 0 || | 49 if (print_length < 0 || |
53 print_length > static_cast<int>(MULTI_PROCESS_LOCK_NAME_MAX_LEN)) { | 50 print_length > static_cast<int>(max_len)) { |
54 PLOG(ERROR) << "Couldn't create sun_path - " << name_; | 51 PLOG(ERROR) << "Couldn't create sun_path - " << name_; |
55 return false; | 52 return false; |
56 } | 53 } |
57 | 54 |
58 // Must set the first character of the path to something non-zero | 55 // Must set the first character of the path to something non-zero |
59 // before we call SUN_LEN which depends on strcpy working. | 56 // before we call SUN_LEN which depends on strcpy working. |
60 address.sun_path[0] = '@'; | 57 address.sun_path[0] = '@'; |
61 size_t length = SUN_LEN(&address); | 58 size_t length = SUN_LEN(&address); |
62 | 59 |
63 // Reset the first character of the path back to zero so that | 60 // Reset the first character of the path back to zero so that |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 | 97 |
101 private: | 98 private: |
102 std::string name_; | 99 std::string name_; |
103 int fd_; | 100 int fd_; |
104 DISALLOW_COPY_AND_ASSIGN(MultiProcessLockLinux); | 101 DISALLOW_COPY_AND_ASSIGN(MultiProcessLockLinux); |
105 }; | 102 }; |
106 | 103 |
107 MultiProcessLock* MultiProcessLock::Create(const std::string &name) { | 104 MultiProcessLock* MultiProcessLock::Create(const std::string &name) { |
108 return new MultiProcessLockLinux(name); | 105 return new MultiProcessLockLinux(name); |
109 } | 106 } |
OLD | NEW |