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