Ignore:
Timestamp:
Jun 18, 2017, 10:06:41 PM (5 years ago)
Author:
alain
Message:

Introduce syscalls.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/kernel/syscalls/sys_open.c

    r1 r23  
    11/*
    2  * kern/sys_open.c - open a named file
     2 * sys_open.c - open a file.
    33 *
    4  * Copyright (c) 2008,2009,2010,2011,2012 Ghassan Almaless
    5  * Copyright (c) 2011,2012 UPMC Sorbonne Universites
     4 * Author        Alain Greiner (2016,2017)
    65 *
    7  * This file is part of ALMOS-kernel.
     6 * Copyright (c) UPMC Sorbonne Universites
    87 *
    9  * ALMOS-kernel is free software; you can redistribute it and/or modify it
     8 * This file is part of ALMOS-MKH.
     9 *
     10 * ALMOS-MKH is free software; you can redistribute it and/or modify it
    1011 * under the terms of the GNU General Public License as published by
    1112 * the Free Software Foundation; version 2.0 of the License.
    1213 *
    13  * ALMOS-kernel is distributed in the hope that it will be useful, but
     14 * ALMOS-MKH is distributed in the hope that it will be useful, but
    1415 * WITHOUT ANY WARRANTY; without even the implied warranty of
    1516 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     
    1718 *
    1819 * You should have received a copy of the GNU General Public License
    19  * along with ALMOS-kernel; if not, write to the Free Software Foundation,
     20 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
    2021 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
    2122 */
    2223
     24#include <kernel_config.h>
     25#include <hal_types.h>
     26#include <hal_uspace.h>
    2327#include <errno.h>
    2428#include <thread.h>
     29#include <printk.h>
    2530#include <vfs.h>
    26 #include <sys-vfs.h>
    2731#include <process.h>
    28 #include <spinlock.h>
    29 #include <cpu-trace.h>
     32#include <remote_spinlock.h>
     33#include <remote_rwlock.h>
    3034
    3135///////////////////////////////////
     
    3438               uint32_t   mode )
    3539{
    36         CPU_HW_TRACE(sys_open);
    3740
    38         error_t            err;
    39         struct vfs_file_s  file;   
    40         struct ku_obj      ku_path;
    41         thread_t         * this     = current_thread;
    42         process_t        * process  = current_process;
    43         uint32_t           fd       = (uint32_t)-1;
     41        error_t        error;
     42        xptr_t         file_xp;                          // extended pointer on vfs_file_t
     43        uint32_t       file_id;                          // file descriptor index
     44    uint32_t       length;                           // pathname length (bytes)
     45    char           kbuf[CONFIG_VFS_MAX_PATH_LENGTH];
    4446
    45         cpu_trace_write(current_cpu, sys_open);
     47        thread_t     * this     = CURRENT_THREAD;
     48        process_t    * process  = this->process;
    4649
    47         if( process_fd_aray_full( process ) )
     50        if( pathname == NULL )
    4851        {
    49                 this->info.errno = ENFILE;
    50         CPU_HW_TRACE(sys_open);
    51         return fd;
     52        printk("\n[ERROR] in %s : pathname is NULL\n", __FUNCTION__ );
     53                this->errno = EINVAL;
     54                return -1;
     55        }       
     56
     57    // check fd_array not full
     58        if( process_fd_array_full() )
     59        {
     60        printk("\n[ERROR] in %s : file descriptor array full for process %x\n",
     61               __FUNCTION__ , process->pid );
     62                this->errno = ENFILE;
     63        return -1;
    5264        }
    5365   
    54         if( VFS_IS( flags , VFS_O_DIRECTORY ) ) VFS_SET( flags , VFS_DIR );
     66    // get pathname length
     67    length = hal_strlen_from_uspace( pathname );
    5568
    56         KU_BUFF( ku_path , pathname );
     69    if( length >= CONFIG_VFS_MAX_PATH_LENGTH )
     70    {
     71        printk("\n[ERROR] in %s : pathname too long\n", __FUNCTION__ );
     72                this->errno = ENFILE;
     73        return -1;
     74    }
     75 
     76        // get pathname copy in kernel space
     77    hal_copy_from_uspace( kbuf, pathname, length );
    5778
    58     // get the cwd lock
    59         rwlock_rdlock( &process->cwd_lock );
     79    // get cluster and local pointer on reference process
     80    xptr_t      ref_xp  = process->ref_xp;
     81    process_t * ref_ptr = (process_t *)GET_PTR( ref_xp );
     82    cxy_t       ref_cxy = GET_CXY( ref_xp );
     83
     84    // get extended pointer on cwd inode
     85    xptr_t cwd_xp = hal_remote_lwd( XPTR( ref_cxy , &ref_ptr->vfs_cwd_xp ) );
    6086   
    61         err = vfs_open( &process->vfs_cwd , &ku_path , flags , mode , &file );
    62         if( err )
     87    // get the cwd lock in read mode from reference process
     88        remote_rwlock_rd_lock( XPTR( ref_cxy , &ref_ptr->cwd_lock ) );
     89
     90    // call the relevant VFS function
     91        error = vfs_open( cwd_xp,
     92                      kbuf,
     93                      flags,
     94                      mode,
     95                      &file_xp,
     96                      &file_id );
     97
     98    // release the cwd lock
     99        remote_rwlock_rd_unlock( XPTR( ref_cxy , &ref_ptr->cwd_lock ) );
     100
     101        if( error )
    63102        {
    64                 this->info.errno = (err < 0 ) ? -err : err;
    65             rwlock_unlock( &process->cwd_lock );
    66             CPU_HW_TRACE(sys_open);
    67             return fd;
    68         }
    69        
    70         err = process_fd_set( process , &file , &fd );
    71         if( err )
    72         {
    73                 vfs_close(&file, NULL);
    74                 this->info.errno = err;
     103        printk("\n[ERROR] in %s : cannot create file descriptor\n", __FUNCTION__ );
     104                this->errno = ENFILE;
     105            return -1;
    75106        }
    76107
    77         rwlock_unlock( &process->cwd_lock );
    78         CPU_HW_TRACE(sys_open);
    79         return fd;
     108    // update local fd_array
     109    remote_spinlock_lock( XPTR( local_cxy , &process->fd_array.lock ) );
     110        process->fd_array.array[file_id] = file_xp;
     111    remote_spinlock_unlock( XPTR( local_cxy , &process->fd_array.lock ) );
     112
     113        return file_id;
    80114}
Note: See TracChangeset for help on using the changeset viewer.