source: trunk/libtransmission/fdlimit.c @ 3650

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

part 1 of the data corruption fix: plug the leaky abstraction in fdlimit

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