aboutsummaryrefslogtreecommitdiffstats
path: root/src/ProcCheckTime.c
blob: 9efbe0579c3be0a79046d6a37fc6ff7567c443cb (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
#ifdef HAVE_CONFIG_H
# include "SctConfig.h"
#endif

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

#include <string.h>
#include <unistd.h>
#include <signal.h>

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

volatile sig_atomic_t sctproc_abort;

static void break_handler(int signo){
    sctproc_abort = 1;
    sem_post(&checktime_sem);
}

void* sctproc_checktime(void* arg){
    PROCINFO* procinfo = arg;
    struct sigaction break_catch;
    struct timespec timelimit, timeinit, timecur, timeexpire;
    long long sleeptime = (long long)(procinfo->i_limit_time) * 1000000;

    pthread_mutex_lock(&checktime_mutex);
    checktime_exist = 1;
    pthread_mutex_unlock(&checktime_mutex);

    clock_gettime(CLOCK_REALTIME, &timeinit);

#ifndef HAVE_CONF_CAP
    enable_setuid();
#endif

    sctproc_abort = 0;
    memset(&break_catch, 0, sizeof(break_catch));
    break_catch.sa_handler = &break_handler;
    sigaction(SIGINT, &break_catch, NULL);
    sigaction(SIGTERM, &break_catch, NULL);

    timelimit.tv_sec = timeinit.tv_sec + sleeptime / 1000000000;
    timelimit.tv_nsec = timeinit.tv_nsec + sleeptime % 1000000000;
    checktimespec(&timelimit);

    do{
        clock_gettime(CLOCK_REALTIME, &timecur);
        timeexpire.tv_sec = timecur.tv_sec;
        timeexpire.tv_nsec = timecur.tv_nsec + SCTJUDGE_PROC_CHECKTIME_INTERVAL;
        checktimespec(&timeexpire);
    }while(comparetimespec(&timecur, &timelimit) < 0 && 
        sem_timedwait(&checktime_sem, &timeexpire));

    if(!sctproc_abort){
        pthread_mutex_lock(&sctproc_tle_mutex);
        sctproc_tle = 1;
        pthread_mutex_unlock(&sctproc_tle_mutex);
    }

    kill(child_pid, SIGKILL);

    pthread_mutex_lock(&checktime_mutex);
    checktime_exist = 0;
    pthread_mutex_unlock(&checktime_mutex);
    return NULL;
}