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 
7 #include <unistd.h>
8 #include <stddef.h>
9 #include <ostream>
10 
11 #include "base/posix/eintr_wrapper.h"
12 #include "base/logging.h"
13 
14 namespace base {
15 
PerformInjectiveMultimapDestructive(InjectiveMultimap * m,InjectionDelegate * delegate)16 bool 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 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];
32       DCHECK(i->dest != j->dest) << "Both fd " << i->source
33           << " and " << j->source << " map to " << i->dest;
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 {
47             RAW_LOG(ERROR, "PerformInjectiveMultimapDestructive overflowed "
48                            "extra_fds. Leaking file descriptors!");
49           }
50         }
51 
52         j->source = temp_fd;
53         j->close = false;
54       }
55 
56       if (i->close && i->source == j->dest)
57         i->close = false;
58 
59       if (i->close && i->source == j->source) {
60         i->close = false;
61         j->close = true;
62       }
63     }
64 
65     if (!is_identity) {
66       if (!delegate->Move(i->source, i->dest))
67         return false;
68     }
69 
70     if (!is_identity && i->close)
71       delegate->Close(i->source);
72   }
73 
74   for (unsigned i = 0; i < next_extra_fd; i++)
75     delegate->Close(extra_fds[i]);
76 
77   return true;
78 }
79 
PerformInjectiveMultimap(const InjectiveMultimap & m_in,InjectionDelegate * delegate)80 bool PerformInjectiveMultimap(const InjectiveMultimap& m_in,
81                               InjectionDelegate* delegate) {
82   InjectiveMultimap m(m_in);
83   return PerformInjectiveMultimapDestructive(&m, delegate);
84 }
85 
Duplicate(int * result,int fd)86 bool FileDescriptorTableInjection::Duplicate(int* result, int fd) {
87   *result = HANDLE_EINTR(dup(fd));
88   return *result >= 0;
89 }
90 
Move(int src,int dest)91 bool FileDescriptorTableInjection::Move(int src, int dest) {
92   return HANDLE_EINTR(dup2(src, dest)) != -1;
93 }
94 
Close(int fd)95 void FileDescriptorTableInjection::Close(int fd) {
96   int ret = IGNORE_EINTR(close(fd));
97   DPCHECK(ret == 0);
98 }
99 
100 }  // namespace base
101