xref: /aosp_15_r20/external/cronet/base/posix/global_descriptors.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
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_GLOBAL_DESCRIPTORS_H_
6 #define BASE_POSIX_GLOBAL_DESCRIPTORS_H_
7 
8 #include <stdint.h>
9 
10 #include <vector>
11 #include <utility>
12 
13 #include "base/base_export.h"
14 #include "base/files/memory_mapped_file.h"
15 #include "base/files/scoped_file.h"
16 #include "base/memory/singleton.h"
17 
18 namespace base {
19 
20 // It's common practice to install file descriptors into well known slot
21 // numbers before execing a child; stdin, stdout and stderr are ubiqutous
22 // examples.
23 //
24 // However, when using a zygote model, this becomes troublesome. Since the
25 // descriptors which need to be in these slots generally aren't known, any code
26 // could open a resource and take one of the reserved descriptors. Simply
27 // overwriting the slot isn't a viable solution.
28 //
29 // We could try to fill the reserved slots as soon as possible, but this is a
30 // fragile solution since global constructors etc are able to open files.
31 //
32 // Instead, we retreat from the idea of installing descriptors in specific
33 // slots and add a layer of indirection in the form of this singleton object.
34 // It maps from an abstract key to a descriptor. If independent modules each
35 // need to define keys, then values should be chosen randomly so as not to
36 // collide.
37 //
38 // Note that this class is deprecated and passing file descriptor should ideally
39 // be done through the command line and using FileDescriptorStore.
40 // See https://crbugs.com/detail?id=692619
41 class BASE_EXPORT GlobalDescriptors {
42  public:
43   typedef uint32_t Key;
44   struct Descriptor {
45     Descriptor(Key key, int fd);
46     Descriptor(Key key, int fd, base::MemoryMappedFile::Region region);
47 
48     // Globally unique key.
49     Key key;
50     // Actual FD.
51     int fd;
52     // Optional region, defaults to kWholeFile.
53     base::MemoryMappedFile::Region region;
54   };
55   typedef std::vector<Descriptor> Mapping;
56 
57   // Often we want a canonical descriptor for a given Key. In this case, we add
58   // the following constant to the key value:
59   static const int kBaseDescriptor = 3;  // 0, 1, 2 are already taken.
60 
61   // Return the singleton instance of GlobalDescriptors.
62   static GlobalDescriptors* GetInstance();
63 
64   // Get a descriptor given a key. It is a fatal error if the key is not known.
65   int Get(Key key) const;
66 
67   // Get a descriptor given a key. Returns -1 on error.
68   int MaybeGet(Key key) const;
69 
70   // Returns a descriptor given a key and removes it from this class mappings.
71   // Also populates |region|.
72   // It is a fatal error if the key is not known.
73   base::ScopedFD TakeFD(Key key, base::MemoryMappedFile::Region* region);
74 
75   // Get a region given a key. It is a fatal error if the key is not known.
76   base::MemoryMappedFile::Region GetRegion(Key key) const;
77 
78   // Set the descriptor for the given |key|. This sets the region associated
79   // with |key| to kWholeFile.
80   void Set(Key key, int fd);
81 
82   // Set the descriptor and |region| for the given |key|.
83   void Set(Key key, int fd, base::MemoryMappedFile::Region region);
84 
85   void Reset(const Mapping& mapping);
86 
87  private:
88   friend struct DefaultSingletonTraits<GlobalDescriptors>;
89   GlobalDescriptors();
90   ~GlobalDescriptors();
91 
92   Mapping descriptors_;
93 };
94 
95 }  // namespace base
96 
97 #endif  // BASE_POSIX_GLOBAL_DESCRIPTORS_H_
98