Skip to content

Commit 26f4dda

Browse files
committed
Make cgroups a configure option (default enabled if available).
1 parent e89fae3 commit 26f4dda

File tree

4 files changed

+69
-22
lines changed

4 files changed

+69
-22
lines changed

configure.ac

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ fi
125125

126126
# }}}
127127

128-
# {{{ runguard user
128+
# {{{ runguard
129129
AC_MSG_CHECKING([runuser])
130130
AC_ARG_WITH([runuser], [AS_HELP_STRING([--with-runuser=USER],
131131
[Unprivileged user under which to run submissions (default: domjudge-run).])], [], [])
@@ -137,6 +137,32 @@ else
137137
AC_SUBST(RUNUSER,$with_runuser)
138138
AC_MSG_RESULT($RUNUSER)
139139
fi
140+
141+
# Check for using Linux cgroups for memory control
142+
AC_CHECK_LIB(cgroup, cgroup_init, AC_SUBST(LIBCGROUP,[-lcgroup]))
143+
144+
if test "x$LIBCGROUP" != x; then
145+
use_cgroups=yes
146+
else
147+
use_cgroups=no
148+
fi
149+
AC_ARG_ENABLE(cgroups,AS_HELP_STRING([--enable-cgroups],
150+
[Use Linux cgroups for memory control (default: $use_cgroups).]), [], [])
151+
152+
if test "x$enable_cgroups" = xno; then
153+
use_cgroups=no
154+
elif test "x$enable_cgroups" != x; then
155+
use_cgroups=yes
156+
fi
157+
158+
# Finally define an export variable that can be substituted into
159+
# etc/runguard-config.h.in:
160+
if test "x$use_cgroups" = xyes; then
161+
AC_SUBST(USE_CGROUPS,1)
162+
else
163+
AC_SUBST(USE_CGROUPS,0)
164+
fi
165+
140166
# }}}
141167

142168
# {{{ FHS directory structure
@@ -225,9 +251,6 @@ AC_PROG_MKDIR_P
225251
# check for htpasswd/htpasswd2
226252
AC_CHECK_PROGS(HTPASSWD,[htpasswd htpasswd2],[no htpasswd binary found])
227253

228-
# Check for including optional libcgroup
229-
AC_CHECK_LIB(cgroup, cgroup_init, AC_SUBST(LIBCGROUP,[-lcgroup]))
230-
231254
# Check for including optional libmagic.
232255
AC_CHECK_LIB(magic,magic_open,AC_SUBST(LIBMAGIC,[-lmagic]))
233256

@@ -315,6 +338,14 @@ echo " * default user........: $DOMJUDGE_USER"
315338
echo " * runguard user.......: $RUNUSER"
316339
echo " * webserver group.....: $WEBSERVER_GROUP"
317340
echo ""
341+
echo " * use Linux cgroups...: $use_cgroups"
342+
echo ""
343+
if test "x$CHECKTESTDATA_ENABLED" = xyes ; then
344+
echo " * checktestdata.......: enabled"
345+
else
346+
echo " * checktestdata.......: disabled"
347+
fi
348+
echo ""
318349
if test $SUBMIT_DEFAULT -eq 0 ; then # {{{
319350
echo " * submitclient........: disabled"
320351
else

etc/runguard-config.h.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77

88
#define CHROOT_PREFIX "@judgehost_judgedir@"
99

10+
#define USE_CGROUPS @USE_CGROUPS@
11+
1012
#endif /* _RUNGUARD_CONFIG_ */

judge/runguard.c

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
runguard -- run command with restrictions.
3-
Copyright (C) 2004-2012 Jaap Eldering (eldering@a-eskwadraat.nl).
3+
Copyright (C) 2004-2013 Jaap Eldering (eldering@a-eskwadraat.nl)
4+
Copyright (C) 2013 Keith Johnson
45
56
Multiple minor improvements ported from the DOMjudge-ETH tree.
67
@@ -39,6 +40,9 @@
3940

4041
#include "config.h"
4142

43+
/* Some system/site specific config: VALID_USERS, CHROOT_PREFIX */
44+
#include "runguard-config.h"
45+
4246
/* For chroot(), which is not POSIX. */
4347
#define _BSD_SOURCE
4448

@@ -64,14 +68,13 @@
6468
#include <time.h>
6569
#include <math.h>
6670
#include <limits.h>
67-
#ifdef HAVE_LIBCGROUP_H
71+
#if ( USE_CGROUPS == 1 )
6872
#include <inttypes.h>
6973
#include <libcgroup.h>
74+
#else
75+
#undef USE_CGROUPS
7076
#endif
7177

72-
/* Some system/site specific config: VALID_USERS, CHROOT_PREFIX */
73-
#include "runguard-config.h"
74-
7578
#define PROGRAM "runguard"
7679
#define VERSION DOMJUDGE_VERSION "/" REVISION
7780
#define AUTHORS "Jaap Eldering"
@@ -103,7 +106,7 @@ char *stdoutfilename;
103106
char *stderrfilename;
104107
char *exitfilename;
105108
char *timefilename;
106-
#ifdef HAVE_LIBCGROUP_H
109+
#ifdef USE_CGROUPS
107110
char *cgroupname;
108111
#endif
109112

