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

Re: [tyndur-devel] [PATCH 2/5] libc: lio_compat_open()



On Sun, Sep 18 14:41, Kevin Wolf wrote:
> + libc: lio_compat_open() öffnet einen io_resource_t und wird von
>   fopen() benutzt um ein FILE zu öffnen. Die Idee ist, dass
>   lio_compat_*() die Weiche zwischen LIOv1 und LIOv2 eingebaut
>   bekommen und stdio von den Unterschieden isoliert bleibt.
> 
> Signed-off-by: Kevin Wolf <kevin@xxxxxxxxxx>
> ---
>  src/modules/include/io_struct.h      |    1 -
>  src/modules/include/lostio.h         |    2 +
>  src/modules/lib/lostio/client/file.c |   64 +++++++++++++++++++++++++++++
>  src/modules/lib/stdlibc/file.c       |   73 ++++++++++++----------------------
>  4 files changed, 91 insertions(+), 49 deletions(-)
>  create mode 100644 src/modules/lib/lostio/client/file.c
> 
> diff --git a/src/modules/include/io_struct.h b/src/modules/include/io_struct.h
> index 813fa5d..b769b09 100644
> --- a/src/modules/include/io_struct.h
> +++ b/src/modules/include/io_struct.h
> @@ -48,7 +48,6 @@ typedef uint8_t io_direntry_type_t;
>  typedef struct
>  {
>      io_resource_id_t    id;
> -    char*               path;

Was für ein schönes Relikt. Ich glaube das war mal irgned ein Hack für
fstat() oder sowas. ;-)

