Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(117)

Side by Side Diff: base/file_descriptor_shuffle.cc

Issue 672003: POSIX: don't allocate memory after forking. (Closed)
Patch Set: ... Created 10 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/file_descriptor_shuffle.h ('k') | base/process_util.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/file_descriptor_shuffle.h" 5 #include "base/file_descriptor_shuffle.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 #include <unistd.h> 8 #include <unistd.h>
9 9
10 #include "base/eintr_wrapper.h" 10 #include "base/eintr_wrapper.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 12
13 namespace base { 13 namespace base {
14 14
15 bool PerformInjectiveMultimap(const InjectiveMultimap& m_in, 15 bool PerformInjectiveMultimapDestructive(
16 InjectionDelegate* delegate) { 16 InjectiveMultimap* m, InjectionDelegate* delegate) {
17 InjectiveMultimap m(m_in); 17 static const size_t kMaxExtraFDs = 16;
18 std::vector<int> extra_fds; 18 int extra_fds[kMaxExtraFDs];
19 unsigned next_extra_fd = 0;
19 20
20 for (InjectiveMultimap::iterator i = m.begin(); i != m.end(); ++i) { 21 // DANGER: this function may not allocate.
22
23 for (InjectiveMultimap::iterator i = m->begin(); i != m->end(); ++i) {
21 int temp_fd = -1; 24 int temp_fd = -1;
22 25
23 // We DCHECK the injectiveness of the mapping. 26 // We DCHECK the injectiveness of the mapping.
24 for (InjectiveMultimap::iterator j = i + 1; j != m.end(); ++j) { 27 for (InjectiveMultimap::iterator j = i + 1; j != m->end(); ++j) {
25 DCHECK(i->dest != j->dest) << "Both fd " << i->source 28 DCHECK(i->dest != j->dest) << "Both fd " << i->source
26 << " and " << j->source << " map to " << i->dest; 29 << " and " << j->source << " map to " << i->dest;
27 } 30 }
28 31
29 const bool is_identity = i->source == i->dest; 32 const bool is_identity = i->source == i->dest;
30 33
31 for (InjectiveMultimap::iterator j = i + 1; j != m.end(); ++j) { 34 for (InjectiveMultimap::iterator j = i + 1; j != m->end(); ++j) {
32 if (!is_identity && i->dest == j->source) { 35 if (!is_identity && i->dest == j->source) {
33 if (temp_fd == -1) { 36 if (temp_fd == -1) {
34 if (!delegate->Duplicate(&temp_fd, i->dest)) 37 if (!delegate->Duplicate(&temp_fd, i->dest))
35 return false; 38 return false;
36 extra_fds.push_back(temp_fd); 39 if (next_extra_fd < kMaxExtraFDs) {
40 extra_fds[next_extra_fd++] = temp_fd;
41 } else {
42 RAW_LOG(ERROR, "PerformInjectiveMultimapDestructive overflowed "
43 "extra_fds. Leaking file descriptors!");
44 }
37 } 45 }
38 46
39 j->source = temp_fd; 47 j->source = temp_fd;
40 j->close = false; 48 j->close = false;
41 } 49 }
42 50
43 if (i->close && i->source == j->dest) 51 if (i->close && i->source == j->dest)
44 i->close = false; 52 i->close = false;
45 53
46 if (i->close && i->source == j->source) { 54 if (i->close && i->source == j->source) {
47 i->close = false; 55 i->close = false;
48 j->close = true; 56 j->close = true;
49 } 57 }
50 } 58 }
51 59
52 if (!is_identity) { 60 if (!is_identity) {
53 if (!delegate->Move(i->source, i->dest)) 61 if (!delegate->Move(i->source, i->dest))
54 return false; 62 return false;
55 } 63 }
56 64
57 if (!is_identity && i->close) 65 if (!is_identity && i->close)
58 delegate->Close(i->source); 66 delegate->Close(i->source);
59 } 67 }
60 68
61 for (std::vector<int>::const_iterator 69 for (unsigned i = 0; i < next_extra_fd; i++)
62 i = extra_fds.begin(); i != extra_fds.end(); ++i) { 70 delegate->Close(extra_fds[i]);
63 delegate->Close(*i);
64 }
65 71
66 return true; 72 return true;
67 } 73 }
68 74
75 bool PerformInjectiveMultimap(const InjectiveMultimap& m_in,
76 InjectionDelegate* delegate) {
77 InjectiveMultimap m(m_in);
78 return PerformInjectiveMultimapDestructive(&m, delegate);
79 }
80
69 bool FileDescriptorTableInjection::Duplicate(int* result, int fd) { 81 bool FileDescriptorTableInjection::Duplicate(int* result, int fd) {
70 *result = HANDLE_EINTR(dup(fd)); 82 *result = HANDLE_EINTR(dup(fd));
71 return *result >= 0; 83 return *result >= 0;
72 } 84 }
73 85
74 bool FileDescriptorTableInjection::Move(int src, int dest) { 86 bool FileDescriptorTableInjection::Move(int src, int dest) {
75 return HANDLE_EINTR(dup2(src, dest)) != -1; 87 return HANDLE_EINTR(dup2(src, dest)) != -1;
76 } 88 }
77 89
78 void FileDescriptorTableInjection::Close(int fd) { 90 void FileDescriptorTableInjection::Close(int fd) {
79 int ret = HANDLE_EINTR(close(fd)); 91 int ret = HANDLE_EINTR(close(fd));
80 DPCHECK(ret == 0); 92 DPCHECK(ret == 0);
81 } 93 }
82 94
83 } // namespace base 95 } // namespace base
OLDNEW
« no previous file with comments | « base/file_descriptor_shuffle.h ('k') | base/process_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698