judge_core源码(part1)

的高亮很好看,可以躺床上看

/*
 * Copyright 2008 sempr 
 *
 * Refacted and modified by zhblue 
 * Bug report email [email protected]
 * 
 * This file is part of HUSTOJ.
 *
 * HUSTOJ is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * HUSTOJ is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with HUSTOJ. if not, see .
 */
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
static int DEBUG = 0; //是否启用调试,来查看日志运行记录,默认0,不启用
#define BUFFER_SIZE 1024
// (1) pid文件的内容:pid文件为文本文件,内容只有一行, 记录了该进程的ID。
// 用cat命令可以看到。
// (2) pid文件的作用:防止进程启动多个副本。
// 只有获得pid文件(固定路径固定文件名)写入权限(F_WRLCK)的进程才能正常启动并把自身的PID写入该文件中。
// 其它同一个程序的多余进程则自动退出。
#define LOCKFILE "/var/run/judged.pid" //新版HUSTOJ将状态文件的目录修改到了/home/judge/etc
#define LOCKMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) //读写权限被占,因此是锁模式
#define STD_MB 1048576
//Judge Status
#define OJ_WT0 0
#define OJ_WT1 1
#define OJ_CI 2
#define OJ_RI 3
#define OJ_AC 4
#define OJ_PE 5
#define OJ_WA 6
#define OJ_TL 7
#define OJ_ML 8
#define OJ_OL 9
#define OJ_RE 10
#define OJ_CE 11
#define OJ_CO 12

static char host_name[BUFFER_SIZE];
static char user_name[BUFFER_SIZE];
static char password[BUFFER_SIZE];
static char db_name[BUFFER_SIZE];
static char oj_home[BUFFER_SIZE];
static char oj_lang_set[BUFFER_SIZE];
static int port_number;
static int max_running;
static int sleep_time;
static int sleep_tmp;
static int oj_tot;
static int oj_mod;
static int http_judge = 0;
static char http_baseurl[BUFFER_SIZE];
static char http_username[BUFFER_SIZE];
static char http_password[BUFFER_SIZE];

static bool STOP = false;

static MYSQL *conn;
static MYSQL_RES *res;  //mysql读取结果集,在_get_http/mysql_jobs()中被更新
static MYSQL_ROW row;
//static FILE *fp_log;
static char query[BUFFER_SIZE];//在init_mysql_conf中更新,固定取2倍最大判题客户端的待评判题目solution_id

void call_for_exit(int s) {
  STOP = true;
  printf("Stopping judged...\n");
}

void write_log(const char *fmt, ...) {
  va_list ap;     //初始化指向可变参数列表的指针
  char buffer[4096];
  //  time_t          t = time(NULL);
  //  int             l;
  sprintf(buffer, "%s/log/client.log", oj_home);
  FILE *fp = fopen(buffer, "a+");
  if (fp == NULL) {
    fprintf(stderr, "openfile error!\n");
    system("pwd");
  }
  va_start(ap, fmt);  //ap指向可变参数列表的开始
  vsprintf(buffer, fmt, ap);//将fmt和ap中指向的参数转换成格式化的字符串,放到buffer中。
                            //用法和sprintf一样,vsprintf参数对应的位置也是一样的。
  fprintf(fp, "%s\n", buffer);//转换后的内容输出到文件中
  if (DEBUG)
    printf("%s\n", buffer);
  va_end(ap);//ap修改为0,使程序更健壮
  fclose(fp);
}

