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

Re: [tyndur-devel] [PATCH 3/5] libc: lio_compat_write/close()



On Sun, Sep 18 14:41, Kevin Wolf wrote:
> * libc: lio_compat_write/close aus den stdio-Funktionen
>   herausgesplittet. Mehr oder weniger nur verschobener Code.
> 
> Signed-off-by: Kevin Wolf <kevin@xxxxxxxxxx>
> ---
>  src/modules/include/lostio.h         |    7 ++++
>  src/modules/lib/lostio/client/file.c |   50 ++++++++++++++++++++++++++++
>  src/modules/lib/stdlibc/file.c       |   60 +++++----------------------------
>  3 files changed, 66 insertions(+), 51 deletions(-)
> 
> diff --git a/src/modules/include/lostio.h b/src/modules/include/lostio.h
> index f8d3a57..794a125 100644
> --- a/src/modules/include/lostio.h
> +++ b/src/modules/include/lostio.h
> @@ -192,6 +192,13 @@ void lostio_type_directory_use_as(typeid_t id);
>  /// Stream oeffnen
>  io_resource_t* lio_compat_open(const char* filename, uint8_t attr);
>  
> +/// Stream schliessen
> +int lio_compat_close(io_resource_t* io_res);
> +
> +/// In einen Stream schreiben
> +size_t lio_compat_write(const void* src, size_t blocksize, size_t blockcount,
> +    io_resource_t* io_res);
> +
>  /// Stream-Position setzen
>  bool lio_seek(struct lostio_internal_file* io_res, uint64_t offset, int origin);
>  
> diff --git a/src/modules/lib/lostio/client/file.c b/src/modules/lib/lostio/client/file.c
> index 3e048bb..f474132 100644
> --- a/src/modules/lib/lostio/client/file.c
> +++ b/src/modules/lib/lostio/client/file.c
> @@ -31,6 +31,7 @@
>  #include <string.h>
>  #include <stdlib.h>
>  #include <rpc.h>
> +#include <syscall.h>
>  
>  io_resource_t* lio_compat_open(const char* filename, uint8_t attr)
>  {
> @@ -62,3 +63,52 @@ io_resource_t* lio_compat_open(const char* filename, uint8_t attr)
>  
>      return result;
>  }
> +
> +int lio_compat_close(io_resource_t* io_res)
> +{
> +    uint32_t result;
> +    result = rpc_get_dword(io_res->pid, "IO_CLOSE",
> +        sizeof(io_resource_id_t), (char*) &(io_res->id));
> +    free(io_res);

So wie der Code aktuell ist, gibt das beim Aufruf aus fclose() ein
ungueltiges free(). Das res-Feld ist ja kein Pointer auf io_resource_t,
sondern direkt io_resource_t. Der in lio_compat_open allozierte Speicher
für die Resource wird ja noch direkt im fopen freigegeben, und die
resource kopiert.

> +
> +    return result;
> +}
> +
> +size_t lio_compat_write(const void* src, size_t blocksize, size_t blockcount,
> +    io_resource_t* io_res)
> +{
> +    size_t data_size = blockcount * blocksize;
> +    size_t request_size = sizeof(io_write_request_t);
> +
> +    // Bei mehr als einem Kilobyte wird shared memory fuer die Daten benutzt
> +    if (data_size < 1024) {
> +        request_size += data_size;
> +    }
> +    uint8_t request[request_size];
> +
> +    io_write_request_t* write_request = (io_write_request_t*) request;
> +    write_request->id = io_res->id;
> +    write_request->blocksize = blocksize;
> +    write_request->blockcount = blockcount;
> +
> +    // Wenn kein SHM benutzt wird, werden die Daten direkt an den Request
> +    // angehaengt
> +    if (data_size < 1024) {
> +        write_request->shared_mem_id = 0;
> +        memcpy(write_request->data, src, data_size);
> +    } else {
> +        write_request->shared_mem_id = create_shared_memory(data_size);
> +        void* data = open_shared_memory(write_request->shared_mem_id);
> +        memcpy(data, src, data_size);
> +    }
> +
> +    size_t resp = rpc_get_dword(io_res->pid, "IO_WRITE",
> +        request_size, (char*) request);
> +
> +    if (data_size >= 1024) {
> +        close_shared_memory(write_request->shared_mem_id);
> +    }
> +    return resp;
> +}
> +
> +
> diff --git a/src/modules/lib/stdlibc/file.c b/src/modules/lib/stdlibc/file.c
> index 522eba6..9f47e13 100644
> --- a/src/modules/lib/stdlibc/file.c
> +++ b/src/modules/lib/stdlibc/file.c
> @@ -174,8 +174,8 @@ FILE* freopen(const char* path, const char* mode, FILE* stream)
>   */
>  int fclose (FILE* io_res)
>  {
> -    uint32_t result;
> -    
> +    int result;
> +
>      if((io_res == NULL) || (io_res->res.pid == 0))
>      {
>          return EOF;
> @@ -184,11 +184,9 @@ int fclose (FILE* io_res)
>      fflush(io_res);
>      setvbuf(io_res, NULL, _IONBF, 0);
>  
> -    result = rpc_get_dword(io_res->res.pid, "IO_CLOSE", sizeof(io_resource_id_t),
> -        (char*) &(io_res->res.id));
> -    free(io_res);
> +    result = lio_compat_close(&io_res->res);
>  
> -    return result;
> +    return result ? EOF : 0;
>  }
>  
>  
> @@ -394,47 +392,6 @@ int ungetc(int c, FILE* io_res)
>  }
>  
>  /**
> - *
> - */
> -static size_t io_write(const void* src, size_t blocksize, size_t blockcount,
> -    FILE* io_res)
> -{   
> -    size_t data_size = blockcount * blocksize;
> -    size_t request_size = sizeof(io_write_request_t);
> -    
> -    // Bei mehr als einem Kilobyte wird shared memory fuer die Daten benutzt
> -    if (data_size < 1024) {
> -        request_size += data_size;
> -    }
> -    uint8_t request[request_size];
> -
> -    io_write_request_t* write_request = (io_write_request_t*) request;    
> -    write_request->id = io_res->res.id;
> -    write_request->blocksize = blocksize;
> -    write_request->blockcount = blockcount;
> -
> -    // Wenn kein SHM benutzt wird, werden die Daten direkt an den Request
> -    // angehaengt
> -    if (data_size < 1024) {
> -        write_request->shared_mem_id = 0;
> -        memcpy(write_request->data, src, data_size);
> -    } else {
> -        write_request->shared_mem_id = create_shared_memory(data_size);
> -        void* data = open_shared_memory(write_request->shared_mem_id);
> -        memcpy(data, src, data_size);
> -    }
> -    
> -    size_t resp = rpc_get_dword(io_res->res.pid, "IO_WRITE",
> -        request_size, (char*) request);
> -    
> -    if (data_size >= 1024) {
> -        close_shared_memory(write_request->shared_mem_id);
> -    }
> -    return resp;
> -}
> -
> -
> -/**
>   * Daten in die Datei schreiben.
>   *
>   * @param data Pointer auf die Quelldaten
> @@ -469,7 +426,7 @@ size_t fwrite(const void* data, size_t blocksize, size_t blockcount,
>      // gespeichert, sondern direkt an den Zielprozess gesendet.
>  	switch (io_res->buffer_mode) {
>  		case IO_BUFFER_MODE_NONE:
> -			return io_write(data, blocksize, blockcount, io_res);
> +			return lio_compat_write(data, blocksize, blockcount, &io_res->res);
>  			break;
>  		
>  		case IO_BUFFER_MODE_FULL:
> @@ -486,7 +443,7 @@ size_t fwrite(const void* data, size_t blocksize, size_t blockcount,
>  				if (fflush(io_res) == EOF) {
>                      return 0;
>                  }
> -				return io_write(data, blocksize, blockcount, io_res);
> +				return lio_compat_write(data, blocksize, blockcount, &io_res->res);
>  			}
>  			break;
>  		
> @@ -501,7 +458,7 @@ size_t fwrite(const void* data, size_t blocksize, size_t blockcount,
>  			    if (fflush(io_res) == EOF) {
>                      return 0;
>                  }
> -				return io_write(data, blocksize, blockcount, io_res);
> +				return lio_compat_write(data, blocksize, blockcount, &io_res->res);
>  			} else {
>  				//Daten in den Buffer kopieren.
>  				memcpy((void*)((uint32_t) io_res->buffer_ptr +
> @@ -710,7 +667,8 @@ int fflush(FILE* io_res)
>          return 0;
>      }
>  
> -    size_t size = io_write(io_res->buffer_ptr, 1, io_res->buffer_pos, io_res);
> +    size_t size = lio_compat_write(io_res->buffer_ptr,
> +        1, io_res->buffer_pos, &io_res->res);
>      
>      if (size == io_res->buffer_pos) {
>          io_res->buffer_pos = 0;

Auch hier vertraue ich mal darauf, dass du alle io_write-Vorkommen
erwischt hast. Beim free() oben stellt sich halt die Frage, einfach
rausnehmen macht halt das lio_compat_*()-Interface etwas inkonsistent,
aber jo da das eh niemand direkt benutzen soll, ist das vielleicht die
einfachste Lösung. Ich könnte auf jeden Fall damit leben.

Somit also ohne free()
Acked-by: Antoine Kaufmann <toni@xxxxxxxxxx>

-- 
Antoine Kaufmann
<toni@xxxxxxxxxxxxxxxx>

Attachment: signature.asc
Description: Digital signature