Chromium Code Reviews| 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. |
| 37 class BASE_EXPORT GlobalDescriptors { | 38 class BASE_EXPORT GlobalDescriptors { |
|
dcheng
2017/02/15 08:05:27
Note that this is deprecated and new code should u
Jay Civelli
2017/02/15 19:53:47
Done.
| |
| 38 public: | 39 public: |
| 39 typedef uint32_t Key; | 40 typedef uint32_t Key; |
| 40 struct Descriptor { | 41 struct Descriptor { |
| 41 Descriptor(Key key, int fd); | 42 Descriptor(Key key, int fd); |
| 42 Descriptor(Key key, int fd, base::MemoryMappedFile::Region region); | 43 Descriptor(Key key, int fd, base::MemoryMappedFile::Region region); |
| 43 | 44 |
| 44 // Globally unique key. | 45 // Globally unique key. |
| 45 Key key; | 46 Key key; |
| 46 // Actual FD. | 47 // Actual FD. |
| 47 int fd; | 48 int fd; |
| 48 // Optional region, defaults to kWholeFile. | 49 // Optional region, defaults to kWholeFile. |
| 49 base::MemoryMappedFile::Region region; | 50 base::MemoryMappedFile::Region region; |
| 50 }; | 51 }; |
| 51 typedef std::vector<Descriptor> Mapping; | 52 typedef std::vector<Descriptor> Mapping; |
| 52 | 53 |
| 53 // Often we want a canonical descriptor for a given Key. In this case, we add | 54 // Often we want a canonical descriptor for a given Key. In this case, we add |
| 54 // the following constant to the key value: | 55 // the following constant to the key value: |
| 55 static const int kBaseDescriptor = 3; // 0, 1, 2 are already taken. | 56 static const int kBaseDescriptor = 3; // 0, 1, 2 are already taken. |
| 56 | 57 |
| 57 // Return the singleton instance of GlobalDescriptors. | 58 // Return the singleton instance of GlobalDescriptors. |
| 58 static GlobalDescriptors* GetInstance(); | 59 static GlobalDescriptors* GetInstance(); |
| 59 | 60 |
| 60 // Get a descriptor given a key. It is a fatal error if the key is not known. | 61 // Get a descriptor given a key. It is a fatal error if the key is not known. |
| 61 int Get(Key key) const; | 62 int Get(Key key) const; |
| 62 | 63 |
| 63 // Get a descriptor given a key. Returns -1 on error. | 64 // Get a descriptor given a key. Returns -1 on error. |
| 64 int MaybeGet(Key key) const; | 65 int MaybeGet(Key key) const; |
| 65 | 66 |
| 67 // Returns a descriptor given a key and removes it from this class mappings. | |
| 68 // Also populates |region| if it's not null. | |
|
dcheng
2017/02/15 08:05:27
I might be missing something, but it seems like no
Jay Civelli
2017/02/15 19:53:47
Done.
| |
| 69 // It is a fatal error if the key is not known. | |
| 70 base::ScopedFD TakeFD(Key key, base::MemoryMappedFile::Region* region); | |
| 71 | |
| 66 // Get a region given a key. It is a fatal error if the key is not known. | 72 // 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; | 73 base::MemoryMappedFile::Region GetRegion(Key key) const; |
| 68 | 74 |
| 69 // Set the descriptor for the given |key|. This sets the region associated | 75 // Set the descriptor for the given |key|. This sets the region associated |
| 70 // with |key| to kWholeFile. | 76 // with |key| to kWholeFile. |
| 71 void Set(Key key, int fd); | 77 void Set(Key key, int fd); |
| 72 | 78 |
| 73 // Set the descriptor and |region| for the given |key|. | 79 // Set the descriptor and |region| for the given |key|. |
| 74 void Set(Key key, int fd, base::MemoryMappedFile::Region region); | 80 void Set(Key key, int fd, base::MemoryMappedFile::Region region); |
| 75 | 81 |
| 76 void Reset(const Mapping& mapping); | 82 void Reset(const Mapping& mapping); |
| 77 | 83 |
| 78 private: | 84 private: |
| 79 friend struct DefaultSingletonTraits<GlobalDescriptors>; | 85 friend struct DefaultSingletonTraits<GlobalDescriptors>; |
| 80 GlobalDescriptors(); | 86 GlobalDescriptors(); |
| 81 ~GlobalDescriptors(); | 87 ~GlobalDescriptors(); |
| 82 | 88 |
| 83 Mapping descriptors_; | 89 Mapping descriptors_; |
| 84 }; | 90 }; |
| 85 | 91 |
| 86 } // namespace base | 92 } // namespace base |
| 87 | 93 |
| 88 #endif // BASE_POSIX_GLOBAL_DESCRIPTORS_H_ | 94 #endif // BASE_POSIX_GLOBAL_DESCRIPTORS_H_ |
| OLD | NEW |