blob: f628bec411cb085a245332c6016dcd8baf41f4f6 [file] [log] [blame]
Scott Graham66962112018-06-08 12:42:08 -07001// 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 Graham66962112018-06-08 12:42:08 -07007#include <stddef.h>
Scott Graham98cd3ca2018-06-14 22:26:55 -07008#include <unistd.h>
Scott Graham66962112018-06-08 12:42:08 -07009#include <ostream>
10
Scott Graham66962112018-06-08 12:42:08 -070011#include "base/logging.h"
Scott Graham98cd3ca2018-06-14 22:26:55 -070012#include "base/posix/eintr_wrapper.h"
Scott Graham66962112018-06-08 12:42:08 -070013
14namespace base {
15
Scott Graham98cd3ca2018-06-14 22:26:55 -070016bool PerformInjectiveMultimapDestructive(InjectiveMultimap* m,
17 InjectionDelegate* delegate) {
Scott Graham66962112018-06-08 12:42:08 -070018 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 Graham98cd3ca2018-06-14 22:26:55 -070032 DCHECK(i->dest != j->dest) << "Both fd " << i->source << " and "
33 << j->source << " map to " << i->dest;
Scott Graham66962112018-06-08 12:42:08 -070034 }
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 Graham98cd3ca2018-06-14 22:26:55 -070047 RAW_LOG(ERROR,
48 "PerformInjectiveMultimapDestructive overflowed "
49 "extra_fds. Leaking file descriptors!");
Scott Graham66962112018-06-08 12:42:08 -070050 }
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
81bool PerformInjectiveMultimap(const InjectiveMultimap& m_in,
82 InjectionDelegate* delegate) {
83 InjectiveMultimap m(m_in);
84 return PerformInjectiveMultimapDestructive(&m, delegate);
85}
86
87bool FileDescriptorTableInjection::Duplicate(int* result, int fd) {
88 *result = HANDLE_EINTR(dup(fd));
89 return *result >= 0;
90}
91
92bool FileDescriptorTableInjection::Move(int src, int dest) {
93 return HANDLE_EINTR(dup2(src, dest)) != -1;
94}
95
96void FileDescriptorTableInjection::Close(int fd) {
97 int ret = IGNORE_EINTR(close(fd));
98 DPCHECK(ret == 0);
99}
100
101} // namespace base