forked from KIDx/Judger
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.cpp
881 lines (816 loc) · 30 KB
/
test.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
/*
* A simple judge
* Author: TT_last
*
* use:
* ./judge -l 语言 -D 数据目录 -d 临时目录 -t 时间限制 -m 内存限制 -o 输出限制 [-S dd] [-T]
* -l 语言 C=1, C++=2, JAVA=3, C++11=4
* -D 数据目录 包括题号的输入输出文件的目录
* -d 临时目录 judge可以用来存放编译后的文件及其他临时文件的
* -t 时间限制 允许程序运行的最长时间, 以毫秒为单位, 默认为1000, 1s
* -m 内存限制 允许程序使用的最大内存量, 以KB为单位, 默认为65536, 64MB
* -o 输出限制 允许程序输出的最大数据量, 以KB为单位, 默认为8192, 8MB
* -S dd 可选, 如指定此参数, 则表示这是一个Special Judge的题目
* -T 可选,如指定此参数,则表示这个topcoder模式题目,只要写相关头文件 和 相关类+接口就可以了。
*
* 其中-S dd和 -T可以混合用.
*
* 用-S dd,data需要有spj.exe spj.cpp
* 用-T 则相应的需要tc.c tc.cpp tc.java之类的配套
* for example:
* ./judge -l 2 -D data -d temp -t 1000 -m 65536 -o 8192
*
* */
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <fstream>
#include <unistd.h>
#include <errno.h>
#include <sys/wait.h>
#include <sys/resource.h>
#include <sys/syscall.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/reg.h>
#include <sys/user.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/time.h>
#include "judge.h"
#include "okcall.h"
#include "json.hpp"
#define Max(x, y) (x) > (y) ? (x) : (y)
#define is_space_char(a) ((a == ' ') || (a == '\t') || (a == '\n') || (a == '\r'))
using namespace std;
using json = nlohmann::json;
//#define JUDGE_DEBUG
extern int errno;
const int MAXN = 8192;
//重构:令语言支持独立于评测逻辑 By Sine 2013_12_01
//In file "language.h"
void output_result(int result, int memory_usage = 0, int time_usage = 0)
{
//OJ_SE发生时,传入本函数的time_usage即是EXIT错误号,取负数是为了强调和提醒
//此时若memory_usage < 0则为errno,说明错误发生的原因,取负数是为了强调和提醒
//在前台看到System Error时,Time一栏的数值取绝对值就是EXIT错误号,Memory一栏取绝对值则为errno
//OJ_RF发生时,传入本函数的time_usage即是syscall号,取负数是为了强调和提醒
//在前台看到Dangerous Code时,Time一栏的数值取绝对值就是Syscall号
//Bugfix:之前版本评测过程中每处错误发生时一般会直接exit,导致前台status一直Running
//本次fix在所有SE导致的exit前都添加了对本函数的调用,并给出EXIT错误号,解决了此问题,更方便了SE发生时对系统进行调试
//fixed By Sine 2014-03-05
if (result == judge_conf::OJ_SE || result == judge_conf::OJ_RF) time_usage *= -1;
//#ifdef JUDGE_DEBUG
LOG_DEBUG("result: %d, %dKB %dms", result, memory_usage, time_usage);
//#endif
printf("%d %d %d\n", result, memory_usage, time_usage);
}
void output_result(json j, string path) {
ofstream myfile;
myfile.open(path);
myfile << j.dump(2);
myfile.close();
// printf("%s\n", j.dump(2).c_str());
}
void timeout(int signo)
{
output_result(judge_conf::OJ_SE, 0, judge_conf::EXIT_TIMEOUT);
if (signo == SIGALRM)
exit(judge_conf::EXIT_TIMEOUT);
}
//normal compare file
void compare_until_nonspace(int &c_std, int &c_usr, FILE *&fd_std, FILE *&fd_usr, int &ret)
{
while((isspace(c_std)) || (isspace(c_usr)))
{
if (c_std != c_usr)
{
if (c_std == EOF || c_usr == EOF) {
return;
}
if (c_std == '\r' && c_usr == '\n')
{
c_std = fgetc(fd_std);
if (c_std != c_usr)
ret = judge_conf::OJ_PE;
}
else
{
ret = judge_conf::OJ_PE;
}
}
if (isspace(c_std))
c_std = fgetc(fd_std);
if (isspace(c_usr))
c_usr = fgetc(fd_usr);
}
}
//合并文件
void addfile(string &main_file, string &tc_file)
{
LOG_DEBUG("TC mode");
char cc[MAXN+5];
FILE *sc_fd = fopen(main_file.c_str(), "a+");
FILE *tc_fd = fopen(tc_file.c_str(), "r");
if (sc_fd && tc_fd)
{
fputs("\n", sc_fd);
while(fgets(cc, MAXN, tc_fd))
{
fputs(cc, sc_fd);
}
}
if (sc_fd) fclose(sc_fd);
if (tc_fd) fclose(tc_fd);
}
int tc_mode()
{
problem::source_file = problem::temp_dir + "/" + Langs[problem::lang]->MainFile;
string syscmd = "mv -f ", source_temp = problem::temp_dir + "/tempfile.txt";
syscmd += problem::source_file + " " + source_temp;
system(syscmd.c_str());
addfile(problem::source_file, problem::tc_head);
addfile(problem::source_file, source_temp);
addfile(problem::source_file, problem::tc_file);
return -1;
}
//特判
int spj_compare_output(
string input_file, //标准输入文件
string output_file, //用户程序的输出文件
string spj_path, //spj路径, change it from exefile to the folder who store the exefile
string file_spj, //spj的输出文件
string output_file_std)
{
#ifdef JUDGE_DEBUG__
cout<<"inputfile: "<<input_file<<endl;
cout<<"outputfile: "<<output_file<<endl;
cout<<"spj_exec: "<<spj_path<<endl;
cout<<"file_spj: "<<file_spj<<endl;
#endif
/*
Improve: Auto rebuild spj.exe while find spj.cpp
Improve: If spj.exe is not exist, return SE
Date & Time: 2013-11-10 08:03
Author: Sine
*/
if (access((spj_path+"/spj.cpp").c_str(), 0) == 0)
{
string syscmd = "g++ -o ";
syscmd += spj_path + "/"+problem::spj_exe_file+" " + spj_path + "/spj.cpp";
system(syscmd.c_str());
syscmd = "mv -f ";
syscmd += spj_path + "/spj.cpp " + spj_path + "/spj.oldcode";
system(syscmd.c_str());
}
if (access((spj_path+"/"+problem::spj_exe_file).c_str(), 0))
{
output_result(judge_conf::OJ_SE, 0, judge_conf::EXIT_ACCESS_SPJ);
exit(judge_conf::EXIT_ACCESS_SPJ);
}
// return judge_conf::OJ_SE;
//End of the Improve*/
int status = 0;
pid_t pid_spj = fork();
if (pid_spj < 0)
{
LOG_WARNING("error for spj failed, %d:%s", errno, strerror(errno));
output_result(judge_conf::OJ_SE, -errno, judge_conf::EXIT_COMPARE_SPJ_FORK);
exit(judge_conf::EXIT_COMPARE_SPJ_FORK);
}
else if (pid_spj == 0)
{
freopen(file_spj.c_str(), "w", stdout);
if (EXIT_SUCCESS == malarm(ITIMER_REAL, judge_conf::spj_time_limit))
{
log_close();
//argv[1] 标准输入 , argv[2] 用户程序输出, argv[3] 标准输出
if (execlp((spj_path+"/"+problem::spj_exe_file).c_str(),
problem::spj_exe_file.c_str(), input_file.c_str(),
output_file.c_str(), output_file_std.c_str(), NULL) < 0)
{
printf("spj execlp error\n");
}
}
}
else
{
if (waitpid(pid_spj, &status, 0) < 0)
{
LOG_BUG("waitpid failed, %d:%s", errno, strerror(errno));
output_result(judge_conf::OJ_SE, -errno, judge_conf::EXIT_COMPARE_SPJ_WAIT);
exit(judge_conf::EXIT_COMPARE_SPJ_WAIT);
}
if (WIFEXITED(status))
{
if (WEXITSTATUS(status) == EXIT_SUCCESS)
{
FILE *fd = fopen(file_spj.c_str(), "r");
if (fd == NULL)
{
output_result(judge_conf::OJ_SE, 0, judge_conf::EXIT_COMPARE_SPJ_OUT);
exit(judge_conf::EXIT_COMPARE_SPJ_OUT);
}
char buf[20];
if (fscanf(fd, "%19s", buf) == EOF)
{
return judge_conf::OJ_WA;
}
if (fd) fclose(fd);
if (strcmp(buf, "AC") == 0)
{
return judge_conf::OJ_AC;
}
else if (strcmp(buf, "PE") == 0)
{
return judge_conf::OJ_PE;
}
else if (strcmp(buf, "WA") == 0)
{
return judge_conf::OJ_WA;
}
}
}
}
return judge_conf::OJ_WA;
}
//普通比较
int tt_compare_output(string &file_std, string &file_usr)
{
int ret = judge_conf::OJ_AC;
int c_std, c_usr;
FILE *fd_std = fopen(file_std.c_str(), "r");
FILE *fd_usr = fopen(file_usr.c_str(), "r");
//char *buffer;
//buffer = getcwd(NULL, 0);
//if (buffer != NULL) LOG_DEBUG("this is %s", buffer);
//LOG_DEBUG("The standard file is %s", file_std.c_str());
if (fd_std == NULL)
{
LOG_BUG("%s open standard file failed %s", strerror(errno), file_std.c_str());
}
//if (fd_usr == NULL)
//{
//LOG_BUG("open user file failed %s", file_usr.c_str());
//}
if (!fd_std || !fd_usr)
{
//LOG_DEBUG("compare This is the file: %s\n", problem::input_file.c_str());
ret = judge_conf::OJ_RE_ABRT;
}
else
{
c_std = fgetc(fd_std);
c_usr = fgetc(fd_usr);
for(;;)
{
compare_until_nonspace(c_std, c_usr, fd_std, fd_usr, ret);
while(!isspace(c_std) || !isspace(c_usr))
{
if (c_std == EOF && c_usr == EOF)
goto end;
// 如果只有一个文件结束
// 但是另一个文件的末尾是回车
// 那么也当做AC处理
// https://github.com/NJUST-FishTeam/OnlineJudgeCore/blob/master/core.h
if (c_std == EOF || c_usr == EOF) {
FILE *fd_tmp;
if (c_std == EOF) {
if (!is_space_char(c_usr)) {
ret = judge_conf::OJ_WA;
goto end;
}
fd_tmp = fd_usr;
} else {
if (!is_space_char(c_std)) {
ret = judge_conf::OJ_WA;
goto end;
}
fd_tmp = fd_std;
}
int c;
while ((c = fgetc(fd_tmp)) != EOF) {
if (c == '\r') c = '\n';
if (!is_space_char(c)) {
ret = judge_conf::OJ_WA;
goto end;
}
}
goto end;
}
if (c_std != c_usr)
{
ret = judge_conf::OJ_WA;
goto end;
}
c_std = fgetc(fd_std);
c_usr = fgetc(fd_usr);
}
}
}
end:
if (fd_std)
fclose(fd_std);
if (fd_usr)
fclose(fd_usr);
return ret;
}
int compare_output(string &file_std, string &file_usr)
{
// LOG_DEBUG("compare_output ");
return tt_compare_output(file_std, file_usr);
}
void parse_arguments(int argc, char *argv[]) //根据命令设置好配置.
{
int opt;
extern char *optarg;
while((opt = getopt(argc, argv, "l:D:d:t:m:o:S:T")) != -1)
{
//LOG_DEBUG(" -%c", opt);
switch(opt)
{
case 'l': sscanf(optarg, "%d", &problem::lang); break;
case 'D': problem::data_dir = optarg; break;
case 'd': problem::temp_dir = optarg; break;
case 't': sscanf(optarg, "%d", &problem::time_limit); break;
case 'm': sscanf(optarg, "%d", &problem::memory_limit); break;
case 'o': sscanf(optarg, "%d", &problem::output_limit); break;
case 'S': problem::spj = true; break;
case 'T': problem::tc = true; break;
default:
LOG_WARNING("unknown option -%c %s", opt, optarg);
output_result(judge_conf::OJ_SE, 0, judge_conf::EXIT_BAD_PARAM);
exit(judge_conf::EXIT_BAD_PARAM);
}
}
problem::time_limit *= Langs[problem::lang]->TimeFactor;
problem::memory_limit *= Langs[problem::lang]->MemFactor;
if (problem::tc)
{
problem::tc_file = problem::data_dir + "/" + Langs[problem::lang]->TCfile;
problem::tc_head = problem::data_dir + "/" + Langs[problem::lang]->TChead;
}
if (problem::spj == true)
{
problem::spj_exe_file = "spj.exe";
problem::stdout_file_spj = "stdout_spj.txt";
}
judge_conf::judge_time_limit += problem::time_limit;
#ifdef JUDGE_DEBUG
problem::Problem_debug();
#endif
}
//redirect io
void io_redirect()
{
freopen(problem::input_file.c_str(), "r", stdin);
freopen(problem::output_file.c_str(), "w", stdout);
freopen(problem::output_file.c_str(), "w", stderr);
if (stdin == NULL || stdout == NULL)
{
LOG_BUG("error in freopen: stdin(%p) stdout(%p)", stdin, stdout);
exit(judge_conf::EXIT_PRE_JUDGE);
}
#ifdef JUDGE_DEBUG
LOG_DEBUG("io redirece ok!");
#endif
}
void set_limit()
{
rlimit lim;
//时间限制
lim.rlim_cur = (problem::time_limit + 999)/1000 + 1;
//LOG_DEBUG("rlim_cur(%d).", lim.rlim_cur);
lim.rlim_max = lim.rlim_cur * 10;
if (setrlimit(RLIMIT_CPU, &lim) < 0)
{
LOG_BUG("error setrlimit for rlimit_cpu");
exit(judge_conf::EXIT_SET_LIMIT);
}
//内存大小限制 java 无法运行
/*
if (problem::lang != judge_conf::LANG_JAVA)
{
lim.rlim_max = problem::memory_limit * judge_conf::KILO;
}else lim.rlim_max = (problem::memory_limit + 8192)*judge_conf::KILO;
lim.rlim_cur = lim.rlim_max;
if (setrlimit(RLIMIT_AS, &lim) < 0)
{
LOG_BUG("error setrlimit for rlimit_as");
exit(judge_conf::EXIT_SET_LIMIT);
}*/
//设置堆栈的大小 漏掉主程序会SIGSEGV
getrlimit(RLIMIT_STACK, &lim);
int rlim = judge_conf::stack_size_limit*judge_conf::KILO;
//LOG_DEBUG("set stack size : %d", rlim);
if (lim.rlim_max <= rlim)
{
LOG_WARNING("can't set stack size to higher(%d <= %d)", lim.rlim_max, rlim);
}
else
{
lim.rlim_max = rlim;
lim.rlim_cur = rlim;
if (setrlimit(RLIMIT_STACK, &lim) < 0)
{
LOG_WARNING("setrlimit RLIMIT_STACK failed: %s", strerror(errno));
exit(judge_conf::EXIT_SET_LIMIT);
}
}
log_close();
//输出文件限制
lim.rlim_max = problem::output_limit * judge_conf::KILO;
lim.rlim_cur = lim.rlim_max;
//LOG_BUG("rlim_fsize %d", lim.rlim_max);
if (setrlimit(RLIMIT_FSIZE, &lim) < 0)
{
//LOG_BUG("error setrlimit for rlimit_fsize");
exit(judge_conf::EXIT_SET_LIMIT);
}
//log_close();
}
int Compiler()
{
int status = 0;
pid_t compiler = fork();
if (compiler < 0)
{
LOG_WARNING("error fork compiler, %d:%s", errno, strerror(errno));
output_result(judge_conf::OJ_SE, -errno, judge_conf::EXIT_COMPILE);
exit(judge_conf::EXIT_COMPILE);
}
else if (compiler == 0)
{
chdir(problem::temp_dir.c_str());
freopen("ce.txt", "w", stderr); //编译出错信息
freopen("/dev/null", "w", stdout); //防止编译器在标准输出中输出额外的信息影响评测
malarm(ITIMER_REAL, judge_conf::compile_time_limit);
execvp( Langs[problem::lang]->CompileCmd[0], (char * const *) Langs[problem::lang]->CompileCmd );
//execvp error
LOG_WARNING("compile evecvp error");
exit(judge_conf::EXIT_COMPILE);
}
else
{
waitpid(compiler, &status, 0);
return status;
}
}
const int bufsize = 1024;
int getmemory(pid_t userexe)
{
int ret = 0;
FILE *pd;
char fn[bufsize], buf[bufsize];
sprintf(fn, "/proc/%d/status", userexe);
pd = fopen(fn, "r");
while(pd && fgets(buf, bufsize-1, pd)) //这里漏掉了pd & 导致主进程SIGSEGV
{
if (strncmp(buf, "VmPeak:", 7) == 0)
{
sscanf(buf+8, "%d", &ret);
}
}
if (pd) fclose(pd); //9.19 发现漏了,会too many open file
return ret;
}
bool isInFile(char *filename)
{
int len = strlen(filename);
if (len < 3 || strcmp(filename+len-3, ".in") != 0)
return false;
return true;
}
json get_meta_json()
{
FILE *ce_msg = fopen("meta.json", "r");
std::string message = "";
char tmp[1024];
while (fgets(tmp, sizeof(tmp), ce_msg)) {
message += tmp;
}
return json::parse(message);
}
void sigseg(int)
{
output_result(judge_conf::OJ_SE, 0, judge_conf::EXIT_UNPRIVILEGED);
exit(judge_conf::EXIT_UNPRIVILEGED);
}
int main(int argc, char *argv[])
{
judge_conf::ReadConf();
log_open(judge_conf::log_file.c_str());
#ifdef JUDGE_DEBUG
LOG_DEBUG("start judge");
#endif
if (geteuid() != 0)
{
#ifdef JUDGE_DEBUG
LOG_DEBUG("euid %d != 0, please run as root , or chmod 4755 judge", geteuid());
#endif
output_result(judge_conf::OJ_SE, 0, judge_conf::EXIT_UNPRIVILEGED);
exit(judge_conf::EXIT_UNPRIVILEGED);
}
parse_arguments(argc, argv);
//设置judge运行时限
if (EXIT_SUCCESS != malarm(ITIMER_REAL, judge_conf::judge_time_limit))
{
LOG_WARNING("set judge alarm failed, %d : %s", errno, strerror(errno));
output_result(judge_conf::OJ_SE, 0, judge_conf::EXIT_VERY_FIRST);
exit(judge_conf::EXIT_VERY_FIRST);
}
signal(SIGALRM, timeout);
//tc 模式
if (problem::tc)
{
tc_mode();
}
//编译
int compile_ok = Compiler();
if (compile_ok != 0) //测试结果OJ_CE
{
output_result(judge_conf::OJ_CE);
exit(judge_conf::EXIT_OK);
}
json meta = get_meta_json();
json result = json::array();
//运行 judge
DIR * dp;
struct dirent *dirp;
dp = opendir(problem::data_dir.c_str());
if (dp == NULL)
{
LOG_WARNING("error opening dir %s", problem::data_dir.c_str());
output_result(judge_conf::OJ_SE, 0, judge_conf::EXIT_PRE_JUDGE);
return judge_conf::EXIT_PRE_JUDGE;
}
// char nametmp[1024];
for (auto& testcase : meta["testcases"])
{
struct rusage rused;
// if (strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0)
// continue;
// if (isInFile(dirp->d_name))
// {
// strcpy(nametmp, dirp->d_name);
// int len = strlen(nametmp);
// nametmp[len-3] = '\0';
string nametmp = testcase["uuid"];
problem::input_file = problem::data_dir + "/" + nametmp + ".in";
problem::output_file_std = problem::data_dir + "/" + nametmp + ".out";
problem::output_file = problem::temp_dir + "/" + nametmp + ".out";
#ifdef JUDGE_DEBUG
problem::Problem_debug();
#endif
pid_t userexe = fork();
if (userexe < 0)
{
LOG_WARNING("fork for userexe failed, %d:%s", errno, strerror(errno));
output_result(judge_conf::OJ_SE, -errno, judge_conf::EXIT_PRE_JUDGE);
exit(judge_conf::EXIT_PRE_JUDGE);
}
else if (userexe == 0)
{
signal(SIGSEGV, sigseg);
//重新定向io
io_redirect();
//设置运行根目录、运行用户
//获得运行用户的信息
struct passwd *judge = getpwnam(judge_conf::sysuser.c_str());
if (judge == NULL)
{
LOG_BUG("no user named %s", judge_conf::sysuser.c_str());
exit(judge_conf::EXIT_SET_SECURITY);
}
//切换目录
if (EXIT_SUCCESS != chdir(problem::temp_dir.c_str()))
{
LOG_BUG("chdir failed");
exit(judge_conf::EXIT_SET_SECURITY);
}
/* char cwd[1024], *tmp = getcwd(cwd, 1024);
if (tmp == NULL)
{ //获取当前目录失败
LOG_BUG("getcwd failed");
exit(judge_conf::EXIT_SET_SECURITY);
}*/
//#ifdef JUDEG_DEBUG
//这里现在在fedora下有bug
//设置根目录
/* if (problem::lang == judge_conf::LANG_JAVA)
* {
if (EXIT_SUCCESS != chroot(cwd))
{
LOG_BUG("chroot failed");
exit(judge_conf::EXIT_SET_SECURITY);
}
}*/
//#endif
//设置有效用户
if (EXIT_SUCCESS != setuid(judge->pw_uid))
{
LOG_BUG("setuid failed");
exit(judge_conf::EXIT_SET_SECURITY);
}
int user_time_limit = problem::time_limit + judge_conf::time_limit_addtion;
//设置用户程序的运行实际时间,防止意外情况卡住
if (EXIT_SUCCESS != malarm(ITIMER_REAL, user_time_limit))
{
LOG_WARNING("malarm failed");
exit(judge_conf::EXIT_PRE_JUDGE);
}
//ptrace 监控下面程序
if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) < 0)
{
LOG_BUG("ptrace failed");
exit(judge_conf::EXIT_PRE_JUDGE_PTRACE);
}
//用setrlimit 设置用户程序的 内存 时间 输出文件大小的限制
//log close for fsize
set_limit();
//运行程序
execvp( Langs[problem::lang]->RunCmd[0], (char * const *) Langs[problem::lang]->RunCmd );
int errsa = errno;
exit(judge_conf::EXIT_PRE_JUDGE_EXECLP);
}
else
{
//父进程监控子进程的状态和系统调用
int status = 0;
int syscall_id = 0;
struct user_regs_struct regs;
init_ok_table(problem::lang);
while (true)
{
if (wait4(userexe, &status, 0, &rused) < 0)
{
LOG_BUG("wait4 failed, %d:%s", errno, strerror(errno));
output_result(judge_conf::OJ_SE, -errno, judge_conf::EXIT_JUDGE);
exit(judge_conf::EXIT_JUDGE);
}
//如果是退出信号
if (WIFEXITED(status))
{
//java 返回非0表示出错
if (!Langs[problem::lang]->VMrunning || WEXITSTATUS(status) == EXIT_SUCCESS)
{
int result;
if (problem::spj)
{
//spj
result = spj_compare_output(problem::input_file,
problem::output_file,
problem::data_dir, //problem::spj_exe_file, modif y in 13-11-10
problem::temp_dir + "/" + problem::stdout_file_spj,
problem::output_file_std);
}
else
{
result = compare_output(problem::output_file_std, problem::output_file);
}
//记录结果
if (result != judge_conf::OJ_AC)
{
problem::result = result;
}
else
{
if (problem::result != judge_conf::OJ_PE)
problem::result = result;
}
}
else
{
LOG_BUG("abort quit code: %d\n", WEXITSTATUS(status));
problem::result = judge_conf::OJ_RE_JAVA;
}
break;
}
// 收到信号
// RE/TLE/OLE 超过sterlimit限制而结束
// 且过滤掉被ptrace暂停的 SIGTRAP
if (WIFSIGNALED(status) || (WIFSTOPPED(status) && WSTOPSIG(status) != SIGTRAP))
{
int signo = 0;
if (WIFSIGNALED(status))
signo = WTERMSIG(status);
else
signo = WSTOPSIG(status);
switch(signo)
{
//TLE
// case SIGALRM:
//LOG_BUG("ALRM");
case SIGXCPU:
//LOG_BUG("XCPU");
// case SIGVTALRM:
//LOG_BUG("TALRM");
case SIGKILL:
//LOG_BUG("KILL");
problem::result = judge_conf::OJ_TLE;
problem::time_usage = problem::time_limit;
break;
//OLE
case SIGXFSZ:
problem::result = judge_conf::OJ_OLE;
break;
case SIGSEGV:
problem::result = judge_conf::OJ_RE_SEGV;
break;
case SIGABRT:
problem::result = judge_conf::OJ_RE_ABRT;
break;
default:
problem::result = judge_conf::OJ_RE_UNKNOW;
}
//This is a debug
LOG_DEBUG("This is the file: %s\n", problem::input_file.c_str());
//
ptrace(PTRACE_KILL, userexe);
break;
}//end if
//MLE
// LOG_DEBUG("old memory: %d , new memory: %d", problem::memory_usage,
// rused.ru_minflt *(getpagesize()/judge_conf::KILO));
int tempmemory = 0;
if (Langs[problem::lang]->VMrunning)
{
tempmemory = rused.ru_minflt *(getpagesize()/judge_conf::KILO);
}
else
{
tempmemory = getmemory(userexe);
}
problem::memory_usage = Max(problem::memory_usage, tempmemory);
if (problem::memory_usage > problem::memory_limit)
{
problem::result = judge_conf::OJ_MLE;
ptrace(PTRACE_KILL, userexe);
break;
}
//检查userexe的syscall
if (ptrace(PTRACE_GETREGS, userexe, 0, ®s) < 0)
{
LOG_BUG("error ptrace ptrace_getregs, %d:%s", errno, strerror(errno));
output_result(judge_conf::OJ_SE, -errno, judge_conf::EXIT_JUDGE);
exit(judge_conf::EXIT_JUDGE);
}
#ifdef __i386__
syscall_id = regs.orig_eax;
#else
syscall_id = regs.orig_rax;
#endif
// 取消对 Java 安全性检查,否则该评测机不能正常运行
if (judge_conf::LANG_JAVA != problem::lang && syscall_id > 0 && (!is_valid_syscall(problem::lang, syscall_id, userexe, regs)))
{
LOG_WARNING("error for syscall %d", syscall_id);
problem::result = judge_conf::OJ_RF;
problem::time_usage = syscall_id;
ptrace(PTRACE_KILL, userexe, NULL, NULL);
break;
}
if (ptrace(PTRACE_SYSCALL, userexe, NULL, NULL) < 0)
{
LOG_BUG("error ptrace ptrace syscall, %d:%s", errno, strerror(errno));
output_result(judge_conf::OJ_SE, -errno, judge_conf::EXIT_JUDGE);
exit(judge_conf::EXIT_JUDGE);
}
}//while (true)
}//else userexe end
// if (problem::result == judge_conf::OJ_RF) break;
int time_tmp = rused.ru_utime.tv_sec*1000 + rused.ru_utime.tv_usec/1000
+ rused.ru_stime.tv_sec*1000 + rused.ru_stime.tv_usec/1000;
if (problem::time_usage < time_tmp)
{
problem::time_usage = time_tmp;
}
//LOG_DEBUG("time_usage(%d).", problem::time_usage);
if (problem::time_usage > problem::time_limit)
{
problem::time_usage = problem::time_limit;
problem::result = judge_conf::OJ_TLE;
}
if (problem::memory_usage > problem::memory_limit)
{
problem::memory_usage = problem::memory_limit;
problem::result = judge_conf::OJ_MLE;
}
// if (problem::result != judge_conf::OJ_AC && problem::result != judge_conf::OJ_PE)
// {
// break;
// }
json j = json::object();
j["uuid"] = nametmp;
j["judge"] = problem::result;
j["time"] = problem::time_usage;
j["memory"] = problem::memory_usage;
result.push_back(j);
// }//if (isInfile())
}//end while, next input file
// output_result(problem::result, problem::memory_usage, problem::time_usage);
output_result(result, problem::temp_dir + "/result.json");
return 0;
}