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