]> skyeroc.xyz Git - dgamelaunch/commitdiff
Allow dgamelaunch to kill processes that are idle for too long with SIGHUP. From...
authorPasi Kallinen <paxed@alt.org>
Tue, 2 Mar 2010 18:02:51 +0000 (18:02 +0000)
committerPasi Kallinen <paxed@alt.org>
Tue, 2 Mar 2010 18:02:51 +0000 (18:02 +0000)
git-svn-id: svn://katsu.triplehelix.org/dgamelaunch/trunk@530 db0b04b0-f4d1-0310-9a6d-de3e77497b0e

config.l
config.y
dgamelaunch.h
dgl-common.c
examples/dgamelaunch.conf
ttyrec.c
ttyrec.h

index e94f2522b859dbdf9edfda722ccc6dbdbd626ce6..17061af6c658ec16fd7a9cbc7d114d303f9dbb48 100644 (file)
--- a/config.l
+++ b/config.l
@@ -82,6 +82,7 @@ cursor                { return TYPE_CURSOR; }
 "lockfile"     { return TYPE_PATH_LOCKFILE; }
 "inprogressdir" { return TYPE_PATH_INPROGRESS; }
 "game_args"    { return TYPE_GAME_ARGS; }
+"max_idle_time"        { return TYPE_MAX_IDLE_TIME; }
 "rc_fmt"       { return TYPE_RC_FMT; }
 "ttyrecdir"    { return TYPE_PATH_TTYREC; }
 server_id      { return TYPE_SERVER_ID; }
index 66947961d26da3f92c1adfbcce76702dcaab1b88..e10b990e1359488b040303b161989d49c113f8d4 100644 (file)
--- a/config.y
+++ b/config.y
@@ -41,6 +41,7 @@ static const char* lookup_token (int t);
 %token TYPE_PATH_PASSWD TYPE_PATH_LOCKFILE TYPE_PATH_TTYREC
 %token TYPE_MALSTRING TYPE_PATH_INPROGRESS TYPE_GAME_ARGS TYPE_RC_FMT
 %token TYPE_CMDQUEUE TYPE_DEFINE_MENU TYPE_BANNER_FILE TYPE_CURSOR
+%token TYPE_MAX_IDLE_TIME
 %token <s> TYPE_VALUE
 %token <i> TYPE_NUMBER TYPE_CMDQUEUENAME
 %type  <kt> KeyType
@@ -355,6 +356,10 @@ game_definition : TYPE_CMDQUEUE
        {
            /* nothing */
        }
+        | TYPE_MAX_IDLE_TIME '=' TYPE_NUMBER
+        {
+            myconfig[ncnf]->max_idle_time = $3;
+        }
        | KeyType '=' TYPE_VALUE
        {
            switch ( $1 ) {
@@ -561,6 +566,7 @@ const char* lookup_token (int t)
     case TYPE_PATH_TTYREC: return "ttyrecdir";
     case TYPE_PATH_INPROGRESS: return "inprogressdir";
     case TYPE_GAME_ARGS: return "game_args";
+    case TYPE_MAX_IDLE_TIME: return "max_idle_time";
     case TYPE_RC_FMT: return "rc_fmt";
     case TYPE_WATCH_SORTMODE: return "sortmode";
     case TYPE_SERVER_ID: return "server_id";
index 7b3dc5f5ac25a1c0b6749a54ed3858c6bdef9039..636808ebe10383d5c6955eddc3f9b23b320c6e19 100644 (file)
@@ -101,6 +101,7 @@ struct dg_config
     char **bin_args; /* args for game binary */
     char *rc_fmt;
     struct dg_cmdpart *cmdqueue;
+    int max_idle_time;
 };
 
 struct dg_globalconfig
index 67581e888bef0dace14fcc03a764c9d43175b6a5..bd5110869f070c0132d151f5618ec10b58aea01b 100644 (file)
@@ -45,7 +45,8 @@ struct dg_config defconfig = {
   /* num_args = */ 0,
   /* bin_args = */ NULL,
   /* rc_fmt = */ "%rrcfiles/%n.nethackrc", /* [dglroot]rcfiles/[username].nethackrc */
-  /* cmdqueue = */ NULL
+  /* cmdqueue = */ NULL,
+  /* max_idle_time = */ 0
 };
 
 char* config = NULL;
index 868e64416be356781eae1d2914af371bdbe14894..827c64562c490deb79603454aabfce1d09983a17 100644 (file)
@@ -192,6 +192,10 @@ menu["watchmenu_help"] {
 #  # From inside the jail, the default .nethackrc that is copied for new users.
 #  # rc_template = "/dgl-default-rcfile"
 #
+#  # If player idles longer than max_idle_time seconds, the game will
+#  # receive a sighup. Default value is 0, which disables the idling timer.
+#  max_idle_time = 2000
+#
 #  # Make sure the inprogress dir actually exists. default is "inprogress/"
 #  # Each game you define here must have it's own.
 #  inprogressdir = "%rinprogress-nethackstub/"
index 28af7e37cff06169e5caeb5866067572f48fc1be..ea9f0d7713c912c1701081c3683fc387ab1ceda1 100644 (file)
--- a/ttyrec.c
+++ b/ttyrec.c
@@ -71,6 +71,7 @@
 #endif
 
 int slave;
+pid_t dgl_parent;
 pid_t child, subchild;
 pid_t input_child;
 char* ipfile = NULL;
@@ -89,6 +90,8 @@ ttyrec_main (int game, char *username, char *ttyrec_path, char* ttyrec_filename)
 {
   char dirname[100];
 
+  /* Note our PID to let children kill the main dgl process for idling */
+  dgl_parent = getpid();
   child = subchild = input_child = 0;
 
   if (!ttyrec_path) {
@@ -140,7 +143,7 @@ ttyrec_main (int game, char *username, char *ttyrec_path, char* ttyrec_filename)
         {
           close (slave);
           ipfile = gen_inprogress_lock (game, child, ttyrec_filename);
-          dooutput ();
+          dooutput (myconfig[game]->max_idle_time);
         }
       else
          doshell (game, username);
@@ -279,8 +282,17 @@ check_output (const char *str, int len)
     }
 }
 
+
+
+void
+game_idle_kill(int signal)
+{
+    kill(child, SIGHUP);
+    kill(dgl_parent, SIGHUP);
+}
+
 void
-dooutput ()
+dooutput (int max_idle_time)
 {
   int cc;
   time_t tvec, time ();
@@ -289,6 +301,8 @@ dooutput ()
   setbuf (stdout, NULL);
   (void) close (0);
   tvec = time ((time_t *) NULL);
+  /* Set up SIGALRM handler to kill idle games */
+  signal(SIGALRM, game_idle_kill);
   for (;;)
     {
       Header h;
@@ -296,6 +310,10 @@ dooutput ()
       cc = read (master, obuf, BUFSIZ);
       if (cc <= 0)
         break;
+
+      if (max_idle_time)
+          alarm(max_idle_time);
+
       if (uflg)
         check_output (obuf, cc);
       h.len = cc;
index 23d951a93d5eabf145462010fe1f9159258ba0e9..34aa3c013f19aedd028798764f068a0a0be3b57a 100644 (file)
--- a/ttyrec.h
+++ b/ttyrec.h
@@ -16,7 +16,7 @@ extern void fail (void);
 extern void fixtty (void);
 extern void getslave (void);
 extern void doinput (void);
-extern void dooutput (void);
+extern void dooutput (int max_idle_time);
 extern void doshell (int, char *);
 extern void finish (int);
 extern void remove_ipfile (void);