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

Last change on this file since 664 was 664, checked in by alain, 4 years ago
  • Introduce the sys_socket.c file implementing all socket related syscalls.
  • Improve the non-standard sys_get_config() function.
File size: 3.6 KB
Line 
1/*
2 * sys_close.c  close an open file
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_inode_type_t   file_type;
43
44        thread_t  * this    = CURRENT_THREAD;
45        process_t * process = this->process;
46
47#if (DEBUG_SYS_CLOSE || CONFIG_INSTRUMENTATION_SYSCALLS)
48uint64_t     tm_start = hal_get_cycles();
49#endif
50
51#if DEBUG_SYS_CLOSE
52if( DEBUG_SYS_CLOSE < 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
62printk("\n[ERROR] in %s : illegal file descriptor index = %d\n",
63__FUNCTION__ , fdid );
64#endif
65                this->errno = EBADFD;
66                return -1;
67        }
68
69    // get extended pointer on remote file descriptor
70    file_xp = process_fd_get_xptr_from_local( process , fdid );
71
72    if( file_xp == XPTR_NULL )
73    {
74
75#if DEBUG_SYSCALLS_ERROR
76printk("\n[ERROR] in %s : undefined file descriptor %d\n",
77__FUNCTION__ , fdid );
78#endif
79        this->errno = EBADFD;
80                return -1;
81    }
82
83    // get file type
84    file_cxy  = GET_CXY( file_xp );
85    file_ptr  = GET_PTR( file_xp );
86    file_type = hal_remote_l32( XPTR( file_cxy , &file_ptr->type ) );
87
88    if( file_type == INODE_TYPE_DIR ) 
89        {
90
91#if DEBUG_SYSCALLS_ERROR
92printk("\n[ERROR] in %s : file descriptor %d is a directory\n",
93__FUNCTION__ , fdid );
94#endif
95                this->errno = EBADFD;
96                return -1;
97        }
98    else if( file_type == INODE_TYPE_SOCK )
99    {
100        // call the relevant socket function
101        error = socket_close( file_xp , fdid );
102    }
103    else if( file_type == INODE_TYPE_FILE )
104    {
105        // call the relevant VFS function
106            error = vfs_close( file_xp , fdid );
107    }
108    else 
109    {
110           
111#if DEBUG_SYSCALLS_ERROR
112printk("\n[WARNING] in %s : type (%d) not supported  / fdid %d\n",
113__FUNCTION__ , file_type , fdid );
114#endif
115        error = 0;       
116    }
117
118        if( error )
119        {
120
121#if DEBUG_SYSCALLS_ERROR
122printk("\n[ERROR] in %s : cannot close file descriptor %d\n",
123__FUNCTION__ , fdid );
124#endif
125                this->errno = error;
126                return -1;
127        }
128
129        hal_fence();
130
131#if (DEBUG_SYS_CLOSE || CONFIG_INSTRUMENTATION_SYSCALLS)
132uint64_t     tm_end = hal_get_cycles();
133#endif
134
135#if DEBUG_SYS_CLOSE
136tm_end = hal_get_cycles();
137if( DEBUG_SYS_CLOSE < tm_start )
138printk("\n[%s] thread[%x,%x] exit / cycle %d\n",
139__FUNCTION__, process->pid, this->trdid, (uint32_t)tm_end );
140#endif
141 
142#if CONFIG_INSTRUMENTATION_SYSCALLS
143hal_atomic_add( &syscalls_cumul_cost[SYS_CLOSE] , tm_end - tm_start );
144hal_atomic_add( &syscalls_occurences[SYS_CLOSE] , 1 );
145#endif
146
147        return 0;
148}
Note: See TracBrowser for help on using the repository browser.