transmission/libtransmission/fdlimit.c

523 lines
14 KiB
C
Raw Normal View History

2006-07-16 19:39:23 +00:00
/******************************************************************************
* $Id$
*
* Copyright (c) 2005-2006 Transmission authors and contributors
*
* 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.
*****************************************************************************/
2007-07-13 00:15:45 +00:00
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
2007-07-12 17:51:45 +00:00
#include <unistd.h>
#include <fcntl.h>
2006-07-16 19:39:23 +00:00
#include "transmission.h"
#define TR_MAX_OPEN_FILES 16 /* That is, real files, not sockets */
#define TR_RESERVED_FDS 16 /* Number of sockets reserved for
connections to trackers */
/***********************************************************************
* Structures
**********************************************************************/
2006-07-16 19:39:23 +00:00
typedef struct tr_openFile_s
{
2007-01-14 12:00:21 +00:00
char folder[MAX_PATH_LENGTH];
char name[MAX_PATH_LENGTH];
2006-07-16 19:39:23 +00:00
int file;
2007-01-14 12:00:21 +00:00
int write;
2006-07-16 19:39:23 +00:00
#define STATUS_INVALID 1
#define STATUS_UNUSED 2
#define STATUS_USED 4
#define STATUS_CLOSING 8
int status;
uint64_t date;
}
tr_openFile_t;
2006-07-16 19:39:23 +00:00
typedef struct tr_fd_s
2006-07-16 19:39:23 +00:00
{
tr_lock_t lock;
2007-01-14 12:00:21 +00:00
tr_cond_t cond;
2006-07-16 19:39:23 +00:00
int reserved;
int normal;
int normalMax;
tr_openFile_t open[TR_MAX_OPEN_FILES];
}
tr_fd_t;
static tr_fd_t * gFd = NULL;
2006-07-16 19:39:23 +00:00
2007-01-14 12:00:21 +00:00
/***********************************************************************
* Local prototypes
**********************************************************************/
static int OpenFile( int i, const char * folder, const char * name, int write );
static void CloseFile( int i );
2007-01-14 12:00:21 +00:00
2006-07-16 19:39:23 +00:00
/***********************************************************************
* tr_fdInit
**********************************************************************/
void tr_fdInit()
2006-07-16 19:39:23 +00:00
{
int i, j, s[4096];
if( gFd )
{
tr_err( "tr_fdInit was called before!" );
return;
}
gFd = calloc( 1, sizeof( tr_fd_t ) );
2006-07-16 19:39:23 +00:00
2007-01-14 12:00:21 +00:00
/* Init lock and cond */
tr_lockInit( &gFd->lock );
tr_condInit( &gFd->cond );
2006-07-16 19:39:23 +00:00
/* Detect the maximum number of open files or sockets */
for( i = 0; i < 4096; i++ )
{
if( ( s[i] = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
{
break;
}
}
for( j = 0; j < i; j++ )
{
2007-01-21 19:42:11 +00:00
#ifdef BEOS_NETSERVER
closesocket( s[j] );
#else
close( s[j] );
#endif
2006-07-16 19:39:23 +00:00
}
tr_dbg( "%d usable file descriptors", i );
gFd->reserved = 0;
gFd->normal = 0;
2006-07-16 19:39:23 +00:00
gFd->normalMax = i - TR_RESERVED_FDS - 10;
2006-07-16 19:39:23 +00:00
/* To be safe, in case the UI needs to write a preferences file
or something */
for( i = 0; i < TR_MAX_OPEN_FILES; i++ )
{
gFd->open[i].status = STATUS_INVALID;
2006-07-16 19:39:23 +00:00
}
}
/***********************************************************************
* tr_fdFileOpen
**********************************************************************/
int tr_fdFileOpen( const char * folder, const char * name, int write )
2006-07-16 19:39:23 +00:00
{
2007-01-14 12:00:21 +00:00
int i, winner, ret;
2006-07-16 19:39:23 +00:00
uint64_t date;
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++ )
{
if( gFd->open[i].status & STATUS_INVALID ||
strcmp( folder, gFd->open[i].folder ) ||
strcmp( name, gFd->open[i].name ) )
2006-07-16 19:39:23 +00:00
{
2007-01-14 12:00:21 +00:00
continue;
}
if( gFd->open[i].status & STATUS_CLOSING )
2007-01-14 12:00:21 +00:00
{
/* File is being closed by another thread, wait until
* it's done before we reopen it */
tr_condWait( &gFd->cond, &gFd->lock );
2007-01-14 12:00:21 +00:00
i = -1;
continue;
}
if( gFd->open[i].write < write )
2007-01-14 12:00:21 +00:00
{
/* File is open read-only and needs to be closed then
* re-opened read-write */
CloseFile( i );
2007-01-14 12:00:21 +00:00
continue;
2006-07-16 19:39:23 +00:00
}
2007-01-14 12:00:21 +00:00
winner = i;
goto done;
2006-07-16 19:39:23 +00:00
}
/* Can we open one more file? */
for( i = 0; i < TR_MAX_OPEN_FILES; i++ )
{
if( gFd->open[i].status & STATUS_INVALID )
2006-07-16 19:39:23 +00:00
{
winner = i;
goto open;
}
}
2007-01-14 12:00:21 +00:00
/* All slots taken - close the oldest currently unused file */
2006-07-16 19:39:23 +00:00
for( ;; )
{
date = tr_date() + 1;
winner = -1;
for( i = 0; i < TR_MAX_OPEN_FILES; i++ )
{
if( !( gFd->open[i].status & STATUS_UNUSED ) )
2006-07-16 19:39:23 +00:00
{
continue;
}
if( gFd->open[i].date < date )
2006-07-16 19:39:23 +00:00
{
winner = i;
date = gFd->open[i].date;
2006-07-16 19:39:23 +00:00
}
}
if( winner >= 0 )
{
CloseFile( winner );
2006-07-16 19:39:23 +00:00
goto open;
}
/* All used! Wait a bit and try again */
tr_condWait( &gFd->cond, &gFd->lock );
2006-07-16 19:39:23 +00:00
}
open:
if( ( ret = OpenFile( winner, folder, name, write ) ) )
2007-01-14 12:00:21 +00:00
{
tr_lockUnlock( &gFd->lock );
2007-01-14 12:00:21 +00:00
return ret;
}
snprintf( gFd->open[winner].folder, MAX_PATH_LENGTH, "%s", folder );
snprintf( gFd->open[winner].name, MAX_PATH_LENGTH, "%s", name );
gFd->open[winner].write = write;
2006-07-16 19:39:23 +00:00
done:
gFd->open[winner].status = STATUS_USED;
gFd->open[winner].date = tr_date();
tr_lockUnlock( &gFd->lock );
2006-07-16 19:39:23 +00:00
return gFd->open[winner].file;
2006-07-16 19:39:23 +00:00
}
/***********************************************************************
* tr_fdFileRelease
**********************************************************************/
void tr_fdFileRelease( int file )
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++ )
{
if( gFd->open[i].file == file )
2006-07-16 19:39:23 +00:00
{
gFd->open[i].status = STATUS_UNUSED;
2006-07-16 19:39:23 +00:00
break;
}
}
tr_condSignal( &gFd->cond );
tr_lockUnlock( &gFd->lock );
2006-07-16 19:39:23 +00:00
}
/***********************************************************************
* tr_fdFileClose
**********************************************************************/
void tr_fdFileClose( const char * folder, const char * name )
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++ )
{
if( gFd->open[i].status & STATUS_INVALID )
2006-07-16 19:39:23 +00:00
{
continue;
}
if( !strcmp( folder, gFd->open[i].folder ) &&
!strcmp( name, gFd->open[i].name ) )
2006-07-16 19:39:23 +00:00
{
CloseFile( i );
2006-07-16 19:39:23 +00:00
}
}
tr_lockUnlock( &gFd->lock );
2006-07-16 19:39:23 +00:00
}
2007-01-21 19:42:11 +00:00
2007-01-14 12:00:21 +00:00
/***********************************************************************
2007-01-21 19:42:11 +00:00
* Sockets
2007-01-14 12:00:21 +00:00
**********************************************************************/
2007-01-21 19:42:11 +00:00
typedef struct
{
int socket;
int priority;
}
tr_socket_t;
/* Remember the priority of every socket we open, so that we can keep
* track of how many reserved file descriptors we are using */
static tr_socket_t * gSockets = NULL;
static int gSocketsSize = 0;
static int gSocketsCount = 0;
static void SocketSetPriority( int s, int priority )
2006-07-16 19:39:23 +00:00
{
2007-01-21 19:42:11 +00:00
if( gSocketsSize < 1 )
{
gSocketsSize = 256;
gSockets = malloc( gSocketsSize * sizeof( tr_socket_t ) );
}
if( gSocketsSize <= gSocketsCount )
{
gSocketsSize *= 2;
gSockets = realloc( gSockets, gSocketsSize * sizeof( tr_socket_t ) );
}
gSockets[gSocketsCount].socket = s;
gSockets[gSocketsCount].priority = priority;
gSocketsCount++;
}
static int SocketGetPriority( int s )
{
int i, ret;
for( i = 0; i < gSocketsCount; i++ )
if( gSockets[i].socket == s )
break;
if( i >= gSocketsCount )
{
tr_err( "could not find that socket (%d)!", s );
return -1;
}
ret = gSockets[i].priority;
gSocketsCount--;
memmove( &gSockets[i], &gSockets[i+1],
( gSocketsCount - i ) * sizeof( tr_socket_t ) );
return ret;
2007-01-21 19:42:11 +00:00
}
2006-07-16 19:39:23 +00:00
2007-01-21 19:42:11 +00:00
/***********************************************************************
* tr_fdSocketCreate
**********************************************************************/
int tr_fdSocketCreate( int type, int priority )
{
int s = -1;
2006-07-16 19:39:23 +00:00
2007-01-21 19:42:11 +00:00
tr_lockLock( &gFd->lock );
if( priority && gFd->reserved >= TR_RESERVED_FDS )
priority = FALSE;
if( priority || ( gFd->normal < gFd->normalMax ) )
2007-01-21 19:42:11 +00:00
if( ( s = socket( AF_INET, type, 0 ) ) < 0 )
tr_err( "Could not create socket (%s)", strerror( errno ) );
2007-01-21 19:42:11 +00:00
if( s > -1 )
2006-07-16 19:39:23 +00:00
{
2007-01-21 19:42:11 +00:00
SocketSetPriority( s, priority );
if( priority )
gFd->reserved++;
2006-07-16 19:39:23 +00:00
else
2007-01-21 19:42:11 +00:00
gFd->normal++;
2006-07-16 19:39:23 +00:00
}
tr_lockUnlock( &gFd->lock );
2006-07-16 19:39:23 +00:00
2007-01-21 19:42:11 +00:00
return s;
2006-07-16 19:39:23 +00:00
}
2007-01-21 19:42:11 +00:00
int tr_fdSocketAccept( int b, struct in_addr * addr, in_port_t * port )
2006-07-16 19:39:23 +00:00
{
2007-01-21 19:42:11 +00:00
int s = -1;
unsigned len;
struct sockaddr_in sock;
2006-07-16 19:39:23 +00:00
2007-01-21 19:42:11 +00:00
tr_lockLock( &gFd->lock );
if( gFd->normal < gFd->normalMax )
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
{
2007-01-21 19:42:11 +00:00
SocketSetPriority( s, 0 );
if( NULL != addr )
{
*addr = sock.sin_addr;
}
if( NULL != port )
{
*port = sock.sin_port;
}
2007-01-21 19:42:11 +00:00
gFd->normal++;
2006-07-16 19:39:23 +00:00
}
2007-01-21 19:42:11 +00:00
tr_lockUnlock( &gFd->lock );
return s;
}
2006-07-16 19:39:23 +00:00
2007-01-21 19:42:11 +00:00
/***********************************************************************
* tr_fdSocketClose
**********************************************************************/
void tr_fdSocketClose( int s )
{
tr_lockLock( &gFd->lock );
#ifdef BEOS_NETSERVER
closesocket( s );
#else
close( s );
#endif
if( SocketGetPriority( s ) )
gFd->reserved--;
else
gFd->normal--;
tr_lockUnlock( &gFd->lock );
2006-07-16 19:39:23 +00:00
}
2007-01-14 12:00:21 +00:00
/***********************************************************************
* tr_fdClose
**********************************************************************/
void tr_fdClose()
2006-07-16 19:39:23 +00:00
{
tr_lockClose( &gFd->lock );
tr_condClose( &gFd->cond );
free( gFd );
2006-07-16 19:39:23 +00:00
}
2007-01-14 12:00:21 +00:00
/***********************************************************************
* Local functions
**********************************************************************/
/***********************************************************************
* CheckFolder
***********************************************************************
*
**********************************************************************/
static int OpenFile( int i, const char * folder, const char * name, int write )
2007-01-14 12:00:21 +00:00
{
tr_openFile_t * file = &gFd->open[i];
2007-01-14 12:00:21 +00:00
struct stat sb;
char path[MAX_PATH_LENGTH];
2007-04-20 02:05:07 +00:00
int ret;
2007-01-14 12:00:21 +00:00
tr_dbg( "Opening %s in %s (%d)", name, folder, write );
/* Make sure the parent folder exists */
if( stat( folder, &sb ) || !S_ISDIR( sb.st_mode ) )
{
return TR_ERROR_IO_PARENT;
}
snprintf( path, sizeof(path), "%s" TR_PATH_DELIMITER_STR "%s",
folder,
name );
2007-01-14 12:00:21 +00:00
/* Create subfolders, if any */
if( write )
{
char * p = path + strlen( folder ) + 1;
char * s;
while( ( s = strchr( p, '/' ) ) )
{
*s = '\0';
if( stat( path, &sb ) )
{
if( mkdir( path, 0777 ) )
{
2007-04-20 02:05:07 +00:00
ret = tr_ioErrorFromErrno();
2007-01-19 11:19:20 +00:00
tr_err( "Could not create folder '%s'", path );
2007-04-20 02:05:07 +00:00
return ret;
2007-01-14 12:00:21 +00:00
}
}
else
{
if( !S_ISDIR( sb.st_mode ) )
{
2007-01-19 11:19:20 +00:00
tr_err( "Is not a folder: '%s'", path );
2007-01-14 12:00:21 +00:00
return TR_ERROR_IO_OTHER;
}
}
*s = '/';
p = s + 1;
}
}
/* Now try to really open the file */
errno = 0;
2007-01-14 12:00:21 +00:00
file->file = open( path, write ? ( O_RDWR | O_CREAT ) : O_RDONLY, 0666 );
if( write && ( file->file < 0 ) )
2007-01-14 12:00:21 +00:00
{
2007-04-20 02:05:07 +00:00
ret = tr_ioErrorFromErrno();
if( errno )
tr_err( "Couldn't open %s in %s: %s", name, folder, strerror(errno) );
else
tr_err( "Couldn't open %s in %s", name, folder );
2007-01-14 12:00:21 +00:00
return ret;
}
return TR_OK;
}
/***********************************************************************
* CloseFile
***********************************************************************
* We first mark it as closing then release the lock while doing so,
* because close() may take same time and we don't want to block other
* threads.
**********************************************************************/
static void CloseFile( int i )
2007-01-14 12:00:21 +00:00
{
tr_openFile_t * file = &gFd->open[i];
2007-01-14 12:00:21 +00:00
/* If it's already being closed by another thread, just wait till
* it is done */
while( file->status & STATUS_CLOSING )
{
tr_condWait( &gFd->cond, &gFd->lock );
2007-01-14 12:00:21 +00:00
}
if( file->status & STATUS_INVALID )
{
return;
}
/* Nobody is closing it already, so let's do it */
if( file->status & STATUS_USED )
{
tr_err( "CloseFile: closing a file that's being used!" );
}
tr_dbg( "Closing %s in %s (%d)", file->name, file->folder, file->write );
file->status = STATUS_CLOSING;
tr_lockUnlock( &gFd->lock );
2007-01-14 12:00:21 +00:00
close( file->file );
tr_lockLock( &gFd->lock );
2007-01-14 12:00:21 +00:00
file->status = STATUS_INVALID;
tr_condSignal( &gFd->cond );
2007-01-14 12:00:21 +00:00
}