michael@0: // Copyright (c) 2009 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "base/file_descriptor_shuffle.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "base/eintr_wrapper.h" michael@0: #include "base/logging.h" michael@0: michael@0: namespace base { michael@0: michael@0: bool PerformInjectiveMultimapDestructive( michael@0: InjectiveMultimap* m, InjectionDelegate* delegate) { michael@0: static const size_t kMaxExtraFDs = 16; michael@0: int extra_fds[kMaxExtraFDs]; michael@0: unsigned next_extra_fd = 0; michael@0: michael@0: // DANGER: this function may not allocate. michael@0: michael@0: for (InjectiveMultimap::iterator i = m->begin(); i != m->end(); ++i) { michael@0: int temp_fd = -1; michael@0: michael@0: // We DCHECK the injectiveness of the mapping. michael@0: for (InjectiveMultimap::iterator j = i + 1; j != m->end(); ++j) { michael@0: DCHECK(i->dest != j->dest) << "Both fd " << i->source michael@0: << " and " << j->source << " map to " << i->dest; michael@0: } michael@0: michael@0: const bool is_identity = i->source == i->dest; michael@0: michael@0: for (InjectiveMultimap::iterator j = i + 1; j != m->end(); ++j) { michael@0: if (!is_identity && i->dest == j->source) { michael@0: if (temp_fd == -1) { michael@0: if (!delegate->Duplicate(&temp_fd, i->dest)) michael@0: return false; michael@0: if (next_extra_fd < kMaxExtraFDs) { michael@0: extra_fds[next_extra_fd++] = temp_fd; michael@0: } else { michael@0: DLOG(ERROR) << "PerformInjectiveMultimapDestructive overflowed " michael@0: << "extra_fds. Leaking file descriptors!"; michael@0: } michael@0: } michael@0: michael@0: j->source = temp_fd; michael@0: j->close = false; michael@0: } michael@0: michael@0: if (i->close && i->source == j->dest) michael@0: i->close = false; michael@0: michael@0: if (i->close && i->source == j->source) { michael@0: i->close = false; michael@0: j->close = true; michael@0: } michael@0: } michael@0: michael@0: if (!is_identity) { michael@0: if (!delegate->Move(i->source, i->dest)) michael@0: return false; michael@0: } michael@0: michael@0: if (!is_identity && i->close) michael@0: delegate->Close(i->source); michael@0: } michael@0: michael@0: for (unsigned i = 0; i < next_extra_fd; i++) michael@0: delegate->Close(extra_fds[i]); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool PerformInjectiveMultimap(const InjectiveMultimap& m_in, michael@0: InjectionDelegate* delegate) { michael@0: InjectiveMultimap m(m_in); michael@0: return PerformInjectiveMultimapDestructive(&m, delegate); michael@0: } michael@0: michael@0: bool FileDescriptorTableInjection::Duplicate(int* result, int fd) { michael@0: *result = HANDLE_EINTR(dup(fd)); michael@0: return *result >= 0; michael@0: } michael@0: michael@0: bool FileDescriptorTableInjection::Move(int src, int dest) { michael@0: return HANDLE_EINTR(dup2(src, dest)) != -1; michael@0: } michael@0: michael@0: void FileDescriptorTableInjection::Close(int fd) { michael@0: HANDLE_EINTR(close(fd)); michael@0: } michael@0: michael@0: } // namespace base