⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 execute.cpp

📁 linux 下 源代码测试系统 用 tar 打开
💻 CPP
字号:
/* * $File: execute.cpp * $Author: Jiakai -- gy_jk@126.com * $Date: Mon Mar 23 23:02:35 2009*//*Copyright (C) (2008) (Jiakai) <gy_jk@126.com>This program 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 any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.*/#include "execute.h"#include <unistd.h>#include <stdio.h>#include <errno.h>#include <sys/types.h>#include <pwd.h>#include <sys/time.h>#include <sys/resource.h>#include <sys/wait.h>int executeprogram(const char *command, const char *directory, unsigned timelimit, unsigned long &realtime, unsigned int &memusage){	pid_t pid = fork();	if (pid < 0)	{		perror("fork ");		_exit(EXECUTE_STATUS_SYSTEMERROR);	}	else if (pid == 0)	{		passwd *pd = getpwnam("nobody");		if (pd == NULL)		{			perror("getpwnam ");			_exit(EXECUTE_STATUS_SYSTEMERROR);		}		if (setresuid(pd->pw_uid, pd->pw_uid, pd->pw_uid) != 0)		{			perror("setresuid ");			_exit(EXECUTE_STATUS_SYSTEMERROR);		}		if (setresgid(pd->pw_gid, pd->pw_gid, pd->pw_gid) != 0)		{			perror("setresgid ");			_exit(EXECUTE_STATUS_SYSTEMERROR);		}		if (directory != NULL)			if(chdir(directory) != 0)			{				perror("chdir ");				_exit(EXECUTE_STATUS_SYSTEMERROR);			}		rlimit limit;		limit.rlim_cur = limit.rlim_max = 0;		if (setrlimit(RLIMIT_NPROC, &limit) != 0)		{			perror("setrlimit ");			_exit(EXECUTE_STATUS_SYSTEMERROR);		}		limit.rlim_cur = limit.rlim_max = timelimit;		if (setrlimit(RLIMIT_CPU, &limit) != 0)		{			perror("setrlimit ");			_exit(EXECUTE_STATUS_SYSTEMERROR);		}		stdin = freopen("/dev/null", "r", stdin);		stdout = freopen("/dev/null", "w", stdout);		stderr = freopen("/dev/null", "w", stderr);		setsid();		execlp(command, command, NULL);		perror("execlp ");		_exit(EXECUTE_STATUS_SYSTEMERROR);	}	else	{		int status;		rusage ru;		if (wait4(pid, &status, 0, &ru) < 0)			return EXECUTE_STATUS_SYSTEMERROR;		realtime = ru.ru_utime.tv_sec * 1000000 + ru.ru_utime.tv_usec + ru.ru_stime.tv_sec * 1000000 + ru.ru_stime.tv_usec;		memusage = ru.ru_minflt * 4;		if (WIFSIGNALED(status) && (WTERMSIG(status) == SIGXCPU || WTERMSIG(status) == SIGKILL))			return EXECUTE_STATUS_OVERTIME;		if (WIFEXITED(status) && WEXITSTATUS(status) != 0)			return WEXITSTATUS(status) == EXECUTE_STATUS_SYSTEMERROR ? EXECUTE_STATUS_SYSTEMERROR :  EXECUTE_STATUS_FAILTORUN;		else return EXECUTE_STATUS_NORMAL;	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -