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 "CServer.h"
|
2002-05-31 14:34:16 +00:00
|
|
|
#include "CConfig.h"
|
2002-07-30 18:31:00 +00:00
|
|
|
#include "IPrimaryScreenFactory.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include "CPlatform.h"
|
|
|
|
#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 "CNetwork.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 "CLock.h"
|
2002-05-31 14:25:26 +00:00
|
|
|
#include "CMutex.h"
|
|
|
|
#include "CThread.h"
|
2002-06-02 18:49:35 +00:00
|
|
|
#include "XThread.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include "CLog.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
|
|
|
|
2002-07-30 18:31:00 +00:00
|
|
|
#if WINDOWS_LIKE
|
|
|
|
#include "CMSWindowsPrimaryScreen.h"
|
2002-08-11 11:49:36 +00:00
|
|
|
#include "resource.h"
|
2002-07-30 18:31:00 +00:00
|
|
|
#elif UNIX_LIKE
|
|
|
|
#include "CXWindowsPrimaryScreen.h"
|
|
|
|
#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 Server"
|
2002-06-19 11:23:49 +00:00
|
|
|
#elif UNIX_LIKE
|
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
|
2002-06-19 11:23:49 +00:00
|
|
|
#if WINDOWS_LIKE
|
2002-08-11 22:43:07 +00:00
|
|
|
#define USR_CONFIG_NAME "synergy.sgc"
|
|
|
|
#define SYS_CONFIG_NAME "synergy.sgc"
|
2002-06-19 11:23:49 +00:00
|
|
|
#elif UNIX_LIKE
|
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
|
|
|
|
|
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
|
|
|
//
|
|
|
|
|
2002-06-03 18:53:18 +00:00
|
|
|
static const char* pname = NULL;
|
2002-08-11 11:49:36 +00:00
|
|
|
static bool s_backend = false;
|
2002-06-03 18:53:18 +00:00
|
|
|
static bool s_restartable = true;
|
|
|
|
static bool s_daemon = true;
|
|
|
|
static const char* s_configFile = NULL;
|
|
|
|
static const char* s_logFilter = NULL;
|
2002-06-09 17:59:32 +00:00
|
|
|
static CString s_name;
|
2002-06-09 16:53:25 +00:00
|
|
|
static CNetworkAddress s_synergyAddress;
|
|
|
|
static CNetworkAddress s_httpAddress;
|
2002-06-03 18:53:18 +00:00
|
|
|
static CConfig s_config;
|
2002-05-31 17:32:26 +00:00
|
|
|
|
2002-05-31 14:25:26 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// logging thread safety
|
|
|
|
//
|
|
|
|
|
|
|
|
static CMutex* s_logMutex = NULL;
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
void
|
2002-06-17 13:31:21 +00:00
|
|
|
logLock(bool lock)
|
2002-05-31 14:25:26 +00:00
|
|
|
{
|
|
|
|
assert(s_logMutex != NULL);
|
|
|
|
|
|
|
|
if (lock) {
|
|
|
|
s_logMutex->lock();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
s_logMutex->unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-30 18:31:00 +00:00
|
|
|
//
|
|
|
|
// platform dependent factories
|
|
|
|
//
|
|
|
|
|
|
|
|
class CPrimaryScreenFactory : public IPrimaryScreenFactory {
|
|
|
|
public:
|
|
|
|
CPrimaryScreenFactory() { }
|
|
|
|
virtual ~CPrimaryScreenFactory() { }
|
|
|
|
|
|
|
|
// IPrimaryScreenFactory overrides
|
|
|
|
virtual CPrimaryScreen*
|
|
|
|
create(IScreenReceiver*, IPrimaryScreenReceiver*);
|
|
|
|
};
|
|
|
|
|
|
|
|
CPrimaryScreen*
|
|
|
|
CPrimaryScreenFactory::create(IScreenReceiver* receiver,
|
|
|
|
IPrimaryScreenReceiver* primaryReceiver)
|
|
|
|
{
|
|
|
|
#if WINDOWS_LIKE
|
|
|
|
return new CMSWindowsPrimaryScreen(receiver, primaryReceiver);
|
|
|
|
#elif UNIX_LIKE
|
|
|
|
return new CXWindowsPrimaryScreen(receiver, primaryReceiver);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
//
|
2001-10-08 19:24:46 +00:00
|
|
|
|
2002-06-08 21:48:00 +00:00
|
|
|
static CServer* s_server = NULL;
|
2002-05-31 14:25:26 +00:00
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
int
|
2002-06-17 13:31:21 +00:00
|
|
|
realMain(CMutex* mutex)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
2002-07-24 13:01:18 +00:00
|
|
|
// caller should have mutex locked on entry
|
2002-05-31 14:25:26 +00:00
|
|
|
|
2002-08-11 11:49:36 +00:00
|
|
|
// initialize threading library
|
|
|
|
CThread::init();
|
|
|
|
|
|
|
|
// make logging thread safe
|
|
|
|
CMutex logMutex;
|
|
|
|
s_logMutex = &logMutex;
|
|
|
|
CLog::setLock(&logLock);
|
|
|
|
|
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
|
|
|
// if configuration has no screens then add this system
|
|
|
|
// as the default
|
|
|
|
if (s_config.begin() == s_config.end()) {
|
|
|
|
s_config.addScreen(s_name);
|
|
|
|
}
|
2002-06-09 16:53:25 +00:00
|
|
|
|
2002-08-11 11:49:36 +00:00
|
|
|
// set the contact address, if provided, in the config.
|
|
|
|
// otherwise, if the config doesn't have an address, use
|
|
|
|
// the default.
|
|
|
|
if (s_synergyAddress.isValid()) {
|
|
|
|
s_config.setSynergyAddress(s_synergyAddress);
|
|
|
|
}
|
|
|
|
else if (!s_config.getSynergyAddress().isValid()) {
|
|
|
|
s_config.setSynergyAddress(CNetworkAddress(kDefaultPort));
|
|
|
|
}
|
2002-07-24 13:01:18 +00:00
|
|
|
|
2002-08-11 11:49:36 +00:00
|
|
|
// set HTTP address if provided
|
|
|
|
if (s_httpAddress.isValid()) {
|
|
|
|
s_config.setHTTPAddress(s_httpAddress);
|
|
|
|
}
|
2002-07-24 13:01:18 +00:00
|
|
|
|
2002-08-11 11:49:36 +00:00
|
|
|
// create server
|
|
|
|
s_server = new CServer(s_name);
|
|
|
|
s_server->setConfig(s_config);
|
|
|
|
s_server->setScreenFactory(new CPrimaryScreenFactory);
|
|
|
|
s_server->setSocketFactory(new CTCPSocketFactory);
|
|
|
|
s_server->setStreamFilterFactory(NULL);
|
2002-07-24 13:01:18 +00:00
|
|
|
|
2002-08-11 11:49:36 +00:00
|
|
|
// open server
|
|
|
|
try {
|
|
|
|
s_server->open();
|
|
|
|
opened = true;
|
2002-07-31 12:39:34 +00:00
|
|
|
|
2002-08-11 11:49:36 +00:00
|
|
|
// run server (unlocked)
|
|
|
|
if (mutex != NULL) {
|
|
|
|
mutex->unlock();
|
2002-07-31 12:39:34 +00:00
|
|
|
}
|
2002-08-11 11:49:36 +00:00
|
|
|
locked = false;
|
|
|
|
s_server->mainLoop();
|
|
|
|
locked = true;
|
2002-07-24 13:01:18 +00:00
|
|
|
|
|
|
|
// clean up
|
2002-08-11 11:49:36 +00:00
|
|
|
#define FINALLY do { \
|
|
|
|
if (!locked && mutex != NULL) { \
|
|
|
|
mutex->lock(); \
|
|
|
|
} \
|
|
|
|
if (s_server != NULL) { \
|
|
|
|
if (opened) { \
|
|
|
|
s_server->close(); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
delete s_server; \
|
|
|
|
s_server = 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
|
|
|
|
if (s_restartable) {
|
|
|
|
CThread::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-07-13 22:00:38 +00:00
|
|
|
}
|
2002-08-11 11:49:36 +00:00
|
|
|
catch (...) {
|
|
|
|
// don't try to restart and fail
|
|
|
|
s_restartable = false;
|
|
|
|
result = kExitFailed;
|
|
|
|
FINALLY;
|
|
|
|
}
|
|
|
|
#undef FINALLY
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
2002-07-24 13:01:18 +00:00
|
|
|
catch (XBase& e) {
|
|
|
|
log((CLOG_CRIT "failed: %s", e.what()));
|
|
|
|
}
|
|
|
|
catch (XThread&) {
|
|
|
|
// terminated
|
2002-08-11 11:49:36 +00:00
|
|
|
s_restartable = false;
|
|
|
|
result = kExitTerminated;
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
CLog::setLock(NULL);
|
|
|
|
s_logMutex = NULL;
|
|
|
|
throw;
|
2002-07-24 13:01:18 +00:00
|
|
|
}
|
|
|
|
} while (s_restartable);
|
2002-06-08 21:48:00 +00:00
|
|
|
|
2002-08-11 11:49:36 +00:00
|
|
|
// clean up
|
|
|
|
CLog::setLock(NULL);
|
|
|
|
s_logMutex = NULL;
|
|
|
|
|
2002-07-24 13:01:18 +00:00
|
|
|
return result;
|
2001-11-19 00:33:36 +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
|
|
|
{
|
|
|
|
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",
|
|
|
|
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
|
|
|
{
|
2002-06-19 11:23:49 +00:00
|
|
|
#if WINDOWS_LIKE
|
2002-06-14 18:08:20 +00:00
|
|
|
|
|
|
|
# define PLATFORM_ARGS \
|
2002-09-02 17:30:04 +00:00
|
|
|
" {--daemon|--no-daemon}"
|
|
|
|
# define PLATFORM_DESC
|
2002-06-14 18:08:20 +00:00
|
|
|
# define PLATFORM_EXTRA \
|
|
|
|
"At least one command line argument is required. If you don't otherwise\n" \
|
|
|
|
"need an argument use `--daemon'.\n" \
|
|
|
|
"\n"
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
# define PLATFORM_ARGS \
|
|
|
|
" [--daemon|--no-daemon]"
|
|
|
|
# define PLATFORM_DESC
|
|
|
|
# define PLATFORM_EXTRA
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2002-06-08 21:48:00 +00:00
|
|
|
CPlatform platform;
|
|
|
|
|
2002-06-03 18:53:18 +00:00
|
|
|
log((CLOG_PRINT
|
|
|
|
"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>]"
|
|
|
|
" [--name <screen-name>]"
|
2002-06-03 18:53:18 +00:00
|
|
|
" [--restart|--no-restart]\n"
|
2002-06-14 18:08:20 +00:00
|
|
|
PLATFORM_ARGS
|
2002-06-08 21:48:00 +00:00
|
|
|
"\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"
|
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.",
|
2002-06-08 21:48:00 +00:00
|
|
|
pname,
|
2002-06-09 16:53:25 +00:00
|
|
|
kDefaultPort,
|
2002-06-08 21:48:00 +00:00
|
|
|
platform.addPathComponent(
|
|
|
|
platform.getUserDirectory(),
|
2002-08-11 22:43:07 +00:00
|
|
|
USR_CONFIG_NAME).c_str(),
|
2002-06-08 21:48:00 +00:00
|
|
|
platform.addPathComponent(
|
|
|
|
platform.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
|
2002-06-17 13:31:21 +00:00
|
|
|
isArg(int argi, int argc, const char** argv,
|
|
|
|
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-06-08 21:48:00 +00:00
|
|
|
log((CLOG_PRINT "%s: missing arguments for `%s'" BYE,
|
|
|
|
pname, argv[argi], 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
|
2002-06-17 13:31:21 +00:00
|
|
|
parse(int argc, const char** argv)
|
2002-06-03 18:53:18 +00:00
|
|
|
{
|
|
|
|
assert(pname != NULL);
|
|
|
|
assert(argv != NULL);
|
|
|
|
assert(argc >= 1);
|
|
|
|
|
2002-06-09 17:59:32 +00:00
|
|
|
// set defaults
|
|
|
|
char hostname[256];
|
|
|
|
if (CNetwork::gethostname(hostname, sizeof(hostname)) != CNetwork::Error) {
|
|
|
|
s_name = hostname;
|
|
|
|
}
|
|
|
|
|
2002-06-03 18:53:18 +00:00
|
|
|
// parse options
|
|
|
|
int i;
|
|
|
|
for (i = 1; i < argc; ++i) {
|
|
|
|
if (isArg(i, argc, argv, "-d", "--debug", 1)) {
|
|
|
|
// change logging level
|
|
|
|
s_logFilter = argv[++i];
|
|
|
|
}
|
|
|
|
|
2002-06-09 16:53:25 +00:00
|
|
|
else if (isArg(i, argc, argv, "-a", "--address", 1)) {
|
|
|
|
// save listen address
|
|
|
|
try {
|
|
|
|
s_synergyAddress = CNetworkAddress(argv[i + 1], kDefaultPort);
|
|
|
|
}
|
2002-07-25 17:52:40 +00:00
|
|
|
catch (XSocketAddress& e) {
|
2002-07-25 17:58:01 +00:00
|
|
|
log((CLOG_PRINT "%s: %s" BYE,
|
|
|
|
pname, e.what(), pname));
|
2002-07-24 13:01:18 +00:00
|
|
|
bye(kExitArgs);
|
2002-06-09 16:53:25 +00:00
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (isArg(i, argc, argv, NULL, "--http", 1)) {
|
|
|
|
// save listen address
|
|
|
|
try {
|
|
|
|
s_httpAddress = CNetworkAddress(argv[i + 1], kDefaultPort + 1);
|
|
|
|
}
|
2002-07-25 17:52:40 +00:00
|
|
|
catch (XSocketAddress& e) {
|
2002-07-25 17:58:01 +00:00
|
|
|
log((CLOG_PRINT "%s: %s" BYE,
|
|
|
|
pname, e.what(), 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
|
|
|
|
s_name = argv[++i];
|
|
|
|
}
|
|
|
|
|
2002-06-03 18:53:18 +00:00
|
|
|
else if (isArg(i, argc, argv, "-c", "--config", 1)) {
|
|
|
|
// save configuration file path
|
|
|
|
s_configFile = argv[++i];
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
s_daemon = false;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
s_daemon = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (isArg(i, argc, argv, "-1", "--no-restart")) {
|
|
|
|
// don't try to restart
|
|
|
|
s_restartable = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (isArg(i, argc, argv, NULL, "--restart")) {
|
|
|
|
// try to restart
|
|
|
|
s_restartable = true;
|
|
|
|
}
|
|
|
|
|
2002-08-11 11:49:36 +00:00
|
|
|
else if (isArg(i, argc, argv, "-z", NULL)) {
|
|
|
|
s_backend = true;
|
|
|
|
}
|
|
|
|
|
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-06-08 21:48:00 +00:00
|
|
|
log((CLOG_PRINT "%s: unrecognized option `%s'" BYE,
|
|
|
|
pname, argv[i], 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-06-08 21:48:00 +00:00
|
|
|
log((CLOG_PRINT "%s: unrecognized option `%s'" BYE,
|
|
|
|
pname, argv[i], 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.
|
|
|
|
if (s_daemon && s_logFilter == NULL) {
|
2002-06-19 11:23:49 +00:00
|
|
|
#if WINDOWS_LIKE
|
2002-06-08 21:48:00 +00:00
|
|
|
if (CPlatform::isWindows95Family()) {
|
|
|
|
// windows 95 has no place for logging so avoid showing
|
|
|
|
// the log console window.
|
|
|
|
s_logFilter = "FATAL";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
s_logFilter = "NOTE";
|
|
|
|
}
|
2002-06-03 18:53:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// set log filter
|
|
|
|
if (!CLog::setFilter(s_logFilter)) {
|
2002-06-08 21:48:00 +00:00
|
|
|
log((CLOG_PRINT "%s: unrecognized log level `%s'" BYE,
|
|
|
|
pname, s_logFilter, 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
|
2002-06-17 13:31:21 +00:00
|
|
|
loadConfig(const char* pathname, bool require)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
|
|
|
assert(pathname != NULL);
|
|
|
|
|
|
|
|
try {
|
|
|
|
// load configuration
|
|
|
|
log((CLOG_DEBUG "opening configuration \"%s\"", pathname));
|
|
|
|
std::ifstream configStream(pathname);
|
|
|
|
if (!configStream) {
|
|
|
|
throw XConfigRead("cannot open configuration");
|
|
|
|
}
|
|
|
|
configStream >> s_config;
|
|
|
|
log((CLOG_DEBUG "configuration read successfully"));
|
|
|
|
return true;
|
2002-06-03 18:53:18 +00:00
|
|
|
}
|
2002-06-08 23:24:40 +00:00
|
|
|
catch (XConfigRead& e) {
|
2002-06-08 21:48:00 +00:00
|
|
|
if (require) {
|
2002-06-08 23:24:40 +00:00
|
|
|
log((CLOG_PRINT "%s: cannot read configuration '%s': %s",
|
|
|
|
pname, pathname, e.what()));
|
2002-07-24 13:01:18 +00:00
|
|
|
bye(kExitConfig);
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
else {
|
2002-06-08 23:24:40 +00:00
|
|
|
log((CLOG_DEBUG "cannot read configuration \"%s\": %s",
|
|
|
|
pathname, 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
|
|
|
{
|
|
|
|
// load the config file, if specified
|
2002-06-03 18:53:18 +00:00
|
|
|
if (s_configFile != NULL) {
|
|
|
|
// require the user specified file to load correctly
|
|
|
|
loadConfig(s_configFile, true);
|
|
|
|
}
|
|
|
|
|
2002-06-08 21:48:00 +00:00
|
|
|
// load the default configuration if no explicit file given
|
|
|
|
else {
|
|
|
|
// get the user's home directory. use the effective user id
|
|
|
|
// so a user can't get a setuid root program to load his file.
|
|
|
|
CPlatform platform;
|
|
|
|
bool loaded = false;
|
|
|
|
CString path = platform.getUserDirectory();
|
|
|
|
if (!path.empty()) {
|
|
|
|
// complete path
|
2002-08-11 22:43:07 +00:00
|
|
|
path = platform.addPathComponent(path, USR_CONFIG_NAME);
|
2002-06-08 21:48:00 +00:00
|
|
|
|
|
|
|
// now try loading the user's configuration
|
|
|
|
loaded = loadConfig(path.c_str(), false);
|
|
|
|
}
|
|
|
|
if (!loaded) {
|
|
|
|
// try the system-wide config file
|
|
|
|
path = platform.getSystemDirectory();
|
|
|
|
if (!path.empty()) {
|
2002-08-11 22:43:07 +00:00
|
|
|
path = platform.addPathComponent(path, SYS_CONFIG_NAME);
|
2002-06-08 21:48:00 +00:00
|
|
|
loadConfig(path.c_str(), false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-06-03 18:53:18 +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
|
|
|
|
|
|
|
#include "CMSWindowsScreen.h"
|
2002-06-08 21:48:00 +00:00
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
bool
|
2002-06-17 13:31:21 +00:00
|
|
|
logMessageBox(int priority, const char* msg)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
2002-09-02 17:30:04 +00:00
|
|
|
if (priority <= (s_backend ? CLog::kERROR : CLog::kFATAL)) {
|
2002-06-08 21:48:00 +00:00
|
|
|
MessageBox(NULL, msg, pname, MB_OK | MB_ICONWARNING);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
2002-09-02 17:30:04 +00:00
|
|
|
return s_backend;
|
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
|
|
|
{
|
|
|
|
throw CWin32Platform::CDaemonFailed(x);
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
void
|
|
|
|
daemonStop(void)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
2002-07-30 15:17:44 +00:00
|
|
|
s_server->exitMainLoop();
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
int
|
2002-06-17 13:31:21 +00:00
|
|
|
daemonStartup(IPlatform* iplatform, int argc, const char** argv)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
|
|
|
// get platform pointer
|
|
|
|
CWin32Platform* platform = static_cast<CWin32Platform*>(iplatform);
|
|
|
|
|
|
|
|
// catch errors that would normally exit
|
|
|
|
bye = &byeThrow;
|
|
|
|
|
|
|
|
// parse command line
|
|
|
|
parse(argc, argv);
|
|
|
|
|
|
|
|
// load configuration
|
|
|
|
loadConfig();
|
|
|
|
|
|
|
|
// run as a service
|
|
|
|
return platform->runDaemon(realMain, daemonStop);
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
2002-06-17 15:44:45 +00:00
|
|
|
int
|
|
|
|
daemonStartup95(IPlatform*, int, const char**)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
2002-07-24 13:01:18 +00:00
|
|
|
return realMain(NULL);
|
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 WINAPI
|
2002-06-17 13:31:21 +00:00
|
|
|
WinMain(HINSTANCE instance, HINSTANCE, LPSTR, int)
|
2001-11-19 00:33:36 +00:00
|
|
|
{
|
2002-06-04 12:26:23 +00:00
|
|
|
CPlatform platform;
|
|
|
|
|
|
|
|
// save instance
|
2001-11-19 00:33:36 +00:00
|
|
|
CMSWindowsScreen::init(instance);
|
|
|
|
|
2002-06-03 18:53:18 +00:00
|
|
|
// get program name
|
2002-06-08 21:48:00 +00:00
|
|
|
pname = platform.getBasename(__argv[0]);
|
|
|
|
|
2002-06-09 16:53:25 +00:00
|
|
|
// initialize network library
|
|
|
|
CNetwork::init();
|
|
|
|
|
2002-06-14 18:08:20 +00:00
|
|
|
// send PRINT and FATAL output to a message box
|
|
|
|
CLog::setOutputter(&logMessageBox);
|
|
|
|
|
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.
|
|
|
|
if (__argc <= 1 && !CWin32Platform::isWindows95Family()) {
|
|
|
|
int result = platform.daemonize(DAEMON_NAME, &daemonStartup);
|
|
|
|
if (result == -1) {
|
|
|
|
log((CLOG_CRIT "failed to start as a service" BYE, pname));
|
2002-07-24 13:01:18 +00:00
|
|
|
return kExitFailed;
|
2002-06-17 15:44:45 +00:00
|
|
|
}
|
|
|
|
return result;
|
2002-06-03 18:53:18 +00:00
|
|
|
}
|
|
|
|
|
2002-06-17 15:44:45 +00:00
|
|
|
// parse command line
|
|
|
|
parse(__argc, const_cast<const char**>(__argv));
|
2001-11-19 00:33:36 +00:00
|
|
|
|
2002-06-08 21:48:00 +00:00
|
|
|
// load configuration
|
|
|
|
loadConfig();
|
|
|
|
|
|
|
|
// daemonize if requested
|
|
|
|
int result;
|
|
|
|
if (s_daemon) {
|
2002-06-17 15:44:45 +00:00
|
|
|
// redirect log messages
|
|
|
|
platform.installDaemonLogger(DAEMON_NAME);
|
|
|
|
|
|
|
|
// start as a daemon
|
2002-06-08 21:48:00 +00:00
|
|
|
if (CWin32Platform::isWindows95Family()) {
|
2002-06-17 15:44:45 +00:00
|
|
|
result = platform.daemonize(DAEMON_NAME, &daemonStartup95);
|
|
|
|
if (result == -1) {
|
|
|
|
log((CLOG_CRIT "failed to start as a service" BYE, pname));
|
2002-08-11 11:49:36 +00:00
|
|
|
result = kExitFailed;
|
2002-06-17 15:44:45 +00:00
|
|
|
}
|
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).
|
2002-07-24 13:01:18 +00:00
|
|
|
result = realMain(NULL);
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
2002-06-02 18:49:35 +00:00
|
|
|
}
|
2002-06-08 21:48:00 +00:00
|
|
|
else {
|
2002-06-14 18:08:20 +00:00
|
|
|
// run
|
2002-07-24 13:01:18 +00:00
|
|
|
result = realMain(NULL);
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
2002-06-21 17:55:47 +00:00
|
|
|
CNetwork::cleanup();
|
|
|
|
|
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
|
2002-06-17 13:31:21 +00:00
|
|
|
daemonStartup(IPlatform*, int, const char**)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
2002-07-24 13:01:18 +00:00
|
|
|
return realMain(NULL);
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
2001-10-08 19:24:46 +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
|
|
|
{
|
2002-06-04 12:26:23 +00:00
|
|
|
CPlatform platform;
|
|
|
|
|
2002-06-03 18:53:18 +00:00
|
|
|
// get program name
|
2002-06-04 12:26:23 +00:00
|
|
|
pname = platform.getBasename(argv[0]);
|
2001-11-19 00:33:36 +00:00
|
|
|
|
2002-06-09 16:53:25 +00:00
|
|
|
// initialize network library
|
|
|
|
CNetwork::init();
|
|
|
|
|
2002-06-03 18:53:18 +00:00
|
|
|
// parse command line
|
2002-06-08 21:48:00 +00:00
|
|
|
parse(argc, const_cast<const char**>(argv));
|
2002-06-03 18:53:18 +00:00
|
|
|
|
2002-06-08 21:48:00 +00:00
|
|
|
// load configuration
|
|
|
|
loadConfig();
|
2002-06-03 18:53:18 +00:00
|
|
|
|
|
|
|
// daemonize if requested
|
2002-06-08 21:48:00 +00:00
|
|
|
int result;
|
2002-06-03 18:53:18 +00:00
|
|
|
if (s_daemon) {
|
2002-06-08 21:48:00 +00:00
|
|
|
result = platform.daemonize(DAEMON_NAME, &daemonStartup);
|
|
|
|
if (result == -1) {
|
2002-06-04 12:26:23 +00:00
|
|
|
log((CLOG_CRIT "failed to daemonize"));
|
2002-07-24 13:01:18 +00:00
|
|
|
return kExitFailed;
|
2002-06-04 12:26:23 +00:00
|
|
|
}
|
2001-11-19 00:33:36 +00:00
|
|
|
}
|
2002-06-08 21:48:00 +00:00
|
|
|
else {
|
2002-07-24 13:01:18 +00:00
|
|
|
result = realMain(NULL);
|
2002-06-02 18:49:35 +00:00
|
|
|
}
|
2002-06-08 21:48:00 +00:00
|
|
|
|
2002-06-21 17:55:47 +00:00
|
|
|
CNetwork::cleanup();
|
|
|
|
|
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-03 18:53:18 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
#error no main() for platform
|
|
|
|
|
2001-11-19 00:33:36 +00:00
|
|
|
#endif
|