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

From: Paul Eggert <[email-masked]>
Date: Wed, 05 Mar 2014 00:07:00 -0800

Thanks for the bug report.  How about the attached (untested) patch 
instead?  It checks for tcgetpgrp failure.  Also, the glibc manual 
suggests that emacsclient should be killing its process group with 
SIGTTIN, not itself with SIGSTOP.  See:

http://www.gnu.org/software/libc/manual/html_node/Foreground-and-Background.html
=== modified file 'lib-src/emacsclient.c'
--- lib-src/emacsclient.c	2014-02-25 19:06:53 +0000
+++ lib-src/emacsclient.c	2014-03-05 07:59:33 +0000
@@ -1105,16 +1105,18 @@
 handle_sigcont (int signalnum)
 {
   int old_errno = errno;
+  pid_t pgrp = getpgrp ();
+  pid_t tcpgrp = tcgetpgrp (1);
 
-  if (tcgetpgrp (1) == getpgrp ())
+  if (tcpgrp == pgrp)
     {
       /* We are in the foreground.  */
       send_to_emacs (emacs_socket, "-resume \n");
     }
-  else if (tty)
+  else if (0 <= tcpgrp && tty)
     {
       /* We are in the background; cancel the continue.  */
-      raise (SIGSTOP);
+      kill (-pgrp, SIGTTIN);
     }
 
   signal (signalnum, handle_sigcont);
@@ -1554,6 +1556,14 @@
       exit (EXIT_FAILURE);
     }
 
+  if (tty)
+    {
+      pid_t pgrp = getpgrp ();
+      pid_t tcpgrp = tcgetpgrp (1);
+      if (0 <= tcpgrp && tcpgrp != pgrp)
+	kill (-pgrp, SIGTTIN);
+    }
+
   /* If alternate_editor is the empty string, start the emacs daemon
      in case of failure to connect.  */
   start_daemon_if_needed = (alternate_editor