/
threadexecutor.cpp
261 lines (225 loc) · 6.68 KB
/
threadexecutor.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team.
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "threadexecutor.h"
#include "cppcheck.h"
#include <iostream>
#include <algorithm>
#if (defined(__GNUC__) || defined(__sun)) && !defined(__MINGW32__)
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <errno.h>
#endif
ThreadExecutor::ThreadExecutor(const std::vector<std::string> &filenames, const Settings &settings, ErrorLogger &errorLogger)
: _filenames(filenames), _settings(settings), _errorLogger(errorLogger), _fileCount(0)
{
}
ThreadExecutor::~ThreadExecutor()
{
//dtor
}
void ThreadExecutor::addFileContent(const std::string &path, const std::string &content)
{
_fileContents[ path ] = content;
}
///////////////////////////////////////////////////////////////////////////////
////// This code is for __GNUC__ and __sun only ///////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#if (defined(__GNUC__) || defined(__sun)) && !defined(__MINGW32__)
int ThreadExecutor::handleRead(unsigned int &result)
{
char type = 0;
if (read(_pipe[0], &type, 1) <= 0)
{
if (errno == EAGAIN)
return 0;
return -1;
}
if (type != '1' && type != '2' && type != '3')
{
std::cerr << "#### You found a bug from cppcheck.\nThreadExecutor::handleRead error, type was:" << type << std::endl;
exit(0);
}
unsigned int len = 0;
if (read(_pipe[0], &len, sizeof(len)) <= 0)
{
std::cerr << "#### You found a bug from cppcheck.\nThreadExecutor::handleRead error, type was:" << type << std::endl;
exit(0);
}
char *buf = new char[len];
if (read(_pipe[0], buf, len) <= 0)
{
std::cerr << "#### You found a bug from cppcheck.\nThreadExecutor::handleRead error, type was:" << type << std::endl;
exit(0);
}
if (type == '1')
{
_errorLogger.reportOut(buf);
}
else if (type == '2')
{
ErrorLogger::ErrorMessage msg;
msg.deserialize(buf);
// Alert only about unique errors
std::string errmsg = msg.toString();
if (std::find(_errorList.begin(), _errorList.end(), errmsg) == _errorList.end())
{
_errorList.push_back(errmsg);
_errorLogger.reportErr(msg);
}
}
else if (type == '3')
{
_fileCount++;
std::istringstream iss(buf);
unsigned int fileResult = 0;
iss >> fileResult;
result += fileResult;
_errorLogger.reportStatus(_fileCount, _filenames.size());
delete [] buf;
return -1;
}
delete [] buf;
return 1;
}
unsigned int ThreadExecutor::check()
{
_fileCount = 0;
unsigned int result = 0;
if (pipe(_pipe) == -1)
{
perror("pipe");
exit(1);
}
int flags = 0;
if ((flags = fcntl(_pipe[0], F_GETFL, 0)) < 0)
{
perror("fcntl");
exit(1);
}
if (fcntl(_pipe[0], F_SETFL, flags | O_NONBLOCK) < 0)
{
perror("fcntl");
exit(1);
}
unsigned int childCount = 0;
unsigned int i = 0;
while (true)
{
// Start a new child
if (i < _filenames.size() && childCount < _settings._jobs)
{
pid_t pid = fork();
if (pid < 0)
{
// Error
std::cerr << "Failed to create child process" << std::endl;
exit(EXIT_FAILURE);
}
else if (pid == 0)
{
CppCheck fileChecker(*this);
fileChecker.settings(_settings);
if (_fileContents.size() > 0 && _fileContents.find(_filenames[i]) != _fileContents.end())
{
// File content was given as a string
fileChecker.addFile(_filenames[i], _fileContents[ _filenames[i] ]);
}
else
{
// Read file from a file
fileChecker.addFile(_filenames[i]);
}
unsigned int resultOfCheck = fileChecker.check();
std::ostringstream oss;
oss << resultOfCheck;
writeToPipe('3', oss.str());
exit(0);
}
++childCount;
++i;
}
else if (childCount > 0)
{
// Wait for child to quit before stating new processes
while (true)
{
int readRes = handleRead(result);
if (readRes == -1)
break;
else if (readRes == 0)
usleep(5000); // 5 ms
}
int stat = 0;
waitpid(0, &stat, 0);
--childCount;
}
else if (childCount == 0)
{
// All done
break;
}
}
return result;
}
void ThreadExecutor::writeToPipe(char type, const std::string &data)
{
unsigned int len = data.length() + 1;
char *out = new char[ len + 1 + sizeof(len)];
out[0] = type;
std::memcpy(&(out[1]), &len, sizeof(len));
std::memcpy(&(out[1+sizeof(len)]), data.c_str(), len);
if (write(_pipe[1], out, len + 1 + sizeof(len)) <= 0)
{
delete [] out;
out = 0;
std::cerr << "#### ThreadExecutor::writeToPipe, Failed to write to pipe" << std::endl;
exit(0);
}
delete [] out;
}
void ThreadExecutor::reportOut(const std::string &outmsg)
{
writeToPipe('1', outmsg);
}
void ThreadExecutor::reportErr(const ErrorLogger::ErrorMessage &msg)
{
writeToPipe('2', msg.serialize());
}
void ThreadExecutor::reportStatus(unsigned int /*index*/, unsigned int /*max*/)
{
// Not used
}
#else
unsigned int ThreadExecutor::check()
{
return 0;
}
void ThreadExecutor::reportOut(const std::string &/*outmsg*/)
{
}
void ThreadExecutor::reportErr(const ErrorLogger::ErrorMessage &/*msg*/)
{
}
void ThreadExecutor::reportStatus(unsigned int /*index*/, unsigned int /*max*/)
{
}
#endif