| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-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 "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 | 9 |
| 10 #include "base/eintr_wrapper.h" | 10 #include "base/eintr_wrapper.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 // kernel resources. | 29 // kernel resources. |
| 30 for (unsigned i = consumed_descriptor_highwater_; | 30 for (unsigned i = consumed_descriptor_highwater_; |
| 31 i < descriptors_.size(); ++i) { | 31 i < descriptors_.size(); ++i) { |
| 32 if (descriptors_[i].auto_close) | 32 if (descriptors_[i].auto_close) |
| 33 if (HANDLE_EINTR(close(descriptors_[i].fd)) < 0) | 33 if (HANDLE_EINTR(close(descriptors_[i].fd)) < 0) |
| 34 PLOG(ERROR) << "close"; | 34 PLOG(ERROR) << "close"; |
| 35 } | 35 } |
| 36 } | 36 } |
| 37 | 37 |
| 38 bool FileDescriptorSet::Add(int fd) { | 38 bool FileDescriptorSet::Add(int fd) { |
| 39 if (descriptors_.size() == MAX_DESCRIPTORS_PER_MESSAGE) | 39 if (descriptors_.size() == kMaxDescriptorsPerMessage) |
| 40 return false; | 40 return false; |
| 41 | 41 |
| 42 struct base::FileDescriptor sd; | 42 struct base::FileDescriptor sd; |
| 43 sd.fd = fd; | 43 sd.fd = fd; |
| 44 sd.auto_close = false; | 44 sd.auto_close = false; |
| 45 descriptors_.push_back(sd); | 45 descriptors_.push_back(sd); |
| 46 return true; | 46 return true; |
| 47 } | 47 } |
| 48 | 48 |
| 49 bool FileDescriptorSet::AddAndAutoClose(int fd) { | 49 bool FileDescriptorSet::AddAndAutoClose(int fd) { |
| 50 if (descriptors_.size() == MAX_DESCRIPTORS_PER_MESSAGE) | 50 if (descriptors_.size() == kMaxDescriptorsPerMessage) |
| 51 return false; | 51 return false; |
| 52 | 52 |
| 53 struct base::FileDescriptor sd; | 53 struct base::FileDescriptor sd; |
| 54 sd.fd = fd; | 54 sd.fd = fd; |
| 55 sd.auto_close = true; | 55 sd.auto_close = true; |
| 56 descriptors_.push_back(sd); | 56 descriptors_.push_back(sd); |
| 57 DCHECK(descriptors_.size() <= MAX_DESCRIPTORS_PER_MESSAGE); | 57 DCHECK(descriptors_.size() <= kMaxDescriptorsPerMessage); |
| 58 return true; | 58 return true; |
| 59 } | 59 } |
| 60 | 60 |
| 61 int FileDescriptorSet::GetDescriptorAt(unsigned index) const { | 61 int FileDescriptorSet::GetDescriptorAt(unsigned index) const { |
| 62 if (index >= descriptors_.size()) | 62 if (index >= descriptors_.size()) |
| 63 return -1; | 63 return -1; |
| 64 | 64 |
| 65 // We should always walk the descriptors in order, so it's reasonable to | 65 // We should always walk the descriptors in order, so it's reasonable to |
| 66 // enforce this. Consider the case where a compromised renderer sends us | 66 // enforce this. Consider the case where a compromised renderer sends us |
| 67 // the following message: | 67 // the following message: |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 i = descriptors_.begin(); i != descriptors_.end(); ++i) { | 115 i = descriptors_.begin(); i != descriptors_.end(); ++i) { |
| 116 if (i->auto_close) | 116 if (i->auto_close) |
| 117 if (HANDLE_EINTR(close(i->fd)) < 0) | 117 if (HANDLE_EINTR(close(i->fd)) < 0) |
| 118 PLOG(ERROR) << "close"; | 118 PLOG(ERROR) << "close"; |
| 119 } | 119 } |
| 120 descriptors_.clear(); | 120 descriptors_.clear(); |
| 121 consumed_descriptor_highwater_ = 0; | 121 consumed_descriptor_highwater_ = 0; |
| 122 } | 122 } |
| 123 | 123 |
| 124 void FileDescriptorSet::SetDescriptors(const int* buffer, unsigned count) { | 124 void FileDescriptorSet::SetDescriptors(const int* buffer, unsigned count) { |
| 125 DCHECK_LE(count, MAX_DESCRIPTORS_PER_MESSAGE); | 125 DCHECK(count <= kMaxDescriptorsPerMessage); |
| 126 DCHECK_EQ(descriptors_.size(), 0u); | 126 DCHECK_EQ(descriptors_.size(), 0u); |
| 127 DCHECK_EQ(consumed_descriptor_highwater_, 0u); | 127 DCHECK_EQ(consumed_descriptor_highwater_, 0u); |
| 128 | 128 |
| 129 descriptors_.reserve(count); | 129 descriptors_.reserve(count); |
| 130 for (unsigned i = 0; i < count; ++i) { | 130 for (unsigned i = 0; i < count; ++i) { |
| 131 struct base::FileDescriptor sd; | 131 struct base::FileDescriptor sd; |
| 132 sd.fd = buffer[i]; | 132 sd.fd = buffer[i]; |
| 133 sd.auto_close = true; | 133 sd.auto_close = true; |
| 134 descriptors_.push_back(sd); | 134 descriptors_.push_back(sd); |
| 135 } | 135 } |
| 136 } | 136 } |
| OLD | NEW |