-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrunner.c
143 lines (122 loc) · 4.52 KB
/
runner.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
#define _GNU_SOURCE
#define _POSIX_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sched.h>
#include <signal.h>
#include <pthread.h>
#include <errno.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/types.h>
#include "runner.h"
#include "killer.h"
#include "child.h"
#include "logger.h"
void init_result(struct result *_result) {
_result->result = _result->error = SUCCESS;
_result->cpu_time = _result->real_time = _result->signal = _result->exit_code = 0;
_result->memory = 0;
}
void run(struct config *_config, struct result *_result) {
// init log fp
FILE *log_fp = log_open(_config->log_path);
// init result
init_result(_result);
// check whether current user is root
uid_t uid = getuid();
if (uid != 0) {
ERROR_EXIT(ROOT_REQUIRED);
}
// check args
if ((_config->max_cpu_time < 1 && _config->max_cpu_time != UNLIMITED) ||
(_config->max_real_time < 1 && _config->max_real_time != UNLIMITED) ||
(_config->max_stack < 1) ||
(_config->max_memory < 1 && _config->max_memory != UNLIMITED) ||
(_config->max_process_number < 1 && _config->max_process_number != UNLIMITED) ||
(_config->max_output_size < 1 && _config->max_output_size != UNLIMITED)) {
ERROR_EXIT(INVALID_CONFIG);
}
// record current time
struct timeval start, end;
gettimeofday(&start, NULL);
pid_t child_pid = fork();
// pid < 0 shows clone failed
if (child_pid < 0) {
ERROR_EXIT(FORK_FAILED);
}
else if (child_pid == 0) {
child_process(log_fp, _config);
}
else if (child_pid > 0){
// create new thread to monitor process running time
pthread_t tid = 0;
if (_config->max_real_time != UNLIMITED) {
struct timeout_killer_args killer_args;
killer_args.timeout = _config->max_real_time;
killer_args.pid = child_pid;
if (pthread_create(&tid, NULL, timeout_killer, (void *) (&killer_args)) != 0) {
kill_pid(child_pid);
ERROR_EXIT(PTHREAD_FAILED);
}
}
int status;
struct rusage resource_usage;
// wait for child process to terminate
// on success, returns the process ID of the child whose state has changed;
// On error, -1 is returned.
if (wait4(child_pid, &status, WSTOPPED, &resource_usage) == -1) {
kill_pid(child_pid);
ERROR_EXIT(WAIT_FAILED);
}
// get end time
gettimeofday(&end, NULL);
_result->real_time = (int) (end.tv_sec * 1000 + end.tv_usec / 1000 - start.tv_sec * 1000 - start.tv_usec / 1000);
// process exited, we may need to cancel timeout killer thread
if (_config->max_real_time != UNLIMITED) {
if (pthread_cancel(tid) != 0) {
// todo logging
};
}
if (WIFSIGNALED(status) != 0) {
_result->signal = WTERMSIG(status);
}
if(_result->signal == SIGUSR1) {
_result->result = SYSTEM_ERROR;
}
else {
_result->exit_code = WEXITSTATUS(status);
_result->cpu_time = (int) (resource_usage.ru_utime.tv_sec * 1000 +
resource_usage.ru_utime.tv_usec / 1000);
_result->memory = resource_usage.ru_maxrss * 1024;
if (_result->exit_code != 0) {
_result->result = RUNTIME_ERROR;
}
if (_result->signal == SIGSEGV) {
if (_config->max_memory != UNLIMITED && _result->memory > _config->max_memory) {
_result->result = MEMORY_LIMIT_EXCEEDED;
}
else {
_result->result = RUNTIME_ERROR;
}
}
else {
if (_result->signal != 0) {
_result->result = RUNTIME_ERROR;
}
if (_config->max_memory != UNLIMITED && _result->memory > _config->max_memory) {
_result->result = MEMORY_LIMIT_EXCEEDED;
}
if (_config->max_real_time != UNLIMITED && _result->real_time > _config->max_real_time) {
_result->result = REAL_TIME_LIMIT_EXCEEDED;
}
if (_config->max_cpu_time != UNLIMITED && _result->cpu_time > _config->max_cpu_time) {
_result->result = CPU_TIME_LIMIT_EXCEEDED;
}
}
}
log_close(log_fp);
}
}