int after_equal(char * c) {
  int i = 0;
  for (; c[i] != '\0' && c[i] != '='; i++)
    ;
  return ++i;
}
void trim(char * c) {
  char buf[BUFFER_SIZE];
  char * start, *end;
  strcpy(buf, c);
  start = buf;
  while (isspace(*start))
    start++;
  end = start;
  while (!isspace(*end))
    end++;
  *end = '\0';
  strcpy(c, start);
}
bool read_buf(char * buf, const char * key, char * value) {
  if (strncmp(buf, key, strlen(key)) == 0) {
    strcpy(value, buf + after_equal(buf));
    trim(value);
    if (DEBUG)
      printf("%s\n", value);
    return 1;
  }
  return 0;
}
void read_int(char * buf, const char * key, int * value) {
  char buf2[BUFFER_SIZE];
  if (read_buf(buf, key, buf2))
    sscanf(buf2, "%d", value);

}
// read the configue file
void init_mysql_conf() {
  FILE *fp = NULL;
  char buf[BUFFER_SIZE];
  host_name[0] = 0;
  user_name[0] = 0;
  password[0] = 0;
  db_name[0] = 0;
  port_number = 3306;
  max_running = 3;
  sleep_time = 1;
  oj_tot = 1;
  oj_mod = 0;
  strcpy(oj_lang_set, "0,1,2,3,4,5,6,7,8,9,10");
  fp = fopen("./etc/judge.conf", "r");
  if (fp != NULL) {
    while (fgets(buf, BUFFER_SIZE - 1, fp)) {
      read_buf(buf, "OJ_HOST_NAME", host_name);
      read_buf(buf, "OJ_USER_NAME", user_name);
      read_buf(buf, "OJ_PASSWORD", password);
      read_buf(buf, "OJ_DB_NAME", db_name);
      read_int(buf, "OJ_PORT_NUMBER", &port_number);
      read_int(buf, "OJ_RUNNING", &max_running);
      read_int(buf, "OJ_SLEEP_TIME", &sleep_time);
      read_int(buf, "OJ_TOTAL", &oj_tot);

      read_int(buf, "OJ_MOD", &oj_mod);

      read_int(buf, "OJ_HTTP_JUDGE", &http_judge);
      read_buf(buf, "OJ_HTTP_BASEURL", http_baseurl);
      read_buf(buf, "OJ_HTTP_USERNAME", http_username);
      read_buf(buf, "OJ_HTTP_PASSWORD", http_password);
      read_buf(buf, "OJ_LANG_SET", oj_lang_set);

    }
    sprintf(query,
        "SELECT solution_id FROM solution WHERE language in (%s) and result<2 and MOD(solution_id,%d)=%d ORDER BY result ASC,solution_id ASC limit %d",
        oj_lang_set, oj_tot, oj_mod, max_running * 2);
    sleep_tmp = sleep_time;
    //  fclose(fp);
  }
}


