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

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: Use base::FilePath instead of std::string in ChannelFactory's constructor. 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), must_unlink_(false) {
25 DCHECK(delegate_);
26 if (!CreatePipe()) {
27 // The pipe may have been closed already.
28 LOG(WARNING) << "Unable to create pipe named \"" << path.value() << "\"";
Mark Mentovai 2013/02/28 04:57:56 Almost every failure case in CreatePipe has alread
jeremya 2013/02/28 05:58:18 Not quite true, I had to add a few extra logs, but
29 }
30 }
31
32 ChannelFactory::~ChannelFactory() {
33 Close();
34 }
35
36 bool ChannelFactory::CreatePipe() {
37 DCHECK(listen_pipe_ == -1);
38
39 int local_pipe = -1;
Mark Mentovai 2013/02/28 04:57:56 Why do you need local_pipe when you can just use &
jeremya 2013/02/28 05:58:18 Good question. Fixed.
40 // Create the socket.
41 if (!CreateServerUnixDomainSocket(path_, &local_pipe))
42 return false;
43 listen_pipe_ = local_pipe;
44 must_unlink_ = true;
Mark Mentovai 2013/02/28 04:57:56 Why do yo need a separate variable for this? Isn’t
jeremya 2013/02/28 05:58:18 Yep, but now that I look at it there's also a case
45 return true;
46 }
47
48 bool ChannelFactory::Listen() {
49 if (listen_pipe_ == -1) {
50 DLOG(INFO) << "Factory creation failed: " << path_.value();
Mark Mentovai 2013/02/28 04:57:56 You’ve got lots of LOG(WARNING)s and LOG(ERROR)s,
jeremya 2013/02/28 05:58:18 ... philosophy? :) A lot of this code was copied i
51 return false;
52 }
53 // Watch the pipe for connections, and turn any connections into
54 // active sockets.
55 MessageLoopForIO::current()->WatchFileDescriptor(
56 listen_pipe_,
57 true,
58 MessageLoopForIO::WATCH_READ,
59 &server_listen_connection_watcher_,
60 this);
61 return true;
62 }
63
64 // Called by libevent when we can read from the pipe without blocking.
65 void ChannelFactory::OnFileCanReadWithoutBlocking(int fd) {
66 DCHECK(fd == listen_pipe_);
67 int new_pipe = 0;
Mark Mentovai 2013/02/28 04:57:56 You probably wanted to initialize this to -1.
jeremya 2013/02/28 05:58:18 Done.
68 if (!ServerAcceptConnection(listen_pipe_, &new_pipe)) {
69 Close();
70 delegate_->OnListenError();
71 return;
72 }
73
74 // Verify that the IPC channel peer is running as the same user.
75 uid_t client_euid;
76 if (!GetPeerEuid(new_pipe, &client_euid)) {
77 DLOG(ERROR) << "Unable to query client euid";
Mark Mentovai 2013/02/28 04:57:56 All of the failure cases have already logged somet
jeremya 2013/02/28 05:58:18 Removed.
78 // TODO close new pipe
79 return;
80 }
81 if (client_euid != geteuid()) {
82 DLOG(WARNING) << "Client euid is not authorised";
83 // TODO close new pipe
Mark Mentovai 2013/02/28 04:57:56 Yup, do these TODOs.
jeremya 2013/02/28 05:58:18 Done.
84 return;
85 }
86
87 ChannelHandle handle("", base::FileDescriptor(new_pipe, true));
88 delegate_->OnClientConnected(handle);
89 }
90
91 void ChannelFactory::Close() {
92 if (must_unlink_) {
93 unlink(path_.value().c_str());
94 must_unlink_ = false;
95 }
96 if (listen_pipe_ != -1) {
97 if (HANDLE_EINTR(close(listen_pipe_)) < 0)
98 DPLOG(ERROR) << "close " << listen_pipe_;
Mark Mentovai 2013/02/28 04:57:56 Logging the FD number is probably never useful.
jeremya 2013/02/28 05:58:18 Done.
99 listen_pipe_ = -1;
100 // Unregister libevent for the listening socket and close it.
101 server_listen_connection_watcher_.StopWatchingFileDescriptor();
102 }
103 }
104
105 }
Mark Mentovai 2013/02/28 04:57:56 } // namespace IPC
jeremya 2013/02/28 05:58:18 Done.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698