Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #ifndef BASE_FILE_DESCRIPTOR_SHUFFLE_H_ |
michael@0 | 6 | #define BASE_FILE_DESCRIPTOR_SHUFFLE_H_ |
michael@0 | 7 | |
michael@0 | 8 | // This code exists to perform the shuffling of file descriptors which is |
michael@0 | 9 | // commonly needed when forking subprocesses. The naive approve is very simple, |
michael@0 | 10 | // just call dup2 to setup the desired descriptors, but wrong. It's tough to |
michael@0 | 11 | // handle the edge cases (like mapping 0 -> 1, 1 -> 0) correctly. |
michael@0 | 12 | // |
michael@0 | 13 | // In order to unittest this code, it's broken into the abstract action (an |
michael@0 | 14 | // injective multimap) and the concrete code for dealing with file descriptors. |
michael@0 | 15 | // Users should use the code like this: |
michael@0 | 16 | // base::InjectiveMultimap file_descriptor_map; |
michael@0 | 17 | // file_descriptor_map.push_back(base::InjectionArc(devnull, 0, true)); |
michael@0 | 18 | // file_descriptor_map.push_back(base::InjectionArc(devnull, 2, true)); |
michael@0 | 19 | // file_descriptor_map.push_back(base::InjectionArc(pipe[1], 1, true)); |
michael@0 | 20 | // base::ShuffleFileDescriptors(file_descriptor_map); |
michael@0 | 21 | // |
michael@0 | 22 | // and trust the the Right Thing will get done. |
michael@0 | 23 | |
michael@0 | 24 | #include <vector> |
michael@0 | 25 | |
michael@0 | 26 | namespace base { |
michael@0 | 27 | |
michael@0 | 28 | // A Delegate which performs the actions required to perform an injective |
michael@0 | 29 | // multimapping in place. |
michael@0 | 30 | class InjectionDelegate { |
michael@0 | 31 | public: |
michael@0 | 32 | // Duplicate |fd|, an element of the domain, and write a fresh element of the |
michael@0 | 33 | // domain into |result|. Returns true iff successful. |
michael@0 | 34 | virtual bool Duplicate(int* result, int fd) = 0; |
michael@0 | 35 | // Destructively move |src| to |dest|, overwriting |dest|. Returns true iff |
michael@0 | 36 | // successful. |
michael@0 | 37 | virtual bool Move(int src, int dest) = 0; |
michael@0 | 38 | // Delete an element of the domain. |
michael@0 | 39 | virtual void Close(int fd) = 0; |
michael@0 | 40 | }; |
michael@0 | 41 | |
michael@0 | 42 | // An implementation of the InjectionDelegate interface using the file |
michael@0 | 43 | // descriptor table of the current process as the domain. |
michael@0 | 44 | class FileDescriptorTableInjection : public InjectionDelegate { |
michael@0 | 45 | bool Duplicate(int* result, int fd); |
michael@0 | 46 | bool Move(int src, int dest); |
michael@0 | 47 | void Close(int fd); |
michael@0 | 48 | }; |
michael@0 | 49 | |
michael@0 | 50 | // A single arc of the directed graph which describes an injective multimapping. |
michael@0 | 51 | struct InjectionArc { |
michael@0 | 52 | InjectionArc(int in_source, int in_dest, bool in_close) |
michael@0 | 53 | : source(in_source), |
michael@0 | 54 | dest(in_dest), |
michael@0 | 55 | close(in_close) { |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | int source; |
michael@0 | 59 | int dest; |
michael@0 | 60 | bool close; // if true, delete the source element after performing the |
michael@0 | 61 | // mapping. |
michael@0 | 62 | }; |
michael@0 | 63 | |
michael@0 | 64 | typedef std::vector<InjectionArc> InjectiveMultimap; |
michael@0 | 65 | |
michael@0 | 66 | bool PerformInjectiveMultimap(const InjectiveMultimap& map, |
michael@0 | 67 | InjectionDelegate* delegate); |
michael@0 | 68 | bool PerformInjectiveMultimapDestructive(InjectiveMultimap* map, |
michael@0 | 69 | InjectionDelegate* delegate); |
michael@0 | 70 | |
michael@0 | 71 | // This function will not call malloc but will mutate |map| |
michael@0 | 72 | static inline bool ShuffleFileDescriptors(InjectiveMultimap *map) { |
michael@0 | 73 | FileDescriptorTableInjection delegate; |
michael@0 | 74 | return PerformInjectiveMultimapDestructive(map, &delegate); |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | } // namespace base |
michael@0 | 78 | |
michael@0 | 79 | #endif // !BASE_FILE_DESCRIPTOR_SHUFFLE_H_ |