OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/socket.h> | 10 #include <sys/socket.h> |
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 server_listen_pipe_ = local_pipe; | 320 server_listen_pipe_ = local_pipe; |
321 local_pipe = -1; | 321 local_pipe = -1; |
322 } | 322 } |
323 | 323 |
324 pipe_ = local_pipe; | 324 pipe_ = local_pipe; |
325 return true; | 325 return true; |
326 } | 326 } |
327 | 327 |
328 bool Channel::ChannelImpl::Connect() { | 328 bool Channel::ChannelImpl::Connect() { |
329 if (server_listen_pipe_ == -1 && pipe_ == -1) { | 329 if (server_listen_pipe_ == -1 && pipe_ == -1) { |
330 DLOG(INFO) << "Channel creation failed: " << pipe_name_; | 330 DLOG(WARNING) << "Channel creation failed: " << pipe_name_; |
331 return false; | 331 return false; |
332 } | 332 } |
333 | 333 |
334 bool did_connect = true; | 334 bool did_connect = true; |
335 if (server_listen_pipe_ != -1) { | 335 if (server_listen_pipe_ != -1) { |
336 // Watch the pipe for connections, and turn any connections into | 336 // Watch the pipe for connections, and turn any connections into |
337 // active sockets. | 337 // active sockets. |
338 base::MessageLoopForIO::current()->WatchFileDescriptor( | 338 base::MessageLoopForIO::current()->WatchFileDescriptor( |
339 server_listen_pipe_, | 339 server_listen_pipe_, |
340 true, | 340 true, |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
457 } else | 457 } else |
458 #endif // IPC_USES_READWRITE | 458 #endif // IPC_USES_READWRITE |
459 { | 459 { |
460 bytes_written = HANDLE_EINTR(sendmsg(pipe_, &msgh, MSG_DONTWAIT)); | 460 bytes_written = HANDLE_EINTR(sendmsg(pipe_, &msgh, MSG_DONTWAIT)); |
461 } | 461 } |
462 } | 462 } |
463 if (bytes_written > 0) | 463 if (bytes_written > 0) |
464 CloseFileDescriptors(msg); | 464 CloseFileDescriptors(msg); |
465 | 465 |
466 if (bytes_written < 0 && !SocketWriteErrorIsRecoverable()) { | 466 if (bytes_written < 0 && !SocketWriteErrorIsRecoverable()) { |
| 467 // We can't close the pipe here, because calling OnChannelError |
| 468 // may destroy this object, and that would be bad if we are |
| 469 // called from Send(). Instead, we return false and hope the |
| 470 // caller will close the pipe. If they do not, the pipe will |
| 471 // still be closed next time OnFileCanReadWithoutBlocking is |
| 472 // called. |
467 #if defined(OS_MACOSX) | 473 #if defined(OS_MACOSX) |
468 // On OSX writing to a pipe with no listener returns EPERM. | 474 // On OSX writing to a pipe with no listener returns EPERM. |
469 if (errno == EPERM) { | 475 if (errno == EPERM) { |
470 Close(); | |
471 return false; | 476 return false; |
472 } | 477 } |
473 #endif // OS_MACOSX | 478 #endif // OS_MACOSX |
474 if (errno == EPIPE) { | 479 if (errno == EPIPE) { |
475 Close(); | |
476 return false; | 480 return false; |
477 } | 481 } |
478 PLOG(ERROR) << "pipe error on " | 482 PLOG(ERROR) << "pipe error on " |
479 << fd_written | 483 << fd_written |
480 << " Currently writing message of size: " | 484 << " Currently writing message of size: " |
481 << msg->size(); | 485 << msg->size(); |
482 return false; | 486 return false; |
483 } | 487 } |
484 | 488 |
485 if (static_cast<size_t>(bytes_written) != amt_to_write) { | 489 if (static_cast<size_t>(bytes_written) != amt_to_write) { |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
673 ClosePipeOnError(); | 677 ClosePipeOnError(); |
674 return; | 678 return; |
675 } | 679 } |
676 } else { | 680 } else { |
677 NOTREACHED() << "Unknown pipe " << fd; | 681 NOTREACHED() << "Unknown pipe " << fd; |
678 } | 682 } |
679 | 683 |
680 // If we're a server and handshaking, then we want to make sure that we | 684 // If we're a server and handshaking, then we want to make sure that we |
681 // only send our handshake message after we've processed the client's. | 685 // only send our handshake message after we've processed the client's. |
682 // This gives us a chance to kill the client if the incoming handshake | 686 // This gives us a chance to kill the client if the incoming handshake |
683 // is invalid. This also flushes any closefd messagse. | 687 // is invalid. This also flushes any closefd messages. |
684 if (!is_blocked_on_write_) { | 688 if (!is_blocked_on_write_) { |
685 if (!ProcessOutgoingMessages()) { | 689 if (!ProcessOutgoingMessages()) { |
686 ClosePipeOnError(); | 690 ClosePipeOnError(); |
687 } | 691 } |
688 } | 692 } |
689 } | 693 } |
690 | 694 |
691 // Called by libevent when we can write to the pipe without blocking. | 695 // Called by libevent when we can write to the pipe without blocking. |
692 void Channel::ChannelImpl::OnFileCanWriteWithoutBlocking(int fd) { | 696 void Channel::ChannelImpl::OnFileCanWriteWithoutBlocking(int fd) { |
693 DCHECK_EQ(pipe_, fd); | 697 DCHECK_EQ(pipe_, fd); |
(...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1101 | 1105 |
1102 | 1106 |
1103 #if defined(OS_LINUX) | 1107 #if defined(OS_LINUX) |
1104 // static | 1108 // static |
1105 void Channel::SetGlobalPid(int pid) { | 1109 void Channel::SetGlobalPid(int pid) { |
1106 ChannelImpl::SetGlobalPid(pid); | 1110 ChannelImpl::SetGlobalPid(pid); |
1107 } | 1111 } |
1108 #endif // OS_LINUX | 1112 #endif // OS_LINUX |
1109 | 1113 |
1110 } // namespace IPC | 1114 } // namespace IPC |
OLD | NEW |