diff options
-rw-r--r-- | process.cpp | 55 | ||||
-rw-r--r-- | process.h | 1 |
2 files changed, 53 insertions, 3 deletions
diff --git a/process.cpp b/process.cpp index 990f5ba..21cfb55 100644 --- a/process.cpp +++ b/process.cpp @@ -7,6 +7,7 @@ #include <sys/socket.h> #include <signal.h> #include <fcntl.h> +#include <QFileInfo> static int pipefd[2]; @@ -15,6 +16,53 @@ static void signalhandler(int) write(pipefd[1], " ", 1); } +static bool analyzeBinary(const QString &binary) +{ + QFileInfo fi(binary); + if (!fi.exists()) { + printf("Binary does not exist.\n"); + return false; + } + if (!fi.isFile()) { + printf("Binary is not a file.\n"); + return false; + } + if (!fi.isReadable()) { + printf("Binary is not readable.\n"); + return false; + } + if (!fi.isExecutable()) { + printf("Binary is not executable.\n"); + return false; + } + + if (fi.size() < 4) { + printf("Binary is smaller than 4 bytes.\n"); + return false; + } + + QFile f(binary); + if (!f.open(QFile::ReadOnly)) { + printf("Could not open binary to analyze.\n"); + return false; + } + + QByteArray elfHeader = f.read(4); + f.close(); + + if (elfHeader.size() < 4) { + printf("Failed to read ELF header.\n"); + return false; + } + + if (elfHeader != QByteArray::fromHex("7f454C46")) { // 0x7f ELF + printf("Binary is not an ELF file.\n"); + return false; + } + + return true; +} + Process::Process() : QObject(0) , mProcess(new QProcess(this)) @@ -81,6 +129,7 @@ void Process::error(QProcess::ProcessError error) switch (error) { case QProcess::FailedToStart: printf("Failed to start\n"); + analyzeBinary(mBinary); break; case QProcess::Crashed: printf("Crashed\n"); @@ -125,10 +174,10 @@ void Process::startup(QStringList args) args.append(mConfig.args); mProcess->setProcessEnvironment(pe); - QString binary = args.first(); + mBinary = args.first(); args.removeFirst(); - qDebug() << binary << args; - mProcess->start(binary, args); + qDebug() << mBinary << args; + mProcess->start(mBinary, args); } void Process::start(const QStringList &args) @@ -49,4 +49,5 @@ private: int mDebuggee; bool mDebug; Config mConfig; + QString mBinary; }; |