| 1 | /* | 
|---|
| 2 |  * sys_getcwd.c - get process current work directory | 
|---|
| 3 |  *  | 
|---|
| 4 |  * Author    Alain Greiner (2016,2017,2018) | 
|---|
| 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_uspace.h> | 
|---|
| 27 | #include <hal_special.h> | 
|---|
| 28 | #include <errno.h> | 
|---|
| 29 | #include <vfs.h> | 
|---|
| 30 | #include <vmm.h> | 
|---|
| 31 | #include <process.h> | 
|---|
| 32 | #include <thread.h> | 
|---|
| 33 | #include <printk.h> | 
|---|
| 34 |  | 
|---|
| 35 | #include <syscalls.h> | 
|---|
| 36 |  | 
|---|
| 37 | /* TODO: user page(s) need to be locked  [AG] */ | 
|---|
| 38 |  | 
|---|
| 39 | //////////////////////////////// | 
|---|
| 40 | int sys_getcwd ( char     * buf,  | 
|---|
| 41 |                  uint32_t   nbytes ) | 
|---|
| 42 | { | 
|---|
| 43 |         error_t    error; | 
|---|
| 44 |     vseg_t   * vseg; | 
|---|
| 45 |     char       kbuf[CONFIG_VFS_MAX_PATH_LENGTH]; | 
|---|
| 46 |    | 
|---|
| 47 |         thread_t  * this    = CURRENT_THREAD; | 
|---|
| 48 |     process_t * process = this->process; | 
|---|
| 49 |  | 
|---|
| 50 |     // check buffer size | 
|---|
| 51 |         if( nbytes < CONFIG_VFS_MAX_PATH_LENGTH ) | 
|---|
| 52 |         { | 
|---|
| 53 |  | 
|---|
| 54 | #if DEBUG_SYSCALLS_ERROR | 
|---|
| 55 | printk("\n[ERROR] in %s : buffer too small / thread %x / process %x\n", | 
|---|
| 56 | __FUNCTION__ , this->trdid , process->pid ); | 
|---|
| 57 | #endif | 
|---|
| 58 |                 this->errno = EINVAL; | 
|---|
| 59 |         return -1; | 
|---|
| 60 |         } | 
|---|
| 61 |  | 
|---|
| 62 |     // check buffer in user space | 
|---|
| 63 |     error = vmm_get_vseg( process, (intptr_t)buf , &vseg ); | 
|---|
| 64 |  | 
|---|
| 65 |         if( error ) | 
|---|
| 66 |         { | 
|---|
| 67 |  | 
|---|
| 68 | #if DEBUG_SYSCALLS_ERROR | 
|---|
| 69 | printk("\n[ERROR] in %s : user buffer unmapped %x / thread %x / process %x\n", | 
|---|
| 70 | __FUNCTION__ , (intptr_t)buf , this->trdid , process->pid ); | 
|---|
| 71 | #endif | 
|---|
| 72 |                 this->errno = EINVAL; | 
|---|
| 73 |         return -1; | 
|---|
| 74 |         } | 
|---|
| 75 |  | 
|---|
| 76 |     // get reference process cluster and local pointer | 
|---|
| 77 |     xptr_t      ref_xp  = process->ref_xp; | 
|---|
| 78 |     cxy_t       ref_cxy = GET_CXY( ref_xp ); | 
|---|
| 79 |     process_t * ref_ptr = (process_t *)GET_PTR( ref_xp ); | 
|---|
| 80 |  | 
|---|
| 81 |     // get CWD lock in read mode | 
|---|
| 82 |         remote_rwlock_rd_lock( XPTR( ref_cxy , &ref_ptr->cwd_lock ) ); | 
|---|
| 83 |  | 
|---|
| 84 |     // call relevant VFS function | 
|---|
| 85 |         error = vfs_get_path( XPTR( ref_cxy , &ref_ptr->vfs_cwd_xp ) , | 
|---|
| 86 |                           kbuf , CONFIG_VFS_MAX_PATH_LENGTH ); | 
|---|
| 87 |  | 
|---|
| 88 |     // release CWD lock | 
|---|
| 89 |         remote_rwlock_rd_unlock( XPTR( ref_cxy , &ref_ptr->cwd_lock ) ); | 
|---|
| 90 |  | 
|---|
| 91 |     // copy kernel buffer to user space | 
|---|
| 92 |     hal_copy_to_uspace( buf , kbuf , CONFIG_VFS_MAX_PATH_LENGTH ); | 
|---|
| 93 |  | 
|---|
| 94 |     hal_fence(); | 
|---|
| 95 |  | 
|---|
| 96 |         return 0; | 
|---|
| 97 |  | 
|---|
| 98 | }  // end sys_getcwd() | 
|---|