ipc/chromium/src/base/file_descriptor_shuffle.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 #include "base/file_descriptor_shuffle.h"
michael@0 6
michael@0 7 #include <errno.h>
michael@0 8 #include <unistd.h>
michael@0 9
michael@0 10 #include "base/eintr_wrapper.h"
michael@0 11 #include "base/logging.h"
michael@0 12
michael@0 13 namespace base {
michael@0 14
michael@0 15 bool PerformInjectiveMultimapDestructive(
michael@0 16 InjectiveMultimap* m, InjectionDelegate* delegate) {
michael@0 17 static const size_t kMaxExtraFDs = 16;
michael@0 18 int extra_fds[kMaxExtraFDs];
michael@0 19 unsigned next_extra_fd = 0;
michael@0 20
michael@0 21 // DANGER: this function may not allocate.
michael@0 22
michael@0 23 for (InjectiveMultimap::iterator i = m->begin(); i != m->end(); ++i) {
michael@0 24 int temp_fd = -1;
michael@0 25
michael@0 26 // We DCHECK the injectiveness of the mapping.
michael@0 27 for (InjectiveMultimap::iterator j = i + 1; j != m->end(); ++j) {
michael@0 28 DCHECK(i->dest != j->dest) << "Both fd " << i->source
michael@0 29 << " and " << j->source << " map to " << i->dest;
michael@0 30 }
michael@0 31
michael@0 32 const bool is_identity = i->source == i->dest;
michael@0 33
michael@0 34 for (InjectiveMultimap::iterator j = i + 1; j != m->end(); ++j) {
michael@0 35 if (!is_identity && i->dest == j->source) {
michael@0 36 if (temp_fd == -1) {
michael@0 37 if (!delegate->Duplicate(&temp_fd, i->dest))
michael@0 38 return false;
michael@0 39 if (next_extra_fd < kMaxExtraFDs) {
michael@0 40 extra_fds[next_extra_fd++] = temp_fd;
michael@0 41 } else {
michael@0 42 DLOG(ERROR) << "PerformInjectiveMultimapDestructive overflowed "
michael@0 43 << "extra_fds. Leaking file descriptors!";
michael@0 44 }
michael@0 45 }
michael@0 46
michael@0 47 j->source = temp_fd;
michael@0 48 j->close = false;
michael@0 49 }
michael@0 50
michael@0 51 if (i->close && i->source == j->dest)
michael@0 52 i->close = false;
michael@0 53
michael@0 54 if (i->close && i->source == j->source) {
michael@0 55 i->close = false;
michael@0 56 j->close = true;
michael@0 57 }
michael@0 58 }
michael@0 59
michael@0 60 if (!is_identity) {
michael@0 61 if (!delegate->Move(i->source, i->dest))
michael@0 62 return false;
michael@0 63 }
michael@0 64
michael@0 65 if (!is_identity && i->close)
michael@0 66 delegate->Close(i->source);
michael@0 67 }
michael@0 68
michael@0 69 for (unsigned i = 0; i < next_extra_fd; i++)
michael@0 70 delegate->Close(extra_fds[i]);
michael@0 71
michael@0 72 return true;
michael@0 73 }
michael@0 74
michael@0 75 bool PerformInjectiveMultimap(const InjectiveMultimap& m_in,
michael@0 76 InjectionDelegate* delegate) {
michael@0 77 InjectiveMultimap m(m_in);
michael@0 78 return PerformInjectiveMultimapDestructive(&m, delegate);
michael@0 79 }
michael@0 80
michael@0 81 bool FileDescriptorTableInjection::Duplicate(int* result, int fd) {
michael@0 82 *result = HANDLE_EINTR(dup(fd));
michael@0 83 return *result >= 0;
michael@0 84 }
michael@0 85
michael@0 86 bool FileDescriptorTableInjection::Move(int src, int dest) {
michael@0 87 return HANDLE_EINTR(dup2(src, dest)) != -1;
michael@0 88 }
michael@0 89
michael@0 90 void FileDescriptorTableInjection::Close(int fd) {
michael@0 91 HANDLE_EINTR(close(fd));
michael@0 92 }
michael@0 93
michael@0 94 } // namespace base

mercurial