xterm resizes sent on SIGWINCH. new terminal applications start a new session.
authorlbatlle@npdl268.bpo.hp.com
Wed, 26 Sep 2007 16:14:47 +0200
changeset 47 8dcc45d8f3e5
parent 46 bb76f8ca177d
child 48 ffea64c65751
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
DOCS
Makefile
app_term.c
signals.c
xterm.c
--- a/DOCS	Wed Sep 26 15:18:30 2007 +0200
+++ b/DOCS	Wed Sep 26 16:14:47 2007 +0200
@@ -4,3 +4,5 @@
 Job Control and sessions:
  http://www.fh-wedel.de/~di/html/glibc/libc_24.html (Job Control)
  Interesting on 'setsid'.
+
+ http://www.hmug.org/man/4/termios.php (on The Controlling Terminal)
--- a/Makefile	Wed Sep 26 15:18:30 2007 +0200
+++ b/Makefile	Wed Sep 26 16:14:47 2007 +0200
@@ -33,7 +33,7 @@
 unix_server.o: unix_server.c main.h handlers.h
 unix_client.o: unix_client.c main.h handlers.h
 error.o: error.c main.h
-signals.o: signals.c main.h
+signals.o: signals.c main.h handlers.h
 gen_sockets.o: gen_sockets.c main.h
 app_control.o: app_control.c main.h handlers.h
 client.o: client.c main.h handlers.h
--- a/app_term.c	Wed Sep 26 15:18:30 2007 +0200
+++ b/app_term.c	Wed Sep 26 16:14:47 2007 +0200
@@ -95,7 +95,9 @@
     int res;
 
     if (command_line.s_param.run_in_subterminal)
+    {
         give_terminal(p_parent, p_child);
+    }
     else
         give_pipes(p_parent, p_child);
 
@@ -118,6 +120,17 @@
             res = dup2(p_child[STDERR], 2);
             if (res == -1) error("Dup2 stderr");
 
+            if (command_line.s_param.run_in_subterminal)
+            {
+                int pid;
+                int res;
+                res = setsid();
+                if (res < 0)
+                  error("failed setsid()");
+                res = ioctl(0, TIOCSCTTY, 0);
+                if (res < 0)
+                  error("failed ioctl(0,TIOCSCTTY,0)");
+            }
             execvp(command[0], command);
 
             error("Cannot execvp %s", command[0]);
--- a/signals.c	Wed Sep 26 15:18:30 2007 +0200
+++ b/signals.c	Wed Sep 26 16:14:47 2007 +0200
@@ -6,8 +6,10 @@
 */
 #include <signal.h>
 #include <string.h>
+#include <sys/types.h>
 
 #include "main.h"
+#include "handlers.h"
 
 static int child;
 
@@ -16,6 +18,24 @@
     kill(child, val);
 }
 
+static void update_window_size(int val)
+{
+    char *xterm_str;
+    int len;
+
+    kill(child, val);
+
+    /* Resend the xterm string */
+    xterm_str = get_xterm_resize_string();
+    len = strlen(xterm_str);
+    if (command_line.s_param.serve_unix)
+        s_unix_send_to_connected(xterm_str, len);
+    if (command_line.s_param.serve_tcp)
+        s_tcp_send_to_connected(xterm_str, len);
+    if (command_line.s_param.serve_eth)
+        s_eth_send_to_connected(xterm_str, len);
+}
+
 void install_signal_forwarders(int _child)
 {
   struct sigaction act;
@@ -29,4 +49,7 @@
 
   sigaction(SIGTERM, &act, 0);
   sigaction(SIGINT, &act, 0);
+
+  act.sa_handler = update_window_size;
+  sigaction(SIGWINCH, &act, 0);
 }
--- a/xterm.c	Wed Sep 26 15:18:30 2007 +0200
+++ b/xterm.c	Wed Sep 26 16:14:47 2007 +0200
@@ -2,6 +2,7 @@
 #include <termios.h>
 #include <sys/types.h>
 #include <stdio.h>
+#include <unistd.h>
 #include "main.h"
 
 static char xterm_resize_string[100];
@@ -11,7 +12,14 @@
     int rows, cols;
     extern struct winsize app_winsize; /* from app_term.c */
 
-    /* Get rows and cols from our connection to the terminal: fd 1 */
+    if (isatty(0))
+    {
+      int res;
+      /* Get rows and cols from our connection to the terminal: fd 0 */
+      res = ioctl(0, TIOCGWINSZ, &app_winsize);
+      if (res == -1)
+          error("ioctl TIOCGWINSZ");
+    }
     rows = app_winsize.ws_row;
     cols = app_winsize.ws_col;