2005-03-19 23:16:49 +01:00
|
|
|
/* -*-mode: C; fill-column: 78; c-basic-offset: 4; -*- */
|
|
|
|
|
|
|
|
/*
|
2006-01-08 02:25:02 +01:00
|
|
|
* Copyright 2005 by Eric House (xwords@eehouse.org). All rights reserved.
|
2005-03-19 23:16:49 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <netdb.h> /* gethostbyname */
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2005-03-31 04:20:50 +02:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
2005-03-19 23:16:49 +01:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
#include "ctrl.h"
|
|
|
|
#include "cref.h"
|
2005-09-02 08:56:34 +02:00
|
|
|
#include "crefmgr.h"
|
2005-10-23 17:49:48 +02:00
|
|
|
#include "mlock.h"
|
2005-03-19 23:16:49 +01:00
|
|
|
#include "xwrelay_priv.h"
|
|
|
|
|
2005-03-25 04:24:00 +01:00
|
|
|
/* this is *only* for testing. Don't abuse!!!! */
|
2005-04-08 16:17:28 +02:00
|
|
|
extern pthread_rwlock_t gCookieMapRWLock;
|
2005-03-25 04:24:00 +01:00
|
|
|
|
2005-03-31 04:20:50 +02:00
|
|
|
typedef int (*CmdPtr)( int socket, const char** args );
|
|
|
|
|
|
|
|
typedef struct FuncRec {
|
|
|
|
char* name;
|
|
|
|
CmdPtr func;
|
|
|
|
} FuncRec;
|
|
|
|
|
2005-10-23 17:49:48 +02:00
|
|
|
vector<int> g_ctrlSocks;
|
|
|
|
pthread_mutex_t g_ctrlSocksMutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
|
|
|
|
2005-03-31 04:20:50 +02:00
|
|
|
static int cmd_quit( int socket, const char** args );
|
2005-04-20 13:57:26 +02:00
|
|
|
static int cmd_print( int socket, const char** args );
|
2005-03-31 04:20:50 +02:00
|
|
|
static int cmd_lock( int socket, const char** args );
|
|
|
|
static int cmd_help( int socket, const char** args );
|
2005-04-20 13:57:26 +02:00
|
|
|
static int cmd_start( int socket, const char** args );
|
|
|
|
static int cmd_stop( int socket, const char** args );
|
2005-10-15 18:28:26 +02:00
|
|
|
static int cmd_kill_eject( int socket, const char** args );
|
2005-04-20 13:57:26 +02:00
|
|
|
static int cmd_get( int socket, const char** args );
|
|
|
|
static int cmd_set( int socket, const char** args );
|
|
|
|
static int cmd_shutdown( int socket, const char** args );
|
2005-10-19 05:39:18 +02:00
|
|
|
static int cmd_uptime( int socket, const char** args );
|
2005-03-31 04:20:50 +02:00
|
|
|
|
2005-03-19 23:16:49 +01:00
|
|
|
static void
|
2005-10-14 10:26:56 +02:00
|
|
|
print_to_sock( int sock, int addCR, const char* what, ... )
|
2005-03-19 23:16:49 +01:00
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start( ap, what );
|
|
|
|
vsnprintf( buf, sizeof(buf) - 1, what, ap );
|
|
|
|
va_end(ap);
|
|
|
|
|
2005-10-14 10:26:56 +02:00
|
|
|
if ( addCR ) {
|
|
|
|
strncat( buf, "\n", sizeof(buf) );
|
|
|
|
}
|
2005-03-19 23:16:49 +01:00
|
|
|
send( sock, buf, strlen(buf), 0 );
|
|
|
|
}
|
|
|
|
|
2005-03-31 04:20:50 +02:00
|
|
|
static const FuncRec gFuncs[] = {
|
2005-10-15 18:28:26 +02:00
|
|
|
{ "?", cmd_help },
|
|
|
|
{ "help", cmd_help },
|
|
|
|
{ "quit", cmd_quit },
|
2005-04-20 13:57:26 +02:00
|
|
|
{ "print", cmd_print },
|
2005-03-31 04:20:50 +02:00
|
|
|
{ "lock", cmd_lock },
|
2005-04-20 13:57:26 +02:00
|
|
|
{ "start", cmd_start },
|
|
|
|
{ "stop", cmd_stop },
|
2005-10-15 18:28:26 +02:00
|
|
|
{ "kill", cmd_kill_eject },
|
|
|
|
{ "eject", cmd_kill_eject },
|
2005-04-20 13:57:26 +02:00
|
|
|
{ "shutdown", cmd_shutdown },
|
|
|
|
{ "get", cmd_get },
|
|
|
|
{ "set", cmd_set },
|
2005-10-19 05:39:18 +02:00
|
|
|
{ "uptime", cmd_uptime },
|
2005-03-31 04:20:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
cmd_quit( int socket, const char** args )
|
2005-03-19 23:16:49 +01:00
|
|
|
{
|
2005-03-31 04:20:50 +02:00
|
|
|
if ( 0 == strcmp( "help", args[1] ) ) {
|
2005-10-15 18:28:26 +02:00
|
|
|
print_to_sock( socket, 1, "* %s (disconnect from ctrl port)", args[0] );
|
2005-03-31 04:20:50 +02:00
|
|
|
} else {
|
2005-04-20 13:57:26 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_cookies( int socket, CookieID theID )
|
|
|
|
{
|
2005-09-02 08:56:34 +02:00
|
|
|
CRefMgr* cmgr = CRefMgr::Get();
|
|
|
|
CookieMapIterator iter = cmgr->GetCookieIterator();
|
2005-04-20 13:57:26 +02:00
|
|
|
CookieID id;
|
|
|
|
for ( id = iter.Next(); id != 0; id = iter.Next() ) {
|
|
|
|
if ( theID == 0 || theID == id ) {
|
2005-09-02 08:56:34 +02:00
|
|
|
SafeCref scr( id );
|
2005-10-01 18:33:45 +02:00
|
|
|
string s;
|
|
|
|
scr.PrintCookieInfo( s );
|
2005-04-20 13:57:26 +02:00
|
|
|
|
2005-10-14 10:26:56 +02:00
|
|
|
print_to_sock( socket, 1, s.c_str() );
|
2005-04-20 13:57:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cmd_start( int socket, const char** args )
|
|
|
|
{
|
2005-10-15 18:28:26 +02:00
|
|
|
print_to_sock( socket, 1, "* %s (unimplemented)", args[0] );
|
2005-04-20 13:57:26 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cmd_stop( int socket, const char** args )
|
|
|
|
{
|
2005-10-15 18:28:26 +02:00
|
|
|
print_to_sock( socket, 1, "* %s (unimplemented)", args[0] );
|
2005-04-20 13:57:26 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2005-10-15 18:28:26 +02:00
|
|
|
cmd_kill_eject( int socket, const char** args )
|
2005-04-20 13:57:26 +02:00
|
|
|
{
|
|
|
|
int found = 0;
|
2005-10-15 18:28:26 +02:00
|
|
|
int isKill = 0 == strcmp( args[0], "kill" );
|
2005-04-20 13:57:26 +02:00
|
|
|
|
|
|
|
if ( 0 == strcmp( args[1], "socket" ) ) {
|
|
|
|
int victim = atoi( args[2] );
|
|
|
|
if ( victim != 0 ) {
|
|
|
|
killSocket( victim, "ctrl command" );
|
|
|
|
found = 1;
|
|
|
|
}
|
2005-10-16 03:19:25 +02:00
|
|
|
} else if ( 0 == strcmp( args[1], "cref" ) ) {
|
2005-04-20 13:57:26 +02:00
|
|
|
const char* idhow = args[2];
|
|
|
|
const char* id = args[3];
|
|
|
|
if ( idhow != NULL && id != NULL ) {
|
|
|
|
if ( 0 == strcmp( idhow, "name" ) ) {
|
2005-09-02 08:56:34 +02:00
|
|
|
CRefMgr::Get()->Delete( id );
|
2005-04-20 13:57:26 +02:00
|
|
|
found = 1;
|
|
|
|
} else if ( 0 == strcmp( idhow, "id" ) ) {
|
2005-09-02 08:56:34 +02:00
|
|
|
CRefMgr::Get()->Delete( atoi( id ) );
|
2005-04-20 13:57:26 +02:00
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
}
|
2005-10-15 18:28:26 +02:00
|
|
|
} else if ( 0 == strcmp( args[1], "relay" ) ) {
|
|
|
|
print_to_sock( socket, 1, "not yet unimplemented" );
|
2005-04-20 13:57:26 +02:00
|
|
|
}
|
|
|
|
|
2005-10-15 18:28:26 +02:00
|
|
|
const char* expl = isKill?
|
|
|
|
"silently remove from game"
|
|
|
|
: "remove from game with error to device";
|
2005-04-20 13:57:26 +02:00
|
|
|
if ( !found ) {
|
|
|
|
char* msg =
|
2005-10-15 18:28:26 +02:00
|
|
|
"* %s socket <num> -- %s\n"
|
2005-10-16 03:19:25 +02:00
|
|
|
" %s cref connName <connName>\n"
|
|
|
|
" %s cref id <id>"
|
2005-04-20 13:57:26 +02:00
|
|
|
;
|
2005-10-15 18:28:26 +02:00
|
|
|
print_to_sock( socket, 1, msg, args[0], expl, args[0], args[0] );
|
2005-04-20 13:57:26 +02:00
|
|
|
}
|
|
|
|
return 1;
|
2005-10-15 18:28:26 +02:00
|
|
|
} /* cmd_kill_eject */
|
2005-04-20 13:57:26 +02:00
|
|
|
|
|
|
|
static int
|
|
|
|
cmd_get( int socket, const char** args )
|
|
|
|
{
|
2005-10-15 18:28:26 +02:00
|
|
|
print_to_sock( socket, 1,
|
|
|
|
"* %s -- lists all attributes (unimplemented)\n"
|
|
|
|
"* %s <attribute> (unimplemented)",
|
|
|
|
args[0], args[0] );
|
2005-04-20 13:57:26 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cmd_set( int socket, const char** args )
|
|
|
|
{
|
2005-10-15 18:28:26 +02:00
|
|
|
print_to_sock( socket, 1, "* %s <attribute> (unimplemented)", args[0] );
|
2005-04-20 13:57:26 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-10-19 05:39:18 +02:00
|
|
|
static int
|
|
|
|
cmd_uptime( int socket, const char** args )
|
|
|
|
{
|
|
|
|
if ( 0 == strcmp( args[1], "help" ) ) {
|
|
|
|
print_to_sock( socket, 1,
|
|
|
|
"* %s -- prints how long the relay's been running",
|
|
|
|
args[0] );
|
|
|
|
} else {
|
|
|
|
time_t seconds = now();
|
|
|
|
|
|
|
|
int days = seconds / (24*60*60);
|
|
|
|
seconds %= (24*60*60);
|
|
|
|
|
|
|
|
int hours = seconds / (60*60);
|
|
|
|
seconds %= (60*60);
|
|
|
|
|
|
|
|
int minutes = seconds / 60;
|
|
|
|
seconds %= 60;
|
|
|
|
|
|
|
|
print_to_sock( socket, 1,
|
|
|
|
"uptime: %d days, %d hours, %d minutes, %ld seconds",
|
|
|
|
days, hours, minutes, seconds );
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-04-20 13:57:26 +02:00
|
|
|
static int
|
|
|
|
cmd_shutdown( int socket, const char** args )
|
|
|
|
{
|
2005-10-15 18:28:26 +02:00
|
|
|
print_to_sock( socket, 1,
|
|
|
|
"* %s -- shuts down relay (exiting main) (unimplemented)",
|
|
|
|
args[0] );
|
2005-04-20 13:57:26 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-10-16 03:19:25 +02:00
|
|
|
print_cookies( int socket, const char* cookie, const char* connName )
|
2005-04-20 13:57:26 +02:00
|
|
|
{
|
2005-09-14 07:14:41 +02:00
|
|
|
CookieMapIterator iter = CRefMgr::Get()->GetCookieIterator();
|
|
|
|
CookieID id;
|
|
|
|
|
|
|
|
for ( id = iter.Next(); id != 0; id = iter.Next() ) {
|
|
|
|
SafeCref scr( id );
|
2005-10-16 03:19:25 +02:00
|
|
|
if ( cookie != NULL && 0 == strcmp( scr.Cookie(), cookie ) ) {
|
|
|
|
/* print this one */
|
|
|
|
} else if ( connName != NULL &&
|
|
|
|
0 == strcmp( scr.ConnName(), connName ) ) {
|
|
|
|
/* print this one */
|
|
|
|
} else {
|
|
|
|
continue;
|
2005-09-14 07:14:41 +02:00
|
|
|
}
|
2005-10-16 03:19:25 +02:00
|
|
|
string s;
|
|
|
|
scr.PrintCookieInfo( s );
|
|
|
|
|
|
|
|
print_to_sock( socket, 1, s.c_str() );
|
2005-09-14 07:14:41 +02:00
|
|
|
}
|
2005-04-20 13:57:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_socket_info( int out, int which )
|
|
|
|
{
|
|
|
|
string s;
|
2005-09-02 08:56:34 +02:00
|
|
|
CRefMgr::Get()->PrintSocketInfo( which, s );
|
2005-10-14 10:26:56 +02:00
|
|
|
print_to_sock( out, 1, s.c_str() );
|
2005-04-20 13:57:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_sockets( int out, int sought )
|
|
|
|
{
|
2005-09-02 08:56:34 +02:00
|
|
|
SocketsIterator iter = CRefMgr::Get()->MakeSocketsIterator();
|
2005-04-20 13:57:26 +02:00
|
|
|
int sock;
|
|
|
|
while ( (sock = iter.Next()) != 0 ) {
|
|
|
|
if ( sought == 0 || sought == sock ) {
|
|
|
|
print_socket_info( out, sock );
|
2005-03-31 04:20:50 +02:00
|
|
|
}
|
2005-04-20 13:57:26 +02:00
|
|
|
}
|
|
|
|
}
|
2005-03-19 23:16:49 +01:00
|
|
|
|
2005-04-20 13:57:26 +02:00
|
|
|
static int
|
|
|
|
cmd_print( int socket, const char** args )
|
|
|
|
{
|
2005-10-02 18:08:42 +02:00
|
|
|
logf( XW_LOGINFO, "cmd_print called" );
|
2005-04-20 13:57:26 +02:00
|
|
|
int found = 0;
|
2005-10-16 03:19:25 +02:00
|
|
|
if ( 0 == strcmp( "cref", args[1] ) ) {
|
2005-04-20 13:57:26 +02:00
|
|
|
if ( 0 == strcmp( "all", args[2] ) ) {
|
|
|
|
print_cookies( socket, (CookieID)0 );
|
|
|
|
found = 1;
|
2005-10-16 03:19:25 +02:00
|
|
|
} else if ( 0 == strcmp( "cookie", args[2] ) ) {
|
|
|
|
print_cookies( socket, args[3], NULL );
|
|
|
|
found = 1;
|
|
|
|
} else if ( 0 == strcmp( "connName", args[2] ) ) {
|
|
|
|
print_cookies( socket, NULL, args[3] );
|
2005-04-20 13:57:26 +02:00
|
|
|
found = 1;
|
|
|
|
} else if ( 0 == strcmp( "id", args[2] ) ) {
|
|
|
|
print_cookies( socket, atoi(args[3]) );
|
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
} else if ( 0 == strcmp( "socket", args[1] ) ) {
|
|
|
|
if ( 0 == strcmp( "all", args[2] ) ) {
|
|
|
|
print_sockets( socket, 0 );
|
|
|
|
found = 1;
|
|
|
|
} else if ( 0 == strcmp( "id", args[2] ) ) {
|
|
|
|
print_sockets( socket, atoi(args[3]) );
|
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !found ) {
|
|
|
|
char* str =
|
2005-10-16 03:19:25 +02:00
|
|
|
"* %s cref all\n"
|
|
|
|
" %s cref name <name>\n"
|
|
|
|
" %s cref connName <name>\n"
|
|
|
|
" %s cref id <id>\n"
|
2005-10-15 18:28:26 +02:00
|
|
|
" %s socket all\n"
|
2005-10-16 03:19:25 +02:00
|
|
|
" %s socket <num> -- print info about crefs and sockets";
|
2005-10-14 10:26:56 +02:00
|
|
|
print_to_sock( socket, 1, str,
|
2005-04-20 13:57:26 +02:00
|
|
|
args[0], args[0], args[0], args[0], args[0] );
|
2005-03-31 04:20:50 +02:00
|
|
|
}
|
|
|
|
return 0;
|
2005-10-14 10:26:56 +02:00
|
|
|
} /* cmd_print */
|
2005-03-19 23:16:49 +01:00
|
|
|
|
|
|
|
static int
|
2005-03-31 04:20:50 +02:00
|
|
|
cmd_lock( int socket, const char** args )
|
2005-03-19 23:16:49 +01:00
|
|
|
{
|
2005-09-02 08:56:34 +02:00
|
|
|
CRefMgr* mgr = CRefMgr::Get();
|
2005-03-31 04:20:50 +02:00
|
|
|
if ( 0 == strcmp( "on", args[1] ) ) {
|
2005-09-02 08:56:34 +02:00
|
|
|
mgr->LockAll();
|
2005-03-31 04:20:50 +02:00
|
|
|
} else if ( 0 == strcmp( "off", args[1] ) ) {
|
2005-09-02 08:56:34 +02:00
|
|
|
mgr->UnlockAll();
|
2005-03-19 23:16:49 +01:00
|
|
|
} else {
|
2005-10-15 18:28:26 +02:00
|
|
|
print_to_sock( socket, 1, "* %s [on|off] -- lock/unlock access mutex",
|
|
|
|
args[0] );
|
2005-03-19 23:16:49 +01:00
|
|
|
}
|
2005-03-31 04:20:50 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
} /* cmd_lock */
|
|
|
|
|
|
|
|
static int
|
|
|
|
cmd_help( int socket, const char** args )
|
|
|
|
{
|
|
|
|
if ( 0 == strcmp( "help", args[1] ) ) {
|
2005-10-15 18:28:26 +02:00
|
|
|
print_to_sock( socket, 1, "* %s -- prints this", args[0] );
|
2005-03-31 04:20:50 +02:00
|
|
|
} else {
|
|
|
|
|
|
|
|
const char* help[] = { NULL, "help", NULL, NULL };
|
|
|
|
const FuncRec* fp = gFuncs;
|
|
|
|
const FuncRec* last = fp + (sizeof(gFuncs) / sizeof(gFuncs[0]));
|
|
|
|
while ( fp < last ) {
|
|
|
|
help[0] = fp->name;
|
|
|
|
(*fp->func)( socket, help );
|
|
|
|
++fp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-10-14 10:26:56 +02:00
|
|
|
static void
|
|
|
|
print_prompt( int socket )
|
|
|
|
{
|
|
|
|
print_to_sock( socket, 0, "=> " );
|
|
|
|
}
|
|
|
|
|
2005-03-31 04:20:50 +02:00
|
|
|
static int
|
|
|
|
dispatch_command( int sock, const char** args )
|
|
|
|
{
|
|
|
|
const char* cmd = args[0];
|
|
|
|
const FuncRec* fp = gFuncs;
|
|
|
|
const FuncRec* last = fp + (sizeof(gFuncs) / sizeof(gFuncs[0]));
|
|
|
|
while ( fp < last ) {
|
|
|
|
if ( 0 == strcmp( cmd, fp->name ) ) {
|
|
|
|
return (*fp->func)( sock, args );
|
|
|
|
}
|
|
|
|
++fp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( fp == last ) {
|
2005-10-14 10:26:56 +02:00
|
|
|
print_to_sock( sock, 1, "unknown command: \"%s\"", cmd );
|
2005-03-31 04:20:50 +02:00
|
|
|
cmd_help( sock, args );
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2005-03-19 23:16:49 +01:00
|
|
|
}
|
|
|
|
|
2005-03-30 03:52:10 +02:00
|
|
|
static void*
|
2005-03-19 23:16:49 +01:00
|
|
|
ctrl_thread_main( void* arg )
|
|
|
|
{
|
2005-03-30 03:52:10 +02:00
|
|
|
int socket = (int)arg;
|
2005-10-16 03:19:25 +02:00
|
|
|
string arg0, arg1, arg2, arg3;
|
2005-03-19 23:16:49 +01:00
|
|
|
|
2005-10-23 17:49:48 +02:00
|
|
|
{
|
|
|
|
MutexLock ml( &g_ctrlSocksMutex );
|
|
|
|
g_ctrlSocks.push_back( socket );
|
|
|
|
}
|
|
|
|
|
2005-03-19 23:16:49 +01:00
|
|
|
for ( ; ; ) {
|
2005-10-14 10:26:56 +02:00
|
|
|
print_prompt( socket );
|
|
|
|
|
2005-03-19 23:16:49 +01:00
|
|
|
char buf[512];
|
2005-03-30 03:52:10 +02:00
|
|
|
ssize_t nGot = recv( socket, buf, sizeof(buf)-1, 0 );
|
2005-03-19 23:16:49 +01:00
|
|
|
if ( nGot <= 1 ) { /* break when just \n comes in */
|
|
|
|
break;
|
2005-10-16 03:19:25 +02:00
|
|
|
} else if ( nGot > 2 ) {
|
|
|
|
/* if nGot is 2, reuse prev string */
|
|
|
|
buf[nGot] = '\0';
|
|
|
|
istringstream cmd( buf );
|
|
|
|
cmd >> arg0 >> arg1 >> arg2 >> arg3;
|
2005-03-19 23:16:49 +01:00
|
|
|
}
|
|
|
|
|
2005-03-31 04:20:50 +02:00
|
|
|
const char* args[] = {
|
|
|
|
arg0.c_str(),
|
|
|
|
arg1.c_str(),
|
|
|
|
arg2.c_str(),
|
|
|
|
arg3.c_str()
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( dispatch_command( socket, args ) ) {
|
2005-03-19 23:16:49 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-10-23 17:49:48 +02:00
|
|
|
|
2005-03-30 03:52:10 +02:00
|
|
|
close ( socket );
|
2005-10-23 17:49:48 +02:00
|
|
|
|
|
|
|
MutexLock ml( &g_ctrlSocksMutex );
|
|
|
|
vector<int>::iterator iter = g_ctrlSocks.begin();
|
|
|
|
while ( iter != g_ctrlSocks.end() ) {
|
|
|
|
if ( *iter == socket ) {
|
|
|
|
g_ctrlSocks.erase(iter);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-04-08 16:17:28 +02:00
|
|
|
return NULL;
|
2005-04-20 13:57:26 +02:00
|
|
|
} /* ctrl_thread_main */
|
2005-03-30 03:52:10 +02:00
|
|
|
|
|
|
|
void
|
2005-10-23 17:49:48 +02:00
|
|
|
run_ctrl_thread( int ctrl_sock )
|
2005-03-30 03:52:10 +02:00
|
|
|
{
|
2005-10-23 17:49:48 +02:00
|
|
|
logf( XW_LOGINFO, "calling accept on socket %d\n", ctrl_sock );
|
2005-03-30 03:52:10 +02:00
|
|
|
|
|
|
|
sockaddr newaddr;
|
|
|
|
socklen_t siz = sizeof(newaddr);
|
2005-10-23 17:49:48 +02:00
|
|
|
int newSock = accept( ctrl_sock, &newaddr, &siz );
|
2005-10-02 18:08:42 +02:00
|
|
|
logf( XW_LOGINFO, "got one for ctrl: %d", newSock );
|
2005-03-30 03:52:10 +02:00
|
|
|
|
|
|
|
pthread_t thread;
|
|
|
|
int result = pthread_create( &thread, NULL,
|
|
|
|
ctrl_thread_main, (void*)newSock );
|
2005-10-23 17:49:48 +02:00
|
|
|
pthread_detach( thread );
|
|
|
|
|
2005-04-20 13:57:26 +02:00
|
|
|
assert( result == 0 );
|
2005-03-19 23:16:49 +01:00
|
|
|
}
|
2005-10-23 17:49:48 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
stop_ctrl_threads()
|
|
|
|
{
|
|
|
|
MutexLock ml( &g_ctrlSocksMutex );
|
|
|
|
vector<int>::iterator iter = g_ctrlSocks.begin();
|
|
|
|
while ( iter != g_ctrlSocks.end() ) {
|
|
|
|
int sock = *iter++;
|
|
|
|
print_to_sock( sock, 1, "relay going down..." );
|
|
|
|
close( sock );
|
|
|
|
}
|
|
|
|
}
|