forked from blackav/ejudge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.c
1992 lines (1807 loc) · 66 KB
/
compile.c
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
/* -*- c -*- */
/* Copyright (C) 2000-2017 Alexander Chernov <[email protected]> */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
/*
* This program compiles incoming source files and puts the resulting
* executables into the spool directory.
*
* Note: this program must compile and work on win32
*/
#include "ejudge/config.h"
#include "ejudge/prepare.h"
#include "ejudge/pathutl.h"
#include "ejudge/errlog.h"
#include "ejudge/parsecfg.h"
#include "ejudge/fileutl.h"
#include "ejudge/interrupt.h"
#include "ejudge/runlog.h"
#include "ejudge/compile_packet.h"
#include "ejudge/curtime.h"
#include "ejudge/serve_state.h"
#include "ejudge/startstop.h"
#include "ejudge/ejudge_cfg.h"
#include "ejudge/compat.h"
#include "ejudge/ej_uuid.h"
#include "ejudge/ej_libzip.h"
#include "ejudge/testinfo.h"
#include "ejudge/misctext.h"
#include "ejudge/random.h"
#include "ejudge/ej_process.h"
#include "ejudge/meta_generic.h"
#include "ejudge/meta/compile_packet_meta.h"
#include "ejudge/meta/prepare_meta.h"
#include "ejudge/xalloc.h"
#include "ejudge/logger.h"
#include "ejudge/osdeps.h"
#include "ejudge/exec.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
enum { MAX_LOG_SIZE = 1024 * 1024, MAX_EXE_SIZE = 128 * 1024 * 1024 };
struct serve_state serve_state;
static int initialize_mode = 0;
static int daemon_mode;
static int restart_mode;
static int
check_style_only(
const struct section_global_data *global,
struct compile_request_packet *req,
struct compile_reply_packet *rpl,
const unsigned char *pkt_name,
const unsigned char *run_name,
const unsigned char *work_run_name,
const unsigned char *report_dir,
const unsigned char *status_dir,
const unsigned char *full_working_dir)
{
void *reply_bin = 0;
size_t reply_bin_size = 0;
unsigned char msgbuf[1024] = { 0 };
path_t log_path;
path_t txt_path;
path_t work_src_path;
path_t work_log_path;
int r, i;
const unsigned char *src_sfx = "";
tpTask tsk = 0;
// input file: ${global->compile_src_dir}/${pkt_name}${req->src_sfx}
// output log file: ${report_dir}/${run_name}
// file listing: ${report_dir}/${run_name} (if OK status)
// working directory: ${global->compile_work_dir}
snprintf(log_path, sizeof(log_path), "%s/%s", report_dir, run_name);
snprintf(txt_path, sizeof(txt_path), "%s/%s.txt", report_dir, run_name);
if (req->src_sfx) src_sfx = req->src_sfx;
snprintf(work_src_path, sizeof(work_src_path), "%s/%s%s",
full_working_dir, work_run_name, src_sfx);
snprintf(work_log_path, sizeof(work_log_path), "%s/%s.log",
full_working_dir, work_run_name);
r = generic_copy_file(REMOVE, global->compile_src_dir, pkt_name, src_sfx,
0, full_working_dir, work_run_name, src_sfx);
if (!r) {
snprintf(msgbuf, sizeof(msgbuf), "The source file %s/%s%s is missing.\n",
global->compile_src_dir, pkt_name, src_sfx);
goto internal_error;
}
if (r < 0) {
snprintf(msgbuf, sizeof(msgbuf),
"Read error on the source file %s/%s%s is missing.\n",
global->compile_src_dir, pkt_name, src_sfx);
goto internal_error;
}
//info("Starting: %s %s", req->style_checker, work_src_path);
tsk = task_New();
task_AddArg(tsk, req->style_checker);
task_AddArg(tsk, work_src_path);
task_SetPathAsArg0(tsk);
task_SetWorkingDir(tsk, full_working_dir);
task_EnableProcessGroup(tsk);
task_SetRedir(tsk, 0, TSR_FILE, "/dev/null", TSK_READ);
task_SetRedir(tsk, 1, TSR_FILE, work_log_path, TSK_REWRITE, 0777);
task_SetRedir(tsk, 2, TSR_DUP, 1);
if (req->sc_env_num > 0) {
for (i = 0; i < req->sc_env_num; i++)
task_PutEnv(tsk, req->sc_env_vars[i]);
}
task_EnableAllSignals(tsk);
task_PrintArgs(tsk);
if (task_Start(tsk) < 0) {
err("Failed to start style checker process");
snprintf(msgbuf, sizeof(msgbuf), "Failed to start style checker %s\n",
req->style_checker);
goto internal_error;
}
task_Wait(tsk);
if (task_IsTimeout(tsk)) {
err("Style checker process is timed out");
snprintf(msgbuf, sizeof(msgbuf), "Style checker %s process timeout\n",
req->style_checker);
goto internal_error;
}
r = task_Status(tsk);
if (r != TSK_EXITED && r != TSK_SIGNALED) {
err("Style checker invalid task status");
snprintf(msgbuf, sizeof(msgbuf),
"Style checker %s invalid task status %d\n",
req->style_checker, r);
goto internal_error;
}
if (r == TSK_SIGNALED) {
err("Style checker terminated by signal");
snprintf(msgbuf, sizeof(msgbuf),
"Style checker %s terminated by signal %d\n",
req->style_checker, task_TermSignal(tsk));
goto internal_error;
}
r = task_ExitCode(tsk);
if (r != 0 && r != RUN_COMPILE_ERR && r != RUN_PRESENTATION_ERR
&& r != RUN_WRONG_ANSWER_ERR && r != RUN_STYLE_ERR) {
err("Invalid style checker exit code");
snprintf(msgbuf, sizeof(msgbuf),
"Style checker %s exit code %d\n",
req->style_checker, r);
goto internal_error;
}
if (r) {
// style checker error
rpl->status = RUN_STYLE_ERR;
get_current_time(&rpl->ts3, &rpl->ts3_us);
generic_copy_file(0, 0, work_log_path, "", 0, 0, log_path, "");
generic_copy_file(0, 0, work_log_path, "", 0, 0, txt_path, "");
} else {
// success
rpl->status = RUN_OK;
get_current_time(&rpl->ts3, &rpl->ts3_us);
generic_copy_file(0, 0, work_log_path, "", 0, 0, txt_path, "");
}
if (compile_reply_packet_write(rpl, &reply_bin_size, &reply_bin) < 0)
goto cleanup;
// ignore error: we cannot do anything anyway
generic_write_file(reply_bin, reply_bin_size, SAFE, status_dir, run_name, 0);
cleanup:
task_Delete(tsk); tsk = 0;
xfree(reply_bin); reply_bin = 0;
req = compile_request_packet_free(req);
clear_directory(full_working_dir);
return 0;
internal_error:
rpl->status = RUN_CHECK_FAILED;
get_current_time(&rpl->ts3, &rpl->ts3_us);
if (compile_reply_packet_write(rpl, &reply_bin_size, &reply_bin) < 0)
goto cleanup;
if (generic_write_file(msgbuf, strlen(msgbuf), 0, 0, log_path, 0) < 0)
goto cleanup;
if (generic_write_file(reply_bin, reply_bin_size, SAFE, status_dir,
run_name, 0) < 0) {
unlink(log_path);
}
goto cleanup;
}
struct testinfo_subst_handler_compile
{
struct testinfo_subst_handler b;
const struct compile_request_packet *request;
const struct section_language_data *lang;
};
static unsigned char *
subst_get_variable(
const void *vp,
const unsigned char *name)
{
const struct testinfo_subst_handler_compile *phc = (const struct testinfo_subst_handler_compile *) vp;
if (!strncmp(name, "request.", 8)) {
return meta_get_variable_str(&meta_compile_request_packet_methods, phc->request, name + 8);
} else if (!strncmp(name, "lang.", 5)) {
return meta_get_variable_str(&cntslang_methods, phc->lang, name + 5);
} else {
return xstrdup("");
}
}
static unsigned char *
testinfo_subst_handler_substitute(struct testinfo_subst_handler *bp, const unsigned char *str)
{
return text_substitute(bp, str, subst_get_variable);
}
#define VALID_SIZE(z) ((z) > 0 && (z) == (size_t) (z))
static int
invoke_style_checker(
FILE *log_f,
const struct serve_state *cs,
const struct section_language_data *lang,
const struct compile_request_packet *req,
const unsigned char *input_file,
const unsigned char *working_dir,
const unsigned char *log_path,
const testinfo_t *tinf)
{
tpTask tsk = 0;
int retval = RUN_CHECK_FAILED;
tsk = task_New();
task_AddArg(tsk, req->style_checker);
task_AddArg(tsk, input_file);
task_SetPathAsArg0(tsk);
task_SetWorkingDir(tsk, working_dir);
task_EnableProcessGroup(tsk);
task_SetRedir(tsk, 0, TSR_FILE, "/dev/null", TSK_READ);
task_SetRedir(tsk, 1, TSR_FILE, log_path, TSK_APPEND, 0777);
task_SetRedir(tsk, 2, TSR_DUP, 1);
if (req->sc_env_num > 0) {
for (int i = 0; i < req->sc_env_num; i++)
task_PutEnv(tsk, req->sc_env_vars[i]);
}
if (tinf && tinf->style_checker_env_u > 0) {
for (int i = 0; i < tinf->style_checker_env_u; ++i)
task_PutEnv(tsk, tinf->style_checker_env_v[i]);
}
if (lang && lang->compile_real_time_limit > 0) {
task_SetMaxRealTime(tsk, lang->compile_real_time_limit);
}
task_EnableAllSignals(tsk);
task_PrintArgs(tsk);
if (task_Start(tsk) < 0) {
err("Failed to start style checker process");
fprintf(log_f, "\nFailed to start style checker %s\n", req->style_checker);
goto cleanup;
}
task_Wait(tsk);
if (task_IsTimeout(tsk)) {
err("Style checker process is timed out");
fprintf(log_f, "\nStyle checker %s process is timed out\n", req->style_checker);
goto cleanup;
}
int r = task_Status(tsk);
if (r != TSK_EXITED && r != TSK_SIGNALED) {
err("Style checker invalid task status");
fprintf(log_f, "\nStyle checker %s invalid task status %d\n", req->style_checker, r);
goto cleanup;
}
if (r == TSK_SIGNALED) {
err("Style checker terminated by signal");
fprintf(log_f, "\nStyle checker %s terminated by signal %d\n", req->style_checker, task_TermSignal(tsk));
goto cleanup;
}
r = task_ExitCode(tsk);
if (r != 0 && r != RUN_COMPILE_ERR && r != RUN_PRESENTATION_ERR && r != RUN_WRONG_ANSWER_ERR && r != RUN_STYLE_ERR) {
err("Invalid style checker exit code");
fprintf(log_f, "\nStyle checker %s invalid exit code %d\n", req->style_checker, r);
goto cleanup;
}
fprintf(log_f, "\n");
if (!r) {
retval = RUN_OK;
} else {
retval = RUN_STYLE_ERR;
fprintf(log_f, "\nStyle checker detected errors\n");
}
cleanup:
task_Delete(tsk);
return retval;
}
static int
invoke_compiler(
FILE *log_f,
const struct serve_state *cs,
const struct section_language_data *lang,
const struct compile_request_packet *req,
const unsigned char *input_file,
const unsigned char *output_file,
const unsigned char *working_dir,
const unsigned char *log_path,
const testinfo_t *tinf)
{
const struct section_global_data *global = serve_state.global;
tpTask tsk = 0;
tsk = task_New();
task_AddArg(tsk, lang->cmd);
task_AddArg(tsk, input_file);
task_AddArg(tsk, output_file);
task_SetPathAsArg0(tsk);
task_EnableProcessGroup(tsk);
if (VALID_SIZE(req->max_vm_size)) {
task_SetVMSize(tsk, req->max_vm_size);
} else if (VALID_SIZE(lang->max_vm_size)) {
task_SetVMSize(tsk, lang->max_vm_size);
} else if (VALID_SIZE(global->compile_max_vm_size)) {
task_SetVMSize(tsk, global->compile_max_vm_size);
}
if (VALID_SIZE(req->max_stack_size)) {
task_SetStackSize(tsk, req->max_stack_size);
} else if (VALID_SIZE(lang->max_stack_size)) {
task_SetStackSize(tsk, lang->max_stack_size);
} else if (VALID_SIZE(global->compile_max_stack_size)) {
task_SetStackSize(tsk, global->compile_max_stack_size);
}
if (VALID_SIZE(req->max_file_size)) {
task_SetMaxFileSize(tsk, req->max_file_size);
} else if (VALID_SIZE(lang->max_file_size)) {
task_SetMaxFileSize(tsk, lang->max_file_size);
} else if (VALID_SIZE(global->compile_max_file_size)) {
task_SetMaxFileSize(tsk, global->compile_max_file_size);
}
if (req->env_num > 0) {
for (int i = 0; i < req->env_num; i++)
task_PutEnv(tsk, req->env_vars[i]);
}
if (tinf && tinf->compiler_env_u > 0) {
for (int i = 0; i < tinf->compiler_env_u; ++i)
task_PutEnv(tsk, tinf->compiler_env_v[i]);
}
task_SetWorkingDir(tsk, working_dir);
task_SetRedir(tsk, 0, TSR_FILE, "/dev/null", TSK_READ);
if (tinf && tinf->compiler_must_fail > 0) {
task_SetRedir(tsk, 1, TSR_FILE, "/dev/null", TSK_WRITE, 0777);
} else {
task_SetRedir(tsk, 1, TSR_FILE, log_path, TSK_APPEND, 0777);
}
task_SetRedir(tsk, 2, TSR_DUP, 1);
if (lang->compile_real_time_limit > 0) {
task_SetMaxRealTime(tsk, lang->compile_real_time_limit);
}
task_EnableAllSignals(tsk);
task_PrintArgs(tsk);
if (task_Start(tsk) < 0) {
err("failed to start compiler '%s'", lang->cmd);
fprintf(log_f, "\nFailed to start compiler '%s'\n", lang->cmd);
task_Delete(tsk);
return RUN_CHECK_FAILED;
}
task_Wait(tsk);
if (task_IsTimeout(tsk)) {
err("Compilation process timed out");
fprintf(log_f, "\nCompilation process timed out\n");
task_Delete(tsk);
return RUN_COMPILE_ERR;
} else if (task_IsAbnormal(tsk)) {
info("Compilation failed");
task_Delete(tsk);
return RUN_COMPILE_ERR;
} else {
info("Compilation sucessful");
task_Delete(tsk);
return RUN_OK;
}
}
static void
handle_packet(
FILE *log_f,
const struct serve_state *cs,
const unsigned char *pkt_name,
const struct compile_request_packet *req,
struct compile_reply_packet *rpl,
const struct section_language_data *lang,
const unsigned char *run_name, // the incoming packet name
const unsigned char *src_path, // path to the source file in the spool directory
const unsigned char *exe_path, // path to the resulting exe file in the spool directory
const unsigned char *working_dir, // the working directory
const unsigned char *log_work_path, // the path to the log file (open in APPEND mode)
unsigned char *exe_work_name, // OUTPUT: the name of the executable
int *p_override_exe,
int *p_exe_copied)
{
struct ZipData *zf = NULL;
if (req->output_only) {
if (req->style_checker && req->style_checker[0]) {
unsigned char src_work_name[PATH_MAX];
snprintf(src_work_name, sizeof(src_work_name), "%06d%s", req->run_id, "" /*lang->src_sfx*/);
unsigned char src_work_path[PATH_MAX];
snprintf(src_work_path, sizeof(src_work_path), "%s/%s", working_dir, src_work_name);
if (generic_copy_file(0, NULL, src_path, "", 0, NULL, src_work_path, "") < 0) {
fprintf(log_f, "cannot copy '%s' -> '%s'\n", src_path, exe_path);
rpl->status = RUN_CHECK_FAILED;
goto cleanup;
}
int r = invoke_style_checker(log_f, cs, lang, req, src_work_name, working_dir, log_work_path, NULL);
if (r != RUN_OK) {
rpl->status = r;
goto cleanup;
}
}
if (rename(src_path, exe_path) >= 0) {
*p_exe_copied = 1;
rpl->status = RUN_OK;
goto cleanup;
}
if (errno != EXDEV) {
fprintf(log_f, "cannot move '%s' -> '%s': %s\n", src_path, exe_path, strerror(errno));
rpl->status = RUN_CHECK_FAILED;
goto cleanup;
}
if (generic_copy_file(REMOVE, NULL, src_path, "", 0, NULL, exe_path, "") < 0) {
fprintf(log_f, "cannot copy '%s' -> '%s'\n", src_path, exe_path);
rpl->status = RUN_CHECK_FAILED;
goto cleanup;
}
*p_exe_copied = 1;
rpl->status = RUN_OK;
goto cleanup;
}
if (!lang) {
fprintf(log_f, "invalid language %d\n", req->lang_id);
rpl->status = RUN_CHECK_FAILED;
goto cleanup;
}
unsigned char src_work_name[PATH_MAX];
snprintf(src_work_name, sizeof(src_work_name), "%06d%s", req->run_id, lang->src_sfx);
unsigned char src_work_path[PATH_MAX];
snprintf(src_work_path, sizeof(src_work_path), "%s/%s", working_dir, src_work_name);
if (rename(src_path, src_work_path) >= 0) {
} else if (errno != EXDEV) {
fprintf(stderr, "cannot move '%s' -> '%s': %s\n", src_path, src_work_path, strerror(errno));
rpl->status = RUN_CHECK_FAILED;
goto cleanup;
} else {
if (generic_copy_file(REMOVE, NULL, src_path, "", 0, NULL, src_work_path, "") < 0) {
fprintf(log_f, "cannot copy '%s' -> '%s'\n", src_path, exe_path);
rpl->status = RUN_CHECK_FAILED;
goto cleanup;
}
}
if (!req->multi_header) {
snprintf(exe_work_name, PATH_MAX, "%06d%s", req->run_id, lang->exe_sfx);
unsigned char exe_work_path[PATH_MAX];
snprintf(exe_work_path, sizeof(exe_work_path), "%s/%s", working_dir, exe_work_name);
/*
if (req->style_checker && req->style_checker[0]) {
int r = invoke_style_checker(log_f, cs, lang, req, src_work_name, working_dir, log_work_path, NULL);
if (r != RUN_OK) {
rpl->status = r;
goto cleanup;
}
if (req->style_check_only) {
rpl->status = RUN_OK;
*p_override_exe = 1;
goto cleanup;
}
}
*/
if (req->style_check_only <= 0) {
int r = invoke_compiler(log_f, cs, lang, req, src_work_name, exe_work_name, working_dir, log_work_path, NULL);
rpl->status = r;
if (r != RUN_OK) goto cleanup;
}
if (req->style_checker && req->style_checker[0]) {
int r = invoke_style_checker(log_f, cs, lang, req, src_work_name, working_dir, log_work_path, NULL);
rpl->status = r;
if (r == RUN_OK && req->style_check_only > 0) *p_override_exe = 1;
}
goto cleanup;
}
// multi-header mode
snprintf(exe_work_name, PATH_MAX, "%06d%s", req->run_id, lang->exe_sfx);
unsigned char exe_work_path[PATH_MAX];
snprintf(exe_work_path, sizeof(exe_work_path), "%s/%s", working_dir, exe_work_name);
zf = ej_libzip_open(log_f, exe_work_path, O_CREAT | O_TRUNC | O_WRONLY);
if (!zf) {
fprintf(log_f, "cannot create zip archive '%s'\n", exe_work_path);
rpl->status = RUN_CHECK_FAILED;
goto cleanup;
}
if (!req->header_dir || !req->header_dir[0]) {
fprintf(log_f, "'header_dir' parameter is not specified\n");
rpl->status = RUN_CHECK_FAILED;
goto cleanup;
}
struct stat stb;
if (stat(req->header_dir, &stb) < 0) {
fprintf(log_f, "header_dir directory '%s' does not exist\n", req->header_dir);
rpl->status = RUN_CHECK_FAILED;
goto cleanup;
}
if (!S_ISDIR(stb.st_mode)) {
fprintf(log_f, "header_dir '%s' is not directory\n", req->header_dir);
rpl->status = RUN_CHECK_FAILED;
goto cleanup;
}
int style_already_checked = 0;
int serial = 0;
int status = RUN_OK;
while (1) {
unsigned char header_base[PATH_MAX];
unsigned char footer_base[PATH_MAX];
unsigned char compiler_env_base[PATH_MAX];
++serial;
header_base[0] = 0;
footer_base[0] = 0;
compiler_env_base[0] = 0;
if (req->header_pat && req->header_pat[0]) {
snprintf(header_base, sizeof(header_base), req->header_pat, serial);
}
if (req->footer_pat && req->footer_pat[0]) {
snprintf(footer_base, sizeof(footer_base), req->footer_pat, serial);
}
if (req->compiler_env_pat && req->compiler_env_pat[0]) {
snprintf(compiler_env_base, sizeof(compiler_env_base), req->compiler_env_pat, serial);
}
unsigned char header_path[PATH_MAX];
unsigned char footer_path[PATH_MAX];
unsigned char compiler_env_path[PATH_MAX];
header_path[0] = 0;
footer_path[0] = 0;
compiler_env_path[0] = 0;
if (header_base[0]) {
if (req->lang_header) {
snprintf(header_path, sizeof(header_path), "%s/%s.%s%s", req->header_dir, header_base, lang->short_name, lang->src_sfx);
} else {
snprintf(header_path, sizeof(header_path), "%s/%s%s", req->header_dir, header_base, lang->src_sfx);
}
}
if (footer_base[0]) {
if (req->lang_header) {
snprintf(footer_path, sizeof(footer_path), "%s/%s.%s%s", req->header_dir, footer_base, lang->short_name, lang->src_sfx);
} else {
snprintf(footer_path, sizeof(footer_path), "%s/%s%s", req->header_dir, footer_base, lang->src_sfx);
}
}
if (compiler_env_base[0]) {
if (req->lang_header) {
snprintf(compiler_env_path, sizeof(compiler_env_path), "%s/%s.%s", req->header_dir, compiler_env_base, lang->short_name);
} else {
snprintf(compiler_env_path, sizeof(compiler_env_path), "%s/%s", req->header_dir, compiler_env_base);
}
}
int header_exists = (header_path[0] && access(header_path, R_OK) >= 0);
int footer_exists = (footer_path[0] && access(footer_path, R_OK) >= 0);
int env_exists = (compiler_env_path[0] && access(compiler_env_path, R_OK) >= 0);
if (!header_exists && !footer_exists && !env_exists) {
if (serial == 1) {
fprintf(log_f, "no test-specific header, footer, or compiler_env file found\n");
status = RUN_CHECK_FAILED;
}
break;
}
testinfo_t test_info;
memset(&test_info, 0, sizeof(test_info));
testinfo_t *tinf = NULL;
if (compiler_env_path[0]) {
struct testinfo_subst_handler_compile hc;
memset(&hc, 0, sizeof(hc));
hc.b.substitute = testinfo_subst_handler_substitute;
hc.request = req;
hc.lang = lang;
if (stat(compiler_env_path, &stb) < 0) {
fprintf(log_f, "compiler env file '%s' does not exist: %s\n", compiler_env_path, strerror(errno));
status = RUN_CHECK_FAILED;
continue;
} else if (!S_ISREG(stb.st_mode)) {
fprintf(log_f, "compiler env file '%s' is not regular\n", compiler_env_path);
status = RUN_CHECK_FAILED;
continue;
} else if (access(compiler_env_path, R_OK) < 0) {
fprintf(log_f, "compiler env file '%s' is not readable: %s\n", compiler_env_path, strerror(errno));
status = RUN_CHECK_FAILED;
continue;
} else if (testinfo_parse(compiler_env_path, &test_info, &hc.b) < 0) {
fprintf(log_f, "invalid env file '%s'\n", compiler_env_path);
status = RUN_CHECK_FAILED;
continue;
} else {
tinf = &test_info;
}
}
int file_check_failed = 0;
char *header_s = NULL, *footer_s = NULL;
size_t header_z = 0, footer_z = 0;
if (header_path[0]) {
if (stat(header_path, &stb) < 0) {
fprintf(log_f, "header file '%s' does not exist: %s\n", header_path, strerror(errno));
file_check_failed = 1;
} else if (!S_ISREG(stb.st_mode)) {
fprintf(log_f, "header file '%s' is not regular\n", header_path);
file_check_failed = 1;
} else if (access(header_path, R_OK) < 0) {
fprintf(log_f, "header file '%s' is not readable: %s\n", header_path, strerror(errno));
file_check_failed = 1;
} else if (generic_read_file(&header_s, 0, &header_z, 0, NULL, header_path, "") < 0) {
fprintf(log_f, "failed to read file '%s'\n", header_path);
file_check_failed = 1;
}
}
if (footer_path[0]) {
if (stat(footer_path, &stb) < 0) {
fprintf(log_f, "footer file '%s' does not exist: %s\n", footer_path, strerror(errno));
file_check_failed = 1;
} else if (!S_ISREG(stb.st_mode)) {
fprintf(log_f, "footer file '%s' is not regular\n", footer_path);
file_check_failed = 1;
} else if (access(footer_path, R_OK) < 0) {
fprintf(log_f, "footer file '%s' is not readable: %s\n", footer_path, strerror(errno));
file_check_failed = 1;
} else if (generic_read_file(&footer_s, 0, &footer_z, 0, NULL, footer_path, "") < 0) {
fprintf(log_f, "failed to read file '%s'\n", footer_path);
file_check_failed = 1;
}
}
if (file_check_failed) {
testinfo_free(tinf);
xfree(header_s);
xfree(footer_s);
status = RUN_CHECK_FAILED;
continue;
}
char *src_s = NULL;
size_t src_z = 0;
if (generic_read_file(&src_s, 0, &src_z, 0, NULL, src_work_path, "") < 0) {
fprintf(log_f, "failed to read source file '%s'\n", src_work_path);
testinfo_free(tinf);
xfree(header_s);
xfree(footer_s);
status = RUN_CHECK_FAILED;
continue;
}
size_t full_z = header_z + src_z + footer_z;
char *full_s = xmalloc(full_z + 1);
if (header_s && header_z > 0) {
memcpy(full_s, header_s, header_z);
}
memcpy(full_s + header_z, src_s, src_z);
if (footer_s && footer_z > 0) {
memcpy(full_s + header_z + src_z, footer_s, footer_z);
}
full_s[full_z] = 0;
unsigned char test_src_name[PATH_MAX];
snprintf(test_src_name, sizeof(test_src_name), "%06d_%03d%s", req->run_id, serial, lang->src_sfx);
unsigned char test_src_path[PATH_MAX];
snprintf(test_src_path, sizeof(test_src_path), "%s/%s", working_dir, test_src_name);
if (generic_write_file(full_s, full_z, 0, NULL, test_src_path, NULL) < 0) {
fprintf(log_f, "failed to write full source file '%s'\n", test_src_path);
testinfo_free(tinf);
xfree(full_s);
status = RUN_CHECK_FAILED;
xfree(header_s); header_s = NULL; header_z = 0;
xfree(footer_s); footer_s = NULL; footer_z = 0;
continue;
}
xfree(full_s); full_s = NULL; full_z = 0;
unsigned char test_exe_name[PATH_MAX];
snprintf(test_exe_name, sizeof(test_exe_name), "%06d_%03d%s", req->run_id, serial, lang->exe_sfx);
unsigned char test_exe_path[PATH_MAX];
snprintf(test_exe_path, sizeof(test_exe_path), "%s/%s", working_dir, test_exe_name);
int cur_status = RUN_OK;
/*
if (req->style_checker && req->style_checker[0]) {
cur_status = invoke_style_checker(log_f, cs, lang, req, test_src_name, working_dir, log_work_path, tinf);
// valid statuses: RUN_OK, RUN_STYLE_ERR, RUN_CHECK_FAILED
if (cur_status == RUN_CHECK_FAILED) {
status = RUN_CHECK_FAILED;
} else if (cur_status == RUN_STYLE_ERR) {
if (status == RUN_OK) {
status = RUN_STYLE_ERR;
}
} else if (cur_status != RUN_OK) {
fprintf(log_f, "invalid status %d returned from invoke_style_checker\n", cur_status);
status = RUN_CHECK_FAILED;
}
}
*/
if (cur_status == RUN_OK) {
fprintf(log_f, "=== compilation for test %d ===\n", serial);
fflush(log_f);
cur_status = invoke_compiler(log_f, cs, lang, req, test_src_name, test_exe_name, working_dir, log_work_path, tinf);
// valid statuses: RUN_OK, RUN_COMPILE_ERR, RUN_CHECK_FAILED
if (cur_status == RUN_CHECK_FAILED) {
status = RUN_CHECK_FAILED;
} else if (cur_status == RUN_COMPILE_ERR) {
if (tinf && tinf->compiler_must_fail > 0 && tinf->source_stub) {
unsigned char source_stub_path[PATH_MAX];
if (req->lang_header) {
snprintf(source_stub_path, sizeof(source_stub_path), "%s/%s.%s%s",
req->header_dir, tinf->source_stub, lang->short_name, lang->src_sfx);
} else {
snprintf(source_stub_path, sizeof(source_stub_path), "%s/%s%s", req->header_dir, tinf->source_stub, lang->src_sfx);
}
if (stat(source_stub_path, &stb) < 0) {
fprintf(log_f, "source stub file '%s' does not exist: %s\n", source_stub_path, strerror(errno));
status = RUN_CHECK_FAILED;
} else if (!S_ISREG(stb.st_mode)) {
fprintf(log_f, "source stub file '%s' is not regular\n", source_stub_path);
status = RUN_CHECK_FAILED;
} else if (access(source_stub_path, R_OK) < 0) {
fprintf(log_f, "source stub file '%s' is not readable: %s\n", source_stub_path, strerror(errno));
status = RUN_CHECK_FAILED;
} else {
char *source_stub_s = NULL;
size_t source_stub_z = 0;
if (generic_read_file(&source_stub_s, 0, &source_stub_z, 0, NULL, source_stub_path, "") < 0) {
fprintf(log_f, "failed to read file '%s'\n", source_stub_path);
status = RUN_CHECK_FAILED;
} else {
// ignore header and footer for now
if (1) {
full_z = source_stub_z;
full_s = xmalloc(full_z + 1);
memcpy(full_s, source_stub_s, source_stub_z);
full_s[full_z] = 0;
} else {
full_z = header_z + source_stub_z + footer_z;
full_s = xmalloc(full_z + 1);
if (header_s && header_z > 0) {
memcpy(full_s, header_s, header_z);
}
memcpy(full_s + header_z, source_stub_s, source_stub_z);
if (footer_s && footer_z > 0) {
memcpy(full_s + header_z + source_stub_z, footer_s, footer_z);
}
full_s[full_z] = 0;
}
xfree(source_stub_s); source_stub_s = NULL; source_stub_z = 0;
if (generic_write_file(full_s, full_z, 0, NULL, test_src_path, NULL) < 0) {
fprintf(log_f, "failed to write full source file '%s'\n", test_src_path);
status = RUN_CHECK_FAILED;
} else {
cur_status = invoke_compiler(log_f, cs, lang, req, test_src_name, test_exe_name, working_dir, log_work_path, tinf);
if (cur_status == RUN_CHECK_FAILED) {
status = RUN_CHECK_FAILED;
} else if (cur_status == RUN_COMPILE_ERR) {
if (status == RUN_OK || status == RUN_STYLE_ERR) {
status = RUN_COMPILE_ERR;
}
} else if (cur_status != RUN_OK) {
fprintf(log_f, "invalid status %d returned from invoke_compiler\n", cur_status);
status = RUN_CHECK_FAILED;
} else {
if (lstat(test_exe_path, &stb) < 0) {
fprintf(log_f, "output file '%s' does not exist: %s\n", test_exe_path, strerror(errno));
status = RUN_CHECK_FAILED;
} else if (!S_ISREG(stb.st_mode)) {
fprintf(log_f, "output file '%s' is not regular\n", test_exe_path);
status = RUN_CHECK_FAILED;
} else if (access(test_exe_path, X_OK) < 0) {
fprintf(log_f, "output file '%s' is not executable: %s\n", test_exe_path, strerror(errno));
status = RUN_CHECK_FAILED;
} else {
if (zf->ops->add_file(zf, test_exe_name, test_exe_path) < 0) {
fprintf(log_f, "cannot add file '%s' to zip archive\n", test_exe_path);
status = RUN_CHECK_FAILED;
}
}
}
}
xfree(full_s); full_s = NULL; full_z = 0;
}
}
} else {
if (status == RUN_OK || status == RUN_STYLE_ERR) {
status = RUN_COMPILE_ERR;
}
}
} else if (cur_status != RUN_OK) {
fprintf(log_f, "invalid status %d returned from invoke_compiler\n", cur_status);
status = RUN_CHECK_FAILED;
} else {
// OK
if (lstat(test_exe_path, &stb) < 0) {
fprintf(log_f, "output file '%s' does not exist: %s\n", test_exe_path, strerror(errno));
status = RUN_CHECK_FAILED;
} else if (!S_ISREG(stb.st_mode)) {
fprintf(log_f, "output file '%s' is not regular\n", test_exe_path);
status = RUN_CHECK_FAILED;
} else if (access(test_exe_path, X_OK) < 0) {
fprintf(log_f, "output file '%s' is not executable: %s\n", test_exe_path, strerror(errno));
status = RUN_CHECK_FAILED;
} else if (tinf && tinf->compiler_must_fail > 0) {
if (status == RUN_OK || status == RUN_STYLE_ERR) {
status = RUN_COMPILE_ERR;
}
fprintf(log_f, "compiler must fail on test %d, but compilation was successful\n", serial);
if (tinf->comment) {
fprintf(log_f, "possible reason:\n");
fprintf(log_f, "%s\n", tinf->comment);
}
} else {
if (zf->ops->add_file(zf, test_exe_name, test_exe_path) < 0) {
fprintf(log_f, "cannot add file '%s' to zip archive\n", test_exe_path);
status = RUN_CHECK_FAILED;
}
}
}
}
if (status == RUN_OK && !style_already_checked && req->style_checker && req->style_checker[0]) {
fprintf(log_f, "=== style checking ===\n");
fflush(log_f);
style_already_checked = 1;
cur_status = invoke_style_checker(log_f, cs, lang, req, test_src_name, working_dir, log_work_path, tinf);
// valid statuses: RUN_OK, RUN_STYLE_ERR, RUN_CHECK_FAILED
if (cur_status == RUN_CHECK_FAILED) {
status = RUN_CHECK_FAILED;
} else if (cur_status == RUN_STYLE_ERR) {
status = RUN_STYLE_ERR;
} else if (cur_status != RUN_OK) {
fprintf(log_f, "invalid status %d returned from invoke_style_checker\n", cur_status);
status = RUN_CHECK_FAILED;
}
}
xfree(header_s); header_s = NULL; header_z = 0;
xfree(footer_s); footer_s = NULL; footer_z = 0;
}
rpl->status = status;
rpl->zip_mode = 1;
cleanup:
if (zf) zf->ops->close(zf);
return;
}
static int
new_loop(int parallel_mode)
{
int retval = 0;
const struct section_global_data *global = serve_state.global;
int override_exe = 0;
int exe_copied = 0;
path_t full_working_dir = { 0 };
if (parallel_mode) {
random_init();
unsigned long long u64 = random_u64();
snprintf(full_working_dir, sizeof(full_working_dir), "%s/%016llx", global->compile_work_dir, u64);
if (make_dir(full_working_dir, 0) < 0) {
err("cannot create '%s': %s", full_working_dir, os_ErrorMsg());
return -1;
}
} else {
snprintf(full_working_dir, sizeof(full_working_dir), "%s", global->compile_work_dir);
}
interrupt_init();
interrupt_disable();
while (1) {
// terminate if signaled
if (interrupt_get_status() || interrupt_restart_requested()) break;
unsigned char pkt_name[PATH_MAX];
pkt_name[0] = 0;
int r = scan_dir(global->compile_queue_dir, pkt_name, sizeof(pkt_name), 0);
if (r < 0) {
switch (-r) {
case ENOMEM:
case ENOENT:
case ENFILE:
err("trying to recover, sleep for 5 seconds");
interrupt_enable();
os_Sleep(5000);
interrupt_disable();
continue;
default:
err("unrecoverable error, exiting");
return -1;
}
}
if (!r) {
interrupt_enable();
os_Sleep(global->sleep_time);
interrupt_disable();
continue;
}
char *pkt_ptr = NULL;
size_t pkt_len = 0;
r = generic_read_file(&pkt_ptr, 0, &pkt_len, SAFE | REMOVE, global->compile_queue_dir, pkt_name, "");
if (r == 0) continue;
if (r < 0 || !pkt_ptr) {
// it looks like there's no reasonable recovery strategy
// so, just ignore the error
continue;
}
struct compile_request_packet *req = NULL;
r = compile_request_packet_read(pkt_len, pkt_ptr, &req);
xfree(pkt_ptr); pkt_ptr = NULL;
if (r < 0) {
continue;
}
if (!req->contest_id) {
// special packets
r = req->lang_id;
req = compile_request_packet_free(req);
switch (r) {
case 1:
interrupt_flag_interrupt();
break;
case 2:
interrupt_flag_sighup();
break;
}
continue;