Skip to content

Commit 9017fcf

Browse files
committed
Group all cgroup functions together.
1 parent 26f4dda commit 9017fcf

File tree

1 file changed

+71
-73
lines changed

1 file changed

+71
-73
lines changed

judge/runguard.c

Lines changed: 71 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,77 @@ void output_cgroup_stats()
345345

346346
cgroup_free(&cg);
347347
}
348-
#endif
348+
349+
void cgroup_create()
350+
{
351+
int ret;
352+
struct cgroup *cg;
353+
struct cgroup_controller *cg_controller;
354+
355+
cg = cgroup_new_cgroup(cgroupname);
356+
if (!cg) {
357+
error(0,"cgroup_new_cgroup");
358+
}
359+
360+
/* Set up the memory restrictions; these two options limit ram use
361+
and ram+swap use. They are the same so no swapping can occur */
362+
cg_controller = cgroup_add_controller(cg, "memory");
363+
cgroup_add_value_int64(cg_controller, "memory.limit_in_bytes", memsize);
364+
cgroup_add_value_int64(cg_controller, "memory.memsw.limit_in_bytes", memsize);
365+
366+
/* Perform the actual creation of the cgroup */
367+
ret = cgroup_create_cgroup(cg, 1);
368+
if ( ret!=0) {
369+
error(0,"creating cgroup - %s(%d)", cgroup_strerror(ret), ret);
370+
}
371+
372+
cgroup_free(&cg);
373+
}
374+
375+
void cgroup_attach()
376+
{
377+
int ret;
378+
struct cgroup *cg;
379+
380+
cg = cgroup_new_cgroup(cgroupname);
381+
if (!cg) {
382+
error(0,"cgroup_new_cgroup");
383+
}
384+
ret = cgroup_get_cgroup(cg);
385+
if ( ret!=0 ) {
386+
error(0,"get cgroup information - %s(%d)", cgroup_strerror(ret), ret);
387+
}
388+
389+
/* Attach task to the cgroup */
390+
ret = cgroup_attach_task(cg);
391+
if ( ret!=0 ) {
392+
error(0,"attach task to cgroup - %s(%d)", cgroup_strerror(ret), ret);
393+
}
394+
395+
cgroup_free(&cg);
396+
}
397+
398+
void cgroup_delete()
399+
{
400+
int ret;
401+
struct cgroup *cg;
402+
403+
cg = cgroup_new_cgroup(cgroupname);
404+
if (!cg) {
405+
error(0,"cgroup_new_cgroup");
406+
}
407+
ret = cgroup_get_cgroup(cg);
408+
if ( ret!=0 ) {
409+
error(0,"get cgroup information - %s(%d)", cgroup_strerror(ret), ret);
410+
}
411+
/* Clean up our cgroup */
412+
ret = cgroup_delete_cgroup(cg, 1);
413+
if ( ret!=0 ) {
414+
error(0,"deleting cgroup - %s(%d)", cgroup_strerror(ret), ret);
415+
}
416+
cgroup_free(&cg);
417+
}
418+
#endif // USE_CGROUPS
349419

350420
void terminate(int sig)
351421
{
@@ -538,78 +608,6 @@ void setrestrictions()
538608
if ( geteuid()==0 || getuid()==0 ) error(0,"root privileges not dropped. Do not run judgedaemon as root.");
539609
}
540610

541-
#ifdef USE_CGROUPS
542-
void cgroup_create()
543-
{
544-
int ret;
545-
struct cgroup *cg;
546-
struct cgroup_controller *cg_controller;
547-
548-
cg = cgroup_new_cgroup(cgroupname);
549-
if (!cg) {
550-
error(0,"cgroup_new_cgroup");
551-
}
552-
553-
/* Set up the memory restrictions; these two options limit ram use
554-
and ram+swap use. They are the same so no swapping can occur */
555-
cg_controller = cgroup_add_controller(cg, "memory");
556-
cgroup_add_value_int64(cg_controller, "memory.limit_in_bytes", memsize);
557-
cgroup_add_value_int64(cg_controller, "memory.memsw.limit_in_bytes", memsize);
558-
559-
/* Perform the actual creation of the cgroup */
560-
ret = cgroup_create_cgroup(cg, 1);
561-
if ( ret!=0) {
562-
error(0,"creating cgroup - %s(%d)", cgroup_strerror(ret), ret);
563-
}
564-
565-
cgroup_free(&cg);
566-
}
567-
568-
void cgroup_attach()
569-
{
570-
int ret;
571-
struct cgroup *cg;
572-
573-
cg = cgroup_new_cgroup(cgroupname);
574-
if (!cg) {
575-
error(0,"cgroup_new_cgroup");
576-
}
577-
ret = cgroup_get_cgroup(cg);
578-
if ( ret!=0 ) {
579-
error(0,"get cgroup information - %s(%d)", cgroup_strerror(ret), ret);
580-
}
581-
582-
/* Attach task to the cgroup */
583-
ret = cgroup_attach_task(cg);
584-
if ( ret!=0 ) {
585-
error(0,"attach task to cgroup - %s(%d)", cgroup_strerror(ret), ret);
586-
}
587-
588-
cgroup_free(&cg);
589-
}
590-
591-
void cgroup_delete()
592-
{
593-
int ret;
594-
struct cgroup *cg;
595-
596-
cg = cgroup_new_cgroup(cgroupname);
597-
if (!cg) {
598-
error(0,"cgroup_new_cgroup");
599-
}
600-
ret = cgroup_get_cgroup(cg);
601-
if ( ret!=0 ) {
602-
error(0,"get cgroup information - %s(%d)", cgroup_strerror(ret), ret);
603-
}
604-
/* Clean up our cgroup */
605-
ret = cgroup_delete_cgroup(cg, 1);
606-
if ( ret!=0 ) {
607-
error(0,"deleting cgroup - %s(%d)", cgroup_strerror(ret), ret);
608-
}
609-
cgroup_free(&cg);
610-
}
611-
#endif
612-
613611
int main(int argc, char **argv)
614612
{
615613
sigset_t sigmask, emptymask;

0 commit comments

Comments
 (0)