Ignore:
Timestamp:
Jan 3, 2008, 4:36:20 PM (14 years ago)
Author:
charles
Message:

#378 - use {get,set}rlimit to deal with open file limits

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/fdlimit.c

    r4404 r4438  
    3232#include <sys/types.h>
    3333#include <sys/stat.h>
     34#include <sys/time.h> /* getrlimit */
     35#include <sys/resource.h> /* getrlimit */
    3436#include <unistd.h>
    3537#include <libgen.h> /* basename, dirname */
     
    8991    TR_MAX_OPEN_FILES = 16, /* real files, not sockets */
    9092
    91     TR_RESERVED_FDS = 16 /* sockets reserved for tracker connections */
     93    NOFILE_BUFFER = 512, /* the process' number of open files is
     94                            globalMaxPeers + NOFILE_BUFFER */
    9295};
    9396
     
    360363    tr_lockLock( gFd->lock );
    361364
    362     if( isReserved && gFd->reserved >= TR_RESERVED_FDS )
    363         isReserved = FALSE;
    364 
    365365    if( isReserved || ( gFd->normal < gFd->normalMax ) )
    366366        if( ( s = socket( AF_INET, type, 0 ) ) < 0 )
     
    450450tr_fdInit( int globalPeerLimit )
    451451{
    452     int i, j, *s;
     452    int i;
     453    struct rlimit rlim;
    453454
    454455    assert( gFd == NULL );
    455 
    456456    gFd = tr_new0( struct tr_fd_s, 1 );
    457457    gFd->lock = tr_lockNew( );
    458458
    459     s = tr_new( int, globalPeerLimit );
    460 
    461     /* count the max number of sockets we can use */
    462     for( i=0; i<globalPeerLimit; ++i )
    463         if( ( s[i] = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
    464             break;
    465     for( j=0; j<i; ++j )
    466         socketClose( s[j] );
    467     tr_dbg( "%d usable file descriptors", i );
    468 
    469     /* set some fds aside for the UI or daemon to use */
    470     gFd->normalMax = i - TR_RESERVED_FDS - 10;
    471 
    472     for( i=0; i<TR_MAX_OPEN_FILES; ++i )
     459    getrlimit( RLIMIT_NOFILE, &rlim );
     460    rlim.rlim_cur = MIN( rlim.rlim_max, (rlim_t)(globalPeerLimit + NOFILE_BUFFER) );
     461    setrlimit( RLIMIT_NOFILE, &rlim );
     462    gFd->normalMax = rlim.rlim_cur - NOFILE_BUFFER;
     463    tr_dbg( "%d usable file descriptors", globalPeerLimit );
     464
     465    for( i=0; i<TR_MAX_OPEN_FILES; ++i )
    473466        gFd->open[i].fd = -1;
    474 
    475     tr_free( s );
    476467}
    477468
Note: See TracChangeset for help on using the changeset viewer.