| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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 DBUS_FILE_DESCRIPTOR_H_ | |
| 6 #define DBUS_FILE_DESCRIPTOR_H_ | |
| 7 | |
| 8 #include <memory> | |
| 9 | |
| 10 #include "base/macros.h" | |
| 11 #include "dbus/dbus_export.h" | |
| 12 | |
| 13 namespace dbus { | |
| 14 | |
| 15 // FileDescriptor is a type used to encapsulate D-Bus file descriptors | |
| 16 // and to follow the RAII idiom appropiate for use with message operations | |
| 17 // where the descriptor might be easily leaked. To guard against this the | |
| 18 // descriptor is closed when an instance is destroyed if it is owned. | |
| 19 // Ownership is asserted only when PutValue is used and TakeValue can be | |
| 20 // used to take ownership. | |
| 21 // | |
| 22 // For example, in the following | |
| 23 // FileDescriptor fd; | |
| 24 // if (!reader->PopString(&name) || | |
| 25 // !reader->PopFileDescriptor(&fd) || | |
| 26 // !reader->PopUint32(&flags)) { | |
| 27 // the descriptor in fd will be closed if the PopUint32 fails. But | |
| 28 // writer.AppendFileDescriptor(dbus::FileDescriptor(1)); | |
| 29 // will not automatically close "1" because it is not owned. | |
| 30 // | |
| 31 // Descriptors must be validated before marshalling in a D-Bus message | |
| 32 // or using them after unmarshalling. We disallow descriptors to a | |
| 33 // directory to reduce the security risks. Splitting out validation | |
| 34 // also allows the caller to do this work on the File thread to conform | |
| 35 // with i/o restrictions. | |
| 36 class CHROME_DBUS_EXPORT FileDescriptor { | |
| 37 public: | |
| 38 // This provides a simple way to pass around file descriptors since they must | |
| 39 // be closed on a thread that is allowed to perform I/O. | |
| 40 struct Deleter { | |
| 41 void CHROME_DBUS_EXPORT operator()(FileDescriptor* fd); | |
| 42 }; | |
| 43 | |
| 44 // Permits initialization without a value for passing to | |
| 45 // dbus::MessageReader::PopFileDescriptor to fill in and from int values. | |
| 46 FileDescriptor() : value_(-1), owner_(false), valid_(false) {} | |
| 47 explicit FileDescriptor(int value) : value_(value), owner_(false), | |
| 48 valid_(false) {} | |
| 49 | |
| 50 FileDescriptor(FileDescriptor&& other); | |
| 51 | |
| 52 virtual ~FileDescriptor(); | |
| 53 | |
| 54 FileDescriptor& operator=(FileDescriptor&& other); | |
| 55 | |
| 56 // Retrieves value as an int without affecting ownership. | |
| 57 int value() const; | |
| 58 | |
| 59 // Retrieves whether or not the descriptor is ok to send/receive. | |
| 60 int is_valid() const { return valid_; } | |
| 61 | |
| 62 // Sets the value and assign ownership. | |
| 63 void PutValue(int value) { | |
| 64 value_ = value; | |
| 65 owner_ = true; | |
| 66 valid_ = false; | |
| 67 } | |
| 68 | |
| 69 // Takes the value and ownership. | |
| 70 int TakeValue(); | |
| 71 | |
| 72 // Checks (and records) validity of the file descriptor. | |
| 73 // We disallow directories to avoid potential sandbox escapes. | |
| 74 // Note this call must be made on a thread where file i/o is allowed. | |
| 75 void CheckValidity(); | |
| 76 | |
| 77 private: | |
| 78 void Swap(FileDescriptor* other); | |
| 79 | |
| 80 int value_; | |
| 81 bool owner_; | |
| 82 bool valid_; | |
| 83 | |
| 84 DISALLOW_COPY_AND_ASSIGN(FileDescriptor); | |
| 85 }; | |
| 86 | |
| 87 using ScopedFileDescriptor = | |
| 88 std::unique_ptr<FileDescriptor, FileDescriptor::Deleter>; | |
| 89 | |
| 90 } // namespace dbus | |
| 91 | |
| 92 #endif // DBUS_FILE_DESCRIPTOR_H_ | |
| OLD | NEW |