Qt 4.8
Public Functions | Private Functions | Properties | Friends | List of all members
QUnixSocketRights Class Reference

The QUnixSocketRights class encapsulates QUnixSocket rights data. More...

#include <qunixsocket_p.h>

Public Functions

int dupFd () const
 Return a duplicate of the file descriptor contained in this object. More...
 
bool isValid () const
 Returns true if this QUnixSocketRights instance is managing a valid file descriptor. More...
 
QUnixSocketRightsoperator= (const QUnixSocketRights &)
 Create a copy of other. More...
 
int peekFd () const
 Returns the file descriptor contained in this object. More...
 
 QUnixSocketRights (int)
 Create a new QUnixSocketRights instance containing the file descriptor fd. More...
 
 QUnixSocketRights (const QUnixSocketRights &)
 Create a copy of other. More...
 
 ~QUnixSocketRights ()
 Destroys the QUnixSocketRights instance. More...
 

Private Functions

 QUnixSocketRights (int, int)
 Construct a QUnixSocketRights instance on fd without dup(2)'ing the file descriptor. More...
 

Properties

QSharedDataPointer< QUnixSocketRightsPrivated
 

Friends

class QUnixSocket
 

Detailed Description

The QUnixSocketRights class encapsulates QUnixSocket rights data.

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

QUnixSocket allows you to transfer Unix file descriptors between processes. A file descriptor is referred to as "rights data" as it allows one process to transfer its right to access a resource to another.

The Unix system verifies resource permissions only when the resource is first opened. For example, consider a file on disk readable only by the user "qt". A process running as user "qt" will be able to open this file for reading. If, while the process was still reading from the file, the ownership was changed from user "qt" to user "root", the process would be allowed to continue reading from the file, even though attempting to reopen the file would be denied. Permissions are associated with special descriptors called file descriptors which are returned to a process after it initially opens a resource.

File descriptors can be duplicated within a process through the dup(2) system call. File descriptors can be passed between processes using the QUnixSocket class in the same way. Even though the receiving process never opened the resource directly, it has the same permissions to access it as the process that did.

See also
QUnixSocket

Definition at line 73 of file qunixsocket_p.h.

Constructors and Destructors

◆ QUnixSocketRights() [1/3]

QUnixSocketRights::QUnixSocketRights ( int  fd)

Create a new QUnixSocketRights instance containing the file descriptor fd.

fd will be dup(2)'d internally, so the application is free to close fd following this call.

If the dup(2) fails, or you pass an invalid fd, an QUnixSocketRights::isValid(){invalid } object will be constructed.

QUnixSocketRights instances are immutable and the internal file descriptor will be shared between any copies made of this object. The system will close(2) the file descriptor once it is no longer needed.

Definition at line 163 of file qunixsocket.cpp.

164 {
165  d = new QUnixSocketRightsPrivate();
166  if(-1 == fd) {
167  d->fd = -1;
168  } else {
169  d->fd = qt_safe_dup(fd);
170 #ifdef QUNIXSOCKET_DEBUG
171  if(-1 == d->fd) {
172  qDebug() << "QUnixSocketRights: Unable to duplicate fd "
173  << fd << " (" << ::strerror(errno) << ')';
174  }
175 #endif
176  }
177 }
QSharedDataPointer< QUnixSocketRightsPrivate > d
Definition: qunixsocket_p.h:89
Q_CORE_EXPORT void qDebug(const char *,...)
int errno
static int qt_safe_dup(int oldfd, int atleast=0, int flags=FD_CLOEXEC)
Definition: qcore_unix_p.h:227

◆ ~QUnixSocketRights()

QUnixSocketRights::~QUnixSocketRights ( )

Destroys the QUnixSocketRights instance.

Definition at line 198 of file qunixsocket.cpp.

199 {
200 }

◆ QUnixSocketRights() [2/3]

QUnixSocketRights::QUnixSocketRights ( const QUnixSocketRights other)

Create a copy of other.

