qdbusunixfiledescriptor.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/dbus/qdbusunixfiledescriptor.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
1/****************************************************************************-
2**-
3** Copyright (C) 2015 The Qt Company Ltd.-
4** Contact: http://www.qt.io/licensing/-
5**-
6** This file is part of the QtDBus module of the Qt Toolkit.-
7**-
8** $QT_BEGIN_LICENSE:LGPL21$-
9** Commercial License Usage-
10** Licensees holding valid commercial Qt licenses may use this file in-
11** accordance with the commercial license agreement provided with the-
12** Software or, alternatively, in accordance with the terms contained in-
13** a written agreement between you and The Qt Company. For licensing terms-
14** and conditions see http://www.qt.io/terms-conditions. For further-
15** information use the contact form at http://www.qt.io/contact-us.-
16**-
17** GNU Lesser General Public License Usage-
18** Alternatively, this file may be used under the terms of the GNU Lesser-
19** General Public License version 2.1 or version 3 as published by the Free-
20** Software Foundation and appearing in the file LICENSE.LGPLv21 and-
21** LICENSE.LGPLv3 included in the packaging of this file. Please review the-
22** following information to ensure the GNU Lesser General Public License-
23** requirements will be met: https://www.gnu.org/licenses/lgpl.html and-
24** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.-
25**-
26** As a special exception, The Qt Company gives you certain additional-
27** rights. These rights are described in The Qt Company LGPL Exception-
28** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.-
29**-
30** $QT_END_LICENSE$-
31**-
32****************************************************************************/-
33-
34-
35#include "qdbusunixfiledescriptor.h"-
36-
37#ifdef Q_OS_UNIX-
38# include <private/qcore_unix_p.h>-
39#endif-
40-
41QT_BEGIN_NAMESPACE-
42-
43#ifndef QT_NO_DBUS-
44-
45/*!-
46 \class QDBusUnixFileDescriptor-
47 \inmodule QtDBus-
48 \ingroup shared-
49 \since 4.8-
50-
51 \brief The QDBusUnixFileDescriptor class holds one Unix file descriptor.-
52-
53 The QDBusUnixFileDescriptor class is used to hold one Unix file-
54 descriptor for use with the Qt D-Bus module. This allows applications to-
55 send and receive Unix file descriptors over the D-Bus connection, mapping-
56 automatically to the D-Bus type 'h'.-
57-
58 Objects of type QDBusUnixFileDescriptors can be used also as parameters-
59 in signals and slots that get exported to D-Bus by registering with-
60 QDBusConnection::registerObject.-
61-
62 QDBusUnixFileDescriptor does not take ownership of the file descriptor.-
63 Instead, it will use the Unix system call \c dup(2) to make a copy of the-
64 file descriptor. This file descriptor belongs to the-
65 QDBusUnixFileDescriptor object and should not be stored or closed by the-
66 user. Instead, you should make your own copy if you need that.-
67-
68 \section2 Availability-
69-
70 Unix file descriptor passing is not available in all D-Bus connections.-
71 This feature is present with D-Bus library and bus daemon version 1.4 and-
72 upwards on Unix systems. Qt D-Bus automatically enables the feature if such-
73 a version was found at compile-time and run-time.-
74-
75 To verify that your connection does support passing file descriptors,-
76 check if the QDBusConnection::UnixFileDescriptorPassing capability is set-
77 with QDBusConnection::connectionCapabilities(). If the flag is not-
78 active, then you will not be able to make calls to methods that have-
79 QDBusUnixFileDescriptor as arguments or even embed such a type in a-
80 variant. You will also not receive calls containing that type.-
81-
82 Note also that remote applications may not have support for Unix file-
83 descriptor passing. If you make a D-Bus to a remote application that-
84 cannot receive such a type, you will receive an error reply. If you try-
85 to send a signal containing a D-Bus file descriptor or return one from a-
86 method call, the message will be silently dropped.-
87-
88 Even if the feature is not available, QDBusUnixFileDescriptor will-
89 continue to operate, so code need not have compile-time checks for the-
90 availability of this feature.-
91-
92 On non-Unix systems, QDBusUnixFileDescriptor will always report an-
93 invalid state and QDBusUnixFileDescriptor::isSupported() will return-
94 false.-
95-
96 \sa QDBusConnection::ConnectionCapabilities, QDBusConnection::connectionCapabilities()-
97*/-
98-
99/*!-
100 \typedef QDBusUnixFileDescriptor::Data-
101 \internal-
102*/-
103-
104/*!-
105 \variable QDBusUnixFileDescriptor::d-
106 \internal-
107*/-
108-
109class QDBusUnixFileDescriptorPrivate : public QSharedData {-
110public:-
111 QDBusUnixFileDescriptorPrivate() : fd(-1) { }
executed 24 times by 1 test: end of block
Executed by:
  • tst_qdbusmarshall - unknown status
24
112 QDBusUnixFileDescriptorPrivate(const QDBusUnixFileDescriptorPrivate &other)-
113 : QSharedData(other), fd(-1)-
114 { }
never executed: end of block
0
115 ~QDBusUnixFileDescriptorPrivate();-
116-
117 QAtomicInt fd;-
118};-
119-
120template<> inline-
121QExplicitlySharedDataPointer<QDBusUnixFileDescriptorPrivate>::~QExplicitlySharedDataPointer()-
122{
executed 104 times by 1 test: end of block
Executed by:
  • tst_qdbusmarshall - unknown status
executed 24 times by 1 test: delete d;
Executed by:
  • tst_qdbusmarshall - unknown status
dDescription
TRUEevaluated 94 times by 1 test
Evaluated by:
  • tst_qdbusmarshall - unknown status
FALSEevaluated 10 times by 1 test
Evaluated by:
  • tst_qdbusmarshall - unknown status
!d->ref.deref()Description
TRUEevaluated 24 times by 1 test
Evaluated by:
  • tst_qdbusmarshall - unknown status
FALSEevaluated 70 times by 1 test
Evaluated by:
  • tst_qdbusmarshall - unknown status
if (d && !d->ref.deref()) delete d; }
executed 104 times by 1 test: end of block
Executed by:
  • tst_qdbusmarshall - unknown status
executed 24 times by 1 test: delete d;
Executed by:
  • tst_qdbusmarshall - unknown status
dDescription
TRUEevaluated 94 times by 1 test
Evaluated by:
  • tst_qdbusmarshall - unknown status
FALSEevaluated 10 times by 1 test
Evaluated by:
  • tst_qdbusmarshall - unknown status
!d->ref.deref()Description
TRUEevaluated 24 times by 1 test
Evaluated by:
  • tst_qdbusmarshall - unknown status
FALSEevaluated 70 times by 1 test
Evaluated by:
  • tst_qdbusmarshall - unknown status
10-104
123-
124/*!-
125 Constructs a QDBusUnixFileDescriptor without a wrapped file descriptor.-
126 This is equivalent to constructing the object with an invalid file-
127 descriptor (like -1).-
128-
129 \sa fileDescriptor(), isValid()-
130*/-
131QDBusUnixFileDescriptor::QDBusUnixFileDescriptor()-
132 : d(0)-
133{-
134}
executed 23 times by 1 test: end of block
Executed by:
  • tst_qdbusmarshall - unknown status
23
135-
136/*!-
137 Constructs a QDBusUnixFileDescriptor object by copying the \a-
138 fileDescriptor parameter. The original file descriptor is not touched and-
139 must be closed by the user.-
140-
141 Note that the value returned by fileDescriptor() will be different from-
142 the \a fileDescriptor parameter passed.-
143-
144 If the \a fileDescriptor parameter is not valid, isValid() will return-
145 false and fileDescriptor() will return -1.-
146-
147 \sa setFileDescriptor(), fileDescriptor()-
148*/-
149QDBusUnixFileDescriptor::QDBusUnixFileDescriptor(int fileDescriptor)-
150 : d(0)-
151{-
152 if (fileDescriptor != -1)
fileDescriptor != -1Description
TRUEevaluated 7 times by 1 test
Evaluated by:
  • tst_qdbusmarshall - unknown status
FALSEevaluated 1 time by 1 test
Evaluated by:
  • tst_qdbusmarshall - unknown status
1-7
153 setFileDescriptor(fileDescriptor);
executed 7 times by 1 test: setFileDescriptor(fileDescriptor);
Executed by:
  • tst_qdbusmarshall - unknown status
7
154}
executed 8 times by 1 test: end of block
Executed by:
  • tst_qdbusmarshall - unknown status
8
155-
156/*!-
157 Constructs a QDBusUnixFileDescriptor object by copying \a other.-
158*/-
159QDBusUnixFileDescriptor::QDBusUnixFileDescriptor(const QDBusUnixFileDescriptor &other)-
160 : d(other.d)-
161{-
162}
executed 73 times by 1 test: end of block
Executed by:
  • tst_qdbusmarshall - unknown status
73
163-
164/*!-
165 Copies the Unix file descriptor from the \a other QDBusUnixFileDescriptor-
166 object. If the current object contained a file descriptor, it will be-
167 properly disposed of before.-
168*/-
169QDBusUnixFileDescriptor &QDBusUnixFileDescriptor::operator=(const QDBusUnixFileDescriptor &other)-
170{-
171 if (this != &other)
this != &otherDescription
TRUEnever evaluated
FALSEnever evaluated
0
172 d.operator=(other.d);
never executed: d.operator=(other.d);
0
173 return *this;
never executed: return *this;
0
174}-
175-
176/*!-
177 \fn QDBusUnixFileDescriptor &QDBusUnixFileDescriptor::operator=(QDBusUnixFileDescriptor &&other)-
178-
179 Move-assigns \a other to this QDBusUnixFileDescriptor.-
180*/-
181-
182/*!-
183 Destroys this QDBusUnixFileDescriptor object and disposes of the Unix file descriptor that it contained.-
184*/-
185QDBusUnixFileDescriptor::~QDBusUnixFileDescriptor()-
186{-
187}-
188-
189/*!-
190 \fn void QDBusUnixFileDescriptor::swap(QDBusUnixFileDescriptor &other)-
191 \since 5.0-
192-
193 Swaps this file descriptor instance with \a other. This function-
194 is very fast and never fails.-
195*/-
196-
197/*!-
198 Returns \c true if this Unix file descriptor is valid. A valid Unix file-
199 descriptor is not -1.-
200-
201 \sa fileDescriptor()-
202*/-
203bool QDBusUnixFileDescriptor::isValid() const-
204{-
205 return d ? d->fd.load() != -1 : false;
executed 6 times by 1 test: return d ? d->fd.load() != -1 : false;
Executed by:
  • tst_qdbusmarshall - unknown status
dDescription
TRUEevaluated 6 times by 1 test
Evaluated by:
  • tst_qdbusmarshall - unknown status
FALSEnever evaluated
0-6
206}-
207-
208/*!-
209 Returns the Unix file descriptor contained by this-
210 QDBusUnixFileDescriptor object. An invalid file descriptor is represented-
211 by the value -1.-
212-
213 Note that the file descriptor returned by this function is owned by the-
214 QDBusUnixFileDescriptor object and must not be stored past the lifetime-
215 of this object. It is ok to use it while this object is valid, but if one-
216 wants to store it for longer use, the file descriptor should be cloned-
217 using the Unix \c dup(2), \c dup2(2) or \c dup3(2) functions.-
218-
219 \sa isValid()-
220*/-
221int QDBusUnixFileDescriptor::fileDescriptor() const-
222{-
223 return d ? d->fd.load() : -1;
executed 27 times by 1 test: return d ? d->fd.load() : -1;
Executed by:
  • tst_qdbusmarshall - unknown status
dDescription
TRUEevaluated 25 times by 1 test
Evaluated by:
  • tst_qdbusmarshall - unknown status
FALSEevaluated 2 times by 1 test
Evaluated by:
  • tst_qdbusmarshall - unknown status
2-27
224}-
225-
226// actual implementation-
227#ifdef Q_OS_UNIX-
228-
229// qdoc documentation is generated on Unix-
230-
231/*!-
232 Returns \c true if Unix file descriptors are supported on this platform. In-
233 other words, this function returns \c true if this is a Unix platform.-
234-
235 Note that QDBusUnixFileDescriptor continues to operate even if this-
236 function returns \c false. The only difference is that the-
237 QDBusUnixFileDescriptor objects will always be in the isValid() == false-
238 state and fileDescriptor() will always return -1. The class will not-
239 consume any operating system resources.-
240*/-
241bool QDBusUnixFileDescriptor::isSupported()-
242{-
243 return true;
never executed: return true;
0
244}-
245-
246/*!-
247 Sets the file descriptor that this QDBusUnixFileDescriptor object holds-
248 to a copy of \a fileDescriptor. The original file descriptor is not-
249 touched and must be closed by the user.-
250-
251 Note that the value returned by fileDescriptor() will be different from-
252 the \a fileDescriptor parameter passed.-
253-
254 If the \a fileDescriptor parameter is not valid, isValid() will return-
255 false and fileDescriptor() will return -1.-
256-
257 \sa isValid(), fileDescriptor()-
258*/-
259void QDBusUnixFileDescriptor::setFileDescriptor(int fileDescriptor)-
260{-
261 if (fileDescriptor != -1)
fileDescriptor != -1Description
TRUEevaluated 7 times by 1 test
Evaluated by:
  • tst_qdbusmarshall - unknown status
FALSEnever evaluated
0-7
262 giveFileDescriptor(qt_safe_dup(fileDescriptor));
executed 7 times by 1 test: giveFileDescriptor(qt_safe_dup(fileDescriptor));
Executed by:
  • tst_qdbusmarshall - unknown status
7
263}
executed 7 times by 1 test: end of block
Executed by:
  • tst_qdbusmarshall - unknown status
7
264-
265/*!-
266 \internal-
267 Sets the Unix file descriptor to \a fileDescriptor without copying.-
268-
269 \sa setFileDescriptor()-
270*/-
271void QDBusUnixFileDescriptor::giveFileDescriptor(int fileDescriptor)-
272{-
273 // if we are the sole ref, d remains unchanged-
274 // if detaching happens, d->fd will be -1-
275 if (d)
dDescription
TRUEnever evaluated
FALSEevaluated 24 times by 1 test
Evaluated by:
  • tst_qdbusmarshall - unknown status
0-24
276 d.detach();
never executed: d.detach();
0
277 else-
278 d = new QDBusUnixFileDescriptorPrivate;
executed 24 times by 1 test: d = new QDBusUnixFileDescriptorPrivate;
Executed by:
  • tst_qdbusmarshall - unknown status
24
279-
280 const int fdl = d->fd.load();-
281 if (fdl != -1)
fdl != -1Description
TRUEnever evaluated
FALSEevaluated 24 times by 1 test
Evaluated by:
  • tst_qdbusmarshall - unknown status
0-24
282 qt_safe_close(fdl);
never executed: qt_safe_close(fdl);
0
283-
284 if (fileDescriptor != -1)
fileDescriptor != -1Description
TRUEevaluated 24 times by 1 test
Evaluated by:
  • tst_qdbusmarshall - unknown status
FALSEnever evaluated
0-24
285 d->fd.store(fileDescriptor);
executed 24 times by 1 test: d->fd.store(fileDescriptor);
Executed by:
  • tst_qdbusmarshall - unknown status
24
286}
executed 24 times by 1 test: end of block
Executed by:
  • tst_qdbusmarshall - unknown status
24
287-
288/*!-
289 \internal-
290 Extracts the Unix file descriptor from the QDBusUnixFileDescriptor object-
291 and transfers ownership.-
292-
293 Note: since QDBusUnixFileDescriptor is implicitly shared, this function-
294 is inherently racy and should be avoided.-
295*/-
296int QDBusUnixFileDescriptor::takeFileDescriptor()-
297{-
298 if (!d)
!dDescription
TRUEnever evaluated
FALSEnever evaluated
0
299 return -1;
never executed: return -1;
0
300-
301 return d->fd.fetchAndStoreRelaxed(-1);
never executed: return d->fd.fetchAndStoreRelaxed(-1);
0
302}-
303-
304QDBusUnixFileDescriptorPrivate::~QDBusUnixFileDescriptorPrivate()-
305{-
306 const int fdl = fd.load();-
307 if (fdl != -1)
fdl != -1Description
TRUEevaluated 24 times by 1 test
Evaluated by:
  • tst_qdbusmarshall - unknown status
FALSEnever evaluated
0-24
308 qt_safe_close(fdl);
executed 24 times by 1 test: qt_safe_close(fdl);
Executed by:
  • tst_qdbusmarshall - unknown status
24
309}
executed 24 times by 1 test: end of block
Executed by:
  • tst_qdbusmarshall - unknown status
24
310-
311#else-
312bool QDBusUnixFileDescriptor::isSupported()-
313{-
314 return false;-
315}-
316-
317void QDBusUnixFileDescriptor::setFileDescriptor(int)-
318{-
319}-
320-
321void QDBusUnixFileDescriptor::giveFileDescriptor(int)-
322{-
323}-
324-
325int QDBusUnixFileDescriptor::takeFileDescriptor()-
326{-
327 return -1;-
328}-
329-
330QDBusUnixFileDescriptorPrivate::~QDBusUnixFileDescriptorPrivate()-
331{-
332}-
333-
334#endif-
335-
336#endif // QT_NO_DBUS-
337-
338QT_END_NAMESPACE-
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9