64,683
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <sys/resource.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
#include <sys/wait.h>
#include <sys/ptrace.h>
int setLimit(int type, long val){
rlimit res;
res.rlim_cur = val;
res.rlim_max = val+1;
return setrlimit(type, &res);
}
int main()
{
pid_t pid;
pid = fork();
if(pid < 0){
printf("%d:%s\n", errno, strerror(errno));
return -1;
} else if (pid == 0){
if(setLimit(RLIMIT_DATA, 1024*1024*1) < 0){ // byte
printf("%d:%s\n", errno, strerror(errno));
return -1;
}
if(setLimit(RLIMIT_CPU, 3) < 0){ // second
printf("%d:%s\n", errno, strerror(errno));
return -1;
}
if(ptrace(PTRACE_TRACEME, 0, NULL, NULL) < 0){
printf("%d:%s\n", errno, strerror(errno));
return -1;
}
execlp("./test", "test", NULL);
} else {
// father
int status;
int ret;
while((ret = waitpid(pid, &status, 0)) > 0){
if(WIFSIGNALED(status)){
//child over
printf("child process over by kill sig.\n");
break;
}
if(!WIFSTOPPED(status)){
printf("child process over succeed.\n");
break;
}
int sig = WSTOPSIG(status);
if(sig != SIGTRAP){
//! debug sig
switch(sig){
case SIGFPE:
printf("float point err.\n");
break;
case SIGXCPU:
printf("TLE.\n");
break;
case SIGBUS:
case SIGSEGV:
printf("segment err.\n");
break;
case SIGILL:
case SIGKILL:
printf("runtime err.\n");
break;
case SIGALRM:
;
}
//retrans to child
ptrace(PTRACE_SYSCALL, pid, NULL, sig);
continue;
}
ptrace(PTRACE_SYSCALL, pid, NULL, NULL);
}
printf("father process over.\n");
}
return 0;
}
int main()
{
int a[1000000] = {0};
for(;;);
return 0;
}
//获得系统调用相关的寄存器值
ptrace(PTRACE_GETREGS, this->pid, 0, ®s);
switch(regs.ORIG_EAX) {
case SYS_brk:
if(!insyscall) {
brk = (unsigned long)regs.EBX;
insyscall = true;
}else{
if(((unsigned long)regs.EAX) < brk) {
DLOG(INFO) << "TraceProcess()::traceMe(): brk request " << brk << " return " << regs.EAX << "\n";
//超内存了
this->_result = MEMORY_LIMIT_EXCEEDED;
ptrace(PTRACE_KILL, this->pid, 0, 0);
continue;
}
insyscall = false;
}
break;
}