OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_posix.h" | 5 #include "ipc/ipc_channel_posix.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <stddef.h> | 9 #include <stddef.h> |
10 #include <sys/types.h> | 10 #include <sys/types.h> |
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 message_send_bytes_written_(0), | 302 message_send_bytes_written_(0), |
303 server_listen_pipe_(-1), | 303 server_listen_pipe_(-1), |
304 pipe_(-1), | 304 pipe_(-1), |
305 client_pipe_(-1), | 305 client_pipe_(-1), |
306 #if defined(IPC_USES_READWRITE) | 306 #if defined(IPC_USES_READWRITE) |
307 fd_pipe_(-1), | 307 fd_pipe_(-1), |
308 remote_fd_pipe_(-1), | 308 remote_fd_pipe_(-1), |
309 #endif // IPC_USES_READWRITE | 309 #endif // IPC_USES_READWRITE |
310 pipe_name_(channel_handle.name), | 310 pipe_name_(channel_handle.name), |
311 listener_(listener), | 311 listener_(listener), |
312 must_unlink_(false), | 312 must_unlink_(false) { |
313 needs_override_peer_pid_(false), | |
314 override_peer_pid_(0) { | |
315 memset(input_buf_, 0, sizeof(input_buf_)); | 313 memset(input_buf_, 0, sizeof(input_buf_)); |
316 memset(input_cmsg_buf_, 0, sizeof(input_cmsg_buf_)); | 314 memset(input_cmsg_buf_, 0, sizeof(input_cmsg_buf_)); |
317 if (!CreatePipe(channel_handle)) { | 315 if (!CreatePipe(channel_handle)) { |
318 // The pipe may have been closed already. | 316 // The pipe may have been closed already. |
319 const char *modestr = (mode_ & MODE_SERVER_FLAG) ? "server" : "client"; | 317 const char *modestr = (mode_ & MODE_SERVER_FLAG) ? "server" : "client"; |
320 // The pipe may have been closed already. | 318 // The pipe may have been closed already. |
321 LOG(WARNING) << "Unable to create pipe named \"" << channel_handle.name | 319 LOG(WARNING) << "Unable to create pipe named \"" << channel_handle.name |
322 << "\" in " << modestr << " mode"; | 320 << "\" in " << modestr << " mode"; |
323 } | 321 } |
324 } | 322 } |
(...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
722 // subsequent file descriptor passing. | 720 // subsequent file descriptor passing. |
723 DCHECK_EQ(m.file_descriptor_set()->size(), 1U); | 721 DCHECK_EQ(m.file_descriptor_set()->size(), 1U); |
724 base::FileDescriptor descriptor; | 722 base::FileDescriptor descriptor; |
725 if (!m.ReadFileDescriptor(&iter, &descriptor)) { | 723 if (!m.ReadFileDescriptor(&iter, &descriptor)) { |
726 NOTREACHED(); | 724 NOTREACHED(); |
727 } | 725 } |
728 fd_pipe_ = descriptor.fd; | 726 fd_pipe_ = descriptor.fd; |
729 CHECK(descriptor.auto_close); | 727 CHECK(descriptor.auto_close); |
730 } | 728 } |
731 #endif // IPC_USES_READWRITE | 729 #endif // IPC_USES_READWRITE |
732 if (needs_override_peer_pid_) { | 730 listener_->OnChannelConnected(pid); |
733 // If we already have the peer PID, use it. Otherwise we'll call | |
734 // OnChannelConnected() in OverridePeerPid() below. | |
735 if (override_peer_pid_ != 0) | |
736 listener_->OnChannelConnected(override_peer_pid_); | |
737 } else { | |
738 listener_->OnChannelConnected(pid); | |
739 } | |
740 } else { | 731 } else { |
741 listener_->OnMessageReceived(m); | 732 listener_->OnMessageReceived(m); |
742 } | 733 } |
743 p = message_tail; | 734 p = message_tail; |
744 } else { | 735 } else { |
745 // Last message is partial. | 736 // Last message is partial. |
746 break; | 737 break; |
747 } | 738 } |
748 input_overflow_fds_ = std::vector<int>(&fds[fds_i], &fds[num_fds]); | 739 input_overflow_fds_ = std::vector<int>(&fds[fds_i], &fds[num_fds]); |
749 fds_i = 0; | 740 fds_i = 0; |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
995 | 986 |
996 // Close any outstanding, received file descriptors. | 987 // Close any outstanding, received file descriptors. |
997 for (std::vector<int>::iterator | 988 for (std::vector<int>::iterator |
998 i = input_overflow_fds_.begin(); i != input_overflow_fds_.end(); ++i) { | 989 i = input_overflow_fds_.begin(); i != input_overflow_fds_.end(); ++i) { |
999 if (HANDLE_EINTR(close(*i)) < 0) | 990 if (HANDLE_EINTR(close(*i)) < 0) |
1000 PLOG(ERROR) << "close"; | 991 PLOG(ERROR) << "close"; |
1001 } | 992 } |
1002 input_overflow_fds_.clear(); | 993 input_overflow_fds_.clear(); |
1003 } | 994 } |
1004 | 995 |
1005 #if defined(OS_LINUX) | |
1006 void Channel::ChannelImpl::SetNeedsOverridePeerPid() { | |
1007 needs_override_peer_pid_ = true; | |
1008 } | |
1009 | |
1010 void Channel::ChannelImpl::OverridePeerPid(int32 peer_pid) { | |
1011 DCHECK(needs_override_peer_pid_); | |
1012 override_peer_pid_ = peer_pid; | |
1013 | |
1014 // The browser learns the global PID of the renderers on the UI thread, and | |
1015 // must post the data to the IO thread for us to use it here. Therefore | |
1016 // there is a race between the IPC channel processing the hello message | |
1017 // and this function being called. If fd_pipe_ != -1 then we've already | |
1018 // received the hello message and we skipped OnChannelConnected() above, | |
1019 // so call it here. | |
1020 if (fd_pipe_ != -1) { | |
1021 listener_->OnChannelConnected(peer_pid); | |
1022 } | |
1023 } | |
1024 #endif // defined(OS_LINUX) | |
1025 | |
1026 // static | 996 // static |
1027 bool Channel::ChannelImpl::IsNamedServerInitialized( | 997 bool Channel::ChannelImpl::IsNamedServerInitialized( |
1028 const std::string& channel_id) { | 998 const std::string& channel_id) { |
1029 return file_util::PathExists(FilePath(channel_id)); | 999 return file_util::PathExists(FilePath(channel_id)); |
1030 } | 1000 } |
1031 | 1001 |
1032 // Called by libevent when we can read from the pipe without blocking. | 1002 // Called by libevent when we can read from the pipe without blocking. |
1033 void Channel::ChannelImpl::OnFileCanReadWithoutBlocking(int fd) { | 1003 void Channel::ChannelImpl::OnFileCanReadWithoutBlocking(int fd) { |
1034 bool send_server_hello_msg = false; | 1004 bool send_server_hello_msg = false; |
1035 if (fd == server_listen_pipe_) { | 1005 if (fd == server_listen_pipe_) { |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1231 } | 1201 } |
1232 | 1202 |
1233 bool Channel::GetClientEuid(uid_t* client_euid) const { | 1203 bool Channel::GetClientEuid(uid_t* client_euid) const { |
1234 return channel_impl_->GetClientEuid(client_euid); | 1204 return channel_impl_->GetClientEuid(client_euid); |
1235 } | 1205 } |
1236 | 1206 |
1237 void Channel::ResetToAcceptingConnectionState() { | 1207 void Channel::ResetToAcceptingConnectionState() { |
1238 channel_impl_->ResetToAcceptingConnectionState(); | 1208 channel_impl_->ResetToAcceptingConnectionState(); |
1239 } | 1209 } |
1240 | 1210 |
1241 #if defined(OS_LINUX) | |
1242 void Channel::SetNeedsOverridePeerPid() { | |
1243 channel_impl_->SetNeedsOverridePeerPid(); | |
1244 } | |
1245 | |
1246 void Channel::OverridePeerPid(int32 peer_pid) { | |
1247 channel_impl_->OverridePeerPid(peer_pid); | |
1248 } | |
1249 #endif // defined(OS_LINUX) | |
1250 | |
1251 // static | 1211 // static |
1252 bool Channel::IsNamedServerInitialized(const std::string& channel_id) { | 1212 bool Channel::IsNamedServerInitialized(const std::string& channel_id) { |
1253 return ChannelImpl::IsNamedServerInitialized(channel_id); | 1213 return ChannelImpl::IsNamedServerInitialized(channel_id); |
1254 } | 1214 } |
1255 | 1215 |
1256 } // namespace IPC | 1216 } // namespace IPC |
OLD | NEW |