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
3 // found in the LICENSE file.
5 #ifndef IPC_FILE_DESCRIPTOR_SET_POSIX_H_
6 #define IPC_FILE_DESCRIPTOR_SET_POSIX_H_
10 #include "base/basictypes.h"
11 #include "base/file_descriptor_posix.h"
12 #include "base/memory/ref_counted.h"
13 #include "ipc/ipc_export.h"
15 // -----------------------------------------------------------------------------
16 // 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 // UNIX domain socket.
19 // -----------------------------------------------------------------------------
20 class IPC_EXPORT FileDescriptorSet
21 : public base::RefCountedThreadSafe
<FileDescriptorSet
> {
25 // 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 // 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
31 // In debugging mode, it's a fatal error to try and add more than this number
32 // of descriptors to a FileDescriptorSet.
33 static const size_t kMaxDescriptorsPerMessage
= 5;
35 // ---------------------------------------------------------------------------
36 // Interfaces for building during message serialisation...
38 // Add a descriptor to the end of the set. Returns false iff the set is full.
40 // Add a descriptor to the end of the set and automatically close it after
41 // transmission. Returns false iff the set is full.
42 bool AddAndAutoClose(int fd
);
44 // ---------------------------------------------------------------------------
47 // ---------------------------------------------------------------------------
48 // Interfaces for accessing during message deserialisation...
50 // Return the number of descriptors
51 unsigned size() const { return descriptors_
.size(); }
52 // Return true if no unconsumed descriptors remain
53 bool empty() const { return descriptors_
.empty(); }
54 // Fetch the nth descriptor from the beginning of the set. Code using this
55 // /must/ access the descriptors in order, except that it may wrap from the
56 // end to index 0 again.
58 // This interface is designed for the deserialising code as it doesn't
59 // support close flags.
60 // returns: file descriptor, or -1 on error
61 int GetDescriptorAt(unsigned n
) const;
63 // ---------------------------------------------------------------------------
66 // ---------------------------------------------------------------------------
67 // Interfaces for transmission...
69 // Fill an array with file descriptors without 'consuming' them. CommitAll
70 // must be called after these descriptors have been transmitted.
71 // buffer: (output) a buffer of, at least, size() integers.
72 void GetDescriptors(int* buffer
) const;
73 // This must be called after transmitting the descriptors returned by
74 // GetDescriptors. It marks all the descriptors as consumed and closes those
75 // which are auto-close.
77 // Returns true if any contained file descriptors appear to be handles to a
79 bool ContainsDirectoryDescriptor() const;
81 // ---------------------------------------------------------------------------
84 // ---------------------------------------------------------------------------
85 // Interfaces for receiving...
87 // Set the contents of the set from the given buffer. This set must be empty
88 // before calling. The auto-close flag is set on all the descriptors so that
89 // unconsumed descriptors are closed on destruction.
90 void SetDescriptors(const int* buffer
, unsigned count
);
92 // ---------------------------------------------------------------------------
95 friend class base::RefCountedThreadSafe
<FileDescriptorSet
>;
99 // A vector of descriptors and close flags. If this message is sent, then
100 // these descriptors are sent as control data. After sending, any descriptors
101 // with a true flag are closed. If this message has been received, then these
102 // are the descriptors which were received and all close flags are true.
103 std::vector
<base::FileDescriptor
> descriptors_
;
105 // This contains the index of the next descriptor which should be consumed.
106 // It's used in a couple of ways. Firstly, at destruction we can check that
107 // all the descriptors have been read (with GetNthDescriptor). Secondly, we
108 // can check that they are read in order.
109 mutable unsigned consumed_descriptor_highwater_
;
111 DISALLOW_COPY_AND_ASSIGN(FileDescriptorSet
);
114 #endif // IPC_FILE_DESCRIPTOR_SET_POSIX_H_