source: trunk/libtransmission/fdlimit.c @ 4598

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

mingw portability fixes: getrlimit/setrlimit in fdlimit

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