Adding a feature: save all stdin/stdout traffic of the server into a file. saveflie
authorviric <viriketo@gmail.com>
Wed, 16 Feb 2011 20:57:32 +0100
branchsaveflie
changeset 94 330324fc7c20
parent 93 7d9b7a6da507
child 95 13360d8af313
Adding a feature: save all stdin/stdout traffic of the server into a file.
app_control.c
main.c
main.h
server.c
--- a/app_control.c	Thu Aug 21 23:21:22 2008 +0200
+++ b/app_control.c	Wed Feb 16 20:57:32 2011 +0100
@@ -19,6 +19,8 @@
 
 static struct FilterRules *fr;
 
+int savefile = -1;
+
 void write_newline_cb(struct FilterRules *myfr,
         const struct FFilter *ff, char *obuf, int *olen,
         const char *ibuf, int pos)
@@ -107,6 +109,8 @@
             if (command_line.s_param.serve_eth)
                 s_eth_send_to_connected(stream_buffer, res);
 #endif /* linux */
+            if (savefile != -1)
+                write(savefile, stream_buffer, res);
         }
     }
     if (app_stderr != -1 && app_stdout != app_stderr &&
@@ -167,6 +171,8 @@
                 command_line.s_param.echo_in_local_terminal)
             write(1, buffer, size);
         write(app_stdin, buffer, size);
+        if (savefile != -1)
+            write(savefile, buffer, size);
     }
 }
 
--- a/main.c	Thu Aug 21 23:21:22 2008 +0200
+++ b/main.c	Wed Feb 16 20:57:32 2011 +0100
@@ -55,6 +55,7 @@
 #ifdef linux
     printf(" -e dev[:port] Also serve/connect using raw ethernet, device 'dev'.\n");
     printf(" -c adr Connect to address (MAC if eth).\n");
+    printf(" -s fil Save the child stdin/stdout to the filename 'fil'.\n");
 #endif /* linux */
     printf(" -x     Send xterm's resize control string to clients.\n");
     return 0;
@@ -112,6 +113,7 @@
     command_line.s_param.echo_in_local_terminal = 0;
     command_line.s_param.send_xterm_resize = 0;
     command_line.s_param.serve_eth = 0;
+    command_line.s_param.savefile = 0;
 
     command_line.tcp_port = 40000; /* Arbitrary */
     command_line.eth_port = 100;   /* Arbitrary */
@@ -132,7 +134,7 @@
     extern int optind, opterr, optopt;
 
     while(1) {
-        c = my_getopt(argc, argv, "tp:nBxdPN:hwCED"
+        c = my_getopt(argc, argv, "s:tp:nBxdPN:hwCED"
 #ifdef linux
                 "c:e:"
 #endif /* linux */
@@ -192,6 +194,9 @@
                 parse_eth_device(optarg);
                 command_line.c_param.transport = ETHERNET;
                 break;
+            case 's':
+                command_line.s_param.savefile = strdup(optarg);
+                break;
             case '?':
                 error("Wrong option %c.\n", optopt);
         }
--- a/main.h	Thu Aug 21 23:21:22 2008 +0200
+++ b/main.h	Wed Feb 16 20:57:32 2011 +0100
@@ -23,6 +23,7 @@
         char client_may_close_app_stdin;
         char client_can_write;
         char nohup; /* detach from terminal as nohup */
+        char *savefile;
         char **command; /* ordono por exec */
     } s_param;
     struct {
--- a/server.c	Thu Aug 21 23:21:22 2008 +0200
+++ b/server.c	Wed Feb 16 20:57:32 2011 +0100
@@ -12,6 +12,7 @@
 #include <errno.h>
 #include <signal.h>
 #include <string.h>
+#include <fcntl.h>
 
 #include "main.h"
 #include "handlers.h"
@@ -19,6 +20,9 @@
 /* signals.c */
 extern int child_died;
 
+/* app_control.c */
+extern int savefile;
+
 /*extern*/
 void fdset_dump(fd_set *set, int maxfd);
 
@@ -143,6 +147,11 @@
         s_eth_init();
 #endif /* linux */
 
+    if (command_line.s_param.savefile)
+    {
+        savefile = open(command_line.s_param.savefile, O_CREAT | O_RDWR, 0666);
+    }
+
     child = fork_app(command_line.s_param.command);
 
     if (command_line.s_param.nohup)