source: trunk/kernel/syscalls/sys_close.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.2 KB
RevLine 
[1]1/*
[670]2 * sys_close.c  kernel function implementing the <close> syscall
[1]3 *
[670]4 * Author       Alain Greiner (2016,2017,2018,2019,2020)
[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_special.h>
[1]27#include <vfs.h>
28#include <process.h>
29#include <thread.h>
[664]30#include <ksocket.h>
[23]31#include <printk.h>
[1]32
[506]33#include <syscalls.h>
34
[664]35///////////////////////////////
36int sys_close ( uint32_t fdid )
[1]37{
[625]38    error_t            error;
39    xptr_t             file_xp;
40    cxy_t              file_cxy;
41    vfs_file_t       * file_ptr;
[670]42    vfs_file_type_t   file_type;
[23]43
44        thread_t  * this    = CURRENT_THREAD;
45        process_t * process = this->process;
46
[670]47#if DEBUG_SYS_CLOSE || DEBUG_SYSCALLS_ERROR || CONFIG_INSTRUMENTATION_SYSCALLS
[594]48uint64_t     tm_start = hal_get_cycles();
49#endif
50
[459]51#if DEBUG_SYS_CLOSE
[670]52if( DEBUG_SYS_CLOSE < (uint32_t)tm_start )
[594]53printk("\n[%s] thread[%x,%x] enter / fdid %d / cycle %d\n",
[664]54__FUNCTION__, process->pid, this->trdid, fdid, (uint32_t)tm_start );
[459]55#endif
56 
[664]57    // check fdid argument
58        if( fdid >= CONFIG_PROCESS_FILE_MAX_NR )
[1]59        {
[625]60
61#if DEBUG_SYSCALLS_ERROR
[670]62if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
63printk("\n[ERROR] in %s : thread[%x,%x] / illegal file descriptor index = %d\n",
64__FUNCTION__ , process->pid , this->trdid , fdid );
[625]65#endif
[23]66                this->errno = EBADFD;
[1]67                return -1;
68        }
69
[23]70    // get extended pointer on remote file descriptor
[664]71    file_xp = process_fd_get_xptr_from_local( process , fdid );
[23]72
73    if( file_xp == XPTR_NULL )
74    {
[594]75
76#if DEBUG_SYSCALLS_ERROR
[670]77if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
78printk("\n[ERROR] in %s : thread[%x,%x] / undefined file descriptor %d\n",
79__FUNCTION__ , process->pid , this->trdid , fdid );
[594]80#endif
81        this->errno = EBADFD;
[23]82                return -1;
83    }
84
[625]85    // get file type
86    file_cxy  = GET_CXY( file_xp );
87    file_ptr  = GET_PTR( file_xp );
88    file_type = hal_remote_l32( XPTR( file_cxy , &file_ptr->type ) );
89
[670]90    if( file_type == FILE_TYPE_DIR ) 
[625]91        {
92
93#if DEBUG_SYSCALLS_ERROR
[670]94if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
95printk("\n[ERROR] in %s : thread[%x,%x] / file descriptor %d is a directory\n",
96__FUNCTION__ , process->pid , this->trdid , fdid );
[625]97#endif
98                this->errno = EBADFD;
99                return -1;
100        }
[670]101    else if( file_type == FILE_TYPE_SOCK )
[664]102    {
103        // call the relevant socket function
104        error = socket_close( file_xp , fdid );
105    }
[670]106    else if( (file_type == FILE_TYPE_REG) ||
107             (file_type == FILE_TYPE_FIFO) )
[664]108    {
109        // call the relevant VFS function
110            error = vfs_close( file_xp , fdid );
111    }
112    else 
113    {
114           
115#if DEBUG_SYSCALLS_ERROR
[670]116if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
117printk("\n[WARNING] in %s : thread[%x,%x] / file_type (%s) unsupported / fdid %d\n",
118__FUNCTION__ , process->pid , this->trdid , vfs_inode_type_str(file_type) , fdid );
[664]119#endif
120        error = 0;       
121    }
[625]122
[23]123        if( error )
[1]124        {
[594]125
126#if DEBUG_SYSCALLS_ERROR
[670]127if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
128printk("\n[ERROR] in %s : thread[%x,%x] cannot close file descriptor %d\n",
129__FUNCTION__ , process->pid , this->trdid , fdid );
[594]130#endif
[23]131                this->errno = error;
[1]132                return -1;
133        }
134
[124]135        hal_fence();
[23]136
[594]137#if (DEBUG_SYS_CLOSE || CONFIG_INSTRUMENTATION_SYSCALLS)
138uint64_t     tm_end = hal_get_cycles();
139#endif
140
[459]141#if DEBUG_SYS_CLOSE
142tm_end = hal_get_cycles();
143if( DEBUG_SYS_CLOSE < tm_start )
[594]144printk("\n[%s] thread[%x,%x] exit / cycle %d\n",
145__FUNCTION__, process->pid, this->trdid, (uint32_t)tm_end );
[459]146#endif
147 
[594]148#if CONFIG_INSTRUMENTATION_SYSCALLS
149hal_atomic_add( &syscalls_cumul_cost[SYS_CLOSE] , tm_end - tm_start );
150hal_atomic_add( &syscalls_occurences[SYS_CLOSE] , 1 );
151#endif
152
[1]153        return 0;
154}
Note: See TracBrowser for help on using the repository browser.