global_descriptors.h 3.3 KB

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