Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/posix/file_descriptor_shuffle.h" |
| 6 | |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 7 | #include <stddef.h> |
Scott Graham | 98cd3ca | 2018-06-14 22:26:55 -0700 | [diff] [blame] | 8 | #include <unistd.h> |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 9 | #include <ostream> |
| 10 | |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 11 | #include "base/logging.h" |
Scott Graham | 98cd3ca | 2018-06-14 22:26:55 -0700 | [diff] [blame] | 12 | #include "base/posix/eintr_wrapper.h" |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 13 | |
| 14 | namespace base { |
| 15 | |
Scott Graham | 98cd3ca | 2018-06-14 22:26:55 -0700 | [diff] [blame] | 16 | bool PerformInjectiveMultimapDestructive(InjectiveMultimap* m, |
| 17 | InjectionDelegate* delegate) { |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 18 | static const size_t kMaxExtraFDs = 16; |
| 19 | int extra_fds[kMaxExtraFDs]; |
| 20 | unsigned next_extra_fd = 0; |
| 21 | |
| 22 | // DANGER: this function must not allocate or lock. |
| 23 | // Cannot use STL iterators here, since debug iterators use locks. |
| 24 | |
| 25 | for (size_t i_index = 0; i_index < m->size(); ++i_index) { |
| 26 | InjectiveMultimap::value_type* i = &(*m)[i_index]; |
| 27 | int temp_fd = -1; |
| 28 | |
| 29 | // We DCHECK the injectiveness of the mapping. |
| 30 | for (size_t j_index = i_index + 1; j_index < m->size(); ++j_index) { |
| 31 | InjectiveMultimap::value_type* j = &(*m)[j_index]; |
Scott Graham | 98cd3ca | 2018-06-14 22:26:55 -0700 | [diff] [blame] | 32 | DCHECK(i->dest != j->dest) << "Both fd " << i->source << " and " |
| 33 | << j->source << " map to " << i->dest; |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | const bool is_identity = i->source == i->dest; |
| 37 | |
| 38 | for (size_t j_index = i_index + 1; j_index < m->size(); ++j_index) { |
| 39 | InjectiveMultimap::value_type* j = &(*m)[j_index]; |
| 40 | if (!is_identity && i->dest == j->source) { |
| 41 | if (temp_fd == -1) { |
| 42 | if (!delegate->Duplicate(&temp_fd, i->dest)) |
| 43 | return false; |
| 44 | if (next_extra_fd < kMaxExtraFDs) { |
| 45 | extra_fds[next_extra_fd++] = temp_fd; |
| 46 | } else { |
Scott Graham | 98cd3ca | 2018-06-14 22:26:55 -0700 | [diff] [blame] | 47 | RAW_LOG(ERROR, |
| 48 | "PerformInjectiveMultimapDestructive overflowed " |
| 49 | "extra_fds. Leaking file descriptors!"); |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
| 53 | j->source = temp_fd; |
| 54 | j->close = false; |
| 55 | } |
| 56 | |
| 57 | if (i->close && i->source == j->dest) |
| 58 | i->close = false; |
| 59 | |
| 60 | if (i->close && i->source == j->source) { |
| 61 | i->close = false; |
| 62 | j->close = true; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | if (!is_identity) { |
| 67 | if (!delegate->Move(i->source, i->dest)) |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | if (!is_identity && i->close) |
| 72 | delegate->Close(i->source); |
| 73 | } |
| 74 | |
| 75 | for (unsigned i = 0; i < next_extra_fd; i++) |
| 76 | delegate->Close(extra_fds[i]); |
| 77 | |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | bool PerformInjectiveMultimap(const InjectiveMultimap& m_in, |
| 82 | InjectionDelegate* delegate) { |
| 83 | InjectiveMultimap m(m_in); |
| 84 | return PerformInjectiveMultimapDestructive(&m, delegate); |
| 85 | } |
| 86 | |
| 87 | bool FileDescriptorTableInjection::Duplicate(int* result, int fd) { |
| 88 | *result = HANDLE_EINTR(dup(fd)); |
| 89 | return *result >= 0; |
| 90 | } |
| 91 | |
| 92 | bool FileDescriptorTableInjection::Move(int src, int dest) { |
| 93 | return HANDLE_EINTR(dup2(src, dest)) != -1; |
| 94 | } |
| 95 | |
| 96 | void FileDescriptorTableInjection::Close(int fd) { |
| 97 | int ret = IGNORE_EINTR(close(fd)); |
| 98 | DPCHECK(ret == 0); |
| 99 | } |
| 100 | |
| 101 | } // namespace base |