OpenCoverage

qsystemsemaphore_systemv.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/corelib/kernel/qsystemsemaphore_systemv.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
1/****************************************************************************-
2**-
3** Copyright (C) 2016 The Qt Company Ltd.-
4** Contact: https://www.qt.io/licensing/-
5**-
6** This file is part of the QtCore module of the Qt Toolkit.-
7**-
8** $QT_BEGIN_LICENSE:LGPL$-
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 https://www.qt.io/terms-conditions. For further-
15** information use the contact form at https://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 3 as published by the Free Software-
20** Foundation and appearing in the file LICENSE.LGPL3 included in the-
21** packaging of this file. Please review the following information to-
22** ensure the GNU Lesser General Public License version 3 requirements-
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.-
24**-
25** GNU General Public License Usage-
26** Alternatively, this file may be used under the terms of the GNU-
27** General Public License version 2.0 or (at your option) the GNU General-
28** Public license version 3 or any later version approved by the KDE Free-
29** Qt Foundation. The licenses are as published by the Free Software-
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3-
31** included in the packaging of this file. Please review the following-
32** information to ensure the GNU General Public License requirements will-
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and-
34** https://www.gnu.org/licenses/gpl-3.0.html.-
35**-
36** $QT_END_LICENSE$-
37**-
38****************************************************************************/-
39-
40#include "qsystemsemaphore.h"-
41#include "qsystemsemaphore_p.h"-
42-
43#include <qdebug.h>-
44#include <qfile.h>-
45#include <qcoreapplication.h>-
46-
47#ifndef QT_POSIX_IPC-
48-
49#ifndef QT_NO_SYSTEMSEMAPHORE-
50-
51#include <sys/types.h>-
52#include <sys/ipc.h>-
53#include <sys/sem.h>-
54#include <fcntl.h>-
55#include <errno.h>-
56-
57#include "private/qcore_unix_p.h"-
58-
59// OpenBSD 4.2 doesn't define EIDRM, see BUGS section:-
60// http://www.openbsd.org/cgi-bin/man.cgi?query=semop&manpath=OpenBSD+4.2-
61#if defined(Q_OS_OPENBSD) && !defined(EIDRM)-
62#define EIDRM EINVAL-
63#endif-
64-
65QT_BEGIN_NAMESPACE-
66-
67/*!-
68 \internal-
69-
70 Setup unix_key-
71 */-
72key_t QSystemSemaphorePrivate::handle(QSystemSemaphore::AccessMode mode)-
73{-
74 if (key.isEmpty()){
key.isEmpty()Description
TRUEevaluated 59 times by 1 test
Evaluated by:
  • tst_QSharedMemory
FALSEevaluated 22452 times by 4 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_QSystemSemaphore
  • tst_qsharedmemory - unknown status
  • tst_qsystemsemaphore - unknown status
59-22452
75 errorString = QCoreApplication::tr("%1: key is empty", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle:"));-
76 error = QSystemSemaphore::KeyError;-
77 return -1;
executed 59 times by 1 test: return -1;
Executed by:
  • tst_QSharedMemory
59
78 }-
79-
80 // ftok requires that an actual file exists somewhere-
81 if (-1 != unix_key)
-1 != unix_keyDescription
TRUEevaluated 15320 times by 4 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_QSystemSemaphore
  • tst_qsharedmemory - unknown status
  • tst_qsystemsemaphore - unknown status
FALSEevaluated 7132 times by 4 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_QSystemSemaphore
  • tst_qsharedmemory - unknown status
  • tst_qsystemsemaphore - unknown status
7132-15320
82 return unix_key;
executed 15320 times by 4 tests: return unix_key;
Executed by:
  • tst_QSharedMemory
  • tst_QSystemSemaphore
  • tst_qsharedmemory - unknown status
  • tst_qsystemsemaphore - unknown status
15320
83-
84 // Create the file needed for ftok-
85 int built = QSharedMemoryPrivate::createUnixKeyFile(fileName);-
86 if (-1 == built) {
-1 == builtDescription
TRUEnever evaluated
FALSEevaluated 7132 times by 4 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_QSystemSemaphore
  • tst_qsharedmemory - unknown status
  • tst_qsystemsemaphore - unknown status
0-7132
87 errorString = QCoreApplication::tr("%1: unable to make key", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle:"));-
88 error = QSystemSemaphore::KeyError;-
89 return -1;
never executed: return -1;
0
90 }-
91 createdFile = (1 == built);-
92-
93 // Get the unix key for the created file-
94 unix_key = qt_safe_ftok(QFile::encodeName(fileName), 'Q');-
95 if (-1 == unix_key) {
-1 == unix_keyDescription
TRUEnever evaluated
FALSEevaluated 7132 times by 4 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_QSystemSemaphore
  • tst_qsharedmemory - unknown status
  • tst_qsystemsemaphore - unknown status
0-7132
96 errorString = QCoreApplication::tr("%1: ftok failed", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle:"));-
97 error = QSystemSemaphore::KeyError;-
98 return -1;
never executed: return -1;
0
99 }-
100-
101 // Get semaphore-
102 semaphore = semget(unix_key, 1, 0600 | IPC_CREAT | IPC_EXCL);-
103 if (-1 == semaphore) {
-1 == semaphoreDescription
TRUEevaluated 3613 times by 3 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_qsharedmemory - unknown status
  • tst_qsystemsemaphore - unknown status
FALSEevaluated 3519 times by 3 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_QSystemSemaphore
  • tst_qsharedmemory - unknown status
3519-3613
104 if (errno == EEXIST)
(*__errno_location ()) == 17Description
TRUEevaluated 3611 times by 3 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_qsharedmemory - unknown status
  • tst_qsystemsemaphore - unknown status
FALSEevaluated 2 times by 1 test
Evaluated by:
  • tst_QSharedMemory
2-3611
105 semaphore = semget(unix_key, 1, 0600 | IPC_CREAT);
executed 3611 times by 3 tests: semaphore = semget(unix_key, 1, 0600 | 01000);
Executed by:
  • tst_QSharedMemory
  • tst_qsharedmemory - unknown status
  • tst_qsystemsemaphore - unknown status
3611
106 if (-1 == semaphore) {
-1 == semaphoreDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_QSharedMemory
FALSEevaluated 3611 times by 3 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_qsharedmemory - unknown status
  • tst_qsystemsemaphore - unknown status
2-3611
107 setErrorString(QLatin1String("QSystemSemaphore::handle"));-
108 cleanHandle();-
109 return -1;
executed 2 times by 1 test: return -1;
Executed by:
  • tst_QSharedMemory
2
110 }-
111 } else {
executed 3611 times by 3 tests: end of block
Executed by:
  • tst_QSharedMemory
  • tst_qsharedmemory - unknown status
  • tst_qsystemsemaphore - unknown status
3611
112 createdSemaphore = true;-
113 // Force cleanup of file, it is possible that it can be left over from a crash-
114 createdFile = true;-
115 }
executed 3519 times by 3 tests: end of block
Executed by:
  • tst_QSharedMemory
  • tst_QSystemSemaphore
  • tst_qsharedmemory - unknown status
3519
116-
117 if (mode == QSystemSemaphore::Create) {
mode == QSyste...aphore::CreateDescription
TRUEevaluated 3513 times by 3 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_QSystemSemaphore
  • tst_qsharedmemory - unknown status
FALSEevaluated 3617 times by 4 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_QSystemSemaphore
  • tst_qsharedmemory - unknown status
  • tst_qsystemsemaphore - unknown status
3513-3617
118 createdSemaphore = true;-
119 createdFile = true;-
120 }
executed 3513 times by 3 tests: end of block
Executed by:
  • tst_QSharedMemory
  • tst_QSystemSemaphore
  • tst_qsharedmemory - unknown status
3513
121-
122 // Created semaphore so initialize its value.-
123 if (createdSemaphore && initialValue >= 0) {
createdSemaphoreDescription
TRUEevaluated 6987 times by 3 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_QSystemSemaphore
  • tst_qsharedmemory - unknown status
FALSEevaluated 143 times by 2 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_qsystemsemaphore - unknown status
initialValue >= 0Description
TRUEevaluated 6987 times by 3 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_QSystemSemaphore
  • tst_qsharedmemory - unknown status
FALSEnever evaluated
0-6987
124 qt_semun init_op;-
125 init_op.val = initialValue;-
126 if (-1 == semctl(semaphore, 0, SETVAL, init_op)) {
-1 == semctl(s..., 16, init_op)Description
TRUEnever evaluated
FALSEevaluated 6987 times by 3 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_QSystemSemaphore
  • tst_qsharedmemory - unknown status
0-6987
127 setErrorString(QLatin1String("QSystemSemaphore::handle"));-
128 cleanHandle();-
129 return -1;
never executed: return -1;
0
130 }-
131 }
executed 6987 times by 3 tests: end of block
Executed by:
  • tst_QSharedMemory
  • tst_QSystemSemaphore
  • tst_qsharedmemory - unknown status
6987
132-
133 return unix_key;
executed 7130 times by 4 tests: return unix_key;
Executed by:
  • tst_QSharedMemory
  • tst_QSystemSemaphore
  • tst_qsharedmemory - unknown status
  • tst_qsystemsemaphore - unknown status
7130
134}-
135-
136/*!-
137 \internal-
138-
139 Cleanup the unix_key-
140 */-
141void QSystemSemaphorePrivate::cleanHandle()-
142{-
143 unix_key = -1;-
144-
145 // remove the file if we made it-
146 if (createdFile) {
createdFileDescription
TRUEevaluated 3522 times by 2 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_QSystemSemaphore
FALSEevaluated 3937 times by 5 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_QSystemSemaphore
  • tst_qapplication - unknown status
  • tst_qsharedmemory - unknown status
  • tst_qsystemsemaphore - unknown status
3522-3937
147 QFile::remove(fileName);-
148 createdFile = false;-
149 }
executed 3522 times by 2 tests: end of block
Executed by:
  • tst_QSharedMemory
  • tst_QSystemSemaphore
3522
150-
151 if (createdSemaphore) {
createdSemaphoreDescription
TRUEevaluated 3520 times by 2 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_QSystemSemaphore
FALSEevaluated 3939 times by 5 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_QSystemSemaphore
  • tst_qapplication - unknown status
  • tst_qsharedmemory - unknown status
  • tst_qsystemsemaphore - unknown status
3520-3939
152 if (-1 != semaphore) {
-1 != semaphoreDescription
TRUEevaluated 3519 times by 2 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_QSystemSemaphore
FALSEevaluated 1 time by 1 test
Evaluated by:
  • tst_QSharedMemory
1-3519
153 if (-1 == semctl(semaphore, 0, IPC_RMID, 0)) {
-1 == semctl(s...hore, 0, 0, 0)Description
TRUEnever evaluated
FALSEevaluated 3519 times by 2 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_QSystemSemaphore
0-3519
154 setErrorString(QLatin1String("QSystemSemaphore::cleanHandle"));-
155#if defined QSYSTEMSEMAPHORE_DEBUG-
156 qDebug("QSystemSemaphore::cleanHandle semctl failed.");-
157#endif-
158 }
never executed: end of block
0
159 semaphore = -1;-
160 }
executed 3519 times by 2 tests: end of block
Executed by:
  • tst_QSharedMemory
  • tst_QSystemSemaphore
3519
161 createdSemaphore = false;-
162 }
executed 3520 times by 2 tests: end of block
Executed by:
  • tst_QSharedMemory
  • tst_QSystemSemaphore
3520
163}
executed 7459 times by 5 tests: end of block
Executed by:
  • tst_QSharedMemory
  • tst_QSystemSemaphore
  • tst_qapplication - unknown status
  • tst_qsharedmemory - unknown status
  • tst_qsystemsemaphore - unknown status
7459
164-
165/*!-
166 \internal-
167 */-
168bool QSystemSemaphorePrivate::modifySemaphore(int count)-
169{-
170 if (-1 == handle())
-1 == handle()Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_QSharedMemory
FALSEevaluated 15320 times by 4 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_QSystemSemaphore
  • tst_qsharedmemory - unknown status
  • tst_qsystemsemaphore - unknown status
2-15320
171 return false;
executed 2 times by 1 test: return false;
Executed by:
  • tst_QSharedMemory
2
172-
173 struct sembuf operation;-
174 operation.sem_num = 0;-
175 operation.sem_op = count;-
176 operation.sem_flg = SEM_UNDO;-
177-
178 int res;-
179 EINTR_LOOP(res, semop(semaphore, &operation, 1));
executed 15320 times by 4 tests: end of block
Executed by:
  • tst_QSharedMemory
  • tst_QSystemSemaphore
  • tst_qsharedmemory - unknown status
  • tst_qsystemsemaphore - unknown status
res == -1Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_QSharedMemory
FALSEevaluated 15318 times by 4 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_QSystemSemaphore
  • tst_qsharedmemory - unknown status
  • tst_qsystemsemaphore - unknown status
(*__errno_location ()) == 4Description
TRUEnever evaluated
FALSEevaluated 2 times by 1 test
Evaluated by:
  • tst_QSharedMemory
0-15320
180 if (-1 == res) {
-1 == resDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_QSharedMemory
FALSEevaluated 15318 times by 4 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_QSystemSemaphore
  • tst_qsharedmemory - unknown status
  • tst_qsystemsemaphore - unknown status
2-15318
181 // If the semaphore was removed be nice and create it and then modifySemaphore again-
182 if (errno == EINVAL || errno == EIDRM) {
(*__errno_location ()) == 22Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_QSharedMemory
FALSEnever evaluated
(*__errno_location ()) == 43Description
TRUEnever evaluated
FALSEnever evaluated
0-2
183 semaphore = -1;-
184 cleanHandle();-
185 handle();-
186 return modifySemaphore(count);
executed 2 times by 1 test: return modifySemaphore(count);
Executed by:
  • tst_QSharedMemory
2
187 }-
188 setErrorString(QLatin1String("QSystemSemaphore::modifySemaphore"));-
189#if defined QSYSTEMSEMAPHORE_DEBUG-
190 qDebug() << QLatin1String("QSystemSemaphore::modify failed") << count << semctl(semaphore, 0, GETVAL) << errno << EIDRM << EINVAL;-
191#endif-
192 return false;
never executed: return false;
0
193 }-
194-
195 clearError();-
196 return true;
executed 15318 times by 4 tests: return true;
Executed by:
  • tst_QSharedMemory
  • tst_QSystemSemaphore
  • tst_qsharedmemory - unknown status
  • tst_qsystemsemaphore - unknown status
15318
197}-
198-
199-
200QT_END_NAMESPACE-
201-
202#endif // QT_NO_SYSTEMSEMAPHORE-
203-
204#endif // QT_POSIX_IPC-
Source codeSwitch to Preprocessed file

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