[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[tyndur-devel] [PATCH] libc: Blockpuffer auf allen Dateien aktivieren



* libc: Blockpuffer auf allen Dateien aktivieren
* libc: Buffer fuer stderr deaktivieren
---
 src/modules/include/lostio.h     |    6 ++++++
 src/modules/lib/lostio/handler.c |   12 +++++++++---
 src/modules/lib/posix/socket.c   |    9 +++++----
 src/modules/lib/stdlibc/file.c   |   30 ++++++++++++++++++++++++++++--
 src/modules/lib/stdlibc/stdio.c  |    4 ++++
 5 files changed, 52 insertions(+), 9 deletions(-)

diff --git a/src/modules/include/lostio.h b/src/modules/include/lostio.h
index 60cddf1..57e3cf7 100644
--- a/src/modules/include/lostio.h
+++ b/src/modules/include/lostio.h
@@ -117,6 +117,12 @@ typedef struct
 } typehandle_t;
 
 
+/// Geoeffnete Datei auf Clientseite (Wrapper fuer FILE)
+struct lostio_internal_file {
+    io_resource_t res;
+    bool          free_buffer;
+};
+
 
 ///LostIO-Schnittstelle initialisieren
 void lostio_init(void);
diff --git a/src/modules/lib/lostio/handler.c b/src/modules/lib/lostio/handler.c
index ce61485..271738e 100644
--- a/src/modules/lib/lostio/handler.c
+++ b/src/modules/lib/lostio/handler.c
@@ -63,6 +63,12 @@ void rpc_io_open(pid_t pid, dword correlation_id, size_t data_size, void* data)
     
     //Wird bei pipes als quelle verwendet
     io_resource_t* io_source = (io_resource_t*) ((dword)data + 1 + sizeof(pid_t));
+
+    // FIXME
+    struct lostio_internal_file* int_source = malloc(sizeof(*int_source));
+    int_source->res = *io_source;
+    int_source->free_buffer = FALSE;
+    io_source = &int_source->res;
     
     byte* attr = (byte*) data;
     pid_t* caller_pid = (pid_t*) ((dword)data + 1 );
@@ -79,6 +85,7 @@ void rpc_io_open(pid_t pid, dword correlation_id, size_t data_size, void* data)
     {
         io_res.pid = 0;
         rpc_send_response(pid, correlation_id, sizeof(io_resource_t), (char*) &io_res);
+        free(int_source);
     }
     else
     {
@@ -120,6 +127,7 @@ void rpc_io_open(pid_t pid, dword correlation_id, size_t data_size, void* data)
         if(io_res.id == 0)
         {
             io_res.pid = 0;
+            free(int_source);
         }
     
         rpc_send_response(pid, correlation_id, sizeof(io_resource_t), (char*) &io_res);
@@ -562,13 +570,11 @@ static lostio_filehandle_t* lostio_open(char* path, byte attr, pid_t pid, io_res
     handle->id      = handle_id++;
     handle->pid     = pid;
     handle->flags   = attr;
-    handle->source  = malloc(sizeof(io_resource_t));
+    handle->source  = io_source;
     handle->pos     = 0;
     handle->data    = NULL;
     handle->node    = node;
     
-    memcpy(handle->source, io_source, sizeof(io_resource_t));
-    
     filehandles = list_push(filehandles, (void*) handle);
     //printf("[ LOSTIO ] Oeffne '%s'  '%s'\n", node->name, path);
     
diff --git a/src/modules/lib/posix/socket.c b/src/modules/lib/posix/socket.c
index b8941d7..87112e8 100644
--- a/src/modules/lib/posix/socket.c
+++ b/src/modules/lib/posix/socket.c
@@ -29,6 +29,7 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <unistd.h>
+#include <lostio.h>
 
 #include <stdlib.h>
 #include <stdio.h>
@@ -42,7 +43,7 @@ struct socket {
     struct tree_item    tinfo;
 
     sa_family_t         sa_family;
-    FILE                conn;
+    struct lostio_internal_file conn;
 };
 
 static tree_t* sockets = NULL;
@@ -68,7 +69,7 @@ static int create_socket(struct socket* socket)
         sockets = tree_create(struct socket, tinfo, id);
     }
 
-    socket->id = fileno(&socket->conn);
+    socket->id = fileno(&socket->conn.res);
     tree_insert(sockets, socket);
     return socket->id;
 }
