source: trunk/libtransmission/fdlimit.c @ 6636

Last change on this file since 6636 was 6636, checked in by charles, 15 years ago

crash deeper inside libtransmission. do not use this build; it crashes on purpose to test part of the nightly build system

  • Property svn:keywords set to Date Rev Author Id
File size: 11.6 KB
Line 
1/******************************************************************************
2 * $Id: fdlimit.c 6636 2008-08-22 20:06:51Z 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#include <assert.h>
30#include <errno.h>
31#include <inttypes.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35
36#include <sys/types.h>
37#include <sys/stat.h>
38#ifdef HAVE_GETRLIMIT
39#include <sys/time.h> /* getrlimit */
40#include <sys/resource.h> /* getrlimit */
41#endif
42#include <unistd.h>
43#include <libgen.h> /* dirname */
44#include <fcntl.h> /* O_LARGEFILE */
45
46#include <event.h>
47#include <evutil.h>
48
49#include "transmission.h"
50#include "fdlimit.h"
51#include "list.h"
52#include "net.h"
53#include "platform.h" /* tr_lock */
54#include "utils.h"
55
56#if SIZEOF_VOIDP==8
57#define TR_UINT_TO_PTR(i) (void*)((uint64_t)i)
58#else
59#define TR_UINT_TO_PTR(i) ((void*)((uint32_t)i))
60#endif
61
62#define dbgmsg(fmt...) tr_deepLog( __FILE__, __LINE__, NULL, ##fmt )
63
64/**
65***
66**/
67
68enum
69{
70    TR_MAX_OPEN_FILES = 16, /* real files, not sockets */
71
72    NOFILE_BUFFER = 512, /* the process' number of open files is
73                            globalMaxPeers + NOFILE_BUFFER */
74};
75
76struct tr_openfile
77{
78    unsigned int  isCheckedOut : 1;
79    unsigned int  isWritable : 1;
80    unsigned int  closeWhenDone : 1;
81    char          filename[MAX_PATH_LENGTH];
82    int           fd;
83    uint64_t      date;
84};
85
86struct tr_fd_s
87{
88    int                  reserved;
89    int                  normal;
90    int                  normalMax;
91    tr_lock            * lock;
92    struct tr_openfile   open[TR_MAX_OPEN_FILES];
93};
94
95static struct tr_fd_s * gFd = NULL;
96
97/***
98****
99****  Local Files
100****
101***/
102
103static tr_errno
104TrOpenFile( int           i,
105            const char  * folder,
106            const char  * torrentFile,
107            int           write )
108{
109    struct tr_openfile * file = &gFd->open[i];
110    int flags;
111    char filename[MAX_PATH_LENGTH];
112    struct stat sb;
113
114    /* confirm the parent folder exists */
115    if( stat( folder, &sb ) || !S_ISDIR( sb.st_mode ) )
116        return TR_ERROR_IO_PARENT;
117
118    /* create subfolders, if any */
119    tr_buildPath ( filename, sizeof(filename), folder, torrentFile, NULL );
120    if( write ) {
121        char * tmp = tr_strdup( filename );
122        const int err = tr_mkdirp( dirname(tmp), 0777 ) ? errno : 0;
123        tr_free( tmp );
124        if( err )
125            return tr_ioErrorFromErrno( err );
126    }
127
128    /* open the file */
129    flags = write ? (O_RDWR | O_CREAT) : O_RDONLY;
130#ifdef O_LARGEFILE
131    flags |= O_LARGEFILE;
132#endif
133#ifdef WIN32
134    flags |= O_BINARY;
135#endif
136    file->fd = open( filename, flags, 0666 );
137    if( file->fd == -1 ) {
138        const int err = errno;
139        tr_err( _( "Couldn't open \"%1$s\": %2$s" ), filename, tr_strerror(err) );
140        return tr_ioErrorFromErrno( err );
141    }
142
143    return TR_OK;
144}
145
146static int
147fileIsOpen( const struct tr_openfile * o )
148{
149    return o->fd >= 0;
150}
151
152static void
153TrCloseFile( int i )
154{
155    struct tr_openfile * o = &gFd->open[i];
156
157    assert( i >= 0 );
158    assert( i < TR_MAX_OPEN_FILES );
159    assert( fileIsOpen( o ) );
160
161    close( o->fd );
162    o->fd = -1;
163    o->isCheckedOut = 0;
164}
165
166static int
167fileIsCheckedOut( const struct tr_openfile * o )
168{
169    return fileIsOpen(o) && o->isCheckedOut;
170}
171
172int
173tr_fdFileCheckout( const char * folder,
174                   const char * torrentFile, 
175                   int          write )
176{
177    int i, winner = -1;
178    struct tr_openfile * o;
179    char filename[MAX_PATH_LENGTH];
180
181    assert( folder && *folder );
182    assert( torrentFile && *torrentFile );
183    assert( write==0 || write==1 );
184
185    tr_buildPath ( filename, sizeof(filename), folder, torrentFile, NULL );
186    dbgmsg( "looking for file '%s', writable %c", filename, write?'y':'n' );
187
188    tr_lockLock( gFd->lock );
189
190    /* Is it already open? */
191    for( i=0; i<TR_MAX_OPEN_FILES; ++i )
192    {
193        o = &gFd->open[i];
194
195        if( !fileIsOpen( o ) )
196            continue;
197
198        if( strcmp( filename, o->filename ) )
199            continue;
200
201        if( fileIsCheckedOut( o ) ) {
202            dbgmsg( "found it!  it's open, but checked out.  waiting..." );
203            tr_lockUnlock( gFd->lock );
204            tr_wait( 200 );
205            tr_lockLock( gFd->lock );
206            i = -1; /* reloop */
207            continue;
208        }
209
210        if( write && !o->isWritable ) {
211            dbgmsg( "found it!  it's open and available, but isn't writable. closing..." );
212            TrCloseFile( i );
213            break;
214        }
215
216        dbgmsg( "found it!  it's ready for use!" );
217        winner = i;
218        break;
219    }
220
221    dbgmsg( "it's not already open.  looking for an open slot or an old file." );
222    while( winner < 0 )
223    {
224        uint64_t date = tr_date( ) + 1;
225
226        /* look for the file that's been open longest */
227        for( i=0; i<TR_MAX_OPEN_FILES; ++i )
228        {
229            o = &gFd->open[i];
230
231            if( !fileIsOpen( o ) ) {
232                winner = i;
233                dbgmsg( "found an empty slot in %d", winner );
234                break;
235            }
236
237            if( date > o->date ) {
238                date = o->date;
239                winner = i;
240            }
241        }
242
243        if( winner >= 0 ) {
244            if( fileIsOpen( &gFd->open[winner] ) ) {
245                dbgmsg( "closing file '%s', slot #%d", gFd->open[winner].filename, winner );
246                TrCloseFile( winner );
247            }
248        } else { 
249            dbgmsg( "everything's full!  waiting for someone else to finish something" );
250            tr_lockUnlock( gFd->lock );
251            tr_wait( 200 );
252            tr_lockLock( gFd->lock );
253        }
254    }
255
256    assert( winner >= 0 );
257    o = &gFd->open[winner];
258    if( !fileIsOpen( o ) )
259    {
260        const tr_errno err = TrOpenFile( winner, folder, torrentFile, write );
261        if( err ) {
262            tr_lockUnlock( gFd->lock );
263            return err;
264        }
265
266        dbgmsg( "opened '%s' in slot %d, write %c", filename, winner, write?'y':'n' );
267        tr_strlcpy( o->filename, filename, sizeof( o->filename ) );
268        o->isWritable = write;
269    }
270
271    dbgmsg( "checking out '%s' in slot %d", filename, winner );
272    o->isCheckedOut = 1;
273    o->closeWhenDone = 0;
274    o->date = tr_date( );
275    tr_lockUnlock( gFd->lock );
276    return o->fd;
277}
278
279void
280tr_fdFileReturn( int fd )
281{
282    int i;
283    tr_lockLock( gFd->lock );
284
285    for( i=0; i<TR_MAX_OPEN_FILES; ++i )
286    {
287        struct tr_openfile * o = &gFd->open[i];
288        if( o->fd != fd )
289            continue;
290
291        dbgmsg( "releasing file '%s' in slot #%d", o->filename, i );
292        o->isCheckedOut = 0;
293        if( o->closeWhenDone )
294            TrCloseFile( i );
295       
296        break;
297    }
298   
299    tr_lockUnlock( gFd->lock );
300}
301
302void
303tr_fdFileClose( const char * filename )
304{
305    int i;
306    tr_lockLock( gFd->lock );
307
308    for( i=0; i<TR_MAX_OPEN_FILES; ++i )
309    {
310        struct tr_openfile * o = &gFd->open[i];
311        if( !fileIsOpen(o) || strcmp(filename,o->filename) )
312            continue;
313
314        dbgmsg( "tr_fdFileClose closing '%s'", filename );
315
316        if( !o->isCheckedOut ) {
317            dbgmsg( "not checked out, so closing it now... '%s'", filename );
318            TrCloseFile( i );
319        } else {
320            dbgmsg( "flagging file '%s', slot #%d to be closed when checked in", gFd->open[i].filename, i );
321            o->closeWhenDone = 1;
322        }
323    }
324   
325    tr_lockUnlock( gFd->lock );
326}
327
328/***
329****
330****  Sockets
331****
332***/
333
334static tr_list * reservedSockets = NULL;
335
336static void
337setSocketPriority( int fd, int isReserved )
338{
339    if( isReserved )
340        tr_list_append( &reservedSockets, TR_UINT_TO_PTR(fd) );
341}
342
343static int
344socketWasReserved( int fd )
345{
346    return tr_list_remove_data( &reservedSockets, TR_UINT_TO_PTR(fd) ) != NULL;
347}
348
349static int
350getSocketMax( struct tr_fd_s * gFd )
351{
352    return gFd->normalMax;
353}
354
355int
356tr_fdSocketCreate( int type, int isReserved )
357{
358    int s = -1;
359    tr_lockLock( gFd->lock );
360
361/* FIXME */
362abort();
363
364    if( isReserved || ( gFd->normal < getSocketMax( gFd ) ) )
365        if( ( s = socket( AF_INET, type, 0 ) ) < 0 )
366            tr_err( _( "Couldn't create socket: %s" ), tr_strerror( sockerrno ) );
367
368    if( s > -1 )
369    {
370        setSocketPriority( s, isReserved );
371
372        if( isReserved )
373            ++gFd->reserved;
374        else
375            ++gFd->normal;
376    }
377
378    assert( gFd->reserved >= 0 );
379    assert( gFd->normal >= 0 );
380
381    tr_lockUnlock( gFd->lock );
382    return s;
383}
384
385int
386tr_fdSocketAccept( int b, struct in_addr * addr, tr_port_t * port )
387{
388    int s = -1;
389    unsigned int len;
390    struct sockaddr_in sock;
391
392    assert( addr );
393    assert( port );
394
395    tr_lockLock( gFd->lock );
396    if( gFd->normal < getSocketMax( gFd ) )
397    {
398        len = sizeof( sock );
399        s = accept( b, (struct sockaddr *) &sock, &len );
400    }
401    if( s > -1 )
402    {
403        setSocketPriority( s, FALSE );
404        *addr = sock.sin_addr;
405        *port = sock.sin_port;
406        gFd->normal++;
407    }
408    tr_lockUnlock( gFd->lock );
409
410    return s;
411}
412
413static void
414socketClose( int fd )
415{
416#ifdef BEOS_NETSERVER
417    closesocket( fd );
418#else
419    EVUTIL_CLOSESOCKET( fd );
420#endif
421}
422
423void
424tr_fdSocketClose( int s )
425{
426    tr_lockLock( gFd->lock );
427
428    if( s >= 0 ) {
429        socketClose( s );
430        if( socketWasReserved( s ) )
431            --gFd->reserved;
432        else
433            --gFd->normal;
434    }
435
436    assert( gFd->reserved >= 0 );
437    assert( gFd->normal >= 0 );
438
439    tr_lockUnlock( gFd->lock );
440}
441
442/***
443****
444****  Startup / Shutdown
445****
446***/
447
448void
449tr_fdInit( int globalPeerLimit )
450{
451    int i;
452
453    assert( gFd == NULL );
454    gFd = tr_new0( struct tr_fd_s, 1 );
455    gFd->lock = tr_lockNew( );
456
457#ifdef HAVE_GETRLIMIT
458    {
459        struct rlimit rlim;
460        getrlimit( RLIMIT_NOFILE, &rlim );
461        rlim.rlim_cur = MIN( rlim.rlim_max,
462               (rlim_t)(globalPeerLimit + NOFILE_BUFFER) );
463        setrlimit( RLIMIT_NOFILE, &rlim );
464        gFd->normalMax = rlim.rlim_cur - NOFILE_BUFFER;
465        tr_dbg( "setrlimit( RLIMIT_NOFILE, %d )", (int)rlim.rlim_cur );
466    }
467#else
468    gFd->normalMax = globalPeerLimit;
469#endif
470    tr_dbg( "%d usable file descriptors", globalPeerLimit );
471
472    for( i=0; i<TR_MAX_OPEN_FILES; ++i ) 
473        gFd->open[i].fd = -1;
474}
475
476void
477tr_fdClose( void )
478{
479    int i = 0;
480
481    for( i=0; i<TR_MAX_OPEN_FILES; ++i )
482        if( fileIsOpen( &gFd->open[i] ) )
483            TrCloseFile( i );
484
485    tr_lockFree( gFd->lock );
486
487    tr_list_free( &reservedSockets, NULL );
488    tr_free( gFd );
489}
490
491void
492tr_fdSetPeerLimit( uint16_t n )
493{
494    assert( gFd!=NULL && "tr_fdInit() must be called first!" );
495    gFd->normalMax = n;
496}
497
498uint16_t
499tr_fdGetPeerLimit( void )
500{
501    return gFd ? gFd->normalMax : -1;
502}
Note: See TracBrowser for help on using the repository browser.