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