Changeset a68f737 in mainline


Ignore:
Timestamp:
2009-06-08T12:34:38Z (15 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
d9c8c81
Parents:
f8ef660
Message:

keep a list of open files to support proper cleanup

Location:
uspace/lib/libc
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/libc/generic/io/io.c

    rf8ef660 ra68f737  
    4343#include <vfs/vfs.h>
    4444#include <ipc/devmap.h>
    45 
    46 FILE stdin_null = {
     45#include <libadt/list.h>
     46
     47static FILE stdin_null = {
    4748        .fd = -1,
    4849        .error = true,
     
    5253};
    5354
    54 FILE stdout_klog = {
     55static FILE stdout_klog = {
    5556        .fd = -1,
    5657        .error = false,
     
    6061};
    6162
    62 FILE *stdin = &stdin_null;
    63 FILE *stdout = &stdout_klog;
    64 FILE *stderr = &stdout_klog;
     63static FILE stderr_klog = {
     64        .fd = -1,
     65        .error = false,
     66        .eof = false,
     67        .klog = true,
     68        .phone = -1
     69};
     70
     71FILE *stdin = NULL;
     72FILE *stdout = NULL;
     73FILE *stderr = NULL;
     74
     75static LIST_INITIALIZE(files);
     76
     77void stdio_init(int filc, fdi_node_t *filv[])
     78{
     79        if (filc > 0) {
     80                stdin = fopen_node(filv[0], "r");
     81        } else {
     82                stdin = &stdin_null;
     83                list_append(&stdin->link, &files);
     84        }
     85       
     86        if (filc > 1) {
     87                stdout = fopen_node(filv[1], "w");
     88        } else {
     89                stdout = &stdout_klog;
     90                list_append(&stdout->link, &files);
     91        }
     92       
     93        if (filc > 2) {
     94                stderr = fopen_node(filv[2], "w");
     95        } else {
     96                stderr = &stderr_klog;
     97                list_append(&stderr->link, &files);
     98        }
     99}
     100
     101void stdio_done(void)
     102{
     103        link_t *link = files.next;
     104       
     105        while (link != &files) {
     106                FILE *file = list_get_instance(link, FILE, link);
     107                fclose(file);
     108                link = files.next;
     109        }
     110}
    65111
    66112static bool parse_mode(const char *mode, int *flags)
     
    142188        stream->phone = -1;
    143189       
     190        list_append(&stream->link, &files);
     191       
    144192        return stream;
    145193}
     
    170218        stream->phone = -1;
    171219       
     220        list_append(&stream->link, &files);
     221       
    172222        return stream;
    173223}
     
    185235                rc = close(stream->fd);
    186236       
    187         if ((stream != &stdin_null) && (stream != &stdout_klog))
     237        list_remove(&stream->link);
     238       
     239        if ((stream != &stdin_null)
     240            && (stream != &stdout_klog)
     241            && (stream != &stderr_klog))
    188242                free(stream);
    189243       
     
    354408}
    355409
    356 void fnode(FILE *stream, fdi_node_t *node)
    357 {
    358         if (stream->fd >= 0) {
    359                 fd_node(stream->fd, node);
    360         } else {
    361                 node->fs_handle = 0;
    362                 node->dev_handle = 0;
    363                 node->index = 0;
    364         }
     410int fnode(FILE *stream, fdi_node_t *node)
     411{
     412        if (stream->fd >= 0)
     413                return fd_node(stream->fd, node);
     414       
     415        return ENOENT;
    365416}
    366417
  • uspace/lib/libc/generic/libc.c

    rf8ef660 ra68f737  
    8080                argc = 0;
    8181                argv = NULL;
     82                stdio_init(0, NULL);
    8283        } else {
    8384                argc = __pcb->argc;
    8485                argv = __pcb->argv;
    85                
    86                 if (__pcb->filc > 0)
    87                         stdin = fopen_node(__pcb->filv[0], "r");
    88                
    89                 if (__pcb->filc > 1)
    90                         stdout = fopen_node(__pcb->filv[1], "w");
    91                
    92                 if (__pcb->filc > 2)
    93                         stderr = fopen_node(__pcb->filv[2], "w");
     86                stdio_init(__pcb->filc, __pcb->filv);
    9487        }
    9588       
    9689        main(argc, argv);
    97        
    98         if (stdin != NULL)
    99                 fclose(stdin);
    100        
    101         if (stdout != NULL)
    102                 fclose(stdout);
    103        
    104         if (stderr != NULL)
    105                 fclose(stderr);
     90        stdio_done();
    10691}
    10792
  • uspace/lib/libc/generic/task.c

    rf8ef660 ra68f737  
    106106        fdi_node_t stderr_node;
    107107       
    108         if ((stdin != NULL) && (stdin != &stdin_null)) {
    109                 fnode(stdin, &stdin_node);
     108        if ((stdin != NULL) && (fnode(stdin, &stdin_node) == EOK))
    110109                files[0] = &stdin_node;
    111         } else
     110        else
    112111                files[0] = NULL;
    113112       
    114         if ((stdout != NULL) && (stdout != &stdout_klog)) {
    115                 fnode(stdout, &stdout_node);
     113        if ((stdout != NULL) && (fnode(stdout, &stdout_node) == EOK))
    116114                files[1] = &stdout_node;
    117         } else
     115        else
    118116                files[1] = NULL;
    119117       
    120         if ((stderr != NULL) && (stderr != &stdout_klog)) {
    121                 fnode(stderr, &stderr_node);
     118        if ((stderr != NULL) && (fnode(stderr, &stderr_node) == EOK))
    122119                files[2] = &stderr_node;
    123         } else
     120        else
    124121                files[2] = NULL;
    125122       
  • uspace/lib/libc/generic/vfs/vfs.c

    rf8ef660 ra68f737  
    333333}
    334334
    335 void fd_node(int fildes, fdi_node_t *node)
     335int fd_node(int fildes, fdi_node_t *node)
    336336{
    337337        futex_down(&vfs_phone_futex);
     
    352352                node->dev_handle = (dev_handle_t) dev_handle;
    353353                node->index = (fs_index_t) index;
    354         } else {
    355                 node->fs_handle = 0;
    356                 node->dev_handle = 0;
    357                 node->index = 0;
    358         }
     354        }
     355       
     356        return rc;
    359357}
    360358
  • uspace/lib/libc/include/stdio.h

    rf8ef660 ra68f737  
    3838#include <sys/types.h>
    3939#include <stdarg.h>
     40#include <libadt/list.h>
    4041
    4142#define EOF  (-1)
     
    5657
    5758typedef struct {
     59        /** Linked list pointer. */
     60        link_t link;
     61       
    5862        /** Underlying file descriptor. */
    5963        int fd;
     
    7175        int phone;
    7276} FILE;
    73 
    74 extern FILE stdin_null;
    75 extern FILE stdout_klog;
    7677
    7778extern FILE *stdin;
  • uspace/lib/libc/include/vfs/vfs.h

    rf8ef660 ra68f737  
    5656    unsigned int);
    5757
     58extern void stdio_init(int filc, fdi_node_t *filv[]);
     59extern void stdio_done(void);
     60
    5861extern int open_node(fdi_node_t *, int);
    5962extern int fd_phone(int);
    60 extern void fd_node(int, fdi_node_t *);
     63extern int fd_node(int, fdi_node_t *);
    6164
    6265extern FILE *fopen_node(fdi_node_t *, const char *);
    6366extern int fphone(FILE *);
    64 extern void fnode(FILE *, fdi_node_t *);
     67extern int fnode(FILE *, fdi_node_t *);
    6568
    6669#endif
Note: See TracChangeset for help on using the changeset viewer.