source: trunk/libtransmission/fdlimit.c @ 3918

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

fix ticket #451 (Files remain in use after removal)

  • Property svn:keywords set to Date Rev Author Id
File size: 11.3 KB
Line 
1/******************************************************************************
2 * $Id: fdlimit.c 3918 2007-11-21 16:16:59Z 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_SOCKETS = 320,
90
91    TR_MAX_OPEN_FILES = 8, /* real files, not sockets */
92
93    TR_RESERVED_FDS = 16 /* sockets reserved for tracker connections */
94};
95
96struct tr_openfile
97{
98    unsigned int  isCheckedOut : 1;
99    unsigned int  isWritable : 1;
100    unsigned int  closeWhenDone : 1;
101    char          filename[MAX_PATH_LENGTH];
102    int           fd;
103    uint64_t      date;
104};
105
106struct tr_fd_s
107{
108    int                  reserved;
109    int                  normal;
110    int                  normalMax;
111    tr_lock            * lock;
112    tr_cond            * cond;
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    tr_dbg( "Opening '%s' (%d)", filename, write );
131
132    /* create subfolders, if any */
133    if( write ) {
134        char * tmp = tr_strdup( filename );
135        const int val = tr_mkdirp( dirname(tmp), 0777 );
136        tr_free( tmp );
137        if( val )
138            return tr_ioErrorFromErrno( );
139    }
140
141    /* open the file */
142    flags = write ? (O_RDWR | O_CREAT) : O_RDONLY;
143#ifdef O_LARGEFILE
144    flags |= O_LARGEFILE;
145#endif
146#ifdef WIN32
147    flags |= O_BINARY;
148#endif
149    errno = 0;
150    file->fd = open( filename, flags, 0666 );
151    if( file->fd < 0 ) {
152        if( errno ) {
153            tr_err( "Couldn't open '%s': %s", filename, strerror(errno) );
154            return tr_ioErrorFromErrno();
155        } else {
156            tr_err( "Couldn't open '%s'", filename );
157            return TR_ERROR_IO_OTHER;
158        }
159    }
160
161    return TR_OK;
162}
163
164static int
165fileIsOpen( const struct tr_openfile * o )
166{
167    return o->fd >= 0;
168}
169
170static void
171TrCloseFile( int i )
172{
173    struct tr_openfile * o = &gFd->open[i];
174
175    assert( i >= 0 );
176    assert( i < TR_MAX_OPEN_FILES );
177    assert( fileIsOpen( o ) );
178
179    dbgmsg( "closing slot #%d, %s", i, o->filename );
180    close( o->fd );
181    o->fd = -1;
182    o->isCheckedOut = 0;
183    tr_condSignal( gFd->cond );
184}
185
186static int
187fileIsCheckedOut( const struct tr_openfile * o )
188{
189    return fileIsOpen(o) && o->isCheckedOut;
190}
191
192int
193tr_fdFileCheckout( const char * filename, int write )
194{
195    int i, winner;
196    struct tr_openfile * o;
197
198    assert( filename && *filename );
199    assert( write==0 || write==1 );
200
201    dbgmsg( "looking for file '%s', writable %c", filename, write?'y':'n' );
202
203    tr_lockLock( gFd->lock );
204
205    /* Is it already open? */
206    for( i=0; i<TR_MAX_OPEN_FILES; ++i )
207    {
208        o = &gFd->open[i];
209
210        if( !fileIsOpen( o ) )
211            continue;
212
213        if( strcmp( filename, o->filename ) )
214            continue;
215
216        if( fileIsCheckedOut( o ) ) {
217            dbgmsg( "found it!  it's open, but checked out.  waiting..." );
218            tr_condWait( gFd->cond, gFd->lock );
219            i = -1; /* reloop */
220            continue;
221        }
222
223        if( write && !o->isWritable ) {
224            dbgmsg( "found it!  it's open and available, but isn't writable. closing..." );
225            TrCloseFile( i );
226            break;
227        }
228
229        dbgmsg( "found it!  it's ready for use!" );
230        winner = i;
231        goto done;
232    }
233
234
235    dbgmsg( "it's not already open.  looking for an open slot or an old file." );
236    for( ;; )
237    {
238        uint64_t date = tr_date( ) + 1;
239        winner = -1;
240
241        for( i=0; i<TR_MAX_OPEN_FILES; ++i )
242        {
243            o = &gFd->open[i];
244
245            if( !fileIsOpen( o ) ) {
246                winner = i;
247                dbgmsg( "found an empty slot in %d", winner );
248                goto done;
249            }
250
251            if( date > o->date ) {
252                date = o->date;
253                winner = i;
254            }
255        }
256
257        if( winner >= 0 ) {
258            dbgmsg( "closing file '%s', slot #%d", gFd->open[winner].filename, winner );
259            TrCloseFile( winner );
260            goto done;
261        }
262
263        /* All used! Wait a bit and try again */
264        dbgmsg( "everything's full!  waiting for someone else to finish something" );
265        tr_condWait( gFd->cond, gFd->lock );
266    }
267
268done:
269
270    o = &gFd->open[winner];
271    if( !fileIsOpen( o ) )
272    {
273        const int ret = TrOpenFile( winner, filename, write );
274        if( ret ) {
275            tr_lockUnlock( gFd->lock );
276            return ret;
277        }
278
279        dbgmsg( "opened '%s' in slot %d, write %c", filename, winner, write?'y':'n' );
280        strlcpy( o->filename, filename, sizeof( o->filename ) );
281        o->isWritable = write;
282    }
283
284    dbgmsg( "checking out '%s' in slot %d", filename, winner );
285    o->isCheckedOut = 1;
286    o->closeWhenDone = 0;
287    o->date = tr_date( );
288    tr_lockUnlock( gFd->lock );
289    return o->fd;
290}
291
292void
293tr_fdFileReturn( int fd )
294{
295    int i;
296    tr_lockLock( gFd->lock );
297
298    for( i=0; i<TR_MAX_OPEN_FILES; ++i )
299    {
300        struct tr_openfile * o = &gFd->open[i];
301        if( o->fd != fd )
302            continue;
303
304        dbgmsg( "releasing file '%s' in slot #%d", o->filename, i );
305        o->isCheckedOut = 0;
306        if( o->closeWhenDone )
307            TrCloseFile( i );
308       
309        break;
310    }
311   
312    tr_condSignal( gFd->cond );
313    tr_lockUnlock( gFd->lock );
314}
315
316void
317tr_fdFileClose( const char * filename )
318{
319    int i;
320    tr_lockLock( gFd->lock );
321    dbgmsg( "tr_fdFileClose closing '%s'", filename );
322
323    for( i=0; i<TR_MAX_OPEN_FILES; ++i )
324    {
325        struct tr_openfile * o = &gFd->open[i];
326        if( !fileIsOpen(o) || strcmp(filename,o->filename) )
327            continue;
328
329        if( !o->isCheckedOut ) {
330            dbgmsg( "not checked out, so closing it now... '%s'", filename );
331            TrCloseFile( i );
332        } else {
333            dbgmsg( "flagging file '%s', slot #%d to be closed when checked in", gFd->open[i].filename, i );
334            o->closeWhenDone = 1;
335        }
336    }
337   
338    tr_condSignal( gFd->cond );
339    tr_lockUnlock( gFd->lock );
340}
341
342/***
343****
344****  Sockets
345****
346***/
347
348static tr_list * reservedSockets = NULL;
349
350static void
351setSocketPriority( int fd, int isReserved )
352{
353    if( isReserved )
354        tr_list_append( &reservedSockets, TR_UINT_TO_PTR(fd) );
355}
356
357static int
358socketWasReserved( int fd )
359{
360    return tr_list_remove_data( &reservedSockets, TR_UINT_TO_PTR(fd) ) != NULL;
361}
362
363int
364tr_fdSocketCreate( int type, int isReserved )
365{
366    int s = -1;
367    tr_lockLock( gFd->lock );
368
369    if( isReserved && gFd->reserved >= TR_RESERVED_FDS )
370        isReserved = FALSE;
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( void )
458{
459    int i, j, s[TR_MAX_SOCKETS];
460
461    assert( gFd == NULL );
462
463    gFd = tr_new0( struct tr_fd_s, 1 );
464    gFd->lock = tr_lockNew( );
465    gFd->cond = tr_condNew( );
466
467    /* count the max number of sockets we can use */
468    for( i=0; i<TR_MAX_SOCKETS; ++i )
469        if( ( s[i] = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
470            break;
471    for( j=0; j<i; ++j )
472        socketClose( s[j] );
473    tr_dbg( "%d usable file descriptors", i );
474
475    /* set some fds aside for the UI or daemon to use */
476    gFd->normalMax = i - TR_RESERVED_FDS - 10;
477
478    for( i=0; i<TR_MAX_OPEN_FILES; ++i )
479        gFd->open[i].fd = -1;
480         
481}
482
483void
484tr_fdClose( void )
485{
486    int i = 0;
487
488    for( i=0; i<TR_MAX_OPEN_FILES; ++i )
489        if( fileIsOpen( &gFd->open[i] ) )
490            TrCloseFile( i );
491
492    tr_lockFree( gFd->lock );
493    tr_condFree( gFd->cond );
494
495    tr_list_free( &reservedSockets, NULL );
496    tr_free( gFd );
497}
Note: See TracBrowser for help on using the repository browser.