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 if (snprintf(&address.sun_path[1], MULTI_PROCESS_LOCK_NAME_MAX_LEN + 1, |
47 "%s", name.c_str()) < 0) { | |
Nico
2010/11/16 23:23:31
from `man snprintf`:
These functions return
| |
48 PLOG(ERROR) << "Couldn't create sun_path - " << name_; | |
49 return false; | |
50 } | |
47 | 51 |
48 // Must set the first character of the path to something non-zero | 52 // Must set the first character of the path to something non-zero |
49 // before we call SUN_LEN which depends on strcpy working. | 53 // before we call SUN_LEN which depends on strcpy working. |
50 address.sun_path[0] = '@'; | 54 address.sun_path[0] = '@'; |
51 size_t length = SUN_LEN(&address); | 55 size_t length = SUN_LEN(&address); |
52 | 56 |
53 // Reset the first character of the path back to zero so that | 57 // Reset the first character of the path back to zero so that |
54 // bind returns an abstract name socket. | 58 // bind returns an abstract name socket. |
55 address.sun_path[0] = 0; | 59 address.sun_path[0] = 0; |
56 address.sun_family = AF_LOCAL; | 60 address.sun_family = AF_LOCAL; |
57 | 61 |
58 int socket_fd = socket(AF_LOCAL, SOCK_STREAM, 0); | 62 int socket_fd = socket(AF_LOCAL, SOCK_STREAM, 0); |
59 if (socket_fd < 0) { | 63 if (socket_fd < 0) { |
60 PLOG(ERROR) << "Couldn't create socket - " << name_; | 64 PLOG(ERROR) << "Couldn't create socket - " << name_; |
61 return false; | 65 return false; |
62 } | 66 } |
63 | 67 |
64 if (bind(socket_fd, | 68 if (bind(socket_fd, |
65 reinterpret_cast<sockaddr *>(&address), | 69 reinterpret_cast<sockaddr *>(&address), |
66 length) == 0) { | 70 length) == 0) { |
67 fd_ = socket_fd; | 71 fd_ = socket_fd; |
68 return true; | 72 return true; |
69 } else { | 73 } else { |
70 PLOG(ERROR) << "Couldn't bind socket - " | 74 PLOG(ERROR) << "Couldn't bind socket - " |
71 << &(address.sun_path[1]) | 75 << &(address.sun_path[1]) |
72 << " Length: " << length; | 76 << " Length: " << length; |
73 HANDLE_EINTR(close(socket_fd)); | 77 if (HANDLE_EINTR(close(socket_fd)) < 0) { |
78 PLOG(ERROR) << "close"; | |
79 } | |
74 return false; | 80 return false; |
75 } | 81 } |
76 } | 82 } |
77 | 83 |
78 virtual void Unlock() { | 84 virtual void Unlock() { |
79 if (fd_ == -1) { | 85 if (fd_ == -1) { |
80 DLOG(ERROR) << "Over-unlocked MultiProcessLock - " << name_; | 86 DLOG(ERROR) << "Over-unlocked MultiProcessLock - " << name_; |
81 return; | 87 return; |
82 } | 88 } |
83 HANDLE_EINTR(close(fd_)); | 89 if (HANDLE_EINTR(close(fd_)) < 0) { |
90 PLOG(ERROR) << "close"; | |
91 } | |
84 fd_ = -1; | 92 fd_ = -1; |
85 } | 93 } |
86 | 94 |
87 private: | 95 private: |
88 std::string name_; | 96 std::string name_; |
89 int fd_; | 97 int fd_; |
90 DISALLOW_COPY_AND_ASSIGN(MultiProcessLockLinux); | 98 DISALLOW_COPY_AND_ASSIGN(MultiProcessLockLinux); |
91 }; | 99 }; |
92 | 100 |
93 MultiProcessLock* MultiProcessLock::Create(const std::string &name) { | 101 MultiProcessLock* MultiProcessLock::Create(const std::string &name) { |
94 return new MultiProcessLockLinux(name); | 102 return new MultiProcessLockLinux(name); |
95 } | 103 } |
OLD | NEW |