| 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> |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 } | 23 } |
| 24 } | 24 } |
| 25 | 25 |
| 26 virtual bool TryLock() { | 26 virtual bool TryLock() { |
| 27 if (fd_ != -1) { | 27 if (fd_ != -1) { |
| 28 DLOG(ERROR) << "MultiProcessLock is already locked - " << name_; | 28 DLOG(ERROR) << "MultiProcessLock is already locked - " << name_; |
| 29 return true; | 29 return true; |
| 30 } | 30 } |
| 31 | 31 |
| 32 if (name_.length() > MULTI_PROCESS_LOCK_NAME_MAX_LEN) { | 32 if (name_.length() > MULTI_PROCESS_LOCK_NAME_MAX_LEN) { |
| 33 DLOG(ERROR) << "Socket name too long (" << name_.length() | 33 LOG(ERROR) << "Socket name too long (" << name_.length() |
| 34 << " > " << MULTI_PROCESS_LOCK_NAME_MAX_LEN << ") - " | 34 << " > " << MULTI_PROCESS_LOCK_NAME_MAX_LEN << ") - " << name_; |
| 35 << name_; | |
| 36 return false; | 35 return false; |
| 37 } | 36 } |
| 38 | 37 |
| 39 struct sockaddr_un address; | 38 struct sockaddr_un address; |
| 40 | 39 |
| 41 // +1 for terminator, +1 for 0 in position 0 that makes it an | 40 // +1 for terminator, +1 for 0 in position 0 that makes it an |
| 42 // abstract named socket. | 41 // abstract named socket. |
| 43 // If this assert fails it is because sockaddr_un.sun_path size has been | 42 // If this assert fails it is because sockaddr_un.sun_path size has been |
| 44 // redefined and MULTI_PROCESS_LOCK_NAME_MAX_LEN can change accordingly. | 43 // redefined and MULTI_PROCESS_LOCK_NAME_MAX_LEN can change accordingly. |
| 45 COMPILE_ASSERT(sizeof(address.sun_path) | 44 COMPILE_ASSERT(sizeof(address.sun_path) |
| 46 == MULTI_PROCESS_LOCK_NAME_MAX_LEN + 2, sun_path_size_changed); | 45 == MULTI_PROCESS_LOCK_NAME_MAX_LEN + 2, sun_path_size_changed); |
| 47 | 46 |
| 48 memset(&address, 0, sizeof(address)); | 47 memset(&address, 0, sizeof(address)); |
| 49 int print_length = snprintf(&address.sun_path[1], | 48 int print_length = snprintf(&address.sun_path[1], |
| 50 MULTI_PROCESS_LOCK_NAME_MAX_LEN + 1, | 49 MULTI_PROCESS_LOCK_NAME_MAX_LEN + 1, |
| 51 "%s", name_.c_str()); | 50 "%s", name_.c_str()); |
| 52 | 51 |
| 53 if (print_length < 0 || | 52 if (print_length < 0 || |
| 54 print_length > static_cast<int>(MULTI_PROCESS_LOCK_NAME_MAX_LEN)) { | 53 print_length > static_cast<int>(MULTI_PROCESS_LOCK_NAME_MAX_LEN)) { |
| 55 DPLOG(ERROR) << "Couldn't create sun_path - " << name_; | 54 PLOG(ERROR) << "Couldn't create sun_path - " << name_; |
| 56 return false; | 55 return false; |
| 57 } | 56 } |
| 58 | 57 |
| 59 // Must set the first character of the path to something non-zero | 58 // Must set the first character of the path to something non-zero |
| 60 // before we call SUN_LEN which depends on strcpy working. | 59 // before we call SUN_LEN which depends on strcpy working. |
| 61 address.sun_path[0] = '@'; | 60 address.sun_path[0] = '@'; |
| 62 size_t length = SUN_LEN(&address); | 61 size_t length = SUN_LEN(&address); |
| 63 | 62 |
| 64 // Reset the first character of the path back to zero so that | 63 // Reset the first character of the path back to zero so that |
| 65 // bind returns an abstract name socket. | 64 // bind returns an abstract name socket. |
| 66 address.sun_path[0] = 0; | 65 address.sun_path[0] = 0; |
| 67 address.sun_family = AF_LOCAL; | 66 address.sun_family = AF_LOCAL; |
| 68 | 67 |
| 69 int socket_fd = socket(AF_LOCAL, SOCK_STREAM, 0); | 68 int socket_fd = socket(AF_LOCAL, SOCK_STREAM, 0); |
| 70 if (socket_fd < 0) { | 69 if (socket_fd < 0) { |
| 71 DPLOG(ERROR) << "Couldn't create socket - " << name_; | 70 PLOG(ERROR) << "Couldn't create socket - " << name_; |
| 72 return false; | 71 return false; |
| 73 } | 72 } |
| 74 | 73 |
| 75 if (bind(socket_fd, | 74 if (bind(socket_fd, |
| 76 reinterpret_cast<sockaddr *>(&address), | 75 reinterpret_cast<sockaddr *>(&address), |
| 77 length) == 0) { | 76 length) == 0) { |
| 78 fd_ = socket_fd; | 77 fd_ = socket_fd; |
| 79 return true; | 78 return true; |
| 80 } else { | 79 } else { |
| 81 DVLOG(1) << "Couldn't bind socket - " | 80 DVLOG(1) << "Couldn't bind socket - " |
| 82 << &(address.sun_path[1]) | 81 << &(address.sun_path[1]) |
| 83 << " Length: " << length; | 82 << " Length: " << length; |
| 84 if (HANDLE_EINTR(close(socket_fd)) < 0) { | 83 if (HANDLE_EINTR(close(socket_fd)) < 0) { |
| 85 DPLOG(ERROR) << "close"; | 84 PLOG(ERROR) << "close"; |
| 86 } | 85 } |
| 87 return false; | 86 return false; |
| 88 } | 87 } |
| 89 } | 88 } |
| 90 | 89 |
| 91 virtual void Unlock() { | 90 virtual void Unlock() { |
| 92 if (fd_ == -1) { | 91 if (fd_ == -1) { |
| 93 DLOG(ERROR) << "Over-unlocked MultiProcessLock - " << name_; | 92 DLOG(ERROR) << "Over-unlocked MultiProcessLock - " << name_; |
| 94 return; | 93 return; |
| 95 } | 94 } |
| 96 if (HANDLE_EINTR(close(fd_)) < 0) { | 95 if (HANDLE_EINTR(close(fd_)) < 0) { |
| 97 DPLOG(ERROR) << "close"; | 96 DPLOG(ERROR) << "close"; |
| 98 } | 97 } |
| 99 fd_ = -1; | 98 fd_ = -1; |
| 100 } | 99 } |
| 101 | 100 |
| 102 private: | 101 private: |
| 103 std::string name_; | 102 std::string name_; |
| 104 int fd_; | 103 int fd_; |
| 105 DISALLOW_COPY_AND_ASSIGN(MultiProcessLockLinux); | 104 DISALLOW_COPY_AND_ASSIGN(MultiProcessLockLinux); |
| 106 }; | 105 }; |
| 107 | 106 |
| 108 MultiProcessLock* MultiProcessLock::Create(const std::string &name) { | 107 MultiProcessLock* MultiProcessLock::Create(const std::string &name) { |
| 109 return new MultiProcessLockLinux(name); | 108 return new MultiProcessLockLinux(name); |
| 110 } | 109 } |
| OLD | NEW |