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 "chrome/common/file_descriptor_set_posix.h" | 5 #include "chrome/common/file_descriptor_set_posix.h" |
6 | 6 |
| 7 #include "base/eintr_wrappers.h" |
7 #include "base/logging.h" | 8 #include "base/logging.h" |
8 | 9 |
9 FileDescriptorSet::FileDescriptorSet() | 10 FileDescriptorSet::FileDescriptorSet() |
10 : consumed_descriptor_highwater_(0) { | 11 : consumed_descriptor_highwater_(0) { |
11 } | 12 } |
12 | 13 |
13 FileDescriptorSet::~FileDescriptorSet() { | 14 FileDescriptorSet::~FileDescriptorSet() { |
14 if (consumed_descriptor_highwater_ == descriptors_.size()) | 15 if (consumed_descriptor_highwater_ == descriptors_.size()) |
15 return; | 16 return; |
16 | 17 |
17 LOG(WARNING) << "FileDescriptorSet destroyed with unconsumed descriptors"; | 18 LOG(WARNING) << "FileDescriptorSet destroyed with unconsumed descriptors"; |
18 // We close all the descriptors where the close flag is set. If this | 19 // 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 // message should have been transmitted, then closing those with close |
20 // flags set mirrors the expected behaviour. | 21 // flags set mirrors the expected behaviour. |
21 // | 22 // |
22 // If this message was received with more descriptors than expected | 23 // If this message was received with more descriptors than expected |
23 // (which could a DOS against the browser by a rogue renderer) then all | 24 // (which could a DOS against the browser by a rogue renderer) then all |
24 // the descriptors have their close flag set and we free all the extra | 25 // the descriptors have their close flag set and we free all the extra |
25 // kernel resources. | 26 // kernel resources. |
26 for (unsigned i = consumed_descriptor_highwater_; | 27 for (unsigned i = consumed_descriptor_highwater_; |
27 i < descriptors_.size(); ++i) { | 28 i < descriptors_.size(); ++i) { |
28 if (descriptors_[i].auto_close) | 29 if (descriptors_[i].auto_close) |
29 close(descriptors_[i].fd); | 30 HANDLE_EINTR(close(descriptors_[i].fd)); |
30 } | 31 } |
31 } | 32 } |
32 | 33 |
33 bool FileDescriptorSet::Add(int fd) { | 34 bool FileDescriptorSet::Add(int fd) { |
34 if (descriptors_.size() == MAX_DESCRIPTORS_PER_MESSAGE) | 35 if (descriptors_.size() == MAX_DESCRIPTORS_PER_MESSAGE) |
35 return false; | 36 return false; |
36 | 37 |
37 struct base::FileDescriptor sd; | 38 struct base::FileDescriptor sd; |
38 sd.fd = fd; | 39 sd.fd = fd; |
39 sd.auto_close = false; | 40 sd.auto_close = false; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 for (std::vector<base::FileDescriptor>::const_iterator | 91 for (std::vector<base::FileDescriptor>::const_iterator |
91 i = descriptors_.begin(); i != descriptors_.end(); ++i) { | 92 i = descriptors_.begin(); i != descriptors_.end(); ++i) { |
92 *(buffer++) = i->fd; | 93 *(buffer++) = i->fd; |
93 } | 94 } |
94 } | 95 } |
95 | 96 |
96 void FileDescriptorSet::CommitAll() { | 97 void FileDescriptorSet::CommitAll() { |
97 for (std::vector<base::FileDescriptor>::iterator | 98 for (std::vector<base::FileDescriptor>::iterator |
98 i = descriptors_.begin(); i != descriptors_.end(); ++i) { | 99 i = descriptors_.begin(); i != descriptors_.end(); ++i) { |
99 if (i->auto_close) | 100 if (i->auto_close) |
100 close(i->fd); | 101 HANDLE_EINTR(close(i->fd)); |
101 } | 102 } |
102 descriptors_.clear(); | 103 descriptors_.clear(); |
103 consumed_descriptor_highwater_ = 0; | 104 consumed_descriptor_highwater_ = 0; |
104 } | 105 } |
105 | 106 |
106 void FileDescriptorSet::SetDescriptors(const int* buffer, unsigned count) { | 107 void FileDescriptorSet::SetDescriptors(const int* buffer, unsigned count) { |
107 DCHECK_LE(count, MAX_DESCRIPTORS_PER_MESSAGE); | 108 DCHECK_LE(count, MAX_DESCRIPTORS_PER_MESSAGE); |
108 DCHECK_EQ(descriptors_.size(), 0u); | 109 DCHECK_EQ(descriptors_.size(), 0u); |
109 DCHECK_EQ(consumed_descriptor_highwater_, 0u); | 110 DCHECK_EQ(consumed_descriptor_highwater_, 0u); |
110 | 111 |
111 descriptors_.reserve(count); | 112 descriptors_.reserve(count); |
112 for (unsigned i = 0; i < count; ++i) { | 113 for (unsigned i = 0; i < count; ++i) { |
113 struct base::FileDescriptor sd; | 114 struct base::FileDescriptor sd; |
114 sd.fd = buffer[i]; | 115 sd.fd = buffer[i]; |
115 sd.auto_close = true; | 116 sd.auto_close = true; |
116 descriptors_.push_back(sd); | 117 descriptors_.push_back(sd); |
117 } | 118 } |
118 } | 119 } |
OLD | NEW |