summaryrefslogtreecommitdiffstats
path: root/src/dialog.cpp
diff options
context:
space:
mode:
authorManuel Schneider2014-11-27 16:40:15 +0100
committerManuel Schneider2014-11-27 16:40:15 +0100
commitcccfe21ef07fe10b9406b9a9e61a78c82549ca4e (patch)
tree5ca2b2ef4b5ad68c9ecf03310eea97f33c002572 /src/dialog.cpp
parentChanged maximum size of help/news box to 300 px. (diff)
downloadvmchooser2-cccfe21ef07fe10b9406b9a9e61a78c82549ca4e.tar.gz
vmchooser2-cccfe21ef07fe10b9406b9a9e61a78c82549ca4e.tar.xz
vmchooser2-cccfe21ef07fe10b9406b9a9e61a78c82549ca4e.zip
Run the startsctripts properly detached
Diffstat (limited to 'src/dialog.cpp')
-rw-r--r--src/dialog.cpp37
1 files changed, 25 insertions, 12 deletions
diff --git a/src/dialog.cpp b/src/dialog.cpp
index 2806d7c..490dd9b 100644
--- a/src/dialog.cpp
+++ b/src/dialog.cpp
@@ -10,12 +10,15 @@
#include <QDateTime>
#include <QTemporaryFile>
+#include "unistd.h"
+#include "stdio.h"
+#include "sys/wait.h"
+
#include "ui_dialog.h"
#include "sessiontreeitem.h"
#include "globals.h"
#include "vsession.h"
#include "choosersettings.h"
-#include <unistd.h>
Dialog::Dialog(QWidget *parent)
: QDialog(parent), ui(new Ui::Dialog) {
@@ -104,24 +107,34 @@ void Dialog::on_treeView_activated(QModelIndex index)
sprintf(pvs_ac, "PVS_AUTO_CONNECT=%s", ui->PVS_checkbox->isChecked()?"TRUE":"FALSE");
putenv(pvs_ac);
- // fork this process
+ // Fork this process twice to detach
pid_t pid = fork();
- if (pid < 0)
- exit(EXIT_FAILURE);
-
- // Branch for the clone
if (pid == 0){
- // At this point we are executing as the child process
- // Create a new SID for the child process (detach)
+
+ // Close all filedescriptors
+ for (int i = 3; i < 1024; ++i) ::close(i);
+
+ // Change process group
pid_t sid = setsid();
if (sid < 0)
exit(EXIT_FAILURE);
- // Replace this clone with the process we'd like to start
- // execv seems to copy this environment.
- char const * const args[] = {VMCHOOSER_SESSION_START_SCRIPT, NULL};
- execv(VMCHOOSER_SESSION_START_SCRIPT, (char**)args);
+ // Reopen standard pipes
+ freopen("/dev/null", "r", stdin);
+ freopen("/dev/null", "w", stdout);
+ freopen("/dev/null", "w", stdout);
+
+ pid = fork();
+ if (pid == 0){
+ // At this point we are executing as the 2nd child process
+ // Replace this clone with the process we'd like to start
+ char const * const args[] = {VMCHOOSER_SESSION_START_SCRIPT, NULL};
+ execv(VMCHOOSER_SESSION_START_SCRIPT, (char**)args);
+ }
+ _exit(0); // Dont use exit hooks
}
+ // Wait and cleanup the intermediate process
+ waitpid(pid, NULL, 0);
}
ChooserSettings::setSetting("last-session", (s->shortDescription()));
ChooserSettings::setSetting("last-tab", QString::number(activeTab_));