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

Last change on this file since 674 was 670, checked in by alain, 4 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
Line 
1/*
2 * sys_open.c - open a file.
3 *
4 * Author        Alain Greiner (2016,2017,2018,2019)
5 *
6 * Copyright (c) UPMC Sorbonne Universites
7 *
8 * This file is part of ALMOS-MKH.
9 *
10 * ALMOS-MKH is free software; you can redistribute it and/or modify it
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 *
14 * ALMOS-MKH is distributed in the hope that it will be useful, but
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
20 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#include <kernel_config.h>
25#include <hal_kernel_types.h>
26#include <hal_uspace.h>
27#include <errno.h>
28#include <thread.h>
29#include <printk.h>
30#include <vfs.h>
31#include <process.h>
32#include <remote_rwlock.h>
33
34#include <syscalls.h>
35
36///////////////////////////////////
37int sys_open ( char     * pathname,
38               uint32_t   flags,
39               uint32_t   mode )
40{
41    vseg_t       * vseg; 
42    error_t        error;
43    xptr_t         file_xp;                 // extended pointer on vfs_file_t
44    uint32_t       file_id;                 // file descriptor index
45    xptr_t         root_inode_xp;           // extended pointer on root inode
46
47    char           kbuf[CONFIG_VFS_MAX_PATH_LENGTH];
48
49    thread_t     * this     = CURRENT_THREAD;
50    process_t    * process  = this->process;
51
52#if DEBUG_SYS_OPEN || DEBUG_SYSCALLS_ERROR || CONFIG_INSTRUMENTATION_SYSCALLS
53uint64_t     tm_start = hal_get_cycles();
54#endif
55
56    // check fd_array not full
57    if( process_fd_array_full() )
58    {
59
60#if DEBUG_SYSCALLS_ERROR
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 );
64#endif
65        this->errno = ENFILE;
66        return -1;
67    }
68
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
82    // check pathname length
83    if( hal_strlen_from_uspace( pathname ) >= CONFIG_VFS_MAX_PATH_LENGTH )
84    {
85
86#if DEBUG_SYSCALLS_ERROR
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 );
90#endif
91        this->errno = ENFILE;
92        return -1;
93    }
94
95    // copy pathname in kernel space
96    hal_strcpy_from_uspace( XPTR( local_cxy , kbuf ),
97                            pathname,
98                            CONFIG_VFS_MAX_PATH_LENGTH );
99#if DEBUG_SYS_OPEN
100if( DEBUG_SYS_OPEN < tm_start )
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 );
103#endif
104 
105    // get cluster and local pointer on reference process
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 );
109
110    // get extended pointer on root inode in path
111    if( kbuf[0] == '/' )                            // absolute path
112    {
113        // use extended pointer on VFS root inode
114        root_inode_xp = process->vfs_root_xp;
115    }
116    else                                            // relative path
117    {
118        // use extended pointer on CWD inode
119        root_inode_xp = hal_remote_l64( XPTR( owner_cxy , &owner_ptr->cwd_xp ) );
120    }
121
122    // call the relevant VFS function
123    error = vfs_open( root_inode_xp,
124                      kbuf,
125                      owner_xp,
126                      flags,
127                      mode,
128                      &file_xp,
129                      &file_id );
130
131    if( error )
132    {
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
139        this->errno = ENFILE;
140        return -1;
141    }
142
143    hal_fence();
144
145#if (DEBUG_SYS_OPEN || CONFIG_INSTRUMENTATION_SYSCALLS)
146uint64_t     tm_end = hal_get_cycles();
147#endif
148
149#if DEBUG_SYS_OPEN
150if( DEBUG_SYS_OPEN < tm_end )
151printk("\n[%s] thread[%x,%x] exit for <%s> / cycle %d\n",
152__FUNCTION__, process->pid, this->trdid, kbuf, (uint32_t)tm_end );
153#endif
154 
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
160    return file_id;
161}
Note: See TracBrowser for help on using the repository browser.