Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add a fallback time check to prevent process from sleeping and not tr…

…iggering the time's limit
  • Loading branch information...
commit d0ad8c9c8584cd03642432ac4226fe526c3ebeee 1 parent c8a1a69
Jérémie Marguerie authored
Showing with 27 additions and 0 deletions.
  1. +23 −0 src/jail.cc
  2. +4 −0 src/jail.hh
View
23 src/jail.cc
@@ -42,6 +42,7 @@
#include <sys/wait.h>
#include <string.h>
#include <pthread.h>
+#include <unistd.h>
jail::jail(const jail::cmd_type& cmd) : cmd_(cmd)
{
@@ -49,6 +50,8 @@ jail::jail(const jail::cmd_type& cmd) : cmd_(cmd)
int jail::run()
{
+ timestamp_start_ = time(NULL);
+
if ((child_pid_ = fork()) == -1)
throw exec_exception("could not fork");
else if (!child_pid_)
@@ -179,6 +182,26 @@ size_t jail::check_time_limit()
kill_process();
return (ERR_MAX_TIME);
}
+
+ return check_time_limit_fallback();
+ }
+
+ return (0);
+}
+
+// If the program take more than 3 times the time allocated, terminate it.
+// It prevent programs from sleeping (and not using CPU time), and thus not
+// being killed.
+size_t jail::check_time_limit_fallback()
+{
+ if (time_limit_ != boost::none)
+ {
+ size_t timeout = 3 * (*time_limit_ / 1000);
+ if ((time(NULL) - timestamp_start_) > timeout)
+ {
+ kill_process();
+ return (ERR_MAX_TIME);
+ }
}
return (0);
View
4 src/jail.hh
@@ -39,6 +39,8 @@
# include <string>
# include <vector>
+# include <time.h>
+
# define SLEEP_TIME_MS 100
# define ERR_PROCESS_TERM 1
# define ERR_MAX_TIME 2
@@ -84,6 +86,7 @@ private:
size_t check_limits();
size_t check_time_limit();
+ size_t check_time_limit_fallback();
size_t check_memory_limit();
int handle_return_code(size_t res_thread, int return_code, int signum);
@@ -98,6 +101,7 @@ private:
boost::optional<size_t> time_limit_;
boost::optional<size_t> mem_limit_;
+ time_t timestamp_start_;
pid_t child_pid_;
};
Please sign in to comment.
Something went wrong with that request. Please try again.