From af9d6791c82a9e848df66efa5fa569d2313812c9 Mon Sep 17 00:00:00 2001 From: Manuel Schneider Date: Tue, 2 Sep 2014 19:14:52 +0200 Subject: Use plain c to fork and detach vm session start script. --- src/dialog.cpp | 86 ++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 50 insertions(+), 36 deletions(-) (limited to 'src/dialog.cpp') diff --git a/src/dialog.cpp b/src/dialog.cpp index ae157ba..2806d7c 100644 --- a/src/dialog.cpp +++ b/src/dialog.cpp @@ -15,6 +15,7 @@ #include "globals.h" #include "vsession.h" #include "choosersettings.h" +#include Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { @@ -79,44 +80,57 @@ void Dialog::changeEvent(QEvent *e) { } } -void Dialog::on_treeView_activated(QModelIndex index) { - // this method gets called when a Session has been activated - - SessionTreeItem* item = - static_cast(index.internalPointer()); - - const Session* s(item->session()); - if (!s) { - // no valid session has been selected, do nothing - return; - } - - if (s->run()) { - // Run session start script - if (QFile::exists(sessionStartScript)) { - // Use the current environment variables and add the necessary - // information for the startUpScipt. In this case it is intended to tell - // the startupscript if the PVS should be started. Further in the future - // the session id will be passed this way. - QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); - if (ui->PVS_checkbox->isChecked()) { - env.insert("PVS_SESSIONID", "xxxx"); - } - QProcess scriptProcess; - scriptProcess.setProcessEnvironment(env); - scriptProcess.start(sessionStartScript, QIODevice::ReadOnly); - scriptProcess.waitForFinished(); - scriptProcess.close(); +void Dialog::on_treeView_activated(QModelIndex index) +{ + // this method gets called when a Session has been activated + + SessionTreeItem* item = + static_cast(index.internalPointer()); + + const Session* s(item->session()); + if (!s) { + // no valid session has been selected, do nothing + return; + } + + if (s->run()) + { + // Run session start script + if (QFile::exists(sessionStartScript)) + { + // Use the current environment variables and add the necessary + // information for the startUpScipt. + char pvs_ac[128]; + sprintf(pvs_ac, "PVS_AUTO_CONNECT=%s", ui->PVS_checkbox->isChecked()?"TRUE":"FALSE"); + putenv(pvs_ac); + + // fork this process + 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) + 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); } - - ChooserSettings::setSetting("last-session", (s->shortDescription())); - ChooserSettings::setSetting("last-tab", QString::number(activeTab_)); - setVisible(false); - } else { - QMessageBox::warning( - this, trUtf8("vmchooser"), - trUtf8("Vmchooser failed to run the selected session!")); } + ChooserSettings::setSetting("last-session", (s->shortDescription())); + ChooserSettings::setSetting("last-tab", QString::number(activeTab_)); + setVisible(false); + } else { + QMessageBox::warning( + this, trUtf8("vmchooser"), + trUtf8("Vmchooser failed to run the selected session!")); + } } void Dialog::addItems(const QList& entries, int tab) { -- cgit v1.2.3-55-g7522