changeset 83032:b8237c556f0e

Fix the case when emacsclient is run on Emacs's controlling tty. src/term.c (O_NOCTTY): Make sure it's defined. (no_controlling_tty): New variable. (init_initial_display, mark_ttys): Remove unused variable. (term_init): Check that the given filename is a terminal device. Dissociate the controlling terminal if we reopen it for other purposes. (Reported by Dan Nicolaescu <dann at ics dot uci dot edu>. (Fdelete_tty): Return nil. git-archimport-id: lorentey@elte.hu--2004/emacs--multi-tty--0--patch-72
author Karoly Lorentey <lorentey@elte.hu>
date Mon, 02 Feb 2004 22:09:56 +0000
parents 1d2f73785d9d
children 3fed8a60d18c
files README.multi-tty src/term.c
diffstat 2 files changed, 71 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/README.multi-tty	Mon Feb 02 19:19:08 2004 +0000
+++ b/README.multi-tty	Mon Feb 02 22:09:56 2004 +0000
@@ -60,22 +60,23 @@
 simultaneous X and tty frame support works fine.  Emacsclient has been
 extended to support opening a new terminal frame.
 
+Please let me know if you find any bugs in this branch.
 
-Please let me know if you find any bugs in this branch.
+HOW TO COMPILE AND TEST
+-----------------------
 
 To try it out, compile and run the multi-tty branch with the following
 commands:
 
    	mkdir +build
 	cd +build
-	../configure
+	../configure <your favourite options>
 	make bootstrap
 	src/emacs -nw		# You can also try without -nw
 	M-x server-start
 
 and then (from a shell prompt on another terminal) start emacsclient
 with
-
 	lib-src/emacsclient -t /optional/file/names...
 
 You'll hopefully have two fully working, independent frames on
@@ -160,9 +161,12 @@
 The following is an (incomplete) list of people who have contributed
 to the project by testing, bug reports, and suggestions.  Thanks!
 
-Robert J. Chassel <bob@rattlesnake.com>
-Romain Francoise <romain@orekobech.com>
-Ami Fischman <ami@fischman.org>
+Robert J. Chassel <bob at rattlesnake dot com>
+Romain Francoise <romain at orekobech dot com>
+Ami Fischman <ami at fischman dot org>
+Dan Nicolaescu <dann at ics dot uci dot edu>
+
+Richard Stallman was kind enough to review my patches.
 
 
 CHANGELOG
@@ -172,6 +176,10 @@
 
 THINGS TO DO
 ------------
+** Dan Nicolaescu (dann at ics dot uci dot edu) suggests that -nw
+   should be added as an alias for -t in emacsclient.  Good idea.
+   (Alas, implementing this is not trivial, getopt_long does not seem
+   to support two-letter ``short'' options.)
 
 ** Robert J. Chassell reports:
 
@@ -634,4 +642,11 @@
 
    (Fixed.)
 
+
+-- Dan Nicolaescu noticed that starting emacsclient on the same
+   terminal device that is the controlling tty of the Emacs process
+   gives unexpected results.
+
+   (Fixed.)
+
 ;;; arch-tag: 8da1619e-2e79-41a8-9ac9-a0485daad17d
--- a/src/term.c	Mon Feb 02 19:19:08 2004 +0000
+++ b/src/term.c	Mon Feb 02 22:09:56 2004 +0000
@@ -28,6 +28,9 @@
 
 #include <sys/file.h>
 
+#include <unistd.h>             /* For isatty. */
+#include <sys/ioctl.h>          /* For TIOCNOTTY. */
+
 #include "lisp.h"
 #include "termchar.h"
 #include "termopts.h"
@@ -66,6 +69,10 @@
 #define O_RDWR 2
 #endif
 
+#ifndef O_NOCTTY
+#define O_NOCTTY 0
+#endif
+
 static void turn_on_face P_ ((struct frame *, int face_id));
 static void turn_off_face P_ ((struct frame *, int face_id));
 static void tty_show_cursor P_ ((struct tty_display_info *));
@@ -153,6 +160,10 @@
 
 FRAME_PTR updating_frame;
 
+/* Non-zero if we have dropped our controlling tty and therefore
+   should not open a frame on stdout. */
+static int no_controlling_tty;
+
 /* Provided for lisp packages.  */
 
 static int system_uses_terminfo;
@@ -2169,8 +2180,6 @@
 struct display *
 init_initial_display (void)
 {
-  struct tty_display_info *tty;
-  
   if (initialized || display_list || tty_list)
     abort ();
 
@@ -2285,12 +2294,41 @@
     {
       int fd;
       FILE *file;
-      fd = emacs_open (name, O_RDWR, 0);
+
+#ifdef O_IGNORE_CTTY
+      /* Open the terminal device.  Don't recognize it as our
+         controlling terminal, and don't make it the controlling tty
+         if we don't have one at the moment.  */
+      fd = emacs_open (name, O_RDWR | O_IGNORE_CTTY | O_NOCTTY, 0);
+#else
+      /* Alas, O_IGNORE_CTTY is a GNU extension that is only defined
+         on Hurd.  On other systems, we need to dissociate ourselves
+         from the controlling tty when we want to open a frame on the
+         same terminal.  The function setsid should be used for this,
+         but it didn't work for me. */
+
+      fd = emacs_open (name, O_RDWR | O_NOCTTY, 0);
+      
+#ifdef TIOCNOTTY
+      /* Drop our controlling tty if it is the same device. */
+      if (ioctl (fd, TIOCNOTTY, 0) != -1)
+        {
+          no_controlling_tty = 1;
+        }
+#endif
+#endif /* O_IGNORE_CTTY */
+
       if (fd < 0)
         {
           delete_tty (display);
           error ("Could not open file: %s", name);
         }
+      if (! isatty (fd))
+        {
+          close (fd);
+          error ("Not a tty device: %s", name);
+        }
+
       file = fdopen (fd, "w+");
       tty->name = xstrdup (name);
       tty->input = file;
@@ -2298,6 +2336,12 @@
     }
   else
     {
+      if (no_controlling_tty)
+        {
+          /* Opening a frame on stdout is unsafe if we have
+             disconnected our controlling terminal. */
+          error ("There is no controlling terminal any more");
+        }
       tty->name = 0;
       tty->input = stdin;
       tty->output = stdout;
@@ -2805,6 +2849,8 @@
     error ("No such terminal device: %s", name);
 
   delete_tty (d);
+
+  return Qnil;
 }
 
 static int deleting_tty = 0;
@@ -2952,7 +2998,7 @@
 mark_ttys ()
 {
   struct tty_display_info *tty;
-  Lisp_Object *p;
+
   for (tty = tty_list; tty; tty = tty->next)
     {
       if (tty->top_frame)