Subject: bug#16892: 24.3; terminal corruption after emacsclient -t &

From: <[email-masked]>
Date: Thu, 27 Feb 2014 13:50:22 +0000

after doing:

emacs -Q -f server-start&
emacsclient -t &

from a terminal, the terminal gets corrupted.  The terminal shows the
emacs buffer, but the shell and not emacs receives the keystrokes.
Expected behaviour would be to stop emacsclient immediately when it is
started in the backround and needs the current tty. This would be
consistent with behaviour of "emacs -Q -nw &"

The attached patch for emacsclient.c fixes this problem by checking if
emacsclient is started in the background, and raising SIGSTOP, if that
is the case.  This fixes the problem for me, but I cannot guarantee that
this has no side effects.

In GNU Emacs 24.3.1 (x86_64-unknown-linux-gnu, GTK+ Version 3.10.7)
 of 2014-01-28 on var-lib-archbuild-extra-x86_64-juergen
Windowing system distributor `The X.Org Foundation', version 11.0.11500000
System Description:	Arch Linux

Configured using:
 `configure '--prefix=/usr' '--sysconfdir=/etc' '--libexecdir=/usr/lib'
 '--localstatedir=/var' '--with-x-toolkit=gtk3' '--with-xft'
 'CFLAGS=-march=x86-64 -mtune=generic -O2 -pipe -fstack-protector
 --param=ssp-buffer-size=4'
 'LDFLAGS=-Wl,-O1,--sort-common,--as-needed,-z,relro'
 'CPPFLAGS=-D_FORTIFY_SOURCE=2''

Important settings:
  value of $LANG: en_US.UTF-8
  locale-coding-system: utf-8-unix
  default enable-multibyte-characters: t

Major mode: Lisp Interaction

Minor modes in effect:
  tooltip-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  line-number-mode: t
  transient-mark-mode: t

Recent input:
M-x r e p o <tab> r <tab> <return>

Recent messages:
For information about GNU Emacs and the GNU system, type C-h C-a.
Making completion list...

Load-path shadows:
None found.

Features:
(shadow sort gnus-util mail-extr emacsbug message format-spec rfc822 mml
mml-sec mm-decode mm-bodies mm-encode mail-parse rfc2231 mailabbrev
gmm-utils mailheader sendmail rfc2047 rfc2045 ietf-drums mm-util
mail-prsvr mail-utils help-mode easymenu time-date tooltip ediff-hook
vc-hooks lisp-float-type mwheel x-win x-dnd tool-bar dnd fontset image
regexp-opt fringe tabulated-list newcomment lisp-mode register page
menu-bar rfn-eshadow timer select scroll-bar mouse jit-lock font-lock
syntax facemenu font-core frame cham georgian utf-8-lang misc-lang
vietnamese tibetan thai tai-viet lao korean japanese hebrew greek
romanian slovak czech european ethiopic indian cyrillic chinese
case-table epa-hook jka-cmpr-hook help simple abbrev minibuffer loaddefs
button faces cus-face macroexp files text-properties overlay sha1 md5
base64 format env code-pages mule custom widget hashtable-print-readable
backquote make-network-process dbusbind dynamic-setting
system-font-setting font-render-setting move-toolbar gtk x-toolkit x
multi-tty emacs)
>From f80558a9b708e08f3a9fc1b0c1aed65b1a43282d Mon Sep 17 00:00:00 2001
From: Andreas Amann <[email-masked]>
Date: Tue, 25 Feb 2014 21:28:44 +0000
Subject: [PATCH] Fix emacsclient's behavior when started in background

 	* emacsclient.c (main): raise SIGSTOP if emacsclient process is started in the background in tty mode.
---
 lib-src/ChangeLog     | 2 ++
 lib-src/emacsclient.c | 7 +++++++
 2 files changed, 9 insertions(+)

diff --git a/lib-src/ChangeLog b/lib-src/ChangeLog
index bd0a16e..cf367ab 100644
--- a/lib-src/ChangeLog
+++ b/lib-src/ChangeLog
@@ -2,6 +2,8 @@
 	
 	Fix emacsclient's handling of SIGCONT (Bug#16883).
 	* emacsclient.c (handle_sigcont): Cancel the continue only if tty.
+	(main): raise SIGSTOP if emacsclient process is started in the
+	background in tty mode.
 
 2014-01-22  Eli Zaretskii  <[email-masked]>
 
diff --git a/lib-src/emacsclient.c b/lib-src/emacsclient.c
index 148182a..7bed240 100644
--- a/lib-src/emacsclient.c
+++ b/lib-src/emacsclient.c
@@ -1554,6 +1554,13 @@ main (int argc, char **argv)
       exit (EXIT_FAILURE);
     }
 
+#if !defined (NO_SOCKETS_IN_FILE_SYSTEM)
+  if (tcgetpgrp (1) != getpgrp () && tty)
+    {
+      raise(SIGSTOP);
+    }
+#endif /* NO_SOCKETS_IN_FILE_SYSTEM */
+
   /* If alternate_editor is the empty string, start the emacs daemon
      in case of failure to connect.  */
   start_daemon_if_needed = (alternate_editor
-- 
1.9.0