Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(355)

Unified Diff: ipc/ipc_channel_factory.cc

Issue 12386010: Implement IPC::ChannelFactory, a class that accept()s on a UNIX socket. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix GetPeerEuid usage on client. Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..8d008356a4c8d8322bede8cdab839225a24e9184
--- /dev/null
+++ b/ipc/ipc_channel_factory.cc
@@ -0,0 +1,91 @@
+// 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) {
+ 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
+ // 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()) {
+ 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;
+ unlink(path_.value().c_str());
palmer 2013/03/05 00:12:31 I'd still check the return value and PLOG if it fa
jeremya 2013/03/05 03:27:19 Done, plus I moved the unlink() after the close().
+ 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

Powered by Google App Engine
This is Rietveld 408576698