淺談HUSTOJ後端源碼 (一)【學習路徑+源碼分析】

寫在前面:該文章只針對學習開發後端HUSTOJ的Judged、Judge_client部分(目前不研究sim部分)代碼的同學。 默認你已瞭解HUSTOJ是個什麼東西和他的所分的各個部分的功能。並且你已經在你的電腦上搭建好了本地的HUSTOJ

首先我想說題主目前是普通本科大二在讀生,如果某些方面寫的不詳細或不太正確的地方還請見諒,歡迎在下面留言交流。

好,接下來進入正文。

先談下學習源碼之前的準備工作
1.如果你想了解HUSTOJ後端源碼具體都是寫什麼意思,你要熟練掌握c語言,(自己用c語言寫過幾千幾萬行代碼應該差不多了),畢竟源碼都是c語言寫的,你如果很基礎的c語言語法都不瞭解,那就沒得看了。然後你如果是初次接觸項目,你會發現裏面很多調用的系統已有函數是你從未接觸過的,你可以邊百度邊看(不需要深究,只用大致知道這裏這個函數用來幹啥就可以)。

2.因爲這個項目是在Linux系統上進行的,你需要了解如何在Linux上編寫c語言。而且你要大致瞭解一下Linux相關知識,比如vim編輯器,比如makefile,比如Linux終端的一些常用命令,因爲要用到的東西很多,所以不需要學很深,只需要都瞭解一些常規操作就可以。

3.你要了解一些進程相關的知識,因爲Judged就是一個守護進程,所以你要明白這個守護進程是用來幹啥的。

4.掌握了以上三個,你可能就擁有了讀這些代碼的能力,但是你要理解每部分是什麼意思,你還需要會在Linux終端下調試你想調試的部分代碼,也就是你要會DEBUG。

5.最後如果你不僅想要明白後端這些代碼的原理,還想要明白前端和後端是如何實現交互的,你可能就需要掌握數據庫的相關知識和工作原理,web的一些相關知識等等。當然目前題主也只是做到前四點,開始看這些源碼。

接下來我們來說源碼部分。
後端源碼分爲core裏面分爲judged、judge_client、sim三部分,分別實現了判題的守護進程,判題的核心代碼,判斷相似性三個功能。

下面我就給出judged部分的有關分析,方便更多人節省時間學習。

在這裏插入圖片描述
上面這張圖就是Judged工作的框架,看代碼的時候可以跟着這個整體的框架看,可以看出,它的主要功能就是爲了不停的輪詢數據庫,當用戶在網頁上提交代碼後web端會存入數據庫時就會發現題目,然後進行一些相應判斷後會調用Judge_client開始判題。

好的,瞭解了它的工作原理,我們開始看代碼,下面的代碼我明白了的部分註釋很詳細了,我就不多做解釋了。
(推薦一篇巨佬的博客,開源評測系統hustoj-代碼解讀1這篇博客裏出現過的註釋下面我就不註釋了,可以兩篇結合起來看,相信可以幫你省掉很多時間)。

/*
 * Copyright 2008 sempr <[email protected]>
 *
 * Refacted and modified by zhblue<[email protected]> 
 * 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 <http://www.gnu.org/licenses/>.
 */
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <mysql/mysql.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <signal.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>


#define BUFFER_SIZE 1024
#define LOCKFILE "/var/run/judged.pid"
#define LOCKMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
#define STD_MB 1048576LL

#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 lock_file[BUFFER_SIZE]=LOCKFILE;
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]; //判題目錄 默認/home/judge 
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 int  oj_udp = 0;
static char oj_udpserver[BUFFER_SIZE];
static int  oj_udpport=1536;
static int  oj_udp_fd;

static int  oj_redis = 0;
static char oj_redisserver[BUFFER_SIZE];
static int  oj_redisport;
static char oj_redisauth[BUFFER_SIZE];
static char oj_redisqname[BUFFER_SIZE];
static int turbo_mode = 0;


