1 /****************************************************************************
2 **
3 ** Copyright (C) 2013 David Faure <faure+bluesystems@kde.org>
4 ** Copyright (C) 2016 The Qt Company Ltd.
5 ** Copyright (C) 2017 Intel Corporation.
6 ** Contact: https://www.qt.io/licensing/
7 **
8 ** This file is part of the QtCore module of the Qt Toolkit.
9 **
10 ** $QT_BEGIN_LICENSE:LGPL$
11 ** Commercial License Usage
12 ** Licensees holding valid commercial Qt licenses may use this file in
13 ** accordance with the commercial license agreement provided with the
14 ** Software or, alternatively, in accordance with the terms contained in
15 ** a written agreement between you and The Qt Company. For licensing terms
16 ** and conditions see https://www.qt.io/terms-conditions. For further
17 ** information use the contact form at https://www.qt.io/contact-us.
18 **
19 ** GNU Lesser General Public License Usage
20 ** Alternatively, this file may be used under the terms of the GNU Lesser
21 ** General Public License version 3 as published by the Free Software
22 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
23 ** packaging of this file. Please review the following information to
24 ** ensure the GNU Lesser General Public License version 3 requirements
25 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
26 **
27 ** GNU General Public License Usage
28 ** Alternatively, this file may be used under the terms of the GNU
29 ** General Public License version 2.0 or (at your option) the GNU General
30 ** Public license version 3 or any later version approved by the KDE Free
31 ** Qt Foundation. The licenses are as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
33 ** included in the packaging of this file. Please review the following
34 ** information to ensure the GNU General Public License requirements will
35 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
36 ** https://www.gnu.org/licenses/gpl-3.0.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "private/qlockfile_p.h"
43 #include "private/qfilesystementry_p.h"
44 #include <qt_windows.h>
45 
46 #include "QtCore/qfileinfo.h"
47 #include "QtCore/qdatetime.h"
48 #include "QtCore/qdebug.h"
49 #include "QtCore/qthread.h"
50 
51 QT_BEGIN_NAMESPACE
52 
fileExists(const wchar_t * fileName)53 static inline bool fileExists(const wchar_t *fileName)
54 {
55     WIN32_FILE_ATTRIBUTE_DATA  data;
56     return GetFileAttributesEx(fileName, GetFileExInfoStandard, &data);
57 }
58 
tryLock_sys()59 QLockFile::LockError QLockFilePrivate::tryLock_sys()
60 {
61     const QFileSystemEntry fileEntry(fileName);
62     // When writing, allow others to read.
63     // When reading, QFile will allow others to read and write, all good.
64     // Adding FILE_SHARE_DELETE would allow forceful deletion of stale files,
65     // but Windows doesn't allow recreating it while this handle is open anyway,
66     // so this would only create confusion (can't lock, but no lock file to read from).
67     const DWORD dwShareMode = FILE_SHARE_READ;
68 #ifndef Q_OS_WINRT
69     SECURITY_ATTRIBUTES securityAtts = { sizeof(SECURITY_ATTRIBUTES), NULL, FALSE };
70     HANDLE fh = CreateFile((const wchar_t*)fileEntry.nativeFilePath().utf16(),
71                            GENERIC_READ | GENERIC_WRITE,
72                            dwShareMode,
73                            &securityAtts,
74                            CREATE_NEW, // error if already exists
75                            FILE_ATTRIBUTE_NORMAL,
76                            NULL);
77 #else // !Q_OS_WINRT
78     HANDLE fh = CreateFile2((const wchar_t*)fileEntry.nativeFilePath().utf16(),
79                             GENERIC_READ | GENERIC_WRITE,
80                             dwShareMode,
81                             CREATE_NEW, // error if already exists
82                             NULL);
83 #endif // Q_OS_WINRT
84     if (fh == INVALID_HANDLE_VALUE) {
85         const DWORD lastError = GetLastError();
86         switch (lastError) {
87         case ERROR_SHARING_VIOLATION:
88         case ERROR_ALREADY_EXISTS:
89         case ERROR_FILE_EXISTS:
90             return QLockFile::LockFailedError;
91         case ERROR_ACCESS_DENIED:
92             // readonly file, or file still in use by another process.
93             // Assume the latter if the file exists, since we don't create it readonly.
94             return fileExists((const wchar_t*)fileEntry.nativeFilePath().utf16())
95                 ? QLockFile::LockFailedError
96                 : QLockFile::PermissionError;
97         default:
98             qWarning("Got unexpected locking error %llu", quint64(lastError));
99             return QLockFile::UnknownError;
100         }
101     }
102 
103     // We hold the lock, continue.
104     fileHandle = fh;
105     QByteArray fileData = lockFileContents();
106     DWORD bytesWritten = 0;
107     QLockFile::LockError error = QLockFile::NoError;
108     if (!WriteFile(fh, fileData.constData(), fileData.size(), &bytesWritten, NULL) || !FlushFileBuffers(fh))
109         error = QLockFile::UnknownError; // partition full
110     return error;
111 }
112 
removeStaleLock()113 bool QLockFilePrivate::removeStaleLock()
114 {
115     // QFile::remove fails on Windows if the other process is still using the file, so it's not stale.
116     return QFile::remove(fileName);
117 }
118 
isProcessRunning(qint64 pid,const QString & appname)119 bool QLockFilePrivate::isProcessRunning(qint64 pid, const QString &appname)
120 {
121     // On WinRT there seems to be no way of obtaining information about other
122     // processes due to sandboxing
123 #ifndef Q_OS_WINRT
124     HANDLE procHandle = ::OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
125     if (!procHandle)
126         return false;
127 
128     // We got a handle but check if process is still alive
129     DWORD exitCode = 0;
130     if (!::GetExitCodeProcess(procHandle, &exitCode))
131         exitCode = 0;
132     ::CloseHandle(procHandle);
133     if (exitCode != STILL_ACTIVE)
134         return false;
135 
136     const QString processName = processNameByPid(pid);
137     if (!processName.isEmpty() && processName != appname)
138         return false; // PID got reused by a different application.
139 
140 #else // !Q_OS_WINRT
141     Q_UNUSED(pid);
142     Q_UNUSED(appname);
143 #endif // Q_OS_WINRT
144 
145     return true;
146 }
147 
processNameByPid(qint64 pid)148 QString QLockFilePrivate::processNameByPid(qint64 pid)
149 {
150 #if !defined(Q_OS_WINRT)
151     typedef DWORD (WINAPI *GetModuleFileNameExFunc)(HANDLE, HMODULE, LPTSTR, DWORD);
152 
153     HMODULE hPsapi = LoadLibraryA("psapi");
154     if (!hPsapi)
155         return QString();
156     GetModuleFileNameExFunc qGetModuleFileNameEx = reinterpret_cast<GetModuleFileNameExFunc>(
157         reinterpret_cast<QFunctionPointer>(GetProcAddress(hPsapi, "GetModuleFileNameExW")));
158     if (!qGetModuleFileNameEx) {
159         FreeLibrary(hPsapi);
160         return QString();
161     }
162 
163     HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, DWORD(pid));
164     if (!hProcess) {
165         FreeLibrary(hPsapi);
166         return QString();
167     }
168     wchar_t buf[MAX_PATH];
169     const DWORD length = qGetModuleFileNameEx(hProcess, NULL, buf, sizeof(buf) / sizeof(wchar_t));
170     CloseHandle(hProcess);
171     FreeLibrary(hPsapi);
172     if (!length)
173         return QString();
174     QString name = QString::fromWCharArray(buf, length);
175     int i = name.lastIndexOf(QLatin1Char('\\'));
176     if (i >= 0)
177         name.remove(0, i + 1);
178     i = name.lastIndexOf(QLatin1Char('.'));
179     if (i >= 0)
180         name.truncate(i);
181     return name;
182 #else
183     Q_UNUSED(pid);
184     return QString();
185 #endif
186 }
187 
unlock()188 void QLockFile::unlock()
189 {
190     Q_D(QLockFile);
191      if (!d->isLocked)
192         return;
193      CloseHandle(d->fileHandle);
194      int attempts = 0;
195      static const int maxAttempts = 500; // 500ms
196      while (!QFile::remove(d->fileName) && ++attempts < maxAttempts) {
197          // Someone is reading the lock file right now (on Windows this prevents deleting it).
198          QThread::msleep(1);
199      }
200      if (attempts == maxAttempts) {
201         qWarning() << "Could not remove our own lock file" << d->fileName << ". Either other users of the lock file are reading it constantly for 500 ms, or we (no longer) have permissions to delete the file";
202         // This is bad because other users of this lock file will now have to wait for the stale-lock-timeout...
203      }
204      d->lockError = QLockFile::NoError;
205      d->isLocked = false;
206 }
207 
208 QT_END_NAMESPACE
209