source: trunk/kernel/syscalls/sys_close.c @ 686

Last change on this file since 686 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.2 KB
Line 
1/*
2 * sys_close.c  kernel function implementing the <close> syscall
3 *
4 * Author       Alain Greiner (2016,2017,2018,2019,2020)
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_special.h>
27#include <vfs.h>
28#include <process.h>
29#include <thread.h>
30#include <ksocket.h>
31#include <printk.h>
32
33#include <syscalls.h>
34
35///////////////////////////////
36int sys_close ( uint32_t fdid )
37{
38    error_t            error;
39    xptr_t             file_xp;
40    cxy_t              file_cxy;
41    vfs_file_t       * file_ptr;
42    vfs_file_type_t   file_type;
43
44        thread_t  * this    = CURRENT_THREAD;
45        process_t * process = this->process;
46
47#if DEBUG_SYS_CLOSE || DEBUG_SYSCALLS_ERROR || CONFIG_INSTRUMENTATION_SYSCALLS
48uint64_t     tm_start = hal_get_cycles();
49#endif
50
51#if DEBUG_SYS_CLOSE
52if( DEBUG_SYS_CLOSE < (uint32_t)tm_start )
53printk("\n[%s] thread[%x,%x] enter / fdid %d / cycle %d\n",
54__FUNCTION__, process->pid, this->trdid, fdid, (uint32_t)tm_start );
55#endif
56 
57    // check fdid argument
58        if( fdid >= CONFIG_PROCESS_FILE_MAX_NR )
59        {
60
61#if DEBUG_SYSCALLS_ERROR
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 );
65#endif
66                this->errno = EBADFD;
67                return -1;
68        }
69
70    // get extended pointer on remote file descriptor
71    file_xp = process_fd_get_xptr_from_local( process , fdid );
72
73    if( file_xp == XPTR_NULL )
74    {
75
76#if DEBUG_SYSCALLS_ERROR
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 );
80#endif
81        this->errno = EBADFD;
82                return -1;
83    }
84
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
90    if( file_type == FILE_TYPE_DIR ) 
91        {
92
93#if DEBUG_SYSCALLS_ERROR
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 );
97#endif
98                this->errno = EBADFD;
99                return -1;
100        }
101    else if( file_type == FILE_TYPE_SOCK )
102    {
103        // call the relevant socket function
104        error = socket_close( file_xp , fdid );
105    }
106    else if( (file_type == FILE_TYPE_REG) ||
107             (file_type == FILE_TYPE_FIFO) )
108    {
109        // call the relevant VFS function
110            error = vfs_close( file_xp , fdid );
111    }
112    else 
113    {
114           
115#if DEBUG_SYSCALLS_ERROR
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 );
119#endif
120        error = 0;       
121    }
122
123        if( error )
124        {
125
126#if DEBUG_SYSCALLS_ERROR
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 );
130#endif
131                this->errno = error;
132                return -1;
133        }
134
135        hal_fence();
136
137#if (DEBUG_SYS_CLOSE || CONFIG_INSTRUMENTATION_SYSCALLS)
138uint64_t     tm_end = hal_get_cycles();
139#endif
140
141#if DEBUG_SYS_CLOSE
142tm_end = hal_get_cycles();
143if( DEBUG_SYS_CLOSE < tm_start )
144printk("\n[%s] thread[%x,%x] exit / cycle %d\n",
145__FUNCTION__, process->pid, this->trdid, (uint32_t)tm_end );
146#endif
147 
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
153        return 0;
154}
Note: See TracBrowser for help on using the repository browser.