source: trunk/kernel/syscalls/sys_getcwd.c @ 615

Last change on this file since 615 was 610, checked in by alain, 6 years ago

Fix several bugs in VFS to support the following
ksh commandis : cp, mv, rm, mkdir, cd, pwd

File size: 3.4 KB
Line 
1/*
2 * sys_getcwd.c - kernel function implementing the "getcwd" syscall.
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///////////////////////////////////
38int sys_getcwd ( char     * buffer,
39                 uint32_t   nbytes )
40{
41        error_t       error;
42    vseg_t      * vseg;
43    char        * first;                   // first character valid in buffer
44
45    char          kbuf[CONFIG_VFS_MAX_PATH_LENGTH]; 
46
47        thread_t  * this    = CURRENT_THREAD;
48    process_t * process = this->process;
49
50#if (DEBUG_SYS_GETCWD || CONFIG_INSTRUMENTATION_SYSCALLS)
51uint64_t     tm_start = hal_get_cycles();
52#endif
53
54    // check buffer size
55        if( nbytes < CONFIG_VFS_MAX_PATH_LENGTH )
56        {
57
58#if DEBUG_SYSCALLS_ERROR
59printk("\n[ERROR] in %s : buffer too small for thread %x,%x]\n",
60__FUNCTION__ , process->pid, this->trdid );
61#endif
62                this->errno = EINVAL;
63        return -1;
64        }
65
66    // check buffer in user space
67    error = vmm_get_vseg( process, (intptr_t)buffer , &vseg );
68
69        if( error )
70        {
71
72#if DEBUG_SYSCALLS_ERROR
73printk("\n[ERROR] in %s : user buffer unmapped %x for thread[%x,%x]\n",
74__FUNCTION__ , (intptr_t)buffer , process->pid, this->trdid );
75#endif
76                this->errno = EINVAL;
77        return -1;
78        }
79
80#if DEBUG_SYS_GETCWD
81if( DEBUG_SYS_GETCWD < tm_start )
82printk("\n[%s] thread[%x,%x] enter / cycle %d\n",
83__FUNCTION__, process->pid, this->trdid, (uint32_t)tm_start );
84#endif
85
86    // get extended pointer on CWD inode from the reference process
87    xptr_t      ref_xp  = process->ref_xp;
88    process_t * ref_ptr = GET_PTR( ref_xp );
89    cxy_t       ref_cxy = GET_CXY( ref_xp );
90    xptr_t      cwd_xp  = hal_remote_l64( XPTR( ref_cxy , &ref_ptr->cwd_xp ) );
91
92    // call relevant VFS function
93        error = vfs_get_path( cwd_xp,
94                          kbuf, 
95                          &first, 
96                          CONFIG_VFS_MAX_PATH_LENGTH );
97
98    // copy kernel buffer to user space
99    hal_strcpy_to_uspace( buffer , first , CONFIG_VFS_MAX_PATH_LENGTH );
100
101    hal_fence();
102
103#if (DEBUG_SYS_GETCWD || CONFIG_INSTRUMENTATION_SYSCALLS)
104uint64_t     tm_end = hal_get_cycles();
105#endif
106
107#if DEBUG_SYS_GETCWD
108if( DEBUG_SYS_GETCWD < tm_end )
109printk("\n[%s] thread[%x,%x] exit / cycle %d\n",
110__FUNCTION__, process->pid, this->trdid, (uint32_t)tm_end );
111#endif
112 
113#if CONFIG_INSTRUMENTATION_SYSCALLS
114hal_atomic_add( &syscalls_cumul_cost[SYS_GETCWD] , tm_end - tm_start );
115hal_atomic_add( &syscalls_occurences[SYS_GETCWD] , 1 );
116#endif
117
118        return 0;
119
120}  // end sys_getcwd()
Note: See TracBrowser for help on using the repository browser.