[1] | 1 | /* |
---|
[23] | 2 | * sys_stat.c - Return statistics on a file or directory. |
---|
[1] | 3 | * |
---|
[440] | 4 | * Author Alain Greiner (2016,2017,2018) |
---|
[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 | |
---|
[457] | 24 | #include <hal_kernel_types.h> |
---|
[23] | 25 | #include <hal_uspace.h> |
---|
| 26 | #include <hal_special.h> |
---|
[1] | 27 | #include <errno.h> |
---|
| 28 | #include <thread.h> |
---|
[23] | 29 | #include <printk.h> |
---|
[1] | 30 | #include <vfs.h> |
---|
[23] | 31 | #include <vmm.h> |
---|
| 32 | #include <process.h> |
---|
[1] | 33 | |
---|
[407] | 34 | ///////////////////////////////////// |
---|
| 35 | int sys_stat( char * pathname, |
---|
| 36 | struct stat * u_stat ) |
---|
[1] | 37 | { |
---|
[407] | 38 | error_t error; |
---|
[440] | 39 | vseg_t * vseg; // for user space checking |
---|
[407] | 40 | struct stat k_stat; // kernel space |
---|
| 41 | xptr_t file_xp; |
---|
| 42 | char kbuf[CONFIG_VFS_MAX_PATH_LENGTH]; |
---|
[23] | 43 | |
---|
| 44 | thread_t * this = CURRENT_THREAD; |
---|
| 45 | process_t * process = this->process; |
---|
[1] | 46 | |
---|
[23] | 47 | // check stat structure in user space |
---|
[440] | 48 | error = vmm_get_vseg( process , (intptr_t)u_stat , &vseg ); |
---|
[1] | 49 | |
---|
[23] | 50 | if( error ) |
---|
[1] | 51 | { |
---|
[440] | 52 | |
---|
| 53 | #if DEBUG_SYCALL_ERROR |
---|
| 54 | printk("\n[ERROR] in %s : stat structure unmapped %x / thread %x / process %x\n", |
---|
| 55 | __FUNCTION__ , (intptr_t)u_stat , this->trdid , process->pid ); |
---|
| 56 | vmm_display( process , false ); |
---|
| 57 | #endif |
---|
[23] | 58 | this->errno = EINVAL; |
---|
[1] | 59 | return -1; |
---|
[23] | 60 | } |
---|
[1] | 61 | |
---|
[407] | 62 | // check pathname length |
---|
| 63 | if( hal_strlen_from_uspace( pathname ) >= CONFIG_VFS_MAX_PATH_LENGTH ) |
---|
| 64 | { |
---|
| 65 | printk("\n[ERROR] in %s : pathname too long\n", __FUNCTION__ ); |
---|
| 66 | this->errno = ENFILE; |
---|
| 67 | return -1; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | // copy pathname in kernel space |
---|
| 71 | hal_strcpy_from_uspace( kbuf , pathname , CONFIG_VFS_MAX_PATH_LENGTH ); |
---|
| 72 | |
---|
| 73 | // get cluster and local pointer on reference process |
---|
| 74 | xptr_t ref_xp = process->ref_xp; |
---|
| 75 | process_t * ref_ptr = (process_t *)GET_PTR( ref_xp ); |
---|
| 76 | cxy_t ref_cxy = GET_CXY( ref_xp ); |
---|
| 77 | |
---|
| 78 | // get extended pointer on cwd inode |
---|
| 79 | xptr_t cwd_xp = hal_remote_lwd( XPTR( ref_cxy , &ref_ptr->vfs_cwd_xp ) ); |
---|
| 80 | |
---|
| 81 | // get the cwd lock in read mode from reference process |
---|
| 82 | remote_rwlock_rd_lock( XPTR( ref_cxy , &ref_ptr->cwd_lock ) ); |
---|
| 83 | |
---|
[23] | 84 | // get extended pointer on remote file descriptor |
---|
[407] | 85 | error = vfs_lookup( cwd_xp, |
---|
| 86 | pathname, |
---|
| 87 | 0, |
---|
| 88 | &file_xp ); |
---|
[23] | 89 | |
---|
[407] | 90 | // release the cwd lock |
---|
| 91 | remote_rwlock_rd_unlock( XPTR( ref_cxy , &ref_ptr->cwd_lock ) ); |
---|
| 92 | |
---|
| 93 | if( error ) |
---|
[23] | 94 | { |
---|
[407] | 95 | printk("\n[ERROR] in %s : cannot found file <%s> for thread %x in process %x\n", |
---|
| 96 | __FUNCTION__ , pathname , this->trdid , process->pid ); |
---|
| 97 | this->errno = error; |
---|
[23] | 98 | return -1; |
---|
| 99 | } |
---|
| 100 | |
---|
[407] | 101 | // call VFS function to get stat info |
---|
[23] | 102 | error = vfs_stat( file_xp, |
---|
| 103 | &k_stat ); |
---|
| 104 | if( error ) |
---|
[1] | 105 | { |
---|
[407] | 106 | printk("\n[ERROR] in %s : cannot get stats for file %s\n", |
---|
| 107 | __FUNCTION__ , pathname ); |
---|
[23] | 108 | this->errno = error; |
---|
[1] | 109 | return -1; |
---|
| 110 | } |
---|
[23] | 111 | |
---|
[407] | 112 | // copy k_stat to u_stat |
---|
| 113 | hal_copy_to_uspace( u_stat , &k_stat , sizeof(struct stat) ); |
---|
[1] | 114 | |
---|
[124] | 115 | hal_fence(); |
---|
[23] | 116 | |
---|
[1] | 117 | return 0; |
---|
[23] | 118 | |
---|
| 119 | } // end sys_stat() |
---|
| 120 | |
---|