I'm running a sort of "sandbox" in C on Ubuntu: it takes a program, and runs it safely under the user nobody
(and intercepts signals, etc). Also, it assigns memory and time limits, and measures time and memory usage.
(In case you're curious, it's for a sort of "online judge" to mark programs on test data)
Currently I've adapted the safeexec module from mooshak. Though most things work properly, the memory usage seems to be a problem. (It's highly inaccurate)
Now I've tried the advice here and parsed VM from /proc/pid/stat
, and now the accuracy problem is fixed. However, for programs that finish really quickly it doesn't work and just gives back 0.
The safeexec program seems to work like this:
- It
fork()
s - Uses
execv()
in the child process to run the desired program - Monitors the program from the parent process until the child process terminates (using
wait4
, which happens to return CPU usage - but not memory?)
So it parses/proc/../stat
of the child process (which has been replaced by the execv)
So why is VM in /proc/child_pid/stat
sometimes equal to 0?
Is it because the execv() finishes too quickly, and /proc/child_pid/stat
just isn't available?
If so, is there some sort of other way to get the memory usage of the child?
(Since this is meant to judge programs under a time limit, I can't afford something with a performance penalty like valgrind)
Thanks in advance.