Qt 4.8
Public Functions | Static Public Functions | Protected Types | Protected Variables | List of all members
QDBusUnixFileDescriptor Class Reference

The QDBusUnixFileDescriptor class holds one Unix file descriptor. More...

#include <qdbusunixfiledescriptor.h>

Public Functions

int fileDescriptor () const
 Returns the Unix file descriptor contained by this QDBusUnixFileDescriptor object. More...
 
void giveFileDescriptor (int fileDescriptor)
 Sets the Unix file descriptor to fileDescriptor without copying. More...
 
bool isValid () const
 Returns true if this Unix file descriptor is valid. More...
 
QDBusUnixFileDescriptoroperator= (const QDBusUnixFileDescriptor &other)
 Copies the Unix file descriptor from the other QDBusUnixFileDescriptor object. More...
 
 QDBusUnixFileDescriptor ()
 Constructs a QDBusUnixFileDescriptor without a wrapped file descriptor. More...
 
 QDBusUnixFileDescriptor (int fileDescriptor)
 Constructs a QDBusUnixFileDescriptor object by copying the fileDescriptor parameter. More...
 
 QDBusUnixFileDescriptor (const QDBusUnixFileDescriptor &other)
 Constructs a QDBusUnixFileDescriptor object by copying other. More...
 
void setFileDescriptor (int fileDescriptor)
 Sets the file descriptor that this QDBusUnixFileDescriptor object holds to a copy of fileDescriptor. More...
 
int takeFileDescriptor ()
 Extracts the Unix file descriptor from the QDBusUnixFileDescriptor object and transfers ownership. More...
 
 ~QDBusUnixFileDescriptor ()
 Destroys this QDBusUnixFileDescriptor object and disposes of the Unix file descriptor that it contained. More...
 

Static Public Functions

static bool isSupported ()
 Returns true if Unix file descriptors are supported on this platform. More...
 

Protected Types

typedef QExplicitlySharedDataPointer< QDBusUnixFileDescriptorPrivateData
 

Protected Variables

Data d
 

Detailed Description

The QDBusUnixFileDescriptor class holds one Unix file descriptor.

Attention
Module: QtDBus
Since
4.8

The QDBusUnixFileDescriptor class is used to hold one Unix file descriptor for use with the QtDBus module. This allows applications to send and receive Unix file descriptors over the D-Bus connection, mapping automatically to the D-Bus type 'h'.

Objects of type QDBusUnixFileDescriptors can be used also as parameters in signals and slots that get exported to D-Bus by registering with QDBusConnection::registerObject.

QDBusUnixFileDescriptor does not take ownership of the file descriptor. Instead, it will use the Unix system call dup(2) to make a copy of the file descriptor. This file descriptor belongs to the QDBusUnixFileDescriptor object and should not be stored or closed by the user. Instead, you should make your own copy if you need that.

Availability

Unix file descriptor passing is not available in all D-Bus connections. This feature is present with D-Bus library and bus daemon version 1.4 and upwards on Unix systems. QtDBus automatically enables the feature if such a version was found at compile-time and run-time.

To verify that your connection does support passing file descriptors, check if the QDBusConnection::UnixFileDescriptorPassing capability is set with QDBusConnection::connectionCapabilities(). If the flag is not active, then you will not be able to make calls to methods that have QDBusUnixFileDescriptor as arguments or even embed such a type in a variant. You will also not receive calls containing that type.

Note also that remote applications may not have support for Unix file descriptor passing. If you make a D-Bus to a remote application that cannot receive such a type, you will receive an error reply. If you try to send a signal containing a D-Bus file descriptor or return one from a method call, the message will be silently dropped.

Even if the feature is not available, QDBusUnixFileDescriptor will continue to operate, so code need not have compile-time checks for the availability of this feature.

On non-Unix systems, QDBusUnixFileDescriptor will always report an invalid state and QDBusUnixFileDescriptor::isSupported() will return false.

See also
QDBusConnection::ConnectionCapabilities, QDBusConnection::connectionCapabilities()

Definition at line 64 of file qdbusunixfiledescriptor.h.

Typedefs

◆ Data

Warning
This function is not part of the public interface.

Definition at line 91 of file qdbusunixfiledescriptor.h.

Constructors and Destructors

◆ QDBusUnixFileDescriptor() [1/3]

QDBusUnixFileDescriptor::QDBusUnixFileDescriptor ( )

Constructs a QDBusUnixFileDescriptor without a wrapped file descriptor.

This is equivalent to constructing the object with an invalid file descriptor (like -1).

See also
fileDescriptor(), isValid()

Definition at line 140 of file qdbusunixfiledescriptor.cpp.

141  : d(0)
142 {
143 }

◆ QDBusUnixFileDescriptor() [2/3]

QDBusUnixFileDescriptor::QDBusUnixFileDescriptor ( int  fileDescriptor)
explicit

Constructs a QDBusUnixFileDescriptor object by copying the fileDescriptor parameter.

The original file descriptor is not touched and must be closed by the user.

Note that the value returned by fileDescriptor() will be different from the fileDescriptor parameter passed.

If the fileDescriptor parameter is not valid, isValid() will return false and fileDescriptor() will return -1.

See also
setFileDescriptor(), fileDescriptor()

Definition at line 158 of file qdbusunixfiledescriptor.cpp.

159  : d(0)
160 {
161  if (fileDescriptor != -1)
163 }
void setFileDescriptor(int fileDescriptor)
Sets the file descriptor that this QDBusUnixFileDescriptor object holds to a copy of fileDescriptor...
int fileDescriptor() const
Returns the Unix file descriptor contained by this QDBusUnixFileDescriptor object.

◆ QDBusUnixFileDescriptor() [3/3]

QDBusUnixFileDescriptor::QDBusUnixFileDescriptor ( const QDBusUnixFileDescriptor other)

Constructs a QDBusUnixFileDescriptor object by copying other.

Definition at line 168 of file qdbusunixfiledescriptor.cpp.

169  : d(other.d)
170 {
171 }

◆ ~QDBusUnixFileDescriptor()

QDBusUnixFileDescriptor::~QDBusUnixFileDescriptor ( )

Destroys this QDBusUnixFileDescriptor object and disposes of the Unix file descriptor that it contained.

Definition at line 188 of file qdbusunixfiledescriptor.cpp.

189 {
190 }

Functions

◆ fileDescriptor()

int QDBusUnixFileDescriptor::fileDescriptor ( ) const

Returns the Unix file descriptor contained by this QDBusUnixFileDescriptor object.

An invalid file descriptor is represented by the value -1.

Note that the file descriptor returned by this function is owned by the QDBusUnixFileDescriptor object and must not be stored past the lifetime of this object. It is ok to use it while this object is valid, but if one wants to store it for longer use, the file descriptor should be cloned using the Unix dup(2), dup2(2) or dup3(2) functions.

See also
isValid()

Definition at line 216 of file qdbusunixfiledescriptor.cpp.

Referenced by QDBusMarshaller::append(), and giveFileDescriptor().

217 {
218  return d ? d->fd.operator int() : -1;
219 }

◆ giveFileDescriptor()

void QDBusUnixFileDescriptor::giveFileDescriptor ( int  fileDescriptor)

Sets the Unix file descriptor to fileDescriptor without copying.

Warning
This function is not part of the public interface.
See also
setFileDescriptor()

Definition at line 269 of file qdbusunixfiledescriptor.cpp.

Referenced by setFileDescriptor(), QDBusDemarshaller::toUnixFileDescriptor(), and QDBusUnixFileDescriptorPrivate::~QDBusUnixFileDescriptorPrivate().

270 {
271  // if we are the sole ref, d remains unchanged
272  // if detaching happens, d->fd will be -1
273  if (d)
274  d.detach();
275  else
277 
278  if (d->fd != -1)
279  qt_safe_close(d->fd);
280 
281  if (fileDescriptor != -1)
282  d->fd = fileDescriptor;
283 }
static int qt_safe_close(int fd)
Definition: qcore_unix_p.h:297
void detach()
If the shared data object&#39;s reference count is greater than 1, this function creates a deep copy of t...
Definition: qshareddata.h:148
int fileDescriptor() const
Returns the Unix file descriptor contained by this QDBusUnixFileDescriptor object.

