Ignore:
Timestamp:
Jun 18, 2017, 10:06:41 PM (7 years ago)
Author:
alain
Message:

Introduce syscalls.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/kernel/syscalls/sys_stat.c

    r1 r23  
    11/*
    2  * kern/sys_stat.c - stats a file or directory
     2 * sys_stat.c - Return statistics on a file or directory.
    33 *
    4  * Copyright (c) 2008,2009,2010,2011,2012 Ghassan Almaless
    5  * Copyright (c) 2011,2012 UPMC Sorbonne Universites
     4 * Author    Alain Greiner  (2016,2017)
    65 *
    7  * This file is part of ALMOS-kernel.
     6 * Copyright (c) UPMC Sorbonne Universites
    87 *
    9  * ALMOS-kernel is free software; you can redistribute it and/or modify it
     8 * This file is part of ALMOS-MKH.
     9 *
     10 * ALMOS-MKH is free software; you can redistribute it and/or modify it
    1011 * under the terms of the GNU General Public License as published by
    1112 * the Free Software Foundation; version 2.0 of the License.
    1213 *
    13  * ALMOS-kernel is distributed in the hope that it will be useful, but
     14 * ALMOS-MKH is distributed in the hope that it will be useful, but
    1415 * WITHOUT ANY WARRANTY; without even the implied warranty of
    1516 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     
    1718 *
    1819 * You should have received a copy of the GNU General Public License
    19  * along with ALMOS-kernel; if not, write to the Free Software Foundation,
     20 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
    2021 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
    2122 */
    2223
     24#include <hal_types.h>
     25#include <hal_uspace.h>
     26#include <hal_special.h>
    2327#include <errno.h>
    2428#include <thread.h>
     29#include <printk.h>
    2530#include <vfs.h>
    26 #include <sys-vfs.h>
    27 #include <task.h>
    28 #include <spinlock.h>
    29 #include <cpu-trace.h>
     31#include <vmm.h>
     32#include <process.h>
    3033
     34//////////////////////////////////////////
     35int sys_stat( uint32_t            file_id,
     36              struct vfs_stat_s * stat )
     37{
     38    error_t           error;
     39    paddr_t           paddr;
     40    struct vfs_stat_s k_stat;
     41    xptr_t            file_xp;
     42       
     43        thread_t  * this    = CURRENT_THREAD;
     44        process_t * process = this->process;
    3145
    32 int sys_stat(char *pathname, struct vfs_stat_s *buff, int fd)
    33 {
    34         CPU_HW_TRACE(sys_stat);
    35         struct thread_s *this;
    36         register error_t err = 0;
    37         struct vfs_file_s *file;
    38         struct ku_obj ku_path;
    39         struct task_s *task;
     46    // check stat structure in user space
     47    error = vmm_v2p_translate( false , stat , &paddr );
    4048
    41         file = NULL;
    42         this = current_thread;
    43         task = current_task;
     49        if( error )
     50        {
     51        printk("\n[ERROR] in %s : stat structure unmapped  for thread %x in process %x\n",
     52               __FUNCTION__ , this->trdid , process->pid );
     53                this->errno = EINVAL;
     54                return -1;
     55        }       
    4456
    45         if((buff == NULL) || ((pathname == NULL) && (fd == -1)))
     57    // get extended pointer on remote file descriptor
     58    file_xp = process_fd_get_xptr( process , file_id );
     59
     60    if( file_xp == XPTR_NULL )
     61    {
     62        printk("\n[ERROR] in %s : undefined file descriptor for thread %x in process %x\n",
     63               __FUNCTION__ , this->trdid , process->pid );
     64        this->errno = EBADFD;
     65        return -1;
     66    }
     67
     68    // call the relevant VFS function
     69    error = vfs_stat( file_xp,
     70                      &k_stat );
     71    if( error )
    4672        {
    47                 this->info.errno = EINVAL;
    48                 CPU_HW_TRACE(sys_stat);
     73        printk("\n[ERROR] in %s : cannot access file %d for thread %x in process %x\n",
     74               __FUNCTION__ , file_id , this->trdid , process->pid );
     75                this->errno = error;
    4976                return -1;
    5077        }
     78   
     79    // copy stat to user space
     80    hal_copy_to_uspace( stat , &k_stat , sizeof(struct vfs_stat_s) );
    5181
    52         if(NOT_IN_USPACE((uint_t)buff))
    53         {
    54                 this->info.errno = EPERM;
    55                 CPU_HW_TRACE(sys_stat);
    56                 return -1;
    57         }
     82    hal_wbflush();
    5883
    59         if(pathname == NULL)
    60         {
    61                 if((fd >= CONFIG_TASK_FILE_MAX_NR) || (task_fd_lookup(task, fd, &file)))
    62                 {
    63                         CPU_HW_TRACE(sys_stat);
    64                         return EBADFD;
    65                 }
    66  
    67                 err = vfs_stat(&task->vfs_cwd, NULL, buff, file);
    68         }
    69         else
    70         {
    71                 KU_BUFF(ku_path, pathname);
    72                 rwlock_rdlock(&task->cwd_lock);
    73                 err = vfs_stat(&task->vfs_cwd, &ku_path, buff, NULL);
    74                 rwlock_unlock(&task->cwd_lock);
    75         }
    76  
    77         this->info.errno = err;
    78         CPU_HW_TRACE(sys_stat);
    7984        return 0;
    80 }
     85
     86}  // end sys_stat()
     87
Note: See TracChangeset for help on using the changeset viewer.