source: trunk/libtransmission/fdlimit.c @ 8702

Last change on this file since 8702 was 8702, checked in by jhujhiti, 12 years ago

(trunk libT) fix "dereferencing pointer does brea strict-aliasing rules" warning

  • Property svn:keywords set to Date Rev Author Id
File size: 17.1 KB
Line 
1/******************************************************************************
2 * $Id: fdlimit.c 8702 2009-06-15 05:16:36Z jhujhiti $
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" /* tr_lock */
66#include "utils.h"
67
68#define dbgmsg( ... ) \
69    do { \
70        if( tr_deepLoggingIsActive( ) ) \
71            tr_deepLog( __FILE__, __LINE__, NULL, __VA_ARGS__ ); \
72    } while( 0 )
73
74/**
75***
76**/
77
78enum
79{
80    NOFILE_BUFFER = 512, /* the process' number of open files is
81                            globalMaxPeers + NOFILE_BUFFER */
82};
83
84struct tr_openfile
85{
86    tr_bool    isCheckedOut;
87    tr_bool    isWritable;
88    tr_bool    closeWhenDone;
89    char       filename[MAX_PATH_LENGTH];
90    int        fd;
91    uint64_t   date;
92};
93
94struct tr_fd_s
95{
96    int                   socketCount;
97    int                   socketLimit;
98
99    struct tr_openfile  * openFiles;
100    int                   openFileLimit;
101
102    tr_lock             * lock;
103};
104
105static struct tr_fd_s * gFd = NULL;
106
107/***
108****
109****  Local Files
110****
111***/
112
113#ifndef O_LARGEFILE
114 #define O_LARGEFILE 0
115#endif
116
117static tr_bool
118preallocateFileSparse( int fd, uint64_t length )
119{
120    const char zero = '\0';
121
122    if( length == 0 )
123        return TRUE;
124
125    if( lseek( fd, length-1, SEEK_SET ) == -1 )
126        return FALSE;
127    if( write( fd, &zero, 1 ) == -1 )
128        return FALSE;
129    if( ftruncate( fd, length ) == -1 )
130        return FALSE;
131
132    return TRUE;
133}
134
135static tr_bool
136preallocateFileFull( const char * filename, uint64_t length )
137{
138    tr_bool success = 0;
139
140#ifdef WIN32
141
142    HANDLE hFile = CreateFile( filename, GENERIC_WRITE, 0, 0, CREATE_NEW, 0, 0 );
143    if( hFile != INVALID_HANDLE_VALUE )
144    {
145        LARGE_INTEGER li;
146        li.QuadPart = length;
147        success = SetFilePointerEx( hFile, li, NULL, FILE_BEGIN ) && SetEndOfFile( hFile );
148        CloseHandle( hFile );
149    }
150
151#else
152
153    int flags = O_RDWR | O_CREAT | O_LARGEFILE;
154    int fd = open( filename, flags, 0666 );
155    if( fd >= 0 )
156    {
157# ifdef HAVE_XFS_XFS_H
158        if( !success && platform_test_xfs_fd( fd ) )
159        {
160            xfs_flock64_t fl;
161            fl.l_whence = 0;
162            fl.l_start = 0;
163            fl.l_len = length;
164            success = !xfsctl( NULL, fd, XFS_IOC_RESVSP64, &fl );
165        }
166# endif
167# ifdef SYS_DARWIN
168        if( !success )
169        {
170            fstore_t fst;
171            fst.fst_flags = F_ALLOCATECONTIG;
172            fst.fst_posmode = F_PEOFPOSMODE;
173            fst.fst_offset = 0;
174            fst.fst_length = length;
175            fst.fst_bytesalloc = 0;
176            success = !fcntl( fd, F_PREALLOCATE, &fst );
177        }
178# endif
179# ifdef HAVE_POSIX_FALLOCATE
180        if( !success )
181        {
182            success = !posix_fallocate( fd, 0, length );
183        }
184# endif
185
186        close( fd );
187    }
188
189#endif
190
191    return success;
192}
193
194tr_bool
195tr_preallocate_file( const char * filename, uint64_t length )
196{
197    return preallocateFileFull( filename, length );
198}
199
200int
201tr_open_file_for_writing( const char * filename )
202{
203    int flags = O_WRONLY | O_CREAT;
204#ifdef O_BINARY
205    flags |= O_BINARY;
206#endif
207#ifdef O_LARGEFILE
208    flags |= O_LARGEFILE;
209#endif
210    return open( filename, flags, 0666 );
211}
212
213int
214tr_open_file_for_scanning( const char * filename )
215{
216    int fd;
217    int flags;
218
219    /* build the flags */
220    flags = O_RDONLY;
221#ifdef O_SEQUENTIAL
222    flags |= O_SEQUENTIAL;
223#endif
224#ifdef O_BINARY
225    flags |= O_BINARY;
226#endif
227#ifdef O_LARGEFILE
228    flags |= O_LARGEFILE;
229#endif
230
231    /* open the file */
232    fd = open( filename, flags, 0666 );
233    if( fd >= 0 )
234    {
235        /* Set hints about the lookahead buffer and caching. It's okay
236           for these to fail silently, so don't let them affect errno */
237        const int err = errno;
238#ifdef HAVE_POSIX_FADVISE
239        posix_fadvise( fd, 0, 0, POSIX_FADV_SEQUENTIAL );
240#endif
241#ifdef SYS_DARWIN
242        fcntl( fd, F_NOCACHE, 1 );
243        fcntl( fd, F_RDAHEAD, 1 );
244#endif
245        errno = err;
246    }
247
248    return fd;
249}
250
251void
252tr_close_file( int fd )
253{
254#if defined(HAVE_POSIX_FADVISE)
255    /* Set hint about not caching this file.
256       It's okay for this to fail silently, so don't let it affect errno */
257    const int err = errno;
258    posix_fadvise( fd, 0, 0, POSIX_FADV_DONTNEED );
259    errno = err;
260#endif
261    close( fd );
262}
263
264/**
265 * returns 0 on success, or an errno value on failure.
266 * errno values include ENOENT if the parent folder doesn't exist,
267 * plus the errno values set by tr_mkdirp() and open().
268 */
269static int
270TrOpenFile( int                      i,
271            const char             * folder,
272            const char             * torrentFile,
273            tr_bool                  doWrite,
274            tr_preallocation_mode    preallocationMode,
275            uint64_t                 desiredFileSize )
276{
277    struct tr_openfile * file = &gFd->openFiles[i];
278    int                  flags;
279    char               * filename;
280    struct stat          sb;
281    int                  alreadyExisted;
282
283    /* confirm the parent folder exists */
284    if( stat( folder, &sb ) || !S_ISDIR( sb.st_mode ) )
285    {
286        tr_err( _( "Couldn't create \"%1$s\": parent folder \"%2$s\" does not exist" ), torrentFile, folder );
287        return ENOENT;
288    }
289
290    /* create subfolders, if any */
291    filename = tr_buildPath( folder, torrentFile, NULL );
292    if( doWrite )
293    {
294        char * tmp = tr_dirname( filename );
295        const int err = tr_mkdirp( tmp, 0777 ) ? errno : 0;
296        if( err ) {
297            tr_err( _( "Couldn't create \"%1$s\": %2$s" ), tmp, tr_strerror( err ) );
298            tr_free( tmp );
299            tr_free( filename );
300            return err;
301        }
302        tr_free( tmp );
303    }
304
305    alreadyExisted = !stat( filename, &sb ) && S_ISREG( sb.st_mode );
306
307    if( doWrite && !alreadyExisted && ( preallocationMode == TR_PREALLOCATE_FULL ) )
308        if( preallocateFileFull( filename, desiredFileSize ) )
309            tr_inf( _( "Preallocated file \"%s\"" ), filename );
310   
311    /* open the file */
312    flags = doWrite ? ( O_RDWR | O_CREAT ) : O_RDONLY;
313#ifdef O_SEQUENTIAL
314    flags |= O_SEQUENTIAL;
315#endif
316#ifdef O_LARGEFILE
317    flags |= O_LARGEFILE;
318#endif
319#ifdef WIN32
320    flags |= O_BINARY;
321#endif
322    file->fd = open( filename, flags, 0666 );
323    if( file->fd == -1 )
324    {
325        const int err = errno;
326        tr_err( _( "Couldn't open \"%1$s\": %2$s" ), filename, tr_strerror( err ) );
327        tr_free( filename );
328        return err;
329    }
330
331    if( doWrite && !alreadyExisted && ( preallocationMode == TR_PREALLOCATE_SPARSE ) )
332        preallocateFileSparse( file->fd, desiredFileSize );
333
334#ifdef HAVE_POSIX_FADVISE
335    posix_fadvise( file->fd, 0, 0, POSIX_FADV_SEQUENTIAL );
336#endif
337
338    tr_free( filename );
339    return 0;
340}
341
342static int
343fileIsOpen( const struct tr_openfile * o )
344{
345    return o->fd >= 0;
346}
347
348static void
349TrCloseFile( int i )
350{
351    struct tr_openfile * o = &gFd->openFiles[i];
352
353    assert( i >= 0 );
354    assert( i < gFd->openFileLimit );
355    assert( fileIsOpen( o ) );
356
357    tr_close_file( o->fd );
358    o->fd = -1;
359    o->isCheckedOut = 0;
360}
361
362static int
363fileIsCheckedOut( const struct tr_openfile * o )
364{
365    return fileIsOpen( o ) && o->isCheckedOut;
366}
367
368/* returns an fd on success, or a -1 on failure and sets errno */
369int
370tr_fdFileCheckout( const char             * folder,
371                   const char             * torrentFile,
372                   tr_bool                  doWrite,
373                   tr_preallocation_mode    preallocationMode,
374                   uint64_t                 desiredFileSize )
375{
376    int i, winner = -1;
377    struct tr_openfile * o;
378    char filename[MAX_PATH_LENGTH];
379
380    assert( folder && *folder );
381    assert( torrentFile && *torrentFile );
382    assert( doWrite == 0 || doWrite == 1 );
383
384    tr_snprintf( filename, sizeof( filename ), "%s%c%s", folder, TR_PATH_DELIMITER, torrentFile );
385    dbgmsg( "looking for file '%s', writable %c", filename, doWrite ? 'y' : 'n' );
386
387    tr_lockLock( gFd->lock );
388
389    /* Is it already open? */
390    for( i = 0; i < gFd->openFileLimit; ++i )
391    {
392        o = &gFd->openFiles[i];
393
394        if( !fileIsOpen( o ) )
395            continue;
396
397        if( strcmp( filename, o->filename ) )
398            continue;
399
400        if( fileIsCheckedOut( o ) )
401        {
402            dbgmsg( "found it!  it's open, but checked out.  waiting..." );
403            tr_lockUnlock( gFd->lock );
404            tr_wait( 200 );
405            tr_lockLock( gFd->lock );
406            i = -1; /* reloop */
407            continue;
408        }
409
410        if( doWrite && !o->isWritable )
411        {
412            dbgmsg(
413                "found it!  it's open and available, but isn't writable. closing..." );
414            TrCloseFile( i );
415            break;
416        }
417
418        dbgmsg( "found it!  it's ready for use!" );
419        winner = i;
420        break;
421    }
422
423    dbgmsg(
424        "it's not already open.  looking for an open slot or an old file." );
425    while( winner < 0 )
426    {
427        uint64_t date = tr_date( ) + 1;
428
429        /* look for the file that's been open longest */
430        for( i = 0; i < gFd->openFileLimit; ++i )
431        {
432            o = &gFd->openFiles[i];
433
434            if( !fileIsOpen( o ) )
435            {
436                winner = i;
437                dbgmsg( "found an empty slot in %d", winner );
438                break;
439            }
440
441            if( date > o->date )
442            {
443                date = o->date;
444                winner = i;
445            }
446        }
447
448        if( winner >= 0 )
449        {
450            if( fileIsOpen( &gFd->openFiles[winner] ) )
451            {
452                dbgmsg( "closing file '%s', slot #%d",
453                        gFd->openFiles[winner].filename,
454                        winner );
455                TrCloseFile( winner );
456            }
457        }
458        else
459        {
460            dbgmsg( "everything's full!  waiting for someone else to finish something" );
461            tr_lockUnlock( gFd->lock );
462            tr_wait( 200 );
463            tr_lockLock( gFd->lock );
464        }
465    }
466
467    assert( winner >= 0 );
468    o = &gFd->openFiles[winner];
469    if( !fileIsOpen( o ) )
470    {
471        const int err = TrOpenFile( winner, folder, torrentFile, doWrite, preallocationMode, desiredFileSize );
472        if( err ) {
473            tr_lockUnlock( gFd->lock );
474            errno = err;
475            return -1;
476        }
477
478        dbgmsg( "opened '%s' in slot %d, doWrite %c", filename, winner,
479                doWrite ? 'y' : 'n' );
480        tr_strlcpy( o->filename, filename, sizeof( o->filename ) );
481        o->isWritable = doWrite;
482    }
483
484    dbgmsg( "checking out '%s' in slot %d", filename, winner );
485    o->isCheckedOut = 1;
486    o->closeWhenDone = 0;
487    o->date = tr_date( );
488    tr_lockUnlock( gFd->lock );
489    return o->fd;
490}
491
492void
493tr_fdFileReturn( int fd )
494{
495    int i;
496
497    tr_lockLock( gFd->lock );
498
499    for( i = 0; i < gFd->openFileLimit; ++i )
500    {
501        struct tr_openfile * o = &gFd->openFiles[i];
502        if( o->fd != fd )
503            continue;
504
505        dbgmsg( "releasing file '%s' in slot #%d", o->filename, i );
506        o->isCheckedOut = 0;
507        if( o->closeWhenDone )
508            TrCloseFile( i );
509
510        break;
511    }
512
513    tr_lockUnlock( gFd->lock );
514}
515
516void
517tr_fdFileClose( const char * filename )
518{
519    int i;
520
521    tr_lockLock( gFd->lock );
522
523    for( i = 0; i < gFd->openFileLimit; ++i )
524    {
525        struct tr_openfile * o = &gFd->openFiles[i];
526        if( !fileIsOpen( o ) || strcmp( filename, o->filename ) )
527            continue;
528
529        dbgmsg( "tr_fdFileClose closing '%s'", filename );
530
531        if( !o->isCheckedOut )
532        {
533            dbgmsg( "not checked out, so closing it now... '%s'", filename );
534            TrCloseFile( i );
535        }
536        else
537        {
538            dbgmsg(
539                "flagging file '%s', slot #%d to be closed when checked in",
540                gFd->openFiles[i].filename, i );
541            o->closeWhenDone = 1;
542        }
543    }
544
545    tr_lockUnlock( gFd->lock );
546}
547
548/***
549****
550****  Sockets
551****
552***/
553
554static int
555getSocketMax( struct tr_fd_s * gFd )
556{
557    return gFd->socketLimit;
558}
559
560int
561tr_fdSocketCreate( int domain, int type )
562{
563    int s = -1;
564
565    tr_lockLock( gFd->lock );
566
567    if( gFd->socketCount < getSocketMax( gFd ) )
568        if( ( s = socket( domain, type, 0 ) ) < 0 )
569        {
570#ifdef SYS_DARWIN
571            if( sockerrno != EAFNOSUPPORT )
572#endif
573            tr_err( _( "Couldn't create socket: %s" ),
574                   tr_strerror( sockerrno ) );
575            s = -sockerrno;
576        }
577
578    if( s > -1 )
579        ++gFd->socketCount;
580
581    assert( gFd->socketCount >= 0 );
582
583    tr_lockUnlock( gFd->lock );
584    return s;
585}
586
587int
588tr_fdSocketAccept( int           b,
589                   tr_address  * addr,
590                   tr_port     * port )
591{
592    int s;
593    unsigned int len;
594    struct sockaddr_storage sock;
595    tr_lockLock( gFd->lock );
596
597    assert( addr );
598    assert( port );
599
600    len = sizeof( struct sockaddr_storage );
601    s = accept( b, (struct sockaddr *) &sock, &len );
602
603    if( ( s >= 0 ) && gFd->socketCount > getSocketMax( gFd ) )
604    {
605        EVUTIL_CLOSESOCKET( s );
606        s = -1;
607    }
608
609    if( s >= 0 )
610    {
611        /* "The ss_family field of the sockaddr_storage structure will always
612         * align with the family field of any protocol-specific structure." */ 
613        if( sock.ss_family == AF_INET ) 
614        {
615            struct sockaddr_in *si;
616            union { struct sockaddr_storage dummy; struct sockaddr_in si; } s;
617            s.dummy = sock;
618            si = &s.si;
619            addr->type = TR_AF_INET; 
620            addr->addr.addr4.s_addr = si->sin_addr.s_addr; 
621            *port = si->sin_port; 
622        } 
623        else 
624        { 
625            struct sockaddr_in6 *si;
626            union { struct sockaddr_storage dummy; struct sockaddr_in6 si; } s;
627            s.dummy = sock;
628            si = &s.si;
629            addr->type = TR_AF_INET6; 
630            addr->addr.addr6 = si->sin6_addr;
631            *port = si->sin6_port; 
632        } 
633        ++gFd->socketCount;
634    }
635
636    tr_lockUnlock( gFd->lock );
637    return s;
638}
639
640static void
641socketClose( int fd )
642{
643    EVUTIL_CLOSESOCKET( fd );
644}
645
646void
647tr_fdSocketClose( int s )
648{
649    tr_lockLock( gFd->lock );
650
651    if( s >= 0 )
652    {
653        socketClose( s );
654        --gFd->socketCount;
655    }
656
657    assert( gFd->socketCount >= 0 );
658
659    tr_lockUnlock( gFd->lock );
660}
661
662/***
663****
664****  Startup / Shutdown
665****
666***/
667
668void
669tr_fdInit( size_t openFileLimit, size_t socketLimit )
670{
671    int i;
672
673    assert( gFd == NULL );
674    gFd = tr_new0( struct tr_fd_s, 1 );
675    gFd->openFiles = tr_new0( struct tr_openfile, openFileLimit );
676    gFd->openFileLimit = openFileLimit;
677    gFd->lock = tr_lockNew( );
678
679#ifdef HAVE_GETRLIMIT
680    {
681        struct rlimit rlim;
682        getrlimit( RLIMIT_NOFILE, &rlim );
683        rlim.rlim_cur = MIN( rlim.rlim_max,
684                            (rlim_t)( socketLimit + NOFILE_BUFFER ) );
685        setrlimit( RLIMIT_NOFILE, &rlim );
686        gFd->socketLimit = rlim.rlim_cur - NOFILE_BUFFER;
687        tr_dbg( "setrlimit( RLIMIT_NOFILE, %d )", (int)rlim.rlim_cur );
688    }
689#else
690    gFd->socketLimit = socketLimit;
691#endif
692    tr_dbg( "%zu usable file descriptors", socketLimit );
693
694    for( i = 0; i < gFd->openFileLimit; ++i )
695        gFd->openFiles[i].fd = -1;
696}
697
698void
699tr_fdClose( void )
700{
701    int i = 0;
702
703    for( i = 0; i < gFd->openFileLimit; ++i )
704        if( fileIsOpen( &gFd->openFiles[i] ) )
705            TrCloseFile( i );
706
707    tr_lockFree( gFd->lock );
708
709    tr_free( gFd->openFiles );
710    tr_free( gFd );
711    gFd = NULL;
712}
713
714void
715tr_fdSetPeerLimit( uint16_t n )
716{
717    assert( gFd != NULL && "tr_fdInit() must be called first!" );
718    gFd->socketLimit = n;
719}
720
721uint16_t
722tr_fdGetPeerLimit( void )
723{
724    return gFd ? gFd->socketLimit : -1;
725}
726
Note: See TracBrowser for help on using the repository browser.