about summary refs log tree commit diff
path: root/Src/Modules
diff options
context:
space:
mode:
authorPeter Stephenson <pws@users.sourceforge.net>2003-07-23 09:27:56 +0000
committerPeter Stephenson <pws@users.sourceforge.net>2003-07-23 09:27:56 +0000
commit93141e69602eed4326cca3097cf0728a5ee7ec8f (patch)
tree71dbb590eabb64e6c1eb147f96fd0a27f904c9fa /Src/Modules
parentef8d5c7214e7b545718cdb10a3bd7be275513a44 (diff)
downloadzsh-93141e69602eed4326cca3097cf0728a5ee7ec8f.tar.gz
zsh-93141e69602eed4326cca3097cf0728a5ee7ec8f.tar.xz
zsh-93141e69602eed4326cca3097cf0728a5ee7ec8f.zip
18888: Philippe Troin: allow clone to acquire a controlling tty
Diffstat (limited to 'Src/Modules')
-rw-r--r--Src/Modules/clone.c47
1 files changed, 30 insertions, 17 deletions
diff --git a/Src/Modules/clone.c b/Src/Modules/clone.c
index 335f0fc80..bb2240d49 100644
--- a/Src/Modules/clone.c
+++ b/Src/Modules/clone.c
@@ -43,7 +43,7 @@
 static int
 bin_clone(char *nam, char **args, Options ops, int func)
 {
-    int ttyfd, pid;
+    int ttyfd, pid, cttyfd;
 
     unmetafy(*args, NULL);
     ttyfd = open(*args, O_RDWR|O_NOCTTY);
@@ -57,29 +57,42 @@ bin_clone(char *nam, char **args, Options ops, int func)
 	ppid = getppid();
 	mypid = getpid();
 #ifdef HAVE_SETSID
-	if (setsid() != mypid) {
+	if (setsid() != mypid)
 	    zwarnnam(nam, "failed to create new session: %e", NULL, errno);
-#endif
-#ifdef TIOCNOTTY
+#elif defined(TIOCNOTTY)
 	    if (ioctl(SHTTY, TIOCNOTTY, 0))
-		zwarnnam(nam, "%e", NULL, errno);
+	    zwarnnam(*args, "%e", NULL, errno);
 	    setpgrp(0L, mypid);
 #endif
-#ifdef HAVE_SETSID
-	}
-#endif
-	if (ttyfd) {
-	    close(0);
-	    dup(ttyfd);
-	} else
-	    ttyfd = -1;
-	close(1);
-	close(2);
-	dup(0);
-	dup(0);
+	dup2(ttyfd,0);
+	dup2(ttyfd,1);
+	dup2(ttyfd,2);
+	if (ttyfd > 2)
+	    close(ttyfd);
 	closem(0);
 	close(coprocin);
 	close(coprocout);
+	/* Acquire a controlling terminal */
+	cttyfd = open(*args, O_RDWR);
+	if (cttyfd == -1)
+	    zwarnnam(nam, "%e", NULL, errno);
+	else {
+#ifdef TIOCSCTTY
+	    ioctl(cttyfd, TIOCSCTTY, 0);
+#endif
+	    close(cttyfd);
+	}
+	/* check if we acquired the tty successfully */
+	cttyfd = open("/dev/tty", O_RDWR);
+	if (cttyfd == -1)
+	    zwarnnam(nam, "could not make %s my controlling tty, job control "
+		     "disabled", *args, 0);
+	else
+	    close(cttyfd);
+
+	/* Clear mygrp so that acquire_pgrp() gets the new process group.
+	 * (acquire_pgrp() is called from init_io()) */
+	mypgrp = 0;
 	init_io();
 	setsparam("TTY", ztrdup(ttystrname));
     }