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