| OLD | NEW |
| 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 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/common/ipc_channel_posix.h" | 5 #include "chrome/common/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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 AutoLock locked(lock_); | 82 AutoLock locked(lock_); |
| 83 | 83 |
| 84 ChannelToFDMap::const_iterator i = map_.find(channel_id); | 84 ChannelToFDMap::const_iterator i = map_.find(channel_id); |
| 85 if (i == map_.end()) | 85 if (i == map_.end()) |
| 86 return -1; | 86 return -1; |
| 87 return i->second; | 87 return i->second; |
| 88 } | 88 } |
| 89 | 89 |
| 90 // Remove the mapping for the given channel id. No error is signaled if the | 90 // Remove the mapping for the given channel id. No error is signaled if the |
| 91 // channel_id doesn't exist | 91 // channel_id doesn't exist |
| 92 void Remove(const std::string& channel_id) { | 92 void RemoveAndClose(const std::string& channel_id) { |
| 93 AutoLock locked(lock_); | 93 AutoLock locked(lock_); |
| 94 | 94 |
| 95 ChannelToFDMap::iterator i = map_.find(channel_id); | 95 ChannelToFDMap::iterator i = map_.find(channel_id); |
| 96 if (i != map_.end()) | 96 if (i != map_.end()) { |
| 97 HANDLE_EINTR(close(i->second)); |
| 97 map_.erase(i); | 98 map_.erase(i); |
| 99 } |
| 98 } | 100 } |
| 99 | 101 |
| 100 // Insert a mapping from @channel_id to @fd. It's a fatal error to insert a | 102 // Insert a mapping from @channel_id to @fd. It's a fatal error to insert a |
| 101 // mapping if one already exists for the given channel_id | 103 // mapping if one already exists for the given channel_id |
| 102 void Insert(const std::string& channel_id, int fd) { | 104 void Insert(const std::string& channel_id, int fd) { |
| 103 AutoLock locked(lock_); | 105 AutoLock locked(lock_); |
| 104 DCHECK(fd != -1); | 106 DCHECK(fd != -1); |
| 105 | 107 |
| 106 ChannelToFDMap::const_iterator i = map_.find(channel_id); | 108 ChannelToFDMap::const_iterator i = map_.find(channel_id); |
| 107 CHECK(i == map_.end()) << "Creating second IPC server (fd " << fd << ") " | 109 CHECK(i == map_.end()) << "Creating second IPC server (fd " << fd << ") " |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 " mode error(" << strerror(errno) << ")."; | 266 " mode error(" << strerror(errno) << ")."; |
| 265 } | 267 } |
| 266 } | 268 } |
| 267 | 269 |
| 268 // static | 270 // static |
| 269 void AddChannelSocket(const std::string& name, int socket) { | 271 void AddChannelSocket(const std::string& name, int socket) { |
| 270 Singleton<PipeMap>()->Insert(name, socket); | 272 Singleton<PipeMap>()->Insert(name, socket); |
| 271 } | 273 } |
| 272 | 274 |
| 273 // static | 275 // static |
| 276 void RemoveAndCloseChannelSocket(const std::string& name) { |
| 277 Singleton<PipeMap>()->RemoveAndClose(name); |
| 278 } |
| 279 |
| 280 // static |
| 274 bool SocketPair(int* fd1, int* fd2) { | 281 bool SocketPair(int* fd1, int* fd2) { |
| 275 int pipe_fds[2]; | 282 int pipe_fds[2]; |
| 276 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipe_fds) != 0) { | 283 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipe_fds) != 0) { |
| 277 LOG(ERROR) << "socketpair(): " << strerror(errno); | 284 LOG(ERROR) << "socketpair(): " << strerror(errno); |
| 278 return false; | 285 return false; |
| 279 } | 286 } |
| 280 | 287 |
| 281 // Set both ends to be non-blocking. | 288 // Set both ends to be non-blocking. |
| 282 if (fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK) == -1 || | 289 if (fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK) == -1 || |
| 283 fcntl(pipe_fds[1], F_SETFL, O_NONBLOCK) == -1) { | 290 fcntl(pipe_fds[1], F_SETFL, O_NONBLOCK) == -1) { |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 } | 423 } |
| 417 } else if (bytes_read == 0) { | 424 } else if (bytes_read == 0) { |
| 418 // The pipe has closed... | 425 // The pipe has closed... |
| 419 Close(); | 426 Close(); |
| 420 return false; | 427 return false; |
| 421 } | 428 } |
| 422 } | 429 } |
| 423 DCHECK(bytes_read); | 430 DCHECK(bytes_read); |
| 424 | 431 |
| 425 if (client_pipe_ != -1) { | 432 if (client_pipe_ != -1) { |
| 426 Singleton<PipeMap>()->Remove(pipe_name_); | 433 Singleton<PipeMap>()->RemoveAndClose(pipe_name_); |
| 427 HANDLE_EINTR(close(client_pipe_)); | |
| 428 client_pipe_ = -1; | 434 client_pipe_ = -1; |
| 429 } | 435 } |
| 430 | 436 |
| 431 // a pointer to an array of |num_wire_fds| file descriptors from the read | 437 // a pointer to an array of |num_wire_fds| file descriptors from the read |
| 432 const int* wire_fds = NULL; | 438 const int* wire_fds = NULL; |
| 433 unsigned num_wire_fds = 0; | 439 unsigned num_wire_fds = 0; |
| 434 | 440 |
| 435 // walk the list of control messages and, if we find an array of file | 441 // walk the list of control messages and, if we find an array of file |
| 436 // descriptors, save a pointer to the array | 442 // descriptors, save a pointer to the array |
| 437 | 443 |
| (...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 769 } | 775 } |
| 770 | 776 |
| 771 // Unregister libevent for the FIFO and close it. | 777 // Unregister libevent for the FIFO and close it. |
| 772 read_watcher_.StopWatchingFileDescriptor(); | 778 read_watcher_.StopWatchingFileDescriptor(); |
| 773 write_watcher_.StopWatchingFileDescriptor(); | 779 write_watcher_.StopWatchingFileDescriptor(); |
| 774 if (pipe_ != -1) { | 780 if (pipe_ != -1) { |
| 775 HANDLE_EINTR(close(pipe_)); | 781 HANDLE_EINTR(close(pipe_)); |
| 776 pipe_ = -1; | 782 pipe_ = -1; |
| 777 } | 783 } |
| 778 if (client_pipe_ != -1) { | 784 if (client_pipe_ != -1) { |
| 779 Singleton<PipeMap>()->Remove(pipe_name_); | 785 Singleton<PipeMap>()->RemoveAndClose(pipe_name_); |
| 780 HANDLE_EINTR(close(client_pipe_)); | |
| 781 client_pipe_ = -1; | 786 client_pipe_ = -1; |
| 782 } | 787 } |
| 783 | 788 |
| 784 if (uses_fifo_) { | 789 if (uses_fifo_) { |
| 785 // Unlink the FIFO | 790 // Unlink the FIFO |
| 786 unlink(pipe_name_.c_str()); | 791 unlink(pipe_name_.c_str()); |
| 787 } | 792 } |
| 788 | 793 |
| 789 while (!output_queue_.empty()) { | 794 while (!output_queue_.empty()) { |
| 790 Message* m = output_queue_.front(); | 795 Message* m = output_queue_.front(); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 825 | 830 |
| 826 bool Channel::Send(Message* message) { | 831 bool Channel::Send(Message* message) { |
| 827 return channel_impl_->Send(message); | 832 return channel_impl_->Send(message); |
| 828 } | 833 } |
| 829 | 834 |
| 830 int Channel::GetClientFileDescriptor() const { | 835 int Channel::GetClientFileDescriptor() const { |
| 831 return channel_impl_->GetClientFileDescriptor(); | 836 return channel_impl_->GetClientFileDescriptor(); |
| 832 } | 837 } |
| 833 | 838 |
| 834 } // namespace IPC | 839 } // namespace IPC |
| OLD | NEW |