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