-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreadPool.cpp
172 lines (145 loc) · 4.3 KB
/
ThreadPool.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
*
* Copyright (c) 2015. Lukas Dresel, Julius Lohmann, Benedikt Lorch, Burkhard Ringlein, Andreas Rupp, Yuriy Sulima, Carola Touchy
*
* This file is part of GarbageCollector.
*
* GarbageCollector is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GarbageCollector is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GarbageCollector. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <iostream>
#include "ThreadPool.h"
#include "GarbageMan.h"
#include "SewagePlant.h"
#include "modules/VerifyJPEG.h"
#include "modules/VerifyPDF.h"
#include "modules/VerifyPNG.h"
#include "modules/VerifySQLite.h"
#define THREADNICE -3
using namespace std;
ThreadPool::ThreadPool(int numberOfThreads, char *pathToImage)
{
this->numberOfThreads = numberOfThreads;
this->pathToImage = pathToImage;
}
ThreadPool::~ThreadPool()
{
threads.clear();
}
void try_nicing(pid_t tid)
{
int nicelevel = THREADNICE;
setpriority(PRIO_PROCESS, tid, nicelevel);
#ifdef DEBUG
nicelevel = getpriority(PRIO_PROCESS, tid);
cout << "Thread with ID " << tid << " started with nice " << nicelevel << endl;
#endif
}
void runModules (BNDBUF *jbuf,uint id,char *pathToImage, SewagePlant *swp)
{
#ifdef DEBUG
cout << "run Modules started with id " << id << endl;
#endif
pid_t tid = (pid_t) syscall(SYS_gettid);
try_nicing(tid);
VerificationJob *mj;
FILE* myImageFile = fopen(pathToImage, "r");
if(myImageFile == NULL)
{
perror("fopen");
return;
}
while (true) {
mj = bbGet(jbuf);
VerifyJPEG jpegV;
VerifyPDF pdfV;
VerifyPNG pngV;
VerifySQLite sqliteV;
uint64_t foundLength = 0; // error: ‘foundLength’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
switch(mj->type) {
case pdf:
foundLength = pdfV.getValidFileLength(mj->startOffset, mj->length, myImageFile);
break;
case jpg:
foundLength = jpegV.getValidFileLength(mj->startOffset, mj->length, myImageFile);
#ifdef DEBUG
cout << "Possible jpeg file at " << showbase << hex << mj->startOffset << " of length " << foundLength << endl;
#endif
break;
case png:
foundLength = pngV.getValidFileLength(mj->startOffset, mj->length, myImageFile);
break;
case sqlite:
foundLength = sqliteV.getValidFileLength(mj->startOffset, mj->length, myImageFile);
break;
case poison:
fclose(myImageFile);
free(mj);
#ifdef DEBUG
cout << "PoisonPill recieved. id: " << id << endl;
#endif
return;
}
if(foundLength != 0) {
//output methode
swp->purify(mj->startOffset, foundLength, mj->type, myImageFile);
}
// else{
// logging
// }
}
}
void runParser (BNDBUF *jbuf, uint id, int numberOfThreads, char *pathToImage) {
#ifdef DEBUG
cout << "run parser started " << endl;
#endif
pid_t tid = (pid_t) syscall(SYS_gettid);
try_nicing(tid);
GarbageMan bene = GarbageMan();
bene.work(pathToImage, jbuf);
for (int i = 1; i < numberOfThreads; i++) {
struct VerificationJob *PoisonPill = (struct VerificationJob *)malloc(sizeof(struct VerificationJob));
assert(PoisonPill != NULL);
PoisonPill->type = poison;
bbPut(jbuf,PoisonPill);
}
#ifdef DEBUG
cout << "run Parser finished" << endl;
#endif
}
void ThreadPool::start (BNDBUF *jbuf, SewagePlant *swp) {
/*#ifdef DEBUG
cout << "numberOfThreads: " << numberOfThreads << endl;
#endif*/
threads.push_back(thread(runParser,jbuf,0,numberOfThreads,pathToImage));
for (int i = 1; i < numberOfThreads; i++) {
threads.push_back(thread(runModules,jbuf,i,pathToImage,swp));
}
#ifdef DEBUG
cout << "all threads started " << endl;
#endif
}
void ThreadPool::waitForJoin () {
for (int i = 0; i < numberOfThreads; i++) {
threads[i].join();
}
}