static bool STOP = false;
static int DEBUG = 0; //是否啓用調試 默認否 
static int ONCE = 0;
#ifdef _mysql_h
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];
#endif
void wait_udp_msg(int fd)
{
    char buf[BUFFER_SIZE];  //......1024..
    socklen_t len;
    int count;
    struct sockaddr_in clent_addr;  //clent_addr............
        memset(buf, 0, BUFFER_SIZE);
        len = sizeof(clent_addr);
        count = recvfrom(fd, buf, BUFFER_SIZE, 0, (struct sockaddr*)&clent_addr, &len);  //recvfrom...............
        if(count == -1)
        {
            printf("recieve data fail!\n");
            return;
        }
        printf("udp client:%s\n",buf);  //..client......
        memset(buf, 0, BUFFER_SIZE);
//        sprintf(buf, "I have recieved %d bytes data!\n", count);  //..client
//        printf("server:%s\n",buf);  //..........
//        sendto(fd, buf, BUFF_LEN, 0, (struct sockaddr*)&clent_addr, len);  //.....client......clent_addr.....

}

void call_for_exit(int s) {												
	if(DEBUG){
		STOP = true;
		printf("Stopping judged...\n");
	}else{
		printf("HUSTOJ Refusing to stop...\n Please use kill -9 !\n");
	}
}		//STOP爲true時,輸出停止判斷。	其實就判斷你現在是否已經打開了調試模式

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, "ae+");
	if (fp == NULL) {
		fprintf(stderr, "openfile error!\n");
		system("pwd");
	}
	va_start(ap, fmt);
	vsprintf(buffer, fmt, ap);
	fprintf(fp, "%s\n", buffer);
	if (DEBUG)
		printf("%s\n", buffer);
	va_end(ap);
	fclose(fp);

}	//將可變參數列表轉化成格式化字符串寫入buffer[4096]中。
//如果DEBUG=1,(即啓用調試開查看日誌運行目錄)	則輸出文件內容


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");
	strcpy(oj_udpserver, "127.0.0.1");
	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);
			
			read_int(buf, "OJ_UDP_ENABLE", &oj_udp);
                        read_buf(buf, "OJ_UDP_SERVER", oj_udpserver);
                        read_int(buf, "OJ_UDP_PORT", &oj_udpport);

			read_int(buf, "OJ_REDISENABLE", &oj_redis);
                        read_buf(buf, "OJ_REDISSERVER", oj_redisserver);
                        read_int(buf, "OJ_REDISPORT", &oj_redisport);
                        read_buf(buf, "OJ_REDISAUTH", oj_redisauth);
                        read_buf(buf, "OJ_REDISQNAME", oj_redisqname);
                        read_int(buf, "OJ_TURBO_MODE", &turbo_mode);


		}
#ifdef _mysql_h
		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, 2 *max_running );
#endif
		sleep_tmp = sleep_time;
		//	fclose(fp);
	}
}
//上面五個函數看起來有點難懂,百度了一下,應該是爲了讀取配置文件./etc/judge.conf,
//然後爲了避免一些項與項之間的影響,所以要判斷幾個條件
//然後我進入這個配置文件看來一下,是一系列變量的賦值,應該就是配置需要用到的一些東西


