transmission/libtransmission/fdlimit.c

499 lines
12 KiB
C
Raw Normal View History

2006-07-16 19:39:23 +00:00
/******************************************************************************
* $Id$
*
2008-01-01 17:20:20 +00:00
* Copyright (c) 2005-2008 Transmission authors and contributors
2006-07-16 19:39:23 +00:00
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*****************************************************************************/
#ifndef WIN32
#define HAVE_GETRLIMIT
#endif
2007-11-09 20:07:52 +00:00
#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2007-07-13 00:15:45 +00:00
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_GETRLIMIT
#include <sys/time.h> /* getrlimit */
#include <sys/resource.h> /* getrlimit */
#endif
2007-07-12 17:51:45 +00:00
#include <unistd.h>
#include <libgen.h> /* dirname */
#include <fcntl.h> /* O_LARGEFILE */
#include <event.h>
#include <evutil.h>
2006-07-16 19:39:23 +00:00
#include "transmission.h"
#include "list.h"
#include "net.h"
2008-02-19 18:39:49 +00:00
#include "platform.h" /* tr_lock */
#include "utils.h"
2006-07-16 19:39:23 +00:00
#if SIZEOF_VOIDP==8
#define TR_UINT_TO_PTR(i) (void*)((uint64_t)i)
#else
#define TR_UINT_TO_PTR(i) ((void*)((uint32_t)i))
#endif
#define dbgmsg(fmt...) tr_deepLog( __FILE__, __LINE__, NULL, ##fmt )
/**
***
**/
2006-07-16 19:39:23 +00:00
enum
{
TR_MAX_OPEN_FILES = 16, /* real files, not sockets */
2007-01-14 12:00:21 +00:00
NOFILE_BUFFER = 512, /* the process' number of open files is
globalMaxPeers + NOFILE_BUFFER */
};
2007-01-14 12:00:21 +00:00
struct tr_openfile
2006-07-16 19:39:23 +00:00
{
unsigned int isCheckedOut : 1;
unsigned int isWritable : 1;
unsigned int closeWhenDone : 1;
char filename[MAX_PATH_LENGTH];
int fd;
uint64_t date;
};
struct tr_fd_s
{
int reserved;
int normal;
int normalMax;
tr_lock * lock;
struct tr_openfile open[TR_MAX_OPEN_FILES];
};
static struct tr_fd_s * gFd = NULL;
/***
****
**** Local Files
****
***/
static tr_errno
TrOpenFile( int i,
const char * folder,
const char * torrentFile,
int write )
{
struct tr_openfile * file = &gFd->open[i];
int flags;
char filename[MAX_PATH_LENGTH];
struct stat sb;
/* confirm the parent folder exists */
if( stat( folder, &sb ) || !S_ISDIR( sb.st_mode ) )
return TR_ERROR_IO_PARENT;
/* create subfolders, if any */
tr_buildPath ( filename, sizeof(filename), folder, torrentFile, NULL );
if( write ) {
2007-11-02 20:27:03 +00:00
char * tmp = tr_strdup( filename );
const int err = tr_mkdirp( dirname(tmp), 0777 ) ? errno : 0;
2007-11-02 20:27:03 +00:00
tr_free( tmp );
if( err )
return tr_ioErrorFromErrno( err );
}
2006-07-16 19:39:23 +00:00
/* open the file */
flags = write ? (O_RDWR | O_CREAT) : O_RDONLY;
#ifdef O_LARGEFILE
flags |= O_LARGEFILE;
#endif
#ifdef WIN32
flags |= O_BINARY;
2007-01-21 19:42:11 +00:00
#endif
file->fd = open( filename, flags, 0666 );
if( file->fd == -1 ) {
const int err = errno;
2008-03-30 13:52:55 +00:00
tr_err( _( "Couldn't open \"%1$s\": %2$s" ), filename, tr_strerror(err) );
return tr_ioErrorFromErrno( err );
2006-07-16 19:39:23 +00:00
}
return TR_OK;
}
static int
fileIsOpen( const struct tr_openfile * o )
{
return o->fd >= 0;
}
2006-07-16 19:39:23 +00:00
static void
TrCloseFile( int i )
{
struct tr_openfile * o = &gFd->open[i];
2006-07-16 19:39:23 +00:00
assert( i >= 0 );
assert( i < TR_MAX_OPEN_FILES );
assert( fileIsOpen( o ) );
2006-07-16 19:39:23 +00:00
close( o->fd );
o->fd = -1;
o->isCheckedOut = 0;
}
static int
fileIsCheckedOut( const struct tr_openfile * o )
{
return fileIsOpen(o) && o->isCheckedOut;
2006-07-16 19:39:23 +00:00
}
int
tr_fdFileCheckout( const char * folder,
const char * torrentFile,
int write )
2006-07-16 19:39:23 +00:00
{
int i, winner = -1;
struct tr_openfile * o;
char filename[MAX_PATH_LENGTH];
assert( folder && *folder );
assert( torrentFile && *torrentFile );
assert( write==0 || write==1 );
tr_buildPath ( filename, sizeof(filename), folder, torrentFile, NULL );
dbgmsg( "looking for file '%s', writable %c", filename, write?'y':'n' );
2006-07-16 19:39:23 +00:00
tr_lockLock( gFd->lock );
2006-07-16 19:39:23 +00:00
/* Is it already open? */
for( i=0; i<TR_MAX_OPEN_FILES; ++i )
2006-07-16 19:39:23 +00:00
{
o = &gFd->open[i];
if( !fileIsOpen( o ) )
2007-01-14 12:00:21 +00:00
continue;
if( strcmp( filename, o->filename ) )
continue;
if( fileIsCheckedOut( o ) ) {
dbgmsg( "found it! it's open, but checked out. waiting..." );
tr_lockUnlock( gFd->lock );
tr_wait( 200 );
tr_lockLock( gFd->lock );
i = -1; /* reloop */
2007-01-14 12:00:21 +00:00
continue;
}
if( write && !o->isWritable ) {
dbgmsg( "found it! it's open and available, but isn't writable. closing..." );
2007-08-01 00:40:49 +00:00
TrCloseFile( i );
break;
2006-07-16 19:39:23 +00:00
}
dbgmsg( "found it! it's ready for use!" );
2007-01-14 12:00:21 +00:00
winner = i;
break;
2006-07-16 19:39:23 +00:00
}
dbgmsg( "it's not already open. looking for an open slot or an old file." );
while( winner < 0 )
2006-07-16 19:39:23 +00:00
{
uint64_t date = tr_date( ) + 1;
2006-07-16 19:39:23 +00:00
/* look for the file that's been open longest */
for( i=0; i<TR_MAX_OPEN_FILES; ++i )
2006-07-16 19:39:23 +00:00
{
o = &gFd->open[i];
if( !fileIsOpen( o ) ) {
winner = i;
dbgmsg( "found an empty slot in %d", winner );
break;
2006-07-16 19:39:23 +00:00
}
if( date > o->date ) {
date = o->date;
winner = i;
2006-07-16 19:39:23 +00:00
}
}
if( winner >= 0 ) {
if( fileIsOpen( &gFd->open[winner] ) ) {
2007-12-03 16:51:22 +00:00
dbgmsg( "closing file '%s', slot #%d", gFd->open[winner].filename, winner );
TrCloseFile( winner );
}
} else {
dbgmsg( "everything's full! waiting for someone else to finish something" );
tr_lockUnlock( gFd->lock );
tr_wait( 200 );
tr_lockLock( gFd->lock );
2006-07-16 19:39:23 +00:00
}
}
assert( winner >= 0 );
o = &gFd->open[winner];
if( !fileIsOpen( o ) )
2007-01-14 12:00:21 +00:00
{
const tr_errno err = TrOpenFile( winner, folder, torrentFile, write );
if( err ) {
tr_lockUnlock( gFd->lock );
return err;
}
dbgmsg( "opened '%s' in slot %d, write %c", filename, winner, write?'y':'n' );
2008-05-18 16:44:30 +00:00
tr_strlcpy( o->filename, filename, sizeof( o->filename ) );
o->isWritable = write;
2007-01-14 12:00:21 +00:00
}
2006-07-16 19:39:23 +00:00
dbgmsg( "checking out '%s' in slot %d", filename, winner );
o->isCheckedOut = 1;
o->closeWhenDone = 0;
o->date = tr_date( );
tr_lockUnlock( gFd->lock );
return o->fd;
2006-07-16 19:39:23 +00:00
}
void
tr_fdFileReturn( int fd )
2006-07-16 19:39:23 +00:00
{
int i;
tr_lockLock( gFd->lock );
2006-07-16 19:39:23 +00:00
for( i=0; i<TR_MAX_OPEN_FILES; ++i )
{
struct tr_openfile * o = &gFd->open[i];
if( o->fd != fd )
continue;
dbgmsg( "releasing file '%s' in slot #%d", o->filename, i );
o->isCheckedOut = 0;
if( o->closeWhenDone )
TrCloseFile( i );
break;
}
tr_lockUnlock( gFd->lock );
}
void
tr_fdFileClose( const char * filename )
{
int i;
tr_lockLock( gFd->lock );
for( i=0; i<TR_MAX_OPEN_FILES; ++i )
{
struct tr_openfile * o = &gFd->open[i];
if( !fileIsOpen(o) || strcmp(filename,o->filename) )
continue;
2008-01-08 20:08:45 +00:00
dbgmsg( "tr_fdFileClose closing '%s'", filename );
if( !o->isCheckedOut ) {
dbgmsg( "not checked out, so closing it now... '%s'", filename );
TrCloseFile( i );
} else {
dbgmsg( "flagging file '%s', slot #%d to be closed when checked in", gFd->open[i].filename, i );
o->closeWhenDone = 1;
2006-07-16 19:39:23 +00:00
}
}
tr_lockUnlock( gFd->lock );
2006-07-16 19:39:23 +00:00
}
/***
****
**** Sockets
****
***/
2007-01-21 19:42:11 +00:00
static tr_list * reservedSockets = NULL;
static void
setSocketPriority( int fd, int isReserved )
2006-07-16 19:39:23 +00:00
{
if( isReserved )
tr_list_append( &reservedSockets, TR_UINT_TO_PTR(fd) );
2007-01-21 19:42:11 +00:00
}
static int
socketWasReserved( int fd )
2007-01-21 19:42:11 +00:00
{
return tr_list_remove_data( &reservedSockets, TR_UINT_TO_PTR(fd) ) != NULL;
2007-01-21 19:42:11 +00:00
}
2006-07-16 19:39:23 +00:00
static int
getSocketMax( struct tr_fd_s * gFd )
{
return gFd->normalMax;
}
int
tr_fdSocketCreate( int type, int isReserved )
2007-01-21 19:42:11 +00:00
{
int s = -1;
tr_lockLock( gFd->lock );
if( isReserved || ( gFd->normal < getSocketMax( gFd ) ) )
if( ( s = socket( AF_INET, type, 0 ) ) < 0 )
tr_err( _( "Couldn't create socket: %s" ), tr_strerror( sockerrno ) );
2007-01-21 19:42:11 +00:00
if( s > -1 )
2006-07-16 19:39:23 +00:00
{
setSocketPriority( s, isReserved );
if( isReserved )
++gFd->reserved;
2006-07-16 19:39:23 +00:00
else
++gFd->normal;
2006-07-16 19:39:23 +00:00
}
assert( gFd->reserved >= 0 );
assert( gFd->normal >= 0 );
tr_lockUnlock( gFd->lock );
2007-01-21 19:42:11 +00:00
return s;
2006-07-16 19:39:23 +00:00
}
int
tr_fdSocketAccept( int b, struct in_addr * addr, tr_port_t * port )
2006-07-16 19:39:23 +00:00
{
2007-01-21 19:42:11 +00:00
int s = -1;
unsigned int len;
2007-01-21 19:42:11 +00:00
struct sockaddr_in sock;
2006-07-16 19:39:23 +00:00
assert( addr != NULL );
assert( port != NULL );
tr_lockLock( gFd->lock );
if( gFd->normal < getSocketMax( gFd ) )
2006-07-16 19:39:23 +00:00
{
2007-01-21 19:42:11 +00:00
len = sizeof( sock );
s = accept( b, (struct sockaddr *) &sock, &len );
2006-07-16 19:39:23 +00:00
}
2007-01-21 19:42:11 +00:00
if( s > -1 )
2006-07-16 19:39:23 +00:00
{
setSocketPriority( s, FALSE );
*addr = sock.sin_addr;
*port = sock.sin_port;
2007-01-21 19:42:11 +00:00
gFd->normal++;
2006-07-16 19:39:23 +00:00
}
tr_lockUnlock( gFd->lock );
2007-01-21 19:42:11 +00:00
return s;
}
2006-07-16 19:39:23 +00:00
static void
socketClose( int fd )
2007-01-21 19:42:11 +00:00
{
#ifdef BEOS_NETSERVER
closesocket( fd );
2007-01-21 19:42:11 +00:00
#else
EVUTIL_CLOSESOCKET( fd );
2007-01-21 19:42:11 +00:00
#endif
}
void
tr_fdSocketClose( int s )
{
tr_lockLock( gFd->lock );
if( s >= 0 ) {
socketClose( s );
if( socketWasReserved( s ) )
--gFd->reserved;
else
--gFd->normal;
}
assert( gFd->reserved >= 0 );
assert( gFd->normal >= 0 );
tr_lockUnlock( gFd->lock );
2006-07-16 19:39:23 +00:00
}
/***
****
**** Startup / Shutdown
****
***/
2007-01-14 12:00:21 +00:00
void
tr_fdInit( int globalPeerLimit )
2007-01-14 12:00:21 +00:00
{
int i;
2007-01-14 12:00:21 +00:00
assert( gFd == NULL );
gFd = tr_new0( struct tr_fd_s, 1 );
gFd->lock = tr_lockNew( );
2007-01-14 12:00:21 +00:00
#ifdef HAVE_GETRLIMIT
{
struct rlimit rlim;
getrlimit( RLIMIT_NOFILE, &rlim );
rlim.rlim_cur = MIN( rlim.rlim_max,
(rlim_t)(globalPeerLimit + NOFILE_BUFFER) );
setrlimit( RLIMIT_NOFILE, &rlim );
gFd->normalMax = rlim.rlim_cur - NOFILE_BUFFER;
2008-02-25 03:09:55 +00:00
tr_dbg( "setrlimit( RLIMIT_NOFILE, %d )", (int)rlim.rlim_cur );
}
#else
gFd->normalMax = globalPeerLimit;
#endif
tr_dbg( "%d usable file descriptors", globalPeerLimit );
2007-01-14 12:00:21 +00:00
for( i=0; i<TR_MAX_OPEN_FILES; ++i )
gFd->open[i].fd = -1;
2007-01-14 12:00:21 +00:00
}
void
tr_fdClose( void )
2007-01-14 12:00:21 +00:00
{
int i = 0;
2007-01-14 12:00:21 +00:00
for( i=0; i<TR_MAX_OPEN_FILES; ++i )
if( fileIsOpen( &gFd->open[i] ) )
TrCloseFile( i );
2007-01-14 12:00:21 +00:00
tr_lockFree( gFd->lock );
2007-01-14 12:00:21 +00:00
tr_list_free( &reservedSockets, NULL );
tr_free( gFd );
}
void
tr_fdSetPeerLimit( uint16_t n )
{
assert( gFd!=NULL && "tr_fdInit() must be called first!" );
gFd->normalMax = n;
}
uint16_t
tr_fdGetPeerLimit( void )
{
return gFd ? gFd->normalMax : -1;
}