forked from svn2github/hustoj
-
Notifications
You must be signed in to change notification settings - Fork 776
/
Copy pathjudge_client.cc
4057 lines (3793 loc) · 124 KB
/
judge_client.cc
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
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// File: judge_client.cc
// Author: sempr
// refacted by zhblue
/*
* Copyright 2008 sempr <[email protected]>
*
* Refacted and modified by zhblue<[email protected]>
* Bug report email [email protected]
*
*
* This file is part of HUSTOJ.
*
* HUSTOJ 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 2 of the License, or
* (at your option) any later version.
*
* HUSTOJ 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 HUSTOJ. if not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <syslog.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <time.h>
#include <math.h>
#include <stdarg.h>
#include <ctype.h>
#include <sys/wait.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/user.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/signal.h>
//#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#ifdef OJ_USE_MYSQL
#include <mysql.h>
#endif
#include <assert.h>
#include "okcalls.h"
#define STD_MB 1048576LL
#define STD_T_LIM 2
#define STD_F_LIM (STD_MB << 9) //default file size limit 512m ,2^9=512
#define STD_M_LIM (STD_MB << 8) //default memory limit 256m ,2^8=256
#define BUFFER_SIZE 4096 //default size of char buffer 5120 bytes
#define LOCKMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
#define OJ_WT0 0 //提交排队
#define OJ_WT1 1 //重判排队
#define OJ_CI 2 //编译中(任务已派发)
#define OJ_RI 3 //运行中
#define OJ_AC 4 //答案正确
#define OJ_PE 5 //格式错误
#define OJ_WA 6 //答案错误
#define OJ_TL 7 //时间超限
#define OJ_ML 8 //内存超限
#define OJ_OL 9 //输出超限
#define OJ_RE 10 //运行错误
#define OJ_CE 11 //编译错误
#define OJ_CO 12 //编译完成
#define OJ_TR 13 //测试运行结束
#define OJ_MC 14 // 等待裁判手工确认
#ifdef __arm__ // arm 的寄存器结构
struct user_regs_struct {
long uregs[18];
};
#define ARM_r7 uregs[7]
#define ARM_ORIG_r0 uregs[17]
#define REG_SYSCALL ARM_r7
#endif
#ifdef __aarch64__ //arm64的寄存器结构
#define NT_PRSTATUS 1
#define NT_ARM_SYSTEM_CALL 0x404
#define ARM_cpsr uregs[16]
#define ARM_pc uregs[15]
#define ARM_lr uregs[14]
#define ARM_sp uregs[13]
#define ARM_ip uregs[12]
#define ARM_fp uregs[11]
#define ARM_r10 uregs[10]
#define ARM_r9 uregs[9]
#define ARM_r8 regs[8]
#define ARM_r7 uregs[7]
#define ARM_r6 uregs[6]
#define ARM_r5 uregs[5]
#define ARM_r4 uregs[4]
#define ARM_r3 uregs[3]
#define ARM_r2 uregs[2]
#define ARM_r1 uregs[1]
#define ARM_r0 uregs[0]
#define ARM_ORIG_r0 uregs[17]
#define PTRACE_GETREGS PTRACE_GETREGSET
#define PTRACE_SETREGS PTRACE_SETREGSET
#define REG_SYSCALL regs[18]
#endif
#ifdef __mips__ //mips 龙芯的寄存器结构
typedef unsigned long long uint64_t;
struct user_regs_struct{
uint64_t uregs[38];
};
#define REG_V0 2
#define REG_A0 4
#define mips_REG_V0 uregs[REG_V0]
#define REG_SYSCALL mips_REG_V0
#endif
#ifdef __i386 //32位x86寄存器
#define REG_SYSCALL orig_eax
#define REG_RET eax
#define REG_ARG0 ebx
#define REG_ARG1 ecx
#endif
#ifdef __x86_64__ //64位x86寄存器
#define REG_SYSCALL orig_rax
#define REG_RET rax
#define REG_ARG0 rdi
#define REG_ARG1 rsi
#endif
static int DEBUG = 0;
static char host_name[BUFFER_SIZE/10]; //数据库服务器地址
static int port_number; //端口
static char user_name[BUFFER_SIZE/10]; //用户名
static char password[BUFFER_SIZE/10]; //密码
static char db_name[BUFFER_SIZE/10]; //库名
static char oj_home[BUFFER_SIZE/10]; //判题系统主目录
static char data_list[BUFFER_SIZE][BUFFER_SIZE]; //测试数据列表
static int data_list_len = 0; //列表长度
static char lock_file[BUFFER_SIZE]="/home/judge/run0/judge_client.pid"; //工作目录锁定文件
static int max_running;
static int sleep_time;
static int java_time_bonus = 5;
static int java_memory_bonus = 512;
static char java_xms[BUFFER_SIZE/10];
static char java_xmx[BUFFER_SIZE/10];
static int sim_enable = 0;
static int oi_mode = 0;
static int full_diff = 1;
static int raw_text_diff = 1;
static int use_max_time = 0;
static int time_limit_to_total= 1;
static int total_time= 0;
static int http_judge = 0;
static int copy_data= 0;
static char http_baseurl[BUFFER_SIZE/10];
static char http_apipath[BUFFER_SIZE/10];
static char http_loginpath[BUFFER_SIZE/10];
static char http_username[BUFFER_SIZE/10];
static char http_password[BUFFER_SIZE/10];
static int http_download = 1;
static double cpu_compensation = 1.0;
static int shm_run = 0;
static char record_call = 0;
static int use_ptrace = 1;
static int ignore_esol= 1;
static int internal_mark= 0;
static int compile_chroot = 0;
static int turbo_mode = 0;
static int python_free=0;
static int use_docker=0;
static const char *tbname = "solution";
static char cc_opt[BUFFER_SIZE/10];
static char cc_std[BUFFER_SIZE/10];
static char cpp_std[BUFFER_SIZE/10];
static int auto_result = OJ_AC ;
int num_of_test = 0;
//static int sleep_tmp;
size_t prelen=16;
static int py2=1; // caution: py2=1 means default using py3
#define ZOJ_COM
#ifdef _mysql_h
MYSQL *conn;
#endif
static char jresult[15][4]={"PD","PR","CI","RJ","AC","PE","WA","TLE","MLE","OLE","RE","CE","CO","TR","MC"};
static char lang_ext[25][8] = {"c", "cc", "pas", "java", "rb", "sh", "py",
"php", "pl", "cs", "m", "bas", "scm", "c", "cc", "lua", "js", "go","sql","f95","m","cob","R","sb3","cj"};
//static char buf[BUFFER_SIZE];
int lockfile(int fd) {
struct flock fl;
fl.l_type = F_WRLCK;
fl.l_start = 0;
fl.l_whence = SEEK_SET;
fl.l_len = 0;
return (fcntl(fd, F_SETLK, &fl));
}
int already_running() {
int fd;
char buf[16];
fd = open(lock_file, O_RDWR | O_CREAT, LOCKMODE);
if (fd < 0) {
if(DEBUG)printf("%s open fail.\n",lock_file);
exit(1);
}
if (lockfile(fd) < 0) {
if (errno == EACCES || errno == EAGAIN) {
close(fd);
return 1;
}
if(DEBUG)printf("%s lock fail.\n",lock_file);
exit(1);
}
if(ftruncate(fd, 0)) printf("close file fail 0 \n");
sprintf(buf, "%d", getpid());
if(write(fd, buf, strlen(buf) + 1)>=BUFFER_SIZE) printf("buffer size overflow!...\n");
return (0);
}
void print_arm_regs(long long unsigned int *d){
for(int i=0;i<32;i++){
printf("[%d]:%lld ",i,d[i]%CALL_ARRAY_SIZE);
}
printf("\n");
}
int data_list_has(char *file)
{
for (int i = 0; i < data_list_len; i++)
{
if (strcmp(data_list[i], file) == 0)
return 1;
}
return 0;
}
int data_list_add(char *file)
{
if (data_list_len < BUFFER_SIZE - 1)
{
strcpy(data_list[data_list_len], file);
data_list_len++;
return 0;
}
else
{
return 1;
}
}
long get_file_size(const char *filename)
{
struct stat f_stat;
if (stat(filename, &f_stat) == -1)
{
return 0;
}
return (long)f_stat.st_size;
}
void write_log(const char *_fmt, ...)
{
va_list ap;
char fmt[BUFFER_SIZE];
strncpy(fmt, _fmt,BUFFER_SIZE);
char buffer[BUFFER_SIZE];
// time_t t = time(NULL);
//int l;
sprintf(buffer, "%s/log/client.log", oj_home);
FILE *fp = fopen(buffer, "ae+");
if (fp == NULL)
{
fprintf(stderr, "%s/log/client.log openfile error!\n",oj_home);
exit(-6);
}
va_start(ap, _fmt);
//l =
vsprintf(buffer, fmt, ap);
fprintf(fp, "%s\n", buffer);
if (DEBUG)
printf("%s\n", buffer);
va_end(ap);
fclose(fp);
}
int execute_cmd(const char *fmt, ...) //执行命令获得返回值
{
char cmd[BUFFER_SIZE];
int ret = 0;
va_list ap;
va_start(ap, fmt);
vsprintf(cmd, fmt, ap);
if (DEBUG)
printf("%s\n", cmd);
ret = system(cmd);
va_end(ap);
if (DEBUG)
printf("\n");
return ret;
}
const int call_array_size = CALL_ARRAY_SIZE;
unsigned int call_id = 0;
int call_counter[call_array_size] = {0};
static char LANG_NAME[BUFFER_SIZE];
void init_syscalls_limits(int lang) //白名单初始化
{
int i;
memset(call_counter, 0, sizeof(call_counter));
if (DEBUG)
write_log("init_call_counter:%d", lang);
if (record_call)
{ // recording for debuging
for (i = 0; i < call_array_size; i++)
{
call_counter[i] = 0;
}
}
else if (lang <= LANG_CPP || lang == LANG_CLANG || lang == LANG_CLANGPP )
{ // C & C++
for (i = 0; i == 0 || LANG_CV[i]; i++)
{
call_counter[LANG_CV[i]] = HOJ_MAX_LIMIT;
}
}
else if (lang == LANG_PASCAL )
{ // Pascal
for (i = 0; i == 0 || LANG_PV[i]; i++)
call_counter[LANG_PV[i]] = HOJ_MAX_LIMIT;
}
else if (lang == LANG_JAVA)
{ // Java
for (i = 0; i == 0 || LANG_JV[i]; i++)
call_counter[LANG_JV[i]] = HOJ_MAX_LIMIT;
}
else if (lang == LANG_RUBY)
{ // Ruby
for (i = 0; i == 0 || LANG_RV[i]; i++)
call_counter[LANG_RV[i]] = HOJ_MAX_LIMIT;
}
else if (lang == LANG_BASH)
{ // Bash
for (i = 0; i == 0 || LANG_BV[i]; i++)
call_counter[LANG_BV[i]] = HOJ_MAX_LIMIT;
}
else if (lang == LANG_PYTHON)
{ // Python
for (i = 0; i == 0 || LANG_YV[i]; i++)
call_counter[LANG_YV[i]] = HOJ_MAX_LIMIT;
}
else if (lang == LANG_PHP)
{ // php
for (i = 0; i == 0 || LANG_PHV[i]; i++)
call_counter[LANG_PHV[i]] = HOJ_MAX_LIMIT;
}
else if (lang == LANG_PERL )
{ // perl
for (i = 0; i == 0 || LANG_PLV[i]; i++)
call_counter[LANG_PLV[i]] = HOJ_MAX_LIMIT;
}
else if (lang == LANG_CSHARP )
{ // mono c#
for (i = 0; i == 0 || LANG_CSV[i]; i++)
call_counter[LANG_CSV[i]] = HOJ_MAX_LIMIT;
}
else if (lang == LANG_OBJC )
{ //objective c
for (i = 0; i == 0 || LANG_OV[i]; i++)
call_counter[LANG_OV[i]] = HOJ_MAX_LIMIT;
}
else if (lang == LANG_FREEBASIC)
{ //free basic
for (i = 0; i == 0 || LANG_BASICV[i]; i++)
call_counter[LANG_BASICV[i]] = HOJ_MAX_LIMIT;
}
else if (lang == LANG_SCHEME )
{ //scheme guile
for (i = 0; i == 0 || LANG_SV[i]; i++)
call_counter[LANG_SV[i]] = HOJ_MAX_LIMIT;
}
else if (lang == LANG_LUA )
{ //lua
for (i = 0; i == 0 || LANG_LUAV[i]; i++)
call_counter[LANG_LUAV[i]] = HOJ_MAX_LIMIT;
}
else if (lang == LANG_JS )
{ //nodejs
for (i = 0; i == 0 || LANG_JSV[i]; i++)
call_counter[LANG_JSV[i]] = HOJ_MAX_LIMIT;
}
else if (lang == LANG_GO )
{ //go
for (i = 0; i == 0 || LANG_GOV[i]; i++)
call_counter[LANG_GOV[i]] = HOJ_MAX_LIMIT;
}
else if (lang == LANG_SQL )
{ //go
for (i = 0; i == 0 || LANG_SQLV[i]; i++)
call_counter[LANG_SQLV[i]] = HOJ_MAX_LIMIT;
}
else if (lang == LANG_FORTRAN )
{ //go
for (i = 0; i == 0 || LANG_FV[i]; i++)
call_counter[LANG_FV[i]] = HOJ_MAX_LIMIT;
}
else if (lang == LANG_MATLAB )
{ //matlab
for (i = 0; i == 0 || LANG_MV[i]; i++)
call_counter[LANG_MV[i]] = HOJ_MAX_LIMIT;
}
else if (lang == LANG_COBOL )
{ //cobol
for (i = 0; i == 0 || LANG_CBV[i]; i++)
call_counter[LANG_CBV[i]] = HOJ_MAX_LIMIT;
}else if(lang == LANG_R ){
for (i = 0; i == 0 || LANG_RLV[i]; i++)
call_counter[LANG_RLV[i]] = HOJ_MAX_LIMIT;
}else if(lang == LANG_SB3 ){
for (i = 0; i == 0 || LANG_SB3V[i]; i++)
call_counter[LANG_SB3V[i]] = HOJ_MAX_LIMIT;
}else if(lang == LANG_CJ ){
for (i = 0; i == 0 || LANG_CJV[i]; i++)
call_counter[LANG_CJV[i]] = HOJ_MAX_LIMIT;
}
#ifdef __aarch64__
if (lang==3)call_counter[220]= 100;
else call_counter[220]= 1;
#else
call_counter[SYS_execve % call_array_size ]= 1;
if(lang == LANG_R ) call_counter[SYS_execve % call_array_size ]= 8;
#endif
printf("SYS_execve:%d [%d] \n",SYS_execve % call_array_size , call_counter[SYS_execve % call_array_size ]);
}
int after_equal(char *c)
{
int i = 0;
for (; c[i] != '\0' && c[i] != '='; i++)
;
return ++i;
}
void trim(char *c)
{
char buf[BUFFER_SIZE];
char *start, *end;
strcpy(buf, c);
start = buf;
while (isspace(*start))
start++;
end = start;
while (!isspace(*end))
end++;
*end = '\0';
strcpy(c, start);
}
bool read_buf(char *buf, const char *key, char *value)
{
if (strncmp(buf, key, strlen(key)) == 0)
{
strcpy(value, buf + after_equal(buf));
trim(value);
if (DEBUG)
printf("%s=%s\n", key, value);
return 1;
}
return 0;
}
void read_double(char *buf, const char *key, double *value)
{
char buf2[BUFFER_SIZE];
if (read_buf(buf, key, buf2))
if(1!=sscanf(buf2, "%lf", value)) printf("double value read fail\n");
}
void read_int(char *buf, const char *key, int *value)
{
char buf2[BUFFER_SIZE];
if (read_buf(buf, key, buf2))
if(1!=sscanf(buf2, "%d", value)) printf("int value read fail\n");
}
FILE *read_cmd_output(const char *fmt, ...)
{
char cmd[BUFFER_SIZE];
FILE *ret = NULL;
va_list ap;
va_start(ap, fmt);
vsprintf(cmd, fmt, ap);
va_end(ap);
if (DEBUG)
printf("%s\n", cmd);
ret = popen(cmd, "r");
return ret;
}
void load_conf(const char * judge_path ,int sys ){
FILE *fp = fopen(judge_path, "re");
if (fp != NULL){
char buf[BUFFER_SIZE]="";
while (fgets(buf, BUFFER_SIZE - 1, fp))
{
if(sys){
read_buf(buf, "OJ_HOST_NAME", host_name);
read_buf(buf, "OJ_USER_NAME", user_name);
read_buf(buf, "OJ_PASSWORD", password);
read_buf(buf, "OJ_DB_NAME", db_name);
read_int(buf, "OJ_PORT_NUMBER", &port_number);
read_int(buf, "OJ_HTTP_JUDGE", &http_judge);
read_buf(buf, "OJ_HTTP_BASEURL", http_baseurl);
read_buf(buf, "OJ_HTTP_APIPATH", http_apipath);
read_buf(buf, "OJ_HTTP_LOGINPATH", http_loginpath);
read_buf(buf, "OJ_HTTP_USERNAME", http_username);
read_buf(buf, "OJ_HTTP_PASSWORD", http_password);
read_int(buf, "OJ_HTTP_DOWNLOAD", &http_download);
read_int(buf, "OJ_USE_PTRACE", &use_ptrace);
read_int(buf, "OJ_COMPILE_CHROOT", &compile_chroot);
read_int(buf, "OJ_USE_DOCKER",&use_docker);
read_int(buf, "OJ_PYTHON_FREE", &python_free);
}
read_int(buf, "OJ_JAVA_TIME_BONUS", &java_time_bonus);
read_int(buf, "OJ_JAVA_MEMORY_BONUS", &java_memory_bonus);
read_int(buf, "OJ_SIM_ENABLE", &sim_enable);
read_buf(buf, "OJ_JAVA_XMS", java_xms);
read_buf(buf, "OJ_JAVA_XMX", java_xmx);
read_int(buf, "OJ_OI_MODE", &oi_mode);
read_int(buf, "OJ_FULL_DIFF", &full_diff);
read_int(buf, "OJ_RAW_TEXT_DIFF", &raw_text_diff);
read_int(buf, "OJ_SHM_RUN", &shm_run);
read_int(buf, "OJ_USE_MAX_TIME", &use_max_time);
read_int(buf, "OJ_TIME_LIMIT_TO_TOTAL", &time_limit_to_total);
read_int(buf, "OJ_IGNORE_ESOL", &ignore_esol);
read_int(buf, "OJ_INTERNAL_MARK", &internal_mark);
read_int(buf, "OJ_TURBO_MODE", &turbo_mode);
read_double(buf, "OJ_CPU_COMPENSATION", &cpu_compensation);
read_int(buf, "OJ_COPY_DATA", ©_data);
read_buf(buf, "OJ_CC_STD", cc_std);
read_buf(buf, "OJ_CPP_STD", cpp_std);
read_buf(buf, "OJ_CC_OPT", cc_opt);
read_int(buf, "OJ_AUTO_RESULT", &auto_result);
}
fclose(fp);
}
}
// read the configue file
void init_judge_conf() //读取判题主目录etc中的配置文件judge.conf
{
char judge_conf[BUFFER_SIZE]="/home/judge/etc/judge.conf";
host_name[0] = 0;
user_name[0] = 0;
password[0] = 0;
db_name[0] = 0;
port_number = 3306;
max_running = 3;
sleep_time = 3;
strcpy(java_xms, "-Xms32m");
strcpy(java_xmx, "-Xmx256m");
strcpy(cc_std,"-std=c99");
strcpy(cc_opt,"-O2");
if(__GNUC__ > 9 || ( __GNUC__ == 9 && __GNUC_MINOR__ >= 3 ) ){
// ubuntu20.04 introduce g++9.3
strcpy(cc_std,"-std=c17");
strcpy(cpp_std,"-std=c++14"); // CCF NOI change settings for NOIP to C++14 on 2021-9-1
}else{
strcpy(cc_std,"-std=c99");
strcpy(cpp_std,"-std=c++11");
}
sprintf(judge_conf, "%s/etc/judge.conf", oj_home);
load_conf(judge_conf,1);
// fclose(fp);
if(use_docker){
shm_run=0;
compile_chroot=0;
}
if(strcmp(http_username,"IP")==0){
FILE * fjobs = fopen("/etc/hostname","r");
if(1!=fscanf(fjobs, "%s", http_username)) printf("IP/HOSTNAME read fail...\n");
fclose(fjobs);
}
if(turbo_mode==2) tbname="solution2";
}
int isInFile(const char fname[])
{
int l = strlen(fname);
if (l <= 3 || strcmp(fname + l - 3, ".in") != 0)
return 0;
else
return l - 3;
}
int inFile(const struct dirent * dp){ //获得测试数据目录中测试数据列表
int l = strlen(dp->d_name);
if(DEBUG) printf("file name:%s\n",dp->d_name);
if(DEBUG) printf("ext name:%s\n",dp->d_name + l - 3);
int ret = isInFile(dp->d_name);
if(DEBUG) printf("\t:%d\n",ret);
return ret;
}
void find_next_nonspace(int &c1, int &c2, FILE *&f1, FILE *&f2, int &ret)
{
// Find the next non-space character or \n.
while ((isspace(c1)) || (isspace(c2)))
{
if (c1 != c2)
{
if (c2 == EOF)
{
do
{
c1 = fgetc(f1);
} while (isspace(c1));
continue;
}
else if (c1 == EOF)
{
do
{
c2 = fgetc(f2);
} while (isspace(c2));
continue;
}else if(ignore_esol){
if (isspace(c1) && isspace(c2))
{
while (c2 == '\n' && isspace(c1) && c1 != '\n')
c1 = fgetc(f1);
while (c1 == '\n' && isspace(c2) && c2 != '\n')
c2 = fgetc(f2);
}
else{
if (DEBUG)
printf("%d=%c\t%d=%c", c1, c1, c2, c2);
;
ret = OJ_PE;
}
}else if(!ignore_esol){
if ((c1 == '\r' && c2 == '\n'))
{
c1 = fgetc(f1);
}
else if ((c2 == '\r' && c1 == '\n'))
{
c2 = fgetc(f2);
}
else{
if (DEBUG)
printf("%d=%c\t%d=%c", c1, c1, c2, c2);
;
ret = OJ_PE;
}
}
}
if (isspace(c1))
{
c1 = fgetc(f1);
}
if (isspace(c2))
{
c2 = fgetc(f2);
}
}
}
/***
int compare_diff(const char *file1,const char *file2){
char diff[1024];
sprintf(diff,"diff -q -B -b -w --strip-trailing-cr %s %s",file1,file2);
int d=system(diff);
if (d) return OJ_WA;
sprintf(diff,"diff -q -B --strip-trailing-cr %s %s",file1,file2);
int p=system(diff);
if (p) return OJ_PE;
else return OJ_AC;
}
*/
const char *getFileNameFromPath(const char *path)
{
for (int i = strlen(path); i >= 0; i--)
{
if (path[i] == '/')
return &path[i + 1];
}
return path;
}
void make_diff_out_full(FILE *f1, FILE *f2, int c1, int c2, const char *path,const char * infile,const char * userfile)
{
execute_cmd("echo '========[%s]========='>>diff.out", getFileNameFromPath(path));
execute_cmd("echo '\\n------test in top 100 lines------'>>diff.out");
execute_cmd("head -100 %s >>diff.out",infile);
execute_cmd("echo '\\n------test out top 100 lines-----'>>diff.out");
execute_cmd("head -100 '%s'>>diff.out", path);
execute_cmd("echo '\\n------user out top 100 lines-----'>>diff.out");
execute_cmd("head -100 %s >>diff.out",userfile);
execute_cmd("echo '\\n------diff out 200 lines-----'>>diff.out");
execute_cmd("diff '%s' %s -y|grep \\||head -200>>diff.out", path,userfile);
execute_cmd("echo '\\n=============================='>>diff.out");
}
void make_diff_out(FILE *f1, FILE *f2, int c1, int c2, const char *path,const char * userfile )
{
execute_cmd("echo '%s\n--\n'>>diff.out", getFileNameFromPath(path));
execute_cmd("echo 'Expected|Yours\n--|--'>>diff.out");
execute_cmd("diff '%s' %s -y|head -100|tr '>/\\' ' ||' >>diff.out", path,userfile);
execute_cmd("echo '\n\n'>>diff.out");
}
char * str_replace(char * old, const char * search, const char * replace){
int p,r;
char *s;
char buf[BUFFER_SIZE];
r=strlen(replace);
p=strlen(search);
while (NULL!=(s=strstr(old,search))){
strcpy(buf,s+p);
if(strlen(old)-strlen(s)+strlen(buf)>BUFFER_SIZE) break;
strcpy(s,replace);
strcpy(s+r-p+1,buf);
}
return old;
}
bool is_str_utf8(const char* str)
{
unsigned int nBytes = 0;//UFT8可用1-6个字节编码,ASCII用一个字节
unsigned char chr = *str;
bool bAllAscii = true;
for (unsigned int i = 0; str[i] != '\0'; ++i){
chr = *(str + i);
//判断是否ASCII编码,如果不是,说明有可能是UTF8,ASCII用7位编码,最高位标记为0,0xxxxxxx
if (nBytes == 0 && (chr & 0x80) != 0){
bAllAscii = false;
}
if (nBytes == 0) {
//如果不是ASCII码,应该是多字节符,计算字节数
if (chr >= 0x80) {
if (chr >= 0xFC && chr <= 0xFD){
nBytes = 6;
}
else if (chr >= 0xF8){
nBytes = 5;
}
else if (chr >= 0xF0){
nBytes = 4;
}
else if (chr >= 0xE0){
nBytes = 3;
}
else if (chr >= 0xC0){
nBytes = 2;
}
else{
return false;
}
nBytes--;
}
}
else{
//多字节符的非首字节,应为 10xxxxxx
if ((chr & 0xC0) != 0x80){
return false;
}
//减到为零为止
nBytes--;
}
}
//违返UTF8编码规则
if (nBytes != 0) {
return false;
}
if (bAllAscii){ //如果全部都是ASCII, 也是UTF8
return true;
}
return true;
}
inline void fprintSafe(FILE * f,char * buf){
if(is_str_utf8(buf)){
str_replace(buf,"|","丨");
str_replace(buf,"[","[");
str_replace(buf,"]","]");
str_replace(buf,"(","(");
str_replace(buf,")",")");
str_replace(buf,"*","*");
str_replace(buf,"\r","");
str_replace(buf,"\n","↩");
fprintf(f,"%s",buf);
}else{
for(size_t i=0;i<strlen(buf);i++){
if(buf[i]==' ')
fprintf(f,"⬜");
else if(buf[i]=='\r')
continue;
else if(buf[i]=='\n')
fprintf(f,"↩");
else if (isprint(buf[i]))
fprintf(f,"%c",buf[i]);
else if(buf[i]==EOF)
fprintf(f,"⛔"); //Binary Code
else
fprintf(f,"`0x%02x` ",buf[i] & 0xff ); //Binary Code
}
}
}
void make_diff_out_simple(FILE *f1, FILE *f2,char * prefix, int c1, int c2, const char *path,const char * userfile )
{
char buf1[BUFFER_SIZE];
char buf2[BUFFER_SIZE];
FILE *diff=fopen("diff.out","a+");
fprintf(diff,"%s\n--\n", getFileNameFromPath(path));
fprintf(diff,"|Expected|Yours\n|--|--\n|...|...\n");
int row=0;
//int need=1;
while(!(feof(f1)&&feof(f2))){
row++;
fprintf(diff,"|");
if(row==1){
fprintSafe(diff,prefix);
if(c1!='\n'){
buf1[0]=c1; // patch buf1 with c1
if(!feof(f1)&&fgets(buf1+1,BUFFER_SIZE-2,f1)){
fprintSafe(diff,buf1);
}
}else{
fprintf(diff,"↩");
}
}else if(!feof(f1)&&fgets(buf1,BUFFER_SIZE-1,f1)){
fprintSafe(diff,buf1);
}else{
fprintf(diff,"⛔"); // standard output ending
}
fprintf(diff,"|");
if(row==1){
fprintSafe(diff,prefix);
if(c2==EOF){
fprintf(diff,"⛔"); //Binary Code
}else {
buf2[0]=c2; // patch buf2 with c2
if(!feof(f2)&&fgets(buf2+1,BUFFER_SIZE-2,f2)){
fprintf(diff,"`");
fprintSafe(diff,buf2);
fprintf(diff,"`");
}
}
}else if(!feof(f2)&&fgets(buf2,BUFFER_SIZE-1,f2)){
fprintSafe(diff,buf2);
}
fprintf(diff,"\n");
if(row>=5||ftell(diff)>128) break;
}
fprintf(diff,"\n\n");
fclose(diff);
}
/*
* translated from ZOJ judger r367
* http://code.google.com/p/zoj/source/browse/trunk/judge_client/client/text_checker.cc#25
* 参考zoj的文件流式比较器,有更低的内存占用
*/
int compare_zoj(const char *file1, const char *file2,const char * infile,const char * userfile,double * spj_mark)
{
int ret = OJ_AC;
int c1, c2;
char prefix[BUFFER_SIZE]="";
int preK=0;
FILE *f1, *f2;
f1 = fopen(file1, "re");
f2 = fopen(file2, "re");
if (!f1 || !f2)
{
ret = OJ_RE;
}
else
for (;;)
{
// Find the first non-space character at the beginning of line.
// Blank lines are skipped.
c1 = fgetc(f1);
c2 = fgetc(f2);
find_next_nonspace(c1, c2, f1, f2, ret);
// Compare the current line.
for (;;)
{
// Read until 2 files return a space or 0 together.
while ((!isspace(c1) && c1) || (!isspace(c2) && c2))
{
if (c1 == EOF && c2 == EOF)
{
goto end;
}
if (c1 == EOF || c2 == EOF)
{
break;
}
if (c1 != c2) {
// Consecutive non-space characters should be all exactly the ifconfig|grep 'inet'|awk -F: '{printf $2}'|awk '{printf $1}'same
ret = OJ_WA;
goto end;
}else if(preK<BUFFER_SIZE-1){
prefix[preK++]=c1;
prefix[preK]='\0';
}else{
preK=0;
prefix[preK]='\0';
}
c1 = fgetc(f1);
c2 = fgetc(f2);
}
find_next_nonspace(c1, c2, f1, f2, ret);
preK=0;
prefix[preK]='\0';
if (c1 == EOF && c2 == EOF)
{
goto end;
}
if (c1 == EOF || c2 == EOF)
{
ret = OJ_WA;
goto end;
}
if ((c1 == '\n' || !c1) && (c2 == '\n' || !c2))
{
break;
}
}
}
end:
long out_size,user_now;
out_size=get_file_size(file1);
user_now=ftell(f2);
if(DEBUG) printf("user/out=%ld/%ld\n",user_now,out_size);
if(internal_mark && user_now>1 && out_size>user_now) *spj_mark=(user_now-1.00)/out_size;
if (ret == OJ_WA || ret == OJ_PE)
{
if (full_diff) make_diff_out_simple(f1, f2, prefix, c1, c2, file1,userfile);
}
if (f1)
fclose(f1);
if (f2)
fclose(f2);
return ret;
}
void delnextline(char s[])
{
int L;
L = strlen(s);
while (L > 0 && (s[L - 1] == '\n' || s[L - 1] == '\r'))
s[--L] = 0;
}
int compare(const char *file1, const char *file2, const char * infile,const char * userfile,double * spj_mark)
{
#ifdef ZOJ_COM
//compare ported and improved from zoj don't limit file size
return compare_zoj(file1, file2,infile,userfile,spj_mark);
#endif
#ifndef ZOJ_COM
//the original compare from the first version of hustoj has file size limit 原始的内存中比较,速度更快但耗用更多内存
//and waste memory
FILE *f1, *f2;
char *s1, *s2, *p1, *p2;
int PEflg;
s1 = new char[STD_F_LIM + 512];
s2 = new char[STD_F_LIM + 512];
if (!(f1 = fopen(file1, "re")))
return OJ_AC;
for (p1 = s1; EOF != fscanf(f1, "%s", p1);)
while (*p1)
p1++;
fclose(f1);
if (!(f2 = fopen(file2, "re")))
return OJ_RE;