forked from lsk-china/my-live2d-qt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runningCheck.cpp
50 lines (46 loc) · 1.37 KB
/
runningCheck.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//
// Created by lsk on 2/10/23.
//
#include <iostream>
#include "runningCheck.h"
static QByteArray calcHash(QString procDir) {
QCryptographicHash hash(QCryptographicHash::Algorithm::Md5);
QFile fileIn(procDir + "/exe");
fileIn.open(QIODevice::ReadOnly);
char buf[2048];
while(fileIn.read(buf, 2048) > 0) {
hash.addData(buf);
}
fileIn.close();
return hash.result();
}
static bool isDigit(QString s) {
for (auto i : s) {
if (!isdigit(i.toLatin1())) {
return false;
}
}
return true;
}
bool check() {
qint64 selfPid = QCoreApplication::applicationPid();
std::cout << selfPid << std::endl;
QString procDir = "/proc/";
QString selfProcDir = procDir + QString::number(selfPid);
QByteArray selfMD5 = calcHash(selfProcDir);
QDirIterator iter("/proc", QDirIterator::Subdirectories);
while (iter.hasNext()) {
QString aProcDir = iter.next();
if (isDigit(aProcDir.right(aProcDir.lastIndexOf('/')))) {
int pid = aProcDir.right(aProcDir.lastIndexOf('/')).toInt();
if (pid > 50 && pid < selfPid) {
QByteArray procHash = calcHash(aProcDir);
if (procHash == selfMD5) {
std::cout<< aProcDir.toStdString() << std::endl;
return true;
}
}
}
}
return false;
}