changeset 80:32fa40c3d174

* OS/X doesn't like fork() without exec(). Scrap that, revert to ioctl
author Chris Cannam
date Mon, 22 Nov 2010 15:21:12 +0000
parents aaeabc920ca8
children 0fa92c94d408
files common.cpp hgrunner.cpp
diffstat 2 files changed, 32 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/common.cpp	Mon Nov 22 12:30:40 2010 +0000
+++ b/common.cpp	Mon Nov 22 15:21:12 2010 +0000
@@ -33,6 +33,10 @@
 #include <errno.h>
 #include <pwd.h>
 #include <unistd.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 #endif
 
 QString findExecutable(QString name)
@@ -148,33 +152,33 @@
 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;
+
+    if (!isatty(0)) {
+        DEBUG << "stdin is not a terminal" << endl;
+    } else {
+        DEBUG << "stdin is a terminal, detaching from it" << endl;
+        if (ioctl(0, TIOCNOTTY, NULL) < 0) {
+            perror("ioctl failed");
+            DEBUG << "ioctl for TIOCNOTTY failed (errno = " << errno << ")" << endl;
         } else {
-            perror("setsid failed");
-            DEBUG << "setsid() failed (errno = " << errno << ")" << endl;
+            DEBUG << "ioctl for TIOCNOTTY succeeded" << endl;
         }
-        switch (fork()) {
-        case 0:
-            return;
-        case -1:
-            perror("fork failed");
-            DEBUG << "second fork failed (errno = " << errno << ")" << endl;
-            return;
-        default:
-            exit(0);
+        return;
+    }
+
+    int ttyfd = open("/dev/tty", O_RDWR);
+    if (ttyfd < 0) {
+        DEBUG << "failed to open controlling terminal" << endl;
+    } else {
+        if (ioctl(ttyfd, TIOCNOTTY, NULL) < 0) {
+            perror("ioctl failed");
+            DEBUG << "ioctl for TIOCNOTTY failed (errno = " << errno << ")" << endl;
+        } else {
+            DEBUG << "ioctl for TIOCNOTTY succeeded" << endl;
         }
-    case -1:
-        perror("fork failed");
-        DEBUG << "fork failed (errno = " << errno << ")" << endl;
         return;
-    default:
-        exit(0);
     }
+
 #endif
 }
 
--- a/hgrunner.cpp	Mon Nov 22 12:30:40 2010 +0000
+++ b/hgrunner.cpp	Mon Nov 22 15:21:12 2010 +0000
@@ -33,8 +33,12 @@
 #include <stdio.h>
 
 #ifndef Q_OS_WIN32
+#ifdef Q_OS_MAC
+#include <util.h>
+#else
 #include <pty.h>
 #endif
+#endif
 
 HgRunner::HgRunner(QWidget * parent): QProgressBar(parent)
 {
@@ -138,7 +142,7 @@
 {
     DEBUG << "stdOutReady" << endl;
     QString chunk = QString::fromUtf8(proc->readAllStandardOutput());
-    DEBUG << "stdout was " << chunk << endl;
+    //DEBUG << "stdout was " << chunk << endl;
     stdOut += chunk;
 }
 
@@ -146,7 +150,7 @@
 {
     DEBUG << "stdErrReady" << endl;
     QString chunk = QString::fromUtf8(proc->readAllStandardError());
-    DEBUG << "stderr was " << chunk << endl;
+    //DEBUG << "stderr was " << chunk << endl;
     stdErr += chunk;
     if (procInput) {
         if (chunk.toLower().trimmed() == "password:") {