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
3 // found in the LICENSE file.
4
5 #ifndef BASE_POSIX_FILE_DESCRIPTOR_SHUFFLE_H_
6 #define BASE_POSIX_FILE_DESCRIPTOR_SHUFFLE_H_
7
8 // This code exists to shuffle file descriptors, which is commonly needed when
9 // forking subprocesses. The naive approach (just call dup2 to set up the
10 // desired descriptors) is very simple, but wrong: it won't handle edge cases
11 // (like mapping 0 -> 1, 1 -> 0) correctly.
12 //
13 // In order to unittest this code, it's broken into the abstract action (an
14 // injective multimap) and the concrete code for dealing with file descriptors.
15 // Users should use the code like this:
16 // base::InjectiveMultimap file_descriptor_map;
17 // file_descriptor_map.push_back(base::InjectionArc(devnull, 0, true));
18 // file_descriptor_map.push_back(base::InjectionArc(devnull, 2, true));
19 // file_descriptor_map.push_back(base::InjectionArc(pipe[1], 1, true));
20 // base::ShuffleFileDescriptors(file_descriptor_map);
21 //
22 // and trust the the Right Thing will get done.
23
24 #include <vector>
25
26 #include "base/compiler_specific.h"
27
28 namespace base {
29
30 // A Delegate which performs the actions required to perform an injective
31 // multimapping in place.
32 class InjectionDelegate {
33 public:
34 // Duplicate |fd|, an element of the domain, and write a fresh element of the
35 // domain into |result|. Returns true iff successful.
36 virtual bool Duplicate(int* result, int fd) = 0;
37 // Destructively move |src| to |dest|, overwriting |dest|. Returns true iff
38 // successful.
39 virtual bool Move(int src, int dest) = 0;
40 // Delete an element of the domain.
41 virtual void Close(int fd) = 0;
42
43 protected:
44 virtual ~InjectionDelegate() = default;
45 };
46
47 // An implementation of the InjectionDelegate interface using the file
48 // descriptor table of the current process as the domain.
49 class FileDescriptorTableInjection : public InjectionDelegate {
50 bool Duplicate(int* result, int fd) override;
51 bool Move(int src, int dest) override;
52 void Close(int fd) override;
53 };
54
55 // A single arc of the directed graph which describes an injective multimapping.
56 struct InjectionArc {
InjectionArcInjectionArc57 InjectionArc(int in_source, int in_dest, bool in_close)
58 : source(in_source), dest(in_dest), close(in_close) {}
59
60 int source;
61 int dest;
62 bool close; // if true, delete the source element after performing the
63 // mapping.
64 };
65
66 typedef std::vector<InjectionArc> InjectiveMultimap;
67
68 bool PerformInjectiveMultimap(const InjectiveMultimap& map,
69 InjectionDelegate* delegate);
70
71 bool PerformInjectiveMultimapDestructive(InjectiveMultimap* map,
72 InjectionDelegate* delegate);
73
74 // This function will not call malloc but will mutate |map|
ShuffleFileDescriptors(InjectiveMultimap * map)75 static inline bool ShuffleFileDescriptors(InjectiveMultimap* map) {
76 FileDescriptorTableInjection delegate;
77 return PerformInjectiveMultimapDestructive(map, &delegate);
78 }
79
80 } // namespace base
81
82 #endif // BASE_POSIX_FILE_DESCRIPTOR_SHUFFLE_H_
83