2007-08-18 03:02:32 +00:00
|
|
|
/*
|
2009-01-10 23:09:07 +00:00
|
|
|
* This file Copyright (C) 2007-2009 Charles Kerr <charles@transmissionbt.com>
|
2007-08-18 03:02:32 +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)
|
2008-09-23 19:11:04 +00:00
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
2007-08-18 03:02:32 +00:00
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
2007-08-18 17:19:49 +00:00
|
|
|
*
|
|
|
|
* $Id$
|
2007-08-18 03:02:32 +00:00
|
|
|
*/
|
|
|
|
|
2007-11-09 20:07:52 +00:00
|
|
|
#include <assert.h>
|
2008-05-06 15:52:57 +00:00
|
|
|
#include <errno.h>
|
2007-08-18 03:02:32 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2007-09-05 23:01:56 +00:00
|
|
|
#include <stdio.h>
|
2007-08-18 03:02:32 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
#include <signal.h>
|
2007-08-18 03:02:32 +00:00
|
|
|
|
2008-12-23 17:27:15 +00:00
|
|
|
#include "transmission.h"
|
|
|
|
#include "session.h"
|
|
|
|
|
2008-05-06 15:52:57 +00:00
|
|
|
#ifdef WIN32
|
2008-10-14 18:17:33 +00:00
|
|
|
|
|
|
|
#include <WinSock2.h>
|
|
|
|
|
|
|
|
static int
|
|
|
|
pgpipe( int handles[2] )
|
|
|
|
{
|
|
|
|
SOCKET s;
|
|
|
|
struct sockaddr_in serv_addr;
|
|
|
|
int len = sizeof( serv_addr );
|
|
|
|
|
|
|
|
handles[0] = handles[1] = INVALID_SOCKET;
|
|
|
|
|
|
|
|
if ( ( s = socket( AF_INET, SOCK_STREAM, 0 ) ) == INVALID_SOCKET )
|
|
|
|
{
|
|
|
|
/* ereport(LOG, (errmsg_internal("pgpipe failed to create socket: %ui", WSAGetLastError()))); */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset( &serv_addr, 0, sizeof( serv_addr ) );
|
|
|
|
serv_addr.sin_family = AF_INET;
|
|
|
|
serv_addr.sin_port = htons(0);
|
|
|
|
serv_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
|
|
if (bind(s, (SOCKADDR *) & serv_addr, len) == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
/* ereport(LOG, (errmsg_internal("pgpipe failed to bind: %ui", WSAGetLastError()))); */
|
|
|
|
closesocket(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (listen(s, 1) == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
/* ereport(LOG, (errmsg_internal("pgpipe failed to listen: %ui", WSAGetLastError()))); */
|
|
|
|
closesocket(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (getsockname(s, (SOCKADDR *) & serv_addr, &len) == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
/* ereport(LOG, (errmsg_internal("pgpipe failed to getsockname: %ui", WSAGetLastError()))); */
|
|
|
|
closesocket(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ((handles[1] = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
|
|
|
|
{
|
|
|
|
/* ereport(LOG, (errmsg_internal("pgpipe failed to create socket 2: %ui", WSAGetLastError()))); */
|
|
|
|
closesocket(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (connect(handles[1], (SOCKADDR *) & serv_addr, len) == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
/* ereport(LOG, (errmsg_internal("pgpipe failed to connect socket: %ui", WSAGetLastError()))); */
|
|
|
|
closesocket(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ((handles[0] = accept(s, (SOCKADDR *) & serv_addr, &len)) == INVALID_SOCKET)
|
|
|
|
{
|
|
|
|
/* ereport(LOG, (errmsg_internal("pgpipe failed to accept socket: %ui", WSAGetLastError()))); */
|
|
|
|
closesocket(handles[1]);
|
|
|
|
handles[1] = INVALID_SOCKET;
|
|
|
|
closesocket(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
closesocket(s);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
piperead( int s, char *buf, int len )
|
|
|
|
{
|
|
|
|
int ret = recv(s, buf, len, 0);
|
|
|
|
|
|
|
|
if (ret < 0 && WSAGetLastError() == WSAECONNRESET)
|
|
|
|
/* EOF on the pipe! (win32 socket based implementation) */
|
|
|
|
ret = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define pipe(a) pgpipe(a)
|
2008-10-15 19:52:15 +00:00
|
|
|
#define pipewrite(a,b,c) send(a,(char*)b,c,0)
|
2008-10-14 18:17:33 +00:00
|
|
|
|
2008-05-06 15:52:57 +00:00
|
|
|
#else
|
2008-10-14 18:17:33 +00:00
|
|
|
#define piperead(a,b,c) read(a,b,c)
|
|
|
|
#define pipewrite(a,b,c) write(a,b,c)
|
2008-05-06 15:52:57 +00:00
|
|
|
#endif
|
|
|
|
|
2008-10-14 18:17:33 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2007-08-18 03:02:32 +00:00
|
|
|
#include <event.h>
|
|
|
|
|
|
|
|
#include "transmission.h"
|
|
|
|
#include "platform.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "trevent.h"
|
2007-08-18 03:02:32 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
typedef struct tr_event_handle
|
2007-08-18 03:02:32 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
uint8_t die;
|
|
|
|
int fds[2];
|
|
|
|
tr_lock * lock;
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_session * session;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_thread * thread;
|
2007-08-20 23:37:08 +00:00
|
|
|
struct event_base * base;
|
2008-05-06 15:52:57 +00:00
|
|
|
struct event pipeEvent;
|
2007-08-18 03:02:32 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_event_handle;
|
2007-08-18 03:02:32 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
typedef int timer_func ( void* );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
struct tr_timer
|
|
|
|
{
|
2008-11-28 22:11:41 +00:00
|
|
|
tr_bool inCallback;
|
2008-09-23 19:11:04 +00:00
|
|
|
timer_func * func;
|
|
|
|
void * user_data;
|
|
|
|
struct tr_event_handle * eh;
|
2008-10-19 19:54:53 +00:00
|
|
|
struct timeval tv;
|
|
|
|
struct event event;
|
2007-09-20 16:32:01 +00:00
|
|
|
};
|
|
|
|
|
2008-05-06 15:52:57 +00:00
|
|
|
struct tr_run_data
|
2007-08-18 03:02:32 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
void ( *func )( void * );
|
|
|
|
void * user_data;
|
2007-09-20 16:32:01 +00:00
|
|
|
};
|
2007-08-18 03:02:32 +00:00
|
|
|
|
2008-10-26 15:39:04 +00:00
|
|
|
#define dbgmsg( ... ) \
|
|
|
|
do { \
|
|
|
|
if( tr_deepLoggingIsActive( ) ) \
|
|
|
|
tr_deepLog( __FILE__, __LINE__, "event", __VA_ARGS__ ); \
|
|
|
|
} while( 0 )
|
2008-05-06 15:52:57 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
readFromPipe( int fd,
|
|
|
|
short eventType,
|
|
|
|
void * veh )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
char ch;
|
|
|
|
int ret;
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_event_handle * eh = veh;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-05-06 15:52:57 +00:00
|
|
|
dbgmsg( "readFromPipe: eventType is %hd", eventType );
|
2007-08-18 03:02:32 +00:00
|
|
|
|
2008-05-06 15:52:57 +00:00
|
|
|
/* read the command type */
|
|
|
|
ch = '\0';
|
2008-09-23 19:11:04 +00:00
|
|
|
do
|
|
|
|
{
|
2008-10-14 18:17:33 +00:00
|
|
|
ret = piperead( fd, &ch, 1 );
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
|
|
|
while( !eh->die && ret < 0 && errno == EAGAIN );
|
|
|
|
|
2008-05-06 15:52:57 +00:00
|
|
|
dbgmsg( "command is [%c], ret is %d, errno is %d", ch, ret, (int)errno );
|
2007-08-18 06:59:20 +00:00
|
|
|
|
2008-05-06 15:52:57 +00:00
|
|
|
switch( ch )
|
|
|
|
{
|
|
|
|
case 'r': /* run in libevent thread */
|
|
|
|
{
|
|
|
|
struct tr_run_data data;
|
2008-09-23 19:11:04 +00:00
|
|
|
const size_t nwant = sizeof( data );
|
2008-10-14 18:17:33 +00:00
|
|
|
const ssize_t ngot = piperead( fd, &data, nwant );
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !eh->die && ( ngot == (ssize_t)nwant ) )
|
|
|
|
{
|
2008-05-06 15:52:57 +00:00
|
|
|
dbgmsg( "invoking function in libevent thread" );
|
2008-09-23 19:11:04 +00:00
|
|
|
( data.func )( data.user_data );
|
2008-05-06 15:52:57 +00:00
|
|
|
}
|
2007-10-08 00:56:12 +00:00
|
|
|
break;
|
2008-05-06 15:52:57 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-05-06 15:52:57 +00:00
|
|
|
case '\0': /* eof */
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-05-06 15:52:57 +00:00
|
|
|
dbgmsg( "pipe eof reached... removing event listener" );
|
|
|
|
event_del( &eh->pipeEvent );
|
|
|
|
break;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-05-06 15:52:57 +00:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
assert( 0 && "unhandled command type!" );
|
|
|
|
break;
|
2008-06-25 11:34:35 +00:00
|
|
|
}
|
2007-08-18 03:02:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-19 03:54:27 +00:00
|
|
|
static void
|
2008-12-14 11:21:11 +00:00
|
|
|
logFunc( int severity, const char * message )
|
2007-08-19 03:54:27 +00:00
|
|
|
{
|
2008-03-18 02:23:39 +00:00
|
|
|
if( severity >= _EVENT_LOG_ERR )
|
2008-10-09 20:03:34 +00:00
|
|
|
tr_err( "%s", message );
|
2008-03-18 02:23:39 +00:00
|
|
|
else
|
2008-10-09 20:03:34 +00:00
|
|
|
tr_dbg( "%s", message );
|
2007-08-19 03:54:27 +00:00
|
|
|
}
|
|
|
|
|
2007-08-18 03:02:32 +00:00
|
|
|
static void
|
|
|
|
libeventThreadFunc( void * veh )
|
|
|
|
{
|
2008-10-01 04:26:38 +00:00
|
|
|
tr_event_handle * eh = veh;
|
2007-08-20 02:18:38 +00:00
|
|
|
tr_dbg( "Starting libevent thread" );
|
2007-08-18 03:02:32 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
#ifndef WIN32
|
|
|
|
/* Don't exit when writing on a broken socket */
|
|
|
|
signal( SIGPIPE, SIG_IGN );
|
|
|
|
#endif
|
2007-08-18 03:02:32 +00:00
|
|
|
|
2009-01-26 02:51:50 +00:00
|
|
|
eh->base = event_init( );
|
2008-12-14 11:21:11 +00:00
|
|
|
eh->session->events = eh;
|
2008-05-06 15:52:57 +00:00
|
|
|
|
|
|
|
/* listen to the pipe's read fd */
|
2009-01-26 02:51:50 +00:00
|
|
|
event_set( &eh->pipeEvent, eh->fds[0], EV_READ | EV_PERSIST, readFromPipe, veh );
|
2008-05-06 15:52:57 +00:00
|
|
|
event_add( &eh->pipeEvent, NULL );
|
2008-10-01 20:23:57 +00:00
|
|
|
event_set_log_callback( logFunc );
|
2009-01-26 02:51:50 +00:00
|
|
|
|
|
|
|
/* loop until all the events are done */
|
2009-05-21 22:26:18 +00:00
|
|
|
while( !eh->die )
|
|
|
|
event_dispatch( );
|
2007-08-18 03:02:32 +00:00
|
|
|
|
2009-01-26 02:51:50 +00:00
|
|
|
/* shut down the thread */
|
2007-08-20 02:18:38 +00:00
|
|
|
tr_lockFree( eh->lock );
|
2007-08-20 23:37:08 +00:00
|
|
|
event_base_free( eh->base );
|
2008-12-14 11:21:11 +00:00
|
|
|
eh->session->events = NULL;
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_free( eh );
|
2007-08-20 02:18:38 +00:00
|
|
|
tr_dbg( "Closing libevent thread" );
|
2007-08-18 03:02:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_eventInit( tr_session * session )
|
2007-08-18 03:02:32 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_event_handle * eh;
|
2007-08-18 03:02:32 +00:00
|
|
|
|
2009-01-26 02:51:50 +00:00
|
|
|
session->events = NULL;
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
eh = tr_new0( tr_event_handle, 1 );
|
2007-08-18 03:02:32 +00:00
|
|
|
eh->lock = tr_lockNew( );
|
2008-05-06 15:52:57 +00:00
|
|
|
pipe( eh->fds );
|
2008-12-14 11:21:11 +00:00
|
|
|
eh->session = session;
|
2008-08-12 14:03:03 +00:00
|
|
|
eh->thread = tr_threadNew( libeventThreadFunc, eh );
|
2009-01-26 02:51:50 +00:00
|
|
|
|
|
|
|
/* wait until the libevent thread is running */
|
|
|
|
while( session->events == NULL )
|
|
|
|
tr_wait( 100 );
|
2007-08-18 03:02:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_eventClose( tr_session * session )
|
2007-08-18 03:02:32 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
session->events->die = TRUE;
|
2008-05-06 15:52:57 +00:00
|
|
|
tr_deepLog( __FILE__, __LINE__, NULL, "closing trevent pipe" );
|
2008-12-14 11:21:11 +00:00
|
|
|
EVUTIL_CLOSESOCKET( session->events->fds[1] );
|
2007-08-18 03:02:32 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2008-12-30 02:43:51 +00:00
|
|
|
tr_bool
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_amInEventThread( tr_session * session )
|
2007-11-15 16:43:46 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
2009-02-04 16:58:52 +00:00
|
|
|
assert( session->events != NULL );
|
2008-10-19 22:04:47 +00:00
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
return tr_amInThread( session->events->thread );
|
2007-11-15 16:43:46 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
static void
|
2008-12-14 11:21:11 +00:00
|
|
|
timerCallback( int fd UNUSED,
|
|
|
|
short event UNUSED,
|
|
|
|
void * vtimer )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int more;
|
2007-09-20 16:32:01 +00:00
|
|
|
struct tr_timer * timer = vtimer;
|
|
|
|
|
2008-05-06 15:52:57 +00:00
|
|
|
timer->inCallback = 1;
|
2008-09-23 19:11:04 +00:00
|
|
|
more = ( *timer->func )( timer->user_data );
|
2008-05-06 15:52:57 +00:00
|
|
|
timer->inCallback = 0;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
if( more )
|
2008-04-24 19:38:59 +00:00
|
|
|
evtimer_add( &timer->event, &timer->tv );
|
2007-09-05 23:01:56 +00:00
|
|
|
else
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_timerFree( &timer );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_timerFree( tr_timer ** ptimer )
|
|
|
|
{
|
|
|
|
tr_timer * timer;
|
|
|
|
|
|
|
|
/* zero out the argument passed in */
|
|
|
|
assert( ptimer );
|
|
|
|
timer = *ptimer;
|
|
|
|
*ptimer = NULL;
|
|
|
|
|
|
|
|
/* destroy the timer directly or via the command queue */
|
2008-05-06 15:52:57 +00:00
|
|
|
if( timer && !timer->inCallback )
|
|
|
|
{
|
2008-12-14 11:21:11 +00:00
|
|
|
assert( tr_amInEventThread( timer->eh->session ) );
|
2007-11-19 16:30:31 +00:00
|
|
|
event_del( &timer->event );
|
|
|
|
tr_free( timer );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_timer*
|
2009-02-04 16:58:52 +00:00
|
|
|
tr_timerNew( tr_session * session,
|
|
|
|
timer_func func,
|
|
|
|
void * user_data,
|
|
|
|
uint64_t interval_milliseconds )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-10-19 20:10:29 +00:00
|
|
|
tr_timer * timer;
|
|
|
|
|
2009-02-04 16:58:52 +00:00
|
|
|
assert( tr_amInEventThread( session ) );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-10-19 20:10:29 +00:00
|
|
|
timer = tr_new0( tr_timer, 1 );
|
2007-09-20 16:32:01 +00:00
|
|
|
timer->func = func;
|
|
|
|
timer->user_data = user_data;
|
2008-12-14 11:21:11 +00:00
|
|
|
timer->eh = session->events;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2009-02-04 16:58:52 +00:00
|
|
|
tr_timevalMsec( interval_milliseconds, &timer->tv );
|
|
|
|
evtimer_set( &timer->event, timerCallback, timer );
|
|
|
|
evtimer_add( &timer->event, &timer->tv );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
return timer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_runInEventThread( tr_session * session,
|
|
|
|
void func( void* ), void * user_data )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
2009-01-28 19:35:39 +00:00
|
|
|
assert( session->events != NULL );
|
2008-10-19 22:04:47 +00:00
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
if( tr_amInThread( session->events->thread ) )
|
2008-05-06 15:52:57 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
(func)( user_data );
|
2008-05-06 15:52:57 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
const char ch = 'r';
|
2008-12-14 11:21:11 +00:00
|
|
|
int fd = session->events->fds[1];
|
|
|
|
tr_lock * lock = session->events->lock;
|
2008-05-06 15:52:57 +00:00
|
|
|
struct tr_run_data data;
|
|
|
|
|
|
|
|
tr_lockLock( lock );
|
2008-10-14 18:17:33 +00:00
|
|
|
pipewrite( fd, &ch, 1 );
|
2008-05-06 15:52:57 +00:00
|
|
|
data.func = func;
|
|
|
|
data.user_data = user_data;
|
2008-10-14 18:17:33 +00:00
|
|
|
pipewrite( fd, &data, sizeof( data ) );
|
2008-05-06 15:52:57 +00:00
|
|
|
tr_lockUnlock( lock );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-09-29 04:26:52 +00:00
|
|
|
struct event_base *
|
|
|
|
tr_eventGetBase( tr_session * session )
|
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-09-29 04:26:52 +00:00
|
|
|
return session->events->base;
|
|
|
|
}
|