source: trunk/kernel/syscalls/sys_open.c

Last change on this file was 670, checked in by alain, 3 years ago

1) Introduce up to 4 command lines arguments in the KSH "load" command.
These arguments are transfered to the user process through the
argc/argv mechanism, using the user space "args" vseg.

2) Introduce the named and anonymous "pipes", for inter-process communication
through the pipe() and mkfifo() syscalls.

3) Introduce the "chat" application to validate the two above mechanisms.

4) Improve printk() and assert() fonctions in printk.c.

File size: 4.8 KB
RevLine 
[1]1/*
[23]2 * sys_open.c - open a file.
[305]3 *
[625]4 * Author        Alain Greiner (2016,2017,2018,2019)
[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_rwlock.h>
[1]33
[506]34#include <syscalls.h>
35
[1]36///////////////////////////////////
[566]37int sys_open ( char     * pathname,
38               uint32_t   flags,
39               uint32_t   mode )
[1]40{
[670]41    vseg_t       * vseg; 
[305]42    error_t        error;
[610]43    xptr_t         file_xp;                 // extended pointer on vfs_file_t
44    uint32_t       file_id;                 // file descriptor index
[670]45    xptr_t         root_inode_xp;           // extended pointer on root inode
[610]46
[23]47    char           kbuf[CONFIG_VFS_MAX_PATH_LENGTH];
[1]48
[305]49    thread_t     * this     = CURRENT_THREAD;
50    process_t    * process  = this->process;
[1]51
[670]52#if DEBUG_SYS_OPEN || DEBUG_SYSCALLS_ERROR || CONFIG_INSTRUMENTATION_SYSCALLS
[594]53uint64_t     tm_start = hal_get_cycles();
[459]54#endif
[594]55
[23]56    // check fd_array not full
[305]57    if( process_fd_array_full() )
58    {
[566]59
60#if DEBUG_SYSCALLS_ERROR
[670]61if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
62printk("\n[ERROR] in %s : file descriptor array full for thread[%x,%x]\n",
63__FUNCTION__ , process->pid , this->trdid );
[566]64#endif
[305]65        this->errno = ENFILE;
[23]66        return -1;
[305]67    }
[1]68
[670]69    // check pathname in user space
70    if( vmm_get_vseg( process, (intptr_t)pathname , &vseg ) )
71        {
72
73#if DEBUG_SYSCALLS_ERROR
74if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
75printk("\n[ERROR] in %s : user buffer unmapped %x for thread[%x,%x]\n",
76__FUNCTION__ , (intptr_t)pathname , process->pid, this->trdid );
77#endif
78                this->errno = EINVAL;
79        return -1;
80        }
81
[407]82    // check pathname length
83    if( hal_strlen_from_uspace( pathname ) >= CONFIG_VFS_MAX_PATH_LENGTH )
[23]84    {
[566]85
86#if DEBUG_SYSCALLS_ERROR
[670]87if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
88printk("\n[ERROR] in %s : pathname too long for thread[%x,%x]\n",
89__FUNCTION__ , process->pid , this->trdid );
[566]90#endif
[305]91        this->errno = ENFILE;
[23]92        return -1;
93    }
[1]94
[407]95    // copy pathname in kernel space
[670]96    hal_strcpy_from_uspace( XPTR( local_cxy , kbuf ),
97                            pathname,
98                            CONFIG_VFS_MAX_PATH_LENGTH );
[459]99#if DEBUG_SYS_OPEN
100if( DEBUG_SYS_OPEN < tm_start )
[594]101printk("\n[%s] thread[%x,%x] enter for <%s> / flags %x / cycle %d\n",
102__FUNCTION__, process->pid, this->trdid, kbuf, flags, (uint32_t)tm_start );
[459]103#endif
104 
[23]105    // get cluster and local pointer on reference process
[670]106    xptr_t      owner_xp  = process->owner_xp;
107    process_t * owner_ptr = GET_PTR( owner_xp );
108    cxy_t       owner_cxy = GET_CXY( owner_xp );
[23]109
[670]110    // get extended pointer on root inode in path
111    if( kbuf[0] == '/' )                            // absolute path
[610]112    {
113        // use extended pointer on VFS root inode
114        root_inode_xp = process->vfs_root_xp;
115    }
[670]116    else                                            // relative path
[610]117    {
118        // use extended pointer on CWD inode
[670]119        root_inode_xp = hal_remote_l64( XPTR( owner_cxy , &owner_ptr->cwd_xp ) );
[610]120    }
[23]121
122    // call the relevant VFS function
[610]123    error = vfs_open( root_inode_xp,
[23]124                      kbuf,
[670]125                      owner_xp,
[23]126                      flags,
127                      mode,
128                      &file_xp,
129                      &file_id );
130
[305]131    if( error )
132    {
[670]133
134#if DEBUG_SYSCALLS_ERROR
135if( DEBUG_SYSCALLS_ERROR < tm_start )
136printk("\n[ERROR] in %s : thread[%x,%x] cannot create file descriptor for %s\n",
137__FUNCTION__ , process->pid , this->trdid , kbuf );
138#endif
[305]139        this->errno = ENFILE;
140        return -1;
141    }
[1]142
[599]143    hal_fence();
[594]144
145#if (DEBUG_SYS_OPEN || CONFIG_INSTRUMENTATION_SYSCALLS)
146uint64_t     tm_end = hal_get_cycles();
147#endif
148
[459]149#if DEBUG_SYS_OPEN
[604]150if( DEBUG_SYS_OPEN < tm_end )
[594]151printk("\n[%s] thread[%x,%x] exit for <%s> / cycle %d\n",
152__FUNCTION__, process->pid, this->trdid, kbuf, (uint32_t)tm_end );
[459]153#endif
154 
[594]155#if CONFIG_INSTRUMENTATION_SYSCALLS
156hal_atomic_add( &syscalls_cumul_cost[SYS_OPEN] , tm_end - tm_start );
157hal_atomic_add( &syscalls_occurences[SYS_OPEN] , 1 );
158#endif
159
[305]160    return file_id;
[1]161}
Note: See TracBrowser for help on using the repository browser.