//這個函數用來評測。當有待評測提交,並且進程數允許的情況下,創建新的子進程調用該評測函數。大致看了一下代碼,應該是
//用來判斷它所能得到的一些資源是否超出限制(cpu,文件大小,虛擬內存,最大子進程數等)。
void run_client(int runid, int clientid) {
	char buf[BUFFER_SIZE], runidstr[BUFFER_SIZE];
	struct rlimit LIM;
	LIM.rlim_max = 800;
	LIM.rlim_cur = 800;
	setrlimit(RLIMIT_CPU, &LIM);

	LIM.rlim_max = 800 * STD_MB;
	LIM.rlim_cur = 800 * STD_MB;
	setrlimit(RLIMIT_FSIZE, &LIM);
#ifdef __mips__
	LIM.rlim_max = STD_MB << 12;
	LIM.rlim_cur = STD_MB << 12;
#endif
#ifdef __arm__
	LIM.rlim_max = STD_MB << 11;
	LIM.rlim_cur = STD_MB << 11;
#endif
#ifdef __aarch64__
	LIM.rlim_max = STD_MB << 15;
	LIM.rlim_cur = STD_MB << 15;
#endif
#ifdef __i386
	LIM.rlim_max = STD_MB << 11;
	LIM.rlim_cur = STD_MB << 11;
#endif
#ifdef __x86_64__
	LIM.rlim_max = STD_MB << 15;
	LIM.rlim_cur = STD_MB << 15;
#endif
	setrlimit(RLIMIT_AS, &LIM);

	LIM.rlim_cur = LIM.rlim_max = 800* max_running;
	setrlimit(RLIMIT_NPROC, &LIM);

	//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
	//	execl("/usr/bin/judge_client", "/usr/bin/judge_client", runidstr, buf,
	//			oj_home, "debug", (char *) NULL);

	//exit(0);
}
#ifdef _mysql_h
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;
		return 1;
	} else
		return 0;
}
#endif

#ifdef _mysql_h
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 executesql("set names utf8");
		}
	} else {
			return executesql("commit");
	}
}
#endif
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);
	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);
	}

}

//我看代碼是在向jobs數組裏賦值,應該是查詢待評測題目信息到jobs數組裏,
//保存solution_id/runid  函數返回值是要評測題目的數量,查詢不成功則返回0
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;
}
#ifdef _mysql_h
int _get_jobs_mysql(int * jobs) {
	if (mysql_real_query(conn, query, strlen(query))) {
		if (DEBUG)
			write_log("%s", mysql_error(conn));
		sleep(20);
		return 0;
	}
	res = mysql_store_result(conn);
	int i = 0;
	int ret = 0;
	while (res!=NULL && (row = mysql_fetch_row(res)) != NULL) {
		jobs[i++] = atoi(row[0]);
	}

	if(res!=NULL&&!executesql("commit")){
		mysql_free_result(res);                         // free the memory
		res=NULL;
	}                        
	else i=0;
	ret = i;
	while (i <= max_running * 2)
		jobs[i++] = 0;
	return ret;
}
#endif
int _get_jobs_redis(int * jobs){
        int ret=0;
        const char * cmd="redis-cli -h %s -p %d -a %s --raw rpop %s";
        while(ret<=max_running){
                FILE * fjobs = read_cmd_output(cmd,oj_redisserver,oj_redisport,oj_redisauth,oj_redisqname);
                if(fscanf(fjobs,"%d",&jobs[ret])==1){
                        ret++;
                        pclose(fjobs);
                }else{
                        pclose(fjobs);
                        break;
                }

        }
        int i=ret;
        while (i <= max_running * 2)
                jobs[i++] = 0;
        if(DEBUG) printf("redis return %d jobs",ret);
        return ret;
}

//這個函數是用來判斷兩種連接方式 http還是mysql
int get_jobs(int * jobs) {
	if (http_judge) {
		return _get_jobs_http(jobs);
	} else {		
		if(oj_redis){
                        return _get_jobs_redis(jobs);
                }else{
#ifdef _mysql_h
                        return _get_jobs_mysql(jobs);
#else
			return 0;
#endif
                }
	}
}

//下面三個函數是對數據庫的一些更新、設置的操作
#ifdef _mysql_h
bool _check_out_mysql(int solution_id, int result) {
	char sql[BUFFER_SIZE];
	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);
	if (mysql_real_query(conn, sql, strlen(sql))) {
		syslog(LOG_ERR | LOG_DAEMON, "%s", mysql_error(conn));
		return false;
	} else {
		if (conn!=NULL&&mysql_affected_rows(conn) > 0ul)
			return true;
		else
			return false;
	}

}
#endif

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;
}
bool check_out(int solution_id, int result) {
        if(oj_redis||oj_tot>1) return true;
	if (http_judge) {
		return _check_out_http(solution_id, result);
	} else{
#ifdef _mysql_h
		return _check_out_mysql(solution_id, result);
#else
		return 0;
#endif
	}

}
	static int workcnt = 0;

