source: trunk/libtransmission/fdlimit.c @ 7232

Last change on this file since 7232 was 7232, checked in by livings124, 12 years ago

BeOS and Amiga support is, at least for now, a lost cause and hasn't worked for long over a year - removing from the source (if you for whatever unknown reason still need this, get it through SVN)

  • Property svn:keywords set to Date Rev Author Id
File size: 13.9 KB
Line 
1/******************************************************************************
2 * $Id: fdlimit.c 7232 2008-12-02 03:57:01Z livings124 $
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#include <assert.h>
30#include <errno.h>
31#include <inttypes.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#ifdef SYS_DARWIN
36#include <fcntl.h>
37#endif
38
39#ifdef HAVE_FALLOCATE
40 #include <linux/falloc.h>
41#endif
42
43#include <sys/types.h>
44#include <sys/stat.h>
45#ifdef HAVE_GETRLIMIT
46 #include <sys/time.h> /* getrlimit */
47 #include <sys/resource.h> /* getrlimit */
48#endif
49#include <unistd.h>
50#include <fcntl.h> /* O_LARGEFILE */
51
52#include <event.h>
53#include <evutil.h>
54
55#include "transmission.h"
56#include "fdlimit.h"
57#include "list.h"
58#include "net.h"
59#include "platform.h" /* tr_lock */
60#include "utils.h"
61
62#define dbgmsg( ... ) \
63    do { \
64        if( tr_deepLoggingIsActive( ) ) \
65            tr_deepLog( __FILE__, __LINE__, NULL, __VA_ARGS__ ); \
66    } while( 0 )
67
68/**
69***
70**/
71
72enum
73{
74    TR_MAX_OPEN_FILES = 16, /* real files, not sockets */
75
76    NOFILE_BUFFER = 512, /* the process' number of open files is
77                            globalMaxPeers + NOFILE_BUFFER */
78};
79
80struct tr_openfile
81{
82    tr_bool    isCheckedOut;
83    tr_bool    isWritable;
84    tr_bool    closeWhenDone;
85    char       filename[MAX_PATH_LENGTH];
86    int        fd;
87    uint64_t   date;
88};
89
90struct tr_fd_s
91{
92    int                   socketCount;
93    int                   socketMax;
94    tr_lock *             lock;
95    struct tr_openfile    open[TR_MAX_OPEN_FILES];
96};
97
98static struct tr_fd_s * gFd = NULL;
99
100/***
101****
102****  Local Files
103****
104***/
105
106#ifndef O_LARGEFILE
107#define O_LARGEFILE 0
108#endif
109
110static int
111preallocateFile( const char * filename, uint64_t length )
112{
113    int success = 0;
114
115#ifdef WIN32
116
117    HANDLE hFile = CreateFile( filename, GENERIC_WRITE, 0, 0, CREATE_NEW, 0, 0 );
118    if( hFile != INVALID_HANDLE_VALUE )
119    {
120        LARGE_INTEGER li;
121        li.QuadPart = length;
122        success = SetFilePointerEx( hFile, li, NULL, FILE_BEGIN ) && SetEndOfFile( hFile );
123        CloseHandle( hFile );
124    }
125
126#else
127
128    int flags = O_RDWR | O_CREAT | O_LARGEFILE;
129    int fd = open( filename, flags, 0666 );
130    if( fd >= 0 )
131    {
132       
133# ifdef HAVE_FALLOCATE
134
135        success = !fallocate( fd, FALLOC_FL_KEEP_SIZE, 0, length );
136
137# elif defined(HAVE_POSIX_FALLOCATE)
138
139        success = !posix_fallocate( fd, 0, length );
140
141# elif defined(SYS_DARWIN)
142
143        fstore_t fst;
144        fst.fst_flags = F_ALLOCATECONTIG;
145        fst.fst_posmode = F_PEOFPOSMODE;
146        fst.fst_offset = 0;
147        fst.fst_length = length;
148        fst.fst_bytesalloc = 0;
149        success = !fcntl( fd, F_PREALLOCATE, &fst );
150
151# else
152
153        #warning no known method to preallocate files on this platform
154        success = 0;
155
156# endif
157
158        close( fd );
159    }
160
161#endif
162
163    return success;
164}
165
166/**
167 * returns 0 on success, or an errno value on failure.
168 * errno values include ENOENT if the parent folder doesn't exist,
169 * plus the errno values set by tr_mkdirp() and open().
170 */
171static int
172TrOpenFile( int          i,
173            const char * folder,
174            const char * torrentFile,
175            int          doWrite,
176            int          doPreallocate,
177            uint64_t     desiredFileSize )
178{
179    struct tr_openfile * file = &gFd->open[i];
180    int                  flags;
181    char               * filename;
182    struct stat          sb;
183    int                  alreadyExisted;
184
185    /* confirm the parent folder exists */
186    if( stat( folder, &sb ) || !S_ISDIR( sb.st_mode ) )
187        return ENOENT;
188
189    /* create subfolders, if any */
190    filename = tr_buildPath( folder, torrentFile, NULL );
191    if( doWrite )
192    {
193        char * tmp = tr_dirname( filename );
194        const int err = tr_mkdirp( tmp, 0777 ) ? errno : 0;
195        tr_free( tmp );
196        if( err ) {
197            tr_free( filename );
198            return err;
199        }
200    }
201
202    alreadyExisted = !stat( filename, &sb ) && S_ISREG( sb.st_mode );
203
204    if( doWrite && !alreadyExisted && doPreallocate )
205        if( preallocateFile( filename, desiredFileSize ) )
206            tr_inf( _( "Preallocated file \"%s\"" ), filename );
207   
208    /* open the file */
209    flags = doWrite ? ( O_RDWR | O_CREAT ) : O_RDONLY;
210#ifdef O_LARGEFILE
211    flags |= O_LARGEFILE;
212#endif
213#ifdef WIN32
214    flags |= O_BINARY;
215#endif
216    file->fd = open( filename, flags, 0666 );
217    if( file->fd == -1 )
218    {
219        const int err = errno;
220        tr_err( _( "Couldn't open \"%1$s\": %2$s" ), filename,
221               tr_strerror( err ) );
222        tr_free( filename );
223        return err;
224    }
225
226    tr_free( filename );
227    return 0;
228}
229
230static int
231fileIsOpen( const struct tr_openfile * o )
232{
233    return o->fd >= 0;
234}
235
236static void
237TrCloseFile( int i )
238{
239    struct tr_openfile * o = &gFd->open[i];
240
241    assert( i >= 0 );
242    assert( i < TR_MAX_OPEN_FILES );
243    assert( fileIsOpen( o ) );
244
245    close( o->fd );
246    o->fd = -1;
247    o->isCheckedOut = 0;
248}
249
250static int
251fileIsCheckedOut( const struct tr_openfile * o )
252{
253    return fileIsOpen( o ) && o->isCheckedOut;
254}
255
256/* returns an fd on success, or a -1 on failure and sets errno */
257int
258tr_fdFileCheckout( const char * folder,
259                   const char * torrentFile,
260                   int          doWrite,
261                   int          doPreallocate,
262                   uint64_t     desiredFileSize )
263{
264    int                  i, winner = -1;
265    struct tr_openfile * o;
266    char               * filename;
267
268    assert( folder && *folder );
269    assert( torrentFile && *torrentFile );
270    assert( doWrite == 0 || doWrite == 1 );
271
272    filename = tr_buildPath( folder, torrentFile, NULL );
273    dbgmsg( "looking for file '%s', writable %c", filename,
274            doWrite ? 'y' : 'n' );
275
276    tr_lockLock( gFd->lock );
277
278    /* Is it already open? */
279    for( i = 0; i < TR_MAX_OPEN_FILES; ++i )
280    {
281        o = &gFd->open[i];
282
283        if( !fileIsOpen( o ) )
284            continue;
285
286        if( strcmp( filename, o->filename ) )
287            continue;
288
289        if( fileIsCheckedOut( o ) )
290        {
291            dbgmsg( "found it!  it's open, but checked out.  waiting..." );
292            tr_lockUnlock( gFd->lock );
293            tr_wait( 200 );
294            tr_lockLock( gFd->lock );
295            i = -1; /* reloop */
296            continue;
297        }
298
299        if( doWrite && !o->isWritable )
300        {
301            dbgmsg(
302                "found it!  it's open and available, but isn't writable. closing..." );
303            TrCloseFile( i );
304            break;
305        }
306
307        dbgmsg( "found it!  it's ready for use!" );
308        winner = i;
309        break;
310    }
311
312    dbgmsg(
313        "it's not already open.  looking for an open slot or an old file." );
314    while( winner < 0 )
315    {
316        uint64_t date = tr_date( ) + 1;
317
318        /* look for the file that's been open longest */
319        for( i = 0; i < TR_MAX_OPEN_FILES; ++i )
320        {
321            o = &gFd->open[i];
322
323            if( !fileIsOpen( o ) )
324            {
325                winner = i;
326                dbgmsg( "found an empty slot in %d", winner );
327                break;
328            }
329
330            if( date > o->date )
331            {
332                date = o->date;
333                winner = i;
334            }
335        }
336
337        if( winner >= 0 )
338        {
339            if( fileIsOpen( &gFd->open[winner] ) )
340            {
341                dbgmsg( "closing file '%s', slot #%d",
342                        gFd->open[winner].filename,
343                        winner );
344                TrCloseFile( winner );
345            }
346        }
347        else
348        {
349            dbgmsg(
350                "everything's full!  waiting for someone else to finish something" );
351            tr_lockUnlock( gFd->lock );
352            tr_wait( 200 );
353            tr_lockLock( gFd->lock );
354        }
355    }
356
357    assert( winner >= 0 );
358    o = &gFd->open[winner];
359    if( !fileIsOpen( o ) )
360    {
361        const int err = TrOpenFile( winner, folder, torrentFile, doWrite, doPreallocate, desiredFileSize );
362        if( err ) {
363            tr_lockUnlock( gFd->lock );
364            tr_free( filename );
365            errno = err;
366            return -1;
367        }
368
369        dbgmsg( "opened '%s' in slot %d, doWrite %c", filename, winner,
370                doWrite ? 'y' : 'n' );
371        tr_strlcpy( o->filename, filename, sizeof( o->filename ) );
372        o->isWritable = doWrite;
373    }
374
375    dbgmsg( "checking out '%s' in slot %d", filename, winner );
376    o->isCheckedOut = 1;
377    o->closeWhenDone = 0;
378    o->date = tr_date( );
379    tr_free( filename );
380    tr_lockUnlock( gFd->lock );
381    return o->fd;
382}
383
384void
385tr_fdFileReturn( int fd )
386{
387    int i;
388
389    tr_lockLock( gFd->lock );
390
391    for( i = 0; i < TR_MAX_OPEN_FILES; ++i )
392    {
393        struct tr_openfile * o = &gFd->open[i];
394        if( o->fd != fd )
395            continue;
396
397        dbgmsg( "releasing file '%s' in slot #%d", o->filename, i );
398        o->isCheckedOut = 0;
399        if( o->closeWhenDone )
400            TrCloseFile( i );
401
402        break;
403    }
404
405    tr_lockUnlock( gFd->lock );
406}
407
408void
409tr_fdFileClose( const char * filename )
410{
411    int i;
412
413    tr_lockLock( gFd->lock );
414
415    for( i = 0; i < TR_MAX_OPEN_FILES; ++i )
416    {
417        struct tr_openfile * o = &gFd->open[i];
418        if( !fileIsOpen( o ) || strcmp( filename, o->filename ) )
419            continue;
420
421        dbgmsg( "tr_fdFileClose closing '%s'", filename );
422
423        if( !o->isCheckedOut )
424        {
425            dbgmsg( "not checked out, so closing it now... '%s'", filename );
426            TrCloseFile( i );
427        }
428        else
429        {
430            dbgmsg(
431                "flagging file '%s', slot #%d to be closed when checked in",
432                gFd->open[i].filename, i );
433            o->closeWhenDone = 1;
434        }
435    }
436
437    tr_lockUnlock( gFd->lock );
438}
439
440/***
441****
442****  Sockets
443****
444***/
445
446static int
447getSocketMax( struct tr_fd_s * gFd )
448{
449    return gFd->socketMax;
450}
451
452int
453tr_fdSocketCreate( int type )
454{
455    int s = -1;
456
457    tr_lockLock( gFd->lock );
458
459    if( gFd->socketCount < getSocketMax( gFd ) )
460        if( ( s = socket( AF_INET, type, 0 ) ) < 0 )
461            tr_err( _( "Couldn't create socket: %s" ),
462                   tr_strerror( sockerrno ) );
463
464    if( s > -1 )
465        ++gFd->socketCount;
466
467    assert( gFd->socketCount >= 0 );
468
469    tr_lockUnlock( gFd->lock );
470    return s;
471}
472
473int
474tr_fdSocketAccept( int           b,
475                   tr_address  * addr,
476                   tr_port     * port )
477{
478    int                s = -1;
479    unsigned int       len;
480    struct sockaddr_storage sock;
481
482    assert( addr );
483    assert( port );
484
485    tr_lockLock( gFd->lock );
486    if( gFd->socketCount < getSocketMax( gFd ) )
487    {
488        len = sizeof( struct sockaddr );
489        s = accept( b, (struct sockaddr *) &sock, &len );
490    }
491    if( s > -1 )
492    {
493        /* "The ss_family field of the sockaddr_storage structure will always
494         * align with the family field of any protocol-specific structure." */ 
495        if( sock.ss_family == AF_INET ) 
496        { 
497            struct sockaddr_in * sock4 = (struct sockaddr_in *)&sock; 
498            addr->type = TR_AF_INET; 
499            addr->addr.addr4.s_addr = sock4->sin_addr.s_addr; 
500            *port = sock4->sin_port; 
501        } 
502        else 
503        { 
504            struct sockaddr_in6 * sock6 = (struct sockaddr_in6 *)&sock; 
505            addr->type = TR_AF_INET6; 
506            memcpy( &addr->addr, &sock6->sin6_addr, 
507                    sizeof( struct sockaddr_in6 ) ); 
508            *port = sock6->sin6_port; 
509        } 
510        ++gFd->socketCount;
511    }
512    tr_lockUnlock( gFd->lock );
513
514    return s;
515}
516
517static void
518socketClose( int fd )
519{
520    EVUTIL_CLOSESOCKET( fd );
521}
522
523void
524tr_fdSocketClose( int s )
525{
526    tr_lockLock( gFd->lock );
527
528    if( s >= 0 )
529    {
530        socketClose( s );
531        --gFd->socketCount;
532    }
533
534    assert( gFd->socketCount >= 0 );
535
536    tr_lockUnlock( gFd->lock );
537}
538
539/***
540****
541****  Startup / Shutdown
542****
543***/
544
545void
546tr_fdInit( int globalPeerLimit )
547{
548    int i;
549
550    assert( gFd == NULL );
551    gFd = tr_new0( struct tr_fd_s, 1 );
552    gFd->lock = tr_lockNew( );
553
554#ifdef HAVE_GETRLIMIT
555    {
556        struct rlimit rlim;
557        getrlimit( RLIMIT_NOFILE, &rlim );
558        rlim.rlim_cur = MIN( rlim.rlim_max,
559                            (rlim_t)( globalPeerLimit + NOFILE_BUFFER ) );
560        setrlimit( RLIMIT_NOFILE, &rlim );
561        gFd->socketMax = rlim.rlim_cur - NOFILE_BUFFER;
562        tr_dbg( "setrlimit( RLIMIT_NOFILE, %d )", (int)rlim.rlim_cur );
563    }
564#else
565    gFd->socketMax = globalPeerLimit;
566#endif
567    tr_dbg( "%d usable file descriptors", globalPeerLimit );
568
569    for( i = 0; i < TR_MAX_OPEN_FILES; ++i )
570        gFd->open[i].fd = -1;
571}
572
573void
574tr_fdClose( void )
575{
576    int i = 0;
577
578    for( i = 0; i < TR_MAX_OPEN_FILES; ++i )
579        if( fileIsOpen( &gFd->open[i] ) )
580            TrCloseFile( i );
581
582    tr_lockFree( gFd->lock );
583
584    tr_free( gFd );
585    gFd = NULL;
586}
587
588void
589tr_fdSetPeerLimit( uint16_t n )
590{
591    assert( gFd != NULL && "tr_fdInit() must be called first!" );
592    gFd->socketMax = n;
593}
594
595uint16_t
596tr_fdGetPeerLimit( void )
597{
598    return gFd ? gFd->socketMax : -1;
599}
600
Note: See TracBrowser for help on using the repository browser.