Chromium Code Reviews| Index: ipc/ipc_channel_factory.cc |
| diff --git a/ipc/ipc_channel_factory.cc b/ipc/ipc_channel_factory.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4041e874d48dc4128f55364e58c3dc426c447ad0 |
| --- /dev/null |
| +++ b/ipc/ipc_channel_factory.cc |
| @@ -0,0 +1,92 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ipc/ipc_channel_factory.h" |
| + |
| +#include <errno.h> |
|
Mark Mentovai
2013/03/05 20:08:03
I’m surprised to see this many #includes for a fil
jeremya
2013/03/06 05:03:18
Yeah, I think I must have just copied this in alon
|
| +#include <fcntl.h> |
| +#include <stddef.h> |
| +#include <sys/socket.h> |
| +#include <sys/stat.h> |
| +#include <sys/types.h> |
| +#include <sys/un.h> |
| +#include <unistd.h> |
| + |
| +#include "base/file_util.h" |
| +#include "base/logging.h" |
| +#include "ipc/ipc_channel_posix.h" |
| +#include "ipc/unix_domain_socket_util.h" |
| + |
| +namespace IPC { |
| + |
| +ChannelFactory::ChannelFactory(const base::FilePath& path, Delegate* delegate) |
| + : path_(path), delegate_(delegate), listen_pipe_(-1) { |
| + DCHECK(delegate_); |
| + CreatePipe(); |
| +} |
| + |
| +ChannelFactory::~ChannelFactory() { |
| + Close(); |
| +} |
| + |
| +bool ChannelFactory::CreatePipe() { |
| + DCHECK(listen_pipe_ < 0); |
| + |
| + // Create the socket. |
| + return CreateServerUnixDomainSocket(path_, &listen_pipe_); |
| +} |
| + |
| +bool ChannelFactory::Listen() { |
| + if (listen_pipe_ < 0) |
| + return false; |
| + // Watch the pipe for connections, and turn any connections into |
|
Mark Mentovai
2013/03/05 20:08:03
I like to see a blank line before comments like th
jeremya
2013/03/06 05:03:18
Done.
|
| + // active sockets. |
| + MessageLoopForIO::current()->WatchFileDescriptor( |
| + listen_pipe_, |
| + true, |
| + MessageLoopForIO::WATCH_READ, |
| + &server_listen_connection_watcher_, |
| + this); |
| + return true; |
| +} |
| + |
| +// Called by libevent when we can read from the pipe without blocking. |
| +void ChannelFactory::OnFileCanReadWithoutBlocking(int fd) { |
| + DCHECK(fd == listen_pipe_); |
| + int new_pipe = -1; |
| + if (!ServerAcceptConnection(listen_pipe_, &new_pipe)) { |
| + Close(); |
|
Mark Mentovai
2013/03/05 20:08:03
I’m not sure that Close() is appropriate here. Wha
jeremya
2013/03/06 05:03:18
Yeah, you're right. I've changed the logic so that
|
| + delegate_->OnListenError(); |
| + return; |
| + } |
| + |
| + // Verify that the IPC channel peer is running as the same user. |
| + uid_t client_euid; |
| + if (!GetPeerEuid(new_pipe, &client_euid)) { |
| + close(new_pipe); |
|
Mark Mentovai
2013/03/05 20:08:03
Close does HANDLE_EINTR around its close. This clo
jeremya
2013/03/06 05:03:18
file_util::ScopedFD to the rescue.
|
| + return; |
| + } |
| + if (client_euid != geteuid()) { |
| + DLOG(ERROR) << "Client euid is not authorised"; |
| + close(new_pipe); |
| + return; |
| + } |
| + |
| + ChannelHandle handle("", base::FileDescriptor(new_pipe, true)); |
| + delegate_->OnClientConnected(handle); |
| +} |
| + |
| +void ChannelFactory::Close() { |
| + if (listen_pipe_ < 0) |
| + return; |
| + if (HANDLE_EINTR(close(listen_pipe_)) < 0) |
| + PLOG(ERROR) << "close"; |
| + if (unlink(path_.value().c_str()) < 0) |
| + PLOG(ERROR) << "unlink"; |
| + listen_pipe_ = -1; |
| + // Unregister libevent for the listening socket and close it. |
| + server_listen_connection_watcher_.StopWatchingFileDescriptor(); |
| +} |
| + |
| +} // namespace IPC |