diff --git a/libtransmission/fdlimit.c b/libtransmission/fdlimit.c index 030f3e902..3fe891edd 100644 --- a/libtransmission/fdlimit.c +++ b/libtransmission/fdlimit.c @@ -28,6 +28,9 @@ #define TR_RESERVED_FDS 16 /* Number of sockets reserved for connections to trackers */ +/*********************************************************************** + * Structures + **********************************************************************/ typedef struct tr_openFile_s { char folder[MAX_PATH_LENGTH]; @@ -42,10 +45,10 @@ typedef struct tr_openFile_s int status; uint64_t date; +} +tr_openFile_t; -} tr_openFile_t; - -struct tr_fd_s +typedef struct tr_fd_s { tr_lock_t lock; tr_cond_t cond; @@ -56,30 +59,37 @@ struct tr_fd_s int normalMax; tr_openFile_t open[TR_MAX_OPEN_FILES]; -}; +} +tr_fd_t; + +static tr_fd_t * gFd = NULL; /*********************************************************************** * Local prototypes **********************************************************************/ static int ErrorFromErrno(); -static int OpenFile( tr_fd_t * f, int i, char * folder, char * name, - int write ); -static void CloseFile( tr_fd_t * f, int i ); +static int OpenFile( int i, char * folder, char * name, int write ); +static void CloseFile( int i ); /*********************************************************************** * tr_fdInit **********************************************************************/ -tr_fd_t * tr_fdInit() +void tr_fdInit() { - tr_fd_t * f; int i, j, s[4096]; - f = calloc( sizeof( tr_fd_t ), 1 ); + if( gFd ) + { + tr_err( "tr_fdInit was called before!" ); + return; + } + + gFd = calloc( sizeof( tr_fd_t ), 1 ); /* Init lock and cond */ - tr_lockInit( &f->lock ); - tr_condInit( &f->cond ); + tr_lockInit( &gFd->lock ); + tr_condInit( &gFd->cond ); /* Detect the maximum number of open files or sockets */ for( i = 0; i < 4096; i++ ) @@ -96,53 +106,51 @@ tr_fd_t * tr_fdInit() tr_dbg( "%d usable file descriptors", i ); - f->reserved = 0; - f->normal = 0; + gFd->reserved = 0; + gFd->normal = 0; - f->normalMax = i - TR_RESERVED_FDS - 10; + gFd->normalMax = i - TR_RESERVED_FDS - 10; /* To be safe, in case the UI needs to write a preferences file or something */ for( i = 0; i < TR_MAX_OPEN_FILES; i++ ) { - f->open[i].status = STATUS_INVALID; + gFd->open[i].status = STATUS_INVALID; } - - return f; } /*********************************************************************** * tr_fdFileOpen **********************************************************************/ -int tr_fdFileOpen( tr_fd_t * f, char * folder, char * name, int write ) +int tr_fdFileOpen( char * folder, char * name, int write ) { int i, winner, ret; uint64_t date; - tr_lockLock( &f->lock ); + tr_lockLock( &gFd->lock ); /* Is it already open? */ for( i = 0; i < TR_MAX_OPEN_FILES; i++ ) { - if( f->open[i].status & STATUS_INVALID || - strcmp( folder, f->open[i].folder ) || - strcmp( name, f->open[i].name ) ) + if( gFd->open[i].status & STATUS_INVALID || + strcmp( folder, gFd->open[i].folder ) || + strcmp( name, gFd->open[i].name ) ) { continue; } - if( f->open[i].status & STATUS_CLOSING ) + if( gFd->open[i].status & STATUS_CLOSING ) { /* File is being closed by another thread, wait until * it's done before we reopen it */ - tr_condWait( &f->cond, &f->lock ); + tr_condWait( &gFd->cond, &gFd->lock ); i = -1; continue; } - if( f->open[i].write < write ) + if( gFd->open[i].write < write ) { /* File is open read-only and needs to be closed then * re-opened read-write */ - CloseFile( f, i ); + CloseFile( i ); continue; } winner = i; @@ -152,7 +160,7 @@ int tr_fdFileOpen( tr_fd_t * f, char * folder, char * name, int write ) /* Can we open one more file? */ for( i = 0; i < TR_MAX_OPEN_FILES; i++ ) { - if( f->open[i].status & STATUS_INVALID ) + if( gFd->open[i].status & STATUS_INVALID ) { winner = i; goto open; @@ -167,106 +175,106 @@ int tr_fdFileOpen( tr_fd_t * f, char * folder, char * name, int write ) for( i = 0; i < TR_MAX_OPEN_FILES; i++ ) { - if( !( f->open[i].status & STATUS_UNUSED ) ) + if( !( gFd->open[i].status & STATUS_UNUSED ) ) { continue; } - if( f->open[i].date < date ) + if( gFd->open[i].date < date ) { winner = i; - date = f->open[i].date; + date = gFd->open[i].date; } } if( winner >= 0 ) { - CloseFile( f, winner ); + CloseFile( winner ); goto open; } /* All used! Wait a bit and try again */ - tr_condWait( &f->cond, &f->lock ); + tr_condWait( &gFd->cond, &gFd->lock ); } open: - if( ( ret = OpenFile( f, winner, folder, name, write ) ) ) + if( ( ret = OpenFile( winner, folder, name, write ) ) ) { - tr_lockUnlock( &f->lock ); + tr_lockUnlock( &gFd->lock ); return ret; } - snprintf( f->open[winner].folder, MAX_PATH_LENGTH, "%s", folder ); - snprintf( f->open[winner].name, MAX_PATH_LENGTH, "%s", name ); - f->open[winner].write = write; + 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; done: - f->open[winner].status = STATUS_USED; - f->open[winner].date = tr_date(); - tr_lockUnlock( &f->lock ); + gFd->open[winner].status = STATUS_USED; + gFd->open[winner].date = tr_date(); + tr_lockUnlock( &gFd->lock ); - return f->open[winner].file; + return gFd->open[winner].file; } /*********************************************************************** * tr_fdFileRelease **********************************************************************/ -void tr_fdFileRelease( tr_fd_t * f, int file ) +void tr_fdFileRelease( int file ) { int i; - tr_lockLock( &f->lock ); + tr_lockLock( &gFd->lock ); for( i = 0; i < TR_MAX_OPEN_FILES; i++ ) { - if( f->open[i].file == file ) + if( gFd->open[i].file == file ) { - f->open[i].status = STATUS_UNUSED; + gFd->open[i].status = STATUS_UNUSED; break; } } - tr_condSignal( &f->cond ); - tr_lockUnlock( &f->lock ); + tr_condSignal( &gFd->cond ); + tr_lockUnlock( &gFd->lock ); } /*********************************************************************** * tr_fdFileClose **********************************************************************/ -void tr_fdFileClose( tr_fd_t * f, char * folder, char * name ) +void tr_fdFileClose( char * folder, char * name ) { int i; - tr_lockLock( &f->lock ); + tr_lockLock( &gFd->lock ); for( i = 0; i < TR_MAX_OPEN_FILES; i++ ) { - if( f->open[i].status & STATUS_INVALID ) + if( gFd->open[i].status & STATUS_INVALID ) { continue; } - if( !strcmp( folder, f->open[i].folder ) && - !strcmp( name, f->open[i].name ) ) + if( !strcmp( folder, gFd->open[i].folder ) && + !strcmp( name, gFd->open[i].name ) ) { - CloseFile( f, i ); + CloseFile( i ); } } - tr_lockUnlock( &f->lock ); + tr_lockUnlock( &gFd->lock ); } /*********************************************************************** * tr_fdSocketWillCreate **********************************************************************/ -int tr_fdSocketWillCreate( tr_fd_t * f, int reserved ) +int tr_fdSocketWillCreate( int reserved ) { int ret; - tr_lockLock( &f->lock ); + tr_lockLock( &gFd->lock ); if( reserved ) { - if( f->reserved < TR_RESERVED_FDS ) + if( gFd->reserved < TR_RESERVED_FDS ) { ret = 0; - (f->reserved)++; + (gFd->reserved)++; } else { @@ -275,10 +283,10 @@ int tr_fdSocketWillCreate( tr_fd_t * f, int reserved ) } else { - if( f->normal < f->normalMax ) + if( gFd->normal < gFd->normalMax ) { ret = 0; - (f->normal)++; + (gFd->normal)++; } else { @@ -286,7 +294,7 @@ int tr_fdSocketWillCreate( tr_fd_t * f, int reserved ) } } - tr_lockUnlock( &f->lock ); + tr_lockUnlock( &gFd->lock ); return ret; } @@ -294,30 +302,30 @@ int tr_fdSocketWillCreate( tr_fd_t * f, int reserved ) /*********************************************************************** * tr_fdSocketClosed **********************************************************************/ -void tr_fdSocketClosed( tr_fd_t * f, int reserved ) +void tr_fdSocketClosed( int reserved ) { - tr_lockLock( &f->lock ); + tr_lockLock( &gFd->lock ); if( reserved ) { - (f->reserved)--; + (gFd->reserved)--; } else { - (f->normal)--; + (gFd->normal)--; } - tr_lockUnlock( &f->lock ); + tr_lockUnlock( &gFd->lock ); } /*********************************************************************** * tr_fdClose **********************************************************************/ -void tr_fdClose( tr_fd_t * f ) +void tr_fdClose() { - tr_lockClose( &f->lock ); - tr_condClose( &f->cond ); - free( f ); + tr_lockClose( &gFd->lock ); + tr_condClose( &gFd->cond ); + free( gFd ); } @@ -340,10 +348,9 @@ static int ErrorFromErrno() *********************************************************************** * **********************************************************************/ -static int OpenFile( tr_fd_t * f, int i, char * folder, char * name, - int write ) +static int OpenFile( int i, char * folder, char * name, int write ) { - tr_openFile_t * file = &f->open[i]; + tr_openFile_t * file = &gFd->open[i]; struct stat sb; char * path; @@ -410,15 +417,15 @@ static int OpenFile( tr_fd_t * f, int i, char * folder, char * name, * because close() may take same time and we don't want to block other * threads. **********************************************************************/ -static void CloseFile( tr_fd_t * f, int i ) +static void CloseFile( int i ) { - tr_openFile_t * file = &f->open[i]; + tr_openFile_t * file = &gFd->open[i]; /* If it's already being closed by another thread, just wait till * it is done */ while( file->status & STATUS_CLOSING ) { - tr_condWait( &f->cond, &f->lock ); + tr_condWait( &gFd->cond, &gFd->lock ); } if( file->status & STATUS_INVALID ) { @@ -432,10 +439,10 @@ static void CloseFile( tr_fd_t * f, int i ) } tr_dbg( "Closing %s in %s (%d)", file->name, file->folder, file->write ); file->status = STATUS_CLOSING; - tr_lockUnlock( &f->lock ); + tr_lockUnlock( &gFd->lock ); close( file->file ); - tr_lockLock( &f->lock ); + tr_lockLock( &gFd->lock ); file->status = STATUS_INVALID; - tr_condSignal( &f->cond ); + tr_condSignal( &gFd->cond ); } diff --git a/libtransmission/fdlimit.h b/libtransmission/fdlimit.h index 40c0f959f..86ff3e3ee 100644 --- a/libtransmission/fdlimit.h +++ b/libtransmission/fdlimit.h @@ -22,14 +22,12 @@ * DEALINGS IN THE SOFTWARE. *****************************************************************************/ -typedef struct tr_fd_s tr_fd_t; - /*********************************************************************** * tr_fdInit *********************************************************************** * Detect the maximum number of open files and initializes things. **********************************************************************/ -tr_fd_t * tr_fdInit(); +void tr_fdInit(); /*********************************************************************** * tr_fdFileOpen @@ -41,7 +39,7 @@ tr_fd_t * tr_fdInit(); * Returns the file descriptor if successful, otherwise returns * one of the TR_ERROR_IO_*. **********************************************************************/ -int tr_fdFileOpen( tr_fd_t *, char * folder, char * name, int write ); +int tr_fdFileOpen( char * folder, char * name, int write ); /*********************************************************************** * tr_fdFileRelease @@ -49,7 +47,7 @@ int tr_fdFileOpen( tr_fd_t *, char * folder, char * name, int write ); * Indicates that the file whose descriptor is 'file' is unused at the * moment and can safely be closed. **********************************************************************/ -void tr_fdFileRelease( tr_fd_t *, int file ); +void tr_fdFileRelease( int file ); /*********************************************************************** * tr_fdFileClose @@ -57,21 +55,17 @@ void tr_fdFileRelease( tr_fd_t *, int file ); * If the file 'name' in directory 'folder' was open, closes it, * flushing data on disk. **********************************************************************/ -void tr_fdFileClose( tr_fd_t *, char * folder, char * name ); +void tr_fdFileClose( char * folder, char * name ); /*********************************************************************** * tr_fdSocketWillCreate - *********************************************************************** - * **********************************************************************/ -int tr_fdSocketWillCreate( tr_fd_t *, int ); +int tr_fdSocketWillCreate( int ); /*********************************************************************** * tr_fdSocketClosed - *********************************************************************** - * **********************************************************************/ -void tr_fdSocketClosed( tr_fd_t *, int ); +void tr_fdSocketClosed( int ); /*********************************************************************** @@ -79,4 +73,5 @@ void tr_fdSocketClosed( tr_fd_t *, int ); *********************************************************************** * Frees resources allocated by tr_fdInit. **********************************************************************/ -void tr_fdClose( tr_fd_t * ); +void tr_fdClose(); + diff --git a/libtransmission/inout.c b/libtransmission/inout.c index 20857404f..108fc194b 100644 --- a/libtransmission/inout.c +++ b/libtransmission/inout.c @@ -297,8 +297,7 @@ static void closeFiles( tr_io_t * io ) for( i = 0; i < inf->fileCount; i++ ) { - tr_fdFileClose( tor->fdlimit, tor->destination, - inf->files[i].name ); + tr_fdFileClose( tor->destination, inf->files[i].name ); } } @@ -368,8 +367,8 @@ static int readOrWriteBytes( tr_io_t * io, uint64_t offset, int size, if( cur > 0 ) { /* Now let's get a descriptor on the file... */ - file = tr_fdFileOpen( tor->fdlimit, tor->destination, - inf->files[i].name, isWrite ); + file = tr_fdFileOpen( tor->destination, inf->files[i].name, + isWrite ); if( file < 0 ) { ret = file; @@ -379,7 +378,7 @@ static int readOrWriteBytes( tr_io_t * io, uint64_t offset, int size, /* seek to the right offset... */ if( lseek( file, offset, SEEK_SET ) < 0 ) { - tr_fdFileRelease( tor->fdlimit, file ); + tr_fdFileRelease( file ); ret = TR_ERROR_IO_OTHER; goto cleanup; } @@ -387,13 +386,13 @@ static int readOrWriteBytes( tr_io_t * io, uint64_t offset, int size, /* do what we are here to do... */ if( readOrWrite( file, buf, cur ) != cur ) { - tr_fdFileRelease( tor->fdlimit, file ); + tr_fdFileRelease( file ); ret = TR_ERROR_IO_OTHER; goto cleanup; } /* and release the descriptor. */ - tr_fdFileRelease( tor->fdlimit, file ); + tr_fdFileRelease( file ); } /* 'cur' bytes done, 'size - cur' bytes to go with the next file */ diff --git a/libtransmission/internal.h b/libtransmission/internal.h index 5af90a655..1b1ead331 100644 --- a/libtransmission/internal.h +++ b/libtransmission/internal.h @@ -150,7 +150,6 @@ struct tr_torrent_s tr_ratecontrol_t * upload; tr_ratecontrol_t * download; tr_ratecontrol_t * swarmspeed; - tr_fd_t * fdlimit; int status; int error; @@ -209,7 +208,6 @@ struct tr_handle_s int bindPort; int uploadLimit; int downloadLimit; - tr_fd_t * fdlimit; tr_shared_t * shared; char id[21]; diff --git a/libtransmission/natpmp.c b/libtransmission/natpmp.c index 213b91714..419857928 100644 --- a/libtransmission/natpmp.c +++ b/libtransmission/natpmp.c @@ -64,7 +64,6 @@ typedef struct tr_natpmp_req_s uint64_t retry; uint64_t timeout; int port; - tr_fd_t * fdlimit; tr_natpmp_uptime_t * uptime; } tr_natpmp_req_t; @@ -82,7 +81,6 @@ struct tr_natpmp_s struct in_addr dest; int newport; int mappedport; - tr_fd_t * fdlimit; tr_lock_t lock; uint64_t renew; tr_natpmp_req_t * req; @@ -93,14 +91,14 @@ struct tr_natpmp_s static int checktime( tr_natpmp_uptime_t * uptime, uint32_t seen ); static void -killsock( int * fd, tr_fd_t * fdlimit ); +killsock( int * fd ); static tr_natpmp_req_t * -newreq( int adding, struct in_addr addr, int port, tr_fd_t * fdlimit, +newreq( int adding, struct in_addr addr, int port, tr_natpmp_uptime_t * uptime ); static tr_tristate_t pulsereq( tr_natpmp_req_t * req, uint64_t * renew ); static int -mcastsetup( tr_fd_t * fdlimit ); +mcastsetup(); static void mcastpulse( tr_natpmp_t * pmp ); static void @@ -112,7 +110,7 @@ readrequest( uint8_t * buf, int len, int adding, int port, tr_natpmp_uptime_t * uptime, uint64_t * renew, int * tmpfail ); tr_natpmp_t * -tr_natpmpInit( tr_fd_t * fdlimit ) +tr_natpmpInit() { tr_natpmp_t * pmp; @@ -123,7 +121,6 @@ tr_natpmpInit( tr_fd_t * fdlimit ) } pmp->state = PMP_STATE_IDLE; - pmp->fdlimit = fdlimit; pmp->mcastfd = -1; if( tr_getDefaultRoute( &pmp->dest ) || INADDR_ANY == pmp->dest.s_addr ) @@ -158,7 +155,7 @@ tr_natpmpStart( tr_natpmp_t * pmp ) pmp->active = 1; if( 0 > pmp->mcastfd ) { - pmp->mcastfd = mcastsetup( pmp->fdlimit ); + pmp->mcastfd = mcastsetup(); } /* XXX should I change state? */ } @@ -175,7 +172,7 @@ tr_natpmpStop( tr_natpmp_t * pmp ) { tr_inf( "stopping nat-pmp" ); pmp->active = 0; - killsock( &pmp->mcastfd, pmp->fdlimit ); + killsock( &pmp->mcastfd ); switch( pmp->state ) { case PMP_STATE_IDLE: @@ -319,7 +316,7 @@ tr_natpmpPulse( tr_natpmp_t * pmp ) else { pmp->req = newreq( 1, pmp->dest, pmp->newport, - pmp->fdlimit, &pmp->uptime ); + &pmp->uptime ); if( NULL == pmp->req ) { pmp->state = PMP_STATE_FAILED; @@ -372,7 +369,7 @@ tr_natpmpPulse( tr_natpmp_t * pmp ) { assert( 0 < pmp->mappedport ); pmp->req = newreq( 0, pmp->dest, pmp->mappedport, - pmp->fdlimit, &pmp->uptime ); + &pmp->uptime ); if( NULL == pmp->req ) { pmp->state = PMP_STATE_FAILED; @@ -468,18 +465,18 @@ checktime( tr_natpmp_uptime_t * uptime, uint32_t cursecs ) } static void -killsock( int * fd, tr_fd_t * fdlimit ) +killsock( int * fd ) { if( 0 <= *fd ) { tr_netClose( *fd ); *fd = -1; - tr_fdSocketClosed( fdlimit, 0 ); + tr_fdSocketClosed( 0 ); } } static tr_natpmp_req_t * -newreq( int adding, struct in_addr addr, int port, tr_fd_t * fdlimit, +newreq( int adding, struct in_addr addr, int port, tr_natpmp_uptime_t * uptime ) { tr_natpmp_req_t * ret; @@ -491,7 +488,7 @@ newreq( int adding, struct in_addr addr, int port, tr_fd_t * fdlimit, goto err; } ret->fd = -1; - if( tr_fdSocketWillCreate( fdlimit, 0 ) ) + if( tr_fdSocketWillCreate( 0 ) ) { goto err; } @@ -511,7 +508,6 @@ newreq( int adding, struct in_addr addr, int port, tr_fd_t * fdlimit, ret->retry = now + PMP_INITIAL_DELAY; ret->timeout = now + PMP_TOTAL_DELAY; ret->port = port; - ret->fdlimit = fdlimit; ret->uptime = uptime; return ret; @@ -519,7 +515,7 @@ newreq( int adding, struct in_addr addr, int port, tr_fd_t * fdlimit, err: if( NULL != ret ) { - killsock( &ret->fd, fdlimit ); + killsock( &ret->fd ); } free( ret ); @@ -582,12 +578,12 @@ pulsereq( tr_natpmp_req_t * req, uint64_t * renew ) } static int -mcastsetup( tr_fd_t * fdlimit ) +mcastsetup() { int fd; struct in_addr addr; - if( tr_fdSocketWillCreate( fdlimit, 0 ) ) + if( tr_fdSocketWillCreate( 0 ) ) { return -1; } @@ -596,7 +592,7 @@ mcastsetup( tr_fd_t * fdlimit ) fd = tr_netMcastOpen( PMP_PORT, addr ); if( 0 > fd ) { - tr_fdSocketClosed( fdlimit, 0 ); + tr_fdSocketClosed( 0 ); return -1; } @@ -621,7 +617,7 @@ mcastpulse( tr_natpmp_t * pmp ) else if( TR_NET_CLOSE & res ) { tr_err( "error reading nat-pmp multicast message" ); - killsock( &pmp->mcastfd, pmp->fdlimit ); + killsock( &pmp->mcastfd ); return; } @@ -648,7 +644,7 @@ killreq( tr_natpmp_req_t ** req ) { if( NULL != *req ) { - killsock( &(*req)->fd, (*req)->fdlimit ); + killsock( &(*req)->fd ); free( *req ); *req = NULL; } diff --git a/libtransmission/natpmp.h b/libtransmission/natpmp.h index 3ec6a099a..f7abc0d9d 100644 --- a/libtransmission/natpmp.h +++ b/libtransmission/natpmp.h @@ -27,7 +27,7 @@ typedef struct tr_natpmp_s tr_natpmp_t; -tr_natpmp_t * tr_natpmpInit( tr_fd_t * ); +tr_natpmp_t * tr_natpmpInit(); void tr_natpmpStart( tr_natpmp_t * ); void tr_natpmpStop( tr_natpmp_t * ); int tr_natpmpStatus( tr_natpmp_t * ); diff --git a/libtransmission/net.c b/libtransmission/net.c index fa7e469de..9ec421617 100644 --- a/libtransmission/net.c +++ b/libtransmission/net.c @@ -41,7 +41,6 @@ int tr_netResolve( const char * address, struct in_addr * addr ) return ( addr->s_addr == 0xFFFFFFFF ); } -/* TODO: Make this code reentrant */ static tr_thread_t resolveThread; static tr_lock_t resolveLock; static tr_cond_t resolveCond; diff --git a/libtransmission/peer.c b/libtransmission/peer.c index 17972d931..a3036382f 100644 --- a/libtransmission/peer.c +++ b/libtransmission/peer.c @@ -185,8 +185,7 @@ void tr_peerDestroy( tr_peer_t * peer ) if( peer->status > PEER_STATUS_IDLE ) { tr_netClose( peer->socket ); - if( tor ) - tr_fdSocketClosed( tor->fdlimit, 0 ); /* XXX */ + tr_fdSocketClosed( 0 ); } tr_rcClose( peer->download ); tr_rcClose( peer->upload ); diff --git a/libtransmission/peerutils.h b/libtransmission/peerutils.h index 9175c134e..bb32e9a0a 100644 --- a/libtransmission/peerutils.h +++ b/libtransmission/peerutils.h @@ -97,13 +97,13 @@ static int checkPeer( tr_peer_t * peer ) /* Connect */ if( ( peer->status & PEER_STATUS_IDLE ) && - !tr_fdSocketWillCreate( tor->fdlimit, 0 ) ) + !tr_fdSocketWillCreate( 0 ) ) { peer->socket = tr_netOpenTCP( peer->addr, peer->port ); if( peer->socket < 0 ) { peer_dbg( "connection failed" ); - tr_fdSocketClosed( tor->fdlimit, 0 ); + tr_fdSocketClosed( 0 ); return TR_ERROR; } peer->status = PEER_STATUS_CONNECTING; diff --git a/libtransmission/shared.c b/libtransmission/shared.c index 979ca1c0d..7e7dba8e3 100644 --- a/libtransmission/shared.c +++ b/libtransmission/shared.c @@ -72,8 +72,8 @@ tr_shared_t * tr_sharedInit( tr_handle_t * h ) s->bindPort = -1; s->bindSocket = -1; - s->natpmp = tr_natpmpInit( h->fdlimit ); - s->upnp = tr_upnpInit( h->fdlimit ); + s->natpmp = tr_natpmpInit(); + s->upnp = tr_upnpInit(); s->choking = tr_chokingInit( h ); /* Launch the thread */ @@ -90,7 +90,6 @@ tr_shared_t * tr_sharedInit( tr_handle_t * h ) **********************************************************************/ void tr_sharedClose( tr_shared_t * s ) { - tr_handle_t * h = s->h; int ii; /* Stop the thread */ @@ -105,7 +104,7 @@ void tr_sharedClose( tr_shared_t * s ) if( s->bindSocket > -1 ) { tr_netClose( s->bindSocket ); - tr_fdSocketClosed( h->fdlimit, 0 ); + tr_fdSocketClosed( 0 ); } tr_lockClose( &s->lock ); tr_natpmpClose( s->natpmp ); @@ -157,17 +156,17 @@ void tr_sharedSetPort( tr_shared_t * s, int port ) if( s->bindSocket > -1 ) { tr_netClose( s->bindSocket ); - tr_fdSocketClosed( h->fdlimit, 0 ); + tr_fdSocketClosed( 0 ); } /* Create the new one */ - if( !tr_fdSocketWillCreate( h->fdlimit, 0 ) ) + if( !tr_fdSocketWillCreate( 0 ) ) { /* XXX should handle failure here in a better way */ s->bindSocket = tr_netBindTCP( port ); if( 0 > s->bindSocket ) { - tr_fdSocketClosed( h->fdlimit, 0 ); + tr_fdSocketClosed( 0 ); } else { @@ -306,7 +305,6 @@ static void SharedLoop( void * _s ) **********************************************************************/ static void AcceptPeers( tr_shared_t * s ) { - tr_handle_t * h = s->h; int socket; in_port_t port; struct in_addr addr; @@ -314,7 +312,7 @@ static void AcceptPeers( tr_shared_t * s ) for( ;; ) { if( s->bindSocket < 0 || s->peerCount >= MAX_PEER_COUNT || - tr_fdSocketWillCreate( h->fdlimit, 0 ) ) + tr_fdSocketWillCreate( 0 ) ) { break;; } @@ -322,7 +320,7 @@ static void AcceptPeers( tr_shared_t * s ) socket = tr_netAccept( s->bindSocket, &addr, &port ); if( socket < 0 ) { - tr_fdSocketClosed( h->fdlimit, 0 ); + tr_fdSocketClosed( 0 ); break; } s->peers[s->peerCount++] = tr_peerInit( addr, port, socket ); diff --git a/libtransmission/torrent.c b/libtransmission/torrent.c index 14b29d8d9..d1a4be713 100644 --- a/libtransmission/torrent.c +++ b/libtransmission/torrent.c @@ -132,7 +132,6 @@ static tr_torrent_t * torrentRealInit( tr_handle_t * h, tr_torrent_t * tor, tr_lockInit( &tor->lock ); tr_condInit( &tor->cond ); - tor->fdlimit = h->fdlimit; tor->upload = tr_rcInit(); tor->download = tr_rcInit(); tor->swarmspeed = tr_rcInit(); diff --git a/libtransmission/tracker.c b/libtransmission/tracker.c index ed29b45da..f31ddd433 100644 --- a/libtransmission/tracker.c +++ b/libtransmission/tracker.c @@ -97,7 +97,7 @@ static tr_http_t * getScrapeQuery ( tr_tracker_t * tc ); static void readAnswer ( tr_tracker_t * tc, const char *, int, int * peerCount, uint8_t ** peerCompact ); static void readScrapeAnswer ( tr_tracker_t * tc, const char *, int ); -static void killHttp ( tr_http_t ** http, tr_fd_t * fdlimit ); +static void killHttp ( tr_http_t ** http ); tr_tracker_t * tr_trackerInit( tr_torrent_t * tor ) { @@ -345,7 +345,7 @@ void tr_trackerAnnouncePulse( tr_tracker_t * tc, int * peerCount, tc->randOffset = tr_rand( 60000 ); - if( tr_fdSocketWillCreate( tor->fdlimit, 1 ) ) + if( tr_fdSocketWillCreate( 1 ) ) { return; } @@ -442,7 +442,7 @@ void tr_trackerAnnouncePulse( tr_tracker_t * tc, int * peerCount, break; case TR_NET_ERROR: - killHttp( &tc->http, tor->fdlimit ); + killHttp( &tc->http ); tc->dateTry = tr_date(); failureAnnouncing( tc ); @@ -452,14 +452,14 @@ void tr_trackerAnnouncePulse( tr_tracker_t * tc, int * peerCount, case TR_NET_OK: readAnswer( tc, data, len, peerCount, peerCompact ); - killHttp( &tc->http, tor->fdlimit ); + killHttp( &tc->http ); break; } } if( ( NULL == tc->httpScrape ) && shouldScrape( tc ) ) { - if( tr_fdSocketWillCreate( tor->fdlimit, 1 ) ) + if( tr_fdSocketWillCreate( 1 ) ) { return; } @@ -496,13 +496,13 @@ void tr_trackerAnnouncePulse( tr_tracker_t * tc, int * peerCount, break; case TR_NET_ERROR: - killHttp( &tc->httpScrape, tor->fdlimit ); + killHttp( &tc->httpScrape ); tc->lastScrapeFailed = 1; break; case TR_NET_OK: readScrapeAnswer( tc, data, len ); - killHttp( &tc->httpScrape, tor->fdlimit ); + killHttp( &tc->httpScrape ); break; } } @@ -519,11 +519,9 @@ void tr_trackerCompleted( tr_tracker_t * tc ) void tr_trackerStopped( tr_tracker_t * tc ) { - tr_torrent_t * tor = tc->tor; - /* If we are already sending a query at the moment, we need to reconnect */ - killHttp( &tc->http, tor->fdlimit ); + killHttp( &tc->http ); tc->started = 0; tc->completed = 0; @@ -540,8 +538,8 @@ void tr_trackerClose( tr_tracker_t * tc ) tr_announce_list_ptr_t * cur, * curFree; int ii; - killHttp( &tc->http, tor->fdlimit ); - killHttp( &tc->httpScrape, tor->fdlimit ); + killHttp( &tc->http ); + killHttp( &tc->httpScrape ); for( ii = 0; ii < inf->trackerTiers; ii++ ) { @@ -1162,12 +1160,12 @@ scrapeDone: return ret; } -static void killHttp( tr_http_t ** http, tr_fd_t * fdlimit ) +static void killHttp( tr_http_t ** http ) { if( NULL != *http ) { tr_httpClose( *http ); - tr_fdSocketClosed( fdlimit, 1 ); + tr_fdSocketClosed( 1 ); *http = NULL; } } diff --git a/libtransmission/transmission.c b/libtransmission/transmission.c index ae985d4e8..d36b89b40 100644 --- a/libtransmission/transmission.c +++ b/libtransmission/transmission.c @@ -64,7 +64,7 @@ tr_handle_t * tr_init() h->uploadLimit = -1; h->downloadLimit = -1; - h->fdlimit = tr_fdInit(); + tr_fdInit(); h->shared = tr_sharedInit( h ); return h; @@ -137,7 +137,7 @@ void tr_torrentIterate( tr_handle_t * h, tr_callback_t func, void * d ) void tr_close( tr_handle_t * h ) { tr_sharedClose( h->shared ); - tr_fdClose( h->fdlimit ); + tr_fdClose(); free( h ); tr_netResolveThreadClose(); diff --git a/libtransmission/upnp.c b/libtransmission/upnp.c index 0902af728..5f91edd3d 100644 --- a/libtransmission/upnp.c +++ b/libtransmission/upnp.c @@ -90,18 +90,17 @@ struct tr_upnp_s unsigned int active : 1; unsigned int discovering : 1; tr_upnp_device_t * devices; - tr_fd_t * fdlimit; tr_lock_t lock; }; static int -sendSSDP( tr_fd_t * fdlimit, int fd ); +sendSSDP( int fd ); static int -mcastStart( tr_fd_t * fdlimit ); +mcastStart(); static void -killSock( tr_fd_t * fdlimit, int * sock ); +killSock( int * sock ); static void -killHttp( tr_fd_t * fdlimit, tr_http_t ** http ); +killHttp( tr_http_t ** http ); static int watchSSDP( tr_upnp_device_t ** devices, int fd ); static tr_tristate_t @@ -112,16 +111,16 @@ static void deviceAdd( tr_upnp_device_t ** first, const char * id, int idLen, const char * url, int urlLen ); static void -deviceRemove( tr_upnp_device_t ** prevptr, tr_fd_t * fdlimit ); +deviceRemove( tr_upnp_device_t ** prevptr ); static int deviceStop( tr_upnp_device_t * dev ); static int -devicePulse( tr_upnp_device_t * dev, tr_fd_t * fdlimit, int port ); +devicePulse( tr_upnp_device_t * dev, int port ); static int -devicePulseHttp( tr_upnp_device_t * dev, tr_fd_t * fdlimit, +devicePulseHttp( tr_upnp_device_t * dev, const char ** body, int * len ); static tr_http_t * -devicePulseGetHttp( tr_upnp_device_t * dev, tr_fd_t * fdlimit ); +devicePulseGetHttp( tr_upnp_device_t * dev ); static int parseRoot( const char *buf, int len, char ** soap, char ** scpd ); static void @@ -155,7 +154,7 @@ actionLookup( tr_upnp_action_t * action, const char * key, int len, char dir, int getname ); tr_upnp_t * -tr_upnpInit( tr_fd_t * fdlimit ) +tr_upnpInit() { tr_upnp_t * upnp; @@ -167,7 +166,6 @@ tr_upnpInit( tr_fd_t * fdlimit ) upnp->infd = -1; upnp->outfd = -1; - upnp->fdlimit = fdlimit; tr_lockInit( &upnp->lock ); @@ -184,7 +182,7 @@ tr_upnpStart( tr_upnp_t * upnp ) tr_inf( "starting upnp" ); upnp->active = 1; upnp->discovering = 1; - upnp->infd = mcastStart( upnp->fdlimit ); + upnp->infd = mcastStart(); upnp->lastdiscover = 0; upnp->lastdelay = SSDP_FIRST_DELAY / 2; } @@ -201,8 +199,8 @@ tr_upnpStop( tr_upnp_t * upnp ) { tr_inf( "stopping upnp" ); upnp->active = 0; - killSock( upnp->fdlimit, &upnp->infd ); - killSock( upnp->fdlimit, &upnp->outfd ); + killSock( &upnp->infd ); + killSock( &upnp->outfd ); } tr_lockUnlock( &upnp->lock ); @@ -264,7 +262,7 @@ tr_upnpClose( tr_upnp_t * upnp ) tr_lockLock( &upnp->lock ); while( NULL != upnp->devices ) { - deviceRemove( &upnp->devices, upnp->fdlimit ); + deviceRemove( &upnp->devices ); } tr_lockClose( &upnp->lock ); @@ -284,7 +282,7 @@ tr_upnpPulse( tr_upnp_t * upnp ) upnp->discovering = 1; for( ii = &upnp->devices; NULL != *ii; ii = &(*ii)->next ) { - if( devicePulse( *ii, upnp->fdlimit, upnp->port ) ) + if( devicePulse( *ii, upnp->port ) ) { upnp->discovering = 0; } @@ -294,7 +292,7 @@ tr_upnpPulse( tr_upnp_t * upnp ) if( upnp->discovering && upnp->lastdelay + upnp->lastdiscover < tr_date() ) { - upnp->outfd = sendSSDP( upnp->fdlimit, upnp->outfd ); + upnp->outfd = sendSSDP( upnp->outfd ); upnp->lastdiscover = tr_date(); upnp->lastdelay = MIN( upnp->lastdelay * 2, SSDP_MAX_DELAY ); } @@ -303,7 +301,7 @@ tr_upnpPulse( tr_upnp_t * upnp ) watchSSDP( &upnp->devices, upnp->infd ); if( watchSSDP( &upnp->devices, upnp->outfd ) ) { - killSock( upnp->fdlimit, &upnp->outfd ); + killSock( &upnp->outfd ); } } else @@ -314,11 +312,11 @@ tr_upnpPulse( tr_upnp_t * upnp ) { if( deviceStop( *ii ) ) { - deviceRemove( ii, upnp->fdlimit ); + deviceRemove( ii ); } else { - devicePulse( *ii, upnp->fdlimit, 0 ); + devicePulse( *ii, 0 ); ii = &(*ii)->next; } } @@ -328,7 +326,7 @@ tr_upnpPulse( tr_upnp_t * upnp ) } static int -sendSSDP( tr_fd_t * fdlimit, int fd ) +sendSSDP( int fd ) { char buf[102]; int len; @@ -336,14 +334,14 @@ sendSSDP( tr_fd_t * fdlimit, int fd ) if( 0 > fd ) { - if( tr_fdSocketWillCreate( fdlimit, 0 ) ) + if( tr_fdSocketWillCreate( 0 ) ) { return -1; } fd = tr_netBindUDP( 0 ); if( 0 > fd ) { - tr_fdSocketClosed( fdlimit, 0 ); + tr_fdSocketClosed( 0 ); return -1; } } @@ -375,7 +373,7 @@ sendSSDP( tr_fd_t * fdlimit, int fd ) tr_err( "Could not send SSDP discover message (%s)", strerror( errno ) ); } - killSock( fdlimit, &fd ); + killSock( &fd ); return -1; } @@ -383,12 +381,12 @@ sendSSDP( tr_fd_t * fdlimit, int fd ) } static int -mcastStart( tr_fd_t * fdlimit ) +mcastStart() { int fd; struct in_addr addr; - if( tr_fdSocketWillCreate( fdlimit, 0 ) ) + if( tr_fdSocketWillCreate( 0 ) ) { return -1; } @@ -397,7 +395,7 @@ mcastStart( tr_fd_t * fdlimit ) fd = tr_netMcastOpen( SSDP_PORT, addr ); if( 0 > fd ) { - tr_fdSocketClosed( fdlimit, 0 ); + tr_fdSocketClosed( 0 ); return -1; } @@ -405,22 +403,22 @@ mcastStart( tr_fd_t * fdlimit ) } static void -killSock( tr_fd_t * fdlimit, int * sock ) +killSock( int * sock ) { if( 0 <= *sock ) { tr_netClose( *sock ); *sock = -1; - tr_fdSocketClosed( fdlimit, 0 ); + tr_fdSocketClosed( 0 ); } } static void -killHttp( tr_fd_t * fdlimit, tr_http_t ** http ) +killHttp( tr_http_t ** http ) { tr_httpClose( *http ); *http = NULL; - tr_fdSocketClosed( fdlimit, 0 ); + tr_fdSocketClosed( 0 ); } static int @@ -590,7 +588,7 @@ deviceAdd( tr_upnp_device_t ** first, const char * id, int idLen, } static void -deviceRemove( tr_upnp_device_t ** prevptr, tr_fd_t * fdlimit ) +deviceRemove( tr_upnp_device_t ** prevptr ) { tr_upnp_device_t * dead; @@ -607,7 +605,7 @@ deviceRemove( tr_upnp_device_t ** prevptr, tr_fd_t * fdlimit ) free( dead->myaddr ); if( NULL != dead->http ) { - killHttp( fdlimit, &dead->http ); + killHttp( &dead->http ); } actionFree( &dead->getcmd ); actionFree( &dead->addcmd ); @@ -634,7 +632,7 @@ deviceStop( tr_upnp_device_t * dev ) } static int -devicePulse( tr_upnp_device_t * dev, tr_fd_t * fdlimit, int port ) +devicePulse( tr_upnp_device_t * dev, int port ) { const char * body; int len, code; @@ -675,7 +673,7 @@ devicePulse( tr_upnp_device_t * dev, tr_fd_t * fdlimit, int port ) len = 0; body = NULL; - code = devicePulseHttp( dev, fdlimit, &body, &len ); + code = devicePulseHttp( dev, &body, &len ); if( 0 > code ) { return 1; @@ -687,7 +685,7 @@ devicePulse( tr_upnp_device_t * dev, tr_fd_t * fdlimit, int port ) dev->host, dev->state ); dev->state = UPNPDEV_STATE_ERROR; dev->looping = 0; - killHttp( fdlimit, &dev->http ); + killHttp( &dev->http ); return 1; } @@ -821,7 +819,7 @@ devicePulse( tr_upnp_device_t * dev, tr_fd_t * fdlimit, int port ) } dev->lastrequest = tr_date(); - killHttp( fdlimit, &dev->http ); + killHttp( &dev->http ); if( UPNPDEV_STATE_ERROR == dev->state ) { @@ -847,12 +845,12 @@ makeHttp( int method, const char * host, int port, const char * path ) } static tr_http_t * -devicePulseGetHttp( tr_upnp_device_t * dev, tr_fd_t * fdlimit ) +devicePulseGetHttp( tr_upnp_device_t * dev ) { tr_http_t * ret; char numstr[6]; - if( tr_fdSocketWillCreate( fdlimit, 0 ) ) + if( tr_fdSocketWillCreate( 0 ) ) { return NULL; } @@ -917,14 +915,14 @@ devicePulseGetHttp( tr_upnp_device_t * dev, tr_fd_t * fdlimit ) if( NULL == ret ) { - tr_fdSocketClosed( fdlimit, 0 ); + tr_fdSocketClosed( 0 ); } return ret; } static int -devicePulseHttp( tr_upnp_device_t * dev, tr_fd_t * fdlimit, +devicePulseHttp( tr_upnp_device_t * dev, const char ** body, int * len ) { const char * headers; @@ -937,7 +935,7 @@ devicePulseHttp( tr_upnp_device_t * dev, tr_fd_t * fdlimit, return -1; } dev->lastrequest = tr_date(); - dev->http = devicePulseGetHttp( dev, fdlimit ); + dev->http = devicePulseGetHttp( dev ); if( NULL == dev->http ) { tr_dbg( "upnp device %s: http init failed, state %hhu -> error", @@ -960,7 +958,7 @@ devicePulseHttp( tr_upnp_device_t * dev, tr_fd_t * fdlimit, if( SOAP_METHOD_NOT_ALLOWED == code && !dev->soapretry ) { dev->soapretry = 1; - killHttp( fdlimit, &dev->http ); + killHttp( &dev->http ); break; } dev->soapretry = 0; @@ -968,7 +966,7 @@ devicePulseHttp( tr_upnp_device_t * dev, tr_fd_t * fdlimit, *len = ( NULL == body ? 0 : hlen - ( *body - headers ) ); return code; case TR_NET_ERROR: - killHttp( fdlimit, &dev->http ); + killHttp( &dev->http ); if( dev->soapretry ) { tr_dbg( "upnp device %s: http pulse failed, state %hhu -> error", diff --git a/libtransmission/upnp.h b/libtransmission/upnp.h index e3b48a7fb..d147bfa56 100644 --- a/libtransmission/upnp.h +++ b/libtransmission/upnp.h @@ -27,7 +27,7 @@ typedef struct tr_upnp_s tr_upnp_t; -tr_upnp_t * tr_upnpInit( tr_fd_t * ); +tr_upnp_t * tr_upnpInit(); void tr_upnpStart( tr_upnp_t * ); void tr_upnpStop( tr_upnp_t * ); int tr_upnpStatus( tr_upnp_t * );