About the signal after trace the child process
I am working on a project of ACM online judge system.
I use the function ptrace() to trace the program submited.
Now,there are some puzzles.
Here is the code:
void sandbox(char* exec,char* input,char* output,int timelimit,int memlimit)
{
struct rusage usage;
struct rlimit limit;
int wait_val,sig;
pid_t pid;
unsigned long tmp;
unsigned long mem = 0;
double utime,stime;
struct user_regs_struct regs;
pid = fork();
if(pid == -1)
{
printf("fork() error!\n");
}
else if(pid == 0)
{
freopen(input,"r",stdin);
freopen(output,"w",stdout);
limit.rlim_cur = timelimit;
limit.rlim_max = timelimit;
setrlimit(RLIMIT_CPU,&limit);
limit.rlim_cur = memlimit * MEGA_BYTES;
limit.rlim_max = memlimit * MEGA_BYTES;
setrlimit(RLIMIT_AS,&limit);
ptrace(PTRACE_TRACEME, 0, NULL, NULL);
execv(exec,NULL);
}
else
{
wait(&wait_val);
while (wait_val == 1407 )
{
ptrace(PTRACE_GETREGS,pid,NULL,®s);
if(regs.orig_rax == SYS_write)
{
ptrace(PTRACE_SINGLESTEP, pid, NULL, NULL);
}
else
{
ptrace(PTRACE_SYSCALL,pid,NULL,NULL);
}
tmp = get_curr_mem(pid);
if(mem < tmp)
mem = tmp;
wait(&wait_val);
}
printf("wait_val:%d\n",wait_val);
ptrace(PTRACE_DETACH,pid,(char *)1,0);
getrusage(RUSAGE_CHILDREN,&usage);
}
}
utime = usage.ru_utime.tv_sec * 1000.0 + usage.ru_utime.tv_usec / 1000.0;
stime = usage.ru_stime.tv_sec * 1000.0 + usage.ru_stime.tv_usec / 1000.0;
printf("The total run mem:%ld\n",mem);
printf("user time:%0.0lf\n",utime);
printf("sys time:%0.0lf\n",stime);
printf("Run Time:%0.0lf\n",utime + stime);
}
when use the wait() ,sometimes it may get a siganl greater than 63,such as 2175.
Why it have happend?
Can anyone help me?
Thank you.
|