| 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/file_descriptor_set_posix.h" | 5 #include "ipc/file_descriptor_set_posix.h" |
| 6 | 6 |
| 7 #include <sys/types.h> | 7 #include <sys/types.h> |
| 8 #include <sys/stat.h> | 8 #include <sys/stat.h> |
| 9 #include <unistd.h> | 9 #include <unistd.h> |
| 10 | 10 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 // message should have been transmitted, then closing those with close | 24 // message should have been transmitted, then closing those with close |
| 25 // flags set mirrors the expected behaviour. | 25 // flags set mirrors the expected behaviour. |
| 26 // | 26 // |
| 27 // If this message was received with more descriptors than expected | 27 // If this message was received with more descriptors than expected |
| 28 // (which could a DOS against the browser by a rogue renderer) then all | 28 // (which could a DOS against the browser by a rogue renderer) then all |
| 29 // the descriptors have their close flag set and we free all the extra | 29 // the descriptors have their close flag set and we free all the extra |
| 30 // kernel resources. | 30 // kernel resources. |
| 31 for (unsigned i = consumed_descriptor_highwater_; | 31 for (unsigned i = consumed_descriptor_highwater_; |
| 32 i < descriptors_.size(); ++i) { | 32 i < descriptors_.size(); ++i) { |
| 33 if (descriptors_[i].auto_close) | 33 if (descriptors_[i].auto_close) |
| 34 if (HANDLE_EINTR(close(descriptors_[i].fd)) < 0) | 34 if (IGNORE_EINTR(close(descriptors_[i].fd)) < 0) |
| 35 PLOG(ERROR) << "close"; | 35 PLOG(ERROR) << "close"; |
| 36 } | 36 } |
| 37 } | 37 } |
| 38 | 38 |
| 39 bool FileDescriptorSet::Add(int fd) { | 39 bool FileDescriptorSet::Add(int fd) { |
| 40 if (descriptors_.size() == kMaxDescriptorsPerMessage) { | 40 if (descriptors_.size() == kMaxDescriptorsPerMessage) { |
| 41 DLOG(WARNING) << "Cannot add file descriptor. FileDescriptorSet full."; | 41 DLOG(WARNING) << "Cannot add file descriptor. FileDescriptorSet full."; |
| 42 return false; | 42 return false; |
| 43 } | 43 } |
| 44 | 44 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 return true; | 112 return true; |
| 113 } | 113 } |
| 114 | 114 |
| 115 return false; | 115 return false; |
| 116 } | 116 } |
| 117 | 117 |
| 118 void FileDescriptorSet::CommitAll() { | 118 void FileDescriptorSet::CommitAll() { |
| 119 for (std::vector<base::FileDescriptor>::iterator | 119 for (std::vector<base::FileDescriptor>::iterator |
| 120 i = descriptors_.begin(); i != descriptors_.end(); ++i) { | 120 i = descriptors_.begin(); i != descriptors_.end(); ++i) { |
| 121 if (i->auto_close) | 121 if (i->auto_close) |
| 122 if (HANDLE_EINTR(close(i->fd)) < 0) | 122 if (IGNORE_EINTR(close(i->fd)) < 0) |
| 123 PLOG(ERROR) << "close"; | 123 PLOG(ERROR) << "close"; |
| 124 } | 124 } |
| 125 descriptors_.clear(); | 125 descriptors_.clear(); |
| 126 consumed_descriptor_highwater_ = 0; | 126 consumed_descriptor_highwater_ = 0; |
| 127 } | 127 } |
| 128 | 128 |
| 129 void FileDescriptorSet::ReleaseFDsToClose(std::vector<int>* fds) { | 129 void FileDescriptorSet::ReleaseFDsToClose(std::vector<int>* fds) { |
| 130 for (std::vector<base::FileDescriptor>::iterator | 130 for (std::vector<base::FileDescriptor>::iterator |
| 131 i = descriptors_.begin(); i != descriptors_.end(); ++i) { | 131 i = descriptors_.begin(); i != descriptors_.end(); ++i) { |
| 132 if (i->auto_close) | 132 if (i->auto_close) |
| 133 fds->push_back(i->fd); | 133 fds->push_back(i->fd); |
| 134 } | 134 } |
| 135 descriptors_.clear(); | 135 descriptors_.clear(); |
| 136 consumed_descriptor_highwater_ = 0; | 136 consumed_descriptor_highwater_ = 0; |
| 137 } | 137 } |
| 138 | 138 |
| 139 void FileDescriptorSet::SetDescriptors(const int* buffer, unsigned count) { | 139 void FileDescriptorSet::SetDescriptors(const int* buffer, unsigned count) { |
| 140 DCHECK(count <= kMaxDescriptorsPerMessage); | 140 DCHECK(count <= kMaxDescriptorsPerMessage); |
| 141 DCHECK_EQ(descriptors_.size(), 0u); | 141 DCHECK_EQ(descriptors_.size(), 0u); |
| 142 DCHECK_EQ(consumed_descriptor_highwater_, 0u); | 142 DCHECK_EQ(consumed_descriptor_highwater_, 0u); |
| 143 | 143 |
| 144 descriptors_.reserve(count); | 144 descriptors_.reserve(count); |
| 145 for (unsigned i = 0; i < count; ++i) { | 145 for (unsigned i = 0; i < count; ++i) { |
| 146 struct base::FileDescriptor sd; | 146 struct base::FileDescriptor sd; |
| 147 sd.fd = buffer[i]; | 147 sd.fd = buffer[i]; |
| 148 sd.auto_close = true; | 148 sd.auto_close = true; |
| 149 descriptors_.push_back(sd); | 149 descriptors_.push_back(sd); |
| 150 } | 150 } |
| 151 } | 151 } |
| OLD | NEW |