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