2010-05-17 16:41:59 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2010 Mnemosyne LLC
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
2010-05-17 16:41:59 +00:00
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
|
|
|
* Transmission project are granted a special exemption to clause 2(b)
|
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
2010-05-17 16:41:59 +00:00
|
|
|
* $Id$
|
|
|
|
*/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-11-23 17:05:44 +00:00
|
|
|
#ifndef WIN32
|
|
|
|
#define HAVE_GETRLIMIT
|
|
|
|
#endif
|
|
|
|
|
2009-01-25 23:47:07 +00:00
|
|
|
#ifdef HAVE_POSIX_FADVISE
|
|
|
|
#ifdef _XOPEN_SOURCE
|
|
|
|
#undef _XOPEN_SOURCE
|
|
|
|
#endif
|
|
|
|
#define _XOPEN_SOURCE 600
|
|
|
|
#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>
|
2008-11-05 05:56:06 +00:00
|
|
|
#ifdef SYS_DARWIN
|
2009-01-08 19:20:24 +00:00
|
|
|
#include <fcntl.h>
|
2008-11-05 05:56:06 +00:00
|
|
|
#endif
|
2007-07-29 18:11:21 +00:00
|
|
|
|
2010-02-02 03:01:25 +00:00
|
|
|
#ifdef HAVE_FALLOCATE64
|
|
|
|
/* FIXME can't find the right #include voodoo to pick up the declaration.. */
|
|
|
|
extern int fallocate64( int fd, int mode, uint64_t offset, uint64_t len );
|
|
|
|
#endif
|
|
|
|
|
2009-04-05 14:04:07 +00:00
|
|
|
#ifdef HAVE_XFS_XFS_H
|
|
|
|
#include <xfs/xfs.h>
|
|
|
|
#endif
|
|
|
|
|
2007-07-13 00:15:45 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2008-11-23 17:05:44 +00:00
|
|
|
#ifdef HAVE_GETRLIMIT
|
|
|
|
#include <sys/time.h> /* getrlimit */
|
|
|
|
#include <sys/resource.h> /* getrlimit */
|
|
|
|
#endif
|
2009-01-25 23:47:07 +00:00
|
|
|
#include <fcntl.h> /* O_LARGEFILE posix_fadvise */
|
2009-12-18 07:05:36 +00:00
|
|
|
#include <unistd.h>
|
2009-01-25 23:40:08 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "transmission.h"
|
2008-08-11 19:05:02 +00:00
|
|
|
#include "fdlimit.h"
|
2007-07-31 14:26:44 +00:00
|
|
|
#include "net.h"
|
2010-05-06 17:02:31 +00:00
|
|
|
#include "platform.h" /* TR_PATH_MAX, TR_PATH_DELIMITER */
|
2009-10-23 03:41:36 +00:00
|
|
|
#include "session.h"
|
|
|
|
#include "torrent.h" /* tr_isTorrent() */
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "utils.h"
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-10-26 15:39:04 +00:00
|
|
|
#define dbgmsg( ... ) \
|
|
|
|
do { \
|
|
|
|
if( tr_deepLoggingIsActive( ) ) \
|
|
|
|
tr_deepLog( __FILE__, __LINE__, NULL, __VA_ARGS__ ); \
|
|
|
|
} while( 0 )
|
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
|
|
|
struct tr_openfile
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2009-10-19 05:05:00 +00:00
|
|
|
tr_bool isWritable;
|
|
|
|
int torrentId;
|
|
|
|
tr_file_index_t fileNum;
|
2010-05-06 17:02:31 +00:00
|
|
|
char filename[TR_PATH_MAX];
|
2009-10-19 05:05:00 +00:00
|
|
|
int fd;
|
|
|
|
uint64_t date;
|
2007-10-30 18:35:06 +00:00
|
|
|
};
|
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
struct tr_fdInfo
|
2007-10-30 18:35:06 +00:00
|
|
|
{
|
2008-10-14 20:44:41 +00:00
|
|
|
int socketCount;
|
2009-01-08 19:20:24 +00:00
|
|
|
int socketLimit;
|
2009-10-23 03:41:36 +00:00
|
|
|
int publicSocketLimit;
|
2009-01-08 19:20:24 +00:00
|
|
|
int openFileLimit;
|
2009-07-04 04:33:23 +00:00
|
|
|
struct tr_openfile * openFiles;
|
2007-10-30 18:35:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
**** Local Files
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-11-15 19:59:18 +00:00
|
|
|
#ifndef O_LARGEFILE
|
2009-01-08 19:20:24 +00:00
|
|
|
#define O_LARGEFILE 0
|
2008-11-15 19:59:18 +00:00
|
|
|
#endif
|
|
|
|
|
2009-01-16 16:38:16 +00:00
|
|
|
static tr_bool
|
|
|
|
preallocateFileSparse( int fd, uint64_t length )
|
|
|
|
{
|
|
|
|
const char zero = '\0';
|
2010-03-09 16:19:59 +00:00
|
|
|
tr_bool success = 0;
|
|
|
|
|
|
|
|
if( !length )
|
|
|
|
success = TRUE;
|
2009-01-16 16:38:16 +00:00
|
|
|
|
2010-03-09 16:19:59 +00:00
|
|
|
#ifdef HAVE_FALLOCATE64
|
|
|
|
if( !success ) /* fallocate64 is always preferred, so try it first */
|
|
|
|
success = !fallocate64( fd, 0, 0, length );
|
|
|
|
#endif
|
2009-01-16 16:38:16 +00:00
|
|
|
|
2010-03-09 16:19:59 +00:00
|
|
|
if( !success ) /* fallback: the old-style seek-and-write */
|
|
|
|
success = ( lseek( fd, length-1, SEEK_SET ) != -1 )
|
|
|
|
&& ( write( fd, &zero, 1 ) != -1 )
|
|
|
|
&& ( ftruncate( fd, length ) != -1 );
|
2009-01-16 16:38:16 +00:00
|
|
|
|
2010-03-09 16:19:59 +00:00
|
|
|
return success;
|
2009-01-16 16:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static tr_bool
|
|
|
|
preallocateFileFull( const char * filename, uint64_t length )
|
2008-11-05 05:56:06 +00:00
|
|
|
{
|
2009-01-16 16:38:16 +00:00
|
|
|
tr_bool success = 0;
|
2008-11-15 19:59:18 +00:00
|
|
|
|
|
|
|
#ifdef WIN32
|
2008-11-05 05:56:06 +00:00
|
|
|
|
2010-06-21 16:44:35 +00:00
|
|
|
HANDLE hFile = CreateFile( filename, GENERIC_WRITE, 0, 0, CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0 );
|
2008-11-15 19:59:18 +00:00
|
|
|
if( hFile != INVALID_HANDLE_VALUE )
|
|
|
|
{
|
|
|
|
LARGE_INTEGER li;
|
2008-11-19 16:06:31 +00:00
|
|
|
li.QuadPart = length;
|
2008-11-19 17:48:24 +00:00
|
|
|
success = SetFilePointerEx( hFile, li, NULL, FILE_BEGIN ) && SetEndOfFile( hFile );
|
2008-11-15 19:59:18 +00:00
|
|
|
CloseHandle( hFile );
|
|
|
|
}
|
2008-11-05 05:56:06 +00:00
|
|
|
|
2008-11-15 19:59:18 +00:00
|
|
|
#else
|
2008-11-05 05:56:06 +00:00
|
|
|
|
2008-11-15 19:59:18 +00:00
|
|
|
int flags = O_RDWR | O_CREAT | O_LARGEFILE;
|
|
|
|
int fd = open( filename, flags, 0666 );
|
|
|
|
if( fd >= 0 )
|
|
|
|
{
|
2010-02-02 03:01:25 +00:00
|
|
|
# ifdef HAVE_FALLOCATE64
|
|
|
|
if( !success )
|
|
|
|
{
|
|
|
|
success = !fallocate64( fd, 0, 0, length );
|
|
|
|
}
|
|
|
|
# endif
|
2009-04-05 14:04:07 +00:00
|
|
|
# ifdef HAVE_XFS_XFS_H
|
|
|
|
if( !success && platform_test_xfs_fd( fd ) )
|
|
|
|
{
|
|
|
|
xfs_flock64_t fl;
|
|
|
|
fl.l_whence = 0;
|
|
|
|
fl.l_start = 0;
|
|
|
|
fl.l_len = length;
|
|
|
|
success = !xfsctl( NULL, fd, XFS_IOC_RESVSP64, &fl );
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
# ifdef SYS_DARWIN
|
|
|
|
if( !success )
|
|
|
|
{
|
|
|
|
fstore_t fst;
|
|
|
|
fst.fst_flags = F_ALLOCATECONTIG;
|
|
|
|
fst.fst_posmode = F_PEOFPOSMODE;
|
|
|
|
fst.fst_offset = 0;
|
|
|
|
fst.fst_length = length;
|
|
|
|
fst.fst_bytesalloc = 0;
|
|
|
|
success = !fcntl( fd, F_PREALLOCATE, &fst );
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
# ifdef HAVE_POSIX_FALLOCATE
|
|
|
|
if( !success )
|
|
|
|
{
|
|
|
|
success = !posix_fallocate( fd, 0, length );
|
|
|
|
}
|
2008-11-15 19:59:18 +00:00
|
|
|
# endif
|
|
|
|
|
2009-06-16 17:10:47 +00:00
|
|
|
if( !success ) /* if nothing else works, do it the old-fashioned way */
|
|
|
|
{
|
2009-08-10 20:04:08 +00:00
|
|
|
uint8_t buf[ 4096 ];
|
|
|
|
memset( buf, 0, sizeof( buf ) );
|
|
|
|
success = TRUE;
|
|
|
|
while ( success && ( length > 0 ) )
|
|
|
|
{
|
|
|
|
const int thisPass = MIN( length, sizeof( buf ) );
|
|
|
|
success = write( fd, buf, thisPass ) == thisPass;
|
|
|
|
length -= thisPass;
|
|
|
|
}
|
2009-06-16 17:10:47 +00:00
|
|
|
}
|
|
|
|
|
2008-11-15 19:59:18 +00:00
|
|
|
close( fd );
|
|
|
|
}
|
2008-11-05 05:56:06 +00:00
|
|
|
|
|
|
|
#endif
|
2008-11-15 19:59:18 +00:00
|
|
|
|
|
|
|
return success;
|
2008-11-05 05:56:06 +00:00
|
|
|
}
|
|
|
|
|
2009-11-08 23:43:38 +00:00
|
|
|
/* Like pread and pwrite, except that the position is undefined afterwards.
|
|
|
|
And of course they are not thread-safe. */
|
|
|
|
|
|
|
|
#ifdef SYS_DARWIN
|
|
|
|
#define HAVE_PREAD
|
|
|
|
#define HAVE_PWRITE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ssize_t
|
|
|
|
tr_pread( int fd, void *buf, size_t count, off_t offset )
|
|
|
|
{
|
|
|
|
#ifdef HAVE_PREAD
|
|
|
|
return pread( fd, buf, count, offset );
|
|
|
|
#else
|
|
|
|
const off_t lrc = lseek( fd, offset, SEEK_SET );
|
|
|
|
if( lrc < 0 )
|
|
|
|
return -1;
|
|
|
|
return read( fd, buf, count );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t
|
2010-05-22 00:09:36 +00:00
|
|
|
tr_pwrite( int fd, const void *buf, size_t count, off_t offset )
|
2009-11-08 23:43:38 +00:00
|
|
|
{
|
|
|
|
#ifdef HAVE_PWRITE
|
|
|
|
return pwrite( fd, buf, count, offset );
|
|
|
|
#else
|
|
|
|
const off_t lrc = lseek( fd, offset, SEEK_SET );
|
|
|
|
if( lrc < 0 )
|
|
|
|
return -1;
|
|
|
|
return write( fd, buf, count );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2010-10-11 21:44:46 +00:00
|
|
|
tr_prefetch( int fd UNUSED, off_t offset UNUSED, size_t count UNUSED )
|
2009-11-08 23:43:38 +00:00
|
|
|
{
|
|
|
|
#ifdef HAVE_POSIX_FADVISE
|
|
|
|
return posix_fadvise( fd, offset, count, POSIX_FADV_WILLNEED );
|
|
|
|
#elif defined(SYS_DARWIN)
|
|
|
|
struct radvisory radv;
|
|
|
|
radv.ra_offset = offset;
|
|
|
|
radv.ra_count = count;
|
|
|
|
return fcntl( fd, F_RDADVISE, &radv );
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-05-13 15:54:04 +00:00
|
|
|
int
|
|
|
|
tr_open_file_for_writing( const char * filename )
|
|
|
|
{
|
|
|
|
int flags = O_WRONLY | O_CREAT;
|
|
|
|
#ifdef O_BINARY
|
|
|
|
flags |= O_BINARY;
|
|
|
|
#endif
|
|
|
|
#ifdef O_LARGEFILE
|
|
|
|
flags |= O_LARGEFILE;
|
|
|
|
#endif
|
|
|
|
return open( filename, flags, 0666 );
|
|
|
|
}
|
|
|
|
|
2009-04-26 20:44:18 +00:00
|
|
|
int
|
2009-04-26 00:51:51 +00:00
|
|
|
tr_open_file_for_scanning( const char * filename )
|
2009-04-07 20:25:32 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
/* build the flags */
|
|
|
|
flags = O_RDONLY;
|
|
|
|
#ifdef O_SEQUENTIAL
|
2009-05-06 13:33:40 +00:00
|
|
|
flags |= O_SEQUENTIAL;
|
2009-04-07 20:25:32 +00:00
|
|
|
#endif
|
|
|
|
#ifdef O_BINARY
|
|
|
|
flags |= O_BINARY;
|
|
|
|
#endif
|
|
|
|
#ifdef O_LARGEFILE
|
|
|
|
flags |= O_LARGEFILE;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* open the file */
|
|
|
|
fd = open( filename, flags, 0666 );
|
2009-04-26 20:44:18 +00:00
|
|
|
if( fd >= 0 )
|
|
|
|
{
|
2009-05-06 19:06:19 +00:00
|
|
|
/* Set hints about the lookahead buffer and caching. It's okay
|
|
|
|
for these to fail silently, so don't let them affect errno */
|
|
|
|
const int err = errno;
|
2009-04-10 14:26:01 +00:00
|
|
|
#ifdef HAVE_POSIX_FADVISE
|
2009-04-26 20:44:18 +00:00
|
|
|
posix_fadvise( fd, 0, 0, POSIX_FADV_SEQUENTIAL );
|
2009-04-07 20:25:32 +00:00
|
|
|
#endif
|
2009-04-26 19:50:31 +00:00
|
|
|
#ifdef SYS_DARWIN
|
2009-04-26 20:44:18 +00:00
|
|
|
fcntl( fd, F_NOCACHE, 1 );
|
|
|
|
fcntl( fd, F_RDAHEAD, 1 );
|
2009-04-26 19:50:31 +00:00
|
|
|
#endif
|
2009-05-06 19:06:19 +00:00
|
|
|
errno = err;
|
2009-04-26 20:44:18 +00:00
|
|
|
}
|
2009-04-07 20:25:32 +00:00
|
|
|
|
2009-04-26 20:44:18 +00:00
|
|
|
return fd;
|
2009-04-07 20:25:32 +00:00
|
|
|
}
|
|
|
|
|
2009-04-26 20:44:18 +00:00
|
|
|
void
|
|
|
|
tr_close_file( int fd )
|
2009-04-26 00:51:51 +00:00
|
|
|
{
|
|
|
|
#if defined(HAVE_POSIX_FADVISE)
|
2009-05-07 11:54:09 +00:00
|
|
|
/* Set hint about not caching this file.
|
|
|
|
It's okay for this to fail silently, so don't let it affect errno */
|
2009-05-07 13:03:39 +00:00
|
|
|
const int err = errno;
|
2009-04-26 00:51:51 +00:00
|
|
|
posix_fadvise( fd, 0, 0, POSIX_FADV_DONTNEED );
|
2009-05-07 11:54:09 +00:00
|
|
|
errno = err;
|
2010-06-21 13:08:04 +00:00
|
|
|
#endif
|
|
|
|
#ifdef SYS_DARWIN
|
|
|
|
/* it's unclear to me from the man pages if this actually flushes out the cache,
|
|
|
|
* but it couldn't hurt... */
|
|
|
|
fcntl( fd, F_NOCACHE, 1 );
|
2009-04-26 00:51:51 +00:00
|
|
|
#endif
|
2009-04-26 20:44:18 +00:00
|
|
|
close( fd );
|
2009-04-26 00:51:51 +00:00
|
|
|
}
|
|
|
|
|
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
|
2009-10-23 03:41:36 +00:00
|
|
|
TrOpenFile( tr_session * session,
|
|
|
|
int i,
|
2009-10-21 19:33:37 +00:00
|
|
|
const char * filename,
|
2009-01-16 16:38:16 +00:00
|
|
|
tr_bool doWrite,
|
|
|
|
tr_preallocation_mode preallocationMode,
|
|
|
|
uint64_t desiredFileSize )
|
2007-10-30 18:35:06 +00:00
|
|
|
{
|
2009-10-23 03:41:36 +00:00
|
|
|
int flags;
|
|
|
|
struct stat sb;
|
|
|
|
tr_bool alreadyExisted;
|
|
|
|
struct tr_openfile * file;
|
|
|
|
|
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
assert( session->fdInfo != NULL );
|
|
|
|
|
|
|
|
file = &session->fdInfo->openFiles[i];
|
2008-01-18 19:13:32 +00:00
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
/* create subfolders, if any */
|
2008-11-05 05:56:06 +00:00
|
|
|
if( doWrite )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2009-10-21 19:33:37 +00:00
|
|
|
char * dir = tr_dirname( filename );
|
|
|
|
const int err = tr_mkdirp( dir, 0777 ) ? errno : 0;
|
2008-10-14 04:51:42 +00:00
|
|
|
if( err ) {
|
2009-10-21 19:33:37 +00:00
|
|
|
tr_err( _( "Couldn't create \"%1$s\": %2$s" ), dir, tr_strerror( err ) );
|
|
|
|
tr_free( dir );
|
2008-10-03 04:49:06 +00:00
|
|
|
return err;
|
2008-10-14 04:51:42 +00:00
|
|
|
}
|
2009-10-21 19:33:37 +00:00
|
|
|
tr_free( dir );
|
2007-10-30 18:35:06 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-11-05 05:56:06 +00:00
|
|
|
alreadyExisted = !stat( filename, &sb ) && S_ISREG( sb.st_mode );
|
2008-11-15 19:59:18 +00:00
|
|
|
|
2009-01-16 16:38:16 +00:00
|
|
|
if( doWrite && !alreadyExisted && ( preallocationMode == TR_PREALLOCATE_FULL ) )
|
|
|
|
if( preallocateFileFull( filename, desiredFileSize ) )
|
2010-02-08 04:43:56 +00:00
|
|
|
tr_dbg( _( "Preallocated file \"%s\"" ), filename );
|
2009-08-10 20:04:08 +00:00
|
|
|
|
2007-10-31 18:10:55 +00:00
|
|
|
/* open the file */
|
2008-11-05 05:56:06 +00:00
|
|
|
flags = doWrite ? ( O_RDWR | O_CREAT ) : O_RDONLY;
|
2009-05-31 21:20:49 +00:00
|
|
|
#ifdef O_SEQUENTIAL
|
|
|
|
flags |= O_SEQUENTIAL;
|
2009-01-25 23:33:10 +00:00
|
|
|
#endif
|
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;
|
2009-01-17 15:08:20 +00:00
|
|
|
tr_err( _( "Couldn't open \"%1$s\": %2$s" ), filename, tr_strerror( err ) );
|
2008-10-03 04:49:06 +00:00
|
|
|
return err;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2009-06-21 07:53:51 +00:00
|
|
|
/* If the file already exists and it's too large, truncate it.
|
|
|
|
* This is a fringe case that happens if a torrent's been updated
|
|
|
|
* and one of the updated torrent's files is smaller.
|
|
|
|
* http://trac.transmissionbt.com/ticket/2228
|
|
|
|
* https://bugs.launchpad.net/ubuntu/+source/transmission/+bug/318249
|
|
|
|
*/
|
|
|
|
if( alreadyExisted && ( desiredFileSize < (uint64_t)sb.st_size ) )
|
|
|
|
ftruncate( file->fd, desiredFileSize );
|
|
|
|
|
2009-01-16 16:38:16 +00:00
|
|
|
if( doWrite && !alreadyExisted && ( preallocationMode == TR_PREALLOCATE_SPARSE ) )
|
|
|
|
preallocateFileSparse( file->fd, desiredFileSize );
|
|
|
|
|
2009-04-10 14:26:01 +00:00
|
|
|
#ifdef HAVE_POSIX_FADVISE
|
2009-07-04 04:33:23 +00:00
|
|
|
/* this doubles the OS level readahead buffer, which in practice
|
|
|
|
* turns out to be a good thing, because many (most?) clients request
|
2009-09-08 06:25:40 +00:00
|
|
|
* chunks of blocks in order.
|
|
|
|
* It's okay for this to fail silently, so don't let it affect errno */
|
|
|
|
{
|
|
|
|
const int err = errno;
|
|
|
|
posix_fadvise( file->fd, 0, 0, POSIX_FADV_SEQUENTIAL );
|
|
|
|
errno = err;
|
|
|
|
}
|
2009-01-25 23:33:10 +00:00
|
|
|
#endif
|
|
|
|
|
2010-06-27 17:44:34 +00:00
|
|
|
#if defined( SYS_DARWIN )
|
|
|
|
/**
|
|
|
|
* 1. Enable readahead for reasons described above w/POSIX_FADV_SEQUENTIAL.
|
|
|
|
*
|
|
|
|
* 2. Disable OS-level caching due to user reports of adverse effects of
|
|
|
|
* excessive inactive memory. However this is experimental because
|
|
|
|
* previous attempts at this have *also* had adverse effects (see r8198)
|
|
|
|
*
|
|
|
|
* It's okay for this to fail silently, so don't let it affect errno
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
const int err = errno;
|
|
|
|
fcntl( file->fd, F_NOCACHE, 1 );
|
|
|
|
fcntl( file->fd, F_RDAHEAD, 1 );
|
|
|
|
errno = err;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-10-03 04:49:06 +00:00
|
|
|
return 0;
|
2007-10-30 18:35:06 +00:00
|
|
|
}
|
|
|
|
|
2010-01-01 22:26:35 +00:00
|
|
|
static inline tr_bool
|
2007-10-30 18:35:06 +00:00
|
|
|
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
|
2009-07-04 04:33:23 +00:00
|
|
|
TrCloseFile( struct tr_openfile * o )
|
2007-10-30 18:35:06 +00:00
|
|
|
{
|
2009-07-04 04:33:23 +00:00
|
|
|
assert( o != NULL );
|
2007-10-31 18:10:55 +00:00
|
|
|
assert( fileIsOpen( o ) );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2009-04-26 20:44:18 +00:00
|
|
|
tr_close_file( o->fd );
|
2007-10-31 18:10:55 +00:00
|
|
|
o->fd = -1;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2009-10-19 05:05:00 +00:00
|
|
|
int
|
2009-10-23 03:41:36 +00:00
|
|
|
tr_fdFileGetCached( tr_session * session,
|
|
|
|
int torrentId,
|
|
|
|
tr_file_index_t fileNum,
|
|
|
|
tr_bool doWrite )
|
2009-10-19 05:05:00 +00:00
|
|
|
{
|
2009-10-20 20:19:41 +00:00
|
|
|
struct tr_openfile * match = NULL;
|
2009-10-23 03:41:36 +00:00
|
|
|
struct tr_fdInfo * gFd;
|
2009-10-19 05:05:00 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
assert( session->fdInfo != NULL );
|
2009-10-19 05:05:00 +00:00
|
|
|
assert( torrentId > 0 );
|
|
|
|
assert( tr_isBool( doWrite ) );
|
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
gFd = session->fdInfo;
|
|
|
|
|
2009-10-19 05:05:00 +00:00
|
|
|
/* is it already open? */
|
|
|
|
{
|
2009-10-20 20:19:41 +00:00
|
|
|
int i;
|
|
|
|
struct tr_openfile * o;
|
|
|
|
for( i=0; i<gFd->openFileLimit; ++i )
|
|
|
|
{
|
|
|
|
o = &gFd->openFiles[i];
|
2009-10-19 05:05:00 +00:00
|
|
|
|
2009-10-20 20:19:41 +00:00
|
|
|
if( torrentId != o->torrentId )
|
|
|
|
continue;
|
|
|
|
if( fileNum != o->fileNum )
|
|
|
|
continue;
|
|
|
|
if( !fileIsOpen( o ) )
|
|
|
|
continue;
|
2009-10-19 05:05:00 +00:00
|
|
|
|
2009-10-20 20:19:41 +00:00
|
|
|
match = o;
|
|
|
|
break;
|
|
|
|
}
|
2009-10-19 05:05:00 +00:00
|
|
|
}
|
|
|
|
|
2009-10-20 20:19:41 +00:00
|
|
|
if( ( match != NULL ) && ( !doWrite || match->isWritable ) )
|
2009-10-19 05:05:00 +00:00
|
|
|
{
|
2010-07-11 20:49:19 +00:00
|
|
|
match->date = tr_time_msec( );
|
2009-10-20 20:19:41 +00:00
|
|
|
return match->fd;
|
2009-10-19 05:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
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
|
2009-10-23 03:41:36 +00:00
|
|
|
tr_fdFileCheckout( tr_session * session,
|
|
|
|
int torrentId,
|
2009-10-19 05:05:00 +00:00
|
|
|
tr_file_index_t fileNum,
|
2009-10-21 19:33:37 +00:00
|
|
|
const char * filename,
|
2009-01-16 16:38:16 +00:00
|
|
|
tr_bool doWrite,
|
|
|
|
tr_preallocation_mode preallocationMode,
|
|
|
|
uint64_t desiredFileSize )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-12-30 03:29:09 +00:00
|
|
|
int i, winner = -1;
|
2009-10-23 03:41:36 +00:00
|
|
|
struct tr_fdInfo * gFd;
|
2007-10-30 18:35:06 +00:00
|
|
|
struct tr_openfile * o;
|
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
assert( session->fdInfo != NULL );
|
2009-06-21 07:36:51 +00:00
|
|
|
assert( torrentId > 0 );
|
2009-10-21 19:33:37 +00:00
|
|
|
assert( filename && *filename );
|
2009-07-04 04:33:23 +00:00
|
|
|
assert( tr_isBool( doWrite ) );
|
2007-10-31 18:10:55 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
gFd = session->fdInfo;
|
|
|
|
|
2008-12-30 03:29:09 +00:00
|
|
|
dbgmsg( "looking for file '%s', writable %c", filename, doWrite ? 'y' : 'n' );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2009-06-21 07:36:51 +00:00
|
|
|
/* is it already open? */
|
|
|
|
for( i=0; i<gFd->openFileLimit; ++i )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2009-01-08 19:20:24 +00:00
|
|
|
o = &gFd->openFiles[i];
|
2007-10-30 18:35:06 +00:00
|
|
|
|
2009-06-21 07:36:51 +00:00
|
|
|
if( torrentId != o->torrentId )
|
|
|
|
continue;
|
2009-10-19 05:05:00 +00:00
|
|
|
if( fileNum != o->fileNum )
|
2007-10-30 18:35:06 +00:00
|
|
|
continue;
|
2009-10-20 20:19:41 +00:00
|
|
|
if( !fileIsOpen( o ) )
|
|
|
|
continue;
|
2007-10-30 18:35:06 +00:00
|
|
|
|
2008-11-05 05:56:06 +00:00
|
|
|
if( doWrite && !o->isWritable )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2009-06-21 07:36:51 +00:00
|
|
|
dbgmsg( "found it! it's open and available, but isn't writable. closing..." );
|
2009-07-04 04:33:23 +00:00
|
|
|
TrCloseFile( o );
|
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
|
|
|
}
|
|
|
|
|
2009-06-21 07:36:51 +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
|
|
|
{
|
2010-07-11 20:49:19 +00:00
|
|
|
uint64_t date = tr_time_msec( ) + 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 */
|
2009-06-21 07:36:51 +00:00
|
|
|
for( i=0; i<gFd->openFileLimit; ++i )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2009-01-08 19:20:24 +00:00
|
|
|
o = &gFd->openFiles[i];
|
2007-10-30 18:35:06 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-04 04:33:23 +00:00
|
|
|
assert( winner >= 0 );
|
2009-08-10 20:04:08 +00:00
|
|
|
|
2009-07-04 04:33:23 +00:00
|
|
|
if( fileIsOpen( &gFd->openFiles[winner] ) )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2009-07-04 04:33:23 +00:00
|
|
|
dbgmsg( "closing file \"%s\"", gFd->openFiles[winner].filename );
|
|
|
|
TrCloseFile( &gFd->openFiles[winner] );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-03 15:27:38 +00:00
|
|
|
assert( winner >= 0 );
|
2009-01-08 19:20:24 +00:00
|
|
|
o = &gFd->openFiles[winner];
|
2007-10-30 18:35:06 +00:00
|
|
|
if( !fileIsOpen( o ) )
|
2007-01-14 12:00:21 +00:00
|
|
|
{
|
2009-10-23 03:41:36 +00:00
|
|
|
const int err = TrOpenFile( session, winner, filename, doWrite,
|
2009-07-04 04:33:23 +00:00
|
|
|
preallocationMode, desiredFileSize );
|
2008-10-03 04:49:06 +00:00
|
|
|
if( err ) {
|
|
|
|
errno = err;
|
|
|
|
return -1;
|
2007-10-30 18:35:06 +00:00
|
|
|
}
|
|
|
|
|
2008-11-05 05:56:06 +00:00
|
|
|
dbgmsg( "opened '%s' in slot %d, doWrite %c", filename, winner,
|
|
|
|
doWrite ? 'y' : 'n' );
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_strlcpy( o->filename, filename, sizeof( o->filename ) );
|
2008-11-05 05:56:06 +00:00
|
|
|
o->isWritable = doWrite;
|
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 );
|
2009-06-21 07:36:51 +00:00
|
|
|
o->torrentId = torrentId;
|
2009-10-19 05:05:00 +00:00
|
|
|
o->fileNum = fileNum;
|
2010-07-11 20:49:19 +00:00
|
|
|
o->date = tr_time_msec( );
|
2007-10-31 18:10:55 +00:00
|
|
|
return o->fd;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-11-21 16:16:59 +00:00
|
|
|
void
|
2009-10-23 03:41:36 +00:00
|
|
|
tr_fdFileClose( tr_session * session,
|
|
|
|
const tr_torrent * tor,
|
|
|
|
tr_file_index_t fileNum )
|
2007-11-21 16:16:59 +00:00
|
|
|
{
|
2009-07-04 04:33:23 +00:00
|
|
|
struct tr_openfile * o;
|
2009-10-23 03:41:36 +00:00
|
|
|
struct tr_fdInfo * gFd;
|
2009-07-04 04:33:23 +00:00
|
|
|
const struct tr_openfile * end;
|
2009-10-19 05:05:00 +00:00
|
|
|
const int torrentId = tr_torrentId( tor );
|
2009-10-23 03:41:36 +00:00
|
|
|
|
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
assert( session->fdInfo != NULL );
|
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
assert( fileNum < tor->info.fileCount );
|
|
|
|
|
|
|
|
gFd = session->fdInfo;
|
|
|
|
|
2009-07-04 04:33:23 +00:00
|
|
|
for( o=gFd->openFiles, end=o+gFd->openFileLimit; o!=end; ++o )
|
2007-11-21 16:16:59 +00:00
|
|
|
{
|
2009-10-19 05:05:00 +00:00
|
|
|
if( torrentId != o->torrentId )
|
|
|
|
continue;
|
|
|
|
if( fileNum != o->fileNum )
|
|
|
|
continue;
|
2009-10-20 20:19:41 +00:00
|
|
|
if( !fileIsOpen( o ) )
|
|
|
|
continue;
|
2009-10-19 05:05:00 +00:00
|
|
|
|
|
|
|
dbgmsg( "tr_fdFileClose closing \"%s\"", o->filename );
|
2009-07-04 04:33:23 +00:00
|
|
|
TrCloseFile( o );
|
2009-06-21 07:36:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-10-23 03:41:36 +00:00
|
|
|
tr_fdTorrentClose( tr_session * session, int torrentId )
|
2009-06-21 07:36:51 +00:00
|
|
|
{
|
2009-10-23 03:41:36 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2010-01-13 01:21:26 +00:00
|
|
|
if( session->fdInfo != NULL )
|
|
|
|
{
|
|
|
|
struct tr_openfile * o;
|
|
|
|
const struct tr_openfile * end;
|
|
|
|
struct tr_fdInfo * gFd = session->fdInfo;
|
2009-10-23 03:41:36 +00:00
|
|
|
|
2010-01-13 01:21:26 +00:00
|
|
|
for( o=gFd->openFiles, end=o+gFd->openFileLimit; o!=end; ++o )
|
|
|
|
if( fileIsOpen( o ) && ( o->torrentId == torrentId ) )
|
|
|
|
TrCloseFile( o );
|
|
|
|
}
|
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
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
int
|
2009-10-23 03:41:36 +00:00
|
|
|
tr_fdSocketCreate( tr_session * session, int domain, int type )
|
2007-01-21 19:42:11 +00:00
|
|
|
{
|
|
|
|
int s = -1;
|
2009-10-23 03:41:36 +00:00
|
|
|
struct tr_fdInfo * gFd;
|
|
|
|
|
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
assert( session->fdInfo != NULL );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
gFd = session->fdInfo;
|
|
|
|
|
|
|
|
if( gFd->socketCount < gFd->socketLimit )
|
2008-12-15 00:17:08 +00:00
|
|
|
if( ( s = socket( domain, type, 0 ) ) < 0 )
|
|
|
|
{
|
2008-12-20 08:51:32 +00:00
|
|
|
if( sockerrno != EAFNOSUPPORT )
|
2009-08-17 18:49:21 +00:00
|
|
|
tr_err( _( "Couldn't create socket: %s" ),
|
|
|
|
tr_strerror( sockerrno ) );
|
2008-12-15 00:17:08 +00:00
|
|
|
}
|
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 );
|
2009-11-29 08:53:14 +00:00
|
|
|
|
|
|
|
if( s >= 0 )
|
|
|
|
{
|
|
|
|
static tr_bool buf_logged = FALSE;
|
|
|
|
if( !buf_logged )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
socklen_t size = sizeof( int );
|
|
|
|
buf_logged = TRUE;
|
|
|
|
getsockopt( s, SOL_SOCKET, SO_SNDBUF, &i, &size );
|
2010-02-08 04:43:56 +00:00
|
|
|
tr_dbg( "SO_SNDBUF size is %d", i );
|
2009-11-29 08:53:14 +00:00
|
|
|
getsockopt( s, SOL_SOCKET, SO_RCVBUF, &i, &size );
|
2010-02-08 04:43:56 +00:00
|
|
|
tr_dbg( "SO_RCVBUF size is %d", i );
|
2009-11-29 08:53:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2009-10-23 03:41:36 +00:00
|
|
|
tr_fdSocketAccept( tr_session * session,
|
|
|
|
int b,
|
2008-12-02 03:41:58 +00:00
|
|
|
tr_address * addr,
|
|
|
|
tr_port * port )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2009-06-06 16:19:34 +00:00
|
|
|
int s;
|
|
|
|
unsigned int len;
|
2009-10-23 03:41:36 +00:00
|
|
|
struct tr_fdInfo * gFd;
|
2008-12-02 03:41:58 +00:00
|
|
|
struct sockaddr_storage sock;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
assert( session->fdInfo != NULL );
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( addr );
|
|
|
|
assert( port );
|
2007-09-25 23:10:34 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
gFd = session->fdInfo;
|
|
|
|
|
2009-06-06 16:19:34 +00:00
|
|
|
len = sizeof( struct sockaddr_storage );
|
|
|
|
s = accept( b, (struct sockaddr *) &sock, &len );
|
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
if( ( s >= 0 ) && gFd->socketCount > gFd->socketLimit )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2009-10-27 20:27:27 +00:00
|
|
|
tr_netCloseSocket( s );
|
2009-06-06 16:19:34 +00:00
|
|
|
s = -1;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2009-06-06 16:19:34 +00:00
|
|
|
|
|
|
|
if( s >= 0 )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2009-08-10 20:04:08 +00:00
|
|
|
/* "The ss_family field of the sockaddr_storage structure will always
|
|
|
|
* align with the family field of any protocol-specific structure." */
|
|
|
|
if( sock.ss_family == AF_INET )
|
2009-06-15 05:16:36 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_in *si;
|
|
|
|
union { struct sockaddr_storage dummy; struct sockaddr_in si; } s;
|
|
|
|
s.dummy = sock;
|
|
|
|
si = &s.si;
|
2009-08-10 20:04:08 +00:00
|
|
|
addr->type = TR_AF_INET;
|
|
|
|
addr->addr.addr4.s_addr = si->sin_addr.s_addr;
|
|
|
|
*port = si->sin_port;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-06-15 05:16:36 +00:00
|
|
|
struct sockaddr_in6 *si;
|
|
|
|
union { struct sockaddr_storage dummy; struct sockaddr_in6 si; } s;
|
|
|
|
s.dummy = sock;
|
|
|
|
si = &s.si;
|
2009-08-10 20:04:08 +00:00
|
|
|
addr->type = TR_AF_INET6;
|
2009-06-15 05:16:36 +00:00
|
|
|
addr->addr.addr6 = si->sin6_addr;
|
2009-08-10 20:04:08 +00:00
|
|
|
*port = si->sin6_port;
|
|
|
|
}
|
2008-10-14 20:44:41 +00:00
|
|
|
++gFd->socketCount;
|
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-10-30 18:35:06 +00:00
|
|
|
void
|
2009-10-23 03:41:36 +00:00
|
|
|
tr_fdSocketClose( tr_session * session, int fd )
|
2007-10-30 18:35:06 +00:00
|
|
|
{
|
2009-10-23 03:41:36 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2009-12-26 23:52:59 +00:00
|
|
|
if( session->fdInfo != NULL )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2009-12-26 23:52:59 +00:00
|
|
|
struct tr_fdInfo * gFd = session->fdInfo;
|
2007-10-31 18:10:55 +00:00
|
|
|
|
2009-12-26 23:52:59 +00:00
|
|
|
if( fd >= 0 )
|
|
|
|
{
|
|
|
|
tr_netCloseSocket( fd );
|
|
|
|
--gFd->socketCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert( gFd->socketCount >= 0 );
|
|
|
|
}
|
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
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
static void
|
|
|
|
ensureSessionFdInfoExists( tr_session * session )
|
2007-01-14 12:00:21 +00:00
|
|
|
{
|
2009-10-23 03:41:36 +00:00
|
|
|
assert( tr_isSession( session ) );
|
2007-01-14 12:00:21 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
if( session->fdInfo == NULL )
|
|
|
|
session->fdInfo = tr_new0( struct tr_fdInfo, 1 );
|
2007-01-14 12:00:21 +00:00
|
|
|
}
|
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
void
|
2009-10-23 03:41:36 +00:00
|
|
|
tr_fdClose( tr_session * session )
|
2007-01-14 12:00:21 +00:00
|
|
|
{
|
2009-10-23 03:41:36 +00:00
|
|
|
struct tr_fdInfo * gFd;
|
2009-07-04 04:33:23 +00:00
|
|
|
struct tr_openfile * o;
|
|
|
|
const struct tr_openfile * end;
|
2007-01-14 12:00:21 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
assert( session->fdInfo != NULL );
|
|
|
|
|
|
|
|
gFd = session->fdInfo;
|
|
|
|
|
2009-07-04 04:33:23 +00:00
|
|
|
for( o=gFd->openFiles, end=o+gFd->openFileLimit; o!=end; ++o )
|
|
|
|
if( fileIsOpen( o ) )
|
|
|
|
TrCloseFile( o );
|
2007-01-14 12:00:21 +00:00
|
|
|
|
2009-01-18 15:15:10 +00:00
|
|
|
tr_free( gFd->openFiles );
|
2007-10-30 18:35:06 +00:00
|
|
|
tr_free( gFd );
|
2009-10-23 03:41:36 +00:00
|
|
|
session->fdInfo = NULL;
|
2007-10-30 18:35:06 +00:00
|
|
|
}
|
2007-12-20 21:44:16 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-12-20 21:44:16 +00:00
|
|
|
void
|
2009-10-23 03:41:36 +00:00
|
|
|
tr_fdSetFileLimit( tr_session * session, int limit )
|
2007-12-20 21:44:16 +00:00
|
|
|
{
|
2009-10-23 03:41:36 +00:00
|
|
|
struct tr_fdInfo * gFd;
|
|
|
|
|
|
|
|
ensureSessionFdInfoExists( session );
|
|
|
|
|
|
|
|
gFd = session->fdInfo;
|
|
|
|
|
|
|
|
if( gFd->openFileLimit != limit )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct tr_openfile * o;
|
|
|
|
const struct tr_openfile * end;
|
|
|
|
|
|
|
|
/* close any files we've got open */
|
|
|
|
for( o=gFd->openFiles, end=o+gFd->openFileLimit; o!=end; ++o )
|
|
|
|
if( fileIsOpen( o ) )
|
|
|
|
TrCloseFile( o );
|
|
|
|
|
|
|
|
/* rebuild the openFiles array */
|
|
|
|
tr_free( gFd->openFiles );
|
|
|
|
gFd->openFiles = tr_new0( struct tr_openfile, limit );
|
|
|
|
gFd->openFileLimit = limit;
|
|
|
|
for( i=0; i<gFd->openFileLimit; ++i )
|
|
|
|
gFd->openFiles[i].fd = -1;
|
|
|
|
}
|
2007-12-20 21:44:16 +00:00
|
|
|
}
|
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
int
|
|
|
|
tr_fdGetFileLimit( const tr_session * session )
|
|
|
|
{
|
|
|
|
return session && session->fdInfo ? session->fdInfo->openFileLimit : -1;
|
|
|
|
}
|
2009-12-18 07:05:36 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
void
|
2009-12-18 07:05:36 +00:00
|
|
|
tr_fdSetPeerLimit( tr_session * session, int socketLimit )
|
2009-10-23 03:41:36 +00:00
|
|
|
{
|
|
|
|
struct tr_fdInfo * gFd;
|
|
|
|
|
|
|
|
ensureSessionFdInfoExists( session );
|
|
|
|
|
|
|
|
gFd = session->fdInfo;
|
|
|
|
|
|
|
|
#ifdef HAVE_GETRLIMIT
|
|
|
|
{
|
|
|
|
struct rlimit rlim;
|
2009-12-18 07:05:36 +00:00
|
|
|
const int NOFILE_BUFFER = 512;
|
|
|
|
const int open_max = sysconf( _SC_OPEN_MAX );
|
2009-10-23 03:41:36 +00:00
|
|
|
getrlimit( RLIMIT_NOFILE, &rlim );
|
2009-12-21 22:03:19 +00:00
|
|
|
rlim.rlim_cur = MAX( 1024, open_max );
|
|
|
|
rlim.rlim_cur = MIN( rlim.rlim_cur, rlim.rlim_max );
|
2009-10-23 03:41:36 +00:00
|
|
|
setrlimit( RLIMIT_NOFILE, &rlim );
|
|
|
|
tr_dbg( "setrlimit( RLIMIT_NOFILE, %d )", (int)rlim.rlim_cur );
|
2009-12-18 07:05:36 +00:00
|
|
|
gFd->socketLimit = MIN( socketLimit, (int)rlim.rlim_cur - NOFILE_BUFFER );
|
2009-10-23 03:41:36 +00:00
|
|
|
}
|
|
|
|
#else
|
2009-12-18 07:05:36 +00:00
|
|
|
gFd->socketLimit = socketLimit;
|
2009-10-23 03:41:36 +00:00
|
|
|
#endif
|
2009-12-18 07:05:36 +00:00
|
|
|
gFd->publicSocketLimit = socketLimit;
|
2009-10-23 03:41:36 +00:00
|
|
|
|
2009-12-18 07:05:36 +00:00
|
|
|
tr_dbg( "socket limit is %d", (int)gFd->socketLimit );
|
2009-10-23 03:41:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tr_fdGetPeerLimit( const tr_session * session )
|
2007-12-20 21:44:16 +00:00
|
|
|
{
|
2009-10-23 03:41:36 +00:00
|
|
|
return session && session->fdInfo ? session->fdInfo->publicSocketLimit : -1;
|
2007-12-20 21:44:16 +00:00
|
|
|
}
|