OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 IPC_FILE_DESCRIPTOR_SET_POSIX_H_ | 5 #ifndef IPC_FILE_DESCRIPTOR_SET_POSIX_H_ |
6 #define IPC_FILE_DESCRIPTOR_SET_POSIX_H_ | 6 #define IPC_FILE_DESCRIPTOR_SET_POSIX_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/file_descriptor_posix.h" | 11 #include "base/files/file.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_vector.h" |
13 #include "ipc/ipc_export.h" | 14 #include "ipc/ipc_export.h" |
14 | 15 |
15 // ----------------------------------------------------------------------------- | 16 // ----------------------------------------------------------------------------- |
16 // A FileDescriptorSet is an ordered set of POSIX file descriptors. These are | 17 // A FileDescriptorSet is an ordered set of POSIX file descriptors. These are |
17 // associated with IPC messages so that descriptors can be transmitted over a | 18 // associated with IPC messages so that descriptors can be transmitted over a |
18 // UNIX domain socket. | 19 // UNIX domain socket. |
19 // ----------------------------------------------------------------------------- | 20 // ----------------------------------------------------------------------------- |
20 class IPC_EXPORT FileDescriptorSet | 21 class IPC_EXPORT FileDescriptorSet |
21 : public base::RefCountedThreadSafe<FileDescriptorSet> { | 22 : public base::RefCountedThreadSafe<FileDescriptorSet> { |
22 public: | 23 public: |
23 FileDescriptorSet(); | 24 FileDescriptorSet(); |
24 | 25 |
25 // This is the maximum number of descriptors per message. We need to know this | 26 // This is the maximum number of descriptors per message. We need to know this |
26 // because the control message kernel interface has to be given a buffer which | 27 // because the control message kernel interface has to be given a buffer which |
27 // is large enough to store all the descriptor numbers. Otherwise the kernel | 28 // is large enough to store all the descriptor numbers. Otherwise the kernel |
28 // tells us that it truncated the control data and the extra descriptors are | 29 // tells us that it truncated the control data and the extra descriptors are |
29 // lost. | 30 // lost. |
30 // | 31 // |
31 // In debugging mode, it's a fatal error to try and add more than this number | 32 // In debugging mode, it's a fatal error to try and add more than this number |
32 // of descriptors to a FileDescriptorSet. | 33 // of descriptors to a FileDescriptorSet. |
33 static const size_t kMaxDescriptorsPerMessage = 7; | 34 static const size_t kMaxDescriptorsPerMessage = 7; |
34 | 35 |
35 // --------------------------------------------------------------------------- | 36 // --------------------------------------------------------------------------- |
36 // Interfaces for building during message serialisation... | 37 // Interfaces for building during message serialisation... |
37 | 38 |
38 // Add a descriptor to the end of the set. Returns false iff the set is full. | 39 // Add a descriptor to the end of the set. Returns false iff the set is full. |
39 bool Add(int fd); | 40 bool AddToBorrow(base::PlatformFile fd); |
40 // Add a descriptor to the end of the set and automatically close it after | 41 // Add a descriptor to the end of the set and automatically close it after |
41 // transmission. Returns false iff the set is full. | 42 // transmission. Returns false iff the set is full. |
42 bool AddAndAutoClose(int fd); | 43 bool AddToOwn(base::ScopedFD fd); |
43 | 44 |
44 // --------------------------------------------------------------------------- | 45 // --------------------------------------------------------------------------- |
45 | 46 |
46 | 47 |
47 // --------------------------------------------------------------------------- | 48 // --------------------------------------------------------------------------- |
48 // Interfaces for accessing during message deserialisation... | 49 // Interfaces for accessing during message deserialisation... |
49 | 50 |
50 // Return the number of descriptors | 51 // Return the number of descriptors |
51 unsigned size() const { return descriptors_.size(); } | 52 unsigned size() const { return descriptors_.size(); } |
52 // Return true if no unconsumed descriptors remain | 53 // Return true if no unconsumed descriptors remain |
53 bool empty() const { return descriptors_.empty(); } | 54 bool empty() const { return 0 == size(); } |
54 // Fetch the nth descriptor from the beginning of the set. Code using this | 55 // Take the nth descriptor from the beginning of the set, |
55 // /must/ access the descriptors in order, except that it may wrap from the | 56 // transferring the ownership of the descriptor taken. Code using this |
56 // end to index 0 again. | 57 // /must/ access the descriptors in order, and must do it at most once. |
57 // | 58 // |
58 // This interface is designed for the deserialising code as it doesn't | 59 // This interface is designed for the deserialising code as it doesn't |
59 // support close flags. | 60 // support close flags. |
60 // returns: file descriptor, or -1 on error | 61 // returns: file descriptor, or -1 on error |
61 int GetDescriptorAt(unsigned n) const; | 62 base::PlatformFile TakeDescriptorAt(unsigned n); |
62 | 63 |
63 // --------------------------------------------------------------------------- | 64 // --------------------------------------------------------------------------- |
64 | 65 |
65 | 66 |
66 // --------------------------------------------------------------------------- | 67 // --------------------------------------------------------------------------- |
67 // Interfaces for transmission... | 68 // Interfaces for transmission... |
68 | 69 |
69 // Fill an array with file descriptors without 'consuming' them. CommitAll | 70 // Fill an array with file descriptors without 'consuming' them. CommitAll |
70 // must be called after these descriptors have been transmitted. | 71 // must be called after these descriptors have been transmitted. |
71 // buffer: (output) a buffer of, at least, size() integers. | 72 // buffer: (output) a buffer of, at least, size() integers. |
72 void GetDescriptors(int* buffer) const; | 73 void PeekDescriptors(base::PlatformFile* buffer) const; |
73 // This must be called after transmitting the descriptors returned by | 74 // This must be called after transmitting the descriptors returned by |
74 // GetDescriptors. It marks all the descriptors as consumed and closes those | 75 // PeekDescriptors. It marks all the descriptors as consumed and closes those |
75 // which are auto-close. | 76 // which are auto-close. |
76 void CommitAll(); | 77 void CommitAll(); |
77 // Returns true if any contained file descriptors appear to be handles to a | 78 // Returns true if any contained file descriptors appear to be handles to a |
78 // directory. | 79 // directory. |
79 bool ContainsDirectoryDescriptor() const; | 80 bool ContainsDirectoryDescriptor() const; |
80 // Fetch all filedescriptors with the "auto close" property. | 81 // Fetch all filedescriptors with the "auto close" property. |
81 // Used instead of CommitAll() when closing must be handled manually. | 82 // Used instead of CommitAll() when closing must be handled manually. |
82 void ReleaseFDsToClose(std::vector<int>* fds); | 83 void ReleaseFDsToClose(std::vector<base::PlatformFile>* fds); |
83 | 84 |
84 // --------------------------------------------------------------------------- | 85 // --------------------------------------------------------------------------- |
85 | 86 |
86 | 87 |
87 // --------------------------------------------------------------------------- | 88 // --------------------------------------------------------------------------- |
88 // Interfaces for receiving... | 89 // Interfaces for receiving... |
89 | 90 |
90 // Set the contents of the set from the given buffer. This set must be empty | 91 // Set the contents of the set from the given buffer. This set must be empty |
91 // before calling. The auto-close flag is set on all the descriptors so that | 92 // before calling. The auto-close flag is set on all the descriptors so that |
92 // unconsumed descriptors are closed on destruction. | 93 // unconsumed descriptors are closed on destruction. |
93 void SetDescriptors(const int* buffer, unsigned count); | 94 void AddDescriptorsToOwn(const base::PlatformFile* buffer, unsigned count); |
94 | 95 |
95 // --------------------------------------------------------------------------- | 96 // --------------------------------------------------------------------------- |
96 | 97 |
97 private: | 98 private: |
98 friend class base::RefCountedThreadSafe<FileDescriptorSet>; | 99 friend class base::RefCountedThreadSafe<FileDescriptorSet>; |
99 | 100 |
100 ~FileDescriptorSet(); | 101 ~FileDescriptorSet(); |
101 | 102 |
102 // A vector of descriptors and close flags. If this message is sent, then | 103 // A vector of descriptors and close flags. If this message is sent, then |
103 // these descriptors are sent as control data. After sending, any descriptors | 104 // these descriptors are sent as control data. After sending, any descriptors |
104 // with a true flag are closed. If this message has been received, then these | 105 // with a true flag are closed. If this message has been received, then these |
105 // are the descriptors which were received and all close flags are true. | 106 // are the descriptors which were received and all close flags are true. |
106 std::vector<base::FileDescriptor> descriptors_; | 107 std::vector<base::PlatformFile> descriptors_; |
| 108 ScopedVector<base::ScopedFD> owned_descriptors_; |
107 | 109 |
108 // This contains the index of the next descriptor which should be consumed. | 110 // This contains the index of the next descriptor which should be consumed. |
109 // It's used in a couple of ways. Firstly, at destruction we can check that | 111 // It's used in a couple of ways. Firstly, at destruction we can check that |
110 // all the descriptors have been read (with GetNthDescriptor). Secondly, we | 112 // all the descriptors have been read (with GetNthDescriptor). Secondly, we |
111 // can check that they are read in order. | 113 // can check that they are read in order. |
112 mutable unsigned consumed_descriptor_highwater_; | 114 mutable unsigned consumed_descriptor_highwater_; |
113 | 115 |
114 DISALLOW_COPY_AND_ASSIGN(FileDescriptorSet); | 116 DISALLOW_COPY_AND_ASSIGN(FileDescriptorSet); |
115 }; | 117 }; |
116 | 118 |
117 #endif // IPC_FILE_DESCRIPTOR_SET_POSIX_H_ | 119 #endif // IPC_FILE_DESCRIPTOR_SET_POSIX_H_ |
OLD | NEW |