OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/common/multi_process_lock.h" |
| 6 |
| 7 #include <sys/socket.h> |
| 8 #include <sys/un.h> |
| 9 #include <unistd.h> |
| 10 |
| 11 #include "base/eintr_wrapper.h" |
| 12 #include "base/logging.h" |
| 13 |
| 14 class MultiProcessLockLinux : public MultiProcessLock { |
| 15 public: |
| 16 explicit MultiProcessLockLinux(const std::string& name) |
| 17 : name_(name), fd_(-1) { } |
| 18 |
| 19 virtual ~MultiProcessLockLinux() { |
| 20 if (fd_ != -1) { |
| 21 Unlock(); |
| 22 } |
| 23 } |
| 24 |
| 25 virtual bool TryLock() { |
| 26 if (fd_ != -1) { |
| 27 DLOG(ERROR) << "MultiProcessLock is already locked - " << name_; |
| 28 return true; |
| 29 } |
| 30 |
| 31 if (name_.length() > MULTI_PROCESS_LOCK_NAME_MAX_LEN) { |
| 32 LOG(ERROR) << "Socket name too long - " << name_; |
| 33 return false; |
| 34 } |
| 35 |
| 36 struct sockaddr_un address; |
| 37 |
| 38 // +1 for terminator, +1 for 0 in position 0 that makes it an |
| 39 // abstract named socket. |
| 40 // If this assert fails it is because sockaddr_un.sun_path size has been |
| 41 // redefined and MULTI_PROCESS_LOCK_NAME_MAX_LEN can change accordingly. |
| 42 COMPILE_ASSERT(sizeof(address.sun_path) |
| 43 == MULTI_PROCESS_LOCK_NAME_MAX_LEN + 2, sun_path_size_changed); |
| 44 |
| 45 memset(&address, 0, sizeof(address)); |
| 46 strcpy(&address.sun_path[1], name_.c_str()); |
| 47 |
| 48 // Must set the first character of the path to something non-zero |
| 49 // before we call SUN_LEN which depends on strcpy working. |
| 50 address.sun_path[0] = '@'; |
| 51 size_t length = SUN_LEN(&address); |
| 52 |
| 53 // Reset the first character of the path back to zero so that |
| 54 // bind returns an abstract name socket. |
| 55 address.sun_path[0] = 0; |
| 56 address.sun_family = AF_LOCAL; |
| 57 |
| 58 int socket_fd = socket(AF_LOCAL, SOCK_STREAM, 0); |
| 59 if (socket_fd < 0) { |
| 60 PLOG(ERROR) << "Couldn't create socket - " << name_; |
| 61 return false; |
| 62 } |
| 63 |
| 64 if (bind(socket_fd, |
| 65 reinterpret_cast<sockaddr *>(&address), |
| 66 length) == 0) { |
| 67 fd_ = socket_fd; |
| 68 return true; |
| 69 } else { |
| 70 PLOG(ERROR) << "Couldn't bind socket - " |
| 71 << &(address.sun_path[1]) |
| 72 << " Length: " << length; |
| 73 HANDLE_EINTR(close(socket_fd)); |
| 74 return false; |
| 75 } |
| 76 } |
| 77 |
| 78 virtual void Unlock() { |
| 79 if (fd_ == -1) { |
| 80 DLOG(ERROR) << "Over-unlocked MultiProcessLock - " << name_; |
| 81 return; |
| 82 } |
| 83 HANDLE_EINTR(close(fd_)); |
| 84 fd_ = -1; |
| 85 } |
| 86 |
| 87 private: |
| 88 std::string name_; |
| 89 int fd_; |
| 90 DISALLOW_COPY_AND_ASSIGN(MultiProcessLockLinux); |
| 91 }; |
| 92 |
| 93 MultiProcessLock* MultiProcessLock::Create(const std::string &name) { |
| 94 return new MultiProcessLockLinux(name); |
| 95 } |
OLD | NEW |