source: trunk/kernel/syscalls/sys_open.c @ 336

Last change on this file since 336 was 305, checked in by max@…, 7 years ago

Style, and use hal_strcpy_from_uspace.

File size: 3.2 KB
RevLine 
[1]1/*
[23]2 * sys_open.c - open a file.
[305]3 *
[23]4 * Author        Alain Greiner (2016,2017)
[1]5 *
[23]6 * Copyright (c) UPMC Sorbonne Universites
[1]7 *
[23]8 * This file is part of ALMOS-MKH.
9 *
10 * ALMOS-MKH is free software; you can redistribute it and/or modify it
[1]11 * under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2.0 of the License.
13 *
[23]14 * ALMOS-MKH is distributed in the hope that it will be useful, but
[1]15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
[23]20 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
[1]21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
[23]24#include <kernel_config.h>
25#include <hal_types.h>
26#include <hal_uspace.h>
[1]27#include <errno.h>
28#include <thread.h>
[23]29#include <printk.h>
[1]30#include <vfs.h>
31#include <process.h>
[23]32#include <remote_spinlock.h>
33#include <remote_rwlock.h>
[1]34
35///////////////////////////////////
36int sys_open ( char     * pathname,
37               uint32_t   flags,
38               uint32_t   mode )
39{
[305]40    error_t        error;
41    xptr_t         file_xp;                          // extended pointer on vfs_file_t
42    uint32_t       file_id;                          // file descriptor index
[23]43    char           kbuf[CONFIG_VFS_MAX_PATH_LENGTH];
[1]44
[305]45    thread_t     * this     = CURRENT_THREAD;
46    process_t    * process  = this->process;
[1]47
[23]48    // check fd_array not full
[305]49    if( process_fd_array_full() )
50    {
[23]51        printk("\n[ERROR] in %s : file descriptor array full for process %x\n",
52               __FUNCTION__ , process->pid );
[305]53        this->errno = ENFILE;
[23]54        return -1;
[305]55    }
[1]56
[305]57    // get pathname copy in kernel space
58    error = hal_strcpy_from_uspace( kbuf , pathname , CONFIG_VFS_MAX_PATH_LENGTH );
59
60    if( error )
[23]61    {
62        printk("\n[ERROR] in %s : pathname too long\n", __FUNCTION__ );
[305]63        this->errno = ENFILE;
[23]64        return -1;
65    }
[1]66
[23]67    // get cluster and local pointer on reference process
68    xptr_t      ref_xp  = process->ref_xp;
69    process_t * ref_ptr = (process_t *)GET_PTR( ref_xp );
70    cxy_t       ref_cxy = GET_CXY( ref_xp );
71
72    // get extended pointer on cwd inode
73    xptr_t cwd_xp = hal_remote_lwd( XPTR( ref_cxy , &ref_ptr->vfs_cwd_xp ) );
[305]74
[23]75    // get the cwd lock in read mode from reference process
[305]76    remote_rwlock_rd_lock( XPTR( ref_cxy , &ref_ptr->cwd_lock ) );
[23]77
78    // call the relevant VFS function
[305]79    error = vfs_open( cwd_xp,
[23]80                      kbuf,
81                      flags,
82                      mode,
83                      &file_xp,
84                      &file_id );
85
86    // release the cwd lock
[305]87    remote_rwlock_rd_unlock( XPTR( ref_cxy , &ref_ptr->cwd_lock ) );
[23]88
[305]89    if( error )
90    {
[23]91        printk("\n[ERROR] in %s : cannot create file descriptor\n", __FUNCTION__ );
[305]92        this->errno = ENFILE;
93        return -1;
94    }
[1]95
[23]96    // update local fd_array
97    remote_spinlock_lock( XPTR( local_cxy , &process->fd_array.lock ) );
[305]98    process->fd_array.array[file_id] = file_xp;
[23]99    remote_spinlock_unlock( XPTR( local_cxy , &process->fd_array.lock ) );
100
[305]101    return file_id;
[1]102}
Note: See TracBrowser for help on using the repository browser.