//当有待评测提交,并且进程数允许的情况下,创建新的子进程调用该评测函数
//输入:待评测提交的solution_id, 子进程在ID[]中的保存位置 i  
void run_client(int runid, int clientid) {
  char buf[BUFFER_SIZE], runidstr[BUFFER_SIZE];
  //在Linux系统中,Resouce limit指在一个进程的执行过程中,它所能得到的资源的限制,
  //比如进程的core file的最大值,虚拟内存的最大值等 ,这是运行时间,内存大小实现的关键 
  /*
  结构体中 rlim_cur是要取得或设置的资源软限制的值,rlim_max是硬限制
  这两个值的设置有一个小的约束: 
  1) 任何进程可以将软限制改为小于或等于硬限制
  2)任何进程都可以将硬限制降低,但普通用户降低了就无法提高,该值必须等于或大于软限制
  3) 只有超级用户可以提高硬限制
  
  setrlimit(int resource,const struct rlimit rlptr);返回:若成功为0,出错为非0  
  RLIMIT_CPU:CPU时间的最大量值(秒),当超过此软限制时向该进程发送SIGXCPU信号
  RLIMIT_FSIZE:可以创建的文件的最大字节长度,当超过此软限制时向进程发送SIGXFSZ
  */ 
  struct rlimit LIM;
  LIM.rlim_max = 800;
  LIM.rlim_cur = 800;
  setrlimit(RLIMIT_CPU, &LIM);//cpu运行时间限制 

  LIM.rlim_max = 80 * STD_MB;
  LIM.rlim_cur = 80 * STD_MB;
  setrlimit(RLIMIT_FSIZE, &LIM);//可文件大小限制,防止恶意程序的吗? 

  LIM.rlim_max = STD_MB << 11;//左移11 STD_MB是2^20 MB 2^11MB 2GB机器起码的2GB虚拟内存? 
  LIM.rlim_cur = STD_MB << 11;
  setrlimit(RLIMIT_AS, &LIM);//最大运行的虚拟内存大小限制 

  LIM.rlim_cur = LIM.rlim_max = 200;
  setrlimit(RLIMIT_NPROC, &LIM);//每个实际用户ID所拥有的最大子进程数,这些都是为了防止恶意程序的吧?? 

  //buf[0]=clientid+'0'; buf[1]=0;
  sprintf(runidstr, "%d", runid);//转换成字符?还是字符串? 
  sprintf(buf, "%d", clientid);

  //write_log("sid=%s\tclient=%s\toj_home=%s\n",runidstr,buf,oj_home);
  //sprintf(err,"%s/run%d/error.out",oj_home,clientid);
  //freopen(err,"a+",stderr);

  if (!DEBUG)
    execl("/usr/bin/judge_client", "/usr/bin/judge_client", runidstr, buf,
        oj_home, (char *) NULL);
  else
  
  //返回值:如果执行成功则函数不会返回, 执行失败则直接返回-1, 失败原因存于errno 中. 
  //execl()其中后缀"l"代表list也就是参数列表的意思,第一参数path字符指针所指向要执行的文件路径, 
  //接下来的参数代表执行该文件时传递的参数列表:argv[0],argv[1]... 最后一个参数须用空指针NULL作结束。 
  //  执行/bin目录下的ls, 第一参数为程序名ls, 第二个参数为"-al", 第三个参数为"/etc/passwd"
  //execl("/bin/ls", "ls", "-al", "/etc/passwd", (char *) 0);
  //这里第一个参数为程序名称judge_client,第二个参数为代评测题目id, 第三个为本进程pid保存位置,第四个参数为oj目录
  //默认/home/judge,第五个参数为“debug” 
    execl("/usr/bin/judge_client", "/usr/bin/judge_client", runidstr, buf,
        oj_home, "debug", (char *) NULL);

  //exit(0);
}
//执行sql语句成功返回1,否则返回0 
//并且关闭是否conn,它在init里初始化开始的 
int executesql(const char * sql) {

  if (mysql_real_query(conn, sql, strlen(sql))) {
    if (DEBUG)
      write_log("%s", mysql_error(conn));
    sleep(20);
    conn = NULL;  //执行失败关闭conn,下次查询的时候重新init
    return 1;
  } else
    return 0;
}

