aboutsummaryrefslogtreecommitdiffstats
path: root/toj/center/src/judge_manage.cpp
blob: 32181bca43f5aea2507345dffc443a36ad093f3e (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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<limits.h>
#include<dirent.h>
#include<dlfcn.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/mman.h>
#include<map>

#include"tpool.h"
#include"judge_def.h"
#include"judge.h"
#include"judgm_line.h"
#include"judgm_lib.h"
#include"judge_manage.h"

int judge_manage_init(){
    int i;

    manage_tp = new tpool(JUDGE_THREAD_MAX);
    judge_server_addtpool(manage_tp);
    manage_tp->start();

    manage_judgk_modfd = open("/dev/judgk",O_RDWR);
    for(i = 0;i < JUDGE_THREAD_JUDGEMAX;i++){
        manage_judgepool[i] = new manage_judgeth_info(i);
    }
    manage_updatepro_thfn = new tpool_static_fn(manage_updatepro_th);
    manage_updatepro_cbfn = new tpool_static_fn(manage_updatepro_cb);
    manage_unpackcode_thfn = new tpool_static_fn(manage_unpackcode_th);
    manage_unpackcode_cbfn = new tpool_static_fn(manage_unpackcode_cb);
    manage_judge_thfn = new tpool_static_fn(manage_judge_th);
    manage_judge_cbfn = new tpool_static_fn(manage_judge_cb);

    judge_manage_updatedata();
    return 0;
}
int judge_manage_updatedata(){
    DIR *dirp;
    char *buf;
    dirent *entry;
    int proid;
    int cacheid;
    judge_pro_info *pro_info;

    if((dirp = opendir("tmp/pro")) == NULL){
        return -1;
    }
    buf = new char[sizeof(dirent) + NAME_MAX + 1];
    
    while(true){
        readdir_r(dirp,(dirent*)buf,&entry); 
        if(entry == NULL){
            break;
        }
        if(strcmp(entry->d_name,".") == 0 || strcmp(entry->d_name,"..") == 0){
            continue;
        }

        if(entry->d_type == DT_DIR){
            sscanf(entry->d_name,"%d_%d",&proid,&cacheid); 
            pro_info = new judge_pro_info(proid,cacheid);
            judge_manage_getpro(pro_info);
            judge_manage_promap.insert(std::make_pair(proid,pro_info));
        }
    }

    delete(buf);
    closedir(dirp);

    return 0;
}

judge_pro_info* judge_manage_getprobyid(int proid){
    std::map<int,judge_pro_info*>::iterator pro_it;
    judge_pro_info *pro_info;

    if((pro_it = judge_manage_promap.find(proid)) == judge_manage_promap.end()){
        return NULL;
    }
    pro_info = pro_it->second;

    if(judge_manage_getpro(pro_info)){
        return NULL;
    }

    return pro_info;
}
int judge_manage_getpro(judge_pro_info *pro_info){
    pro_info->ref_count++;
    return 0;
}
int judge_manage_putpro(judge_pro_info *pro_info){
    std::map<int,judge_pro_info*> pro_it;
    char tpath[PATH_MAX + 1];
    char src_path[PATH_MAX + 1];

    pro_info->ref_count--;
    if(pro_info->ref_count == 0){
        snprintf(tpath,sizeof(tpath),"tmp/pro/%d_%d",pro_info->proid,pro_info->cacheid);
        tool_cleardir(tpath);
        rmdir(tpath);

        delete pro_info;
    }
    return 0;
}
int judge_manage_updatepro(int proid,int cacheid,bool check_flag,judge_pro_info **update_pro_info){ //If check_flag = true, just check
    int ret;

    std::map<int,judge_pro_info*>::iterator pro_it;
    judge_pro_info *old_pro_info;

    if((pro_it = judge_manage_promap.find(proid)) == judge_manage_promap.end()){
        if(check_flag == true){
            return 0;
        }
    }else{
        old_pro_info = pro_it->second;

        if(old_pro_info->state == JUDGE_CACHESTATE_READY && cacheid == old_pro_info->cacheid){
            return 1;
        }
        if(old_pro_info->state == JUDGE_CACHESTATE_UPDATE && (cacheid <= old_pro_info->cacheid || cacheid <= old_pro_info->update_cacheid)){
            return -1;
        }

        if(check_flag == true){
            return 0;
        }

        old_pro_info->update_cacheid = cacheid; 
        old_pro_info->state = JUDGE_CACHESTATE_UPDATE;
    }

    *update_pro_info = new judge_pro_info(proid,cacheid);
    judge_manage_getpro(*update_pro_info);

    return 0;
}
int judge_manage_done_updatepro(judge_pro_info *pro_info){
    manage_tp->add(manage_updatepro_thfn,pro_info,manage_updatepro_cbfn,pro_info);
    return 0;
}
static void manage_updatepro_th(void *data){
    judge_pro_info *pro_info;
    int proid;
    int cacheid;
    char pack_path[PATH_MAX + 1];
    char dir_path[PATH_MAX + 1];
    char tpath[PATH_MAX + 1];
    FILE *f;

    pro_info = (judge_pro_info*)data;

    snprintf(pack_path,sizeof(pack_path),"tmp/propack/%d_%d.tar.bz2",pro_info->proid,pro_info->cacheid);
    snprintf(dir_path,sizeof(dir_path),"tmp/pro/%d_%d",pro_info->proid,pro_info->cacheid);
    mkdir(dir_path,0755);
    tool_cleardir(dir_path);
    tool_unpack(pack_path,dir_path);
    //unlink(pack_path);

    snprintf(tpath,sizeof(tpath),"tmp/pro/%d_%d/cacheinfo",pro_info->proid,pro_info->cacheid);
    f = fopen(tpath,"w");
    fprintf(f,"%d",pro_info->cacheid);
    fclose(f);
}
static void manage_updatepro_cb(void *data){
    judge_pro_info *old_pro_info;
    judge_pro_info *update_pro_info;
    std::pair<std::map<int,judge_pro_info*>::iterator,bool> ins_ret;
    std::vector<std::pair<int,int> > pro_list;

    update_pro_info = (judge_pro_info*)data;
    
    ins_ret = judge_manage_promap.insert(std::make_pair(update_pro_info->proid,update_pro_info));
    if(ins_ret.second == false){
        old_pro_info = ins_ret.first->second;

        if(update_pro_info->cacheid < old_pro_info->cacheid){
            judge_manage_putpro(update_pro_info);
            return;
        }

        judge_manage_putpro(ins_ret.first->second);
        ins_ret.first->second = update_pro_info;
    }

    pro_list.push_back(std::make_pair(update_pro_info->proid,update_pro_info->cacheid));
    judge_server_setpro(pro_list);
}


int judge_manage_submit(int subid,int proid,int lang,char *set_data,int set_len){
    judge_pro_info *pro_info;
    judge_submit_info *sub_info;

    char tpath[PATH_MAX + 1];
    struct stat st;

    pro_info = judge_manage_getprobyid(proid); 
    sub_info = new judge_submit_info(subid,pro_info,lang,set_data,set_len);

    if(manage_submap.find(subid) == manage_submap.end()){
        snprintf(tpath,sizeof(tpath),"tmp/code/%d",subid);
        if(!stat(tpath,&st)){
            manage_queuejudge(sub_info);        
        }else{
            judge_server_reqcode(subid);
            manage_submap.insert(std::make_pair(subid,sub_info));
        }
    }else{
        manage_submap.insert(std::make_pair(subid,sub_info));
    }

    return 0;
}
int judge_manage_done_code(int subid){
    manage_tp->add(manage_unpackcode_thfn,(void*)((long)subid),manage_unpackcode_cbfn,(void*)((long)subid));
    return 0;
}
static void manage_unpackcode_th(void *data){
    int subid;
    char pack_path[PATH_MAX + 1];
    char dir_path[PATH_MAX + 1];
    char tpath[PATH_MAX + 1];
    FILE *f;

    subid = (int)((long)data);

    snprintf(pack_path,sizeof(pack_path),"tmp/codepack/%d.tar.bz2",subid);
    snprintf(dir_path,sizeof(dir_path),"tmp/code/%d",subid);
    mkdir(dir_path,0755);
    tool_cleardir(dir_path);
    tool_unpack(pack_path,dir_path);
}
static void manage_unpackcode_cb(void *data){
    int subid;
    std::multimap<int,judge_submit_info*>::iterator sub_it;
    judge_submit_info *sub_info;

    subid = (int)((long)data);

    while((sub_it = manage_submap.find(subid)) != manage_submap.end()){
        sub_info = sub_it->second;
        manage_queuejudge(sub_info);    
        manage_submap.erase(sub_it);
    }
}
static int manage_queuejudge(judge_submit_info *sub_info){
    int i;

    printf("queue judge %d %d\n",sub_info->subid,sub_info->pro_info->proid);
    for(i = 0;i < 16;i++){
        if(manage_judgepool[i]->use_flag == false){
            manage_judgepool[i]->use_flag = true;
            manage_judgepool[i]->sub_info = sub_info;
            manage_tp->add(manage_judge_thfn,manage_judgepool[i],manage_judge_cbfn,manage_judgepool[i]);
            break;
        }
    }

    return 0;
}
static void manage_judge_th(void *data){
    manage_judgeth_info *th_info;
    judge_submit_info *sub_info;
    judge_pro_info *pro_info;
    char pro_path[PATH_MAX + 1];
    char code_path[PATH_MAX + 1];
    char *set_data;

    th_info = (manage_judgeth_info*)data;
    sub_info = th_info->sub_info;
    pro_info = sub_info->pro_info;

    snprintf(pro_path,sizeof(pro_path),"tmp/pro/%d_%d",pro_info->proid,pro_info->cacheid);
    snprintf(code_path,sizeof(code_path),"tmp/code/%d",sub_info->subid);
    manage_judge(sub_info->subid,pro_path,code_path,th_info->run_path,sub_info->lang,sub_info->set_data,th_info->res_data,th_info->res_len);
}
static void manage_judge_cb(void *data){
    manage_judgeth_info *th_info;
    judge_submit_info *sub_info;
    std::vector<std::pair<int,int> > pro_list;

    th_info = (manage_judgeth_info*)data;
    sub_info = th_info->sub_info;

    judge_server_result(sub_info->subid,th_info->res_data,th_info->res_len);
    judge_manage_putpro(sub_info->pro_info);

    th_info->use_flag = false;
    th_info->sub_info = NULL;
    delete sub_info;
}
static int manage_judge(int subid,char *pro_path,char *code_path,char *run_path,int lang,char *set_data,char *res_data,size_t &res_len){
    judgm_line_info *line_info;
    int pid;

    char tpath[PATH_MAX + 1];
    FILE *set_file;
    char cwd_path[PATH_MAX + 1];
    char jmod_name[NAME_MAX + 1];
    char line_path[PATH_MAX + 1];
    char check_name[NAME_MAX + 1];
    char check_path[PATH_MAX + 1];
    char lchr;
    char tchr;

    void *line_dll;
    void *check_dll;
    judgm_line_run_fn run_fn;

    snprintf(tpath,sizeof(tpath),"%s/setting",pro_path);
    set_file = fopen(tpath,"r");

    getcwd(cwd_path,sizeof(cwd_path));
    fscanf(set_file,"%s",jmod_name);
    snprintf(line_path,sizeof(line_path),"%s/tmp/jmod/%s/%s_line.so",cwd_path,jmod_name,jmod_name);
    fscanf(set_file,"%s",check_name);
    if(check_name[0] == '/'){
        snprintf(check_path,sizeof(check_path),"%s/%s/private%s.so",cwd_path,pro_path,check_name);
    }else{
        snprintf(check_path,sizeof(check_path),"%s/tmp/jmod/%s/%s.so",cwd_path,jmod_name,check_name);
    }

    lchr = '\n';
    while((tchr = fgetc(set_file)) != EOF){
        if(lchr == '\n' && tchr == '='){
            while(fgetc(set_file) != '\n');
            break;
        }
        lchr = tchr;
    }

    line_dll = dlopen(line_path,RTLD_NOW);
    check_dll = dlopen(check_path,RTLD_NOW);

    line_info = (judgm_line_info*)mmap(NULL,sizeof(struct judgm_line_info),PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS,-1,0);
    line_info->subid = subid;
    line_info->pro_path = pro_path;
    line_info->code_path = code_path;
    line_info->run_path = run_path;

    line_info->judgk_modfd = manage_judgk_modfd;
    line_info->line_dll = line_dll;
    line_info->check_dll = check_dll;

    line_info->lang = lang;
    line_info->set_file = set_file;
    line_info->set_data = set_data;

    tool_cleardir(line_info->run_path);

    run_fn = (judgm_line_run_fn)dlsym(line_dll,"run");
    if((pid = fork()) == 0){
        run_fn(line_info);
        exit(0);
    }
    waitpid(pid,NULL,0);

    memcpy(res_data,line_info->res_data,line_info->res_len);
    res_len = line_info->res_len;

    munmap(line_info,sizeof(judgm_line_info));
    fclose(set_file);
    return 0;
}