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