source: trunk/libtransmission/fdlimit.c @ 3854

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

fix permissions issue reported by Jaybird in http://transmission.m0k.org/forum/viewtopic.php?t=2944

  • Property svn:keywords set to Date Rev Author Id
File size: 10.6 KB
Line 
1/******************************************************************************
2 * $Id: fdlimit.c 3854 2007-11-17 16:20:08Z 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    TR_MKDIR_PERM = 0777,
96
97    TR_CREAT_PERM = 0666
98};
99
100struct tr_openfile
101{
102    unsigned int  isCheckedOut : 1;
103    unsigned int  isWritable : 1;
104    char          filename[MAX_PATH_LENGTH];
105    int           fd;
106    uint64_t      date;
107};
108
109struct tr_fd_s
110{
111    int                  reserved;
112    int                  normal;
113    int                  normalMax;
114    tr_lock            * lock;
115    tr_cond            * cond;
116    struct tr_openfile   open[TR_MAX_OPEN_FILES];
117};
118
119static struct tr_fd_s * gFd = NULL;
120
121/***
122****
123****  Local Files
124****
125***/
126
127static int
128TrOpenFile( int i, const char * filename, int write )
129{
130    struct tr_openfile * file = &gFd->open[i];
131    int flags;
132
133    tr_dbg( "Opening '%s' (%d)", filename, write );
134
135    /* create subfolders, if any */
136    if( write ) {
137        char * tmp = tr_strdup( filename );
138        const int val = tr_mkdirp( dirname(tmp), TR_MKDIR_PERM );
139        tr_free( tmp );
140        if( val )
141            return tr_ioErrorFromErrno( );
142    }
143
144    /* open the file */
145    flags = write ? (O_RDWR | O_CREAT) : O_RDONLY;
146#ifdef O_LARGEFILE
147    flags |= O_LARGEFILE;
148#endif
149#ifdef WIN32
150    flags |= O_BINARY;
151#endif
152    errno = 0;
153    file->fd = open( filename, flags, TR_CREAT_PERM );
154    if( file->fd < 0 ) {
155        if( errno ) {
156            tr_err( "Couldn't open '%s': %s", filename, strerror(errno) );
157            return tr_ioErrorFromErrno();
158        } else {
159            tr_err( "Couldn't open '%s'", filename );
160            return TR_ERROR_IO_OTHER;
161        }
162    }
163
164    return TR_OK;
165}
166
167static int
168fileIsOpen( const struct tr_openfile * o )
169{
170    return o->fd >= 0;
171}
172
173static void
174TrCloseFile( int i )
175{
176    struct tr_openfile * o = &gFd->open[i];
177
178    assert( i >= 0 );
179    assert( i < TR_MAX_OPEN_FILES );
180    assert( fileIsOpen( o ) );
181
182    dbgmsg( "closing slot #%d, %s", i, o->filename );
183    close( o->fd );
184    o->fd = -1;
185    o->isCheckedOut = 0;
186    tr_condSignal( gFd->cond );
187}
188
189static int
190fileIsCheckedOut( const struct tr_openfile * o )
191{
192    return fileIsOpen(o) && o->isCheckedOut;
193}
194
195int
196tr_fdFileOpen( const char * filename, int write )
197{
198    int i, winner;
199    struct tr_openfile * o;
200
201    assert( filename && *filename );
202    assert( write==0 || write==1 );
203
204    dbgmsg( "looking for file '%s', writable %c", filename, write?'y':'n' );
205
206    tr_lockLock( gFd->lock );
207
208    /* Is it already open? */
209    for( i=0; i<TR_MAX_OPEN_FILES; ++i )
210    {
211        o = &gFd->open[i];
212
213        if( !fileIsOpen( o ) )
214            continue;
215
216        if( strcmp( filename, o->filename ) )
217            continue;
218
219        if( fileIsCheckedOut( o ) ) {
220            dbgmsg( "found it!  it's open, but checked out.  waiting..." );
221            tr_condWait( gFd->cond, gFd->lock );
222            i = -1; /* reloop */
223            continue;
224        }
225
226        if( write && !o->isWritable ) {
227            dbgmsg( "found it!  it's open and available, but isn't writable. closing..." );
228            TrCloseFile( i );
229            break;
230        }
231
232        dbgmsg( "found it!  it's ready for use!" );
233        winner = i;
234        goto done;
235    }
236
237
238    dbgmsg( "it's not already open.  looking for an open slot or an old file." );
239    for( ;; )
240    {
241        uint64_t date = tr_date( ) + 1;
242        winner = -1;
243
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                goto done;
252            }
253
254            if( date > o->date ) {
255                date = o->date;
256                winner = i;
257            }
258        }
259
260        if( winner >= 0 ) {
261            dbgmsg( "closing file '%s', slot #%d", gFd->open[winner].filename, winner );
262            TrCloseFile( winner );
263            goto done;
264        }
265
266        /* All used! Wait a bit and try again */
267        dbgmsg( "everything's full!  waiting for someone else to finish something" );
268        tr_condWait( gFd->cond, gFd->lock );
269    }
270
271done:
272
273    o = &gFd->open[winner];
274    if( !fileIsOpen( o ) )
275    {
276        const int ret = TrOpenFile( winner, filename, write );
277        if( ret ) {
278            tr_lockUnlock( gFd->lock );
279            return ret;
280        }
281
282        dbgmsg( "opened '%s' in slot %d, write %c", filename, winner, write?'y':'n' );
283        strlcpy( o->filename, filename, sizeof( o->filename ) );
284        o->isWritable = write;
285    }
286
287    dbgmsg( "checking out '%s' in slot %d", filename, winner );
288    o->isCheckedOut = 1;
289    o->date = tr_date( );
290    tr_lockUnlock( gFd->lock );
291    return o->fd;
292}
293
294void
295tr_fdFileRelease( int file )
296{
297    int i;
298    tr_lockLock( gFd->lock );
299
300    for( i=0; i<TR_MAX_OPEN_FILES; ++i ) {
301        struct tr_openfile * o = &gFd->open[i];
302        if( o->fd == file ) {
303            dbgmsg( "releasing file '%s' in slot #%d", o->filename, i );
304            /* fsync( o->fd ); */
305            o->isCheckedOut = 0;
306            break;
307        }
308    }
309   
310    tr_condSignal( gFd->cond );
311    tr_lockUnlock( gFd->lock );
312}
313
314/***
315****
316****  Sockets
317****
318***/
319
320static tr_list * reservedSockets = NULL;
321
322static void
323setSocketPriority( int fd, int isReserved )
324{
325    if( isReserved )
326        tr_list_append( &reservedSockets, TR_UINT_TO_PTR(fd) );
327}
328
329static int
330socketWasReserved( int fd )
331{
332    return tr_list_remove_data( &reservedSockets, TR_UINT_TO_PTR(fd) ) != NULL;
333}
334
335int
336tr_fdSocketCreate( int type, int isReserved )
337{
338    int s = -1;
339    tr_lockLock( gFd->lock );
340
341    if( isReserved && gFd->reserved >= TR_RESERVED_FDS )
342        isReserved = FALSE;
343
344    if( isReserved || ( gFd->normal < gFd->normalMax ) )
345        if( ( s = socket( AF_INET, type, 0 ) ) < 0 )
346            tr_err( "Couldn't create socket (%s)", strerror( sockerrno ) );
347
348    if( s > -1 )
349    {
350        setSocketPriority( s, isReserved );
351
352        if( isReserved )
353            ++gFd->reserved;
354        else
355            ++gFd->normal;
356    }
357
358    assert( gFd->reserved >= 0 );
359    assert( gFd->normal >= 0 );
360
361    tr_lockUnlock( gFd->lock );
362    return s;
363}
364
365int
366tr_fdSocketAccept( int b, struct in_addr * addr, tr_port_t * port )
367{
368    int s = -1;
369    unsigned int len;
370    struct sockaddr_in sock;
371
372    assert( addr != NULL );
373    assert( port != NULL );
374
375    tr_lockLock( gFd->lock );
376    if( gFd->normal < gFd->normalMax )
377    {
378        len = sizeof( sock );
379        s = accept( b, (struct sockaddr *) &sock, &len );
380    }
381    if( s > -1 )
382    {
383        setSocketPriority( s, FALSE );
384        *addr = sock.sin_addr;
385        *port = sock.sin_port;
386        gFd->normal++;
387    }
388    tr_lockUnlock( gFd->lock );
389
390    return s;
391}
392
393static void
394socketClose( int fd )
395{
396#ifdef BEOS_NETSERVER
397    closesocket( fd );
398#else
399    EVUTIL_CLOSESOCKET( fd );
400#endif
401}
402
403void
404tr_fdSocketClose( int s )
405{
406    tr_lockLock( gFd->lock );
407
408    if( s >= 0 ) {
409        socketClose( s );
410        if( socketWasReserved( s ) )
411            --gFd->reserved;
412        else
413            --gFd->normal;
414    }
415
416    assert( gFd->reserved >= 0 );
417    assert( gFd->normal >= 0 );
418
419    tr_lockUnlock( gFd->lock );
420}
421
422/***
423****
424****  Startup / Shutdown
425****
426***/
427
428void
429tr_fdInit( void )
430{
431    int i, j, s[TR_MAX_SOCKETS];
432
433    assert( gFd == NULL );
434
435    gFd = tr_new0( struct tr_fd_s, 1 );
436    gFd->lock = tr_lockNew( );
437    gFd->cond = tr_condNew( );
438
439    /* count the max number of sockets we can use */
440    for( i=0; i<TR_MAX_SOCKETS; ++i )
441        if( ( s[i] = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
442            break;
443    for( j=0; j<i; ++j )
444        socketClose( s[j] );
445    tr_dbg( "%d usable file descriptors", i );
446
447    /* set some fds aside for the UI or daemon to use */
448    gFd->normalMax = i - TR_RESERVED_FDS - 10;
449
450    for( i=0; i<TR_MAX_OPEN_FILES; ++i )
451        gFd->open[i].fd = -1;
452         
453}
454
455void
456tr_fdClose( void )
457{
458    int i = 0;
459
460    for( i=0; i<TR_MAX_OPEN_FILES; ++i )
461        if( fileIsOpen( &gFd->open[i] ) )
462            TrCloseFile( i );
463
464    tr_lockFree( gFd->lock );
465    tr_condFree( gFd->cond );
466
467    tr_list_free( &reservedSockets, NULL );
468    tr_free( gFd );
469}
Note: See TracBrowser for help on using the repository browser.