[1] | 1 | /* |
---|
[23] | 2 | * sys_close.c close an open file |
---|
[1] | 3 | * |
---|
[664] | 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 | /////////////////////////////// |
---|
| 36 | int 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; |
---|
| 42 | vfs_inode_type_t file_type; |
---|
[23] | 43 | |
---|
| 44 | thread_t * this = CURRENT_THREAD; |
---|
| 45 | process_t * process = this->process; |
---|
| 46 | |
---|
[594] | 47 | #if (DEBUG_SYS_CLOSE || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
| 48 | uint64_t tm_start = hal_get_cycles(); |
---|
| 49 | #endif |
---|
| 50 | |
---|
[459] | 51 | #if DEBUG_SYS_CLOSE |
---|
| 52 | if( DEBUG_SYS_CLOSE < tm_start ) |
---|
[594] | 53 | printk("\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 |
---|
| 62 | printk("\n[ERROR] in %s : illegal file descriptor index = %d\n", |
---|
[664] | 63 | __FUNCTION__ , fdid ); |
---|
[625] | 64 | #endif |
---|
[23] | 65 | this->errno = EBADFD; |
---|
[1] | 66 | return -1; |
---|
| 67 | } |
---|
| 68 | |
---|
[23] | 69 | // get extended pointer on remote file descriptor |
---|
[664] | 70 | file_xp = process_fd_get_xptr_from_local( process , fdid ); |
---|
[23] | 71 | |
---|
| 72 | if( file_xp == XPTR_NULL ) |
---|
| 73 | { |
---|
[594] | 74 | |
---|
| 75 | #if DEBUG_SYSCALLS_ERROR |
---|
| 76 | printk("\n[ERROR] in %s : undefined file descriptor %d\n", |
---|
[664] | 77 | __FUNCTION__ , fdid ); |
---|
[594] | 78 | #endif |
---|
| 79 | this->errno = EBADFD; |
---|
[23] | 80 | return -1; |
---|
| 81 | } |
---|
| 82 | |
---|
[625] | 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 |
---|
| 92 | printk("\n[ERROR] in %s : file descriptor %d is a directory\n", |
---|
[664] | 93 | __FUNCTION__ , fdid ); |
---|
[625] | 94 | #endif |
---|
| 95 | this->errno = EBADFD; |
---|
| 96 | return -1; |
---|
| 97 | } |
---|
[664] | 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 |
---|
| 112 | printk("\n[WARNING] in %s : type (%d) not supported / fdid %d\n", |
---|
| 113 | __FUNCTION__ , file_type , fdid ); |
---|
| 114 | #endif |
---|
| 115 | error = 0; |
---|
| 116 | } |
---|
[625] | 117 | |
---|
[23] | 118 | if( error ) |
---|
[1] | 119 | { |
---|
[594] | 120 | |
---|
| 121 | #if DEBUG_SYSCALLS_ERROR |
---|
| 122 | printk("\n[ERROR] in %s : cannot close file descriptor %d\n", |
---|
[664] | 123 | __FUNCTION__ , fdid ); |
---|
[594] | 124 | #endif |
---|
[23] | 125 | this->errno = error; |
---|
[1] | 126 | return -1; |
---|
| 127 | } |
---|
| 128 | |
---|
[124] | 129 | hal_fence(); |
---|
[23] | 130 | |
---|
[594] | 131 | #if (DEBUG_SYS_CLOSE || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
| 132 | uint64_t tm_end = hal_get_cycles(); |
---|
| 133 | #endif |
---|
| 134 | |
---|
[459] | 135 | #if DEBUG_SYS_CLOSE |
---|
| 136 | tm_end = hal_get_cycles(); |
---|
| 137 | if( DEBUG_SYS_CLOSE < tm_start ) |
---|
[594] | 138 | printk("\n[%s] thread[%x,%x] exit / cycle %d\n", |
---|
| 139 | __FUNCTION__, process->pid, this->trdid, (uint32_t)tm_end ); |
---|
[459] | 140 | #endif |
---|
| 141 | |
---|
[594] | 142 | #if CONFIG_INSTRUMENTATION_SYSCALLS |
---|
| 143 | hal_atomic_add( &syscalls_cumul_cost[SYS_CLOSE] , tm_end - tm_start ); |
---|
| 144 | hal_atomic_add( &syscalls_occurences[SYS_CLOSE] , 1 ); |
---|
| 145 | #endif |
---|
| 146 | |
---|
[1] | 147 | return 0; |
---|
| 148 | } |
---|