OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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_GLOBAL_DESCRIPTORS_POSIX_H_ |
| 6 #define BASE_GLOBAL_DESCRIPTORS_POSIX_H_ |
| 7 |
| 8 #include "build/build_config.h" |
| 9 |
| 10 #include <vector> |
| 11 #include <utility> |
| 12 |
| 13 #include <stdint.h> |
| 14 |
| 15 #include "base/singleton.h" |
| 16 |
| 17 namespace base { |
| 18 |
| 19 // It's common practice to install file descriptors into well known slot |
| 20 // numbers before execing a child; stdin, stdout and stderr are ubiqutous |
| 21 // examples. |
| 22 // |
| 23 // However, when using a zygote model, this becomes troublesome. Since the |
| 24 // descriptors which need to be in these slots generally aren't known, any code |
| 25 // could open a resource and take one of the reserved descriptors. Simply |
| 26 // overwriting the slot isn't a viable solution. |
| 27 // |
| 28 // We could try to fill the reserved slots as soon as possible, but this is a |
| 29 // fragile solution since global constructors etc are able to open files. |
| 30 // |
| 31 // Instead, we retreat from the idea of installing descriptors in specific |
| 32 // slots and add a layer of indirection in the form of this singleton object. |
| 33 // It maps from an abstract key to a descriptor. If independent modules each |
| 34 // need to define keys, then values should be chosen randomly so as not to |
| 35 // collide. |
| 36 class GlobalDescriptors { |
| 37 public: |
| 38 typedef uint32_t Key; |
| 39 |
| 40 // Get a descriptor given a key. It is a fatal error if the key is not known. |
| 41 int Get(Key key) const; |
| 42 // Get a descriptor give a key. Returns -1 on error. |
| 43 int MaybeGet(Key key) const; |
| 44 |
| 45 typedef std::vector<std::pair<Key, int> > Mapping; |
| 46 |
| 47 // Set the descriptor for the given key. |
| 48 void Set(Key key, int fd); |
| 49 |
| 50 void Reset(const Mapping& mapping) { |
| 51 descriptors_ = mapping; |
| 52 } |
| 53 |
| 54 private: |
| 55 GlobalDescriptors() { } |
| 56 friend struct DefaultSingletonTraits<GlobalDescriptors>; |
| 57 |
| 58 Mapping descriptors_; |
| 59 }; |
| 60 |
| 61 } // namespace base |
| 62 |
| 63 #endif // BASE_GLOBAL_DESCRIPTORS_POSIX_H_ |
OLD | NEW |