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.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2008-01-10 19:22:11 +00:00
|
|
|
#ifndef WIN32
|
2008-09-23 19:11:04 +00:00
|
|
|
#define HAVE_GETRLIMIT
|
2008-01-10 19:22:11 +00:00
|
|
|
#endif
|
|
|
|
|
2007-11-09 20:07:52 +00:00
|
|
|
#include <assert.h>
|
2007-07-29 18:11:21 +00:00
|
|
|
#include <errno.h>
|
2007-10-31 18:10:55 +00:00
|
|
|
#include <inttypes.h>
|
2007-07-29 18:11:21 +00:00
|
|
|
#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>
|
2008-01-10 19:22:11 +00:00
|
|
|
#ifdef HAVE_GETRLIMIT
|
2008-09-23 19:11:04 +00:00
|
|
|
#include <sys/time.h> /* getrlimit */
|
|
|
|
#include <sys/resource.h> /* getrlimit */
|
2008-01-10 19:22:11 +00:00
|
|
|
#endif
|
2007-07-12 17:51:45 +00:00
|
|
|
#include <unistd.h>
|
2007-11-09 04:32:19 +00:00
|
|
|
#include <fcntl.h> /* O_LARGEFILE */
|
2007-07-14 16:29:21 +00:00
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
#include <event.h>
|
2007-09-25 23:10:34 +00:00
|
|
|
#include <evutil.h>
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "transmission.h"
|
2008-08-11 19:05:02 +00:00
|
|
|
#include "fdlimit.h"
|
2007-10-31 18:10:55 +00:00
|
|
|
#include "list.h"
|
2007-07-31 14:26:44 +00:00
|
|
|
#include "net.h"
|
2008-02-19 18:39:49 +00:00
|
|
|
#include "platform.h" /* tr_lock */
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "utils.h"
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-10-13 22:45:05 +00:00
|
|
|
#define dbgmsg( ... ) tr_deepLog( __FILE__, __LINE__, NULL, __VA_ARGS__ )
|
2007-01-21 07:16:18 +00:00
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
enum
|
|
|
|
{
|
2007-12-15 22:22:30 +00:00
|
|
|
TR_MAX_OPEN_FILES = 16, /* real files, not sockets */
|
2007-01-14 12:00:21 +00:00
|
|
|
|
2008-01-03 16:36:20 +00:00
|
|
|
NOFILE_BUFFER = 512, /* the process' number of open files is
|
|
|
|
globalMaxPeers + NOFILE_BUFFER */
|
2007-10-30 18:35:06 +00:00
|
|
|
};
|
2007-01-14 12:00:21 +00:00
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
struct tr_openfile
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
unsigned int isCheckedOut : 1;
|
|
|
|
unsigned int isWritable : 1;
|
|
|
|
unsigned int closeWhenDone : 1;
|
|
|
|
char filename[MAX_PATH_LENGTH];
|
|
|
|
int fd;
|
|
|
|
uint64_t date;
|
2007-10-30 18:35:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct tr_fd_s
|
|
|
|
{
|
2008-10-14 20:44:41 +00:00
|
|
|
int socketCount;
|
|
|
|
int socketMax;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_lock * lock;
|
|
|
|
struct tr_openfile open[TR_MAX_OPEN_FILES];
|
2007-10-30 18:35:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct tr_fd_s * gFd = NULL;
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
**** Local Files
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-10-03 04:49:06 +00:00
|
|
|
/**
|
|
|
|
* returns 0 on success, or an errno value on failure.
|
|
|
|
* errno values include ENOENT if the parent folder doesn't exist,
|
|
|
|
* plus the errno values set by tr_mkdirp() and open().
|
|
|
|
*/
|
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
TrOpenFile( int i,
|
|
|
|
const char * folder,
|
|
|
|
const char * torrentFile,
|
|
|
|
int write )
|
2007-10-30 18:35:06 +00:00
|
|
|
{
|
|
|
|
struct tr_openfile * file = &gFd->open[i];
|
2008-09-23 19:11:04 +00:00
|
|
|
int flags;
|
2008-10-14 03:03:29 +00:00
|
|
|
char * filename;
|
2008-09-23 19:11:04 +00:00
|
|
|
struct stat sb;
|
2008-01-18 19:13:32 +00:00
|
|
|
|
|
|
|
/* confirm the parent folder exists */
|
|
|
|
if( stat( folder, &sb ) || !S_ISDIR( sb.st_mode ) )
|
2008-10-03 04:49:06 +00:00
|
|
|
return ENOENT;
|
2007-01-21 07:16:18 +00:00
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
/* create subfolders, if any */
|
2008-10-14 03:03:29 +00:00
|
|
|
filename = tr_buildPath( folder, torrentFile, NULL );
|
2008-09-23 19:11:04 +00:00
|
|
|
if( write )
|
|
|
|
{
|
2008-10-14 03:39:16 +00:00
|
|
|
char * tmp = tr_dirname( filename );
|
|
|
|
const int err = tr_mkdirp( tmp, 0777 ) ? errno : 0;
|
2007-11-02 20:27:03 +00:00
|
|
|
tr_free( tmp );
|
2008-10-14 04:51:42 +00:00
|
|
|
if( err ) {
|
|
|
|
tr_free( filename );
|
2008-10-03 04:49:06 +00:00
|
|
|
return err;
|
2008-10-14 04:51:42 +00:00
|
|
|
}
|
2007-10-30 18:35:06 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-10-31 18:10:55 +00:00
|
|
|
/* open the file */
|
2008-09-23 19:11:04 +00:00
|
|
|
flags = write ? ( O_RDWR | O_CREAT ) : O_RDONLY;
|
2007-11-09 04:32:19 +00:00
|
|
|
#ifdef O_LARGEFILE
|
|
|
|
flags |= O_LARGEFILE;
|
|
|
|
#endif
|
2007-10-30 18:35:06 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
flags |= O_BINARY;
|
2007-01-21 19:42:11 +00:00
|
|
|
#endif
|
2007-11-19 12:59:26 +00:00
|
|
|
file->fd = open( filename, flags, 0666 );
|
2008-09-23 19:11:04 +00:00
|
|
|
if( file->fd == -1 )
|
|
|
|
{
|
2008-01-18 19:13:32 +00:00
|
|
|
const int err = errno;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_err( _( "Couldn't open \"%1$s\": %2$s" ), filename,
|
|
|
|
tr_strerror( err ) );
|
2008-10-14 03:03:29 +00:00
|
|
|
tr_free( filename );
|
2008-10-03 04:49:06 +00:00
|
|
|
return err;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2008-10-14 03:03:29 +00:00
|
|
|
tr_free( filename );
|
2008-10-03 04:49:06 +00:00
|
|
|
return 0;
|
2007-10-30 18:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
fileIsOpen( const struct tr_openfile * o )
|
|
|
|
{
|
2007-10-31 18:10:55 +00:00
|
|
|
return o->fd >= 0;
|
2007-10-30 18:35:06 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
static void
|
|
|
|
TrCloseFile( int i )
|
|
|
|
{
|
2007-10-31 18:10:55 +00:00
|
|
|
struct tr_openfile * o = &gFd->open[i];
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
assert( i >= 0 );
|
|
|
|
assert( i < TR_MAX_OPEN_FILES );
|
2007-10-31 18:10:55 +00:00
|
|
|
assert( fileIsOpen( o ) );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-10-31 18:10:55 +00:00
|
|
|
close( o->fd );
|
|
|
|
o->fd = -1;
|
|
|
|
o->isCheckedOut = 0;
|
2007-10-30 18:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
fileIsCheckedOut( const struct tr_openfile * o )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
return fileIsOpen( o ) && o->isCheckedOut;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2008-10-03 04:49:06 +00:00
|
|
|
/* returns an fd on success, or a -1 on failure and sets errno */
|
2007-10-30 18:35:06 +00:00
|
|
|
int
|
2008-01-18 19:13:32 +00:00
|
|
|
tr_fdFileCheckout( const char * folder,
|
2008-09-23 19:11:04 +00:00
|
|
|
const char * torrentFile,
|
2008-01-18 19:13:32 +00:00
|
|
|
int write )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int i, winner = -1;
|
2007-10-30 18:35:06 +00:00
|
|
|
struct tr_openfile * o;
|
2008-10-14 03:03:29 +00:00
|
|
|
char * filename;
|
2007-10-30 18:35:06 +00:00
|
|
|
|
2008-01-18 19:13:32 +00:00
|
|
|
assert( folder && *folder );
|
|
|
|
assert( torrentFile && *torrentFile );
|
2008-09-23 19:11:04 +00:00
|
|
|
assert( write == 0 || write == 1 );
|
2007-10-31 18:10:55 +00:00
|
|
|
|
2008-10-14 03:03:29 +00:00
|
|
|
filename = tr_buildPath( folder, torrentFile, NULL );
|
2008-09-23 19:11:04 +00:00
|
|
|
dbgmsg( "looking for file '%s', writable %c", filename,
|
|
|
|
write ? 'y' : 'n' );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-07-30 15:27:52 +00:00
|
|
|
tr_lockLock( gFd->lock );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
/* Is it already open? */
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < TR_MAX_OPEN_FILES; ++i )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-10-30 18:35:06 +00:00
|
|
|
o = &gFd->open[i];
|
|
|
|
|
|
|
|
if( !fileIsOpen( o ) )
|
2007-01-14 12:00:21 +00:00
|
|
|
continue;
|
2007-10-30 18:35:06 +00:00
|
|
|
|
|
|
|
if( strcmp( filename, o->filename ) )
|
|
|
|
continue;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( fileIsCheckedOut( o ) )
|
|
|
|
{
|
2007-10-30 18:35:06 +00:00
|
|
|
dbgmsg( "found it! it's open, but checked out. waiting..." );
|
2007-12-03 15:27:38 +00:00
|
|
|
tr_lockUnlock( gFd->lock );
|
|
|
|
tr_wait( 200 );
|
|
|
|
tr_lockLock( gFd->lock );
|
2007-10-31 18:10:55 +00:00
|
|
|
i = -1; /* reloop */
|
2007-01-14 12:00:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
2007-10-30 18:35:06 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
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 );
|
2007-10-31 18:10:55 +00:00
|
|
|
break;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-10-30 18:35:06 +00:00
|
|
|
|
|
|
|
dbgmsg( "found it! it's ready for use!" );
|
2007-01-14 12:00:21 +00:00
|
|
|
winner = i;
|
2007-12-03 15:27:38 +00:00
|
|
|
break;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
dbgmsg(
|
|
|
|
"it's not already open. looking for an open slot or an old file." );
|
2007-12-03 15:27:38 +00:00
|
|
|
while( winner < 0 )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-11-01 13:47:32 +00:00
|
|
|
uint64_t date = tr_date( ) + 1;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-12-03 15:27:38 +00:00
|
|
|
/* look for the file that's been open longest */
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < TR_MAX_OPEN_FILES; ++i )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-10-30 18:35:06 +00:00
|
|
|
o = &gFd->open[i];
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !fileIsOpen( o ) )
|
|
|
|
{
|
2007-10-30 18:35:06 +00:00
|
|
|
winner = i;
|
|
|
|
dbgmsg( "found an empty slot in %d", winner );
|
2007-12-03 15:27:38 +00:00
|
|
|
break;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-10-30 18:35:06 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( date > o->date )
|
|
|
|
{
|
2007-10-30 18:35:06 +00:00
|
|
|
date = o->date;
|
2007-10-31 18:10:55 +00:00
|
|
|
winner = i;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( winner >= 0 )
|
|
|
|
{
|
|
|
|
if( fileIsOpen( &gFd->open[winner] ) )
|
|
|
|
{
|
|
|
|
dbgmsg( "closing file '%s', slot #%d",
|
|
|
|
gFd->open[winner].filename,
|
|
|
|
winner );
|
2007-12-03 16:51:22 +00:00
|
|
|
TrCloseFile( winner );
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dbgmsg(
|
|
|
|
"everything's full! waiting for someone else to finish something" );
|
2007-12-03 15:27:38 +00:00
|
|
|
tr_lockUnlock( gFd->lock );
|
|
|
|
tr_wait( 200 );
|
|
|
|
tr_lockLock( gFd->lock );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-03 15:27:38 +00:00
|
|
|
assert( winner >= 0 );
|
2007-10-30 18:35:06 +00:00
|
|
|
o = &gFd->open[winner];
|
|
|
|
if( !fileIsOpen( o ) )
|
2007-01-14 12:00:21 +00:00
|
|
|
{
|
2008-10-03 04:49:06 +00:00
|
|
|
const int err = TrOpenFile( winner, folder, torrentFile, write );
|
|
|
|
if( err ) {
|
2007-10-30 18:35:06 +00:00
|
|
|
tr_lockUnlock( gFd->lock );
|
2008-10-14 03:03:29 +00:00
|
|
|
tr_free( filename );
|
2008-10-03 04:49:06 +00:00
|
|
|
errno = err;
|
|
|
|
return -1;
|
2007-10-30 18:35:06 +00:00
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
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 ) );
|
2007-10-31 18:10:55 +00:00
|
|
|
o->isWritable = write;
|
2007-01-14 12:00:21 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
dbgmsg( "checking out '%s' in slot %d", filename, winner );
|
2007-10-31 18:10:55 +00:00
|
|
|
o->isCheckedOut = 1;
|
2007-11-21 16:16:59 +00:00
|
|
|
o->closeWhenDone = 0;
|
2007-11-01 13:47:32 +00:00
|
|
|
o->date = tr_date( );
|
2008-10-14 03:03:29 +00:00
|
|
|
tr_free( filename );
|
2007-07-30 15:27:52 +00:00
|
|
|
tr_lockUnlock( gFd->lock );
|
2007-10-31 18:10:55 +00:00
|
|
|
return o->fd;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
void
|
2007-11-21 16:16:59 +00:00
|
|
|
tr_fdFileReturn( int fd )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
int i;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-07-30 15:27:52 +00:00
|
|
|
tr_lockLock( gFd->lock );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < TR_MAX_OPEN_FILES; ++i )
|
2007-11-21 16:16:59 +00:00
|
|
|
{
|
2007-10-30 18:35:06 +00:00
|
|
|
struct tr_openfile * o = &gFd->open[i];
|
2007-11-21 16:16:59 +00:00
|
|
|
if( o->fd != fd )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
dbgmsg( "releasing file '%s' in slot #%d", o->filename, i );
|
|
|
|
o->isCheckedOut = 0;
|
|
|
|
if( o->closeWhenDone )
|
|
|
|
TrCloseFile( i );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-11-21 16:16:59 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-11-21 16:16:59 +00:00
|
|
|
tr_lockUnlock( gFd->lock );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_fdFileClose( const char * filename )
|
|
|
|
{
|
|
|
|
int i;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-11-21 16:16:59 +00:00
|
|
|
tr_lockLock( gFd->lock );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < TR_MAX_OPEN_FILES; ++i )
|
2007-11-21 16:16:59 +00:00
|
|
|
{
|
|
|
|
struct tr_openfile * o = &gFd->open[i];
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !fileIsOpen( o ) || strcmp( filename, o->filename ) )
|
2007-11-21 16:16:59 +00:00
|
|
|
continue;
|
|
|
|
|
2008-01-08 20:08:45 +00:00
|
|
|
dbgmsg( "tr_fdFileClose closing '%s'", filename );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !o->isCheckedOut )
|
|
|
|
{
|
2007-11-21 16:16:59 +00:00
|
|
|
dbgmsg( "not checked out, so closing it now... '%s'", filename );
|
|
|
|
TrCloseFile( i );
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dbgmsg(
|
|
|
|
"flagging file '%s', slot #%d to be closed when checked in",
|
|
|
|
gFd->open[i].filename, i );
|
2007-11-21 16:16:59 +00:00
|
|
|
o->closeWhenDone = 1;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-07-30 15:27:52 +00:00
|
|
|
tr_lockUnlock( gFd->lock );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
**** Sockets
|
|
|
|
****
|
|
|
|
***/
|
2007-01-21 19:42:11 +00:00
|
|
|
|
2008-01-21 02:11:57 +00:00
|
|
|
static int
|
|
|
|
getSocketMax( struct tr_fd_s * gFd )
|
|
|
|
{
|
2008-10-14 20:44:41 +00:00
|
|
|
return gFd->socketMax;
|
2008-01-21 02:11:57 +00:00
|
|
|
}
|
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
int
|
2008-10-14 20:44:41 +00:00
|
|
|
tr_fdSocketCreate( int type )
|
2007-01-21 19:42:11 +00:00
|
|
|
{
|
|
|
|
int s = -1;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-07-30 15:27:52 +00:00
|
|
|
tr_lockLock( gFd->lock );
|
2007-07-21 17:35:47 +00:00
|
|
|
|
2008-10-14 20:44:41 +00:00
|
|
|
if( gFd->socketCount < getSocketMax( gFd ) )
|
2007-10-31 18:10:55 +00:00
|
|
|
if( ( s = socket( AF_INET, type, 0 ) ) < 0 )
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_err( _( "Couldn't create socket: %s" ),
|
|
|
|
tr_strerror( sockerrno ) );
|
2007-07-21 17:35:47 +00:00
|
|
|
|
2007-01-21 19:42:11 +00:00
|
|
|
if( s > -1 )
|
2008-10-14 20:44:41 +00:00
|
|
|
++gFd->socketCount;
|
2007-10-31 18:10:55 +00:00
|
|
|
|
2008-10-14 20:44:41 +00:00
|
|
|
assert( gFd->socketCount >= 0 );
|
2007-10-31 18:10:55 +00:00
|
|
|
|
|
|
|
tr_lockUnlock( gFd->lock );
|
2007-01-21 19:42:11 +00:00
|
|
|
return s;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-09-25 23:10:34 +00:00
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_fdSocketAccept( int b,
|
|
|
|
struct in_addr * addr,
|
|
|
|
tr_port_t * port )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +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
|
|
|
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( addr );
|
|
|
|
assert( port );
|
2007-09-25 23:10:34 +00:00
|
|
|
|
2007-07-30 15:27:52 +00:00
|
|
|
tr_lockLock( gFd->lock );
|
2008-10-14 20:44:41 +00:00
|
|
|
if( gFd->socketCount < 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
|
|
|
{
|
2007-09-25 23:10:34 +00:00
|
|
|
*addr = sock.sin_addr;
|
|
|
|
*port = sock.sin_port;
|
2008-10-14 20:44:41 +00:00
|
|
|
++gFd->socketCount;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-07-30 15:27:52 +00:00
|
|
|
tr_lockUnlock( gFd->lock );
|
2007-01-21 19:42:11 +00:00
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
static void
|
|
|
|
socketClose( int fd )
|
2007-01-21 19:42:11 +00:00
|
|
|
{
|
|
|
|
#ifdef BEOS_NETSERVER
|
2007-10-30 18:35:06 +00:00
|
|
|
closesocket( fd );
|
2007-01-21 19:42:11 +00:00
|
|
|
#else
|
2007-10-30 18:35:06 +00:00
|
|
|
EVUTIL_CLOSESOCKET( fd );
|
2007-01-21 19:42:11 +00:00
|
|
|
#endif
|
2007-10-30 18:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_fdSocketClose( int s )
|
|
|
|
{
|
2007-10-31 18:10:55 +00:00
|
|
|
tr_lockLock( gFd->lock );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( s >= 0 )
|
|
|
|
{
|
2007-10-30 18:35:06 +00:00
|
|
|
socketClose( s );
|
2008-10-14 20:44:41 +00:00
|
|
|
--gFd->socketCount;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2007-10-31 18:10:55 +00:00
|
|
|
|
2008-10-14 20:44:41 +00:00
|
|
|
assert( gFd->socketCount >= 0 );
|
2007-10-31 18:10:55 +00:00
|
|
|
|
|
|
|
tr_lockUnlock( gFd->lock );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
**** Startup / Shutdown
|
|
|
|
****
|
|
|
|
***/
|
2007-01-14 12:00:21 +00:00
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
void
|
2007-12-24 07:02:40 +00:00
|
|
|
tr_fdInit( int globalPeerLimit )
|
2007-01-14 12:00:21 +00:00
|
|
|
{
|
2008-01-03 16:36:20 +00:00
|
|
|
int i;
|
2007-01-14 12:00:21 +00:00
|
|
|
|
2007-10-30 18:35:06 +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
|
|
|
|
2008-01-10 19:22:11 +00:00
|
|
|
#ifdef HAVE_GETRLIMIT
|
|
|
|
{
|
|
|
|
struct rlimit rlim;
|
|
|
|
getrlimit( RLIMIT_NOFILE, &rlim );
|
|
|
|
rlim.rlim_cur = MIN( rlim.rlim_max,
|
2008-09-23 19:11:04 +00:00
|
|
|
(rlim_t)( globalPeerLimit + NOFILE_BUFFER ) );
|
2008-01-10 19:22:11 +00:00
|
|
|
setrlimit( RLIMIT_NOFILE, &rlim );
|
2008-10-14 20:44:41 +00:00
|
|
|
gFd->socketMax = rlim.rlim_cur - NOFILE_BUFFER;
|
2008-02-25 03:09:55 +00:00
|
|
|
tr_dbg( "setrlimit( RLIMIT_NOFILE, %d )", (int)rlim.rlim_cur );
|
2008-01-10 19:22:11 +00:00
|
|
|
}
|
|
|
|
#else
|
2008-10-14 20:44:41 +00:00
|
|
|
gFd->socketMax = globalPeerLimit;
|
2008-01-10 19:22:11 +00:00
|
|
|
#endif
|
2008-01-03 16:36:20 +00:00
|
|
|
tr_dbg( "%d usable file descriptors", globalPeerLimit );
|
2007-01-14 12:00:21 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < TR_MAX_OPEN_FILES; ++i )
|
2007-10-31 18:10:55 +00:00
|
|
|
gFd->open[i].fd = -1;
|
2007-01-14 12:00:21 +00:00
|
|
|
}
|
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
void
|
|
|
|
tr_fdClose( void )
|
2007-01-14 12:00:21 +00:00
|
|
|
{
|
2007-10-30 18:35:06 +00:00
|
|
|
int i = 0;
|
2007-01-14 12:00:21 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < TR_MAX_OPEN_FILES; ++i )
|
2007-10-30 18:35:06 +00:00
|
|
|
if( fileIsOpen( &gFd->open[i] ) )
|
|
|
|
TrCloseFile( i );
|
2007-01-14 12:00:21 +00:00
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
tr_lockFree( gFd->lock );
|
2007-01-14 12:00:21 +00:00
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
tr_free( gFd );
|
2008-10-19 17:43:04 +00:00
|
|
|
gFd = NULL;
|
2007-10-30 18:35:06 +00:00
|
|
|
}
|
2007-12-20 21:44:16 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
tr_fdSetPeerLimit( uint16_t n )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
assert( gFd != NULL && "tr_fdInit() must be called first!" );
|
2008-10-14 20:44:41 +00:00
|
|
|
gFd->socketMax = n;
|
2007-12-20 21:44:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t
|
|
|
|
tr_fdGetPeerLimit( void )
|
|
|
|
{
|
2008-10-14 20:44:41 +00:00
|
|
|
return gFd ? gFd->socketMax : -1;
|
2007-12-20 21:44:16 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|