@@ -126,7 +129,7 @@ int show_help;
126129
int show_version;
127130

128131
double runtime, cputime; /* in seconds */
129-
#ifdef HAVE_LIBCGROUP_H
132+
#ifdef USE_CGROUPS
130133
int64_t memsize;
131134
#else
132135
rlim_t memsize;
@@ -316,8 +319,10 @@ void output_exit_time(int exitcode, double timediff)
316319
}
317320
}
318321
}
319-
#ifdef HAVE_LIBCGROUP_H
320-
void output_cgroup_stats() {
322+
323+
#ifdef USE_CGROUPS
324+
void output_cgroup_stats()
325+
{
321326
int ret;
322327
int64_t max_usage;
323328
struct cgroup *cg;
@@ -448,7 +453,7 @@ void setrestrictions()
448453
}
449454

450455
/* Memory limits may be handled by cgroups now */
451-
#ifndef HAVE_LIBCGROUP_H
456+
#ifndef USE_CGROUPS
452457
if ( memsize!=RLIM_INFINITY ) {
453458
verbose("setting memory limits to %d bytes",(int)memsize);
454459
lim.rlim_cur = lim.rlim_max = memsize;
@@ -533,8 +538,9 @@ void setrestrictions()
533538
if ( geteuid()==0 || getuid()==0 ) error(0,"root privileges not dropped. Do not run judgedaemon as root.");
534539
}
535540

536-
#ifdef HAVE_LIBCGROUP_H
537-
void cgroup_create() {
541+
#ifdef USE_CGROUPS
542+
void cgroup_create()
543+
{
538544
int ret;
539545
struct cgroup *cg;
540546
struct cgroup_controller *cg_controller;
@@ -558,7 +564,9 @@ void cgroup_create() {
558564

559565
cgroup_free(&cg);
560566
}
561-
void cgroup_attach() {
567+
568+
void cgroup_attach()
569+
{
562570
int ret;
563571
struct cgroup *cg;
564572

@@ -579,7 +587,9 @@ void cgroup_attach() {
579587

580588
cgroup_free(&cg);
581589
}
582-
void cgroup_delete() {
590+
591+
void cgroup_delete()
592+
{
583593
int ret;
584594
struct cgroup *cg;
585595

@@ -606,7 +616,7 @@ int main(int argc, char **argv)
606616
fd_set readfds;
607617
pid_t pid;
608618
int i, r, nfds;
609-
#ifdef HAVE_LIBCGROUP_H
619+
#ifdef USE_CGROUPS
610620
int ret;
611621
#endif
612622
int status;
@@ -772,7 +782,7 @@ int main(int argc, char **argv)
772782
error(errno,"installing signal handler");
773783
}
774784

775-
#ifdef HAVE_LIBCGROUP_H
785+
#ifdef USE_CGROUPS
776786
/* Make libcgroup ready for use */
777787
ret = cgroup_init();
778788
if ( ret!=0 ) {
@@ -808,7 +818,7 @@ int main(int argc, char **argv)
808818
and all its children can be killed off with one signal. */
809819
if ( setsid()==-1 ) error(errno,"setsid failed");
810820

811-
#ifdef HAVE_LIBCGROUP_H
821+
#ifdef USE_CGROUPS
812822
/* Put the child process in the cgroup */
813823
cgroup_attach();
814824
#endif
@@ -968,7 +978,7 @@ int main(int argc, char **argv)
968978
exitcode = WEXITSTATUS(status);
969979
}
970980

971-
#ifdef HAVE_LIBCGROUP_H
981+
#ifdef USE_CGROUPS
972982
output_cgroup_stats();
973983
cgroup_delete();
974984
#endif

paths.mk.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ INSTALL = @INSTALL@
3838
# Build checktestdata program?
3939
CHECKTESTDATA_ENABLED = @CHECKTESTDATA_ENABLED@
4040

41+
# Use Linux cgroups?
42+
USE_CGROUPS = @USE_CGROUPS@
43+
4144
# Submit protocols
4245
SUBMIT_DEFAULT = @SUBMIT_DEFAULT@
4346
SUBMIT_ENABLE_CMD = @SUBMIT_ENABLE_CMD@
@@ -167,6 +170,7 @@ define substconfigvars
167170
-e 's,@WEBSERVER_GROUP[@],@WEBSERVER_GROUP@,g' \
168171
-e 's,@BEEP[@],@BEEP@,g' \
169172
-e 's,@RUNUSER[@],@RUNUSER@,g' \
173+
-e 's,@USE_CGROUPS[@],@USE_CGROUPS@,g' \
170174
-e 's,@SUBMIT_DEFAULT[@],@SUBMIT_DEFAULT@,g' \
171175
-e 's,@SUBMIT_ENABLE_CMD[@],@SUBMIT_ENABLE_CMD@,g' \
172176
-e 's,@SUBMIT_ENABLE_WEB[@],@SUBMIT_ENABLE_WEB@,g' \

0 commit comments

Comments
 (0)