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