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 3 // found in the LICENSE file. 4 5 #ifndef BASE_POSIX_GLOBAL_DESCRIPTORS_H_ 6 #define BASE_POSIX_GLOBAL_DESCRIPTORS_H_ 7 8 #include "build/build_config.h" 9 10 #include <vector> 11 #include <utility> 12 13 #include <stdint.h> 14 15 #include "base/files/memory_mapped_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 class BASE_EXPORT GlobalDescriptors { 38 public: 39 typedef uint32_t Key; 40 struct Descriptor { 41 Descriptor(Key key, int fd); 42 Descriptor(Key key, int fd, base::MemoryMappedFile::Region region); 43 44 // Globally unique key. 45 Key key; 46 // Actual FD. 47 int fd; 48 // Optional region, defaults to kWholeFile. 49 base::MemoryMappedFile::Region region; 50 }; 51 typedef std::vector<Descriptor> Mapping; 52 53 // Often we want a canonical descriptor for a given Key. In this case, we add 54 // the following constant to the key value: 55 #if !defined(OS_ANDROID) 56 static const int kBaseDescriptor = 3; // 0, 1, 2 are already taken. 57 #else 58 // 3 used by __android_log_write(). 59 // 4 used by... something important on Android M. 60 // 5 used by... something important on Android L... on low-end devices. 61 // TODO(amistry): An Android, this mechanism is only used for tests since the 62 // content child launcher spawns a process by creating a new Activity using 63 // the Android APIs. For tests, come up with a way that doesn't require using 64 // a pre-defined fd. 65 static const int kBaseDescriptor = 6; 66 #endif 67 68 // Return the singleton instance of GlobalDescriptors. 69 static GlobalDescriptors* GetInstance(); 70 71 // Get a descriptor given a key. It is a fatal error if the key is not known. 72 int Get(Key key) const; 73 74 // Get a descriptor given a key. Returns -1 on error. 75 int MaybeGet(Key key) const; 76 77 // Get a region given a key. It is a fatal error if the key is not known. 78 base::MemoryMappedFile::Region GetRegion(Key key) const; 79 80 // Set the descriptor for the given |key|. This sets the region associated 81 // with |key| to kWholeFile. 82 void Set(Key key, int fd); 83 84 // Set the descriptor and |region| for the given |key|. 85 void Set(Key key, int fd, base::MemoryMappedFile::Region region); 86 87 void Reset(const Mapping& mapping); 88 89 private: 90 friend struct DefaultSingletonTraits<GlobalDescriptors>; 91 GlobalDescriptors(); 92 ~GlobalDescriptors(); 93 94 Mapping descriptors_; 95 }; 96 97 } // namespace base 98 99 #endif // BASE_POSIX_GLOBAL_DESCRIPTORS_H_ 100