//覺這個函數應該是這個守護進程的核心部分,看了一下守護進程的運行框架。結合這個部分的代碼
//我感覺應該是不斷的去進行遍歷所有評測題目投入到新的評測進程裏,然後中間有一個處理就是要保證
//當前進程不能超過最大進程數,如果超過就wait,直到有一個進程結束。
int work() {
//      char buf[1024];
	int retcnt = 0;
	int i = 0;
	static pid_t ID[100];
	int runid = 0;
	int jobs[max_running * 2 + 1];
	pid_t tmp_pid = 0;

	//for(i=0;i<max_running;i++){
	//      ID[i]=0;
	//}
	for(i=0;i<max_running *2 +1 ;i++)
		jobs[i]=0;

	//sleep_time=sleep_tmp;
	/* get the database info */
	if (!get_jobs(jobs)){
		return 0;
	}
	/* exec the submit */
	for (int j = 0; jobs[j] > 0; j++) {
		runid = jobs[j];
		if (runid % oj_tot != oj_mod)
			continue;
		if (workcnt >= max_running) {           // if no more client can running
			tmp_pid = waitpid(-1, NULL, WNOHANG);     // wait 4 one child exit
			if (DEBUG) printf("try get one tmp_pid=%d\n",tmp_pid);
			for (i = 0; i < max_running; i++){     // get the client id
				if (ID[i] == tmp_pid){
					workcnt--;
					retcnt++;
					ID[i] = 0;
					break; // got the client id
				}
			}
		} else {                                             // have free client

			for (i = 0; i < max_running; i++)     // find the client id
				if (ID[i] == 0)
					break;    // got the client id
		}
		if(i<max_running){
			if (workcnt < max_running && check_out(runid, OJ_CI)) {
				workcnt++;
				ID[i] = fork();                                   // start to fork
				if (ID[i] == 0) {
					if (DEBUG){
						write_log("Judging solution %d", runid);
						write_log("<<=sid=%d===clientid=%d==>>\n", runid, i);
					}
					run_client(runid, i);    // if the process is the son, run it
					workcnt--;
					exit(0);
				}

			} else {
			//	ID[i] = 0;
				if(DEBUG){
					if(workcnt<max_running)
						printf("check out failure ! runid:%d pid:%d \n",i,ID[i]);
					else
						printf("workcnt:%d max_running:%d ! \n",workcnt,max_running);
						
				}
			}
		}
		if(DEBUG)
			  printf("workcnt:%d max_running:%d ! \n",workcnt,max_running);
	}
	while ((tmp_pid = waitpid(-1, NULL,WNOHANG)) > 0) {
		for (i = 0; i < max_running; i++){     // get the client id
			if (ID[i] == tmp_pid){
			
				workcnt--;
				retcnt++;
				ID[i] = 0;
				break; // got the client id
			}
		}
		printf("tmp_pid = %d\n", tmp_pid);
	}
	if (!http_judge) {
#ifdef _mysql_h
		if(res!=NULL) {
			mysql_free_result(res);                         // free the memory
			res=NULL;
		}
		executesql("commit");
#endif
	}
	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(lock_file, 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);
}

