blob: 1554c38510d29e01460935ba9eab7b78a13d390b [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]a17b7ca62009-02-12 18:09:112// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]946d1b22009-07-22 23:57:215#ifndef IPC_FILE_DESCRIPTOR_SET_POSIX_H_
6#define IPC_FILE_DESCRIPTOR_SET_POSIX_H_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
[email protected]a17b7ca62009-02-12 18:09:118
9#include <vector>
10
11#include "base/basictypes.h"
12#include "base/file_descriptor_posix.h"
[email protected]3b63f8f42011-03-28 01:54:1513#include "base/memory/ref_counted.h"
[email protected]7c854372011-08-15 20:41:4614#include "ipc/ipc_export.h"
[email protected]a17b7ca62009-02-12 18:09:1115
16// -----------------------------------------------------------------------------
[email protected]d0c0b1d2009-02-12 18:32:4517// A FileDescriptorSet is an ordered set of POSIX file descriptors. These are
[email protected]a17b7ca62009-02-12 18:09:1118// associated with IPC messages so that descriptors can be transmitted over a
19// UNIX domain socket.
20// -----------------------------------------------------------------------------
[email protected]7c854372011-08-15 20:41:4621class IPC_EXPORT FileDescriptorSet
22 : public base::RefCountedThreadSafe<FileDescriptorSet> {
[email protected]a17b7ca62009-02-12 18:09:1123 public:
[email protected]d0c0b1d2009-02-12 18:32:4524 FileDescriptorSet();
[email protected]a17b7ca62009-02-12 18:09:1125
26 // 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
28 // 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
30 // lost.
31 //
32 // In debugging mode, it's a fatal error to try and add more than this number
[email protected]d0c0b1d2009-02-12 18:32:4533 // of descriptors to a FileDescriptorSet.
[email protected]a17b7ca62009-02-12 18:09:1134 enum {
[email protected]49d37ef2011-05-17 10:22:2835 MAX_DESCRIPTORS_PER_MESSAGE = 5,
[email protected]a17b7ca62009-02-12 18:09:1136 };
37
38 // ---------------------------------------------------------------------------
39 // Interfaces for building during message serialisation...
40
41 // Add a descriptor to the end of the set. Returns false iff the set is full.
42 bool Add(int fd);
43 // Add a descriptor to the end of the set and automatically close it after
44 // transmission. Returns false iff the set is full.
45 bool AddAndAutoClose(int fd);
46
47 // ---------------------------------------------------------------------------
48
49
50 // ---------------------------------------------------------------------------
51 // Interfaces for accessing during message deserialisation...
52
53 // Return the number of descriptors
54 unsigned size() const { return descriptors_.size(); }
55 // Return true if no unconsumed descriptors remain
56 bool empty() const { return descriptors_.empty(); }
57 // Fetch the nth descriptor from the beginning of the set. Code using this
58 // /must/ access the descriptors in order, except that it may wrap from the
59 // end to index 0 again.
60 //
61 // This interface is designed for the deserialising code as it doesn't
62 // support close flags.
63 // returns: file descriptor, or -1 on error
64 int GetDescriptorAt(unsigned n) const;
65
66 // ---------------------------------------------------------------------------
67
68
69 // ---------------------------------------------------------------------------
70 // Interfaces for transmission...
71
72 // Fill an array with file descriptors without 'consuming' them. CommitAll
73 // must be called after these descriptors have been transmitted.
74 // buffer: (output) a buffer of, at least, size() integers.
75 void GetDescriptors(int* buffer) const;
76 // This must be called after transmitting the descriptors returned by
77 // GetDescriptors. It marks all the descriptors as consumed and closes those
78 // which are auto-close.
79 void CommitAll();
[email protected]aac449e2010-06-10 21:39:0480 // Returns true if any contained file descriptors appear to be handles to a
81 // directory.
82 bool ContainsDirectoryDescriptor() const;
[email protected]a17b7ca62009-02-12 18:09:1183
84 // ---------------------------------------------------------------------------
85
86
87 // ---------------------------------------------------------------------------
88 // Interfaces for receiving...
89
90 // 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 // unconsumed descriptors are closed on destruction.
93 void SetDescriptors(const int* buffer, unsigned count);
94
95 // ---------------------------------------------------------------------------
96
97 private:
[email protected]877d55d2009-11-05 21:53:0898 friend class base::RefCountedThreadSafe<FileDescriptorSet>;
99
100 ~FileDescriptorSet();
101
[email protected]a17b7ca62009-02-12 18:09:11102 // 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 // 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 std::vector<base::FileDescriptor> descriptors_;
107
108 // 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
110 // all the descriptors have been read (with GetNthDescriptor). Secondly, we
111 // can check that they are read in order.
112 mutable unsigned consumed_descriptor_highwater_;
113
[email protected]d0c0b1d2009-02-12 18:32:45114 DISALLOW_COPY_AND_ASSIGN(FileDescriptorSet);
[email protected]a17b7ca62009-02-12 18:09:11115};
116
[email protected]946d1b22009-07-22 23:57:21117#endif // IPC_FILE_DESCRIPTOR_SET_POSIX_H_