summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin Ottens <ervin@kde.org>2013-10-09 13:12:11 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-10-11 18:47:27 +0200
commitd8d6880682c8d3ad52a2b5e5670560dca399d854 (patch)
tree93fb3ac61a4419b8deebcf3335d3633f205a7029
parent059857d4d5dc5862d0135c839e7dabaa7331e4b5 (diff)
Reduce risks of lock file corruption
Trying to do as few operations as possible once the file got opened. That includes generating the data which will go in the file. Indeed if we crashed at that point the lock file is already emptied. Could happen because of the qAppName call. It's then safer to prepare the data upfront, and just open/write/close if possible. Change-Id: Iad32fa822c6a5958ae89d84a2fe02ed5366ea278 Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@digia.com> Reviewed-by: Frederik Gladhorn <frederik.gladhorn@digia.com>
-rw-r--r--src/corelib/io/qlockfile_unix.cpp20
1 files changed, 10 insertions, 10 deletions
diff --git a/src/corelib/io/qlockfile_unix.cpp b/src/corelib/io/qlockfile_unix.cpp
index 1676b71133..de21ee217a 100644
--- a/src/corelib/io/qlockfile_unix.cpp
+++ b/src/corelib/io/qlockfile_unix.cpp
@@ -140,6 +140,16 @@ static bool setNativeLocks(int fd)
QLockFile::LockError QLockFilePrivate::tryLock_sys()
{
+ // Assemble data, to write in a single call to write
+ // (otherwise we'd have to check every write call)
+ QByteArray fileData;
+ fileData += QByteArray::number(QCoreApplication::applicationPid());
+ fileData += '\n';
+ fileData += qAppName().toUtf8();
+ fileData += '\n';
+ fileData += localHostName().toUtf8();
+ fileData += '\n';
+
const QByteArray lockFileName = QFile::encodeName(fileName);
const int fd = qt_safe_open(lockFileName.constData(), O_WRONLY | O_CREAT | O_EXCL, 0644);
if (fd < 0) {
@@ -160,16 +170,6 @@ QLockFile::LockError QLockFilePrivate::tryLock_sys()
// We hold the lock, continue.
fileHandle = fd;
- // Assemble data, to write in a single call to write
- // (otherwise we'd have to check every write call)
- QByteArray fileData;
- fileData += QByteArray::number(QCoreApplication::applicationPid());
- fileData += '\n';
- fileData += qAppName().toUtf8();
- fileData += '\n';
- fileData += localHostName().toUtf8();
- fileData += '\n';
-
QLockFile::LockError error = QLockFile::NoError;
if (qt_write_loop(fd, fileData.constData(), fileData.size()) < fileData.size())
error = QLockFile::UnknownError; // partition full