| 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/compiler_specific.h" |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 13 #include "base/posix/eintr_wrapper.h" | 14 #include "base/posix/eintr_wrapper.h" |
| 14 | 15 |
| 15 class MultiProcessLockLinux : public MultiProcessLock { | 16 class MultiProcessLockLinux : public MultiProcessLock { |
| 16 public: | 17 public: |
| 17 explicit MultiProcessLockLinux(const std::string& name) | 18 explicit MultiProcessLockLinux(const std::string& name) |
| 18 : name_(name), fd_(-1) { } | 19 : name_(name), fd_(-1) { } |
| 19 | 20 |
| 20 virtual ~MultiProcessLockLinux() { | 21 virtual ~MultiProcessLockLinux() { |
| 21 if (fd_ != -1) { | 22 if (fd_ != -1) { |
| 22 Unlock(); | 23 Unlock(); |
| 23 } | 24 } |
| 24 } | 25 } |
| 25 | 26 |
| 26 virtual bool TryLock() { | 27 virtual bool TryLock() OVERRIDE { |
| 27 struct sockaddr_un address; | 28 struct sockaddr_un address; |
| 28 | 29 |
| 29 // +1 for terminator, +1 for 0 in position 0 that makes it an | 30 // +1 for terminator, +1 for 0 in position 0 that makes it an |
| 30 // abstract named socket. | 31 // abstract named socket. |
| 31 const size_t max_len = sizeof(address.sun_path) - 2; | 32 const size_t max_len = sizeof(address.sun_path) - 2; |
| 32 | 33 |
| 33 if (fd_ != -1) { | 34 if (fd_ != -1) { |
| 34 DLOG(ERROR) << "MultiProcessLock is already locked - " << name_; | 35 DLOG(ERROR) << "MultiProcessLock is already locked - " << name_; |
| 35 return true; | 36 return true; |
| 36 } | 37 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 DVLOG(1) << "Couldn't bind socket - " | 78 DVLOG(1) << "Couldn't bind socket - " |
| 78 << &(address.sun_path[1]) | 79 << &(address.sun_path[1]) |
| 79 << " Length: " << length; | 80 << " Length: " << length; |
| 80 if (HANDLE_EINTR(close(socket_fd)) < 0) { | 81 if (HANDLE_EINTR(close(socket_fd)) < 0) { |
| 81 PLOG(ERROR) << "close"; | 82 PLOG(ERROR) << "close"; |
| 82 } | 83 } |
| 83 return false; | 84 return false; |
| 84 } | 85 } |
| 85 } | 86 } |
| 86 | 87 |
| 87 virtual void Unlock() { | 88 virtual void Unlock() OVERRIDE { |
| 88 if (fd_ == -1) { | 89 if (fd_ == -1) { |
| 89 DLOG(ERROR) << "Over-unlocked MultiProcessLock - " << name_; | 90 DLOG(ERROR) << "Over-unlocked MultiProcessLock - " << name_; |
| 90 return; | 91 return; |
| 91 } | 92 } |
| 92 if (HANDLE_EINTR(close(fd_)) < 0) { | 93 if (HANDLE_EINTR(close(fd_)) < 0) { |
| 93 DPLOG(ERROR) << "close"; | 94 DPLOG(ERROR) << "close"; |
| 94 } | 95 } |
| 95 fd_ = -1; | 96 fd_ = -1; |
| 96 } | 97 } |
| 97 | 98 |
| 98 private: | 99 private: |
| 99 std::string name_; | 100 std::string name_; |
| 100 int fd_; | 101 int fd_; |
| 101 DISALLOW_COPY_AND_ASSIGN(MultiProcessLockLinux); | 102 DISALLOW_COPY_AND_ASSIGN(MultiProcessLockLinux); |
| 102 }; | 103 }; |
| 103 | 104 |
| 104 MultiProcessLock* MultiProcessLock::Create(const std::string &name) { | 105 MultiProcessLock* MultiProcessLock::Create(const std::string &name) { |
| 105 return new MultiProcessLockLinux(name); | 106 return new MultiProcessLockLinux(name); |
| 106 } | 107 } |
| OLD | NEW |