@@ -126,7 +127,7 @@ int connect(int sock, struct sockaddr* address, socklen_t address_len)
     uint16_t port;
     char* path;
     int ret = 0;
-    FILE* conn;
+    struct lostio_internal_file* conn;
 
     if (socket == NULL) {
         errno = EBADF;
@@ -147,7 +148,7 @@ int connect(int sock, struct sockaddr* address, socklen_t address_len)
         goto out_ip_string;
     }
 
-    conn = fopen(path, "r+");
+    conn = (struct lostio_internal_file*) fopen(path, "r+");
     if (conn == NULL) {
         errno = ETIMEDOUT;
         ret = -1;
diff --git a/src/modules/lib/stdlibc/file.c b/src/modules/lib/stdlibc/file.c
index bd4cb1c..34b8171 100644
--- a/src/modules/lib/stdlibc/file.c
+++ b/src/modules/lib/stdlibc/file.c
@@ -142,6 +142,16 @@ FILE* fopen (const char* filename, const char* mode)
         // Pfad im Handle hinterlegen
         io_res->path = malloc(strlen(filename) + 1);
         strcpy(io_res->path, filename);
+
+
+        struct lostio_internal_file* int_res = malloc(sizeof(*int_res));
+        int_res->res = *io_res;
+        int_res->free_buffer = FALSE;
+        setvbuf(&int_res->res, malloc(BUFSIZ), _IOFBF, BUFSIZ);
+        int_res->free_buffer = TRUE;
+
+        free(io_res);
+        io_res = &int_res->res;
     }
     free(resp);
     
@@ -191,6 +201,8 @@ int fclose (FILE* io_res)
         return EOF;
     }
 
+    setvbuf(io_res, NULL, _IONBF, 0);
+
     result = rpc_get_dword(io_res->pid, "IO_CLOSE", sizeof(io_resource_id_t),
         (char*) &(io_res->id));
     free(io_res);
@@ -749,15 +761,21 @@ int fpurge(io_resource_t* io_res)
  */
 int setvbuf(FILE* io_res, char* buffer, int mode, size_t size)
 {
+    struct lostio_internal_file* int_res =
+        (struct lostio_internal_file*) io_res;
+    char* old_buffer;
+
     // Wenn das Dateihandle nicht in Ordung ist, wird abgebrochen
     if (io_res == NULL) {
         return -1;
     }
 
+    old_buffer = io_res->buffer_ptr;
+
 	if (buffer == NULL) {
 		io_res->buffer_ptr = NULL;
 		io_res->buffer_mode = IO_BUFFER_MODE_NONE;
-		return 0;
+        goto out;
 	}
 
 	//Hier wird ein switch benutzt, damit keine nicht Definierten Werte fuer
@@ -791,7 +809,15 @@ int setvbuf(FILE* io_res, char* buffer, int mode, size_t size)
 		default:
 			return -1;
 	}
-	
+
+out:
+    // Wenn bis jetzt ein intern Alloziierter Puffer benutzt wurde, muss er
+    // freigegeben werden.
+    if (int_res->free_buffer && old_buffer && (old_buffer != buffer)) {
+        free(old_buffer);
+        int_res->free_buffer = FALSE;
+    }
+
 	return 0;
 }
 
diff --git a/src/modules/lib/stdlibc/stdio.c b/src/modules/lib/stdlibc/stdio.c
index 6685a82..e1fa127 100644
--- a/src/modules/lib/stdlibc/stdio.c
+++ b/src/modules/lib/stdlibc/stdio.c
@@ -102,6 +102,10 @@ void stdio_init(void)
         } else {
             stderr = fopen(path,  "a");
         }
+
+        // stderr soll ungepuffert sein
+        setvbuf(stderr, NULL, _IONBF, 0);
+
         fclose(path_file);
     }
 }
-- 
1.6.0.6