Added 'nohup' option -n.
authorviric@llimona
Tue, 25 Sep 2007 22:22:45 +0200
changeset 41 954941c6e40a
parent 40 da3ea7f666e3
child 42 eb897a05a804
Added 'nohup' option -n.
app_control.c
app_term.c
dump.c
filter_telnet.c
main.c
main.h
server.c
xterm.c
--- a/app_control.c	Mon Sep 24 14:14:30 2007 +0200
+++ b/app_control.c	Tue Sep 25 22:22:45 2007 +0200
@@ -81,15 +81,16 @@
         if (res == 0)
         {
             close(app_stdout);
-            close(1); /* MOVE */
+            if (!command_line.s_param.nohup)
+                close(1);
             app_stdout = -1;
             if (app_stdout == app_stderr || app_stderr == -1)
                 return -1;
         } else
         {
-            /* MOVE */
             hex_dump("from app", stream_buffer, res);
-            write(1, stream_buffer, res);
+            if (!command_line.s_param.nohup)
+                write(1, stream_buffer, res);
             if (command_line.s_param.serve_unix)
                 s_unix_send_to_connected(stream_buffer, res);
             if (command_line.s_param.serve_tcp)
@@ -110,8 +111,8 @@
                 return -1;
         } else
         {
-            /* MOVE */
-            write(2, stream_buffer, res);
+            if (!command_line.s_param.nohup)
+                write(2, stream_buffer, res);
 
             if (command_line.s_param.serve_unix)
                 s_unix_send_to_connected(stream_buffer, res);
@@ -138,7 +139,8 @@
     if (size > 0)
     {
         hex_dump("to app", buffer, size);
-        if(command_line.s_param.echo_in_local_terminal)
+        if( !command_line.s_param.nohup &&
+                command_line.s_param.echo_in_local_terminal)
             write(1, buffer, size);
         write(app_stdin, buffer, size);
     }
--- a/app_term.c	Mon Sep 24 14:14:30 2007 +0200
+++ b/app_term.c	Tue Sep 25 22:22:45 2007 +0200
@@ -10,6 +10,9 @@
 
 #include "main.h"
 
+
+struct winsize app_winsize; /* for xterm.c */
+
 enum
 {
     STDIN,
@@ -36,10 +39,16 @@
     if (res == -1)
         error("tcgetatttr");
 
+    /* if starting using shells' &, it doesn't get ECHO set.
+     * Let's force it. */
+    tios.c_lflag |= ECHO | ECHOE | ECHOK | ECHONL;
+
     res = ioctl(1, TIOCGWINSZ, &wsize);
     if (res == -1)
         error("ioctl TIOCGWINSZ");
 
+    app_winsize = wsize;
+
     res = openpty(&master, &slave, 0, &tios, &wsize);
     if (res == -1)
         error("openpty");
--- a/dump.c	Mon Sep 24 14:14:30 2007 +0200
+++ b/dump.c	Tue Sep 25 22:22:45 2007 +0200
@@ -3,7 +3,7 @@
 
 #include "main.h"
 
-const int should_dump = 0;
+const int should_dump = 1;
 
 void dump_line(const char *msg, ...)
 {
--- a/filter_telnet.c	Mon Sep 24 14:14:30 2007 +0200
+++ b/filter_telnet.c	Tue Sep 25 22:22:45 2007 +0200
@@ -26,7 +26,6 @@
 static void ftelnet_reset(struct FFilter *ff)
 {
     struct FTelnet *ft = (struct FTelnet *) ff;
-    dump_line("ftelnet reset.\n");
     ft->state = NOTHING;
 }
 
@@ -34,14 +33,11 @@
 {
     struct FTelnet *ft = (struct FTelnet *) ff;
 
-    dump_line("ftelnet_function called\n");
     switch (ft->state)
     {
         case NOTHING:
-            dump_line("ftelnet still in NOTHING.\n");
             if (c == IAC)
             {
-                dump_line("ftelnet received IAC\n");
                 ft->state = RECEIVED_IAC;
                 ft->base.matched++;
             }
@@ -49,27 +45,22 @@
         case RECEIVED_IAC:
             if (c == SB)
             {
-                dump_line("ftelnet received SB\n");
                 ft->state = SUBNEGOTIATED;
                 ft->base.matched++;
             } else /* Received the control char */
             {
                 ft->state = RECEIVED_OPT;
-                dump_line("ftelnet received command.\n");
                 ft->base.matched++;
             }
             break;
         case RECEIVED_OPT:
-            dump_line("ftelnet received option.\n");
             ft->base.matched++;
             return 1;
             break;
         case SUBNEGOTIATED:
             ft->base.matched++;
-            dump_line("ftelnet still in SUBNEGOTIATED.\n");
             if (c == SE)
             {
-                dump_line("ftelnet received SE. Filtering.\n");
                 return 1;
             }
             break;
--- a/main.c	Mon Sep 24 14:14:30 2007 +0200
+++ b/main.c	Tue Sep 25 22:22:45 2007 +0200
@@ -43,7 +43,8 @@
     printf(" -P     Run the child as connected to a pipe (default).\n");
     printf(" -t     Run the child as connected to a terminal (raw mode in "
         "client).\n");
-    printf(" -n MAX Serve at most MAX sockets for each transport (1 default)."
+    printf(" -n     Unlink the program from the terminal (as 'nohup').\n");
+    printf(" -N MAX Serve at most MAX sockets for each transport (1 default)."
             "\n");
     printf(" -w     The remote clients can write to the application.\n");
     printf(" -x     The remote clients end will close app's stdin.\n");
@@ -87,7 +88,7 @@
     command_line.s_param.serve_tcp = 0;
     command_line.s_param.send_xterm_resize = 1;
     command_line.s_param.client_may_close_app_stdin = 0;
-    command_line.s_param.detach = 0;
+    command_line.s_param.nohup = 0;
     command_line.s_param.client_can_write = 0;
     command_line.s_param.echo_in_local_terminal = 0;
     command_line.s_param.send_xterm_resize = 0;
@@ -109,7 +110,7 @@
     extern int optind, opterr, optopt;
 
     while(1) {
-        c = my_getopt(argc, argv, "tp:NrdPn:c:hwxe");
+        c = my_getopt(argc, argv, "tp:nBrdPN:c:hwxe");
 
         if (c == -1)
             break;
@@ -123,18 +124,18 @@
             case 'P':
                 command_line.s_param.run_in_subterminal = 0;
                 break;
-            case 'N':
+            case 'B':
                 command_line.s_param.use_blocking_sockets = 0;
                 break;
-            case 'd':
-                command_line.s_param.detach = 1;
+            case 'n':
+                command_line.s_param.nohup = 1;
                 break;
             case 'p':
                 command_line.tcp_port = atoi(optarg);
                 command_line.s_param.serve_tcp = 1;
                 command_line.s_param.serve_unix = 0;
                 break;
-            case 'n':
+            case 'N':
                 command_line.s_param.max_served = atoi(optarg);
                 break;
             case 'c':
--- a/main.h	Mon Sep 24 14:14:30 2007 +0200
+++ b/main.h	Tue Sep 25 22:22:45 2007 +0200
@@ -19,7 +19,7 @@
         char send_xterm_resize;
         char client_may_close_app_stdin;
         char client_can_write;
-        int detach; /* detach from terminal as nohup */
+        char nohup; /* detach from terminal as nohup */
         char **command; /* ordono por exec */
     } s_param;
     struct {
--- a/server.c	Mon Sep 24 14:14:30 2007 +0200
+++ b/server.c	Tue Sep 25 22:22:45 2007 +0200
@@ -16,7 +16,11 @@
     int stdin_opened;
     int res;
 
-    stdin_opened = 1;
+    if (command_line.s_param.nohup)
+        stdin_opened = 0;
+    else
+        stdin_opened = 1;
+
     do
     {
         FD_ZERO(&read_set);
@@ -45,7 +49,7 @@
         if (res == -1) /* app_stdout and app_stderr closed */
             break;
 
-        if (FD_ISSET(0, &read_set))
+        if (stdin_opened && FD_ISSET(0, &read_set))
         {
             res = read(0, stream_buffer, stream_buffer_size);
             /* if res is 0, the fcall will close app_stdin */
@@ -79,8 +83,14 @@
 
     child = fork_app(command_line.s_param.command);
 
-    if (command_line.s_param.run_in_subterminal)
-        prepare_user_terminal();
+    if (command_line.s_param.nohup)
+    {
+        close(0);
+        close(1);
+        close(2);
+    } else
+        if (command_line.s_param.run_in_subterminal)
+            prepare_user_terminal();
 
     install_signal_forwarders(child);
 
@@ -92,7 +102,8 @@
     if (command_line.s_param.serve_tcp)
         s_tcp_shutdown();
 
-    if (command_line.s_param.run_in_subterminal)
+    if (!command_line.s_param.nohup &&
+            command_line.s_param.run_in_subterminal)
         restore_user_terminal();
 
     return 0;
--- a/xterm.c	Mon Sep 24 14:14:30 2007 +0200
+++ b/xterm.c	Tue Sep 25 22:22:45 2007 +0200
@@ -8,13 +8,12 @@
 
 char * get_xterm_resize_string()
 {
-    struct winsize win;
     int rows, cols;
+    extern struct winsize app_winsize; /* from app_term.c */
 
     /* Get rows and cols from our connection to the terminal: fd 1 */
-    ioctl(1, TIOCGWINSZ, (char *)&win);
-    rows = win.ws_row;
-    cols = win.ws_col;
+    rows = app_winsize.ws_row;
+    cols = app_winsize.ws_col;
 
     /* Prepare the xterm resize string */
     snprintf(xterm_resize_string, sizeof xterm_resize_string,