OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/common/descriptor_set_posix.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 |
| 9 DescriptorSet::DescriptorSet() |
| 10 : next_descriptor_(0) { |
| 11 } |
| 12 |
| 13 DescriptorSet::~DescriptorSet() { |
| 14 if (next_descriptor_ == descriptors_.size()) |
| 15 return; |
| 16 |
| 17 LOG(WARNING) << "DescriptorSet destroyed with unconsumed descriptors"; |
| 18 // We close all the descriptors where the close flag is set. If this |
| 19 // message should have been transmitted, then closing those with close |
| 20 // flags set mirrors the expected behaviour. |
| 21 // |
| 22 // If this message was received with more descriptors than expected |
| 23 // (which could a DOS against the browser by a rouge renderer) then all |
| 24 // the descriptors have their close flag set and we free all the extra |
| 25 // kernel resources. |
| 26 for (unsigned i = next_descriptor_; i < descriptors_.size(); ++i) { |
| 27 if (descriptors_[i].auto_close) |
| 28 close(descriptors_[i].fd); |
| 29 } |
| 30 } |
| 31 |
| 32 bool DescriptorSet::Add(int fd) { |
| 33 if (descriptors_.size() == MAX_DESCRIPTORS_PER_MESSAGE) |
| 34 return false; |
| 35 |
| 36 struct base::FileDescriptor sd; |
| 37 sd.fd = fd; |
| 38 sd.auto_close = false; |
| 39 descriptors_.push_back(sd); |
| 40 return true; |
| 41 } |
| 42 |
| 43 bool DescriptorSet::AddAndAutoClose(int fd) { |
| 44 if (descriptors_.size() == MAX_DESCRIPTORS_PER_MESSAGE) |
| 45 return false; |
| 46 |
| 47 struct base::FileDescriptor sd; |
| 48 sd.fd = fd; |
| 49 sd.auto_close = true; |
| 50 descriptors_.push_back(sd); |
| 51 DCHECK(descriptors_.size() <= MAX_DESCRIPTORS_PER_MESSAGE); |
| 52 return true; |
| 53 } |
| 54 |
| 55 int DescriptorSet::NextDescriptor() { |
| 56 if (next_descriptor_ == descriptors_.size()) |
| 57 return -1; |
| 58 |
| 59 return descriptors_[next_descriptor_++].fd; |
| 60 } |
| 61 |
| 62 void DescriptorSet::GetDescriptors(int* buffer) const { |
| 63 DCHECK_EQ(next_descriptor_, 0u); |
| 64 |
| 65 for (std::vector<base::FileDescriptor>::const_iterator |
| 66 i = descriptors_.begin(); i != descriptors_.end(); ++i) { |
| 67 *(buffer++) = i->fd; |
| 68 } |
| 69 } |
| 70 |
| 71 void DescriptorSet::CommitAll() { |
| 72 for (std::vector<base::FileDescriptor>::iterator |
| 73 i = descriptors_.begin(); i != descriptors_.end(); ++i) { |
| 74 if (i->auto_close) |
| 75 close(i->fd); |
| 76 } |
| 77 descriptors_.clear(); |
| 78 next_descriptor_ = 0; |
| 79 } |
| 80 |
| 81 void DescriptorSet::SetDescriptors(const int* buffer, unsigned count) { |
| 82 DCHECK(count <= MAX_DESCRIPTORS_PER_MESSAGE); |
| 83 DCHECK(descriptors_.size() == 0); |
| 84 |
| 85 descriptors_.reserve(count); |
| 86 for (unsigned i = 0; i < count; ++i) { |
| 87 struct base::FileDescriptor sd; |
| 88 sd.fd = buffer[i]; |
| 89 sd.auto_close = true; |
| 90 descriptors_.push_back(sd); |
| 91 } |
| 92 } |
| 93 |
| 94 void DescriptorSet::TakeFrom(DescriptorSet* other) { |
| 95 DCHECK(descriptors_.size() == 0); |
| 96 |
| 97 descriptors_.swap(other->descriptors_); |
| 98 next_descriptor_ = other->next_descriptor_; |
| 99 other->next_descriptor_ = 0; |
| 100 } |
OLD | NEW |