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

Side by Side 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, 9 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2013 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
5 #include "ipc/ipc_channel_factory.h"
6
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <stddef.h>
10 #include <sys/socket.h>
11 #include <sys/stat.h>
12 #include <sys/types.h>
13 #include <sys/un.h>
14 #include <unistd.h>
15
16 #include "base/file_util.h"
17 #include "base/logging.h"
18 #include "ipc/ipc_channel_posix.h"
19 #include "ipc/unix_domain_socket_util.h"
20
21 namespace IPC {
22
23 ChannelFactory::ChannelFactory(const base::FilePath& path, Delegate* delegate)
24 : path_(path), delegate_(delegate), listen_pipe_(-1) {
25 DCHECK(delegate_);
26 CreatePipe();
27 }
28
29 ChannelFactory::~ChannelFactory() {
30 Close();
31 }
32
33 bool ChannelFactory::CreatePipe() {
34 DCHECK(listen_pipe_ < 0);
35
36 // Create the socket.
37 return CreateServerUnixDomainSocket(path_, &listen_pipe_);
38 }
39
40 bool ChannelFactory::Listen() {
41 if (listen_pipe_ < 0)
42 return false;
43 // Watch the pipe for connections, and turn any connections into
44 // active sockets.
45 MessageLoopForIO::current()->WatchFileDescriptor(
46 listen_pipe_,
47 true,
48 MessageLoopForIO::WATCH_READ,
49 &server_listen_connection_watcher_,
50 this);
51 return true;
52 }
53
54 // Called by libevent when we can read from the pipe without blocking.
55 void ChannelFactory::OnFileCanReadWithoutBlocking(int fd) {
56 DCHECK(fd == listen_pipe_);
57 int new_pipe = -1;
58 if (!ServerAcceptConnection(listen_pipe_, &new_pipe)) {
59 Close();
60 delegate_->OnListenError();
61 return;
62 }
63
64 // Verify that the IPC channel peer is running as the same user.
65 uid_t client_euid;
66 if (!GetPeerEuid(new_pipe, &client_euid)) {
67 close(new_pipe);
68 return;
69 }
70 if (client_euid != geteuid()) {
71 DLOG(ERROR) << "Client euid is not authorised";
72 close(new_pipe);
73 return;
74 }
75
76 ChannelHandle handle("", base::FileDescriptor(new_pipe, true));
77 delegate_->OnClientConnected(handle);
78 }
79
80 void ChannelFactory::Close() {
81 if (listen_pipe_ < 0)
82 return;
83 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().
84 if (HANDLE_EINTR(close(listen_pipe_)) < 0)
85 PLOG(ERROR) << "close";
86 listen_pipe_ = -1;
87 // Unregister libevent for the listening socket and close it.
88 server_listen_connection_watcher_.StopWatchingFileDescriptor();
89 }
90
91 } // namespace IPC
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698