OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ipc/ipc_channel_factory.h" | 5 #include "ipc/ipc_channel_factory.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/files/scoped_file.h" | |
9 #include "base/logging.h" | 8 #include "base/logging.h" |
10 #include "ipc/unix_domain_socket_util.h" | 9 #include "ipc/unix_domain_socket_util.h" |
11 | 10 |
12 namespace IPC { | 11 namespace IPC { |
13 | 12 |
14 ChannelFactory::ChannelFactory(const base::FilePath& path, Delegate* delegate) | 13 ChannelFactory::ChannelFactory(const base::FilePath& path, Delegate* delegate) |
15 : path_(path), delegate_(delegate), listen_fd_(-1) { | 14 : path_(path), delegate_(delegate), listen_fd_(-1) { |
16 DCHECK(delegate_); | 15 DCHECK(delegate_); |
17 CreateSocket(); | 16 CreateSocket(); |
18 } | 17 } |
(...skipping 26 matching lines...) Expand all Loading... |
45 | 44 |
46 // Called by libevent when we can read from the fd without blocking. | 45 // Called by libevent when we can read from the fd without blocking. |
47 void ChannelFactory::OnFileCanReadWithoutBlocking(int fd) { | 46 void ChannelFactory::OnFileCanReadWithoutBlocking(int fd) { |
48 DCHECK(fd == listen_fd_); | 47 DCHECK(fd == listen_fd_); |
49 int new_fd = -1; | 48 int new_fd = -1; |
50 if (!ServerAcceptConnection(listen_fd_, &new_fd)) { | 49 if (!ServerAcceptConnection(listen_fd_, &new_fd)) { |
51 Close(); | 50 Close(); |
52 delegate_->OnListenError(); | 51 delegate_->OnListenError(); |
53 return; | 52 return; |
54 } | 53 } |
55 base::ScopedFD scoped_fd(new_fd); | |
56 | 54 |
57 if (!scoped_fd.is_valid()) { | 55 if (new_fd < 0) { |
58 // The accept() failed, but not in such a way that the factory needs to be | 56 // The accept() failed, but not in such a way that the factory needs to be |
59 // shut down. | 57 // shut down. |
60 return; | 58 return; |
61 } | 59 } |
62 | 60 |
| 61 file_util::ScopedFD scoped_fd(&new_fd); |
| 62 |
63 // Verify that the IPC channel peer is running as the same user. | 63 // Verify that the IPC channel peer is running as the same user. |
64 if (!IsPeerAuthorized(scoped_fd.get())) | 64 if (!IsPeerAuthorized(new_fd)) |
65 return; | 65 return; |
66 | 66 |
67 ChannelHandle handle(std::string(), | 67 ChannelHandle handle(std::string(), |
68 base::FileDescriptor(scoped_fd.release(), true)); | 68 base::FileDescriptor(*scoped_fd.release(), true)); |
69 delegate_->OnClientConnected(handle); | 69 delegate_->OnClientConnected(handle); |
70 } | 70 } |
71 | 71 |
72 void ChannelFactory::OnFileCanWriteWithoutBlocking(int fd) { | 72 void ChannelFactory::OnFileCanWriteWithoutBlocking(int fd) { |
73 NOTREACHED() << "Listen fd should never be writable."; | 73 NOTREACHED() << "Listen fd should never be writable."; |
74 } | 74 } |
75 | 75 |
76 void ChannelFactory::Close() { | 76 void ChannelFactory::Close() { |
77 if (listen_fd_ < 0) | 77 if (listen_fd_ < 0) |
78 return; | 78 return; |
79 if (IGNORE_EINTR(close(listen_fd_)) < 0) | 79 if (IGNORE_EINTR(close(listen_fd_)) < 0) |
80 PLOG(ERROR) << "close"; | 80 PLOG(ERROR) << "close"; |
81 listen_fd_ = -1; | 81 listen_fd_ = -1; |
82 if (unlink(path_.value().c_str()) < 0) | 82 if (unlink(path_.value().c_str()) < 0) |
83 PLOG(ERROR) << "unlink"; | 83 PLOG(ERROR) << "unlink"; |
84 | 84 |
85 // Unregister libevent for the listening socket and close it. | 85 // Unregister libevent for the listening socket and close it. |
86 server_listen_connection_watcher_.StopWatchingFileDescriptor(); | 86 server_listen_connection_watcher_.StopWatchingFileDescriptor(); |
87 } | 87 } |
88 | 88 |
89 } // namespace IPC | 89 } // namespace IPC |
OLD | NEW |