xref: /aosp_15_r20/external/cronet/base/posix/file_descriptor_shuffle.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2011 The Chromium Authors
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/check.h"
12 #include "base/logging.h"
13 #include "base/posix/eintr_wrapper.h"
14 
15 namespace base {
16 
PerformInjectiveMultimapDestructive(InjectiveMultimap * m,InjectionDelegate * delegate)17 bool PerformInjectiveMultimapDestructive(InjectiveMultimap* m,
18                                          InjectionDelegate* delegate) {
19   // DANGER: this function must not allocate or lock.
20   // Cannot use STL iterators here, since debug iterators use locks.
21 
22   for (size_t i_index = 0; i_index < m->size(); ++i_index) {
23     InjectiveMultimap::value_type* i = &(*m)[i_index];
24     int temp_fd = -1;
25 
26     // We DCHECK the injectiveness of the mapping.
27     for (size_t j_index = i_index + 1; j_index < m->size(); ++j_index) {
28       InjectiveMultimap::value_type* j = &(*m)[j_index];
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 (size_t j_index = i_index + 1; j_index < m->size(); ++j_index) {
36       InjectiveMultimap::value_type* j = &(*m)[j_index];
37       if (!is_identity && i->dest == j->source) {
38         if (temp_fd == -1 && !delegate->Duplicate(&temp_fd, i->dest)) {
39           return false;
40         }
41 
42         j->source = temp_fd;
43         j->close = true;
44       }
45 
46       if (i->close && i->source == j->dest)
47         i->close = false;
48 
49       if (i->close && i->source == j->source) {
50         i->close = false;
51         j->close = true;
52       }
53     }
54 
55     if (!is_identity) {
56       if (!delegate->Move(i->source, i->dest))
57         return false;
58     }
59 
60     if (!is_identity && i->close)
61       delegate->Close(i->source);
62   }
63 
64   return true;
65 }
66 
PerformInjectiveMultimap(const InjectiveMultimap & m_in,InjectionDelegate * delegate)67 bool PerformInjectiveMultimap(const InjectiveMultimap& m_in,
68                               InjectionDelegate* delegate) {
69   InjectiveMultimap m(m_in);
70   return PerformInjectiveMultimapDestructive(&m, delegate);
71 }
72 
Duplicate(int * result,int fd)73 bool FileDescriptorTableInjection::Duplicate(int* result, int fd) {
74   *result = HANDLE_EINTR(dup(fd));
75   return *result >= 0;
76 }
77 
Move(int src,int dest)78 bool FileDescriptorTableInjection::Move(int src, int dest) {
79   return HANDLE_EINTR(dup2(src, dest)) != -1;
80 }
81 
Close(int fd)82 void FileDescriptorTableInjection::Close(int fd) {
83   int ret = IGNORE_EINTR(close(fd));
84   DPCHECK(ret == 0);
85 }
86 
87 }  // namespace base
88