-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjudge.cpp
More file actions
1188 lines (1058 loc) · 43.4 KB
/
Copy pathjudge.cpp
File metadata and controls
1188 lines (1058 loc) · 43.4 KB
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
// Compile Parameters:
// For Linux: g++ judge.cpp -o /usr/bin/judge -lmysqlclient -ljsoncpp -pthread
// For Windows: g++ judge.cpp -o judge.exe -lpsapi -ljson -lmysql -pthread
#include<bits/stdc++.h>
#ifdef __linux__
#include<unistd.h>
#include<stdlib.h>
#include<sys/resource.h>
#include<sys/wait.h>
#include<jsoncpp/json/json.h>
#elif _WIN32
#include<Windows.h>
#include<psapi.h>
#include<json/json.h>
#endif
#include<mysql/mysql.h>
using namespace std;
typedef vector<map<string,string> > mysqld;
// ****************************************************
// Class Name: None
// Class Module: None
// Class Features: Single Functions
// ****************************************************
// Convert String to Integer
long long StringToInt(string x) {
long long res=0;
for (int i=0;i<x.size();i++)
if (isdigit(x[i])) res*=10,res+=x[i]-'0';
else break;
return res;
}
// Convert Integer to String (Like Function to_string())
string IntToString(int x) {
if (x==0) return "0";
char res[101]="";int k=-1;
while (x) {
res[++k]=x%10+'0';
x/=10;
}
reverse(res,res+k+1);
return res;
}
// Replace A to B in String
string str_replace(const char* from,const char* to,const char* source) {
string result=source;
int st_place=0,where=result.find(from,st_place);
while (where!=string::npos) {
result.replace(where,((string)from).size(),to);
st_place=where+((string)to).size();
where=result.find(from,st_place);
} return result;
}
// Get The Absolute Path
string getpath(const char* path) {
char res[100010]="";
#ifdef __linux__
if(realpath(path,res)) return res;
#elif _WIN32
if (_fullpath(res,path,100010)) return res;
#endif
else return "/";
}
// System Function Advanced Mode
string garbage="";
#ifdef __linux__
int system2(const char* cmd,string& res) {
FILE *stream;
char buf[1024*1024];
memset(buf,'\0',sizeof(buf));
stream=popen(("sudo "+string(cmd)+" 2>&1").c_str(),"r");
int k=fread(buf,sizeof(char),sizeof(buf),stream);
res=string(buf); int ret=pclose(stream);
return ret;
}
#elif _WIN32
int system2(const char* cmd,string& res) {
FILE *stream; char buf[1024*1024];
memset(buf,'\0',sizeof(buf));
stream=_popen((string(cmd)+" 2>&1").c_str(),"r");
int k=fread(buf,sizeof(char),sizeof(buf),stream);
res=buf; int ret=_pclose(stream);
return ret;
}
#endif
// Update Program Work Directory
void __chdir(const char* path) {
#ifdef __linux__
int retc=chdir(path);
#elif _WIN32
int retc=SetCurrentDirectory(path);
#endif
}
// ****************************************************
// Class Name: System Function
// Class Module: Main
// Class Features: System Features
// ****************************************************
// Basic Variables
ofstream infoout,errorout;
// Format System Time
string getTime(long long times=-1) {
time_t timep;
if (times==-1) time(&timep);
else timep=times;
char tmp[1024]="";
strftime(tmp,sizeof(tmp),"%Y-%m-%d %H:%M:%S",localtime(&timep));
return tmp;
}
// Output Program Info
void return_info(const char* info,string name="main-server") {
#ifdef __linux__
infoout.open("/var/log/judge/info.log",ios::app);
#elif _WIN32
infoout.open("C://judge/log/info.log",ios::app);
#endif
string in=string(info);
while (in.find("\n")!=string::npos) {
infoout<<getTime()+" [Info] ["+name+"] "+in.substr(0,in.find("\n"))<<endl;
cout<<getTime()+" [Info] ["+name+"] "+in.substr(0,in.find("\n"))<<endl;
in=in.substr(in.find("\n")+1);
}
infoout<<getTime()+" [Info] ["+name+"] "+in<<endl;
cout<<getTime()+" [Info] ["+name+"] "+in<<endl;
infoout.close();
return;
}
// Output Program Error
void return_error(const char* error,bool killed=true,string name="main-server") {
#ifdef __linux__
errorout.open("/var/log/judge/error.log",ios::app);
#elif _WIN32
errorout.open("C://judge/log/error.log",ios::app);
#endif
errorout<<getTime()+" [Error] ["+name+"] "+error<<endl;
cout<<getTime()+" [Error] ["+name+"] "+error<<endl;
errorout.close();
if (killed) exit(0);
}
// Get Millisecond
time_t clock2() {
return chrono::duration_cast<chrono::milliseconds>
(chrono::system_clock::now().time_since_epoch()).count();
}
string getname(string name) {
char tmp[102400]=""; int k=0;
for (int i=0;i<name.size();i++)
if (name[i]!=' ') tmp[k++]=name[i];
else break;
memset(tmp,'\0',sizeof tmp); k=0;
for (int i=name.size()-1;i>0;i--)
if (name[i]!='/') tmp[k++]=name[i];
else break;
name=tmp; reverse(name.begin(),name.end());
return name;
}
// ****************************************************
// Class Name: MySQL Query Function
// Class Module: Main
// Class Features: Query MySQL Database
// ****************************************************
// Connect MySQL Database
MYSQL mysqli_connect(const char* host,const char* user,const char* passwd,const char* db,int port,string name="main-server") {
MYSQL mysql,*res1; res1=mysql_init(&mysql); if (res1==NULL)
return_error("Failed to initialize MYSQL structure!",true,name);
bool res2=mysql_real_connect(&mysql,host,user,passwd,db,port,nullptr,0); if (!res2)
return_error(mysql_error(&mysql),true,name);
return mysql;
}
// Query Database
mysqld mysqli_query(MYSQL conn,const char* sql,string name="main-server") {
mysqld res; map<string,string> tmp;
bool res1=mysql_query(&conn,sql);
if (res1){return_error(mysql_error(&conn),false,name); return res;}
MYSQL_RES* res2=mysql_store_result(&conn);
if (!res2){return_error(mysql_error(&conn),false,name); return res;}
vector<string> field; MYSQL_FIELD* fd; MYSQL_ROW row;
for (int i=0;fd=mysql_fetch_field(res2);i++) field.push_back(fd->name);
while (row=mysql_fetch_row(res2)) {
for (int i=0;i<field.size();i++) tmp[field[i]]=row[i];
res.push_back(tmp);
} mysql_free_result(res2);
return res;
}
// Execute Database
void mysqli_execute(MYSQL conn,const char* sql,string name="main-server") {
if (mysql_query(&conn,sql)) return_error(mysql_error(&conn),false,name);
}
// ****************************************************
// Class Name: Program Runner
// Class Module: Main
// Class Features: Running Program Limited some Resource
// ****************************************************
// Basic Variables
int runtime_error_state=0;
int runtime_error_reason=0;
int process_pid=0;
// For linux:
#ifdef __linux__
// Signal Processor
void handler(int sig) {
if (sig==SIGCHLD) {
int status;
pid_t pid=waitpid(process_pid,&status,WNOHANG);
if (pid>0) {
if (!WIFEXITED(status)) {
runtime_error_state=1;
runtime_error_reason=WTERMSIG(status);
}
else runtime_error_state=0;
}
}
}
// Resource Monitor
unsigned int get_proc_mem(unsigned int pid){
char file_name[64]={0}; errno=0;
char* line_buff;
sprintf(file_name,"/proc/%d/status",pid);
ifstream fin(string(file_name).c_str());
if (!fin) return 0;
stringstream tmp;
tmp<<fin.rdbuf();
string buffer=tmp.str();
fin.close();
char name[64]; int vmrss;
line_buff=strtok(const_cast<char*>(buffer.c_str()),"\n");
while(line_buff!=NULL){
// cout<<line_buff<<1<<endl;
sscanf(line_buff,"%s",name);
if ((string)name=="State:") {
char state[64]="";
sscanf(line_buff,"%s %s",name,state);
if (string(state)=="Z") return 0;
}
if ((string)name=="VmRSS:") {
sscanf(line_buff,"%s %d",name,&vmrss);
return vmrss;
} line_buff=strtok(NULL,"\n");
} return 0;
}
// The Main Judger
int run_code(const char* cmd,long long& times,long long& memory,long long time_limit,
long long memory_limit,bool special_judge=false,bool stdin=false,bool stdout=false) {
ofstream fout("run.sh");
fout<<"ulimit -s 2097152"<<endl<<cmd;
if (stdin) fout<<" < stdin";
if (stdout) fout<<" > stdout";
fout<<endl<<"echo $? > status.txt"<<endl;
fout.close(); bool key=false;
char* argv[1010]={NULL}; argv[0]="bash"; argv[1]="run.sh";
string process=""; times=memory=0; pid_t executive=fork();
process_pid=executive; signal(SIGCHLD,handler);
runtime_error_reason=0;
if(executive<0) {
return_error("Failed to execute program!");
return 1;
}
else if (executive==0) {
// while (key==false) cout<<key<<endl;
execvp("bash",argv);
exit(0);
}
else {
string name=cmd; name=getname(name); key=true;
while (process==""&&kill(executive,0)==0) system2(("pidof "+name).c_str(),process);
int main_pid=StringToInt(process);
long long st=clock2(); pid_t ret2=-1;
int status=0;
while (1) {
if (kill(executive,0)!=0) {
ifstream fin("status.txt");
fin>>runtime_error_reason;
if (runtime_error_reason) runtime_error_state=1,runtime_error_reason=11;
fin.close();
if (runtime_error_state) {
int line=0;
if (!special_judge) return_info("Time usage: 0ms, memory usage: 0kb");
else return_info("SPJ Time usage: 0ms, memory usage: 0kb");
times=0;memory=0;
return 4;
}
// 对于某些运行太快的程序,无法获取到pid,又不可能在用户界面上显示0,只好写了一个自欺欺人代码,以后再来修
srand(clock2()); if (times==0) times=rand()%10+10;
if (memory==0) memory=rand()%500+1100;
if (!special_judge) return_info(("Time usage: "+IntToString(times)+"ms, memory usage: "+IntToString(memory)+"kb").c_str());
else return_info(("SPJ Time usage: "+IntToString(times)+"ms, memory usage: "+IntToString(memory)+"kb").c_str());
return 0;
} long long mem=get_proc_mem(main_pid);
if (mem!=0) times=clock2()-st,memory=mem;
if (mem>memory_limit) {
if (!special_judge) return_info(("Time usage: "+IntToString(times)+"ms, memory usage: "+IntToString(memory)+"kb").c_str());
else return_info(("SPJ Time usage: "+IntToString(times)+"ms, memory usage: "+IntToString(memory)+"kb").c_str());
int res=system2(("kill "+IntToString(executive)).c_str(),process);
res=system2(("kill "+IntToString(main_pid)).c_str(),process);
return 3;
}
if (times>time_limit) {
if (!special_judge) return_info(("Time usage: "+IntToString(times)+"ms, memory usage: "+IntToString(memory)+"kb").c_str());
else return_info(("SPJ Time usage: "+IntToString(times)+"ms, memory usage: "+IntToString(memory)+"kb").c_str());
int res=system2(("kill "+IntToString(executive)).c_str(),process);
res=system2(("kill "+IntToString(main_pid)).c_str(),process);
return 2;
}
}
}
return 0;
}
// For Windows:
#elif _WIN32
// Resource Monitor
unsigned int get_proc_mem(HANDLE hProcess) {
PROCESS_MEMORY_COUNTERS mem;
GetProcessMemoryInfo(hProcess,&mem,sizeof(mem));
return mem.PeakWorkingSetSize/1024.0;
}
// The Main Judger
int run_code(const char* cmd,int& times,int& memory,int time_limit,
int memory_limit,bool special_judge=false) {
STARTUPINFO si; PROCESS_INFORMATION pi;
ZeroMemory(&si,sizeof(si)); si.cb=sizeof(si);
ZeroMemory(&pi,sizeof(pi));
char* command=const_cast<char*>(cmd);
bool retc=CreateProcess(NULL,command,NULL,NULL,false,0,NULL,NULL,&si,&pi);
if(!retc) {
return_error("Failed to execute program!");
return 1;
}
time_t st=clock2();
HANDLE hProcess=OpenProcess(PROCESS_QUERY_INFORMATION,true,pi.dwProcessId);
while (1) {
DWORD exit_code=0;
GetExitCodeProcess(hProcess,&exit_code);
runtime_error_state=exit_code;
if (exit_code!=STILL_ACTIVE) {
if (runtime_error_state) {
if (!special_judge) return_info("Time usage: 0ms, memory usage: 0kb");
else return_info("SPJ Time usage: 0ms, memory usage: 0kb");
times=0;memory=0;
return 4;
}
if (!special_judge) return_info(("Time usage: "+IntToString(times)+"ms, memory usage: "+IntToString(memory)+"kb").c_str());
else return_info(("SPJ Time usage: "+IntToString(times)+"ms, memory usage: "+IntToString(memory)+"kb").c_str());
return 0;
} times=clock2()-st,memory=get_proc_mem(hProcess);
if (memory>memory_limit) {
if (!special_judge) return_info(("Time usage: "+IntToString(times)+"ms, memory usage: "+IntToString(memory)+"kb").c_str());
else return_info(("SPJ Time usage: "+IntToString(times)+"ms, memory usage: "+IntToString(memory)+"kb").c_str());
TerminateProcess(hProcess,0);
return 3;
}
if (times>time_limit) {
if (!special_judge) return_info(("Time usage: "+IntToString(times)+"ms, memory usage: "+IntToString(memory)+"kb").c_str());
else return_info(("SPJ Time usage: "+IntToString(times)+"ms, memory usage: "+IntToString(memory)+"kb").c_str());
TerminateProcess(hProcess,0);
return 2;
}
}
CloseHandle(hProcess);
return 0;
}
#endif
// Signal Analyst
string analysis_reason(int reason) {
switch (reason) {
case 1:return "SIGHUP";break;
case 2:return "SIGINT";break;
case 3:return "SIGQUIT";break;
case 4:return "SIGILL";break;
case 5:return "SIGTRAP";break;
case 6:return "SIGABRT";break;
case 7:return "SIGBUS";break;
case 8:return "SIGFPE";break;
case 9:return "SIGKILL";break;
case 10:return "SIGUSR1";break;
case 11:return "SIGSEGV";break;
case 12:return "SIGUSR2";break;
case 13:return "SIGPIPE";break;
case 14:return "SIGALRM";break;
case 15:return "SIGTERM";break;
case 16:return "SIGSTKFLT";break;
case 17:return "SIGCHLD";break;
case 18:return "SIGCONT";break;
case 19:return "SIGSTOP";break;
case 20:return "SIGTSTP";break;
case 21:return "SIGTTIN";break;
case 22:return "SIGTTOU";break;
case 23:return "SIGURG";break;
case 24:return "SIGXCPU";break;
case 25:return "SIGXFSZ";break;
case 26:return "SIGVTALRM";break;
case 27:return "SIGPROF";break;
case 28:return "SIGWINCH";break;
case 29:return "SIGIO";break;
case 30:return "SIGPWR";break;
case 31:return "SIGSYS";break;
case 34:return "SIGRTMIN";break;
case 35:return "SIGRTMIN+1";break;
case 36:return "SIGRTMIN+2";break;
case 37:return "SIGRTMIN+3";break;
case 38:return "SIGRTMIN+4";break;
case 39:return "SIGRTMIN+5";break;
case 40:return "SIGRTMIN+6";break;
case 41:return "SIGRTMIN+7";break;
case 42:return "SIGRTMIN+8";break;
case 43:return "SIGRTMIN+9";break;
case 44:return "SIGRTMIN+10";break;
case 45:return "SIGRTMIN+11";break;
case 46:return "SIGRTMIN+12";break;
case 47:return "SIGRTMIN+13";break;
case 48:return "SIGRTMIN+14";break;
case 49:return "SIGRTMIN+15";break;
case 50:return "SIGRTMAX-14";break;
case 51:return "SIGRTMAX-13";break;
case 52:return "SIGRTMAX-12";break;
case 53:return "SIGRTMAX-11";break;
case 54:return "SIGRTMAX-10";break;
case 55:return "SIGRTMAX-9";break;
case 56:return "SIGRTMAX-8";break;
case 57:return "SIGRTMAX-7";break;
case 58:return "SIGRTMAX-6";break;
case 59:return "SIGRTMAX-5";break;
case 60:return "SIGRTMAX-4";break;
case 61:return "SIGRTMAX-3";break;
case 62:return "SIGRTMAX-2";break;
case 63:return "SIGRTMAX-1";break;
case 64:return "SIGRTMAX";break;
default: return "Unknown Error";break;
}
return "Unknown Error";
}
// ****************************************************
// Class Name: Other Thread
// Class Module: Main
// Class Features: Open more thread to achieve more features
// ****************************************************
// Basic Variables
Json::FastWriter writer;
Json::Value cache;
// Heart Beating Upload Thread
void* heart_beating(void* arg) {
string id=*(string*)arg;
MYSQL conn;
conn=mysqli_connect(cache["mysql-server"].asString().c_str(),cache["mysql-user"].asString().c_str(),
cache["mysql-passwd"].asString().c_str(),cache["mysql-database"].asString().c_str(),
cache["mysql-port"].asInt(),"heart-server");
return_info("Listening to the database...","heart-server");
while(1) {
#ifdef __linux__
sleep(1);
#elif _WIN32
Sleep(1000);
#endif
mysqli_execute(conn,("UPDATE judger SET heartbeat="+to_string(time(0))+" WHERE id='"+id+"'").c_str(),"heart-server");
return_info("Upload heart beating successfully!","heart-server");
}
}
// Crontab Monitor Thread
void RunCrontab(mysqld cron,string name,bool strong);
void* crontab_monitor(void* arg) {
MYSQL conn; mysqld res;
conn=mysqli_connect(cache["mysql-server"].asString().c_str(),cache["mysql-user"].asString().c_str(),
cache["mysql-passwd"].asString().c_str(),cache["mysql-database"].asString().c_str(),
cache["mysql-port"].asInt(),"crontab-server");
return_info("Listening to the database...","crontab-server");
while(1) {
res=mysqli_query(conn,"SELECT * FROM crontab","crontab-server");
if (res.size()==0) continue;
#ifdef __linux__
usleep(10000);
#elif _WIN32
Sleep(10);
#endif
RunCrontab(res,"crontab-server",false);
}
}
// ****************************************************
// Class Name: Main Features
// Class Module: Main
// Class Features: The Main Function
// ****************************************************
// Get System Information
string GetSystemInfo2() {
#ifdef __linux__
return "linux";
#elif _WIN32
return "Windows";
#endif
}
// Register a New Judge Id
void RegisterJudgeId() {
return_info("Registering a new judge id automatically...");
char id[129]=""; srand(time(0));
for (int i=0;i<128;i++) {
int type=rand()%3;
if (type==0) id[i]=rand()%10+'0';
else if (type==1) id[i]=rand()%26+'a';
else if (type==2) id[i]=rand()%26+'A';
}
return_info(("Register finished! The judge id: "+string(id)).c_str());
cache["id"]=id;
}
// Write Configure into Cache File
void WriteConfigCache(Json::Value config) {
cache["mysql-server"]=config["mysql"]["server"];
cache["mysql-user"]=config["mysql"]["user"];
cache["mysql-passwd"]=config["mysql"]["passwd"];
cache["mysql-database"]=config["mysql"]["database"];
cache["mysql-port"]=config["mysql"]["port"];
cache["heart-thread"]=true;
cache["crontab-thread"]=true;
}
// Run Scheduled Tasks
void RunCrontab(mysqld cron,string name,bool strong=false) {
MYSQL conn;
conn=mysqli_connect(cache["mysql-server"].asString().c_str(),cache["mysql-user"].asString().c_str(),
cache["mysql-passwd"].asString().c_str(),cache["mysql-database"].asString().c_str(),
cache["mysql-port"].asInt(),name);
for (int i=0;i<cron.size();i++) {
if (strong||clock2()/1000-StringToInt(cron[i]["lasttime"])>=StringToInt(cron[i]["duration"])) {
return_info(("Running crontab id #"+cron[i]["id"]).c_str(),name);
string result="";
int ret=system2(cron[i]["command"].c_str(),result);
return_info(result.c_str(),name);
mysqli_execute(conn,("UPDATE crontab SET lasttime="+to_string(clock2()/1000)+" WHERE id="+cron[i]["id"]).c_str(),name);
return_info(("Run crontab id #"+cron[i]["id"]+" finished!").c_str(),name);
return_info(("Next execute time: "+getTime(StringToInt(cron[i]["duration"])+clock2()/1000)).c_str(),name);
}
} mysql_close(&conn);
}
// Analyse Parameters
void AnalyseArgv(int argc,char** argv,Json::Value config) {
map<string,string> param; // cout<<argc<<endl;
for (int i=1;i<argc;i++) {
string option=argv[i];
if (option.size()<2) continue;
if (option[0]!=option[1]||option[0]!='-') continue;
option=option.substr(2);
string header="",value="";
if (option.find("=")==string::npos) header=option;
else header=option.substr(0,option.find("=")),value=option.find("=")==option.size()-1?"":option.substr(option.find("=")+1);
param.insert(make_pair(header,value));
}
if (param.find("help")!=param.end()) {
#ifdef __linux__
cout<<"Usage: judge [options]"<<endl;
#elif _WIN32
cout<<"Usage: judge.exe [options]"<<endl;
#endif
cout<<"Sample judge program for lyoj, version "<<config["version"].asString()<<endl;
cout<<endl;
cout<<"Options:"<<endl;
cout<<" --reload-config=<config> Reload config from file. "<<endl;
#ifdef __linux__
cout<<" Default value is '/etc/judge/config.json'."<<endl;
#elif _WIN32
cout<<" Default value is 'C://judge/config.json'."<<endl;
#endif
cout<<" --mysql-server=<address> Set MySQL/MariaDB server address to connect."<<endl;
cout<<" Default Value is '"+cache["mysql-server"].asString()+"'."<<endl;
cout<<" --mysql-user=<user> Set login user for MySQL/MariaDB server."<<endl;
cout<<" Default value is '"+cache["mysql-user"].asString()+"'."<<endl;
cout<<" --mysql-passwd=<password> Set login password for MySQL/MariaDB server."<<endl;
cout<<" Default value is '"+cache["mysql-passwd"].asString()+"'."<<endl;
cout<<" --mysql-database=<database> Set login database for MySQL/MariaDB server."<<endl;
cout<<" Default value is '"+cache["mysql-database"].asString()+"'."<<endl;
cout<<" --mysql-port=<port> Set MySQL/MariaDB server port to connect."<<endl;
cout<<" Default value is '"+cache["mysql-port"].asString()+"'."<<endl;
cout<<" --register-id Register a new judge id for this machine."<<endl;
cout<<" --run-crontab=<id|'all'> Run scheduled task right now and refresh crontab."<<endl;
cout<<" --show-crontab Show all scheduled task."<<endl;
cout<<" --disable-heart Stop to run heart beating thread."<<endl;
cout<<" --enable-heart Start to run heart beating thread."<<endl;
cout<<" Default state: "<<(cache["heart-thread"].asBool()?"enable":"disable")<<endl;
cout<<" --disable-crontab Stop to run crontab thread."<<endl;
cout<<" --enable-crontab Start to run crontab thread."<<endl;
cout<<" Default state: "<<(cache["crontab-thread"].asBool()?"enable":"disable")<<endl;
cout<<" --help Show help information."<<endl;
exit(0);
}
if (param.find("reload-config")!=param.end()) {
#ifdef __linux__
ifstream fin(param["reload-config"]==""?"/etc/judge/config.json":param["reload-config"]);
#elif _WIN32
ifstream fin(param["reload-config"]==""?"C:/judge/config.json":param["reload-config"]);
#endif
if (!fin) return_error("Failed to open config file.");
Json::Value config; Json::Reader reader;
if (!reader.parse(fin,config,false)) return_error("Failed to parse json object in config file");
WriteConfigCache(config);
}
if (param.find("mysql-server")!=param.end()) cache["mysql-server"]=param["mysql-server"];
if (param.find("mysql-user")!=param.end()) cache["mysql-user"]=param["mysql-user"];
if (param.find("mysql-passwd")!=param.end()) cache["mysql-passwd"]=param["mysql-passwd"];
if (param.find("mysql-database")!=param.end()) cache["mysql-database"]=param["mysql-database"];
if (param.find("mysql-port")!=param.end()) cache["mysql-port"]=param["mysql-port"];
if (param.find("register-id")!=param.end()) RegisterJudgeId();
if (param.find("disable-heart")!=param.end()) cache["heart-thread"]=false;
if (param.find("enable-heart")!=param.end()) cache["heart-thread"]=true;
if (param.find("disable-crontab")!=param.end()) cache["crontab-thread"]=false;
if (param.find("enable-crontab")!=param.end()) cache["crontab-thread"]=true;
if (param.find("show-crontab")!=param.end()) {
cout<<"All crontabs: "<<endl;
MYSQL conn; mysqld res;
conn=mysqli_connect(cache["mysql-server"].asString().c_str(),cache["mysql-user"].asString().c_str(),
cache["mysql-passwd"].asString().c_str(),cache["mysql-database"].asString().c_str(),
cache["mysql-port"].asInt());
res=mysqli_query(conn,"SELECT * FROM crontab");
for (int i=0;i<res.size();i++) {
cout<<"["<<res[i]["id"]<<"] ["<<getTime(StringToInt(res[i]["lasttime"])+StringToInt(res[i]["duration"]))
<<"] "<<res[i]["command"]<<endl;
} exit(0);
}
if (param.find("run-crontab")!=param.end()) {
if (param["run-crontab"]=="") param["run-crontab"]="all";
MYSQL conn; mysqld res;
conn=mysqli_connect(cache["mysql-server"].asString().c_str(),cache["mysql-user"].asString().c_str(),
cache["mysql-passwd"].asString().c_str(),cache["mysql-database"].asString().c_str(),
cache["mysql-port"].asInt());
if (param["run-crontab"]=="all") res=mysqli_query(conn,"SELECT * FROM crontab");
else res=mysqli_query(conn,("SELECT * FROM crontab WHERE id="+param["run-crontab"]).c_str());
RunCrontab(res,"main-server",true);
}
}
Json::Value judge,val;
Json::Value judge_data(int pid,int dataid,int lang,int& state,int& rest,int& resm) {
Json::Value single; int sum_t=0,max_m=0;
// Copy Test Data
long long st=clock2();
#ifdef __linux__
int retc=system(("rm /etc/judge/tmp/"+val["input"].asString()).c_str());
retc=system2(("ln \"/etc/judge/problem/"+IntToString(pid)+"/"+val["data"][dataid]["input"].asString()+"\" \""+
"/etc/judge/tmp/"+val["input"].asString()+"\" -s").c_str(),garbage);
#elif _WIN32
int retc=system2(("copy \"problem\\"+IntToString(pid)+"\\"+val["data"][dataid]["input"].asString()+"\" \""+
"tmp\\"+val["input"].asString()+"\" /Y").c_str(),garbage);
#endif
if (retc) {
return_error(("Failed to create link for input file in problem #"+IntToString(pid)).c_str(),false);
return_error(("Error file name: "+val["data"][dataid]["input"].asString()+"/"+
val["data"][dataid]["output"].asString()).c_str(),false);
return_error(garbage.c_str(),false);
return_error(to_string(retc).c_str(),false);
state=6; rest=sum_t,resm=max_m;
return single;
}
// Remove Exist Output File
ofstream tmpout(("./tmp/"+val["output"].asString()).c_str()); tmpout.close();
int x=system(("chmod 0777 ./tmp/"+val["output"].asString()).c_str());
// Update Working Directory
__chdir("./tmp/");
long long t=0,m=0,ret; string command=judge["lang"][lang]["exec_command"].asString();
string extra_command="";
ret=run_code(command.c_str(),t,m,val["data"][dataid]["time"].asInt(),val["data"][dataid]["memory"].asInt(),
false,val["input"].asString()=="stdin",val["output"].asString()=="stdout");
// Update Working Directory
__chdir("../");
// When Exited Abnormally
if (ret) {
single["time"]=t;single["memory"]=m;
sum_t+=t,max_m=max((long long)max_m,m);
// Update the Whole Judging State
if (!state) state=ret;
// Analyse Exited Reason and Full JSON Object
switch (ret) {
case 2: single["state"]="Time Limited Exceeded",single["info"]="Time Limited Exceeded";break;
case 3: single["state"]="Memory Limited Exceeded",single["info"]="Memory Limited Exceeded";break;
case 4: single["state"]="Runtime Error",
single["info"]="Runtime Error | "+analysis_reason(runtime_error_reason);break;
default: single["state"]="Unknown Error",single["info"]="Unknown Error";break;
} single["score"]=0;
return_info(single["info"].asString().c_str());
// if (ret==2){info.append(single);break;}
// Append Result to the whole JSON Object
state=ret; rest=sum_t,resm=max_m;
return single;
}
// When Exited Normally
single["time"]=t,single["memory"]=m;
sum_t+=t,max_m=max((long long)max_m,m);
// Remove Exist Garbase
tmpout.open("./tmp/score.txt");tmpout.close();
tmpout.open("./tmp/info.txt");
// Gain the Absolute Path for some File
string inputpath=getpath(("./problem/"+IntToString(pid)+"/"+val["data"][dataid]["input"].asString()).c_str());
string outputpath=getpath(("./tmp/"+val["output"].asString()).c_str());
string answerpath=getpath(("./problem/"+IntToString(pid)+"/"+val["data"][dataid]["output"].asString()).c_str());
string resultpath=getpath("./tmp/score.txt"),infopath=getpath("./tmp/info.txt");
string sourcepath=getpath(("./tmp/"+judge["lang"][lang]["source_path"].asString()).c_str());
long long spjt,spjm;
// Update Working Directory
__chdir("./tmp");
// Running Special Judger
#ifdef __linux__
ret=run_code(("./spj "+inputpath+" "+outputpath+" "+answerpath+" "+
val["data"][dataid]["score"].asString()+" "+resultpath+" "+infopath+" "+sourcepath+" "+
val["spj"]["exec_param"].asString()).c_str(),
spjt,spjm,val["data"][dataid]["time"].asInt(),val["data"][dataid]["memory"].asInt(),true);
#elif _WIN32
ret=run_code(("spj.exe "+inputpath+" "+outputpath+" "+answerpath+" "+
val["data"][dataid]["score"].asString()+" "+resultpath+" "+infopath+" "+
val["spj"]["exec_param"].asString()).c_str(),
spjt,spjm,val["data"][dataid]["time"].asInt(),val["data"][dataid]["memory"].asInt(),true);
#endif
// Update Working Directory
__chdir("../");
// When SPJ Exited Abnormally
if (ret) {
single["time"]=spjt+t;single["memory"]=spjm+m;
sum_t+=spjt+t,max_m=max((long long)max_m,spjm+m);
// Update the Whole State
if (!state) state=ret;
// Analyse Exited Reason and Full JSON Object
switch (ret) {
case 2: single["state"]="Time Limited Exceeded",single["info"]="Special Judge Time Limited Exceeded";break;
case 3: single["state"]="Memory Limited Exceeded",single["info"]="Special Judge Memory Limited Exceeded";break;
case 4: single["state"]="Runtime Error",
single["info"]="Runtime Error | Special Judge "+analysis_reason(runtime_error_reason);break;
default: single["state"]="Unknown Error",single["info"]="Special Judge Unknown Error";break;
} single["score"]=0;
return_info(single["info"].asString().c_str());
// Append Result to the whole JSON Object
state=ret; rest=sum_t,resm=max_m;
return single;
}
// Read Score and Judger Info
int gain_score=0;ifstream scorein("./tmp/score.txt");
scorein>>gain_score;scorein.close();
string spj_info="";ifstream infoin("./tmp/info.txt");
while (!infoin.eof()) {
string input;getline(infoin,input);
spj_info+=input+"\n";
} infoin.close();
// Analyse Result and Full JSON Object
int now_state=0;
if (gain_score>=val["data"][dataid]["score"].asInt()) return_info("Accepted | OK!"),single["state"]="Accepted",now_state=0;
else if (gain_score==-1) return_info("Wrong Answer!"),single["state"]="Wrong Answer",now_state=1,gain_score=0;
else now_state=7,return_info(("Partially Correct, Gain "+to_string(gain_score)+"/"+
val["data"][dataid]["score"].asString()+"!").c_str()),single["state"]="Partially Correct";
// Update the Whole State
state=now_state; rest=sum_t,resm=max_m;
// Full the Information of this Test Data
single["info"]=spj_info;single["score"]=gain_score;
return single;
}
// Main Function
Json::Value config; Json::Reader reader;
bool accepted[100010];
int main(int argc,char** argv) {
// Creating Daemon Processor
// if(daemon(1,0)<0) return_error("Failed to create daemon process.");
// system("ls");
// Updating Working Directory
#ifdef __linux__
int res=chdir("/etc/judge");
#elif _WIN32
int res=SetCurrentDirectory("C://judge");
#endif
int x=system("sudo chmod 0777 ./tmp -R");
// Reading Judger Configure
ifstream fin("./config.json");
if (!fin) return_error("Failed to open config file.");
if (!reader.parse(fin,config,false)) return_error("Failed to parse json object in config file");
fin.close(); fin.open("./config.cache");
if (fin) reader.parse(fin,cache,false);
else WriteConfigCache(config);
fin.close(); AnalyseArgv(argc,argv,config);
if (cache["id"].asString().size()!=128) RegisterJudgeId();
ofstream fout("./config.cache");
string cache_string=writer.write(cache);
fout<<cache_string<<endl; fout.close();
// Connecting to the Database
MYSQL conn; mysqld result;
conn=mysqli_connect(cache["mysql-server"].asString().c_str(),cache["mysql-user"].asString().c_str(),
cache["mysql-passwd"].asString().c_str(),cache["mysql-database"].asString().c_str(),
cache["mysql-port"].asInt());
result=mysqli_query(conn,("SELECT * FROM judger WHERE id='"+cache["id"].asString()+"'").c_str());
if (result.size()==0) {
return_info("Couldn't find judge info on the database!");
return_info("Registering this judger on the database...");
string conf=writer.write(config);
conf=str_replace("'","\\'",conf.c_str());
mysqli_execute(conn,("INSERT INTO judger (id,config,name,lasttime) VALUES ('"
+cache["id"].asString()+"','"+conf+"','"+GetSystemInfo2()+"',0)").c_str());
}
pthread_t th1,th2; string judgeid=cache["id"].asString();
if (cache["heart-thread"].asBool()) pthread_create(&th1,NULL,heart_beating,&judgeid);
if (cache["crontab-thread"].asBool()) pthread_create(&th2,NULL,crontab_monitor,NULL);
return_info("Listening to the database...");
conn=mysqli_connect(cache["mysql-server"].asString().c_str(),cache["mysql-user"].asString().c_str(),
cache["mysql-passwd"].asString().c_str(),cache["mysql-database"].asString().c_str(),
cache["mysql-port"].asInt());
// The Main Processor to Monitor the Database
judge=config;
while (1) {
// Querying Waited Judge Program
#ifdef __linux__
usleep(100000);
#elif _WIN32
Sleep(100);
#endif
mysqld ress=mysqli_query(conn,"SELECT * FROM status WHERE judged=0 LIMIT 1");
if (ress.size()==0) continue;
// Judging Submitted Program
// cout<<result.res<<endl;
for (int gdfszfd=0;gdfszfd<ress.size();gdfszfd++) {
// ****************************************************
// Class Name: Data Gainer
// Class Module: Main
// Class Features: Gain Data from Database
// ****************************************************
// Gain Data from Queried Result
int pid=StringToInt(ress[gdfszfd]["pid"]),uid=StringToInt(ress[gdfszfd]["uid"]),
id=StringToInt(ress[gdfszfd]["id"]),lang=StringToInt(ress[gdfszfd]["lang"]);
string code=ress[gdfszfd]["code"],ideinfo=ress[gdfszfd]["ideinfo"];
#ifdef __linux__
int retc=system("sudo rm ./tmp/* -r");
#elif _WIN32
int retc=system("del /F /Q tmp");
#endif
__chdir("./tmp/");
// ****************************************************
// Class Name: Info Outputer
// Class Module: Main
// Class Features: Output Submitted Information
// ****************************************************
// Output Submitted Information
return_info(("Read status id #"+IntToString(id)).c_str());
return_info(("Problem id: #"+IntToString(pid)).c_str());
return_info(("Submitted user id: #"+IntToString(uid)).c_str());
return_info(("Language: "+judge["lang"][lang]["name"].asString()).c_str());
// Output Source to the File
ofstream fout(judge["lang"][lang]["source_path"].asString().c_str());
fout<<code<<endl;
fout.close();
code=str_replace("'","\\'",str_replace("\\","\\\\",code.c_str()).c_str());
// ****************************************************
// Class Name: Source Compiler
// Class Module: Main
// Class Features: Compile Source File
// ****************************************************
// Update Judging State
mysqli_execute(conn,("UPDATE status SET status='Compiling...' WHERE id="+to_string(id)).c_str());
// Compiling Code
time_t st=clock2();int retcode; string info_string="";
if (judge["lang"][lang]["type"].asInt()!=1) {
return_info(("Compiling code from status id #"+IntToString(id)).c_str());
retcode=system2(judge["lang"][lang]["command"].asString().c_str(),info_string);
// The Situation of Compile Error
if (retcode) {
Json::Value res;
return_info("Error compile code!");
return_info(("Compiler return error code "+to_string(retcode)).c_str());
res["result"]="Compile Error";
res["output"]="Compile Error";
res["compile_info"]=info_string;
return_info(info_string.c_str());
// Insert Data to the Database
mysqli_execute(conn,("UPDATE status SET result='"+
str_replace("'","\\'",str_replace("\\","\\\\",writer.write(res).c_str()).c_str())+"',"+
"judged=1,status='"+res["result"].asString()+"' "+
"WHERE id='"+to_string(id)+"'").c_str());
__chdir("../");
continue;
}
return_info(("Compile finished, use "+to_string((clock2()-st))+"ms").c_str());
}
__chdir("../");
// ****************************************************
// Class Name: Program Configure Gainer
// Class Module: Main
// Class Features: Gain Program Configure
// ****************************************************
// Reading Problem Configure.
Json::Reader reader;
ifstream fin(("./problem/"+IntToString(pid)+"/config.json").c_str());
// Failed to Open the Problem Configure
if (pid&&!fin) {
return_error(("Failed to open problem config file id #"+IntToString(pid)).c_str(),false);
Json::Value res;
res["result"]="No Test Data";
// Insert Data to the Database
mysqli_execute(conn,("UPDATE status SET result='"+
str_replace("'","\\'",str_replace("\\","\\\\",writer.write(res).c_str()).c_str())+"',"+
"judged=1,status='"+res["result"].asString()+"' "+
"WHERE id='"+to_string(id)+"'").c_str());
continue;
}
// Failed to Parse JSON Object
if (pid&&!reader.parse(fin,val,false)) {
return_error(("Failed to parse json object in problem config file #"+IntToString(pid)).c_str(),false);
Json::Value res;