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/file_descriptor_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 void DescriptorSet::Add(int fd) { | |
33 struct FileDescriptor sd; | |
34 sd.fd = fd; | |
35 sd.auto_close = false; | |
36 descriptors_.push_back(sd); | |
37 DCHECK(descriptors_.size() <= MAX_DESCRIPTORS_PER_MESSAGE); | |
38 } | |
39 | |
40 void DescriptorSet::AddAndAutoClose(int fd) { | |
41 struct FileDescriptor sd; | |
42 sd.fd = fd; | |
43 sd.auto_close = true; | |
44 descriptors_.push_back(sd); | |
45 DCHECK(descriptors_.size() <= MAX_DESCRIPTORS_PER_MESSAGE); | |
46 } | |
47 | |
48 int DescriptorSet::NextDescriptor() { | |
49 if (next_descriptor_ == descriptors_.size()) | |
50 return -1; | |
51 | |
52 return descriptors_[next_descriptor_++].fd; | |
53 } | |
54 | |
55 void DescriptorSet::GetDescriptors(int* buffer) const { | |
56 for (std::vector<FileDescriptor>::const_iterator | |
57 i = descriptors_.begin(); i != descriptors_.end(); ++i) { | |
58 *(buffer++) = i->fd; | |
59 } | |
60 } | |
61 | |
62 void DescriptorSet::CommitAll() { | |
63 for (std::vector<FileDescriptor>::iterator | |
64 i = descriptors_.begin(); i != descriptors_.end(); ++i) { | |
65 if (i->auto_close) | |
66 close(i->fd); | |
67 } | |
68 descriptors_.clear(); | |
69 } | |
70 | |
71 void DescriptorSet::SetDescriptors(const int* buffer, unsigned count) { | |
72 DCHECK(count <= MAX_DESCRIPTORS_PER_MESSAGE); | |
73 DCHECK(descriptors_.size() == 0); | |
74 | |
75 descriptors_.reserve(count); | |
76 for (unsigned i = 0; i < count; ++i) { | |
77 struct FileDescriptor sd; | |
78 sd.fd = buffer[i]; | |
79 sd.auto_close = true; | |
80 descriptors_.push_back(sd); | |
81 } | |
82 } | |
83 | |
84 void DescriptorSet::TakeFrom(DescriptorSet* other) { | |
85 DCHECK(descriptors_.size() == 0); | |
86 | |
87 descriptors_.swap(other->descriptors_); | |
88 next_descriptor_ = other->next_descriptor_; | |
89 other->next_descriptor_ = 0; | |
90 } | |
OLD | NEW |