Definition at line 215 of file qunixsocket.cpp.

216 : d(other.d)
217 {
218 }
QSharedDataPointer< QUnixSocketRightsPrivate > d
Definition: qunixsocket_p.h:89

◆ QUnixSocketRights() [3/3]

QUnixSocketRights::QUnixSocketRights ( int  fd,
int   
)
private

Construct a QUnixSocketRights instance on fd without dup(2)'ing the file descriptor.

Warning
This function is not part of the public interface.

Definition at line 188 of file qunixsocket.cpp.

189 {
190  Q_ASSERT(-1 != fd);
191  d = new QUnixSocketRightsPrivate();
192  d->fd = fd;
193 }
QSharedDataPointer< QUnixSocketRightsPrivate > d
Definition: qunixsocket_p.h:89
#define Q_ASSERT(cond)
Definition: qglobal.h:1823

Functions

◆ dupFd()

int QUnixSocketRights::dupFd ( ) const

Return a duplicate of the file descriptor contained in this object.

If this is an QUnixSocketRights::isValid(){invalid } object, or the dup(2) call fails, an invalid file descriptor (-1) will be returned.

See also
QUnixSocketRights::peekFd()

Definition at line 238 of file qunixsocket.cpp.

239 {
240  if(-1 == d->fd) return -1;
241 
242  int rv = qt_safe_dup(d->fd);
243 
244 #ifdef QUNIXSOCKET_DEBUG
245  if(-1 == rv)
246  qDebug() << "QUnixSocketRights: Unable to duplicate managed file "
247  "descriptor (" << ::strerror(errno) << ')';
248 #endif
249 
250  return rv;
251 }
QSharedDataPointer< QUnixSocketRightsPrivate > d
Definition: qunixsocket_p.h:89
Q_CORE_EXPORT void qDebug(const char *,...)
int errno
static int qt_safe_dup(int oldfd, int atleast=0, int flags=FD_CLOEXEC)
Definition: qcore_unix_p.h:227

◆ isValid()

bool QUnixSocketRights::isValid ( ) const

Returns true if this QUnixSocketRights instance is managing a valid file descriptor.

This method is equivalent to (-1 != peekFd()).

See also
QUnixSocketRights::peekFd()

Definition at line 226 of file qunixsocket.cpp.

Referenced by QUnixSocketPrivate::writeActivated().

227 {
228  return d->fd != -1;
229 }
QSharedDataPointer< QUnixSocketRightsPrivate > d
Definition: qunixsocket_p.h:89

◆ operator=()

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

Create a copy of other.

Definition at line 206 of file qunixsocket.cpp.

207 {
208  d = other.d;
209  return *this;
210 }
QSharedDataPointer< QUnixSocketRightsPrivate > d
Definition: qunixsocket_p.h:89

◆ peekFd()

int QUnixSocketRights::peekFd ( ) const

Returns the file descriptor contained in this object.

If this is an QUnixSocketRights::isValid(){invalid } object an invalid file descriptor (-1) will be returned.

The lifetime of this file descriptor is tied to the lifetime of the QUnixSocketRights instance. The file descriptor returned by this method may be close(2)'d when the QUnixSocketRights instance is destroyed. If you want to continue to use the file descriptor use QUnixSocketRights::dupFd() instead.

See also
QUnixSocketRights::dupFd()

Definition at line 266 of file qunixsocket.cpp.

Referenced by QUnixSocketPrivate::writeActivated().

267 {
268  return d->fd;
269 }
QSharedDataPointer< QUnixSocketRightsPrivate > d
Definition: qunixsocket_p.h:89

Friends and Related Functions

◆ QUnixSocket

friend class QUnixSocket
friend

Definition at line 87 of file qunixsocket_p.h.

Properties

◆ d

QSharedDataPointer<QUnixSocketRightsPrivate> QUnixSocketRights::d
private

Definition at line 89 of file qunixsocket_p.h.

Referenced by dupFd(), isValid(), operator=(), and peekFd().


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