Skip to content

Instantly share code, notes, and snippets.

@ubergeek42
Created May 19, 2014 17:15
Show Gist options
  • Save ubergeek42/f4a557b2cffcb277ad1d to your computer and use it in GitHub Desktop.
Save ubergeek42/f4a557b2cffcb277ad1d to your computer and use it in GitHub Desktop.
protect against forksbombs and friends in runguard
diff --git a/judge/runguard.c b/judge/runguard.c
index 84d7f40..2cb28c1 100644
--- a/judge/runguard.c
+++ b/judge/runguard.c
@@ -462,6 +462,21 @@ void cgroup_attach()
cgroup_free(&cg);
}
+void cgroup_kill()
+{
+ int ret;
+ void *handle = NULL;
+ pid_t pid;
+
+ /* kill any remaining tasks, and wait for them to be gone */
+ while(1) {
+ ret = cgroup_get_task_begin(cgroupname, "memory", &handle, &pid);
+ cgroup_get_task_end(&handle);
+ if (ret == ECGEOF) break;
+ kill(pid, SIGKILL);
+ }
+}
+
void cgroup_delete()
{
int ret;
@@ -1151,6 +1166,7 @@ int main(int argc, char **argv)
#ifdef USE_CGROUPS
output_cgroup_stats();
+ cgroup_kill();
cgroup_delete();
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment