michael@0: // Copyright (c) 2009 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #ifndef BASE_FILE_DESCRIPTOR_SHUFFLE_H_ michael@0: #define BASE_FILE_DESCRIPTOR_SHUFFLE_H_ michael@0: michael@0: // This code exists to perform the shuffling of file descriptors which is michael@0: // commonly needed when forking subprocesses. The naive approve is very simple, michael@0: // just call dup2 to setup the desired descriptors, but wrong. It's tough to michael@0: // handle the edge cases (like mapping 0 -> 1, 1 -> 0) correctly. michael@0: // michael@0: // In order to unittest this code, it's broken into the abstract action (an michael@0: // injective multimap) and the concrete code for dealing with file descriptors. michael@0: // Users should use the code like this: michael@0: // base::InjectiveMultimap file_descriptor_map; michael@0: // file_descriptor_map.push_back(base::InjectionArc(devnull, 0, true)); michael@0: // file_descriptor_map.push_back(base::InjectionArc(devnull, 2, true)); michael@0: // file_descriptor_map.push_back(base::InjectionArc(pipe[1], 1, true)); michael@0: // base::ShuffleFileDescriptors(file_descriptor_map); michael@0: // michael@0: // and trust the the Right Thing will get done. michael@0: michael@0: #include michael@0: michael@0: namespace base { michael@0: michael@0: // A Delegate which performs the actions required to perform an injective michael@0: // multimapping in place. michael@0: class InjectionDelegate { michael@0: public: michael@0: // Duplicate |fd|, an element of the domain, and write a fresh element of the michael@0: // domain into |result|. Returns true iff successful. michael@0: virtual bool Duplicate(int* result, int fd) = 0; michael@0: // Destructively move |src| to |dest|, overwriting |dest|. Returns true iff michael@0: // successful. michael@0: virtual bool Move(int src, int dest) = 0; michael@0: // Delete an element of the domain. michael@0: virtual void Close(int fd) = 0; michael@0: }; michael@0: michael@0: // An implementation of the InjectionDelegate interface using the file michael@0: // descriptor table of the current process as the domain. michael@0: class FileDescriptorTableInjection : public InjectionDelegate { michael@0: bool Duplicate(int* result, int fd); michael@0: bool Move(int src, int dest); michael@0: void Close(int fd); michael@0: }; michael@0: michael@0: // A single arc of the directed graph which describes an injective multimapping. michael@0: struct InjectionArc { michael@0: InjectionArc(int in_source, int in_dest, bool in_close) michael@0: : source(in_source), michael@0: dest(in_dest), michael@0: close(in_close) { michael@0: } michael@0: michael@0: int source; michael@0: int dest; michael@0: bool close; // if true, delete the source element after performing the michael@0: // mapping. michael@0: }; michael@0: michael@0: typedef std::vector InjectiveMultimap; michael@0: michael@0: bool PerformInjectiveMultimap(const InjectiveMultimap& map, michael@0: InjectionDelegate* delegate); michael@0: bool PerformInjectiveMultimapDestructive(InjectiveMultimap* map, michael@0: InjectionDelegate* delegate); michael@0: michael@0: // This function will not call malloc but will mutate |map| michael@0: static inline bool ShuffleFileDescriptors(InjectiveMultimap *map) { michael@0: FileDescriptorTableInjection delegate; michael@0: return PerformInjectiveMultimapDestructive(map, &delegate); michael@0: } michael@0: michael@0: } // namespace base michael@0: michael@0: #endif // !BASE_FILE_DESCRIPTOR_SHUFFLE_H_