source: trunk/libtransmission/fdlimit.c @ 9495

Last change on this file since 9495 was 9495, checked in by charles, 12 years ago

(trunk libT) #2551 "better buffer cache management": apply jch's prefetch-base.patch

  • Property svn:keywords set to Date Rev Author Id
File size: 20.5 KB
Line 
1/******************************************************************************
2 * $Id: fdlimit.c 9495 2009-11-08 23:43:38Z charles $
3 *
4 * Copyright (c) 2005-2008 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#ifndef WIN32
26 #define HAVE_GETRLIMIT
27#endif
28
29#ifdef HAVE_POSIX_FADVISE
30 #ifdef _XOPEN_SOURCE
31  #undef _XOPEN_SOURCE
32 #endif
33 #define _XOPEN_SOURCE 600
34#endif
35
36#include <assert.h>
37#include <errno.h>
38#include <inttypes.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#ifdef SYS_DARWIN
43 #include <fcntl.h>
44#endif
45
46#ifdef HAVE_XFS_XFS_H
47 #include <xfs/xfs.h>
48#endif
49
50#include <sys/types.h>
51#include <sys/stat.h>
52#ifdef HAVE_GETRLIMIT
53 #include <sys/time.h> /* getrlimit */
54 #include <sys/resource.h> /* getrlimit */
55#endif
56#include <unistd.h>
57#include <fcntl.h> /* O_LARGEFILE posix_fadvise */
58
59#include <evutil.h>
60
61#include "transmission.h"
62#include "fdlimit.h"
63#include "list.h"
64#include "net.h"
65#include "platform.h" /* MAX_PATH_LENGTH, TR_PATH_DELIMITER */
66#include "session.h"
67#include "torrent.h" /* tr_isTorrent() */
68#include "utils.h"
69
70#define dbgmsg( ... ) \
71    do { \
72        if( tr_deepLoggingIsActive( ) ) \
73            tr_deepLog( __FILE__, __LINE__, NULL, __VA_ARGS__ ); \
74    } while( 0 )
75
76/**
77***
78**/
79
80enum
81{
82    NOFILE_BUFFER = 512, /* the process' number of open files is
83                            globalMaxPeers + NOFILE_BUFFER */
84};
85
86struct tr_openfile
87{
88    tr_bool          isWritable;
89    int              torrentId;
90    tr_file_index_t  fileNum;
91    char             filename[MAX_PATH_LENGTH];
92    int              fd;
93    uint64_t         date;
94};
95
96struct tr_fdInfo
97{
98    int                   socketCount;
99    int                   socketLimit;
100    int                   publicSocketLimit;
101    int                   openFileLimit;
102    struct tr_openfile  * openFiles;
103};
104
105/***
106****
107****  Local Files
108****
109***/
110
111#ifndef O_LARGEFILE
112 #define O_LARGEFILE 0
113#endif
114
115static tr_bool
116preallocateFileSparse( int fd, uint64_t length )
117{
118    const char zero = '\0';
119
120    if( length == 0 )
121        return TRUE;
122
123    if( lseek( fd, length-1, SEEK_SET ) == -1 )
124        return FALSE;
125    if( write( fd, &zero, 1 ) == -1 )
126        return FALSE;
127    if( ftruncate( fd, length ) == -1 )
128        return FALSE;
129
130    return TRUE;
131}
132
133static tr_bool
134preallocateFileFull( const char * filename, uint64_t length )
135{
136    tr_bool success = 0;
137
138#ifdef WIN32
139
140    HANDLE hFile = CreateFile( filename, GENERIC_WRITE, 0, 0, CREATE_NEW, 0, 0 );
141    if( hFile != INVALID_HANDLE_VALUE )
142    {
143        LARGE_INTEGER li;
144        li.QuadPart = length;
145        success = SetFilePointerEx( hFile, li, NULL, FILE_BEGIN ) && SetEndOfFile( hFile );
146        CloseHandle( hFile );
147    }
148
149#else
150
151    int flags = O_RDWR | O_CREAT | O_LARGEFILE;
152    int fd = open( filename, flags, 0666 );
153    if( fd >= 0 )
154    {
155# ifdef HAVE_XFS_XFS_H
156        if( !success && platform_test_xfs_fd( fd ) )
157        {
158            xfs_flock64_t fl;
159            fl.l_whence = 0;
160            fl.l_start = 0;
161            fl.l_len = length;
162            success = !xfsctl( NULL, fd, XFS_IOC_RESVSP64, &fl );
163        }
164# endif
165# ifdef SYS_DARWIN
166        if( !success )
167        {
168            fstore_t fst;
169            fst.fst_flags = F_ALLOCATECONTIG;
170            fst.fst_posmode = F_PEOFPOSMODE;
171            fst.fst_offset = 0;
172            fst.fst_length = length;
173            fst.fst_bytesalloc = 0;
174            success = !fcntl( fd, F_PREALLOCATE, &fst );
175        }
176# endif
177# ifdef HAVE_POSIX_FALLOCATE
178        if( !success )
179        {
180            success = !posix_fallocate( fd, 0, length );
181        }
182# endif
183
184        if( !success ) /* if nothing else works, do it the old-fashioned way */
185        {
186            uint8_t buf[ 4096 ];
187            memset( buf, 0, sizeof( buf ) );
188            success = TRUE;
189            while ( success && ( length > 0 ) )
190            {
191                const int thisPass = MIN( length, sizeof( buf ) );
192                success = write( fd, buf, thisPass ) == thisPass;
193                length -= thisPass;
194            }
195        }
196
197        close( fd );
198    }
199
200#endif
201
202    return success;
203}
204
205tr_bool
206tr_preallocate_file( const char * filename, uint64_t length )
207{
208    return preallocateFileFull( filename, length );
209}
210
211/* Like pread and pwrite, except that the position is undefined afterwards.
212   And of course they are not thread-safe. */
213
214#ifdef SYS_DARWIN
215 #define HAVE_PREAD
216 #define HAVE_PWRITE
217#endif
218
219ssize_t
220tr_pread( int fd, void *buf, size_t count, off_t offset )
221{
222#ifdef HAVE_PREAD
223    return pread( fd, buf, count, offset );
224#else
225    const off_t lrc = lseek( fd, offset, SEEK_SET );
226    if( lrc < 0 )
227        return -1;
228    return read( fd, buf, count );
229#endif
230}
231
232ssize_t
233tr_pwrite( int fd, void *buf, size_t count, off_t offset )
234{
235#ifdef HAVE_PWRITE
236    return pwrite( fd, buf, count, offset );
237#else
238    const off_t lrc = lseek( fd, offset, SEEK_SET );
239    if( lrc < 0 )
240        return -1;
241    return write( fd, buf, count );
242#endif
243}
244
245int
246tr_prefetch( int fd, off_t offset, size_t count )
247{
248#ifdef HAVE_POSIX_FADVISE
249    return posix_fadvise( fd, offset, count, POSIX_FADV_WILLNEED );
250#elif defined(SYS_DARWIN)
251    int val;
252    struct radvisory radv;
253    radv.ra_offset = offset;
254    radv.ra_count = count;
255    return fcntl( fd, F_RDADVISE, &radv );
256#else
257    return 0;
258#endif
259}
260
261int
262tr_open_file_for_writing( const char * filename )
263{
264    int flags = O_WRONLY | O_CREAT;
265#ifdef O_BINARY
266    flags |= O_BINARY;
267#endif
268#ifdef O_LARGEFILE
269    flags |= O_LARGEFILE;
270#endif
271    return open( filename, flags, 0666 );
272}
273
274int
275tr_open_file_for_scanning( const char * filename )
276{
277    int fd;
278    int flags;
279
280    /* build the flags */
281    flags = O_RDONLY;
282#ifdef O_SEQUENTIAL
283    flags |= O_SEQUENTIAL;
284#endif
285#ifdef O_BINARY
286    flags |= O_BINARY;
287#endif
288#ifdef O_LARGEFILE
289    flags |= O_LARGEFILE;
290#endif
291
292    /* open the file */
293    fd = open( filename, flags, 0666 );
294    if( fd >= 0 )
295    {
296        /* Set hints about the lookahead buffer and caching. It's okay
297           for these to fail silently, so don't let them affect errno */
298        const int err = errno;
299#ifdef HAVE_POSIX_FADVISE
300        posix_fadvise( fd, 0, 0, POSIX_FADV_SEQUENTIAL );
301#endif
302#ifdef SYS_DARWIN
303        fcntl( fd, F_NOCACHE, 1 );
304        fcntl( fd, F_RDAHEAD, 1 );
305#endif
306        errno = err;
307    }
308
309    return fd;
310}
311
312void
313tr_close_file( int fd )
314{
315#if defined(HAVE_POSIX_FADVISE)
316    /* Set hint about not caching this file.
317       It's okay for this to fail silently, so don't let it affect errno */
318    const int err = errno;
319    posix_fadvise( fd, 0, 0, POSIX_FADV_DONTNEED );
320    errno = err;
321#endif
322    close( fd );
323}
324
325/**
326 * returns 0 on success, or an errno value on failure.
327 * errno values include ENOENT if the parent folder doesn't exist,
328 * plus the errno values set by tr_mkdirp() and open().
329 */
330static int
331TrOpenFile( tr_session             * session,
332            int                      i,
333            const char             * filename,
334            tr_bool                  doWrite,
335            tr_preallocation_mode    preallocationMode,
336            uint64_t                 desiredFileSize )
337{
338    int flags;
339    struct stat sb;
340    tr_bool alreadyExisted;
341    struct tr_openfile * file;
342
343    assert( tr_isSession( session ) );
344    assert( session->fdInfo != NULL );
345
346    file = &session->fdInfo->openFiles[i];
347
348    /* create subfolders, if any */
349    if( doWrite )
350    {
351        char * dir = tr_dirname( filename );
352        const int err = tr_mkdirp( dir, 0777 ) ? errno : 0;
353        if( err ) {
354            tr_err( _( "Couldn't create \"%1$s\": %2$s" ), dir, tr_strerror( err ) );
355            tr_free( dir );
356            return err;
357        }
358        tr_free( dir );
359    }
360
361    alreadyExisted = !stat( filename, &sb ) && S_ISREG( sb.st_mode );
362
363    if( doWrite && !alreadyExisted && ( preallocationMode == TR_PREALLOCATE_FULL ) )
364        if( preallocateFileFull( filename, desiredFileSize ) )
365            tr_inf( _( "Preallocated file \"%s\"" ), filename );
366
367    /* open the file */
368    flags = doWrite ? ( O_RDWR | O_CREAT ) : O_RDONLY;
369#ifdef O_SEQUENTIAL
370    flags |= O_SEQUENTIAL;
371#endif
372#ifdef O_LARGEFILE
373    flags |= O_LARGEFILE;
374#endif
375#ifdef WIN32
376    flags |= O_BINARY;
377#endif
378    file->fd = open( filename, flags, 0666 );
379    if( file->fd == -1 )
380    {
381        const int err = errno;
382        tr_err( _( "Couldn't open \"%1$s\": %2$s" ), filename, tr_strerror( err ) );
383        return err;
384    }
385
386    /* If the file already exists and it's too large, truncate it.
387     * This is a fringe case that happens if a torrent's been updated
388     * and one of the updated torrent's files is smaller.
389     * http://trac.transmissionbt.com/ticket/2228
390     * https://bugs.launchpad.net/ubuntu/+source/transmission/+bug/318249
391     */
392    if( alreadyExisted && ( desiredFileSize < (uint64_t)sb.st_size ) )
393        ftruncate( file->fd, desiredFileSize );
394
395    if( doWrite && !alreadyExisted && ( preallocationMode == TR_PREALLOCATE_SPARSE ) )
396        preallocateFileSparse( file->fd, desiredFileSize );
397
398#ifdef HAVE_POSIX_FADVISE
399    /* this doubles the OS level readahead buffer, which in practice
400     * turns out to be a good thing, because many (most?) clients request
401     * chunks of blocks in order.
402     * It's okay for this to fail silently, so don't let it affect errno */
403    {
404        const int err = errno;
405        posix_fadvise( file->fd, 0, 0, POSIX_FADV_SEQUENTIAL );
406        errno = err;
407    }
408#endif
409
410    return 0;
411}
412
413static TR_INLINE tr_bool
414fileIsOpen( const struct tr_openfile * o )
415{
416    return o->fd >= 0;
417}
418
419static void
420TrCloseFile( struct tr_openfile * o )
421{
422    assert( o != NULL );
423    assert( fileIsOpen( o ) );
424
425    tr_close_file( o->fd );
426    o->fd = -1;
427}
428
429int
430tr_fdFileGetCached( tr_session       * session,
431                    int                torrentId,
432                    tr_file_index_t    fileNum,
433                    tr_bool            doWrite )
434{
435    struct tr_openfile * match = NULL;
436    struct tr_fdInfo * gFd;
437
438    assert( tr_isSession( session ) );
439    assert( session->fdInfo != NULL );
440    assert( torrentId > 0 );
441    assert( tr_isBool( doWrite ) );
442
443    gFd = session->fdInfo;
444
445    /* is it already open? */
446    {
447        int i;
448        struct tr_openfile * o;
449        for( i=0; i<gFd->openFileLimit; ++i )
450        {
451            o = &gFd->openFiles[i];
452
453            if( torrentId != o->torrentId )
454                continue;
455            if( fileNum != o->fileNum )
456                continue;
457            if( !fileIsOpen( o ) )
458                continue;
459
460            match = o;
461            break;
462        }
463    }
464
465    if( ( match != NULL ) && ( !doWrite || match->isWritable ) )
466    {
467        match->date = tr_date( );
468        return match->fd;
469    }
470
471    return -1;
472}
473
474/* returns an fd on success, or a -1 on failure and sets errno */
475int
476tr_fdFileCheckout( tr_session             * session,
477                   int                      torrentId,
478                   tr_file_index_t          fileNum,
479                   const char             * filename,
480                   tr_bool                  doWrite,
481                   tr_preallocation_mode    preallocationMode,
482                   uint64_t                 desiredFileSize )
483{
484    int i, winner = -1;
485    struct tr_fdInfo * gFd;
486    struct tr_openfile * o;
487
488    assert( tr_isSession( session ) );
489    assert( session->fdInfo != NULL );
490    assert( torrentId > 0 );
491    assert( filename && *filename );
492    assert( tr_isBool( doWrite ) );
493
494    gFd = session->fdInfo;
495
496    dbgmsg( "looking for file '%s', writable %c", filename, doWrite ? 'y' : 'n' );
497
498    /* is it already open? */
499    for( i=0; i<gFd->openFileLimit; ++i )
500    {
501        o = &gFd->openFiles[i];
502
503        if( torrentId != o->torrentId )
504            continue;
505        if( fileNum != o->fileNum )
506            continue;
507        if( !fileIsOpen( o ) )
508            continue;
509
510        if( doWrite && !o->isWritable )
511        {
512            dbgmsg( "found it!  it's open and available, but isn't writable. closing..." );
513            TrCloseFile( o );
514            break;
515        }
516
517        dbgmsg( "found it!  it's ready for use!" );
518        winner = i;
519        break;
520    }
521
522    dbgmsg( "it's not already open.  looking for an open slot or an old file." );
523    while( winner < 0 )
524    {
525        uint64_t date = tr_date( ) + 1;
526
527        /* look for the file that's been open longest */
528        for( i=0; i<gFd->openFileLimit; ++i )
529        {
530            o = &gFd->openFiles[i];
531
532            if( !fileIsOpen( o ) )
533            {
534                winner = i;
535                dbgmsg( "found an empty slot in %d", winner );
536                break;
537            }
538
539            if( date > o->date )
540            {
541                date = o->date;
542                winner = i;
543            }
544        }
545
546        assert( winner >= 0 );
547
548        if( fileIsOpen( &gFd->openFiles[winner] ) )
549        {
550            dbgmsg( "closing file \"%s\"", gFd->openFiles[winner].filename );
551            TrCloseFile( &gFd->openFiles[winner] );
552        }
553    }
554
555    assert( winner >= 0 );
556    o = &gFd->openFiles[winner];
557    if( !fileIsOpen( o ) )
558    {
559        const int err = TrOpenFile( session, winner, filename, doWrite,
560                                    preallocationMode, desiredFileSize );
561        if( err ) {
562            errno = err;
563            return -1;
564        }
565
566        dbgmsg( "opened '%s' in slot %d, doWrite %c", filename, winner,
567                doWrite ? 'y' : 'n' );
568        tr_strlcpy( o->filename, filename, sizeof( o->filename ) );
569        o->isWritable = doWrite;
570    }
571
572    dbgmsg( "checking out '%s' in slot %d", filename, winner );
573    o->torrentId = torrentId;
574    o->fileNum = fileNum;
575    o->date = tr_date( );
576    return o->fd;
577}
578
579void
580tr_fdFileClose( tr_session        * session,
581                const tr_torrent  * tor,
582                tr_file_index_t     fileNum )
583{
584    struct tr_openfile * o;
585    struct tr_fdInfo * gFd;
586    const struct tr_openfile * end;
587    const int torrentId = tr_torrentId( tor );
588
589    assert( tr_isSession( session ) );
590    assert( session->fdInfo != NULL );
591    assert( tr_isTorrent( tor ) );
592    assert( fileNum < tor->info.fileCount );
593
594    gFd = session->fdInfo;
595
596    for( o=gFd->openFiles, end=o+gFd->openFileLimit; o!=end; ++o )
597    {
598        if( torrentId != o->torrentId )
599            continue;
600        if( fileNum != o->fileNum )
601            continue;
602        if( !fileIsOpen( o ) )
603            continue;
604
605        dbgmsg( "tr_fdFileClose closing \"%s\"", o->filename );
606        TrCloseFile( o );
607    }
608}
609
610void
611tr_fdTorrentClose( tr_session * session, int torrentId )
612{
613    struct tr_openfile * o;
614    struct tr_fdInfo * gFd;
615    const struct tr_openfile * end;
616
617    assert( tr_isSession( session ) );
618    assert( session->fdInfo != NULL );
619
620    gFd = session->fdInfo;
621
622    for( o=gFd->openFiles, end=o+gFd->openFileLimit; o!=end; ++o )
623        if( fileIsOpen( o ) && ( o->torrentId == torrentId ) )
624            TrCloseFile( o );
625}
626
627/***
628****
629****  Sockets
630****
631***/
632
633int
634tr_fdSocketCreate( tr_session * session, int domain, int type )
635{
636    int s = -1;
637    struct tr_fdInfo * gFd;
638
639    assert( tr_isSession( session ) );
640    assert( session->fdInfo != NULL );
641
642    gFd = session->fdInfo;
643
644    if( gFd->socketCount < gFd->socketLimit )
645        if( ( s = socket( domain, type, 0 ) ) < 0 )
646        {
647            if( sockerrno != EAFNOSUPPORT )
648                tr_err( _( "Couldn't create socket: %s" ),
649                        tr_strerror( sockerrno ) );
650        }
651
652    if( s > -1 )
653        ++gFd->socketCount;
654
655    assert( gFd->socketCount >= 0 );
656    return s;
657}
658
659int
660tr_fdSocketAccept( tr_session  * session,
661                   int           b,
662                   tr_address  * addr,
663                   tr_port     * port )
664{
665    int s;
666    unsigned int len;
667    struct tr_fdInfo * gFd;
668    struct sockaddr_storage sock;
669
670    assert( tr_isSession( session ) );
671    assert( session->fdInfo != NULL );
672    assert( addr );
673    assert( port );
674
675    gFd = session->fdInfo;
676
677    len = sizeof( struct sockaddr_storage );
678    s = accept( b, (struct sockaddr *) &sock, &len );
679
680    if( ( s >= 0 ) && gFd->socketCount > gFd->socketLimit )
681    {
682        tr_netCloseSocket( s );
683        s = -1;
684    }
685
686    if( s >= 0 )
687    {
688        /* "The ss_family field of the sockaddr_storage structure will always
689         * align with the family field of any protocol-specific structure." */
690        if( sock.ss_family == AF_INET )
691        {
692            struct sockaddr_in *si;
693            union { struct sockaddr_storage dummy; struct sockaddr_in si; } s;
694            s.dummy = sock;
695            si = &s.si;
696            addr->type = TR_AF_INET;
697            addr->addr.addr4.s_addr = si->sin_addr.s_addr;
698            *port = si->sin_port;
699        }
700        else
701        {
702            struct sockaddr_in6 *si;
703            union { struct sockaddr_storage dummy; struct sockaddr_in6 si; } s;
704            s.dummy = sock;
705            si = &s.si;
706            addr->type = TR_AF_INET6;
707            addr->addr.addr6 = si->sin6_addr;
708            *port = si->sin6_port;
709        }
710        ++gFd->socketCount;
711    }
712
713    return s;
714}
715
716void
717tr_fdSocketClose( tr_session * session, int fd )
718{
719    struct tr_fdInfo * gFd;
720
721    assert( tr_isSession( session ) );
722    assert( session->fdInfo != NULL );
723
724    gFd = session->fdInfo;
725
726    if( fd >= 0 )
727    {
728        tr_netCloseSocket( fd );
729        --gFd->socketCount;
730    }
731
732    assert( gFd->socketCount >= 0 );
733}
734
735/***
736****
737****  Startup / Shutdown
738****
739***/
740
741static void
742ensureSessionFdInfoExists( tr_session * session )
743{
744    assert( tr_isSession( session ) );
745
746    if( session->fdInfo == NULL )
747        session->fdInfo = tr_new0( struct tr_fdInfo, 1 );
748}
749
750void
751tr_fdClose( tr_session * session )
752{
753    struct tr_fdInfo * gFd;
754    struct tr_openfile * o;
755    const struct tr_openfile * end;
756
757    assert( tr_isSession( session ) );
758    assert( session->fdInfo != NULL );
759
760    gFd = session->fdInfo;
761
762    for( o=gFd->openFiles, end=o+gFd->openFileLimit; o!=end; ++o )
763        if( fileIsOpen( o ) )
764            TrCloseFile( o );
765
766    tr_free( gFd->openFiles );
767    tr_free( gFd );
768    session->fdInfo = NULL;
769}
770
771/***
772****
773***/
774
775void
776tr_fdSetFileLimit( tr_session * session, int limit )
777{
778    struct tr_fdInfo * gFd;
779
780    ensureSessionFdInfoExists( session );
781
782    gFd = session->fdInfo;
783
784    if( gFd->openFileLimit != limit )
785    {
786        int i;
787        struct tr_openfile * o;
788        const struct tr_openfile * end;
789
790        /* close any files we've got open  */
791        for( o=gFd->openFiles, end=o+gFd->openFileLimit; o!=end; ++o )
792            if( fileIsOpen( o ) )
793                TrCloseFile( o );
794
795        /* rebuild the openFiles array */
796        tr_free( gFd->openFiles );
797        gFd->openFiles = tr_new0( struct tr_openfile, limit );
798        gFd->openFileLimit = limit;
799        for( i=0; i<gFd->openFileLimit; ++i )
800            gFd->openFiles[i].fd = -1;
801    }
802}
803
804int
805tr_fdGetFileLimit( const tr_session * session )
806{
807    return session && session->fdInfo ? session->fdInfo->openFileLimit : -1;
808}
809
810void
811tr_fdSetPeerLimit( tr_session * session, int limit )
812{
813    struct tr_fdInfo * gFd;
814
815    ensureSessionFdInfoExists( session );
816
817    gFd = session->fdInfo;
818
819#ifdef HAVE_GETRLIMIT
820    {
821        struct rlimit rlim;
822        getrlimit( RLIMIT_NOFILE, &rlim );
823        rlim.rlim_cur = MIN( rlim.rlim_max, (rlim_t)( limit + NOFILE_BUFFER ) );
824        setrlimit( RLIMIT_NOFILE, &rlim );
825        gFd->socketLimit = rlim.rlim_cur - NOFILE_BUFFER;
826        tr_dbg( "setrlimit( RLIMIT_NOFILE, %d )", (int)rlim.rlim_cur );
827    }
828#else
829    gFd->socketLimit = limit;
830#endif
831    gFd->publicSocketLimit = limit;
832
833    tr_dbg( "%d usable file descriptors", limit );
834}
835
836int
837tr_fdGetPeerLimit( const tr_session * session )
838{
839    return session && session->fdInfo ? session->fdInfo->publicSocketLimit : -1;
840}
Note: See TracBrowser for help on using the repository browser.