| 1 | // Copyright (C) 2013 David Faure <faure+bluesystems@kde.org> | 
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only | 
| 3 |  | 
| 4 | #ifndef QLOCKFILE_P_H | 
| 5 | #define QLOCKFILE_P_H | 
| 6 |  | 
| 7 | // | 
| 8 | //  W A R N I N G | 
| 9 | //  ------------- | 
| 10 | // | 
| 11 | // This file is not part of the Qt API.  It exists purely as an | 
| 12 | // implementation detail.  This header file may change from version to | 
| 13 | // version without notice, or even be removed. | 
| 14 | // | 
| 15 | // We mean it. | 
| 16 | // | 
| 17 |  | 
| 18 | #include <QtCore/private/qglobal_p.h> | 
| 19 | #include <QtCore/qlockfile.h> | 
| 20 | #include <QtCore/qfile.h> | 
| 21 |  | 
| 22 | #include <qplatformdefs.h> | 
| 23 |  | 
| 24 | #ifdef Q_OS_WIN | 
| 25 | #include <io.h> | 
| 26 | #include <qt_windows.h> | 
| 27 | #endif | 
| 28 |  | 
| 29 | QT_BEGIN_NAMESPACE | 
| 30 |  | 
| 31 | class QLockFilePrivate | 
| 32 | { | 
| 33 | public: | 
| 34 |     QLockFilePrivate(const QString &fn) | 
| 35 |         : fileName(fn) | 
| 36 |     { | 
| 37 |     } | 
| 38 |     QLockFile::LockError tryLock_sys(); | 
| 39 |     bool removeStaleLock(); | 
| 40 |     QByteArray lockFileContents() const; | 
| 41 |     // Returns \c true if the lock belongs to dead PID, or is old. | 
| 42 |     // The attempt to delete it will tell us if it was really stale or not, though. | 
| 43 |     bool isApparentlyStale() const; | 
| 44 |  | 
| 45 |     // used in dbusmenu | 
| 46 |     Q_CORE_EXPORT static QString processNameByPid(qint64 pid); | 
| 47 |     static bool isProcessRunning(qint64 pid, const QString &appname); | 
| 48 |  | 
| 49 |     QString fileName; | 
| 50 |  | 
| 51 | #ifdef Q_OS_WIN | 
| 52 |     Qt::HANDLE fileHandle = INVALID_HANDLE_VALUE; | 
| 53 | #else | 
| 54 |     int fileHandle = -1; | 
| 55 | #endif | 
| 56 |  | 
| 57 |     std::chrono::milliseconds staleLockTime = std::chrono::seconds{30}; | 
| 58 |     QLockFile::LockError lockError = QLockFile::NoError; | 
| 59 |     bool isLocked = false; | 
| 60 |  | 
| 61 |     static int getLockFileHandle(QLockFile *f) | 
| 62 |     { | 
| 63 |         int fd; | 
| 64 | #ifdef Q_OS_WIN | 
| 65 |         // Use of this function on Windows WILL leak a file descriptor. | 
| 66 |         fd = _open_osfhandle(intptr_t(f->d_func()->fileHandle), 0); | 
| 67 | #else | 
| 68 |         fd = f->d_func()->fileHandle; | 
| 69 | #endif | 
| 70 |         QT_LSEEK(fd: fd, offset: 0, SEEK_SET); | 
| 71 |         return fd; | 
| 72 |     } | 
| 73 | }; | 
| 74 |  | 
| 75 | QT_END_NAMESPACE | 
| 76 |  | 
| 77 | #endif /* QLOCKFILE_P_H */ | 
| 78 |  |