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 "base/posix/file_descriptor_shuffle.h" | 5 #include "base/posix/file_descriptor_shuffle.h" |
6 | 6 |
7 #include <unistd.h> | 7 #include <unistd.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <ostream> | 9 #include <ostream> |
10 | 10 |
11 #include "base/posix/eintr_wrapper.h" | 11 #include "base/posix/eintr_wrapper.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 | 13 |
14 namespace base { | 14 namespace base { |
15 | 15 |
16 bool PerformInjectiveMultimapDestructive( | 16 bool PerformInjectiveMultimapDestructive( |
17 InjectiveMultimap* m, InjectionDelegate* delegate) { | 17 InjectiveMultimap* m, InjectionDelegate* delegate) { |
18 static const size_t kMaxExtraFDs = 16; | 18 static const size_t kMaxExtraFDs = 16; |
19 int extra_fds[kMaxExtraFDs]; | 19 int extra_fds[kMaxExtraFDs]; |
20 unsigned next_extra_fd = 0; | 20 unsigned next_extra_fd = 0; |
21 | 21 |
22 // DANGER: this function may not allocate. | 22 // DANGER: this function must not allocate or lock. |
| 23 // Cannot use STL iterators here, since debug iterators use locks. |
23 | 24 |
24 for (InjectiveMultimap::iterator i = m->begin(); i != m->end(); ++i) { | 25 for (size_t i_index = 0; i_index < m->size(); ++i_index) { |
| 26 InjectiveMultimap::value_type* i = &(*m)[i_index]; |
25 int temp_fd = -1; | 27 int temp_fd = -1; |
26 | 28 |
27 // We DCHECK the injectiveness of the mapping. | 29 // We DCHECK the injectiveness of the mapping. |
28 for (InjectiveMultimap::iterator j = i + 1; j != m->end(); ++j) { | 30 for (size_t j_index = i_index + 1; j_index < m->size(); ++j_index) { |
| 31 InjectiveMultimap::value_type* j = &(*m)[j_index]; |
29 DCHECK(i->dest != j->dest) << "Both fd " << i->source | 32 DCHECK(i->dest != j->dest) << "Both fd " << i->source |
30 << " and " << j->source << " map to " << i->dest; | 33 << " and " << j->source << " map to " << i->dest; |
31 } | 34 } |
32 | 35 |
33 const bool is_identity = i->source == i->dest; | 36 const bool is_identity = i->source == i->dest; |
34 | 37 |
35 for (InjectiveMultimap::iterator j = i + 1; j != m->end(); ++j) { | 38 for (size_t j_index = i_index + 1; j_index < m->size(); ++j_index) { |
| 39 InjectiveMultimap::value_type* j = &(*m)[j_index]; |
36 if (!is_identity && i->dest == j->source) { | 40 if (!is_identity && i->dest == j->source) { |
37 if (temp_fd == -1) { | 41 if (temp_fd == -1) { |
38 if (!delegate->Duplicate(&temp_fd, i->dest)) | 42 if (!delegate->Duplicate(&temp_fd, i->dest)) |
39 return false; | 43 return false; |
40 if (next_extra_fd < kMaxExtraFDs) { | 44 if (next_extra_fd < kMaxExtraFDs) { |
41 extra_fds[next_extra_fd++] = temp_fd; | 45 extra_fds[next_extra_fd++] = temp_fd; |
42 } else { | 46 } else { |
43 RAW_LOG(ERROR, "PerformInjectiveMultimapDestructive overflowed " | 47 RAW_LOG(ERROR, "PerformInjectiveMultimapDestructive overflowed " |
44 "extra_fds. Leaking file descriptors!"); | 48 "extra_fds. Leaking file descriptors!"); |
45 } | 49 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 bool FileDescriptorTableInjection::Move(int src, int dest) { | 91 bool FileDescriptorTableInjection::Move(int src, int dest) { |
88 return HANDLE_EINTR(dup2(src, dest)) != -1; | 92 return HANDLE_EINTR(dup2(src, dest)) != -1; |
89 } | 93 } |
90 | 94 |
91 void FileDescriptorTableInjection::Close(int fd) { | 95 void FileDescriptorTableInjection::Close(int fd) { |
92 int ret = IGNORE_EINTR(close(fd)); | 96 int ret = IGNORE_EINTR(close(fd)); |
93 DPCHECK(ret == 0); | 97 DPCHECK(ret == 0); |
94 } | 98 } |
95 | 99 |
96 } // namespace base | 100 } // namespace base |
OLD | NEW |