int init_mysql() {
  if (conn == NULL) {
    conn = mysql_init(NULL);    // init the database connection
    /* connect the database */
    const char timeout = 30;
    mysql_options(conn, MYSQL_OPT_CONNECT_TIMEOUT, &timeout); //设置数据库查询超时时间

    if (!mysql_real_connect(conn, host_name, user_name, password, db_name, port_number, 0, 0)) {
      if (DEBUG)
        write_log("%s", mysql_error(conn));
      sleep(2);
      return 1;
    } else {
      return 0;
    }
  } else {  //set names utf8告诉服务器将来从这个客户端传来的信息采用字符集utf8
            //每次这样做可以看成是一步保证字符集不出错的查询。
    return executesql("set names utf8");
  }
}
FILE * read_cmd_output(const char * fmt, ...) {
  char cmd[BUFFER_SIZE];

  FILE * ret = NULL;
  va_list ap;

  va_start(ap, fmt);
  vsprintf(cmd, fmt, ap);
  va_end(ap);
  //if(DEBUG) printf("%s\n",cmd);

  //popen() 函数通过创建一个管道,调用 fork 产生一个子进程,执行一个 shell 以运行命令来开启一个进程。
  //这个进程必须由 pclose() 函数关闭,而不是 fclose() 函数。
  // pclose() 函数关闭标准 I/O 流,等待命令执行结束,然后返回 shell 的终止状态。
  // 如果 shell 不能被执行,则 pclose() 返回的终止状态与 shell 已执行 exit 一样。
  ret = popen(cmd, "r");

  return ret;
}
int read_int_http(FILE * f) {
  char buf[BUFFER_SIZE];
  fgets(buf, BUFFER_SIZE - 1, f);
  return atoi(buf);
}
bool check_login() {
  const char * cmd =
      "wget --post-data=\"checklogin=1\" --load-cookies=cookie --save-cookies=cookie --keep-session-cookies -q -O - \"%s/admin/problem_judge.php\"";
  int ret = 0;

  FILE * fjobs = read_cmd_output(cmd, http_baseurl);
  ret = read_int_http(fjobs);
  pclose(fjobs);

  return ret > 0;
}
void login() {
  if (!check_login()) {
    char cmd[BUFFER_SIZE];
    sprintf(cmd,
        "wget --post-data=\"user_id=%s&password=%s\" --load-cookies=cookie --save-cookies=cookie --keep-session-cookies -q -O - \"%s/login.php\"",
        http_username, http_password, http_baseurl);
    system(cmd);
  }

}
int _get_jobs_http(int * jobs) {
  login();
  int ret = 0;
  int i = 0;
  char buf[BUFFER_SIZE];
  const char * cmd =
      "wget --post-data=\"getpending=1&oj_lang_set=%s&max_running=%d\" --load-cookies=cookie --save-cookies=cookie --keep-session-cookies -q -O - \"%s/admin/problem_judge.php\"";
  FILE * fjobs = read_cmd_output(cmd, oj_lang_set, max_running, http_baseurl);
  while (fscanf(fjobs, "%s", buf) != EOF) {
    //puts(buf);
    int sid = atoi(buf);
    if (sid > 0)
      jobs[i++] = sid;
    //i++;
  }
  pclose(fjobs);
  ret = i;
  while (i <= max_running * 2)
    jobs[i++] = 0;
  return ret;
  return ret;
}
//功能:取得待评测题目信息到jobs数组
//输入:int * jobs :保存solution_id/runid
//输出:如果查询成功则返回:要评测题目数量 
//如果查询待判题目不成功则返回0

int _get_jobs_mysql(int * jobs) {
  //mysql.h
  //如果查询数据包括二进制或者更快速度 用这个
  //如果执行成功,返回0;不成功非0
  if (mysql_real_query(conn, query, strlen(query))) {
    if (DEBUG)
      write_log("%s", mysql_error(conn));
    sleep(20);
    return 0;
  }
  
  //mysql.h
  //返回具有多个结果的MYSQL_RES结果集合。如果出现错误,返回NULL
  //具体参见百度
  res = mysql_store_result(conn);
  int i = 0;
  int ret = 0;
  //遍历结果集mysql_fetch_row()
  while ((row = mysql_fetch_row(res)) != NULL) {
    jobs[i++] = atoi(row[0]);
  }
  ret = i; //要评测jobs末端 如 0 1 2 有数据,则i=3代表数据
  while (i <= max_running * 2)
    jobs[i++] = 0; //设定的最大工作数目为max_running*2,将0-8置位0共9个 max_running*2+1数组开这么大 
  return ret;
  return ret;
}
int get_jobs(int * jobs) {
  if (http_judge) { //web和core默认连接方式:数据库,web插入solution,core轮训/更新solution-result,web轮训solution-result
    return _get_jobs_http(jobs);
  } else
    return _get_jobs_mysql(jobs);//读取要判题的任务数量

}

//更新初始化solution表格
//更新成功返回1;否则0
// 疑问:OJ_CI为2,and result < 2这句怎么都是不成立,这个Sql语句怎么都不会执行成功才对啊 
//用limit 1加了一层保障。避免where 条件出现异常时,错误更新影响太多。 
//不知道php初始写多少,但是调用给的参数为2啊,不懂!!!! 
bool _check_out_mysql(int solution_id, int result) {
  char sql[BUFFER_SIZE]; //sql语句保存 
  sprintf(sql,
      "UPDATE solution SET result=%d,time=0,memory=0,judgetime=NOW() WHERE solution_id=%d and result<2 LIMIT 1",
      result, solution_id);
//执行sql语句,成功返回0;否则非0 
  if (mysql_real_query(conn, sql, strlen(sql))) {
    syslog(LOG_ERR | LOG_DAEMON, "%s", mysql_error(conn));
    return false;
  } else {
    //影响行数,更新数大于0,执行成功,返回1,否则0 
    if (mysql_affected_rows(conn) > 0ul)
      return true;
    else
      return false;
  }

}

