2022-10-09 00:06:07 +08:00
|
|
|
#include "err.h"
|
2023-12-27 23:40:11 +08:00
|
|
|
#include "library.h"
|
2022-10-09 00:06:07 +08:00
|
|
|
#include "resource.h"
|
|
|
|
#include "utils/log.h"
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2023-12-28 00:58:15 +08:00
|
|
|
char *config[CFG_IS_VALID + 1];
|
2022-10-09 00:06:07 +08:00
|
|
|
|
|
|
|
void print_help(char *self) {
|
|
|
|
LOG_WARN("Usage:");
|
|
|
|
LOG_WARN(" %s [options]", self);
|
|
|
|
LOG_WARN("Options:");
|
|
|
|
LOG_WARN(" --memory_limit memory limit in MB");
|
|
|
|
LOG_WARN(" --nproc_limit number of processes limit");
|
|
|
|
LOG_WARN(" --time_limit time limit in ms");
|
|
|
|
LOG_WARN(" --sandbox_template sandbox template");
|
|
|
|
LOG_WARN(" --sandbox_action sandbox action");
|
2024-01-01 21:31:54 +08:00
|
|
|
LOG_WARN(" --uid user id");
|
|
|
|
LOG_WARN(" --gid group id");
|
2022-10-09 00:06:07 +08:00
|
|
|
LOG_WARN(" --file_input path to input file");
|
|
|
|
LOG_WARN(" --file_output path to output file");
|
|
|
|
LOG_WARN(" --file_info path to info file");
|
|
|
|
LOG_WARN(" --program program to run");
|
|
|
|
LOG_WARN(" --help print this help message");
|
|
|
|
}
|
|
|
|
|
|
|
|
void parse(int argc, char *argv[]) {
|
2022-10-11 16:35:55 +08:00
|
|
|
static struct option options[] = {
|
2023-12-28 00:58:15 +08:00
|
|
|
[CFG_MEMORY_LIMIT] = {"memory_limit", required_argument, NULL, 0},
|
|
|
|
[CFG_NPROC_LIMIT] = {"nproc_limit", required_argument, NULL, 0},
|
|
|
|
[CFG_TIME_LIMIT] = {"time_limit", required_argument, NULL, 0},
|
|
|
|
[CFG_SANDBOX_TEMPLATE] = {"sandbox_template", required_argument, NULL, 0},
|
|
|
|
[CFG_SANDBOX_ACTION] = {"sandbox_action", required_argument, NULL, 0},
|
2024-01-01 21:31:54 +08:00
|
|
|
[CFG_UID] = {"uid", optional_argument, NULL, 0},
|
|
|
|
[CFG_GID] = {"gid", optional_argument, NULL, 0},
|
2023-12-28 00:58:15 +08:00
|
|
|
[CFG_FILE_INPUT] = {"file_input", required_argument, NULL, 0},
|
|
|
|
[CFG_FILE_OUTPUT] = {"file_output", required_argument, NULL, 0},
|
|
|
|
[CFG_FILE_INFO] = {"file_info", required_argument, NULL, 0},
|
|
|
|
[CFG_PROGRAM] = {"program", required_argument, NULL, 0},
|
|
|
|
[CFG_IS_VALID] = {"help", no_argument, NULL, 0},
|
|
|
|
[CFG_IS_VALID + 1] = {NULL, 0, NULL, 0}
|
2022-10-09 00:06:07 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
int c, idx = 0;
|
2022-10-11 16:35:55 +08:00
|
|
|
while ((c = getopt_long_only(argc, argv, "", options, &idx)) != -1) {
|
2022-10-09 00:06:07 +08:00
|
|
|
if (c != 0) break;
|
|
|
|
|
2023-12-28 00:58:15 +08:00
|
|
|
if (idx < CFG_IS_VALID)
|
2022-10-11 16:35:55 +08:00
|
|
|
config[idx] = optarg;
|
2023-12-28 00:58:15 +08:00
|
|
|
else if (idx == CFG_IS_VALID) {
|
2022-10-09 00:06:07 +08:00
|
|
|
print_help(argv[0]);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-28 00:58:15 +08:00
|
|
|
for (int i = 0; i < CFG_IS_VALID; i++) {
|
2024-01-01 21:31:54 +08:00
|
|
|
if (!config[i] && options[i].has_arg == required_argument) {
|
2022-10-09 00:06:07 +08:00
|
|
|
print_help(argv[0]);
|
|
|
|
LOG_ERR("Missing arguments");
|
|
|
|
exit(ERR_ARGUMENTS);
|
|
|
|
}
|
|
|
|
}
|
2023-12-27 23:40:11 +08:00
|
|
|
|
2023-12-28 00:58:15 +08:00
|
|
|
config[CFG_IS_VALID] = (char *)1;
|
2022-10-09 00:06:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void launch_child() {
|
2023-12-28 00:58:15 +08:00
|
|
|
char *args[] = {config[CFG_PROGRAM], NULL};
|
|
|
|
|
|
|
|
/* build stdin */ {
|
|
|
|
int fd = open(config[CFG_FILE_INPUT], O_RDONLY);
|
|
|
|
dup2(fd, STDIN_FILENO);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* build stdout */ {
|
|
|
|
int fd = open(config[CFG_FILE_OUTPUT], O_WRONLY | O_CREAT, 0644);
|
|
|
|
dup2(fd, STDOUT_FILENO);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
setup_all();
|
|
|
|
|
2023-12-28 01:10:14 +08:00
|
|
|
execve(config[CFG_PROGRAM], args, NULL);
|
2022-10-09 00:06:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void dump_info(FILE *dest, struct rusage *usage, int status, long long time_usage) {
|
|
|
|
fprintf(dest, "{\"real_time\":%lld,\"cpu_time\":%ld,\"memory\":%ld,", time_usage,
|
|
|
|
usage->ru_utime.tv_sec * 1000 + usage->ru_utime.tv_usec / 1000, usage->ru_maxrss);
|
|
|
|
if (WIFEXITED(status))
|
|
|
|
fprintf(dest, "\"status\":\"exited\",\"code\":%d}", WEXITSTATUS(status));
|
|
|
|
else if (WIFSIGNALED(status))
|
|
|
|
fprintf(dest, "\"status\":\"killed\",\"code\":%d}", WTERMSIG(status));
|
|
|
|
else
|
|
|
|
fprintf(dest, "\"status\":\"unknown\",\"code\":0}");
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
parse(argc, argv);
|
|
|
|
|
|
|
|
pid_t child = fork();
|
|
|
|
if (child < 0) {
|
|
|
|
LOG_ERR("Failed to fork (child)");
|
|
|
|
exit(ERR_FORK);
|
|
|
|
|
|
|
|
} else if (child == 0) { // Program
|
2024-01-01 17:31:21 +08:00
|
|
|
launch_child();
|
2022-10-09 00:06:07 +08:00
|
|
|
LOG_ERR("Failed to execute child program");
|
|
|
|
exit(ERR_EXEC);
|
|
|
|
|
|
|
|
} else { // Supervisor
|
|
|
|
pid_t killer = fork();
|
|
|
|
if (killer < 0) {
|
|
|
|
LOG_ERR("Failed to fork (killer)");
|
|
|
|
exit(ERR_FORK);
|
|
|
|
|
|
|
|
} else if (killer == 0) { // Killer
|
2024-01-30 11:37:59 +08:00
|
|
|
long limit = (strtol(config[CFG_TIME_LIMIT], NULL, 10) + 999) / 1000 + 1;
|
2022-10-09 00:06:07 +08:00
|
|
|
LOG_INFO("Killer started, time limit: %lds", limit);
|
2024-01-27 16:25:37 +08:00
|
|
|
sleep(limit); // 1 more seconds
|
2022-10-09 00:06:07 +08:00
|
|
|
LOG_WARN("Killer killed child");
|
|
|
|
kill(child, SIGKILL);
|
|
|
|
exit(0);
|
|
|
|
|
|
|
|
} else { // stat
|
|
|
|
int status;
|
|
|
|
struct rusage usage;
|
|
|
|
pid_t ret;
|
|
|
|
struct timespec time_begin, time_end;
|
|
|
|
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &time_begin);
|
|
|
|
ret = wait4(child, &status, 0, &usage);
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &time_end);
|
|
|
|
|
|
|
|
long long time_usage =
|
|
|
|
(time_end.tv_sec - time_begin.tv_sec) * 1000 + (time_end.tv_nsec - time_begin.tv_nsec) / 1000000;
|
|
|
|
|
|
|
|
kill(killer, SIGKILL);
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
LOG_ERR("Failed to wait for child process");
|
|
|
|
exit(ERR_WAIT);
|
|
|
|
}
|
|
|
|
|
2023-12-28 00:58:15 +08:00
|
|
|
FILE *info = fopen(config[CFG_FILE_INFO], "w");
|
2022-10-09 00:06:07 +08:00
|
|
|
dump_info(info, &usage, status, time_usage);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
2022-10-09 12:44:09 +08:00
|
|
|
}
|