Chromium Code Reviews| Index: ipc/ipc_channel_posix.cc |
| diff --git a/ipc/ipc_channel_posix.cc b/ipc/ipc_channel_posix.cc |
| index 7c03f44f13fe5228cb6238bba09374920f07421d..eb608e0d454fdfed36873be590ea78f23b840b12 100644 |
| --- a/ipc/ipc_channel_posix.cc |
| +++ b/ipc/ipc_channel_posix.cc |
| @@ -30,18 +30,25 @@ |
| #include "ipc/ipc_logging.h" |
| #include "ipc/ipc_message_utils.h" |
| +#if defined (OS_MACOSX) |
| +#include <poll.h> |
| +#endif // defined (OS_MACOSX) |
|
jeremy
2010/12/12 14:59:29
// OS_MACOSX
dmac
2010/12/13 18:32:29
Done.
|
| + |
| namespace IPC { |
| // IPC channels on Windows use named pipes (CreateNamedPipe()) with |
| -// channel ids as the pipe names. Channels on POSIX use anonymous |
| -// Unix domain sockets created via socketpair() as pipes. These don't |
| -// quite line up. |
| +// channel ids as the pipe names. Channels on POSIX use sockets as |
| +// pipes These don't quite line up. |
| // |
| -// When creating a child subprocess, the parent side of the fork |
| -// arranges it such that the initial control channel ends up on the |
| +// When creating a child subprocess we use a socket pair and the parent side of |
| +// the fork arranges it such that the initial control channel ends up on the |
| // magic file descriptor kPrimaryIPCChannel in the child. Future |
| // connections (file descriptors) can then be passed via that |
| // connection via sendmsg(). |
| +// |
| +// A POSIX IPC channel can also be set up as a server for a bound UNIX domain |
| +// socket, and will handle multiple connect and disconnect sequences. Currently |
| +// it is limited to one connection at a time. |
| //------------------------------------------------------------------------------ |
| namespace { |
| @@ -128,22 +135,6 @@ class PipeMap { |
| friend struct DefaultSingletonTraits<PipeMap>; |
| }; |
| -// Used to map a channel name to the equivalent FD # in the current process. |
| -// Returns -1 if the channel is unknown. |
| -int ChannelNameToFD(const std::string& channel_id) { |
| - // See the large block comment above PipeMap for the reasoning here. |
| - const int fd = PipeMap::GetInstance()->Lookup(channel_id); |
| - |
| - if (fd != -1) { |
| - int dup_fd = dup(fd); |
| - if (dup_fd < 0) |
| - PLOG(FATAL) << "dup(" << fd << ") " << channel_id; |
| - return dup_fd; |
| - } |
| - |
| - return fd; |
| -} |
| - |
| //------------------------------------------------------------------------------ |
| // The standard size on linux is 108, mac is 104. To maintain consistency |
| // across platforms we standardize on the smaller value. |
| @@ -151,8 +142,10 @@ const size_t kMaxPipeNameLength = 104; |
| COMPILE_ASSERT(sizeof(((sockaddr_un*)0)->sun_path) >= kMaxPipeNameLength, |
| BAD_SUN_PATH_LENGTH); |
| -// Creates a Fifo with the specified name ready to listen on. |
| -bool CreateServerFifo(const std::string& pipe_name, int* server_listen_fd) { |
| +// Creates a unix domain socket bound to the specified name that is listening |
| +// for connections. |
| +bool CreateServerUnixDomainSocket(const std::string& pipe_name, |
| + int* server_listen_fd) { |
| DCHECK(server_listen_fd); |
| DCHECK_GT(pipe_name.length(), 0u); |
| DCHECK_LT(pipe_name.length(), kMaxPipeNameLength); |
| @@ -169,7 +162,7 @@ bool CreateServerFifo(const std::string& pipe_name, int* server_listen_fd) { |
| // Make socket non-blocking |
| if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { |
| - PLOG(ERROR) << "fcntl " << pipe_name; |
| + PLOG(ERROR) << "fcntl(O_NONBLOCK) " << pipe_name; |
| if (HANDLE_EINTR(close(fd)) < 0) |
| PLOG(ERROR) << "close " << pipe_name; |
| return false; |
| @@ -208,15 +201,15 @@ bool CreateServerFifo(const std::string& pipe_name, int* server_listen_fd) { |
| return true; |
| } |
| -// Accept a connection on a fifo. |
| -bool ServerAcceptFifoConnection(int server_listen_fd, int* server_socket) { |
| +// Accept a connection on a socket we are listening to. |
| +bool ServerAcceptConnection(int server_listen_fd, int* server_socket) { |
| DCHECK(server_socket); |
| int accept_fd = HANDLE_EINTR(accept(server_listen_fd, NULL, 0)); |
| if (accept_fd < 0) |
| return false; |
| if (fcntl(accept_fd, F_SETFL, O_NONBLOCK) == -1) { |
| - PLOG(ERROR) << "fcntl " << accept_fd; |
| + PLOG(ERROR) << "fcntl(O_NONBLOCK) " << accept_fd; |
| if (HANDLE_EINTR(close(accept_fd)) < 0) |
| PLOG(ERROR) << "close " << accept_fd; |
| return false; |
| @@ -226,7 +219,8 @@ bool ServerAcceptFifoConnection(int server_listen_fd, int* server_socket) { |
| return true; |
| } |
| -bool ClientConnectToFifo(const std::string &pipe_name, int* client_socket) { |
| +bool CreateClientUnixDomainSocket(const std::string& pipe_name, |
| + int* client_socket) { |
| DCHECK(client_socket); |
| DCHECK_GT(pipe_name.length(), 0u); |
| DCHECK_LT(pipe_name.length(), kMaxPipeNameLength); |
| @@ -244,7 +238,7 @@ bool ClientConnectToFifo(const std::string &pipe_name, int* client_socket) { |
| // Make socket non-blocking |
| if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { |
| - PLOG(ERROR) << "fcntl " << pipe_name; |
| + PLOG(ERROR) << "fcntl(O_NONBLOCK) " << pipe_name; |
| if (HANDLE_EINTR(close(fd)) < 0) |
| PLOG(ERROR) << "close " << pipe_name; |
| return false; |
| @@ -297,10 +291,8 @@ Channel::ChannelImpl::ChannelImpl(const IPC::ChannelHandle& channel_handle, |
| Mode mode, Listener* listener) |
| : mode_(mode), |
| is_blocked_on_write_(false), |
| + waiting_connect_(true), |
| message_send_bytes_written_(0), |
| - uses_fifo_( |
| - CommandLine::ForCurrentProcess()->HasSwitch(switches::kIPCUseFIFO) || |
| - mode == MODE_NAMED_SERVER || mode == MODE_NAMED_CLIENT), |
| server_listen_pipe_(-1), |
| pipe_(-1), |
| client_pipe_(-1), |
| @@ -308,19 +300,16 @@ Channel::ChannelImpl::ChannelImpl(const IPC::ChannelHandle& channel_handle, |
| fd_pipe_(-1), |
| remote_fd_pipe_(-1), |
| #endif |
| + pipe_name_(channel_handle.name), |
| listener_(listener), |
| - waiting_connect_(true), |
| factory_(this) { |
| - if (mode_ == MODE_NAMED_SERVER) |
| - mode_ = MODE_SERVER; |
| - if (mode_ == MODE_NAMED_CLIENT) |
| - mode_ = MODE_CLIENT; |
| - |
| if (!CreatePipe(channel_handle, mode_)) { |
| // The pipe may have been closed already. |
| + const char *modestr = (mode_ == MODE_SERVER |
| + || mode_ == MODE_NAMED_SERVER) ? "server" : "client"; |
| + // The pipe may have been closed already. |
| LOG(WARNING) << "Unable to create pipe named \"" << channel_handle.name |
| - << "\" in " << (mode_ == MODE_SERVER ? "server" : "client") |
| - << " mode"; |
| + << "\" in " << modestr << " mode"; |
| } |
| } |
| @@ -328,22 +317,6 @@ Channel::ChannelImpl::~ChannelImpl() { |
| Close(); |
| } |
| -// static |
| -void AddChannelSocket(const std::string& name, int socket) { |
| - PipeMap::GetInstance()->Insert(name, socket); |
| -} |
| - |
| -// static |
| -void RemoveAndCloseChannelSocket(const std::string& name) { |
| - PipeMap::GetInstance()->RemoveAndClose(name); |
| -} |
| - |
| -// static |
| -bool ChannelSocketExists(const std::string& name) { |
| - return PipeMap::GetInstance()->Lookup(name) != -1; |
| -} |
| - |
| -// static |
| bool SocketPair(int* fd1, int* fd2) { |
| int pipe_fds[2]; |
| if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipe_fds) != 0) { |
| @@ -368,43 +341,67 @@ bool SocketPair(int* fd1, int* fd2) { |
| return true; |
| } |
| -bool Channel::ChannelImpl::CreatePipe(const IPC::ChannelHandle &channel_handle, |
| +bool Channel::ChannelImpl::CreatePipe(const IPC::ChannelHandle& channel_handle, |
| Mode mode) { |
|
Nico
2010/12/10 18:08:03
do you need the |mode| parameter?
dmac
2010/12/13 18:32:29
Done.
|
| DCHECK(server_listen_pipe_ == -1 && pipe_ == -1); |
| - pipe_name_ = channel_handle.name; |
| - pipe_ = channel_handle.socket.fd; |
| - if (uses_fifo_) { |
| - // This only happens in unit tests; see the comment above PipeMap. |
| - // TODO(playmobil): We shouldn't need to create fifos on disk. |
| - // TODO(playmobil): If we do, they should be in the user data directory. |
| - // TODO(playmobil): Cleanup any stale fifos. |
| - if (mode == MODE_SERVER) { |
| - if (!CreateServerFifo(pipe_name_, &server_listen_pipe_)) { |
| + |
| + // Four possible cases: |
| + // 1) It's a channel wrapping a pipe that is given to us. |
| + // 2) It's for a named channel, so we create it. |
| + // 3) It's for a client that we implement ourself. This is used |
| + // in unittesting. |
| + // 4) It's the initial IPC channel: |
| + // 4a) Client side: Pull the pipe out of the GlobalDescriptors set. |
| + // 4b) Server side: create the pipe. |
| + |
| + // Check to see if we want to implement using domain sockets. |
| + bool uses_domain_socket = false; |
|
Nico
2010/12/10 18:08:03
= mode_ == MODE_NAMED_SERVER || mode_ == MODE_NAME
|
| + if (mode_ == MODE_NAMED_SERVER) { |
| + uses_domain_socket = true; |
| + mode_ = MODE_SERVER; |
|
Nico
2010/12/10 18:08:03
|mode_| changing somewhere in the middle of this c
dmac
2010/12/13 18:32:29
Done.
|
| + } else if (mode_ == MODE_NAMED_CLIENT) { |
| + uses_domain_socket = true; |
| + mode_ = MODE_CLIENT; |
| + } |
| + |
| + if (channel_handle.socket.fd != -1) { |
| + // Case 1 |
|
jeremy
2010/12/12 14:59:29
Could you add a bit of descriptive text to these r
dmac
2010/12/13 18:32:29
Done. Mapped them to the comment above to avoid du
|
| + pipe_ = channel_handle.socket.fd; |
| +#if defined(IPC_USES_READWRITE) |
| + // Test the socket passed into us to make sure it is nonblocking. |
| + // We don't want to call read/write on a blocking socket. |
| + int value = fcntl(pipe_, F_GETFL); |
| + if (value == -1) { |
| + PLOG(ERROR) << "fcntl(F_GETFL) " << pipe_name_; |
| + return false; |
| + } |
| + if (!(value & O_NONBLOCK)) { |
| + LOG(ERROR) << "Socket " << pipe_name_ << " must be O_NONBLOCK"; |
| + return false; |
| + } |
| +#endif // defined(IPC_USES_READWRITE) |
|
jeremy
2010/12/12 14:59:29
// IPC_USES_READWRITE
dmac
2010/12/13 18:32:29
Done.
|
| + } else if (uses_domain_socket) { |
| + // Case 2 |
| + must_unlink_ = true; |
| + if (mode_ == MODE_SERVER) { |
| + if (!CreateServerUnixDomainSocket(pipe_name_, &pipe_)) { |
| return false; |
| } |
| - } else { |
| - if (!ClientConnectToFifo(pipe_name_, &pipe_)) { |
| + } else if (mode_ == MODE_CLIENT) { |
| + if (!CreateClientUnixDomainSocket(pipe_name_, &pipe_)) { |
| return false; |
| } |
| - waiting_connect_ = false; |
| } |
| } else { |
| - // This is the normal (non-unit-test) case, where we're using sockets. |
| - // Three possible cases: |
| - // 1) It's for a channel we already have a pipe for; reuse it. |
| - // 2) It's the initial IPC channel: |
| - // 2a) Server side: create the pipe. |
| - // 2b) Client side: Pull the pipe out of the GlobalDescriptors set. |
| - if (pipe_ < 0) { |
| - pipe_ = ChannelNameToFD(pipe_name_); |
| - } |
| - if (pipe_ < 0) { |
| - // Initial IPC channel. |
| - if (mode == MODE_SERVER) { |
| - if (!SocketPair(&pipe_, &client_pipe_)) |
| - return false; |
| - AddChannelSocket(pipe_name_, client_pipe_); |
| + pipe_ = Singleton<PipeMap>()->Lookup(pipe_name_); |
| + if (mode_ == MODE_CLIENT) { |
| + if (pipe_ != -1) { |
| + // Case 3 |
| + // We only allow one connection. |
| + pipe_ = dup(pipe_); |
|
jeremy
2010/12/12 14:59:29
HANDLE_EINTR(dup(pipe_));
dmac
2010/12/13 18:32:29
Done.
|
| + Singleton<PipeMap>()->RemoveAndClose(pipe_name_); |
| } else { |
| + // Case 4a |
| // Guard against inappropriate reuse of the initial IPC channel. If |
| // an IPC channel closes and someone attempts to reuse it by name, the |
| // initial channel must not be recycled here. http://crbug.com/26754. |
| @@ -418,42 +415,77 @@ bool Channel::ChannelImpl::CreatePipe(const IPC::ChannelHandle &channel_handle, |
| pipe_ = base::GlobalDescriptors::GetInstance()->Get(kPrimaryIPCChannel); |
| } |
| + } else if (mode_ == MODE_SERVER) { |
| + // Case 4b |
| + if (pipe_ != -1) { |
| + LOG(ERROR) << "Server already exists for " << pipe_name_; |
| + return false; |
| + } |
| + if (!SocketPair(&pipe_, &client_pipe_)) |
| + return false; |
| + Singleton<PipeMap>()->Insert(pipe_name_, client_pipe_); |
| } else { |
| - waiting_connect_ = mode == MODE_SERVER; |
| + LOG(FATAL) << "Unknown mode " << mode_; |
| + return false; |
| } |
| } |
| - // Create the Hello message to be sent when Connect is called |
| - scoped_ptr<Message> msg(new Message(MSG_ROUTING_NONE, |
| - HELLO_MESSAGE_TYPE, |
| - IPC::Message::PRIORITY_NORMAL)); |
| - #if defined(IPC_USES_READWRITE) |
| - if (!uses_fifo_) { |
| - // Create a dedicated socketpair() for exchanging file descriptors. |
| - // See comments for IPC_USES_READWRITE for details. |
| - if (mode == MODE_SERVER) { |
| - fd_pipe_ = -1; |
| - } else if (remote_fd_pipe_ == -1) { |
| - if (!SocketPair(&fd_pipe_, &remote_fd_pipe_)) { |
| - return false; |
| - } |
| + if (mode_ == MODE_SERVER) { |
| + int listening = 0; |
| + // We support sockets that we are connected to and listening to. |
| + // Currently we only support one connection per socket that we are |
| + // listening to. |
| + // Check to see if the socket is one that we are connected to |
| + // or one that we are listening to for connections. |
| +#if !defined(OS_MACOSX) |
| + socklen_t len = sizeof(listening); |
| + if (getsockopt(pipe_, SOL_SOCKET, SO_ACCEPTCONN, |
| + &listening, &len) < 0) { |
| + PLOG(ERROR) << "Unable to get socket options for " << pipe_name_; |
| + return false; |
| + } |
| +#else |
| + // Darwin <= Mac OS X 10.6.4 does not support SO_ACCEPTCONN. |
| + // The poor man's version is to call poll and check to see if the socket |
| + // in question supports writing. If it doesn't support writing, it is |
| + // assumed that it is listening for connections. |
| + struct pollfd pfd = { pipe_, POLLOUT, 0}; |
| + int count = poll(&pfd, 1, 0); |
| + if (count < 0) { |
| + PLOG(ERROR) << "Unable to poll " << pipe_name_; |
| + return false; |
| + } |
| + listening = count > 0 ? 0 : 1; |
|
Nico
2010/12/10 18:48:07
drop | ? 0 : 1|
dmac
2010/12/13 18:32:29
Done.
|
| +#endif |
|
jeremy
2010/12/12 14:59:29
// OS_MACOSX
dmac
2010/12/13 18:32:29
Done.
|
| + if (listening) { |
| + server_listen_pipe_ = pipe_; |
| + pipe_ = -1; |
| } |
| } |
| - #endif |
| - if (!msg->WriteInt(base::GetCurrentProcId())) { |
| - Close(); |
| - return false; |
| + |
| +#if defined(IPC_USES_READWRITE) |
| + // Create a dedicated socketpair() for exchanging file descriptors. |
| + // See comments for IPC_USES_READWRITE for details. |
| + if (mode_ == MODE_CLIENT) { |
| + if (!SocketPair(&fd_pipe_, &remote_fd_pipe_)) { |
| + return false; |
| + } |
| } |
| +#endif |
|
jeremy
2010/12/12 14:59:29
// IPC_USES_READWRITE
dmac
2010/12/13 18:32:29
Done.
|
| - output_queue_.push(msg.release()); |
| return true; |
| } |
| bool Channel::ChannelImpl::Connect() { |
| - if (mode_ == MODE_SERVER && uses_fifo_) { |
| - if (server_listen_pipe_ == -1) { |
| - return false; |
| - } |
| + if (server_listen_pipe_ == -1 && pipe_ == -1) { |
| + NOTREACHED() << "Must call create on a channel before calling connect"; |
| + return false; |
| + } |
| + |
| + bool did_connect = true; |
| + if (server_listen_pipe_ != -1) { |
| + // Watch the pipe for connections, and turn any connections into |
| + // active sockets. |
| MessageLoopForIO::current()->WatchFileDescriptor( |
| server_listen_pipe_, |
| true, |
| @@ -461,21 +493,9 @@ bool Channel::ChannelImpl::Connect() { |
| &server_listen_connection_watcher_, |
| this); |
| } else { |
| - if (pipe_ == -1) { |
| - return false; |
| - } |
| - MessageLoopForIO::current()->WatchFileDescriptor( |
| - pipe_, |
| - true, |
| - MessageLoopForIO::WATCH_READ, |
| - &read_watcher_, |
| - this); |
| - waiting_connect_ = mode_ == MODE_SERVER; |
| + did_connect = AcceptConnection(); |
| } |
| - |
| - if (!waiting_connect_) |
| - return ProcessOutgoingMessages(); |
| - return true; |
| + return did_connect; |
| } |
| bool Channel::ChannelImpl::ProcessIncomingMessages() { |
| @@ -572,7 +592,7 @@ bool Channel::ChannelImpl::ProcessIncomingMessages() { |
| << " fd:" << pipe_; |
| for (unsigned i = 0; i < num_wire_fds; ++i) |
| if (HANDLE_EINTR(close(wire_fds[i])) < 0) |
| - PLOG(ERROR) << "close" << i; |
| + PLOG(ERROR) << "close " << i; |
| return false; |
| } |
| break; |
| @@ -631,47 +651,45 @@ bool Channel::ChannelImpl::ProcessIncomingMessages() { |
| // the message has been completely received, but we didn't get |
| // enough file descriptors. |
| #if defined(IPC_USES_READWRITE) |
| - if (!uses_fifo_) { |
| - char dummy; |
| - struct iovec fd_pipe_iov = { &dummy, 1 }; |
| - msg.msg_iov = &fd_pipe_iov; |
| - msg.msg_controllen = sizeof(input_cmsg_buf_); |
| - ssize_t n = HANDLE_EINTR(recvmsg(fd_pipe_, &msg, MSG_DONTWAIT)); |
| - if (n == 1 && msg.msg_controllen > 0) { |
| - for (struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); cmsg; |
| - cmsg = CMSG_NXTHDR(&msg, cmsg)) { |
| - if (cmsg->cmsg_level == SOL_SOCKET && |
| - cmsg->cmsg_type == SCM_RIGHTS) { |
| - const unsigned payload_len = cmsg->cmsg_len - CMSG_LEN(0); |
| - DCHECK(payload_len % sizeof(int) == 0); |
| - wire_fds = reinterpret_cast<int*>(CMSG_DATA(cmsg)); |
| - num_wire_fds = payload_len / 4; |
| - |
| - if (msg.msg_flags & MSG_CTRUNC) { |
| - LOG(ERROR) << "SCM_RIGHTS message was truncated" |
| - << " cmsg_len:" << cmsg->cmsg_len |
| - << " fd:" << pipe_; |
| - for (unsigned i = 0; i < num_wire_fds; ++i) |
| - if (HANDLE_EINTR(close(wire_fds[i])) < 0) |
| - PLOG(ERROR) << "close" << i; |
| - return false; |
| - } |
| - break; |
| + char dummy; |
| + struct iovec fd_pipe_iov = { &dummy, 1 }; |
| + msg.msg_iov = &fd_pipe_iov; |
| + msg.msg_controllen = sizeof(input_cmsg_buf_); |
| + ssize_t n = HANDLE_EINTR(recvmsg(fd_pipe_, &msg, MSG_DONTWAIT)); |
| + if (n == 1 && msg.msg_controllen > 0) { |
| + for (struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); cmsg; |
| + cmsg = CMSG_NXTHDR(&msg, cmsg)) { |
| + if (cmsg->cmsg_level == SOL_SOCKET && |
| + cmsg->cmsg_type == SCM_RIGHTS) { |
| + const unsigned payload_len = cmsg->cmsg_len - CMSG_LEN(0); |
| + DCHECK(payload_len % sizeof(int) == 0); |
| + wire_fds = reinterpret_cast<int*>(CMSG_DATA(cmsg)); |
| + num_wire_fds = payload_len / 4; |
| + |
| + if (msg.msg_flags & MSG_CTRUNC) { |
| + LOG(ERROR) << "SCM_RIGHTS message was truncated" |
| + << " cmsg_len:" << cmsg->cmsg_len |
| + << " fd:" << pipe_; |
| + for (unsigned i = 0; i < num_wire_fds; ++i) |
| + if (HANDLE_EINTR(close(wire_fds[i])) < 0) |
| + PLOG(ERROR) << "close " << i; |
| + return false; |
| } |
| + break; |
| } |
| - if (input_overflow_fds_.empty()) { |
| - fds = wire_fds; |
| - num_fds = num_wire_fds; |
| - } else { |
| - if (num_wire_fds > 0) { |
| - const size_t prev_size = input_overflow_fds_.size(); |
| - input_overflow_fds_.resize(prev_size + num_wire_fds); |
| - memcpy(&input_overflow_fds_[prev_size], wire_fds, |
| - num_wire_fds * sizeof(int)); |
| - } |
| - fds = &input_overflow_fds_[0]; |
| - num_fds = input_overflow_fds_.size(); |
| + } |
| + if (input_overflow_fds_.empty()) { |
| + fds = wire_fds; |
| + num_fds = num_wire_fds; |
| + } else { |
| + if (num_wire_fds > 0) { |
| + const size_t prev_size = input_overflow_fds_.size(); |
| + input_overflow_fds_.resize(prev_size + num_wire_fds); |
| + memcpy(&input_overflow_fds_[prev_size], wire_fds, |
| + num_wire_fds * sizeof(int)); |
| } |
| + fds = &input_overflow_fds_[0]; |
| + num_fds = input_overflow_fds_.size(); |
| } |
| } |
| if (header_fds > num_fds - fds_i) |
| @@ -700,7 +718,7 @@ bool Channel::ChannelImpl::ProcessIncomingMessages() { |
| // close the existing file descriptors so that we don't leak them |
| for (unsigned i = fds_i; i < num_fds; ++i) |
| if (HANDLE_EINTR(close(fds[i])) < 0) |
| - PLOG(ERROR) << "close" << i; |
| + PLOG(ERROR) << "close " << i; |
| input_overflow_fds_.clear(); |
| // abort the connection |
| return false; |
| @@ -712,8 +730,7 @@ bool Channel::ChannelImpl::ProcessIncomingMessages() { |
| } |
| DVLOG(2) << "received message on channel @" << this |
| << " with type " << m.type() << " on fd " << pipe_; |
| - if (m.routing_id() == MSG_ROUTING_NONE && |
| - m.type() == HELLO_MESSAGE_TYPE) { |
| + if (IsHelloMessage(&m)) { |
| // The Hello message contains only the process id. |
| void *iter = NULL; |
| int pid; |
| @@ -721,9 +738,9 @@ bool Channel::ChannelImpl::ProcessIncomingMessages() { |
| NOTREACHED(); |
| } |
| #if defined(IPC_USES_READWRITE) |
| - if (mode_ == MODE_SERVER && !uses_fifo_) { |
| - // On non-Mac, the Hello message from the client to the server |
| - // also contains the fd_pipe_, which will be used for all |
| + if (mode_ == MODE_SERVER) { |
| + // With IPC_USES_READWRITE, the Hello message from the client to the |
| + // server also contains the fd_pipe_, which will be used for all |
| // subsequent file descriptor passing. |
| DCHECK_EQ(m.file_descriptor_set()->size(), 1U); |
| base::FileDescriptor descriptor; |
| @@ -761,52 +778,22 @@ bool Channel::ChannelImpl::ProcessIncomingMessages() { |
| bytes_read = 0; // Get more data. |
| } |
| - |
| - return true; |
|
jeremy
2010/12/12 14:59:29
You sure it's ok to remove this?
dmac
2010/12/13 18:32:29
Yep, it compiles cleanly without it.
|
| } |
| bool Channel::ChannelImpl::ProcessOutgoingMessages() { |
| DCHECK(!waiting_connect_); // Why are we trying to send messages if there's |
| // no connection? |
| - is_blocked_on_write_ = false; |
| - |
| - if (output_queue_.empty()) { |
| + if (output_queue_.empty()) |
| return true; |
| - } |
| - if (pipe_ == -1) { |
| + if (pipe_ == -1) |
| return false; |
| - } |
| // Write out all the messages we can till the write blocks or there are no |
| // more outgoing messages. |
| while (!output_queue_.empty()) { |
| Message* msg = output_queue_.front(); |
| -#if defined(IPC_USES_READWRITE) |
| - scoped_ptr<Message> hello; |
| - if (remote_fd_pipe_ != -1 && |
| - msg->routing_id() == MSG_ROUTING_NONE && |
| - msg->type() == HELLO_MESSAGE_TYPE) { |
| - hello.reset(new Message(MSG_ROUTING_NONE, |
| - HELLO_MESSAGE_TYPE, |
| - IPC::Message::PRIORITY_NORMAL)); |
| - void* iter = NULL; |
| - int pid; |
| - if (!msg->ReadInt(&iter, &pid) || |
| - !hello->WriteInt(pid)) { |
| - NOTREACHED(); |
| - } |
| - DCHECK_EQ(hello->size(), msg->size()); |
| - if (!hello->WriteFileDescriptor(base::FileDescriptor(remote_fd_pipe_, |
| - false))) { |
| - NOTREACHED(); |
| - } |
| - msg = hello.get(); |
| - DCHECK_EQ(msg->file_descriptor_set()->size(), 1U); |
| - } |
| -#endif |
| - |
| size_t amt_to_write = msg->size() - message_send_bytes_written_; |
| DCHECK(amt_to_write != 0); |
| const char* out_bytes = reinterpret_cast<const char*>(msg->data()) + |
| @@ -854,9 +841,7 @@ bool Channel::ChannelImpl::ProcessOutgoingMessages() { |
| msg->header()->num_fds = static_cast<uint16>(num_fds); |
| #if defined(IPC_USES_READWRITE) |
| - if (!uses_fifo_ && |
| - (msg->routing_id() != MSG_ROUTING_NONE || |
| - msg->type() != HELLO_MESSAGE_TYPE)) { |
| + if (!IsHelloMessage(msg)) { |
| // Only the Hello message sends the file descriptor with the message. |
| // Subsequently, we can send file descriptors on the dedicated |
| // fd_pipe_ which makes Seccomp sandbox operation more efficient. |
| @@ -876,12 +861,10 @@ bool Channel::ChannelImpl::ProcessOutgoingMessages() { |
| if (bytes_written == 1) { |
| fd_written = pipe_; |
| #if defined(IPC_USES_READWRITE) |
| - if (mode_ != MODE_SERVER && !uses_fifo_ && |
| - msg->routing_id() == MSG_ROUTING_NONE && |
| - msg->type() == HELLO_MESSAGE_TYPE) { |
| + if (mode_ != MODE_SERVER && IsHelloMessage(msg)) { |
| DCHECK_EQ(msg->file_descriptor_set()->size(), 1U); |
| } |
| - if (!uses_fifo_ && !msgh.msg_controllen) { |
| + if (!msgh.msg_controllen) { |
| bytes_written = HANDLE_EINTR(write(pipe_, out_bytes, amt_to_write)); |
| } else |
| #endif |
| @@ -949,11 +932,8 @@ bool Channel::ChannelImpl::Send(Message* message) { |
| #endif |
| output_queue_.push(message); |
| - if (!waiting_connect_) { |
| - if (!is_blocked_on_write_) { |
| - if (!ProcessOutgoingMessages()) |
| - return false; |
| - } |
| + if (!is_blocked_on_write_ && !waiting_connect_) { |
| + return ProcessOutgoingMessages(); |
| } |
| return true; |
| @@ -963,43 +943,89 @@ int Channel::ChannelImpl::GetClientFileDescriptor() const { |
| return client_pipe_; |
| } |
| -// Called by libevent when we can read from th pipe without blocking. |
| -void Channel::ChannelImpl::OnFileCanReadWithoutBlocking(int fd) { |
| - bool send_server_hello_msg = false; |
| - if (waiting_connect_ && mode_ == MODE_SERVER) { |
| - if (uses_fifo_) { |
| - if (!ServerAcceptFifoConnection(server_listen_pipe_, &pipe_)) { |
| - Close(); |
| - } |
| +bool Channel::ChannelImpl::AcceptsConnections() const { |
| + return server_listen_pipe_ != -1; |
| +} |
| - // No need to watch the listening socket any longer since only one client |
| - // can connect. So unregister with libevent. |
| - server_listen_connection_watcher_.StopWatchingFileDescriptor(); |
| +bool Channel::ChannelImpl::HasAcceptedConnection() const { |
| + return AcceptsConnections() && pipe_ != -1; |
| +} |
| - // Start watching our end of the socket. |
| - MessageLoopForIO::current()->WatchFileDescriptor( |
| - pipe_, |
| - true, |
| - MessageLoopForIO::WATCH_READ, |
| - &read_watcher_, |
| - this); |
| +void Channel::ChannelImpl::ResetToAcceptingConnectionState() { |
| + if (pipe_ == -1) |
| + return; |
| - waiting_connect_ = false; |
| - } else { |
| - // In the case of a socketpair() the server starts listening on its end |
| - // of the pipe in Connect(). |
| - waiting_connect_ = false; |
| - } |
| - send_server_hello_msg = true; |
| + // Unregister libevent for the unix domain socket and close it. |
| + read_watcher_.StopWatchingFileDescriptor(); |
| + write_watcher_.StopWatchingFileDescriptor(); |
| + if (HANDLE_EINTR(close(pipe_)) < 0) |
| + PLOG(ERROR) << "close pipe_ " << pipe_name_; |
| + pipe_ = -1; |
| +#if defined(IPC_USES_READWRITE) |
| + if (fd_pipe_ != -1) { |
| + if (HANDLE_EINTR(close(fd_pipe_)) < 0) |
| + PLOG(ERROR) << "close fd_pipe_ " << pipe_name_; |
| + fd_pipe_ = -1; |
| + } |
| + if (remote_fd_pipe_ != -1) { |
| + if (HANDLE_EINTR(close(remote_fd_pipe_)) < 0) |
| + PLOG(ERROR) << "close remote_fd_pipe_ " << pipe_name_; |
| + remote_fd_pipe_ = -1; |
| + } |
| +#endif |
| + |
| + while (!output_queue_.empty()) { |
| + Message* m = output_queue_.front(); |
| + output_queue_.pop(); |
| + delete m; |
| } |
| - if (!waiting_connect_ && fd == pipe_) { |
| - if (!ProcessIncomingMessages()) { |
| + // Close any outstanding, received file descriptors |
|
jeremy
2010/12/12 14:59:29
.
dmac
2010/12/13 18:32:29
Done.
|
| + for (std::vector<int>::iterator |
| + i = input_overflow_fds_.begin(); i != input_overflow_fds_.end(); ++i) { |
| + if (HANDLE_EINTR(close(*i)) < 0) |
| + PLOG(ERROR) << "close"; |
| + } |
| + input_overflow_fds_.clear(); |
| +} |
| + |
| +// Called by libevent when we can read from the pipe without blocking. |
| +void Channel::ChannelImpl::OnFileCanReadWithoutBlocking(int fd) { |
| + bool send_server_hello_msg = false; |
| + if (fd == server_listen_pipe_) { |
| + int new_pipe = 0; |
| + if (!ServerAcceptConnection(server_listen_pipe_, &new_pipe)) { |
| Close(); |
| - listener_->OnChannelError(); |
| - // The OnChannelError() call may delete this, so we need to exit now. |
| + listener_->OnChannelListenError(); |
| + } |
| + |
| + if (pipe_ != -1) { |
| + // We already have a connection. We only handle one at a time. |
| + // close our new descriptor. |
| + if (HANDLE_EINTR(shutdown(new_pipe, SHUT_RDWR)) < 0) |
| + PLOG(ERROR) << "shutdown " << pipe_name_; |
| + if (HANDLE_EINTR(close(new_pipe)) < 0) |
| + PLOG(ERROR) << "close " << pipe_name_; |
| + listener_->OnChannelDenied(); |
| return; |
| } |
| + pipe_ = new_pipe; |
| + |
| + if (!AcceptConnection()) { |
| + NOTREACHED() << "AcceptConnection should not fail on server"; |
| + } |
| + send_server_hello_msg = true; |
| + waiting_connect_ = false; |
| + } else if (fd == pipe_) { |
| + if (waiting_connect_ && mode_ == MODE_SERVER) { |
| + send_server_hello_msg = true; |
| + waiting_connect_ = false; |
| + } |
| + if (!ProcessIncomingMessages()) { |
| + ClosePipeOnError(); |
| + } |
| + } else { |
| + NOTREACHED() << "Unknown pipe " << fd; |
| } |
| // If we're a server and handshaking, then we want to make sure that we |
| @@ -1013,68 +1039,94 @@ void Channel::ChannelImpl::OnFileCanReadWithoutBlocking(int fd) { |
| // Called by libevent when we can write to the pipe without blocking. |
| void Channel::ChannelImpl::OnFileCanWriteWithoutBlocking(int fd) { |
| + DCHECK(fd == pipe_); |
| + is_blocked_on_write_ = false; |
| if (!ProcessOutgoingMessages()) { |
| - Close(); |
| + ClosePipeOnError(); |
| + } |
| +} |
| + |
| +bool Channel::ChannelImpl::AcceptConnection() { |
| + MessageLoopForIO::current()->WatchFileDescriptor(pipe_, |
| + true, |
| + MessageLoopForIO::WATCH_READ, |
| + &read_watcher_, |
| + this); |
| + QueueHelloMessage(); |
| + |
| + if (mode_ == MODE_CLIENT) { |
| + // If we are a client we want to send a hello message out immediately. |
| + // In server mode we will send a hello message when we receive one from a |
| + // client. |
| + waiting_connect_ = false; |
| + return ProcessOutgoingMessages(); |
| + } else { |
| + waiting_connect_ = true; |
| + return true; |
| + } |
| +} |
| + |
| +void Channel::ChannelImpl::ClosePipeOnError() { |
| + if (HasAcceptedConnection()) { |
| + ResetToAcceptingConnectionState(); |
| listener_->OnChannelError(); |
| + } else { |
| + Close(); |
| + if (AcceptsConnections()) { |
| + listener_->OnChannelListenError(); |
| + } else { |
| + listener_->OnChannelError(); |
| + } |
| } |
| } |
| +void Channel::ChannelImpl::QueueHelloMessage() { |
| + // Create the Hello message |
| + scoped_ptr<Message> msg(new Message(MSG_ROUTING_NONE, |
| + HELLO_MESSAGE_TYPE, |
| + IPC::Message::PRIORITY_NORMAL)); |
| + |
| + if (!msg->WriteInt(base::GetCurrentProcId())) { |
| + NOTREACHED() << "Unable to pickle hello message proc id"; |
| + } |
| +#if defined(IPC_USES_READWRITE) |
| + scoped_ptr<Message> hello; |
| + if (remote_fd_pipe_ != -1) { |
| + if (!msg->WriteFileDescriptor(base::FileDescriptor(remote_fd_pipe_, |
| + false))) { |
| + NOTREACHED() << "Unable to pickle hello message file descriptors"; |
| + } |
| + DCHECK_EQ(msg->file_descriptor_set()->size(), 1U); |
| + } |
| +#endif |
| + output_queue_.push(msg.release()); |
| +} |
| + |
| +bool Channel::ChannelImpl::IsHelloMessage(const Message* m) const { |
| + return m->routing_id() == MSG_ROUTING_NONE && m->type() == HELLO_MESSAGE_TYPE; |
| +} |
| + |
| void Channel::ChannelImpl::Close() { |
| // Close can be called multiple time, so we need to make sure we're |
| // idempotent. |
| - // Unregister libevent for the listening socket and close it. |
| - server_listen_connection_watcher_.StopWatchingFileDescriptor(); |
| + ResetToAcceptingConnectionState(); |
| + if (must_unlink_) { |
| + unlink(pipe_name_.c_str()); |
|
Nico
2010/12/10 18:48:07
Do you need to set must_link_ to false here? See c
dmac
2010/12/13 18:32:29
Done.
|
| + } |
| if (server_listen_pipe_ != -1) { |
| if (HANDLE_EINTR(close(server_listen_pipe_)) < 0) |
| PLOG(ERROR) << "close " << server_listen_pipe_; |
| server_listen_pipe_ = -1; |
| + // Unregister libevent for the listening socket and close it. |
| + server_listen_connection_watcher_.StopWatchingFileDescriptor(); |
| } |
| - // Unregister libevent for the FIFO and close it. |
| - read_watcher_.StopWatchingFileDescriptor(); |
| - write_watcher_.StopWatchingFileDescriptor(); |
| - if (pipe_ != -1) { |
| - if (HANDLE_EINTR(close(pipe_)) < 0) |
| - PLOG(ERROR) << "close " << pipe_; |
| - pipe_ = -1; |
| - } |
| if (client_pipe_ != -1) { |
| PipeMap::GetInstance()->RemoveAndClose(pipe_name_); |
| client_pipe_ = -1; |
| } |
| -#if defined(IPC_USES_READWRITE) |
| - if (fd_pipe_ != -1) { |
| - if (HANDLE_EINTR(close(fd_pipe_)) < 0) |
| - PLOG(ERROR) << "close " << fd_pipe_; |
| - fd_pipe_ = -1; |
| - } |
| - if (remote_fd_pipe_ != -1) { |
| - if (HANDLE_EINTR(close(remote_fd_pipe_)) < 0) |
| - PLOG(ERROR) << "close " << remote_fd_pipe_; |
| - remote_fd_pipe_ = -1; |
| - } |
| -#endif |
| - |
| - if (uses_fifo_) { |
| - // Unlink the FIFO |
| - unlink(pipe_name_.c_str()); |
| - } |
| - |
| - while (!output_queue_.empty()) { |
| - Message* m = output_queue_.front(); |
| - output_queue_.pop(); |
| - delete m; |
| - } |
| - |
| - // Close any outstanding, received file descriptors |
| - for (std::vector<int>::iterator |
| - i = input_overflow_fds_.begin(); i != input_overflow_fds_.end(); ++i) { |
| - if (HANDLE_EINTR(close(*i)) < 0) |
| - PLOG(ERROR) << "close " << *i; |
| - } |
| - input_overflow_fds_.clear(); |
| } |
| //------------------------------------------------------------------------------ |
| @@ -1108,4 +1160,16 @@ int Channel::GetClientFileDescriptor() const { |
| return channel_impl_->GetClientFileDescriptor(); |
| } |
| +bool Channel::AcceptsConnections() const { |
| + return channel_impl_->AcceptsConnections(); |
| +} |
| + |
| +bool Channel::HasAcceptedConnection() const { |
| + return channel_impl_->HasAcceptedConnection(); |
| +} |
| + |
| +void Channel::ResetToAcceptingConnectionState() { |
| + channel_impl_->ResetToAcceptingConnectionState(); |
| +} |
| + |
| } // namespace IPC |