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> | |
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 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 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.
| |
38 return false; | |
39 return true; | |
40 } | |
41 | |
42 bool ChannelFactory::Listen() { | |
43 if (listen_pipe_ < 0) | |
44 return false; | |
45 // Watch the pipe for connections, and turn any connections into | |
46 // active sockets. | |
47 MessageLoopForIO::current()->WatchFileDescriptor( | |
48 listen_pipe_, | |
49 true, | |
50 MessageLoopForIO::WATCH_READ, | |
51 &server_listen_connection_watcher_, | |
52 this); | |
53 return true; | |
54 } | |
55 | |
56 // Called by libevent when we can read from the pipe without blocking. | |
57 void ChannelFactory::OnFileCanReadWithoutBlocking(int fd) { | |
58 DCHECK(fd == listen_pipe_); | |
59 int new_pipe = -1; | |
60 if (!ServerAcceptConnection(listen_pipe_, &new_pipe)) { | |
61 Close(); | |
62 delegate_->OnListenError(); | |
63 return; | |
64 } | |
65 | |
66 // Verify that the IPC channel peer is running as the same user. | |
67 uid_t client_euid; | |
68 if (!GetPeerEuid(new_pipe, &client_euid)) { | |
69 close(new_pipe); | |
70 return; | |
71 } | |
72 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.
| |
73 DLOG(ERROR) << "Client euid is not authorised"; | |
74 close(new_pipe); | |
75 return; | |
76 } | |
77 | |
78 ChannelHandle handle("", base::FileDescriptor(new_pipe, true)); | |
79 delegate_->OnClientConnected(handle); | |
80 } | |
81 | |
82 void ChannelFactory::Close() { | |
83 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.
| |
84 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
| |
85 if (HANDLE_EINTR(close(listen_pipe_)) < 0) | |
86 PLOG(ERROR) << "close"; | |
87 listen_pipe_ = -1; | |
88 // Unregister libevent for the listening socket and close it. | |
89 server_listen_connection_watcher_.StopWatchingFileDescriptor(); | |
90 } | |
91 } | |
92 | |
93 } // namespace IPC | |
OLD | NEW |