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.
|
|
|
|
*/
|
|
|
|
|
2001-10-08 19:24:46 +00:00
|
|
|
#include "CClient.h"
|
2002-07-30 18:31:00 +00:00
|
|
|
#include "ISecondaryScreenFactory.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"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include "CNetworkAddress.h"
|
2002-07-30 18:31:00 +00:00
|
|
|
#include "CTCPSocketFactory.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include "XSocket.h"
|
2002-06-08 21:48:00 +00:00
|
|
|
#include "CCondVar.h"
|
|
|
|
#include "CLock.h"
|
2002-05-31 14:25:26 +00:00
|
|
|
#include "CMutex.h"
|
2001-10-14 18:29:43 +00:00
|
|
|
#include "CThread.h"
|
2002-06-02 18:49:35 +00:00
|
|
|
#include "XThread.h"
|
2003-03-12 22:34:07 +00:00
|
|
|
#include "CFunctionJob.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include "CLog.h"
|
2003-01-04 22:01:32 +00:00
|
|
|
#include "LogOutputters.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include "CString.h"
|
2003-01-04 22:01:32 +00:00
|
|
|
#include "CArch.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include <cstring>
|
2001-10-08 19:24:46 +00:00
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
#define DAEMON_RUNNING(running_)
|
2002-07-30 18:31:00 +00:00
|
|
|
#if WINDOWS_LIKE
|
2003-03-12 22:34:07 +00:00
|
|
|
#include "CMSWindowsScreen.h"
|
2002-07-30 18:31:00 +00:00
|
|
|
#include "CMSWindowsSecondaryScreen.h"
|
2003-03-21 19:34:08 +00:00
|
|
|
#include "CArchMiscWindows.h"
|
2003-03-12 22:34:07 +00:00
|
|
|
#include "CMSWindowsClientTaskBarReceiver.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_)
|
2002-07-30 18:31:00 +00:00
|
|
|
#elif UNIX_LIKE
|
|
|
|
#include "CXWindowsSecondaryScreen.h"
|
2003-03-12 22:34:07 +00:00
|
|
|
#include "CXWindowsClientTaskBarReceiver.h"
|
2002-07-30 18:31:00 +00:00
|
|
|
#endif
|
|
|
|
|
2002-06-08 21:48:00 +00:00
|
|
|
// platform dependent name of a daemon
|
2002-06-19 11:23:49 +00:00
|
|
|
#if WINDOWS_LIKE
|
2002-06-08 21:48:00 +00:00
|
|
|
#define DAEMON_NAME "Synergy Client"
|
2002-06-19 11:23:49 +00:00
|
|
|
#elif UNIX_LIKE
|
2002-08-11 22:43:07 +00:00
|
|
|
#define DAEMON_NAME "synergyc"
|
2002-06-08 21:48:00 +00:00
|
|
|
#endif
|
|
|
|
|
2002-06-04 11:06:26 +00:00
|
|
|
//
|
|
|
|
// program arguments
|
|
|
|
//
|
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
#define ARG CArgs::s_instance
|
2002-06-04 11:06: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),
|
|
|
|
m_logFilter(NULL)
|
|
|
|
{ s_instance = this; }
|
|
|
|
~CArgs() { s_instance = NULL; }
|
2002-06-04 11:06: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;
|
|
|
|
const char* m_logFilter;
|
|
|
|
CString m_name;
|
|
|
|
CNetworkAddress m_serverAddress;
|
|
|
|
};
|
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
|
|
|
|
//
|
|
|
|
|
2003-01-05 21:48:54 +00:00
|
|
|
//! Factory for creating secondary screens
|
|
|
|
/*!
|
|
|
|
Objects of this type create secondary screens appropriate for the
|
|
|
|
platform.
|
|
|
|
*/
|
2002-07-30 18:31:00 +00:00
|
|
|
class CSecondaryScreenFactory : public ISecondaryScreenFactory {
|
|
|
|
public:
|
|
|
|
CSecondaryScreenFactory() { }
|
|
|
|
virtual ~CSecondaryScreenFactory() { }
|
|
|
|
|
|
|
|
// ISecondaryScreenFactory overrides
|
|
|
|
virtual CSecondaryScreen*
|
|
|
|
create(IScreenReceiver*);
|
|
|
|
};
|
|
|
|
|
|
|
|
CSecondaryScreen*
|
|
|
|
CSecondaryScreenFactory::create(IScreenReceiver* receiver)
|
|
|
|
{
|
|
|
|
#if WINDOWS_LIKE
|
|
|
|
return new CMSWindowsSecondaryScreen(receiver);
|
|
|
|
#elif UNIX_LIKE
|
|
|
|
return new CXWindowsSecondaryScreen(receiver);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-12 22:34:07 +00:00
|
|
|
//! CQuitJob
|
|
|
|
/*!
|
|
|
|
A job that cancels a given thread.
|
|
|
|
*/
|
|
|
|
class CQuitJob : public IJob {
|
|
|
|
public:
|
|
|
|
CQuitJob(const CThread& thread);
|
|
|
|
~CQuitJob();
|
|
|
|
|
|
|
|
// IJob overrides
|
|
|
|
virtual void run();
|
|
|
|
|
|
|
|
private:
|
|
|
|
CThread m_thread;
|
|
|
|
};
|
|
|
|
|
|
|
|
CQuitJob::CQuitJob(const CThread& thread) :
|
|
|
|
m_thread(thread)
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
CQuitJob::~CQuitJob()
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CQuitJob::run()
|
|
|
|
{
|
|
|
|
m_thread.cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-05-31 14:25:26 +00:00
|
|
|
//
|
2002-06-08 21:48:00 +00:00
|
|
|
// platform independent main
|
2002-05-31 14:25:26 +00:00
|
|
|
//
|
|
|
|
|
2003-03-12 22:34:07 +00:00
|
|
|
static CClient* s_client = NULL;
|
|
|
|
static CClientTaskBarReceiver* s_taskBarReceiver = NULL;
|
2002-05-31 14:25:26 +00:00
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
int
|
2003-01-04 22:01:32 +00:00
|
|
|
realMain(void)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
2002-07-24 13:01:18 +00:00
|
|
|
int result = kExitSuccess;
|
|
|
|
do {
|
2002-08-11 11:49:36 +00:00
|
|
|
bool opened = false;
|
|
|
|
bool locked = true;
|
2002-06-08 21:48:00 +00:00
|
|
|
try {
|
2002-08-11 11:49:36 +00:00
|
|
|
// create client
|
2003-01-04 22:01:32 +00:00
|
|
|
s_client = new CClient(ARG->m_name);
|
|
|
|
s_client->setAddress(ARG->m_serverAddress);
|
2002-08-11 11:49:36 +00:00
|
|
|
s_client->setScreenFactory(new CSecondaryScreenFactory);
|
|
|
|
s_client->setSocketFactory(new CTCPSocketFactory);
|
|
|
|
s_client->setStreamFilterFactory(NULL);
|
|
|
|
|
|
|
|
// open client
|
2002-07-24 13:01:18 +00:00
|
|
|
try {
|
2003-03-12 22:34:07 +00:00
|
|
|
s_taskBarReceiver->setClient(s_client);
|
2002-08-11 11:49:36 +00:00
|
|
|
s_client->open();
|
|
|
|
opened = true;
|
2002-07-31 12:39:34 +00:00
|
|
|
|
2002-08-11 11:49:36 +00:00
|
|
|
// run client
|
2003-01-04 22:01:32 +00:00
|
|
|
DAEMON_RUNNING(true);
|
2002-08-11 11:49:36 +00:00
|
|
|
locked = false;
|
|
|
|
s_client->mainLoop();
|
|
|
|
locked = true;
|
2003-01-22 08:36:43 +00:00
|
|
|
DAEMON_RUNNING(false);
|
2002-08-11 11:49:36 +00:00
|
|
|
|
|
|
|
// get client status
|
|
|
|
if (s_client->wasRejected()) {
|
|
|
|
// try again later. we don't want to bother
|
|
|
|
// the server very often if it doesn't want us.
|
|
|
|
throw XScreenUnavailable(60.0);
|
2002-07-31 12:39:34 +00:00
|
|
|
}
|
2002-07-24 13:01:18 +00:00
|
|
|
|
|
|
|
// clean up
|
2002-08-11 11:49:36 +00:00
|
|
|
#define FINALLY do { \
|
2003-01-04 22:01:32 +00:00
|
|
|
if (!locked) { \
|
|
|
|
DAEMON_RUNNING(false); \
|
2003-01-22 08:36:43 +00:00
|
|
|
locked = true; \
|
2002-08-11 11:49:36 +00:00
|
|
|
} \
|
2003-03-12 22:34:07 +00:00
|
|
|
if (opened) { \
|
|
|
|
s_client->close(); \
|
2002-08-11 11:49:36 +00:00
|
|
|
} \
|
2003-03-12 22:34:07 +00:00
|
|
|
s_taskBarReceiver->setClient(NULL); \
|
2002-08-11 11:49:36 +00:00
|
|
|
delete s_client; \
|
|
|
|
s_client = NULL; \
|
|
|
|
} while (false)
|
|
|
|
FINALLY;
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
2002-08-11 11:49:36 +00:00
|
|
|
catch (XScreenUnavailable& e) {
|
|
|
|
// wait before retrying if we're going to retry
|
2003-01-04 22:01:32 +00:00
|
|
|
if (ARG->m_restartable) {
|
2003-07-12 17:57:31 +00:00
|
|
|
LOG((CLOG_DEBUG "waiting %.0f seconds to retry", e.getRetryTime()));
|
2003-01-04 22:01:32 +00:00
|
|
|
ARCH->sleep(e.getRetryTime());
|
2002-07-24 13:01:18 +00:00
|
|
|
}
|
2002-08-11 11:49:36 +00:00
|
|
|
else {
|
|
|
|
result = kExitFailed;
|
2002-07-24 13:01:18 +00:00
|
|
|
}
|
2002-08-11 11:49:36 +00:00
|
|
|
FINALLY;
|
|
|
|
}
|
|
|
|
catch (XThread&) {
|
|
|
|
FINALLY;
|
2002-07-24 13:01:18 +00:00
|
|
|
throw;
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
2002-08-11 11:49:36 +00:00
|
|
|
catch (...) {
|
|
|
|
// don't try to restart and fail
|
2003-01-04 22:01:32 +00:00
|
|
|
ARG->m_restartable = false;
|
|
|
|
result = kExitFailed;
|
2002-08-11 11:49:36 +00:00
|
|
|
FINALLY;
|
|
|
|
}
|
|
|
|
#undef FINALLY
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
2002-07-24 13:01:18 +00:00
|
|
|
catch (XBase& e) {
|
2002-10-15 21:29:44 +00:00
|
|
|
LOG((CLOG_CRIT "failed: %s", e.what()));
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
2002-07-24 13:01:18 +00:00
|
|
|
catch (XThread&) {
|
|
|
|
// terminated
|
2003-01-04 22:01:32 +00:00
|
|
|
ARG->m_restartable = false;
|
|
|
|
result = kExitTerminated;
|
2002-07-24 13:01:18 +00:00
|
|
|
}
|
2003-01-04 22:01:32 +00:00
|
|
|
} while (ARG->m_restartable);
|
2002-08-11 11:49:36 +00:00
|
|
|
|
2002-07-24 13:01:18 +00:00
|
|
|
return result;
|
2001-11-19 00:33:36 +00:00
|
|
|
}
|
|
|
|
|
2003-03-12 22:34:07 +00:00
|
|
|
static
|
|
|
|
void
|
2003-04-13 18:14:01 +00:00
|
|
|
realMainEntry(void* vresult)
|
2003-03-12 22:34:07 +00:00
|
|
|
{
|
2003-04-13 18:14:01 +00:00
|
|
|
*reinterpret_cast<int*>(vresult) = realMain();
|
2003-03-12 22:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int
|
|
|
|
runMainInThread(void)
|
|
|
|
{
|
2003-04-16 20:59:14 +00:00
|
|
|
int result = 0;
|
2003-04-13 18:14:01 +00:00
|
|
|
CThread appThread(new CFunctionJob(&realMainEntry, &result));
|
2003-03-12 22:34:07 +00:00
|
|
|
try {
|
|
|
|
#if WINDOWS_LIKE
|
|
|
|
MSG msg;
|
|
|
|
while (appThread.waitForEvent(-1.0) == CThread::kEvent) {
|
|
|
|
// check for a quit event
|
|
|
|
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
|
|
|
|
if (msg.message == WM_QUIT) {
|
|
|
|
CThread::getCurrentThread().cancel();
|
|
|
|
}
|
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessage(&msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
appThread.wait(-1.0);
|
|
|
|
#endif
|
2003-04-13 18:14:01 +00:00
|
|
|
return result;
|
2003-03-12 22:34:07 +00:00
|
|
|
}
|
|
|
|
catch (XThread&) {
|
|
|
|
appThread.cancel();
|
|
|
|
appThread.wait(-1.0);
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-05-31 14:25:26 +00:00
|
|
|
|
2002-06-04 11:06:26 +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-04 11:06:26 +00:00
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
void
|
|
|
|
version()
|
2002-06-04 11:06:26 +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-04 11:06:26 +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-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
void
|
|
|
|
help()
|
2002-06-04 11:06:26 +00:00
|
|
|
{
|
2002-10-15 21:29:44 +00:00
|
|
|
LOG((CLOG_PRINT
|
2002-06-04 11:06:26 +00:00
|
|
|
"Usage: %s"
|
2002-06-09 22:20:28 +00:00
|
|
|
" [--camp|--no-camp]"
|
2002-06-14 18:08:20 +00:00
|
|
|
" [--daemon|--no-daemon]"
|
2002-06-09 17:59:32 +00:00
|
|
|
" [--debug <level>]"
|
|
|
|
" [--name <screen-name>]"
|
2002-06-04 11:06:26 +00:00
|
|
|
" [--restart|--no-restart]"
|
2002-09-02 17:30:04 +00:00
|
|
|
" <server-address>\n"
|
2002-06-14 18:08:20 +00:00
|
|
|
"\n"
|
2002-06-04 11:06:26 +00:00
|
|
|
"Start the synergy mouse/keyboard sharing server.\n"
|
|
|
|
"\n"
|
|
|
|
" -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"
|
2002-06-14 18:08:20 +00:00
|
|
|
" -f, --no-daemon run the client in the foreground.\n"
|
|
|
|
"* --daemon run the client 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"
|
|
|
|
" ourself to the server.\n"
|
2002-06-04 11:06:26 +00:00
|
|
|
" -1, --no-restart do not try to restart the client if it fails for\n"
|
|
|
|
" some reason.\n"
|
2002-07-24 13:01:18 +00:00
|
|
|
"* --restart restart the client automatically if it fails.\n"
|
2002-06-04 11:06:26 +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"
|
2002-06-04 11:06:26 +00:00
|
|
|
"\n"
|
2002-06-09 16:53:25 +00:00
|
|
|
"The server address is of the form: [<hostname>][:<port>]. The hostname\n"
|
|
|
|
"must be the address or hostname of the server. The port overrides the\n"
|
|
|
|
"default port, %d.\n"
|
|
|
|
"\n"
|
2002-06-04 11:06:26 +00:00
|
|
|
"Where log messages go depends on the platform and whether or not the\n"
|
2002-06-14 18:08:20 +00:00
|
|
|
"client is running as a daemon.",
|
2003-01-04 22:01:32 +00:00
|
|
|
ARG->m_pname, kDefaultPort));
|
2002-06-04 11:06:26 +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-04 11:06:26 +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-04 11:06:26 +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-04 11:06:26 +00:00
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
assert(ARG->m_pname != NULL);
|
|
|
|
assert(argv != NULL);
|
|
|
|
assert(argc >= 1);
|
2002-06-04 11:06:26 +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-04 11:06:26 +00:00
|
|
|
// parse options
|
|
|
|
int i;
|
|
|
|
for (i = 1; i < argc; ++i) {
|
|
|
|
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-04 11:06:26 +00:00
|
|
|
}
|
|
|
|
|
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-09 22:20:28 +00:00
|
|
|
else if (isArg(i, argc, argv, NULL, "--camp")) {
|
2003-07-12 17:57:31 +00:00
|
|
|
// ignore -- included for backwards compatibility
|
2002-06-09 22:20:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (isArg(i, argc, argv, NULL, "--no-camp")) {
|
2003-07-12 17:57:31 +00:00
|
|
|
// ignore -- included for backwards compatibility
|
2002-06-09 22:20:28 +00:00
|
|
|
}
|
|
|
|
|
2002-06-14 18:08:20 +00:00
|
|
|
else if (isArg(i, argc, argv, "-f", "--no-daemon")) {
|
2002-06-04 11:06:26 +00:00
|
|
|
// not a daemon
|
2003-01-04 22:01:32 +00:00
|
|
|
ARG->m_daemon = false;
|
2002-06-04 11:06:26 +00:00
|
|
|
}
|
|
|
|
|
2002-06-14 18:08:20 +00:00
|
|
|
else if (isArg(i, argc, argv, NULL, "--daemon")) {
|
2002-06-04 11:06:26 +00:00
|
|
|
// daemonize
|
2003-01-04 22:01:32 +00:00
|
|
|
ARG->m_daemon = true;
|
2002-06-04 11:06:26 +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-04 11:06:26 +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-04 11:06:26 +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-04 11:06:26 +00:00
|
|
|
else if (isArg(i, argc, argv, "-h", "--help")) {
|
|
|
|
help();
|
2002-07-24 13:01:18 +00:00
|
|
|
bye(kExitSuccess);
|
2002-06-04 11:06:26 +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-04 11:06:26 +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-04 11:06:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
// this and remaining arguments are not options
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-02 17:30:04 +00:00
|
|
|
// exactly one non-option argument (server-address)
|
|
|
|
if (i == argc) {
|
2002-10-15 21:29:44 +00:00
|
|
|
LOG((CLOG_PRINT "%s: a server address or name is required" BYE,
|
2003-01-04 22:01:32 +00:00
|
|
|
ARG->m_pname, ARG->m_pname));
|
2002-09-02 17:30:04 +00:00
|
|
|
bye(kExitArgs);
|
|
|
|
}
|
|
|
|
if (i + 1 != 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-09-02 17:30:04 +00:00
|
|
|
bye(kExitArgs);
|
|
|
|
}
|
2002-06-09 16:53:25 +00:00
|
|
|
|
2002-09-02 17:30:04 +00:00
|
|
|
// save server address
|
|
|
|
try {
|
2003-01-04 22:01:32 +00:00
|
|
|
ARG->m_serverAddress = CNetworkAddress(argv[i], kDefaultPort);
|
2002-09-02 17:30:04 +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-09-02 17:30:04 +00:00
|
|
|
bye(kExitFailed);
|
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) {
|
2002-06-19 11:23:49 +00:00
|
|
|
#if WINDOWS_LIKE
|
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-04 11:06:26 +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-04 11:06:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
|
2002-05-31 14:25:26 +00:00
|
|
|
//
|
|
|
|
// platform dependent entry points
|
|
|
|
//
|
|
|
|
|
2002-06-19 11:23:49 +00:00
|
|
|
#if WINDOWS_LIKE
|
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
|
2003-01-04 22:01:32 +00:00
|
|
|
daemonStartup(int argc, const char** argv)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
2003-03-12 22:34:07 +00:00
|
|
|
CSystemLogger sysLogger(DAEMON_NAME);
|
|
|
|
|
|
|
|
// have to cancel this thread to quit
|
|
|
|
s_taskBarReceiver->setQuitJob(new CQuitJob(CThread::getCurrentThread()));
|
2002-06-08 21:48:00 +00:00
|
|
|
|
|
|
|
// catch errors that would normally exit
|
|
|
|
bye = &byeThrow;
|
|
|
|
|
|
|
|
// parse command line
|
|
|
|
parse(argc, argv);
|
|
|
|
|
2002-09-14 12:03:43 +00:00
|
|
|
// cannot run as backend if running as a service
|
2003-01-04 22:01:32 +00:00
|
|
|
ARG->m_backend = false;
|
2002-09-14 12:03:43 +00:00
|
|
|
|
2002-06-08 21:48:00 +00:00
|
|
|
// run as a service
|
2003-01-22 08:36:43 +00:00
|
|
|
return CArchMiscWindows::runDaemon(realMain);
|
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
|
2003-01-04 22:01:32 +00:00
|
|
|
daemonStartup95(int, const char**)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
2003-03-12 22:34:07 +00:00
|
|
|
CSystemLogger sysLogger(DAEMON_NAME);
|
|
|
|
return runMainInThread();
|
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
|
|
|
|
int
|
|
|
|
run(int argc, char** argv)
|
2001-11-19 00:33:36 +00:00
|
|
|
{
|
2002-06-17 15:44:45 +00:00
|
|
|
// 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.
|
2003-03-12 22:34:07 +00:00
|
|
|
if (argc <= 1 && !CArchMiscWindows::isWindows95Family()) {
|
2003-01-22 08:36:43 +00:00
|
|
|
try {
|
2003-03-12 22:34:07 +00:00
|
|
|
return ARCH->daemonize(DAEMON_NAME, &daemonStartup);
|
2003-01-22 08:36:43 +00:00
|
|
|
}
|
|
|
|
catch (XArchDaemon& e) {
|
|
|
|
LOG((CLOG_CRIT "failed to start as a service: %s" BYE, e.what().c_str(), ARG->m_pname));
|
2002-06-17 15:44:45 +00:00
|
|
|
}
|
2003-03-12 22:34:07 +00:00
|
|
|
return kExitFailed;
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
2002-06-04 11:06:26 +00:00
|
|
|
|
2002-06-17 15:44:45 +00:00
|
|
|
// parse command line
|
2003-03-12 22:34:07 +00:00
|
|
|
parse(argc, argv);
|
2002-06-08 21:48:00 +00:00
|
|
|
|
|
|
|
// daemonize if requested
|
2003-01-04 22:01:32 +00:00
|
|
|
if (ARG->m_daemon) {
|
2002-06-17 15:44:45 +00:00
|
|
|
// start as a daemon
|
2003-01-04 22:01:32 +00:00
|
|
|
if (CArchMiscWindows::isWindows95Family()) {
|
|
|
|
try {
|
2003-03-12 22:34:07 +00:00
|
|
|
return ARCH->daemonize(DAEMON_NAME, &daemonStartup95);
|
2003-01-04 22:01:32 +00:00
|
|
|
}
|
2003-01-22 08:36:43 +00:00
|
|
|
catch (XArchDaemon& e) {
|
|
|
|
LOG((CLOG_CRIT "failed to start as a service: %s" BYE, e.what().c_str(), ARG->m_pname));
|
2002-06-17 15:44:45 +00:00
|
|
|
}
|
2003-03-12 22:34:07 +00:00
|
|
|
return kExitFailed;
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
else {
|
2002-06-17 15:44:45 +00:00
|
|
|
// cannot start a service from the command line so just
|
|
|
|
// run normally (except with log messages redirected).
|
2003-03-12 22:34:07 +00:00
|
|
|
CSystemLogger sysLogger(DAEMON_NAME);
|
|
|
|
return runMainInThread();
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2002-06-14 18:08:20 +00:00
|
|
|
// run
|
2003-03-12 22:34:07 +00:00
|
|
|
return runMainInThread();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int WINAPI
|
|
|
|
WinMain(HINSTANCE instance, HINSTANCE, LPSTR, int)
|
|
|
|
{
|
|
|
|
CArch arch(instance);
|
|
|
|
CLOG;
|
|
|
|
CArgs args;
|
|
|
|
|
|
|
|
// save instance
|
|
|
|
CMSWindowsScreen::init(instance);
|
|
|
|
|
|
|
|
// get program name
|
|
|
|
ARG->m_pname = ARCH->getBasename(__argv[0]);
|
|
|
|
|
|
|
|
// send PRINT and FATAL output to a message box
|
|
|
|
CLOG->insert(new CMessageBoxOutputter);
|
|
|
|
|
2003-06-02 20:06:03 +00:00
|
|
|
// save log messages
|
|
|
|
CBufferedLogOutputter logBuffer(1000);
|
|
|
|
CLOG->insert(&logBuffer, true);
|
|
|
|
|
2003-03-12 22:34:07 +00:00
|
|
|
// make the task bar receiver. the user can control this app
|
|
|
|
// through the task bar.
|
2003-06-02 20:06:03 +00:00
|
|
|
s_taskBarReceiver = new CMSWindowsClientTaskBarReceiver(instance,
|
|
|
|
&logBuffer);
|
2003-03-12 22:34:07 +00:00
|
|
|
s_taskBarReceiver->setQuitJob(new CQuitJob(CThread::getCurrentThread()));
|
|
|
|
|
|
|
|
int result;
|
|
|
|
try {
|
|
|
|
// run in foreground or as a daemon
|
|
|
|
result = run(__argc, __argv);
|
2002-06-02 18:49:35 +00:00
|
|
|
}
|
2003-03-12 22:34:07 +00:00
|
|
|
catch (...) {
|
|
|
|
// note that we don't rethrow thread cancellation. we'll
|
|
|
|
// be exiting soon so it doesn't matter. what we'd like
|
|
|
|
// is for everything after this try/catch to be in a
|
|
|
|
// finally block.
|
|
|
|
result = kExitFailed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// done with task bar receiver
|
|
|
|
delete s_taskBarReceiver;
|
2002-06-08 21:48:00 +00:00
|
|
|
|
2003-06-02 20:06:03 +00:00
|
|
|
// done with log buffer
|
|
|
|
CLOG->remove(&logBuffer);
|
|
|
|
|
2002-09-14 12:03:43 +00:00
|
|
|
// let user examine any messages if we're running as a backend
|
|
|
|
// by putting up a dialog box before exiting.
|
2003-01-04 22:01:32 +00:00
|
|
|
if (ARG->m_backend && s_hasImportantLogMessages) {
|
2002-09-14 12:03:43 +00:00
|
|
|
char msg[1024];
|
|
|
|
msg[0] = '\0';
|
|
|
|
LoadString(instance, IDS_FAILED, msg, sizeof(msg) / sizeof(msg[0]));
|
2003-01-04 22:01:32 +00:00
|
|
|
MessageBox(NULL, msg, ARG->m_pname, MB_OK | MB_ICONWARNING);
|
2002-09-14 12:03:43 +00:00
|
|
|
}
|
|
|
|
|
2003-02-16 19:49:44 +00:00
|
|
|
delete CLOG;
|
2002-06-08 21:48:00 +00:00
|
|
|
return result;
|
2001-11-19 00:33:36 +00:00
|
|
|
}
|
|
|
|
|
2002-06-19 11:23:49 +00:00
|
|
|
#elif UNIX_LIKE
|
2001-11-19 00:33:36 +00:00
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
int
|
2003-01-04 22:01:32 +00:00
|
|
|
daemonStartup(int, const char**)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
2003-03-12 22:34:07 +00:00
|
|
|
CSystemLogger sysLogger(DAEMON_NAME);
|
2003-01-04 22:01:32 +00:00
|
|
|
return realMain();
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
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)
|
2002-06-04 11:06:26 +00:00
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
CArch arch;
|
|
|
|
CLOG;
|
|
|
|
CArgs args;
|
2002-06-04 12:26:23 +00:00
|
|
|
|
2002-06-04 11:06:26 +00:00
|
|
|
// get program name
|
2003-01-04 22:01:32 +00:00
|
|
|
ARG->m_pname = ARCH->getBasename(argv[0]);
|
2002-06-09 16:53:25 +00:00
|
|
|
|
2003-03-12 22:34:07 +00:00
|
|
|
// make the task bar receiver. the user can control this app
|
|
|
|
// through the task bar.
|
|
|
|
s_taskBarReceiver = new CXWindowsClientTaskBarReceiver;
|
|
|
|
s_taskBarReceiver->setQuitJob(new CQuitJob(CThread::getCurrentThread()));
|
|
|
|
|
2002-06-04 11:06:26 +00:00
|
|
|
// parse command line
|
2003-03-12 22:34:07 +00:00
|
|
|
parse(argc, argv);
|
2002-06-04 11:06:26 +00:00
|
|
|
|
|
|
|
// daemonize if requested
|
2002-06-08 21:48:00 +00:00
|
|
|
int result;
|
2003-01-04 22:01:32 +00:00
|
|
|
if (ARG->m_daemon) {
|
|
|
|
try {
|
|
|
|
result = ARCH->daemonize(DAEMON_NAME, &daemonStartup);
|
|
|
|
}
|
|
|
|
catch (XArchDaemon&) {
|
2002-10-15 21:29:44 +00:00
|
|
|
LOG((CLOG_CRIT "failed to daemonize"));
|
2003-01-04 22:01:32 +00:00
|
|
|
result = kExitFailed;
|
2002-06-04 12:26:23 +00:00
|
|
|
}
|
2002-06-04 11:06:26 +00:00
|
|
|
}
|
2002-06-08 21:48:00 +00:00
|
|
|
else {
|
2003-01-04 22:01:32 +00:00
|
|
|
result = realMain();
|
2002-06-02 18:49:35 +00:00
|
|
|
}
|
2002-06-08 21:48:00 +00:00
|
|
|
|
2003-03-12 22:34:07 +00:00
|
|
|
// done with task bar receiver
|
|
|
|
delete s_taskBarReceiver;
|
|
|
|
|
2002-06-08 21:48:00 +00:00
|
|
|
return result;
|
2001-10-08 19:24:46 +00:00
|
|
|
}
|
2001-11-19 00:33:36 +00:00
|
|
|
|
2002-06-04 11:06:26 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
#error no main() for platform
|
|
|
|
|
2001-11-19 00:33:36 +00:00
|
|
|
#endif
|