changeset 78:07405f3a428b

* Use fork/setsid/fork to escape our controlling terminal, to ensure the pty we create is used for password requests
author Chris Cannam
date Mon, 22 Nov 2010 10:03:15 +0000
parents b522aaa2c053
children aaeabc920ca8
files common.cpp common.h main.cpp
diffstat 3 files changed, 42 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/common.cpp	Sat Nov 20 11:40:19 2010 +0000
+++ b/common.cpp	Mon Nov 22 10:03:15 2010 +0000
@@ -30,7 +30,9 @@
 #include <windows.h>
 #include <security.h>
 #else
+#include <errno.h>
 #include <pwd.h>
+#include <unistd.h>
 #endif
 
 QString findExecutable(QString name)
@@ -143,3 +145,35 @@
 #endif
 #endif
 
+void loseControllingTerminal()
+{
+#ifndef Q_OS_WIN32
+    pid_t sid;
+    switch (fork()) {
+    case 0:
+        sid = setsid();
+        if (sid != (pid_t)-1) {
+            DEBUG << "setsid() succeeded, session ID is " << sid << endl;
+        } else {
+            perror("setsid failed");
+            DEBUG << "setsid() failed (errno = " << errno << ")" << endl;
+        }
+        switch (fork()) {
+        case 0:
+            return;
+        case -1:
+            perror("fork failed");
+            DEBUG << "second fork failed (errno = " << errno << ")" << endl;
+            return;
+        default:
+            exit(0);
+        }
+    case -1:
+        perror("fork failed");
+        DEBUG << "fork failed (errno = " << errno << ")" << endl;
+        return;
+    default:
+        exit(0);
+    }
+#endif
+}
--- a/common.h	Sat Nov 20 11:40:19 2010 +0000
+++ b/common.h	Mon Nov 22 10:03:15 2010 +0000
@@ -48,6 +48,8 @@
 
 extern QString getUserRealName();
 
+extern void loseControllingTerminal();
+
 #endif 	//COMMON_H
 
 
--- a/main.cpp	Sat Nov 20 11:40:19 2010 +0000
+++ b/main.cpp	Mon Nov 22 10:03:15 2010 +0000
@@ -18,6 +18,8 @@
 #include <QApplication>
 
 #include "mainwindow.h"
+#include "common.h"
+#include "debug.h"
 
 int main(int argc, char *argv[])
 {
@@ -25,6 +27,10 @@
     QApplication::setOrganizationDomain("easymercurial.org");
     QApplication::setApplicationName(QApplication::tr("EasyMercurial"));
 
+    // Lose our controlling terminal (so we can provide a new pty to
+    // capture password requests)
+    loseControllingTerminal();
+
     QApplication app(argc, argv);
     MainWindow mainWin;
     mainWin.show();