From cccfe21ef07fe10b9406b9a9e61a78c82549ca4e Mon Sep 17 00:00:00 2001 From: Manuel Schneider Date: Thu, 27 Nov 2014 16:40:15 +0100 Subject: Run the startsctripts properly detached --- src/dialog.cpp | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) (limited to 'src/dialog.cpp') 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 #include +#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 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_)); -- cgit v1.2.3-55-g7522