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..ab9b604fb5f418cfad59387656298b3bcbb914e8 |
| --- /dev/null |
| +++ b/ipc/ipc_channel_factory.cc |
| @@ -0,0 +1,93 @@ |
| +// 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> |
| +#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), must_unlink_(false) { |
| + DCHECK(delegate_); |
| + CreatePipe(); |
| +} |
| + |
| +ChannelFactory::~ChannelFactory() { |
| + Close(); |
| +} |
| + |
| +bool ChannelFactory::CreatePipe() { |
| + DCHECK(listen_pipe_ < 0); |
| + |
| + // Create the socket. |
| + if (!CreateServerUnixDomainSocket(path_, &listen_pipe_)) |
|
palmer
2013/02/28 23:48:28
Maybe just
return CreateServerUnixDomainSocke
jeremya
2013/03/01 03:49:25
lol, good catch.
|
| + return false; |
| + return true; |
| +} |
| + |
| +bool ChannelFactory::Listen() { |
| + if (listen_pipe_ < 0) |
| + return false; |
| + // Watch the pipe for connections, and turn any connections into |
| + // 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(); |
| + 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); |
| + return; |
| + } |
| + if (client_euid != geteuid()) { |
|
palmer
2013/02/28 23:48:28
Both clients and servers will use this check on ea
jeremya
2013/03/01 03:49:25
Currently only the server checks.
palmer
2013/03/01 19:14:30
Ahh. We'll need for both to check.
jeremya
2013/03/03 21:39:03
Done.
|
| + 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) { |
|
palmer
2013/02/28 23:48:28
I'd sav a level of indentation by doing
if (lis
jeremya
2013/03/01 03:49:25
Done.
jeremya
2013/03/01 03:49:25
Done.
|
| + HANDLE_EINTR(unlink(path_.value().c_str())); |
|
palmer
2013/02/28 23:48:28
Neither the Linux nor the OS X man pages for unlin
jeremya
2013/03/01 03:49:25
Cool, I was confused when I should use it and when
|
| + if (HANDLE_EINTR(close(listen_pipe_)) < 0) |
| + PLOG(ERROR) << "close"; |
| + listen_pipe_ = -1; |
| + // Unregister libevent for the listening socket and close it. |
| + server_listen_connection_watcher_.StopWatchingFileDescriptor(); |
| + } |
| +} |
| + |
| +} // namespace IPC |