blob: 545ad0ca401566f949ae06763beda727866ebd22 [file] [log] [blame]
[email protected]d4651ff2008-12-02 16:51:581// 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]946d1b22009-07-22 23:57:215#include "ipc/ipc_channel_posix.h"
[email protected]d4651ff2008-12-02 16:51:586
[email protected]c3110742008-12-11 00:36:477#include <errno.h>
[email protected]fa95fc92008-12-08 18:10:148#include <fcntl.h>
[email protected]e45e6c02008-12-15 22:02:179#include <stddef.h>
[email protected]fa95fc92008-12-08 18:10:1410#include <sys/types.h>
11#include <sys/socket.h>
12#include <sys/stat.h>
[email protected]e45e6c02008-12-15 22:02:1713#include <sys/un.h>
[email protected]e45e6c02008-12-15 22:02:1714
[email protected]e8fce882009-01-20 22:02:5815#include <string>
16#include <map>
17
[email protected]df3c1ca12008-12-19 21:37:0118#include "base/command_line.h"
[email protected]157c61b2009-05-01 21:37:3119#include "base/eintr_wrapper.h"
[email protected]cc8f1462009-06-12 17:36:5520#include "base/global_descriptors_posix.h"
[email protected]e8fce882009-01-20 22:02:5821#include "base/lock.h"
[email protected]fa95fc92008-12-08 18:10:1422#include "base/logging.h"
23#include "base/process_util.h"
24#include "base/scoped_ptr.h"
[email protected]e8fce882009-01-20 22:02:5825#include "base/singleton.h"
[email protected]e6f02ab2009-04-10 22:29:2926#include "base/stats_counters.h"
[email protected]946d1b22009-07-22 23:57:2127#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]d4651ff2008-12-02 16:51:5833
34namespace IPC {
35
[email protected]5f594c02009-05-01 22:37:5936// 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]cc8f1462009-06-12 17:36:5543// magic file descriptor kPrimaryIPCChannel in the child. Future
[email protected]5f594c02009-05-01 22:37:5944// connections (file descriptors) can then be passed via that
45// connection via sendmsg().
46
[email protected]fa95fc92008-12-08 18:10:1447//------------------------------------------------------------------------------
[email protected]fa95fc92008-12-08 18:10:1448namespace {
49
[email protected]5f594c02009-05-01 22:37:5950// The PipeMap class works around this quirk related to unit tests:
[email protected]e8fce882009-01-20 22:02:5851//
[email protected]5f594c02009-05-01 22:37:5952// When running as a server, we install the client socket in a
[email protected]cc8f1462009-06-12 17:36:5553// specific file descriptor number (@kPrimaryIPCChannel). However, we
[email protected]5f594c02009-05-01 22:37:5954// 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]e8fce882009-01-20 22:02:5856//
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]cc8f1462009-06-12 17:36:5561// the magic slot (@kPrimaryIPCChannel). The client will search for the
[email protected]e8fce882009-01-20 22:02:5862// 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]5f594c02009-05-01 22:37:5964// close its copy of the client socket and remove the mapping.
[email protected]e8fce882009-01-20 22:02:5865//
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
77class 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]f0ef2da2009-07-08 01:26:3491 void RemoveAndClose(const std::string& channel_id) {
[email protected]e8fce882009-01-20 22:02:5892 AutoLock locked(lock_);
93
94 ChannelToFDMap::iterator i = map_.find(channel_id);
[email protected]f0ef2da2009-07-08 01:26:3495 if (i != map_.end()) {
96 HANDLE_EINTR(close(i->second));
[email protected]e8fce882009-01-20 22:02:5897 map_.erase(i);
[email protected]f0ef2da2009-07-08 01:26:3498 }
[email protected]e8fce882009-01-20 22:02:5899 }
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]d2e884d2009-06-22 20:37:52108 CHECK(i == map_.end()) << "Creating second IPC server (fd " << fd << ") "
109 << "for '" << channel_id << "' while first "
110 << "(fd " << i->second << ") still exists";
[email protected]e8fce882009-01-20 22:02:58111 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]d2e884d2009-06-22 20:37:52120// Used to map a channel name to the equivalent FD # in the current process.
121// Returns -1 if the channel is unknown.
122int ChannelNameToFD(const std::string& channel_id) {
[email protected]e8fce882009-01-20 22:02:58123 // See the large block comment above PipeMap for the reasoning here.
124 const int fd = Singleton<PipeMap>()->Lookup(channel_id);
[email protected]e8fce882009-01-20 22:02:58125
[email protected]d2e884d2009-06-22 20:37:52126 if (fd != -1) {
127 int dup_fd = dup(fd);
128 if (dup_fd < 0)
[email protected]57b765672009-10-13 18:27:40129 PLOG(FATAL) << "dup(" << fd << ")";
[email protected]d2e884d2009-06-22 20:37:52130 return dup_fd;
131 }
132
133 return fd;
[email protected]df3c1ca12008-12-19 21:37:01134}
135
136//------------------------------------------------------------------------------
[email protected]02d66512009-03-17 01:48:35137sockaddr_un sizecheck;
138const size_t kMaxPipeNameLength = sizeof(sizecheck.sun_path);
[email protected]fa95fc92008-12-08 18:10:14139
140// Creates a Fifo with the specified name ready to listen on.
[email protected]5f594c02009-05-01 22:37:59141bool CreateServerFifo(const std::string& pipe_name, int* server_listen_fd) {
[email protected]fa95fc92008-12-08 18:10:14142 DCHECK(server_listen_fd);
[email protected]02d66512009-03-17 01:48:35143 DCHECK_GT(pipe_name.length(), 0u);
144 DCHECK_LT(pipe_name.length(), kMaxPipeNameLength);
[email protected]fa95fc92008-12-08 18:10:14145
[email protected]02d66512009-03-17 01:48:35146 if (pipe_name.length() == 0 || pipe_name.length() >= kMaxPipeNameLength) {
[email protected]fa95fc92008-12-08 18:10:14147 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]157c61b2009-05-01 21:37:31158 HANDLE_EINTR(close(fd));
[email protected]fa95fc92008-12-08 18:10:14159 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]02d66512009-03-17 01:48:35169 snprintf(unix_addr.sun_path, kMaxPipeNameLength, "%s", pipe_name.c_str());
[email protected]fa95fc92008-12-08 18:10:14170 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]157c61b2009-05-01 21:37:31176 HANDLE_EINTR(close(fd));
[email protected]fa95fc92008-12-08 18:10:14177 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]157c61b2009-05-01 21:37:31183 HANDLE_EINTR(close(fd));
[email protected]fa95fc92008-12-08 18:10:14184 return false;
185 }
186
187 *server_listen_fd = fd;
188 return true;
189}
190
191// Accept a connection on a fifo.
192bool ServerAcceptFifoConnection(int server_listen_fd, int* server_socket) {
193 DCHECK(server_socket);
194
[email protected]157c61b2009-05-01 21:37:31195 int accept_fd = HANDLE_EINTR(accept(server_listen_fd, NULL, 0));
[email protected]fa95fc92008-12-08 18:10:14196 if (accept_fd < 0)
197 return false;
[email protected]3af21262008-12-16 21:49:34198 if (fcntl(accept_fd, F_SETFL, O_NONBLOCK) == -1) {
[email protected]157c61b2009-05-01 21:37:31199 HANDLE_EINTR(close(accept_fd));
[email protected]3af21262008-12-16 21:49:34200 return false;
201 }
[email protected]fa95fc92008-12-08 18:10:14202
203 *server_socket = accept_fd;
204 return true;
205}
206
207bool ClientConnectToFifo(const std::string &pipe_name, int* client_socket) {
208 DCHECK(client_socket);
[email protected]02d66512009-03-17 01:48:35209 DCHECK_LT(pipe_name.length(), kMaxPipeNameLength);
[email protected]fa95fc92008-12-08 18:10:14210
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]02d66512009-03-17 01:48:35220 LOG(ERROR) << "fcntl failed";
[email protected]157c61b2009-05-01 21:37:31221 HANDLE_EINTR(close(fd));
[email protected]fa95fc92008-12-08 18:10:14222 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]02d66512009-03-17 01:48:35229 snprintf(server_unix_addr.sun_path, kMaxPipeNameLength, "%s",
[email protected]fa95fc92008-12-08 18:10:14230 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]157c61b2009-05-01 21:37:31234 if (HANDLE_EINTR(connect(fd, reinterpret_cast<sockaddr*>(&server_unix_addr),
235 server_unix_addr_len)) != 0) {
236 HANDLE_EINTR(close(fd));
[email protected]fa95fc92008-12-08 18:10:14237 return false;
238 }
239
240 *client_socket = fd;
241 return true;
242}
243
[email protected]86c3d9e2009-12-08 14:48:08244bool 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]fa95fc92008-12-08 18:10:14263} // namespace
[email protected]d4651ff2008-12-02 16:51:58264//------------------------------------------------------------------------------
265
[email protected]9a3a293b2009-06-04 22:28:16266Channel::ChannelImpl::ChannelImpl(const std::string& channel_id, Mode mode,
[email protected]514411fc2008-12-10 22:28:11267 Listener* listener)
[email protected]fa95fc92008-12-08 18:10:14268 : mode_(mode),
[email protected]e45e6c02008-12-15 22:02:17269 is_blocked_on_write_(false),
270 message_send_bytes_written_(0),
[email protected]bb975362009-01-21 01:00:22271 uses_fifo_(CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]97c42bcf2009-02-27 07:21:58272 switches::kIPCUseFIFO)),
[email protected]e45e6c02008-12-15 22:02:17273 server_listen_pipe_(-1),
274 pipe_(-1),
[email protected]df3c1ca12008-12-19 21:37:01275 client_pipe_(-1),
[email protected]baf556a2009-09-04 21:34:05276#if defined(OS_LINUX)
277 fd_pipe_(-1),
278 remote_fd_pipe_(-1),
279#endif
[email protected]e45e6c02008-12-15 22:02:17280 listener_(listener),
281 waiting_connect_(true),
[email protected]e45e6c02008-12-15 22:02:17282 factory_(this) {
[email protected]fa95fc92008-12-08 18:10:14283 if (!CreatePipe(channel_id, mode)) {
284 // The pipe may have been closed already.
[email protected]57b765672009-10-13 18:27:40285 PLOG(WARNING) << "Unable to create pipe named \"" << channel_id
286 << "\" in " << (mode == MODE_SERVER ? "server" : "client")
287 << " mode";
[email protected]fa95fc92008-12-08 18:10:14288 }
[email protected]d4651ff2008-12-02 16:51:58289}
290
[email protected]d2e884d2009-06-22 20:37:52291// static
292void AddChannelSocket(const std::string& name, int socket) {
293 Singleton<PipeMap>()->Insert(name, socket);
294}
295
296// static
[email protected]f0ef2da2009-07-08 01:26:34297void RemoveAndCloseChannelSocket(const std::string& name) {
298 Singleton<PipeMap>()->RemoveAndClose(name);
299}
300
301// static
[email protected]d2e884d2009-06-22 20:37:52302bool SocketPair(int* fd1, int* fd2) {
303 int pipe_fds[2];
304 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipe_fds) != 0) {
[email protected]57b765672009-10-13 18:27:40305 PLOG(ERROR) << "socketpair()";
[email protected]d2e884d2009-06-22 20:37:52306 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]57b765672009-10-13 18:27:40312 PLOG(ERROR) << "fcntl(O_NONBLOCK)";
[email protected]d2e884d2009-06-22 20:37:52313 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]9a3a293b2009-06-04 22:28:16324bool Channel::ChannelImpl::CreatePipe(const std::string& channel_id,
[email protected]514411fc2008-12-10 22:28:11325 Mode mode) {
[email protected]fa95fc92008-12-08 18:10:14326 DCHECK(server_listen_pipe_ == -1 && pipe_ == -1);
327
[email protected]df3c1ca12008-12-19 21:37:01328 if (uses_fifo_) {
[email protected]5f594c02009-05-01 22:37:59329 // 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]9a3a293b2009-06-04 22:28:16333 pipe_name_ = "/var/tmp/chrome_" + channel_id;
[email protected]df3c1ca12008-12-19 21:37:01334 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]fa95fc92008-12-08 18:10:14343 }
344 } else {
[email protected]d2e884d2009-06-22 20:37:52345 // 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]9a3a293b2009-06-04 22:28:16351 pipe_name_ = channel_id;
[email protected]d2e884d2009-06-22 20:37:52352 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]554a8852009-11-30 22:14:37360 // 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]d2e884d2009-06-22 20:37:52370 pipe_ = Singleton<base::GlobalDescriptors>()->Get(kPrimaryIPCChannel);
[email protected]df3c1ca12008-12-19 21:37:01371 }
[email protected]df3c1ca12008-12-19 21:37:01372 } else {
[email protected]baf556a2009-09-04 21:34:05373 waiting_connect_ = mode == MODE_SERVER;
[email protected]fa95fc92008-12-08 18:10:14374 }
[email protected]fa95fc92008-12-08 18:10:14375 }
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]baf556a2009-09-04 21:34:05381 #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]fa95fc92008-12-08 18:10:14400 if (!msg->WriteInt(base::GetCurrentProcId())) {
401 Close();
402 return false;
403 }
404
405 output_queue_.push(msg.release());
406 return true;
[email protected]d4651ff2008-12-02 16:51:58407}
408
[email protected]514411fc2008-12-10 22:28:11409bool Channel::ChannelImpl::Connect() {
[email protected]df3c1ca12008-12-19 21:37:01410 if (mode_ == MODE_SERVER && uses_fifo_) {
[email protected]fa95fc92008-12-08 18:10:14411 if (server_listen_pipe_ == -1) {
412 return false;
413 }
[email protected]e45e6c02008-12-15 22:02:17414 MessageLoopForIO::current()->WatchFileDescriptor(
415 server_listen_pipe_,
416 true,
417 MessageLoopForIO::WATCH_READ,
418 &server_listen_connection_watcher_,
419 this);
[email protected]fa95fc92008-12-08 18:10:14420 } else {
421 if (pipe_ == -1) {
422 return false;
423 }
[email protected]e45e6c02008-12-15 22:02:17424 MessageLoopForIO::current()->WatchFileDescriptor(
425 pipe_,
426 true,
427 MessageLoopForIO::WATCH_READ,
428 &read_watcher_,
429 this);
[email protected]baf556a2009-09-04 21:34:05430 waiting_connect_ = mode_ == MODE_SERVER;
[email protected]fa95fc92008-12-08 18:10:14431 }
432
433 if (!waiting_connect_)
434 return ProcessOutgoingMessages();
435 return true;
[email protected]d4651ff2008-12-02 16:51:58436}
[email protected]fa95fc92008-12-08 18:10:14437
[email protected]514411fc2008-12-10 22:28:11438bool Channel::ChannelImpl::ProcessIncomingMessages() {
[email protected]fa95fc92008-12-08 18:10:14439 ssize_t bytes_read = 0;
440
[email protected]526776c2009-02-07 00:39:26441 struct msghdr msg = {0};
442 struct iovec iov = {input_buf_, Channel::kReadBufferSize};
443
[email protected]526776c2009-02-07 00:39:26444 msg.msg_iovlen = 1;
445 msg.msg_control = input_cmsg_buf_;
[email protected]526776c2009-02-07 00:39:26446
[email protected]fa95fc92008-12-08 18:10:14447 for (;;) {
[email protected]baf556a2009-09-04 21:34:05448 msg.msg_iov = &iov;
[email protected]cef7c522009-02-10 08:15:02449
[email protected]fa95fc92008-12-08 18:10:14450 if (bytes_read == 0) {
451 if (pipe_ == -1)
452 return false;
453
454 // Read from pipe.
[email protected]526776c2009-02-07 00:39:26455 // recvmsg() returns 0 if the connection has closed or EAGAIN if no data
456 // is waiting on the pipe.
[email protected]baf556a2009-09-04 21:34:05457#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]fa95fc92008-12-08 18:10:14468 if (bytes_read < 0) {
469 if (errno == EAGAIN) {
470 return true;
[email protected]7f6f06232009-05-22 21:15:39471#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]7bf54f5e2009-10-23 01:48:21478 } else if (errno == ECONNRESET || errno == EPIPE) {
[email protected]d8365fc2009-10-16 19:44:31479 return false;
[email protected]fa95fc92008-12-08 18:10:14480 } else {
[email protected]57b765672009-10-13 18:27:40481 PLOG(ERROR) << "pipe error (" << pipe_ << ")";
[email protected]fa95fc92008-12-08 18:10:14482 return false;
483 }
484 } else if (bytes_read == 0) {
485 // The pipe has closed...
[email protected]e8fce882009-01-20 22:02:58486 return false;
[email protected]fa95fc92008-12-08 18:10:14487 }
488 }
489 DCHECK(bytes_read);
490
[email protected]e8fce882009-01-20 22:02:58491 if (client_pipe_ != -1) {
[email protected]f0ef2da2009-07-08 01:26:34492 Singleton<PipeMap>()->RemoveAndClose(pipe_name_);
[email protected]e8fce882009-01-20 22:02:58493 client_pipe_ = -1;
494 }
495
[email protected]526776c2009-02-07 00:39:26496 // a pointer to an array of |num_wire_fds| file descriptors from the read
[email protected]337c6bf2009-02-07 00:51:58497 const int* wire_fds = NULL;
[email protected]526776c2009-02-07 00:39:26498 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]526776c2009-02-07 00:39:26502
[email protected]afb4bad2009-02-12 02:39:31503 // 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]157c61b2009-05-01 21:37:31531 HANDLE_EINTR(close(wire_fds[i]));
[email protected]afb4bad2009-02-12 02:39:31532 return false;
533 }
534 break;
[email protected]526776c2009-02-07 00:39:26535 }
[email protected]526776c2009-02-07 00:39:26536 }
537 }
538
[email protected]fa95fc92008-12-08 18:10:14539 // 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]526776c2009-02-07 00:39:26557 // 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]baf556a2009-09-04 21:34:05559 const int* fds = NULL;
560 unsigned num_fds = 0;
[email protected]526776c2009-02-07 00:39:26561 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]baf556a2009-09-04 21:34:05567 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]526776c2009-02-07 00:39:26573 fds = &input_overflow_fds_[0];
574 num_fds = input_overflow_fds_.size();
575 }
576
[email protected]fa95fc92008-12-08 18:10:14577 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]7135bb042009-02-12 04:05:28581 Message m(p, len);
[email protected]baf556a2009-09-04 21:34:05582
[email protected]526776c2009-02-07 00:39:26583 if (m.header()->num_fds) {
584 // the message has file descriptors
[email protected]7135bb042009-02-12 04:05:28585 const char* error = NULL;
[email protected]526776c2009-02-07 00:39:26586 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]baf556a2009-09-04 21:34:05589#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]7135bb042009-02-12 04:05:28635 }
636
637 if (m.header()->num_fds >
[email protected]d0c0b1d2009-02-12 18:32:45638 FileDescriptorSet::MAX_DESCRIPTORS_PER_MESSAGE) {
[email protected]7135bb042009-02-12 04:05:28639 // 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]526776c2009-02-07 00:39:26645 << " 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]157c61b2009-05-01 21:37:31652 HANDLE_EINTR(close(fds[i]));
[email protected]526776c2009-02-07 00:39:26653 input_overflow_fds_.clear();
[email protected]7135bb042009-02-12 04:05:28654 // abort the connection
[email protected]526776c2009-02-07 00:39:26655 return false;
656 }
657
[email protected]d0c0b1d2009-02-12 18:32:45658 m.file_descriptor_set()->SetDescriptors(
659 &fds[fds_i], m.header()->num_fds);
[email protected]526776c2009-02-07 00:39:26660 fds_i += m.header()->num_fds;
661 }
[email protected]fa95fc92008-12-08 18:10:14662#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]baf556a2009-09-04 21:34:05669 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]fa95fc92008-12-08 18:10:14689 } else {
690 listener_->OnMessageReceived(m);
691 }
692 p = message_tail;
693 } else {
694 // Last message is partial.
695 break;
696 }
[email protected]baf556a2009-09-04 21:34:05697 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]fa95fc92008-12-08 18:10:14701 }
702 input_overflow_buf_.assign(p, end - p);
[email protected]526776c2009-02-07 00:39:26703 input_overflow_fds_ = std::vector<int>(&fds[fds_i], &fds[num_fds]);
[email protected]fa95fc92008-12-08 18:10:14704
[email protected]afb4bad2009-02-12 02:39:31705 // 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]fa95fc92008-12-08 18:10:14713 bytes_read = 0; // Get more data.
714 }
715
716 return true;
717}
718
[email protected]514411fc2008-12-10 22:28:11719bool Channel::ChannelImpl::ProcessOutgoingMessages() {
[email protected]fa95fc92008-12-08 18:10:14720 DCHECK(!waiting_connect_); // Why are we trying to send messages if there's
721 // no connection?
[email protected]e45e6c02008-12-15 22:02:17722 is_blocked_on_write_ = false;
[email protected]fa95fc92008-12-08 18:10:14723
[email protected]baf556a2009-09-04 21:34:05724 if (output_queue_.empty()) {
[email protected]fa95fc92008-12-08 18:10:14725 return true;
[email protected]baf556a2009-09-04 21:34:05726 }
[email protected]fa95fc92008-12-08 18:10:14727
[email protected]baf556a2009-09-04 21:34:05728 if (pipe_ == -1) {
[email protected]fa95fc92008-12-08 18:10:14729 return false;
[email protected]baf556a2009-09-04 21:34:05730 }
[email protected]fa95fc92008-12-08 18:10:14731
[email protected]fa95fc92008-12-08 18:10:14732 // 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]baf556a2009-09-04 21:34:05737#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]fa95fc92008-12-08 18:10:14761 size_t amt_to_write = msg->size() - message_send_bytes_written_;
[email protected]3d1b6662009-01-29 17:03:11762 DCHECK(amt_to_write != 0);
[email protected]baf556a2009-09-04 21:34:05763 const char* out_bytes = reinterpret_cast<const char*>(msg->data()) +
[email protected]fa95fc92008-12-08 18:10:14764 message_send_bytes_written_;
[email protected]526776c2009-02-07 00:39:26765
[email protected]157c61b2009-05-01 21:37:31766 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]526776c2009-02-07 00:39:26772
[email protected]baf556a2009-09-04 21:34:05773 ssize_t bytes_written = 1;
774 int fd_written = -1;
775
[email protected]157c61b2009-05-01 21:37:31776 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]526776c2009-02-07 00:39:26781
[email protected]157c61b2009-05-01 21:37:31782 DCHECK_LE(num_fds, FileDescriptorSet::MAX_DESCRIPTORS_PER_MESSAGE);
[email protected]526776c2009-02-07 00:39:26783
[email protected]157c61b2009-05-01 21:37:31784 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]526776c2009-02-07 00:39:26793
[email protected]168ae922009-12-04 18:08:45794 // 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]baf556a2009-09-04 21:34:05797
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]157c61b2009-05-01 21:37:31816 }
817
[email protected]baf556a2009-09-04 21:34:05818 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]157c61b2009-05-01 21:37:31834 if (bytes_written > 0)
835 msg->file_descriptor_set()->CommitAll();
[email protected]fa95fc92008-12-08 18:10:14836
[email protected]86c3d9e2009-12-08 14:48:08837 if (bytes_written < 0 && !SocketWriteErrorIsRecoverable()) {
[email protected]cb38c0b22009-05-27 18:29:48838#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]7bf54f5e2009-10-23 01:48:21845 if (errno == EPIPE) {
846 Close();
847 return false;
848 }
[email protected]780ae942009-12-03 13:35:46849 PLOG(ERROR) << "pipe error on "
850 << fd_written
851 << " Currently writing message of size:"
[email protected]86c3d9e2009-12-08 14:48:08852 << msg->size();
[email protected]fa95fc92008-12-08 18:10:14853 return false;
854 }
855
856 if (static_cast<size_t>(bytes_written) != amt_to_write) {
[email protected]3d1b6662009-01-29 17:03:11857 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]fa95fc92008-12-08 18:10:14861
862 // Tell libevent to call us back once things are unblocked.
[email protected]e45e6c02008-12-15 22:02:17863 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]3d1b6662009-01-29 17:03:11870 return true;
[email protected]fa95fc92008-12-08 18:10:14871 } 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]baf556a2009-09-04 21:34:05879 delete output_queue_.front();
[email protected]fa95fc92008-12-08 18:10:14880 output_queue_.pop();
[email protected]fa95fc92008-12-08 18:10:14881 }
882 }
883 return true;
884}
885
[email protected]514411fc2008-12-10 22:28:11886bool Channel::ChannelImpl::Send(Message* message) {
[email protected]fa95fc92008-12-08 18:10:14887#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]4c2c1db2009-03-20 20:56:55893#ifdef IPC_MESSAGE_LOG_ENABLED
[email protected]9a3a293b2009-06-04 22:28:16894 Logging::current()->OnSendMessage(message, "");
[email protected]4c2c1db2009-03-20 20:56:55895#endif
[email protected]fa95fc92008-12-08 18:10:14896
897 output_queue_.push(message);
898 if (!waiting_connect_) {
[email protected]e45e6c02008-12-15 22:02:17899 if (!is_blocked_on_write_) {
[email protected]fa95fc92008-12-08 18:10:14900 if (!ProcessOutgoingMessages())
901 return false;
902 }
903 }
904
905 return true;
906}
907
[email protected]cc8f1462009-06-12 17:36:55908int Channel::ChannelImpl::GetClientFileDescriptor() const {
909 return client_pipe_;
[email protected]df3c1ca12008-12-19 21:37:01910}
911
[email protected]fa95fc92008-12-08 18:10:14912// Called by libevent when we can read from th pipe without blocking.
[email protected]e45e6c02008-12-15 22:02:17913void Channel::ChannelImpl::OnFileCanReadWithoutBlocking(int fd) {
[email protected]fa95fc92008-12-08 18:10:14914 bool send_server_hello_msg = false;
915 if (waiting_connect_ && mode_ == MODE_SERVER) {
[email protected]baf556a2009-09-04 21:34:05916 if (uses_fifo_) {
917 if (!ServerAcceptFifoConnection(server_listen_pipe_, &pipe_)) {
918 Close();
919 }
[email protected]df3c1ca12008-12-19 21:37:01920
[email protected]baf556a2009-09-04 21:34:05921 // 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]fa95fc92008-12-08 18:10:14938 }
[email protected]fa95fc92008-12-08 18:10:14939 send_server_hello_msg = true;
940 }
941
942 if (!waiting_connect_ && fd == pipe_) {
943 if (!ProcessIncomingMessages()) {
944 Close();
945 listener_->OnChannelError();
[email protected]9808ca42009-09-25 23:35:39946 // The OnChannelError() call may delete this, so we need to exit now.
947 return;
[email protected]fa95fc92008-12-08 18:10:14948 }
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]e45e6c02008-12-15 22:02:17961void Channel::ChannelImpl::OnFileCanWriteWithoutBlocking(int fd) {
[email protected]fa95fc92008-12-08 18:10:14962 if (!ProcessOutgoingMessages()) {
963 Close();
964 listener_->OnChannelError();
965 }
966}
967
[email protected]514411fc2008-12-10 22:28:11968void Channel::ChannelImpl::Close() {
[email protected]fa95fc92008-12-08 18:10:14969 // 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]e45e6c02008-12-15 22:02:17973 server_listen_connection_watcher_.StopWatchingFileDescriptor();
[email protected]fa95fc92008-12-08 18:10:14974
975 if (server_listen_pipe_ != -1) {
[email protected]157c61b2009-05-01 21:37:31976 HANDLE_EINTR(close(server_listen_pipe_));
[email protected]fa95fc92008-12-08 18:10:14977 server_listen_pipe_ = -1;
978 }
979
980 // Unregister libevent for the FIFO and close it.
[email protected]e45e6c02008-12-15 22:02:17981 read_watcher_.StopWatchingFileDescriptor();
982 write_watcher_.StopWatchingFileDescriptor();
[email protected]fa95fc92008-12-08 18:10:14983 if (pipe_ != -1) {
[email protected]157c61b2009-05-01 21:37:31984 HANDLE_EINTR(close(pipe_));
[email protected]fa95fc92008-12-08 18:10:14985 pipe_ = -1;
986 }
[email protected]df3c1ca12008-12-19 21:37:01987 if (client_pipe_ != -1) {
[email protected]f0ef2da2009-07-08 01:26:34988 Singleton<PipeMap>()->RemoveAndClose(pipe_name_);
[email protected]df3c1ca12008-12-19 21:37:01989 client_pipe_ = -1;
990 }
[email protected]baf556a2009-09-04 21:34:05991#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]fa95fc92008-12-08 18:10:141001
[email protected]5f594c02009-05-01 22:37:591002 if (uses_fifo_) {
1003 // Unlink the FIFO
1004 unlink(pipe_name_.c_str());
1005 }
[email protected]fa95fc92008-12-08 18:10:141006
1007 while (!output_queue_.empty()) {
1008 Message* m = output_queue_.front();
1009 output_queue_.pop();
1010 delete m;
1011 }
[email protected]526776c2009-02-07 00:39:261012
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]157c61b2009-05-01 21:37:311016 HANDLE_EINTR(close(*i));
[email protected]526776c2009-02-07 00:39:261017 }
1018 input_overflow_fds_.clear();
[email protected]fa95fc92008-12-08 18:10:141019}
1020
[email protected]514411fc2008-12-10 22:28:111021//------------------------------------------------------------------------------
1022// Channel's methods simply call through to ChannelImpl.
[email protected]9a3a293b2009-06-04 22:28:161023Channel::Channel(const std::string& channel_id, Mode mode,
[email protected]514411fc2008-12-10 22:28:111024 Listener* listener)
1025 : channel_impl_(new ChannelImpl(channel_id, mode, listener)) {
1026}
1027
1028Channel::~Channel() {
1029 delete channel_impl_;
1030}
1031
1032bool Channel::Connect() {
1033 return channel_impl_->Connect();
1034}
1035
1036void Channel::Close() {
1037 channel_impl_->Close();
1038}
1039
1040void Channel::set_listener(Listener* listener) {
1041 channel_impl_->set_listener(listener);
1042}
1043
1044bool Channel::Send(Message* message) {
1045 return channel_impl_->Send(message);
1046}
1047
[email protected]cc8f1462009-06-12 17:36:551048int Channel::GetClientFileDescriptor() const {
1049 return channel_impl_->GetClientFileDescriptor();
[email protected]df3c1ca12008-12-19 21:37:011050}
1051
[email protected]d4651ff2008-12-02 16:51:581052} // namespace IPC