[email protected] | d4651ff | 2008-12-02 16:51:58 | [diff] [blame] | 1 | // Copyright (c) 2008 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 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 5 | #include "ipc/ipc_channel_posix.h" |
[email protected] | d4651ff | 2008-12-02 16:51:58 | [diff] [blame] | 6 | |
[email protected] | c311074 | 2008-12-11 00:36:47 | [diff] [blame] | 7 | #include <errno.h> |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 8 | #include <fcntl.h> |
[email protected] | e45e6c0 | 2008-12-15 22:02:17 | [diff] [blame] | 9 | #include <stddef.h> |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 10 | #include <sys/types.h> |
| 11 | #include <sys/socket.h> |
| 12 | #include <sys/stat.h> |
[email protected] | e45e6c0 | 2008-12-15 22:02:17 | [diff] [blame] | 13 | #include <sys/un.h> |
[email protected] | e45e6c0 | 2008-12-15 22:02:17 | [diff] [blame] | 14 | |
[email protected] | e8fce88 | 2009-01-20 22:02:58 | [diff] [blame] | 15 | #include <string> |
| 16 | #include <map> |
| 17 | |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 18 | #include "base/command_line.h" |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 19 | #include "base/eintr_wrapper.h" |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 20 | #include "base/global_descriptors_posix.h" |
[email protected] | e8fce88 | 2009-01-20 22:02:58 | [diff] [blame] | 21 | #include "base/lock.h" |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 22 | #include "base/logging.h" |
| 23 | #include "base/process_util.h" |
| 24 | #include "base/scoped_ptr.h" |
[email protected] | e8fce88 | 2009-01-20 22:02:58 | [diff] [blame] | 25 | #include "base/singleton.h" |
[email protected] | e6f02ab | 2009-04-10 22:29:29 | [diff] [blame] | 26 | #include "base/stats_counters.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 27 | #include "base/string_util.h" |
| 28 | #include "ipc/ipc_descriptors.h" |
| 29 | #include "ipc/ipc_switches.h" |
| 30 | #include "ipc/file_descriptor_set_posix.h" |
| 31 | #include "ipc/ipc_logging.h" |
| 32 | #include "ipc/ipc_message_utils.h" |
[email protected] | d4651ff | 2008-12-02 16:51:58 | [diff] [blame] | 33 | |
| 34 | namespace IPC { |
| 35 | |
[email protected] | 5f594c0 | 2009-05-01 22:37:59 | [diff] [blame] | 36 | // IPC channels on Windows use named pipes (CreateNamedPipe()) with |
| 37 | // channel ids as the pipe names. Channels on POSIX use anonymous |
| 38 | // Unix domain sockets created via socketpair() as pipes. These don't |
| 39 | // quite line up. |
| 40 | // |
| 41 | // When creating a child subprocess, the parent side of the fork |
| 42 | // arranges it such that the initial control channel ends up on the |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 43 | // magic file descriptor kPrimaryIPCChannel in the child. Future |
[email protected] | 5f594c0 | 2009-05-01 22:37:59 | [diff] [blame] | 44 | // connections (file descriptors) can then be passed via that |
| 45 | // connection via sendmsg(). |
| 46 | |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 47 | //------------------------------------------------------------------------------ |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 48 | namespace { |
| 49 | |
[email protected] | 5f594c0 | 2009-05-01 22:37:59 | [diff] [blame] | 50 | // The PipeMap class works around this quirk related to unit tests: |
[email protected] | e8fce88 | 2009-01-20 22:02:58 | [diff] [blame] | 51 | // |
[email protected] | 5f594c0 | 2009-05-01 22:37:59 | [diff] [blame] | 52 | // When running as a server, we install the client socket in a |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 53 | // specific file descriptor number (@kPrimaryIPCChannel). However, we |
[email protected] | 5f594c0 | 2009-05-01 22:37:59 | [diff] [blame] | 54 | // also have to support the case where we are running unittests in the |
| 55 | // same process. (We do not support forking without execing.) |
[email protected] | e8fce88 | 2009-01-20 22:02:58 | [diff] [blame] | 56 | // |
| 57 | // Case 1: normal running |
| 58 | // The IPC server object will install a mapping in PipeMap from the |
| 59 | // name which it was given to the client pipe. When forking the client, the |
| 60 | // GetClientFileDescriptorMapping will ensure that the socket is installed in |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 61 | // the magic slot (@kPrimaryIPCChannel). The client will search for the |
[email protected] | e8fce88 | 2009-01-20 22:02:58 | [diff] [blame] | 62 | // mapping, but it won't find any since we are in a new process. Thus the |
| 63 | // magic fd number is returned. Once the client connects, the server will |
[email protected] | 5f594c0 | 2009-05-01 22:37:59 | [diff] [blame] | 64 | // close its copy of the client socket and remove the mapping. |
[email protected] | e8fce88 | 2009-01-20 22:02:58 | [diff] [blame] | 65 | // |
| 66 | // Case 2: unittests - client and server in the same process |
| 67 | // The IPC server will install a mapping as before. The client will search |
| 68 | // for a mapping and find out. It duplicates the file descriptor and |
| 69 | // connects. Once the client connects, the server will close the original |
| 70 | // copy of the client socket and remove the mapping. Thus, when the client |
| 71 | // object closes, it will close the only remaining copy of the client socket |
| 72 | // in the fd table and the server will see EOF on its side. |
| 73 | // |
| 74 | // TODO(port): a client process cannot connect to multiple IPC channels with |
| 75 | // this scheme. |
| 76 | |
| 77 | class PipeMap { |
| 78 | public: |
| 79 | // Lookup a given channel id. Return -1 if not found. |
| 80 | int Lookup(const std::string& channel_id) { |
| 81 | AutoLock locked(lock_); |
| 82 | |
| 83 | ChannelToFDMap::const_iterator i = map_.find(channel_id); |
| 84 | if (i == map_.end()) |
| 85 | return -1; |
| 86 | return i->second; |
| 87 | } |
| 88 | |
| 89 | // Remove the mapping for the given channel id. No error is signaled if the |
| 90 | // channel_id doesn't exist |
[email protected] | f0ef2da | 2009-07-08 01:26:34 | [diff] [blame] | 91 | void RemoveAndClose(const std::string& channel_id) { |
[email protected] | e8fce88 | 2009-01-20 22:02:58 | [diff] [blame] | 92 | AutoLock locked(lock_); |
| 93 | |
| 94 | ChannelToFDMap::iterator i = map_.find(channel_id); |
[email protected] | f0ef2da | 2009-07-08 01:26:34 | [diff] [blame] | 95 | if (i != map_.end()) { |
| 96 | HANDLE_EINTR(close(i->second)); |
[email protected] | e8fce88 | 2009-01-20 22:02:58 | [diff] [blame] | 97 | map_.erase(i); |
[email protected] | f0ef2da | 2009-07-08 01:26:34 | [diff] [blame] | 98 | } |
[email protected] | e8fce88 | 2009-01-20 22:02:58 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // Insert a mapping from @channel_id to @fd. It's a fatal error to insert a |
| 102 | // mapping if one already exists for the given channel_id |
| 103 | void Insert(const std::string& channel_id, int fd) { |
| 104 | AutoLock locked(lock_); |
| 105 | DCHECK(fd != -1); |
| 106 | |
| 107 | ChannelToFDMap::const_iterator i = map_.find(channel_id); |
[email protected] | d2e884d | 2009-06-22 20:37:52 | [diff] [blame] | 108 | CHECK(i == map_.end()) << "Creating second IPC server (fd " << fd << ") " |
| 109 | << "for '" << channel_id << "' while first " |
| 110 | << "(fd " << i->second << ") still exists"; |
[email protected] | e8fce88 | 2009-01-20 22:02:58 | [diff] [blame] | 111 | map_[channel_id] = fd; |
| 112 | } |
| 113 | |
| 114 | private: |
| 115 | Lock lock_; |
| 116 | typedef std::map<std::string, int> ChannelToFDMap; |
| 117 | ChannelToFDMap map_; |
| 118 | }; |
| 119 | |
[email protected] | d2e884d | 2009-06-22 20:37:52 | [diff] [blame] | 120 | // Used to map a channel name to the equivalent FD # in the current process. |
| 121 | // Returns -1 if the channel is unknown. |
| 122 | int ChannelNameToFD(const std::string& channel_id) { |
[email protected] | e8fce88 | 2009-01-20 22:02:58 | [diff] [blame] | 123 | // See the large block comment above PipeMap for the reasoning here. |
| 124 | const int fd = Singleton<PipeMap>()->Lookup(channel_id); |
[email protected] | e8fce88 | 2009-01-20 22:02:58 | [diff] [blame] | 125 | |
[email protected] | d2e884d | 2009-06-22 20:37:52 | [diff] [blame] | 126 | if (fd != -1) { |
| 127 | int dup_fd = dup(fd); |
| 128 | if (dup_fd < 0) |
[email protected] | 57b76567 | 2009-10-13 18:27:40 | [diff] [blame] | 129 | PLOG(FATAL) << "dup(" << fd << ")"; |
[email protected] | d2e884d | 2009-06-22 20:37:52 | [diff] [blame] | 130 | return dup_fd; |
| 131 | } |
| 132 | |
| 133 | return fd; |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | //------------------------------------------------------------------------------ |
[email protected] | 02d6651 | 2009-03-17 01:48:35 | [diff] [blame] | 137 | sockaddr_un sizecheck; |
| 138 | const size_t kMaxPipeNameLength = sizeof(sizecheck.sun_path); |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 139 | |
| 140 | // Creates a Fifo with the specified name ready to listen on. |
[email protected] | 5f594c0 | 2009-05-01 22:37:59 | [diff] [blame] | 141 | bool CreateServerFifo(const std::string& pipe_name, int* server_listen_fd) { |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 142 | DCHECK(server_listen_fd); |
[email protected] | 02d6651 | 2009-03-17 01:48:35 | [diff] [blame] | 143 | DCHECK_GT(pipe_name.length(), 0u); |
| 144 | DCHECK_LT(pipe_name.length(), kMaxPipeNameLength); |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 145 | |
[email protected] | 02d6651 | 2009-03-17 01:48:35 | [diff] [blame] | 146 | if (pipe_name.length() == 0 || pipe_name.length() >= kMaxPipeNameLength) { |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 147 | return false; |
| 148 | } |
| 149 | |
| 150 | // Create socket. |
| 151 | int fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 152 | if (fd < 0) { |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | // Make socket non-blocking |
| 157 | if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 158 | HANDLE_EINTR(close(fd)); |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 159 | return false; |
| 160 | } |
| 161 | |
| 162 | // Delete any old FS instances. |
| 163 | unlink(pipe_name.c_str()); |
| 164 | |
| 165 | // Create unix_addr structure |
| 166 | struct sockaddr_un unix_addr; |
| 167 | memset(&unix_addr, 0, sizeof(unix_addr)); |
| 168 | unix_addr.sun_family = AF_UNIX; |
[email protected] | 02d6651 | 2009-03-17 01:48:35 | [diff] [blame] | 169 | snprintf(unix_addr.sun_path, kMaxPipeNameLength, "%s", pipe_name.c_str()); |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 170 | size_t unix_addr_len = offsetof(struct sockaddr_un, sun_path) + |
| 171 | strlen(unix_addr.sun_path) + 1; |
| 172 | |
| 173 | // Bind the socket. |
| 174 | if (bind(fd, reinterpret_cast<const sockaddr*>(&unix_addr), |
| 175 | unix_addr_len) != 0) { |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 176 | HANDLE_EINTR(close(fd)); |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 177 | return false; |
| 178 | } |
| 179 | |
| 180 | // Start listening on the socket. |
| 181 | const int listen_queue_length = 1; |
| 182 | if (listen(fd, listen_queue_length) != 0) { |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 183 | HANDLE_EINTR(close(fd)); |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 184 | return false; |
| 185 | } |
| 186 | |
| 187 | *server_listen_fd = fd; |
| 188 | return true; |
| 189 | } |
| 190 | |
| 191 | // Accept a connection on a fifo. |
| 192 | bool ServerAcceptFifoConnection(int server_listen_fd, int* server_socket) { |
| 193 | DCHECK(server_socket); |
| 194 | |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 195 | int accept_fd = HANDLE_EINTR(accept(server_listen_fd, NULL, 0)); |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 196 | if (accept_fd < 0) |
| 197 | return false; |
[email protected] | 3af2126 | 2008-12-16 21:49:34 | [diff] [blame] | 198 | if (fcntl(accept_fd, F_SETFL, O_NONBLOCK) == -1) { |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 199 | HANDLE_EINTR(close(accept_fd)); |
[email protected] | 3af2126 | 2008-12-16 21:49:34 | [diff] [blame] | 200 | return false; |
| 201 | } |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 202 | |
| 203 | *server_socket = accept_fd; |
| 204 | return true; |
| 205 | } |
| 206 | |
| 207 | bool ClientConnectToFifo(const std::string &pipe_name, int* client_socket) { |
| 208 | DCHECK(client_socket); |
[email protected] | 02d6651 | 2009-03-17 01:48:35 | [diff] [blame] | 209 | DCHECK_LT(pipe_name.length(), kMaxPipeNameLength); |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 210 | |
| 211 | // Create socket. |
| 212 | int fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 213 | if (fd < 0) { |
| 214 | LOG(ERROR) << "fd is invalid"; |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | // Make socket non-blocking |
| 219 | if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { |
[email protected] | 02d6651 | 2009-03-17 01:48:35 | [diff] [blame] | 220 | LOG(ERROR) << "fcntl failed"; |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 221 | HANDLE_EINTR(close(fd)); |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 222 | return false; |
| 223 | } |
| 224 | |
| 225 | // Create server side of socket. |
| 226 | struct sockaddr_un server_unix_addr; |
| 227 | memset(&server_unix_addr, 0, sizeof(server_unix_addr)); |
| 228 | server_unix_addr.sun_family = AF_UNIX; |
[email protected] | 02d6651 | 2009-03-17 01:48:35 | [diff] [blame] | 229 | snprintf(server_unix_addr.sun_path, kMaxPipeNameLength, "%s", |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 230 | pipe_name.c_str()); |
| 231 | size_t server_unix_addr_len = offsetof(struct sockaddr_un, sun_path) + |
| 232 | strlen(server_unix_addr.sun_path) + 1; |
| 233 | |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 234 | if (HANDLE_EINTR(connect(fd, reinterpret_cast<sockaddr*>(&server_unix_addr), |
| 235 | server_unix_addr_len)) != 0) { |
| 236 | HANDLE_EINTR(close(fd)); |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 237 | return false; |
| 238 | } |
| 239 | |
| 240 | *client_socket = fd; |
| 241 | return true; |
| 242 | } |
| 243 | |
[email protected] | 86c3d9e | 2009-12-08 14:48:08 | [diff] [blame^] | 244 | bool SocketWriteErrorIsRecoverable() { |
| 245 | #if defined(OS_MACOSX) |
| 246 | // On OS X if sendmsg() is trying to send fds between processes and there |
| 247 | // isn't enough room in the output buffer to send the fd structure over |
| 248 | // atomically then EMSGSIZE is returned. |
| 249 | // |
| 250 | // EMSGSIZE presents a problem since the system APIs can only call us when |
| 251 | // there's room in the socket buffer and not when there is "enough" room. |
| 252 | // |
| 253 | // The current behavior is to return to the event loop when EMSGSIZE is |
| 254 | // received and hopefull service another FD. This is however still |
| 255 | // technically a busy wait since the event loop will call us right back until |
| 256 | // the receiver has read enough data to allow passing the FD over atomically. |
| 257 | return errno == EAGAIN || errno == EMSGSIZE; |
| 258 | #else |
| 259 | return errno == EAGAIN; |
| 260 | #endif |
| 261 | } |
| 262 | |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 263 | } // namespace |
[email protected] | d4651ff | 2008-12-02 16:51:58 | [diff] [blame] | 264 | //------------------------------------------------------------------------------ |
| 265 | |
[email protected] | 9a3a293b | 2009-06-04 22:28:16 | [diff] [blame] | 266 | Channel::ChannelImpl::ChannelImpl(const std::string& channel_id, Mode mode, |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 267 | Listener* listener) |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 268 | : mode_(mode), |
[email protected] | e45e6c0 | 2008-12-15 22:02:17 | [diff] [blame] | 269 | is_blocked_on_write_(false), |
| 270 | message_send_bytes_written_(0), |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 271 | uses_fifo_(CommandLine::ForCurrentProcess()->HasSwitch( |
[email protected] | 97c42bcf | 2009-02-27 07:21:58 | [diff] [blame] | 272 | switches::kIPCUseFIFO)), |
[email protected] | e45e6c0 | 2008-12-15 22:02:17 | [diff] [blame] | 273 | server_listen_pipe_(-1), |
| 274 | pipe_(-1), |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 275 | client_pipe_(-1), |
[email protected] | baf556a | 2009-09-04 21:34:05 | [diff] [blame] | 276 | #if defined(OS_LINUX) |
| 277 | fd_pipe_(-1), |
| 278 | remote_fd_pipe_(-1), |
| 279 | #endif |
[email protected] | e45e6c0 | 2008-12-15 22:02:17 | [diff] [blame] | 280 | listener_(listener), |
| 281 | waiting_connect_(true), |
[email protected] | e45e6c0 | 2008-12-15 22:02:17 | [diff] [blame] | 282 | factory_(this) { |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 283 | if (!CreatePipe(channel_id, mode)) { |
| 284 | // The pipe may have been closed already. |
[email protected] | 57b76567 | 2009-10-13 18:27:40 | [diff] [blame] | 285 | PLOG(WARNING) << "Unable to create pipe named \"" << channel_id |
| 286 | << "\" in " << (mode == MODE_SERVER ? "server" : "client") |
| 287 | << " mode"; |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 288 | } |
[email protected] | d4651ff | 2008-12-02 16:51:58 | [diff] [blame] | 289 | } |
| 290 | |
[email protected] | d2e884d | 2009-06-22 20:37:52 | [diff] [blame] | 291 | // static |
| 292 | void AddChannelSocket(const std::string& name, int socket) { |
| 293 | Singleton<PipeMap>()->Insert(name, socket); |
| 294 | } |
| 295 | |
| 296 | // static |
[email protected] | f0ef2da | 2009-07-08 01:26:34 | [diff] [blame] | 297 | void RemoveAndCloseChannelSocket(const std::string& name) { |
| 298 | Singleton<PipeMap>()->RemoveAndClose(name); |
| 299 | } |
| 300 | |
| 301 | // static |
[email protected] | d2e884d | 2009-06-22 20:37:52 | [diff] [blame] | 302 | bool SocketPair(int* fd1, int* fd2) { |
| 303 | int pipe_fds[2]; |
| 304 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipe_fds) != 0) { |
[email protected] | 57b76567 | 2009-10-13 18:27:40 | [diff] [blame] | 305 | PLOG(ERROR) << "socketpair()"; |
[email protected] | d2e884d | 2009-06-22 20:37:52 | [diff] [blame] | 306 | return false; |
| 307 | } |
| 308 | |
| 309 | // Set both ends to be non-blocking. |
| 310 | if (fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK) == -1 || |
| 311 | fcntl(pipe_fds[1], F_SETFL, O_NONBLOCK) == -1) { |
[email protected] | 57b76567 | 2009-10-13 18:27:40 | [diff] [blame] | 312 | PLOG(ERROR) << "fcntl(O_NONBLOCK)"; |
[email protected] | d2e884d | 2009-06-22 20:37:52 | [diff] [blame] | 313 | HANDLE_EINTR(close(pipe_fds[0])); |
| 314 | HANDLE_EINTR(close(pipe_fds[1])); |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | *fd1 = pipe_fds[0]; |
| 319 | *fd2 = pipe_fds[1]; |
| 320 | |
| 321 | return true; |
| 322 | } |
| 323 | |
[email protected] | 9a3a293b | 2009-06-04 22:28:16 | [diff] [blame] | 324 | bool Channel::ChannelImpl::CreatePipe(const std::string& channel_id, |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 325 | Mode mode) { |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 326 | DCHECK(server_listen_pipe_ == -1 && pipe_ == -1); |
| 327 | |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 328 | if (uses_fifo_) { |
[email protected] | 5f594c0 | 2009-05-01 22:37:59 | [diff] [blame] | 329 | // This only happens in unit tests; see the comment above PipeMap. |
| 330 | // TODO(playmobil): We shouldn't need to create fifos on disk. |
| 331 | // TODO(playmobil): If we do, they should be in the user data directory. |
| 332 | // TODO(playmobil): Cleanup any stale fifos. |
[email protected] | 9a3a293b | 2009-06-04 22:28:16 | [diff] [blame] | 333 | pipe_name_ = "/var/tmp/chrome_" + channel_id; |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 334 | if (mode == MODE_SERVER) { |
| 335 | if (!CreateServerFifo(pipe_name_, &server_listen_pipe_)) { |
| 336 | return false; |
| 337 | } |
| 338 | } else { |
| 339 | if (!ClientConnectToFifo(pipe_name_, &pipe_)) { |
| 340 | return false; |
| 341 | } |
| 342 | waiting_connect_ = false; |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 343 | } |
| 344 | } else { |
[email protected] | d2e884d | 2009-06-22 20:37:52 | [diff] [blame] | 345 | // This is the normal (non-unit-test) case, where we're using sockets. |
| 346 | // Three possible cases: |
| 347 | // 1) It's for a channel we already have a pipe for; reuse it. |
| 348 | // 2) It's the initial IPC channel: |
| 349 | // 2a) Server side: create the pipe. |
| 350 | // 2b) Client side: Pull the pipe out of the GlobalDescriptors set. |
[email protected] | 9a3a293b | 2009-06-04 22:28:16 | [diff] [blame] | 351 | pipe_name_ = channel_id; |
[email protected] | d2e884d | 2009-06-22 20:37:52 | [diff] [blame] | 352 | pipe_ = ChannelNameToFD(pipe_name_); |
| 353 | if (pipe_ < 0) { |
| 354 | // Initial IPC channel. |
| 355 | if (mode == MODE_SERVER) { |
| 356 | if (!SocketPair(&pipe_, &client_pipe_)) |
| 357 | return false; |
| 358 | AddChannelSocket(pipe_name_, client_pipe_); |
| 359 | } else { |
[email protected] | 554a885 | 2009-11-30 22:14:37 | [diff] [blame] | 360 | // Guard against inappropriate reuse of the initial IPC channel. If |
| 361 | // an IPC channel closes and someone attempts to reuse it by name, the |
| 362 | // initial channel must not be recycled here. http://crbug.com/26754. |
| 363 | static bool used_initial_channel = false; |
| 364 | if (used_initial_channel) { |
| 365 | LOG(FATAL) << "Denying attempt to reuse initial IPC channel"; |
| 366 | return false; |
| 367 | } |
| 368 | used_initial_channel = true; |
| 369 | |
[email protected] | d2e884d | 2009-06-22 20:37:52 | [diff] [blame] | 370 | pipe_ = Singleton<base::GlobalDescriptors>()->Get(kPrimaryIPCChannel); |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 371 | } |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 372 | } else { |
[email protected] | baf556a | 2009-09-04 21:34:05 | [diff] [blame] | 373 | waiting_connect_ = mode == MODE_SERVER; |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 374 | } |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | // Create the Hello message to be sent when Connect is called |
| 378 | scoped_ptr<Message> msg(new Message(MSG_ROUTING_NONE, |
| 379 | HELLO_MESSAGE_TYPE, |
| 380 | IPC::Message::PRIORITY_NORMAL)); |
[email protected] | baf556a | 2009-09-04 21:34:05 | [diff] [blame] | 381 | #if defined(OS_LINUX) |
| 382 | if (!uses_fifo_) { |
| 383 | // On Linux, the seccomp sandbox makes it very expensive to call |
| 384 | // recvmsg() and sendmsg(). Often, we are perfectly OK with resorting to |
| 385 | // read() and write(), which are cheap. |
| 386 | // |
| 387 | // As we cannot anticipate, when the sender will provide us with file |
| 388 | // handles, we have to make the decision about whether we call read() or |
| 389 | // recvmsg() before we actually make the call. The easiest option is to |
| 390 | // create a dedicated socketpair() for exchanging file handles. |
| 391 | if (mode == MODE_SERVER) { |
| 392 | fd_pipe_ = -1; |
| 393 | } else if (remote_fd_pipe_ == -1) { |
| 394 | if (!SocketPair(&fd_pipe_, &remote_fd_pipe_)) { |
| 395 | return false; |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | #endif |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 400 | if (!msg->WriteInt(base::GetCurrentProcId())) { |
| 401 | Close(); |
| 402 | return false; |
| 403 | } |
| 404 | |
| 405 | output_queue_.push(msg.release()); |
| 406 | return true; |
[email protected] | d4651ff | 2008-12-02 16:51:58 | [diff] [blame] | 407 | } |
| 408 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 409 | bool Channel::ChannelImpl::Connect() { |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 410 | if (mode_ == MODE_SERVER && uses_fifo_) { |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 411 | if (server_listen_pipe_ == -1) { |
| 412 | return false; |
| 413 | } |
[email protected] | e45e6c0 | 2008-12-15 22:02:17 | [diff] [blame] | 414 | MessageLoopForIO::current()->WatchFileDescriptor( |
| 415 | server_listen_pipe_, |
| 416 | true, |
| 417 | MessageLoopForIO::WATCH_READ, |
| 418 | &server_listen_connection_watcher_, |
| 419 | this); |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 420 | } else { |
| 421 | if (pipe_ == -1) { |
| 422 | return false; |
| 423 | } |
[email protected] | e45e6c0 | 2008-12-15 22:02:17 | [diff] [blame] | 424 | MessageLoopForIO::current()->WatchFileDescriptor( |
| 425 | pipe_, |
| 426 | true, |
| 427 | MessageLoopForIO::WATCH_READ, |
| 428 | &read_watcher_, |
| 429 | this); |
[email protected] | baf556a | 2009-09-04 21:34:05 | [diff] [blame] | 430 | waiting_connect_ = mode_ == MODE_SERVER; |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | if (!waiting_connect_) |
| 434 | return ProcessOutgoingMessages(); |
| 435 | return true; |
[email protected] | d4651ff | 2008-12-02 16:51:58 | [diff] [blame] | 436 | } |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 437 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 438 | bool Channel::ChannelImpl::ProcessIncomingMessages() { |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 439 | ssize_t bytes_read = 0; |
| 440 | |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 441 | struct msghdr msg = {0}; |
| 442 | struct iovec iov = {input_buf_, Channel::kReadBufferSize}; |
| 443 | |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 444 | msg.msg_iovlen = 1; |
| 445 | msg.msg_control = input_cmsg_buf_; |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 446 | |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 447 | for (;;) { |
[email protected] | baf556a | 2009-09-04 21:34:05 | [diff] [blame] | 448 | msg.msg_iov = &iov; |
[email protected] | cef7c52 | 2009-02-10 08:15:02 | [diff] [blame] | 449 | |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 450 | if (bytes_read == 0) { |
| 451 | if (pipe_ == -1) |
| 452 | return false; |
| 453 | |
| 454 | // Read from pipe. |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 455 | // recvmsg() returns 0 if the connection has closed or EAGAIN if no data |
| 456 | // is waiting on the pipe. |
[email protected] | baf556a | 2009-09-04 21:34:05 | [diff] [blame] | 457 | #if defined(OS_LINUX) |
| 458 | if (fd_pipe_ >= 0) { |
| 459 | bytes_read = HANDLE_EINTR(read(pipe_, input_buf_, |
| 460 | Channel::kReadBufferSize)); |
| 461 | msg.msg_controllen = 0; |
| 462 | } else |
| 463 | #endif |
| 464 | { |
| 465 | msg.msg_controllen = sizeof(input_cmsg_buf_); |
| 466 | bytes_read = HANDLE_EINTR(recvmsg(pipe_, &msg, MSG_DONTWAIT)); |
| 467 | } |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 468 | if (bytes_read < 0) { |
| 469 | if (errno == EAGAIN) { |
| 470 | return true; |
[email protected] | 7f6f0623 | 2009-05-22 21:15:39 | [diff] [blame] | 471 | #if defined(OS_MACOSX) |
| 472 | } else if (errno == EPERM) { |
| 473 | // On OSX, reading from a pipe with no listener returns EPERM |
| 474 | // treat this as a special case to prevent spurious error messages |
| 475 | // to the console. |
| 476 | return false; |
| 477 | #endif // defined(OS_MACOSX) |
[email protected] | 7bf54f5e | 2009-10-23 01:48:21 | [diff] [blame] | 478 | } else if (errno == ECONNRESET || errno == EPIPE) { |
[email protected] | d8365fc | 2009-10-16 19:44:31 | [diff] [blame] | 479 | return false; |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 480 | } else { |
[email protected] | 57b76567 | 2009-10-13 18:27:40 | [diff] [blame] | 481 | PLOG(ERROR) << "pipe error (" << pipe_ << ")"; |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 482 | return false; |
| 483 | } |
| 484 | } else if (bytes_read == 0) { |
| 485 | // The pipe has closed... |
[email protected] | e8fce88 | 2009-01-20 22:02:58 | [diff] [blame] | 486 | return false; |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | DCHECK(bytes_read); |
| 490 | |
[email protected] | e8fce88 | 2009-01-20 22:02:58 | [diff] [blame] | 491 | if (client_pipe_ != -1) { |
[email protected] | f0ef2da | 2009-07-08 01:26:34 | [diff] [blame] | 492 | Singleton<PipeMap>()->RemoveAndClose(pipe_name_); |
[email protected] | e8fce88 | 2009-01-20 22:02:58 | [diff] [blame] | 493 | client_pipe_ = -1; |
| 494 | } |
| 495 | |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 496 | // a pointer to an array of |num_wire_fds| file descriptors from the read |
[email protected] | 337c6bf | 2009-02-07 00:51:58 | [diff] [blame] | 497 | const int* wire_fds = NULL; |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 498 | unsigned num_wire_fds = 0; |
| 499 | |
| 500 | // walk the list of control messages and, if we find an array of file |
| 501 | // descriptors, save a pointer to the array |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 502 | |
[email protected] | afb4bad | 2009-02-12 02:39:31 | [diff] [blame] | 503 | // This next if statement is to work around an OSX issue where |
| 504 | // CMSG_FIRSTHDR will return non-NULL in the case that controllen == 0. |
| 505 | // Here's a test case: |
| 506 | // |
| 507 | // int main() { |
| 508 | // struct msghdr msg; |
| 509 | // msg.msg_control = &msg; |
| 510 | // msg.msg_controllen = 0; |
| 511 | // if (CMSG_FIRSTHDR(&msg)) |
| 512 | // printf("Bug found!\n"); |
| 513 | // } |
| 514 | if (msg.msg_controllen > 0) { |
| 515 | // On OSX, CMSG_FIRSTHDR doesn't handle the case where controllen is 0 |
| 516 | // and will return a pointer into nowhere. |
| 517 | for (struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); cmsg; |
| 518 | cmsg = CMSG_NXTHDR(&msg, cmsg)) { |
| 519 | if (cmsg->cmsg_level == SOL_SOCKET && |
| 520 | cmsg->cmsg_type == SCM_RIGHTS) { |
| 521 | const unsigned payload_len = cmsg->cmsg_len - CMSG_LEN(0); |
| 522 | DCHECK(payload_len % sizeof(int) == 0); |
| 523 | wire_fds = reinterpret_cast<int*>(CMSG_DATA(cmsg)); |
| 524 | num_wire_fds = payload_len / 4; |
| 525 | |
| 526 | if (msg.msg_flags & MSG_CTRUNC) { |
| 527 | LOG(ERROR) << "SCM_RIGHTS message was truncated" |
| 528 | << " cmsg_len:" << cmsg->cmsg_len |
| 529 | << " fd:" << pipe_; |
| 530 | for (unsigned i = 0; i < num_wire_fds; ++i) |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 531 | HANDLE_EINTR(close(wire_fds[i])); |
[email protected] | afb4bad | 2009-02-12 02:39:31 | [diff] [blame] | 532 | return false; |
| 533 | } |
| 534 | break; |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 535 | } |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 539 | // Process messages from input buffer. |
| 540 | const char *p; |
| 541 | const char *end; |
| 542 | if (input_overflow_buf_.empty()) { |
| 543 | p = input_buf_; |
| 544 | end = p + bytes_read; |
| 545 | } else { |
| 546 | if (input_overflow_buf_.size() > |
| 547 | static_cast<size_t>(kMaximumMessageSize - bytes_read)) { |
| 548 | input_overflow_buf_.clear(); |
| 549 | LOG(ERROR) << "IPC message is too big"; |
| 550 | return false; |
| 551 | } |
| 552 | input_overflow_buf_.append(input_buf_, bytes_read); |
| 553 | p = input_overflow_buf_.data(); |
| 554 | end = p + input_overflow_buf_.size(); |
| 555 | } |
| 556 | |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 557 | // A pointer to an array of |num_fds| file descriptors which includes any |
| 558 | // fds that have spilled over from a previous read. |
[email protected] | baf556a | 2009-09-04 21:34:05 | [diff] [blame] | 559 | const int* fds = NULL; |
| 560 | unsigned num_fds = 0; |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 561 | unsigned fds_i = 0; // the index of the first unused descriptor |
| 562 | |
| 563 | if (input_overflow_fds_.empty()) { |
| 564 | fds = wire_fds; |
| 565 | num_fds = num_wire_fds; |
| 566 | } else { |
[email protected] | baf556a | 2009-09-04 21:34:05 | [diff] [blame] | 567 | if (num_wire_fds > 0) { |
| 568 | const size_t prev_size = input_overflow_fds_.size(); |
| 569 | input_overflow_fds_.resize(prev_size + num_wire_fds); |
| 570 | memcpy(&input_overflow_fds_[prev_size], wire_fds, |
| 571 | num_wire_fds * sizeof(int)); |
| 572 | } |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 573 | fds = &input_overflow_fds_[0]; |
| 574 | num_fds = input_overflow_fds_.size(); |
| 575 | } |
| 576 | |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 577 | while (p < end) { |
| 578 | const char* message_tail = Message::FindNext(p, end); |
| 579 | if (message_tail) { |
| 580 | int len = static_cast<int>(message_tail - p); |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame] | 581 | Message m(p, len); |
[email protected] | baf556a | 2009-09-04 21:34:05 | [diff] [blame] | 582 | |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 583 | if (m.header()->num_fds) { |
| 584 | // the message has file descriptors |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame] | 585 | const char* error = NULL; |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 586 | if (m.header()->num_fds > num_fds - fds_i) { |
| 587 | // the message has been completely received, but we didn't get |
| 588 | // enough file descriptors. |
[email protected] | baf556a | 2009-09-04 21:34:05 | [diff] [blame] | 589 | #if defined(OS_LINUX) |
| 590 | if (!uses_fifo_) { |
| 591 | char dummy; |
| 592 | struct iovec fd_pipe_iov = { &dummy, 1 }; |
| 593 | msg.msg_iov = &fd_pipe_iov; |
| 594 | msg.msg_controllen = sizeof(input_cmsg_buf_); |
| 595 | ssize_t n = HANDLE_EINTR(recvmsg(fd_pipe_, &msg, MSG_DONTWAIT)); |
| 596 | if (n == 1 && msg.msg_controllen > 0) { |
| 597 | for (struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); cmsg; |
| 598 | cmsg = CMSG_NXTHDR(&msg, cmsg)) { |
| 599 | if (cmsg->cmsg_level == SOL_SOCKET && |
| 600 | cmsg->cmsg_type == SCM_RIGHTS) { |
| 601 | const unsigned payload_len = cmsg->cmsg_len - CMSG_LEN(0); |
| 602 | DCHECK(payload_len % sizeof(int) == 0); |
| 603 | wire_fds = reinterpret_cast<int*>(CMSG_DATA(cmsg)); |
| 604 | num_wire_fds = payload_len / 4; |
| 605 | |
| 606 | if (msg.msg_flags & MSG_CTRUNC) { |
| 607 | LOG(ERROR) << "SCM_RIGHTS message was truncated" |
| 608 | << " cmsg_len:" << cmsg->cmsg_len |
| 609 | << " fd:" << pipe_; |
| 610 | for (unsigned i = 0; i < num_wire_fds; ++i) |
| 611 | HANDLE_EINTR(close(wire_fds[i])); |
| 612 | return false; |
| 613 | } |
| 614 | break; |
| 615 | } |
| 616 | } |
| 617 | if (input_overflow_fds_.empty()) { |
| 618 | fds = wire_fds; |
| 619 | num_fds = num_wire_fds; |
| 620 | } else { |
| 621 | if (num_wire_fds > 0) { |
| 622 | const size_t prev_size = input_overflow_fds_.size(); |
| 623 | input_overflow_fds_.resize(prev_size + num_wire_fds); |
| 624 | memcpy(&input_overflow_fds_[prev_size], wire_fds, |
| 625 | num_wire_fds * sizeof(int)); |
| 626 | } |
| 627 | fds = &input_overflow_fds_[0]; |
| 628 | num_fds = input_overflow_fds_.size(); |
| 629 | } |
| 630 | } |
| 631 | } |
| 632 | if (m.header()->num_fds > num_fds - fds_i) |
| 633 | #endif |
| 634 | error = "Message needs unreceived descriptors"; |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | if (m.header()->num_fds > |
[email protected] | d0c0b1d | 2009-02-12 18:32:45 | [diff] [blame] | 638 | FileDescriptorSet::MAX_DESCRIPTORS_PER_MESSAGE) { |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame] | 639 | // There are too many descriptors in this message |
| 640 | error = "Message requires an excessive number of descriptors"; |
| 641 | } |
| 642 | |
| 643 | if (error) { |
| 644 | LOG(WARNING) << error |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 645 | << " channel:" << this |
| 646 | << " message-type:" << m.type() |
| 647 | << " header()->num_fds:" << m.header()->num_fds |
| 648 | << " num_fds:" << num_fds |
| 649 | << " fds_i:" << fds_i; |
| 650 | // close the existing file descriptors so that we don't leak them |
| 651 | for (unsigned i = fds_i; i < num_fds; ++i) |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 652 | HANDLE_EINTR(close(fds[i])); |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 653 | input_overflow_fds_.clear(); |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame] | 654 | // abort the connection |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 655 | return false; |
| 656 | } |
| 657 | |
[email protected] | d0c0b1d | 2009-02-12 18:32:45 | [diff] [blame] | 658 | m.file_descriptor_set()->SetDescriptors( |
| 659 | &fds[fds_i], m.header()->num_fds); |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 660 | fds_i += m.header()->num_fds; |
| 661 | } |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 662 | #ifdef IPC_MESSAGE_DEBUG_EXTRA |
| 663 | DLOG(INFO) << "received message on channel @" << this << |
| 664 | " with type " << m.type(); |
| 665 | #endif |
| 666 | if (m.routing_id() == MSG_ROUTING_NONE && |
| 667 | m.type() == HELLO_MESSAGE_TYPE) { |
| 668 | // The Hello message contains only the process id. |
[email protected] | baf556a | 2009-09-04 21:34:05 | [diff] [blame] | 669 | void *iter = NULL; |
| 670 | int pid; |
| 671 | if (!m.ReadInt(&iter, &pid)) { |
| 672 | NOTREACHED(); |
| 673 | } |
| 674 | #if defined(OS_LINUX) |
| 675 | if (mode_ == MODE_SERVER && !uses_fifo_) { |
| 676 | // On Linux, the Hello message from the client to the server |
| 677 | // also contains the fd_pipe_, which will be used for all |
| 678 | // subsequent file descriptor passing. |
| 679 | DCHECK_EQ(m.file_descriptor_set()->size(), 1); |
| 680 | base::FileDescriptor descriptor; |
| 681 | if (!m.ReadFileDescriptor(&iter, &descriptor)) { |
| 682 | NOTREACHED(); |
| 683 | } |
| 684 | fd_pipe_ = descriptor.fd; |
| 685 | CHECK(descriptor.auto_close); |
| 686 | } |
| 687 | #endif |
| 688 | listener_->OnChannelConnected(pid); |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 689 | } else { |
| 690 | listener_->OnMessageReceived(m); |
| 691 | } |
| 692 | p = message_tail; |
| 693 | } else { |
| 694 | // Last message is partial. |
| 695 | break; |
| 696 | } |
[email protected] | baf556a | 2009-09-04 21:34:05 | [diff] [blame] | 697 | input_overflow_fds_ = std::vector<int>(&fds[fds_i], &fds[num_fds]); |
| 698 | fds_i = 0; |
| 699 | fds = &input_overflow_fds_[0]; |
| 700 | num_fds = input_overflow_fds_.size(); |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 701 | } |
| 702 | input_overflow_buf_.assign(p, end - p); |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 703 | input_overflow_fds_ = std::vector<int>(&fds[fds_i], &fds[num_fds]); |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 704 | |
[email protected] | afb4bad | 2009-02-12 02:39:31 | [diff] [blame] | 705 | // When the input data buffer is empty, the overflow fds should be too. If |
| 706 | // this is not the case, we probably have a rogue renderer which is trying |
| 707 | // to fill our descriptor table. |
| 708 | if (input_overflow_buf_.empty() && !input_overflow_fds_.empty()) { |
| 709 | // We close these descriptors in Close() |
| 710 | return false; |
| 711 | } |
| 712 | |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 713 | bytes_read = 0; // Get more data. |
| 714 | } |
| 715 | |
| 716 | return true; |
| 717 | } |
| 718 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 719 | bool Channel::ChannelImpl::ProcessOutgoingMessages() { |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 720 | DCHECK(!waiting_connect_); // Why are we trying to send messages if there's |
| 721 | // no connection? |
[email protected] | e45e6c0 | 2008-12-15 22:02:17 | [diff] [blame] | 722 | is_blocked_on_write_ = false; |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 723 | |
[email protected] | baf556a | 2009-09-04 21:34:05 | [diff] [blame] | 724 | if (output_queue_.empty()) { |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 725 | return true; |
[email protected] | baf556a | 2009-09-04 21:34:05 | [diff] [blame] | 726 | } |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 727 | |
[email protected] | baf556a | 2009-09-04 21:34:05 | [diff] [blame] | 728 | if (pipe_ == -1) { |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 729 | return false; |
[email protected] | baf556a | 2009-09-04 21:34:05 | [diff] [blame] | 730 | } |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 731 | |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 732 | // Write out all the messages we can till the write blocks or there are no |
| 733 | // more outgoing messages. |
| 734 | while (!output_queue_.empty()) { |
| 735 | Message* msg = output_queue_.front(); |
| 736 | |
[email protected] | baf556a | 2009-09-04 21:34:05 | [diff] [blame] | 737 | #if defined(OS_LINUX) |
| 738 | scoped_ptr<Message> hello; |
| 739 | if (remote_fd_pipe_ != -1 && |
| 740 | msg->routing_id() == MSG_ROUTING_NONE && |
| 741 | msg->type() == HELLO_MESSAGE_TYPE) { |
| 742 | hello.reset(new Message(MSG_ROUTING_NONE, |
| 743 | HELLO_MESSAGE_TYPE, |
| 744 | IPC::Message::PRIORITY_NORMAL)); |
| 745 | void* iter = NULL; |
| 746 | int pid; |
| 747 | if (!msg->ReadInt(&iter, &pid) || |
| 748 | !hello->WriteInt(pid)) { |
| 749 | NOTREACHED(); |
| 750 | } |
| 751 | DCHECK_EQ(hello->size(), msg->size()); |
| 752 | if (!hello->WriteFileDescriptor(base::FileDescriptor(remote_fd_pipe_, |
| 753 | false))) { |
| 754 | NOTREACHED(); |
| 755 | } |
| 756 | msg = hello.get(); |
| 757 | DCHECK_EQ(msg->file_descriptor_set()->size(), 1); |
| 758 | } |
| 759 | #endif |
| 760 | |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 761 | size_t amt_to_write = msg->size() - message_send_bytes_written_; |
[email protected] | 3d1b666 | 2009-01-29 17:03:11 | [diff] [blame] | 762 | DCHECK(amt_to_write != 0); |
[email protected] | baf556a | 2009-09-04 21:34:05 | [diff] [blame] | 763 | const char* out_bytes = reinterpret_cast<const char*>(msg->data()) + |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 764 | message_send_bytes_written_; |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 765 | |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 766 | struct msghdr msgh = {0}; |
| 767 | struct iovec iov = {const_cast<char*>(out_bytes), amt_to_write}; |
| 768 | msgh.msg_iov = &iov; |
| 769 | msgh.msg_iovlen = 1; |
| 770 | char buf[CMSG_SPACE( |
| 771 | sizeof(int[FileDescriptorSet::MAX_DESCRIPTORS_PER_MESSAGE]))]; |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 772 | |
[email protected] | baf556a | 2009-09-04 21:34:05 | [diff] [blame] | 773 | ssize_t bytes_written = 1; |
| 774 | int fd_written = -1; |
| 775 | |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 776 | if (message_send_bytes_written_ == 0 && |
| 777 | !msg->file_descriptor_set()->empty()) { |
| 778 | // This is the first chunk of a message which has descriptors to send |
| 779 | struct cmsghdr *cmsg; |
| 780 | const unsigned num_fds = msg->file_descriptor_set()->size(); |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 781 | |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 782 | DCHECK_LE(num_fds, FileDescriptorSet::MAX_DESCRIPTORS_PER_MESSAGE); |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 783 | |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 784 | msgh.msg_control = buf; |
| 785 | msgh.msg_controllen = CMSG_SPACE(sizeof(int) * num_fds); |
| 786 | cmsg = CMSG_FIRSTHDR(&msgh); |
| 787 | cmsg->cmsg_level = SOL_SOCKET; |
| 788 | cmsg->cmsg_type = SCM_RIGHTS; |
| 789 | cmsg->cmsg_len = CMSG_LEN(sizeof(int) * num_fds); |
| 790 | msg->file_descriptor_set()->GetDescriptors( |
| 791 | reinterpret_cast<int*>(CMSG_DATA(cmsg))); |
| 792 | msgh.msg_controllen = cmsg->cmsg_len; |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 793 | |
[email protected] | 168ae92 | 2009-12-04 18:08:45 | [diff] [blame] | 794 | // DCHECK_LE above already checks that |
| 795 | // num_fds < MAX_DESCRIPTORS_PER_MESSAGE so no danger of overflow. |
| 796 | msg->header()->num_fds = static_cast<uint16>(num_fds); |
[email protected] | baf556a | 2009-09-04 21:34:05 | [diff] [blame] | 797 | |
| 798 | #if defined(OS_LINUX) |
| 799 | if (!uses_fifo_ && |
| 800 | (msg->routing_id() != MSG_ROUTING_NONE || |
| 801 | msg->type() != HELLO_MESSAGE_TYPE)) { |
| 802 | // Only the Hello message sends the file descriptor with the message. |
| 803 | // Subsequently, we can send file descriptors on the dedicated |
| 804 | // fd_pipe_ which makes Seccomp sandbox operation more efficient. |
| 805 | struct iovec fd_pipe_iov = { const_cast<char *>(""), 1 }; |
| 806 | msgh.msg_iov = &fd_pipe_iov; |
| 807 | fd_written = fd_pipe_; |
| 808 | bytes_written = HANDLE_EINTR(sendmsg(fd_pipe_, &msgh, MSG_DONTWAIT)); |
| 809 | msgh.msg_iov = &iov; |
| 810 | msgh.msg_controllen = 0; |
| 811 | if (bytes_written > 0) { |
| 812 | msg->file_descriptor_set()->CommitAll(); |
| 813 | } |
| 814 | } |
| 815 | #endif |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 816 | } |
| 817 | |
[email protected] | baf556a | 2009-09-04 21:34:05 | [diff] [blame] | 818 | if (bytes_written == 1) { |
| 819 | fd_written = pipe_; |
| 820 | #if defined(OS_LINUX) |
| 821 | if (mode_ != MODE_SERVER && !uses_fifo_ && |
| 822 | msg->routing_id() == MSG_ROUTING_NONE && |
| 823 | msg->type() == HELLO_MESSAGE_TYPE) { |
| 824 | DCHECK_EQ(msg->file_descriptor_set()->size(), 1); |
| 825 | } |
| 826 | if (!uses_fifo_ && !msgh.msg_controllen) { |
| 827 | bytes_written = HANDLE_EINTR(write(pipe_, out_bytes, amt_to_write)); |
| 828 | } else |
| 829 | #endif |
| 830 | { |
| 831 | bytes_written = HANDLE_EINTR(sendmsg(pipe_, &msgh, MSG_DONTWAIT)); |
| 832 | } |
| 833 | } |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 834 | if (bytes_written > 0) |
| 835 | msg->file_descriptor_set()->CommitAll(); |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 836 | |
[email protected] | 86c3d9e | 2009-12-08 14:48:08 | [diff] [blame^] | 837 | if (bytes_written < 0 && !SocketWriteErrorIsRecoverable()) { |
[email protected] | cb38c0b2 | 2009-05-27 18:29:48 | [diff] [blame] | 838 | #if defined(OS_MACOSX) |
| 839 | // On OSX writing to a pipe with no listener returns EPERM. |
| 840 | if (errno == EPERM) { |
| 841 | Close(); |
| 842 | return false; |
| 843 | } |
| 844 | #endif // OS_MACOSX |
[email protected] | 7bf54f5e | 2009-10-23 01:48:21 | [diff] [blame] | 845 | if (errno == EPIPE) { |
| 846 | Close(); |
| 847 | return false; |
| 848 | } |
[email protected] | 780ae94 | 2009-12-03 13:35:46 | [diff] [blame] | 849 | PLOG(ERROR) << "pipe error on " |
| 850 | << fd_written |
| 851 | << " Currently writing message of size:" |
[email protected] | 86c3d9e | 2009-12-08 14:48:08 | [diff] [blame^] | 852 | << msg->size(); |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 853 | return false; |
| 854 | } |
| 855 | |
| 856 | if (static_cast<size_t>(bytes_written) != amt_to_write) { |
[email protected] | 3d1b666 | 2009-01-29 17:03:11 | [diff] [blame] | 857 | if (bytes_written > 0) { |
| 858 | // If write() fails with EAGAIN then bytes_written will be -1. |
| 859 | message_send_bytes_written_ += bytes_written; |
| 860 | } |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 861 | |
| 862 | // Tell libevent to call us back once things are unblocked. |
[email protected] | e45e6c0 | 2008-12-15 22:02:17 | [diff] [blame] | 863 | is_blocked_on_write_ = true; |
| 864 | MessageLoopForIO::current()->WatchFileDescriptor( |
| 865 | pipe_, |
| 866 | false, // One shot |
| 867 | MessageLoopForIO::WATCH_WRITE, |
| 868 | &write_watcher_, |
| 869 | this); |
[email protected] | 3d1b666 | 2009-01-29 17:03:11 | [diff] [blame] | 870 | return true; |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 871 | } else { |
| 872 | message_send_bytes_written_ = 0; |
| 873 | |
| 874 | // Message sent OK! |
| 875 | #ifdef IPC_MESSAGE_DEBUG_EXTRA |
| 876 | DLOG(INFO) << "sent message @" << msg << " on channel @" << this << |
| 877 | " with type " << msg->type(); |
| 878 | #endif |
[email protected] | baf556a | 2009-09-04 21:34:05 | [diff] [blame] | 879 | delete output_queue_.front(); |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 880 | output_queue_.pop(); |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 881 | } |
| 882 | } |
| 883 | return true; |
| 884 | } |
| 885 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 886 | bool Channel::ChannelImpl::Send(Message* message) { |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 887 | #ifdef IPC_MESSAGE_DEBUG_EXTRA |
| 888 | DLOG(INFO) << "sending message @" << message << " on channel @" << this |
| 889 | << " with type " << message->type() |
| 890 | << " (" << output_queue_.size() << " in queue)"; |
| 891 | #endif |
| 892 | |
[email protected] | 4c2c1db | 2009-03-20 20:56:55 | [diff] [blame] | 893 | #ifdef IPC_MESSAGE_LOG_ENABLED |
[email protected] | 9a3a293b | 2009-06-04 22:28:16 | [diff] [blame] | 894 | Logging::current()->OnSendMessage(message, ""); |
[email protected] | 4c2c1db | 2009-03-20 20:56:55 | [diff] [blame] | 895 | #endif |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 896 | |
| 897 | output_queue_.push(message); |
| 898 | if (!waiting_connect_) { |
[email protected] | e45e6c0 | 2008-12-15 22:02:17 | [diff] [blame] | 899 | if (!is_blocked_on_write_) { |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 900 | if (!ProcessOutgoingMessages()) |
| 901 | return false; |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | return true; |
| 906 | } |
| 907 | |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 908 | int Channel::ChannelImpl::GetClientFileDescriptor() const { |
| 909 | return client_pipe_; |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 910 | } |
| 911 | |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 912 | // Called by libevent when we can read from th pipe without blocking. |
[email protected] | e45e6c0 | 2008-12-15 22:02:17 | [diff] [blame] | 913 | void Channel::ChannelImpl::OnFileCanReadWithoutBlocking(int fd) { |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 914 | bool send_server_hello_msg = false; |
| 915 | if (waiting_connect_ && mode_ == MODE_SERVER) { |
[email protected] | baf556a | 2009-09-04 21:34:05 | [diff] [blame] | 916 | if (uses_fifo_) { |
| 917 | if (!ServerAcceptFifoConnection(server_listen_pipe_, &pipe_)) { |
| 918 | Close(); |
| 919 | } |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 920 | |
[email protected] | baf556a | 2009-09-04 21:34:05 | [diff] [blame] | 921 | // No need to watch the listening socket any longer since only one client |
| 922 | // can connect. So unregister with libevent. |
| 923 | server_listen_connection_watcher_.StopWatchingFileDescriptor(); |
| 924 | |
| 925 | // Start watching our end of the socket. |
| 926 | MessageLoopForIO::current()->WatchFileDescriptor( |
| 927 | pipe_, |
| 928 | true, |
| 929 | MessageLoopForIO::WATCH_READ, |
| 930 | &read_watcher_, |
| 931 | this); |
| 932 | |
| 933 | waiting_connect_ = false; |
| 934 | } else { |
| 935 | // In the case of a socketpair() the server starts listening on its end |
| 936 | // of the pipe in Connect(). |
| 937 | waiting_connect_ = false; |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 938 | } |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 939 | send_server_hello_msg = true; |
| 940 | } |
| 941 | |
| 942 | if (!waiting_connect_ && fd == pipe_) { |
| 943 | if (!ProcessIncomingMessages()) { |
| 944 | Close(); |
| 945 | listener_->OnChannelError(); |
[email protected] | 9808ca4 | 2009-09-25 23:35:39 | [diff] [blame] | 946 | // The OnChannelError() call may delete this, so we need to exit now. |
| 947 | return; |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 948 | } |
| 949 | } |
| 950 | |
| 951 | // If we're a server and handshaking, then we want to make sure that we |
| 952 | // only send our handshake message after we've processed the client's. |
| 953 | // This gives us a chance to kill the client if the incoming handshake |
| 954 | // is invalid. |
| 955 | if (send_server_hello_msg) { |
| 956 | ProcessOutgoingMessages(); |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | // Called by libevent when we can write to the pipe without blocking. |
[email protected] | e45e6c0 | 2008-12-15 22:02:17 | [diff] [blame] | 961 | void Channel::ChannelImpl::OnFileCanWriteWithoutBlocking(int fd) { |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 962 | if (!ProcessOutgoingMessages()) { |
| 963 | Close(); |
| 964 | listener_->OnChannelError(); |
| 965 | } |
| 966 | } |
| 967 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 968 | void Channel::ChannelImpl::Close() { |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 969 | // Close can be called multiple time, so we need to make sure we're |
| 970 | // idempotent. |
| 971 | |
| 972 | // Unregister libevent for the listening socket and close it. |
[email protected] | e45e6c0 | 2008-12-15 22:02:17 | [diff] [blame] | 973 | server_listen_connection_watcher_.StopWatchingFileDescriptor(); |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 974 | |
| 975 | if (server_listen_pipe_ != -1) { |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 976 | HANDLE_EINTR(close(server_listen_pipe_)); |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 977 | server_listen_pipe_ = -1; |
| 978 | } |
| 979 | |
| 980 | // Unregister libevent for the FIFO and close it. |
[email protected] | e45e6c0 | 2008-12-15 22:02:17 | [diff] [blame] | 981 | read_watcher_.StopWatchingFileDescriptor(); |
| 982 | write_watcher_.StopWatchingFileDescriptor(); |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 983 | if (pipe_ != -1) { |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 984 | HANDLE_EINTR(close(pipe_)); |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 985 | pipe_ = -1; |
| 986 | } |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 987 | if (client_pipe_ != -1) { |
[email protected] | f0ef2da | 2009-07-08 01:26:34 | [diff] [blame] | 988 | Singleton<PipeMap>()->RemoveAndClose(pipe_name_); |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 989 | client_pipe_ = -1; |
| 990 | } |
[email protected] | baf556a | 2009-09-04 21:34:05 | [diff] [blame] | 991 | #if defined(OS_LINUX) |
| 992 | if (fd_pipe_ != -1) { |
| 993 | HANDLE_EINTR(close(fd_pipe_)); |
| 994 | fd_pipe_ = -1; |
| 995 | } |
| 996 | if (remote_fd_pipe_ != -1) { |
| 997 | HANDLE_EINTR(close(remote_fd_pipe_)); |
| 998 | remote_fd_pipe_ = -1; |
| 999 | } |
| 1000 | #endif |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 1001 | |
[email protected] | 5f594c0 | 2009-05-01 22:37:59 | [diff] [blame] | 1002 | if (uses_fifo_) { |
| 1003 | // Unlink the FIFO |
| 1004 | unlink(pipe_name_.c_str()); |
| 1005 | } |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 1006 | |
| 1007 | while (!output_queue_.empty()) { |
| 1008 | Message* m = output_queue_.front(); |
| 1009 | output_queue_.pop(); |
| 1010 | delete m; |
| 1011 | } |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 1012 | |
| 1013 | // Close any outstanding, received file descriptors |
| 1014 | for (std::vector<int>::iterator |
| 1015 | i = input_overflow_fds_.begin(); i != input_overflow_fds_.end(); ++i) { |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 1016 | HANDLE_EINTR(close(*i)); |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 1017 | } |
| 1018 | input_overflow_fds_.clear(); |
[email protected] | fa95fc9 | 2008-12-08 18:10:14 | [diff] [blame] | 1019 | } |
| 1020 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 1021 | //------------------------------------------------------------------------------ |
| 1022 | // Channel's methods simply call through to ChannelImpl. |
[email protected] | 9a3a293b | 2009-06-04 22:28:16 | [diff] [blame] | 1023 | Channel::Channel(const std::string& channel_id, Mode mode, |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 1024 | Listener* listener) |
| 1025 | : channel_impl_(new ChannelImpl(channel_id, mode, listener)) { |
| 1026 | } |
| 1027 | |
| 1028 | Channel::~Channel() { |
| 1029 | delete channel_impl_; |
| 1030 | } |
| 1031 | |
| 1032 | bool Channel::Connect() { |
| 1033 | return channel_impl_->Connect(); |
| 1034 | } |
| 1035 | |
| 1036 | void Channel::Close() { |
| 1037 | channel_impl_->Close(); |
| 1038 | } |
| 1039 | |
| 1040 | void Channel::set_listener(Listener* listener) { |
| 1041 | channel_impl_->set_listener(listener); |
| 1042 | } |
| 1043 | |
| 1044 | bool Channel::Send(Message* message) { |
| 1045 | return channel_impl_->Send(message); |
| 1046 | } |
| 1047 | |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 1048 | int Channel::GetClientFileDescriptor() const { |
| 1049 | return channel_impl_->GetClientFileDescriptor(); |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 1050 | } |
| 1051 | |
[email protected] | d4651ff | 2008-12-02 16:51:58 | [diff] [blame] | 1052 | } // namespace IPC |