source: trunk/libtransmission/fdlimit.c @ 3666

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

"corruption" fix part 3: cleanup. remove unneeded possible points of failure.

  • Property svn:keywords set to Date Rev Author Id
File size: 10.5 KB
Line 
1/******************************************************************************
2 * $Id: fdlimit.c 3666 2007-10-31 18:10:55Z 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>
37
38#include <sys/queue.h> /* libevent needs this */
39#include <sys/types.h> /* libevent needs this */
40#include <event.h>
41#include <evhttp.h>
42#include <evutil.h>
43
44#include "transmission.h"
45#include "trcompat.h"
46#include "list.h"
47#include "net.h"
48#include "platform.h"
49#include "utils.h"
50
51#if SIZEOF_VOIDP==8
52#define TR_UINT_TO_PTR(i) (void*)((uint64_t)i)
53#else
54#define TR_UINT_TO_PTR(i) ((void*)((uint32_t)i))
55#endif
56
57/**
58***
59**/
60
61static void
62myDebug( const char * file, int line, const char * fmt, ... )
63{
64    FILE * fp = tr_getLog( );
65    if( fp != NULL )
66    {
67        va_list args;
68        char s[64];
69        struct evbuffer * buf = evbuffer_new( );
70        char * myfile = tr_strdup( file );
71
72        evbuffer_add_printf( buf, "[%s] ", tr_getLogTimeStr( s, sizeof(s) ) );
73        va_start( args, fmt );
74        evbuffer_add_vprintf( buf, fmt, args );
75        va_end( args );
76        evbuffer_add_printf( buf, " (%s:%d)\n", basename(myfile), line );
77        fwrite( EVBUFFER_DATA(buf), 1, EVBUFFER_LENGTH(buf), fp );
78
79        tr_free( myfile );
80        evbuffer_free( buf );
81    }
82}
83
84#define dbgmsg(fmt...) myDebug(__FILE__, __LINE__, ##fmt )
85
86/**
87***
88**/
89
90enum
91{
92    TR_MAX_OPEN_FILES = 16, /* real files, not sockets */
93
94    TR_RESERVED_FDS   = 16 /* sockets reserved for tracker connections */
95};
96
97struct tr_openfile
98{
99    unsigned int  isCheckedOut : 1;
100    unsigned int  isWritable : 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    char * dir;
129    int flags;
130
131    tr_dbg( "Opening '%s' (%d)", filename, write );
132
133    /* create subfolders, if any */
134    dir = dirname( tr_strdup( filename ) );
135    if( write && tr_mkdirp( dir, 0700 ) ) {
136        free( dir );
137        return tr_ioErrorFromErrno( );
138    }
139
140    /* open the file */
141    flags = write ? (O_RDWR | O_CREAT) : O_RDONLY;
142#ifdef WIN32
143    flags |= O_BINARY;
144#endif
145    errno = 0;
146    file->fd = open( filename, flags, 0600 );
147    free( dir );
148    if( file->fd < 0 ) {
149        if( errno ) {
150            tr_err( "Couldn't open '%s': %s", filename, strerror(errno) );
151            return tr_ioErrorFromErrno();
152        } else {
153            tr_err( "Couldn't open '%s'", filename );
154            return TR_ERROR_IO_OTHER;
155        }
156    }
157
158    return TR_OK;
159}
160
161static int
162fileIsOpen( const struct tr_openfile * o )
163{
164    return o->fd >= 0;
165}
166
167static void
168TrCloseFile( int i )
169{
170    struct tr_openfile * o = &gFd->open[i];
171
172    assert( i >= 0 );
173    assert( i < TR_MAX_OPEN_FILES );
174    assert( fileIsOpen( o ) );
175
176    dbgmsg( "closing slot #%d, %s", i, o->filename );
177    close( o->fd );
178    o->fd = -1;
179    o->isCheckedOut = 0;
180    tr_condSignal( gFd->cond );
181}
182
183static int
184fileIsCheckedOut( const struct tr_openfile * o )
185{
186    return fileIsOpen(o) && o->isCheckedOut;
187}
188
189int
190tr_fdFileOpen( const char * filename, int write )
191{
192    int i, winner;
193    struct tr_openfile * o;
194
195    assert( filename && *filename );
196    assert( write==0 || write==1 );
197
198    dbgmsg( "looking for file '%s', writable %c", filename, write?'y':'n' );
199
200    tr_lockLock( gFd->lock );
201
202    /* Is it already open? */
203    for( i=0; i<TR_MAX_OPEN_FILES; ++i )
204    {
205        o = &gFd->open[i];
206
207        if( !fileIsOpen( o ) )
208            continue;
209
210        if( strcmp( filename, o->filename ) )
211            continue;
212
213        if( fileIsCheckedOut( o ) ) {
214            dbgmsg( "found it!  it's open, but checked out.  waiting..." );
215            tr_condWait( gFd->cond, gFd->lock );
216            i = -1; /* reloop */
217            continue;
218        }
219
220        if( write && !o->isWritable ) {
221            dbgmsg( "found it!  it's open and available, but isn't writable. closing..." );
222            TrCloseFile( i );
223            break;
224        }
225
226        dbgmsg( "found it!  it's ready for use!" );
227        winner = i;
228        goto done;
229    }
230
231
232    dbgmsg( "it's not already open.  looking for an open slot or an old file." );
233    for( ;; )
234    {
235        uint64_t date = tr_date() + 1;
236        winner = -1;
237
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                goto done;
246            }
247
248            if( date > o->date ) {
249                date = o->date;
250                winner = i;
251            }
252        }
253
254        if( winner >= 0 ) {
255            dbgmsg( "closing file '%s', slot #%d", gFd->open[winner].filename, winner );
256            TrCloseFile( winner );
257            goto done;
258        }
259
260        /* All used! Wait a bit and try again */
261        dbgmsg( "everything's full!  waiting for someone else to finish something" );
262        tr_condWait( gFd->cond, gFd->lock );
263    }
264
265done:
266
267    o = &gFd->open[winner];
268    if( !fileIsOpen( o ) )
269    {
270        const int ret = TrOpenFile( winner, filename, write );
271        if( ret ) {
272            tr_lockUnlock( gFd->lock );
273            return ret;
274        }
275
276        dbgmsg( "opened '%s' in slot %d, write %c", filename, winner, write?'y':'n' );
277        strlcpy( o->filename, filename, sizeof( o->filename ) );
278        o->isWritable = write;
279    }
280
281    dbgmsg( "checking out '%s' in slot %d", filename, winner );
282    o->isCheckedOut = 1;
283    o->date = tr_date();
284    tr_lockUnlock( gFd->lock );
285    return o->fd;
286}
287
288void
289tr_fdFileRelease( int file )
290{
291    int i;
292    tr_lockLock( gFd->lock );
293
294    for( i=0; i<TR_MAX_OPEN_FILES; ++i ) {
295        struct tr_openfile * o = &gFd->open[i];
296        if( o->fd == file ) {
297            dbgmsg( "releasing file '%s' in slot #%d", o->filename, i );
298            if( o->isWritable )
299                fsync( o->fd ); /* fflush */
300            o->isCheckedOut = 0;
301            break;
302        }
303    }
304   
305    tr_condSignal( gFd->cond );
306    tr_lockUnlock( gFd->lock );
307}
308
309/***
310****
311****  Sockets
312****
313***/
314
315static tr_list * reservedSockets = NULL;
316
317static void
318setSocketPriority( int fd, int isReserved )
319{
320    if( isReserved )
321        tr_list_append( &reservedSockets, TR_UINT_TO_PTR(fd) );
322}
323
324static int
325socketWasReserved( int fd )
326{
327    return tr_list_remove_data( &reservedSockets, TR_UINT_TO_PTR(fd) ) != NULL;
328}
329
330int
331tr_fdSocketCreate( int type, int priority )
332{
333    int s = -1;
334    tr_lockLock( gFd->lock );
335
336    if( priority && gFd->reserved >= TR_RESERVED_FDS )
337        priority = FALSE;
338
339    if( priority || ( gFd->normal < gFd->normalMax ) )
340        if( ( s = socket( AF_INET, type, 0 ) ) < 0 )
341            tr_err( "Couldn't create socket (%s)", strerror( sockerrno ) );
342
343    if( s > -1 )
344    {
345        setSocketPriority( s, priority );
346
347        if( priority )
348            ++gFd->reserved;
349        else
350            ++gFd->normal;
351    }
352
353    assert( gFd->reserved >= 0 );
354    assert( gFd->normal >= 0 );
355
356    tr_lockUnlock( gFd->lock );
357    return s;
358}
359
360int
361tr_fdSocketAccept( int b, struct in_addr * addr, tr_port_t * port )
362{
363    int s = -1;
364    unsigned int len;
365    struct sockaddr_in sock;
366
367    assert( addr != NULL );
368    assert( port != NULL );
369
370    tr_lockLock( gFd->lock );
371    if( gFd->normal < gFd->normalMax )
372    {
373        len = sizeof( sock );
374        s = accept( b, (struct sockaddr *) &sock, &len );
375    }
376    if( s > -1 )
377    {
378        setSocketPriority( s, 0 );
379        *addr = sock.sin_addr;
380        *port = sock.sin_port;
381        gFd->normal++;
382    }
383    tr_lockUnlock( gFd->lock );
384
385    return s;
386}
387
388static void
389socketClose( int fd )
390{
391#ifdef BEOS_NETSERVER
392    closesocket( fd );
393#else
394    EVUTIL_CLOSESOCKET( fd );
395#endif
396}
397
398void
399tr_fdSocketClose( int s )
400{
401    tr_lockLock( gFd->lock );
402
403    if( s >= 0 ) {
404        socketClose( s );
405        if( socketWasReserved( s ) )
406            --gFd->reserved;
407        else
408            --gFd->normal;
409    }
410
411    assert( gFd->reserved >= 0 );
412    assert( gFd->normal >= 0 );
413
414    tr_lockUnlock( gFd->lock );
415}
416
417/***
418****
419****  Startup / Shutdown
420****
421***/
422
423void
424tr_fdInit( void )
425{
426    int i, j, s[4096];
427
428    assert( gFd == NULL );
429
430    gFd = tr_new0( struct tr_fd_s, 1 );
431    gFd->lock = tr_lockNew( );
432    gFd->cond = tr_condNew( );
433
434    /* count the max number of sockets we can use */
435    for( i=0; i<4096; ++i )
436        if( ( s[i] = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
437            break;
438    for( j=0; j<i; ++j )
439        socketClose( s[j] );
440    tr_dbg( "%d usable file descriptors", i );
441
442    /* set some fds aside for the UI or daemon to use */
443    gFd->normalMax = i - TR_RESERVED_FDS - 10;
444
445    for( i=0; i<TR_MAX_OPEN_FILES; ++i )
446        gFd->open[i].fd = -1;
447         
448}
449
450void
451tr_fdClose( void )
452{
453    int i = 0;
454
455    for( i=0; i<TR_MAX_OPEN_FILES; ++i )
456        if( fileIsOpen( &gFd->open[i] ) )
457            TrCloseFile( i );
458
459    tr_lockFree( gFd->lock );
460    tr_condFree( gFd->cond );
461
462    tr_list_free( &reservedSockets, NULL );
463    tr_free( gFd );
464}
Note: See TracBrowser for help on using the repository browser.