OLD | NEW |
---|---|
(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> | |
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
| |
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 | |
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.
| |
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(); | |
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
| |
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); | |
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.
| |
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 if (HANDLE_EINTR(close(listen_pipe_)) < 0) | |
84 PLOG(ERROR) << "close"; | |
85 if (unlink(path_.value().c_str()) < 0) | |
86 PLOG(ERROR) << "unlink"; | |
87 listen_pipe_ = -1; | |
88 // Unregister libevent for the listening socket and close it. | |
89 server_listen_connection_watcher_.StopWatchingFileDescriptor(); | |
90 } | |
91 | |
92 } // namespace IPC | |
OLD | NEW |