source: trunk/kernel/syscalls/sys_stat.c @ 545

Last change on this file since 545 was 509, checked in by viala@…, 6 years ago

[syscalls] Typo in macro invocations found with -Wundef.

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