diff common.cpp @ 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
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
+}