-
Notifications
You must be signed in to change notification settings - Fork 0
/
testbot.cpp
348 lines (301 loc) · 10.6 KB
/
testbot.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include <iostream>
#include <fstream>
#include <sys/types.h>
#include <unistd.h>
#include <cstdlib>
#include <sys/wait.h>
#include <string>
#include <ctime>
#include <cstring>
#include <cassert>
using namespace std;
string document_root = "/var/www/hermes/public_html/";
template <std::size_t N>
int execvp(const char *file, const char *const (&argv)[N])
{
assert((N > 0) && (argv[N - 1] == nullptr));
return execvp(file, const_cast<char *const *>(argv));
}
inline bool exists_test(const std::string &name)
{
return (access(name.c_str(), F_OK) != -1);
}
//argv[1] - id submita
//argv[2] - rozszerzenie
//argv[3] - id (shortcut) zadania
enum LANG
{
CPP,
PYT,
RAM,
BAP
};
enum LOGTYPE
{
FORK_ERR,
LANG_ERR,
ARGS_ERR,
CHILD_ERR,
COPY_CONF_ERR,
UNDEFINED_STATUS_ERR
};
const std::string currentDateTime()
{
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
strftime(buf, sizeof(buf), "%Y-%m-%d %X", &tstruct);
return buf;
}
void logsomething(int what, string rest="")
{
fstream log;
string logpath = document_root+"testbot.log";
log.open(logpath, ios::out | ios::app);
log << currentDateTime() << " ";
if (what == FORK_ERR)
log << "Error fork!"
<< "-" << rest;
else if (what == LANG_ERR)
log << "The file is missing or unknown"
<< "-" << rest;
else if (what == ARGS_ERR)
log << "invalid arguments"
<< "-" << rest;
else if (what == CHILD_ERR)
log << "ERROR: Child has not terminated correctly"
<< "-" << rest;
else if (what == COPY_CONF_ERR)
log << "The conf file hasn't been copied properly"
<< "-" << rest;
else if (what == UNDEFINED_STATUS_ERR)
log << "Undefined status: "
<< "-" << rest;
log << endl;
log.close();
}
int whatlang(char *submit, int nr)
{
string t = submit;
if (t == ".cpp")
return CPP;
else if (t == ".py")
return PYT;
else if (t == ".mrram")
return RAM;
else if (t == ".bap")
return BAP;
logsomething(LANG_ERR, to_string(nr) + t);
exit(1);
}
int main(int argc, char *argv[])
{
pid_t pid;
int status;
if (argc != 4)
{
logsomething(ARGS_ERR, "");
exit(1);
}
int nr = atoi(argv[1]);
string snr = (string)argv[1];
string ext = (string)argv[2];
int lang = whatlang(argv[2], nr);
if ((pid = fork()) < 0)
{
logsomething(FORK_ERR, "id_submit:" + snr);
perror("Error fork!");
exit(1);
}
string OPTS, command, program, in_test, out_test, tmp, playgroundpath, out_file, sio2jail_file;
string resultpath = document_root + "results/" + snr + ".txt";
string sio2jailpath = document_root + "oiejq/sio2jail";
//path to work directory
playgroundpath = document_root + "playground/";
//path to submits
string submitspath = document_root+"submits/";
string submit_file = submitspath + snr + ext;
//path to work directory
string compfile = playgroundpath + snr;
string taskpath = document_root + "tasks/" + (string)argv[3];
string confpath = taskpath + "/conf.txt";
if (pid == 0) //child
{
switch (lang)
{
case CPP:
{
const char *comp[] = {"g++", "-std=c++14", submit_file.c_str(), "-o", compfile.c_str(), "-O2", nullptr};
execvp("g++", comp);
break;
}
default:
break;
}
return EXIT_SUCCESS;
}
else //parent
{
bool compilation_error = false;
pid_t cpid = waitpid(pid, &status, 0); //wait for die a child
if (WIFEXITED(status))
{
if (WEXITSTATUS(status) != 0)
compilation_error = true;
}
else
logsomething(CHILD_ERR, "Status:" + to_string(status));
fstream conffile, result, sio2jail_file_stream, output, correct_output;
//settings of sio2jail
OPTS += " --mount-namespace off";
OPTS += " --pid-namespace off";
OPTS += " --uts-namespace off";
OPTS += " --ipc-namespace off";
OPTS += " --net-namespace off";
OPTS += " --capability-drop off --user-namespace off";
//OPTS += " -l /var/www/hermes/public_html/oiejq/sio2jail.log";
OPTS += " -s";
string newconfpath = playgroundpath + snr + ".conf";
string copy_conf_command = "cp " + confpath + " " + newconfpath;
system(copy_conf_command.c_str());
string lockfile_command = "touch " + playgroundpath + snr + ".lock";
system(lockfile_command.c_str());
if(!exists_test(newconfpath))
{
logsomething(COPY_CONF_ERR, "submit: "+snr+" task: "+(string)argv[3]);
exit(1);
}
int priority_status = 0;
if((lang == CPP) && !exists_test(playgroundpath+snr))
{
priority_status = 3;
compilation_error = true;
}
conffile.open(newconfpath, ios::in);
result.open(resultpath, ios::out);
long long int n_test, int_time_limit, int_memory_limit;
double memory_limit, time_limit, max_points, memory, time, points;
conffile >> n_test;
for (int i = 0; i < n_test; i++)
{
conffile >> tmp;
result << tmp << endl;
if (compilation_error)
{
result << "0" << endl; //points
result << "-" << endl; //time
result << "-" << endl; //memory
result << "3" << endl; //status
}
else
{
conffile >> max_points;
conffile >> time_limit;
conffile >> memory_limit;
time_limit *= 1000;
int_time_limit = (long long int)time_limit;
if(lang == CPP)
memory_limit *= 10;
int_memory_limit = (long long int)memory_limit;
if(lang == PYT)
int_memory_limit += 50;
string OPTS1 = OPTS + " -m " + to_string(int_memory_limit)+"M";
OPTS1 += " --rtimelimit " + to_string(int_time_limit)+"ms";
in_test = taskpath + "/in/" + to_string(i) + ".in";
out_test = taskpath + "/out/" + to_string(i) + ".out";
out_file = playgroundpath + snr + ".out";
sio2jail_file = playgroundpath + snr + ".sio2jail";
if (lang == CPP)
{
program = playgroundpath + snr;
command = sio2jailpath + " -f 3 -o oiaug " + OPTS1 + " -- " + program
+ " < " + in_test + " > " + out_file + " 3> "+ sio2jail_file;
system(command.c_str());
}else if(lang == PYT){
program = "/usr/bin/python3 " + submit_file;
command = sio2jailpath + " -f 3 -o oiaug " + OPTS1 + " -- " + program
+ " < " + in_test + " > " + out_file + " 3> "+ sio2jail_file;
system(command.c_str());
}
sio2jail_file_stream.open(sio2jail_file, ios::in);
string sio_status, sio_exitcode, sio_nvm, sio_sysc;
double sio_memory, sio_time;
int sio_status_code = 0;
sio2jail_file_stream >> sio_status;
sio2jail_file_stream >> sio_exitcode;
sio2jail_file_stream >> sio_time;
sio2jail_file_stream >> sio_nvm;
sio2jail_file_stream >> sio_memory;
sio2jail_file_stream >> sio_sysc;
sio2jail_file_stream.close();
sio_memory/=1000;
if(lang==CPP)
sio_memory/=10;
sio_time/=1000;
if(sio_status=="OK")
{
bool correct = true;
string output_user, output_test;
output.open(out_file, ios::in);
correct_output.open(out_test, ios::in);
while(!output.eof())
{
output >> output_user;
correct_output >> output_test;
if(output_user!=output_test)
{
correct = false;
break;
}
}
correct_output.close();
output.close();
if(correct)
{
sio_status_code = 1;
result << max_points << endl; //points
result << sio_time << endl; //time
result << sio_memory << endl; //memory
result << sio_status_code << endl; //status
}else
{
sio_status_code = 2;
result << "0" << endl; //points
result << sio_time << endl; //time
result << sio_memory << endl; //memory
result << sio_status_code << endl; //status
}
}else if(sio_status=="TLE")
{
sio_status_code = 4;
result << "0" << endl; //points
result << int_time_limit/100 << endl; //time
result << sio_memory << endl; //memory
result << sio_status_code << endl; //status
}else if(sio_status=="MLE")
{
sio_status_code = 5;
result << "0" << endl; //points
result << sio_time << endl; //time
result << "-" << endl; //memory
result << sio_status_code << endl; //status
}else
{
logsomething(UNDEFINED_STATUS_ERR, sio_status+" submit: "+snr);
exit(1);
}
if(!compilation_error && sio_status_code>priority_status)
priority_status = sio_status_code;
}
}
result << priority_status << endl;
result.close();
conffile.close();
string delete_work_files = "rm " + playgroundpath + snr + "*";
string delete_lock_file = "rm " + playgroundpath + snr + ".lock";
system(delete_lock_file.c_str());
//system(delete_work_files.c_str());
}
return EXIT_SUCCESS;
}