Wed, 31 Dec 2014 13:27:57 +0100
Ignore runtime configuration files generated during quality assurance.
michael@0 | 1 | // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #ifndef CHROME_COMMON_FILE_DESCRIPTOR_SET_POSIX_H_ |
michael@0 | 6 | #define CHROME_COMMON_FILE_DESCRIPTOR_SET_POSIX_H_ |
michael@0 | 7 | |
michael@0 | 8 | #include <vector> |
michael@0 | 9 | |
michael@0 | 10 | #include "base/basictypes.h" |
michael@0 | 11 | #include "base/file_descriptor_posix.h" |
michael@0 | 12 | #include "base/ref_counted.h" |
michael@0 | 13 | |
michael@0 | 14 | // ----------------------------------------------------------------------------- |
michael@0 | 15 | // A FileDescriptorSet is an ordered set of POSIX file descriptors. These are |
michael@0 | 16 | // associated with IPC messages so that descriptors can be transmitted over a |
michael@0 | 17 | // UNIX domain socket. |
michael@0 | 18 | // ----------------------------------------------------------------------------- |
michael@0 | 19 | class FileDescriptorSet : public base::RefCountedThreadSafe<FileDescriptorSet> { |
michael@0 | 20 | public: |
michael@0 | 21 | FileDescriptorSet(); |
michael@0 | 22 | ~FileDescriptorSet(); |
michael@0 | 23 | |
michael@0 | 24 | // This is the maximum number of descriptors per message. We need to know this |
michael@0 | 25 | // because the control message kernel interface has to be given a buffer which |
michael@0 | 26 | // is large enough to store all the descriptor numbers. Otherwise the kernel |
michael@0 | 27 | // tells us that it truncated the control data and the extra descriptors are |
michael@0 | 28 | // lost. |
michael@0 | 29 | // |
michael@0 | 30 | // In debugging mode, it's a fatal error to try and add more than this number |
michael@0 | 31 | // of descriptors to a FileDescriptorSet. |
michael@0 | 32 | enum { |
michael@0 | 33 | MAX_DESCRIPTORS_PER_MESSAGE = 4 |
michael@0 | 34 | }; |
michael@0 | 35 | |
michael@0 | 36 | // --------------------------------------------------------------------------- |
michael@0 | 37 | // Interfaces for building during message serialisation... |
michael@0 | 38 | |
michael@0 | 39 | // Add a descriptor to the end of the set. Returns false iff the set is full. |
michael@0 | 40 | bool Add(int fd); |
michael@0 | 41 | // Add a descriptor to the end of the set and automatically close it after |
michael@0 | 42 | // transmission. Returns false iff the set is full. |
michael@0 | 43 | bool AddAndAutoClose(int fd); |
michael@0 | 44 | |
michael@0 | 45 | // --------------------------------------------------------------------------- |
michael@0 | 46 | |
michael@0 | 47 | |
michael@0 | 48 | // --------------------------------------------------------------------------- |
michael@0 | 49 | // Interfaces for accessing during message deserialisation... |
michael@0 | 50 | |
michael@0 | 51 | // Return the number of descriptors |
michael@0 | 52 | unsigned size() const { return descriptors_.size(); } |
michael@0 | 53 | // Return true if no unconsumed descriptors remain |
michael@0 | 54 | bool empty() const { return descriptors_.empty(); } |
michael@0 | 55 | // Fetch the nth descriptor from the beginning of the set. Code using this |
michael@0 | 56 | // /must/ access the descriptors in order, except that it may wrap from the |
michael@0 | 57 | // end to index 0 again. |
michael@0 | 58 | // |
michael@0 | 59 | // This interface is designed for the deserialising code as it doesn't |
michael@0 | 60 | // support close flags. |
michael@0 | 61 | // returns: file descriptor, or -1 on error |
michael@0 | 62 | int GetDescriptorAt(unsigned n) const; |
michael@0 | 63 | |
michael@0 | 64 | // --------------------------------------------------------------------------- |
michael@0 | 65 | |
michael@0 | 66 | |
michael@0 | 67 | // --------------------------------------------------------------------------- |
michael@0 | 68 | // Interfaces for transmission... |
michael@0 | 69 | |
michael@0 | 70 | // Fill an array with file descriptors without 'consuming' them. CommitAll |
michael@0 | 71 | // must be called after these descriptors have been transmitted. |
michael@0 | 72 | // buffer: (output) a buffer of, at least, size() integers. |
michael@0 | 73 | void GetDescriptors(int* buffer) const; |
michael@0 | 74 | // This must be called after transmitting the descriptors returned by |
michael@0 | 75 | // GetDescriptors. It marks all the descriptors as consumed and closes those |
michael@0 | 76 | // which are auto-close. |
michael@0 | 77 | void CommitAll(); |
michael@0 | 78 | |
michael@0 | 79 | // --------------------------------------------------------------------------- |
michael@0 | 80 | |
michael@0 | 81 | |
michael@0 | 82 | // --------------------------------------------------------------------------- |
michael@0 | 83 | // Interfaces for receiving... |
michael@0 | 84 | |
michael@0 | 85 | // Set the contents of the set from the given buffer. This set must be empty |
michael@0 | 86 | // before calling. The auto-close flag is set on all the descriptors so that |
michael@0 | 87 | // unconsumed descriptors are closed on destruction. |
michael@0 | 88 | void SetDescriptors(const int* buffer, unsigned count); |
michael@0 | 89 | |
michael@0 | 90 | // --------------------------------------------------------------------------- |
michael@0 | 91 | |
michael@0 | 92 | private: |
michael@0 | 93 | // A vector of descriptors and close flags. If this message is sent, then |
michael@0 | 94 | // these descriptors are sent as control data. After sending, any descriptors |
michael@0 | 95 | // with a true flag are closed. If this message has been received, then these |
michael@0 | 96 | // are the descriptors which were received and all close flags are true. |
michael@0 | 97 | std::vector<base::FileDescriptor> descriptors_; |
michael@0 | 98 | |
michael@0 | 99 | // This contains the index of the next descriptor which should be consumed. |
michael@0 | 100 | // It's used in a couple of ways. Firstly, at destruction we can check that |
michael@0 | 101 | // all the descriptors have been read (with GetNthDescriptor). Secondly, we |
michael@0 | 102 | // can check that they are read in order. |
michael@0 | 103 | mutable unsigned consumed_descriptor_highwater_; |
michael@0 | 104 | |
michael@0 | 105 | DISALLOW_COPY_AND_ASSIGN(FileDescriptorSet); |
michael@0 | 106 | }; |
michael@0 | 107 | |
michael@0 | 108 | #endif // CHROME_COMMON_FILE_DESCRIPTOR_SET_POSIX_H_ |