导航

hustOJ 添加 golang 支持

Posted on 2018-09-24 23:33  Morya  阅读(704)  评论(0编辑  收藏  举报

hustOJ 支持Go1.7.1

是否为docker环境不重要,此处所有内容均为docker中执行,普通主机手动安装则更加如此

建议在docker中执行,因为OJ为严控恶意权限,judge_client做了很多特殊指令

hustOJ 虽然有部分代码涉及到了golang
但,实际还无法正常执行。

本次支持的是go 1.7.1

关键改动都在core组件里面的judge_client

系统修改

  1. 配置apt使用清华大学镜像下载golang

文件 /etc/apt/sources.list

deb https://mirrors.tuna.tsinghua.edu.cn/debian/ jessie main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ jessie main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ jessie-updates main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ jessie-updates main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ jessie-backports main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ jessie-backports main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security jessie/updates main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security jessie/updates main contrib non-free

安装 golang

apt-get install golang-1.7.1

改动点:

  1. okcalls64.h

在数组里面增加 186 信号,baidu说,此信号实际对应值是:186 gettid

如果不增加,golang编译的程序会被judge_client fork 出的parent监控并停止。

int LANG_GOV[256]={0,1,9,11,13,14,56,59,131,158,186,202,204,228,231,0};
  1. judge_client.cc

copy_js_runtime 函数后,新增函数 copy_go_runtime


void copy_go_runtime(char *work_dir) {
    char envbuff[1024] = {0};

    copy_shell_runtime(work_dir);

    execute_cmd("/bin/mkdir %s/usr", work_dir);
    execute_cmd("/bin/mkdir %s/usr/lib", work_dir);
    execute_cmd("/bin/mkdir %s/usr/bin", work_dir);
    // execute_cmd("/bin/cp /usr/lib/go-1.7/bin/go %s/usr/bin/", work_dir);
    putenv((char *)"GOROOT=/usr/lib/go-1.7");
    sprintf(envbuff, "GOPATH=%s", work_dir);
    putenv(envbuff);
}
  1. 修改 int compile(int lang, char *work_dir) 函数
// 此处为方便,直接写死了go-1.7的绝对位置
// 主要judge_client在执行真正的程序前,会先执行很多环境准备
// 甚至包括chroot指令
// 最终导致环境混乱
const char *CP_GO[] = { "/usr/lib/go-1.7/bin/go", "build", "-o", "Main", "Main.go", NULL };
  1. 修改 main 函数
int main(int argc, char **argv) {
    // init_parameters
    // init mysql
    // get_solution_info
    // compile

    // 根据逻辑相关部分新增如下函数
    // copy_go_runtime
  1. make it
cd /home/judge/src/core/judge_client/
make
  1. 单独测试 golang 程序

可以用go源码提交一份其它语言的程序到题目中,此程序会被存储到数据库,
并分配一个solution_id,假设其为1008.

登录数据库,修改其语言为go

mysql -udebian-sys-maint -paaabbb

> use jol;
> update solution set language=17 where solution_id = 1008;
> commit;

以上sql为手写,假装正确

然后手动执行 judge_client

/home/judge/src/core/judge_client/judge_client 1008 1 /home/judge/ debug

如果,最后输出 result=4 则代表实际结果正确了。