Mercurial > hg > syntilista
annotate src/runguard.cpp @ 259:14ed3a2500e6
Fix build.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 24 Dec 2019 05:14:48 +0200 |
parents | 37d5f4329449 |
children |
rev | line source |
---|---|
228
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1 // |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
2 // Taken and modified from https://stackoverflow.com/questions/5006547/qt-best-practice-for-a-single-instance-app-protection |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
3 // |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
4 #include "runguard.h" |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
5 #include <QCryptographicHash> |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
6 |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
7 |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
8 namespace |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
9 { |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
10 |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
11 QString generateKeyHash(const QString &key, const QString &salt) |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
12 { |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
13 QByteArray data; |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
14 |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
15 data.append(key.toUtf8()); |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
16 data.append(salt.toUtf8()); |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
17 data = QCryptographicHash::hash(data, QCryptographicHash::Sha1).toHex(); |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
18 |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
19 return data; |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
20 } |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
21 |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
22 } |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
23 |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
24 |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
25 RunGuard::RunGuard(const QString &key) |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
26 : key(key) |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
27 , memLockKey(generateKeyHash(key, "_memLockKey")) |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
28 , sharedmemKey(generateKeyHash(key, "_sharedmemKey")) |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
29 , sharedMem(sharedmemKey) |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
30 , memLock(memLockKey, 1) |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
31 { |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
32 memLock.acquire(); |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
33 |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
34 QSharedMemory fix(sharedmemKey); // Fix for *nix: http://habrahabr.ru/post/173281/ |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
35 fix.attach(); |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
36 |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
37 memLock.release(); |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
38 } |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
39 |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
40 |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
41 RunGuard::~RunGuard() |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
42 { |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
43 release(); |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
44 } |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
45 |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
46 |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
47 bool RunGuard::isAnotherRunning() |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
48 { |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
49 if (sharedMem.isAttached()) |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
50 return false; |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
51 |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
52 memLock.acquire(); |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
53 |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
54 const bool isRunning = sharedMem.attach(); |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
55 if (isRunning) |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
56 sharedMem.detach(); |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
57 |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
58 memLock.release(); |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
59 |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
60 return isRunning; |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
61 } |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
62 |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
63 |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
64 bool RunGuard::tryToRun() |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
65 { |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
66 if (isAnotherRunning()) |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
67 return false; |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
68 |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
69 memLock.acquire(); |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
70 const bool result = sharedMem.create(sizeof(quint64)); |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
71 memLock.release(); |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
72 |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
73 if (!result) |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
74 release(); |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
75 |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
76 return result; |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
77 } |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
78 |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
79 |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
80 void RunGuard::release() |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
81 { |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
82 memLock.acquire(); |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
83 |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
84 if (sharedMem.isAttached()) |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
85 sharedMem.detach(); |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
86 |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
87 memLock.release(); |
37d5f4329449
Implement single running instance check to prevent problems with the SQLite database.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
88 } |