source: trunk/libtransmission/fdlimit.c @ 3765

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