>      pid_t               pid;
>      io_resource_id_t    resid;
>  
> diff --git a/src/modules/include/lostio.h b/src/modules/include/lostio.h
> index 3579bf4..f8d3a57 100644
> --- a/src/modules/include/lostio.h
> +++ b/src/modules/include/lostio.h
> @@ -189,6 +189,8 @@ void lostio_type_directory_use(void);
>  void lostio_type_directory_use_as(typeid_t id);
>  
>  
> +/// Stream oeffnen
> +io_resource_t* lio_compat_open(const char* filename, uint8_t attr);
>  
>  /// 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
> new file mode 100644
> index 0000000..3e048bb
> --- /dev/null
> +++ b/src/modules/lib/lostio/client/file.c
> @@ -0,0 +1,64 @@
> +/*
> + * Copyright (c) 2006 The tyndur Project. All rights reserved.
> + *
> + * This code is derived from software contributed to the tyndur Project
> + * by Antoine Kaufmann.
> + *
> + * Redistribution and use in source and binary forms, with or without
> + * modification, are permitted provided that the following conditions
> + * are met:
> + * 1. Redistributions of source code must retain the above copyright
> + *    notice, this list of conditions and the following disclaimer.
> + * 2. Redistributions in binary form must reproduce the above copyright
> + *    notice, this list of conditions and the following disclaimer in the
> + *    documentation and/or other materials provided with the distribution.
> + *
> + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
> + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
> + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
> + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
> + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
> + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
> + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
> + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
> + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
> + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
> + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
> + */
> +
> +#include <lostio.h>
> +#include <stdint.h>
> +#include <string.h>
> +#include <stdlib.h>
> +#include <rpc.h>
> +
> +io_resource_t* lio_compat_open(const char* filename, uint8_t attr)
> +{
> +    char* full_path = io_get_absolute_path(filename);
> +    io_resource_t* result;
> +
> +    if (!full_path) {
> +        return NULL;
> +    }
> +
> +    // RPC-Daten zusammenstellen
> +    char msg[strlen(full_path) + 2];
> +    msg[0] = attr;
> +    memcpy(msg + 1, full_path, strlen(full_path) + 1);
> +
> +    response_t* resp = rpc_get_response(1, "IO_OPEN ", strlen(full_path) + 2,
> +        (char*) msg);
> +    if (resp == NULL || resp->data_length == 0) {

Memory-Leak, full_path wurde nicht freigegeben, hier hilft es wohl 

> +        return NULL;
> +    };
> +
> +    result = resp->data;
> +    if (result->pid == 0) {

dito für full_path, plus resp wird hier geleakt

> +        return NULL;
> +    }
> +
> +    free(full_path);
> +    free(resp);
> +
> +    return result;
> +}
> diff --git a/src/modules/lib/stdlibc/file.c b/src/modules/lib/stdlibc/file.c
> index fa20680..522eba6 100644
> --- a/src/modules/lib/stdlibc/file.c
> +++ b/src/modules/lib/stdlibc/file.c
> @@ -73,91 +73,68 @@ static void* get_shm(size_t size)
>   */
>  FILE* fopen (const char* filename, const char* mode)
>  {
> -    char* full_path = io_get_absolute_path(filename);
> -    if (!full_path) {
> -        return NULL;
> -    }
> +    uint8_t attr;
>  
> -    //RPC-Daten zusammenstellen
> -    char msg[strlen(full_path) + 2];
> -    uint8_t* attr = (uint8_t*) msg;
> -    
>      //Attribute aus dem mode-String parsen
> -    *attr = 0;
> +    attr = 0;
>      int i;
>      for (i = 0; i < strlen(mode); i++) {
>          switch (mode[i]) {
>              case 'r':
> -                *attr |= IO_OPEN_MODE_READ;
> +                attr |= IO_OPEN_MODE_READ;
>                  if (mode[i + 1] == '+') {
> -                    *attr |= IO_OPEN_MODE_WRITE;
> +                    attr |= IO_OPEN_MODE_WRITE;
>                  }
>                  break;
>  
>              case 'w':
> -                *attr |= IO_OPEN_MODE_WRITE | IO_OPEN_MODE_CREATE |
> +                attr |= IO_OPEN_MODE_WRITE | IO_OPEN_MODE_CREATE |
>                      IO_OPEN_MODE_TRUNC;
>  
>                  // Bei w+ muss Lesen auch aktiviert werden.
>                  if (mode[i + 1] == '+') {
> -                    *attr |= IO_OPEN_MODE_READ;
> +                    attr |= IO_OPEN_MODE_READ;
>                  }
>                  break;
>  
>              case 'a':
> -                *attr |= IO_OPEN_MODE_WRITE | IO_OPEN_MODE_CREATE 
> +                attr |= IO_OPEN_MODE_WRITE | IO_OPEN_MODE_CREATE
>                      | IO_OPEN_MODE_APPEND;
>                  break;
>  
>              case 'd':
> -                *attr |= IO_OPEN_MODE_DIRECTORY;
> +                attr |= IO_OPEN_MODE_DIRECTORY;
>                  break;
>  
>              case 'l':
> -                *attr |= IO_OPEN_MODE_LINK;
> +                attr |= IO_OPEN_MODE_LINK;
>                  break;
>  
>          }
>      }
>  
> -    //Dateiname inkl Null-Byte kopieren
> -    memcpy((void*) ((uint32_t)msg + 1), full_path, strlen(full_path) + 1);
> -
> -    response_t* resp = rpc_get_response(1, "IO_OPEN ", strlen(full_path) + 2, 
> -        (char*) msg);
> -
> -    //Da der Pfad vorhin allokiert wurde, muss er hier wieder freigegeben
> -    //weren.
> -    free(full_path);
> +    // Datei oeffnen
> +    io_resource_t* io_res = lio_compat_open(filename, attr);
> +    if (io_res == NULL) {
> +        return NULL;
> +    }
>  
> +    // FILE-Struktur anlegen
>      FILE* int_res;
> -    io_resource_t* io_res = (io_resource_t*) resp->data;
>  
> -    //Wenn ein Fehler beim oeffnen aufgetreten ist, wird NULL zurueck gegeben.
> -    if ((io_res == NULL) || (io_res->pid == 0) || (resp->data_length == 0)) {
> -        int_res = NULL;
> -    } else {
> -
> -        // Pfad im Handle hinterlegen
> -        io_res->path = malloc(strlen(filename) + 1);
> -        strcpy(io_res->path, filename);
> +    int_res = calloc(1, sizeof(*int_res));
> +    int_res->res = *io_res;
>  
> +    int_res->buffer_mode = IO_BUFFER_MODE_NONE;
> +    int_res->buffer_ptr = NULL;
> +    int_res->buffer_size = 0;
> +    int_res->buffer_pos = 0;
> +    int_res->free_buffer = false;
> +    setvbuf(int_res, malloc(BUFSIZ), _IOFBF, BUFSIZ);
> +    int_res->free_buffer = true;
>  
> -        int_res = calloc(1, sizeof(*int_res));
> -        int_res->res = *io_res;
> -
> -        int_res->buffer_mode = IO_BUFFER_MODE_NONE;
> -        int_res->buffer_ptr = NULL;
> -        int_res->buffer_size = 0;
> -        int_res->buffer_pos = 0;
> -        int_res->free_buffer = false;
> -        setvbuf(int_res, malloc(BUFSIZ), _IOFBF, BUFSIZ);
> -        int_res->free_buffer = true;
> +    free(io_res);
>  
> -        free(io_res);
> -    }
> -    free(resp);
> -    
>      return int_res;
>  }

Wenn du die beiden Leaks abgedichtet hast, kannst du das pushen.

Acked-by: Antoine Kaufmann <toni@xxxxxxxxxx>

-- 
Antoine Kaufmann
<toni@xxxxxxxxxxxxxxxx>

Attachment: signature.asc
Description: Digital signature