Changeset 302


Ignore:
Timestamp:
Jul 31, 2017, 2:36:48 PM (4 years ago)
Author:
max@…
Message:

Style, and use hal_strcpy_from_uspace.

Location:
trunk/kernel/syscalls
Files:
2 edited

Legend:

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

    r23 r302  
    11/*
    22 * sys_chmod.c - Change file access rights.
    3  * 
     3 *
    44 * Author    Alain Greiner  (2016,2017)
    55 *
     
    3535{
    3636    error_t     error;
    37     paddr_t     paddr;
    38     uint32_t    length;
    3937    char        kbuf[CONFIG_VFS_MAX_PATH_LENGTH];
    40        
    41         thread_t  * this    = CURRENT_THREAD;
    42         process_t * process = this->process;
    4338
    44     // check pathname in user space
    45     error = vmm_v2p_translate( false , pathname , &paddr );
     39    thread_t  * this    = CURRENT_THREAD;
     40    process_t * process = this->process;
    4641
    47         if( error )
    48         {
    49         printk("\n[ERROR] in %s : user pathname unmapped for thread %x in process %x\n",
    50                __FUNCTION__ , this->trdid , process->pid );
    51                 this->errno = EINVAL;
    52                 return -1;
    53         }       
     42    // get pathname copy in kernel space
     43    error = hal_strcpy_from_uspace( kbuf , pathname , CONFIG_VFS_MAX_PATH_LENGTH );
    5444
    55     // get pathname length
    56     length = hal_strlen_from_uspace( pathname );
    57 
    58     if( length >= CONFIG_VFS_MAX_PATH_LENGTH )
     45    if( error )
    5946    {
    6047        printk("\n[ERROR] in %s : pathname too long\n", __FUNCTION__ );
    61                 this->errno = ENFILE;
     48        this->errno = ENFILE;
    6249        return -1;
    6350    }
    64  
    65         // get pathname copy in kernel space
    66     hal_copy_from_uspace( kbuf, pathname, length );
    6751
    6852    // get cluster and local pointer on reference process
     
    7357    // get extended pointer on cwd inode
    7458    xptr_t cwd_xp = hal_remote_lwd( XPTR( ref_cxy , &ref_ptr->vfs_cwd_xp ) );
    75    
     59
    7660    // get the cwd lock in read mode from reference process
    77         remote_rwlock_rd_lock( XPTR( ref_cxy , &ref_ptr->cwd_lock ) );
     61    remote_rwlock_rd_lock( XPTR( ref_cxy , &ref_ptr->cwd_lock ) );
    7862
    7963    // call the relevant VFS function
    80         error = vfs_chmod( cwd_xp,
     64    error = vfs_chmod( cwd_xp,
    8165                       kbuf,
    8266                       rights );
    8367
    8468    // release the cwd lock
    85         remote_rwlock_rd_unlock( XPTR( ref_cxy , &ref_ptr->cwd_lock ) );
     69    remote_rwlock_rd_unlock( XPTR( ref_cxy , &ref_ptr->cwd_lock ) );
    8670
    8771    if( error )
    88         {
     72    {
    8973        printk("\n[ERROR] in %s : cannot remove directory %s\n",
    9074               __FUNCTION__ , kbuf );
    91                 this->errno = error;
    92                 return -1;
    93         }
    94    
    95         return 0;
     75        this->errno = error;
     76        return -1;
     77    }
    9678
    97 }  // end sys_chmod()   
     79    return 0;
     80
     81}  // end sys_chmod()
  • trunk/kernel/syscalls/sys_exec.c

    r101 r302  
    11/*
    22 * sys_exec.c - Kernel function implementing the "exec" system call.
    3  * 
     3 *
    44 * Authors   Alain Greiner (2016,2017)
    55 *
     
    3737
    3838
    39 ////////////////////////////////////////////////i////////////////////////////////////// 
     39////////////////////////////////////////////////i//////////////////////////////////////
    4040// This static function is called by the sys_exec() function to register the .elf
    4141// pathname in the exec_info  structure, from a string stored in user space.
    42 ////////////////////////////////////////////////i////////////////////////////////////// 
     42////////////////////////////////////////////////i//////////////////////////////////////
    4343// @ exec_info : pointer on the exec_info structure.
    4444// @ pathname  : string containing the path to the .elf file (user space).
    45 // return 0 if success / non-zero if one string too long, or too many strings. 
     45// return 0 if success / non-zero if one string too long, or too many strings.
    4646///////////////////////////////////////////////////////////////////////////////////////
    4747static error_t process_exec_get_path( exec_info_t  * exec_info,
    4848                                      char         * pathname )
    4949{
    50     uint32_t length;
    51 
    52     // get string length
    53         length = hal_strlen_from_uspace( pathname );
    54 
    55         if( length >= CONFIG_VFS_MAX_PATH_LENGTH ) return EINVAL;
     50    error_t error;
    5651
    5752    // copy string to exec_info
    58     hal_copy_from_uspace( &exec_info->path[0] , pathname , length );
     53    error = hal_strcpy_from_uspace( &exec_info->path[0] , pathname ,
     54                                    CONFIG_VFS_MAX_PATH_LENGTH );
     55    if( error )
     56        return EINVAL;
    5957
    6058    return 0;
    6159}
    6260
    63 ////////////////////////////////////////////////i////////////////////////////////////// 
     61////////////////////////////////////////////////i//////////////////////////////////////
    6462// This static function is called twice by the sys_exec() function :
    6563// - to register the main() arguments (args) in the exec_info structure.
    6664// - to register the environment variables (envs) in the exec_info structure.
    6765// In both cases the input is an array of string pointers in user space,
    68 // and a set of strings in user space. 
     66// and a set of strings in user space.
    6967// We allocate one physical page to store a kernel copy of the array of pointers,
    7068// we allocate one or several physical pages to store the strings themselve,
    7169// and register these buffers and the number of strings in the exec_info structure.
    72 // The max number of strings is 1024 (for both args and envs). The numbers of pages 
     70// The max number of strings is 1024 (for both args and envs). The numbers of pages
    7371// to store the (args) and (envs) strings are configuration parameters.
    74 /////////////////////////////////////////////////////////////////////////////////////// 
     72///////////////////////////////////////////////////////////////////////////////////////
    7573// @ exec_info : pointer on the exec_info structure.
    7674// @ is_args   : true if called for (args) / false if called for (envs).
    7775// @ pointers  : array of pointers on the strings (in user space).
    78 // @ return 0 if success / non-zero if too many strings or no more memory. 
     76// @ return 0 if success / non-zero if too many strings or no more memory.
    7977///////////////////////////////////////////////////////////////////////////////////////
    8078static error_t process_exec_get_strings( exec_info_t  * exec_info,
    8179                                         bool_t         is_args,
    82                                                      char        ** u_pointers )
     80                                         char        ** u_pointers )
    8381{
    8482    uint32_t     index;       // string index
     
    9694    else          order = bits_log2( CONFIG_VMM_ENVS_SIZE );
    9795
    98         req.type   = KMEM_PAGE;
    99         req.flags  = AF_KERNEL | AF_ZERO;
     96    req.type   = KMEM_PAGE;
     97    req.flags  = AF_KERNEL | AF_ZERO;
    10098
    10199    // allocate one physical page for kernel array of pointers
     
    106104
    107105    k_pointers = ppm_page2vaddr( page );
    108    
     106
    109107    // allocate several physical pages to store the strings themselve
    110108    req.type   = order;
     
    114112
    115113    k_buf_base = ppm_page2vaddr( page );
    116    
    117     // copy the array of pointers to kernel buffer 
    118     hal_copy_from_uspace( k_pointers, 
     114
     115    // copy the array of pointers to kernel buffer
     116    hal_copy_from_uspace( k_pointers,
    119117                          u_pointers,
    120118                          CONFIG_PPM_PAGE_SIZE );
     
    125123    for( index = 0 ; index < 1024 ; index++ )
    126124    {
    127         if( k_pointers[index] == NULL ) 
     125        if( k_pointers[index] == NULL )
    128126        {
    129127            found_null = 1;
     
    132130
    133131        // compute string length
    134             length = hal_strlen_from_uspace( k_pointers[index] );
    135  
     132        length = hal_strlen_from_uspace( k_pointers[index] );
     133
    136134        // copy the user string to kernel buffer
    137135        hal_copy_from_uspace( k_buf_ptr,
     
    146144    }
    147145
    148     // update into exec_info structure 
     146    // update into exec_info structure
    149147    if( found_null && is_args )
    150148    {
     
    160158        exec_info->envs_nr        =  index;
    161159    }
    162     else 
     160    else
    163161    {
    164162        return EINVAL;
     
    181179{
    182180    exec_info_t  exec_info;         // structure to pass to process_make_exec()
    183         error_t      error;
     181    error_t      error;
    184182    paddr_t      paddr;
    185183
    186184    thread_t   * this    = CURRENT_THREAD;
    187         process_t  * process = this->process;
     185    process_t  * process = this->process;
    188186
    189187    // check argument fileme
    190188    error = vmm_v2p_translate( false , filename , &paddr );
    191189
    192         if( error )
     190    if( error )
    193191    {
    194192        printk("\n[ERROR] in %s : filename unmapped\n", __FUNCTION__ );
     
    200198    error = vmm_v2p_translate( false , args , &paddr );
    201199
    202         if( error )
     200    if( error )
    203201    {
    204202        printk("\n[ERROR] in %s : args unmapped\n", __FUNCTION__ );
     
    210208    error = vmm_v2p_translate( false , envs , &paddr );
    211209
    212         if( error )
     210    if( error )
    213211    {
    214212        printk("\n[ERROR] in %s : envs unmapped\n", __FUNCTION__ );
     
    224222    exec_dmsg("\n[INFO] %s starts for process %x on core %d in cluster %x"
    225223                 " / target_cluster = %x / cycle %d\n",
    226                  __FUNCTION__, process->pid , CURRENT_CORE->lid, 
     224                 __FUNCTION__, process->pid , CURRENT_CORE->lid,
    227225                 cxy_client, cxy_server, hal_get_cycles());
    228226
    229     // register reference parent process in exec_info 
     227    // register reference parent process in exec_info
    230228    exec_info.parent_xp   = process->ref_xp;
    231229
    232         // check pathname and store it in exec_info structure
    233         error = process_exec_get_path( &exec_info , filename );
    234 
    235         if ( error )
     230    // check pathname and store it in exec_info structure
     231    error = process_exec_get_path( &exec_info , filename );
     232
     233    if ( error )
    236234    {
    237235        printk("\n[ERROR] in %s : elf pathname too long\n", __FUNCTION__ );
     
    240238    }
    241239
    242         // check and store args in exec_info structure
    243         error = process_exec_get_strings( &exec_info , true , args );
    244 
    245         if( error ) 
     240    // check and store args in exec_info structure
     241    error = process_exec_get_strings( &exec_info , true , args );
     242
     243    if( error )
    246244    {
    247245        printk("\n[ERROR] in %s : cannot access args\n", __FUNCTION__ );
     
    249247        return -1;
    250248    }
    251        
    252         // check and store envs in exec_info structure
    253         error = process_exec_get_strings( &exec_info , false , envs );
    254 
    255         if( error )
     249
     250    // check and store envs in exec_info structure
     251    error = process_exec_get_strings( &exec_info , false , envs );
     252
     253    if( error )
    256254    {
    257255        printk("\n[ERROR] in %s : cannot access envs\n", __FUNCTION__ );
     
    259257        return -1;
    260258    }
    261        
     259
    262260    exec_dmsg("\n[INFO] %s starts exec for process %x at cycle %d\n",
    263261              __FUNCTION__, process->pid, hal_get_cycles() );
Note: See TracChangeset for help on using the changeset viewer.