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



commit 603dfa3debad8f8eb7a0a4efa68cd33bfa26435e
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 721bf34..d507e80 100644
--- a/configure.ac
+++ b/configure.ac
@@ -42,6 +42,9 @@ AC_SUBST([GNOME_CHESS_CFLAGS])
 TEST_CFLAGS="$TEST_CFLAGS -w"
 AC_SUBST([TEST_CFLAGS])
 
+AC_CHECK_HEADERS([linux/prctl.h])
+AM_CONDITIONAL([WITH_PRCTL], [test x$ac_cv_header_linux_prctl_h = xyes])
+
 dnl ###########################################################################
 dnl Internationalization
 dnl ###########################################################################
diff --git a/src/Makefile.am b/src/Makefile.am
index 1c54089..18edb60 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 \
@@ -66,6 +67,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 945e2f9..d5d98c0 100644
--- a/src/chess-engine.vala
+++ b/src/chess-engine.vala
@@ -63,7 +63,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]