bool _check_out_http(int solution_id, int result) {
  login();
  const char * cmd =
      "wget --post-data=\"checkout=1&sid=%d&result=%d\" --load-cookies=cookie --save-cookies=cookie --keep-session-cookies -q -O - \"%s/admin/problem_judge.php\"";
  int ret = 0;
  FILE * fjobs = read_cmd_output(cmd, solution_id, result, http_baseurl);
  fscanf(fjobs, "%d", &ret);
  pclose(fjobs);

  return ret;
}

//初始更新solution表
//依据参数不同执行不同的更新函数 
bool check_out(int solution_id, int result) {

  if (http_judge) {
    return _check_out_http(solution_id, result);
  } else
    return _check_out_mysql(solution_id, result);

}
int work() {
//      char buf[1024];
  static int retcnt = 0;//统计 已经 完成评测次数  
  int i = 0;
  static pid_t ID[100];  //short类型的宏定义,进程表中的索引项,进程号;保存正在执行的子进程pid 
  static int workcnt = 0;//统计 现用 judge_client进程数量 
  int runid = 0;      //solution_id,测试运行编号
  int jobs[max_running * 2 + 1];//max_running 从judge.conf获取,一般为4,这里设置为工作目录:9
  pid_t tmp_pid = 0;

  //for(i=0;i 0; j++) {
    runid = jobs[j]; //读取solution_id,待评测提交题目id 
    //老式并发处理中,默认oj_tot 为 1 oj_mod 为0,在init_sql_conf中设置 所以无用 
    if (runid % oj_tot != oj_mod)  
      continue;
    if (DEBUG) //调试用默认0 无用 
      write_log("Judging solution %d", runid);
    //workcnt 为static 变量,相当于死锁,统计现用run_client进程 数目 
    //本if 等待可用 子进程,并且用 i 腾出保存 新子进程的位置 
    if (workcnt >= max_running) {           // if no more client can running
        //如果达到了可用最大进程数目,那么等待一个子进程结束
      //waitpid,参考linux 下 c 语言编程下的 进程管理 
      //waitpid()会暂时停止目前进程的执行,直到有信号来到或子进程结束
      //pid_t waitpid(pid_t pid,int * status,int options);
      //pid=-1 代表任意子进程;status 取回子进程识别码,这里不需要所以NULL; 
      //参数options提供了一些额外的选项来控制waitpid,比如不等待继续执行,这里0代表不使用,进程挂起
      //如果 有子进程已经结束,那么执行到这里的时候会直接跳过,子进程也会由僵尸进程释放  
      //返回结束的子进程pid  
      tmp_pid = waitpid(-1, NULL, 0);     // wait 4 one child exit
      workcnt--;//子进程结束了个,那么现用judge_client数量减一  
      retcnt++;//评测完成数加1 
      //清除保存在 ID[]里的已经结束的子进程信息 
      for (i = 0; i < max_running; i++)     // get the client id
        if (ID[i] == tmp_pid)
          break; // got the client id
      ID[i] = 0;
    } else {                                             // have free client

      for (i = 0; i < max_running; i++)     // find the client id
        if (ID[i] == 0)
          break;    // got the client id
    }
    
    //其实这里worknct>\n", runid, i);
        run_client(runid, i);  //在子进程里更新ID[0]=pid  // if the process is the son, run it
        exit(0);//子进程执行完毕退出0,父进程不会执行这段if ,在run_client里进程会跳转到execl(judge_client)
                //执行成功不返回,不成功返回非0,保存在erro里,那么这里又是怎么执行到的,子进程如何退出的?????? 
      }

    } else {//理论上,在上个if里已经保证了这里为ID[i] = 0,这里估计是为了进一步保证 
      ID[i] = 0;
    }
  }

  //把本次轮训到的代评测题目全部投入评测后 
  //在非挂起等待子进程的结束,如果有子进程评测完成结束 
  //在上个的for里,当可用进程没有的时候,那么就必须等其中一个进程结束,那么才能继续执行,哪怕在for里已经有 
  // 子进程结束是僵尸进程了,只要workcnt 0) {
    workcnt--;
    retcnt++;
    for (i = 0; i < max_running; i++)     // get the client id
      if (ID[i] == tmp_pid)
        break; // got the client id
    ID[i] = 0;
    printf("tmp_pid = %d\n", tmp_pid);
  }
  //释放数据库资源 
  //这里commit的调用,不知道是为了关闭conn,还是数据库不支持自动commit
  //还是彻底缩小日志,不给机会rollback,待学习?????? 
  if (!http_judge) {
    mysql_free_result(res);                         // free the memory
    executesql("commit");
  }
  if (DEBUG && retcnt)
    write_log("<<%ddone!>>", retcnt);
  //free(ID);
  //free(jobs);
  //返回已经评测的次数 
  return retcnt;
}

