From 11eef77614844149eadec35cceafe0d70acd4c1c Mon Sep 17 00:00:00 2001 From: Pasi Kallinen Date: Thu, 25 Feb 2010 16:40:55 +0000 Subject: [PATCH] Allow setting resource limits on core dump size and memory usage. Idea and some code from crawl.develz.org dgl. git-svn-id: svn://katsu.triplehelix.org/dgamelaunch/trunk@523 db0b04b0-f4d1-0310-9a6d-de3e77497b0e --- configure.ac | 38 ++++++++++++++++++++++++++++++++++++++ dgamelaunch.c | 23 +++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/configure.ac b/configure.ac index 421add2..8034a99 100644 --- a/configure.ac +++ b/configure.ac @@ -108,6 +108,44 @@ if test "$enable_sqlite" = yes; then AC_DEFINE_UNQUOTED(USE_SQLITE_DB, "$dgl_sqlite_db", [Path and filename of the SQLite database.]) fi + + + +dgl_rlimit_core_default=157286400 +AC_ARG_WITH(rlimit-core, +[AC_HELP_STRING([--with-rlimit-core=SIZE], [Enable and set the core dump maximum size.])], +[dgl_rlimit_core=$withval; enable_rlimit=yes], [dgl_rlimit_core=$dgl_rlimit_core_default]) + +if test "$enable_rlimit" = yes; then + if test "$dgl_rlimit_core" = yes; then + dgl_rlimit_core=$dgl_rlimit_core_default + fi + AC_CHECK_HEADERS([sys/resource.h], [], [AC_MSG_ERROR([sys/resource.h not found.])], []) + AC_MSG_RESULT([Enabled and set maximum core dump size to $dgl_rlimit_core]) + AC_DEFINE(USE_RLIMIT,1,[Use getrlimit/setrlimit]) + AC_DEFINE_UNQUOTED(USE_RLIMIT_CORE, $dgl_rlimit_core, [Maximum core dump size]) + enable_rlimit=no +fi + +dgl_rlimit_as_default=104857600 +AC_ARG_WITH(rlimit-as, +[AC_HELP_STRING([--with-rlimit-as=SIZE], [Enable and set the maximum memory usage.])], +[dgl_rlimit_as=$withval; enable_rlimit=yes], [dgl_rlimit_as=$dgl_rlimit_as_default]) + +if test "$enable_rlimit" = yes; then + if test "$dgl_rlimit_as" = yes; then + dgl_rlimit_as=$dgl_rlimit_as_default + fi + AC_CHECK_HEADERS([sys/resource.h], [], [AC_MSG_ERROR([sys/resource.h not found.])], []) + AC_MSG_RESULT([Enabled and set maximum memory usage limit to $dgl_rlimit_as]) + AC_DEFINE(USE_RLIMIT,1,[Use getrlimit/setrlimit]) + AC_DEFINE_UNQUOTED(USE_RLIMIT_AS, $dgl_rlimit_as, [Maximum mem usage]) + enable_rlimit=no +fi + + + + AC_ARG_WITH(config-file, [AC_HELP_STRING([--with-config-file=PATH], [Define the path to the default configuration file.])], [configfile=$withval], [configfile="/etc/dgamelaunch.conf"]) diff --git a/dgamelaunch.c b/dgamelaunch.c index 7ec2e44..c57ceb2 100644 --- a/dgamelaunch.c +++ b/dgamelaunch.c @@ -50,6 +50,9 @@ #include #include /* ttyrec */ #include +#ifdef USE_RLIMIT +#include +#endif #include #include #include @@ -1987,6 +1990,9 @@ main (int argc, char** argv) int userchoice; char *tmp; char *wall_email_str = NULL; +#ifdef USE_RLIMIT + struct rlimit lim; +#endif #ifndef HAVE_SETPROCTITLE /* save argc, argv */ @@ -2082,6 +2088,23 @@ main (int argc, char** argv) if (!nhext && !nhauth) ttyrec_getpty (); +#ifdef USE_RLIMIT +#ifdef USE_RLIMIT_CORE + /* enable and set core dump size */ + if (!getrlimit(RLIMIT_CORE, &lim)) { + lim.rlim_cur = USE_RLIMIT_CORE; + setrlimit(RLIMIT_CORE, &lim); + } +#endif +#ifdef USE_RLIMIT_AS + /* set maximum memory usage */ + if (!getrlimit(RLIMIT_AS, &lim)) { + lim.rlim_cur = USE_RLIMIT_AS; + setrlimit(RLIMIT_AS, &lim); + } +#endif +#endif + if (geteuid () != globalconfig.shed_uid) { /* chroot */ -- 2.47.3