Complete.Org: Mailing Lists: Archives: freeciv-dev: January 2005:
[Freeciv-Dev] (PR#11887) CivServer SIGINT Handler Patch
Home

[Freeciv-Dev] (PR#11887) CivServer SIGINT Handler Patch

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: dl@xxxxxxxxx
Subject: [Freeciv-Dev] (PR#11887) CivServer SIGINT Handler Patch
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 30 Jan 2005 00:04:14 -0800
Reply-to: bugs@xxxxxxxxxxx

<URL: http://bugs.freeciv.org/Ticket/Display.html?id=11887 >

Here is I think a better version of the patch.

- It checks for signal.h instead of checking for WIN32_NATIVE.  In
particular, signal.h is present on my mingw32 cross-compiler and I can
compile in the sigint support (although I can't test it since wine
catches the sigint and exits).

- It uses timers for more accurate timing.

After this gets a little testing in CVS I'd like to add support for
SIGHUP and SIGTERM.  We may have to do a little research on how programs
are supposed to respond to these signals, but at least with SIGTERM the
server should save the game before exiting.  There was an old patch that
did this that we can probably dig up off of the freeciv-dev archives.

-jason

Index: configure.ac
===================================================================
RCS file: /home/freeciv/CVS/freeciv/configure.ac,v
retrieving revision 1.92
diff -u -r1.92 configure.ac
--- configure.ac        27 Jan 2005 02:14:40 -0000      1.92
+++ configure.ac        30 Jan 2005 08:01:22 -0000
@@ -463,8 +463,8 @@
 dnl Checks for header files.
 AC_HEADER_STDC
 AC_HEADER_SYS_WAIT
-AC_CHECK_HEADERS(fcntl.h sys/time.h sys/types.h unistd.h sys/utsname.h \
-                 sys/file.h libintl.h)
+AC_CHECK_HEADERS([fcntl.h sys/time.h sys/types.h unistd.h sys/utsname.h \
+                  sys/file.h libintl.h signal.h])
 AC_HEADER_STDBOOL
 dnl Avoid including the unix emulation layer if we build mingw executables
 dnl There would be type conflicts between winsock and bsd/unix includes
Index: server/civserver.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/civserver.c,v
retrieving revision 1.228
diff -u -r1.228 civserver.c
--- server/civserver.c  21 Jan 2005 00:00:45 -0000      1.228
+++ server/civserver.c  30 Jan 2005 08:01:23 -0000
@@ -19,6 +19,10 @@
 #include <stdlib.h>
 #include <string.h>
 
+#ifdef HAVE_SIGNAL_H
+#include <signal.h>
+#endif
+
 #ifdef GENERATING_MAC  /* mac header(s) */
 #include <Controls.h>
 #include <Dialogs.h>
@@ -33,6 +37,7 @@
 #include "log.h"
 #include "shared.h"
 #include "support.h"
+#include "timing.h"
 #include "version.h"
 
 #include "console.h"
@@ -46,6 +51,32 @@
 static void Mac_options(int argc);  /* don't need argv */
 #endif
 
+#ifdef HAVE_SIGNAL_H
+#  define USE_INTERRUPT_HANDLERS
+#endif
+
+#ifdef USE_INTERRUPT_HANDLERS
+/**************************************************************************
+  This function is called when a SIGINT (ctrl-c) is received.  It will exit
+  only if two SIGINTs are received within a second.
+
+  TODO: SIGHUP and SIGTERM should be handled too.  At a minimum we should
+  save the game before exiting.
+**************************************************************************/
+static void sigint_handler(int sig)
+{
+  static struct timer *timer = NULL;
+
+  if (timer && read_timer_seconds(timer) <= 1.0) {
+    exit(EXIT_SUCCESS);
+  } else if (!timer) {
+    freelog(LOG_NORMAL, _("You must interrupt Freeciv twice"
+                         " within one second to make it exit.\n"));
+  }
+  timer = renew_timer_start(timer, TIMER_USER, TIMER_ACTIVE);
+}
+#endif
+
 /**************************************************************************
  Entry point for Freeciv server.  Basically, does two things:
   1. Parses command-line arguments (possibly dialog, on mac).
@@ -69,6 +100,10 @@
 # endif
 #endif
 
+#ifdef USE_INTERRUPT_HANDLERS
+  signal(SIGINT, sigint_handler);
+#endif
+
   /* initialize server */
   srv_init();
 

[Prev in Thread] Current Thread [Next in Thread]