7
7
#include < sys/socket.h>
8
8
#include < signal.h>
9
9
#include < fcntl.h>
10
+ #include < QFileInfo>
10
11
11
12
static int pipefd[2 ];
12
13
@@ -15,6 +16,53 @@ static void signalhandler(int)
15
16
write (pipefd[1 ], " " , 1 );
16
17
}
17
18
19
+ static bool analyzeBinary (const QString &binary)
20
+ {
21
+ QFileInfo fi (binary);
22
+ if (!fi.exists ()) {
23
+ printf (" Binary does not exist.\n " );
24
+ return false ;
25
+ }
26
+ if (!fi.isFile ()) {
27
+ printf (" Binary is not a file.\n " );
28
+ return false ;
29
+ }
30
+ if (!fi.isReadable ()) {
31
+ printf (" Binary is not readable.\n " );
32
+ return false ;
33
+ }
34
+ if (!fi.isExecutable ()) {
35
+ printf (" Binary is not executable.\n " );
36
+ return false ;
37
+ }
38
+
39
+ if (fi.size () < 4 ) {
40
+ printf (" Binary is smaller than 4 bytes.\n " );
41
+ return false ;
42
+ }
43
+
44
+ QFile f (binary);
45
+ if (!f.open (QFile::ReadOnly)) {
46
+ printf (" Could not open binary to analyze.\n " );
47
+ return false ;
48
+ }
49
+
50
+ QByteArray elfHeader = f.read (4 );
51
+ f.close ();
52
+
53
+ if (elfHeader.size () < 4 ) {
54
+ printf (" Failed to read ELF header.\n " );
55
+ return false ;
56
+ }
57
+
58
+ if (elfHeader != QByteArray::fromHex (" 7f454C46" )) { // 0x7f ELF
59
+ printf (" Binary is not an ELF file.\n " );
60
+ return false ;
61
+ }
62
+
63
+ return true ;
64
+ }
65
+
18
66
Process::Process ()
19
67
: QObject(0 )
20
68
, mProcess(new QProcess(this ))
@@ -81,6 +129,7 @@ void Process::error(QProcess::ProcessError error)
81
129
switch (error) {
82
130
case QProcess::FailedToStart:
83
131
printf (" Failed to start\n " );
132
+ analyzeBinary (mBinary );
84
133
break ;
85
134
case QProcess::Crashed:
86
135
printf (" Crashed\n " );
@@ -125,10 +174,10 @@ void Process::startup(QStringList args)
125
174
args.append (mConfig .args );
126
175
127
176
mProcess ->setProcessEnvironment (pe);
128
- QString binary = args.first ();
177
+ mBinary = args.first ();
129
178
args.removeFirst ();
130
- qDebug () << binary << args;
131
- mProcess ->start (binary , args);
179
+ qDebug () << mBinary << args;
180
+ mProcess ->start (mBinary , args);
132
181
}
133
182
134
183
void Process::start (const QStringList &args)
0 commit comments