31
32
#include <sys/types.h>
32
33
#include <sys/stat.h>
33
34
#include <unistd.h>
35
#include <libgen.h> /* basename, dirname */
36
#include <fcntl.h> /* O_LARGEFILE */
38
#include <sys/queue.h> /* libevent needs this */
39
#include <sys/types.h> /* libevent needs this */
36
42
#include <evutil.h>
38
44
#include "transmission.h"
40
48
#include "platform.h"
43
#define TR_MAX_OPEN_FILES 16 /* That is, real files, not sockets */
44
#define TR_RESERVED_FDS 16 /* Number of sockets reserved for
45
connections to trackers */
47
/***********************************************************************
49
**********************************************************************/
50
typedef struct tr_openFile_s
52
char folder[MAX_PATH_LENGTH];
53
char name[MAX_PATH_LENGTH];
57
#define STATUS_INVALID 1
58
#define STATUS_UNUSED 2
60
#define STATUS_CLOSING 8
67
typedef struct tr_fd_s
77
tr_openFile_t open[TR_MAX_OPEN_FILES];
81
static tr_fd_t * gFd = NULL;
83
/***********************************************************************
85
**********************************************************************/
86
static int TrOpenFile( int i, const char * folder, const char * name, int write );
87
static void TrCloseFile( int i );
90
/***********************************************************************
92
**********************************************************************/
93
void tr_fdInit( void )
99
tr_err( "tr_fdInit was called before!" );
103
gFd = calloc( 1, sizeof( tr_fd_t ) );
105
/* Init lock and cond */
106
gFd->lock = tr_lockNew( );
107
gFd->cond = tr_condNew( );
109
/* Detect the maximum number of open files or sockets */
110
for( i = 0; i < 4096; i++ )
112
if( ( s[i] = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
52
#define TR_UINT_TO_PTR(i) (void*)((uint64_t)i)
54
#define TR_UINT_TO_PTR(i) ((void*)((uint32_t)i))
62
myDebug( const char * file, int line, const char * fmt, ... )
64
FILE * fp = tr_getLog( );
69
struct evbuffer * buf = evbuffer_new( );
70
char * myfile = tr_strdup( file );
72
evbuffer_add_printf( buf, "[%s] ", tr_getLogTimeStr( s, sizeof(s) ) );
73
va_start( args, fmt );
74
evbuffer_add_vprintf( buf, fmt, args );
76
evbuffer_add_printf( buf, " (%s:%d)\n", basename(myfile), line );
77
fwrite( EVBUFFER_DATA(buf), 1, EVBUFFER_LENGTH(buf), fp );
84
#define dbgmsg(fmt...) myDebug(__FILE__, __LINE__, ##fmt )
94
TR_MAX_OPEN_FILES = 16, /* real files, not sockets */
96
TR_RESERVED_FDS = 16 /* sockets reserved for tracker connections */
101
unsigned int isCheckedOut : 1;
102
unsigned int isWritable : 1;
103
char filename[MAX_PATH_LENGTH];
115
struct tr_openfile open[TR_MAX_OPEN_FILES];
118
static struct tr_fd_s * gFd = NULL;
127
TrOpenFile( int i, const char * filename, int write )
129
struct tr_openfile * file = &gFd->open[i];
132
tr_dbg( "Opening '%s' (%d)", filename, write );
134
/* create subfolders, if any */
136
char * tmp = tr_strdup( filename );
137
const int val = tr_mkdirp( dirname(tmp), 0700 );
140
return tr_ioErrorFromErrno( );
144
flags = write ? (O_RDWR | O_CREAT) : O_RDONLY;
146
flags |= O_LARGEFILE;
152
file->fd = open( filename, flags, 0600 );
155
tr_err( "Couldn't open '%s': %s", filename, strerror(errno) );
156
return tr_ioErrorFromErrno();
158
tr_err( "Couldn't open '%s'", filename );
159
return TR_ERROR_IO_OTHER;
117
for( j = 0; j < i; j++ )
119
#ifdef BEOS_NETSERVER
122
EVUTIL_CLOSESOCKET( s[j] );
126
tr_dbg( "%d usable file descriptors", i );
131
gFd->normalMax = i - TR_RESERVED_FDS - 10;
132
/* To be safe, in case the UI needs to write a preferences file
135
for( i = 0; i < TR_MAX_OPEN_FILES; i++ )
137
gFd->open[i].status = STATUS_INVALID;
141
/***********************************************************************
143
**********************************************************************/
144
int tr_fdFileOpen( const char * folder, const char * name, int write )
167
fileIsOpen( const struct tr_openfile * o )
175
struct tr_openfile * o = &gFd->open[i];
178
assert( i < TR_MAX_OPEN_FILES );
179
assert( fileIsOpen( o ) );
181
dbgmsg( "closing slot #%d, %s", i, o->filename );
185
tr_condSignal( gFd->cond );
189
fileIsCheckedOut( const struct tr_openfile * o )
191
return fileIsOpen(o) && o->isCheckedOut;
195
tr_fdFileOpen( const char * filename, int write )
198
struct tr_openfile * o;
200
assert( filename && *filename );
201
assert( write==0 || write==1 );
203
dbgmsg( "looking for file '%s', writable %c", filename, write?'y':'n' );
149
205
tr_lockLock( gFd->lock );
151
207
/* Is it already open? */
152
for( i = 0; i < TR_MAX_OPEN_FILES; i++ )
208
for( i=0; i<TR_MAX_OPEN_FILES; ++i )
154
if( gFd->open[i].status & STATUS_INVALID ||
155
strcmp( folder, gFd->open[i].folder ) ||
156
strcmp( name, gFd->open[i].name ) )
160
if( gFd->open[i].status & STATUS_CLOSING )
162
/* File is being closed by another thread, wait until
163
* it's done before we reopen it */
212
if( !fileIsOpen( o ) )
215
if( strcmp( filename, o->filename ) )
218
if( fileIsCheckedOut( o ) ) {
219
dbgmsg( "found it! it's open, but checked out. waiting..." );
164
220
tr_condWait( gFd->cond, gFd->lock );
168
if( gFd->open[i].write < write )
170
/* File is open read-only and needs to be closed then
171
* re-opened read-write */
225
if( write && !o->isWritable ) {
226
dbgmsg( "found it! it's open and available, but isn't writable. closing..." );
172
227
TrCloseFile( i );
231
dbgmsg( "found it! it's ready for use!" );
179
/* Can we open one more file? */
180
for( i = 0; i < TR_MAX_OPEN_FILES; i++ )
182
if( gFd->open[i].status & STATUS_INVALID )
189
/* All slots taken - close the oldest currently unused file */
237
dbgmsg( "it's not already open. looking for an open slot or an old file." );
192
date = tr_date() + 1;
240
uint64_t date = tr_date( ) + 1;
195
for( i = 0; i < TR_MAX_OPEN_FILES; i++ )
243
for( i=0; i<TR_MAX_OPEN_FILES; ++i )
197
if( !( gFd->open[i].status & STATUS_UNUSED ) )
247
if( !fileIsOpen( o ) ) {
249
dbgmsg( "found an empty slot in %d", winner );
201
if( gFd->open[i].date < date )
253
if( date > o->date ) {
204
date = gFd->open[i].date;
260
dbgmsg( "closing file '%s', slot #%d", gFd->open[winner].filename, winner );
210
261
TrCloseFile( winner );
214
265
/* All used! Wait a bit and try again */
266
dbgmsg( "everything's full! waiting for someone else to finish something" );
215
267
tr_condWait( gFd->cond, gFd->lock );
219
if( ( ret = TrOpenFile( winner, folder, name, write ) ) )
221
tr_lockUnlock( gFd->lock );
224
snprintf( gFd->open[winner].folder, MAX_PATH_LENGTH, "%s", folder );
225
snprintf( gFd->open[winner].name, MAX_PATH_LENGTH, "%s", name );
226
gFd->open[winner].write = write;
229
gFd->open[winner].status = STATUS_USED;
230
gFd->open[winner].date = tr_date();
272
o = &gFd->open[winner];
273
if( !fileIsOpen( o ) )
275
const int ret = TrOpenFile( winner, filename, write );
277
tr_lockUnlock( gFd->lock );
281
dbgmsg( "opened '%s' in slot %d, write %c", filename, winner, write?'y':'n' );
282
strlcpy( o->filename, filename, sizeof( o->filename ) );
283
o->isWritable = write;
286
dbgmsg( "checking out '%s' in slot %d", filename, winner );
288
o->date = tr_date( );
231
289
tr_lockUnlock( gFd->lock );
233
return gFd->open[winner].file;
236
/***********************************************************************
238
**********************************************************************/
239
void tr_fdFileRelease( int file )
294
tr_fdFileRelease( int file )
242
297
tr_lockLock( gFd->lock );
244
for( i = 0; i < TR_MAX_OPEN_FILES; i++ )
246
if( gFd->open[i].file == file )
248
gFd->open[i].status = STATUS_UNUSED;
299
for( i=0; i<TR_MAX_OPEN_FILES; ++i ) {
300
struct tr_openfile * o = &gFd->open[i];
301
if( o->fd == file ) {
302
dbgmsg( "releasing file '%s' in slot #%d", o->filename, i );
303
/* fsync( o->fd ); */
254
310
tr_lockUnlock( gFd->lock );
257
/***********************************************************************
259
**********************************************************************/
260
void tr_fdFileClose( const char * folder, const char * name )
264
tr_lockLock( gFd->lock );
266
for( i = 0; i < TR_MAX_OPEN_FILES; i++ )
268
if( gFd->open[i].status & STATUS_INVALID )
272
if( !strcmp( folder, gFd->open[i].folder ) &&
273
!strcmp( name, gFd->open[i].name ) )
279
tr_lockUnlock( gFd->lock );
283
/***********************************************************************
285
**********************************************************************/
293
/* Remember the priority of every socket we open, so that we can keep
294
* track of how many reserved file descriptors we are using */
295
static tr_socket_t * gSockets = NULL;
296
static int gSocketsSize = 0;
297
static int gSocketsCount = 0;
298
static void SocketSetPriority( int s, int priority )
300
if( gSocketsSize < 1 )
303
gSockets = malloc( gSocketsSize * sizeof( tr_socket_t ) );
305
if( gSocketsSize <= gSocketsCount )
308
gSockets = realloc( gSockets, gSocketsSize * sizeof( tr_socket_t ) );
310
gSockets[gSocketsCount].socket = s;
311
gSockets[gSocketsCount].priority = priority;
314
static int SocketGetPriority( int s )
317
for( i = 0; i < gSocketsCount; i++ )
318
if( gSockets[i].socket == s )
320
if( i >= gSocketsCount )
322
tr_err( "could not find that socket (%d)!", s );
325
ret = gSockets[i].priority;
327
memmove( &gSockets[i], &gSockets[i+1],
328
( gSocketsCount - i ) * sizeof( tr_socket_t ) );
332
/***********************************************************************
334
**********************************************************************/
335
int tr_fdSocketCreate( int type, int priority )
319
static tr_list * reservedSockets = NULL;
322
setSocketPriority( int fd, int isReserved )
325
tr_list_append( &reservedSockets, TR_UINT_TO_PTR(fd) );
329
socketWasReserved( int fd )
331
return tr_list_remove_data( &reservedSockets, TR_UINT_TO_PTR(fd) ) != NULL;
335
tr_fdSocketCreate( int type, int isReserved )
339
338
tr_lockLock( gFd->lock );
341
if( priority && gFd->reserved >= TR_RESERVED_FDS )
340
if( isReserved && gFd->reserved >= TR_RESERVED_FDS )
344
if( priority || ( gFd->normal < gFd->normalMax ) )
345
if( ( s = socket( AF_INET, type, 0 ) ) < 0 )
346
tr_err( "Couldn't create socket (%s)", strerror( sockerrno ) );
343
if( isReserved || ( gFd->normal < gFd->normalMax ) )
344
if( ( s = socket( AF_INET, type, 0 ) ) < 0 )
345
tr_err( "Couldn't create socket (%s)", strerror( sockerrno ) );
350
SocketSetPriority( s, priority );
349
setSocketPriority( s, isReserved );
357
assert( gFd->reserved >= 0 );
358
assert( gFd->normal >= 0 );
356
360
tr_lockUnlock( gFd->lock );
389
/***********************************************************************
391
**********************************************************************/
392
void tr_fdSocketClose( int s )
393
socketClose( int fd )
396
tr_lockLock( gFd->lock );
397
395
#ifdef BEOS_NETSERVER
400
EVUTIL_CLOSESOCKET( s );
398
EVUTIL_CLOSESOCKET( fd );
402
if( SocketGetPriority( s ) )
403
tr_fdSocketClose( int s )
405
tr_lockLock( gFd->lock );
409
if( socketWasReserved( s ) )
406
tr_lockUnlock( gFd->lock );
410
/***********************************************************************
412
**********************************************************************/
413
void tr_fdClose( void )
415
assert( gFd->reserved >= 0 );
416
assert( gFd->normal >= 0 );
418
tr_lockUnlock( gFd->lock );
423
**** Startup / Shutdown
430
int i, j, s[TR_MAX_SOCKETS];
432
assert( gFd == NULL );
434
gFd = tr_new0( struct tr_fd_s, 1 );
435
gFd->lock = tr_lockNew( );
436
gFd->cond = tr_condNew( );
438
/* count the max number of sockets we can use */
439
for( i=0; i<TR_MAX_SOCKETS; ++i )
440
if( ( s[i] = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
444
tr_dbg( "%d usable file descriptors", i );
446
/* set some fds aside for the UI or daemon to use */
447
gFd->normalMax = i - TR_RESERVED_FDS - 10;
449
for( i=0; i<TR_MAX_OPEN_FILES; ++i )
450
gFd->open[i].fd = -1;
459
for( i=0; i<TR_MAX_OPEN_FILES; ++i )
460
if( fileIsOpen( &gFd->open[i] ) )
415
463
tr_lockFree( gFd->lock );
416
464
tr_condFree( gFd->cond );
421
/***********************************************************************
423
**********************************************************************/
425
/***********************************************************************
427
***********************************************************************
429
**********************************************************************/
430
static int TrOpenFile( int i, const char * folder, const char * name, int write )
432
tr_openFile_t * file = &gFd->open[i];
434
char path[MAX_PATH_LENGTH];
438
tr_dbg( "Opening %s in %s (%d)", name, folder, write );
440
/* Make sure the parent folder exists */
441
if( stat( folder, &sb ) || !S_ISDIR( sb.st_mode ) )
443
return TR_ERROR_IO_PARENT;
446
snprintf( path, sizeof(path), "%s" TR_PATH_DELIMITER_STR "%s",
450
/* Create subfolders, if any */
453
char * p = path + strlen( folder ) + 1;
456
while( ( s = strchr( p, TR_PATH_DELIMITER ) ) )
459
if( stat( path, &sb ) )
461
if( tr_mkdir( path, 0777 ) )
463
ret = tr_ioErrorFromErrno();
464
tr_err( "Couldn't create folder '%s'", path );
470
if( !S_ISDIR( sb.st_mode ) )
472
tr_err( "Is not a folder: '%s'", path );
473
return TR_ERROR_IO_OTHER;
476
*s = TR_PATH_DELIMITER;
481
/* Now try to really open the file */
487
flags |= write ? (O_RDWR | O_CREAT) : O_RDONLY;
488
file->file = open( path, flags, 0666 );
489
if( write && ( file->file < 0 ) )
491
ret = tr_ioErrorFromErrno();
493
tr_err( "Couldn't open %s in %s: %s", name, folder, strerror(errno) );
495
tr_err( "Couldn't open %s in %s", name, folder );
502
/***********************************************************************
504
***********************************************************************
505
* We first mark it as closing then release the lock while doing so,
506
* because close() may take same time and we don't want to block other
508
**********************************************************************/
509
static void TrCloseFile( int i )
511
tr_openFile_t * file = &gFd->open[i];
513
/* If it's already being closed by another thread, just wait till
515
while( file->status & STATUS_CLOSING )
517
tr_condWait( gFd->cond, gFd->lock );
519
if( file->status & STATUS_INVALID )
524
/* Nobody is closing it already, so let's do it */
525
if( file->status & STATUS_USED )
527
tr_err( "TrCloseFile: closing a file that's being used!" );
529
tr_dbg( "Closing %s in %s (%d)", file->name, file->folder, file->write );
530
file->status = STATUS_CLOSING;
531
tr_lockUnlock( gFd->lock );
533
tr_lockLock( gFd->lock );
534
file->status = STATUS_INVALID;
535
tr_condSignal( gFd->cond );
466
tr_list_free( &reservedSockets, NULL );