int lockfile(int fd) {
  struct flock fl;
  fl.l_type = F_WRLCK;
  fl.l_start = 0;
  fl.l_whence = SEEK_SET;
  fl.l_len = 0;
  return (fcntl(fd, F_SETLK, &fl));
}

int already_running() {
  int fd;
  char buf[16];
  fd = open(LOCKFILE, O_RDWR | O_CREAT, LOCKMODE);
  if (fd < 0) {
    syslog(LOG_ERR | LOG_DAEMON, "can't open %s: %s", LOCKFILE,
        strerror(errno));
    exit(1);
  }
  if (lockfile(fd) < 0) {
    if (errno == EACCES || errno == EAGAIN) {
      close(fd);
      return 1;
    }
    syslog(LOG_ERR | LOG_DAEMON, "can't lock %s: %s", LOCKFILE,
        strerror(errno));
    exit(1);
  }
  ftruncate(fd, 0);
  sprintf(buf, "%d", getpid());
  write(fd, buf, strlen(buf) + 1);
  return (0);
}
int daemon_init(void)

{
  pid_t pid;

  if ((pid = fork()) < 0)
    return (-1);

  else if (pid != 0)
    exit(0); /* parent exit */

  /* child continues */

  setsid(); /* become session leader */

  chdir(oj_home); /* change working directory */

  umask(0); /* clear file mode creation mask */

  close(0); /* close stdin */

  close(1); /* close stdout */

  close(2); /* close stderr */

  return (0);
}

int main(int argc, char** argv) {
  DEBUG = (argc > 2);
  if (argc > 1)
    strcpy(oj_home, argv[1]);
  else
    strcpy(oj_home, "/home/judge");
  chdir(oj_home);    // change the dir

  if (!DEBUG)
    daemon_init();//创建一个daemon守护进程 
  if (strcmp(oj_home, "/home/judge") == 0 && already_running()) {
    syslog(LOG_ERR | LOG_DAEMON,
        "This daemon program is already running!\n");
    return 1;
  }
//  struct timespec final_sleep;
//  final_sleep.tv_sec=0;
//  final_sleep.tv_nsec=500000000;
  init_mysql_conf();  // set the database info
  signal(SIGQUIT, call_for_exit);
  signal(SIGKILL, call_for_exit);
  signal(SIGTERM, call_for_exit);
  int j = 1;
  while (1) {     // start to run
    //这个while的好处在于,只要一有任务就抓紧占用系统优先把所以任务处理完成,哪怕会空循环几次的可能存在
    //但是没有任务后,就会进入到“懒散”的 休息sleep(time)后再轮询是不是有任务,释放系统的资源,避免Damon一直
    //死循环占用系统 
    while (j && (http_judge || !init_mysql())) {

      j = work();//如果读取失败或者没有要评测的数据,那么返回0,利用那么有限的几个进程来评测无限的任务量 

    }
    sleep(sleep_time);
    j = 1;
  }
  return 0;
}

你可能感兴趣的:(judge_core源码(part1))