OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef BASE_FILE_DESCRIPTOR_SHUFFLE_H_ | 5 #ifndef BASE_FILE_DESCRIPTOR_SHUFFLE_H_ |
6 #define BASE_FILE_DESCRIPTOR_SHUFFLE_H_ | 6 #define BASE_FILE_DESCRIPTOR_SHUFFLE_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 // This code exists to perform the shuffling of file descriptors which is | 9 // This code exists to perform the shuffling of file descriptors which is |
10 // commonly needed when forking subprocesses. The naive approve is very simple, | 10 // commonly needed when forking subprocesses. The naive approve is very simple, |
11 // just call dup2 to setup the desired descriptors, but wrong. It's tough to | 11 // just call dup2 to setup the desired descriptors, but wrong. It's tough to |
12 // handle the edge cases (like mapping 0 -> 1, 1 -> 0) correctly. | 12 // handle the edge cases (like mapping 0 -> 1, 1 -> 0) correctly. |
13 // | 13 // |
14 // In order to unittest this code, it's broken into the abstract action (an | 14 // In order to unittest this code, it's broken into the abstract action (an |
15 // injective multimap) and the concrete code for dealing with file descriptors. | 15 // injective multimap) and the concrete code for dealing with file descriptors. |
16 // Users should use the code like this: | 16 // Users should use the code like this: |
17 // base::InjectiveMultimap file_descriptor_map; | 17 // base::InjectiveMultimap file_descriptor_map; |
18 // file_descriptor_map.push_back(base::InjectionArc(devnull, 0, true)); | 18 // file_descriptor_map.push_back(base::InjectionArc(devnull, 0, true)); |
19 // file_descriptor_map.push_back(base::InjectionArc(devnull, 2, true)); | 19 // file_descriptor_map.push_back(base::InjectionArc(devnull, 2, true)); |
20 // file_descriptor_map.push_back(base::InjectionArc(pipe[1], 1, true)); | 20 // file_descriptor_map.push_back(base::InjectionArc(pipe[1], 1, true)); |
21 // base::ShuffleFileDescriptors(file_descriptor_map); | 21 // base::ShuffleFileDescriptors(file_descriptor_map); |
22 // | 22 // |
23 // and trust the the Right Thing will get done. | 23 // and trust the the Right Thing will get done. |
24 | 24 |
25 #include <vector> | 25 #include <vector> |
26 | 26 |
27 #include "base/base_api.h" | 27 #include "base/base_export.h" |
28 | 28 |
29 namespace base { | 29 namespace base { |
30 | 30 |
31 // A Delegate which performs the actions required to perform an injective | 31 // A Delegate which performs the actions required to perform an injective |
32 // multimapping in place. | 32 // multimapping in place. |
33 class InjectionDelegate { | 33 class InjectionDelegate { |
34 public: | 34 public: |
35 // Duplicate |fd|, an element of the domain, and write a fresh element of the | 35 // Duplicate |fd|, an element of the domain, and write a fresh element of the |
36 // domain into |result|. Returns true iff successful. | 36 // domain into |result|. Returns true iff successful. |
37 virtual bool Duplicate(int* result, int fd) = 0; | 37 virtual bool Duplicate(int* result, int fd) = 0; |
(...skipping 24 matching lines...) Expand all Loading... |
62 } | 62 } |
63 | 63 |
64 int source; | 64 int source; |
65 int dest; | 65 int dest; |
66 bool close; // if true, delete the source element after performing the | 66 bool close; // if true, delete the source element after performing the |
67 // mapping. | 67 // mapping. |
68 }; | 68 }; |
69 | 69 |
70 typedef std::vector<InjectionArc> InjectiveMultimap; | 70 typedef std::vector<InjectionArc> InjectiveMultimap; |
71 | 71 |
72 BASE_API bool PerformInjectiveMultimap(const InjectiveMultimap& map, | 72 BASE_EXPORT bool PerformInjectiveMultimap(const InjectiveMultimap& map, |
73 InjectionDelegate* delegate); | 73 InjectionDelegate* delegate); |
74 | 74 |
75 BASE_API bool PerformInjectiveMultimapDestructive(InjectiveMultimap* map, | 75 BASE_EXPORT bool PerformInjectiveMultimapDestructive( |
76 InjectionDelegate* delegate); | 76 InjectiveMultimap* map, |
| 77 InjectionDelegate* delegate); |
77 | 78 |
78 // This function will not call malloc but will mutate |map| | 79 // This function will not call malloc but will mutate |map| |
79 static inline bool ShuffleFileDescriptors(InjectiveMultimap* map) { | 80 static inline bool ShuffleFileDescriptors(InjectiveMultimap* map) { |
80 FileDescriptorTableInjection delegate; | 81 FileDescriptorTableInjection delegate; |
81 return PerformInjectiveMultimapDestructive(map, &delegate); | 82 return PerformInjectiveMultimapDestructive(map, &delegate); |
82 } | 83 } |
83 | 84 |
84 } // namespace base | 85 } // namespace base |
85 | 86 |
86 #endif // BASE_FILE_DESCRIPTOR_SHUFFLE_H_ | 87 #endif // BASE_FILE_DESCRIPTOR_SHUFFLE_H_ |
OLD | NEW |