source: trunk/libtransmission/fdlimit.c @ 4366

Last change on this file since 4366 was 4366, checked in by charles, 14 years ago

silence a debug message gone wild

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