forked from robertknight/Qt-Inspector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreloadInjector.cpp
71 lines (57 loc) · 1.72 KB
/
PreloadInjector.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "PreloadInjector.h"
#include "lib/InspectorServer.h"
#include <QtCore/QDebug>
#include <QtCore/QFileInfo>
#include <QtCore/QProcess>
#include <QtCore/QProcessEnvironment>
#include <unistd.h>
bool PreloadInjector::startAndInject(const QString& program, const QStringList& args,
const QString& libraryPath, const QString& entryPoint, int* pid)
{
*pid = 0;
QProcess* process = new QProcess;
QObject::connect(process, SIGNAL(finished(int,QProcess::ExitStatus)), process, SLOT(deleteLater()));
process->setProcessChannelMode(QProcess::ForwardedChannels);
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
QString libPath = QFileInfo(libraryPath).absoluteFilePath();
if (!QFile::exists(libPath))
{
return false;
}
#ifdef Q_OS_MAC
QString var = "DYLD_INSERT_LIBRARIES";
QStringList currentLibs = env.value(var).split(':', QString::SkipEmptyParts);
currentLibs.prepend(libraryPath);
env.insert(var, currentLibs.join(":"));
#elif defined(Q_OS_LINUX)
QString var = "LD_PRELOAD";
QStringList currentLibs = env.value(var).split(' ', QString::SkipEmptyParts);
currentLibs.prepend(libraryPath);
env.insert(var, currentLibs.join(" "));
#else
#error Platform not supported
#endif
process->setProcessEnvironment(env);
process->start(program, args);
if (!process->waitForStarted())
{
qWarning() << "Failed to start" << program;
return false;
}
*pid = process->pid();
QString socketPath = InspectorServer::socketName(*pid);
while (!QFile::exists(socketPath))
{
#ifdef Q_OS_UNIX
usleep(100 * 1000);
#endif
}
return true;
}
bool PreloadInjector::inject(int pid, const QString& libraryPath, const QString& entryPoint)
{
Q_UNUSED(pid);
Q_UNUSED(libraryPath);
Q_UNUSED(entryPoint);
return false;
}