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 "ipc/ipc_channel_posix.h" | 5 #include "ipc/ipc_channel_posix.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <stddef.h> | 9 #include <stddef.h> |
10 #include <sys/types.h> | 10 #include <sys/types.h> |
11 #include <sys/socket.h> | 11 #include <sys/socket.h> |
12 #include <sys/stat.h> | 12 #include <sys/stat.h> |
13 #include <sys/un.h> | 13 #include <sys/un.h> |
14 | 14 |
15 #include <string> | 15 #include <string> |
16 #include <map> | 16 #include <map> |
17 | 17 |
18 #include "base/command_line.h" | 18 #include "base/command_line.h" |
19 #include "base/eintr_wrapper.h" | 19 #include "base/eintr_wrapper.h" |
20 #include "base/file_path.h" | 20 #include "base/file_path.h" |
21 #include "base/file_util.h" | 21 #include "base/file_util.h" |
22 #include "base/global_descriptors_posix.h" | 22 #include "base/global_descriptors_posix.h" |
23 #include "base/lock.h" | |
24 #include "base/logging.h" | 23 #include "base/logging.h" |
25 #include "base/process_util.h" | 24 #include "base/process_util.h" |
26 #include "base/scoped_ptr.h" | 25 #include "base/scoped_ptr.h" |
27 #include "base/singleton.h" | 26 #include "base/singleton.h" |
28 #include "base/string_util.h" | 27 #include "base/string_util.h" |
| 28 #include "base/synchronization/lock.h" |
29 #include "ipc/ipc_descriptors.h" | 29 #include "ipc/ipc_descriptors.h" |
30 #include "ipc/ipc_switches.h" | 30 #include "ipc/ipc_switches.h" |
31 #include "ipc/file_descriptor_set_posix.h" | 31 #include "ipc/file_descriptor_set_posix.h" |
32 #include "ipc/ipc_logging.h" | 32 #include "ipc/ipc_logging.h" |
33 #include "ipc/ipc_message_utils.h" | 33 #include "ipc/ipc_message_utils.h" |
34 | 34 |
35 namespace IPC { | 35 namespace IPC { |
36 | 36 |
37 // IPC channels on Windows use named pipes (CreateNamedPipe()) with | 37 // IPC channels on Windows use named pipes (CreateNamedPipe()) with |
38 // channel ids as the pipe names. Channels on POSIX use sockets as | 38 // channel ids as the pipe names. Channels on POSIX use sockets as |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 return Singleton<PipeMap>::get(); | 84 return Singleton<PipeMap>::get(); |
85 } | 85 } |
86 | 86 |
87 ~PipeMap() { | 87 ~PipeMap() { |
88 // Shouldn't have left over pipes. | 88 // Shouldn't have left over pipes. |
89 DCHECK(map_.size() == 0); | 89 DCHECK(map_.size() == 0); |
90 } | 90 } |
91 | 91 |
92 // Lookup a given channel id. Return -1 if not found. | 92 // Lookup a given channel id. Return -1 if not found. |
93 int Lookup(const std::string& channel_id) { | 93 int Lookup(const std::string& channel_id) { |
94 AutoLock locked(lock_); | 94 base::AutoLock locked(lock_); |
95 | 95 |
96 ChannelToFDMap::const_iterator i = map_.find(channel_id); | 96 ChannelToFDMap::const_iterator i = map_.find(channel_id); |
97 if (i == map_.end()) | 97 if (i == map_.end()) |
98 return -1; | 98 return -1; |
99 return i->second; | 99 return i->second; |
100 } | 100 } |
101 | 101 |
102 // Remove the mapping for the given channel id. No error is signaled if the | 102 // Remove the mapping for the given channel id. No error is signaled if the |
103 // channel_id doesn't exist | 103 // channel_id doesn't exist |
104 void RemoveAndClose(const std::string& channel_id) { | 104 void RemoveAndClose(const std::string& channel_id) { |
105 AutoLock locked(lock_); | 105 base::AutoLock locked(lock_); |
106 | 106 |
107 ChannelToFDMap::iterator i = map_.find(channel_id); | 107 ChannelToFDMap::iterator i = map_.find(channel_id); |
108 if (i != map_.end()) { | 108 if (i != map_.end()) { |
109 if (HANDLE_EINTR(close(i->second)) < 0) | 109 if (HANDLE_EINTR(close(i->second)) < 0) |
110 PLOG(ERROR) << "close " << channel_id; | 110 PLOG(ERROR) << "close " << channel_id; |
111 map_.erase(i); | 111 map_.erase(i); |
112 } | 112 } |
113 } | 113 } |
114 | 114 |
115 // Insert a mapping from @channel_id to @fd. It's a fatal error to insert a | 115 // Insert a mapping from @channel_id to @fd. It's a fatal error to insert a |
116 // mapping if one already exists for the given channel_id | 116 // mapping if one already exists for the given channel_id |
117 void Insert(const std::string& channel_id, int fd) { | 117 void Insert(const std::string& channel_id, int fd) { |
118 AutoLock locked(lock_); | 118 base::AutoLock locked(lock_); |
119 DCHECK(fd != -1); | 119 DCHECK(fd != -1); |
120 | 120 |
121 ChannelToFDMap::const_iterator i = map_.find(channel_id); | 121 ChannelToFDMap::const_iterator i = map_.find(channel_id); |
122 CHECK(i == map_.end()) << "Creating second IPC server (fd " << fd << ") " | 122 CHECK(i == map_.end()) << "Creating second IPC server (fd " << fd << ") " |
123 << "for '" << channel_id << "' while first " | 123 << "for '" << channel_id << "' while first " |
124 << "(fd " << i->second << ") still exists"; | 124 << "(fd " << i->second << ") still exists"; |
125 map_[channel_id] = fd; | 125 map_[channel_id] = fd; |
126 } | 126 } |
127 | 127 |
128 private: | 128 private: |
129 Lock lock_; | 129 base::Lock lock_; |
130 typedef std::map<std::string, int> ChannelToFDMap; | 130 typedef std::map<std::string, int> ChannelToFDMap; |
131 ChannelToFDMap map_; | 131 ChannelToFDMap map_; |
132 | 132 |
133 friend struct DefaultSingletonTraits<PipeMap>; | 133 friend struct DefaultSingletonTraits<PipeMap>; |
134 }; | 134 }; |
135 | 135 |
136 //------------------------------------------------------------------------------ | 136 //------------------------------------------------------------------------------ |
137 // Verify that kMaxPipeNameLength is a decent size. | 137 // Verify that kMaxPipeNameLength is a decent size. |
138 COMPILE_ASSERT(sizeof(((sockaddr_un*)0)->sun_path) >= kMaxPipeNameLength, | 138 COMPILE_ASSERT(sizeof(((sockaddr_un*)0)->sun_path) >= kMaxPipeNameLength, |
139 BAD_SUN_PATH_LENGTH); | 139 BAD_SUN_PATH_LENGTH); |
(...skipping 1009 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1149 | 1149 |
1150 bool Channel::HasAcceptedConnection() const { | 1150 bool Channel::HasAcceptedConnection() const { |
1151 return channel_impl_->HasAcceptedConnection(); | 1151 return channel_impl_->HasAcceptedConnection(); |
1152 } | 1152 } |
1153 | 1153 |
1154 void Channel::ResetToAcceptingConnectionState() { | 1154 void Channel::ResetToAcceptingConnectionState() { |
1155 channel_impl_->ResetToAcceptingConnectionState(); | 1155 channel_impl_->ResetToAcceptingConnectionState(); |
1156 } | 1156 } |
1157 | 1157 |
1158 } // namespace IPC | 1158 } // namespace IPC |
OLD | NEW |