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