2007-04-18 16:39:10 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (c) 2007 Joshua Elsasser
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <event.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2007-07-18 23:04:26 +00:00
|
|
|
#include <libtransmission/trcompat.h>
|
|
|
|
#include <libtransmission/version.h>
|
|
|
|
|
2007-04-18 16:39:10 +00:00
|
|
|
#include "errors.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "server.h"
|
|
|
|
#include "torrents.h"
|
|
|
|
|
|
|
|
static void usage ( const char *, ... );
|
2007-08-14 04:02:50 +00:00
|
|
|
static void readargs ( int, char **, int *, int *, char **, char ** );
|
2007-05-24 03:59:53 +00:00
|
|
|
static int trylocksock ( const char * );
|
2007-04-18 16:39:10 +00:00
|
|
|
static int getlock ( const char * );
|
|
|
|
static int getsock ( const char * );
|
|
|
|
static void exitcleanup ( void );
|
|
|
|
static void setupsigs ( struct event_base * );
|
|
|
|
static void gotsig ( int, short, void * );
|
2007-08-14 04:02:50 +00:00
|
|
|
static int savepid ( const char * );
|
2007-04-18 16:39:10 +00:00
|
|
|
|
|
|
|
static int gl_lockfd = -1;
|
|
|
|
static char gl_lockpath[MAXPATHLEN] = "";
|
|
|
|
static int gl_sockfd = -1;
|
|
|
|
static char gl_sockpath[MAXPATHLEN] = "";
|
2007-08-14 04:02:50 +00:00
|
|
|
static char gl_pidfile[MAXPATHLEN] = "";
|
2007-04-18 16:39:10 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
main( int argc, char ** argv )
|
|
|
|
{
|
|
|
|
struct event_base * evbase;
|
2007-04-26 07:03:36 +00:00
|
|
|
int nofork, debug, sockfd;
|
2007-08-14 04:02:50 +00:00
|
|
|
char * sockpath, * pidfile;
|
2007-04-18 16:39:10 +00:00
|
|
|
|
|
|
|
setmyname( argv[0] );
|
2007-08-14 04:02:50 +00:00
|
|
|
readargs( argc, argv, &nofork, &debug, &sockpath, &pidfile );
|
2007-04-18 16:39:10 +00:00
|
|
|
|
|
|
|
if( !nofork )
|
|
|
|
{
|
|
|
|
if( 0 > daemon( 1, 0 ) )
|
|
|
|
{
|
|
|
|
errnomsg( "failed to daemonize" );
|
|
|
|
exit( 1 );
|
|
|
|
}
|
|
|
|
errsyslog( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
atexit( exitcleanup );
|
2007-05-24 03:59:53 +00:00
|
|
|
sockfd = trylocksock( sockpath );
|
2007-04-18 16:39:10 +00:00
|
|
|
if( 0 > sockfd )
|
|
|
|
{
|
|
|
|
exit( 1 );
|
|
|
|
}
|
2007-08-16 21:17:02 +00:00
|
|
|
if( NULL != pidfile && 0 > savepid( pidfile ) )
|
|
|
|
{
|
|
|
|
exit( 1 );
|
|
|
|
}
|
2007-04-18 16:39:10 +00:00
|
|
|
|
|
|
|
evbase = event_init();
|
|
|
|
setupsigs( evbase );
|
|
|
|
torrent_init( evbase );
|
|
|
|
server_init( evbase );
|
2007-04-26 07:03:36 +00:00
|
|
|
server_debug( debug );
|
2007-04-18 16:39:10 +00:00
|
|
|
server_listen( sockfd );
|
|
|
|
|
2007-08-16 00:10:56 +00:00
|
|
|
event_base_dispatch( evbase );
|
2007-04-18 16:39:10 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
usage( const char * msg, ... )
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
if( NULL != msg )
|
|
|
|
{
|
|
|
|
printf( "%s: ", getmyname() );
|
|
|
|
va_start( ap, msg );
|
|
|
|
vprintf( msg, ap );
|
|
|
|
va_end( ap );
|
|
|
|
printf( "\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
printf(
|
2007-08-14 04:02:50 +00:00
|
|
|
"usage: %s [-dfh] [-p file] [-s file]\n"
|
2007-04-18 16:39:10 +00:00
|
|
|
"\n"
|
2007-07-10 19:40:21 +00:00
|
|
|
"Transmission %s http://transmission.m0k.org/\n"
|
2007-04-18 16:39:10 +00:00
|
|
|
"A free, lightweight BitTorrent client with a simple, intuitive interface\n"
|
|
|
|
"\n"
|
2007-04-26 07:03:36 +00:00
|
|
|
" -d --debug Print data send and received, implies -f\n"
|
2007-04-18 16:39:10 +00:00
|
|
|
" -f --foreground Run in the foreground and log to stderr\n"
|
|
|
|
" -h --help Display this message and exit\n"
|
2007-08-14 04:02:50 +00:00
|
|
|
" -p --pidfile <path> Save the process id in a file at <path>\n"
|
2007-05-24 03:59:53 +00:00
|
|
|
" -s --socket <path> Place the socket file at <path>\n"
|
2007-04-18 16:39:10 +00:00
|
|
|
"\n"
|
|
|
|
"To add torrents or set options, use the transmission-remote program.\n",
|
2007-07-10 19:40:21 +00:00
|
|
|
getmyname(), LONG_VERSION_STRING );
|
2007-04-18 16:39:10 +00:00
|
|
|
exit( 0 );
|
|
|
|
}
|
|
|
|
|
2007-04-26 07:03:36 +00:00
|
|
|
void
|
2007-08-14 04:02:50 +00:00
|
|
|
readargs( int argc, char ** argv, int * nofork, int * debug, char ** sock,
|
|
|
|
char ** pidfile )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2007-08-14 04:02:50 +00:00
|
|
|
char optstr[] = "dfhp:s:";
|
2007-04-18 16:39:10 +00:00
|
|
|
struct option longopts[] =
|
|
|
|
{
|
2007-04-26 07:03:36 +00:00
|
|
|
{ "debug", no_argument, NULL, 'd' },
|
2007-04-18 16:39:10 +00:00
|
|
|
{ "foreground", no_argument, NULL, 'f' },
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
2007-08-14 04:02:50 +00:00
|
|
|
{ "pidfile", required_argument, NULL, 'p' },
|
2007-05-24 03:59:53 +00:00
|
|
|
{ "socket", required_argument, NULL, 's' },
|
2007-04-18 16:39:10 +00:00
|
|
|
{ NULL, 0, NULL, 0 }
|
|
|
|
};
|
2007-04-26 07:03:36 +00:00
|
|
|
int opt;
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2007-08-14 04:02:50 +00:00
|
|
|
*nofork = 0;
|
|
|
|
*debug = 0;
|
|
|
|
*sock = NULL;
|
|
|
|
*pidfile = NULL;
|
2007-04-18 16:39:10 +00:00
|
|
|
|
|
|
|
while( 0 <= ( opt = getopt_long( argc, argv, optstr, longopts, NULL ) ) )
|
|
|
|
{
|
|
|
|
switch( opt )
|
|
|
|
{
|
2007-04-26 07:03:36 +00:00
|
|
|
case 'd':
|
|
|
|
*debug = 1;
|
|
|
|
/* FALLTHROUGH */
|
2007-04-18 16:39:10 +00:00
|
|
|
case 'f':
|
2007-04-26 07:03:36 +00:00
|
|
|
*nofork = 1;
|
2007-04-18 16:39:10 +00:00
|
|
|
break;
|
2007-08-14 04:02:50 +00:00
|
|
|
case 'p':
|
|
|
|
*pidfile = optarg;
|
|
|
|
break;
|
2007-05-24 03:59:53 +00:00
|
|
|
case 's':
|
|
|
|
*sock = optarg;
|
|
|
|
break;
|
2007-04-18 16:39:10 +00:00
|
|
|
default:
|
|
|
|
usage( NULL );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-05-24 03:59:53 +00:00
|
|
|
trylocksock( const char * sockpath )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
|
|
|
char path[MAXPATHLEN];
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
confpath( path, sizeof path, NULL, CONF_PATH_TYPE_DAEMON );
|
|
|
|
if( 0 > mkdir( path, 0777 ) && EEXIST != errno )
|
|
|
|
{
|
|
|
|
errnomsg( "failed to create directory: %s", path );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
confpath( path, sizeof path, CONF_FILE_LOCK, 0 );
|
|
|
|
fd = getlock( path );
|
|
|
|
if( 0 > fd )
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
gl_lockfd = fd;
|
|
|
|
strlcpy( gl_lockpath, path, sizeof gl_lockpath );
|
|
|
|
|
2007-05-24 03:59:53 +00:00
|
|
|
if( NULL == sockpath )
|
|
|
|
{
|
|
|
|
confpath( path, sizeof path, CONF_FILE_SOCKET, 0 );
|
2007-06-17 15:47:09 +00:00
|
|
|
sockpath = path;
|
2007-05-24 03:59:53 +00:00
|
|
|
}
|
2007-06-17 15:47:09 +00:00
|
|
|
fd = getsock( sockpath );
|
2007-04-18 16:39:10 +00:00
|
|
|
if( 0 > fd )
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
gl_sockfd = fd;
|
2007-06-17 15:47:09 +00:00
|
|
|
strlcpy( gl_sockpath, sockpath, sizeof gl_sockpath );
|
2007-04-18 16:39:10 +00:00
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
getlock( const char * path )
|
|
|
|
{
|
|
|
|
struct flock lk;
|
|
|
|
int fd;
|
|
|
|
char pid[64];
|
|
|
|
|
|
|
|
fd = open( path, O_RDWR | O_CREAT, 0666 );
|
|
|
|
if( 0 > fd )
|
|
|
|
{
|
|
|
|
errnomsg( "failed to open file: %s", path );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-07-19 00:57:26 +00:00
|
|
|
memset( &lk, 0, sizeof lk );
|
2007-04-18 16:39:10 +00:00
|
|
|
lk.l_start = 0;
|
|
|
|
lk.l_len = 0;
|
|
|
|
lk.l_type = F_WRLCK;
|
|
|
|
lk.l_whence = SEEK_SET;
|
|
|
|
if( 0 > fcntl( fd, F_SETLK, &lk ) )
|
|
|
|
{
|
|
|
|
if( EAGAIN == errno )
|
|
|
|
{
|
2007-05-09 06:58:46 +00:00
|
|
|
errmsg( "another copy of %s is already running", getmyname() );
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
errnomsg( "failed to obtain lock on file: %s", path );
|
|
|
|
}
|
|
|
|
close( fd );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ftruncate( fd, 0 );
|
|
|
|
snprintf( pid, sizeof pid, "%i\n", getpid() );
|
|
|
|
write( fd, pid, strlen( pid ) );
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
getsock( const char * path )
|
|
|
|
{
|
2007-07-19 01:25:36 +00:00
|
|
|
struct sockaddr_un sa;
|
2007-04-18 16:39:10 +00:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = socket( PF_LOCAL, SOCK_STREAM, 0 );
|
|
|
|
if( 0 > fd )
|
|
|
|
{
|
|
|
|
errnomsg( "failed to create socket file: %s", path );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-07-19 01:25:36 +00:00
|
|
|
memset( &sa, 0, sizeof sa );
|
|
|
|
sa.sun_family = AF_LOCAL;
|
|
|
|
strlcpy( sa.sun_path, path, sizeof sa.sun_path );
|
2007-04-18 16:39:10 +00:00
|
|
|
unlink( path );
|
2007-07-19 01:25:36 +00:00
|
|
|
if( 0 > bind( fd, ( struct sockaddr * )&sa, SUN_LEN( &sa ) ) )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
|
|
|
/* bind can sometimes fail on the first call */
|
|
|
|
unlink( path );
|
2007-07-19 01:25:36 +00:00
|
|
|
if( 0 > bind( fd, ( struct sockaddr * )&sa, SUN_LEN( &sa ) ) )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
|
|
|
errnomsg( "failed to bind socket file: %s", path );
|
|
|
|
close( fd );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
exitcleanup( void )
|
|
|
|
{
|
|
|
|
if( 0 <= gl_sockfd )
|
|
|
|
{
|
|
|
|
unlink( gl_sockpath );
|
|
|
|
close( gl_sockfd );
|
|
|
|
}
|
2007-08-16 21:17:02 +00:00
|
|
|
if( 0 != gl_pidfile[0] )
|
|
|
|
{
|
|
|
|
unlink( gl_pidfile );
|
|
|
|
}
|
2007-04-18 16:39:10 +00:00
|
|
|
if( 0 <= gl_lockfd )
|
|
|
|
{
|
|
|
|
unlink( gl_lockpath );
|
|
|
|
close( gl_lockfd );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-08-16 00:10:56 +00:00
|
|
|
setupsigs( struct event_base * base )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
|
|
|
static struct event ev_int;
|
|
|
|
static struct event ev_quit;
|
|
|
|
static struct event ev_term;
|
|
|
|
|
|
|
|
signal_set( &ev_int, SIGINT, gotsig, NULL );
|
2007-08-16 00:10:56 +00:00
|
|
|
event_base_set( base, &ev_int );
|
2007-04-18 16:39:10 +00:00
|
|
|
signal_add( &ev_int, NULL );
|
|
|
|
|
2007-06-29 01:06:46 +00:00
|
|
|
signal_set( &ev_quit, SIGQUIT, gotsig, NULL );
|
2007-08-16 00:10:56 +00:00
|
|
|
event_base_set( base, &ev_quit );
|
2007-04-18 16:39:10 +00:00
|
|
|
signal_add( &ev_quit, NULL );
|
|
|
|
|
2007-06-29 01:06:46 +00:00
|
|
|
signal_set( &ev_term, SIGTERM, gotsig, NULL );
|
2007-08-16 00:10:56 +00:00
|
|
|
event_base_set( base, &ev_term );
|
2007-04-18 16:39:10 +00:00
|
|
|
signal_add( &ev_term, NULL );
|
|
|
|
|
|
|
|
signal( SIGPIPE, SIG_IGN );
|
|
|
|
signal( SIGHUP, SIG_IGN );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gotsig( int sig, short what UNUSED, void * arg UNUSED )
|
|
|
|
{
|
|
|
|
static int exiting = 0;
|
|
|
|
|
|
|
|
if( !exiting )
|
|
|
|
{
|
|
|
|
exiting = 1;
|
|
|
|
errmsg( "received fatal signal %i, attempting to exit cleanly", sig );
|
|
|
|
torrent_exit( 0 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
errmsg( "received fatal signal %i while exiting, exiting immediately",
|
|
|
|
sig );
|
|
|
|
signal( sig, SIG_DFL );
|
|
|
|
raise( sig );
|
|
|
|
}
|
|
|
|
}
|
2007-08-14 04:02:50 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
savepid( const char * file )
|
|
|
|
{
|
|
|
|
FILE * pid;
|
|
|
|
|
|
|
|
pid = fopen( file, "wb" );
|
|
|
|
if( NULL == pid )
|
|
|
|
{
|
|
|
|
errnomsg( "failed to open pid file: %s", file );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( 0 > fprintf( pid, "%d\n", (int) getpid() ) )
|
|
|
|
{
|
|
|
|
errnomsg( "failed to write pid to file: %s", file );
|
|
|
|
fclose( pid );
|
|
|
|
unlink( file );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose( pid );
|
|
|
|
strlcpy( gl_pidfile, file, sizeof gl_pidfile );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|