[X2Go-Commits] [x2goclient] 214/257: src/unixhelper.{cpp, h}: rewrite kill_pgroup () to act as a wrapper around real_kill_pgroup ().

git-admin at x2go.org git-admin at x2go.org
Mon Nov 28 16:06:18 CET 2016


This is an automated email from the git hooks/post-receive script.

x2go pushed a commit to branch bugfix/osx
in repository x2goclient.

commit 8d78f15852e104816d7d6a84b650e8ffbb4934c8
Author: Mihai Moldovan <ionic at ionic.de>
Date:   Fri Aug 12 09:11:16 2016 +0200

    src/unixhelper.{cpp,h}: rewrite kill_pgroup () to act as a wrapper around real_kill_pgroup ().
    
    Adjust comment.
    
    We need to do this so the cleanup process doesn't kill itself before all
    other processes in the process group are killed.
---
 debian/changelog   |    4 ++++
 src/unixhelper.cpp |   38 ++++++++++++++++++++++++++++++++++++++
 src/unixhelper.h   |   12 ++++++------
 3 files changed, 48 insertions(+), 6 deletions(-)

diff --git a/debian/changelog b/debian/changelog
index 657619b..bfd6b8e 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -371,6 +371,10 @@ x2goclient (4.0.5.3-0x2go1) UNRELEASED; urgency=medium
     - src/unixhelper.h: style and general fixes within comments.
     - src/unixhelper.{cpp,h}: split off core functionality of kill_pgroup ()
       into a new function called real_kill_pgroup ().
+    - src/unixhelper.{cpp,h}: rewrite kill_pgroup () to act as a wrapper
+      around real_kill_pgroup (). Adjust comment. We need to do this so the
+      cleanup process doesn't kill itself before all other processes in the
+      process group are killed.
 
   [ Bernard Cafarelli ]
   * New upstream version (4.0.5.3):
diff --git a/src/unixhelper.cpp b/src/unixhelper.cpp
index 4d9c56f..82e4374 100644
--- a/src/unixhelper.cpp
+++ b/src/unixhelper.cpp
@@ -37,6 +37,44 @@
 
 namespace unixhelper {
   void kill_pgroup (const int signal) {
+    pid_t pgid_to_kill = getpgrp ();
+
+    if ((SIGHUP == signal) || (-1 == signal)) {
+      /*
+       * In order to not kill ourselves, we need to run this
+       * code in a new process group.
+       */
+      pid_t tmp_pid = fork ();
+
+      /* Child. */
+      if (0 == tmp_pid) {
+        /* Create new pgid. */
+        int err = setpgid (0, 0);
+
+        if (0 != err) {
+          std::perror ("WARNING: unable to change PGID");
+          std::cerr << "Continuing with normal operation, but process might kill itself before tree vanishes." << std::endl;
+        }
+
+        real_kill_pgroup (pgid_to_kill);
+      }
+      /* Error. */
+      else if (-1 == tmp_pid) {
+        perror ("WARNING: unable to fork off another process to kill original process group");
+        std::cerr << "Proceeding with normal operation, but  process might kill itself before tree vanishes." << std::endl;
+
+        real_kill_pgroup (pgid_to_kill);
+      }
+      /* Parent. */
+      else {
+        /*
+         * No need to do anything, just exit here in order to not
+         * spawn a bunch of new processes due to subsequent calls
+         * to kill_pgroup () from unix_cleanup ().
+         */
+        exit (EXIT_SUCCESS);
+      }
+    }
   }
 
   void real_kill_pgroup (const pid_t pgid) {
diff --git a/src/unixhelper.h b/src/unixhelper.h
index 662b3be..7d30bcf 100644
--- a/src/unixhelper.h
+++ b/src/unixhelper.h
@@ -47,12 +47,12 @@ namespace unixhelper {
   int unix_cleanup (const pid_t parent);
 
   /*
-   * Kills the whole process group.
-   * First, SIGTERM is sent to the group.
-   * A 10 seconds grace period is granted to make sure
-   * processes exit cleanly on their own.
-   * Lastly, SIGKILL is sent to the group -- which also
-   * implies the demise of this program.
+   * Wrapper for killing a whole process group.
+   * The "real" killing work is done by real_kill_pgroup ().
+   * This function tries to fork off another process and change
+   * the new function's process group ID.
+   * If any of these operations fail, killing the original process
+   * group ID will still continue, albeit with warning messages.
    *
    * signal may be any of:
    *   * -1       to indicate an error leading to emergency termination

--
Alioth's /srv/git/code.x2go.org/x2goclient.git//..//_hooks_/post-receive-email on /srv/git/code.x2go.org/x2goclient.git


More information about the x2go-commits mailing list