| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "chrome/browser/apps/app_shim/unix_domain_socket_acceptor.h" | 5 #include "chrome/browser/apps/app_shim/unix_domain_socket_acceptor.h" |
| 6 | 6 |
| 7 #include "base/files/file_util.h" | 7 #include <utility> |
| 8 #include "base/files/scoped_file.h" | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "ipc/unix_domain_socket_util.h" | 10 #include "mojo/edk/embedder/named_platform_handle_utils.h" |
| 11 #include "mojo/edk/embedder/platform_channel_utils_posix.h" |
| 11 | 12 |
| 12 namespace apps { | 13 namespace apps { |
| 13 | 14 |
| 14 UnixDomainSocketAcceptor::UnixDomainSocketAcceptor(const base::FilePath& path, | 15 UnixDomainSocketAcceptor::UnixDomainSocketAcceptor(const base::FilePath& path, |
| 15 Delegate* delegate) | 16 Delegate* delegate) |
| 16 : path_(path), delegate_(delegate), listen_fd_(-1) { | 17 : named_pipe_(path.value()), |
| 18 delegate_(delegate), |
| 19 listen_handle_(mojo::edk::CreateServerHandle(named_pipe_, false)) { |
| 17 DCHECK(delegate_); | 20 DCHECK(delegate_); |
| 18 CreateSocket(); | |
| 19 } | 21 } |
| 20 | 22 |
| 21 UnixDomainSocketAcceptor::~UnixDomainSocketAcceptor() { | 23 UnixDomainSocketAcceptor::~UnixDomainSocketAcceptor() { |
| 22 Close(); | 24 Close(); |
| 23 } | 25 } |
| 24 | 26 |
| 25 bool UnixDomainSocketAcceptor::CreateSocket() { | |
| 26 DCHECK(listen_fd_ < 0); | |
| 27 | |
| 28 // Create the socket. | |
| 29 return IPC::CreateServerUnixDomainSocket(path_, &listen_fd_); | |
| 30 } | |
| 31 | |
| 32 bool UnixDomainSocketAcceptor::Listen() { | 27 bool UnixDomainSocketAcceptor::Listen() { |
| 33 if (listen_fd_ < 0) | 28 if (!listen_handle_.is_valid()) |
| 34 return false; | 29 return false; |
| 35 | 30 |
| 36 // Watch the fd for connections, and turn any connections into | 31 // Watch the fd for connections, and turn any connections into |
| 37 // active sockets. | 32 // active sockets. |
| 38 base::MessageLoopForIO::current()->WatchFileDescriptor( | 33 base::MessageLoopForIO::current()->WatchFileDescriptor( |
| 39 listen_fd_, | 34 listen_handle_.get().handle, true, base::MessageLoopForIO::WATCH_READ, |
| 40 true, | 35 &server_listen_connection_watcher_, this); |
| 41 base::MessageLoopForIO::WATCH_READ, | |
| 42 &server_listen_connection_watcher_, | |
| 43 this); | |
| 44 return true; | 36 return true; |
| 45 } | 37 } |
| 46 | 38 |
| 47 // Called by libevent when we can read from the fd without blocking. | 39 // Called by libevent when we can read from the fd without blocking. |
| 48 void UnixDomainSocketAcceptor::OnFileCanReadWithoutBlocking(int fd) { | 40 void UnixDomainSocketAcceptor::OnFileCanReadWithoutBlocking(int fd) { |
| 49 DCHECK(fd == listen_fd_); | 41 DCHECK(fd == listen_handle_.get().handle); |
| 50 int new_fd = -1; | 42 mojo::edk::ScopedPlatformHandle connection_handle; |
| 51 if (!IPC::ServerOnConnect(listen_fd_, &new_fd)) { | 43 if (!mojo::edk::ServerAcceptConnection(listen_handle_.get(), |
| 44 &connection_handle)) { |
| 52 Close(); | 45 Close(); |
| 53 delegate_->OnListenError(); | 46 delegate_->OnListenError(); |
| 54 return; | 47 return; |
| 55 } | 48 } |
| 56 base::ScopedFD scoped_fd(new_fd); | |
| 57 | 49 |
| 58 if (!scoped_fd.is_valid()) { | 50 if (!connection_handle.is_valid()) { |
| 59 // The accept() failed, but not in such a way that the factory needs to be | 51 // The accept() failed, but not in such a way that the factory needs to be |
| 60 // shut down. | 52 // shut down. |
| 61 return; | 53 return; |
| 62 } | 54 } |
| 63 | 55 |
| 64 // Verify that the IPC channel peer is running as the same user. | 56 delegate_->OnClientConnected(std::move(connection_handle)); |
| 65 if (!IPC::IsPeerAuthorized(scoped_fd.get())) | |
| 66 return; | |
| 67 | |
| 68 IPC::ChannelHandle handle(std::string(), | |
| 69 base::FileDescriptor(scoped_fd.release(), true)); | |
| 70 delegate_->OnClientConnected(handle); | |
| 71 } | 57 } |
| 72 | 58 |
| 73 void UnixDomainSocketAcceptor::OnFileCanWriteWithoutBlocking(int fd) { | 59 void UnixDomainSocketAcceptor::OnFileCanWriteWithoutBlocking(int fd) { |
| 74 NOTREACHED() << "Listen fd should never be writable."; | 60 NOTREACHED() << "Listen fd should never be writable."; |
| 75 } | 61 } |
| 76 | 62 |
| 77 void UnixDomainSocketAcceptor::Close() { | 63 void UnixDomainSocketAcceptor::Close() { |
| 78 if (listen_fd_ < 0) | 64 if (!listen_handle_.is_valid()) |
| 79 return; | 65 return; |
| 80 if (IGNORE_EINTR(close(listen_fd_)) < 0) | 66 listen_handle_.reset(); |
| 81 PLOG(ERROR) << "close"; | 67 if (unlink(named_pipe_.name.c_str()) < 0) |
| 82 listen_fd_ = -1; | |
| 83 if (unlink(path_.value().c_str()) < 0) | |
| 84 PLOG(ERROR) << "unlink"; | 68 PLOG(ERROR) << "unlink"; |
| 85 | 69 |
| 86 // Unregister libevent for the listening socket and close it. | 70 // Unregister libevent for the listening socket and close it. |
| 87 server_listen_connection_watcher_.StopWatchingFileDescriptor(); | 71 server_listen_connection_watcher_.StopWatchingFileDescriptor(); |
| 88 } | 72 } |
| 89 | 73 |
| 90 } // namespace apps | 74 } // namespace apps |
| OLD | NEW |