aboutsummaryrefslogtreecommitdiffstats
path: root/src/ProcMain.c
blob: d28853cfb2d951fae5b9075d2e550b73dcac16dc (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
#ifdef HAVE_CONFIG_H
# include "SctConfig.h"
#endif

#include "SctVersion.h"
#include "SctConst.h"
#include "SctCommon.h"
#include "ProcCommon.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <fcntl.h>
#include <dirent.h>
#include <unistd.h>
#include <errno.h>
#include <grp.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <sys/wait.h>

#include <pthread.h>
#include <semaphore.h>

#ifdef HAVE_CONF_CAP
#include <sys/prctl.h>
#include <sys/capability.h>
#endif

#define SCTJUDGE_PROC_CHILD_CLOSE_STDIN        0
#define SCTJUDGE_PROC_CHILD_OPEN_STDIN_SINK    1
#define SCTJUDGE_PROC_CHILD_OPEN_STDIN_FILE    2
#define SCTJUDGE_PROC_CHILD_DUP2_STDIN         3
#define SCTJUDGE_PROC_CHILD_CLOSE_STDOUT       4
#define SCTJUDGE_PROC_CHILD_OPEN_STDOUT_SINK   5
#define SCTJUDGE_PROC_CHILD_OPEN_STDOUT_FILE   6
#define SCTJUDGE_PROC_CHILD_DUP2_STDOUT        7
#define SCTJUDGE_PROC_CHILD_CLOSE_STDERR       8
#define SCTJUDGE_PROC_CHILD_REDIR_STDERR       9
#define SCTJUDGE_PROC_CHILD_OPEN_STDERR_FILE  10
#define SCTJUDGE_PROC_CHILD_DUP2_STDERR       11
#define SCTJUDGE_PROC_CHILD_CLOSE_OTHER_FDS   12
#define SCTJUDGE_PROC_CHILD_CHROOT            13
#define SCTJUDGE_PROC_CHILD_SETGID            14
#define SCTJUDGE_PROC_CHILD_SETGROUPS         15
#define SCTJUDGE_PROC_CHILD_SETUID            16
#define SCTJUDGE_PROC_CHILD_RESET_UID         17
#define SCTJUDGE_PROC_CHILD_CAP_SET_PROC      18
#define SCTJUDGE_PROC_CHILD_SETRLIMIT_MEM     19
#define SCTJUDGE_PROC_CHILD_SETRLIMIT_CORE    20
#define SCTJUDGE_PROC_CHILD_SETRLIMIT_CPU     21
#define SCTJUDGE_PROC_CHILD_SETRLIMIT_OUT     22
#define SCTJUDGE_PROC_CHILD_SETRLIMIT_FILE    23
#define SCTJUDGE_PROC_CHILD_SETRLIMIT_PROC    24
#define SCTJUDGE_PROC_CHILD_SETRLIMIT_STACK   25
#define SCTJUDGE_PROC_CHILD_EXEC              26
#define SCTJUDGE_PROC_CHILD_MAX               27


/* 變數用途在 ProcCommon.h 解釋過了 */
pid_t           child_pid;
pthread_mutex_t child_mutex;

_Bool           checktime_exist;
sem_t           checktime_sem;
pthread_t       checktime_thread;
pthread_mutex_t checktime_mutex;

_Bool           monitor_exist;
sem_t           monitor_sem;
pthread_t       monitor_thread;
pthread_mutex_t monitor_mutex;

_Bool           sctproc_tle;
pthread_mutex_t sctproc_tle_mutex;

pthread_mutex_t output_mutex = PTHREAD_MUTEX_INITIALIZER;

/* 內部供 logger 和 err 使用的變數 */
static void (*sctproc_output_debug)(void*, const char*, va_list);
static void (*sctproc_output_normal)(void*, const char*, va_list);
static void (*sctproc_output_err)(void*, const char*, va_list);
static void* sctproc_output_arg;
static int sctproc_verbose;

/* 如果回傳負數表示失敗,就用這個 */
static void sctproc_childonly_fail_if_negative(int fd, int rval, int workname){
    int childonly_mesg[2];
    childonly_mesg[0] = workname;
    childonly_mesg[1] = (rval < 0) ? errno : 0;
    write(fd, &childonly_mesg, sizeof(int) * 2);
    if(rval < 0){
        exit(1);
    }
}

/* 如果回傳 NULL 表示失敗,就用這個 */
static void sctproc_childonly_fail_if_null(int fd, void* rval, int workname){
    int childonly_mesg[2];
    childonly_mesg[0] = workname;
    childonly_mesg[1] = (rval == NULL) ? errno : 0;
    write(fd, &childonly_mesg, sizeof(int) * 2);
    if(rval == NULL){
        exit(2);
    }
}

void sctproc_setdefault(PROCINFO* procinfo){
    procinfo->i_verbose = 1;
    procinfo->i_exec = NULL;
    procinfo->i_argv = NULL;
    procinfo->i_chroot = NULL;
    procinfo->i_infile = NULL;
    procinfo->i_outfile = NULL;
    procinfo->i_errfile = NULL;
    procinfo->i_uid = 65535;
    procinfo->i_gid = 65535;
    procinfo->i_limit_time = 10 * 10000;
    procinfo->i_limit_mem = 64 * 1024 * 1024;
    procinfo->i_limit_core = 0;
    procinfo->i_limit_cpu = 0;
    procinfo->i_limit_out = 0;
    procinfo->i_limit_file = 0;
    procinfo->i_limit_proc = 0;
    procinfo->i_limit_stack = 8 * 1024 * 1024;
    procinfo->i_func_arg = NULL;
    procinfo->i_flag = 
        PROCINFO_FLAG_SECURE_CHECK |
        PROCINFO_FLAG_LIMIT_FILE | 
        PROCINFO_FLAG_LIMIT_PROC |
        PROCINFO_FLAG_LIMIT_CORE;
}

void sctproc_freestring(PROCINFO* procinfo){
    int i;

    if(procinfo->i_exec != NULL){
        free(procinfo->i_exec);
        procinfo->i_exec = NULL;
    }

    if(procinfo->i_argv != NULL){
        for(i=0; i<l4da_getlen(procinfo->i_argv); i++){
            if(l4da_v(procinfo->i_argv, char*, i) != NULL){
                free(l4da_v(procinfo->i_argv, char*, i));
            }
        }
        l4da_free(procinfo->i_argv);
        procinfo->i_argv = NULL;
    }

    if(procinfo->i_chroot != NULL){
        free(procinfo->i_chroot);
        procinfo->i_chroot = NULL;
    }

    if(procinfo->i_infile != NULL){
        free(procinfo->i_infile);
        procinfo->i_infile = NULL;
    }
    if(procinfo->i_outfile != NULL){
        free(procinfo->i_outfile);
        procinfo->i_outfile = NULL;
    }
    if(procinfo->i_errfile != NULL){
        free(procinfo->i_errfile);
        procinfo->i_chroot = NULL;
    }
}

int sctproc_main(PROCINFO* procinfo){

    /* private defines */
#define private_BUFMAX 256
#define private_READLEN ((sizeof(int))*2)

    sctproc_output_debug = procinfo->f_debug;
    sctproc_output_normal = procinfo->f_normal;
    sctproc_output_err = procinfo->f_err;
    sctproc_output_arg = procinfo->i_func_arg;
    sctproc_verbose = procinfo->i_verbose;

    int i;

    int rerr;    /* 接收錯誤碼的地方 */
    char errbuf[private_BUFMAX];  /* 系統錯誤訊息緩衝區 */

    int child_pipe[2]; /* 接收 child process 狀態用的 pipe */
    int child_mesg[2]; /* child process 傳來的訊息,[0]是動作,[1]是 errno */
    int child_status;  /* wait 時回傳的 child process 狀態 */
    _Bool child_terminated;/* child process 終止了沒? */
    char* child_workstr;   /* 指向 child process 目前狀態訊息的指標 */

    struct timespec exec_begin;
    struct timespec exec_end;

    /* 一些 child process 中專用的變數 */
    int    childonly_fdopt;
    int    childonly_fdcount;
    int    childonly_fdin;
    int    childonly_fdout;
    int    childonly_fderr;
    pid_t  childonly_pid;
    struct rlimit childonly_rlimit;
#ifdef HAVE_CONF_CAP
    cap_t  childonly_cap;
#endif

    /* 列出設定值,僅供偵錯使用 */
    sctproc_logger(2, "%s: 列出 PROCINFO 中的設定值\n", __func__);
    sctproc_logger(2, "%s: i_exec: %s\n", 
        __func__, STRING_NULL(procinfo->i_exec));
    for(i=0; l4da_v(procinfo->i_argv, char*, i) != NULL; i++){
        sctproc_logger(2, "%s: i_argv[%d]: %s\n",
            __func__, i, STRING_NULL(l4da_v(procinfo->i_argv, char*, i)));
    }
    sctproc_logger(2, "%s: i_chroot: %s\n", 
        __func__, STRING_NULL(procinfo->i_chroot));
    sctproc_logger(2, "%s: i_infile: %s\n",
        __func__, STRING_NULL(procinfo->i_infile));
    sctproc_logger(2, "%s: i_outfile: %s\n",
        __func__, STRING_NULL(procinfo->i_outfile));
    sctproc_logger(2, "%s: i_errfile: %s\n",
        __func__, STRING_NULL(procinfo->i_errfile));
    sctproc_logger(2, "%s: i_uid: %u\n", __func__, procinfo->i_uid);
    sctproc_logger(2, "%s: i_gid: %u\n", __func__, procinfo->i_gid);
    sctproc_logger(2, "%s: i_flag: FORCE: %s\n", __func__,
        DISPLAY_YESNO(procinfo->i_flag, PROCINFO_FLAG_FORCE));
    sctproc_logger(2, "%s: i_flag: DRYRUN: %s\n", __func__,
        DISPLAY_YESNO(procinfo->i_flag, PROCINFO_FLAG_DRYRUN));
    sctproc_logger(2, "%s: i_flag: SECURE_CHECK: %s\n", __func__,
        DISPLAY_YESNO(procinfo->i_flag, PROCINFO_FLAG_SECURE_CHECK));
    sctproc_logger(2, "%s: i_flag: REDIR_STDERR: %s\n", __func__,
        DISPLAY_YESNO(procinfo->i_flag, PROCINFO_FLAG_REDIR_STDERR));
    sctproc_logger(2, "%s: i_flag: SETUID: %s\n", __func__,
        DISPLAY_YESNO(procinfo->i_flag, PROCINFO_FLAG_SETUID));
    sctproc_logger(2, "%s: i_flag: SETGID: %s\n", __func__,
        DISPLAY_YESNO(procinfo->i_flag, PROCINFO_FLAG_SETGID));
    sctproc_logger(2, "%s: i_flag: CLOSE_FD: %s\n", __func__,
        DISPLAY_YESNO(procinfo->i_flag, PROCINFO_FLAG_CLOSE_FD));
    sctproc_logger(2, "%s: i_flag: LIMIT_MEM: %s\n", __func__,
        DISPLAY_YESNO(procinfo->i_flag, PROCINFO_FLAG_LIMIT_MEM));
    sctproc_logger(2, "%s: i_flag: LIMIT_CORE: %s\n", __func__,
        DISPLAY_YESNO(procinfo->i_flag, PROCINFO_FLAG_LIMIT_CORE));
    sctproc_logger(2, "%s: i_flag: LIMIT_CPU: %s\n", __func__,
        DISPLAY_YESNO(procinfo->i_flag, PROCINFO_FLAG_LIMIT_CPU));
    sctproc_logger(2, "%s: i_flag: LIMIT_OUT: %s\n", __func__,
        DISPLAY_YESNO(procinfo->i_flag, PROCINFO_FLAG_LIMIT_OUT));
    sctproc_logger(2, "%s: i_flag: LIMIT_FILE: %s\n", __func__,
        DISPLAY_YESNO(procinfo->i_flag, PROCINFO_FLAG_LIMIT_FILE));
    sctproc_logger(2, "%s: i_flag: LIMIT_PROC: %s\n", __func__,
        DISPLAY_YESNO(procinfo->i_flag, PROCINFO_FLAG_LIMIT_PROC));
    sctproc_logger(2, "%s: i_flag: LIMIT_STACK: %s\n", __func__,
        DISPLAY_YESNO(procinfo->i_flag, PROCINFO_FLAG_LIMIT_STACK));
    sctproc_logger(2, "%s: i_limit_time: %u\n", 
        __func__, procinfo->i_limit_time);
    sctproc_logger(2, "%s: i_limit_mem: %u\n",
        __func__, procinfo->i_limit_mem);
    sctproc_logger(2, "%s: i_limit_core: %u\n",
        __func__, procinfo->i_limit_core);
    sctproc_logger(2, "%s: i_limit_cpu: %u\n",
        __func__, procinfo->i_limit_cpu);
    sctproc_logger(2, "%s: i_limit_out: %u\n",
        __func__, procinfo->i_limit_out);
    sctproc_logger(2, "%s: i_limit_file: %u\n",
        __func__, procinfo->i_limit_file);
    sctproc_logger(2, "%s: i_limit_proc: %u\n",
        __func__, procinfo->i_limit_proc);
    sctproc_logger(2, "%s: i_limit_stack: %u\n",
        __func__, procinfo->i_limit_stack);

    /* 檢查 UID == 0 的狀況 */
    sctproc_logger(2, "%s: Check for user ID setting\n", __func__);
    if(procinfo->i_uid == 0 && (procinfo->i_flag & PROCINFO_FLAG_SETUID)){
        if(procrealuid == 0){ /* 我們不用 getuid(),因為可能被 swap 過 */
            if(!(procinfo->i_flag & PROCINFO_FLAG_FORCE)){
                sctproc_err("拒絕使用 root 執行受測程式\n");
                return SCTJUDGE_PROC_EXIT_INSECURE;
            }else{
                sctproc_logger(1, 
                    "警告:使用 root 身份執行受測程式,所有限制都可能被忽略\n");
            }
        }else{
            sctproc_err("非超級使用者不能把 UID 設定為 0\n");
            return SCTJUDGE_PROC_EXIT_REJECT;
        }
    }

    /* 檢查安全設定值,如果有要求的話。我們不檢查 chroot 目錄是不是空的,
     * 因為執行檔應該會複製進去 */
    if(procinfo->i_flag & PROCINFO_FLAG_SECURE_CHECK){
        sctproc_logger(2, "%s: Check for security\n", __func__);
        if(procinfo->i_chroot == NULL){
            sctproc_err("您沒有指定 chroot 目錄\n");
            return SCTJUDGE_PROC_EXIT_INSECURE;
        }
        if(!(procinfo->i_flag & PROCINFO_FLAG_SETUID)){
            sctproc_err("您沒有修改執行受測程式時的使用者\n");
            return SCTJUDGE_PROC_EXIT_INSECURE;
        }
        if(!(procinfo->i_flag & PROCINFO_FLAG_SETGID)){
            sctproc_err("您沒有修改執行受測程式時的群組\n");
            return SCTJUDGE_PROC_EXIT_INSECURE;
        }
        if(!(procinfo->i_flag & PROCINFO_FLAG_LIMIT_MEM)){
            sctproc_err("您沒有設定記憶體使用量上限\n");
            return SCTJUDGE_PROC_EXIT_INSECURE;
        }
        if(!(procinfo->i_flag & PROCINFO_FLAG_LIMIT_CORE)){
            sctproc_err("您沒有設定 core 檔案大小上限\n");
            return SCTJUDGE_PROC_EXIT_INSECURE;
        }
        if(!(procinfo->i_flag & PROCINFO_FLAG_LIMIT_PROC)){
            sctproc_err("您沒有設定 process 數量上限\n");
            return SCTJUDGE_PROC_EXIT_INSECURE;
        }
    }
    
    /* 檢查參數,若強制執行就忽略 */
    if(!(procinfo->i_flag & PROCINFO_FLAG_FORCE)){
        sctproc_logger(2, "%s: Validate process configuration\n", __func__);
        if(procinfo->i_exec == NULL){
            sctproc_err("沒有指定受測程式可執行檔名稱\n");
            return SCTJUDGE_PROC_EXIT_INVALID;
        }
        if(procinfo->i_argv == NULL){
            sctproc_err("沒有指定執行程式時的命令列\n");
            return SCTJUDGE_PROC_EXIT_INVALID;
        }
        if(procinfo->i_outfile == NULL){
            sctproc_err("沒有指定輸出檔案\n");
            return SCTJUDGE_PROC_EXIT_INVALID;
        }
        if(procinfo->i_limit_time <= 0){
            sctproc_err("沒有指定執行時間限制\n");
            return SCTJUDGE_PROC_EXIT_INVALID;
        }
        if((procinfo->i_flag & PROCINFO_FLAG_REDIR_STDERR) && 
            (procinfo->i_errfile != NULL)){
            sctproc_err("重新導向 stderr 時,不能指定 stderr 要導向的檔案\n");
            return SCTJUDGE_PROC_EXIT_INVALID;
        }
    }


    /* 產生 child process */
    sctproc_logger(2, "%s: Creating a pipe for status reporting\n", __func__);
    pipe(child_pipe);
    sctproc_logger(2, "%s: Creating a child process\n", __func__);
    child_pid = fork();

    if(child_pid < 0){
        rerr = errno;
        strerror_threadsafe(rerr, errbuf, private_BUFMAX);
        sctproc_logger(2, "%s: fork() = %d\n", __func__, child_pid);
        sctproc_logger(2, "%s: errno = %d (%s)\n", __func__, rerr, errbuf);
        sctproc_err("無法建立新程序:%s\n", errbuf);
        close(child_pipe[0]);
        close(child_pipe[1]);
        return SCTJUDGE_PROC_EXIT_FAILED;

    }else if(child_pid > 0){
        sctproc_logger(2, "%s: fork() = %d\n", __func__, child_pid);
        close(child_pipe[1]);

        /* 持續監測 child process 的狀態 */
        while(read_complete(child_pipe[0], (void*)child_mesg, private_READLEN)
            == private_READLEN){

            strerror_threadsafe(child_mesg[1], errbuf, private_BUFMAX);

            switch(child_mesg[0]){
                case SCTJUDGE_PROC_CHILD_CLOSE_STDIN:
                    sctproc_logger(2, "%s: child: close(%d), "
                        "errno = %d (%s)\n", 
                        __func__, STDIN_FILENO, child_mesg[1], errbuf);
                    sprintf_malloc(&child_workstr, "關閉標準輸入");
                    break;

                case SCTJUDGE_PROC_CHILD_OPEN_STDIN_SINK:
                    sctproc_logger(2, "%s: child: open(%s, O_RDONLY), "
                        "errno = %d (%s)\n",
                        __func__, WITH_NULL, child_mesg[1], errbuf);
                    sprintf_malloc(&child_workstr, "開啟輸入檔案 "WITH_NULL);
                    break;

                case SCTJUDGE_PROC_CHILD_OPEN_STDIN_FILE:
                    sctproc_logger(2, "%s: child: open(%s, O_RDONLY), "
                        "errno = %d (%s)\n",
                        __func__, procinfo->i_infile, child_mesg[1], errbuf);
                    sprintf_malloc(&child_workstr, "開啟輸入檔案 %s",
                        procinfo->i_infile);
                    break;

                case SCTJUDGE_PROC_CHILD_DUP2_STDIN:
                    sctproc_logger(2, "%s: child: dup2(<input file>, %d), "
                        "errno = %d (%s)\n",
                        __func__, STDIN_FILENO, child_mesg[1], errbuf);
                    sprintf_malloc(&child_workstr, "重新導向標準輸入");
                    break;

                case SCTJUDGE_PROC_CHILD_CLOSE_STDOUT:
                    sctproc_logger(2, "%s: child: close(%d), "
                        "errno = %d (%s)\n",
                        __func__, STDOUT_FILENO, child_mesg[1], errbuf);
                    sprintf_malloc(&child_workstr, "關閉標準輸出");
                    break;

                case SCTJUDGE_PROC_CHILD_OPEN_STDOUT_SINK:
                    sctproc_logger(2, "%s: child: open(%s, "
                        "O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR), "
                        "errno = %d (%s)\n",
                        __func__, WITH_NULL, child_mesg[1], errbuf);
                    sprintf_malloc(&child_workstr, "開啟輸出檔案"WITH_NULL);
                    break;

                case SCTJUDGE_PROC_CHILD_OPEN_STDOUT_FILE:
                    sctproc_logger(2, "%s: child: open(%s, "
                        "O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR), "
                        "errno = %d (%s)\n",
                        __func__, procinfo->i_outfile, child_mesg[1], errbuf);
                    sprintf_malloc(&child_workstr, "開啟輸出檔案 %s",
                        procinfo->i_outfile);
                    break;

                case SCTJUDGE_PROC_CHILD_DUP2_STDOUT:
                    sctproc_logger(2, "%s: child: dup2(<output file>, %d), "
                        "errno = %d (%s)\n",
                        __func__, STDOUT_FILENO, child_mesg[1], errbuf);
                    sprintf_malloc(&child_workstr, "重新導向標準輸出");
                    break;

                case SCTJUDGE_PROC_CHILD_CLOSE_STDERR:
                    sctproc_logger(2, "%s: child: close(%d), "
                        "errno = %d (%s)\n",
                        __func__, STDERR_FILENO, child_mesg[1], errbuf);
                    sprintf_malloc(&child_workstr, "關閉標準錯誤");
                    break;

                case SCTJUDGE_PROC_CHILD_REDIR_STDERR:
                    sctproc_logger(2, "%s: child: dup2(<output file>, %d), "
                        "errno = %d (%s)\n",
                        __func__, STDERR_FILENO, child_mesg[1], errbuf);
                    sprintf_malloc(&child_workstr, "將標準輸出導向標準錯誤");
                    break;

                case SCTJUDGE_PROC_CHILD_OPEN_STDERR_FILE:
                    sctproc_logger(2, "%s: child: open(%s, "
                        "O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR), "
                        "errno = %d (%s)\n",
                        __func__, procinfo->i_errfile, child_mesg[1], errbuf);
                    sprintf_malloc(&child_workstr, "開啟錯誤檔案 %s",
                        procinfo->i_errfile);
                    break;

                case SCTJUDGE_PROC_CHILD_DUP2_STDERR:
                    sctproc_logger(2, "%s: child: dup2(<err file>, %d), "
                        "errno = %d (%s)\n",
                        __func__, procinfo->i_errfile, child_mesg[1], errbuf);
                    sprintf_malloc(&child_workstr, "重新導向標準錯誤");
                    break;

                case SCTJUDGE_PROC_CHILD_CLOSE_OTHER_FDS:
                    sctproc_logger(2, "%s: child: for(i=3; i<getdtablesize();"
                        " i++){ close(i); }, errno = %d (%s)\n", 
                        __func__, child_mesg[1], errbuf);
                    sprintf_malloc(&child_workstr, "關閉所有其他已開啟的檔案");
                    break;

                case SCTJUDGE_PROC_CHILD_CHROOT:
                    sctproc_logger(2, "%s: child: chroot(\"%s\"), "
                        "errno = %d (%s)\n", 
                        __func__, procinfo->i_chroot, child_mesg[1], errbuf);
                    sprintf_malloc(&child_workstr, "切換根目錄至 %s", 
                        procinfo->i_chroot);
                    break;

                case SCTJUDGE_PROC_CHILD_SETGID:
                    sctproc_logger(2, "%s: child: setgid(%u), "
                        "errno = %d (%s)\n",
                        __func__, procinfo->i_gid, child_mesg[1], errbuf);
                    sprintf_malloc(&child_workstr, "設定 GID 為 %u",
                        procinfo->i_gid);
                    break;

                case SCTJUDGE_PROC_CHILD_SETGROUPS:
                    sctproc_logger(2, "%s: child: setgroups(1, { %u }), "
                        "errno = %d (%s)\n",
                        __func__, procinfo->i_gid, child_mesg[1], errbuf);
                    sprintf_malloc(&child_workstr, "設定 supplementary GID 為 "
                        "%u", procinfo->i_gid);
                    break;

                case SCTJUDGE_PROC_CHILD_SETUID:
                case SCTJUDGE_PROC_CHILD_RESET_UID:
                    sctproc_logger(2, "%s: child: setuid(%u), "
                        "errno = %d (%s)\n",
                        __func__, procinfo->i_uid, child_mesg[1], errbuf);
                    sprintf_malloc(&child_workstr, "設定 UID 為 %d\n", 
                        procinfo->i_uid);
                    break;

                case SCTJUDGE_PROC_CHILD_CAP_SET_PROC:
                    sctproc_logger(2, "%s: child: cap_set_proc(), "
                        "errno = %d (%s)\n",
                        __func__, child_mesg[1], errbuf);
                    sprintf_malloc(&child_workstr, "Drop Linux capabilites");
                    break;

                case SCTJUDGE_PROC_CHILD_SETRLIMIT_MEM:
                    sctproc_logger(2, "%s: child: setrlimit(RLIMIT_AS, "
                        "{ %u, %u }), errno = %d (%s)\n",
                        __func__, procinfo->i_limit_mem, procinfo->i_limit_mem,
                        child_mesg[1], errbuf);
                    sprintf_malloc(&child_workstr, "設定記憶體用量限制");
                    break;

                case SCTJUDGE_PROC_CHILD_SETRLIMIT_CORE:
                    sctproc_logger(2, "%s: child: setrlimit(RLIMIT_CORE, "
                        "{ %u, %u }), errno = %d (%s)\n",
                        __func__, procinfo->i_limit_core, 
                        procinfo->i_limit_core,
                        child_mesg[1], errbuf);
                    sprintf_malloc(&child_workstr, "設定 core dump 大小限制");
                    break;

                case SCTJUDGE_PROC_CHILD_SETRLIMIT_CPU:
                    sctproc_logger(2, "%s: child: setrlimit(RLIMIT_CPU, "
                        "{ %u, %u }), errno = %d (%s)\n",
                        __func__, procinfo->i_limit_cpu, procinfo->i_limit_cpu,
                        child_mesg[1], errbuf);
                    sprintf_malloc(&child_workstr, "設定 CPU 時間限制");
                    break;

                case SCTJUDGE_PROC_CHILD_SETRLIMIT_OUT:
                    sctproc_logger(2, "%s: child: setrlimit(RLIMIT_FSIZE, "
                        "{ %u, %u }), errno = %d (%s)\n",
                        __func__, procinfo->i_limit_out, procinfo->i_limit_out,
                        child_mesg[1], errbuf);
                    sprintf_malloc(&child_workstr, "設定輸出大小限制");
                    break;

                case SCTJUDGE_PROC_CHILD_SETRLIMIT_FILE:
                    sctproc_logger(2, "%s: child: setrlimit(RLIMIT_NOFILE, "
                        "{ %u, %u }), errno = %d (%s)\n",
                        __func__, procinfo->i_limit_file, 
                        procinfo->i_limit_file,
                        child_mesg[1], errbuf);
                    sprintf_malloc(&child_workstr, "設定檔案開啟數量限制");
                    break;

                case SCTJUDGE_PROC_CHILD_SETRLIMIT_PROC:
                    sctproc_logger(2, "%s: child: setrlimit(RLIMIT_NPROC, "
                        "{ %u, %u }), errno = %d (%s)\n",
                        __func__, procinfo->i_limit_proc, 
                        procinfo->i_limit_proc,
                        child_mesg[1], errbuf);
                    sprintf_malloc(&child_workstr, "設定程序數量限制");
                    break;

                case SCTJUDGE_PROC_CHILD_SETRLIMIT_STACK:
                    sctproc_logger(2, "%s: child: setrlimit(RLIMIT_STACK, "
                        "{ %u, %u }), errno = %d (%s)\n",
                        __func__, procinfo->i_limit_stack, 
                        procinfo->i_limit_stack,
                        child_mesg[1], errbuf);
                    sprintf_malloc(&child_workstr, "設定 stack 大小限制");
                    break;

                case SCTJUDGE_PROC_CHILD_EXEC:
                    sctproc_logger(2, "%s: child: execv(\"%s\", ...), "
                        "errno = %d (%s)\n",
                        __func__, procinfo->i_exec, procinfo->i_exec,
                        child_mesg[1], errbuf);
                    sprintf_malloc(&child_workstr, "執行受測程式 %s", 
                        procinfo->i_exec);
                    break;

                default:
                    sctproc_logger(2, "%s: child: unknown message\n", __func__);
                    sprintf_malloc(&child_workstr, "不明的訊息");
            }

            if(child_mesg[1]){
                sctproc_err("子程序傳回錯誤:%s%s\n", child_workstr, errbuf);
                free(child_workstr);
                sctproc_logger(2, "%s: Kill the child process NOW!", __func__);
                kill(child_pid, SIGKILL);
                sctproc_logger(2, "%s: Wait for the child process", __func__);
                waitpid(child_pid, NULL, 0);
                close(child_pipe[0]);
                return SCTJUDGE_PROC_EXIT_FAILED;

            }else{
                free(child_workstr);
            }
        }

        /* exec 成功,正式啟動所有 judge 所需的東西 */
        sctproc_logger(2, "%s: 受測程式已成功執行\n", __func__);
        clock_gettime(CLOCK_REALTIME, &exec_begin);
        sctproc_logger(2, "%s: 現在時間是 (timespec) %d.%ld\n", __func__, 
            exec_begin.tv_sec, exec_begin.tv_nsec);
        close(child_pipe[0]);

        /* 初始化所有同步用的東西 */
        sctproc_logger(2, "%s: Initialize mutexes and semaphores\n", __func__);
        pthread_mutex_init(&checktime_mutex, NULL);
        pthread_mutex_init(&sctproc_tle_mutex, NULL);
        sem_init(&checktime_sem, 0, 0);
        if(procinfo->i_verbose > 0){
            pthread_mutex_init(&monitor_mutex, NULL);
            sem_init(&monitor_sem, 0, 0);
        }

        /* 產生 thread,一樣要有 verbose > 0 才有 monitor */
        sctproc_logger(2, "%s: Create thread: checktime\n", __func__);
        pthread_create(&checktime_thread, NULL, &sctproc_checktime, procinfo);
        pthread_mutex_lock(&checktime_mutex);
        checktime_exist = 1;
        pthread_mutex_unlock(&checktime_mutex);
        if(procinfo->i_verbose > 0){
            sctproc_logger(2, "%s: Create thread: monitor\n", __func__);
            pthread_create(&monitor_thread, NULL, &sctproc_monitor, procinfo);
            pthread_mutex_lock(&monitor_mutex);
            monitor_exist = 1;
            pthread_mutex_unlock(&monitor_mutex);
        }
        
        /* 開始要 wait 了,莫名其妙 stop 的程式我最多只會送
         * SCTJUDGE_PROC_MAX_STOP_TIMES 次 SIGCONT 給你(避免進入無限迴圈)
         * 這種奇怪的程式就等著 SLE 吧 
         * (這個常數定義在 SctConst.h,我想這不是很重要的東西)*/
        for(i=0, child_terminated=0; i<= SCTJUDGE_PROC_MAX_STOP_TIMES; i++){
            wait4(child_pid, &child_status, WUNTRACED, &procinfo->o_rusage);
            if(WIFSTOPPED(child_status) && i!=SCTJUDGE_PROC_MAX_STOP_TIMES){
                sctproc_logger(1, "受測程式因訊號 %d 而暫停,自動傳送 SIGCONT "
                    "(第 %d 次,最多可傳 %d 次)\n", WSTOPSIG(child_status),
                    i+1, SCTJUDGE_PROC_MAX_STOP_TIMES);
                kill(child_pid, SIGCONT);
            }else{
                if(WIFSIGNALED(child_status)){
                    clock_gettime(CLOCK_REALTIME, &exec_end);
                    child_terminated = 1;
                    procinfo->o_signal = WTERMSIG(child_status);
                    if(procinfo->o_signal == SIGXFSZ){
                        procinfo->o_result = PROCINFO_RESULT_OLE;
                    }else{
                        procinfo->o_result = PROCINFO_RESULT_RE;
                    }
                    procinfo->o_procexit = WEXITSTATUS(child_status);
                    break;
                }
                if(WIFEXITED(child_status)){
                    clock_gettime(CLOCK_REALTIME, &exec_end);
                    child_terminated = 1;
                    procinfo->o_result = PROCINFO_RESULT_OK;
                    procinfo->o_procexit = WEXITSTATUS(child_status);
                    break;
                }
                sctproc_logger(2, "%s: The child process is terminated\n",
                    __func__);
            }
        }

        /* 暫停次數超過限制 */
        if(!child_terminated){
            sctproc_logger(2, "%s: Sending SIGKILL\n", __func__);
            kill(child_pid, SIGKILL);
            wait4(child_pid, &child_status, WUNTRACED, &procinfo->o_rusage);
            procinfo->o_result = PROCINFO_RESULT_SLE;
            procinfo->o_procexit = WEXITSTATUS(child_status);
            clock_gettime(CLOCK_REALTIME, &exec_end);
        }


        /* 通知兩個 thread,請他們結束,也等他們結束 */
        pthread_mutex_lock(&checktime_mutex);
        if(checktime_exist){
            checktime_exist = 0;
            pthread_mutex_unlock(&checktime_mutex);
            sctproc_logger(2, "%s: Wait for thread checktime to exit\n",
                __func__);
            sem_post(&checktime_sem);
            pthread_join(checktime_thread, NULL);
        }else{
            pthread_mutex_unlock(&checktime_mutex);
        }

        pthread_mutex_lock(&monitor_mutex);
        if(monitor_exist){
            monitor_exist = 0;
            pthread_mutex_unlock(&monitor_mutex);
            sctproc_logger(2, "%s: Wait for thread monitor to exit\n",
                __func__);
            sem_post(&monitor_sem);
            pthread_join(monitor_thread, NULL);
        }else{
            pthread_mutex_unlock(&monitor_mutex);
        }

        /* 判斷 TLE 和執行時間 */
        difftimespec(&exec_begin, &exec_end, &(procinfo->o_runtime));
        checktimespec(&(procinfo->o_runtime));

        pthread_mutex_lock(&sctproc_tle_mutex);
        if(sctproc_tle){
            procinfo->o_result = PROCINFO_RESULT_TLE;
        }
        pthread_mutex_unlock(&sctproc_tle_mutex);

        if(sctproc_abort){
            procinfo->o_result = PROCINFO_RESULT_AB;
        }

        /* 移除掉所有同步用的東西 */
        sctproc_logger(2, "%s: Destroy mutexes and semaphores\n", __func__);
        pthread_mutex_destroy(&child_mutex);
        pthread_mutex_destroy(&checktime_mutex);
        pthread_mutex_destroy(&monitor_mutex);
        pthread_mutex_destroy(&sctproc_tle_mutex);
        sem_destroy(&checktime_sem);
        sem_destroy(&monitor_sem);

    }else{ 
        /* 這裡是 child process,也是要用到 setuid 的地方 
         * 因為我們在 main() 就已經停用 setuid 了
         * 所以我們必須在這段中需要的地方啟用他
         * 如果是用 Linux capabilites 舊部需要做而外處理
         * 如 */

        close(child_pipe[0]);

        /* close-on-exec 用來關閉這個暫時的資料傳輸通道 */
        childonly_fdopt = fcntl(child_pipe[1], F_GETFD);
        fcntl(child_pipe[1], F_SETFD, childonly_fdopt | FD_CLOEXEC);

        /* 設為獨立的 process group,不過失敗就算了,這不重要 */
        childonly_pid = getpid();
        setpgid(childonly_pid, childonly_pid);

        /* 重設所有 signal handler
         * XXX 我猜 signal 最大值是 31,還有其他更好的寫法嗎? */
        for(i=1; i<=31; i++){
            signal(i, SIG_DFL);
        }


        /* 開啟或關閉輸入檔案 */
        if(procinfo->i_infile == NULL &&
            (procinfo->i_flag & PROCINFO_FLAG_CLOSE_FD)){
            sctproc_childonly_fail_if_negative(child_pipe[1],
                close(STDIN_FILENO),
                SCTJUDGE_PROC_CHILD_CLOSE_STDIN);
        }else{
            if(procinfo->i_infile == NULL){
                sctproc_childonly_fail_if_negative(child_pipe[1],
                    childonly_fdin = open(WITH_NULL, O_RDONLY),
                    SCTJUDGE_PROC_CHILD_OPEN_STDIN_SINK);
            }else{
                sctproc_childonly_fail_if_negative(child_pipe[1],
                    childonly_fdin = open(procinfo->i_infile, O_RDONLY),
                    SCTJUDGE_PROC_CHILD_OPEN_STDIN_FILE);
            }
            sctproc_childonly_fail_if_negative(child_pipe[1],
                dup2(childonly_fdin, STDIN_FILENO),
                SCTJUDGE_PROC_CHILD_DUP2_STDIN);
        }

        /* 開啟或關閉輸出檔案 */
        childonly_fdout = -1;
        if(procinfo->i_outfile == NULL &&
            (procinfo->i_flag & PROCINFO_FLAG_CLOSE_FD)){
            sctproc_childonly_fail_if_negative(child_pipe[1],
                close(STDOUT_FILENO),
                SCTJUDGE_PROC_CHILD_CLOSE_STDOUT);
        }else{
            if(procinfo->i_outfile == NULL){
                sctproc_childonly_fail_if_negative(child_pipe[1],
                    childonly_fdout = open(WITH_NULL, 
                        O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR),
                    SCTJUDGE_PROC_CHILD_OPEN_STDOUT_SINK);
            }else{
                sctproc_childonly_fail_if_negative(child_pipe[1],
                    childonly_fdout = open(procinfo->i_outfile,
                        O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR),
                    SCTJUDGE_PROC_CHILD_OPEN_STDOUT_FILE);
            }
            sctproc_childonly_fail_if_negative(child_pipe[1],
                dup2(childonly_fdout, STDOUT_FILENO),
                SCTJUDGE_PROC_CHILD_DUP2_STDOUT);
        }

        /* 開啟或關閉標準錯誤檔案 */
        if(procinfo->i_errfile == NULL){
            if(procinfo->i_flag & PROCINFO_FLAG_CLOSE_FD){
                sctproc_childonly_fail_if_negative(child_pipe[1],
                    close(STDERR_FILENO),
                    SCTJUDGE_PROC_CHILD_CLOSE_STDERR);
            }
            if(procinfo->i_flag & PROCINFO_FLAG_REDIR_STDERR && 
                    childonly_fdout >= 0){
                sctproc_childonly_fail_if_negative(child_pipe[1],
                    dup2(childonly_fdout, STDERR_FILENO),
                    SCTJUDGE_PROC_CHILD_REDIR_STDERR);
            }
        }else{
            sctproc_childonly_fail_if_negative(child_pipe[1],
                childonly_fderr = open(procinfo->i_errfile,
                    O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR),
                SCTJUDGE_PROC_CHILD_OPEN_STDERR_FILE);
            sctproc_childonly_fail_if_negative(child_pipe[1],
                dup2(childonly_fderr, STDERR_FILENO),
                SCTJUDGE_PROC_CHILD_DUP2_STDERR);
        }

        /* 很暴力地把所有不該留著的 fd 關掉 */
        childonly_fdcount = getdtablesize();
        for(i=3; i<childonly_fdcount; i++){
            close(i);
        }
        child_mesg[0] = SCTJUDGE_PROC_CHILD_CLOSE_OTHER_FDS;
        child_mesg[1] = 0;
        write(child_pipe[1], &child_mesg, sizeof(int) * 2);

        /* 接下來的操作可能需要提升的權限 */
#ifndef HAVE_CONF_CAP
        enable_setuid();
#endif

        /* 進行 chroot */
        if(procinfo->i_chroot != NULL){
            sctproc_childonly_fail_if_negative(child_pipe[1],
                chroot(procinfo->i_chroot),
                SCTJUDGE_PROC_CHILD_CHROOT);
            chdir("/");
        }

#ifdef HAVE_CONF_CAP
        /* 確保修改身份的時候 capabilities 仍然留著
         * 等一下再自己把 capabilities 丟掉 */
        prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
#endif      

        /* 我要 setgid 嗎? */
        if(procinfo->i_flag & PROCINFO_FLAG_SETGID){
            sctproc_childonly_fail_if_negative(child_pipe[1],
                setgid(procinfo->i_gid),
                SCTJUDGE_PROC_CHILD_SETGID);
            sctproc_childonly_fail_if_negative(child_pipe[1],
                setgroups(1, &(procinfo->i_gid)),
                SCTJUDGE_PROC_CHILD_SETGROUPS);
        }
        
        /* 我要 setuid 嗎? */
        if(procinfo->i_flag & PROCINFO_FLAG_SETGID){
            sctproc_childonly_fail_if_negative(child_pipe[1],
                setuid(procinfo->i_uid),
                SCTJUDGE_PROC_CHILD_SETUID);
        }
        else{
            /* 即使不要求 setuid,也不能讓受測程式拿到 setuid root */
            sctproc_childonly_fail_if_negative(child_pipe[1],
                setuid(procrealuid),
                SCTJUDGE_PROC_CHILD_RESET_UID);
        }


#ifdef HAVE_CONF_CAP
        /* 丟掉所有 Linux capabilities */
        childonly_cap = cap_init();
        cap_clear_flag(childonly_cap, CAP_EFFECTIVE);
        cap_clear_flag(childonly_cap, CAP_INHERITABLE);
        cap_clear_flag(childonly_cap, CAP_PERMITTED);
        sctproc_childonly_fail_if_negative(child_pipe[1],
            cap_set_proc(childonly_cap),
            SCTJUDGE_PROC_CHILD_CAP_SET_PROC);
        /* 需要 drop bound 嗎?再考慮看看 */
#endif

        /* 至此所有特殊權力都已丟棄 */

        
        /* 開始設定資源限制 */

        /* -- MEM -- */
        if(procinfo->i_flag & PROCINFO_FLAG_LIMIT_MEM){
            childonly_rlimit.rlim_cur = procinfo->i_limit_mem;
            childonly_rlimit.rlim_max = procinfo->i_limit_mem;
            sctproc_childonly_fail_if_negative(child_pipe[1],
                setrlimit(RLIMIT_AS, &childonly_rlimit),
                SCTJUDGE_PROC_CHILD_SETRLIMIT_MEM);
        }

        /* -- CORE -- */
        if(procinfo->i_flag & PROCINFO_FLAG_LIMIT_CORE){
            childonly_rlimit.rlim_cur = procinfo->i_limit_core;
            childonly_rlimit.rlim_max = procinfo->i_limit_core;
            sctproc_childonly_fail_if_negative(child_pipe[1],
                setrlimit(RLIMIT_CORE, &childonly_rlimit),
                SCTJUDGE_PROC_CHILD_SETRLIMIT_CORE);
        }

        /* -- CPU -- */
        if(procinfo->i_flag & PROCINFO_FLAG_LIMIT_CPU){
            childonly_rlimit.rlim_cur = procinfo->i_limit_cpu;
            childonly_rlimit.rlim_max = procinfo->i_limit_cpu;
            sctproc_childonly_fail_if_negative(child_pipe[1],
                setrlimit(RLIMIT_CPU, &childonly_rlimit),
                SCTJUDGE_PROC_CHILD_SETRLIMIT_CPU);
        }

        /* -- OUT -- */
        if(procinfo->i_flag & PROCINFO_FLAG_LIMIT_OUT){
            childonly_rlimit.rlim_cur = procinfo->i_limit_out;
            childonly_rlimit.rlim_max = procinfo->i_limit_out;
            sctproc_childonly_fail_if_negative(child_pipe[1],
                setrlimit(RLIMIT_FSIZE, &childonly_rlimit),
                SCTJUDGE_PROC_CHILD_SETRLIMIT_FILE);
        }

        /* -- FILE -- */
        if(procinfo->i_flag & PROCINFO_FLAG_LIMIT_FILE){
            childonly_rlimit.rlim_cur = procinfo->i_limit_file;
            childonly_rlimit.rlim_max = procinfo->i_limit_file;
            sctproc_childonly_fail_if_negative(child_pipe[1],
                setrlimit(
#ifdef RLIMIT_NOFILE
                    RLIMIT_NOFILE
#else
                    RLIMIT_OFILE
#endif
                    , &childonly_rlimit),
                SCTJUDGE_PROC_CHILD_SETRLIMIT_FILE);
        }

        /* -- PROC -- */
        if(procinfo->i_flag & PROCINFO_FLAG_LIMIT_PROC){
            childonly_rlimit.rlim_cur = procinfo->i_limit_proc;
            childonly_rlimit.rlim_max = procinfo->i_limit_proc;
            sctproc_childonly_fail_if_negative(child_pipe[1],
                setrlimit(RLIMIT_NPROC, &childonly_rlimit),
                SCTJUDGE_PROC_CHILD_SETRLIMIT_PROC);
        }

        /* -- STACK -- */
        if(procinfo->i_flag & PROCINFO_FLAG_LIMIT_STACK){
            childonly_rlimit.rlim_cur = procinfo->i_limit_stack;
            childonly_rlimit.rlim_max = procinfo->i_limit_stack;
            sctproc_childonly_fail_if_negative(child_pipe[1],
                setrlimit(RLIMIT_STACK, &childonly_rlimit),
                SCTJUDGE_PROC_CHILD_SETRLIMIT_STACK);
        }
    
        /* 一切都完成了,現在要 exec 了! */
        sctproc_childonly_fail_if_negative(child_pipe[1],
            execv(procinfo->i_exec, l4da_data(procinfo->i_argv)),
            SCTJUDGE_PROC_CHILD_EXEC);
    }


    return SCTJUDGE_PROC_EXIT_SUCCESS;

    /* drop private defines */
#undef private_BUFMAX
#undef private_READLEN

}

void sctproc_logger(int level, const char* format, ...){
    va_list ap;
    va_start(ap, format);
    if(sctproc_verbose >= level){
        pthread_mutex_lock(&output_mutex);
        if(level >= 2){
            (*sctproc_output_debug)(sctproc_output_arg, format, ap);
        }else{
            (*sctproc_output_normal)(sctproc_output_arg, format, ap);
        }
        pthread_mutex_unlock(&output_mutex);
    }
    va_end(ap);
}

void sctproc_err(const char* format, ...){
    va_list ap;
    va_start(ap, format);
    pthread_mutex_lock(&output_mutex);
    (*sctproc_output_err)(sctproc_output_arg, format, ap);
    pthread_mutex_unlock(&output_mutex);
    va_end(ap);
}

#if 0
static const char* childmsg_text[SCTCHILD_MSGMAX] = {
    "開啟輸入檔案",
    "重新導向標準輸入",
    "開啟輸出檔案",
    "重新導向標準輸出",
    "開啟用於導向標準錯誤的檔案",
    "重新導向標準錯誤",
    "關閉所有不使用的檔案",
    "設定記憶體限制",
    "設定輸出限制",
    "設定禁止開啟其他檔案",
    "設定禁止產生新程序",
    "執行 chroot",
    "修改 real 和 effective UID",
    "修改 real 和 effective GID",
    "修改 supplementary GIDs",
    "丟棄所有 Linux capabilities",
    "執行受測程式"
};

static void sctjudge_makechild_cleanup_p1(void){
    pthread_mutex_lock(&tkill_mx);
    if(tkill_yes){
        tkill_yes = 0;
        pthread_mutex_unlock(&tkill_mx);
        sem_post(&tlethr);
    }else{
        pthread_mutex_unlock(&tkill_mx);
    }

    pthread_mutex_lock(&tdisplay_mx);
    if(tdisplay_yes){
        tdisplay_yes = 0;
        pthread_mutex_unlock(&tdisplay_mx);
        sem_post(&dispthr);
    }else{
        pthread_mutex_unlock(&tdisplay_mx);
    }

}
    
static void sctjudge_makechild_cleanup_p2(mcopt, oldperm, copiedexe)
    const struct makechildopt* mcopt;
    const mode_t oldperm;
    char* copiedexe;
{
    if(oldperm != -1 && mcopt->chrootdir != NULL){
        if(mcopt->flags & SCTMC_VERBOSE){
            printf("恢復 %s 的權限\n", mcopt->chrootdir);
        }
        chmod(mcopt->chrootdir, oldperm);
    }
    if(copiedexe != NULL && mcopt->chrootdir != NULL && 
            !(mcopt->flags & SCTMC_NOCOPY)){
        if(mcopt->flags & SCTMC_VERBOSE){
            printf("移除 %s......\n", copiedexe);
        }
        unlink(copiedexe);
        free(copiedexe);
    }
}

static void sctjudge_makechild_cleanup(mcopt, oldperm, copiedexe)
    const struct makechildopt* mcopt;
    const mode_t oldperm;
    char* copiedexe;
{
    sctjudge_makechild_cleanup_p1();
    sctjudge_makechild_cleanup_p2(mcopt, oldperm, copiedexe);
}

static int copyfilebyname(const char* src, const char* destdir
        , char** srcsn, char** destfn){
    /* strerror 並不 thread safe,因此必須確定只有一個 thread 會用到 */
    /* 為了確保安全,要求目的地目錄必須是空的 */
    DIR* dirp;
    if((dirp=opendir(destdir)) == NULL){
        fprintf(stderr, "無法開啟目錄 %s%s\n", destdir, strerror(errno));
        return -1;
    }
    struct dirent* entryp;
    while((entryp=readdir(dirp)) != NULL){
        if(entryp->d_name[0] != '.' || 
            (entryp->d_name[1] != '.' && entryp->d_name[1] != '\0')){
            fprintf(stderr, "拒絕複製檔案:目錄 %s 不是空的\n", destdir);
            return -1;
        }
    }
    int fd, fd2;
    if((fd=open(src, O_RDONLY)) < 0){
        fprintf(stderr, "無法讀取檔案 %s%s\n", src, strerror(errno));
        return -1;
    }
    const int bufsize = 4096;
    void* buf = malloc(bufsize);
    char* srcshortname;
    char* tmp;
    if((tmp=strrchr(src, '/')) == NULL){
        srcshortname = (char*)src;
    }else{
        srcshortname = tmp + 1;
    }
    *srcsn = srcshortname;
    char* destfilename = (char*)
        malloc(strlen(srcshortname) + strlen(destdir) + 1);
    sprintf(destfilename, "%s/%s", destdir, srcshortname);
    *destfn = destfilename;
    /* 新檔案權限是 0755,umask 亂弄的我可不管 */
    if((fd2=open(destfilename, O_CREAT | O_WRONLY | O_EXCL, 
                S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) < 0){
        fprintf(stderr, "無法建立檔案 %s%s\n", 
                destfilename, strerror(errno));
        free(buf);
        return -1;
    }
    int readcount;
    while((readcount = read(fd, buf, bufsize)) > 0){
        write(fd2, buf, readcount);
    }
    close(fd);
    close(fd2);
    free(buf);
    return 0;
}

static void sctjudge_list_settings(const struct makechildopt* mcopt){
    printf("\t受測程式可執行檔名稱:%s", mcopt->executable);
    if(mcopt->flags & SCTMC_NOCOPY && mcopt->chrootdir != NULL){
        printf(" (相對於 %s)\n"
            "\t受測程式執行時的根目錄:%s\n"
            "\t執行前複製檔案:否\n"
            , mcopt->chrootdir, mcopt->chrootdir);
    }else{
        putchar('\n');
        if(mcopt->chrootdir != NULL){
            printf("\t受測程式執行時的根目錄:%s\n"
                "\t執行前複製檔案:是\n"
                , mcopt->chrootdir);
        }else{
            puts("\t受測程式執行時的根目錄:/\n"
            "\t執行前複製檔案:否");
        }
    }
    printf("\t輸入檔案:%s\n"
        "\t輸出檔案:%s"
        , mcopt->inputfile, mcopt->outputfile);
    if(mcopt->flags & SCTMC_REDIR_STDERR){
        puts(" (重新導向標準輸出和標準錯誤)");
    }else{
        puts(" (重新導向標準輸出)");
    }
    printf("\t執行時間限制:%d 毫秒\n", mcopt->exectime);
    fputs("\t記憶體使用量限制:", stdout);
    if(mcopt->memlimit > 0){
        printf("%d 位元組\n", mcopt->memlimit);
    }else{
        puts("無限制");
    }
    fputs("\t輸出限制:", stdout);
    if(mcopt->outlimit > 0){
        printf("%d 位元組\n", mcopt->outlimit);
    }else{
        puts("無限制");
    }
    if(mcopt->flags & SCTMC_SETUID){
        printf("\t設定受測程式執行時的 UID 為 %u\n", mcopt->uid);
    }else{
        puts("\t執行受測程式時維持原有的 real UID 和 effective UID");
    }
    if(mcopt->flags & SCTMC_SETGID){
        printf("\t設定受測程式執行時的 GID 為 %u\n", mcopt->gid);
    }else{
        puts("\t執行受測程式時維持原有的 real GID、effective GID "
                "和 supplementary GID");
    }
}

static int read_size(int fd, void* buf, size_t count){
    /* 一定要讀到 count 位元組的資料才會回傳,所以只要回傳 < count
     * 就表示已經讀到 EOF 了 */
    char* bufp = (char*)buf;
    size_t curcount = 0;
    int rval;
    do{
        rval = read(fd, bufp, count - curcount);
        if(rval < 0){
            return rval;
        }else if(rval == 0){
            return curcount;
        }else{
            bufp += rval;
            curcount += rval;
        }
    }while(curcount < count);
    return count;
}

void* sctjudge_makechild(void* arg){
    /* 首先等所有 thread 都啟動完成再開始動作 */
    sem_wait(&mcthr);

    /* 宣告變數囉! */
    int i;

    /* 函式參數與回傳值,Judge 結果就當作回傳值 */
    struct makechildopt mcopt = *(struct makechildopt*)arg;
    struct makechildrval* toreturn = 
        (struct makechildrval*)malloc(sizeof(struct makechildrval));
    memset(toreturn, 0, sizeof(struct makechildrval));

    /* 可執行檔名稱儲存區 */
    char* execshortname = NULL; /* 可以說就是 basename,chroot+copy 才會用 */
    char* execdestname = NULL; /* 複製出來的暫時執行檔,chroot+copy 才會用 */
    /* 這幾個變數用來儲存 Judge 結果,如果沒有啟用 verbose 
     * 且一切正常,那這些是唯一會輸出的東西了,目的是輸出容
     * 易被其他程式分析 */

    /* 要 chroot 的目錄的相關資訊 */
    struct stat chrdirinfo;      /* 取得原始的 mode 用的 */
    mode_t chrdir_oldmode = -1;  /* 這樣我們才能在結束時 chmod 回去 */

    /* 子程序的 PID,因為不會變,所以複製一份下來就不用去全域變數拿的 */
    pid_t pidcopy;

    /* 和子程序溝通用的 */
    int childpipe[2];
    int childmsg[2]; /* 每個訊息的大小都是 sizeof(int)*2 
                        [0]=發生事情的名稱 [1]=errno */
    int readsize = sizeof(int) * 2;

    /* 計時用的 */
    struct timespec execstart, execfinish;

    /* 子程序結束狀態 */
    int childexitstat;
    char childterminated = 0;

    /* 子程序才用的到的變數 */
    int fdinput, fdoutput, fderr, childressize;
    const int fdtablesize = getdtablesize();
    struct rlimit childres;

    /* 變數宣告完了 */

    if(mcopt.flags & SCTMC_VERBOSE){
        puts("列出設定值:");
        sctjudge_list_settings(&mcopt);
        fflush(stdout);
    }
    if(mcopt.flags & SCTMC_DRYRUN){
        abort_makechild(SCTMCRVAL_SUCCESS);
    }
    
    /* 我們要開始做正事了 */
    /* 由此開始,我是唯一會用到 strerror() 的 thread
     * 所以說應該沒有問題 */
    if(mcopt.chrootdir != NULL && !(mcopt.flags & SCTMC_NOCOPY)){
        /* 必須要複製可執行檔 */
        if(mcopt.flags & SCTMC_VERBOSE){
            puts("開始複製可執行檔......");
        }
        if(copyfilebyname(mcopt.executable, mcopt.chrootdir, &execshortname
                    , &execdestname) < 0){
            abort_makechild(SCTMCRVAL_PREPARE);
        }
    }
    /* 再來是 chroot 的問題,避免受測程式亂開目錄之類的,
     * 所以把權限設成 0555,不過如果沒有變更 uid 和 gid ,
     * 受測程式還是可以 chmod 回來 */
    if(mcopt.chrootdir != NULL){
        if(mcopt.flags & SCTMC_VERBOSE){
            puts("取得用於 chroot 的目錄相關資訊");
        }
        if(stat(mcopt.chrootdir, &chrdirinfo) < 0){
            fprintf(stderr, "無法取得目錄 %s 的相關資訊:%s\n", 
                mcopt.chrootdir, strerror(errno));
            abort_makechild(SCTMCRVAL_PREPARE);
        }else{
            if((chrdirinfo.st_mode & S_IFMT) == S_IFDIR){
                chrdir_oldmode = chrdirinfo.st_mode &
                    (S_ISUID |S_ISGID |S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO);
            }else{
                fprintf(stderr, "%s 並不是目錄\n", mcopt.chrootdir);
                abort_makechild(SCTMCRVAL_PREPARE);
            }
        }
        if(mcopt.flags & SCTMC_VERBOSE){
            puts("嘗試變更此目錄的權限");
        }
        if(chmod(mcopt.chrootdir, 
            S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0){
            fprintf(stderr, "無法變更目錄 %s 的權限\n", mcopt.chrootdir);
            abort_makechild(SCTMCRVAL_PREPARE);
        }
    }

    fflush(stdout);
#ifndef HAVE_CONF_CAP
    enable_setuid();
#endif
    /* 建立用來和 chlid process 溝通的 pipe */
    pipe(childpipe);
    pthread_mutex_lock(&pidmutex);
#ifdef HAVE_WORKING_FORK
    pidchild = fork();
#else
#error "This program requires a working fork function."
#endif
    if(pidchild < 0){
#ifndef HAVE_CONF_CAP
        disable_setuid();
#endif
        pthread_mutex_unlock(&pidmutex);
        fprintf(stderr, "子程序建立失敗:%s\n", strerror(errno));
        abort_makechild(SCTMCRVAL_FORK);
    }
    else if(pidchild > 0){
        /* 我們原本的 thread */
        /* 從這裡開始,我們需要 setuid 來 kill 子程序,
         * 所以說不要執行 disable_setuid() */
        pidcopy = pidchild;
        pthread_mutex_unlock(&pidmutex);
        close(childpipe[1]);
        /* 從 pipe 接收 child process 狀態,每次固定要讀 sizeof(int)*2 */
        while(read_size(childpipe[0], (void*)childmsg, readsize) == readsize){
            if(childmsg[0] < 0 || childmsg[0] >= SCTCHILD_MSGMAX){
                /* 會發生嗎?除非被亂七八糟 ptrace 吧 
                 * 這時候先砍掉子程序再說 */
                kill(pidcopy, SIGKILL);
                close(childpipe[0]);
                abort_makechild(SCTMCRVAL_INVALID);
            }else{
                if(childmsg[1]){
                    fprintf(stderr, "子程序[%d]發生錯誤:%s%s\n",
                            pidcopy, childmsg_text[childmsg[0]], 
                            strerror(childmsg[1]));
                    kill(pidcopy, SIGKILL);
                    close(childpipe[0]);
                    abort_makechild(SCTMCRVAL_CHILDFAIL);
                }else{
                    if(mcopt.flags & SCTMC_VERBOSE){
                        printf("子程序[%d]:%s\n", pidcopy, 
                                childmsg_text[childmsg[0]]);
                    }
                }
            }
        }
        clock_gettime(CLOCK_REALTIME, &execstart);
        close(childpipe[0]);

        /* 現在我們確定受測程式已經在執行了,所以需要記錄一下時間
         * 通知其他 thread */
        sem_post(&tlethr);
        sem_post(&dispthr);

        /* 開始要 wait 了,莫名其妙 stop 的程式我最多只會送
         * 5 次 SIGCONT 給你(避免進入無限迴圈) 
         * 這種奇怪的程式就等著 SLE 吧 */
        for(i=0; i<=5; i++){
            wait4(pidcopy, &childexitstat, WUNTRACED, &toreturn->judge_rusage);
            if(WIFSTOPPED(childexitstat) && i!=5){
                if(mcopt.flags & SCTMC_VERBOSE){
                    printf("\n子程序因訊號 %d 而暫停,自動傳送 SIGCONT "
                            "(第 %d 次)\n",
                        WSTOPSIG(childexitstat), i+1);
                }
                kill(pidcopy, SIGCONT);
            }else{
                if(WIFSIGNALED(childexitstat)){
                    clock_gettime(CLOCK_REALTIME, &execfinish);
                    childterminated = 1;
                    toreturn->judge_signal = WTERMSIG(childexitstat);
                    if(toreturn->judge_signal == SIGXFSZ){
                        toreturn->judge_result = SCTRES_OLE;
                    }else{
                        toreturn->judge_result = SCTRES_RE;
                    }
                    toreturn->judge_exitcode = WEXITSTATUS(childexitstat);
                    break;
                }
                if(WIFEXITED(childexitstat)){
                    clock_gettime(CLOCK_REALTIME, &execfinish);
                    childterminated = 1;
                    toreturn->judge_result = SCTRES_OK;
                    toreturn->judge_exitcode = WEXITSTATUS(childexitstat);
                    break;
                }
            }
        }
        if(!childterminated){
            kill(pidcopy, SIGKILL);
            wait4(pidcopy, &childexitstat, WUNTRACED, &toreturn->judge_rusage);
            toreturn->judge_result = SCTRES_SLE;
            toreturn->judge_exitcode = WEXITSTATUS(childexitstat);
            clock_gettime(CLOCK_REALTIME, &execfinish);
        }
        pthread_mutex_lock(&judge_tle_mx);
        if(judge_tle && toreturn->judge_result == SCTRES_RE){
            toreturn->judge_result = SCTRES_TLE;
        }
        pthread_mutex_unlock(&judge_tle_mx);
        difftimespec(&execstart, &execfinish, &toreturn->judge_time);
        if(break_flag){
            toreturn->judge_result = SCTRES_AB;
        }
        sctjudge_makechild_cleanup_p1();
        if(mcopt.flags & SCTMC_VERBOSE){
            /* XXX 有更好的方法洗掉文字嗎? */
            fputs("\r                                                       \r"
                    , stdout);
        }
    }else{
        /* 子程序 
         * 由此開始要很小心,因為我們是在 thread 裡面 fork (?) */
        close(childpipe[0]);
        /* close-on-exec 用來關閉這個暫時的資料傳輸通道 */
        fcntl(childpipe[1], F_SETFD, fcntl(childpipe[1],F_GETFD) | FD_CLOEXEC);

        /* 設為獨立的 process group,不過失敗就算了,這不重要 */
        setpgid(getpid(), getpid());

        /* 重設所有 signal handler
         * XXX 我猜 signal 最大值是 32,還有其他更好的寫法嗎? */
        for(i=1; i<=32; i++){
            signal(i, SIG_DFL);
        }

#ifndef HAVE_CONF_CAP
        disable_setuid();
#endif

        fdinput = open(mcopt.inputfile, O_RDONLY);
        if(fdinput < 0){
            SCTCHILD_WRITE_FAILMSG(SCTCHILD_OPEN_INPUT);
            exit(1);
        }else{
            SCTCHILD_WRITE_SUCCMSG(SCTCHILD_OPEN_INPUT);
        }
        if(dup2(fdinput, STDIN_FILENO) < 0){
            SCTCHILD_WRITE_FAILMSG(SCTCHILD_DUP2_INPUT);
            exit(1);
        }else{
            SCTCHILD_WRITE_SUCCMSG(SCTCHILD_DUP2_INPUT);
        }

        /* 注意:開啟輸出檔會直接覆寫現存檔案! */
        fdoutput = open(mcopt.outputfile, O_WRONLY | O_CREAT | O_TRUNC,
                S_IRUSR | S_IWUSR);
        if(fdoutput < 0){
            SCTCHILD_WRITE_FAILMSG(SCTCHILD_OPEN_OUTPUT);
            exit(1);
        }else{
            SCTCHILD_WRITE_SUCCMSG(SCTCHILD_OPEN_OUTPUT);
        }
        if(dup2(fdoutput, STDOUT_FILENO) < 0){
            SCTCHILD_WRITE_FAILMSG(SCTCHILD_DUP2_OUTPUT);
            exit(1);
        }else{
            SCTCHILD_WRITE_SUCCMSG(SCTCHILD_DUP2_OUTPUT);
        }

#ifndef HAVE_CONF_CAP
        enable_setuid();
#endif
        fchown(fdoutput, procrealuid, -1);

        /* 再來就是 stderr 了 */
        if(mcopt.flags & SCTMC_REDIR_STDERR){
            if(dup2(fdoutput, STDERR_FILENO) < 0){
                SCTCHILD_WRITE_FAILMSG(SCTCHILD_DUP2_STDERR);
                exit(1);
            }else{
                SCTCHILD_WRITE_SUCCMSG(SCTCHILD_DUP2_STDERR);
            }
        }else{
            fderr = open(WITH_NULL, O_RDONLY);
            if(fderr < 0){
                SCTCHILD_WRITE_FAILMSG(SCTCHILD_OPEN_STDERR);
                exit(1);
            }else{
                SCTCHILD_WRITE_SUCCMSG(SCTCHILD_OPEN_STDERR);
            }
            if(dup2(fderr, STDERR_FILENO) < 0){
                SCTCHILD_WRITE_FAILMSG(SCTCHILD_DUP2_STDERR);
                exit(1);
            }else{
                SCTCHILD_WRITE_SUCCMSG(SCTCHILD_DUP2_STDERR);
            }
        }

        /* 很暴力地把所有不該留著的 fd 關掉 */
        for(i=3; i<fdtablesize; i++){
            if(i != childpipe[1]){
                close(i);
            }
        }
        SCTCHILD_WRITE_SUCCMSG(SCTCHILD_CLOSE_OTHERFD);

        /* 有人要求我要 chroot 嗎? */
        if(mcopt.chrootdir != NULL){
            if(chroot(mcopt.chrootdir) < 0){
                SCTCHILD_WRITE_FAILMSG(SCTCHILD_CHROOT);
                exit(1);
            }else{
                chdir("/");
                SCTCHILD_WRITE_SUCCMSG(SCTCHILD_CHROOT);
            }
        }


#ifdef HAVE_CONF_CAP
        /* 確保修改身份的時候 capabilities 仍然留著
         * 等一下再自己把 capabilities 丟掉 */
        prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
#endif      

        /* 我要 setgid 嗎? */
        if(mcopt.flags & SCTMC_SETGID){
            if(setregid(mcopt.gid, mcopt.gid) < 0){
                SCTCHILD_WRITE_FAILMSG(SCTCHILD_SETGID);
                exit(1);
            }else{
                SCTCHILD_WRITE_SUCCMSG(SCTCHILD_SETGID);
            }
            if(setgroups(1, &mcopt.gid) < 0){
                SCTCHILD_WRITE_FAILMSG(SCTCHILD_SETGROUPS);
                exit(1);
            }else{
                SCTCHILD_WRITE_SUCCMSG(SCTCHILD_SETGROUPS);
            }
        }

        /* 我要 setuid 嗎? */
        if(mcopt.flags & SCTMC_SETUID){
            if(setreuid(mcopt.uid, mcopt.uid) < 0){
                SCTCHILD_WRITE_FAILMSG(SCTCHILD_SETUID);
                exit(1);
            }else{
                SCTCHILD_WRITE_SUCCMSG(SCTCHILD_SETUID);
            }
        }
#ifndef HAVE_CONF_CAP
        else{
            /* 確保 setuid 可執行檔所造成的 effective UID 改變不會傳給子程序 */
            setuid(procrealuid);
        }
#endif

        /* 開始設定資源限制 */
        if(mcopt.memlimit > 0){
            childressize = mcopt.memlimit;
            childres.rlim_cur = childressize;
            childres.rlim_max = childressize;
            if(setrlimit(RLIMIT_AS, &childres) < 0){
                SCTCHILD_WRITE_FAILMSG(SCTCHILD_SETRLIMIT_VM);
                exit(1);
            }else{
                SCTCHILD_WRITE_SUCCMSG(SCTCHILD_SETRLIMIT_VM);
            }
        }

        if(mcopt.outlimit > 0){
            childressize = mcopt.outlimit;
            childres.rlim_cur = childressize;
            childres.rlim_max = childressize;
            if(setrlimit(RLIMIT_FSIZE, &childres) < 0){
                SCTCHILD_WRITE_FAILMSG(SCTCHILD_SETRLIMIT_FSIZE);
                exit(1);
            }else{
                SCTCHILD_WRITE_SUCCMSG(SCTCHILD_SETRLIMIT_FSIZE);
            }
        }

        /* 從現在開始,不准再開檔案了 */
        childres.rlim_cur = 0;
        childres.rlim_max = 0;
        if(setrlimit(
#ifdef RLIMIT_NOFILE
                    RLIMIT_NOFILE
#else
                    RLIMIT_OFILE
#endif
                    , &childres) < 0){
            SCTCHILD_WRITE_FAILMSG(SCTCHILD_SETRLIMIT_OPENFILE);
            exit(1);
        }else{
            SCTCHILD_WRITE_SUCCMSG(SCTCHILD_SETRLIMIT_OPENFILE);
        }

        /* 從現在開始,不可以再產生新程序了 */
        if(setrlimit(RLIMIT_NPROC, &childres) < 0){
            SCTCHILD_WRITE_FAILMSG(SCTCHILD_SETRLIMIT_PROC);
            exit(1);
        }else{
            SCTCHILD_WRITE_SUCCMSG(SCTCHILD_SETRLIMIT_PROC);
        }
        

        /* 非 Linux 系統就到此結束了,可以 exec 了 */

#ifdef HAVE_CONF_CAP
        /* 正在丟掉 capabilities */
        cap_t nocap = cap_init();
        cap_clear_flag(nocap, CAP_EFFECTIVE);
        cap_clear_flag(nocap, CAP_INHERITABLE);
        cap_clear_flag(nocap, CAP_PERMITTED);
        if(cap_set_proc(nocap) < 0){
            SCTCHILD_WRITE_FAILMSG(SCTCHILD_CAPSET);
            exit(1);
        }else{
            SCTCHILD_WRITE_SUCCMSG(SCTCHILD_CAPSET);
        }
        /* 需要 drop bound 嗎?再考慮看看 */
#endif

        if(mcopt.chrootdir == NULL){
            execl(mcopt.executable, mcopt.executable, NULL);
        }else{
            if(mcopt.flags & SCTMC_NOCOPY){
                execl(mcopt.executable, mcopt.executable, NULL);
            }else{
                execl(execshortname, execshortname, NULL);
            }
        }

        SCTCHILD_WRITE_FAILMSG(SCTCHILD_EXEC);
        exit(2);
    }
    /* Judge 完成,準備離開 */
    fflush(stdout);
    sctjudge_makechild_cleanup_p2(&mcopt, chrdir_oldmode, execdestname);
    fflush(stdout);
    (toreturn->mc_exitcode) = SCTMCRVAL_SUCCESS;
    return (void*)toreturn;
}
#endif