[gnome-chess] Don't leave an orphaned engine if we crash



commit 5f86c3a299c4a2fa66aa27b890201776e157aee1
Author: Michael Catanzaro <mcatanzaro gnome org>
Date:   Sun Jan 12 10:41:50 2014 -0600

    Don't leave an orphaned engine if we crash
    
    If we crash or receive a signal that kills us, the chess engine might
    live on forever. Linux (and only Linux) provides a nice way to avoid
    this by tying the life of a child process to its parent. Use it.
    
    (To get equivalent behavior on other systems, we would need to fork a
    third process to function as a monitor, which is not worth the bother.
    Non-Linux users should simply not crash the game.)

 configure.ac          |    3 +++
 src/Makefile.am       |    5 +++++
 src/chess-engine.vala |    4 ++++
 src/prctl.vapi        |    9 +++++++++
 4 files changed, 21 insertions(+), 0 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index caf1014..878d834 100644
--- a/configure.ac
+++ b/configure.ac
@@ -33,6 +33,9 @@ PKG_CHECK_MODULES(TEST, [
   gio-2.0
 ])
 
+AC_CHECK_HEADERS([linux/prctl.h])
+AM_CONDITIONAL([WITH_PRCTL], [test x$ac_cv_header_linux_prctl_h = xyes])
+
 AC_PATH_PROG([APPDATA_VALIDATE], [appdata-validate], [/bin/true])
 AC_PATH_PROG([DESKTOP_FILE_VALIDATE], [desktop-file-validate], [/bin/true])
 
diff --git a/src/Makefile.am b/src/Makefile.am
index 36d8dec..014d521 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -9,6 +9,7 @@ gnome_chess_SOURCES = \
        gl.vapi \
        glu.vapi \
        glx.vapi \
+       prctl.vapi \
        3ds.vala \
        gnome-chess.vala \
        ai-profile.vala \
@@ -69,6 +70,10 @@ gnome_chess_VALAFLAGS = \
     --pkg posix \
     --pkg sqlite3
 
+if WITH_PRCTL
+    gnome_chess_VALAFLAGS += -D HAVE_LINUX_PRCTL_H
+endif
+
 CLEANFILES = \
        $(patsubst %.vala,%.c,$(filter %.vala, $(SOURCES))) \
        *_vala.stamp \
diff --git a/src/chess-engine.vala b/src/chess-engine.vala
index fba1926..9474296 100644
--- a/src/chess-engine.vala
+++ b/src/chess-engine.vala
@@ -64,7 +64,11 @@ public abstract class ChessEngine : Object
         {
             Process.spawn_async_with_pipes (null, argv, null,
                                             SpawnFlags.SEARCH_PATH | SpawnFlags.DO_NOT_REAP_CHILD,
+#if HAVE_LINUX_PRCTL_H
+                                            () => Prctl.prctl (Prctl.SET_PDEATHSIG, Posix.SIGTERM),
+#else
                                             null,
+#endif
                                             out pid, out stdin_fd, out stdout_fd, out stderr_fd);
         }
         catch (SpawnError e)
diff --git a/src/prctl.vapi b/src/prctl.vapi
new file mode 100644
index 0000000..c7c2513
--- /dev/null
+++ b/src/prctl.vapi
@@ -0,0 +1,9 @@
+[CCode (cprefix = "", lower_case_cprefix = "")]
+namespace Prctl
+{
+    [CCode (cheader_filename = "linux/prctl.h", cname = "PR_SET_PDEATHSIG")]
+    public const int SET_PDEATHSIG;
+
+    [CCode (cheader_filename = "sys/prctl.h", sentinel = "")]
+    public int prctl (int option, ...);
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]