source: trunk/libtransmission/fdlimit.c @ 4570

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

mollify a noisy debugging message

  • Property svn:keywords set to Date Rev Author Id
File size: 11.3 KB
Line 
1/******************************************************************************
2 * $Id: fdlimit.c 4570 2008-01-08 20:08:45Z 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#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 <sys/time.h> /* getrlimit */
35#include <sys/resource.h> /* getrlimit */
36#include <unistd.h>
37#include <libgen.h> /* basename, dirname */
38#include <fcntl.h> /* O_LARGEFILE */
39
40#include <event.h>
41#include <evutil.h>
42
43#include "transmission.h"
44#include "trcompat.h"
45#include "list.h"
46#include "net.h"
47#include "platform.h"
48#include "utils.h"
49
50#if SIZEOF_VOIDP==8
51#define TR_UINT_TO_PTR(i) (void*)((uint64_t)i)
52#else
53#define TR_UINT_TO_PTR(i) ((void*)((uint32_t)i))
54#endif
55
56/**
57***
58**/
59
60static void
61myDebug( const char * file, int line, const char * fmt, ... )
62{
63    FILE * fp = tr_getLog( );
64    if( fp != NULL )
65    {
66        va_list args;
67        char s[64];
68        struct evbuffer * buf = evbuffer_new( );
69        char * myfile = tr_strdup( file );
70
71        evbuffer_add_printf( buf, "[%s] ", tr_getLogTimeStr( s, sizeof(s) ) );
72        va_start( args, fmt );
73        evbuffer_add_vprintf( buf, fmt, args );
74        va_end( args );
75        evbuffer_add_printf( buf, " (%s:%d)\n", basename(myfile), line );
76        fwrite( EVBUFFER_DATA(buf), 1, EVBUFFER_LENGTH(buf), fp );
77
78        tr_free( myfile );
79        evbuffer_free( buf );
80    }
81}
82
83#define dbgmsg(fmt...) myDebug(__FILE__, __LINE__, ##fmt )
84
85/**
86***
87**/
88
89enum
90{
91    TR_MAX_OPEN_FILES = 16, /* real files, not sockets */
92
93    NOFILE_BUFFER = 512, /* the process' number of open files is
94                            globalMaxPeers + NOFILE_BUFFER */
95};
96
97struct tr_openfile
98{
99    unsigned int  isCheckedOut : 1;
100    unsigned int  isWritable : 1;
101    unsigned int  closeWhenDone : 1;
102    char          filename[MAX_PATH_LENGTH];
103    int           fd;
104    uint64_t      date;
105};
106
107struct tr_fd_s
108{
109    int                  reserved;
110    int                  normal;
111    int                  normalMax;
112    tr_lock            * lock;
113    struct tr_openfile   open[TR_MAX_OPEN_FILES];
114};
115
116static struct tr_fd_s * gFd = NULL;
117
118/***
119****
120****  Local Files
121****
122***/
123
124static int
125TrOpenFile( int i, const char * filename, int write )
126{
127    struct tr_openfile * file = &gFd->open[i];
128    int flags;
129
130    /* create subfolders, if any */
131    if( write ) {
132        char * tmp = tr_strdup( filename );
133        const int val = tr_mkdirp( dirname(tmp), 0777 );
134        tr_free( tmp );
135        if( val )
136            return tr_ioErrorFromErrno( );
137    }
138
139    /* open the file */
140    flags = write ? (O_RDWR | O_CREAT) : O_RDONLY;
141#ifdef O_LARGEFILE
142    flags |= O_LARGEFILE;
143#endif
144#ifdef WIN32
145    flags |= O_BINARY;
146#endif
147    errno = 0;
148    file->fd = open( filename, flags, 0666 );
149    if( file->fd < 0 ) {
150        if( errno ) {
151            tr_err( "Couldn't open '%s': %s", filename, strerror(errno) );
152            return tr_ioErrorFromErrno();
153        } else {
154            tr_err( "Couldn't open '%s'", filename );
155            return TR_ERROR_IO_OTHER;
156        }
157    }
158
159    return TR_OK;
160}
161
162static int
163fileIsOpen( const struct tr_openfile * o )
164{
165    return o->fd >= 0;
166}
167
168static void
169TrCloseFile( int i )
170{
171    struct tr_openfile * o = &gFd->open[i];
172
173    assert( i >= 0 );
174    assert( i < TR_MAX_OPEN_FILES );
175    assert( fileIsOpen( o ) );
176
177    close( o->fd );
178    o->fd = -1;
179    o->isCheckedOut = 0;
180}
181
182static int
183fileIsCheckedOut( const struct tr_openfile * o )
184{
185    return fileIsOpen(o) && o->isCheckedOut;
186}
187
188int
189tr_fdFileCheckout( const char * filename, int write )
190{
191    int i, winner = -1;
192    struct tr_openfile * o;
193
194    assert( filename && *filename );
195    assert( write==0 || write==1 );
196
197    dbgmsg( "looking for file '%s', writable %c", filename, write?'y':'n' );
198
199    tr_lockLock( gFd->lock );
200
201    /* Is it already open? */
202    for( i=0; i<TR_MAX_OPEN_FILES; ++i )
203    {
204        o = &gFd->open[i];
205
206        if( !fileIsOpen( o ) )
207            continue;
208
209        if( strcmp( filename, o->filename ) )
210            continue;
211
212        if( fileIsCheckedOut( o ) ) {
213            dbgmsg( "found it!  it's open, but checked out.  waiting..." );
214            tr_lockUnlock( gFd->lock );
215            tr_wait( 200 );
216            tr_lockLock( gFd->lock );
217            i = -1; /* reloop */
218            continue;
219        }
220
221        if( write && !o->isWritable ) {
222            dbgmsg( "found it!  it's open and available, but isn't writable. closing..." );
223            TrCloseFile( i );
224            break;
225        }
226
227        dbgmsg( "found it!  it's ready for use!" );
228        winner = i;
229        break;
230    }
231
232    dbgmsg( "it's not already open.  looking for an open slot or an old file." );
233    while( winner < 0 )
234    {
235        uint64_t date = tr_date( ) + 1;
236
237        /* look for the file that's been open longest */
238        for( i=0; i<TR_MAX_OPEN_FILES; ++i )
239        {
240            o = &gFd->open[i];
241
242            if( !fileIsOpen( o ) ) {
243                winner = i;
244                dbgmsg( "found an empty slot in %d", winner );
245                break;
246            }
247
248            if( date > o->date ) {
249                date = o->date;
250                winner = i;
251            }
252        }
253
254        if( winner >= 0 ) {
255            if( fileIsOpen( &gFd->open[winner] ) ) {
256                dbgmsg( "closing file '%s', slot #%d", gFd->open[winner].filename, winner );
257                TrCloseFile( winner );
258            }
259        } else { 
260            dbgmsg( "everything's full!  waiting for someone else to finish something" );
261            tr_lockUnlock( gFd->lock );
262            tr_wait( 200 );
263            tr_lockLock( gFd->lock );
264        }
265    }
266
267    assert( winner >= 0 );
268    o = &gFd->open[winner];
269    if( !fileIsOpen( o ) )
270    {
271        const int ret = TrOpenFile( winner, filename, write );
272        if( ret ) {
273            tr_lockUnlock( gFd->lock );
274            return ret;
275        }
276
277        dbgmsg( "opened '%s' in slot %d, write %c", filename, winner, write?'y':'n' );
278        strlcpy( o->filename, filename, sizeof( o->filename ) );
279        o->isWritable = write;
280    }
281
282    dbgmsg( "checking out '%s' in slot %d", filename, winner );
283    o->isCheckedOut = 1;
284    o->closeWhenDone = 0;
285    o->date = tr_date( );
286    tr_lockUnlock( gFd->lock );
287    return o->fd;
288}
289
290void
291tr_fdFileReturn( int fd )
292{
293    int i;
294    tr_lockLock( gFd->lock );
295
296    for( i=0; i<TR_MAX_OPEN_FILES; ++i )
297    {
298        struct tr_openfile * o = &gFd->open[i];
299        if( o->fd != fd )
300            continue;
301
302        dbgmsg( "releasing file '%s' in slot #%d", o->filename, i );
303        o->isCheckedOut = 0;
304        if( o->closeWhenDone )
305            TrCloseFile( i );
306       
307        break;
308    }
309   
310    tr_lockUnlock( gFd->lock );
311}
312
313void
314tr_fdFileClose( const char * filename )
315{
316    int i;
317    tr_lockLock( gFd->lock );
318
319    for( i=0; i<TR_MAX_OPEN_FILES; ++i )
320    {
321        struct tr_openfile * o = &gFd->open[i];
322        if( !fileIsOpen(o) || strcmp(filename,o->filename) )
323            continue;
324
325        dbgmsg( "tr_fdFileClose closing '%s'", filename );
326
327        if( !o->isCheckedOut ) {
328            dbgmsg( "not checked out, so closing it now... '%s'", filename );
329            TrCloseFile( i );
330        } else {
331            dbgmsg( "flagging file '%s', slot #%d to be closed when checked in", gFd->open[i].filename, i );
332            o->closeWhenDone = 1;
333        }
334    }
335   
336    tr_lockUnlock( gFd->lock );
337}
338
339/***
340****
341****  Sockets
342****
343***/
344
345static tr_list * reservedSockets = NULL;
346
347static void
348setSocketPriority( int fd, int isReserved )
349{
350    if( isReserved )
351        tr_list_append( &reservedSockets, TR_UINT_TO_PTR(fd) );
352}
353
354static int
355socketWasReserved( int fd )
356{
357    return tr_list_remove_data( &reservedSockets, TR_UINT_TO_PTR(fd) ) != NULL;
358}
359
360int
361tr_fdSocketCreate( int type, int isReserved )
362{
363    int s = -1;
364    tr_lockLock( gFd->lock );
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;
454    struct rlimit rlim;
455
456    assert( gFd == NULL );
457    gFd = tr_new0( struct tr_fd_s, 1 );
458    gFd->lock = tr_lockNew( );
459
460    getrlimit( RLIMIT_NOFILE, &rlim );
461    rlim.rlim_cur = MIN( rlim.rlim_max, (rlim_t)(globalPeerLimit + NOFILE_BUFFER) );
462    setrlimit( RLIMIT_NOFILE, &rlim );
463    gFd->normalMax = rlim.rlim_cur - NOFILE_BUFFER;
464    tr_dbg( "%d usable file descriptors", globalPeerLimit );
465
466    for( i=0; i<TR_MAX_OPEN_FILES; ++i ) 
467        gFd->open[i].fd = -1;
468}
469
470void
471tr_fdClose( void )
472{
473    int i = 0;
474
475    for( i=0; i<TR_MAX_OPEN_FILES; ++i )
476        if( fileIsOpen( &gFd->open[i] ) )
477            TrCloseFile( i );
478
479    tr_lockFree( gFd->lock );
480
481    tr_list_free( &reservedSockets, NULL );
482    tr_free( gFd );
483}
484
485void
486tr_fdSetPeerLimit( uint16_t n )
487{
488    assert( gFd!=NULL && "tr_fdInit() must be called first!" );
489    gFd->normalMax = n;
490}
491
492uint16_t
493tr_fdGetPeerLimit( void )
494{
495    return gFd ? gFd->normalMax : -1;
496}
Note: See TracBrowser for help on using the repository browser.