2002-08-02 19:57:46 +00:00
|
|
|
/*
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
|
|
|
* Copyright (C) 2002 Chris Schoeneman
|
|
|
|
*
|
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* found in the file COPYING that should have accompanied this file.
|
|
|
|
*
|
|
|
|
* This package 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.
|
|
|
|
*/
|
|
|
|
|
2004-02-14 14:04:36 +00:00
|
|
|
#include "CClientListener.h"
|
|
|
|
#include "CClientProxy.h"
|
2002-05-31 14:34:16 +00:00
|
|
|
#include "CConfig.h"
|
2004-02-14 14:04:36 +00:00
|
|
|
#include "CPrimaryClient.h"
|
|
|
|
#include "CServer.h"
|
|
|
|
#include "CScreen.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include "ProtocolTypes.h"
|
|
|
|
#include "Version.h"
|
2002-07-31 12:39:34 +00:00
|
|
|
#include "XScreen.h"
|
2004-02-14 14:04:36 +00:00
|
|
|
#include "CSocketMultiplexer.h"
|
2002-07-30 18:31:00 +00:00
|
|
|
#include "CTCPSocketFactory.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include "XSocket.h"
|
2002-05-31 14:25:26 +00:00
|
|
|
#include "CThread.h"
|
2004-02-14 14:04:36 +00:00
|
|
|
#include "CEventQueue.h"
|
|
|
|
#include "CFunctionEventJob.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include "CLog.h"
|
2004-02-28 12:19:49 +00:00
|
|
|
#include "CString.h"
|
|
|
|
#include "CStringUtil.h"
|
2003-01-04 22:01:32 +00:00
|
|
|
#include "LogOutputters.h"
|
|
|
|
#include "CArch.h"
|
2004-02-14 14:04:36 +00:00
|
|
|
#include "XArch.h"
|
2002-06-01 19:26:11 +00:00
|
|
|
#include "stdfstream.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include <cstring>
|
2002-05-31 17:32:26 +00:00
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
#define DAEMON_RUNNING(running_)
|
2004-04-11 14:58:08 +00:00
|
|
|
#if WINAPI_MSWINDOWS
|
2003-03-21 19:34:08 +00:00
|
|
|
#include "CArchMiscWindows.h"
|
2004-02-28 12:19:49 +00:00
|
|
|
#include "CMSWindowsScreen.h"
|
|
|
|
#include "CMSWindowsUtil.h"
|
2003-03-12 22:34:07 +00:00
|
|
|
#include "CMSWindowsServerTaskBarReceiver.h"
|
2002-08-11 11:49:36 +00:00
|
|
|
#include "resource.h"
|
2003-01-04 22:01:32 +00:00
|
|
|
#undef DAEMON_RUNNING
|
|
|
|
#define DAEMON_RUNNING(running_) CArchMiscWindows::daemonRunning(running_)
|
2004-04-11 14:58:08 +00:00
|
|
|
#elif WINAPI_XWINDOWS
|
2003-09-02 22:05:47 +00:00
|
|
|
#include "CXWindowsScreen.h"
|
2003-03-12 22:34:07 +00:00
|
|
|
#include "CXWindowsServerTaskBarReceiver.h"
|
2004-04-11 14:58:08 +00:00
|
|
|
#elif WINAPI_CARBON
|
|
|
|
#include "COSXScreen.h"
|
|
|
|
#include "COSXServerTaskBarReceiver.h"
|
2002-07-30 18:31:00 +00:00
|
|
|
#endif
|
|
|
|
|
2002-06-08 21:48:00 +00:00
|
|
|
// platform dependent name of a daemon
|
2004-04-11 14:58:08 +00:00
|
|
|
#if SYSAPI_WIN32
|
2002-06-08 21:48:00 +00:00
|
|
|
#define DAEMON_NAME "Synergy Server"
|
2004-04-11 14:58:08 +00:00
|
|
|
#elif SYSAPI_UNIX
|
2002-08-11 22:43:07 +00:00
|
|
|
#define DAEMON_NAME "synergys"
|
2002-06-08 21:48:00 +00:00
|
|
|
#endif
|
|
|
|
|
2002-06-03 18:53:18 +00:00
|
|
|
// configuration file name
|
2004-04-11 14:58:08 +00:00
|
|
|
#if SYSAPI_WIN32
|
2002-08-11 22:43:07 +00:00
|
|
|
#define USR_CONFIG_NAME "synergy.sgc"
|
|
|
|
#define SYS_CONFIG_NAME "synergy.sgc"
|
2004-04-11 14:58:08 +00:00
|
|
|
#elif SYSAPI_UNIX
|
2002-08-11 22:43:07 +00:00
|
|
|
#define USR_CONFIG_NAME ".synergy.conf"
|
|
|
|
#define SYS_CONFIG_NAME "synergy.conf"
|
2002-06-03 18:53:18 +00:00
|
|
|
#endif
|
|
|
|
|
2004-02-28 12:19:49 +00:00
|
|
|
typedef int (*StartupFunc)(int, char**);
|
|
|
|
static void parse(int argc, const char* const* argv);
|
2004-02-28 17:49:29 +00:00
|
|
|
static bool loadConfig(const CString& pathname);
|
2004-02-28 12:19:49 +00:00
|
|
|
static void loadConfig();
|
|
|
|
|
2002-05-31 17:32:26 +00:00
|
|
|
//
|
2002-06-03 18:53:18 +00:00
|
|
|
// program arguments
|
2002-05-31 17:32:26 +00:00
|
|
|
//
|
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
#define ARG CArgs::s_instance
|
2002-05-31 17:32:26 +00:00
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
class CArgs {
|
|
|
|
public:
|
|
|
|
CArgs() :
|
|
|
|
m_pname(NULL),
|
|
|
|
m_backend(false),
|
|
|
|
m_restartable(true),
|
|
|
|
m_daemon(true),
|
2004-02-28 17:49:29 +00:00
|
|
|
m_configFile(),
|
2004-11-02 20:43:55 +00:00
|
|
|
m_logFilter(NULL),
|
|
|
|
m_display(NULL),
|
|
|
|
m_synergyAddress(NULL),
|
|
|
|
m_config(NULL)
|
2003-01-04 22:01:32 +00:00
|
|
|
{ s_instance = this; }
|
|
|
|
~CArgs() { s_instance = NULL; }
|
2002-05-31 14:25:26 +00:00
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
public:
|
|
|
|
static CArgs* s_instance;
|
|
|
|
const char* m_pname;
|
|
|
|
bool m_backend;
|
|
|
|
bool m_restartable;
|
|
|
|
bool m_daemon;
|
2004-02-28 17:49:29 +00:00
|
|
|
CString m_configFile;
|
2003-01-04 22:01:32 +00:00
|
|
|
const char* m_logFilter;
|
2004-11-02 20:43:55 +00:00
|
|
|
const char* m_display;
|
2003-01-04 22:01:32 +00:00
|
|
|
CString m_name;
|
2004-02-28 12:19:49 +00:00
|
|
|
CNetworkAddress* m_synergyAddress;
|
|
|
|
CConfig* m_config;
|
2003-01-04 22:01:32 +00:00
|
|
|
};
|
2002-05-31 14:25:26 +00:00
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
CArgs* CArgs::s_instance = NULL;
|
2002-05-31 14:25:26 +00:00
|
|
|
|
|
|
|
|
2002-07-30 18:31:00 +00:00
|
|
|
//
|
|
|
|
// platform dependent factories
|
|
|
|
//
|
|
|
|
|
2004-02-14 14:04:36 +00:00
|
|
|
static
|
|
|
|
CScreen*
|
|
|
|
createScreen()
|
2002-07-30 18:31:00 +00:00
|
|
|
{
|
2004-04-11 14:58:08 +00:00
|
|
|
#if WINAPI_MSWINDOWS
|
2004-03-13 17:13:55 +00:00
|
|
|
return new CScreen(new CMSWindowsScreen(true, NULL, NULL));
|
2004-04-11 14:58:08 +00:00
|
|
|
#elif WINAPI_XWINDOWS
|
2004-11-02 20:43:55 +00:00
|
|
|
return new CScreen(new CXWindowsScreen(ARG->m_display, true));
|
2004-04-11 14:58:08 +00:00
|
|
|
#elif WINAPI_CARBON
|
|
|
|
return new CScreen(new COSXScreen(true));
|
2002-07-30 18:31:00 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2004-02-28 12:19:49 +00:00
|
|
|
static
|
|
|
|
CServerTaskBarReceiver*
|
|
|
|
createTaskBarReceiver(const CBufferedLogOutputter* logBuffer)
|
|
|
|
{
|
2004-04-11 14:58:08 +00:00
|
|
|
#if WINAPI_MSWINDOWS
|
2004-02-28 12:19:49 +00:00
|
|
|
return new CMSWindowsServerTaskBarReceiver(
|
|
|
|
CMSWindowsScreen::getInstance(), logBuffer);
|
2004-04-11 14:58:08 +00:00
|
|
|
#elif WINAPI_XWINDOWS
|
2004-02-28 12:19:49 +00:00
|
|
|
return new CXWindowsServerTaskBarReceiver(logBuffer);
|
2004-04-11 14:58:08 +00:00
|
|
|
#elif WINAPI_CARBON
|
|
|
|
return new COSXServerTaskBarReceiver(logBuffer);
|
2004-02-28 12:19:49 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2002-07-30 18:31:00 +00:00
|
|
|
|
2004-02-14 14:04:36 +00:00
|
|
|
//
|
|
|
|
// platform independent main
|
|
|
|
//
|
2003-03-12 22:34:07 +00:00
|
|
|
|
2004-11-01 22:25:39 +00:00
|
|
|
static CServer* s_server = NULL;
|
|
|
|
static CScreen* s_serverScreen = NULL;
|
|
|
|
static CPrimaryClient* s_primaryClient = NULL;
|
|
|
|
static CClientListener* s_listener = NULL;
|
|
|
|
static CServerTaskBarReceiver* s_taskBarReceiver = NULL;
|
|
|
|
static CEvent::Type s_reloadConfigEvent = CEvent::kUnknown;
|
|
|
|
static CEvent::Type s_forceReconnectEvent = CEvent::kUnknown;
|
2003-03-12 22:34:07 +00:00
|
|
|
|
2004-02-29 21:31:24 +00:00
|
|
|
CEvent::Type
|
|
|
|
getReloadConfigEvent()
|
|
|
|
{
|
|
|
|
return CEvent::registerTypeOnce(s_reloadConfigEvent, "reloadConfig");
|
|
|
|
}
|
|
|
|
|
2004-11-01 22:25:39 +00:00
|
|
|
CEvent::Type
|
|
|
|
getForceReconnectEvent()
|
|
|
|
{
|
|
|
|
return CEvent::registerTypeOnce(s_forceReconnectEvent, "forceReconnect");
|
|
|
|
}
|
|
|
|
|
2004-02-14 14:04:36 +00:00
|
|
|
static
|
|
|
|
void
|
|
|
|
updateStatus()
|
|
|
|
{
|
|
|
|
s_taskBarReceiver->updateStatus(s_server, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void
|
|
|
|
updateStatus(const CString& msg)
|
|
|
|
{
|
|
|
|
s_taskBarReceiver->updateStatus(s_server, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void
|
|
|
|
handleClientConnected(const CEvent&, void* vlistener)
|
|
|
|
{
|
|
|
|
CClientListener* listener = reinterpret_cast<CClientListener*>(vlistener);
|
|
|
|
CClientProxy* client = listener->getNextClient();
|
|
|
|
if (client != NULL) {
|
|
|
|
s_server->adoptClient(client);
|
|
|
|
updateStatus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
CClientListener*
|
|
|
|
openClientListener(const CNetworkAddress& address)
|
|
|
|
{
|
|
|
|
CClientListener* listen =
|
|
|
|
new CClientListener(address, new CTCPSocketFactory, NULL);
|
|
|
|
EVENTQUEUE->adoptHandler(CClientListener::getConnectedEvent(), listen,
|
|
|
|
new CFunctionEventJob(
|
|
|
|
&handleClientConnected, listen));
|
|
|
|
return listen;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void
|
|
|
|
closeClientListener(CClientListener* listen)
|
|
|
|
{
|
|
|
|
if (listen != NULL) {
|
|
|
|
EVENTQUEUE->removeHandler(CClientListener::getConnectedEvent(), listen);
|
|
|
|
delete listen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-14 16:30:27 +00:00
|
|
|
static
|
2004-02-15 17:32:11 +00:00
|
|
|
void
|
|
|
|
handleScreenError(const CEvent&, void*)
|
2004-02-14 16:30:27 +00:00
|
|
|
{
|
2004-02-15 17:32:11 +00:00
|
|
|
LOG((CLOG_CRIT "error on screen"));
|
|
|
|
EVENTQUEUE->addEvent(CEvent(CEvent::kQuit));
|
2004-02-14 16:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
2004-02-15 17:32:11 +00:00
|
|
|
CScreen*
|
|
|
|
openServerScreen()
|
2004-02-14 16:30:27 +00:00
|
|
|
{
|
2004-02-15 17:32:11 +00:00
|
|
|
CScreen* screen = createScreen();
|
|
|
|
EVENTQUEUE->adoptHandler(IScreen::getErrorEvent(),
|
|
|
|
screen->getEventTarget(),
|
|
|
|
new CFunctionEventJob(
|
|
|
|
&handleScreenError));
|
|
|
|
return screen;
|
2004-02-14 16:30:27 +00:00
|
|
|
}
|
|
|
|
|
2004-02-14 14:04:36 +00:00
|
|
|
static
|
|
|
|
void
|
2004-02-15 17:32:11 +00:00
|
|
|
closeServerScreen(CScreen* screen)
|
2004-02-14 14:04:36 +00:00
|
|
|
{
|
2004-02-15 17:32:11 +00:00
|
|
|
if (screen != NULL) {
|
|
|
|
EVENTQUEUE->removeHandler(IScreen::getErrorEvent(),
|
|
|
|
screen->getEventTarget());
|
|
|
|
delete screen;
|
|
|
|
}
|
2004-02-14 14:04:36 +00:00
|
|
|
}
|
2003-03-12 22:34:07 +00:00
|
|
|
|
2004-02-14 14:04:36 +00:00
|
|
|
static
|
|
|
|
CPrimaryClient*
|
2004-02-14 16:30:27 +00:00
|
|
|
openPrimaryClient(const CString& name, CScreen* screen)
|
2003-03-12 22:34:07 +00:00
|
|
|
{
|
2004-02-14 14:04:36 +00:00
|
|
|
LOG((CLOG_DEBUG1 "creating primary screen"));
|
2004-02-15 17:32:11 +00:00
|
|
|
return new CPrimaryClient(name, screen);
|
2003-03-12 22:34:07 +00:00
|
|
|
}
|
|
|
|
|
2004-02-14 14:04:36 +00:00
|
|
|
static
|
|
|
|
void
|
|
|
|
closePrimaryClient(CPrimaryClient* primaryClient)
|
2003-03-12 22:34:07 +00:00
|
|
|
{
|
2004-02-15 17:32:11 +00:00
|
|
|
delete primaryClient;
|
2003-03-12 22:34:07 +00:00
|
|
|
}
|
|
|
|
|
2004-02-14 14:04:36 +00:00
|
|
|
static
|
2003-03-12 22:34:07 +00:00
|
|
|
void
|
2004-02-14 14:04:36 +00:00
|
|
|
handleNoClients(const CEvent&, void*)
|
2003-03-12 22:34:07 +00:00
|
|
|
{
|
2004-02-14 14:04:36 +00:00
|
|
|
updateStatus();
|
2003-03-12 22:34:07 +00:00
|
|
|
}
|
|
|
|
|
2004-02-14 14:04:36 +00:00
|
|
|
static
|
|
|
|
void
|
|
|
|
handleClientsDisconnected(const CEvent&, void*)
|
|
|
|
{
|
|
|
|
EVENTQUEUE->addEvent(CEvent(CEvent::kQuit));
|
|
|
|
}
|
2003-03-12 22:34:07 +00:00
|
|
|
|
2004-02-14 14:04:36 +00:00
|
|
|
static
|
|
|
|
CServer*
|
|
|
|
openServer(const CConfig& config, CPrimaryClient* primaryClient)
|
|
|
|
{
|
|
|
|
CServer* server = new CServer(config, primaryClient);
|
|
|
|
EVENTQUEUE->adoptHandler(CServer::getDisconnectedEvent(), server,
|
|
|
|
new CFunctionEventJob(handleNoClients));
|
|
|
|
return server;
|
|
|
|
}
|
2001-10-08 19:24:46 +00:00
|
|
|
|
2004-02-14 14:04:36 +00:00
|
|
|
static
|
|
|
|
void
|
|
|
|
closeServer(CServer* server)
|
|
|
|
{
|
|
|
|
if (server == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// tell all clients to disconnect
|
|
|
|
server->disconnect();
|
|
|
|
|
|
|
|
// wait for clients to disconnect for up to timeout seconds
|
|
|
|
double timeout = 3.0;
|
|
|
|
CEventQueueTimer* timer = EVENTQUEUE->newOneShotTimer(timeout, NULL);
|
|
|
|
EVENTQUEUE->adoptHandler(CEvent::kTimer, timer,
|
|
|
|
new CFunctionEventJob(handleClientsDisconnected));
|
|
|
|
EVENTQUEUE->adoptHandler(CServer::getDisconnectedEvent(), server,
|
|
|
|
new CFunctionEventJob(handleClientsDisconnected));
|
|
|
|
CEvent event;
|
|
|
|
EVENTQUEUE->getEvent(event);
|
|
|
|
while (event.getType() != CEvent::kQuit) {
|
|
|
|
EVENTQUEUE->dispatchEvent(event);
|
|
|
|
CEvent::deleteData(event);
|
|
|
|
EVENTQUEUE->getEvent(event);
|
|
|
|
}
|
|
|
|
EVENTQUEUE->removeHandler(CEvent::kTimer, timer);
|
|
|
|
EVENTQUEUE->deleteTimer(timer);
|
|
|
|
EVENTQUEUE->removeHandler(CServer::getDisconnectedEvent(), server);
|
|
|
|
|
|
|
|
// done with server
|
|
|
|
delete server;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool startServer();
|
|
|
|
|
|
|
|
static
|
|
|
|
void
|
|
|
|
retryStartHandler(const CEvent&, void* vtimer)
|
|
|
|
{
|
|
|
|
// discard old timer
|
|
|
|
CEventQueueTimer* timer = reinterpret_cast<CEventQueueTimer*>(vtimer);
|
|
|
|
EVENTQUEUE->deleteTimer(timer);
|
|
|
|
EVENTQUEUE->removeHandler(CEvent::kTimer, NULL);
|
|
|
|
|
|
|
|
// try starting the server again
|
|
|
|
LOG((CLOG_DEBUG1 "retry starting server"));
|
|
|
|
startServer();
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
bool
|
|
|
|
startServer()
|
|
|
|
{
|
|
|
|
double retryTime;
|
2004-02-14 16:30:27 +00:00
|
|
|
CScreen* serverScreen = NULL;
|
2004-02-14 14:04:36 +00:00
|
|
|
CPrimaryClient* primaryClient = NULL;
|
|
|
|
CClientListener* listener = NULL;
|
|
|
|
try {
|
2004-02-28 12:19:49 +00:00
|
|
|
CString name = ARG->m_config->getCanonicalName(ARG->m_name);
|
2004-02-14 16:30:27 +00:00
|
|
|
serverScreen = openServerScreen();
|
|
|
|
primaryClient = openPrimaryClient(name, serverScreen);
|
2004-02-28 12:19:49 +00:00
|
|
|
listener = openClientListener(ARG->m_config->getSynergyAddress());
|
|
|
|
s_server = openServer(*ARG->m_config, primaryClient);
|
2004-02-14 16:30:27 +00:00
|
|
|
s_serverScreen = serverScreen;
|
2004-02-14 14:04:36 +00:00
|
|
|
s_primaryClient = primaryClient;
|
|
|
|
s_listener = listener;
|
|
|
|
updateStatus();
|
|
|
|
LOG((CLOG_NOTE "started server"));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (XScreenUnavailable& e) {
|
|
|
|
LOG((CLOG_WARN "cannot open primary screen: %s", e.what()));
|
|
|
|
closeClientListener(listener);
|
|
|
|
closePrimaryClient(primaryClient);
|
2004-02-14 16:30:27 +00:00
|
|
|
closeServerScreen(serverScreen);
|
2004-02-14 14:04:36 +00:00
|
|
|
updateStatus(CString("cannot open primary screen: ") + e.what());
|
|
|
|
retryTime = e.getRetryTime();
|
|
|
|
}
|
|
|
|
catch (XSocketAddressInUse& e) {
|
|
|
|
LOG((CLOG_WARN "cannot listen for clients: %s", e.what()));
|
|
|
|
closeClientListener(listener);
|
|
|
|
closePrimaryClient(primaryClient);
|
2004-02-14 16:30:27 +00:00
|
|
|
closeServerScreen(serverScreen);
|
2004-02-14 14:04:36 +00:00
|
|
|
updateStatus(CString("cannot listen for clients: ") + e.what());
|
|
|
|
retryTime = 10.0;
|
|
|
|
}
|
|
|
|
catch (XScreenOpenFailure& e) {
|
|
|
|
LOG((CLOG_CRIT "cannot open primary screen: %s", e.what()));
|
|
|
|
closeClientListener(listener);
|
|
|
|
closePrimaryClient(primaryClient);
|
2004-02-14 16:30:27 +00:00
|
|
|
closeServerScreen(serverScreen);
|
2004-02-14 14:04:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch (XBase& e) {
|
|
|
|
LOG((CLOG_CRIT "failed to start server: %s", e.what()));
|
|
|
|
closeClientListener(listener);
|
|
|
|
closePrimaryClient(primaryClient);
|
2004-02-14 16:30:27 +00:00
|
|
|
closeServerScreen(serverScreen);
|
2004-02-14 14:04:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ARG->m_restartable) {
|
|
|
|
// install a timer and handler to retry later
|
|
|
|
LOG((CLOG_DEBUG "retry in %.0f seconds", retryTime));
|
|
|
|
CEventQueueTimer* timer = EVENTQUEUE->newOneShotTimer(retryTime, NULL);
|
|
|
|
EVENTQUEUE->adoptHandler(CEvent::kTimer, timer,
|
|
|
|
new CFunctionEventJob(&retryStartHandler, timer));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// don't try again
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2002-05-31 14:25:26 +00:00
|
|
|
|
2004-02-14 16:30:27 +00:00
|
|
|
static
|
|
|
|
void
|
|
|
|
stopServer()
|
|
|
|
{
|
|
|
|
closeClientListener(s_listener);
|
|
|
|
closeServer(s_server);
|
|
|
|
closePrimaryClient(s_primaryClient);
|
|
|
|
closeServerScreen(s_serverScreen);
|
|
|
|
s_server = NULL;
|
|
|
|
s_listener = NULL;
|
|
|
|
s_primaryClient = NULL;
|
|
|
|
s_serverScreen = NULL;
|
|
|
|
}
|
|
|
|
|
2004-02-28 17:49:29 +00:00
|
|
|
static
|
|
|
|
void
|
|
|
|
reloadSignalHandler(CArch::ESignal, void*)
|
|
|
|
{
|
2004-02-29 21:31:24 +00:00
|
|
|
EVENTQUEUE->addEvent(CEvent(getReloadConfigEvent(),
|
2004-02-28 17:49:29 +00:00
|
|
|
IEventQueue::getSystemTarget()));
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void
|
|
|
|
reloadConfig(const CEvent&, void*)
|
|
|
|
{
|
|
|
|
LOG((CLOG_DEBUG "reload configuration"));
|
|
|
|
if (loadConfig(ARG->m_configFile)) {
|
|
|
|
if (s_server != NULL) {
|
|
|
|
s_server->setConfig(*ARG->m_config);
|
|
|
|
}
|
|
|
|
LOG((CLOG_NOTE "reloaded configuration"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-01 22:25:39 +00:00
|
|
|
static
|
|
|
|
void
|
|
|
|
forceReconnect(const CEvent&, void*)
|
|
|
|
{
|
|
|
|
if (s_server != NULL) {
|
|
|
|
s_server->disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
int
|
2004-02-28 12:19:49 +00:00
|
|
|
mainLoop()
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
2004-02-28 17:49:29 +00:00
|
|
|
// create socket multiplexer. this must happen after daemonization
|
|
|
|
// on unix because threads evaporate across a fork().
|
|
|
|
CSocketMultiplexer multiplexer;
|
|
|
|
|
|
|
|
// create the event queue
|
|
|
|
CEventQueue eventQueue;
|
|
|
|
|
2004-02-14 14:04:36 +00:00
|
|
|
// if configuration has no screens then add this system
|
|
|
|
// as the default
|
2004-02-28 12:19:49 +00:00
|
|
|
if (ARG->m_config->begin() == ARG->m_config->end()) {
|
|
|
|
ARG->m_config->addScreen(ARG->m_name);
|
2004-02-14 14:04:36 +00:00
|
|
|
}
|
2002-06-09 16:53:25 +00:00
|
|
|
|
2004-02-14 14:04:36 +00:00
|
|
|
// set the contact address, if provided, in the config.
|
|
|
|
// otherwise, if the config doesn't have an address, use
|
|
|
|
// the default.
|
2004-02-28 12:19:49 +00:00
|
|
|
if (ARG->m_synergyAddress->isValid()) {
|
|
|
|
ARG->m_config->setSynergyAddress(*ARG->m_synergyAddress);
|
2004-02-14 14:04:36 +00:00
|
|
|
}
|
2004-02-28 12:19:49 +00:00
|
|
|
else if (!ARG->m_config->getSynergyAddress().isValid()) {
|
|
|
|
ARG->m_config->setSynergyAddress(CNetworkAddress(kDefaultPort));
|
2004-02-14 14:04:36 +00:00
|
|
|
}
|
2002-07-24 13:01:18 +00:00
|
|
|
|
2004-02-14 14:04:36 +00:00
|
|
|
// canonicalize the primary screen name
|
2004-02-28 12:19:49 +00:00
|
|
|
CString primaryName = ARG->m_config->getCanonicalName(ARG->m_name);
|
2004-02-14 14:04:36 +00:00
|
|
|
if (primaryName.empty()) {
|
|
|
|
LOG((CLOG_CRIT "unknown screen name `%s'", ARG->m_name.c_str()));
|
|
|
|
return kExitFailed;
|
|
|
|
}
|
2002-07-24 13:01:18 +00:00
|
|
|
|
2004-02-14 14:04:36 +00:00
|
|
|
// start the server. if this return false then we've failed and
|
|
|
|
// we shouldn't retry.
|
|
|
|
LOG((CLOG_DEBUG1 "starting server"));
|
|
|
|
if (!startServer()) {
|
|
|
|
return kExitFailed;
|
|
|
|
}
|
2002-08-11 11:49:36 +00:00
|
|
|
|
2004-02-28 17:49:29 +00:00
|
|
|
// handle hangup signal by reloading the server's configuration
|
|
|
|
ARCH->setSignalHandler(CArch::kHANGUP, &reloadSignalHandler, NULL);
|
2004-02-29 21:31:24 +00:00
|
|
|
EVENTQUEUE->adoptHandler(getReloadConfigEvent(),
|
2004-02-28 17:49:29 +00:00
|
|
|
IEventQueue::getSystemTarget(),
|
|
|
|
new CFunctionEventJob(&reloadConfig));
|
|
|
|
|
2004-11-01 22:25:39 +00:00
|
|
|
// handle force reconnect event by disconnecting clients. they'll
|
|
|
|
// reconnect automatically.
|
|
|
|
EVENTQUEUE->adoptHandler(getForceReconnectEvent(),
|
|
|
|
IEventQueue::getSystemTarget(),
|
|
|
|
new CFunctionEventJob(&forceReconnect));
|
|
|
|
|
2004-02-14 14:04:36 +00:00
|
|
|
// run event loop. if startServer() failed we're supposed to retry
|
|
|
|
// later. the timer installed by startServer() will take care of
|
|
|
|
// that.
|
|
|
|
CEvent event;
|
2004-02-28 12:19:49 +00:00
|
|
|
DAEMON_RUNNING(true);
|
2004-02-14 14:04:36 +00:00
|
|
|
EVENTQUEUE->getEvent(event);
|
|
|
|
while (event.getType() != CEvent::kQuit) {
|
|
|
|
EVENTQUEUE->dispatchEvent(event);
|
|
|
|
CEvent::deleteData(event);
|
|
|
|
EVENTQUEUE->getEvent(event);
|
|
|
|
}
|
|
|
|
DAEMON_RUNNING(false);
|
|
|
|
|
|
|
|
// close down
|
|
|
|
LOG((CLOG_DEBUG1 "stopping server"));
|
2004-11-01 22:25:39 +00:00
|
|
|
EVENTQUEUE->removeHandler(getForceReconnectEvent(),
|
|
|
|
IEventQueue::getSystemTarget());
|
2004-02-29 21:31:24 +00:00
|
|
|
EVENTQUEUE->removeHandler(getReloadConfigEvent(),
|
2004-02-28 17:49:29 +00:00
|
|
|
IEventQueue::getSystemTarget());
|
2004-02-14 16:30:27 +00:00
|
|
|
stopServer();
|
2004-02-14 14:04:36 +00:00
|
|
|
updateStatus();
|
|
|
|
LOG((CLOG_NOTE "stopped server"));
|
|
|
|
|
|
|
|
return kExitSuccess;
|
2001-11-19 00:33:36 +00:00
|
|
|
}
|
|
|
|
|
2003-03-12 22:34:07 +00:00
|
|
|
static
|
2004-02-28 12:19:49 +00:00
|
|
|
int
|
|
|
|
daemonMainLoop(int, const char**)
|
2003-03-12 22:34:07 +00:00
|
|
|
{
|
2004-02-28 12:19:49 +00:00
|
|
|
CSystemLogger sysLogger(DAEMON_NAME);
|
|
|
|
return mainLoop();
|
2003-03-12 22:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int
|
2004-02-28 12:19:49 +00:00
|
|
|
standardStartup(int argc, char** argv)
|
2003-03-12 22:34:07 +00:00
|
|
|
{
|
2004-02-28 12:19:49 +00:00
|
|
|
// parse command line
|
|
|
|
parse(argc, argv);
|
|
|
|
|
|
|
|
// load configuration
|
|
|
|
loadConfig();
|
|
|
|
|
|
|
|
// daemonize if requested
|
|
|
|
if (ARG->m_daemon) {
|
|
|
|
return ARCH->daemonize(DAEMON_NAME, &daemonMainLoop);
|
2003-03-12 22:34:07 +00:00
|
|
|
}
|
2004-02-28 12:19:49 +00:00
|
|
|
else {
|
|
|
|
return mainLoop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int
|
|
|
|
run(int argc, char** argv, ILogOutputter* outputter, StartupFunc startup)
|
|
|
|
{
|
|
|
|
// general initialization
|
|
|
|
ARG->m_synergyAddress = new CNetworkAddress;
|
|
|
|
ARG->m_config = new CConfig;
|
|
|
|
ARG->m_pname = ARCH->getBasename(argv[0]);
|
|
|
|
|
|
|
|
// install caller's output filter
|
|
|
|
if (outputter != NULL) {
|
|
|
|
CLOG->insert(outputter);
|
2003-03-12 22:34:07 +00:00
|
|
|
}
|
2004-02-28 12:19:49 +00:00
|
|
|
|
|
|
|
// save log messages
|
|
|
|
CBufferedLogOutputter logBuffer(1000);
|
|
|
|
CLOG->insert(&logBuffer, true);
|
|
|
|
|
|
|
|
// make the task bar receiver. the user can control this app
|
|
|
|
// through the task bar.
|
|
|
|
s_taskBarReceiver = createTaskBarReceiver(&logBuffer);
|
|
|
|
|
2004-11-01 22:25:39 +00:00
|
|
|
// identify system
|
|
|
|
LOG((CLOG_INFO "Synergy server on %s", ARCH->getOSName().c_str()));
|
|
|
|
|
2004-02-28 12:19:49 +00:00
|
|
|
// run
|
|
|
|
int result = startup(argc, argv);
|
|
|
|
|
|
|
|
// done with task bar receiver
|
|
|
|
delete s_taskBarReceiver;
|
|
|
|
|
|
|
|
// done with log buffer
|
|
|
|
CLOG->remove(&logBuffer);
|
|
|
|
|
|
|
|
delete ARG->m_config;
|
|
|
|
delete ARG->m_synergyAddress;
|
|
|
|
return result;
|
2003-03-12 22:34:07 +00:00
|
|
|
}
|
2004-02-28 12:19:49 +00:00
|
|
|
|
2002-05-31 14:25:26 +00:00
|
|
|
|
2002-06-03 18:53:18 +00:00
|
|
|
//
|
|
|
|
// command line parsing
|
|
|
|
//
|
|
|
|
|
2002-06-08 21:48:00 +00:00
|
|
|
#define BYE "\nTry `%s --help' for more information."
|
|
|
|
|
|
|
|
static void (*bye)(int) = &exit;
|
2002-06-03 18:53:18 +00:00
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
void
|
|
|
|
version()
|
2002-06-03 18:53:18 +00:00
|
|
|
{
|
2002-10-15 21:29:44 +00:00
|
|
|
LOG((CLOG_PRINT
|
2002-07-31 16:57:26 +00:00
|
|
|
"%s %s, protocol version %d.%d\n"
|
2002-06-03 18:53:18 +00:00
|
|
|
"%s",
|
2003-01-04 22:01:32 +00:00
|
|
|
ARG->m_pname,
|
2002-07-31 16:57:26 +00:00
|
|
|
kVersion,
|
2002-06-04 11:06:26 +00:00
|
|
|
kProtocolMajorVersion,
|
|
|
|
kProtocolMinorVersion,
|
|
|
|
kCopyright));
|
2002-06-03 18:53:18 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
void
|
|
|
|
help()
|
2002-06-03 18:53:18 +00:00
|
|
|
{
|
2004-11-02 20:43:55 +00:00
|
|
|
#if WINAPI_XWINDOWS
|
|
|
|
# define USAGE_DISPLAY_ARG \
|
|
|
|
" [--display <display>]"
|
|
|
|
# define USAGE_DISPLAY_INFO \
|
|
|
|
" --display <display> connect to the X server at <display>\n"
|
|
|
|
#else
|
|
|
|
# define USAGE_DISPLAY_ARG
|
|
|
|
# define USAGE_DISPLAY_INFO
|
|
|
|
#endif
|
|
|
|
|
2004-04-11 14:58:08 +00:00
|
|
|
#if SYSAPI_WIN32
|
2002-06-14 18:08:20 +00:00
|
|
|
|
2004-11-02 20:43:55 +00:00
|
|
|
# define PLATFORM_ARGS \
|
|
|
|
" [--daemon|--no-daemon]"
|
2002-09-02 17:30:04 +00:00
|
|
|
# define PLATFORM_DESC
|
2004-11-02 20:43:55 +00:00
|
|
|
# define PLATFORM_EXTRA \
|
2002-06-14 18:08:20 +00:00
|
|
|
"At least one command line argument is required. If you don't otherwise\n" \
|
|
|
|
"need an argument use `--daemon'.\n" \
|
|
|
|
"\n"
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2004-11-02 20:43:55 +00:00
|
|
|
# define PLATFORM_ARGS \
|
2002-06-14 18:08:20 +00:00
|
|
|
" [--daemon|--no-daemon]"
|
|
|
|
# define PLATFORM_DESC
|
|
|
|
# define PLATFORM_EXTRA
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2002-10-15 21:29:44 +00:00
|
|
|
LOG((CLOG_PRINT
|
2002-06-03 18:53:18 +00:00
|
|
|
"Usage: %s"
|
2002-06-09 17:59:32 +00:00
|
|
|
" [--address <address>]"
|
2002-06-03 18:53:18 +00:00
|
|
|
" [--config <pathname>]"
|
2002-06-09 17:59:32 +00:00
|
|
|
" [--debug <level>]"
|
2004-11-02 20:43:55 +00:00
|
|
|
USAGE_DISPLAY_ARG
|
2002-06-09 17:59:32 +00:00
|
|
|
" [--name <screen-name>]"
|
2004-11-02 20:43:55 +00:00
|
|
|
" [--restart|--no-restart]"
|
2002-06-14 18:08:20 +00:00
|
|
|
PLATFORM_ARGS
|
2004-11-02 20:43:55 +00:00
|
|
|
"\n\n"
|
2002-06-03 18:53:18 +00:00
|
|
|
"Start the synergy mouse/keyboard sharing server.\n"
|
|
|
|
"\n"
|
2002-06-09 16:53:25 +00:00
|
|
|
" -a, --address <address> listen for clients on the given address.\n"
|
2002-06-14 18:08:20 +00:00
|
|
|
" -c, --config <pathname> use the named configuration file instead.\n"
|
2002-06-03 18:53:18 +00:00
|
|
|
" -d, --debug <level> filter out log messages with priorty below level.\n"
|
|
|
|
" level may be: FATAL, ERROR, WARNING, NOTE, INFO,\n"
|
|
|
|
" DEBUG, DEBUG1, DEBUG2.\n"
|
2004-11-02 20:43:55 +00:00
|
|
|
USAGE_DISPLAY_INFO
|
2002-06-14 18:08:20 +00:00
|
|
|
" -f, --no-daemon run the server in the foreground.\n"
|
|
|
|
"* --daemon run the server as a daemon.\n"
|
2002-06-09 17:59:32 +00:00
|
|
|
" -n, --name <screen-name> use screen-name instead the hostname to identify\n"
|
|
|
|
" this screen in the configuration.\n"
|
2002-06-03 18:53:18 +00:00
|
|
|
" -1, --no-restart do not try to restart the server if it fails for\n"
|
|
|
|
" some reason.\n"
|
2002-06-08 21:48:00 +00:00
|
|
|
"* --restart restart the server automatically if it fails.\n"
|
2002-06-14 18:08:20 +00:00
|
|
|
PLATFORM_DESC
|
2002-06-03 18:53:18 +00:00
|
|
|
" -h, --help display this help and exit.\n"
|
|
|
|
" --version display version information and exit.\n"
|
|
|
|
"\n"
|
2002-06-08 21:48:00 +00:00
|
|
|
"* marks defaults.\n"
|
|
|
|
"\n"
|
2002-06-14 18:08:20 +00:00
|
|
|
PLATFORM_EXTRA
|
2002-06-09 16:53:25 +00:00
|
|
|
"The argument for --address is of the form: [<hostname>][:<port>]. The\n"
|
|
|
|
"hostname must be the address or hostname of an interface on the system.\n"
|
|
|
|
"The default is to listen on all interfaces. The port overrides the\n"
|
|
|
|
"default port, %d.\n"
|
|
|
|
"\n"
|
2002-06-08 21:48:00 +00:00
|
|
|
"If no configuration file pathname is provided then the first of the\n"
|
2002-09-02 17:30:04 +00:00
|
|
|
"following to load successfully sets the configuration:\n"
|
2002-06-08 21:48:00 +00:00
|
|
|
" %s\n"
|
|
|
|
" %s\n"
|
2002-06-03 18:53:18 +00:00
|
|
|
"If no configuration file can be loaded then the configuration uses its\n"
|
|
|
|
"defaults with just the server screen.\n"
|
|
|
|
"\n"
|
|
|
|
"Where log messages go depends on the platform and whether or not the\n"
|
2002-06-14 18:08:20 +00:00
|
|
|
"server is running as a daemon.",
|
2003-01-04 22:01:32 +00:00
|
|
|
ARG->m_pname,
|
2002-06-09 16:53:25 +00:00
|
|
|
kDefaultPort,
|
2003-01-04 22:01:32 +00:00
|
|
|
ARCH->concatPath(
|
|
|
|
ARCH->getUserDirectory(),
|
2002-08-11 22:43:07 +00:00
|
|
|
USR_CONFIG_NAME).c_str(),
|
2003-01-04 22:01:32 +00:00
|
|
|
ARCH->concatPath(
|
|
|
|
ARCH->getSystemDirectory(),
|
2002-08-11 22:43:07 +00:00
|
|
|
SYS_CONFIG_NAME).c_str()));
|
2002-06-03 18:53:18 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
bool
|
2003-03-12 22:34:07 +00:00
|
|
|
isArg(int argi, int argc, const char* const* argv,
|
2002-06-17 13:31:21 +00:00
|
|
|
const char* name1, const char* name2,
|
|
|
|
int minRequiredParameters = 0)
|
2002-06-03 18:53:18 +00:00
|
|
|
{
|
|
|
|
if ((name1 != NULL && strcmp(argv[argi], name1) == 0) ||
|
|
|
|
(name2 != NULL && strcmp(argv[argi], name2) == 0)) {
|
|
|
|
// match. check args left.
|
|
|
|
if (argi + minRequiredParameters >= argc) {
|
2002-10-15 21:29:44 +00:00
|
|
|
LOG((CLOG_PRINT "%s: missing arguments for `%s'" BYE,
|
2003-01-04 22:01:32 +00:00
|
|
|
ARG->m_pname, argv[argi], ARG->m_pname));
|
2002-07-24 13:01:18 +00:00
|
|
|
bye(kExitArgs);
|
2002-06-03 18:53:18 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// no match
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
void
|
2003-03-12 22:34:07 +00:00
|
|
|
parse(int argc, const char* const* argv)
|
2002-06-03 18:53:18 +00:00
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
assert(ARG->m_pname != NULL);
|
|
|
|
assert(argv != NULL);
|
|
|
|
assert(argc >= 1);
|
2002-06-03 18:53:18 +00:00
|
|
|
|
2002-06-09 17:59:32 +00:00
|
|
|
// set defaults
|
2003-01-04 22:01:32 +00:00
|
|
|
ARG->m_name = ARCH->getHostName();
|
2002-06-09 17:59:32 +00:00
|
|
|
|
2002-06-03 18:53:18 +00:00
|
|
|
// parse options
|
2004-04-11 19:15:09 +00:00
|
|
|
int i = 1;
|
|
|
|
for (; i < argc; ++i) {
|
2002-06-03 18:53:18 +00:00
|
|
|
if (isArg(i, argc, argv, "-d", "--debug", 1)) {
|
|
|
|
// change logging level
|
2003-01-04 22:01:32 +00:00
|
|
|
ARG->m_logFilter = argv[++i];
|
2002-06-03 18:53:18 +00:00
|
|
|
}
|
|
|
|
|
2002-06-09 16:53:25 +00:00
|
|
|
else if (isArg(i, argc, argv, "-a", "--address", 1)) {
|
|
|
|
// save listen address
|
|
|
|
try {
|
2004-02-28 12:19:49 +00:00
|
|
|
*ARG->m_synergyAddress = CNetworkAddress(argv[i + 1],
|
2003-01-04 22:01:32 +00:00
|
|
|
kDefaultPort);
|
2002-06-09 16:53:25 +00:00
|
|
|
}
|
2002-07-25 17:52:40 +00:00
|
|
|
catch (XSocketAddress& e) {
|
2002-10-15 21:29:44 +00:00
|
|
|
LOG((CLOG_PRINT "%s: %s" BYE,
|
2003-01-04 22:01:32 +00:00
|
|
|
ARG->m_pname, e.what(), ARG->m_pname));
|
2002-07-24 13:01:18 +00:00
|
|
|
bye(kExitArgs);
|
2002-06-09 16:53:25 +00:00
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
2002-06-09 17:59:32 +00:00
|
|
|
else if (isArg(i, argc, argv, "-n", "--name", 1)) {
|
|
|
|
// save screen name
|
2003-01-04 22:01:32 +00:00
|
|
|
ARG->m_name = argv[++i];
|
2002-06-09 17:59:32 +00:00
|
|
|
}
|
|
|
|
|
2002-06-03 18:53:18 +00:00
|
|
|
else if (isArg(i, argc, argv, "-c", "--config", 1)) {
|
|
|
|
// save configuration file path
|
2003-01-04 22:01:32 +00:00
|
|
|
ARG->m_configFile = argv[++i];
|
2002-06-03 18:53:18 +00:00
|
|
|
}
|
|
|
|
|
2004-11-02 20:43:55 +00:00
|
|
|
#if WINAPI_XWINDOWS
|
|
|
|
else if (isArg(i, argc, argv, "-display", "--display", 1)) {
|
|
|
|
// use alternative display
|
|
|
|
ARG->m_display = argv[++i];
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-06-14 18:08:20 +00:00
|
|
|
else if (isArg(i, argc, argv, "-f", "--no-daemon")) {
|
2002-06-03 18:53:18 +00:00
|
|
|
// not a daemon
|
2003-01-04 22:01:32 +00:00
|
|
|
ARG->m_daemon = false;
|
2002-06-03 18:53:18 +00:00
|
|
|
}
|
|
|
|
|
2002-06-14 18:08:20 +00:00
|
|
|
else if (isArg(i, argc, argv, NULL, "--daemon")) {
|
2002-06-03 18:53:18 +00:00
|
|
|
// daemonize
|
2003-01-04 22:01:32 +00:00
|
|
|
ARG->m_daemon = true;
|
2002-06-03 18:53:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (isArg(i, argc, argv, "-1", "--no-restart")) {
|
|
|
|
// don't try to restart
|
2003-01-04 22:01:32 +00:00
|
|
|
ARG->m_restartable = false;
|
2002-06-03 18:53:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (isArg(i, argc, argv, NULL, "--restart")) {
|
|
|
|
// try to restart
|
2003-01-04 22:01:32 +00:00
|
|
|
ARG->m_restartable = true;
|
2002-06-03 18:53:18 +00:00
|
|
|
}
|
|
|
|
|
2002-08-11 11:49:36 +00:00
|
|
|
else if (isArg(i, argc, argv, "-z", NULL)) {
|
2003-01-04 22:01:32 +00:00
|
|
|
ARG->m_backend = true;
|
2002-08-11 11:49:36 +00:00
|
|
|
}
|
|
|
|
|
2002-06-03 18:53:18 +00:00
|
|
|
else if (isArg(i, argc, argv, "-h", "--help")) {
|
|
|
|
help();
|
2002-07-24 13:01:18 +00:00
|
|
|
bye(kExitSuccess);
|
2002-06-03 18:53:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (isArg(i, argc, argv, NULL, "--version")) {
|
|
|
|
version();
|
2002-07-24 13:01:18 +00:00
|
|
|
bye(kExitSuccess);
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
2002-06-03 18:53:18 +00:00
|
|
|
else if (isArg(i, argc, argv, "--", NULL)) {
|
|
|
|
// remaining arguments are not options
|
|
|
|
++i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (argv[i][0] == '-') {
|
2002-10-15 21:29:44 +00:00
|
|
|
LOG((CLOG_PRINT "%s: unrecognized option `%s'" BYE,
|
2003-01-04 22:01:32 +00:00
|
|
|
ARG->m_pname, argv[i], ARG->m_pname));
|
2002-07-24 13:01:18 +00:00
|
|
|
bye(kExitArgs);
|
2002-06-03 18:53:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
// this and remaining arguments are not options
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// no non-option arguments are allowed
|
|
|
|
if (i != argc) {
|
2002-10-15 21:29:44 +00:00
|
|
|
LOG((CLOG_PRINT "%s: unrecognized option `%s'" BYE,
|
2003-01-04 22:01:32 +00:00
|
|
|
ARG->m_pname, argv[i], ARG->m_pname));
|
2002-07-24 13:01:18 +00:00
|
|
|
bye(kExitArgs);
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// increase default filter level for daemon. the user must
|
|
|
|
// explicitly request another level for a daemon.
|
2003-01-04 22:01:32 +00:00
|
|
|
if (ARG->m_daemon && ARG->m_logFilter == NULL) {
|
2004-04-11 14:58:08 +00:00
|
|
|
#if SYSAPI_WIN32
|
2003-01-04 22:01:32 +00:00
|
|
|
if (CArchMiscWindows::isWindows95Family()) {
|
2002-06-08 21:48:00 +00:00
|
|
|
// windows 95 has no place for logging so avoid showing
|
|
|
|
// the log console window.
|
2003-01-04 22:01:32 +00:00
|
|
|
ARG->m_logFilter = "FATAL";
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
ARG->m_logFilter = "NOTE";
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
2002-06-03 18:53:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// set log filter
|
2003-01-04 22:01:32 +00:00
|
|
|
if (!CLOG->setFilter(ARG->m_logFilter)) {
|
2002-10-15 21:29:44 +00:00
|
|
|
LOG((CLOG_PRINT "%s: unrecognized log level `%s'" BYE,
|
2003-01-04 22:01:32 +00:00
|
|
|
ARG->m_pname, ARG->m_logFilter, ARG->m_pname));
|
2002-07-24 13:01:18 +00:00
|
|
|
bye(kExitArgs);
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
bool
|
2004-02-28 17:49:29 +00:00
|
|
|
loadConfig(const CString& pathname)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
// load configuration
|
2004-02-28 17:49:29 +00:00
|
|
|
LOG((CLOG_DEBUG "opening configuration \"%s\"", pathname.c_str()));
|
|
|
|
std::ifstream configStream(pathname.c_str());
|
2004-07-29 22:09:28 +00:00
|
|
|
if (!configStream.is_open()) {
|
2004-09-27 21:04:37 +00:00
|
|
|
// report failure to open configuration as a debug message
|
|
|
|
// since we try several paths and we expect some to be
|
|
|
|
// missing.
|
|
|
|
LOG((CLOG_DEBUG "cannot open configuration \"%s\"",
|
|
|
|
pathname.c_str()));
|
|
|
|
return false;
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
2004-02-28 12:19:49 +00:00
|
|
|
configStream >> *ARG->m_config;
|
2002-10-15 21:29:44 +00:00
|
|
|
LOG((CLOG_DEBUG "configuration read successfully"));
|
2002-06-08 21:48:00 +00:00
|
|
|
return true;
|
2002-06-03 18:53:18 +00:00
|
|
|
}
|
2002-06-08 23:24:40 +00:00
|
|
|
catch (XConfigRead& e) {
|
2004-09-27 21:04:37 +00:00
|
|
|
// report error in configuration file
|
|
|
|
LOG((CLOG_ERR "cannot read configuration \"%s\": %s",
|
2004-02-28 17:49:29 +00:00
|
|
|
pathname.c_str(), e.what()));
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2002-06-03 18:53:18 +00:00
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
void
|
|
|
|
loadConfig()
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
2004-02-15 17:32:11 +00:00
|
|
|
bool loaded = false;
|
|
|
|
|
2002-06-08 21:48:00 +00:00
|
|
|
// load the config file, if specified
|
2004-02-28 17:49:29 +00:00
|
|
|
if (!ARG->m_configFile.empty()) {
|
2004-02-15 17:32:11 +00:00
|
|
|
loaded = loadConfig(ARG->m_configFile);
|
2002-06-03 18:53:18 +00:00
|
|
|
}
|
|
|
|
|
2002-06-08 21:48:00 +00:00
|
|
|
// load the default configuration if no explicit file given
|
|
|
|
else {
|
2004-02-15 17:32:11 +00:00
|
|
|
// get the user's home directory
|
2003-01-04 22:01:32 +00:00
|
|
|
CString path = ARCH->getUserDirectory();
|
2002-06-08 21:48:00 +00:00
|
|
|
if (!path.empty()) {
|
|
|
|
// complete path
|
2003-01-04 22:01:32 +00:00
|
|
|
path = ARCH->concatPath(path, USR_CONFIG_NAME);
|
2002-06-08 21:48:00 +00:00
|
|
|
|
|
|
|
// now try loading the user's configuration
|
2004-02-28 17:49:29 +00:00
|
|
|
if (loadConfig(path)) {
|
|
|
|
loaded = true;
|
|
|
|
ARG->m_configFile = path;
|
|
|
|
}
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
if (!loaded) {
|
|
|
|
// try the system-wide config file
|
2003-01-04 22:01:32 +00:00
|
|
|
path = ARCH->getSystemDirectory();
|
2002-06-08 21:48:00 +00:00
|
|
|
if (!path.empty()) {
|
2003-01-04 22:01:32 +00:00
|
|
|
path = ARCH->concatPath(path, SYS_CONFIG_NAME);
|
2004-02-28 17:49:29 +00:00
|
|
|
if (loadConfig(path)) {
|
|
|
|
loaded = true;
|
|
|
|
ARG->m_configFile = path;
|
|
|
|
}
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-02-15 17:32:11 +00:00
|
|
|
|
|
|
|
if (!loaded) {
|
|
|
|
LOG((CLOG_PRINT "%s: no configuration available", ARG->m_pname));
|
|
|
|
bye(kExitConfig);
|
|
|
|
}
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
2002-06-03 18:53:18 +00:00
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
|
2002-05-31 14:25:26 +00:00
|
|
|
//
|
|
|
|
// platform dependent entry points
|
|
|
|
//
|
|
|
|
|
2004-04-11 14:58:08 +00:00
|
|
|
#if SYSAPI_WIN32
|
2001-11-19 00:33:36 +00:00
|
|
|
|
2002-09-14 12:03:43 +00:00
|
|
|
static bool s_hasImportantLogMessages = false;
|
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
//
|
|
|
|
// CMessageBoxOutputter
|
|
|
|
//
|
|
|
|
// This class writes severe log messages to a message box
|
|
|
|
//
|
|
|
|
|
|
|
|
class CMessageBoxOutputter : public ILogOutputter {
|
|
|
|
public:
|
|
|
|
CMessageBoxOutputter() { }
|
|
|
|
virtual ~CMessageBoxOutputter() { }
|
|
|
|
|
|
|
|
// ILogOutputter overrides
|
|
|
|
virtual void open(const char*) { }
|
|
|
|
virtual void close() { }
|
|
|
|
virtual bool write(ELevel level, const char* message);
|
|
|
|
virtual const char* getNewline() const { return ""; }
|
|
|
|
};
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
bool
|
2003-01-04 22:01:32 +00:00
|
|
|
CMessageBoxOutputter::write(ELevel level, const char* message)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
2002-09-14 12:03:43 +00:00
|
|
|
// note any important messages the user may need to know about
|
2003-01-04 22:01:32 +00:00
|
|
|
if (level <= CLog::kWARNING) {
|
2002-09-14 12:03:43 +00:00
|
|
|
s_hasImportantLogMessages = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FATAL and PRINT messages get a dialog box if not running as
|
|
|
|
// backend. if we're running as a backend the user will have
|
|
|
|
// a chance to see the messages when we exit.
|
2003-01-04 22:01:32 +00:00
|
|
|
if (!ARG->m_backend && level <= CLog::kFATAL) {
|
|
|
|
MessageBox(NULL, message, ARG->m_pname, MB_OK | MB_ICONWARNING);
|
|
|
|
return false;
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
else {
|
2003-01-04 22:01:32 +00:00
|
|
|
return true;
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
void
|
|
|
|
byeThrow(int x)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
CArchMiscWindows::daemonFailed(x);
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
int
|
2004-02-28 12:19:49 +00:00
|
|
|
daemonNTMainLoop(int argc, const char** argv)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
|
|
|
parse(argc, argv);
|
2003-01-04 22:01:32 +00:00
|
|
|
ARG->m_backend = false;
|
2002-06-08 21:48:00 +00:00
|
|
|
loadConfig();
|
2004-02-28 12:19:49 +00:00
|
|
|
return CArchMiscWindows::runDaemon(mainLoop);
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
2002-06-17 15:44:45 +00:00
|
|
|
int
|
2004-02-28 12:19:49 +00:00
|
|
|
daemonNTStartup(int, char**)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
2003-03-12 22:34:07 +00:00
|
|
|
CSystemLogger sysLogger(DAEMON_NAME);
|
2004-02-28 12:19:49 +00:00
|
|
|
bye = &byeThrow;
|
|
|
|
return ARCH->daemonize(DAEMON_NAME, &daemonNTMainLoop);
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
2001-11-19 00:33:36 +00:00
|
|
|
|
2003-03-12 22:34:07 +00:00
|
|
|
static
|
2004-02-28 12:19:49 +00:00
|
|
|
void
|
|
|
|
showError(HINSTANCE instance, const char* title, UINT id, const char* arg)
|
2001-11-19 00:33:36 +00:00
|
|
|
{
|
2004-02-28 12:19:49 +00:00
|
|
|
CString fmt = CMSWindowsUtil::getString(instance, id);
|
|
|
|
CString msg = CStringUtil::format(fmt.c_str(), arg);
|
|
|
|
MessageBox(NULL, msg.c_str(), title, MB_OK | MB_ICONWARNING);
|
2003-03-12 22:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int WINAPI
|
|
|
|
WinMain(HINSTANCE instance, HINSTANCE, LPSTR, int)
|
|
|
|
{
|
2004-02-28 12:19:49 +00:00
|
|
|
try {
|
|
|
|
CArch arch(instance);
|
|
|
|
CMSWindowsScreen::init(instance);
|
|
|
|
CLOG;
|
2004-02-28 17:51:55 +00:00
|
|
|
CThread::getCurrentThread().setPriority(-14);
|
2004-02-28 12:19:49 +00:00
|
|
|
CArgs args;
|
|
|
|
|
|
|
|
// windows NT family starts services using no command line options.
|
|
|
|
// since i'm not sure how to tell the difference between that and
|
|
|
|
// a user providing no options we'll assume that if there are no
|
|
|
|
// arguments and we're on NT then we're being invoked as a service.
|
|
|
|
// users on NT can use `--daemon' or `--no-daemon' to force us out
|
|
|
|
// of the service code path.
|
|
|
|
StartupFunc startup = &standardStartup;
|
|
|
|
if (__argc <= 1 && !CArchMiscWindows::isWindows95Family()) {
|
|
|
|
startup = &daemonNTStartup;
|
|
|
|
}
|
2003-03-12 22:34:07 +00:00
|
|
|
|
2004-02-28 12:19:49 +00:00
|
|
|
// send PRINT and FATAL output to a message box
|
|
|
|
int result = run(__argc, __argv, new CMessageBoxOutputter, startup);
|
2003-06-02 20:06:03 +00:00
|
|
|
|
2004-02-28 12:19:49 +00:00
|
|
|
// let user examine any messages if we're running as a backend
|
|
|
|
// by putting up a dialog box before exiting.
|
|
|
|
if (args.m_backend && s_hasImportantLogMessages) {
|
|
|
|
showError(instance, args.m_pname, IDS_FAILED, "");
|
|
|
|
}
|
2003-03-12 22:34:07 +00:00
|
|
|
|
2004-02-28 12:19:49 +00:00
|
|
|
delete CLOG;
|
|
|
|
return result;
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
2004-02-28 12:19:49 +00:00
|
|
|
catch (XBase& e) {
|
|
|
|
showError(instance, __argv[0], IDS_UNCAUGHT_EXCEPTION, e.what());
|
|
|
|
throw;
|
2003-03-12 22:34:07 +00:00
|
|
|
}
|
2004-02-28 12:19:49 +00:00
|
|
|
catch (XArch& e) {
|
|
|
|
showError(instance, __argv[0], IDS_INIT_FAILED, e.what().c_str());
|
|
|
|
return kExitFailed;
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
showError(instance, __argv[0], IDS_UNCAUGHT_EXCEPTION, "<unknown>");
|
|
|
|
throw;
|
2002-09-14 12:03:43 +00:00
|
|
|
}
|
2001-11-19 00:33:36 +00:00
|
|
|
}
|
|
|
|
|
2004-04-11 14:58:08 +00:00
|
|
|
#elif SYSAPI_UNIX
|
2001-11-19 00:33:36 +00:00
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
int
|
2002-06-17 13:31:21 +00:00
|
|
|
main(int argc, char** argv)
|
2001-11-19 00:33:36 +00:00
|
|
|
{
|
2004-02-14 14:04:36 +00:00
|
|
|
CArgs args;
|
2004-02-28 12:19:49 +00:00
|
|
|
try {
|
|
|
|
int result;
|
|
|
|
CArch arch;
|
|
|
|
CLOG;
|
|
|
|
CArgs args;
|
|
|
|
result = run(argc, argv, NULL, &standardStartup);
|
|
|
|
delete CLOG;
|
|
|
|
return result;
|
2001-11-19 00:33:36 +00:00
|
|
|
}
|
2004-02-28 12:19:49 +00:00
|
|
|
catch (XBase& e) {
|
|
|
|
LOG((CLOG_CRIT "Uncaught exception: %s\n", e.what()));
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch (XArch& e) {
|
|
|
|
LOG((CLOG_CRIT "Initialization failed: %s" BYE, e.what().c_str()));
|
|
|
|
return kExitFailed;
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
LOG((CLOG_CRIT "Uncaught exception: <unknown exception>\n"));
|
|
|
|
throw;
|
2002-06-02 18:49:35 +00:00
|
|
|
}
|
2001-10-08 19:24:46 +00:00
|
|
|
}
|
2001-11-19 00:33:36 +00:00
|
|
|
|
2002-06-03 18:53:18 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
#error no main() for platform
|
|
|
|
|
2001-11-19 00:33:36 +00:00
|
|
|
#endif
|