//下面這個函數就是用來創建一個守護進程
//看了一下代碼,就是一般創建守護進程的流程。
/*
->fork子進程(此時子進程繼承了父進程的會話期、進程組、終端、工作目錄、文件權限)
->退出父進程
->調用setsid函數創建一個新的對話並擔任該會話組的組長(擺脫原繼承的會話、進程、終端的控制)
->改變當前目錄(因爲繼承原目錄會導致一直運行無法卸載原目錄)
->重設文件權限	通常調用umask(0) 將文件掩碼設爲0
->關閉文件描述符	這裏調用關閉了stdin stout sterr
*/
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 */
	
	int fd = open( "/dev/null", O_RDWR );
	dup2( fd, 0 );
	dup2( fd, 1 );
	dup2( fd, 2 );
	if ( fd > 2 ){
		close( fd );
	}

	return (0);
}
void turbo_mode2(){
#ifdef _mysql_h
	if(turbo_mode==2){
			char sql[BUFFER_SIZE];
			sprintf(sql," CALL `sync_result`();");
			if (mysql_real_query(conn, sql, strlen(sql)));
	}
#endif

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

	sprintf(lock_file,"%s/etc/judge.pid",oj_home);
	if (!DEBUG)
		daemon_init();
	if ( already_running()) {
		syslog(LOG_ERR | LOG_DAEMON,
				"This daemon program is already running!\n");
		printf("%s already has one judged on it!\n",oj_home);
		return 1;
	}
	if(!DEBUG)
		system("/sbin/iptables -A OUTPUT -m owner --uid-owner judge -j DROP");
//	struct timespec final_sleep;
//	final_sleep.tv_sec=0;
//	final_sleep.tv_nsec=500000000;
#ifdef _mysql_h
	init_mysql_conf();	// set the database info
#endif
	if(oj_udp){
		oj_udp_fd = socket(AF_INET, SOCK_DGRAM, 0);
		if(oj_udp_fd<0) 
			printf("udp fd open failed! \n");		
		struct sockaddr_in ser_addr;
		memset(&ser_addr, 0, sizeof(ser_addr));
		ser_addr.sin_family = AF_INET;
		ser_addr.sin_addr.s_addr = inet_addr(oj_udpserver);
		ser_addr.sin_port = htons(oj_udpport);
		struct timeval timeOut;
		timeOut.tv_sec = sleep_time;                 //..5s..
		timeOut.tv_usec = 0;
		if (setsockopt(oj_udp_fd, SOL_SOCKET, SO_RCVTIMEO, &timeOut, sizeof(timeOut)) < 0)
		{
			printf("time out setting failed\n");
		}
		oj_udp_ret=bind(oj_udp_fd, (struct sockaddr*)&ser_addr, sizeof(ser_addr));
		if(oj_udp_ret<0) 
			printf("udp fd open failed! \n");
	}
	signal(SIGQUIT, call_for_exit);
	signal(SIGINT, call_for_exit);
	signal(SIGTERM, call_for_exit);
	int j = 1;
	int n = 0;
	while (!STOP) {			// start to run until call for exit
		n=0;
		while (j && (http_judge
#ifdef _mysql_h
			 || !init_mysql()
#endif
		)) {

			j = work();
			n+=j;
			if(turbo_mode==2&&(n>max_running*10||j<max_running)){
				turbo_mode2();
				n=0;
			}
	

			if(ONCE) break;
		}
		turbo_mode2();
		if(ONCE) break;
                if(n==0){
			printf("workcnt:%d\n",workcnt);
			if(oj_udp&&oj_udp_ret==0){
				if(STOP) return 1;
				wait_udp_msg(oj_udp_fd);
                        	if(DEBUG) printf("udp job ... \n");

			}else{
                        	sleep(sleep_time);
                        	if(DEBUG) printf("sleeping ... %ds \n",sleep_time);
			}
                }
		j = 1;
	}
	return 0;
}

這篇博客只爲幫助那些和我一樣剛開始研究這個項目但不知如何下手的人。我只能給你提供一些方法和可能會省掉一些不必要的時間的路徑,接下來還是要靠你自己艱難的沿着你已經確定的路走下去。

最後我想說,這個探索的過程是一個很艱難的過程,儘管在很多大牛眼裏這只是一個平白無奇並未涉及很多技術的項目,但對於我們來說,不斷的去探索,學習相關知識,學到最後你會發現你掌握的不僅僅是這個“平白無奇”的項目,其實,這就是一個自我“救贖”的過程,誰讓我們學習能力確實不如別人呢。
先寫到這裏把,我如果有更多的發現會在代碼上更改的。

非常感謝zhblue貢獻了這麼美麗的代碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章