[X2Go-Commits] [x2goclient] 08/47: x2goclient.cpp: use setsid() on UNIX to become session and process group leader.

git-admin at x2go.org git-admin at x2go.org
Thu Jun 4 01:46:27 CEST 2015


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

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

commit d9462ce205c7b8b62b7324b28c5a9b2dbc43b73b
Author: Mihai Moldovan <ionic at ionic.de>
Date:   Mon Mar 16 20:30:49 2015 +0100

    x2goclient.cpp: use setsid() on UNIX to become session and process group leader.
    
    If that fails, fork, terminate the parent and execute setsid() in the
    child process. Use fork_helper() to start the UNIX cleanup helper in a
    child process and continue with the main application in the parent.
---
 debian/changelog   |    4 ++++
 src/x2goclient.cpp |   43 ++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 44 insertions(+), 3 deletions(-)

diff --git a/debian/changelog b/debian/changelog
index e65b6d3..edfd78c 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -53,6 +53,10 @@ x2goclient (4.0.4.1-0x2go1) UNRELEASED; urgency=low
     - x2goclient.cpp: wrap X2Go Client main function and use that.
     - x2goclient.cpp: add fork_helper() function to start up the UNIX cleanup
       helper.
+    - x2goclient.cpp: use setsid() on UNIX to become session and process group
+      leader. If that fails, fork, terminate the parent and execute setsid()
+      in the child process. Use fork_helper() to start the UNIX cleanup helper
+      in a child process and continue with the main application in the parent.
 
  -- X2Go Release Manager <git-admin at x2go.org>  Tue, 26 May 2015 21:42:09 +0200
 
diff --git a/src/x2goclient.cpp b/src/x2goclient.cpp
index f285f40..1a57178 100644
--- a/src/x2goclient.cpp
+++ b/src/x2goclient.cpp
@@ -54,7 +54,44 @@ int fork_helper (int argc, char **argv) {
 }
 #endif /* defined (Q_OS_UNIX) */
 
-int main(int argc, char *argv[])
-{
-	return (wrap_x2go_main (argc, argv));
+int main (int argc, char **argv) {
+#ifdef Q_OS_UNIX
+  if (-1 == setsid ()) {
+    std::cerr << "Unable to create a new process session: " << std::strerror (errno) << "\n";
+
+    std::cerr << "Trying to fork." << std::endl;
+    pid_t tmp_pid = fork ();
+
+    /* Child. */
+    if (0 == tmp_pid) {
+        /* Trying to get a new session and become session + process group leader again. */
+        if (-1 == setsid ()) {
+          std::cerr << "Child was unable to create a new process session: " << std::strerror (errno) << "\n";
+          std::cerr << "Terminating. Please report a bug, refer to this documentation: http://wiki.x2go.org/doku.php/wiki:bugs" << std::endl;
+
+          std::exit (EXIT_FAILURE);
+        }
+
+        /* By now, we should be session and group leader. */
+        return (fork_helper (argc, argv));
+    }
+    /* Error. */
+    else if (-1 == tmp_pid) {
+      std::cerr << "Error while forking: " << std::strerror (errno) << std::endl;
+      std::cerr << "Terminating. Please report a bug, refer to this documentation: http://wiki.x2go.org/doku.php/wiki:bugs" << std::endl;
+
+      std::edit (EXIT_FAILURE);
+    }
+    /* Parent. Just die here. */
+    else {
+      std::exit (EXIT_SUCCESS);
+    }
+  }
+  else {
+    /* setsid() worked. Starting helper and main program. */
+    return (fork_helper (argc, argv));
+  }
+#else /* defined (Q_OS_UNIX) */
+  return (wrap_x2go_main (argc, argv));
+#endif /* defined (Q_OS_UNIX) */
 }

--
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