1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/chromium/src/chrome/common/file_descriptor_set_posix.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,108 @@ 1.4 +// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#ifndef CHROME_COMMON_FILE_DESCRIPTOR_SET_POSIX_H_ 1.9 +#define CHROME_COMMON_FILE_DESCRIPTOR_SET_POSIX_H_ 1.10 + 1.11 +#include <vector> 1.12 + 1.13 +#include "base/basictypes.h" 1.14 +#include "base/file_descriptor_posix.h" 1.15 +#include "base/ref_counted.h" 1.16 + 1.17 +// ----------------------------------------------------------------------------- 1.18 +// A FileDescriptorSet is an ordered set of POSIX file descriptors. These are 1.19 +// associated with IPC messages so that descriptors can be transmitted over a 1.20 +// UNIX domain socket. 1.21 +// ----------------------------------------------------------------------------- 1.22 +class FileDescriptorSet : public base::RefCountedThreadSafe<FileDescriptorSet> { 1.23 + public: 1.24 + FileDescriptorSet(); 1.25 + ~FileDescriptorSet(); 1.26 + 1.27 + // This is the maximum number of descriptors per message. We need to know this 1.28 + // because the control message kernel interface has to be given a buffer which 1.29 + // is large enough to store all the descriptor numbers. Otherwise the kernel 1.30 + // tells us that it truncated the control data and the extra descriptors are 1.31 + // lost. 1.32 + // 1.33 + // In debugging mode, it's a fatal error to try and add more than this number 1.34 + // of descriptors to a FileDescriptorSet. 1.35 + enum { 1.36 + MAX_DESCRIPTORS_PER_MESSAGE = 4 1.37 + }; 1.38 + 1.39 + // --------------------------------------------------------------------------- 1.40 + // Interfaces for building during message serialisation... 1.41 + 1.42 + // Add a descriptor to the end of the set. Returns false iff the set is full. 1.43 + bool Add(int fd); 1.44 + // Add a descriptor to the end of the set and automatically close it after 1.45 + // transmission. Returns false iff the set is full. 1.46 + bool AddAndAutoClose(int fd); 1.47 + 1.48 + // --------------------------------------------------------------------------- 1.49 + 1.50 + 1.51 + // --------------------------------------------------------------------------- 1.52 + // Interfaces for accessing during message deserialisation... 1.53 + 1.54 + // Return the number of descriptors 1.55 + unsigned size() const { return descriptors_.size(); } 1.56 + // Return true if no unconsumed descriptors remain 1.57 + bool empty() const { return descriptors_.empty(); } 1.58 + // Fetch the nth descriptor from the beginning of the set. Code using this 1.59 + // /must/ access the descriptors in order, except that it may wrap from the 1.60 + // end to index 0 again. 1.61 + // 1.62 + // This interface is designed for the deserialising code as it doesn't 1.63 + // support close flags. 1.64 + // returns: file descriptor, or -1 on error 1.65 + int GetDescriptorAt(unsigned n) const; 1.66 + 1.67 + // --------------------------------------------------------------------------- 1.68 + 1.69 + 1.70 + // --------------------------------------------------------------------------- 1.71 + // Interfaces for transmission... 1.72 + 1.73 + // Fill an array with file descriptors without 'consuming' them. CommitAll 1.74 + // must be called after these descriptors have been transmitted. 1.75 + // buffer: (output) a buffer of, at least, size() integers. 1.76 + void GetDescriptors(int* buffer) const; 1.77 + // This must be called after transmitting the descriptors returned by 1.78 + // GetDescriptors. It marks all the descriptors as consumed and closes those 1.79 + // which are auto-close. 1.80 + void CommitAll(); 1.81 + 1.82 + // --------------------------------------------------------------------------- 1.83 + 1.84 + 1.85 + // --------------------------------------------------------------------------- 1.86 + // Interfaces for receiving... 1.87 + 1.88 + // Set the contents of the set from the given buffer. This set must be empty 1.89 + // before calling. The auto-close flag is set on all the descriptors so that 1.90 + // unconsumed descriptors are closed on destruction. 1.91 + void SetDescriptors(const int* buffer, unsigned count); 1.92 + 1.93 + // --------------------------------------------------------------------------- 1.94 + 1.95 + private: 1.96 + // A vector of descriptors and close flags. If this message is sent, then 1.97 + // these descriptors are sent as control data. After sending, any descriptors 1.98 + // with a true flag are closed. If this message has been received, then these 1.99 + // are the descriptors which were received and all close flags are true. 1.100 + std::vector<base::FileDescriptor> descriptors_; 1.101 + 1.102 + // This contains the index of the next descriptor which should be consumed. 1.103 + // It's used in a couple of ways. Firstly, at destruction we can check that 1.104 + // all the descriptors have been read (with GetNthDescriptor). Secondly, we 1.105 + // can check that they are read in order. 1.106 + mutable unsigned consumed_descriptor_highwater_; 1.107 + 1.108 + DISALLOW_COPY_AND_ASSIGN(FileDescriptorSet); 1.109 +}; 1.110 + 1.111 +#endif // CHROME_COMMON_FILE_DESCRIPTOR_SET_POSIX_H_