aboutsummaryrefslogtreecommitdiffstats
path: root/toj/center/src/judgm_lib.h
blob: 4aa02fa6bda0d93de9ec761dc5bd1cd7e77335a7 (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
#include<string.h>
#include<limits.h>
#include<unistd.h>
#include<signal.h>
#include<limits.h>
#include<errno.h>
#include<pthread.h>
#include<semaphore.h>
#include<fcntl.h>
#include<sys/ioctl.h>
#include<sys/resource.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<sys/mman.h>
#include<map>
#include<utility>

#include"judgk_com.h"

typedef int (*judgm_proc_check_fn)();

class judgm_proc{
private:
    int init(){
        int i;
        int j;
        struct stat st;

        if(stat(exe_path,&st)){
            return -1;
        }
        if(!S_ISREG(st.st_mode)){
            return -1;
        }

        exe_name[NAME_MAX] = '\0';
        for(i = 0,j = 0;exe_path[i] != '\0' && j < NAME_MAX;i++){
            if(exe_path[i] == '/'){
                j = 0;
            }else{
                exe_name[j] = exe_path[i];
                j++;
            }
        }
        exe_name[j] = '\0';

        pid = 0;
        kern_task = 0;
        status = JUDGE_WAIT;
        runtime = 0;
        memory = 0;

        return 0;
    }
    int protect(){
        rlimit limit;
        judgk_com_proc_add com_proc_add;

        limit.rlim_cur = 1;
        limit.rlim_max = limit.rlim_cur;
        prlimit(pid,RLIMIT_NPROC,&limit,NULL);

        limit.rlim_cur = 8L;
        limit.rlim_max = limit.rlim_cur;
        prlimit(pid,RLIMIT_NOFILE,&limit,NULL);

        com_proc_add.run_path[0] = '\0';
        strncat(com_proc_add.run_path,run_path,sizeof(com_proc_add.run_path));
        com_proc_add.pid = pid;
        com_proc_add.timelimit = timelimit * 1000L;
        com_proc_add.hardtimelimit = hardtimelimit * 1000L;
        com_proc_add.memlimit = memlimit * 1024L + 4096L * 128L;
        if(ioctl(judgk_modfd,IOCTL_PROC_ADD,&com_proc_add)){
            return -1;
        }
        kern_task = com_proc_add.kern_task;

        return 0;
    }

public:
    int judgk_modfd;
    char run_path[PATH_MAX + 1];
    char exe_path[PATH_MAX + 1];
    char exe_name[NAME_MAX + 1];
    unsigned long timelimit;
    unsigned long hardtimelimit;
    unsigned long memlimit;
    judgm_proc_check_fn check_fn;

    pid_t pid;
    unsigned long kern_task;
    int status;
    unsigned long runtime;
    unsigned long memory;

    judgm_proc(int judgk_modfd,char *runpath,char *exe_path,unsigned long timelimit,unsigned long hardtimelimit,unsigned long memlimit,judgm_proc_check_fn check_fn){
        this->judgk_modfd = judgk_modfd;
        this->run_path[0] = '\0';
        strncat(this->run_path,runpath,sizeof(this->run_path));
        this->exe_path[0] = '\0';
        strncat(this->exe_path,exe_path,sizeof(this->exe_path));

        this->timelimit = timelimit;
        this->hardtimelimit = hardtimelimit;
        this->memlimit = memlimit;
        this->check_fn = check_fn;
    }
    
    int proc_run(){
        char abspath[PATH_MAX + 1];

        if(init()){
            return -1;
        }

        realpath(exe_path,abspath);
        if((pid = fork()) == 0){
            char *argv[] = {NULL,NULL};
            char *envp[] = {NULL};

            chdir(run_path);
            check_fn();

            setgid(99);
            setuid(99);
            kill(getpid(),SIGSTOP);
            
            argv[0] = exe_name;
            execve(abspath,argv,envp);
            exit(0);
        }

        if(pid == -1){
            return -1;
        }
        waitpid(pid,NULL,WUNTRACED);

        if(protect()){
            kill(pid,SIGKILL);
            return -1;
        }
        status = JUDGE_RUN;
        kill(pid,SIGCONT);
        
        return 0;
    }
    int proc_wait(bool blockflag){
        int wstatus;
        struct judgk_com_proc_get com_proc_get;

        if(blockflag == true){
            if(waitpid(pid,&wstatus,WUNTRACED) == -1){
                return -1;
            }
        }else{
            if(waitpid(pid,&wstatus,WUNTRACED | WNOHANG) <= 0){
                return -1;
            }
        }

        com_proc_get.kern_task = kern_task;
        if(ioctl(judgk_modfd,IOCTL_PROC_GET,&com_proc_get)){
            return -1;
        }

        runtime = com_proc_get.runtime / 1000L;
        memory = com_proc_get.memory;

        printf("runtime:%lu memory:%lu\n",runtime,memory);

        if(com_proc_get.status != JUDGE_AC){
            status = com_proc_get.status;
        }else if(memory > (memlimit * 1024L)){
            status = JUDGE_MLE;
        }else if(runtime > timelimit){
            status = JUDGE_TLE;
        }else if(WIFEXITED(wstatus) || (WIFSIGNALED(wstatus) && WTERMSIG(wstatus) == SIGKILL)){
            status = JUDGE_AC;
        }else{
            status = JUDGE_RE;
        }

        return 0;
    }
    int proc_kill(){
        if(kill(pid,SIGKILL)){
            return -1;
        }
        return 0;
    }
};

class judgm_hyperio{
private:
    int judgk_modfd;
    char *read_buf;
    off_t read_off;

public:
    int tty_idx;

    judgm_hyperio(int judgk_modfd){
        this->judgk_modfd = judgk_modfd;
        this->tty_idx = ioctl(this->judgk_modfd,IOCTL_HYPERIO_ADD,0);
        this->read_buf = (char*)mmap(NULL,JUDGK_COM_HYPERIO_BUFSIZE,PROT_READ,MAP_SHARED,judgk_modfd,0);
        this->read_off = 0;
    }
    ~judgm_hyperio(){
        munmap(read_buf,JUDGK_COM_HYPERIO_BUFSIZE);
        ioctl(judgk_modfd,IOCTL_HYPERIO_DEL,0);
    }

    static int get_ttyfd(int idx){
        char tpath[PATH_MAX + 1];
        
        snprintf(tpath,sizeof(tpath),"/dev/jtty%d",idx);
        return open(tpath,O_RDWR);
    }
    size_t wait(){
        return ioctl(judgk_modfd,IOCTL_HYPERIO_READ,0);
    }
    int compare(char *buf,size_t len){
        int flag;
        size_t remain;
        off_t off;
        size_t data_len;
        size_t cmp_len;

        flag = 0;
        remain = len;
        off = 0;
        data_len = 0;
        cmp_len = 0;
        while(remain > 0 && flag == 0){
            if(data_len == 0){
                if((data_len = ioctl(judgk_modfd,IOCTL_HYPERIO_READ,cmp_len)) <= 0){
                    return -1;
                }
            }
            if(remain < data_len){
                cmp_len = remain;
            }else{
                cmp_len = data_len;
            }

            if((cmp_len + read_off) < JUDGK_COM_HYPERIO_BUFSIZE){
                flag |= memcmp(read_buf + read_off,buf + off,cmp_len);
                read_off += cmp_len;
            }else{
                flag |= memcmp(read_buf + read_off,buf + off,JUDGK_COM_HYPERIO_BUFSIZE - read_off);
                flag |= memcmp(read_buf,buf + off + (JUDGK_COM_HYPERIO_BUFSIZE - read_off),(cmp_len + read_off) - JUDGK_COM_HYPERIO_BUFSIZE);
                read_off = (cmp_len + read_off) - JUDGK_COM_HYPERIO_BUFSIZE;
            }
            remain -= cmp_len;
            off += cmp_len;
            data_len -= cmp_len;
        }
        if(cmp_len > 0){
            ioctl(judgk_modfd,IOCTL_HYPERIO_READ,-(long)cmp_len);
        }

        if(flag == 0){
            return 0;
        }else{
            return -1;
        }
    }
};

static int judgm_compile(int subid,char *code_path,char *exe_path,int lang,bool force_flag,char *err_msg,size_t err_len){
    int ret;
    int i;

    char main_path[PATH_MAX + 1];
    char sem_path[PATH_MAX + 1];
    struct stat st;
    sem_t *wait_sem;
    bool ce_flag;
    char dir_path[PATH_MAX + 1];
    char *out_path;
    int io[2];
    int pid;
    int wstatus;
    off_t off;
    
    if(force_flag == false){
        snprintf(main_path,sizeof(main_path),"tmp/exe/%d/main",subid);
        snprintf(sem_path,sizeof(sem_path),"/judgm_compile_wait_%d",subid);
        if((wait_sem = sem_open(sem_path,0)) == SEM_FAILED){
            if(stat(main_path,&st)){
                if((wait_sem = sem_open(sem_path,O_CREAT | O_EXCL,0644,0)) != SEM_FAILED){
                    out_path = main_path;
                    goto compile;
                }else if((wait_sem = sem_open(sem_path,0)) != SEM_FAILED){

                    sem_wait(wait_sem);
    
                    sem_close(wait_sem);
                }
            }
        }else{

            sem_wait(wait_sem);

            sem_close(wait_sem);
        }

        if(!link(main_path,exe_path)){
            return 0;
        }
    }

    force_flag = true;
    out_path = exe_path;

compile:

    if(force_flag == false){
        snprintf(dir_path,sizeof(dir_path),"tmp/exe/%d",subid);
        mkdir(dir_path,0755);
    }
    ce_flag = false;

    if(lang == JUDGE_CPP){
        pipe(io);
        
        if((pid = fork()) == 0){
            char *argv[] = {"g++","-static","-O2",code_path,"-std=c++0x","-o",out_path,NULL};

            dup2(io[1],1);
            dup2(io[1],2);
            execvp("g++",argv);
        }

        close(io[1]);
        off = 0;
        while((ret = read(io[0],err_msg + off,err_len - off - 1)) > 0){
            off += ret;
        }
        err_msg[off] = '\0';
        close(io[0]);

        waitpid(pid,&wstatus,0);
        if(wstatus != 0){
            ce_flag = true;
        }
    }

    if(force_flag == false){
        if(ce_flag == true){
            rmdir(dir_path);
        }

        for(i = 0;i < 8;i++){
            sem_post(wait_sem);
        }
        sem_close(wait_sem);
        sem_unlink(sem_path);
    }
    if(ce_flag == true){
        return -1;
    }

    link(main_path,exe_path);

    return 0;
}