Ignore:
Timestamp:
Oct 10, 2020, 4:50:41 PM (4 years ago)
Author:
alain
Message:

Introduce the ksocket.h & ksocket.c files in kernel/kern.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/kernel/kern/process.c

    r657 r662  
    10771077
    10781078///////////////////////////////////////////
     1079char * process_fd_type_str( uint32_t type )
     1080{
     1081    switch( type )
     1082    {
     1083        case INODE_TYPE_FILE : return "FILE";
     1084        case INODE_TYPE_DIR  : return "DIR";
     1085        case INODE_TYPE_FIFO : return "FIFO";
     1086        case INODE_TYPE_PIPE : return "PIPE";
     1087        case INODE_TYPE_SOCK : return "SOCK";
     1088        case INODE_TYPE_DEV  : return "DEV";
     1089        case INODE_TYPE_BLK  : return "BLK";
     1090        case INODE_TYPE_SYML : return "SYML";
     1091       
     1092        default              : return "undefined";
     1093    }
     1094}
     1095   
     1096///////////////////////////////////////////
    10791097void process_fd_init( process_t * process )
    10801098{
     
    10851103
    10861104    // initialize number of open files
    1087     process->fd_array.current = 0;
     1105    process->fd_array.max = 0;
    10881106
    10891107    // initialize array
     
    11011119    bool_t    found;
    11021120    uint32_t  id;
    1103     xptr_t    xp;
     1121    uint32_t  max;             // current value of max non-free slot index
     1122    xptr_t    entry_xp;        // current value of one fd_array entry
     1123    xptr_t    lock_xp;         // extended pointer on lock protecting fd_array
     1124    xptr_t    max_xp;          // extended pointer on max field in fd_array
    11041125
    11051126    // get target process cluster and local pointer
     
    11071128    cxy_t       process_cxy = GET_CXY( process_xp );
    11081129
    1109 // check target process is reference process
    1110 assert( (process_xp == hal_remote_l64( XPTR( process_cxy , &process_ptr->ref_xp ) ) ),
    1111 "client process must be reference process\n" );
     1130// check target process is owner process
     1131assert( (process_xp == hal_remote_l64( XPTR( process_cxy , &process_ptr->owner_xp ) ) ),
     1132"process must be owner process\n" );
    11121133
    11131134#if DEBUG_PROCESS_FD_REGISTER
     
    11201141#endif
    11211142
    1122     // build extended pointer on lock protecting reference fd_array
    1123     xptr_t lock_xp = XPTR( process_cxy , &process_ptr->fd_array.lock );
     1143    // build extended pointers on lock & max
     1144    lock_xp = XPTR( process_cxy , &process_ptr->fd_array.lock );
     1145    max_xp  = XPTR( process_cxy , &process_ptr->fd_array.max );
    11241146
    11251147    // take lock protecting reference fd_array
     
    11281150    found   = false;
    11291151
     1152    // get current value of max_fdid
     1153    max = hal_remote_l32( max_xp );
     1154
    11301155    for ( id = 0; id < CONFIG_PROCESS_FILE_MAX_NR ; id++ )
    11311156    {
    1132         xp = hal_remote_l64( XPTR( process_cxy , &process_ptr->fd_array.array[id] ) );
    1133         if ( xp == XPTR_NULL )
     1157        // get fd_array entry
     1158        entry_xp = hal_remote_l64( XPTR( process_cxy , &process_ptr->fd_array.array[id] ) );
     1159       
     1160        if ( entry_xp == XPTR_NULL )
    11341161        {
    1135             // update reference fd_array
     1162            // update fd_array
    11361163            hal_remote_s64( XPTR( process_cxy , &process_ptr->fd_array.array[id] ) , file_xp );
    1137                 hal_remote_atomic_add( XPTR( process_cxy , &process_ptr->fd_array.current ) , 1 );
    1138 
    1139             // exit
     1164
     1165            // update max when required
     1166            if( id > max ) hal_remote_s32( max_xp , id );
     1167
     1168            // increase file refcount
     1169            vfs_file_count_up( file_xp );
     1170
     1171            // exit loop
    11401172                        *fdid = id;
    11411173            found = true;
     
    11651197    pid_t       pid;           // target process PID
    11661198    lpid_t      lpid;          // target process LPID
     1199    xptr_t      file_xp;       // extended pointer on file descriptor
    11671200    xptr_t      iter_xp;       // iterator for list of process copies
    11681201    xptr_t      copy_xp;       // extended pointer on process copy
     
    11701203    cxy_t       copy_cxy;      // process copy cluster identifier
    11711204
    1172 // check process_xp argument
    1173 assert( (process_xp != XPTR_NULL), "process_xp argument cannot be XPTR_NULL");
    1174 
    11751205    // get target process cluster and local pointer
    11761206    process_t * process_ptr = GET_PTR( process_xp );
    11771207    cxy_t       process_cxy = GET_CXY( process_xp );
    11781208
     1209// check target process is owner process
     1210assert( (process_xp == hal_remote_l64( XPTR( process_cxy , &process_ptr->owner_xp ) ) ),
     1211"process must be owner process\n" );
     1212
    11791213    // get target process pid and lpid
    11801214    pid  = hal_remote_l32( XPTR( process_cxy , &process_ptr->pid) );
    11811215    lpid = LPID_FROM_PID( pid );
    1182 
    1183     // get process descriptor in owner cluster and in reference cluster
    1184     xptr_t  owner_xp = hal_remote_l64( XPTR( process_cxy , &process_ptr->owner_xp ));
    1185     xptr_t  ref_xp   = hal_remote_l64( XPTR( process_cxy , &process_ptr->ref_xp   ));
    1186 
    1187 // check target process in in owner cluster
    1188 assert( (process_xp == owner_xp), "target process must be in owner process\n" );
    11891216
    11901217#if DEBUG_PROCESS_FD_REMOVE
     
    11961223#endif
    11971224
     1225    // get extended pointer on file descriptor
     1226    file_xp = hal_remote_l64( XPTR( process_cxy , &process_ptr->fd_array.array[fdid] ));
     1227
    11981228    // build extended pointers on list_of_copies root and lock (in owner cluster)
    11991229    xptr_t copies_root_xp = XPTR( process_cxy , &LOCAL_CLUSTER->pmgr.copies_root[lpid] );
    12001230    xptr_t copies_lock_xp = XPTR( process_cxy , &LOCAL_CLUSTER->pmgr.copies_lock[lpid] );
    12011231 
    1202     // get reference process cluster and local pointer
    1203     process_t * ref_ptr = GET_PTR( ref_xp );
    1204     cxy_t       ref_cxy = GET_CXY( ref_xp );
    1205 
    1206     // build extended pointer on lock protecting reference fd_array
    1207     xptr_t fd_lock_xp = XPTR( ref_cxy , &ref_ptr->fd_array.lock );
    1208 
    1209     // take lock protecting reference fd_array
     1232    // build extended pointer on fd_array lock and max
     1233    xptr_t fd_lock_xp = XPTR( process_cxy , &process_ptr->fd_array.lock );
     1234    xptr_t fd_max_xp  = XPTR( process_cxy , &process_ptr->fd_array.max );
     1235
     1236    // take lock protecting fd_array
    12101237        remote_queuelock_acquire( fd_lock_xp );
    12111238
    12121239    // take the lock protecting the list of copies
    12131240    remote_queuelock_acquire( copies_lock_xp );
     1241
     1242    // get max value
     1243    uint32_t max = hal_remote_l32( fd_max_xp );
    12141244
    12151245    // loop on list of process copies
     
    12231253        // release the fd_array entry in process copy
    12241254        hal_remote_s64( XPTR( copy_cxy , &copy_ptr->fd_array.array[fdid] ), XPTR_NULL );
    1225     }
     1255
     1256        // decrease file refcount
     1257        vfs_file_count_down( file_xp );
     1258    }
     1259
     1260    // update max when required
     1261    if( fdid == max ) hal_remote_s32( fd_max_xp , max-1 );
    12261262
    12271263    // release the lock protecting reference fd_array
     
    12401276}  // end process_fd_remove()
    12411277
    1242 ////////////////////////////////////////////////
    1243 xptr_t process_fd_get_xptr( process_t * process,
    1244                             uint32_t    fdid )
     1278//////////////////////////////////////////////
     1279void process_fd_clean_all( xptr_t process_xp )
     1280{
     1281    uint32_t  id;
     1282    xptr_t    file_xp;         // one fd_array entry
     1283    xptr_t    lock_xp;         // extendad pointer on lock protecting fd_array
     1284    uint32_t  max;             // number of registered files
     1285    error_t   error;
     1286
     1287    // get process cluster, local pointer and PID
     1288    process_t * process_ptr = GET_PTR( process_xp );
     1289    cxy_t       process_cxy = GET_CXY( process_xp );
     1290    pid_t       pid         = hal_remote_l32( XPTR( process_cxy , &process_ptr->pid) );
     1291
     1292// check target process is owner process
     1293assert( (process_xp == hal_remote_l64( XPTR( process_cxy , &process_ptr->owner_xp )) ),
     1294"process must be owner process\n" );
     1295
     1296#if DEBUG_PROCESS_FD_CLEAN_ALL
     1297thread_t * this  = CURRENT_THREAD;
     1298uint32_t   cycle = (uint32_t)hal_get_cycles();
     1299if( DEBUG_PROCESS_FD_CLEAN_ALL < cycle )
     1300printk("\n[%s] thread[%x,%x] enter for process %x / cycle %d\n",
     1301__FUNCTION__, this->process->pid, this->trdid, pid, cycle );
     1302
     1303process_fd_display( process_xp );
     1304#endif
     1305
     1306    // build extended pointer on lock protecting the fd_array
     1307    lock_xp = XPTR( process_cxy , &process_ptr->fd_array.lock );
     1308
     1309    // get max index for fd_array
     1310    max = hal_remote_l32( XPTR( process_cxy , &process_ptr->fd_array.max ));
     1311
     1312    // take lock protecting fd_array
     1313        remote_queuelock_acquire( lock_xp );
     1314
     1315    for ( id = 0; id <= max ; id++ )
     1316    {
     1317        // get fd_array entry
     1318        file_xp = hal_remote_l64( XPTR( process_cxy , &process_ptr->fd_array.array[id] ) );
     1319       
     1320        if ( file_xp != XPTR_NULL )
     1321        {
     1322            // close the file or socket
     1323            error = sys_close( id );
     1324
     1325            if( error )
     1326            printk("/n[ERROR] in %s : cannot close the file %d for process %x\n",
     1327            __FUNCTION__, id, pid );
     1328        }
     1329    }
     1330
     1331    // release lock protecting fd_array
     1332        remote_queuelock_release( lock_xp );
     1333
     1334#if DEBUG_PROCESS_FD_CLEAN_ALL
     1335cycle = (uint32_t)hal_get_cycles();
     1336if( DEBUG_PROCESS_FD_CLEAN_ALL < cycle )
     1337printk("\n[%s] thread[%x,%x] exit for process %x / cycle %d\n",
     1338__FUNCTION__, this->process->pid, this->trdid, pid, cycle );
     1339#endif
     1340
     1341}  // end process_fd_clean_all()
     1342
     1343//////////////////////////////////////////////////////////////
     1344xptr_t process_fd_get_xptr_from_owner( xptr_t      process_xp,
     1345                                       uint32_t    fdid )
     1346{
     1347    cxy_t       process_cxy = GET_CXY( process_xp );
     1348    process_t * process_ptr = GET_PTR( process_xp );
     1349
     1350assert( (hal_remote_l64( XPTR( process_cxy , &process_ptr->owner_xp )) == process_xp),
     1351"process_xp argument must be the owner process" );
     1352
     1353    // access owner process fd_array
     1354    return hal_remote_l64( XPTR( process_cxy , &process_ptr->fd_array.array[fdid] ));
     1355
     1356}  // end process_fd_get_xptr_from_owner()
     1357
     1358///////////////////////////////////////////////////////////
     1359xptr_t process_fd_get_xptr_from_local( process_t * process,
     1360                                       uint32_t    fdid )
    12451361{
    12461362    xptr_t  file_xp;
     
    12521368    if( file_xp == XPTR_NULL )
    12531369    {
    1254         // get reference process cluster and local pointer
    1255         xptr_t      ref_xp  = process->ref_xp;
    1256         cxy_t       ref_cxy = GET_CXY( ref_xp );
    1257         process_t * ref_ptr = GET_PTR( ref_xp );
    1258 
    1259         // build extended pointer on lock protecting reference fd_array
    1260         lock_xp = XPTR( ref_cxy , &ref_ptr->fd_array.lock );
    1261 
    1262         // take lock protecting reference fd_array
     1370        // get owner process cluster and local pointer
     1371        xptr_t      owner_xp  = process->owner_xp;
     1372        cxy_t       owner_cxy = GET_CXY( owner_xp );
     1373        process_t * owner_ptr = GET_PTR( owner_xp );
     1374
     1375        // build extended pointer on lock protecting fd_array
     1376        lock_xp = XPTR( owner_cxy , &owner_ptr->fd_array.lock );
     1377
     1378        // take lock protecting fd_array
    12631379            remote_queuelock_acquire( lock_xp );
    12641380
    12651381        // access reference process descriptor
    1266         file_xp = hal_remote_l64( XPTR( ref_cxy , &ref_ptr->fd_array.array[fdid] ) );
    1267 
    1268         // update local fd_array if found
    1269         if( file_xp != XPTR_NULL )  process->fd_array.array[fdid] = file_xp;
     1382        file_xp = hal_remote_l64( XPTR( owner_cxy , &owner_ptr->fd_array.array[fdid] ) );
     1383
     1384        if( file_xp != XPTR_NULL ) 
     1385        {
     1386           // update local fd_array
     1387            process->fd_array.array[fdid] = file_xp;
    12701388       
    1271         // release lock protecting reference fd_array
     1389            // increase file refcount
     1390            vfs_file_count_up( file_xp );
     1391        }
     1392
     1393        // release lock protecting fd_array
    12721394            remote_queuelock_release( lock_xp );
    12731395    }
     
    12751397    return file_xp;
    12761398
    1277 }  // end process_fd_get_xptr()
     1399}  // end process_fd_get_xptr_from_local()
    12781400
    12791401///////////////////////////////////////////
     
    13191441bool_t process_fd_array_full( void )
    13201442{
    1321     // get extended pointer on reference process
    1322     xptr_t ref_xp = CURRENT_THREAD->process->ref_xp;
    1323 
    1324     // get reference process cluster and local pointer
    1325     process_t * ref_ptr = GET_PTR( ref_xp );
    1326     cxy_t       ref_cxy = GET_CXY( ref_xp );
    1327 
    1328     // get number of open file descriptors from reference fd_array
    1329     uint32_t current = hal_remote_l32( XPTR( ref_cxy , &ref_ptr->fd_array.current ) );
    1330 
    1331         return ( current >= CONFIG_PROCESS_FILE_MAX_NR );
     1443    // get extended pointer on owner process
     1444    xptr_t owner_xp = CURRENT_THREAD->process->owner_xp;
     1445
     1446    // get owner process cluster and local pointer
     1447    process_t * owner_ptr = GET_PTR( owner_xp );
     1448    cxy_t       owner_cxy = GET_CXY( owner_xp );
     1449
     1450    // get number of open file descriptors from fd_array
     1451    uint32_t max = hal_remote_l32( XPTR( owner_cxy , &owner_ptr->fd_array.max ));
     1452
     1453        return ( max == CONFIG_PROCESS_FILE_MAX_NR - 1 );
    13321454}
    13331455
     1456////////////////////////////////////////////
     1457void process_fd_display( xptr_t process_xp )
     1458{
     1459    uint32_t      fdid;
     1460    xptr_t        file_xp;
     1461    vfs_file_t *  file_ptr;
     1462    cxy_t         file_cxy;
     1463    uint32_t      file_type;
     1464    xptr_t        inode_xp;
     1465    vfs_inode_t * inode_ptr;
     1466
     1467    char          name[CONFIG_VFS_MAX_NAME_LENGTH];
     1468
     1469    // get process cluster and local pointer
     1470    process_t * process_ptr = GET_PTR( process_xp );
     1471    cxy_t       process_cxy = GET_CXY( process_xp );
     1472
     1473    // get process PID
     1474    pid_t  pid = hal_remote_l32( XPTR( process_cxy , &process_ptr->pid ));
     1475
     1476    // get pointers on owner process descriptor
     1477    xptr_t      owner_xp  = hal_remote_l64( XPTR( process_cxy , &process_ptr->owner_xp ));
     1478    process_t * owner_ptr = GET_PTR( owner_xp );
     1479    cxy_t       owner_cxy = GET_CXY( owner_xp );
     1480
     1481    // get max fdid from owner process descriptor
     1482    uint32_t max = hal_remote_l32( XPTR( owner_cxy , &owner_ptr->fd_array.max ));
     1483
     1484    printk("\n***** fd_array for pid %x in cluster %x / max %d *****\n",
     1485    pid, process_cxy, max );
     1486
     1487    for( fdid = 0 ; fdid <= max ; fdid++ )
     1488    {
     1489        // get pointers on file descriptor
     1490        file_xp  = hal_remote_l64( XPTR( process_cxy , &process_ptr->fd_array.array[fdid] ));
     1491        file_ptr = GET_PTR( file_xp );
     1492        file_cxy = GET_CXY( file_xp );
     1493
     1494        if( file_xp != XPTR_NULL )
     1495        {
     1496            // get file type
     1497            file_type = hal_remote_l32( XPTR( file_cxy , &file_ptr->type ));
     1498
     1499            // get file name for a true file
     1500            if( file_type == INODE_TYPE_FILE )
     1501            {
     1502                // get inode pointers
     1503                inode_ptr = hal_remote_lpt( XPTR( file_cxy , &file_ptr->inode ));
     1504                inode_xp  = XPTR( file_cxy , inode_ptr );
     1505
     1506                // get file name
     1507                vfs_inode_get_name( inode_xp , name );
     1508
     1509                // display relevant file decriptor info
     1510                printk(" - %d : type %s (%s)\n",
     1511                fdid , process_fd_type_str(file_type), name );
     1512            }
     1513            else
     1514            {
     1515                // display relevant file decriptor info
     1516                printk(" - %d : type %s\n",
     1517                fdid , process_fd_type_str(file_type) );
     1518            }
     1519        }
     1520        else
     1521        {
     1522            // display relevant file decriptor info
     1523            printk(" - %d : empty slot\n",
     1524            fdid );
     1525        }
     1526    }
     1527}   // end process_fd_display()
    13341528
    13351529////////////////////////////////////////////////////////////////////////////////////
Note: See TracChangeset for help on using the changeset viewer.