◆ isSupported()

bool QDBusUnixFileDescriptor::isSupported ( )
static

Returns true if Unix file descriptors are supported on this platform.

In other words, this function returns true if this is a Unix platform.

Note that QDBusUnixFileDescriptor continues to operate even if this function returns false. The only difference is that the QDBusUnixFileDescriptor objects will always be in the isValid() == false state and fileDescriptor() will always return -1. The class will not consume any operating system resources.

Definition at line 236 of file qdbusunixfiledescriptor.cpp.

Referenced by QDBusUnixFileDescriptorPrivate::~QDBusUnixFileDescriptorPrivate().

237 {
238  return true;
239 }

◆ isValid()

bool QDBusUnixFileDescriptor::isValid ( ) const

Returns true if this Unix file descriptor is valid.

A valid Unix file descriptor is not -1.

See also
fileDescriptor()

Definition at line 198 of file qdbusunixfiledescriptor.cpp.

199 {
200  return d ? d->fd != -1 : false;
201 }

◆ operator=()

QDBusUnixFileDescriptor & QDBusUnixFileDescriptor::operator= ( const QDBusUnixFileDescriptor other)

Copies the Unix file descriptor from the other QDBusUnixFileDescriptor object.

If the current object contained a file descriptor, it will be properly disposed of before.

Definition at line 178 of file qdbusunixfiledescriptor.cpp.

179 {
180  if (this != &other)
181  d.operator=(other.d);
182  return *this;
183 }

◆ setFileDescriptor()

void QDBusUnixFileDescriptor::setFileDescriptor ( int  fileDescriptor)

Sets the file descriptor that this QDBusUnixFileDescriptor object holds to a copy of fileDescriptor.

The original file descriptor is not touched and must be closed by the user.

Note that the value returned by fileDescriptor() will be different from the fileDescriptor parameter passed.

If the fileDescriptor parameter is not valid, isValid() will return false and fileDescriptor() will return -1.

See also
isValid(), fileDescriptor()

Definition at line 254 of file qdbusunixfiledescriptor.cpp.

Referenced by QDBusUnixFileDescriptor(), and QDBusUnixFileDescriptorPrivate::~QDBusUnixFileDescriptorPrivate().

255 {
256  if (fileDescriptor != -1)
258 }
int fileDescriptor() const
Returns the Unix file descriptor contained by this QDBusUnixFileDescriptor object.
void giveFileDescriptor(int fileDescriptor)
Sets the Unix file descriptor to fileDescriptor without copying.
static int qt_safe_dup(int oldfd, int atleast=0, int flags=FD_CLOEXEC)
Definition: qcore_unix_p.h:227

◆ takeFileDescriptor()

int QDBusUnixFileDescriptor::takeFileDescriptor ( )

Extracts the Unix file descriptor from the QDBusUnixFileDescriptor object and transfers ownership.

Warning
This function is not part of the public interface.

Note: since QDBusUnixFileDescriptor is implicitly shared, this function is inherently racy and should be avoided.

Definition at line 296 of file qdbusunixfiledescriptor.cpp.

Referenced by QDBusUnixFileDescriptorPrivate::~QDBusUnixFileDescriptorPrivate().

297 {
298  if (!d)
299  return -1;
300 
301  return d->fd.fetchAndStoreRelaxed(-1);
302 }
int fetchAndStoreRelaxed(int newValue)
Atomic fetch-and-store.

Properties

◆ d

QDBusUnixFileDescriptor::d
protected
Warning
This function is not part of the public interface.

Definition at line 92 of file qdbusunixfiledescriptor.h.

Referenced by fileDescriptor(), giveFileDescriptor(), isValid(), operator=(), and takeFileDescriptor().


The documentation for this class was generated from the following files: