Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/io/qlockfile_unix.cpp |
Switch to Source code | Preprocessed file |
Line | Source | Count | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | - | |||||||||||||||||||||||||
2 | - | |||||||||||||||||||||||||
3 | - | |||||||||||||||||||||||||
4 | - | |||||||||||||||||||||||||
5 | - | |||||||||||||||||||||||||
6 | static QByteArray localHostName() | - | ||||||||||||||||||||||||
7 | { | - | ||||||||||||||||||||||||
8 | QByteArray hostName(512, Qt::Uninitialized); | - | ||||||||||||||||||||||||
9 | if (gethostname(hostName.data(), hostName.size()) == -1
| 0-608 | ||||||||||||||||||||||||
10 | return never executed: QByteArray();return QByteArray(); never executed: return QByteArray(); | 0 | ||||||||||||||||||||||||
11 | hostName.truncate(strlen(hostName.data())); | - | ||||||||||||||||||||||||
12 | return executed 608 times by 8 tests: hostName;return hostName; Executed by:
executed 608 times by 8 tests: return hostName; Executed by:
| 608 | ||||||||||||||||||||||||
13 | } | - | ||||||||||||||||||||||||
14 | - | |||||||||||||||||||||||||
15 | - | |||||||||||||||||||||||||
16 | static qint64 qt_write_loop(int fd, const char *data, qint64 len) | - | ||||||||||||||||||||||||
17 | { | - | ||||||||||||||||||||||||
18 | qint64 pos = 0; | - | ||||||||||||||||||||||||
19 | while (pos < len
| 578 | ||||||||||||||||||||||||
20 | const qint64 ret = qt_safe_write(fd, data + pos, len - pos); | - | ||||||||||||||||||||||||
21 | if (ret == -1
| 0-578 | ||||||||||||||||||||||||
22 | return never executed: pos;return pos; never executed: return pos; | 0 | ||||||||||||||||||||||||
23 | pos += ret; | - | ||||||||||||||||||||||||
24 | } executed 578 times by 8 tests: end of block Executed by:
| 578 | ||||||||||||||||||||||||
25 | return executed 578 times by 8 tests: pos;return pos; Executed by:
executed 578 times by 8 tests: return pos; Executed by:
| 578 | ||||||||||||||||||||||||
26 | } | - | ||||||||||||||||||||||||
27 | - | |||||||||||||||||||||||||
28 | int QLockFilePrivate::checkFcntlWorksAfterFlock(const QString &fn) | - | ||||||||||||||||||||||||
29 | { | - | ||||||||||||||||||||||||
30 | - | |||||||||||||||||||||||||
31 | QTemporaryFile file(fn); | - | ||||||||||||||||||||||||
32 | if (!file.open()
| 0-23 | ||||||||||||||||||||||||
33 | return never executed: 0;return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
34 | const int fd = file.d_func()->engine()->handle(); | - | ||||||||||||||||||||||||
35 | - | |||||||||||||||||||||||||
36 | if (flock(fd, 2 | 4) == -1
| 0-23 | ||||||||||||||||||||||||
37 | return never executed: 0;return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
38 | - | |||||||||||||||||||||||||
39 | struct flock flockData; | - | ||||||||||||||||||||||||
40 | flockData.l_type = 1; | - | ||||||||||||||||||||||||
41 | flockData.l_whence = 0; | - | ||||||||||||||||||||||||
42 | flockData.l_start = 0; | - | ||||||||||||||||||||||||
43 | flockData.l_len = 0; | - | ||||||||||||||||||||||||
44 | flockData.l_pid = getpid(); | - | ||||||||||||||||||||||||
45 | if (fcntl(fd, 6, &flockData) == -1
| 0-23 | ||||||||||||||||||||||||
46 | return never executed: 0;return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
47 | return executed 23 times by 8 tests: 1;return 1; Executed by:
executed 23 times by 8 tests: return 1; Executed by:
| 23 | ||||||||||||||||||||||||
48 | - | |||||||||||||||||||||||||
49 | - | |||||||||||||||||||||||||
50 | - | |||||||||||||||||||||||||
51 | } | - | ||||||||||||||||||||||||
52 | - | |||||||||||||||||||||||||
53 | - | |||||||||||||||||||||||||
54 | - | |||||||||||||||||||||||||
55 | - | |||||||||||||||||||||||||
56 | - | |||||||||||||||||||||||||
57 | - | |||||||||||||||||||||||||
58 | - | |||||||||||||||||||||||||
59 | typedef QCache<QString, bool> CacheType; | - | ||||||||||||||||||||||||
60 | namespace { namespace Q_QGS_fcntlOK { typedef CacheType Type; QBasicAtomicInt guard = { QtGlobalStatic::Uninitialized }; __attribute__((visibility("hidden"))) inline Type *innerFunction() { struct HolderBase { ~HolderBase() noexcept { if (guard.load() == QtGlobalStatic::Initialized
executed 7 times by 7 tests: }guard.store(QtGlobalStatic::Destroyed); Executed by:
executed 7 times by 7 tests: }; static struct Holder : public HolderBase { Type value; Holder() noexcept(noexcept(Type (10))) : value (10) { guard.store(QtGlobalStatic::Initialized); } } holder; returnend of block Executed by:
executed 600 times by 7 tests: &holder.value;return &holder.value; Executed by:
executed 600 times by 7 tests: } } } static QGlobalStatic<CacheType, Q_QGS_fcntlOK::innerFunction, Q_QGS_fcntlOK::guard> fcntlOK;;return &holder.value; Executed by:
| 0-600 | ||||||||||||||||||||||||
61 | static QBasicMutex fcntlLock; | - | ||||||||||||||||||||||||
62 | - | |||||||||||||||||||||||||
63 | - | |||||||||||||||||||||||||
64 | - | |||||||||||||||||||||||||
65 | - | |||||||||||||||||||||||||
66 | - | |||||||||||||||||||||||||
67 | - | |||||||||||||||||||||||||
68 | static bool fcntlWorksAfterFlock(const QString &fn) | - | ||||||||||||||||||||||||
69 | { | - | ||||||||||||||||||||||||
70 | QMutexLocker lock(&fcntlLock); | - | ||||||||||||||||||||||||
71 | if (fcntlOK.isDestroyed()
| 1-578 | ||||||||||||||||||||||||
72 | return executed 1 time by 1 test: QLockFilePrivate::checkFcntlWorksAfterFlock(fn);return QLockFilePrivate::checkFcntlWorksAfterFlock(fn); Executed by:
executed 1 time by 1 test: return QLockFilePrivate::checkFcntlWorksAfterFlock(fn); Executed by:
| 1 | ||||||||||||||||||||||||
73 | bool *worksPtr = fcntlOK->object(fn); | - | ||||||||||||||||||||||||
74 | if (worksPtr
| 22-556 | ||||||||||||||||||||||||
75 | return executed 556 times by 7 tests: *worksPtr;return *worksPtr; Executed by:
executed 556 times by 7 tests: return *worksPtr; Executed by:
| 556 | ||||||||||||||||||||||||
76 | - | |||||||||||||||||||||||||
77 | const bool val = QLockFilePrivate::checkFcntlWorksAfterFlock(fn); | - | ||||||||||||||||||||||||
78 | worksPtr = new bool(val); | - | ||||||||||||||||||||||||
79 | fcntlOK->insert(fn, worksPtr); | - | ||||||||||||||||||||||||
80 | - | |||||||||||||||||||||||||
81 | return executed 22 times by 7 tests: val;return val; Executed by:
executed 22 times by 7 tests: return val; Executed by:
| 22 | ||||||||||||||||||||||||
82 | } | - | ||||||||||||||||||||||||
83 | - | |||||||||||||||||||||||||
84 | static bool setNativeLocks(const QString &fileName, int fd) | - | ||||||||||||||||||||||||
85 | { | - | ||||||||||||||||||||||||
86 | - | |||||||||||||||||||||||||
87 | if (flock(fd, 2 | 4) == -1
| 0-579 | ||||||||||||||||||||||||
88 | return never executed: false;return false; never executed: return false; | 0 | ||||||||||||||||||||||||
89 | - | |||||||||||||||||||||||||
90 | struct flock flockData; | - | ||||||||||||||||||||||||
91 | flockData.l_type = 1; | - | ||||||||||||||||||||||||
92 | flockData.l_whence = 0; | - | ||||||||||||||||||||||||
93 | flockData.l_start = 0; | - | ||||||||||||||||||||||||
94 | flockData.l_len = 0; | - | ||||||||||||||||||||||||
95 | flockData.l_pid = getpid(); | - | ||||||||||||||||||||||||
96 | if (fcntlWorksAfterFlock(QDir::cleanPath(QFileInfo(fileName).absolutePath()) + QString('/'))
| 0-579 | ||||||||||||||||||||||||
97 | && fcntl(fd, 6, &flockData) == -1
| 0-579 | ||||||||||||||||||||||||
98 | return never executed: false;return false; never executed: return false; | 0 | ||||||||||||||||||||||||
99 | } | - | ||||||||||||||||||||||||
100 | return executed 579 times by 8 tests: true;return true; Executed by:
executed 579 times by 8 tests: return true; Executed by:
| 579 | ||||||||||||||||||||||||
101 | } | - | ||||||||||||||||||||||||
102 | - | |||||||||||||||||||||||||
103 | QLockFile::LockError QLockFilePrivate::tryLock_sys() | - | ||||||||||||||||||||||||
104 | { | - | ||||||||||||||||||||||||
105 | - | |||||||||||||||||||||||||
106 | - | |||||||||||||||||||||||||
107 | - | |||||||||||||||||||||||||
108 | QByteArray fileData = QByteArray::number(QCoreApplication::applicationPid()) % '\n' | - | ||||||||||||||||||||||||
109 | % QCoreApplication::applicationName().toUtf8() % '\n' | - | ||||||||||||||||||||||||
110 | % localHostName() % '\n'; | - | ||||||||||||||||||||||||
111 | - | |||||||||||||||||||||||||
112 | const QByteArray lockFileName = QFile::encodeName(fileName); | - | ||||||||||||||||||||||||
113 | const int fd = qt_safe_open(lockFileName.constData(), 01 | 0100 | 0200, 0666); | - | ||||||||||||||||||||||||
114 | if (fd < 0
| 18-578 | ||||||||||||||||||||||||
115 | switch ((*__errno_location ())) { | - | ||||||||||||||||||||||||
116 | case executed 17 times by 1 test: 17:case 17: Executed by:
executed 17 times by 1 test: case 17: Executed by:
| 17 | ||||||||||||||||||||||||
117 | return executed 17 times by 1 test: QLockFile::LockFailedError;return QLockFile::LockFailedError; Executed by:
executed 17 times by 1 test: return QLockFile::LockFailedError; Executed by:
| 17 | ||||||||||||||||||||||||
118 | case executed 1 time by 1 test: 13:case 13: Executed by:
executed 1 time by 1 test: case 13: Executed by:
| 1 | ||||||||||||||||||||||||
119 | case never executed: 30:case 30: never executed: case 30: | 0 | ||||||||||||||||||||||||
120 | return executed 1 time by 1 test: QLockFile::PermissionError;return QLockFile::PermissionError; Executed by:
executed 1 time by 1 test: return QLockFile::PermissionError; Executed by:
| 1 | ||||||||||||||||||||||||
121 | default never executed: :default: never executed: default: | 0 | ||||||||||||||||||||||||
122 | return never executed: QLockFile::UnknownError;return QLockFile::UnknownError; never executed: return QLockFile::UnknownError; | 0 | ||||||||||||||||||||||||
123 | } | - | ||||||||||||||||||||||||
124 | } | - | ||||||||||||||||||||||||
125 | - | |||||||||||||||||||||||||
126 | if (!setNativeLocks(fileName, fd)
| 0-578 | ||||||||||||||||||||||||
127 | const int errnoSaved = (*__errno_location ()); | - | ||||||||||||||||||||||||
128 | QMessageLogger(__FILE__, 194, __PRETTY_FUNCTION__).warning() << "setNativeLocks failed:" << qt_error_string(errnoSaved); | - | ||||||||||||||||||||||||
129 | } never executed: end of block | 0 | ||||||||||||||||||||||||
130 | - | |||||||||||||||||||||||||
131 | if (qt_write_loop(fd, fileData.constData(), fileData.size()) < fileData.size()
| 0-578 | ||||||||||||||||||||||||
132 | close(fd); | - | ||||||||||||||||||||||||
133 | if (!QFile::remove(fileName)
| 0 | ||||||||||||||||||||||||
134 | QMessageLogger(__FILE__, 200, __PRETTY_FUNCTION__).warning("QLockFile: Could not remove our own lock file %s.", QString(fileName).toLocal8Bit().constData()); never executed: QMessageLogger(__FILE__, 200, __PRETTY_FUNCTION__).warning("QLockFile: Could not remove our own lock file %s.", QString(fileName).toLocal8Bit().constData()); | 0 | ||||||||||||||||||||||||
135 | return never executed: QLockFile::UnknownError;return QLockFile::UnknownError; never executed: return QLockFile::UnknownError; | 0 | ||||||||||||||||||||||||
136 | } | - | ||||||||||||||||||||||||
137 | - | |||||||||||||||||||||||||
138 | - | |||||||||||||||||||||||||
139 | fileHandle = fd; | - | ||||||||||||||||||||||||
140 | - | |||||||||||||||||||||||||
141 | - | |||||||||||||||||||||||||
142 | - | |||||||||||||||||||||||||
143 | fdatasync(fileHandle); | - | ||||||||||||||||||||||||
144 | - | |||||||||||||||||||||||||
145 | - | |||||||||||||||||||||||||
146 | - | |||||||||||||||||||||||||
147 | - | |||||||||||||||||||||||||
148 | return executed 578 times by 8 tests: QLockFile::NoError;return QLockFile::NoError; Executed by:
executed 578 times by 8 tests: return QLockFile::NoError; Executed by:
| 578 | ||||||||||||||||||||||||
149 | } | - | ||||||||||||||||||||||||
150 | - | |||||||||||||||||||||||||
151 | bool QLockFilePrivate::removeStaleLock() | - | ||||||||||||||||||||||||
152 | { | - | ||||||||||||||||||||||||
153 | const QByteArray lockFileName = QFile::encodeName(fileName); | - | ||||||||||||||||||||||||
154 | const int fd = qt_safe_open(lockFileName.constData(), 01, 0666); | - | ||||||||||||||||||||||||
155 | if (fd < 0
| 0-1 | ||||||||||||||||||||||||
156 | return never executed: false;return false; never executed: return false; | 0 | ||||||||||||||||||||||||
157 | bool success = setNativeLocks(fileName, fd)
| 0-1 | ||||||||||||||||||||||||
158 | close(fd); | - | ||||||||||||||||||||||||
159 | return executed 1 time by 1 test: success;return success; Executed by:
executed 1 time by 1 test: return success; Executed by:
| 1 | ||||||||||||||||||||||||
160 | } | - | ||||||||||||||||||||||||
161 | - | |||||||||||||||||||||||||
162 | bool QLockFilePrivate::isApparentlyStale() const | - | ||||||||||||||||||||||||
163 | { | - | ||||||||||||||||||||||||
164 | qint64 pid; | - | ||||||||||||||||||||||||
165 | QString hostname, appname; | - | ||||||||||||||||||||||||
166 | if (getLockInfo(&pid, &hostname, &appname)
| 2-12 | ||||||||||||||||||||||||
167 | if (hostname.isEmpty()
| 0-12 | ||||||||||||||||||||||||
168 | if (::
| 0-12 | ||||||||||||||||||||||||
169 | return never executed: true;return true; never executed: return true; | 0 | ||||||||||||||||||||||||
170 | const QString processName = processNameByPid(pid); | - | ||||||||||||||||||||||||
171 | if (!processName.isEmpty()
| 0-12 | ||||||||||||||||||||||||
172 | QFileInfo fi(appname); | - | ||||||||||||||||||||||||
173 | if (fi.isSymLink()
| 0-12 | ||||||||||||||||||||||||
174 | fi.setFile(fi.symLinkTarget()); never executed: fi.setFile(fi.symLinkTarget()); | 0 | ||||||||||||||||||||||||
175 | if (processName != fi.fileName()
| 0-12 | ||||||||||||||||||||||||
176 | return never executed: true;return true; never executed: return true; | 0 | ||||||||||||||||||||||||
177 | } executed 12 times by 1 test: end of block Executed by:
| 12 | ||||||||||||||||||||||||
178 | } executed 12 times by 1 test: end of block Executed by:
| 12 | ||||||||||||||||||||||||
179 | } executed 12 times by 1 test: end of block Executed by:
| 12 | ||||||||||||||||||||||||
180 | const qint64 age = QFileInfo(fileName).lastModified().msecsTo(QDateTime::currentDateTime()); | - | ||||||||||||||||||||||||
181 | return executed 14 times by 1 test: staleLockTime > 0return staleLockTime > 0 && age > staleLockTime; Executed by:
executed 14 times by 1 test: return staleLockTime > 0 && age > staleLockTime; Executed by:
| 0-14 | ||||||||||||||||||||||||
182 | } | - | ||||||||||||||||||||||||
183 | - | |||||||||||||||||||||||||
184 | QString QLockFilePrivate::processNameByPid(qint64 pid) | - | ||||||||||||||||||||||||
185 | { | - | ||||||||||||||||||||||||
186 | - | |||||||||||||||||||||||||
187 | - | |||||||||||||||||||||||||
188 | - | |||||||||||||||||||||||||
189 | - | |||||||||||||||||||||||||
190 | - | |||||||||||||||||||||||||
191 | if (!QFile::exists(([]() -> QString { enum { Size = sizeof(u"" "/proc/version")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "/proc/version" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return executed 12 times by 1 test: qstring_literal_temp;return qstring_literal_temp; Executed by:
executed 12 times by 1 test: }()))return qstring_literal_temp; Executed by:
| 0-12 | ||||||||||||||||||||||||
192 | return never executed: QString();return QString(); never executed: return QString(); | 0 | ||||||||||||||||||||||||
193 | char exePath[64]; | - | ||||||||||||||||||||||||
194 | char buf[4096 + 1]; | - | ||||||||||||||||||||||||
195 | sprintf(exePath, "/proc/%lld/exe", pid); | - | ||||||||||||||||||||||||
196 | size_t len = (size_t)readlink(exePath, buf, sizeof(buf)); | - | ||||||||||||||||||||||||
197 | if (len >= sizeof(buf)
| 0-12 | ||||||||||||||||||||||||
198 | - | |||||||||||||||||||||||||
199 | return never executed: ([]() -> QString { enum { Size = sizeof(u"" "/ERROR/")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "/ERROR/" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); returnreturn ([]() -> QString { enum { Size = sizeof(u"" "/ERROR/")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "/ERROR/" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }()); never executed: qstring_literal_temp;return qstring_literal_temp; never executed: }());return qstring_literal_temp; never executed: return ([]() -> QString { enum { Size = sizeof(u"" "/ERROR/")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "/ERROR/" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }()); | 0 | ||||||||||||||||||||||||
200 | } | - | ||||||||||||||||||||||||
201 | buf[len] = 0; | - | ||||||||||||||||||||||||
202 | return executed 12 times by 1 test: QFileInfo(QFile::decodeName(buf)).fileName();return QFileInfo(QFile::decodeName(buf)).fileName(); Executed by:
executed 12 times by 1 test: return QFileInfo(QFile::decodeName(buf)).fileName(); Executed by:
| 12 | ||||||||||||||||||||||||
203 | } | - | ||||||||||||||||||||||||
204 | - | |||||||||||||||||||||||||
205 | void QLockFile::unlock() | - | ||||||||||||||||||||||||
206 | { | - | ||||||||||||||||||||||||
207 | QLockFilePrivate * const d = d_func(); | - | ||||||||||||||||||||||||
208 | if (!d->isLocked
| 578-3626 | ||||||||||||||||||||||||
209 | return; executed 3626 times by 31 tests: return; Executed by:
| 3626 | ||||||||||||||||||||||||
210 | close(d->fileHandle); | - | ||||||||||||||||||||||||
211 | d->fileHandle = -1; | - | ||||||||||||||||||||||||
212 | if (!QFile::remove(d->fileName)
| 0-578 | ||||||||||||||||||||||||
213 | QMessageLogger(__FILE__, 311, __PRETTY_FUNCTION__).warning() << "Could not remove our own lock file" << d->fileName << "maybe permissions changed meanwhile?"; | - | ||||||||||||||||||||||||
214 | - | |||||||||||||||||||||||||
215 | } never executed: end of block | 0 | ||||||||||||||||||||||||
216 | d->lockError = QLockFile::NoError; | - | ||||||||||||||||||||||||
217 | d->isLocked = false; | - | ||||||||||||||||||||||||
218 | } executed 578 times by 8 tests: end of block Executed by:
| 578 | ||||||||||||||||||||||||
219 | - | |||||||||||||||||||||||||
220 | - | |||||||||||||||||||||||||
Switch to Source code | Preprocessed file |