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

Last change on this file since 521 was 506, checked in by viala@…, 6 years ago

[syscalls] add interface in implementation.

Add const where possible, fix protoypes to follow interface.

File size: 3.8 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>
[457]25#include <hal_kernel_types.h>
[23]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
[506]35#include <syscalls.h>
36
[1]37///////////////////////////////////
[506]38int sys_open ( const char * pathname,
39               uint32_t     flags,
40               uint32_t     mode )
[1]41{
[305]42    error_t        error;
43    xptr_t         file_xp;                          // extended pointer on vfs_file_t
44    uint32_t       file_id;                          // file descriptor index
[23]45    char           kbuf[CONFIG_VFS_MAX_PATH_LENGTH];
[1]46
[305]47    thread_t     * this     = CURRENT_THREAD;
48    process_t    * process  = this->process;
[1]49
[459]50#if DEBUG_SYS_OPEN
51uint32_t     tm_start;
52uint32_t     tm_end;
53tm_start = hal_get_cycles();
54#endif
55 
[23]56    // check fd_array not full
[305]57    if( process_fd_array_full() )
58    {
[23]59        printk("\n[ERROR] in %s : file descriptor array full for process %x\n",
60               __FUNCTION__ , process->pid );
[305]61        this->errno = ENFILE;
[23]62        return -1;
[305]63    }
[1]64
[407]65    // check pathname length
66    if( hal_strlen_from_uspace( pathname ) >= CONFIG_VFS_MAX_PATH_LENGTH )
[23]67    {
68        printk("\n[ERROR] in %s : pathname too long\n", __FUNCTION__ );
[305]69        this->errno = ENFILE;
[23]70        return -1;
71    }
[1]72
[407]73    // copy pathname in kernel space
74    hal_strcpy_from_uspace( kbuf , pathname , CONFIG_VFS_MAX_PATH_LENGTH );
75
[459]76#if DEBUG_SYS_OPEN
77if( DEBUG_SYS_OPEN < tm_start )
78printk("\n[DBG] %s : thread %x in process %x enter / path %s / flags %x / cycle %d\n",
79__FUNCTION__, this->trdid, process->pid, kbuf, flags, (uint32_t)tm_start );
80#endif
81 
[23]82    // get cluster and local pointer on reference process
83    xptr_t      ref_xp  = process->ref_xp;
84    process_t * ref_ptr = (process_t *)GET_PTR( ref_xp );
85    cxy_t       ref_cxy = GET_CXY( ref_xp );
86
87    // get the cwd lock in read mode from reference process
[305]88    remote_rwlock_rd_lock( XPTR( ref_cxy , &ref_ptr->cwd_lock ) );
[23]89
90    // call the relevant VFS function
[407]91    error = vfs_open( process,
[23]92                      kbuf,
93                      flags,
94                      mode,
95                      &file_xp,
96                      &file_id );
97
98    // release the cwd lock
[305]99    remote_rwlock_rd_unlock( XPTR( ref_cxy , &ref_ptr->cwd_lock ) );
[23]100
[305]101    if( error )
102    {
[459]103        printk("\n[ERROR] in %s : cannot create file descriptor for %s\n",
104        __FUNCTION__ , kbuf );
[305]105        this->errno = ENFILE;
106        return -1;
107    }
[1]108
[23]109    // update local fd_array
110    remote_spinlock_lock( XPTR( local_cxy , &process->fd_array.lock ) );
[305]111    process->fd_array.array[file_id] = file_xp;
[23]112    remote_spinlock_unlock( XPTR( local_cxy , &process->fd_array.lock ) );
113
[459]114    hal_fence();
115
116#if DEBUG_SYS_OPEN
117tm_end = hal_get_cycles();
118if( DEBUG_SYS_OPEN < tm_start )
119printk("\n[DBG] %s : thread %x in process %x exit / cost %d / cycle %d\n",
120__FUNCTION__, this->trdid, process->pid, (uint32_t)(tm_end - tm_start), (uint32_t)tm_start );
121#endif
122 
[305]123    return file_id;
[1]124}
Note: See TracBrowser for help on using the repository browser.